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