MoCSI API Reference
Loading...
Searching...
No Matches
Density.h
Go to the documentation of this file.
1#ifndef DENSITY_H
2#define DENSITY_H
3
4#define DENSITY_VERSION "5"
5
6#include <iostream>
7#include <memory>
8#include <string>
9#include <valarray>
10
11#include "../../src/GenericManagingModule.h"
12#include "../../src/ModuleFactory.h"
13
35template <typename T>
37{
38 private:
44 static bool m_registered;
45
49 std::string m_ini_filepath{"density/Density.ini"};
50
51 public:
60
69 bool setup(std::vector<std::shared_ptr<GenericSubmodule<T>>> all_submodules) override;
70
83 bool exec(std::string_view param) override; // main functionality of the module
84
92 bool init() override;
93
101 bool preTimeStep() override;
102
110 bool postTimeStep() override { return true; };
111
119 bool output() override { return true; };
120
126 void writeToSim();
127
136 std::vector<std::string> getChainInsertion() const override
137 {
138 return this->ini_file_data.getStringVectorParameters("chain_insertion");
139 };
140
149 static std::shared_ptr<GenericManagingModule<T>> createMethode(SimulationClassBase<T>* sim)
150 {
151 return std::make_shared<Density<T>>(sim);
152 };
153
159 static std::string getName() { return "Density"; };
160
166 std::string_view getNameLocal() const override { return "Density"; };
167};
168
169// ================= Implementation =================
170
171template <typename T>
173{
174 try
175 {
176 std::string ini_folder_path{
177 this->sim->m_simulation_config.getStringParameters("ini_folder_path")};
178 this->ini_file_data.loadUserInput(ini_folder_path + m_ini_filepath);
180 }
181 catch (const BadInput& e)
182 {
183 std::cerr << "[Density]: " << e.what() << '\n';
184 throw BadInput(static_cast<std::string>("[Density]: ")
185 + static_cast<std::string>(e.what()));
186 }
187}
188
189template <typename T>
190bool Density<T>::setup(std::vector<std::shared_ptr<GenericSubmodule<T>>> all_submodules)
191{
192 try
193 {
194 this->sim->m_field_map.insert(
195 {"Density",
196 std::valarray<T>(
197 this->sim->m_simulation_config.getIntParameters("numerical_layers")
198 * this->sim->m_simulation_config.getIntParameters("number_of_facets"))});
199 this->module_field = std::make_shared<std::valarray<T>>(
200 this->sim->m_simulation_config.getIntParameters("numerical_layers")
201 * this->sim->m_simulation_config.getIntParameters("number_of_facets"));
202 this->setSubmodules(all_submodules);
203 }
204 catch (const BadInput& e)
205 {
206 std::cerr << "[Density]: Failed to insert field map and/or local field!\n[Density]: "
207 << e.what() << '\n';
208 throw BadInput(static_cast<std::string>(
209 "[Density]: Failed to insert field map and/or local field!\n[Density]: ")
210 + static_cast<std::string>(e.what()));
211 }
212 return true;
213}
214
215template <typename T>
216bool Density<T>::exec(std::string_view param)
217{
218 if (param == "InitChain")
219 {
220 return init();
221 }
222 if (param == "PreTimeStepChain")
223 {
224 return preTimeStep();
225 }
226 if (param == "PostTimeStepChain")
227 {
228 return postTimeStep();
229 }
230 if (param == "OutputChain")
231 {
232 return output();
233 }
234 return false;
235}
236
237template <typename T>
239{
240 this->sim->m_field_map["Density"] = (*this->module_field);
241}
242
243template <typename T>
245{
246 this->sub_module_chain.runChain("InitChain");
247 writeToSim();
248 return true;
249}
250
251template <typename T>
253{
254 this->sub_module_chain.runChain("PreTimeStepChain");
255 writeToSim();
256 return true;
257}
258
259template <typename T>
260bool Density<T>::m_registered = ModuleFactory<T>::registerModule(getName(), createMethode);
261
262#endif // DENSITY_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
Managing module that calculates bulk density based on submodules.
Definition Density.h:37
bool setup(std::vector< std::shared_ptr< GenericSubmodule< T > > > all_submodules) override
Setup method called after construction.
Definition Density.h:190
static std::string getName()
Get the name of the module.
Definition Density.h:159
bool init() override
Initialization step.
Definition Density.h:244
bool exec(std::string_view param) override
Main execution method of the module.
Definition Density.h:216
bool preTimeStep() override
Pre-time-step processing.
Definition Density.h:252
std::vector< std::string > getChainInsertion() const override
Get chain insertion points.
Definition Density.h:136
bool output() override
Output stage.
Definition Density.h:119
std::string_view getNameLocal() const override
Get the localized name of the module.
Definition Density.h:166
void writeToSim()
Write the current density field to the simulation.
Definition Density.h:238
bool postTimeStep() override
Post-time-step processing.
Definition Density.h:110
static std::shared_ptr< GenericManagingModule< T > > createMethode(SimulationClassBase< T > *sim)
Factory method for creating a Density module.
Definition Density.h:149
Density(SimulationClassBase< T > *sim)
Construct the Density module.
Definition Density.h:172
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:508
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:45
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:498
static constexpr bool registerModule(std::string name, creation_method module) noexcept
Function that adds a module to the module registry map.
Definition ModuleFactory.h:75
Definition SimulationClassBase.h:19
InputManager m_simulation_config
Definition SimulationClassBase.h:29