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 std::vector<std::shared_ptr<GenericManagingModule<T>>> all_modules{
113 std::vector<std::shared_ptr<GenericSubmodule<T>>> all_submodules{
115
116 snapshot_data.overwriteIniFiles(this, all_modules, all_submodules);
117
118 try
119 {
120 // If a user supplied managing module list is present, use this
121 global_chain.autoLoader(
122 all_modules, this->m_simulation_config.getStringVectorParameters("managing_modules"));
123 }
124 catch (const std::exception& e)
125 {
126 std::cerr << "[Warning] Failed to load managing modules from config: " << e.what() << "\n"
127 << "Falling back to loading all modules.\n";
128 global_chain.autoLoader(all_modules);
129 }
130 runSetup<T>(all_modules, all_submodules, global_chain.getManagingModuleNames());
131
132 snapshot_data.overwriteSimFields(this);
133
134 // Insertion point "Init"
135 this->setInitChainStr();
136 global_chain.runChain(this->m_current_position);
137
138 // this->printField("HeliocentricDistance");
139
140 // Solver reads matrix size from sim class, so has to be created after matrix size is set.
141 std::shared_ptr<SolverInterface<T>> solver{
142 std::make_shared<TridiagonalMatrixSolver<T>>(this, matrix_manager)};
143
144 // Hardcoded timesteps here, of course taken from ini in real class
145 int timesteps{static_cast<int>(this->m_simulation_config.getDoubleParameters("time_max")
146 / this->m_simulation_config.getDoubleParameters("time_delta"))};
147 int inner_iterations{this->m_simulation_config.getIntParameters("inner_iterations")};
148 T epsilon{this->m_simulation_config.getDoubleParameters("epsilon_inner_iterations")};
149 T delta_t{this->m_simulation_config.getDoubleParameters("time_delta")};
150
151 while (this->time_step * delta_t < this->m_simulation_config.getDoubleParameters("time_max"))
152 {
154 this->m_field_map[m_dependent_variable_name]};
156 this->m_field_map[m_dependent_variable_name]};
157 this->elapsed_time += delta_t;
158 this->time_step++;
159
160 // this->m_field_map["SolarVector"][0] = cos(this->time_step * 0.36 * std::numbers::pi /
161 // 180); this->m_field_map["SolarVector"][2] = sin(this->time_step * 0.36 * std::numbers::pi
162 // / 180);
163 for (int iter{0}; iter < inner_iterations; iter++)
164 {
165 // Insertion point "PreTimeStep"
166 this->setPreTimeStepChainStr();
167 global_chain.runChain(this->m_current_position);
168
170 {
171 // Change temperature field after the module run, so they use the updated
172 // temperatures!
173 this->m_field_map[m_dependent_variable_name] = dependent_variable_previous_timestep;
174 }
175
176 // Do something with the temperature field
177 element_manager->assembleFemMatrices();
178 matrix_manager->assembleGlobalLse(element_manager->m_capacitance_matrix,
179 element_manager->m_stiffness_matrix,
180 element_manager->m_forcing_vector);
181 solver->solveMatrixSystem(m_dependent_variable_name);
182
183 // Insertion point "PostNonLinIter"
184 this->setPostNonLinIterChainStr();
185 global_chain.runChain(this->m_current_position);
186
187#ifdef MPI_PRESENT
189 - this->m_field_map[m_dependent_variable_name]))
190 .max();
194 {
195 break;
196 }
197#else
199 - this->m_field_map[m_dependent_variable_name]))
200 .max()
201 < epsilon
202 || inner_iterations == 1)
203 {
204 break;
205 }
206#endif
207 if (iter < inner_iterations - 1)
208 {
210 this->m_field_map[m_dependent_variable_name];
211 }
212 else
213 {
214 std::cerr << "Reached specified inner iterations limit without fullfilling the "
215 "requested convergence criterion.\n";
216 }
217 }
218 // this->time_step++;
219 // Insertion point "PostTimeStep"
220 this->setPostTimeStepChainStr();
221 global_chain.runChain(this->m_current_position);
222
223 delta_t = this->m_simulation_config.getDoubleParameters("time_delta");
224
225 // this->printField(m_dependent_variable_name);
226 }
227
228 // Insertion point "Output"
229 this->setOutputChainStr();
230 global_chain.runChain(this->m_current_position);
231
232#ifdef MPI_PRESENT
233 if (this->world_size > 1)
234 {
235 std::valarray<T> global_temperature(
236 this->m_simulation_config.getIntParameters("numerical_layers")
237 * this->m_simulation_config.getIntParameters("global_number_of_facets"));
238 commPatternAllDataToAll(global_temperature, this->getField("Temperature"), this);
239 if (this->my_rank == 0)
240 {
241 std::cout << "Full temperature:\n";
242 for (const auto& val : global_temperature)
243 {
244 std::cout << val << " ";
245 }
246 std::cout << "\n";
247 }
248 }
249 else
250 {
251 this->printField(m_dependent_variable_name);
252 }
253#else
254 this->printField(m_dependent_variable_name);
255#endif
256
258 // this->printField("HeatConductivity");
259 // this->printField("HeatCapacity");
260 // this->printField("Density");
261
262 return 0;
263}
264
265#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
static std::vector< std::shared_ptr< GenericSubmodule< T > > > createAllSubmodules(SimulationClassBase< T > *sim_pointer)
Creates every submodule in the submodule registry.
Definition ModuleFactory.h:165
static std::vector< std::shared_ptr< GenericManagingModule< T > > > createAllManagingModules(SimulationClassBase< T > *sim_pointer)
Creates every module in the module registry.
Definition ModuleFactory.h:146
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