OGS
NumLib::FixedTimeStepping Class Referencefinal

Detailed Description

Fixed time stepping algorithm.

This algorithm returns time step size defined by a user priori.

If the prescribed time step sizes are used up before the end time is reached—which happens if the steps actually taken are smaller than the prescribed ones, as in the staggered coupling scheme—the last prescribed time step size taken by the scheme is repeated until the end time.

Definition at line 25 of file FixedTimeStepping.h.

#include <FixedTimeStepping.h>

Inheritance diagram for NumLib::FixedTimeStepping:
[legend]
Collaboration diagram for NumLib::FixedTimeStepping:
[legend]

Public Member Functions

 FixedTimeStepping (double t0, double t_end, double dt)
 FixedTimeStepping (double t0, double tn, std::vector< RepeatDtPair > const &repeat_dt_pairs, 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
Public Member Functions inherited from NumLib::TimeStepAlgorithm
 TimeStepAlgorithm (const double t0, const double t_end)
virtual ~TimeStepAlgorithm ()=default
Time begin () const
 return the beginning of time steps
Time end () const
 return the end of time steps
virtual bool isSolutionErrorComputationNeeded () const
virtual bool canReduceTimestepSize (NumLib::TimeStep const &, NumLib::TimeStep const &) const
 Query the timestepper if further time step size reduction is possible.

Static Public Member Functions

static bool areRepeatDtPairsValid (std::vector< RepeatDtPair > const &repeat_dt_pairs)

Private Attributes

std::vector< double > dt_vector_
 a vector of time step sizes
double last_prescribed_dt_ = 0.0
bool reuse_of_last_dt_reported_ = false

Additional Inherited Members

Protected Attributes inherited from NumLib::TimeStepAlgorithm
const Time _t_initial
 initial time
const Time _t_end
 end time

Constructor & Destructor Documentation

◆ FixedTimeStepping() [1/2]

NumLib::FixedTimeStepping::FixedTimeStepping ( double t0,
double t_end,
double dt )

Constructor with homogeneous time step size

A user provides a single time step size \(\Delta t\). Total number of time steps is calculated by

\[ n=\frac{t_{\rm n} - t_0}{\Delta t} \]

.

Precondition
t_end is larger than t0 and dt is positive; both are required for a non-empty sequence of time steps and are checked with OGS_FATAL.
Parameters
t0start time
t_endend time
dtuniform time step size

Definition at line 204 of file FixedTimeStepping.cpp.

205 : TimeStepAlgorithm(t0, t_end), last_prescribed_dt_(dt)
206{
207 // Checked before the cast below, which is undefined for a negative value.
208 if (Time(t_end) <= Time(t0) || dt <= 0.0)
209 {
210 OGS_FATAL(
211 "FixedTimeStepping: No time steps can be generated for the time "
212 "interval [{}, {}] with the time step size {:g}. The end time must "
213 "be larger than the start time and the time step size must be "
214 "positive.",
215 t0, t_end, dt);
216 }
217
218 auto const new_size =
219 static_cast<std::size_t>(std::ceil((t_end - t0) / dt));
220 try
221 {
222 dt_vector_ = std::vector<double>(new_size, dt);
223 }
224 catch (std::length_error const& e)
225 {
226 OGS_FATAL(
227 "Resize of the time steps vector failed for the requested new "
228 "size {}. Probably there is not enough memory ({:g} GiB "
229 "requested).\n"
230 "Thrown exception: {}",
231 new_size, new_size * sizeof(double) / 1024. / 1024. / 1024.,
232 e.what());
233 }
234 catch (std::bad_alloc const& e)
235 {
236 OGS_FATAL(
237 "Allocation of the time steps vector failed for the requested "
238 "size {}. Probably there is not enough memory ({:g} GiB "
239 "requested).\n"
240 "Thrown exception: {}",
241 new_size,
242 new_size * sizeof(double) / 1024. / 1024. / 1024.,
243 e.what());
244 }
245}
#define OGS_FATAL(...)
Definition Error.h:10
std::vector< double > dt_vector_
a vector of time step sizes
TimeStepAlgorithm(const double t0, const double t_end)

References NumLib::TimeStepAlgorithm::TimeStepAlgorithm(), dt_vector_, last_prescribed_dt_, and OGS_FATAL.

◆ FixedTimeStepping() [2/2]

NumLib::FixedTimeStepping::FixedTimeStepping ( double t0,
double tn,
std::vector< RepeatDtPair > const & repeat_dt_pairs,
std::vector< double > const & fixed_times_for_output )

Constructor with user-specified time step sizes including additional time steps for output.

Precondition
tn is larger than t0 and repeat_dt_pairs is valid in the sense of areRepeatDtPairsValid(), i.e. non-empty with positive delta_t and non-zero repeat entries; both are required for a non-empty sequence of time steps and are checked with OGS_FATAL.
Parameters
t0start time
tnend time
repeat_dt_pairspairs of the number of repetitions and the time step size
fixed_times_for_outputtimes at which output is requested; the time step containing such a time is split at it

Definition at line 143 of file FixedTimeStepping.cpp.

146 : TimeStepAlgorithm(t0, tn)
147{
148 Time t_curr = _t_initial;
149
150 if (Time(tn) <= Time(t0))
151 {
152 OGS_FATAL(
153 "FixedTimeStepping: No time steps can be generated for the time "
154 "interval [{}, {}]. The end time must be larger than the start "
155 "time.",
156 t0, tn);
157 }
158
159 if (!areRepeatDtPairsValid(repeat_dt_pairs))
160 {
161 OGS_FATAL("FixedTimeStepping: Couldn't construct object from data");
162 }
163
164 for (auto const& [repeat, delta_t] : repeat_dt_pairs)
165 {
166 if (t_curr <= _t_end)
167 {
168 t_curr = addTimeIncrement(dt_vector_, repeat, delta_t, t_curr);
169 last_prescribed_dt_ = delta_t;
170 }
171 }
172
173 // append last delta_t until t_end is reached
174 if (t_curr <= _t_end)
175 {
176 auto const repeat = static_cast<std::size_t>(
177 std::ceil((_t_end() - t_curr()) / last_prescribed_dt_));
179 }
180
181 // The non-empty interval and areRepeatDtPairsValid(), which guarantees at
182 // least one pair with <repeat> >= 1 and <delta_t> > 0, imply that the loop
183 // above has appended at least one step size and has set
184 // last_prescribed_dt_.
185 if (dt_vector_.empty())
186 {
187 OGS_FATAL(
188 "FixedTimeStepping: No time steps were generated for the time "
189 "interval [{}, {}].",
190 t0, tn);
191 }
192 if (last_prescribed_dt_ <= 0.0)
193 {
194 OGS_FATAL(
195 "FixedTimeStepping: The last prescribed time step size is {:g}, "
196 "but must be positive.",
198 }
199
201 fixed_times_for_output);
202}
static bool areRepeatDtPairsValid(std::vector< RepeatDtPair > const &repeat_dt_pairs)
const Time _t_initial
initial time
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)
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.

