SetVariables.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 <utility>
9 
13 #include "Domain/CoordinateMaps/Tags.hpp"
14 #include "Domain/Mesh.hpp"
15 #include "Domain/Tags.hpp"
16 #include "Domain/TagsTimeDependent.hpp"
17 #include "Evolution/Initialization/InitialData.hpp"
18 #include "Evolution/Initialization/Tags.hpp"
19 #include "Evolution/TypeTraits.hpp"
21 #include "ParallelAlgorithms/Initialization/MergeIntoDataBox.hpp"
22 #include "PointwiseFunctions/AnalyticData/Tags.hpp"
23 #include "PointwiseFunctions/AnalyticSolutions/Tags.hpp"
24 #include "Utilities/Gsl.hpp"
25 #include "Utilities/Requires.hpp"
26 #include "Utilities/TMPL.hpp"
27 #include "Utilities/TaggedTuple.hpp"
28 
29 /// \cond
30 namespace Parallel {
31 template <typename Metavariables>
32 class ConstGlobalCache;
33 } // namespace Parallel
34 /// \endcond
35 
36 namespace evolution {
37 namespace Initialization {
38 namespace Actions {
39 /// \ingroup InitializationGroup
40 /// \brief Sets variables needed for evolution of hyperbolic systems
41 ///
42 /// Uses:
43 /// - DataBox:
44 /// * `CoordinatesTag`
45 /// - ConstGlobalCache:
46 /// * `OptionTags::AnalyticSolutionBase` or `OptionTags::AnalyticDataBase`
47 ///
48 /// DataBox changes:
49 /// - Adds: nothing
50 /// - Removes: nothing
51 /// - Modifies:
52 /// * System::variables_tag (if system has no primitive variables)
53 /// * System::primitive_variables_tag (if system has primitive variables)
54 template <typename LogicalCoordinatesTag>
55 struct SetVariables {
56  using initialization_option_tags =
57  tmpl::list<::Initialization::Tags::InitialTime>;
58 
59  template <typename DbTagsList, typename... InboxTags, typename Metavariables,
60  typename ArrayIndex, typename ActionList,
61  typename ParallelComponent>
62  static std::tuple<db::DataBox<DbTagsList>&&> apply(
64  const tuples::TaggedTuple<InboxTags...>& /*inboxes*/,
66  const ArrayIndex& /*array_index*/, ActionList /*meta*/,
67  const ParallelComponent* const /*meta*/) noexcept {
68  if constexpr (tmpl::list_contains_v<
71  impl<Metavariables>(make_not_null(&box));
72  } else {
73  ERROR(
74  "Could not find dependency 'Initialization::Tags::InitialTime' in "
75  "DataBox.");
76  }
77  return {std::move(box)};
78  }
79 
80  private:
81  template <typename Metavariables, typename DbTagsList>
82  static void impl(const gsl::not_null<db::DataBox<DbTagsList>*> box) noexcept {
83  const double initial_time =
84  db::get<::Initialization::Tags::InitialTime>(*box);
85  const auto inertial_coords =
87  Metavariables::volume_dim, Frame::Grid, Frame::Inertial>>(*box)(
88  db::get<::domain::Tags::ElementMap<Metavariables::volume_dim,
89  Frame::Grid>>(*box)(
90  db::get<LogicalCoordinatesTag>(*box)),
91  initial_time, db::get<::domain::Tags::FunctionsOfTime>(*box));
92 
93  using system = typename Metavariables::system;
94 
95  const auto& solution_or_data =
96  db::get<::Tags::AnalyticSolutionOrData>(*box);
97 
98  if constexpr (Metavariables::system::has_primitive_and_conservative_vars) {
99  using primitives_tag = typename system::primitive_variables_tag;
100  // Set initial data from analytic solution
101  db::mutate<primitives_tag>(
102  box, [&initial_time, &inertial_coords, &solution_or_data](
104  primitive_vars) noexcept {
105  primitive_vars->assign_subset(evolution::initial_data(
106  solution_or_data, inertial_coords, initial_time,
107  typename Metavariables::analytic_variables_tags{}));
108  });
109  } else {
110  using variables_tag = typename system::variables_tag;
111 
112  // Set initial data from analytic solution
113  using Vars = typename variables_tag::type;
114  db::mutate<variables_tag>(
115  box, [&initial_time, &inertial_coords,
116  &solution_or_data](const gsl::not_null<Vars*> vars) noexcept {
117  vars->assign_subset(evolution::initial_data(
118  solution_or_data, inertial_coords, initial_time,
119  typename Vars::tags_list{}));
120  });
121  }
122  }
123 };
124 } // namespace Actions
125 } // namespace Initialization
126 } // namespace evolution
Parallel::ConstGlobalCache
Definition: ElementReceiveInterpPoints.hpp:16
DataBoxTag.hpp
utility
Frame::Inertial
Definition: IndexType.hpp:44
evolution
Functionality for evolving hyperbolic partial differential equations.
Definition: AddMeshVelocityNonconservative.hpp:29
Frame::Grid
Definition: IndexType.hpp:43
Tags.hpp
tuple
db::get
const auto & get(const DataBox< TagList > &box) noexcept
Retrieve the item with tag Tag from the DataBox.
Definition: DataBox.hpp:1134
Initialization::Tags::InitialTime
Definition: Tags.hpp:18
ERROR
#define ERROR(m)
prints an error message to the standard error stream and aborts the program.
Definition: Error.hpp:36
evolution::initial_data
decltype(auto) initial_data(const SolutionOrData &solution_or_data, Coordinates &&coordinates, const double time, const tmpl::list< Tags... > tags) noexcept
Extract initial data either from analytic data or from an analytic solution at a specified time.
Definition: InitialData.hpp:17
DataBox.hpp
cstddef
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:272
domain::CoordinateMaps::Tags::CoordinateMap
Definition: Tags.hpp:30
Variables.hpp
evolution::Initialization::Actions::SetVariables
Sets variables needed for evolution of hyperbolic systems.
Definition: SetVariables.hpp:55
Gsl.hpp
Requires.hpp
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:879
Mesh.hpp
db::DataBox
Definition: InterpolationTargetWedgeSectionTorus.hpp:24
Parallel
Contains functions that forward to Charm++ parallel functions.
Definition: ElementReceiveInterpPoints.hpp:14
TMPL.hpp
ConstGlobalCache.hpp
gsl::not_null
Require a pointer to not be a nullptr
Definition: Gsl.hpp:182
domain::Tags::ElementMap
Definition: Tags.hpp:114