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"
26
27#ifdef MPI_PRESENT
28#include <mpi.h>
29
32#endif
33
34template <typename T>
36{
37 public:
38 std::string m_dependent_variable_name{"Temperature"};
39 std::string m_dependent_variable_initial_state{"initial_temperature"};
40
42 SimulationClassDefault(int argc, const char* argv[]);
43 SimulationClassDefault(const std::string& user_ini);
44
45 int run() override;
46
47 ~SimulationClassDefault() override = default;
48};
49
50template <typename T>
54
55template <typename T>
60
61template <typename T>
66
67template <typename T>
69{
71 snapshot_data.overwriteSimConfig(this);
72
73 // Loading of the shape model so that all the mesh info is available at grid creation.
74 std::unique_ptr<ShapeFactory<T>> shape_creator{std::make_unique<ShapeFactory<T>>(
75 this, this->m_simulation_config.getBoolParameters("use_shape_file"),
76 this->m_simulation_config.getStringParameters("spatial_dimension"))};
77 std::shared_ptr<ShapeBase<T>> simulation_shape = shape_creator->getShapePtr();
78
79#ifdef MPI_PRESENT
81#endif
82
83 std::unique_ptr<GridFactory<T>> grid_creator{std::make_unique<GridFactory<T>>(
84 this, simulation_shape,
85 this->m_simulation_config.getStringParameters("spatial_dimension"))};
86 std::shared_ptr<GridBase<T>> simulation_grid = grid_creator->getGridPtr();
87
88#ifdef MPI_PRESENT
89 // After (potentially) updating the numerical layers, the final splitting point of larger to
90 // smaller processes can be calculated.
92 * this->m_simulation_config.getIntParameters("numerical_layers");
93#endif
94
95 this->m_field_map.insert(
96 {m_dependent_variable_name,
97 std::valarray<T>(
98 this->m_simulation_config.getDoubleParameters(m_dependent_variable_initial_state),
99 this->m_simulation_config.getIntParameters("numerical_layers")
100 * this->m_simulation_config.getIntParameters("number_of_facets"))});
101
102 std::shared_ptr<MatrixFactory<T>> matrix_factory{std::make_shared<MatrixFactory<T>>("Csr")};
103
104 std::shared_ptr<ElementManager<T>> element_manager{std::make_shared<ElementManager<T>>(
105 simulation_grid->getGridSize(), simulation_grid->getCells(), matrix_factory)};
106
107 std::shared_ptr<MatrixManager<T>> matrix_manager{
108 std::make_shared<MatrixManager<T>>(simulation_grid->getGridSize(), this, matrix_factory)};
109
111 // Load all managing modules if no list is specified or only the listed ones if it is.
112 std::vector<std::shared_ptr<GenericManagingModule<T>>> all_modules;
113 try
114 {
116 this, this->m_simulation_config.getStringVectorParameters("managing_modules"));
117 }
118 catch (const BadInput&)
119 {
121 }
122 std::vector<std::shared_ptr<GenericSubmodule<T>>> all_submodules{
124
125 snapshot_data.overwriteIniFiles(this, all_modules, all_submodules);
126
127 try
128 {
129 // If a user supplied managing module list is present, use this
130 global_chain.autoLoader(
131 all_modules, this->m_simulation_config.getStringVectorParameters("managing_modules"));
132 }
133 catch (const std::exception& e)
134 {
135 std::cerr << "[Warning] Failed to load managing modules from config: " << e.what() << "\n"
136 << "Falling back to loading all modules.\n";
137 global_chain.autoLoader(all_modules);
138 }
139 runSetup<T>(all_modules, all_submodules, global_chain.getManagingModuleNames());
140
141 snapshot_data.overwriteSimFields(this);
142
143 // Insertion point "Init"
144 this->setInitChainStr();
145 global_chain.runChain(this->m_current_position);
146
147 // this->printField("HeliocentricDistance");
148
149 // Solver reads matrix size from sim class, so has to be created after matrix size is set.
150 std::shared_ptr<SolverInterface<T>> solver{
151 std::make_shared<TridiagonalMatrixSolver<T>>(this, matrix_manager)};
152
153 // Hardcoded timesteps here, of course taken from ini in real class
154 int timesteps{static_cast<int>(this->m_simulation_config.getDoubleParameters("time_max")
155 / this->m_simulation_config.getDoubleParameters("time_delta"))};
156 int inner_iterations{this->m_simulation_config.getIntParameters("inner_iterations")};
157 T epsilon{this->m_simulation_config.getDoubleParameters("epsilon_inner_iterations")};
158 T delta_t{this->m_simulation_config.getDoubleParameters("time_delta")};
159
160 while (this->time_step * delta_t < this->m_simulation_config.getDoubleParameters("time_max"))
161 {
163 this->m_field_map[m_dependent_variable_name]};
165 this->m_field_map[m_dependent_variable_name]};
166 this->elapsed_time += delta_t;
167 this->time_step++;
168
169 // this->m_field_map["SolarVector"][0] = cos(this->time_step * 0.36 * std::numbers::pi /
170 // 180); this->m_field_map["SolarVector"][2] = sin(this->time_step * 0.36 * std::numbers::pi
171 // / 180);
172 for (int iter{0}; iter < inner_iterations; iter++)
173 {
174 // Insertion point "PreTimeStep"
175 this->setPreTimeStepChainStr();
176 global_chain.runChain(this->m_current_position);
177
179 {
180 // Change temperature field after the module run, so they use the updated
181 // temperatures!
182 this->m_field_map[m_dependent_variable_name] = dependent_variable_previous_timestep;
183 }
184
185 // Do something with the temperature field
186 element_manager->assembleFemMatrices();
187 matrix_manager->assembleGlobalLse(element_manager->m_capacitance_matrix,
188 element_manager->m_stiffness_matrix,
189 element_manager->m_forcing_vector);
190 solver->solveMatrixSystem(m_dependent_variable_name);
191
192 // Insertion point "PostNonLinIter"
193 this->setPostNonLinIterChainStr();
194 global_chain.runChain(this->m_current_position);
195
196#ifdef MPI_PRESENT
198 - this->m_field_map[m_dependent_variable_name]))
199 .max();
203 {
204 break;
205 }
206#else
208 - this->m_field_map[m_dependent_variable_name]))
209 .max()
210 < epsilon
211 || inner_iterations == 1)
212 {
213 break;
214 }
215#endif
216 if (iter < inner_iterations - 1)
217 {
219 this->m_field_map[m_dependent_variable_name];
220 }
221 else
222 {
223 std::cerr << "Reached specified inner iterations limit without fullfilling the "
224 "requested convergence criterion.\n";
225 }
226 }
227 // this->time_step++;
228 // Insertion point "PostTimeStep"
229 this->setPostTimeStepChainStr();
230 global_chain.runChain(this->m_current_position);
231
232 delta_t = this->m_simulation_config.getDoubleParameters("time_delta");
233
234 // this->printField(m_dependent_variable_name);
235 }
236
237 // Insertion point "Output"
238 this->setOutputChainStr();
239 global_chain.runChain(this->m_current_position);
240
241#ifdef MPI_PRESENT
242 if (this->world_size > 1)
243 {
244 std::valarray<T> global_temperature(
245 this->m_simulation_config.getIntParameters("numerical_layers")
246 * this->m_simulation_config.getIntParameters("global_number_of_facets"));
247 commPatternAllDataToAll(global_temperature, this->getField("Temperature"), this);
248 if (this->my_rank == 0)
249 {
250 std::cout << "Full temperature:\n";
251 for (const auto& val : global_temperature)
252 {
253 std::cout << val << " ";
254 }
255 std::cout << "\n";
256 }
257 }
258 else
259 {
260 this->printField(m_dependent_variable_name);
261 }
262#else
263 this->printField(m_dependent_variable_name);
264#endif
265
267 // this->printField("HeatConductivity");
268 // this->printField("HeatCapacity");
269 // this->printField("Density");
270
271 return 0;
272}
273
274#endif
This error class inherits from std::exception and marks faulty parameter or CL inputs....
Definition IniParser.h:71
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
static std::vector< std::shared_ptr< GenericManagingModule< T > > > createAllSelectedManagingModules(SimulationClassBase< T > *sim_pointer, const std::vector< std::string > &list_of_mananging_modules_to_load)
Only creates instances of managing modules that have been specified by the user. (This allows only ha...
Definition ModuleFactory.h:195
static std::vector< std::shared_ptr< GenericSubmodule< T > > > createAllSelectedSubmodules(SimulationClassBase< T > *sim_pointer, const std::vector< std::shared_ptr< GenericManagingModule< T > > > &loaded_managing_modules)
Only creates submodules that are used within the simulation. Iteratively requests the submodule list ...
Definition ModuleFactory.h:225
static std::vector< std::shared_ptr< GenericManagingModule< T > > > createAllManagingModules(SimulationClassBase< T > *sim_pointer)
Creates every module in the module registry.
Definition ModuleFactory.h:153
Definition SimulationClassBase.h:19
Definition SimulationClassDefault.h:36
std::string m_dependent_variable_initial_state
Definition SimulationClassDefault.h:39
SimulationClassDefault()
Definition SimulationClassDefault.h:51
int run() override
Definition SimulationClassDefault.h:68
~SimulationClassDefault() override=default
std::string m_dependent_variable_name
Definition SimulationClassDefault.h:38
Class which creates MoCSI snapshot files. Saves all the specified fields from m_save_fields and all t...
Definition SnapshotCreator.h:19