References NumLib::TimeStepAlgorithm::TimeStepAlgorithm(), NumLib::TimeStepAlgorithm::_t_end, NumLib::TimeStepAlgorithm::_t_initial, areRepeatDtPairsValid(), dt_vector_, NumLib::incorporateFixedTimesForOutput(), last_prescribed_dt_, and OGS_FATAL.

Member Function Documentation

◆ areRepeatDtPairsValid()

bool NumLib::FixedTimeStepping::areRepeatDtPairsValid ( std::vector< RepeatDtPair > const & repeat_dt_pairs)
static

Definition at line 292 of file FixedTimeStepping.cpp.

294{
295 if (repeat_dt_pairs.empty())
296 {
297 return false;
298 }
299
300 for (auto const& [repeat, delta_t] : repeat_dt_pairs)
301 {
302 if (repeat == 0)
303 {
304 ERR("FixedTimeStepping: <repeat> is zero.");
305 return false;
306 }
307 if (delta_t <= 0.0)
308 {
309 ERR("FixedTimeStepping: timestep <delta_t> is <= 0.0.");
310 return false;
311 }
312 }
313 return true;
314}
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:40

References ERR().

Referenced by FixedTimeStepping(), and NumLib::createFixedTimeStepping().

◆ next()

double NumLib::FixedTimeStepping::next ( double solution_error,
int number_iterations,
NumLib::TimeStep & ts_previous,
NumLib::TimeStep & ts_current )
overridevirtual

