Filter.hpp
1 // Distributed under the MIT License.
2 // See LICENSE.txt for details.
3 
4 #pragma once
5 
6 #include <memory>
7 #include <pup.h>
8 #include <pup_stl.h>
9 #include <string>
10 #include <utility>
11 
13 #include "Evolution/EventsAndDenseTriggers/DenseTrigger.hpp"
14 #include "Options/Options.hpp"
16 #include "ParallelAlgorithms/EventsAndTriggers/Trigger.hpp"
17 #include "Utilities/Registration.hpp"
18 #include "Utilities/TMPL.hpp"
19 
20 namespace DenseTriggers {
21 /// \cond
22 template <typename DenseTriggerRegistrars>
23 class Filter;
24 /// \endcond
25 
26 namespace Registrars {
28 } // namespace Registrars
29 
30 /// \ingroup EventsAndTriggersGroup
31 /// %Filter activations of a dense trigger using a non-dense trigger.
32 ///
33 /// For example, to trigger every 10 starting at 100, one could use
34 ///
35 /// \snippet Test_Filter.cpp example
36 template <typename DenseTriggerRegistrars>
37 class Filter : public DenseTrigger<DenseTriggerRegistrars> {
38  public:
39  /// \cond
40  Filter() = default;
41  explicit Filter(CkMigrateMessage* const msg) noexcept
43  using PUP::able::register_constructor;
45  /// \endcond
46 
47  using Result = typename DenseTrigger<DenseTriggerRegistrars>::Result;
48 
49  struct TriggerOption {
50  static std::string name() noexcept { return "Trigger"; }
52  static constexpr Options::String help = "Dense trigger to filter";
53  };
54 
55  struct FilterOption {
56  static std::string name() noexcept { return "Filter"; }
58  static constexpr Options::String help = "Non-dense trigger to filter with";
59  };
60 
61  using options = tmpl::list<TriggerOption, FilterOption>;
62  static constexpr Options::String help =
63  "Filter activations of a dense trigger using a non-dense trigger.";
64 
66  std::unique_ptr<Trigger> filter) noexcept
67  : trigger_(std::move(trigger)), filter_(std::move(filter)) {}
68 
69  using is_triggered_argument_tags = tmpl::list<Tags::DataBox>;
70 
71  template <typename DbTags>
72  Result is_triggered(const db::DataBox<DbTags>& box) const noexcept {
73  auto result = trigger_->is_triggered(box);
74  if (not filter_->is_triggered(box)) {
75  result.is_triggered = false;
76  }
77  return result;
78  }
79 
80  using is_ready_argument_tags = tmpl::list<Tags::DataBox>;
81 
82  template <typename DbTags>
83  bool is_ready(const db::DataBox<DbTags>& box) const noexcept {
84  return trigger_->is_ready(box);
85  }
86 
87  // NOLINTNEXTLINE(google-runtime-references)
88  void pup(PUP::er& p) noexcept override {
90  p | trigger_;
91  p | filter_;
92  }
93 
94  private:
96  std::unique_ptr<Trigger> filter_{};
97 };
98 
99 /// \cond
100 template <typename DenseTriggerRegistrars>
101 PUP::able::PUP_ID Filter<DenseTriggerRegistrars>::my_PUP_ID = 0; // NOLINT
102 /// \endcond
103 } // namespace DenseTriggers
std::string
CharmPupable.hpp
utility
Options.hpp
DenseTriggers::Filter::TriggerOption
Definition: Filter.hpp:49
Registration::Registrar
A template for defining a registrar.
Definition: Registration.hpp:42
DenseTrigger
Definition: DenseTrigger.hpp:31
WRAPPED_PUPable_decl_template
#define WRAPPED_PUPable_decl_template(className)
Mark derived classes as serializable.
Definition: CharmPupable.hpp:22
DataBox.hpp
DenseTriggers::Filter::FilterOption
Definition: Filter.hpp:55
memory
DenseTrigger::Result
Result type for the is_triggered method.
Definition: DenseTrigger.hpp:38
Options::String
const char *const String
The string used in option structs.
Definition: Options.hpp:32
DenseTriggers::Filter
Definition: Filter.hpp:37
std::unique_ptr< DenseTrigger< DenseTriggerRegistrars > >
TMPL.hpp
string