OGS 6.1.0-1699-ge946d4c5f
TimeStepAlgorithm.h
Go to the documentation of this file.
1 
13 #pragma once
14 
15 #include <cmath>
16 #include <vector>
17 
18 #include "BaseLib/Error.h"
20 
21 namespace NumLib
22 {
28 {
29 public:
30  TimeStepAlgorithm(const double t0, const double t_end)
31  : _t_initial(t0), _t_end(t_end), _ts_prev(t0), _ts_current(t0)
32  {
33  }
34 
35  TimeStepAlgorithm(const double t0, const double t_end, const double dt)
36  : _t_initial(t0), _t_end(t_end), _ts_prev(t0), _ts_current(t0)
37  {
38  auto const new_size =
39  static_cast<std::size_t>(std::ceil((t_end - t0) / dt));
40  try
41  {
42  _dt_vector = std::vector<double>(new_size, dt);
43  }
44  catch (std::length_error const& e)
45  {
46  OGS_FATAL(
47  "Resize of the time steps vector failed for the requested new "
48  "size %u. Probably there is not enough memory (%g GiB "
49  "requested).\n"
50  "Thrown exception: %s",
51  new_size, new_size * sizeof(double) / 1024. / 1024. / 1024.,
52  e.what());
53  }
54  catch (std::bad_alloc const& e)
55  {
56  OGS_FATAL(
57  "Allocation of the time steps vector failed for the requested "
58  "size %u. Probably there is not enough memory (%d GiB "
59  "requested).\n"
60  "Thrown exception: %s",
61  new_size,
62  new_size * sizeof(double) / 1024. / 1024. / 1024.,
63  e.what());
64  }
65  }
66 
67  TimeStepAlgorithm(const double t0, const double t_end,
68  const std::vector<double>& all_step_sizes)
69  : _t_initial(t0),
70  _t_end(t_end),
71  _ts_prev(t0),
72  _ts_current(t0),
73  _dt_vector(all_step_sizes)
74  {
75  }
76 
77  virtual ~TimeStepAlgorithm() = default;
78 
80  double begin() const { return _t_initial; }
82  double end() const { return _t_end; }
84  const TimeStep getTimeStep() const { return _ts_current; }
86  void resetCurrentTimeStep(const double dt)
87  {
89  _ts_current += dt;
90  }
91 
97  virtual bool next(const double solution_error, int number_iterations) = 0;
98 
100  virtual bool accepted() const = 0;
101 
105  virtual void setAcceptedOrNot(const bool accepted) { (void)accepted; };
106 
108  const std::vector<double>& getTimeStepSizeHistory() const
109  {
110  return _dt_vector;
111  }
112 
115  virtual bool isSolutionErrorComputationNeeded() { return false; }
116 
122  virtual void addFixedOutputTimes(
123  std::vector<double> const& /*fixed_output_times*/)
124  {
125  }
126 
127 protected:
129  const double _t_initial;
131  const double _t_end;
132 
137 
139  std::vector<double> _dt_vector;
140 };
141 
142 } // namespace NumLib
Interface of time stepping algorithms.
std::vector< double > _dt_vector
a vector of time step sizes
const TimeStep getTimeStep() const
return current time step
const std::vector< double > & getTimeStepSizeHistory() const
return a history of time step sizes
Time step object.
Definition: TimeStep.h:26
TimeStep _ts_current
current time step information
double end() const
return the end of time steps
virtual void setAcceptedOrNot(const bool accepted)
void resetCurrentTimeStep(const double dt)
reset the current step size from the previous time
const double _t_initial
initial time
TimeStepAlgorithm(const double t0, const double t_end, const double dt)
TimeStepAlgorithm(const double t0, const double t_end, const std::vector< double > &all_step_sizes)
const double _t_end
end time
virtual bool isSolutionErrorComputationNeeded()
virtual ~TimeStepAlgorithm()=default
double begin() const
return the beginning of time steps
TimeStep _ts_prev
previous time step information
virtual bool next(const double solution_error, int number_iterations)=0
TimeStepAlgorithm(const double t0, const double t_end)
virtual bool accepted() const =0
return if current time step is accepted or not
#define OGS_FATAL(fmt,...)
Definition: Error.h:71
virtual void addFixedOutputTimes(std::vector< double > const &)