MoCSI API Reference
Loading...
Searching...
No Matches
SimulationClassUnitTesting.h
Go to the documentation of this file.
1#ifndef SIMULATION_CLASS_UNIT_TESTING_H
2#define SIMULATION_CLASS_UNIT_TESTING_H
3
4#include <filesystem>
5#include <fstream>
6#include <iostream>
7#include <map>
8#include <memory>
9#include <string>
10#include <valarray>
11#include <vector>
12
13#include "ChainManager.h"
14#include "GridFactory.h"
15#include "ModuleFactory.h"
16#include "SetupRunner.h"
17#include "ShapeFactory.h"
18#include "SimulationClassBase.h"
19
20template <typename T>
22{
23 public:
25 SimulationClassUnitTesting(const std::string& user_ini);
26
28 void setUserIni(const std::string& user_ini) override;
29
30 int run() override;
31 int runInitChain();
32 bool runModuleCheck(const std::vector<std::string>& managing_modules,
33 const std::vector<std::string>& submodules);
34
35 ~SimulationClassUnitTesting() override = default;
36
37 private:
38 std::string m_init_str{"InitChain"};
39 std::string m_preTs_str{"PreTimeStepChain"};
40 std::string m_postTs_str{"PostTimeStepChain"};
41 std::string m_postNonLinIter_str{"PostNonLinIterChain"};
42 std::string m_output_str{"OutputChain"};
43 ChainManager<GenericManagingModule<double>> m_global_chain{"global"};
44
45 std::vector<std::shared_ptr<GenericManagingModule<T>>> m_all_modules{};
46 std::vector<std::shared_ptr<GenericSubmodule<T>>> m_all_submodules{};
47};
48
49template <typename T>
51{
52 // Set absolute paths and load ini file
53 std::filesystem::path ini_folder_path =
54 (std::filesystem::path(PROJECT_ROOT) / "build/tests/ini_files/");
55 std::string default_ini_path =
56 std::filesystem::absolute(ini_folder_path / "default.ini").string();
57
58 std::vector<std::string> lines;
59 std::ifstream infile(default_ini_path);
60 std::string line;
61
62 while (std::getline(infile, line))
63 {
64 if (line.rfind("ini_folder_path =", 0) == 0)
65 {
66 line = "ini_folder_path = \"" + ini_folder_path.string() + "\"";
67 }
68 lines.push_back(line);
69 }
70 infile.close();
71
72 std::ofstream outfile(default_ini_path, std::ios::trunc);
73 for (const auto& l : lines)
74 {
75 outfile << l << "\n";
76 }
77 outfile.close();
78
79 std::cout << "PATH: " << default_ini_path << "\n";
80
81 this->m_simulation_config = InputManager(default_ini_path);
82}
83
84template <typename T>
86{
87 this->m_simulation_config.loadUserInput(
88 this->m_simulation_config.getStringParameters("ini_folder_path") + user_ini);
89}
90
91template <typename T>
93{
94 this->setAbsoluteDefaultIniPath();
95}
96
97template <typename T>
100{
101 // Do not use when working with absolute paths since base constructor will always run first
102}
103
104template <typename T>
106{
107 // Loading of the shape model so that all the mesh info is available at grid creation.
108 std::unique_ptr<ShapeFactory<T>> shape_creator;
109 std::shared_ptr<ShapeBase<T>> simulation_shape;
110 try
111 {
112 shape_creator = std::make_unique<ShapeFactory<T>>(
113 this, this->m_simulation_config.getBoolParameters("use_shape_file"),
114 this->m_simulation_config.getStringParameters("spatial_dimension"));
115 simulation_shape = shape_creator->getShapePtr();
116 }
117 catch (const BadInput& e)
118 {
119 // Map access errors.
120 std::cerr
121 << "[SimulationClassUnitTesting]: Failed creating ShapeBase object from ShapeFactory "
122 << e.what() << '\n';
123 throw BadInput(
124 static_cast<std::string>(
125 "[SimulationClassUnitTesting]: Failed creating ShapeBase object from ShapeFactory ")
126 + static_cast<std::string>(e.what()));
127 }
128 catch (const std::runtime_error& e)
129 {
130 // ShapeFactory internal errors.
131 std::cerr
132 << "[SimulationClassUnitTesting]: Failed creating ShapeBase object from ShapeFactory "
133 << e.what() << '\n';
134 throw std::runtime_error(
135 static_cast<std::string>(
136 "[SimulationClassUnitTesting]: Failed creating ShapeBase object from ShapeFactory ")
137 + static_cast<std::string>(e.what()));
138 }
139
140 std::unique_ptr<GridFactory<T>> grid_creator;
141 std::shared_ptr<GridBase<T>> simulation_grid;
142 try
143 {
144 grid_creator = std::make_unique<GridFactory<T>>(
145 this, simulation_shape,
146 this->m_simulation_config.getStringParameters("spatial_dimension"));
147 simulation_grid = grid_creator->getGridPtr();
148 }
149 catch (const BadInput& e)
150 {
151 // Map access errors or GridBase internal errors.
152 std::cerr
153 << "[SimulationClassUnitTesting]: Failed creating GridBase object from GridFactory "
154 << e.what() << '\n';
155 throw BadInput(
156 static_cast<std::string>(
157 "[SimulationClassUnitTesting]: Failed creating GridBase object from GridFactory ")
158 + static_cast<std::string>(e.what()));
159 }
160 catch (const std::runtime_error& e)
161 {
162 // GridFactory internal errors.
163 std::cerr
164 << "[SimulationClassUnitTesting]: Failed creating GridBase object from GridFactory "
165 << e.what() << '\n';
166 throw std::runtime_error(
167 static_cast<std::string>(
168 "[SimulationClassUnitTesting]: Failed creating GridBase object from GridFactory ")
169 + static_cast<std::string>(e.what()));
170 }
171
172 try
173 {
174 this->m_field_map.insert(
175 {"Temperature",
176 std::valarray<double>(
177 this->m_simulation_config.getDoubleParameters("initial_temperature"),
178 this->m_simulation_config.getIntParameters("numerical_layers")
179 * this->m_simulation_config.getIntParameters("number_of_facets"))});
180 }
181 catch (const BadInput& e)
182 {
183 std::cerr
184 << "[SimulationClassUnitTesting]: Failed to create main independent variable field "
185 "(usually temperature) "
186 << e.what() << '\n';
187 throw BadInput(
188 static_cast<std::string>("[SimulationClassUnitTesting]: Failed to create main "
189 "independent variable field (usually temperature) ")
190 + static_cast<std::string>(e.what()));
191 }
192
193 try
194 {
196 this, this->m_simulation_config.getStringVectorParameters("managing_modules"));
197 }
198 catch (const BadInput&)
199 {
200 try
201 {
202 m_all_modules = ModuleFactory<T>::createAllManagingModules(this);
203 }
204 catch (const std::runtime_error& e)
205 {
206 std::cerr << "[SimulationClassUnitTesting]: Failed at module creation step " << e.what()
207 << '\n';
208 throw std::runtime_error(
209 static_cast<std::string>(
210 "[SimulationClassUnitTesting]: Failed at module creation step ")
211 + static_cast<std::string>(e.what()));
212 }
213 }
214 catch (const std::runtime_error& e)
215 {
216 std::cerr << "[SimulationClassUnitTesting]: Failed at module creation step " << e.what()
217 << '\n';
218 throw std::runtime_error(
219 static_cast<std::string>(
220 "[SimulationClassUnitTesting]: Failed at module creation step ")
221 + static_cast<std::string>(e.what()));
222 }
223
224 try
225 {
226 m_all_submodules = ModuleFactory<T>::createAllSelectedSubmodules(this, m_all_modules);
227 }
228 catch (const std::runtime_error& e)
229 {
230 std::cerr << "[SimulationClassUnitTesting]: Failed at submodule creation step " << e.what()
231 << '\n';
232 throw std::runtime_error(
233 static_cast<std::string>(
234 "[SimulationClassUnitTesting]: Failed at submodule creation step ")
235 + static_cast<std::string>(e.what()));
236 }
237
238 try
239 {
240 // If a user supplied managing module list is present, use this
241 m_global_chain.autoLoader(
242 m_all_modules, this->m_simulation_config.getStringVectorParameters("managing_modules"));
243 }
244 catch (const std::exception& e)
245 {
246 std::cerr << "[SimulationClassUnitTesting]: Warning - Failed to load managing modules from "
247 "config: "
248 << e.what() << "\n"
249 << "Falling back to loading all modules.\n";
250 m_global_chain.autoLoader(m_all_modules);
251 }
252
253 try
254 {
255 runSetup<T>(m_all_modules, m_all_submodules, m_global_chain.getManagingModuleNames());
256 }
257 catch (const BadInput& e)
258 {
259 // runSetup itself does not throw an error but has a high chance to trigger a BadInput
260 // within the setup functions of the modules.
261 std::cerr << "[SimulationClassDefault]: Failed at setting up the modules " << e.what()
262 << '\n';
263 throw std::runtime_error(
264 static_cast<std::string>("[SimulationClassDefault]: Failed at setting up the modules ")
265 + static_cast<std::string>(e.what()));
266 }
267 return 0;
268}
269
270template <typename T>
272 const std::vector<std::string>& submodules)
273{
274 // Loading of the shape model so that all the mesh info is available at grid creation.
275 std::unique_ptr<ShapeFactory<T>> shape_creator;
276 std::shared_ptr<ShapeBase<T>> simulation_shape;
277 try
278 {
279 shape_creator = std::make_unique<ShapeFactory<T>>(
280 this, this->m_simulation_config.getBoolParameters("use_shape_file"),
281 this->m_simulation_config.getStringParameters("spatial_dimension"));
282 simulation_shape = shape_creator->getShapePtr();
283 }
284 catch (const BadInput& e)
285 {
286 // Map access errors.
287 std::cerr
288 << "[SimulationClassUnitTesting]: Failed creating ShapeBase object from ShapeFactory "
289 << e.what() << '\n';
290 throw BadInput(
291 static_cast<std::string>(
292 "[SimulationClassUnitTesting]: Failed creating ShapeBase object from ShapeFactory ")
293 + static_cast<std::string>(e.what()));
294 }
295 catch (const std::runtime_error& e)
296 {
297 // ShapeFactory internal errors.
298 std::cerr
299 << "[SimulationClassUnitTesting]: Failed creating ShapeBase object from ShapeFactory "
300 << e.what() << '\n';
301 throw std::runtime_error(
302 static_cast<std::string>(
303 "[SimulationClassUnitTesting]: Failed creating ShapeBase object from ShapeFactory ")
304 + static_cast<std::string>(e.what()));
305 }
306
307 std::unique_ptr<GridFactory<T>> grid_creator;
308 std::shared_ptr<GridBase<T>> simulation_grid;
309 try
310 {
311 grid_creator = std::make_unique<GridFactory<T>>(
312 this, simulation_shape,
313 this->m_simulation_config.getStringParameters("spatial_dimension"));
314 simulation_grid = grid_creator->getGridPtr();
315 }
316 catch (const BadInput& e)
317 {
318 // Map access errors or GridBase internal errors.
319 std::cerr
320 << "[SimulationClassUnitTesting]: Failed creating GridBase object from GridFactory "
321 << e.what() << '\n';
322 throw BadInput(
323 static_cast<std::string>(
324 "[SimulationClassUnitTesting]: Failed creating GridBase object from GridFactory ")
325 + static_cast<std::string>(e.what()));
326 }
327 catch (const std::runtime_error& e)
328 {
329 // GridFactory internal errors.
330 std::cerr
331 << "[SimulationClassUnitTesting]: Failed creating GridBase object from GridFactory "
332 << e.what() << '\n';
333 throw std::runtime_error(
334 static_cast<std::string>(
335 "[SimulationClassUnitTesting]: Failed creating GridBase object from GridFactory ")
336 + static_cast<std::string>(e.what()));
337 }
338
339 try
340 {
341 this->m_field_map.insert(
342 {"Temperature",
343 std::valarray<double>(
344 this->m_simulation_config.getDoubleParameters("initial_temperature"),
345 this->m_simulation_config.getIntParameters("numerical_layers")
346 * this->m_simulation_config.getIntParameters("number_of_facets"))});
347 }
348 catch (const BadInput& e)
349 {
350 std::cerr
351 << "[SimulationClassUnitTesting]: Failed to create main independent variable field "
352 "(usually temperature) "
353 << e.what() << '\n';
354 throw BadInput(
355 static_cast<std::string>("[SimulationClassUnitTesting]: Failed to create main "
356 "independent variable field (usually temperature) ")
357 + static_cast<std::string>(e.what()));
358 }
359
360 try
361 {
363 this, this->m_simulation_config.getStringVectorParameters("managing_modules"));
364 }
365 catch (const BadInput&)
366 {
367 try
368 {
369 m_all_modules = ModuleFactory<T>::createAllManagingModules(this);
370 }
371 catch (const std::runtime_error& e)
372 {
373 std::cerr << "[SimulationClassUnitTesting]: Failed at module creation step " << e.what()
374 << '\n';
375 throw std::runtime_error(
376 static_cast<std::string>(
377 "[SimulationClassUnitTesting]: Failed at module creation step ")
378 + static_cast<std::string>(e.what()));
379 }
380 }
381 catch (const std::runtime_error& e)
382 {
383 std::cerr << "[SimulationClassUnitTesting]: Failed at module creation step " << e.what()
384 << '\n';
385 throw std::runtime_error(
386 static_cast<std::string>(
387 "[SimulationClassUnitTesting]: Failed at module creation step ")
388 + static_cast<std::string>(e.what()));
389 }
390
391 try
392 {
393 m_all_submodules = ModuleFactory<T>::createAllSelectedSubmodules(this, m_all_modules);
394 }
395 catch (const std::runtime_error& e)
396 {
397 std::cerr << "[SimulationClassUnitTesting]: Failed at submodule creation step " << e.what()
398 << '\n';
399 throw std::runtime_error(
400 static_cast<std::string>(
401 "[SimulationClassUnitTesting]: Failed at submodule creation step ")
402 + static_cast<std::string>(e.what()));
403 }
404
405 try
406 {
407 // If a user supplied managing module list is present, use this
408 m_global_chain.autoLoader(
409 m_all_modules, this->m_simulation_config.getStringVectorParameters("managing_modules"));
410 }
411 catch (const std::exception& e)
412 {
413 std::cerr << "[SimulationClassUnitTesting]: Warning - Failed to load managing modules from "
414 "config: "
415 << e.what() << "\n"
416 << "Falling back to loading all modules.\n";
417 m_global_chain.autoLoader(m_all_modules);
418 }
419
420 try
421 {
422 runSetup<T>(m_all_modules, m_all_submodules, m_global_chain.getManagingModuleNames());
423 }
424 catch (const BadInput& e)
425 {
426 // runSetup itself does not throw an error but has a high chance to trigger a BadInput
427 // within the setup functions of the modules.
428 std::cerr << "[SimulationClassDefault]: Failed at setting up the modules " << e.what()
429 << '\n';
430 throw std::runtime_error(
431 static_cast<std::string>("[SimulationClassDefault]: Failed at setting up the modules ")
432 + static_cast<std::string>(e.what()));
433 }
434
435 bool result{true};
436 std::unordered_set<std::string> set_mm(managing_modules.begin(), managing_modules.end());
437 for (const auto& module_pointer : m_all_modules)
438 {
439 std::string module_name{module_pointer->getNameLocal()};
440 if (!(set_mm.contains(module_name)))
441 {
442 result = false;
443 }
444 }
445 std::unordered_set<std::string> set_sm(submodules.begin(), submodules.end());
446 for (const auto& submodule_pointer : m_all_submodules)
447 {
448 std::string submodule_name{submodule_pointer->getNameLocal()};
449 if (!(set_sm.contains(submodule_name)))
450 {
451 result = false;
452 }
453 }
454 if ((m_all_modules.size() != managing_modules.size())
455 || (m_all_submodules.size() != submodules.size()))
456 {
457 result = false;
458 }
459 return result;
460}
461
462template <typename T>
464{
465 this->setInitChainStr();
466 m_global_chain.runChain(this->m_current_position);
467 return 0;
468}
469#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:37
CsrSparseMatrix()
Constructor for an empty CsrSparseMatrix object. Leaves all storage arrays empty but sets the flag to...
Definition CsrMatrix.h:94
std::pair< int, int > size() const
Function that returns the size of the matrix as a pair in (rows, columns) format.
Definition GenericMatrix.h:120
This class that manages parameter input through the default ini and user supplied ini files + CLI....
Definition InputManager.h:26
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:199
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:228
static std::vector< std::shared_ptr< GenericManagingModule< T > > > createAllManagingModules(SimulationClassBase< T > *sim_pointer)
Creates every module in the module registry.
Definition ModuleFactory.h:157
Definition SimulationClassBase.h:19
Definition SimulationClassUnitTesting.h:22
bool runModuleCheck(const std::vector< std::string > &managing_modules, const std::vector< std::string > &submodules)
Definition SimulationClassUnitTesting.h:271
SimulationClassUnitTesting()
Definition SimulationClassUnitTesting.h:92
int runInitChain()
Definition SimulationClassUnitTesting.h:463
void setAbsoluteDefaultIniPath()
Definition SimulationClassUnitTesting.h:50
void setUserIni(const std::string &user_ini) override
Definition SimulationClassUnitTesting.h:85
int run() override
Definition SimulationClassUnitTesting.h:105
~SimulationClassUnitTesting() override=default