OGS
FixedTimeStepping.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
4#include "FixedTimeStepping.h"
5
6#include <spdlog/fmt/ranges.h>
7
8#include <algorithm>
9#include <cassert>
10#include <limits>
11#include <numeric>
12
14
15namespace
16{
18NumLib::Time addTimeIncrement(std::vector<double>& delta_ts,
19 std::size_t const repeat, double const delta_t,
20 NumLib::Time const t_curr)
21{
22 auto const new_size = delta_ts.size() + repeat;
23 try
24 {
25 delta_ts.resize(new_size, delta_t);
26 }
27 catch (std::exception const& e)
28 {
30 "Resize of the time steps vector failed for the requested new size "
31 "{:d}. Probably there is not enough memory ({:g} GiB "
32 "requested).\nThrown exception: {:s}",
33 new_size,
34 new_size * sizeof(double) / 1024. / 1024. / 1024.,
35 e.what());
36 }
37
38 // Multiplying dt * repeat is not the same as in the current
39 // implementation of the time loop, where the dt's are added.
40 // Therefore the sum of all dt is taken here.
41 return std::accumulate(end(delta_ts) - repeat, end(delta_ts), t_curr);
42}
43} // namespace
44
45namespace NumLib
46{
47std::size_t findDeltatInterval(Time const& t_initial,
48 std::vector<double> const& delta_ts,
49 Time const& fixed_output_time)
50{
51 if (fixed_output_time < t_initial)
52 {
53 return std::numeric_limits<std::size_t>::max();
54 }
55
56 auto timestepper_time = t_initial;
57 for (std::size_t k = 0; k < delta_ts.size(); ++k)
58 {
59 if (timestepper_time <= fixed_output_time &&
60 fixed_output_time < timestepper_time + delta_ts[k])
61 {
62 return k;
63 }
64 timestepper_time += delta_ts[k];
65 }
66
67 return std::numeric_limits<std::size_t>::max();
68}
69
71 NumLib::Time const t_initial, NumLib::Time const t_end,
72 std::vector<double>& delta_ts,
73 std::vector<double> const& fixed_times_for_output)
74{
75 if (fixed_times_for_output.empty())
76 {
77 return;
78 }
79
80 if (auto lower_bound = std::lower_bound(
81 begin(fixed_times_for_output), end(fixed_times_for_output),
82 t_initial,
83 [](auto const time, NumLib::Time const& initial_time)
84 { return NumLib::Time(time) < initial_time; });
85 lower_bound != begin(fixed_times_for_output))
86 {
87 WARN(
88 "Request for output at times {}, but the simulation's start time "
89 "is {}. Output will be skipped.",
90 fmt::join(begin(fixed_times_for_output), lower_bound, ", "),
91 t_initial());
92 }
93
94 if (auto upper_bound =
95 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 = findDeltatInterval(
116 t_initial, delta_ts, Time(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
124 auto const lower_bound = std::accumulate(
125 begin(delta_ts), begin(delta_ts) + interval_number, t_initial);
126 auto const upper_bound = lower_bound + delta_ts[interval_number];
127 // in order to use the comparison from struct Time
128 if (NumLib::Time(fixed_time_for_output) == lower_bound)
129 {
130 continue;
131 }
132 if (upper_bound == NumLib::Time(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 Time 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 = static_cast<std::size_t>(
167 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
207double FixedTimeStepping::next(double const /*solution_error*/,
208 int const /*number_iterations*/,
209 NumLib::TimeStep& /*ts_previous*/,
210 NumLib::TimeStep& ts_current)
211{
212 // check if last time step
213 if (ts_current.timeStepNumber() == _dt_vector.size() ||
214 ts_current.current() >= end())
215 {
216 return 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 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:19
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:40
void WARN(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:34
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)
double next(double solution_error, int number_iterations, NumLib::TimeStep &ts_previous, NumLib::TimeStep &ts_current) override
const Time _t_initial
initial time
Time end() const
return the end of time steps
TimeStepAlgorithm(const double t0, const double t_end)
Time step object.
Definition TimeStep.h:24
Time current() const
return current time step
Definition TimeStep.h:71
std::size_t timeStepNumber() const
the time step number
Definition TimeStep.h:75
void incorporateFixedTimesForOutput(NumLib::Time const t_initial, NumLib::Time const t_end, std::vector< double > &delta_ts, std::vector< double > const &fixed_times_for_output)
std::size_t findDeltatInterval(Time const &t_initial, std::vector< double > const &delta_ts, Time const &fixed_output_time)
NumLib::Time addTimeIncrement(std::vector< double > &delta_ts, std::size_t const repeat, double const delta_t, NumLib::Time const t_curr)
Returns sum of the newly added time increments.