MoCSI API Reference
Loading...
Searching...
No Matches
FluxSmoothingTool.h
Go to the documentation of this file.
1#ifndef FLUX_SMOOTHING_TOOL_H
2#define FLUX_SMOOTHING_TOOL_H
3
4#define FLUX_SMOOTHING_TOOL_VERSION "7"
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
40template <typename T>
42{
43 private:
44 static bool m_registered;
45 std::string m_ini_filepath{"tools/FluxSmoothingTool.ini"};
46 std::valarray<T> m_temp_previous_timestep;
47 T m_initial_dt;
48 int m_smoothing_counter{0};
49 T m_upper_threshold{};
50 T m_lower_threshold{};
51 bool m_flux_smoothing_active{false};
52 bool m_pre_iter{true};
53
54 void fluxSmoothing();
55
56 public:
58 bool setup(std::vector<std::shared_ptr<GenericSubmodule<T>>> all_submodules) override;
59 bool exec(std::string_view param) override; // main functionality of the module
60
61 bool init() override { return true; };
62 bool preTimeStep() override;
63 bool postNonLinIter() override { return true; };
64 bool postTimeStep() override;
65 bool output() override { return true; };
66 std::vector<std::string> getChainInsertion() const override
67 {
68 return this->ini_file_data.getStringVectorParameters("chain_insertion");
69 };
70
71 static std::shared_ptr<GenericManagingModule<T>> createMethode(SimulationClassBase<T>* sim)
72 {
73 return std::make_shared<FluxSmoothingTool<T>>(sim);
74 };
75 static std::string getName() { return "FluxSmoothingTool"; };
76 std::string_view getNameLocal() const override { return "FluxSmoothingTool"; };
77};
78
79// ================= Implementation =================
80
81template <typename T>
83{
84 try
85 {
86 std::string ini_folder_path{
87 this->sim->m_simulation_config.getStringParameters("ini_folder_path")};
88 this->ini_file_data.loadUserInput(ini_folder_path + m_ini_filepath);
90 }
91 catch (const BadInput& e)
92 {
93 std::cerr << "[FluxSmoothingTool]: " << e.what() << '\n';
94 throw BadInput(static_cast<std::string>("[FluxSmoothingTool]: ")
95 + static_cast<std::string>(e.what()));
96 }
97}
98
99template <typename T>
101{
102 try
103 {
104 this->m_generic_submodules = this->ini_file_data.getStringVectorParameters("submodules");
105 this->setSubmodules(all_submodules);
106 m_upper_threshold = this->ini_file_data.getDoubleParameters("upper_threshold");
107 m_lower_threshold = this->ini_file_data.getDoubleParameters("lower_threshold");
108 m_initial_dt = this->sim->m_simulation_config.getDoubleParameters("time_delta");
109 }
110 catch (const BadInput& e)
111 {
112 std::cerr << "[FluxSmoothingTool]: " << e.what() << '\n';
113 throw BadInput(static_cast<std::string>("[FluxSmoothingTool]: ")
114 + static_cast<std::string>(e.what()));
115 }
116 return true;
117}
118
119template <typename T>
120bool FluxSmoothingTool<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 == "PostNonLinIterChain")
131 {
132 return postNonLinIter();
133 }
134 if (param == "PostTimeStepChain")
135 {
136 return postTimeStep();
137 }
138 if (param == "OutputChain")
139 {
140 return output();
141 }
142 return false;
143}
144
145template <typename T>
147{
148 // Get the previous temp in the step here. Since this is within the inner iteration loop
149 // (iterations within one time step for non-isothermal/non-linear parameters), we only want to
150 // grab the first one
151 if (m_pre_iter)
152 {
153 m_temp_previous_timestep = this->sim->getField("Temperature");
154 m_pre_iter = false;
155 }
156 return true;
157}
158
159template <typename T>
161{
162 fluxSmoothing();
163 return true;
164}
165
166template <typename T>
168 ModuleFactory<T>::registerModule(getName(), createMethode);
169
176template <typename T>
178{
179 // Reset pre_iter bool here, as this is after the inner iteration loop (iterations within one
180 // time step for non-isothermal/non-linear parameters)
181 m_pre_iter = true;
182 if (!m_flux_smoothing_active
183 && (this->sim->getFieldValue("Temperature", 0)
184 > m_upper_threshold * m_temp_previous_timestep[0]
185 || this->sim->getFieldValue("Temperature", 0)
186 < m_lower_threshold * m_temp_previous_timestep[0]))
187 {
188 if (m_flux_smoothing_active)
189 {
190 throw std::runtime_error(
191 "Double smoothing needed, which is not implemented! Please choose a smaller "
192 "timestep.");
193 }
194 this->sim->m_field_map["Temperature"] = m_temp_previous_timestep;
195 this->sim->m_simulation_config.forceValueOverwrite("time_delta",
196 std::to_string(m_initial_dt / 5));
197 this->sim->elapsed_time -= m_initial_dt;
198 this->sim->time_step =
199 (this->sim->time_step - 1)
200 * 5; // Map the time steps on the smaller time delta and reset to previous step
201 m_flux_smoothing_active = true;
202 m_smoothing_counter = 0;
203 }
204
205 if (m_flux_smoothing_active)
206 {
207 if (m_smoothing_counter == 5)
208 {
209 this->sim->m_simulation_config.forceValueOverwrite("time_delta",
210 std::to_string(m_initial_dt));
211 this->sim->time_step /= 5;
212 m_flux_smoothing_active = false;
213 m_smoothing_counter = 0;
214 }
215 m_smoothing_counter++;
216 }
217}
218
219#endif // FLUX_SMOOTHING_TOOL_H
This error class inherits from std::exception and marks faulty parameter or CL inputs....
Definition IniParser.h:71
Concrete implementation of a matrix class representing a Compressed Sparse Rows (CSR) matrix....
Definition CsrMatrix.h:37
Managing module that adaptively reduces the timestep to limit excessive temperature changes.
Definition FluxSmoothingTool.h:42
std::vector< std::string > getChainInsertion() const override
Definition FluxSmoothingTool.h:66
bool init() override
Definition FluxSmoothingTool.h:61
bool preTimeStep() override
Definition FluxSmoothingTool.h:146
static std::string getName()
Definition FluxSmoothingTool.h:75
FluxSmoothingTool(SimulationClassBase< T > *sim)
Definition FluxSmoothingTool.h:82
bool postTimeStep() override
Definition FluxSmoothingTool.h:160
bool setup(std::vector< std::shared_ptr< GenericSubmodule< T > > > all_submodules) override
Definition FluxSmoothingTool.h:100
static std::shared_ptr< GenericManagingModule< T > > createMethode(SimulationClassBase< T > *sim)
Definition FluxSmoothingTool.h:71
bool postNonLinIter() override
Definition FluxSmoothingTool.h:63
std::string_view getNameLocal() const override
Definition FluxSmoothingTool.h:76
bool output() override
Definition FluxSmoothingTool.h:65
bool exec(std::string_view param) override
Definition FluxSmoothingTool.h:120
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
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
static constexpr bool registerModule(std::string name, creation_method module) noexcept
Function that adds a module to the module registry map.
Definition ModuleFactory.h:76