MoCSI API Reference
Loading...
Searching...
No Matches
HeatConductivityTwoLayers.h
Go to the documentation of this file.
1#ifndef HEAT_CONDUCTIVITY_TWO_LAYERS_H
2#define HEAT_CONDUCTIVITY_TWO_LAYERS_H
3
4#define HEAT_CONDUCTIVITY_TWO_LAYERS_VERSION "6"
5
6#include <iostream>
7#include <memory>
8#include <string>
9#include <valarray>
10
11#include "../../src/GenericSubmodule.h"
12#include "../../src/ModuleFactory.h"
13
28template <typename T>
30{
31 private:
32 static bool m_registered;
33 std::string m_ini_filepath{"heat_conductivity/HeatConductivityTwoLayers.ini"};
34 std::valarray<T> m_temporary_field;
35 double m_transition_depth;
36 int m_transition_node;
37 int m_numerical_layers;
38 int m_upper_hc_on_node{0};
39
40 public:
42 bool setup(std::vector<std::shared_ptr<GenericSubmodule<T>>> all_submodules)
43 override; // loads module into necessary module chains (auto load for calculation
44 // chains)
45 bool exec(std::string_view param) override; // main functionality of the module
46
47 // different implementations for exec function, depending on module chain
48 bool init() override;
49 bool preTimeStep() override;
50 // bool postOuter() {};
51 bool postTimeStep() override;
52 // bool rheology(){};
53 // bool energy() {};
54 bool output() override;
55
57 bool floatingCompare(T f1, T f2, T epsilon);
58
60
61 void setFieldPtr(std::shared_ptr<std::valarray<T>> field_ptr) override
62 {
63 this->module_field = field_ptr;
64 }
65
66 // These static functions are needed for self registration
67 static std::shared_ptr<GenericSubmodule<T>> createMethode(SimulationClassBase<T>* sim)
68 {
69 return std::make_shared<HeatConductivityTwoLayers<T>>(sim);
70 }
71 // Replace this string with the module name for the registry
72 static std::string getName() { return "HeatConductivityTwoLayers"; }
73 std::string_view getNameLocal() const override { return "HeatConductivityTwoLayers"; };
74 std::vector<std::string> getDependencies() const override
75 {
76 return this->ini_file_data.getStringVectorParameters("dependencies");
77 };
78};
79
80// ================= Implementation =================
81
82template <typename T>
84 : GenericSubmodule<T>(sim)
85{
86 try
87 {
88 std::string ini_folder_path{
89 this->sim->m_simulation_config.getStringParameters("ini_folder_path")};
90 this->ini_file_data.loadUserInput(ini_folder_path + m_ini_filepath);
92 }
93 catch (const BadInput& e)
94 {
95 std::cerr << "[HeatConductivityTwoLayers]: " << e.what() << '\n';
96 throw BadInput(static_cast<std::string>("[HeatConductivityTwoLayers]: ")
97 + static_cast<std::string>(e.what()));
98 }
99}
100
101template <typename T>
103 std::vector<std::shared_ptr<GenericSubmodule<T>>> all_submodules)
104{
105 try
106 {
107 this->setSubmodules(all_submodules);
108 // This would also be set by the general options within the sim class:
109 m_numerical_layers = this->sim->m_simulation_config.getIntParameters("numerical_layers");
110 m_temporary_field.resize(
111 m_numerical_layers
112 * this->sim->m_simulation_config.getIntParameters("number_of_facets"));
113 m_transition_depth = this->ini_file_data.getDoubleParameters("transition_depth");
114 // If the upper heat conductivity value should be used when the transition depth is exactly
115 // on a node Set the parameter added to the transition node to 1.
116 if (this->ini_file_data.getBoolParameters("use_upper_hc_on_node"))
117 {
118 m_upper_hc_on_node = 1;
119 }
120 }
121 catch (const BadInput& e)
122 {
123 std::cerr << "[HeatConductivityTwoLayers]: " << e.what() << '\n';
124 throw BadInput(static_cast<std::string>("[HeatConductivityTwoLayers]: ")
125 + static_cast<std::string>(e.what()));
126 }
127 calculateTransitionRegion();
128 return true;
129}
130
131template <typename T>
132bool HeatConductivityTwoLayers<T>::exec(std::string_view param)
133{
134 if (param == "InitChain")
135 {
136 return init();
137 }
138 if (param == "PreTimeStepChain")
139 {
140 return preTimeStep();
141 }
142 if (param == "PostTimeStepChain")
143 {
144 return postTimeStep();
145 }
146 if (param == "OutputChain")
147 {
148 return output();
149 }
150 return false;
151}
152
158template <typename T>
160{
161 double sum{0.0};
162 const std::valarray<T>& cell_lengths{this->sim->getField("CellLength")};
163 if (m_transition_depth <= 0.0)
164 {
165 std::cerr
166 << "[HeatConductivityTwoLayers]: Negative or zero transition depth chosen. The model "
167 "will only represent the second layer heat capacity and no layering will occur.\n";
168 m_transition_node = -1;
169 return;
170 }
171 for (int i{0}; i < cell_lengths.size(); i++)
172 {
173 sum += cell_lengths[i];
174 if (floatingCompare(sum, m_transition_depth, 1e-8))
175 {
176 m_transition_node = i + m_upper_hc_on_node;
177 return;
178 }
179 else if (sum > m_transition_depth)
180 {
181 m_transition_node = i;
182 return;
183 }
184 }
185 std::cerr
186 << "[HeatConductivityTwoLayers]: Chosen transition depth larger than 1D domain size. The "
187 "model will only represent the first layer heat capacity and no layering will occur.\n";
188 m_transition_node = m_numerical_layers + 1;
189}
190
202template <typename T>
204{
205 if (std::abs(f1 - f2) < epsilon)
206 {
207 return true;
208 }
209 else
210 {
211 return std::abs(f1 - f2) < epsilon * std::max(std::abs(f1), std::abs(f2));
212 }
213}
214
215template <typename T>
217{
218 try
219 {
220 this->sub_module_chain.runSingleModuleInChain(
221 "PreTimeStepChain", this->ini_file_data.getStringVectorParameters("submodules")[0]);
222 // for( auto& heat_conductivity : (*this->module_field) )
223 m_temporary_field = *(this->module_field);
224 this->sub_module_chain.runSingleModuleInChain(
225 "PreTimeStepChain", this->ini_file_data.getStringVectorParameters("submodules")[1]);
226 }
227 catch (const BadInput& e)
228 {
229 std::cerr << "[HeatConductivityTwoLayers]: Problems with finding or running one or both of "
230 "the selected submodules!\n[HeatConductivityTwoLayers]: "
231 << e.what() << '\n';
232 throw BadInput(
233 static_cast<std::string>(
234 "[HeatConductivityTwoLayers]: Problems with finding or running one or both of the "
235 "selected submodules!\n[HeatConductivityTwoLayers]: ")
236 + static_cast<std::string>(e.what()));
237 }
238
239 for (int i{0}; i < m_temporary_field.size(); i++)
240 {
241 // Check if the layer within the current facet is above the transition region (in terms of
242 // depth)
243 if ((i % m_numerical_layers) <= m_transition_node)
244 {
245 (*this->module_field)[i] = m_temporary_field[i];
246 }
247 }
248}
249
250template <typename T>
252{
253 calculateHeatConductivity();
254 return true;
255}
256
257template <typename T>
259{
260 calculateHeatConductivity();
261 return true;
262}
263
264template <typename T>
266{
267 return true;
268}
269
270template <typename T>
272{
273 return true;
274}
275
276template <typename T>
278 ModuleFactory<T>::registerModule(getName(), createMethode);
279
280#endif // HEAT_CONDUCTIVITY_TWO_LAYERS_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 submodules. Submodules are below managing modules and will only be run by...
Definition GenericSubmodule.h:25
InputManager ini_file_data
Definition GenericSubmodule.h:36
std::vector< std::string > m_generic_submodules
Definition GenericSubmodule.h:34
const SimulationClassBase< T > * sim
Definition GenericSubmodule.h:32
std::shared_ptr< std::valarray< T > > module_field
Definition GenericSubmodule.h:29
Definition HeatConductivityTwoLayers.h:30
bool output() override
Definition HeatConductivityTwoLayers.h:271
void setFieldPtr(std::shared_ptr< std::valarray< T > > field_ptr) override
Definition HeatConductivityTwoLayers.h:61
std::vector< std::string > getDependencies() const override
Definition HeatConductivityTwoLayers.h:74
HeatConductivityTwoLayers(SimulationClassBase< T > *sim)
Definition HeatConductivityTwoLayers.h:83
static std::shared_ptr< GenericSubmodule< T > > createMethode(SimulationClassBase< T > *sim)
Definition HeatConductivityTwoLayers.h:67
bool setup(std::vector< std::shared_ptr< GenericSubmodule< T > > > all_submodules) override
Definition HeatConductivityTwoLayers.h:102
static std::string getName()
Definition HeatConductivityTwoLayers.h:72
bool postTimeStep() override
Definition HeatConductivityTwoLayers.h:265
bool exec(std::string_view param) override
Definition HeatConductivityTwoLayers.h:132
bool floatingCompare(T f1, T f2, T epsilon)
Equals comparison for two floating point numbers.
Definition HeatConductivityTwoLayers.h:203
bool preTimeStep() override
Definition HeatConductivityTwoLayers.h:258
bool init() override
Definition HeatConductivityTwoLayers.h:251
void calculateTransitionRegion()
Calculates the node after which the layer changes.
Definition HeatConductivityTwoLayers.h:159
std::string_view getNameLocal() const override
Definition HeatConductivityTwoLayers.h:73
void calculateHeatConductivity()
Definition HeatConductivityTwoLayers.h:216
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