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"
9#include "TimeStepAlgorithm.h"
10
11namespace NumLib
12{
14 BaseLib::ConfigTree const& config)
15{
17 config.checkConfigParameter("type", "FixedTimeStepping");
18
20 auto const t_initial = config.getConfigParameter<double>("t_initial");
22 auto const t_end = config.getConfigParameter<double>("t_end");
23
25 auto const n_steps = config.getConfigParameterOptional<int>("n_steps");
26 if (n_steps.has_value())
27 {
28 if (t_end <= t_initial)
29 {
31 "Creating linearly spaced time steps vector using "
32 "FixedTimeStepping algorithm failed! "
33 "User provided start value (t_initial) "
34 "{} is not smaller then end value (t_end) {}.",
35 t_initial, t_end);
36 }
37
38 if (*n_steps <= 0)
39 {
41 "Requested number of time steps in time steps vector "
42 "(n_steps) must be greater then 0. "
43 "{} time steps were requested",
44 *n_steps);
45 }
46 // Create the RepeatDtPair
47 double const t_step =
48 (t_end - t_initial) / static_cast<double>(*n_steps);
49 std::vector const repeat_pairs = {
50 RepeatDtPair{static_cast<std::size_t>(*n_steps), t_step}};
51 return {t_initial, t_end, repeat_pairs};
52 }
53
55 auto const delta_ts_config = config.getConfigSubtree("timesteps");
56
57 // TODO: consider adding call "listNonEmpty" to config tree
59 auto const range = delta_ts_config.getConfigSubtreeList("pair");
60 if (range.begin() == range.end())
61 {
62 OGS_FATAL("no timesteps have been given");
63 }
64
65 std::vector<RepeatDtPair> repeat_dt_pairs;
66 for (auto const pair : range)
67 {
68 repeat_dt_pairs.emplace_back(
70 pair.getConfigParameter<std::size_t>("repeat"),
72 pair.getConfigParameter<double>("delta_t"));
73 }
74
75 return {t_initial, t_end, repeat_dt_pairs};
76}
77
78std::unique_ptr<TimeStepAlgorithm> createFixedTimeStepping(
79 FixedTimeSteppingParameters const& parameters,
80 std::vector<double> const& fixed_times_for_output)
81{
82 if (parameters.t_end < parameters.t_initial)
83 {
85 "fixed timestepping: end time ({}) is smaller than initial time "
86 "({})",
87 parameters.t_end,
88 parameters.t_initial);
89 }
90
92 {
94 "CreateFixedTimeStepping: invalid specification of (repeat, "
95 "delta_t) pairs");
96 }
97
98 return std::make_unique<FixedTimeStepping>(parameters.t_initial,
99 parameters.t_end,
100 parameters.repeat_dt_pairs,
101 fixed_times_for_output);
102}
103} // end of namespace NumLib
#define OGS_FATAL(...)
Definition Error.h:19
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