OGS 6.2.1-97-g73d1aeda3
IterationNumberBasedTimeStepping.cpp
Go to the documentation of this file.
1 
14 
15 #include <algorithm>
16 #include <cassert>
17 #include <cmath>
18 #include <limits>
19 #include <utility>
20 
21 namespace NumLib
22 {
24  double const t_initial, double const t_end, double const min_dt,
25  double const max_dt, double const initial_dt,
26  std::vector<int>&& iter_times_vector,
27  std::vector<double>&& multiplier_vector)
28  : TimeStepAlgorithm(t_initial, t_end),
29  _iter_times_vector(std::move(iter_times_vector)),
30  _multiplier_vector(std::move(multiplier_vector)),
31  _min_dt(min_dt),
32  _max_dt(max_dt),
33  _initial_dt(initial_dt),
34  _max_iter(_iter_times_vector.empty() ? 0 : _iter_times_vector.back())
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 
53 bool IterationNumberBasedTimeStepping::next(double const /*solution_error*/,
54  int const number_iterations)
55 {
56  _iter_times = number_iterations;
57 
58  // confirm current time and move to the next if accepted
59  if (accepted())
60  {
62  _dt_vector.push_back(_ts_current.dt());
63  }
64  else
65  {
67  // time step was rejected, keep dt for the next dt computation.
68  _ts_prev = // essentially equal to _ts_prev.dt = _ts_current.dt.
71  }
72 
73  // prepare the next time step info
76 
77  return true;
78 }
79 
81  int const number_iterations) const
82 {
83  double multiplier = _multiplier_vector.front();
84  for (std::size_t i = 0; i < _iter_times_vector.size(); i++)
85  {
86  if (number_iterations >= _iter_times_vector[i])
87  {
88  multiplier = _multiplier_vector[i];
89  }
90  }
91  return multiplier;
92 }
93 
95 {
96  double dt = 0.0;
97 
98  // In first time step and first non-linear iteration take the initial dt.
99  if (_ts_prev.steps() == 0 && _iter_times == 0)
100  {
101  dt = _initial_dt;
102  }
103  else
104  {
105  // Attention: for the first time step and second iteration the
106  // ts_prev.dt is 0 and 0*multiplier is the next dt, which will be
107  // clamped to the minimum dt.
109  }
110 
111  dt = std::min(std::max(dt, _min_dt), _max_dt);
112 
113  double const t_next = dt + _ts_prev.current();
114  if (t_next > end())
115  {
116  dt = end() - _ts_prev.current();
117  }
118 
119  return dt;
120 }
121 
123 {
124  return _accepted;
125 }
126 } // namespace NumLib
double dt() const
time step size from _previous
Definition: TimeStep.h:110
const double _min_dt
The minimum allowed time step size.
int _iter_times
The number of nonlinear iterations.
Interface of time stepping algorithms.
std::size_t steps() const
the number of time _steps
Definition: TimeStep.h:112
std::vector< double > _dt_vector
a vector of time step sizes
double findMultiplier(int number_iterations) const
Find a multiplier for the given number of iterations.
double previous() const
return previous time step
Definition: TimeStep.h:106
Time step object.
Definition: TimeStep.h:26
TimeStep _ts_current
current time step information
double end() const
return the end of time steps
bool next(double solution_error, int number_iterations) override
double getNextTimeStepSize() const
Calculate the next time step size.
IterationNumberBasedTimeStepping(double const t_initial, double const t_end, double const min_dt, double const max_dt, double const initial_dt, std::vector< int > &&iter_times_vector, std::vector< double > &&multiplier_vector)
bool _accepted
True, if the timestep is accepted.
TimeStep _ts_prev
previous time step information
double current() const
return current time step
Definition: TimeStep.h:108
const double _max_dt
The maximum allowed time step size.
#define OGS_FATAL(fmt,...)
Definition: Error.h:63
bool accepted() const override
return if current time step is accepted or not
const std::vector< double > _multiplier_vector
This vector stores the multiplier coefficients.