Mesh.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 the class template Mesh.
6 
7 #pragma once
8 
9 #include <array>
10 #include <cstddef>
11 
12 #include "DataStructures/Index.hpp"
13 #include "Utilities/Gsl.hpp"
14 #include "Utilities/Requires.hpp"
15 #include "Utilities/TypeTraits.hpp" // IWYU pragma: keep
16 
17 /// \cond
18 namespace PUP {
19 class er;
20 } // namespace PUP
21 namespace Spectral {
22 enum class Basis;
23 enum class Quadrature;
24 } // namespace Spectral
25 /// \endcond
26 
27 /*!
28  * \ingroup DataStructuresGroup
29  * \brief Holds the number of grid points, basis, and quadrature in each
30  * direction of the computational grid.
31  *
32  * \details A mesh encapsulates all information necessary to construct the
33  * placement of grid points in the computational domain. It does so through a
34  * choice of basis functions, quadrature and number of points \f$N\f$ in each
35  * dimension. The grid points are the associated collocation points and can be
36  * obtained from Spectral::collocation_points(const Mesh<1>&):
37  *
38  * \snippet Test_Spectral.cpp get_points_for_mesh
39  *
40  * A simulated physical field can be represented by a DataVector of length
41  * number_of_grid_points() that holds the field value on each point of
42  * the computational grid. These values are identical to the field's nodal
43  * expansion coefficients. They approximate the field by a polynomial of degree
44  * \f$p=N-1\f$ through a linear combination of Lagrange polynomials.
45  *
46  * \tparam Dim the number of dimensions of the computational grid.
47  */
48 template <size_t Dim>
49 class Mesh {
50  public:
51  Mesh() noexcept = default;
52 
53  /*!
54  * \brief Construct a computational grid with the same number of collocation
55  * points in each dimension.
56  *
57  * \param isotropic_extents The number of collocation points in each
58  * dimension.
59  * \param basis The choice of spectral basis to compute the
60  * collocation points
61  * \param quadrature The choice of quadrature to compute
62  * the collocation points
63  */
64  Mesh(const size_t isotropic_extents, const Spectral::Basis basis,
65  const Spectral::Quadrature quadrature) noexcept
66  : extents_(isotropic_extents) {
67  bases_.fill(basis);
68  quadratures_.fill(quadrature);
69  }
70 
71  /*!
72  * \brief Construct a computational grid where each dimension can have a
73  * different number of collocation points.
74  *
75  * \param extents The number of collocation points per dimension
76  * \param basis The choice of spectral basis to compute the
77  * collocation points
78  * \param quadrature The choice of quadrature to compute
79  * the collocation points
80  */
82  const Spectral::Quadrature quadrature) noexcept
83  : extents_(std::move(extents)) {
84  bases_.fill(basis);
85  quadratures_.fill(quadrature);
86  }
87 
88  /*!
89  * \brief Construct a computational grid where each dimension can have both a
90  * different number and placement of collocation points.
91  *
92  * \param extents The number of collocation points per dimension
93  * \param bases The choice of spectral bases to compute the
94  * collocation points per dimension
95  * \param quadratures The choice of quadratures to compute
96  * the collocation points per dimension
97  */
99  std::array<Spectral::Quadrature, Dim> quadratures) noexcept
100  : extents_(std::move(extents)),
101  bases_(std::move(bases)),
102  quadratures_(std::move(quadratures)) {}
103 
104  /*!
105  * \brief The number of grid points in each dimension of the grid.
106  */
107  const Index<Dim>& extents() const noexcept { return extents_; }
108 
109  /*!
110  * \brief The number of grid points in dimension \p d of the grid
111  * (zero-indexed).
112  */
113  size_t extents(const size_t d) const noexcept { return extents_[d]; }
114 
115  /*!
116  * \brief The total number of grid points in all dimensions.
117  *
118  * \details `DataVector`s that represent field values on the grid have this
119  * many entries.
120  *
121  * \note A zero-dimensional mesh has one grid point, since it is the slice
122  * through a one-dimensional mesh (a line).
123  */
124  size_t number_of_grid_points() const noexcept { return extents_.product(); }
125 
126  /*!
127  * \brief The basis chosen in each dimension of the grid.
128  */
129  const std::array<Spectral::Basis, Dim>& basis() const noexcept {
130  return bases_;
131  }
132 
133  /*!
134  * \brief The basis chosen in dimension \p d of the grid (zero-indexed).
135  */
136  Spectral::Basis basis(const size_t d) const noexcept {
137  return gsl::at(bases_, d);
138  }
139 
140  /*!
141  * \brief The quadrature chosen in each dimension of the grid.
142  */
144  return quadratures_;
145  }
146 
147  /*!
148  * \brief The quadrature chosen in dimension \p d of the grid (zero-indexed).
149  */
150  Spectral::Quadrature quadrature(const size_t d) const noexcept {
151  return gsl::at(quadratures_, d);
152  }
153 
154  /*!
155  * \brief Returns a Mesh with dimension \p d removed (zero-indexed).
156  *
157  * \see slice_through()
158  */
159  // clang-tidy: incorrectly reported redundancy in template expression
160  template <size_t N = Dim, Requires<(N > 0 and N == Dim)> = nullptr> // NOLINT
161  Mesh<Dim - 1> slice_away(size_t d) const noexcept;
162 
163  /*!
164  * \brief Returns a Mesh with the dimensions \p d, ... present (zero-indexed).
165  *
166  * \details Generally you use this method to obtain a lower-dimensional Mesh
167  * by slicing through a subset of the dimensions. However, you can also
168  * reorder dimensions using this method by slicing through the dimensions in
169  * an order you choose.
170  *
171  * \see slice_away()
172  */
173  template <typename... D, Requires<(sizeof...(D) <= Dim)> = nullptr>
174  Mesh<sizeof...(D)> slice_through(D... d) const noexcept {
175  static_assert(cpp17::conjunction_v<tt::is_integer<D>...>,
176  "The dimensions must be integers.");
177  const std::array<size_t, sizeof...(D)> dims{{static_cast<size_t>(d)...}};
178  return slice_through(dims);
179  }
180 
181  /*!
182  * \brief Returns a Mesh with the dimensions \p dims present (zero-indexed).
183  *
184  * \see slice_through() The templated overload of this function
185  */
186  template <size_t SliceDim, Requires<(SliceDim <= Dim)> = nullptr>
187  Mesh<SliceDim> slice_through(const std::array<size_t, SliceDim>& dims) const
188  noexcept;
189 
190  /*!
191  * \brief Returns the Meshes representing 1D slices of this Mesh.
192  *
193  * The is the same as the array filled with `slice_through(d)` for
194  * `d` from `0` to `Dim - 1` except in dimension 0 where
195  * `slice_through(d)` is not defined.
196  */
197  std::array<Mesh<1>, Dim> slices() const noexcept;
198 
199  // clang-tidy: runtime-references
200  void pup(PUP::er& p) noexcept; // NOLINT
201 
202  private:
203  Index<Dim> extents_{};
204  std::array<Spectral::Basis, Dim> bases_{};
205  std::array<Spectral::Quadrature, Dim> quadratures_{};
206 };
207 
208 /// \cond HIDDEN_SYMBOLS
209 template <size_t Dim>
210 bool operator==(const Mesh<Dim>& lhs, const Mesh<Dim>& rhs) noexcept;
211 
212 template <size_t Dim>
213 bool operator!=(const Mesh<Dim>& lhs, const Mesh<Dim>& rhs) noexcept;
214 
215 template <size_t Dim>
216 std::ostream& operator<<(std::ostream& os, const Mesh<Dim>& mesh) noexcept {
217  os << mesh.extents();
218  return os;
219 }
220 /// \endcond
Quadrature
The choice of quadrature method to compute integration weights.
Definition: Spectral.hpp:68
const std::array< Spectral::Quadrature, Dim > & quadrature() const noexcept
The quadrature chosen in each dimension of the grid.
Definition: Mesh.hpp:143
Definition: Strahlkorper.hpp:14
Holds the number of grid points, basis, and quadrature in each direction of the computational grid...
Definition: Mesh.hpp:49
Mesh(std::array< size_t, Dim > extents, std::array< Spectral::Basis, Dim > bases, std::array< Spectral::Quadrature, Dim > quadratures) noexcept
Construct a computational grid where each dimension can have both a different number and placement of...
Definition: Mesh.hpp:98
Defines the type alias Requires.
Mesh< sizeof...(D)> slice_through(D... d) const noexcept
Returns a Mesh with the dimensions d, ... present (zero-indexed).
Definition: Mesh.hpp:174
Spectral::Quadrature quadrature(const size_t d) const noexcept
The quadrature chosen in dimension d of the grid (zero-indexed).
Definition: Mesh.hpp:150
Defines class template Index.
size_t number_of_grid_points() const noexcept
The total number of grid points in all dimensions.
Definition: Mesh.hpp:124
Mesh(const size_t isotropic_extents, const Spectral::Basis basis, const Spectral::Quadrature quadrature) noexcept
Construct a computational grid with the same number of collocation points in each dimension...
Definition: Mesh.hpp:64
Spectral::Basis basis(const size_t d) const noexcept
The basis chosen in dimension d of the grid (zero-indexed).
Definition: Mesh.hpp:136
Basis
The choice of basis functions for computing collocation points and weights.
Definition: Spectral.hpp:53
constexpr bool conjunction_v
Definition: TypeTraits.hpp:108
size_t extents(const size_t d) const noexcept
The number of grid points in dimension d of the grid (zero-indexed).
Definition: Mesh.hpp:113
const Index< Dim > & extents() const noexcept
The number of grid points in each dimension of the grid.
Definition: Mesh.hpp:107
Mesh(std::array< size_t, Dim > extents, const Spectral::Basis basis, const Spectral::Quadrature quadrature) noexcept
Construct a computational grid where each dimension can have a different number of collocation points...
Definition: Mesh.hpp:81
An integer multi-index.
Definition: Index.hpp:28
Defines functions and classes from the GSL.
Functionality associated with a particular choice of basis functions and quadrature for spectral oper...
Definition: Chebyshev.cpp:16
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
Defines type traits, some of which are future STL type_traits header.
const std::array< Spectral::Basis, Dim > & basis() const noexcept
The basis chosen in each dimension of the grid.
Definition: Mesh.hpp:129
Check if I is an integer type (non-bool, non-character), unlike std::is_integral. ...
Definition: TypeTraits.hpp:1281
constexpr T & at(std::array< T, N > &arr, Size index)
Retrieve a entry from a container, with checks in Debug mode that the index being retrieved is valid...
Definition: Gsl.hpp:124