MockRuntimeSystemFreeFunctions.hpp
Go to the documentation of this file.
1 // Distributed under the MIT License.
2 // See LICENSE.txt for details.
3 
4 /// \file
5 /// Defines free functions that wrap MockRuntimeSystem member functions.
6 
7 #pragma once
8 
9 #include <cstddef>
10 #include <random>
11 #include <utility>
12 #include <vector>
13 
14 #include "Framework/MockRuntimeSystem.hpp"
15 #include "Utilities/Gsl.hpp"
16 #include "Utilities/Requires.hpp"
17 #include "Utilities/TMPL.hpp"
18 #include "Utilities/TypeTraits.hpp"
19 
20 namespace ActionTesting {
21 /// Set the phase of all parallel components to `phase`
22 template <typename Metavariables>
24  const typename Metavariables::Phase& phase) noexcept {
25  runner->set_phase(phase);
26 }
27 
28 /// Emplaces a distributed object with index `array_index` into the parallel
29 /// component `Component`. The options `opts` are forwarded to be used in a call
30 /// to `detail::ForwardAllOptionsToDataBox::apply`.
31 template <typename Component, typename... Options>
34  runner,
35  const typename Component::array_index& array_index,
36  Options&&... opts) noexcept {
37  runner->template emplace_component<Component>(array_index,
38  std::forward<Options>(opts)...);
39 }
40 
41 /// Emplaces a distributed object with index `array_index` into the parallel
42 /// component `Component`. The options `opts` are forwarded to be used in a call
43 /// to `detail::ForwardAllOptionsToDataBox::apply` Additionally, the simple tags
44 /// in the DataBox are initialized from the values set in `initial_values`.
45 template <typename Component, typename... Options,
46  typename Metavars = typename Component::metavariables,
50  runner,
51  const typename Component::array_index& array_index,
52  const typename detail::get_initialization<Component>::InitialValues&
53  initial_values,
54  Options&&... opts) noexcept {
55  runner->template emplace_component_and_initialize<Component>(
56  array_index, initial_values, std::forward<Options>(opts)...);
57 }
58 
59 // @{
60 /// Retrieves the DataBox with tags `TagsList` (omitting the `GlobalCache`
61 /// and `add_from_options` tags) from the parallel component `Component` with
62 /// index `array_index`.
63 template <typename Component, typename TagsList, typename Metavariables>
64 const auto& get_databox(
66  const typename Component::array_index& array_index) noexcept {
67  return runner.template mock_distributed_objects<Component>()
68  .at(array_index)
69  .template get_databox<TagsList>();
70 }
71 
72 template <typename Component, typename TagsList, typename Metavariables>
73 auto& get_databox(const gsl::not_null<MockRuntimeSystem<Metavariables>*> runner,
74  const typename Component::array_index& array_index) noexcept {
75  return runner->template mock_distributed_objects<Component>()
76  .at(array_index)
77  .template get_databox<TagsList>();
78 }
79 // @}
80 
81 /// Get the index in the action list of the next action.
82 template <typename Component, typename Metavariables>
85  const typename Component::array_index& array_index) noexcept {
86  return runner.template get_next_action_index<Component>(array_index);
87 }
88 
89 /// Returns the `Tag` from the `DataBox` of the parallel component `Component`
90 /// with array index `array_index`. If the component's current `DataBox` type
91 /// does not contain `Tag` then an error is emitted.
92 template <typename Component, typename Tag, typename Metavariables>
93 const auto& get_databox_tag(
95  const typename Component::array_index& array_index) noexcept {
96  return runner.template mock_distributed_objects<Component>()
97  .at(array_index)
98  .template get_databox_tag<Tag>();
99 }
100 
101 // @{
102 /// Returns the `InboxTag` from the parallel component `Component` with array
103 /// index `array_index`.
104 template <typename Component, typename InboxTag, typename Metavariables>
105 const auto& get_inbox_tag(
106  const MockRuntimeSystem<Metavariables>& runner,
107  const typename Component::array_index& array_index) noexcept {
108  return tuples::get<InboxTag>(
109  runner.template inboxes<Component>().at(array_index));
110 }
111 
112 template <typename Component, typename InboxTag, typename Metavariables>
113 auto& get_inbox_tag(
114  const gsl::not_null<MockRuntimeSystem<Metavariables>*> runner,
115  const typename Component::array_index& array_index) noexcept {
116  return tuples::get<InboxTag>(
117  runner->template inboxes<Component>().at(array_index));
118 }
119 // @}
120 
121 /// Returns `true` if the current DataBox of `Component` with index
122 /// `array_index` contains the tag `Tag`. If the tag is not contained, returns
123 /// `false`.
124 template <typename Component, typename Tag, typename Metavariables>
126  const typename Component::array_index& array_index) noexcept {
127  return runner.template mock_distributed_objects<Component>()
128  .at(array_index)
129  .template box_contains<Tag>();
130 }
131 
132 /// Returns `true` if the tag `Tag` can be retrieved from the current DataBox
133 /// of `Component` with index `array_index`.
134 template <typename Component, typename Tag, typename Metavariables>
136  const MockRuntimeSystem<Metavariables>& runner,
137  const typename Component::array_index& array_index) noexcept {
138  return runner.template mock_distributed_objects<Component>()
139  .at(array_index)
140  .template tag_is_retrievable<Tag>();
141 }
142 
143 /// Runs the next action in the current phase on the `array_index`th element
144 /// of the parallel component `Component`.
145 template <typename Component, typename Metavariables>
147  const typename Component::array_index& array_index) noexcept {
148  runner->template next_action<Component>(array_index);
149 }
150 
151 /// Runs the `is_ready` function and returns the result for the next action in
152 /// the current phase on the `array_index`th element of the parallel component
153 /// `Component`.
154 template <typename Component, typename Metavariables>
156  const typename Component::array_index& array_index) noexcept {
157  return runner.template is_ready<Component>(array_index);
158 }
159 
160 /// Runs the simple action `Action` on the `array_index`th element of the
161 /// parallel component `Component`.
162 template <typename Component, typename Action, typename Metavariables,
163  typename... Args>
166  const typename Component::array_index& array_index,
167  Args&&... args) noexcept {
168  runner->template simple_action<Component, Action>(
169  array_index, std::forward<Args>(args)...);
170 }
171 
172 /// Runs the simple action `Action` on the `array_index`th element of the
173 /// parallel component `Component`.
174 template <typename Component, typename Action, typename Metavariables,
175  typename... Args>
178  const typename Component::array_index& array_index,
179  Args&&... args) noexcept {
180  runner->template threaded_action<Component, Action>(
181  array_index, std::forward<Args>(args)...);
182 }
183 
184 /// Runs the next queued simple action on the `array_index`th element of
185 /// the parallel component `Component`.
186 template <typename Component, typename Metavariables>
189  const typename Component::array_index& array_index) noexcept {
190  runner->template invoke_queued_simple_action<Component>(array_index);
191 }
192 
193 /// Returns `true` if there are no simple actions in the queue.
194 template <typename Component, typename Metavariables>
196  const MockRuntimeSystem<Metavariables>& runner,
197  const typename Component::array_index& array_index) noexcept {
198  return runner.template is_simple_action_queue_empty<Component>(array_index);
199 }
200 
201 /// Runs the next queued threaded action on the `array_index`th element of
202 /// the parallel component `Component`.
203 template <typename Component, typename Metavariables>
206  const typename Component::array_index& array_index) noexcept {
207  runner->template invoke_queued_threaded_action<Component>(array_index);
208 }
209 
210 /// Returns `true` if there are no threaded actions in the queue.
211 template <typename Component, typename Metavariables>
213  const MockRuntimeSystem<Metavariables>& runner,
214  const typename Component::array_index& array_index) noexcept {
215  return runner.template is_threaded_action_queue_empty<Component>(array_index);
216 }
217 
218 /// Returns whether or not the `Component` with index `array_index` has been
219 /// terminated.
220 template <typename Component, typename Metavariables>
222  const MockRuntimeSystem<Metavariables>& runner,
223  const typename Component::array_index& array_index) noexcept {
224  return runner.template mock_distributed_objects<Component>()
225  .at(array_index)
226  .get_terminate();
227 }
228 
229 /// Returns a vector of all the indices of the Components
230 /// in the ComponentList that have queued actions.
231 template <typename ComponentList, typename MockRuntimeSystem,
232  typename ArrayIndex>
235  const ArrayIndex& array_index) noexcept {
236  std::vector<size_t> result{};
237  size_t i = 0;
238  tmpl::for_each<ComponentList>([&](auto tag) noexcept {
239  using Tag = typename decltype(tag)::type;
240  if (not runner->template is_simple_action_queue_empty<Tag>(array_index)) {
241  result.push_back(i);
242  }
243  ++i;
244  });
245  return result;
246 }
247 
248 /// \cond
249 namespace detail {
250 // Helper function used in invoke_queued_simple_action.
251 template <typename ComponentList, typename MockRuntimeSystem,
252  typename ArrayIndex, size_t... Is>
253 void invoke_queued_action(const gsl::not_null<MockRuntimeSystem*> runner,
254  const size_t component_to_invoke,
255  const ArrayIndex& array_index,
256  std::index_sequence<Is...> /*meta*/) noexcept {
257  const auto helper = [component_to_invoke, &runner,
258  &array_index](auto I) noexcept {
259  if (I.value == component_to_invoke) {
260  runner->template invoke_queued_simple_action<
261  tmpl::at_c<ComponentList, I.value>>(array_index);
262  }
263  };
265 }
266 } // namespace detail
267 /// \endcond
268 
269 /// Invokes the next queued action on a random Component.
270 /// `index_map` is the thing returned by
271 /// `indices_of_components_with_queued_actions`
272 template <typename ComponentList, typename MockRuntimeSystem,
273  typename Generator, typename ArrayIndex>
275  const gsl::not_null<Generator*> generator,
276  const std::vector<size_t>& index_map,
277  const ArrayIndex& array_index) noexcept {
278  std::uniform_int_distribution<size_t> ran(0, index_map.size() - 1);
279  const size_t component_to_invoke = index_map.at(ran(*generator));
280  detail::invoke_queued_action<ComponentList>(
281  runner, component_to_invoke, array_index,
282  std::make_index_sequence<tmpl::size<ComponentList>::value>{});
283 }
284 } // namespace ActionTesting
ActionTesting::emplace_component_and_initialize
void emplace_component_and_initialize(const gsl::not_null< MockRuntimeSystem< typename Component::metavariables > * > runner, const typename Component::array_index &array_index, const typename detail::get_initialization< Component >::InitialValues &initial_values, Options &&... opts) noexcept
Emplaces a distributed object with index array_index into the parallel component Component....
Definition: MockRuntimeSystemFreeFunctions.hpp:48
ActionTesting::invoke_queued_simple_action
void invoke_queued_simple_action(const gsl::not_null< MockRuntimeSystem< Metavariables > * > runner, const typename Component::array_index &array_index) noexcept
Runs the next queued simple action on the array_indexth element of the parallel component Component.
Definition: MockRuntimeSystemFreeFunctions.hpp:187
ActionTesting::get_next_action_index
size_t get_next_action_index(const MockRuntimeSystem< Metavariables > &runner, const typename Component::array_index &array_index) noexcept
Get the index in the action list of the next action.
Definition: MockRuntimeSystemFreeFunctions.hpp:83
ActionTesting::box_contains
bool box_contains(const MockRuntimeSystem< Metavariables > &runner, const typename Component::array_index &array_index) noexcept
Returns true if the current DataBox of Component with index array_index contains the tag Tag....
Definition: MockRuntimeSystemFreeFunctions.hpp:125
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
std::integral_constant
EXPAND_PACK_LEFT_TO_RIGHT
#define EXPAND_PACK_LEFT_TO_RIGHT(...)
Expand a parameter pack evaluating the terms from left to right.
Definition: TMPL.hpp:563
std::uniform_int_distribution
utility
ActionTesting::MockRuntimeSystem
Definition: MockRuntimeSystem.hpp:91
ActionTesting::indices_of_components_with_queued_actions
std::vector< size_t > indices_of_components_with_queued_actions(const gsl::not_null< MockRuntimeSystem * > runner, const ArrayIndex &array_index) noexcept
Returns a vector of all the indices of the Components in the ComponentList that have queued actions.
Definition: MockRuntimeSystemFreeFunctions.hpp:233
ActionTesting
Structures used for mocking the parallel components framework in order to test actions.
Definition: ActionTesting.hpp:308
ActionTesting::invoke_queued_threaded_action
void invoke_queued_threaded_action(const gsl::not_null< MockRuntimeSystem< Metavariables > * > runner, const typename Component::array_index &array_index) noexcept
Runs the next queued threaded action on the array_indexth element of the parallel component Component...
Definition: MockRuntimeSystemFreeFunctions.hpp:204
std::index_sequence
vector
random
ActionTesting::threaded_action
void threaded_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:176
Options
Utilities for parsing input files.
Definition: MinmodType.hpp:8
ActionTesting::is_ready
bool is_ready(MockRuntimeSystem< Metavariables > &runner, const typename Component::array_index &array_index) noexcept
Runs the is_ready function and returns the result for the next action in the current phase on the arr...
Definition: MockRuntimeSystemFreeFunctions.hpp:155
ActionTesting::get_terminate
bool get_terminate(const MockRuntimeSystem< Metavariables > &runner, const typename Component::array_index &array_index) noexcept
Returns whether or not the Component with index array_index has been terminated.
Definition: MockRuntimeSystemFreeFunctions.hpp:221
cstddef
ActionTesting::emplace_component
void emplace_component(const gsl::not_null< MockRuntimeSystem< typename Component::metavariables > * > runner, const typename Component::array_index &array_index, Options &&... opts) noexcept
Emplaces a distributed object with index array_index into the parallel component Component....
Definition: MockRuntimeSystemFreeFunctions.hpp:32
ActionTesting::get_inbox_tag
const auto & get_inbox_tag(const MockRuntimeSystem< Metavariables > &runner, const typename Component::array_index &array_index) noexcept
Returns the InboxTag from the parallel component Component with array index array_index.
Definition: MockRuntimeSystemFreeFunctions.hpp:105
ActionTesting::is_threaded_action_queue_empty
bool is_threaded_action_queue_empty(const MockRuntimeSystem< Metavariables > &runner, const typename Component::array_index &array_index) noexcept
Returns true if there are no threaded actions in the queue.
Definition: MockRuntimeSystemFreeFunctions.hpp:212
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:93
ActionTesting::get_databox
const auto & get_databox(const MockRuntimeSystem< Metavariables > &runner, const typename Component::array_index &array_index) noexcept
Retrieves the DataBox with tags TagsList (omitting the GlobalCache and add_from_options tags) from th...
Definition: MockRuntimeSystemFreeFunctions.hpp:64
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:164
Gsl.hpp
ActionTesting::tag_is_retrievable
bool tag_is_retrievable(const MockRuntimeSystem< Metavariables > &runner, const typename Component::array_index &array_index) noexcept
Returns true if the tag Tag can be retrieved from the current DataBox of Component with index array_i...
Definition: MockRuntimeSystemFreeFunctions.hpp:135
Requires.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: MockRuntimeSystemFreeFunctions.hpp:195
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
ActionTesting::invoke_random_queued_action
void invoke_random_queued_action(const gsl::not_null< MockRuntimeSystem * > runner, const gsl::not_null< Generator * > generator, const std::vector< size_t > &index_map, const ArrayIndex &array_index) noexcept
Invokes the next queued action on a random Component. index_map is the thing returned by indices_of_c...
Definition: MockRuntimeSystemFreeFunctions.hpp:274
ActionTesting::next_action
void next_action(const gsl::not_null< MockRuntimeSystem< Metavariables > * > runner, const typename Component::array_index &array_index) noexcept
Runs the next action in the current phase on the array_indexth element of the parallel component Comp...
Definition: MockRuntimeSystemFreeFunctions.hpp:146
TMPL.hpp
gsl::not_null
Require a pointer to not be a nullptr
Definition: ReadSpecThirdOrderPiecewisePolynomial.hpp:13