OGS
IterationNumberBasedTimeStepping.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) OpenGeoSys Community (opengeosys.org)
2// SPDX-License-Identifier: BSD-3-Clause
3
5
6#include <algorithm>
7#include <cassert>
8#include <cmath>
9#include <limits>
10#include <utility>
11
12#include "BaseLib/Algorithm.h"
16
17namespace NumLib
18{
20 double const t_initial, double const t_end, double const min_dt,
21 double const max_dt, double const initial_dt,
22 MultiplyerInterpolationType const multiplier_interpolation_type,
23 std::vector<int>&& iter_times_vector,
24 std::vector<double>&& multiplier_vector,
25 std::vector<double> const& fixed_times_for_output)
26 : TimeStepAlgorithm(t_initial, t_end),
27 _iter_times_vector(std::move(iter_times_vector)),
28 _multiplier_vector(std::move(multiplier_vector)),
29 _min_dt(min_dt),
30 _max_dt(max_dt),
31 _initial_dt(initial_dt),
32 _multiplier_interpolation_type(multiplier_interpolation_type),
34 _fixed_times_for_output(fixed_times_for_output)
35{
36 if (_iter_times_vector.empty())
37 {
38 OGS_FATAL("Vector of iteration numbers must not be empty.");
39 }
40 if (_iter_times_vector.size() != _multiplier_vector.size())
41 {
42 OGS_FATAL(
43 "Vector of iteration numbers must be of the same size as the "
44 "vector of multipliers.");
45 }
46 if (!std::is_sorted(std::begin(_iter_times_vector),
47 std::end(_iter_times_vector)))
48 {
49 OGS_FATAL("Vector of iteration numbers must be sorted.");
50 }
51}
52
53double IterationNumberBasedTimeStepping::next(double const /*solution_error*/,
54 int const number_iterations,
55 NumLib::TimeStep& ts_previous,
56 NumLib::TimeStep& ts_current)
57{
58 _iter_times = number_iterations;
59
60 if (ts_previous.isAccepted())
61 {
62 ts_previous = ts_current;
63 }
64
65 // confirm current time and move to the next if accepted
66 if (ts_current.isAccepted())
67 {
68 ts_previous.setAccepted(true);
69 return getNextTimeStepSize(ts_previous, ts_current);
70 }
71
72 double dt = getNextTimeStepSize(ts_previous, ts_current);
73 // In case it is the first time be rejected, re-computed dt again with
74 // current dt
75 if (std::abs(dt - ts_current.dt()) < std::numeric_limits<double>::epsilon())
76 {
77 // time step was rejected, keep dt for the next dt computation.
78 ts_previous = // essentially equal to _ts_prev.dt = _ts_current.dt.
79 TimeStep{ts_previous.previous(), ts_previous.previous() + dt,
80 ts_previous.timeStepNumber()};
81 dt = getNextTimeStepSize(ts_previous, ts_current);
82 }
83
84 // time step was rejected, keep dt for the next dt computation.
85 ts_previous = // essentially equal to ts_previous.dt = _ts_current.dt.
86 TimeStep{ts_previous.previous(), ts_previous.previous() + dt,
87 ts_previous.timeStepNumber()};
88 ts_current = TimeStep{ts_current.previous(), ts_current.previous() + dt,
89 ts_current.timeStepNumber()};
90
91 return dt;
92}
93
95 int const number_iterations, bool const current_time_step_is_accepted,
96 std::vector<int> const& nonlinear_iteration_numbers,
97 std::vector<double> const& multipliers,
98 MultiplyerInterpolationType const multiplier_interpolation_type)
99{
100 double multiplier = multipliers.front();
101 switch (multiplier_interpolation_type)
102 {
104 {
105 auto const& pwli = MathLib::PiecewiseLinearInterpolation(
106 nonlinear_iteration_numbers, multipliers, false);
107 multiplier = pwli.getValue(number_iterations);
108 break;
109 }
111 auto const& piecewise_constant_interpolation =
113 nonlinear_iteration_numbers, multipliers);
114 multiplier =
115 piecewise_constant_interpolation.value(number_iterations);
116 break;
117 }
118
119 if (!current_time_step_is_accepted && (multiplier >= 1.0))
120 {
121 return *std::min_element(multipliers.begin(), multipliers.end());
122 }
123
124 return multiplier;
125}
126
128 NumLib::TimeStep const& ts_previous,
129 NumLib::TimeStep const& ts_current) const
130{
131 double dt = 0.0;
132
133 // In first time step and first non-linear iteration take the initial dt.
134 if (ts_previous.timeStepNumber() == 0 && _iter_times == 0)
135 {
136 dt = _initial_dt;
137 }
138 else
139 {
140 // Attention: for the first time step and second iteration the
141 // ts_prev.dt is 0 and 0*multiplier is the next dt, which will be
142 // clamped to the minimum dt.
143 dt = ts_previous.dt() *
147 }
148
149 if (_fixed_times_for_output.empty())
150 {
151 return std::clamp(dt, _min_dt, _max_dt);
152 }
153
154 // restrict dt to _max_dt before taking fixed times for output into account
155 dt = std::min(dt, _max_dt);
156
157 // find first fixed timestep for output larger than the current time, i.e.,
158 // current time < fixed output time
159 auto fixed_output_time_it = std::find_if(
161 [&ts_current](auto const fixed_output_time)
162 { return ts_current.current()() < fixed_output_time; });
163
164 if (fixed_output_time_it != _fixed_times_for_output.end())
165 {
166 // check if the fixed output time is in the interval
167 // (current time, current time + dt)
168 if (*fixed_output_time_it < ts_current.current()() + dt)
169 {
170 // check if the potential adjusted time step is larger than zero
171 if (std::abs(*fixed_output_time_it - ts_current.current()()) >
172 std::numeric_limits<double>::epsilon() * ts_current.current()())
173 {
174 return *fixed_output_time_it - ts_current.current()();
175 }
176 }
177 }
178 return std::clamp(dt, _min_dt, _max_dt);
179}
180
182 NumLib::TimeStep const& timestep_previous,
183 NumLib::TimeStep const& timestep_current) const
184{
185 return NumLib::canReduceTimestepSize(timestep_previous, timestep_current,
186 _min_dt);
187}
188
189} // namespace NumLib
#define OGS_FATAL(...)
Definition Error.h:19
IterationNumberBasedTimeStepping(double const t_initial, double const t_end, double const min_dt, double const max_dt, double const initial_dt, MultiplyerInterpolationType const multiplier_interpolation_type, std::vector< int > &&iter_times_vector, std::vector< double > &&multiplier_vector, std::vector< double > const &fixed_times_for_output)
double next(double solution_error, int number_iterations, NumLib::TimeStep &ts_previous, NumLib::TimeStep &ts_current) override
const MultiplyerInterpolationType _multiplier_interpolation_type
Interpolation type for the multiplier.
double getNextTimeStepSize(NumLib::TimeStep const &ts_previous, NumLib::TimeStep const &ts_current) const
Calculate the next time step size.
const double _max_dt
The maximum allowed time step size.
const std::vector< double > _multiplier_vector
This vector stores the multiplier coefficients.
bool canReduceTimestepSize(NumLib::TimeStep const &timestep_previous, NumLib::TimeStep const &timestep_current) const override
Query the timestepper if further time step size reduction is possible.
int _iter_times
The number of nonlinear iterations.
const double _min_dt
The minimum allowed time step size.
const int _max_iter
The maximum allowed iteration number to accept current time step.
TimeStepAlgorithm(const double t0, const double t_end)
Time step object.
Definition TimeStep.h:24
void setAccepted(bool const accepted)
Definition TimeStep.h:77
Time current() const
return current time step
Definition TimeStep.h:71
bool isAccepted() const
Definition TimeStep.h:78
Time previous() const
return previous time step
Definition TimeStep.h:69
double dt() const
time step size from _previous
Definition TimeStep.h:73
std::size_t timeStepNumber() const
the time step number
Definition TimeStep.h:75
double findMultiplier(int const number_iterations, bool const current_time_step_is_accepted, std::vector< int > const &nonlinear_iteration_numbers, std::vector< double > const &multipliers, MultiplyerInterpolationType const multiplier_interpolation_type)
Find a multiplier for the given number of iterations.
bool canReduceTimestepSize(TimeStep const &timestep_previous, TimeStep const &timestep_current, double const min_dt)