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/Actions/SendPointsToInterpolator.hpp"
19 #include "NumericalAlgorithms/Interpolation/InitializeInterpolationTarget.hpp"
20 #include "NumericalAlgorithms/Interpolation/InitializeInterpolator.hpp"
21 #include "NumericalAlgorithms/Interpolation/InterpolatedVars.hpp"
22 #include "NumericalAlgorithms/Interpolation/InterpolationTargetDetail.hpp"
23 #include "Parallel/ParallelComponentHelpers.hpp"
24 #include "Time/Slab.hpp"
25 #include "Time/Time.hpp"
26 #include "Time/TimeStepId.hpp"
27 #include "Utilities/Gsl.hpp"
28 #include "Utilities/Requires.hpp"
29 #include "Utilities/TMPL.hpp"
30 #include "Utilities/TaggedTuple.hpp"
31 
32 /// \cond
33 namespace intrp {
34 namespace Actions {
35 template <typename InterpolationTargetTag>
36 struct ReceivePoints;
37 } // namespace Actions
38 } // namespace intrp
39 template <typename IdType, typename DataType>
40 class IdPair;
41 namespace Parallel {
42 template <typename Metavariables>
43 class ConstGlobalCache;
44 } // namespace Parallel
45 namespace db {
46 template <typename TagsList>
47 class DataBox;
48 } // namespace db
49 namespace intrp {
50 namespace Tags {
51 template <typename TemporalId>
52 struct IndicesOfFilledInterpPoints;
53 template <typename TemporalId>
54 struct InterpolatedVarsHolders;
55 struct NumberOfElements;
56 } // namespace Tags
57 } // namespace intrp
58 namespace domain {
59 class BlockId;
60 } // namespace domain
61 /// \endcond
62 
63 namespace InterpTargetTestHelpers {
64 
65 template <typename Metavariables, typename InterpolationTargetTag>
67  using metavariables = Metavariables;
69  using array_index = size_t;
70  using component_being_mocked = void; // not needed.
71  using const_global_cache_tags = tmpl::flatten<tmpl::append<
73  typename Metavariables::InterpolationTargetA::compute_target_points>>,
74  tmpl::list<domain::Tags::Domain<Metavariables::volume_dim>>>>;
75  using phase_dependent_action_list = tmpl::list<
77  typename Metavariables::Phase, Metavariables::Phase::Initialization,
79  Metavariables, InterpolationTargetTag>>>,
80  Parallel::PhaseActions<typename Metavariables::Phase,
81  Metavariables::Phase::Testing, tmpl::list<>>>;
82 };
83 
84 template <typename InterpolationTargetTag>
86  template <
87  typename ParallelComponent, typename DbTags, typename Metavariables,
88  typename ArrayIndex, size_t VolumeDim,
90  nullptr>
91  static void apply(
94  const ArrayIndex& /*array_index*/,
95  const typename Metavariables::temporal_id::type& temporal_id,
96  std::vector<boost::optional<
98  tnsr::I<double, VolumeDim, typename Frame::Logical>>>>&&
99  block_coord_holders) noexcept {
100  db::mutate<intrp::Tags::InterpolatedVarsHolders<Metavariables>>(
101  make_not_null(&box),
102  [
103  &temporal_id, &block_coord_holders
104  ](const gsl::not_null<
106  vars_holders) noexcept {
107  auto& vars_infos =
108  get<intrp::Vars::HolderTag<InterpolationTargetTag,
109  Metavariables>>(*vars_holders)
110  .infos;
111 
112  // Add the target interpolation points at this timestep.
113  vars_infos.emplace(std::make_pair(
114  temporal_id,
115  intrp::Vars::Info<VolumeDim, typename InterpolationTargetTag::
116  vars_to_interpolate_to_target>{
117  std::move(block_coord_holders)}));
118  });
119  }
120 };
121 
122 template <typename Metavariables>
124  using metavariables = Metavariables;
126  using array_index = size_t;
127  using phase_dependent_action_list = tmpl::list<
129  typename Metavariables::Phase, Metavariables::Phase::Initialization,
130  tmpl::list<intrp::Actions::InitializeInterpolator>>,
131  Parallel::PhaseActions<typename Metavariables::Phase,
132  Metavariables::Phase::Testing, tmpl::list<>>>;
133 
135  using replace_these_simple_actions = tmpl::list<intrp::Actions::ReceivePoints<
136  typename Metavariables::InterpolationTargetA>>;
137  using with_these_simple_actions = tmpl::list<
139 };
140 
141 template <typename MetaVariables, typename InterpolationTargetOptionTag,
142  typename DomainCreator, typename BlockCoordHolder>
143 void test_interpolation_target(
144  const DomainCreator& domain_creator,
145  typename InterpolationTargetOptionTag::type options,
146  const BlockCoordHolder& expected_block_coord_holders) noexcept {
147  using metavars = MetaVariables;
148  using temporal_id_type = typename metavars::temporal_id::type;
149  using target_component =
150  mock_interpolation_target<metavars,
151  typename metavars::InterpolationTargetA>;
152  using interp_component = mock_interpolator<metavars>;
153 
154  tuples::TaggedTuple<InterpolationTargetOptionTag,
156  tuple_of_opts{std::move(options),
157  std::move(domain_creator.create_domain())};
158  ActionTesting::MockRuntimeSystem<metavars> runner{std::move(tuple_of_opts)};
160  metavars::Phase::Initialization);
161  ActionTesting::emplace_component<interp_component>(&runner, 0);
162  ActionTesting::next_action<interp_component>(make_not_null(&runner), 0);
163  ActionTesting::emplace_component<target_component>(&runner, 0);
164  ActionTesting::next_action<target_component>(make_not_null(&runner), 0);
165  ActionTesting::set_phase(make_not_null(&runner), metavars::Phase::Testing);
166 
167  Slab slab(0.0, 1.0);
168  TimeStepId temporal_id(true, 0, Time(slab, 0));
169 
170  ActionTesting::simple_action<target_component,
172  typename metavars::InterpolationTargetA>>(
173  make_not_null(&runner), 0, temporal_id);
174 
175  // This should not have changed.
177  target_component,
179  runner, 0)
180  .empty());
181 
182  // Should be no queued actions in mock_interpolation_target
183  CHECK(
184  ActionTesting::is_simple_action_queue_empty<target_component>(runner, 0));
185 
186  // But there should be one in mock_interpolator
187 
188  ActionTesting::invoke_queued_simple_action<interp_component>(
189  make_not_null(&runner), 0);
190 
191  // Should be no more queued actions in mock_interpolator
192  CHECK(
193  ActionTesting::is_simple_action_queue_empty<mock_interpolator<metavars>>(
194  runner, 0));
195 
196  const auto& vars_holders = ActionTesting::get_databox_tag<
197  interp_component, intrp::Tags::InterpolatedVarsHolders<metavars>>(runner,
198  0);
199  const auto& vars_infos =
200  get<intrp::Vars::HolderTag<typename metavars::InterpolationTargetA,
201  metavars>>(vars_holders)
202  .infos;
203  // Should be one entry in the vars_infos
204  CHECK(vars_infos.size() == 1);
205  const auto& info = vars_infos.at(temporal_id);
206  const auto& block_coord_holders = info.block_coord_holders;
207 
208  // Check number of points
209  const size_t number_of_points = expected_block_coord_holders.size();
210  CHECK(block_coord_holders.size() == number_of_points);
211 
212  for (size_t i = 0; i < number_of_points; ++i) {
213  CHECK(block_coord_holders[i].get().id ==
214  expected_block_coord_holders[i].get().id);
215  CHECK_ITERABLE_APPROX(block_coord_holders[i].get().data,
216  expected_block_coord_holders[i].get().data);
217  }
218 
219  // Call again at a different temporal_id
220  TimeStepId new_temporal_id(true, 0, Time(slab, 1));
221  ActionTesting::simple_action<target_component,
223  typename metavars::InterpolationTargetA>>(
224  make_not_null(&runner), 0, new_temporal_id);
225  ActionTesting::invoke_queued_simple_action<interp_component>(
226  make_not_null(&runner), 0);
227 
228  // Should be two entries in the vars_infos
229  CHECK(vars_infos.size() == 2);
230  const auto& new_block_coord_holders =
231  vars_infos.at(new_temporal_id).block_coord_holders;
232  for (size_t i = 0; i < number_of_points; ++i) {
233  CHECK(new_block_coord_holders[i].get().id ==
234  expected_block_coord_holders[i].get().id);
235  CHECK_ITERABLE_APPROX(new_block_coord_holders[i].get().data,
236  expected_block_coord_holders[i].get().data);
237  }
238 }
239 
240 } // namespace InterpTargetTestHelpers
Parallel::ConstGlobalCache
Definition: ElementReceiveInterpPoints.hpp:16
ActionTesting::set_phase
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:1920
DataBoxTag.hpp
utility
ActionTesting::MockRuntimeSystem
Definition: ActionTesting.hpp:1613
Slab
Definition: Slab.hpp:27
get
constexpr Tag::type & get(Variables< TagList > &v) noexcept
Return Tag::type pointing into the contiguous array.
Definition: Variables.hpp:689
intrp::Actions::InitializeInterpolationTarget
Initializes an InterpolationTarget.
Definition: InitializeInterpolationTarget.hpp:100
InterpTargetTestHelpers::mock_interpolator
Definition: InterpolationTargetTestHelpers.hpp:123
Parallel::get_const_global_cache_tags_from_actions
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' const_global_cache_t...
Definition: ParallelComponentHelpers.hpp:66
Tags.hpp
intrp::Actions::ReceivePoints
Receives target points from an InterpolationTarget.
Definition: InterpolatorReceivePoints.hpp:63
vector
intrp::Actions::SendPointsToInterpolator
Sets up points on an InterpolationTarget at a new temporal_id and sends these points to an Interpolat...
Definition: SendPointsToInterpolator.hpp:36
TestingFramework.hpp
InterpTargetTestHelpers::MockReceivePoints
Definition: InterpolationTargetTestHelpers.hpp:85
CHECK_ITERABLE_APPROX
#define CHECK_ITERABLE_APPROX(a, b)
A wrapper around Catch's CHECK macro that checks approximate equality of entries in iterable containe...
Definition: TestingFramework.hpp:139
IdPair
A data structure that contains an ID and data associated with that ID.
Definition: IdPair.hpp:16
intrp::Interpolator
ParallelComponent responsible for collecting data from Elements and interpolating it onto Interpolati...
Definition: Interpolator.hpp:22
Parallel::PhaseActions
List of all the actions to be executed in the specified phase.
Definition: PhaseDependentActionList.hpp:16
DataBox.hpp
InterpTargetTestHelpers::mock_interpolation_target
Definition: InterpolationTargetTestHelpers.hpp:66
cstddef
intrp::Vars::Info
Holds a Variables interpolated onto a list of points, and information about those points,...
Definition: InterpolatedVars.hpp:28
db::item_type
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
tuples::TaggedTuple
An associative container that is indexed by structs.
Definition: TaggedTuple.hpp:271
Time.hpp
intrp::Vars::HolderTag
Indexes a particular Holder in the TaggedTuple that is accessed from the Interpolator's DataBox with ...
Definition: InterpolatedVars.hpp:112
TimeStepId
Definition: TimeStepId.hpp:25
ActionTesting::get_databox_tag
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:1990
DomainCreator
Base class for creating Domains from an option string.
Definition: DomainCreator.hpp:88
ActionTesting::MockArrayChare
A mock class for the CMake-generated Parallel::Algorithms::Array
Definition: ActionTesting.hpp:1588
TimeStepId.hpp
ActionTesting::simple_action
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:2061
Gsl.hpp
Time
Definition: Time.hpp:29
Tensor.hpp
Requires.hpp
domain::BlockId
Index a block of the computational domain.
Definition: BlockId.hpp:21
std::empty
T empty(T... args)
Slab.hpp
ActionTesting::is_simple_action_queue_empty
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:2092
make_not_null
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,...
Definition: Gsl.hpp:880
domain::Tags::Domain
Definition: Tags.hpp:54
Requires
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
intrp::Tags::IndicesOfFilledInterpPoints
Keeps track of which points have been filled with interpolated data.
Definition: Tags.hpp:45
db::DataBox
Definition: InterpolationTargetWedgeSectionTorus.hpp:24
Parallel
Contains functions that forward to Charm++ parallel functions.
Definition: ElementReceiveInterpPoints.hpp:14
TMPL.hpp
db
Namespace for DataBox related things.
Definition: DataBox.hpp:42
intrp::Tags::InterpolatedVarsHolders
TaggedTuple containing all local Vars::Holders for all InterpolationTargets.
Definition: Tags.hpp:113
gsl::not_null
Require a pointer to not be a nullptr
Definition: Gsl.hpp:183