Magnitude.hpp
1 // Distributed under the MIT License.
2 // See LICENSE.txt for details.
3 
4 #pragma once
5 
6 #include <string>
7 
9 #include "DataStructures/DataVector.hpp"
12 #include "Utilities/TMPL.hpp"
13 
14 /*!
15  * \ingroup TensorGroup
16  * \brief Compute the Euclidean magnitude of a rank-1 tensor
17  *
18  * \details
19  * Computes the square root of the sum of the squares of the components of
20  * the rank-1 tensor.
21  */
22 template <typename DataType, typename Index>
24  const Tensor<DataType, Symmetry<1>, index_list<Index>>& vector) noexcept {
25  return Scalar<DataType>{sqrt(get(dot_product(vector, vector)))};
26 }
27 
28 /*!
29  * \ingroup TensorGroup
30  * \brief Compute the magnitude of a rank-1 tensor
31  *
32  * \details
33  * Returns the square root of the input tensor contracted twice with the given
34  * metric.
35  */
36 template <typename DataType, typename Index>
38  const Tensor<DataType, Symmetry<1>, index_list<Index>>& vector,
39  const Tensor<DataType, Symmetry<1, 1>,
40  index_list<change_index_up_lo<Index>,
42  metric) noexcept {
43  return Scalar<DataType>{sqrt(get(dot_product(vector, vector, metric)))};
44 }
45 
46 namespace Tags {
47 /// \ingroup DataBoxTagsGroup
48 /// \ingroup DataStructuresGroup
49 /// The magnitude of a (co)vector
50 ///
51 /// \snippet Test_Magnitude.cpp magnitude_name
52 template <typename Tag>
54  static std::string name() noexcept {
55  return "Magnitude(" + Tag::name() + ")";
56  }
57  using tag = Tag;
58  using type = Scalar<DataVector>;
59 };
60 
61 /// \ingroup DataBoxTagsGroup
62 /// \ingroup DataStructuresGroup
63 /// The Euclidean magnitude of a (co)vector
64 ///
65 /// This tag inherits from `Tags::Magnitude<Tag>`
66 template <typename Tag>
68  using base = Magnitude<Tag>;
69  static constexpr Scalar<DataVector> (*function)(const db::item_type<Tag>&) =
70  magnitude;
71  using argument_tags = tmpl::list<Tag>;
72 };
73 
74 /// \ingroup DataBoxTagsGroup
75 /// \ingroup DataStructuresGroup
76 /// The magnitude of a (co)vector with respect to a specific metric
77 ///
78 /// This tag inherits from `Tags::Magnitude<Tag>`
79 template <typename Tag, typename MetricTag>
81  using base = Magnitude<Tag>;
82  static constexpr Scalar<DataVector> (*function)(
84  using argument_tags = tmpl::list<Tag, MetricTag>;
85 };
86 
87 /// \ingroup DataBoxTagsGroup
88 /// \ingroup DataStructuresGroup
89 /// The normalized (co)vector represented by Tag
90 ///
91 /// \snippet Test_Magnitude.cpp normalized_name
92 template <typename Tag>
94  static std::string name() noexcept {
95  return "Normalized(" + Tag::name() + ")";
96  }
97  static constexpr auto function(
98  const db::item_type<Tag>&
99  vector_in, // Compute items need to take const references
100  const db::item_type<Magnitude<Tag>>& magnitude) noexcept {
101  auto vector = vector_in;
102  for (size_t d = 0; d < vector.index_dim(0); ++d) {
103  vector.get(d) /= get(magnitude);
104  }
105  return vector;
106  }
107  using argument_tags = tmpl::list<Tag, Magnitude<Tag>>;
108 };
109 } // namespace Tags
The normalized (co)vector represented by Tag.
Definition: Magnitude.hpp:93
The Euclidean magnitude of a (co)vector.
Definition: Magnitude.hpp:67
Marks a DataBoxTag as being a compute item that executes a function.
Definition: DataBoxTag.hpp:155
Defines functions euclidean dot_product and dot_product with a metric.
Tags for the DataBox inherit from this type.
Definition: DataBoxTag.hpp:65
Scalar< DataType > magnitude(const Tensor< DataType, Symmetry< 1 >, index_list< Index >> &vector) noexcept
Compute the Euclidean magnitude of a rank-1 tensor.
Definition: Magnitude.hpp:23
typename detail::SymmetryImpl< std::make_index_sequence< sizeof...(T)>, tmpl::integral_list< std::int32_t, T... > >::type Symmetry
Computes the canonical symmetry from the integers T
Definition: Symmetry.hpp:81
Definition: DataBoxTag.hpp:29
Defines classes for Tensor.
Tensor_detail::TensorIndexType< Index::index_type==IndexType::Spatial ? Index::value :Index::value - 1, Index::ul==UpLo::Up ? UpLo::Lo :UpLo::Up, typename Index::Frame, Index::index_type > change_index_up_lo
Change the TensorIndexType to be covariant if it&#39;s contravariant and vice-versa.
Definition: IndexType.hpp:233
Wraps the template metaprogramming library used (brigand)
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
Marks an item as being a prefix to another tag.
Definition: DataBoxTag.hpp:112
The magnitude of a (co)vector with respect to a specific metric.
Definition: Magnitude.hpp:80
The magnitude of a (co)vector.
Definition: Magnitude.hpp:53
void dot_product(const gsl::not_null< Scalar< DataType > *> dot_product, const Tensor< DataType, Symmetry< 1 >, index_list< Index >> &vector_a, const Tensor< DataType, Symmetry< 1 >, index_list< Index >> &vector_b) noexcept
Compute the Euclidean dot product of two vectors or one forms.
Definition: DotProduct.hpp:24
Defines classes SimpleTag, PrefixTag, ComputeTag and several functions for retrieving tag info...
Tensor< T, Symmetry<>, index_list<> > Scalar
Scalar type.
Definition: TypeAliases.hpp:21