MoCSI API Reference
Loading...
Searching...
No Matches
OutputCsvTool.h
Go to the documentation of this file.
1#ifndef OUTPUT_CSV_TOOL_H
2#define OUTPUT_CSV_TOOL_H
3
4#define VERSION "5"
5
6#include <fstream>
7#include <iomanip>
8#include <iostream>
9#include <memory>
10#include <ostream>
11#include <string>
12#include <valarray>
13#include <vector>
14
15#include "../../src/GenericManagingModule.h"
16#include "../../src/ModuleFactory.h"
17
40template <typename T>
42{
43 private:
44 static bool m_registered;
45 std::string m_ini_filepath{"tools/OutputCsvTool.ini"};
46 std::string m_output_filepath{};
47 std::string m_save_mode{};
48 std::vector<std::string>
49 m_save_fields; // Right now this lives only in the local ini, but we make it live in
50 // the sim class, so that the other modules can write their save request
51 // to it.
52 std::vector<int> m_save_steps{};
53 int m_currentIndex{0};
54 std::ofstream output_file;
55
56 void _writeData();
57 void _writeDataSurface(int period);
58
59 public:
61 bool setup(std::vector<std::shared_ptr<GenericSubmodule<T>>> all_submodules) override;
62 bool exec(std::string_view param) override; // main functionality of the module
63
64 bool init() override { return true; };
65 bool preTimeStep() override { return true; };
66 bool postNonLinIter() override { return true; };
67 bool postTimeStep() override;
68 bool output() override;
69 std::vector<std::string> getChainInsertion() const override
70 {
71 return this->ini_file_data.getStringVectorParameters("chain_insertion");
72 };
73
74 static std::shared_ptr<GenericManagingModule<T>> createMethode(SimulationClassBase<T>* sim)
75 {
76 return std::make_shared<OutputCsvTool<T>>(sim);
77 };
78 static std::string getName() { return "OutputCsvTool"; };
79 std::string_view getNameLocal() const override { return "OutputCsvTool"; };
80};
81
82// ================= Implementation =================
83
84template <typename T>
86{
87 try
88 {
89 std::string ini_folder_path{
90 this->sim->m_simulation_config.getStringParameters("ini_folder_path")};
91 this->ini_file_data.loadUserInput(ini_folder_path + m_ini_filepath);
92 m_output_filepath = this->ini_file_data.getStringParameters("file_path");
93 m_save_mode = this->ini_file_data.getStringParameters("save_mode");
94 m_save_fields = this->ini_file_data.getStringVectorParameters("save_fields");
96 }
97 catch (const BadInput& e)
98 {
99 std::cerr << e.what() << '\n';
100 }
101}
102
103template <typename T>
104bool OutputCsvTool<T>::setup(std::vector<std::shared_ptr<GenericSubmodule<T>>> all_submodules)
105{
106 // Flush the file
107 std::ofstream file(m_output_filepath);
108
109 if (m_save_mode == "specific_time_steps")
110 {
111 try
112 {
113 std::vector<double> save_steps{
114 this->ini_file_data.getDoubleVectorParameters("save_time_steps")};
115 for (int i{0}; i < save_steps.size(); i++)
116 {
117 m_save_steps.push_back(static_cast<int>(save_steps[i]));
118 }
119 }
120 catch (const BadInput& e)
121 {
122 std::cerr << e.what() << '\n';
123 }
124 }
125 return true;
126}
127
128template <typename T>
129bool OutputCsvTool<T>::exec(std::string_view param)
130{
131 if (param == "InitChain")
132 {
133 return init();
134 }
135 if (param == "PreTimeStepChain")
136 {
137 return preTimeStep();
138 }
139 if (param == "PostNonLinIterChain")
140 {
141 return postNonLinIter();
142 }
143 if (param == "PostTimeStepChain")
144 {
145 return postTimeStep();
146 }
147 if (param == "OutputChain")
148 {
149 return output();
150 }
151 return false;
152}
153
154template <typename T>
156{
157 if ((m_save_mode == "specific_time_steps") && (m_currentIndex < m_save_steps.size())
158 && (m_save_steps[m_currentIndex] == this->sim->time_step))
159 {
160 _writeData();
161 m_currentIndex++;
162 }
163 else if ((m_save_mode == "specific_time_steps_surface")
164 && (m_currentIndex < m_save_steps.size())
165 && (m_save_steps[m_currentIndex] == this->sim->time_step))
166 {
167 _writeDataSurface(this->sim->m_simulation_config.getIntParameters("numerical_layers"));
168 m_currentIndex++;
169 }
170 else if ((m_save_mode == "interval")
171 && (this->sim->time_step % this->ini_file_data.getIntParameters("interval") == 0))
172 {
173 _writeData();
174 }
175 else if ((m_save_mode == "interval_surface")
176 && (this->sim->time_step % this->ini_file_data.getIntParameters("interval") == 0))
177 {
178 _writeDataSurface(this->sim->m_simulation_config.getIntParameters("numerical_layers"));
179 }
180 return true;
181}
182
183template <typename T>
185{
186 if (m_save_mode == "surface")
187 {
188 _writeDataSurface(this->sim->m_simulation_config.getIntParameters("numerical_layers"));
189 }
190 else
191 {
192 _writeData();
193 }
194 return true;
195}
196
197template <typename T>
199{
200 output_file.open(m_output_filepath, std::ios::app);
201
202 for (const auto& field_name : m_save_fields)
203 {
204 output_file << field_name << "," << this->sim->elapsed_time << ",";
205 const std::valarray<T>& field{this->sim->getField(field_name)};
206 for (int i{0}; i < field.size() - 1; i++)
207 {
208 output_file << field[i] << std::setprecision(15) << ",";
209 }
210 output_file << field[field.size() - 1] << std::setprecision(15) << "\n";
211 }
212
213 output_file.close();
214}
215
216template <typename T>
218{
219 output_file.open(m_output_filepath, std::ios::app);
220
221 for (const auto& field_name : m_save_fields)
222 {
223 output_file << field_name << "," << this->sim->elapsed_time << ",";
224 const std::valarray<T>& field{this->sim->getField(field_name)};
225 for (int i{0}; i < static_cast<int>(field.size() / period) - 1; i++)
226 {
227 output_file << field[i * period] << std::setprecision(15) << ",";
228 }
229 output_file << field[field.size() - period] << std::setprecision(15) << "\n";
230 }
231
232 output_file.close();
233}
234
235template <typename T>
237
238#endif // OUTPUT_CSV_TOOL_H
This error class inherits from std::exception and marks faulty parameter or CL inputs....
Definition IniParser.h:71
const char * what() const noexcept override
Definition IniParser.h:78
Abstract base class for the managing modules. Managing modules are the highest tier of modules and ar...
Definition GenericManagingModule.h:29
InputManager ini_file_data
Definition GenericManagingModule.h:38
SimulationClassBase< T > * sim
Definition GenericManagingModule.h:31
std::vector< std::string > m_generic_submodules
Definition GenericManagingModule.h:36
Abstract base class for the submodules. Submodules are below managing modules and will only be run by...
Definition GenericSubmodule.h:25
std::vector< std::string > getStringVectorParameters(const std::string &key) const
Definition InputManager.cpp:503
void loadUserInput(const std::string &user_ini_file_path)
Public function to load and merge user supplied ini files with the already stored data.
Definition InputManager.cpp:42
std::string getStringParameters(const std::string &key) const
Getter function to access the m_parameters map and returns a string. Throws a BadInput error,...
Definition InputManager.cpp:493
static constexpr bool registerModule(std::string name, creation_method module) noexcept
Function that adds a module to the module registry map.
Definition ModuleFactory.h:68
Saves specified simulation field data into CSV files.
Definition OutputCsvTool.h:42
bool output() override
Definition OutputCsvTool.h:184
bool preTimeStep() override
Definition OutputCsvTool.h:65
std::string_view getNameLocal() const override
Definition OutputCsvTool.h:79
bool postNonLinIter() override
Definition OutputCsvTool.h:66
bool init() override
Definition OutputCsvTool.h:64
static std::string getName()
Definition OutputCsvTool.h:78
bool exec(std::string_view param) override
Definition OutputCsvTool.h:129
bool setup(std::vector< std::shared_ptr< GenericSubmodule< T > > > all_submodules) override
Definition OutputCsvTool.h:104
OutputCsvTool(SimulationClassBase< T > *sim)
Definition OutputCsvTool.h:85
static std::shared_ptr< GenericManagingModule< T > > createMethode(SimulationClassBase< T > *sim)
Definition OutputCsvTool.h:74
std::vector< std::string > getChainInsertion() const override
Definition OutputCsvTool.h:69
bool postTimeStep() override
Definition OutputCsvTool.h:155
Definition SimulationClassBase.h:15
InputManager m_simulation_config
Definition SimulationClassBase.h:24