InterpolationTargetTestHelpers.hpp
1 // Distributed under the MIT License.
2 // See LICENSE.txt for details.
3 
4 #pragma once
5 
7 
8 #include <boost/optional.hpp>
9 #include <cstddef>
10 #include <utility>
11 #include <vector>
12 
16 #include "Domain/Tags.hpp"
17 #include "Framework/ActionTesting.hpp"
18 #include "NumericalAlgorithms/Interpolation/InitializeInterpolationTarget.hpp"
19 #include "NumericalAlgorithms/Interpolation/InitializeInterpolator.hpp"
20 #include "NumericalAlgorithms/Interpolation/InterpolatedVars.hpp"
21 #include "NumericalAlgorithms/Interpolation/SendPointsToInterpolator.hpp"
22 #include "Parallel/ParallelComponentHelpers.hpp"
23 #include "Time/Slab.hpp"
24 #include "Time/Time.hpp"
25 #include "Time/TimeStepId.hpp"
26 #include "Utilities/Gsl.hpp"
27 #include "Utilities/Requires.hpp"
28 #include "Utilities/TMPL.hpp"
29 #include "Utilities/TaggedTuple.hpp"
30 
31 /// \cond
32 namespace intrp {
33 namespace Actions {
34 template <typename InterpolationTargetTag>
35 struct ReceivePoints;
36 } // namespace Actions
37 } // namespace intrp
38 template <typename IdType, typename DataType>
39 class IdPair;
40 namespace Parallel {
41 template <typename Metavariables>
42 class ConstGlobalCache;
43 } // namespace Parallel
44 namespace db {
45 template <typename TagsList>
46 class DataBox;
47 } // namespace db
48 namespace intrp {
49 namespace Tags {
50 template <typename TemporalId>
51 struct IndicesOfFilledInterpPoints;
52 template <typename TemporalId>
53 struct InterpolatedVarsHolders;
54 struct NumberOfElements;
55 } // namespace Tags
56 } // namespace intrp
57 namespace domain {
58 class BlockId;
59 } // namespace domain
60 /// \endcond
61 
62 namespace InterpTargetTestHelpers {
63 
64 template <typename Metavariables, typename InterpolationTargetTag>
66  using metavariables = Metavariables;
68  using array_index = size_t;
69  using component_being_mocked = void; // not needed.
70  using const_global_cache_tags = tmpl::flatten<tmpl::append<
72  typename Metavariables::InterpolationTargetA::compute_target_points>>,
73  tmpl::list<domain::Tags::Domain<Metavariables::volume_dim>>>>;
74  using phase_dependent_action_list = tmpl::list<
76  typename Metavariables::Phase, Metavariables::Phase::Initialization,
78  Metavariables, InterpolationTargetTag>>>,
79  Parallel::PhaseActions<typename Metavariables::Phase,
80  Metavariables::Phase::Testing, tmpl::list<>>>;
81 };
82 
83 template <typename InterpolationTargetTag>
85  template <
86  typename ParallelComponent, typename DbTags, typename Metavariables,
87  typename ArrayIndex, size_t VolumeDim,
89  nullptr>
90  static void apply(
93  const ArrayIndex& /*array_index*/,
94  const typename Metavariables::temporal_id::type& temporal_id,
95  std::vector<boost::optional<
97  tnsr::I<double, VolumeDim, typename Frame::Logical>>>>&&
98  block_coord_holders) noexcept {
99  db::mutate<intrp::Tags::InterpolatedVarsHolders<Metavariables>>(
100  make_not_null(&box),
101  [
102  &temporal_id, &block_coord_holders
103  ](const gsl::not_null<
105  vars_holders) noexcept {
106  auto& vars_infos =
107  get<intrp::Vars::HolderTag<InterpolationTargetTag,
108  Metavariables>>(*vars_holders)
109  .infos;
110 
111  // Add the target interpolation points at this timestep.
112  vars_infos.emplace(std::make_pair(
113  temporal_id,
114  intrp::Vars::Info<VolumeDim, typename InterpolationTargetTag::
115  vars_to_interpolate_to_target>{
116  std::move(block_coord_holders)}));
117  });
118  }
119 };
120 
121 template <typename Metavariables>
123  using metavariables = Metavariables;
125  using array_index = size_t;
126  using phase_dependent_action_list = tmpl::list<
128  typename Metavariables::Phase, Metavariables::Phase::Initialization,
129  tmpl::list<intrp::Actions::InitializeInterpolator>>,
130  Parallel::PhaseActions<typename Metavariables::Phase,
131  Metavariables::Phase::Testing, tmpl::list<>>>;
132 
134  using replace_these_simple_actions = tmpl::list<intrp::Actions::ReceivePoints<
135  typename Metavariables::InterpolationTargetA>>;
136  using with_these_simple_actions = tmpl::list<
138 };
139 
140 template <typename MetaVariables, typename InterpolationTargetOptionTag,
141  typename DomainCreator, typename BlockCoordHolder>
142 void test_interpolation_target(
143  const DomainCreator& domain_creator,
144  typename InterpolationTargetOptionTag::type options,
145  const BlockCoordHolder& expected_block_coord_holders) noexcept {
146  using metavars = MetaVariables;
147  using temporal_id_type = typename metavars::temporal_id::type;
148  using target_component =
149  mock_interpolation_target<metavars,
150  typename metavars::InterpolationTargetA>;
151  using interp_component = mock_interpolator<metavars>;
152 
153  tuples::TaggedTuple<InterpolationTargetOptionTag,
155  tuple_of_opts{std::move(options),
156  std::move(domain_creator.create_domain())};
157  ActionTesting::MockRuntimeSystem<metavars> runner{std::move(tuple_of_opts)};
159  metavars::Phase::Initialization);
160  ActionTesting::emplace_component<interp_component>(&runner, 0);
161  ActionTesting::next_action<interp_component>(make_not_null(&runner), 0);
162  ActionTesting::emplace_component<target_component>(&runner, 0);
163  ActionTesting::next_action<target_component>(make_not_null(&runner), 0);
164  ActionTesting::set_phase(make_not_null(&runner), metavars::Phase::Testing);
165 
166  Slab slab(0.0, 1.0);
167  TimeStepId temporal_id(true, 0, Time(slab, 0));
168 
170  target_component,
171  typename metavars::InterpolationTargetA::compute_target_points>(
172  make_not_null(&runner), 0, temporal_id);
173 
174  // This should not have changed.
176  target_component,
178  runner, 0)
179  .empty());
180 
181  // Should be no queued actions in mock_interpolation_target
182  CHECK(
183  ActionTesting::is_simple_action_queue_empty<target_component>(runner, 0));
184 
185  // But there should be one in mock_interpolator
186 
187  ActionTesting::invoke_queued_simple_action<interp_component>(
188  make_not_null(&runner), 0);
189 
190  // Should be no more queued actions in mock_interpolator
191  CHECK(
193  runner, 0));
194 
195  const auto& vars_holders = ActionTesting::get_databox_tag<
196  interp_component, intrp::Tags::InterpolatedVarsHolders<metavars>>(runner,
197  0);
198  const auto& vars_infos =
199  get<intrp::Vars::HolderTag<typename metavars::InterpolationTargetA,
200  metavars>>(vars_holders)
201  .infos;
202  // Should be one entry in the vars_infos
203  CHECK(vars_infos.size() == 1);
204  const auto& info = vars_infos.at(temporal_id);
205  const auto& block_coord_holders = info.block_coord_holders;
206 
207  // Check number of points
208  const size_t number_of_points = expected_block_coord_holders.size();
209  CHECK(block_coord_holders.size() == number_of_points);
210 
211  for (size_t i = 0; i < number_of_points; ++i) {
212  CHECK(block_coord_holders[i].get().id ==
213  expected_block_coord_holders[i].get().id);
214  CHECK_ITERABLE_APPROX(block_coord_holders[i].get().data,
215  expected_block_coord_holders[i].get().data);
216  }
217 
218  // Call again at a different temporal_id
219  TimeStepId new_temporal_id(true, 0, Time(slab, 1));
221  target_component,
222  typename metavars::InterpolationTargetA::compute_target_points>(
223  make_not_null(&runner), 0, new_temporal_id);
224  ActionTesting::invoke_queued_simple_action<interp_component>(
225  make_not_null(&runner), 0);
226 
227  // Should be two entries in the vars_infos
228  CHECK(vars_infos.size() == 2);
229  const auto& new_block_coord_holders =
230  vars_infos.at(new_temporal_id).block_coord_holders;
231  for (size_t i = 0; i < number_of_points; ++i) {
232  CHECK(new_block_coord_holders[i].get().id ==
233  expected_block_coord_holders[i].get().id);
234  CHECK_ITERABLE_APPROX(new_block_coord_holders[i].get().data,
235  expected_block_coord_holders[i].get().data);
236  }
237 }
238 
239 } // namespace InterpTargetTestHelpers
tmpl::remove_duplicates< tmpl::join< tmpl::transform< ActionsList, detail::get_const_global_cache_tags_from_parallel_struct< tmpl::_1 > >> > get_const_global_cache_tags_from_actions
Given a list of Actions, get a list of the unique tags specified in the actions&#39; const_global_cache_t...
Definition: ParallelComponentHelpers.hpp:66
Definition: InterpolationTargetTestHelpers.hpp:65
A data structure that contains an ID and data associated with that ID.
Definition: IdPair.hpp:16
Index a block of the computational domain.
Definition: BlockId.hpp:21
Definition: BlockId.hpp:16
Holds a Variables interpolated onto a list of points, and information about those points...
Definition: InterpolatedVars.hpp:28
The time in a simulation. Times can be safely compared for exact equality as long as they do not belo...
Definition: Time.hpp:29
Definition: AddTemporalIdsToInterpolationTarget.hpp:17
Defines class TimeStepId.
A chunk of time. Every element must reach slab boundaries exactly, no matter how it actually takes ti...
Definition: Slab.hpp:27
TaggedTuple containing all local Vars::Holders for all InterpolationTargets.
Definition: Tags.hpp:113
Contains functions that forward to Charm++ parallel functions.
Definition: Abort.hpp:13
Keeps track of which points have been filled with interpolated data.
Definition: Tags.hpp:45
Defines the type alias Requires.
Base class for creating Domains from an option string.
Definition: DomainCreator.hpp:88
The Domain.
Definition: Tags.hpp:53
Defines Time and TimeDelta.
A unique identifier for the temporal state of an integrated system.
Definition: TimeStepId.hpp:25
Definition: InterpolationTargetTestHelpers.hpp:122
ParallelComponent responsible for collecting data from Elements and interpolating it onto Interpolati...
Definition: Interpolator.hpp:22
Receives target points from an InterpolationTarget.
Definition: InterpolatorReceivePoints.hpp:63
void set_phase(const gsl::not_null< MockRuntimeSystem< Metavariables > *> runner, const typename Metavariables::Phase &phase) noexcept
Set the phase of all parallel components to phase
Definition: ActionTesting.hpp:1899
An associative container that is indexed by structs.
Definition: TaggedTuple.hpp:272
Definition: InterpolationTargetTestHelpers.hpp:62
Defines classes and functions used for manipulating DataBox&#39;s.
A mock class for the CMake-generated Parallel::Algorithms::Array
Definition: ActionTesting.hpp:1567
Defines class Slab.
Definition: InterpolationTargetWedgeSectionTorus.hpp:25
Definition: DataBoxTag.hpp:27
A Charm++ chare that caches constant data once per Charm++ node.
Definition: ConstGlobalCache.hpp:136
Defines classes for Tensor.
constexpr auto apply(F &&f, const DataBox< BoxTags > &box, Args &&... args) noexcept
Apply the invokable f with argument Tags TagsList from DataBox box
Definition: DataBox.hpp:1633
Namespace for DataBox related things.
Definition: DataBox.hpp:42
Definition: InterpolationTargetTestHelpers.hpp:84
#define CHECK_ITERABLE_APPROX(a, b)
A wrapper around Catch&#39;s CHECK macro that checks approximate equality of entries in iterable containe...
Definition: TestingFramework.hpp:139
Wraps the template metaprogramming library used (brigand)
List of all the actions to be executed in the specified phase.
Definition: PhaseDependentActionList.hpp:16
typename DataBox_detail::item_type_impl< TagList, Tag >::type item_type
Get the type that can be written to the Tag. If it is a base tag then a TagList must be passed as a s...
Definition: DataBoxTag.hpp:246
Defines functions and classes from the GSL.
gsl::not_null< T * > make_not_null(T *ptr) noexcept
Construct a not_null from a pointer. Often this will be done as an implicit conversion, but it may be necessary to perform the conversion explicitly when type deduction is desired.
Definition: Gsl.hpp:879
Defines tags related to domain quantities.
Code to wrap or improve the Catch testing framework used for unit tests.
typename Requires_detail::requires_impl< B >::template_error_type_failed_to_meet_requirements_on_template_parameters Requires
Express requirements on the template parameters of a function or class, replaces std::enable_if_t ...
Definition: Requires.hpp:67
const auto & get_databox_tag(const MockRuntimeSystem< Metavariables > &runner, const typename Component::array_index &array_index) noexcept
Returns the Tag from the DataBox of the parallel component Component with array index array_index...
Definition: ActionTesting.hpp:1969
void simple_action(const gsl::not_null< MockRuntimeSystem< Metavariables > *> runner, const typename Component::array_index &array_index, Args &&... args) noexcept
Runs the simple action Action on the array_indexth element of the parallel component Component...
Definition: ActionTesting.hpp:2040
Defines classes SimpleTag, PrefixTag, ComputeTag and several functions for retrieving tag info...
A class that mocks the infrastructure needed to run actions. It simulates message passing using the i...
Definition: ActionTesting.hpp:1592
bool is_simple_action_queue_empty(const MockRuntimeSystem< Metavariables > &runner, const typename Component::array_index &array_index) noexcept
Returns true if there are no simple actions in the queue.
Definition: ActionTesting.hpp:2071
Initializes an InterpolationTarget.
Definition: InitializeInterpolationTarget.hpp:100
Definition: ComputeTimeDerivative.hpp:29
Require a pointer to not be a nullptr
Definition: Gsl.hpp:182
Indexes a particular Holder in the TaggedTuple that is accessed from the Interpolator&#39;s DataBox with ...
Definition: InterpolatedVars.hpp:112