MoCSI API Reference
Loading...
Searching...
No Matches
SnapshotLoader.h
Go to the documentation of this file.
1#include <algorithm>
2#include <cstdint>
3#include <exception>
4#include <memory>
5#include <valarray>
6#include <vector>
7
8#include "CsvParser.h"
10#include "GenericSubmodule.h"
11#include "IniParser.h"
13
14#ifdef MPI_PRESENT
15#include <mpi.h>
16
18#endif
19
25template <typename T>
27{
28 private:
29 std::unique_ptr<CsvParser> m_snapshot_field_data;
30 std::unique_ptr<IniParserSnapshot> m_snapshot_ini_data;
31 bool m_no_snapshot{false};
32 bool m_keep_sim_config{false};
33 bool m_keep_module_inis{false};
34#ifdef MPI_PRESENT
40 int count{0};
43 long data_sizes[2];
46 void _parseFooterString(const std::vector<unsigned char>& footer,
47 std::vector<std::string>& field_names, T& elapsed_time,
48 std::vector<MPI_Offset>& field_sizes);
49 void _fillField(const std::string& field_name, const MPI_Offset field_size,
52 std::string field_file{};
53#endif
54
55 public:
57
60 std::vector<std::shared_ptr<GenericManagingModule<T>>>& all_modules,
61 std::vector<std::shared_ptr<GenericSubmodule<T>>>& all_submodules);
63};
64
70template <typename T>
72{
73 // Check if sim config or module ini file retention is selected.
74 try
75 {
76 m_keep_sim_config = sim->m_simulation_config.getBoolParameters("_snapshot_keep_sim");
77 }
78 catch (const BadInput&)
79 {
80 // Do nothing here
81 }
82 try
83 {
84 m_keep_module_inis = sim->m_simulation_config.getBoolParameters("_snapshot_keep_module");
85 }
86 catch (const BadInput&)
87 {
88 // Do nothing here
89 }
90
91 // -----------------------------
92 // Load snapshot if present
93 // -----------------------------
94 std::string snapshotStr;
95 try
96 {
97 snapshotStr = sim->m_simulation_config.getStringParameters("snapshot");
98 }
99 catch (const BadInput&)
100 {
101 std::cout << "[SnapshotLoader] No snapshot key found in config. Skipping snapshot load.\n";
102 m_no_snapshot = true;
103 return;
104 }
105
106 if (snapshotStr.empty() || snapshotStr == "None")
107 {
108 std::cout << "[SnapshotLoader] Snapshot value is 'None'. Skipping snapshot load.\n";
109 m_no_snapshot = true;
110 return;
111 }
112
113 // Key exists and value is valid
114 std::string filename = snapshotStr;
115 std::string filetype{"snapshot"};
116
117 try
118 {
119#ifdef MPI_PRESENT
120 _getFieldFile(sim);
121#else
122 m_snapshot_field_data = std::make_unique<CsvParser>(filename, filetype);
123#endif
124 m_snapshot_ini_data = std::make_unique<IniParserSnapshot>(filename);
125 if (sim->my_rank == 0)
126 {
127 std::cout << "\033[32m[SnapshotLoader] Successfully read snapshot file: " << filename
128 << "\033[0m\n";
129 }
130 }
131 catch (const std::exception& e)
132 {
133 std::cerr << "[SnapshotLoader] Failed to read snapshot file '" << filename
134 << "': " << e.what() << "\n";
135 m_no_snapshot = true;
136 }
137}
138
144template <typename T>
146{
147 if (m_no_snapshot)
148 {
149 return;
150 }
151#ifdef MPI_PRESENT
152 _parseFieldsMPI(sim);
153#else
154 std::vector<std::vector<T>> all_fields_content;
155 std::vector<std::string> all_field_names;
156 double elapsed_time{0.0};
157
158 m_snapshot_field_data->getSnapshotContents(all_fields_content, all_field_names, elapsed_time);
159
160 // For each field that is part of the snapshot
161 for (int i{0}; i < all_field_names.size(); i++)
162 {
163 // Write the snapshot data into the field map. Can't just copy here due to valarray vector
164 // mismatch.
165 for (int j{0}; j < all_fields_content[i].size(); j++)
166 {
167 sim->m_field_map[all_field_names[i]][j] = all_fields_content[i][j];
168 }
169 }
170
171 sim->elapsed_time = elapsed_time;
172 sim->time_step = int(elapsed_time / sim->m_simulation_config.getDoubleParameters("time_delta"));
173#endif
174 if (sim->my_rank == 0)
175 {
176 std::cout
177 << "\033[32m[SnapshotLoader] Successfully loaded snapshot file field data\033[0m\n";
178 }
179}
180
190template <typename T>
193 std::vector<std::shared_ptr<GenericManagingModule<T>>>& all_modules,
194 std::vector<std::shared_ptr<GenericSubmodule<T>>>& all_submodules)
195{
196 if (m_no_snapshot || m_keep_module_inis)
197 {
198 return;
199 }
200 std::vector<std::string> all_module_names;
201 all_module_names = m_snapshot_ini_data->getModuleNames();
202
203 bool module_placed{false};
204 for (const auto& module_name : all_module_names)
205 {
206 if (module_name == "SimulationConfig")
207 {
208 module_placed = true;
209 }
210 for (const auto& loaded_module : all_modules)
211 {
212 if (module_placed)
213 {
214 break;
215 }
216 if (module_name == loaded_module->getNameLocal())
217 {
218 loaded_module->overwriteLocalIniContent(
219 m_snapshot_ini_data->getModuleContents(module_name));
220 module_placed = true;
221 }
222 }
223
224 for (const auto& loaded_submodule : all_submodules)
225 {
226 if (module_placed)
227 {
228 break;
229 }
230 if (module_name == loaded_submodule->getNameLocal())
231 {
232 loaded_submodule->overwriteLocalIniContent(
233 m_snapshot_ini_data->getModuleContents(module_name));
234 module_placed = true;
235 }
236 }
237 if (module_placed)
238 {
239 module_placed = false;
240 }
241 else
242 {
243 std::cerr << "[SnapshotLoader]: Couldn't find module: " << module_name
244 << " in all loaded modules. Please load this module to complete snapshot "
245 "loading.\n";
246 throw std::runtime_error(
247 static_cast<std::string>("[SnapshotLoader]: Couldn't find specified module from "
248 "snapshot file in all loaded modules."));
249 }
250 }
251 if (sim->my_rank == 0)
252 {
253 std::cout << "\033[32m[SnapshotLoader] Successfully loaded snapshot file module ini "
254 "files\033[0m\n";
255 }
256}
257
263template <typename T>
265{
266 if (m_no_snapshot || m_keep_sim_config)
267 {
268 return;
269 }
270 for (const auto& [key, map_content] :
271 m_snapshot_ini_data->getModuleContents("SimulationConfig"))
272 {
273 // Do not write snapshot save file into the config/overwrite it, as it would lead to a loop
274 // that always overwrites the current snapshot.
275 if (key != "snapshot_save_file")
276 {
277 sim->m_simulation_config.forceValueOverwrite(key,
278 std::any_cast<std::string>(map_content));
279 }
280 }
281 if (sim->my_rank == 0)
282 {
283 std::cout << "\033[32m[SnapshotLoader] Successfully loaded snapshot file simulation "
284 "configuration data\033[0m\n";
285 }
286}
287
288#ifdef MPI_PRESENT
296template <typename T>
298{
301 T elapsed_time{};
302 std::vector<std::string> field_names{};
303 std::vector<MPI_Offset> field_sizes{};
304
305 numerical_layers = sim->m_simulation_config.getIntParameters("numerical_layers");
306 local_number_of_facets = sim->m_simulation_config.getIntParameters("number_of_facets");
307 global_number_of_facets = sim->m_simulation_config.getIntParameters("global_number_of_facets");
308 // Set datatype of field to the one used across this simulation
312
319 std::vector<unsigned char> footer_content(footer_data_size);
323 for (int i{0}; i < field_names.size(); i++)
324 {
326 }
327 sim->elapsed_time = elapsed_time;
328 sim->time_step = int(elapsed_time / sim->m_simulation_config.getDoubleParameters("time_delta"));
329}
330
340template <typename T>
341void SnapshotLoader<T>::_parseFooterString(const std::vector<unsigned char>& footer,
342 std::vector<std::string>& field_names, T& elapsed_time,
343 std::vector<MPI_Offset>& field_sizes)
344{
345 std::string footer_string{footer.begin(), footer.end()};
346 auto starting_it{footer_string.begin()};
347 int current_entry{0};
348 for (auto it{footer_string.begin()}; it != footer_string.end(); it++)
349 {
350 if (*it == ',')
351 {
352 std::string current_string{starting_it, it};
353 if (current_entry % 3 == 0)
354 {
355 field_names.push_back(current_string);
356 }
357 else if (current_entry % 3 == 1)
358 {
359 elapsed_time = std::stod(current_string);
360 }
361 else if (current_entry % 3 == 2)
362 {
363 field_sizes.push_back(std::stoi(current_string));
364 }
365 starting_it = it + 1;
367 }
368 }
369}
370
378template <typename T>
380{
381 field_file = sim->m_simulation_config.getStringParameters("snapshot");
382 const auto& file_type_start{std::find(field_file.begin(), field_file.end(), '.')};
383 if (file_type_start == field_file.end())
384 {
385 std::cerr << "Could not find file type separator '.' in supplied snapshot file path. "
386 "Please specify the full path with file type.";
387 throw std::runtime_error(
388 "Could not find file type separator '.' in supplied snapshot file path. "
389 "Please specify the full path with file type.");
390 }
391 std::string insertion{"_fields"};
392 field_file.insert(file_type_start, insertion.begin(), insertion.end());
393}
394
406template <typename T>
409{
410 if (sim->m_field_map.find(field_name) == sim->m_field_map.end())
411 {
412 std::cerr << "Field: " << field_name
413 << " not found in m_field_map! Could not fully load snapshot - terminating.";
414 throw std::runtime_error(
415 "Field could not be found in m_field_map despite loading snapshot. Check "
416 "your snapshot file and configuration!");
417 }
418
421 static_cast<int>((sim->m_field_map[field_name]).size()), m_dtype,
424}
425
438template <typename T>
441{
444 {
445 // For the block of same size processes, displacement is rank * local_number_of_facets *
446 // data_type_byte_size.
447 if (sim->my_rank < sim->number_of_larger_procs)
448 {
451 }
452 // For the second block, add the first block displacement and count how many second block
453 // processes are between the targeted position and the first block.
454 // local_number_of_facets_large_proc = local_number_of_facets_small_proc + numerical_layers.
455 else
456 {
458 (sim->number_of_larger_procs * (local_number_of_facets + 1)
459 + (sim->my_rank - sim->number_of_larger_procs) * local_number_of_facets)
461 }
462 }
464 {
465 // For the block of same size processes, displacement is rank * local_number_of_facets *
466 // data_type_byte_size.
467 if (sim->my_rank < sim->number_of_larger_procs)
468 {
470 }
471 // For the second block, add the first block displacement and count how many second block
472 // processes are between the targeted position and the first block.
473 // local_number_of_facets_large_proc = local_number_of_facets_small_proc + 1.
474 else
475 {
477 (sim->number_of_larger_procs * (local_number_of_facets + 1)
478 + (sim->my_rank - sim->number_of_larger_procs) * local_number_of_facets)
479 * type_size;
480 }
481 }
482 else
483 {
484 // Replace this section in the new MR with handling of "irregular" field sizes.
485 std::cerr
486 << "[SnapshotLoader]: Field in SnapshotLoader is of irregular size! Currently only "
487 "supporting sizes of numerical_layers * numbers_of_facets or numbers_of_facets.\n";
488 throw std::runtime_error(
489 "[SnapshotLoader]: Field in SnapshotLoader is of irregular size! Currently only "
490 "supporting sizes of numerical_layers * numbers_of_facets or numbers_of_facets.\n");
491 }
492 return proc_displacement;
493}
494#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
Class which loads snapshot files and writes their data to the inis and fields. Loads any file created...
Definition SnapshotLoader.h:27
SnapshotLoader(SimulationClassBase< T > *sim)
Constructor of the SnapshotLoader.
Definition SnapshotLoader.h:71
void overwriteIniFiles(SimulationClassBase< T > *sim, std::vector< std::shared_ptr< GenericManagingModule< T > > > &all_modules, std::vector< std::shared_ptr< GenericSubmodule< T > > > &all_submodules)
Overwrites the content of the module ini files within each module. Throws an error,...
Definition SnapshotLoader.h:191
void overwriteSimFields(SimulationClassBase< T > *sim)
Overwrites the fields in sim with the ones from the snapshot.
Definition SnapshotLoader.h:145
void overwriteSimConfig(SimulationClassBase< T > *sim)
Overwrite the simulation config data (default.ini + user supplied run ini).
Definition SnapshotLoader.h:264