Times.hpp
1 // Distributed under the MIT License.
2 // See LICENSE.txt for details.
3 
4 #pragma once
5 
6 #include <cmath>
7 #include <memory>
8 #include <pup.h>
9 #include <pup_stl.h>
10 #include <utility>
11 
12 #include "Options/Options.hpp"
13 #include "Options/ParseOptions.hpp"
15 #include "ParallelAlgorithms/EventsAndTriggers/Trigger.hpp"
16 #include "Time/TimeSequence.hpp"
17 #include "Time/TimeStepId.hpp"
18 #include "Time/Utilities.hpp"
19 #include "Utilities/TMPL.hpp"
20 
21 /// \cond
22 namespace Tags {
23 struct Time;
24 struct TimeStepId;
25 } // namespace Tags
26 /// \endcond
27 
28 namespace Triggers {
29 /// \ingroup EventsAndTriggersGroup
30 /// \ingroup TimeGroup
31 /// Trigger at particular times.
32 ///
33 /// \warning This trigger will only fire if it is actually checked at
34 /// the times specified. The StepToTimes StepChooser can be useful
35 /// for this.
36 class Times : public Trigger {
37  public:
38  /// \cond
39  Times() = default;
40  explicit Times(CkMigrateMessage* /*unused*/) noexcept {}
41  using PUP::able::register_constructor;
43  /// \endcond
44 
45  static constexpr Options::String help{"Trigger at particular times."};
46 
47  explicit Times(std::unique_ptr<TimeSequence<double>> times) noexcept
48  : times_(std::move(times)) {}
49 
50  using argument_tags = tmpl::list<Tags::Time, Tags::TimeStepId>;
51 
52  bool operator()(const double now, const TimeStepId& time_id) const noexcept {
53  const auto& substep_time = time_id.substep_time();
54  // Trying to step to a given time might not get us exactly there
55  // because of rounding errors.
56  const double sloppiness = slab_rounding_error(substep_time);
57 
58  const auto nearby_time = times_->times_near(now)[1];
59  return nearby_time and std::abs(*nearby_time - now) < sloppiness;
60  }
61 
62  // NOLINTNEXTLINE(google-runtime-references)
63  void pup(PUP::er& p) noexcept override { p | times_; }
64 
65  private:
67 };
68 } // namespace Triggers
69 
70 template <>
71 struct Options::create_from_yaml<Triggers::Times> {
72  template <typename Metavariables>
73  static Triggers::Times create(const Option& options) {
74  return Triggers::Times(
76  }
77 };
CharmPupable.hpp
utility
Options.hpp
slab_rounding_error
double slab_rounding_error(const Time &time) noexcept
ParseOptions.hpp
cmath
Triggers::Times
Definition: Times.hpp:36
Options::Option
Definition: Options.hpp:108
TimeSequence
Represents a sequence of times.
Definition: TimeSequence.hpp:31
WRAPPED_PUPable_decl_template
#define WRAPPED_PUPable_decl_template(className)
Mark derived classes as serializable.
Definition: CharmPupable.hpp:22
Options::create_from_yaml
Definition: MinmodType.hpp:11
Trigger
Definition: Trigger.hpp:17
memory
TimeStepId
Definition: TimeStepId.hpp:25
TimeStepId.hpp
Options::Option::parse_as
T parse_as() const
Convert to an object of type T.
Definition: ParseOptions.hpp:93
Time
Definition: Time.hpp:29
Options::String
const char *const String
The string used in option structs.
Definition: Options.hpp:32
TimeStepId::substep_time
const Time & substep_time() const noexcept
Time of the current substep.
Definition: TimeStepId.hpp:60
std::unique_ptr
TMPL.hpp