Move to the next time step

Parameters
solution_errorSolution error \(e_n\) between two successive time steps.
number_iterationsNumber of non-linear iterations used.
ts_previousthe previous time step used to compute the size of the next step
ts_currentthe current time step used to compute the size of the next step
Returns
the computed step size.

Implements NumLib::TimeStepAlgorithm.

Definition at line 247 of file FixedTimeStepping.cpp.

251{
252 // check if last time step
253 if (ts_current.current() >= end())
254 {
255 return 0.0;
256 }
257
258 // The prescribed step sizes can be used up before t_end is reached if the
259 // steps actually taken are smaller than the prescribed ones. This is the
260 // case in the staggered coupling scheme, where all processes advance with
261 // the minimum of the step sizes of all processes, but each process consumes
262 // one of its own prescribed step sizes per time step. The last prescribed
263 // step size is repeated then, such that this process does not restrict the
264 // step size of the other processes any more.
265 if (ts_current.timeStepNumber() >= dt_vector_.size())
266 {
268 {
270 WARN(
271 "FixedTimeStepping: All {:d} time step sizes of the fixed time "
272 "stepping scheme are used up at time {} before the end time "
273 "{} is reached. The last prescribed time step size of {:g} "
274 "will be repeated until the end time. Please check whether the "
275 "prescribed number of time steps is large enough for the "
276 "number of time steps that are actually taken.",
277 dt_vector_.size(), ts_current.current()(), end()(),
279 }
280 return std::min(last_prescribed_dt_, end()() - ts_current.current()());
281 }
282
283 double dt = dt_vector_[ts_current.timeStepNumber()];
284 if (ts_current.current() + dt > end())
285 { // upper bound by t_end
286 dt = end()() - ts_current.current()();
287 }
288
289 return dt;
290}
void WARN(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:34
Time end() const
return the end of time steps
Time current() const
return current time step
Definition TimeStep.h:71
std::size_t timeStepNumber() const
the time step number
Definition TimeStep.h:75

References NumLib::TimeStep::current(), dt_vector_, NumLib::TimeStepAlgorithm::end(), last_prescribed_dt_, reuse_of_last_dt_reported_, NumLib::TimeStep::timeStepNumber(), and WARN().

Member Data Documentation

◆ dt_vector_

std::vector<double> NumLib::FixedTimeStepping::dt_vector_
private

a vector of time step sizes

Definition at line 76 of file FixedTimeStepping.h.

Referenced by FixedTimeStepping(), FixedTimeStepping(), and next().

◆ last_prescribed_dt_

double NumLib::FixedTimeStepping::last_prescribed_dt_ = 0.0
private

The size of the last time step the scheme takes as prescribed by the user. Kept separately from dt_vector_ because the latter's last entry is not necessarily a prescribed size: incorporateFixedTimesForOutput() splits the interval containing a fixed output time into two smaller ones. It is not the last (repeat, delta_t) pair's size either, because pairs whose steps start beyond the end time are not part of the scheme.

Definition at line 84 of file FixedTimeStepping.h.

Referenced by FixedTimeStepping(), FixedTimeStepping(), and next().

◆ reuse_of_last_dt_reported_

bool NumLib::FixedTimeStepping::reuse_of_last_dt_reported_ = false
private

set if it has already been reported that the prescribed time step sizes are used up and the last one is repeated, in order to report it once only.

Definition at line 89 of file FixedTimeStepping.h.

Referenced by next().


The documentation for this class was generated from the following files: