InterpolationTargetTestHelpers.hpp
1 // Distributed under the MIT License.
2 // See LICENSE.txt for details.
3 
4 #pragma once
5 
7 
8 #include <cstddef>
9 #include <optional>
10 #include <utility>
11 #include <vector>
12 
15 #include "Domain/Tags.hpp"
16 #include "Framework/ActionTesting.hpp"
17 #include "NumericalAlgorithms/Interpolation/Actions/SendPointsToInterpolator.hpp"
18 #include "NumericalAlgorithms/Interpolation/InitializeInterpolationTarget.hpp"
19 #include "NumericalAlgorithms/Interpolation/InitializeInterpolator.hpp"
20 #include "NumericalAlgorithms/Interpolation/InterpolatedVars.hpp"
21 #include "NumericalAlgorithms/Interpolation/InterpolationTargetDetail.hpp"
22 #include "Parallel/Actions/SetupDataBox.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 GlobalCache;
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,
78  tmpl::list<Actions::SetupDataBox,
80  Metavariables, InterpolationTargetTag>>>,
81  Parallel::PhaseActions<typename Metavariables::Phase,
82  Metavariables::Phase::Testing, tmpl::list<>>>;
83 };
84 
85 template <typename InterpolationTargetTag>
87  template <
88  typename ParallelComponent, typename DbTags, typename Metavariables,
89  typename ArrayIndex, size_t VolumeDim,
91  nullptr>
92  static void apply(
93  db::DataBox<DbTags>& box,
95  const ArrayIndex& /*array_index*/,
96  const typename Metavariables::temporal_id::type& temporal_id,
99  tnsr::I<double, VolumeDim, typename Frame::Logical>>>>&&
100  block_coord_holders) noexcept {
101  db::mutate<intrp::Tags::InterpolatedVarsHolders<Metavariables>>(
102  make_not_null(&box),
103  [
104  &temporal_id, &block_coord_holders
105  ](const gsl::not_null<
106  typename intrp::Tags::InterpolatedVarsHolders<Metavariables>::type*>
107  vars_holders) noexcept {
108  auto& vars_infos =
109  get<intrp::Vars::HolderTag<InterpolationTargetTag,
110  Metavariables>>(*vars_holders)
111  .infos;
112 
113  // Add the target interpolation points at this timestep.
114  vars_infos.emplace(std::make_pair(
115  temporal_id,
116  intrp::Vars::Info<VolumeDim, typename InterpolationTargetTag::
117  vars_to_interpolate_to_target>{
118  std::move(block_coord_holders)}));
119  });
120  }
121 };
122 
123 template <typename Metavariables>
125  using metavariables = Metavariables;
127  using array_index = size_t;
128  using phase_dependent_action_list = tmpl::list<
130  typename Metavariables::Phase, Metavariables::Phase::Initialization,
131  tmpl::list<Actions::SetupDataBox,
135  Parallel::PhaseActions<typename Metavariables::Phase,
136  Metavariables::Phase::Testing, tmpl::list<>>>;
137 
139  using replace_these_simple_actions = tmpl::list<intrp::Actions::ReceivePoints<
140  typename Metavariables::InterpolationTargetA>>;
141  using with_these_simple_actions = tmpl::list<
143 };
144 
145 template <typename MetaVariables, typename InterpolationTargetOptionTag,
146  typename DomainCreator, typename BlockCoordHolder>
147 void test_interpolation_target(
148  const DomainCreator& domain_creator,
149  typename InterpolationTargetOptionTag::type options,
150  const BlockCoordHolder& expected_block_coord_holders) noexcept {
151  using metavars = MetaVariables;
152  using temporal_id_type = typename metavars::temporal_id::type;
153  using target_component =
154  mock_interpolation_target<metavars,
155  typename metavars::InterpolationTargetA>;
156  using interp_component = mock_interpolator<metavars>;
157 
158  tuples::TaggedTuple<InterpolationTargetOptionTag,
160  tuple_of_opts{std::move(options),
161  std::move(domain_creator.create_domain())};
162  ActionTesting::MockRuntimeSystem<metavars> runner{std::move(tuple_of_opts)};
164  metavars::Phase::Initialization);
165  ActionTesting::emplace_component<interp_component>(&runner, 0);
166  for (size_t i = 0; i < 2; ++i) {
167  ActionTesting::next_action<interp_component>(make_not_null(&runner), 0);
168  }
169  ActionTesting::emplace_component<target_component>(&runner, 0);
170  for (size_t i = 0; i < 2; ++i) {
171  ActionTesting::next_action<target_component>(make_not_null(&runner), 0);
172  }
173  ActionTesting::set_phase(make_not_null(&runner), metavars::Phase::Testing);
174 
175  Slab slab(0.0, 1.0);
176  TimeStepId temporal_id(true, 0, Time(slab, 0));
177 
178  ActionTesting::simple_action<target_component,
180  typename metavars::InterpolationTargetA>>(
181  make_not_null(&runner), 0, temporal_id);
182 
183  // This should not have changed.
185  target_component,
187  runner, 0)
188  .empty());
189 
190  // Should be no queued actions in mock_interpolation_target
191  CHECK(
192  ActionTesting::is_simple_action_queue_empty<target_component>(runner, 0));
193 
194  // But there should be one in mock_interpolator
195 
196  ActionTesting::invoke_queued_simple_action<interp_component>(
197  make_not_null(&runner), 0);
198 
199  // Should be no more queued actions in mock_interpolator
200  CHECK(
201  ActionTesting::is_simple_action_queue_empty<mock_interpolator<metavars>>(
202  runner, 0));
203 
204  const auto& vars_holders = ActionTesting::get_databox_tag<
205  interp_component, intrp::Tags::InterpolatedVarsHolders<metavars>>(runner,
206  0);
207  const auto& vars_infos =
208  get<intrp::Vars::HolderTag<typename metavars::InterpolationTargetA,
209  metavars>>(vars_holders)
210  .infos;
211  // Should be one entry in the vars_infos
212  CHECK(vars_infos.size() == 1);
213  const auto& info = vars_infos.at(temporal_id);
214  const auto& block_coord_holders = info.block_coord_holders;
215 
216  // Check number of points
217  const size_t number_of_points = expected_block_coord_holders.size();
218  CHECK(block_coord_holders.size() == number_of_points);
219 
220  for (size_t i = 0; i < number_of_points; ++i) {
221  CHECK(block_coord_holders[i].value().id ==
222  expected_block_coord_holders[i].value().id);
223  CHECK_ITERABLE_APPROX(block_coord_holders[i].value().data,
224  expected_block_coord_holders[i].value().data);
225  }
226 
227  // Call again at a different temporal_id
228  TimeStepId new_temporal_id(true, 0, Time(slab, 1));
229  ActionTesting::simple_action<target_component,
231  typename metavars::InterpolationTargetA>>(
232  make_not_null(&runner), 0, new_temporal_id);
233  ActionTesting::invoke_queued_simple_action<interp_component>(
234  make_not_null(&runner), 0);
235 
236  // Should be two entries in the vars_infos
237  CHECK(vars_infos.size() == 2);
238  const auto& new_block_coord_holders =
239  vars_infos.at(new_temporal_id).block_coord_holders;
240  for (size_t i = 0; i < number_of_points; ++i) {
241  CHECK(new_block_coord_holders[i].value().id ==
242  expected_block_coord_holders[i].value().id);
243  CHECK_ITERABLE_APPROX(new_block_coord_holders[i].value().data,
244  expected_block_coord_holders[i].value().data);
245  }
246 }
247 
248 } // namespace InterpTargetTestHelpers
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: MockRuntimeSystemFreeFunctions.hpp:23
Actions::SetupDataBox
Add into the DataBox default constructed items for the collection of tags requested by any of the act...
Definition: SetupDataBox.hpp:102
utility
ActionTesting::MockRuntimeSystem
Definition: MockRuntimeSystem.hpp:97
Slab
Definition: Slab.hpp:27
intrp::Actions::InitializeInterpolationTarget
Initializes an InterpolationTarget.
Definition: InitializeInterpolationTarget.hpp:118
Parallel::GlobalCache
Definition: ElementReceiveInterpPoints.hpp:15
MakeWithValueImpls::number_of_points
size_t number_of_points(const T &input) noexcept
The number of points represented by an object.
Definition: MakeWithValue.hpp:40
InterpTargetTestHelpers::mock_interpolator
Definition: InterpolationTargetTestHelpers.hpp:124
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:89
intrp
Contains classes and functions for interpolation.
Definition: ElementInitInterpPoints.hpp:23
Tags.hpp
intrp::Actions::ReceivePoints
Receives target points from an InterpolationTarget.
Definition: InterpolatorReceivePoints.hpp:62
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:35
TestingFramework.hpp
InterpTargetTestHelpers::MockReceivePoints
Definition: InterpolationTargetTestHelpers.hpp:86
db::get
const auto & get(const DataBox< TagList > &box) noexcept
Retrieve the item with tag Tag from the DataBox.
Definition: DataBox.hpp:791
intrp::Tags::VolumeVarsInfo
Volume variables at all temporal_ids for all local Elements.
Definition: Tags.hpp:94
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:122
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:23
intrp::Actions::InitializeInterpolator
Initializes an Interpolator.
Definition: InitializeInterpolator.hpp:54
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
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: MockRuntimeSystemFreeFunctions.hpp:224
DomainCreator
Base class for creating Domains from an option string.
Definition: DomainCreator.hpp:32
ActionTesting::MockArrayChare
A mock class for the CMake-generated Parallel::Algorithms::Array
Definition: ActionTesting.hpp:649
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: MockRuntimeSystemFreeFunctions.hpp:297
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
optional
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: MockRuntimeSystemFreeFunctions.hpp:328
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:55
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
Parallel
Functionality for parallelization.
Definition: ElementReceiveInterpPoints.hpp:13
TMPL.hpp
db
Namespace for DataBox related things.
Definition: DataBox.hpp:44
intrp::Tags::InterpolatedVarsHolders
TaggedTuple containing all local Vars::Holders for all InterpolationTargets.
Definition: Tags.hpp:122
gsl::not_null
Require a pointer to not be a nullptr
Definition: ReadSpecPiecewisePolynomial.hpp:13