MoCSI API Reference
Loading...
Searching...
No Matches
SnapshotLoader.h
Go to the documentation of this file.
1#include <memory>
2#include <valarray>
3#include <vector>
4
5#include "CsvParser.h"
7#include "GenericSubmodule.h"
8#include "IniParser.h"
10
16template <typename T>
18{
19 private:
20 std::unique_ptr<CsvParser> m_snapshot_field_data;
21 std::unique_ptr<IniParserSnapshot> m_snapshot_ini_data;
22 bool m_no_snapshot{false};
23 bool m_keep_sim_config{false};
24 bool m_keep_module_inis{false};
25
26 public:
28
31 std::vector<std::shared_ptr<GenericManagingModule<T>>>& all_modules,
32 std::vector<std::shared_ptr<GenericSubmodule<T>>>& all_submodules);
34};
35
41template <typename T>
43{
44 // Check if sim config or module ini file retention is selected.
45 try
46 {
47 m_keep_sim_config = sim->m_simulation_config.getBoolParameters("_snapshot_keep_sim");
48 }
49 catch (const BadInput&)
50 {
51 // Do nothing here
52 }
53 try
54 {
55 m_keep_module_inis = sim->m_simulation_config.getBoolParameters("_snapshot_keep_module");
56 }
57 catch (const BadInput&)
58 {
59 // Do nothing here
60 }
61
62 // If a snapshot is given, load it. If not, do nothing, as the user won't have requested a load.
63 try
64 {
65 if (sim->m_simulation_config.getStringParameters("snapshot") == "None")
66 {
67 // I wanted to use return here, but this is apparently disallowed by the standard
68 throw BadInput("String is None.");
69 }
70 std::string filename{sim->m_simulation_config.getStringParameters("snapshot")};
71 std::string filetype{"snapshot"};
72 m_snapshot_field_data = std::make_unique<CsvParser>(filename, filetype);
73 m_snapshot_ini_data = std::make_unique<IniParserSnapshot>(filename);
74 std::cout << "\033[32m[SnapshotLoader] Successfully read snapshot file: "
75 << sim->m_simulation_config.getStringParameters("snapshot") << "\033[0m\n";
76 }
77 catch (const BadInput&)
78 {
79 m_no_snapshot = true;
80 }
81}
82
88template <typename T>
90{
91 if (m_no_snapshot)
92 {
93 return;
94 }
95 std::vector<std::vector<T>> all_fields_content;
96 std::vector<std::string> all_field_names;
97 double elapsed_time{0.0};
98
99 m_snapshot_field_data->getSnapshotContents(all_fields_content, all_field_names, elapsed_time);
100
101 // For each field that is part of the snapshot
102 for (int i{0}; i < all_field_names.size(); i++)
103 {
104 // Write the snapshot data into the field map. Can't just copy here due to valarray vector
105 // mismatch.
106 for (int j{0}; j < all_fields_content[i].size(); j++)
107 {
108 sim->m_field_map[all_field_names[i]][j] = all_fields_content[i][j];
109 }
110 }
111
112 sim->elapsed_time = elapsed_time;
113 sim->time_step = int(elapsed_time / sim->m_simulation_config.getDoubleParameters("time_delta"));
114 std::cout << "\033[32m[SnapshotLoader] Successfully loaded snapshot file field data\033[0m\n";
115}
116
126template <typename T>
129 std::vector<std::shared_ptr<GenericManagingModule<T>>>& all_modules,
130 std::vector<std::shared_ptr<GenericSubmodule<T>>>& all_submodules)
131{
132 if (m_no_snapshot || m_keep_module_inis)
133 {
134 return;
135 }
136 std::vector<std::string> all_module_names;
137 all_module_names = m_snapshot_ini_data->getModuleNames();
138
139 bool module_placed{false};
140 for (const auto& module_name : all_module_names)
141 {
142 if (module_name == "SimulationConfig")
143 {
144 module_placed = true;
145 }
146 for (const auto& loaded_module : all_modules)
147 {
148 if (module_placed)
149 {
150 break;
151 }
152 if (module_name == loaded_module->getNameLocal())
153 {
154 loaded_module->overwriteLocalIniContent(
155 m_snapshot_ini_data->getModuleContents(module_name));
156 module_placed = true;
157 }
158 }
159
160 for (const auto& loaded_submodule : all_submodules)
161 {
162 if (module_placed)
163 {
164 break;
165 }
166 if (module_name == loaded_submodule->getNameLocal())
167 {
168 loaded_submodule->overwriteLocalIniContent(
169 m_snapshot_ini_data->getModuleContents(module_name));
170 module_placed = true;
171 }
172 }
173 if (module_placed)
174 {
175 module_placed = false;
176 }
177 else
178 {
179 std::cerr << "[SnapshotLoader]: Couldn't find module: " << module_name
180 << " in all loaded modules. Please load this module to complete snapshot "
181 "loading.\n";
182 throw std::runtime_error(
183 static_cast<std::string>("[SnapshotLoader]: Couldn't find specified module from "
184 "snapshot file in all loaded modules."));
185 }
186 }
187 std::cout
188 << "\033[32m[SnapshotLoader] Successfully loaded snapshot file module ini files\033[0m\n";
189}
190
196template <typename T>
198{
199 if (m_no_snapshot || m_keep_sim_config)
200 {
201 return;
202 }
203 for (const auto& [key, map_content] :
204 m_snapshot_ini_data->getModuleContents("SimulationConfig"))
205 {
206 // Do not write snapshot save file into the config/overwrite it, as it would lead to a loop
207 // that always overwrites the current snapshot.
208 if (key != "snapshot_save_file")
209 {
210 sim->m_simulation_config.forceValueOverwrite(key,
211 std::any_cast<std::string>(map_content));
212 }
213 }
214 std::cout << "\033[32m[SnapshotLoader] Successfully loaded snapshot file simulation "
215 "configuration data\033[0m\n";
216}
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:18
SnapshotLoader(SimulationClassBase< T > *sim)
Constructor of the SnapshotLoader.
Definition SnapshotLoader.h:42
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:127
void overwriteSimFields(SimulationClassBase< T > *sim)
Overwrites the fields in sim with the ones from the snapshot.
Definition SnapshotLoader.h:89
void overwriteSimConfig(SimulationClassBase< T > *sim)
Overwrite the simulation config data (default.ini + user supplied run ini).
Definition SnapshotLoader.h:197