OGS
CreateFixedTimeStepping.cpp
Go to the documentation of this file.
1 
13 
14 #include <string>
15 
16 #include "BaseLib/ConfigTree.h"
17 #include "BaseLib/Error.h"
18 #include "FixedTimeStepping.h"
19 #include "TimeStepAlgorithm.h"
20 
21 namespace NumLib
22 {
23 class TimeStepAlgorithm;
24 std::unique_ptr<TimeStepAlgorithm> createFixedTimeStepping(
25  BaseLib::ConfigTree const& config)
26 {
28  config.checkConfigParameter("type", "FixedTimeStepping");
29 
31  auto const t_initial = config.getConfigParameter<double>("t_initial");
33  auto const t_end = config.getConfigParameter<double>("t_end");
35  auto const delta_ts = config.getConfigSubtree("timesteps");
36 
37  std::vector<double> timesteps;
38  double t_curr = t_initial;
39  double delta_t = 0.0;
40 
41  // TODO: consider adding call "listNonEmpty" to config tree
43  auto const range = delta_ts.getConfigSubtreeList("pair");
44  if (range.begin() == range.end())
45  {
46  OGS_FATAL("no timesteps have been given");
47  }
48  for (auto const pair : range)
49  {
51  auto const repeat = pair.getConfigParameter<std::size_t>("repeat");
53  delta_t = pair.getConfigParameter<double>("delta_t");
54 
55  if (repeat == 0)
56  {
57  OGS_FATAL("<repeat> is zero.");
58  }
59  if (delta_t <= 0.0)
60  {
61  OGS_FATAL("timestep <delta_t> is <= 0.0.");
62  }
63 
64  if (t_curr <= t_end)
65  {
66  auto const new_size = timesteps.size() + repeat;
67  try
68  {
69  timesteps.resize(new_size, delta_t);
70  }
71  catch (std::length_error const& e)
72  {
73  OGS_FATAL(
74  "Resize of the time steps vector failed for the requested "
75  "new size {:d}. Probably there is not enough memory ({:g} "
76  "GiB requested).\n"
77  "Thrown exception: {:s}",
78  new_size,
79  new_size * sizeof(double) / 1024. / 1024. / 1024.,
80  e.what());
81  }
82  catch (std::bad_alloc const& e)
83  {
84  OGS_FATAL(
85  "Resize of the time steps vector failed for the requested "
86  "new size {:d}. Probably there is not enough memory ({:g} "
87  "GiB requested).\n"
88  "Thrown exception: {:s}",
89  new_size, new_size * sizeof(double) / 1024. / 1024. / 1024.,
90  e.what());
91  }
92 
93  t_curr += repeat * delta_t;
94  }
95  }
96 
97  // append last delta_t until t_end is reached
98  if (t_curr <= t_end)
99  {
100  auto const repeat =
101  static_cast<std::size_t>(std::ceil((t_end - t_curr) / delta_t));
102  auto const new_size = timesteps.size() + repeat;
103  try
104  {
105  timesteps.resize(new_size, delta_t);
106  }
107  catch (std::length_error const& e)
108  {
109  OGS_FATAL(
110  "Resize of the time steps vector failed for the requested new "
111  "size {:d}. Probably there is not enough memory ({:g} GiB "
112  "requested).\n"
113  "Thrown exception: {:s}",
114  new_size,
115  new_size * sizeof(double) / 1024. / 1024. / 1024.,
116  e.what());
117  }
118  catch (std::bad_alloc const& e)
119  {
120  OGS_FATAL(
121  "Resize of the time steps vector failed for the requested new "
122  "size {:d}. Probably there is not enough memory ({:g} GiB "
123  "requested).\n"
124  "Thrown exception: {:s}",
125  new_size, new_size * sizeof(double) / 1024. / 1024. / 1024.,
126  e.what());
127  }
128  }
129 
130  return std::make_unique<FixedTimeStepping>(t_initial, t_end, timesteps);
131 }
132 } // end of namespace NumLib
#define OGS_FATAL(...)
Definition: Error.h:26
void checkConfigParameter(std::string const &param, T const &value) const
T getConfigParameter(std::string const &param) const
Range< SubtreeIterator > getConfigSubtreeList(std::string const &root) const
Definition: ConfigTree.cpp:169
ConfigTree getConfigSubtree(std::string const &root) const
Definition: ConfigTree.cpp:146
std::unique_ptr< TimeStepAlgorithm > createFixedTimeStepping(BaseLib::ConfigTree const &config)