OGS
FixedTimeStepping.cpp
Go to the documentation of this file.
1
13#include "FixedTimeStepping.h"
14
15#include <algorithm>
16#include <cassert>
17#include <limits>
18#include <numeric>
19
20namespace
21{
23double addTimeIncrement(std::vector<double>& delta_ts, std::size_t const repeat,
24 double const delta_t, double const t_curr)
25{
26 auto const new_size = delta_ts.size() + repeat;
27 try
28 {
29 delta_ts.resize(new_size, delta_t);
30 }
31 catch (std::exception const& e)
32 {
34 "Resize of the time steps vector failed for the requested new size "
35 "{:d}. Probably there is not enough memory ({:g} GiB "
36 "requested).\nThrown exception: {:s}",
37 new_size,
38 new_size * sizeof(double) / 1024. / 1024. / 1024.,
39 e.what());
40 }
41
42 // Multiplying dt * repeat is not the same as in the current
43 // implementation of the time loop, where the dt's are added.
44 // Therefore the sum of all dt is taken here.
45 return std::accumulate(end(delta_ts) - repeat, end(delta_ts), t_curr);
46}
47} // namespace
48
49namespace NumLib
50{
51std::size_t findDeltatInterval(double const t_initial,
52 std::vector<double> const& delta_ts,
53 double const fixed_output_time)
54{
55 if (fixed_output_time < t_initial)
56 {
57 return std::numeric_limits<std::size_t>::max();
58 }
59
60 auto timestepper_time = t_initial;
61 for (std::size_t k = 0; k < delta_ts.size(); ++k)
62 {
63 if (timestepper_time <= fixed_output_time &&
64 fixed_output_time < timestepper_time + delta_ts[k])
65 {
66 return k;
67 }
68 timestepper_time += delta_ts[k];
69 }
70
71 return std::numeric_limits<std::size_t>::max();
72}
73
75 double const t_initial, double const t_end, std::vector<double>& delta_ts,
76 std::vector<double> const& fixed_times_for_output)
77{
78 if (fixed_times_for_output.empty())
79 {
80 return;
81 }
82
83 if (auto lower_bound =
84 std::lower_bound(begin(fixed_times_for_output),
85 end(fixed_times_for_output), t_initial);
86 lower_bound != begin(fixed_times_for_output))
87 {
88 WARN(
89 "Request for output at times {}, but the simulation's start time "
90 "is {}. Output will be skipped.",
91 fmt::join(begin(fixed_times_for_output), lower_bound, ", "),
92 t_initial);
93 }
94
95 if (auto upper_bound = std::upper_bound(begin(fixed_times_for_output),
96 end(fixed_times_for_output), t_end);
97 upper_bound != end(fixed_times_for_output))
98 {
99 WARN(
100 "Request for output at times {}, but simulation's end time is {}. "
101 "Output will be skipped.",
102 fmt::join(upper_bound, end(fixed_times_for_output), ", "),
103 t_end);
104 }
105
106 if (delta_ts.empty())
107 {
108 WARN("No timesteps specified.");
109 return;
110 }
111
112 // incorporate fixed output times into dts vector
113 for (auto const fixed_time_for_output : fixed_times_for_output)
114 {
115 auto const interval_number =
116 findDeltatInterval(t_initial, delta_ts, fixed_time_for_output);
117 if (interval_number == std::numeric_limits<std::size_t>::max())
118 {
119 WARN("Did not find interval for fixed output time {}",
120 fixed_time_for_output);
121 continue;
122 }
123 auto const lower_bound = std::accumulate(
124 begin(delta_ts), begin(delta_ts) + interval_number, t_initial);
125 auto const upper_bound = lower_bound + delta_ts[interval_number];
126 if (fixed_time_for_output - lower_bound <=
128 {
129 continue;
130 }
131 if (upper_bound - fixed_time_for_output <=
133 {
134 continue;
135 }
136 delta_ts[interval_number] = fixed_time_for_output - lower_bound;
137
138 delta_ts.insert(delta_ts.begin() + interval_number + 1,
139 upper_bound - fixed_time_for_output);
140 }
141}
142
144 double t0, double tn, std::vector<RepeatDtPair> const& repeat_dt_pairs,
145 std::vector<double> const& fixed_times_for_output)
146 : TimeStepAlgorithm(t0, tn)
147{
148 double t_curr = _t_initial;
149
150 if (!areRepeatDtPairsValid(repeat_dt_pairs))
151 {
152 OGS_FATAL("FixedTimeStepping: Couldn't construct object from data");
153 }
154 for (auto const& [repeat, delta_t] : repeat_dt_pairs)
155 {
156 if (t_curr <= _t_end)
157 {
158 t_curr = addTimeIncrement(_dt_vector, repeat, delta_t, t_curr);
159 }
160 }
161
162 // append last delta_t until t_end is reached
163 if (t_curr <= _t_end)
164 {
165 auto const delta_t = std::get<1>(repeat_dt_pairs.back());
166 auto const repeat =
167 static_cast<std::size_t>(std::ceil((_t_end - t_curr) / delta_t));
168 addTimeIncrement(_dt_vector, repeat, delta_t, t_curr);
169 }
170
172 fixed_times_for_output);
173}
174
175FixedTimeStepping::FixedTimeStepping(double t0, double t_end, double dt)
176 : TimeStepAlgorithm(t0, t_end)
177{
178 auto const new_size =
179 static_cast<std::size_t>(std::ceil((t_end - t0) / dt));
180 try
181 {
182 _dt_vector = std::vector<double>(new_size, dt);
183 }
184 catch (std::length_error const& e)
185 {
186 OGS_FATAL(
187 "Resize of the time steps vector failed for the requested new "
188 "size {}. Probably there is not enough memory ({:g} GiB "
189 "requested).\n"
190 "Thrown exception: {}",
191 new_size, new_size * sizeof(double) / 1024. / 1024. / 1024.,
192 e.what());
193 }
194 catch (std::bad_alloc const& e)
195 {
196 OGS_FATAL(
197 "Allocation of the time steps vector failed for the requested "
198 "size {}. Probably there is not enough memory ({:g} GiB "
199 "requested).\n"
200 "Thrown exception: {}",
201 new_size,
202 new_size * sizeof(double) / 1024. / 1024. / 1024.,
203 e.what());
204 }
205}
206
207std::tuple<bool, double> FixedTimeStepping::next(
208 double const /*solution_error*/, int const /*number_iterations*/,
209 NumLib::TimeStep& /*ts_previous*/, NumLib::TimeStep& ts_current)
210{
211 // check if last time step
212 if (ts_current.timeStepNumber() == _dt_vector.size() ||
213 std::abs(ts_current.current() - end()) <
214 std::numeric_limits<double>::epsilon())
215 {
216 return std::make_tuple(false, 0.0);
217 }
218
219 double dt = _dt_vector[ts_current.timeStepNumber()];
220 if (ts_current.current() + dt > end())
221 { // upper bound by t_end
222 dt = end() - ts_current.current();
223 }
224
225 return std::make_tuple(true, dt);
226}
227
229 std::vector<RepeatDtPair> const& repeat_dt_pairs)
230{
231 if (repeat_dt_pairs.empty())
232 {
233 return false;
234 }
235
236 for (auto const& [repeat, delta_t] : repeat_dt_pairs)
237 {
238 if (repeat == 0)
239 {
240 ERR("FixedTimeStepping: <repeat> is zero.");
241 return false;
242 }
243 if (delta_t <= 0.0)
244 {
245 ERR("FixedTimeStepping: timestep <delta_t> is <= 0.0.");
246 return false;
247 }
248 }
249 return true;
250}
251
252} // namespace NumLib
#define OGS_FATAL(...)
Definition Error.h:26
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:45
void WARN(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:40
std::tuple< bool, double > next(double solution_error, int number_iterations, NumLib::TimeStep &ts_previous, NumLib::TimeStep &ts_current) override
static bool areRepeatDtPairsValid(std::vector< RepeatDtPair > const &repeat_dt_pairs)
std::vector< double > _dt_vector
a vector of time step sizes
FixedTimeStepping(double t0, double t_end, double dt)
Interface of time stepping algorithms.
const double _t_initial
initial time
const double _t_end
end time
double end() const
return the end of time steps
Time step object.
Definition TimeStep.h:29
double current() const
return current time step
Definition TimeStep.h:91
static constexpr double minimalTimeStepSize
Definition TimeStep.h:100
std::size_t timeStepNumber() const
the time step number
Definition TimeStep.h:95
void incorporateFixedTimesForOutput(double const t_initial, double const t_end, std::vector< double > &delta_ts, std::vector< double > const &fixed_times_for_output)
std::size_t findDeltatInterval(double const t_initial, std::vector< double > const &delta_ts, double const fixed_output_time)
double addTimeIncrement(std::vector< double > &delta_ts, std::size_t const repeat, double const delta_t, double const t_curr)
Returns sum of the newly added time increments.