MoCSI API Reference
Loading...
Searching...
No Matches
SimulationClassDefault.h
Go to the documentation of this file.
1#ifndef SIMULATION_CLASS_DEFAULT_H
2#define SIMULATION_CLASS_DEFAULT_H
3
4#include <iostream>
5#include <map>
6#include <memory>
7#include <string>
8#include <valarray>
9#include <vector>
10
11#include "ChainManager.h"
12#include "CsvParser.h"
13#include "ElementManager.h"
14#include "GenericSolver.h"
15#include "GridFactory.h"
16#include "InputManager.h"
17#include "MatrixFactory.h"
18#include "MatrixManager.h"
19#include "ModuleFactory.h"
20#include "SetupRunner.h"
21#include "ShapeFactory.h"
22#include "SimulationClassBase.h"
23#include "SnapshotCreator.h"
24#include "SnapshotLoader.h"
27
28#ifdef MPI_PRESENT
29#include <mpi.h>
30
33#endif
34
35template <typename T>
37{
38 public:
39 std::string m_dependent_variable_name{"Temperature"};
40 std::string m_dependent_variable_initial_state{"initial_temperature"};
41
42 std::vector<std::shared_ptr<GenericManagingModule<T>>> m_all_modules{};
43 std::vector<std::shared_ptr<GenericSubmodule<T>>> m_all_submodules{};
44
46 SimulationClassDefault(int argc, const char* argv[]);
47 SimulationClassDefault(const std::string& user_ini);
48
49 int run() override;
50
51 ~SimulationClassDefault() override = default;
52};
53
54template <typename T>
58
59template <typename T>
64
65template <typename T>
70
71template <typename T>
73{
75 snapshot_data.overwriteSimConfig(this);
76
77 // Loading of the shape model so that all the mesh info is available at grid creation.
78 std::unique_ptr<ShapeFactory<T>> shape_creator{std::make_unique<ShapeFactory<T>>(
79 this, this->m_simulation_config.getBoolParameters("use_shape_file"),
80 this->m_simulation_config.getStringParameters("spatial_dimension"))};
81 std::shared_ptr<ShapeBase<T>> simulation_shape = shape_creator->getShapePtr();
82
83#ifdef MPI_PRESENT
85#endif
86
87 std::unique_ptr<GridFactory<T>> grid_creator{std::make_unique<GridFactory<T>>(
88 this, simulation_shape,
89 this->m_simulation_config.getStringParameters("spatial_dimension"))};
90 std::shared_ptr<GridBase<T>> simulation_grid = grid_creator->getGridPtr();
91
92#ifdef MPI_PRESENT
93 // After (potentially) updating the numerical layers, the final splitting point of larger to
94 // smaller processes can be calculated.
96 * this->m_simulation_config.getIntParameters("numerical_layers");
97#endif
98
99 this->m_field_map.insert(
100 {m_dependent_variable_name,
101 std::valarray<T>(
102 this->m_simulation_config.getDoubleParameters(m_dependent_variable_initial_state),
103 this->m_simulation_config.getIntParameters("numerical_layers")
104 * this->m_simulation_config.getIntParameters("number_of_facets"))});
105
106 std::shared_ptr<MatrixFactory<T>> matrix_factory{std::make_shared<MatrixFactory<T>>("Csr")};
107
108 std::shared_ptr<ElementManager<T>> element_manager{std::make_shared<ElementManager<T>>(
109 simulation_grid->getGridSize(), simulation_grid->getCells(), matrix_factory)};
110
111 std::shared_ptr<MatrixManager<T>> matrix_manager{
112 std::make_shared<MatrixManager<T>>(simulation_grid->getGridSize(), this, matrix_factory)};
113
115
116 // Create the modules and allow static access
120
121 snapshot_data.overwriteIniFiles(this, m_all_modules, m_all_submodules);
122
123 try
124 {
125 // If a user supplied managing module list is present, use this
126 global_chain.autoLoader(
127 m_all_modules, this->m_simulation_config.getStringVectorParameters("managing_modules"));
128 }
129 catch (const std::exception& e)
130 {
131 std::cerr << "[Warning] Failed to load managing modules from config: " << e.what() << "\n"
132 << "Falling back to loading all modules.\n";
133 global_chain.autoLoader(m_all_modules);
134 }
135 runSetup<T>(m_all_modules, m_all_submodules, global_chain.getManagingModuleNames());
136
137 snapshot_data.overwriteSimFields(this);
138
139 // Insertion point "Init"
140 this->setInitChainStr();
141 global_chain.runChain(this->m_current_position);
142
143 // this->printField("HeliocentricDistance");
144
145 // Solver reads matrix size from sim class, so has to be created after matrix size is set.
146 std::shared_ptr<SolverInterface<T>> solver{
147 std::make_shared<TridiagonalMatrixSolver<T>>(this, matrix_manager)};
148
149 // Hardcoded timesteps here, of course taken from ini in real class
150 int timesteps{static_cast<int>(this->m_simulation_config.getDoubleParameters("time_max")
151 / this->m_simulation_config.getDoubleParameters("time_delta"))};
152 int inner_iterations{this->m_simulation_config.getIntParameters("inner_iterations")};
153 T epsilon{this->m_simulation_config.getDoubleParameters("epsilon_inner_iterations")};
154 T delta_t{this->m_simulation_config.getDoubleParameters("time_delta")};
155
156 while (this->time_step * delta_t < this->m_simulation_config.getDoubleParameters("time_max"))
157 {
159 this->m_field_map[m_dependent_variable_name]};
161 this->m_field_map[m_dependent_variable_name]};
162 this->elapsed_time += delta_t;
163 this->time_step++;
164
165 // this->m_field_map["SolarVector"][0] = cos(this->time_step * 0.36 * std::numbers::pi /
166 // 180); this->m_field_map["SolarVector"][2] = sin(this->time_step * 0.36 * std::numbers::pi
167 // / 180);
168 for (int iter{0}; iter < inner_iterations; iter++)
169 {
170 // Insertion point "PreTimeStep"
171 this->setPreTimeStepChainStr();
172 global_chain.runChain(this->m_current_position);
173
175 {
176 // Change temperature field after the module run, so they use the updated
177 // temperatures!
178 this->m_field_map[m_dependent_variable_name] = dependent_variable_previous_timestep;
179 }
180
181 // Do something with the temperature field
182 element_manager->assembleFemMatrices();
183 matrix_manager->assembleGlobalLse(element_manager->m_capacitance_matrix,
184 element_manager->m_stiffness_matrix,
185 element_manager->m_forcing_vector);
186 solver->solveMatrixSystem(m_dependent_variable_name);
187
188 // Insertion point "PostNonLinIter"
189 this->setPostNonLinIterChainStr();
190 global_chain.runChain(this->m_current_position);
191
192#ifdef MPI_PRESENT
194 - this->m_field_map[m_dependent_variable_name]))
195 .max();
199 {
200 break;
201 }
202#else
204 - this->m_field_map[m_dependent_variable_name]))
205 .max()
206 < epsilon
207 || inner_iterations == 1)
208 {
209 break;
210 }
211#endif
212 if (iter < inner_iterations - 1)
213 {
215 this->m_field_map[m_dependent_variable_name];
216 }
217 else
218 {
219 std::cerr << "Reached specified inner iterations limit without fullfilling the "
220 "requested convergence criterion.\n";
221 }
222 }
223 // this->time_step++;
224 // Insertion point "PostTimeStep"
225 this->setPostTimeStepChainStr();
226 global_chain.runChain(this->m_current_position);
227
228 delta_t = this->m_simulation_config.getDoubleParameters("time_delta");
229
230 // this->printField(m_dependent_variable_name);
231 }
232
233 // Insertion point "Output"
234 this->setOutputChainStr();
235 global_chain.runChain(this->m_current_position);
236
237#ifdef MPI_PRESENT
238 if (this->world_size > 1)
239 {
240 std::valarray<T> global_temperature(
241 this->m_simulation_config.getIntParameters("numerical_layers")
242 * this->m_simulation_config.getIntParameters("global_number_of_facets"));
243 commPatternAllDataToAll(global_temperature, this->getField("Temperature"), this);
244 if (this->my_rank == 0)
245 {
246 std::cout << "Full temperature:\n";
247 for (const auto& val : global_temperature)
248 {
249 std::cout << val << " ";
250 }
251 std::cout << "\n";
252 }
253 }
254 else
255 {
256 this->printField(m_dependent_variable_name);
257 }
258#else
259 this->printField(m_dependent_variable_name);
260#endif
261
262 SnapshotCreator{this, m_all_modules, m_all_submodules};
263 // this->printField("HeatConductivity");
264 // this->printField("HeatCapacity");
265 // this->printField("Density");
266
267 return 0;
268}
269
270#endif
Concrete implementation of a matrix class representing a Compressed Sparse Rows (CSR) matrix....
Definition CsrMatrix.h:35
CsrSparseMatrix()
Constructor for an empty CsrSparseMatrix object. Leaves all storage arrays empty but sets the flag to...
Definition CsrMatrix.h:92
Definition SimulationClassBase.h:19
Definition SimulationClassDefault.h:37
std::vector< std::shared_ptr< GenericSubmodule< T > > > m_all_submodules
Definition SimulationClassDefault.h:43
std::string m_dependent_variable_initial_state
Definition SimulationClassDefault.h:40
SimulationClassDefault()
Definition SimulationClassDefault.h:55
std::vector< std::shared_ptr< GenericManagingModule< T > > > m_all_modules
Definition SimulationClassDefault.h:42
int run() override
Definition SimulationClassDefault.h:72
~SimulationClassDefault() override=default
std::string m_dependent_variable_name
Definition SimulationClassDefault.h:39
Class which creates MoCSI snapshot files. Saves all the specified fields from m_save_fields and all t...
Definition SnapshotCreator.h:19
static std::vector< std::shared_ptr< GenericSubmodule< T > > > & getAllSelectedSubmodules()
Get all selected submodules (static).
Definition StaticModuleContainer.h:82
static std::vector< std::shared_ptr< GenericManagingModule< T > > > & getAllSelectedManagingModules()
Get all selected managing modules (static).
Definition StaticModuleContainer.h:72