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 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
39template <typename T>
41{
42 private:
43 static bool m_registered;
44 std::string m_ini_filepath{"tools/FluxSmoothingTool.ini"};
45 std::valarray<T> m_temp_previous_timestep;
46 T m_initial_dt;
47 int m_smoothing_counter{0};
48 T m_upper_threshold{};
49 T m_lower_threshold{};
50 bool m_flux_smoothing_active{false};
51 bool m_pre_iter{true};
52
53 void fluxSmoothing();
54
55 public:
57 bool setup(std::vector<std::shared_ptr<GenericSubmodule<T>>> all_submodules) override;
58 bool exec(std::string_view param) override; // main functionality of the module
59
60 bool init() override { return true; };
61 bool preTimeStep() override;
62 bool postNonLinIter() override { return true; };
63 bool postTimeStep() override;
64 bool output() override { return true; };
65 std::vector<std::string> getChainInsertion() const override
66 {
67 return this->ini_file_data.getStringVectorParameters("chain_insertion");
68 };
69
70 static std::shared_ptr<GenericManagingModule<T>> createMethode(SimulationClassBase<T>* sim)
71 {
72 return std::make_shared<FluxSmoothingTool<T>>(sim);
73 };
74 static std::string getName() { return "FluxSmoothingTool"; };
75 std::string_view getNameLocal() const override { return "FluxSmoothingTool"; };
76};
77
78// ================= Implementation =================
79
80template <typename T>
82{
83 try
84 {
85 std::string ini_folder_path{
86 this->sim->m_simulation_config.getStringParameters("ini_folder_path")};
87 this->ini_file_data.loadUserInput(ini_folder_path + m_ini_filepath);
89 }
90 catch (const BadInput& e)
91 {
92 std::cerr << e.what() << '\n';
93 }
94}
95
96template <typename T>
97bool FluxSmoothingTool<T>::setup(std::vector<std::shared_ptr<GenericSubmodule<T>>> all_submodules)
98{
99 this->m_generic_submodules = this->ini_file_data.getStringVectorParameters("submodules");
100 this->setSubmodules(all_submodules);
101 m_upper_threshold = this->ini_file_data.getDoubleParameters("upper_threshold");
102 m_lower_threshold = this->ini_file_data.getDoubleParameters("lower_threshold");
103 m_initial_dt = this->sim->m_simulation_config.getDoubleParameters("time_delta");
104
105 return true;
106}
107
108template <typename T>
109bool FluxSmoothingTool<T>::exec(std::string_view param)
110{
111 if (param == "InitChain")
112 {
113 return init();
114 }
115 if (param == "PreTimeStepChain")
116 {
117 return preTimeStep();
118 }
119 if (param == "PostNonLinIterChain")
120 {
121 return postNonLinIter();
122 }
123 if (param == "PostTimeStepChain")
124 {
125 return postTimeStep();
126 }
127 if (param == "OutputChain")
128 {
129 return output();
130 }
131 return false;
132}
133
134template <typename T>
136{
137 // Get the previous temp in the step here. Since this is within the inner iteration loop
138 // (iterations within one time step for non-isothermal/non-linear parameters), we only want to
139 // grab the first one
140 if (m_pre_iter)
141 {
142 m_temp_previous_timestep = this->sim->getField("Temperature");
143 m_pre_iter = false;
144 }
145 return true;
146}
147
148template <typename T>
150{
151 fluxSmoothing();
152 return true;
153}
154
155template <typename T>
157 ModuleFactory<T>::registerModule(getName(), createMethode);
158
165template <typename T>
167{
168 // Reset pre_iter bool here, as this is after the inner iteration loop (iterations within one
169 // time step for non-isothermal/non-linear parameters)
170 m_pre_iter = true;
171 if (!m_flux_smoothing_active
172 && (this->sim->getFieldValue("Temperature", 0)
173 > m_upper_threshold * m_temp_previous_timestep[0]
174 || this->sim->getFieldValue("Temperature", 0)
175 < m_lower_threshold * m_temp_previous_timestep[0]))
176 {
177 if (m_flux_smoothing_active)
178 {
179 throw std::runtime_error(
180 "Double smoothing needed, which is not implemented! Please choose a smaller "
181 "timestep.");
182 }
183 this->sim->m_field_map["Temperature"] = m_temp_previous_timestep;
184 this->sim->m_simulation_config.forceValueOverwrite("time_delta",
185 std::to_string(m_initial_dt / 5));
186 this->sim->elapsed_time -= m_initial_dt;
187 this->sim->time_step =
188 (this->sim->time_step - 1)
189 * 5; // Map the time steps on the smaller time delta and reset to previous step
190 m_flux_smoothing_active = true;
191 m_smoothing_counter = 0;
192 }
193
194 if (m_flux_smoothing_active)
195 {
196 if (m_smoothing_counter == 5)
197 {
198 this->sim->m_simulation_config.forceValueOverwrite("time_delta",
199 std::to_string(m_initial_dt));
200 this->sim->time_step /= 5;
201 m_flux_smoothing_active = false;
202 m_smoothing_counter = 0;
203 }
204 m_smoothing_counter++;
205 }
206}
207
208#endif // FLUX_SMOOTHING_TOOL_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 adaptively reduces the timestep to limit excessive temperature changes.
Definition FluxSmoothingTool.h:41
std::vector< std::string > getChainInsertion() const override
Definition FluxSmoothingTool.h:65
bool init() override
Definition FluxSmoothingTool.h:60
bool preTimeStep() override
Definition FluxSmoothingTool.h:135
static std::string getName()
Definition FluxSmoothingTool.h:74
FluxSmoothingTool(SimulationClassBase< T > *sim)
Definition FluxSmoothingTool.h:81
bool postTimeStep() override
Definition FluxSmoothingTool.h:149
bool setup(std::vector< std::shared_ptr< GenericSubmodule< T > > > all_submodules) override
Definition FluxSmoothingTool.h:97
static std::shared_ptr< GenericManagingModule< T > > createMethode(SimulationClassBase< T > *sim)
Definition FluxSmoothingTool.h:70
bool postNonLinIter() override
Definition FluxSmoothingTool.h:62
std::string_view getNameLocal() const override
Definition FluxSmoothingTool.h:75
bool output() override
Definition FluxSmoothingTool.h:64
bool exec(std::string_view param) override
Definition FluxSmoothingTool.h:109
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: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