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 (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 (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 std::cout << sim->m_simulation_config.getStringParameters("snapshot") << "\n";
66 if (sim->m_simulation_config.getStringParameters("snapshot") == "None")
67 {
68 // I wanted to use return here, but this is apparently disallowed by the standard
69 throw BadInput("String is None.");
70 }
71 std::string filename{sim->m_simulation_config.getStringParameters("snapshot")};
72 std::string filetype{"snapshot"};
73 m_snapshot_field_data = std::make_unique<CsvParser>(filename, filetype);
74 m_snapshot_ini_data = std::make_unique<IniParserSnapshot>(filename);
75 std::cout << "\033[32m[SnapshotLoader] Successfully read snapshot file: "
76 << sim->m_simulation_config.getStringParameters("snapshot") << "\033[0m\n";
77 }
78 catch (BadInput)
79 {
80 m_no_snapshot = true;
81 }
82}
83
89template <typename T>
91{
92 if (m_no_snapshot)
93 {
94 return;
95 }
96 std::vector<std::vector<T>> all_fields_content;
97 std::vector<std::string> all_field_names;
98 double elapsed_time{0.0};
99
100 m_snapshot_field_data->getSnapshotContents(all_fields_content, all_field_names, elapsed_time);
101
102 // For each field that is part of the snapshot
103 for (int i{0}; i < all_field_names.size(); i++)
104 {
105 // Write the snapshot data into the field map. Can't just copy here due to valarray vector
106 // mismatch.
107 for (int j{0}; j < all_fields_content[i].size(); j++)
108 {
109 sim->m_field_map[all_field_names[i]][j] = all_fields_content[i][j];
110 }
111 }
112
113 sim->elapsed_time = elapsed_time;
114 sim->time_step = int(elapsed_time / sim->m_simulation_config.getDoubleParameters("time_delta"));
115 std::cout << "\033[32m[SnapshotLoader] Successfully loaded snapshot file field data\033[0m\n";
116}
117
127template <typename T>
130 std::vector<std::shared_ptr<GenericManagingModule<T>>>& all_modules,
131 std::vector<std::shared_ptr<GenericSubmodule<T>>>& all_submodules)
132{
133 if (m_no_snapshot || m_keep_module_inis)
134 {
135 return;
136 }
137 std::vector<std::string> all_module_names;
138 all_module_names = m_snapshot_ini_data->getModuleNames();
139
140 bool module_placed{false};
141 for (const auto& module_name : all_module_names)
142 {
143 if (module_name == "SimulationConfig")
144 {
145 module_placed = true;
146 }
147 for (const auto& loaded_module : all_modules)
148 {
149 if (module_placed)
150 {
151 break;
152 }
153 if (module_name == loaded_module->getNameLocal())
154 {
155 loaded_module->overwriteLocalIniContent(
156 m_snapshot_ini_data->getModuleContents(module_name));
157 module_placed = true;
158 }
159 }
160
161 for (const auto& loaded_submodule : all_submodules)
162 {
163 if (module_placed)
164 {
165 break;
166 }
167 if (module_name == loaded_submodule->getNameLocal())
168 {
169 loaded_submodule->overwriteLocalIniContent(
170 m_snapshot_ini_data->getModuleContents(module_name));
171 module_placed = true;
172 }
173 }
174 if (module_placed)
175 {
176 module_placed = false;
177 }
178 else
179 {
180 std::cerr << "[SnapshotLoader] Couldn't find module: " << module_name
181 << " in all loaded modules. Please load this module to complete snapshot "
182 "loading.\n";
183 throw std::runtime_error(
184 "Couldn't find specified module from snapshot file in all loaded modules.\n");
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:35
CsrSparseMatrix()
Constructor for an empty CsrSparseMatrix object. Leaves all storage arrays empty but sets the flag to...
Definition CsrMatrix.h:92
std::pair< int, int > size() const
Function that returns the size of the matrix as a pair in (rows, columns) format.
Definition GenericMatrix.h:118
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:128
void overwriteSimFields(SimulationClassBase< T > *sim)
Overwrites the fields in sim with the ones from the snapshot.
Definition SnapshotLoader.h:90
void overwriteSimConfig(SimulationClassBase< T > *sim)
Overwrite the simulation config data (default.ini + user supplied run ini).
Definition SnapshotLoader.h:197