DataBoxHelpers.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 helper classes and functions for manipulating DataBox's and items
6 
7 #pragma once
8 
11 
12 /// \cond
13 template <typename X, typename Symm, typename IndexList>
14 class Tensor;
15 
16 class DataVector;
17 /// \endcond
18 
19 template <typename Tag, typename = std::nullptr_t>
21 
22 template <typename Tag>
24  Tag, Requires<std::is_base_of<db::SimpleTag, Tag>::value>>
25  : boost::static_visitor<db::item_type<Tag>> {
26  template <typename DataBox_t>
27  constexpr db::item_type<Tag> operator()(DataBox_t& box) const {
28  return box.template get<Tag>();
29  }
30 };
31 
32 template <typename TagType>
34  TagType, Requires<not std::is_base_of<db::SimpleTag, TagType>::value>>
35  : boost::static_visitor<TagType> {
37  : var_name(std::move(name)) {}
38  template <typename DataBox_t>
39  constexpr TagType operator()(DataBox_t& box) const {
40  return db::get_item_from_box<TagType>(box, var_name);
41  }
42 
43  private:
44  const std::string var_name;
45 };
46 
47 namespace DataBoxHelpers_detail {
48 template <typename Tags, typename TagList,
49  Requires<(tmpl::size<Tags>::value == 1)> = nullptr>
50 auto get_tensor_from_box(const db::DataBox<TagList>& box,
51  const std::string& tag_name) {
52  using tag = tmpl::front<Tags>;
53  if (db::get_tag_name<tag>() != tag_name) {
54  ERROR("Could not find the tag named \"" << tag_name << "\" in the DataBox");
55  }
56  return ::db::get<tag>(box).get_vector_of_data();
57 }
58 
59 template <typename Tags, typename TagList,
60  Requires<(tmpl::size<Tags>::value > 1)> = nullptr>
61 auto get_tensor_from_box(const db::DataBox<TagList>& box,
62  const std::string& tag_name) {
63  using tag = tmpl::front<Tags>;
64  return db::get_tag_name<tag>() == tag_name
65  ? ::db::get<tag>(box).serialize()
66  : get_tensor_from_box<tmpl::pop_front<Tags>>(box, tag_name);
67 }
68 
69 template <typename T>
70 using is_a_tensor = tt::is_a<Tensor, T>;
71 } // namespace DataBoxHelpers_detail
72 
73 template <typename TagsList>
74 auto get_tensor_from_box(const db::DataBox<TagsList>& box,
75  const std::string& tag_name) {
76  using tags =
77  tmpl::filter<TagsList, tmpl::bind<DataBoxHelpers_detail::is_a_tensor,
78  tmpl::bind<db::item_type, tmpl::_1>>>;
79  return DataBoxHelpers_detail::get_tensor_from_box<tags>(box, tag_name);
80 }
81 
82 // namespace DataBoxHelpers_detail {
83 // template <typename Tags, typename TagsList,
84 // Requires<(tmpl::size<Tags>::value == 1)> = nullptr>
85 // auto get_tensor_norm_from_box(
86 // const db::DataBox<TagsList>& box,
87 // const std::pair<std::string, TypeOfNorm>& tag_name) {
88 // using tag = tmpl::front<Tags>;
89 // if (db::get_tag_name<tag>() != tag_name.first) {
90 // ERROR("Could not find the tag named \"" << tag_name.first
91 // << "\" in the DataBox");
92 // }
93 // return compute_norm_core(box.template get<tag>(), tag_name.second);
94 //}
95 //
96 // template <typename Tags, typename TagsList,
97 // Requires<(tmpl::size<Tags>::value > 1)> = nullptr>
98 // auto get_tensor_norm_from_box(
99 // const db::DataBox<TagsList>& box,
100 // const std::pair<std::string, TypeOfNorm>& tag_name) {
101 // using tag = tmpl::front<Tags>;
102 // if (db::get_tag_name<tag>() != tag_name.first) {
103 // return get_tensor_norm_from_box<tmpl::pop_front<Tags>>(box, tag_name);
104 // }
105 // return compute_norm_core(box.template get<tag>(), tag_name.second);
106 //}
107 //} // namespace DataBoxHelpers_detail
108 
109 // template <typename TagsList>
110 // auto get_tensor_norm_from_box(
111 // const db::DataBox<TagsList>& box,
112 // const std::pair<std::string, TypeOfNorm>& tag_name) {
113 // using tags =
114 // tmpl::filter<TagsList, tmpl::bind<DataBoxHelpers_detail::is_a_tensor,
115 // tmpl::bind<db::item_type, tmpl::_1>>>;
116 //
117 // return DataBoxHelpers_detail::get_tensor_norm_from_box<tags>(box, tag_name);
118 //}
119 
121  : boost::static_visitor<
122  std::pair<std::vector<std::string>, std::vector<DataVector>>> {
124  : var_name(std::move(name)) {}
125  template <typename DataBox_t>
127  DataBox_t& box) const {
128  return get_tensor_from_box(box, var_name);
129  }
130 
131  private:
132  const std::string var_name;
133 };
Definition: DataBoxHelpers.hpp:47
#define ERROR(m)
prints an error message to the standard error stream and aborts the program.
Definition: Error.hpp:35
Definition: DataBoxHelpers.hpp:20
Defines classes and functions used for manipulating DataBox&#39;s.
Defines helper functions for working with boost.
Definition: InterpolationTargetWedgeSectionTorus.hpp:24
Definition: DataBoxTag.hpp:29
Check if type T is a template specialization of U
Definition: TypeTraits.hpp:536
Stores a collection of function values.
Definition: DataVector.hpp:46
typename DataBox_detail::item_type_impl< TagList, Tag >::type item_type
Get the type that is returned by the Tag. If it is a base tag then a TagList must be passed as a seco...
Definition: DataBoxTag.hpp:410
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
Definition: DataBoxHelpers.hpp:120