BinaryFraction.hpp
Go to the documentation of this file.
2 // See LICENSE.txt for details.
3
4 /// \file
5 /// Defines class StepControllers::BinaryFraction
6
7 #pragma once
8
9 #include <algorithm>
10 #include <cmath>
11 #include <cstddef>
12 #include <pup.h>
13
14 #include "ErrorHandling/Assert.hpp"
15 #include "Options/Options.hpp"
17 #include "Time/Slab.hpp"
18 #include "Time/StepControllers/StepController.hpp" // IWYU pragma: keep
19 #include "Time/Time.hpp"
21 // IWYU pragma: no_include "Utilities/Rational.hpp"
22 #include "Utilities/TMPL.hpp"
23
24 namespace StepControllers {
25
26 /// \ingroup TimeSteppersGroup
27 ///
28 /// A StepController that chooses steps to be 1/2^n of a slab.
30  public:
31  /// \cond
32  BinaryFraction() = default;
33  explicit BinaryFraction(CkMigrateMessage* /*unused*/) noexcept {}
35  /// \endcond
36
37  using options = tmpl::list<>;
38  static constexpr OptionString help = {
39  "Chooses steps to be binary fractions of a slab"};
40
41  TimeDelta choose_step(const Time& time,
42  const double desired_step) const noexcept override {
43  ASSERT((time.fraction().denominator() &
44  (time.fraction().denominator() - 1)) == 0,
45  "Not at a binary-fraction time within slab: " << time.fraction());
46  const TimeDelta full_slab =
47  desired_step > 0 ? time.slab().duration() : -time.slab().duration();
48  const double desired_step_count = full_slab.value() / desired_step;
49  const size_t desired_step_power =
50  desired_step_count <= 1
51  ? 0
52  : static_cast<size_t>(std::ceil(std::log2(desired_step_count)));
53
54  // Ensure we will hit the slab boundary if we continue taking
55  // constant-sized steps.
56  const auto step_count =
57  std::max(static_cast<decltype(time.fraction().denominator())>(
58  two_to_the(desired_step_power)),
59  time.fraction().denominator());
60
61  return full_slab / step_count;
62  }
63 };
64 } // namespace StepControllers
A StepController that chooses steps to be 1/2^n of a slab.
Definition: BinaryFraction.hpp:29
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 base class StepController.
Defines classes and functions for making classes creatable from input files.
Defines macros to allow serialization of abstract template base classes.
#define ASSERT(a, m)
Assert that an expression should be true.
Definition: Assert.hpp:49
Defines Time and TimeDelta.
const char *const OptionString
The string used in option structs.
Definition: Options.hpp:26
StepControllers take desired step sizes (generally determined by StepChoosers) and convert them into ...
Definition: StepController.hpp:31
#define WRAPPED_PUPable_decl(className)
Wraps the Charm++ macro, see the Charm++ documentation.
Definition: CharmPupable.hpp:36
Holds all the StepControllers.
Definition: BinaryFraction.cpp:6
Represents an interval of time within a single slab.
Definition: Time.hpp:108
Defines class Slab.
constexpr T two_to_the(T n)
Compute 2 to the n for integral types.
Definition: ConstantExpressions.hpp:34
Define simple functions for constant expressions.
Defines macro ASSERT.
Wraps the template metaprogramming library used (brigand)
double value() const noexcept
Approximate numerical length of the interval.
Definition: Time.hpp:131