SetupDataBox.hpp
1 // Distributed under the MIT License.
2 // See LICENSE.txt for details.
3 
4 #pragma once
5 
6 #include <cstddef>
7 #include <tuple>
8 #include <type_traits>
9 
11 #include "Parallel/GlobalCache.hpp"
12 #include "ParallelAlgorithms/Initialization/MergeIntoDataBox.hpp"
13 #include "Utilities/TMPL.hpp"
14 #include "Utilities/TaggedTuple.hpp"
15 
16 namespace Actions {
17 /// \cond
18 struct SetupDataBox;
19 /// \endcond
20 
21 namespace detail {
22 template <typename Action, typename enable = std::void_t<>>
23 struct optional_simple_tags {
24  using type = tmpl::list<>;
25 };
26 
27 template <typename Action>
28 struct optional_simple_tags<Action, std::void_t<typename Action::simple_tags>> {
29  using type = typename Action::simple_tags;
30 };
31 
32 template <typename Action, typename enable = std::void_t<>>
33 struct optional_compute_tags {
34  using type = tmpl::list<>;
35 };
36 
37 template <typename Action>
38 struct optional_compute_tags<Action,
39  std::void_t<typename Action::compute_tags>> {
40  using type = typename Action::compute_tags;
41 };
42 
43 template <typename ActionList>
44 using get_action_list_simple_tags = tmpl::remove_duplicates<
45  tmpl::flatten<tmpl::transform<ActionList, optional_simple_tags<tmpl::_1>>>>;
46 
47 template <typename ActionList>
48 using get_action_list_compute_tags = tmpl::remove_duplicates<tmpl::flatten<
49  tmpl::transform<ActionList, optional_compute_tags<tmpl::_1>>>>;
50 
51 template <typename DbTags, typename... SimpleTags, typename... ComputeTags>
52 auto merge_into_databox_helper(db::DataBox<DbTags>&& box,
53  tmpl::list<SimpleTags...> /*meta*/,
54  tmpl::list<ComputeTags...> /*meta*/) noexcept {
55  return db::create_from<db::RemoveTags<>, db::AddSimpleTags<SimpleTags...>,
56  db::AddComputeTags<ComputeTags...>>(
57  std::move(box), typename SimpleTags::type{}...);
58 }
59 } // namespace detail
60 
61 /*!
62  * \brief Add into the \ref DataBoxGroup "DataBox" default constructed items for
63  * the collection of tags requested by any of the actions in the current phase.
64  *
65  * \details This action adds all of the simple tags given in the `simple_tags`
66  * type lists in each of the other actions in the current phase, and all of the
67  * compute tags given in the `compute_tags` type lists. If an action does not
68  * give either of the type lists, it is treated as an empty type list.
69  *
70  * To prevent the proliferation of many \ref DataBoxGroup "DataBox" types, which
71  * can drastically slow compile times, it is preferable to use only this action
72  * to add tags to the \ref DataBoxGroup "DataBox", and place this action at the
73  * start of the `Initialization` phase action list. The rest of the
74  * initialization actions should specify `simple_tags` and `compute_tags`, and
75  * assign initial values to those tags, but not add those tags into the \ref
76  * DataBoxGroup "DataBox".
77  *
78  * An example initialization action:
79  * \snippet Test_SetupDataBox.cpp initialization_action
80  */
81 struct SetupDataBox {
82  template <typename DbTags, typename... InboxTags, typename Metavariables,
83  typename ArrayIndex, typename ActionList,
84  typename ParallelComponent>
85  static auto apply(db::DataBox<DbTags>& box,
86  const tuples::TaggedTuple<InboxTags...>& /*inboxes*/,
87  const Parallel::GlobalCache<Metavariables>& /*cache*/,
88  const ArrayIndex& /*array_index*/,
89  const ActionList /*meta*/,
90  const ParallelComponent* const /*meta*/) noexcept {
91  using action_list_simple_tags =
92  typename detail::get_action_list_simple_tags<ActionList>;
93  using action_list_compute_tags =
94  typename detail::get_action_list_compute_tags<ActionList>;
95  // grab the simple_tags, compute_tags, mutate the databox, creating
96  // default-constructed objects.
97  return std::make_tuple(detail::merge_into_databox_helper(
98  std::move(box),
99  tmpl::list_difference<action_list_simple_tags,
100  typename db::DataBox<DbTags>::simple_item_tags>{},
101  tmpl::list_difference<
102  action_list_compute_tags,
103  typename db::DataBox<DbTags>::compute_item_tags>{}));
104  }
105 };
106 } // namespace Actions
Actions::SetupDataBox
Add into the DataBox default constructed items for the collection of tags requested by any of the act...
Definition: SetupDataBox.hpp:81
Parallel::GlobalCache
Definition: ElementReceiveInterpPoints.hpp:15
GlobalCache.hpp
db::AddComputeTags
tmpl::flatten< tmpl::list< Tags... > > AddComputeTags
List of Compute Item Tags to add to the DataBox.
Definition: DataBox.hpp:1006
tuple
DataBox.hpp
cstddef
tuples::TaggedTuple
An associative container that is indexed by structs.
Definition: TaggedTuple.hpp:271
db::AddSimpleTags
tmpl::flatten< tmpl::list< Tags... > > AddSimpleTags
List of Tags to add to the DataBox.
Definition: DataBox.hpp:985
type_traits
TMPL.hpp