OGS
CreateFixedTimeStepping.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
5
7#include "BaseLib/Error.h"
8#include "FixedTimeStepping.h"
10#include "TimeStepAlgorithm.h"
11
12namespace NumLib
13{
15 BaseLib::ConfigTree const& config)
16{
18 config.checkConfigParameter("type", "FixedTimeStepping");
19
21 auto const t_initial = config.getConfigParameter<double>("t_initial");
23 auto const t_end = config.getConfigParameter<double>("t_end");
24
26 auto const n_steps = config.getConfigParameterOptional<int>("n_steps");
27 if (n_steps.has_value())
28 {
29 if (t_end <= t_initial)
30 {
32 "Creating linearly spaced time steps vector using "
33 "FixedTimeStepping algorithm failed! "
34 "User provided start value (t_initial) "
35 "{} is not smaller then end value (t_end) {}.",
36 t_initial, t_end);
37 }
38
39 if (*n_steps <= 0)
40 {
42 "Requested number of time steps in time steps vector "
43 "(n_steps) must be greater then 0. "
44 "{} time steps were requested",
45 *n_steps);
46 }
47 // Create the RepeatDtPair
48 double const t_step =
49 (t_end - t_initial) / static_cast<double>(*n_steps);
50 std::vector const repeat_pairs = {
51 RepeatDtPair{static_cast<std::size_t>(*n_steps), t_step}};
52 return {t_initial, t_end, repeat_pairs};
53 }
54
56 auto const delta_ts_config = config.getConfigSubtree("timesteps");
57
58 // TODO: consider adding call "listNonEmpty" to config tree
60 auto const range = delta_ts_config.getConfigSubtreeList("pair");
61 if (range.begin() == range.end())
62 {
63 OGS_FATAL("no timesteps have been given");
64 }
65
66 std::vector<RepeatDtPair> repeat_dt_pairs;
67 for (auto const pair : range)
68 {
69 repeat_dt_pairs.emplace_back(
71 pair.getConfigParameter<std::size_t>("repeat"),
73 pair.getConfigParameter<double>("delta_t"));
74 }
75
76 return {t_initial, t_end, repeat_dt_pairs};
77}
78
79std::unique_ptr<TimeStepAlgorithm> createFixedTimeStepping(
80 FixedTimeSteppingParameters const& parameters,
81 std::vector<double> const& fixed_times_for_output)
82{
83 // Same condition as in the FixedTimeStepping constructor, but reported
84 // with the parsed parameters as context.
85 if (Time(parameters.t_end) <= Time(parameters.t_initial))
86 {
88 "fixed timestepping: end time ({}) must be larger than initial "
89 "time ({})",
90 parameters.t_end,
91 parameters.t_initial);
92 }
93
95 {
97 "CreateFixedTimeStepping: invalid specification of (repeat, "
98 "delta_t) pairs");
99 }
100
101 return std::make_unique<FixedTimeStepping>(parameters.t_initial,
102 parameters.t_end,
103 parameters.repeat_dt_pairs,
104 fixed_times_for_output);
105}
106} // end of namespace NumLib
#define OGS_FATAL(...)
Definition Error.h:10
std::optional< T > getConfigParameterOptional(std::string const &param) const
T getConfigParameter(std::string const &param) const
Range< SubtreeIterator > getConfigSubtreeList(std::string const &root) const
ConfigTree getConfigSubtree(std::string const &root) const
void checkConfigParameter(std::string const &param, std::string_view const value) const
static bool areRepeatDtPairsValid(std::vector< RepeatDtPair > const &repeat_dt_pairs)
FixedTimeSteppingParameters parseFixedTimeStepping(BaseLib::ConfigTree const &config)
std::tuple< std::size_t, double > RepeatDtPair
std::unique_ptr< TimeStepAlgorithm > createFixedTimeStepping(FixedTimeSteppingParameters const &parameters, std::vector< double > const &fixed_times_for_output)
std::vector< RepeatDtPair > repeat_dt_pairs