FirstOrderEllipticSolutionsTestHelpers.hpp
1 // Distributed under the MIT License.
2 // See LICENSE.txt for details.
3 
4 #pragma once
5 
6 #include <cstddef>
7 #include <limits>
8 
9 #include "DataStructures/DataBox/PrefixHelpers.hpp"
10 #include "DataStructures/DataBox/Tag.hpp"
11 #include "DataStructures/DataBox/TagName.hpp"
12 #include "DataStructures/DataVector.hpp"
16 #include "Elliptic/FirstOrderOperator.hpp"
19 #include "NumericalAlgorithms/LinearOperators/Divergence.tpp"
23 #include "Utilities/Numeric.hpp"
24 #include "Utilities/Tuple.hpp"
25 
26 namespace FirstOrderEllipticSolutionsTestHelpers {
27 
28 namespace Tags {
29 
30 template <typename Tag>
32  using type = typename Tag::type;
33  using tag = Tag;
34 };
35 
36 } // namespace Tags
37 
38 /// \ingroup TestingFrameworkGroup
39 /// Test that the `solution` numerically solves the `System` on the given grid
40 /// for the given tolerance
41 template <typename System, typename SolutionType, typename... Maps,
42  typename... FluxesArgs, typename... SourcesArgs>
44  const SolutionType& solution,
45  const typename System::fluxes& fluxes_computer,
46  const Mesh<System::volume_dim>& mesh,
48  coord_map,
49  const double tolerance,
50  const std::tuple<FluxesArgs...>& fluxes_args = std::tuple<>{},
51  const std::tuple<SourcesArgs...>& sources_args = std::tuple<>{}) {
52  using all_fields = typename System::fields_tag::tags_list;
53  using primal_fields = typename System::primal_fields;
54  using auxiliary_fields = typename System::auxiliary_fields;
55 
56  CAPTURE(mesh);
57 
58  const size_t num_points = mesh.number_of_grid_points();
59  const auto logical_coords = logical_coordinates(mesh);
60  const auto inertial_coords = coord_map(logical_coords);
61  const auto solution_fields = variables_from_tagged_tuple(
62  solution.variables(inertial_coords, all_fields{}));
63 
64  // Apply operator to solution fields
65  auto fluxes = std::apply(
66  [&solution_fields,
67  &fluxes_computer](const auto&... expanded_fluxes_args) {
68  return ::elliptic::first_order_fluxes<System::volume_dim, primal_fields,
69  auxiliary_fields>(
70  solution_fields, fluxes_computer, expanded_fluxes_args...);
71  },
72  fluxes_args);
73  auto div_fluxes =
74  divergence(fluxes, mesh, coord_map.inv_jacobian(logical_coords));
75  auto sources = std::apply(
76  [&solution_fields, &fluxes](const auto&... expanded_sources_args) {
77  return ::elliptic::first_order_sources<System::volume_dim,
78  primal_fields, auxiliary_fields,
79  typename System::sources>(
80  solution_fields, fluxes, expanded_sources_args...);
81  },
82  sources_args);
83  Variables<db::wrap_tags_in<Tags::OperatorAppliedTo, all_fields>>
84  operator_applied_to_fields{num_points};
85  elliptic::first_order_operator(make_not_null(&operator_applied_to_fields),
86  std::move(div_fluxes), std::move(sources));
87 
88  // Set fixed sources to zero for auxiliary fields, and retrieve the fixed
89  // sources for primal fields from the solution
90  Variables<db::wrap_tags_in<::Tags::FixedSource, all_fields>> fixed_sources{
91  num_points, 0.};
92  fixed_sources.assign_subset(solution.variables(
94 
95  // Check error norms against the given tolerance
96  tmpl::for_each<all_fields>([&operator_applied_to_fields, &fixed_sources,
97  &tolerance](auto field_tag_v) {
98  using field_tag = tmpl::type_from<decltype(field_tag_v)>;
99  const auto& operator_applied_to_field =
100  get<Tags::OperatorAppliedTo<field_tag>>(operator_applied_to_fields);
101  const auto& fixed_source =
102  get<::Tags::FixedSource<field_tag>>(fixed_sources);
103  double l2_error_square = 0.;
104  double linf_error = 0.;
105  for (size_t i = 0; i < operator_applied_to_field.size(); ++i) {
106  const auto error = abs(operator_applied_to_field[i] - fixed_source[i]);
107  l2_error_square += alg::accumulate(square(error), 0.) / error.size();
108  const double component_linf_error = *alg::max_element(error);
109  if (component_linf_error > linf_error) {
110  linf_error = component_linf_error;
111  }
112  }
113  const double l2_error =
114  sqrt(l2_error_square / operator_applied_to_field.size());
115  CAPTURE(db::tag_name<field_tag>());
116  CAPTURE(l2_error);
117  CAPTURE(linf_error);
118  CHECK(l2_error < tolerance);
119  CHECK(linf_error < tolerance);
120  });
121 }
122 
123 /*!
124  * \ingroup TestingFrameworkGroup
125  * Test that the `solution` numerically solves the `System` on the given grid
126  * and that the discretization error decreases as expected for a smooth
127  * function.
128  *
129  * \details We expect exponential convergence for a smooth solution, so the
130  * tolerance is computed as
131  *
132  * \f{equation}
133  * C_1 \exp{\left(-C_2 * N_\mathrm{points}\right)}
134  * \f}
135  *
136  * where \f$C_1\f$ is the `tolerance_offset`, \f$C_2\f$ is the
137  * `tolerance_scaling` and \f$N_\mathrm{points}\f$ is the number of grid points
138  * per dimension.
139  */
140 template <typename System, typename SolutionType,
141  size_t Dim = System::volume_dim, typename... Maps,
142  typename PackageFluxesArgs>
144  const SolutionType& solution,
145  const typename System::fluxes& fluxes_computer,
147  coord_map,
148  const double tolerance_offset, const double tolerance_scaling,
149  PackageFluxesArgs&& package_fluxes_args) {
150  INFO("Verify smooth solution");
151  for (size_t num_points = Spectral::minimum_number_of_points<
152  Spectral::Basis::Legendre, Spectral::Quadrature::GaussLobatto>;
153  num_points <=
154  Spectral::maximum_number_of_points<Spectral::Basis::Legendre>;
155  num_points++) {
156  CAPTURE(num_points);
157  const double tolerance =
158  tolerance_offset * exp(-tolerance_scaling * num_points);
159  CAPTURE(tolerance);
160  const Mesh<Dim> mesh{num_points, Spectral::Basis::Legendre,
161  Spectral::Quadrature::GaussLobatto};
162  FirstOrderEllipticSolutionsTestHelpers::verify_solution<System>(
163  solution, fluxes_computer, mesh, coord_map, tolerance,
164  package_fluxes_args(mesh));
165  }
166 }
167 
168 /*!
169  * \ingroup TestingFrameworkGroup
170  * Test that the `solution` numerically solves the `System` on the given grid
171  * and that the discretization error decreases as a power law.
172  *
173  * \details The tolerance is computed as
174  *
175  * \f{equation}
176  * C \left(N_\mathrm{points}\right)^{-p}
177  * \f}
178  *
179  * where \f$C\f$ is the `tolerance_offset`, \f$p\f$ is the `tolerance_pow` and
180  * \f$N_\mathrm{points}\f$ is the number of grid points per dimension.
181  */
182 template <typename System, typename SolutionType,
183  size_t Dim = System::volume_dim, typename... Maps>
185  const SolutionType& solution,
186  const typename System::fluxes& fluxes_computer,
188  coord_map,
189  const double tolerance_offset, const double tolerance_pow) {
190  INFO("Verify solution with power-law convergence");
191  for (size_t num_points = Spectral::minimum_number_of_points<
192  Spectral::Basis::Legendre, Spectral::Quadrature::GaussLobatto>;
193  num_points <=
194  Spectral::maximum_number_of_points<Spectral::Basis::Legendre>;
195  num_points++) {
196  CAPTURE(num_points);
197  const double tolerance = tolerance_offset * pow(num_points, -tolerance_pow);
198  CAPTURE(tolerance);
199  FirstOrderEllipticSolutionsTestHelpers::verify_solution<System>(
200  solution, fluxes_computer,
201  Mesh<Dim>{num_points, Spectral::Basis::Legendre,
202  Spectral::Quadrature::GaussLobatto},
203  coord_map, tolerance);
204  }
205 }
206 
207 } // namespace FirstOrderEllipticSolutionsTestHelpers
elliptic::first_order_operator
void first_order_operator(const gsl::not_null< Variables< OperatorTags > * > operator_applied_to_vars, const Variables< DivFluxesTags > &div_fluxes, const Variables< SourcesTags > &sources) noexcept
Compute the bulk contribution to the operator represented by the OperatorTags.
Definition: FirstOrderOperator.hpp:182
variables_from_tagged_tuple
Variables< tmpl::list< Tags... > > variables_from_tagged_tuple(const tuples::TaggedTuple< Tags... > &tuple) noexcept
Definition: Variables.hpp:797
pow
constexpr decltype(auto) pow(const T &t) noexcept
Compute t^N where N is an integer (positive or negative)
Definition: ConstantExpressions.hpp:160
db::PrefixTag
Mark a struct as a prefix tag by inheriting from this.
Definition: Tag.hpp:103
square
constexpr decltype(auto) square(const T &x)
Compute the square of x
Definition: ConstantExpressions.hpp:55
MakeWithRandomValues.hpp
std::tuple
db::SimpleTag
Mark a struct as a simple tag by inheriting from this.
Definition: Tag.hpp:36
CoordinateMap.hpp
std::sqrt
T sqrt(T... args)
divergence
Scalar< DataVector > divergence(const tnsr::I< DataVector, Dim, DerivativeFrame > &input, const Mesh< Dim > &mesh, const InverseJacobian< DataVector, Dim, Frame::Logical, DerivativeFrame > &inverse_jacobian) noexcept
Compute the divergence of the vector input
Definition: Divergence.cpp:26
TestHelpers.hpp
domain::CoordinateMap::inv_jacobian
constexpr InverseJacobian< double, dim, SourceFrame, TargetFrame > inv_jacobian(tnsr::I< double, dim, SourceFrame > source_point, const double time=std::numeric_limits< double >::signaling_NaN(), const std::unordered_map< std::string, std::unique_ptr< domain::FunctionsOfTime::FunctionOfTime >> &functions_of_time=std::unordered_map< std::string, std::unique_ptr< domain::FunctionsOfTime::FunctionOfTime >>{}) const noexcept override
Compute the inverse Jacobian of the Maps... at the point(s) source_point
Definition: CoordinateMap.hpp:333
Spectral::minimum_number_of_points
constexpr size_t minimum_number_of_points
Minimum number of possible collocation points for a quadrature type.
Definition: Spectral.hpp:128
cstddef
MakeWithValue.hpp
LogicalCoordinates.hpp
Mesh::number_of_grid_points
size_t number_of_grid_points() const noexcept
The total number of grid points in all dimensions.
Definition: Mesh.hpp:127
ConstantExpressions.hpp
Tuple.hpp
FirstOrderEllipticSolutionsTestHelpers::verify_solution_with_power_law_convergence
void verify_solution_with_power_law_convergence(const SolutionType &solution, const typename System::fluxes &fluxes_computer, const domain::CoordinateMap< Frame::Logical, Frame::Inertial, Maps... > &coord_map, const double tolerance_offset, const double tolerance_pow)
Definition: FirstOrderEllipticSolutionsTestHelpers.hpp:184
alg::max_element
decltype(auto) max_element(const Container &c)
Convenience wrapper around std::max_element.
Definition: Algorithm.hpp:327
Mesh
Holds the number of grid points, basis, and quadrature in each direction of the computational grid.
Definition: Mesh.hpp:47
FirstOrderEllipticSolutionsTestHelpers::verify_solution
void verify_solution(const SolutionType &solution, const typename System::fluxes &fluxes_computer, const Mesh< System::volume_dim > &mesh, const domain::CoordinateMap< Frame::Logical, Frame::Inertial, Maps... > coord_map, const double tolerance, const std::tuple< FluxesArgs... > &fluxes_args=std::tuple<>{}, const std::tuple< SourcesArgs... > &sources_args=std::tuple<>{})
Definition: FirstOrderEllipticSolutionsTestHelpers.hpp:43
logical_coordinates
void logical_coordinates(gsl::not_null< tnsr::I< DataVector, VolumeDim, Frame::Logical > * > logical_coords, const Mesh< VolumeDim > &mesh) noexcept
Compute the logical coordinates in an Element.
FirstOrderEllipticSolutionsTestHelpers::Tags::OperatorAppliedTo
Definition: FirstOrderEllipticSolutionsTestHelpers.hpp:31
limits
FirstOrderEllipticSolutionsTestHelpers::verify_smooth_solution
void verify_smooth_solution(const SolutionType &solution, const typename System::fluxes &fluxes_computer, const domain::CoordinateMap< Frame::Logical, Frame::Inertial, Maps... > &coord_map, const double tolerance_offset, const double tolerance_scaling, PackageFluxesArgs &&package_fluxes_args)
Definition: FirstOrderEllipticSolutionsTestHelpers.hpp:143
Tensor.hpp
db::wrap_tags_in
tmpl::transform< TagList, tmpl::bind< Wrapper, tmpl::_1, tmpl::pin< Args >... > > wrap_tags_in
Create a new tmpl::list of tags by wrapping each tag in TagList in Wrapper<_, Args....
Definition: PrefixHelpers.hpp:30
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:880
Mesh.hpp
alg::accumulate
decltype(auto) accumulate(const Container &c, T init)
Convenience wrapper around std::accumulate, returns std::accumulate(begin(c), end(c),...
Definition: Numeric.hpp:54
domain::CoordinateMap
A coordinate map or composition of coordinate maps.
Definition: CoordinateMap.hpp:236