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 VERSION "4"
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 << e.what() << '\n';
96 }
97}
98
99template <typename T>
101 std::vector<std::shared_ptr<GenericSubmodule<T>>> all_submodules)
102{
103 this->setSubmodules(all_submodules);
104 // This would also be set by the general options within the sim class:
105 m_numerical_layers = this->sim->m_simulation_config.getIntParameters("numerical_layers");
106 m_temporary_field.resize(m_numerical_layers
107 * this->sim->m_simulation_config.getIntParameters("number_of_facets"));
108 m_transition_depth = this->ini_file_data.getDoubleParameters("transition_depth");
109 // If the upper heat conductivity value should be used when the transition depth is exactly on a
110 // node Set the parameter added to the transition node to 1.
111 if (this->ini_file_data.getBoolParameters("use_upper_hc_on_node"))
112 {
113 m_upper_hc_on_node = 1;
114 }
115 calculateTransitionRegion();
116 return true;
117}
118
119template <typename T>
120bool HeatConductivityTwoLayers<T>::exec(std::string_view param)
121{
122 if (param == "InitChain")
123 {
124 return init();
125 }
126 if (param == "PreTimeStepChain")
127 {
128 return preTimeStep();
129 }
130 if (param == "PostTimeStepChain")
131 {
132 return postTimeStep();
133 }
134 if (param == "OutputChain")
135 {
136 return output();
137 }
138 return false;
139}
140
146template <typename T>
148{
149 double sum{0.0};
150 const std::valarray<T>& cell_lengths{this->sim->getField("CellLength")};
151 if (m_transition_depth <= 0.0)
152 {
153 std::cerr
154 << "[HeatConductivityTwoLayers] Negative or zero transition depth chosen. The model "
155 "will only represent the second layer heat capacity and no layering will occur.\n";
156 m_transition_node = -1;
157 return;
158 }
159 for (int i{0}; i < cell_lengths.size(); i++)
160 {
161 sum += cell_lengths[i];
162 if (floatingCompare(sum, m_transition_depth, 1e-8))
163 {
164 m_transition_node = i + m_upper_hc_on_node;
165 return;
166 }
167 else if (sum > m_transition_depth)
168 {
169 m_transition_node = i;
170 return;
171 }
172 }
173 std::cerr
174 << "[HeatConductivityTwoLayers] Chosen transition depth larger than 1D domain size. The "
175 "model will only represent the first layer heat capacity and no layering will occur.\n";
176 m_transition_node = m_numerical_layers + 1;
177}
178
190template <typename T>
192{
193 if (std::abs(f1 - f2) < epsilon)
194 {
195 return true;
196 }
197 else
198 {
199 return std::abs(f1 - f2) < epsilon * std::max(std::abs(f1), std::abs(f2));
200 }
201}
202
203template <typename T>
205{
206 this->sub_module_chain.runSingleModuleInChain(
207 "PreTimeStepChain", this->ini_file_data.getStringVectorParameters("submodules")[0]);
208 // for( auto& heat_conductivity : (*this->module_field) )
209 m_temporary_field = *(this->module_field);
210 this->sub_module_chain.runSingleModuleInChain(
211 "PreTimeStepChain", this->ini_file_data.getStringVectorParameters("submodules")[1]);
212 for (int i{0}; i < m_temporary_field.size(); i++)
213 {
214 // Check if the layer within the current facet is above the transition region (in terms of
215 // depth)
216 if ((i % m_numerical_layers) <= m_transition_node)
217 {
218 (*this->module_field)[i] = m_temporary_field[i];
219 }
220 }
221}
222
223template <typename T>
225{
226 std::cout << "I am the init function of the HeatConductivityTwoLayers!\n";
227 calculateHeatConductivity();
228 return true;
229}
230
231template <typename T>
233{
234 std::cout << "I am the preTS function of the HeatConductivityTwoLayers!\n";
235 calculateHeatConductivity();
236 return true;
237}
238
239template <typename T>
241{
242 std::cout << "I am the postTS function of the HeatConductivityTwoLayers!\n";
243 return true;
244}
245
246template <typename T>
248{
249 std::cout << "I am the output function of the HeatConductivityTwoLayers!\n";
250 return true;
251}
252
253template <typename T>
255 ModuleFactory<T>::registerModule(getName(), createMethode);
256
257#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:247
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:100
static std::string getName()
Definition HeatConductivityTwoLayers.h:72
bool postTimeStep() override
Definition HeatConductivityTwoLayers.h:240
bool exec(std::string_view param) override
Definition HeatConductivityTwoLayers.h:120
bool floatingCompare(T f1, T f2, T epsilon)
Equals comparison for two floating point numbers.
Definition HeatConductivityTwoLayers.h:191
bool preTimeStep() override
Definition HeatConductivityTwoLayers.h:232
bool init() override
Definition HeatConductivityTwoLayers.h:224
void calculateTransitionRegion()
Calculates the node after which the layer changes.
Definition HeatConductivityTwoLayers.h:147
std::string_view getNameLocal() const override
Definition HeatConductivityTwoLayers.h:73
void calculateHeatConductivity()
Definition HeatConductivityTwoLayers.h:204
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
Definition SimulationClassBase.h:15
InputManager m_simulation_config
Definition SimulationClassBase.h:24