EveryNIterations.hpp
1 // Distributed under the MIT License.
2 // See LICENSE.txt for details.
3 
4 #pragma once
5 
6 #include <cstdint>
7 #include <pup.h>
8 
10 #include "Options/Options.hpp"
12 #include "ParallelAlgorithms/EventsAndTriggers/Trigger.hpp"
13 #include "Utilities/Registration.hpp"
14 #include "Utilities/TMPL.hpp"
15 
16 namespace elliptic {
17 namespace Triggers {
18 /// \cond
19 template <typename IterationId, typename TriggerRegistrars>
20 class EveryNIterations;
21 /// \endcond
22 
23 namespace Registrars {
24 template <typename IterationId>
25 using EveryNIterations =
27 } // namespace Registrars
28 
29 /// \ingroup EventsAndTriggersGroup
30 /// Trigger every N iterations after a given offset.
31 template <typename IterationId,
32  typename TriggerRegistrars =
33  tmpl::list<Registrars::EveryNIterations<IterationId>>>
34 class EveryNIterations : public Trigger<TriggerRegistrars> {
35  public:
36  /// \cond
37  EveryNIterations() = default;
38  explicit EveryNIterations(CkMigrateMessage* /*unused*/) noexcept {}
39  using PUP::able::register_constructor;
41  /// \endcond
42 
43  struct N {
44  using type = uint64_t;
45  static constexpr OptionString help{"How frequently to trigger."};
46  static type lower_bound() noexcept { return 1; }
47  };
48  struct Offset {
49  using type = uint64_t;
50  static constexpr OptionString help{"First iteration to trigger on."};
51  static type default_value() noexcept { return 0; }
52  };
53 
54  using options = tmpl::list<N, Offset>;
55  static constexpr OptionString help{
56  "Trigger every N iterations after a given offset."};
57 
58  EveryNIterations(const uint64_t interval, const uint64_t offset) noexcept
59  : interval_(interval), offset_(offset) {}
60 
61  using argument_tags = tmpl::list<IterationId>;
62 
63  bool operator()(const db::const_item_type<IterationId>& iteration_id) const
64  noexcept {
65  const auto step_number = static_cast<uint64_t>(iteration_id);
66  return step_number >= offset_ and (step_number - offset_) % interval_ == 0;
67  }
68 
69  // clang-tidy: google-runtime-references
70  void pup(PUP::er& p) noexcept { // NOLINT
71  p | interval_;
72  p | offset_;
73  }
74 
75  private:
76  uint64_t interval_{0};
77  uint64_t offset_{0};
78 };
79 
80 /// \cond
81 template <typename IterationId, typename TriggerRegistrars>
83  0; // NOLINT
84 /// \endcond
85 } // namespace Triggers
86 } // namespace elliptic
Definition: EveryNIterations.hpp:48
Definition: InitializeAnalyticSolution.hpp:27
Defines classes and functions for making classes creatable from input files.
Trigger every N iterations after a given offset.
Definition: EveryNIterations.hpp:34
Defines macros to allow serialization of abstract template base classes.
#define WRAPPED_PUPable_decl_template(className)
Mark derived classes as serializable.
Definition: CharmPupable.hpp:22
const char *const OptionString
The string used in option structs.
Definition: Options.hpp:29
A template for defining a registrar.
Definition: Registration.hpp:42
Wraps the template metaprogramming library used (brigand)
Defines classes SimpleTag, PrefixTag, ComputeTag and several functions for retrieving tag info...
Base class for checking whether to run an Event.
Definition: Trigger.hpp:34
Definition: LogicalTriggers.hpp:13
Definition: EveryNIterations.hpp:43