MoCSI API Reference
Loading...
Searching...
No Matches
RuntimeProgressTool.h
Go to the documentation of this file.
1#ifndef RUNTIME_PROGRESS_TOOL_H
2#define RUNTIME_PROGRESS_TOOL_H
3
4#define VERSION "5"
5
6#include <chrono>
7#include <iostream>
8#include <memory>
9#include <string>
10#include <valarray>
11
12#include "../../src/GenericManagingModule.h"
13#include "../../src/ModuleFactory.h"
14
39template <typename T>
41{
42 private:
43 static bool m_registered;
44 int iteration_interval;
45 int total_time_steps;
46 std::string m_ini_filepath{"tools/RuntimeProgressTool.ini"};
47 std::chrono::time_point<std::chrono::high_resolution_clock> start_time;
48 std::chrono::time_point<std::chrono::high_resolution_clock> last_call_time;
49
50 void _calculate_iteration_time();
51
52 public:
54 bool setup(std::vector<std::shared_ptr<GenericSubmodule<T>>> all_submodules) override;
55 bool exec(std::string_view param) override; // main functionality of the module
56
57 bool init() override { return true; };
58 bool preTimeStep() override;
59 bool postNonLinIter() override { return true; };
60 bool postTimeStep() override;
61 bool output() override { return true; };
62
63 void checkProgress();
64
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<RuntimeProgressTool<T>>(sim);
73 };
74 static std::string getName() { return "RuntimeProgressTool"; };
75 std::string_view getNameLocal() const override { return "RuntimeProgressTool"; };
76};
77
78// ================= Implementation =================
79
80template <typename T>
82 : GenericManagingModule<T>(sim)
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 << e.what() << '\n';
94 }
95}
96
97template <typename T>
98bool RuntimeProgressTool<T>::setup(std::vector<std::shared_ptr<GenericSubmodule<T>>> all_submodules)
99{
100 this->setSubmodules(all_submodules);
101 iteration_interval = this->ini_file_data.getDoubleParameters("iteration_interval");
102 total_time_steps =
103 std::ceil(this->sim->m_simulation_config.getDoubleParameters("time_max")
104 / this->sim->m_simulation_config.getDoubleParameters("time_delta"));
105 start_time = std::chrono::high_resolution_clock::now();
106 last_call_time = start_time;
107
108 return true;
109}
110
111template <typename T>
112bool RuntimeProgressTool<T>::exec(std::string_view param)
113{
114 if (param == "InitChain")
115 {
116 return init();
117 }
118 if (param == "PreTimeStepChain")
119 {
120 return preTimeStep();
121 }
122 if (param == "PostNonLinIterChain")
123 {
124 return postNonLinIter();
125 }
126 if (param == "PostTimeStepChain")
127 {
128 return postTimeStep();
129 }
130 if (param == "OutputChain")
131 {
132 return output();
133 }
134 return false;
135}
136
141template <typename T>
143{
144 // Using milliseconds as seconds can too quickly lead to time intervals of 0
145 // That's why multiplication by 1000 is done to still get the iterations per seconds.
146 std::chrono::time_point current_time{std::chrono::high_resolution_clock::now()};
147 std::chrono::duration passed_time_since_start{
148 std::chrono::duration_cast<std::chrono::milliseconds>(current_time - start_time)};
149 std::chrono::duration passed_time_since_last_call{
150 std::chrono::duration_cast<std::chrono::milliseconds>(current_time - last_call_time)};
151 // I'm using the ANSI escape code for color here, to highlight that this is a status related
152 // message and not outputs or errors. This only works on terminals supporting these escape
153 // codes.
154 std::cout << "\033[32m[RuntimeProgressTool] Time steps: " << this->sim->time_step << "/"
155 << total_time_steps << "\033[0m\n";
156
157 if (passed_time_since_last_call.count() == 0)
158 {
159 // Time intervall chosen to small for the current simulation.
160 }
161 else
162 {
163 std::cout << "\033[32m[RuntimeProgress] Simulation speed of "
164 << (iteration_interval * 1000 / passed_time_since_last_call.count())
165 << " it/s for the last interval. Simulation speed of "
166 << (this->sim->time_step * 1000 / passed_time_since_start.count())
167 << " it/s since start.\033[0m\n";
168 }
169 last_call_time = current_time;
170}
171
172template <typename T>
174{
175 if (this->sim->time_step % iteration_interval == 0)
176 {
177 _calculate_iteration_time();
178 }
179}
180
181template <typename T>
183{
184 checkProgress();
185 return true;
186}
187
188template <typename T>
190{
191 checkProgress();
192 return true;
193}
194
195template <typename T>
197 ModuleFactory<T>::registerModule(getName(), createMethode);
198
199#endif // RUNTIME_PROGRESS_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
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
Monitors and reports the simulation progress and iterations per second.
Definition RuntimeProgressTool.h:41
bool init() override
Definition RuntimeProgressTool.h:57
bool postNonLinIter() override
Definition RuntimeProgressTool.h:59
bool output() override
Definition RuntimeProgressTool.h:61
bool setup(std::vector< std::shared_ptr< GenericSubmodule< T > > > all_submodules) override
Definition RuntimeProgressTool.h:98
bool postTimeStep() override
Definition RuntimeProgressTool.h:189
RuntimeProgressTool(SimulationClassBase< T > *sim)
Definition RuntimeProgressTool.h:81
static std::shared_ptr< GenericManagingModule< T > > createMethode(SimulationClassBase< T > *sim)
Definition RuntimeProgressTool.h:70
std::string_view getNameLocal() const override
Definition RuntimeProgressTool.h:75
std::vector< std::string > getChainInsertion() const override
Definition RuntimeProgressTool.h:65
bool exec(std::string_view param) override
Definition RuntimeProgressTool.h:112
static std::string getName()
Definition RuntimeProgressTool.h:74
bool preTimeStep() override
Definition RuntimeProgressTool.h:182
void checkProgress()
Definition RuntimeProgressTool.h:173
Definition SimulationClassBase.h:15
InputManager m_simulation_config
Definition SimulationClassBase.h:24