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
27template <typename T>
29{
30 public:
31 std::string m_dependent_variable_name{"Temperature"};
32 std::string m_dependent_variable_initial_state{"initial_temperature"};
33
35 SimulationClassDefault(int argc, const char* argv[]);
36 SimulationClassDefault(const std::string& user_ini);
37
38 int run() override;
39
40 ~SimulationClassDefault() override = default;
41
42 private:
43 std::string m_init_str{"InitChain"};
44 std::string m_preTs_str{"PreTimeStepChain"};
45 std::string m_postNonLinIter_str{"PostNonLinIterChain"};
46 std::string m_postTs_str{"PostTimeStepChain"};
47 std::string m_output_str{"OutputChain"};
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 std::unique_ptr<GridFactory<T>> grid_creator{std::make_unique<GridFactory<T>>(
79 this, simulation_shape,
80 this->m_simulation_config.getStringParameters("spatial_dimension"))};
81 std::shared_ptr<GridBase<T>> simulation_grid = grid_creator->getGridPtr();
82
83 this->m_field_map.insert(
84 {m_dependent_variable_name,
85 std::valarray<double>(
86 this->m_simulation_config.getDoubleParameters(m_dependent_variable_initial_state),
87 this->m_simulation_config.getIntParameters("numerical_layers")
88 * this->m_simulation_config.getIntParameters("number_of_facets"))});
89
90 std::shared_ptr<MatrixFactory<T>> matrix_factory{std::make_shared<MatrixFactory<T>>("Csr")};
91
92 std::shared_ptr<ElementManager<T>> element_manager{std::make_shared<ElementManager<T>>(
93 simulation_grid->getGridSize(), simulation_grid->getCells(), matrix_factory)};
94
95 std::shared_ptr<MatrixManager<T>> matrix_manager{
96 std::make_shared<MatrixManager<T>>(simulation_grid->getGridSize(), this, matrix_factory)};
97
99 std::vector<std::shared_ptr<GenericManagingModule<T>>> all_modules{
101 std::vector<std::shared_ptr<GenericSubmodule<T>>> all_submodules{
103
104 snapshot_data.overwriteIniFiles(this, all_modules, all_submodules);
105
106 try
107 {
108 // If a user supplied managing module list is present, use this
109 global_chain.autoLoader(
110 all_modules, this->m_simulation_config.getStringVectorParameters("managing_modules"));
111 }
112 catch (const std::exception& e)
113 {
114 std::cerr << "[Warning] Failed to load managing modules from config: " << e.what() << "\n"
115 << "Falling back to loading all modules.\n";
116 global_chain.autoLoader(all_modules);
117 }
118 runSetup<T>(all_modules, all_submodules, global_chain.getManagingModuleNames());
119
120 snapshot_data.overwriteSimFields(this);
121
122 // Insertion point "Init"
123 this->setInitChainStr();
124 global_chain.runChain(this->m_current_position);
125
126 // this->printField("HeliocentricDistance");
127
128 // Solver reads matrix size from sim class, so has to be created after matrix size is set.
129 std::shared_ptr<SolverInterface<T>> solver{
130 std::make_shared<TridiagonalMatrixSolver<T>>(this, matrix_manager)};
131
132 // Hardcoded timesteps here, of course taken from ini in real class
133 int timesteps{static_cast<int>(this->m_simulation_config.getDoubleParameters("time_max")
134 / this->m_simulation_config.getDoubleParameters("time_delta"))};
135 int inner_iterations{this->m_simulation_config.getIntParameters("inner_iterations")};
136 T epsilon{this->m_simulation_config.getDoubleParameters("epsilon_inner_iterations")};
137 T delta_t{this->m_simulation_config.getDoubleParameters("time_delta")};
138
139 while (this->time_step * delta_t < this->m_simulation_config.getDoubleParameters("time_max"))
140 {
142 this->m_field_map[m_dependent_variable_name]};
144 this->m_field_map[m_dependent_variable_name]};
145 this->elapsed_time += delta_t;
146 this->time_step++;
147
148 // this->m_field_map["SolarVector"][0] = cos(this->time_step * 0.36 * std::numbers::pi /
149 // 180); this->m_field_map["SolarVector"][2] = sin(this->time_step * 0.36 * std::numbers::pi
150 // / 180);
151 for (int iter{0}; iter < inner_iterations; iter++)
152 {
153 // Insertion point "PreTimeStep"
154 this->setPreTimeStepChainStr();
155 global_chain.runChain(this->m_current_position);
156
158 {
159 // Change temperature field after the module run, so they use the updated
160 // temperatures!
161 this->m_field_map[m_dependent_variable_name] = dependent_variable_previous_timestep;
162 }
163 /*this->printField("HeatConductivity");
164 this->printField("HeatCapacity");
165 this->printField("Density");
166 this->printField("QSol");
167 this->print_parameter("HeliocentricDistance");
168 this->printField("SolarVector");*/
169
170 // Do something with the temperature field
171 element_manager->assembleFemMatrices();
172 matrix_manager->assembleGlobalLse(element_manager->m_capacitance_matrix,
173 element_manager->m_stiffness_matrix,
174 element_manager->m_forcing_vector);
175 solver->solveMatrixSystem(m_dependent_variable_name);
176
177 // Insertion point "PostNonLinIter"
178 this->setPostNonLinIterChainStr();
179 global_chain.runChain(this->m_current_position);
180
182 - this->m_field_map[m_dependent_variable_name]))
183 .max()
184 < epsilon
185 || inner_iterations == 1)
186 {
187 break;
188 }
189 if (iter < inner_iterations - 1)
190 {
192 this->m_field_map[m_dependent_variable_name];
193 }
194 else
195 {
196 std::cerr << "Reached specified inner iterations limit without fullfilling the "
197 "requested convergence criterion.\n";
198 }
199 }
200 // this->time_step++;
201 // Insertion point "PostTimeStep"
202 this->setPostTimeStepChainStr();
203 global_chain.runChain(this->m_current_position);
204
205 delta_t = this->m_simulation_config.getDoubleParameters("time_delta");
206
207 // this->printField(m_dependent_variable_name);
208 }
209
210 // Insertion point "Output"
211 this->setOutputChainStr();
212 global_chain.runChain(this->m_current_position);
213
214 this->printField(m_dependent_variable_name);
215
217 // this->printField("HeatConductivity");
218 // this->printField("HeatCapacity");
219 // this->printField("Density");
220
221 return 0;
222}
223
224#endif
Error class to throw when a module chain has circular or missing dependencies. Inherits from std::exc...
Definition ChainManager.h:28
const char * what() const noexcept override
Supplies the error message as a c-style string/char array.
Definition ChainManager.h:57
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:15
Definition SimulationClassDefault.h:29
std::string m_dependent_variable_initial_state
Definition SimulationClassDefault.h:32
SimulationClassDefault()
Definition SimulationClassDefault.h:51
int run() override
Definition SimulationClassDefault.h:68
~SimulationClassDefault() override=default
std::string m_dependent_variable_name
Definition SimulationClassDefault.h:31
Class which creates MoCSI snapshot files. Saves all the specified fields from m_save_fields and all t...
Definition SnapshotCreator.h:19