/home/runner/work/DiFfRG_current/DiFfRG_current/DiFfRG/include/DiFfRG/timestepping/timestep_control/default.hh Source File#

DiFfRG: /home/runner/work/DiFfRG_current/DiFfRG_current/DiFfRG/include/DiFfRG/timestepping/timestep_control/default.hh Source File
DiFfRG
Discretization Framework for functional Renormalization Group flows
default.hh
Go to the documentation of this file.
1#pragma once
2
3// standard library
4#include <iomanip>
5#include <sstream>
6
8
9namespace DiFfRG
10{
19 template <typename NEWT> class TC_Default
20 {
21 public:
22 TC_Default(NEWT &newton_, unsigned int alg_order_, double t_, double max_t_, double dt_, double min_dt_,
23 double max_dt_, double output_dt_, ReportPort log = {})
24 : newton(newton_), alg_order(alg_order_), t(t_), max_t(max_t_), sug_dt(dt_), min_dt(min_dt_), max_dt(max_dt_),
25 output_dt(output_dt_), cur_dt(sug_dt), last_save(t), last_t(t_), stuck(0), fin(false), log(std::move(log))
26 {
27 }
28
29 double get_dt() const { return cur_dt; }
30 double get_t() const { return t; }
34 unsigned int get_stuck() const { return stuck; }
35 bool finished() const { return fin; }
36
43 template <typename F, typename OF> void advance(F &f, OF &of)
44 {
45 // first, try to do the timestep
46 try {
47 f(t, cur_dt);
48 // on a success, possibly amend dt
49 this->step_success();
50 } catch (const std::exception &e) {
51 // on a fail, possibly amend dt
52 this->step_fail(e);
53 }
54
55 // check if the stepper got stuck on some time t
56 if (is_close(t, last_t))
57 stuck++;
58 else {
59 stuck = 0;
60 last_t = t;
61 }
62
63 // check if the stepper should output data
64 if (t > last_save + output_dt) {
65 of(t);
66 last_save = int(t / output_dt) * output_dt;
67 }
68
69 // should the suggested timestep be below the minimum timestep, tell the solver that it should
70 // accept even timesteps which do not fulfill the convergence criterion (otherwise the evolution would just stop)
71 newton.set_ignore_nonconv(false);
72 if (sug_dt < min_dt) {
73 cur_dt = min_dt;
74 newton.set_ignore_nonconv(true);
75 } else if (sug_dt > max_dt)
76 cur_dt = max_dt;
77 else
78 cur_dt = sug_dt;
79
80 // do not over-step the final time
81 if (t + cur_dt > max_t) cur_dt = max_t - t;
82
83 // if we are over max_t, the timestepping is finished
84 if (t - 1e-4 * cur_dt >= max_t) {
85 of(t);
86 fin = true;
87 }
88 // if we got unrecoverably stuck also abort the timestepping
89 if (stuck > 10) {
90 of(t);
91 fin = true;
92 log.error("Timestepping got stuck at t = {}", t);
93 }
94 }
95
97 {
98 std::ostringstream message;
99 message << std::setw(16) << "| step from t = " << std::setw(8) << std::setprecision(5) << last_t << std::setw(8)
100 << " to t = ";
101 }
102
103 protected:
108 virtual void step_success()
109 {
110 t += cur_dt;
111 if (t + sug_dt - 1e-4 * sug_dt > last_save + output_dt)
113 else
114 cur_dt = sug_dt;
115 }
119 virtual void step_fail(const std::exception &e)
120 {
121 throw std::runtime_error("Timestepping failed. Error: \n" + std::string(e.what()));
122 }
123
124 NEWT &newton;
125 unsigned int alg_order;
127 unsigned int stuck;
128
129 bool fin;
131 };
132} // namespace DiFfRG
Definition run_reporter.hh:96
void error(spdlog::format_string_t< Args... > format, Args &&...args) const
Definition run_reporter.hh:111
This is a default time controller implementation which should be used as a base class for any other t...
Definition default.hh:20
bool finished() const
Definition default.hh:35
unsigned int alg_order
Definition default.hh:125
double get_t() const
Definition default.hh:30
double output_dt
Definition default.hh:126
bool fin
Definition default.hh:129
unsigned int stuck
Definition default.hh:127
double max_t
Definition default.hh:126
double last_t
Definition default.hh:126
double last_save
Definition default.hh:126
double get_dt() const
Definition default.hh:29
double min_dt
Definition default.hh:126
TC_Default(NEWT &newton_, unsigned int alg_order_, double t_, double max_t_, double dt_, double min_dt_, double max_dt_, double output_dt_, ReportPort log={})
Definition default.hh:22
void print_step_info()
Definition default.hh:96
double sug_dt
Definition default.hh:126
double cur_dt
Definition default.hh:126
unsigned int get_stuck() const
Get how many times in succession the timestepper was at the same time step.
Definition default.hh:34
ReportPort log
Definition default.hh:130
virtual void step_success()
The default implementation of step_success does nothing except incrementing time and trying to plan t...
Definition default.hh:108
NEWT & newton
Definition default.hh:124
double t
Definition default.hh:126
virtual void step_fail(const std::exception &e)
The default implementation of step_fail immediately aborts the program.
Definition default.hh:119
double max_dt
Definition default.hh:126
void advance(F &f, OF &of)
Method to perform a single time step.
Definition default.hh:43
Definition complex_math.hh:10
bool KOKKOS_INLINE_FUNCTION is_close(T1 a, T2 b, T3 eps_)
Function to evaluate whether two floats are equal to numerical precision. Tests for both relative and...
Definition math.hh:177