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