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

DiFfRG: /home/runner/work/DiFfRG_current/DiFfRG_current/DiFfRG/include/DiFfRG/timestepping/abstract_timestepper.hh Source File
DiFfRG
Discretization Framework for functional Renormalization Group flows
abstract_timestepper.hh
Go to the documentation of this file.
1#pragma once
2
3// DiFfRG
12#include <algorithm>
13#include <chrono>
14#include <cmath>
15#include <cstddef>
16#include <limits>
17#include <optional>
18#include <vector>
19
20namespace DiFfRG
21{
23 types::global_dof_index dof = dealii::numbers::invalid_dof_index;
24 double value = 0.;
26 double error_weight = 0.;
27 double contribution = 0.;
28 };
29
38 struct CalcDtTimer {
39 std::chrono::high_resolution_clock::time_point mark = std::chrono::high_resolution_clock::now();
40
41 double lap()
42 {
43 const auto now = std::chrono::high_resolution_clock::now();
44 const double ms = double(std::chrono::duration_cast<std::chrono::milliseconds>(now - mark).count());
45 mark = now;
46 return ms;
47 }
48 };
49
51 double t = 0.;
52 double k = std::numeric_limits<double>::quiet_NaN();
53 long int reject_delta = 0;
54 long int total_rejects = 0;
55 long int ida_steps = 0;
56 double ida_last_step_size = 0.;
58 double ida_current_time = 0.;
59 double wrms = 0.;
60 std::vector<IDAErrorDofRecord> top_dofs;
61 };
62
64 long int ida_steps = 0;
70 double ida_last_step_size = 0.;
72 double ida_current_time = 0.;
73 void append_to(ProgressEvent &event) const
74 {
75 event.field("h", ida_current_step_size, 2)
76 .field("last_h", ida_last_step_size, 2)
77 .field("steps", ida_steps, 2)
78 .field("rejects", ida_error_test_failures, 2)
79 .field("nl_fail", ida_nonlinear_convergence_failures, 2);
80 }
81 };
82
89
91 bool has_failures() const
92 {
94 }
95 void append_to(ProgressEvent &event) const
96 {
97 if (!has_failures()) return;
98 event.field("nan", nonfinite_failures(), 2)
99 .field("res_exc", residual_exceptions, 2)
100 .field("jac_fail", jacobian_failures, 2)
101 .field("lin_fail", linear_solver_failures, 2);
102 }
103 };
104
106 std::optional<IDAProgressDiagnostics> ida;
108
109 void append_to(ProgressEvent &event) const
110 {
111 if (ida) ida->append_to(event);
112 callbacks.append_to(event);
113 }
114 bool empty() const { return !ida && !callbacks.has_failures(); }
115 };
116
154 template <typename VectorType_, typename SparseMatrixType_, uint dim_> class AbstractTimestepper
155 {
156 protected:
157 static constexpr uint dim = dim_;
158 using VectorType = VectorType_;
160 using SparseMatrixType = SparseMatrixType_;
161 // NOTE: InverseSparseMatrixType is deliberately NOT declared here. A member alias of a
162 // class template is instantiated with the class, so declaring it in the common base forces
163 // every timestepper to have a mass-matrix inverse type -- including the implicit ones,
164 // which never invert a mass matrix. That made the whole hierarchy unusable with any matrix
165 // type lacking a SparseDirectUMFPACK-shaped inverse (i.e. every distributed matrix). It now
166 // lives only in the explicit steppers that actually use it.
168 "VectorType is not a vector type DiFfRG supports; see get_type in common/types.hh.");
170
171 public:
184 OutputSession_impl<dim, VectorType> &data_out, const bool implicit_stepper,
185 const bool explicit_stepper)
187 log(data_out.report_port()), m_is_implicit(implicit_stepper), m_is_explicit(explicit_stepper)
188 {
190 }
191
200
201 private:
203 {
204 output_dt = config.get_double("/timestepping/output_dt", 1e-1);
205
206 if (m_is_implicit) {
207 // Stuff you should really set
208 impl.abs_tol = config.get_double_or_warn("/timestepping/implicit/abs_tol", 1e-13);
209 impl.rel_tol = config.get_double_or_warn("/timestepping/implicit/rel_tol", 1e-7);
210
211 // Stuff you can set, but defaults are reasonable
212 impl.dt = config.get_double("/timestepping/implicit/dt", 1e-4);
213 impl.minimal_dt = config.get_double("/timestepping/implicit/minimal_dt", 1e-8);
214 impl.maximal_dt = config.get_double("/timestepping/implicit/maximal_dt", 1.);
215 impl.max_steps = config.get_uint("/timestepping/implicit/max_steps", 1e6);
216 impl.max_non_linear_iterations = config.get_uint("/timestepping/implicit/max_non_linear_iterations", 10);
217 impl.jacobian_diagnostics = config.get_bool("/timestepping/implicit/jacobian_diagnostics", false);
218 impl.ida_callback_trace = config.get_bool("/timestepping/implicit/ida_callback_trace", false);
219 impl.ida_callback_trace_min_t = config.get_double("/timestepping/implicit/ida_callback_trace_min_t", 0.0);
220 impl.ida_callback_trace_max_lines = config.get_uint("/timestepping/implicit/ida_callback_trace_max_lines", 200);
222 config.get_bool("/timestepping/implicit/ida_callback_trace_successes", false);
223 impl.ida_error_dof_diagnostics = config.get_bool("/timestepping/implicit/ida_error_dof_diagnostics", false);
225 config.get_uint("/timestepping/implicit/ida_error_dof_diagnostics_top_n", 8);
226
227 // Sanity checks:
228 if (impl.minimal_dt <= 0.0) throw std::invalid_argument("Minimal timestep size must be positive.");
229 if (impl.maximal_dt <= 0.0) throw std::invalid_argument("Maximal timestep size must be positive.");
231 throw std::invalid_argument("Minimal timestep size must be smaller than maximal timestep size.");
233 throw std::invalid_argument("Initial timestep size must be within the minimal and maximal timestep size.");
234 if (impl.abs_tol <= 0.0) throw std::invalid_argument("Absolute tolerance must be > 0.");
235 if (impl.rel_tol <= 0.0) throw std::invalid_argument("Relative tolerance must be > 0.");
236 }
237
238 if (m_is_explicit) {
239 expl.dt = config.get_double_or_warn("/timestepping/explicit/dt", 1e-2);
240 expl.minimal_dt = config.get_double("/timestepping/explicit/minimal_dt", 1e-16);
241 expl.maximal_dt = config.get_double("/timestepping/explicit/maximal_dt", 1e16);
242 expl.abs_tol = config.get_double_or_warn("/timestepping/explicit/abs_tol", 1e-3);
243 expl.rel_tol = config.get_double_or_warn("/timestepping/explicit/rel_tol", 1e-3);
244 expl.detect_stuck = config.get_bool("/timestepping/explicit/detect_stuck", true);
245
246 // Sanity checks:
247 if (expl.minimal_dt <= 0.0) throw std::invalid_argument("Minimal timestep size must be positive.");
248 if (expl.maximal_dt <= 0.0) throw std::invalid_argument("Maximal timestep size must be positive.");
250 throw std::invalid_argument("Minimal timestep size must be smaller than maximal timestep size.");
252 throw std::invalid_argument("Initial timestep size must be within the minimal and maximal timestep size.");
253 if (expl.abs_tol <= 0.0) throw std::invalid_argument("Absolute tolerance must be > 0.");
254 if (expl.rel_tol <= 0.0) throw std::invalid_argument("Relative tolerance must be > 0.");
255 }
256 }
257
258 protected:
260 {
261 log.summary(assembler.summary());
262 data_out.drain();
263 }
264
275 template <typename EmitFinalFrame> void finalize_output_after_failure(EmitFinalFrame &&emit_final_frame)
276 {
277 try {
278 std::forward<EmitFinalFrame>(emit_final_frame)();
279 } catch (const std::exception &e) {
280 log.error("Output of the final frame after the failure failed: {}", e.what());
281 } catch (...) {
282 log.error("Output of the final frame after the failure failed.");
283 }
284 try {
285 drain_output();
286 } catch (const std::exception &e) {
287 log.error("Draining pending output after the failure failed: {}", e.what());
288 } catch (...) {
289 log.error("Draining pending output after the failure failed.");
290 }
291 }
292
300 virtual void run(AbstractFlowingVariables<NumberType, VectorType> &initial_condition, const double t_start,
301 const double t_stop) = 0;
302
303 bool is_implicit() const { return m_is_implicit; }
304 bool is_explicit() const { return m_is_explicit; }
305
306 protected:
313
314 const bool m_is_implicit;
315 const bool m_is_explicit;
316
317
318 double output_dt;
338
347
348 std::size_t next_jacobian_build_id = 0;
350 };
351} // namespace DiFfRG
Implement a simple interface to do all adaptivity tasks, i.e. solution transfer, reinit of dofHandler...
Definition abstract_adaptor.hh:11
This is the general assembler interface for any kind of discretization. An assembler is responsible f...
Definition abstract_assembler.hh:54
A class to set up initial data for whatever discretization we have chosen. Also used to switch/manage...
Definition abstract_data.hh:21
The abstract base class for all timestepping algorithms. It provides a standard constructor which pop...
Definition abstract_timestepper.hh:155
VectorType_ VectorType
Definition abstract_timestepper.hh:158
struct DiFfRG::AbstractTimestepper::ExplicitParameters expl
ReportPort log
Definition abstract_timestepper.hh:312
void drain_output()
Definition abstract_timestepper.hh:259
std::size_t next_jacobian_build_id
Definition abstract_timestepper.hh:348
SparseMatrixType_ SparseMatrixType
Definition abstract_timestepper.hh:160
virtual void run(AbstractFlowingVariables< NumberType, VectorType > &initial_condition, const double t_start, const double t_stop)=0
Any derived class must implement this method to run the timestepping algorithm.
void finalize_output_after_failure(EmitFinalFrame &&emit_final_frame)
Best-effort output cleanup while a timestepping failure is being reported.
Definition abstract_timestepper.hh:275
const bool m_is_explicit
Definition abstract_timestepper.hh:315
typename get_type::BlockVectorType< VectorType > BlockVectorType
Definition abstract_timestepper.hh:169
AbstractAssembler< VectorType, SparseMatrixType, dim > & assembler
Definition abstract_timestepper.hh:309
AbstractTimestepper(const ConfigTree &config, AbstractAssembler< VectorType, SparseMatrixType, dim > &assembler, OutputSession_impl< dim, VectorType > &data_out, AbstractAdaptor< VectorType > &adaptor, const bool implicit_stepper, const bool explicit_stepper)
Definition abstract_timestepper.hh:192
bool is_explicit() const
Definition abstract_timestepper.hh:304
TimestepperJacobianDiagnosticsState jacobian_diagnostics_state
Definition abstract_timestepper.hh:349
OutputSession_impl< dim, VectorType > & data_out
Definition abstract_timestepper.hh:310
AbstractTimestepper(const ConfigTree &config, AbstractAssembler< VectorType, SparseMatrixType, dim > &assembler, OutputSession_impl< dim, VectorType > &data_out, const bool implicit_stepper, const bool explicit_stepper)
Construct a new Abstract Timestepper object.
Definition abstract_timestepper.hh:183
bool is_implicit() const
Definition abstract_timestepper.hh:303
AbstractAdaptor< VectorType > & adaptor
Definition abstract_timestepper.hh:311
typename get_type::NumberType< VectorType > NumberType
Definition abstract_timestepper.hh:159
void read_parameters()
Definition abstract_timestepper.hh:202
const bool m_is_implicit
Definition abstract_timestepper.hh:314
const ConfigTree config
Definition abstract_timestepper.hh:307
struct DiFfRG::AbstractTimestepper::ImplicitParameters impl
NoAdaptivity< VectorType > adaptor_default
Definition abstract_timestepper.hh:308
static constexpr uint dim
Definition abstract_timestepper.hh:157
double output_dt
Definition abstract_timestepper.hh:318
A hierarchical configuration tree, readable from JSON and TOML files.
Definition config_tree.hh:32
double get_double_or_warn(const std::string &key, const double def) const
uint get_uint(const std::string &key) const
Get the value of a key in the configuration object.
double get_double(const std::string &key) const
Get the value of a key in the configuration object.
bool get_bool(const std::string &key) const
Get the value of a key in the configuration object.
Definition no_adaptivity.hh:25
Definition output_session.hh:176
Definition run_reporter.hh:96
void summary(const SummaryEvent &event) const
void error(spdlog::format_string_t< Args... > format, Args &&...args) const
Definition run_reporter.hh:111
Definition jacobian_diagnostics.hh:59
A vector type DiFfRG's timesteppers and assemblers can work with.
Definition linear_algebra.hh:179
All compile-time knowledge about which linear algebra types DiFfRG uses.
typename internal::_NumberType< VectorType >::value NumberType
Definition linear_algebra.hh:152
typename internal::_BlockVectorType< VectorType >::value BlockVectorType
The block-vector type belonging to a given vector type.
Definition linear_algebra.hh:167
Definition complex_math.hh:10
unsigned int uint
Definition utils.hh:24
Definition abstract_timestepper.hh:339
double abs_tol
Definition abstract_timestepper.hh:343
double minimal_dt
Definition abstract_timestepper.hh:341
bool detect_stuck
Definition abstract_timestepper.hh:345
double dt
Definition abstract_timestepper.hh:340
double maximal_dt
Definition abstract_timestepper.hh:342
double rel_tol
Definition abstract_timestepper.hh:344
Definition abstract_timestepper.hh:319
double ida_callback_trace_min_t
Definition abstract_timestepper.hh:332
bool ida_callback_trace_successes
Definition abstract_timestepper.hh:334
double dt
Definition abstract_timestepper.hh:320
double maximal_dt
Definition abstract_timestepper.hh:322
double minimal_dt
Definition abstract_timestepper.hh:321
bool ida_error_dof_diagnostics
Definition abstract_timestepper.hh:335
uint ida_callback_trace_max_lines
Definition abstract_timestepper.hh:333
uint ida_error_dof_diagnostics_top_n
Definition abstract_timestepper.hh:336
bool ida_callback_trace
Definition abstract_timestepper.hh:331
double rel_tol
Definition abstract_timestepper.hh:324
uint max_non_linear_iterations
Definition abstract_timestepper.hh:326
uint max_steps
Definition abstract_timestepper.hh:325
bool jacobian_diagnostics
Definition abstract_timestepper.hh:330
double abs_tol
Definition abstract_timestepper.hh:323
Stopwatch feeding structured progress durations.
Definition abstract_timestepper.hh:38
std::chrono::high_resolution_clock::time_point mark
Definition abstract_timestepper.hh:39
double lap()
Definition abstract_timestepper.hh:41
Definition abstract_timestepper.hh:50
double k
Definition abstract_timestepper.hh:52
double wrms
Definition abstract_timestepper.hh:59
double ida_current_step_size
Definition abstract_timestepper.hh:57
long int reject_delta
Definition abstract_timestepper.hh:53
double ida_last_step_size
Definition abstract_timestepper.hh:56
double ida_current_time
Definition abstract_timestepper.hh:58
std::vector< IDAErrorDofRecord > top_dofs
Definition abstract_timestepper.hh:60
double t
Definition abstract_timestepper.hh:51
long int ida_steps
Definition abstract_timestepper.hh:55
long int total_rejects
Definition abstract_timestepper.hh:54
Definition abstract_timestepper.hh:22
double contribution
Definition abstract_timestepper.hh:27
double estimated_local_error
Definition abstract_timestepper.hh:25
double error_weight
Definition abstract_timestepper.hh:26
double value
Definition abstract_timestepper.hh:24
types::global_dof_index dof
Definition abstract_timestepper.hh:23
Definition abstract_timestepper.hh:63
long int ida_nonlinear_convergence_failures
Definition abstract_timestepper.hh:66
void append_to(ProgressEvent &event) const
Definition abstract_timestepper.hh:73
long int ida_nonlinear_iterations
Definition abstract_timestepper.hh:69
double ida_current_step_size
Definition abstract_timestepper.hh:71
long int ida_steps
Definition abstract_timestepper.hh:64
long int ida_error_test_failures
Definition abstract_timestepper.hh:65
long int ida_step_solve_failures
Definition abstract_timestepper.hh:67
double ida_last_step_size
Definition abstract_timestepper.hh:70
double ida_current_time
Definition abstract_timestepper.hh:72
long int ida_residual_evaluations
Definition abstract_timestepper.hh:68
Definition run_reporter.hh:50
Definition abstract_timestepper.hh:83
size_t linear_solver_failures
Definition abstract_timestepper.hh:88
size_t nonfinite_failures() const
Definition abstract_timestepper.hh:90
size_t nonfinite_residual_failures
Definition abstract_timestepper.hh:85
void append_to(ProgressEvent &event) const
Definition abstract_timestepper.hh:95
size_t nonfinite_solution_failures
Definition abstract_timestepper.hh:84
bool has_failures() const
Definition abstract_timestepper.hh:91
size_t jacobian_failures
Definition abstract_timestepper.hh:87
size_t residual_exceptions
Definition abstract_timestepper.hh:86
Definition abstract_timestepper.hh:105
bool empty() const
Definition abstract_timestepper.hh:114
void append_to(ProgressEvent &event) const
Definition abstract_timestepper.hh:109
std::optional< IDAProgressDiagnostics > ida
Definition abstract_timestepper.hh:106
SolverCallbackDiagnostics callbacks
Definition abstract_timestepper.hh:107