MoCSI API Reference
Loading...
Searching...
No Matches
GenericSubmodule.h
Go to the documentation of this file.
1#ifndef TPM_SRC_GENERIC_SUBMODULE_H
2#define TPM_SRC_GENERIC_SUBMODULE_H
3
4#include <fstream>
5#include <functional>
6#include <string>
7#include <unordered_set>
8#include <valarray>
9
10#include "ChainManager.h"
11#include "IniParser.h"
12#include "InputManager.h"
13#include "SimulationClassBase.h"
14
23template <typename T>
25{
26 protected:
28 "global"}; // A chain manager to store and execute submodel chains.
29 std::shared_ptr<std::valarray<T>> module_field; // pointer to the field that is defined by
30 // module managing module (not a must)
32 sim{}; // const access to simulation class parameters --> pointer (shared pointer) or
33 // reference? Dummy here as no Simulation class exists so far.
34 std::vector<std::string> m_generic_submodules; // The submodules that will be loaded into
35 // the chain, set by children.
36 InputManager ini_file_data; // The handler for the data which gets passed through the
37 // module-specific ini-file
38 bool in_snapshot{false}; // should something (default the field) be included in snapshot?
39 bool in_output{false}; // should something (default the field) be included in output?
41 '?'}; // the output field character. Also identifies return-call from load_snapshot
42 bool is_set_up{false};
43
44 public:
50
51 virtual ~GenericSubmodule() = default;
52
59
60 virtual bool setup(std::vector<std::shared_ptr<GenericSubmodule<T>>> all_submodules)
61 {
62 return true;
63 }; // loads module into necessary module chains (auto load for calculation chains)
64 virtual bool exec(std::string_view param) = 0; // main functionality of the module
65
66 // different implementations for exec function, depending on module chain
67 virtual bool init() { return true; };
68 virtual bool preTimeStep() { return true; };
69 virtual bool postTimeStep() { return true; };
70 virtual bool postNonLinIter() { return true; };
71 virtual bool output() { return true; };
72
73 virtual std::vector<std::string> getDependencies() const
74 {
75 return {};
76 }; // Return empty vector as default
77 std::vector<std::string> getSubmoduleList() const { return m_generic_submodules; }
78
79 bool isSetUp() { return this->is_set_up; }
80 void submodulesSetUp() { this->is_set_up = true; }
81
82 // Function to access the name of the submodule via the base class
83 virtual std::string_view getNameLocal() const = 0; // return name of module
84
85 virtual void setFieldPtr(std::shared_ptr<std::valarray<T>> field_ptr) {};
86
93
99 void setSubmodules(std::vector<std::shared_ptr<GenericSubmodule<T>>> all_submodules)
100 {
101 std::vector<std::string> chain_names{sub_module_chain.getChainNames()};
103 for (auto& submodule : all_submodules)
104 {
105 std::unordered_set<std::string> requested_sub_modules(m_generic_submodules.begin(),
107 if (requested_sub_modules.find(static_cast<std::string>(submodule->getNameLocal()))
108 != requested_sub_modules.end())
109 {
110 for (const auto& key : chain_names)
111 {
113 if (module_field)
114 {
115 submodule->setFieldPtr(module_field);
116 }
117 }
119 }
120 }
122 {
123 std::cerr << "[GenericSubmodule]: " << this->getNameLocal()
124 << ": Not all requested modules found in the registry.\n";
125 throw std::runtime_error(static_cast<std::string>("[GenericSubmodule]: ")
126 + static_cast<std::string>(this->getNameLocal())
127 + static_cast<std::string>(
128 ": Not all requested modules found in the registry."));
129 }
131 }
132
141 {
142 std::unordered_map<std::string, std::any> ini_file_content{this->ini_file_data.data()};
143 for (const auto& [key, map_content] : ini_file_content)
144 {
146 << " = " << std::any_cast<std::string>(map_content) << "\n";
147 }
148 }
149
159 const std::unordered_map<std::string, std::any>& global_ini_content)
160 {
161 for (const auto& [key, map_content] : global_ini_content)
162 {
163 this->ini_file_data.forceValueOverwrite(key,
164 std::any_cast<std::string>(map_content));
165 }
166 }
167};
168
169#endif
void insertModule(std::string_view key, const std::shared_ptr< ModuleType > &module)
Insert modules into chains. Generates a new chain, if the chain key does not yet exist.
Definition ChainManager.h:134
std::vector< std::string > getChainNames() const
Getter function for the chain names.
Definition ChainManager.h:311
void resolveDependencies()
Guarantees that the module order is correct to ensure all dependencies are met. Throws a DependencyEr...
Definition ChainManager.h:234
Concrete implementation of a matrix class representing a Compressed Sparse Rows (CSR) matrix....
Definition CsrMatrix.h:37
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
Abstract base class for the submodules. Submodules are below managing modules and will only be run by...
Definition GenericSubmodule.h:25
InputManager ini_file_data
Definition GenericSubmodule.h:36
virtual void setFieldPtr(std::shared_ptr< std::valarray< T > > field_ptr)
Definition GenericSubmodule.h:85
virtual std::vector< std::string > getDependencies() const
Definition GenericSubmodule.h:73
virtual bool setup(std::vector< std::shared_ptr< GenericSubmodule< T > > > all_submodules)
Definition GenericSubmodule.h:60
virtual std::string_view getNameLocal() const =0
GenericSubmodule(const SimulationClassBase< T > *l_sim)
Constructor to be preferably used when a module is created during runtime.
Definition GenericSubmodule.h:58
virtual bool postTimeStep()
Definition GenericSubmodule.h:69
std::vector< std::string > m_generic_submodules
Definition GenericSubmodule.h:34
char field_id
Definition GenericSubmodule.h:40
virtual bool exec(std::string_view param)=0
virtual bool init()
Definition GenericSubmodule.h:67
GenericSubmodule()
Constructor without arguments. Will be employed when using the static modules registry.
Definition GenericSubmodule.h:49
virtual ~GenericSubmodule()=default
const SimulationClassBase< T > * sim
Definition GenericSubmodule.h:32
void setSimPointer(SimulationClassBase< T > *sim)
Setter function for the sim pointer. As the module is usually registered during static initialization...
Definition GenericSubmodule.h:92
ChainManager< GenericSubmodule< T > > sub_module_chain
Definition GenericSubmodule.h:27
bool in_snapshot
Definition GenericSubmodule.h:38
std::shared_ptr< std::valarray< T > > module_field
Definition GenericSubmodule.h:29
virtual bool output()
Definition GenericSubmodule.h:71
bool is_set_up
Definition GenericSubmodule.h:42
virtual bool postNonLinIter()
Definition GenericSubmodule.h:70
bool in_output
Definition GenericSubmodule.h:39
void setSubmodules(std::vector< std::shared_ptr< GenericSubmodule< T > > > all_submodules)
Sets all the submodules of this module into their corresponding chains. Also sets their field-pointer...
Definition GenericSubmodule.h:99
std::vector< std::string > getSubmoduleList() const
Definition GenericSubmodule.h:77
bool isSetUp()
Definition GenericSubmodule.h:79
void overwriteLocalIniContent(const std::unordered_map< std::string, std::any > &global_ini_content)
Writes the (already trimmed to the local content) data from the global ini file to the local ini file...
Definition GenericSubmodule.h:158
virtual bool preTimeStep()
Definition GenericSubmodule.h:68
void writeToGlobalIniContent(std::ofstream &global_stream)
Function that writes the local ini file contents to the global ini file stream. "Tags" the keys with ...
Definition GenericSubmodule.h:140
void submodulesSetUp()
Definition GenericSubmodule.h:80
This class that manages parameter input through the default ini and user supplied ini files + CLI....
Definition InputManager.h:26
std::unordered_map< std::string, std::any > data()
Definition InputManager.h:44
void forceValueOverwrite(const std::string &key, const std::string &value)
Definition InputManager.cpp:63