PreventRapidIncrease.hpp
1 // Distributed under the MIT License.
2 // See LICENSE.txt for details.
3 
4 #pragma once
5 
6 #include <cmath> // IWYU pragma: keep // for abs
7 #include <limits>
8 #include <pup.h>
9 
10 #include "Options/Options.hpp"
12 #include "Time/StepChoosers/StepChooser.hpp" // IWYU pragma: keep
13 #include "Time/Tags.hpp"
14 #include "Utilities/Registration.hpp"
15 #include "Utilities/TMPL.hpp"
16 
17 /// \cond
18 class Time;
19 namespace Parallel {
20 template <typename Metavariables>
21 class ConstGlobalCache;
22 } // namespace Parallel
23 /// \endcond
24 
25 namespace StepChoosers {
26 template <typename StepChooserRegistrars>
28 
29 namespace Registrars {
32 } // namespace Registrars
33 
34 /// Avoids instabilities due to rapid increases in the step size by
35 /// preventing the step size from increasing unless all steps in the
36 /// time-stepper history are the same size. If there have been recent
37 /// step size changes the new size bound is the size of the most
38 /// recent step, otherwise it is infinite (no restriction is imposed).
39 ///
40 /// Changes in step size resulting from a slab size change are not
41 /// taken into account. In practice, this should not be an issue as
42 /// long as there are many steps between slab size changes.
43 template <typename StepChooserRegistrars =
44  tmpl::list<Registrars::PreventRapidIncrease>>
45 class PreventRapidIncrease : public StepChooser<StepChooserRegistrars> {
46  public:
47  /// \cond
48  PreventRapidIncrease() = default;
49  explicit PreventRapidIncrease(CkMigrateMessage* /*unused*/) noexcept {}
50  using PUP::able::register_constructor;
52  /// \endcond
53 
54  static constexpr OptionString help{
55  "Prevents rapid increases in time step that can cause integrator \n"
56  "instabilities."};
57  using options = tmpl::list<>;
58 
59  using argument_tags =
60  tmpl::list<Tags::SubstepTime, Tags::HistoryEvolvedVariables<>>;
61 
62  template <typename Metavariables, typename History>
63  double operator()(const Time& time, const History& history,
65  const noexcept {
66  if (history.size() < 2) {
68  }
69 
70  const auto last_step = time - history[history.size() - 1];
71 
72  // Slab boundaries are complicated, so we'll just ignore the slab
73  // information and assume slab sizes don't change too frequently.
74  // An occasional double increase should not be harmful.
75  for (auto step = history.begin(); step != history.end() - 1; ++step) {
76  if ((*(step + 1) - *step).fraction() != last_step.fraction()) {
77  return abs(last_step.value());
78  }
79  }
80  // Request that the step size be at most infinity. This imposes
81  // no restriction on the chosen step.
83  }
84 };
85 
86 /// \cond
87 template <typename StepChooserRegistrars>
89  0; // NOLINT
90 /// \endcond
91 } // namespace StepChoosers
The time in a simulation. Times can be safely compared for exact equality as long as they do not belo...
Definition: Time.hpp:31
Defines classes and functions for making classes creatable from input files.
Defines macros to allow serialization of abstract template base classes.
Contains functions that forward to Charm++ parallel functions.
Definition: Abort.hpp:13
#define WRAPPED_PUPable_decl_template(className)
Mark derived classes as serializable.
Definition: CharmPupable.hpp:22
StepChoosers suggest upper bounds on step sizes. Concrete StepChoosers should define operator() retur...
Definition: StepChooser.hpp:45
const char *const OptionString
The string used in option structs.
Definition: Options.hpp:29
Avoids instabilities due to rapid increases in the step size by preventing the step size from increas...
Definition: PreventRapidIncrease.hpp:27
T infinity(T... args)
A template for defining a registrar.
Definition: Registration.hpp:42
A Charm++ chare that caches constant data once per Charm++ node.
Definition: ConstGlobalCache.hpp:135
Wraps the template metaprogramming library used (brigand)
Defines tags related to Time quantities.
Holds all the StepChoosers.
Definition: ByBlock.hpp:31