TestHelpers.hpp
1 // Distributed under the MIT License.
2 // See LICENSE.txt for details.
3 
4 #pragma once
5 
7 
8 #include <memory>
9 #include <pup.h>
10 #include <string>
11 #include <tuple>
12 #include <utility>
13 
15 #include "Evolution/Systems/GeneralizedHarmonic/ConstraintDamping/DampingFunction.hpp"
16 #include "Framework/CheckWithRandomValues.hpp"
17 #include "Framework/SetupLocalPythonEnvironment.hpp"
20 #include "Parallel/PupStlCpp11.hpp"
21 #include "Utilities/Gsl.hpp"
22 #include "Utilities/Literals.hpp"
24 #include "Utilities/Overloader.hpp"
25 
26 namespace TestHelpers::GeneralizedHarmonic::ConstraintDamping {
27 namespace detail {
28 template <size_t VolumeDim, typename Fr, class... MemberArgs, class T>
29 void check_impl(
30  const std::unique_ptr<
32  VolumeDim, Fr>>& in_gh_damping_function,
33  const std::string& python_function_prefix, const T& used_for_size,
34  const std::array<std::pair<double, double>, 1> random_value_bounds,
35  const MemberArgs&... member_args) noexcept {
36  using GhDampingFunc =
38  Fr>;
39  using CallOperatorFunction =
40  Scalar<T> (GhDampingFunc::*)(const tnsr::I<T, VolumeDim, Fr>&)
41  const noexcept;
42 
43  const auto member_args_tuple = std::make_tuple(member_args...);
44  const auto helper =
45  [&python_function_prefix, &random_value_bounds, &member_args_tuple,
46  &used_for_size](
47  const std::unique_ptr<GhDampingFunc>& gh_damping_function) noexcept {
48  INFO("Testing call operator...")
49  pypp::check_with_random_values<1>(
50  static_cast<CallOperatorFunction>(&GhDampingFunc::operator()),
51  *gh_damping_function, "TestFunctions",
52  python_function_prefix + "_call_operator", random_value_bounds,
53  member_args_tuple, used_for_size);
54  INFO("Done testing call operator...")
55  INFO("Done\n\n")
56  };
57  helper(in_gh_damping_function);
58  helper(serialize_and_deserialize(in_gh_damping_function));
59 }
60 } // namespace detail
61 // @{
62 /*!
63  * \ingroup TestingFrameworkGroup
64  * \brief Test a DampingFunction by comparing to python functions
65  *
66  * The python functions must be added to TestFunctions.py in
67  * tests/Unit/Evolution/Systems/GeneralizedHarmonic/ConstraintDamping/Python.
68  * Each python function for a corresponding DampingFunction should begin
69  * with a prefix `python_function_prefix`. The prefix for each class of
70  * DampingFunction is arbitrary, but should generally be descriptive (e.g.
71  * 'gaussian_plus_constant') of the DampingFunction.
72  *
73  * If a DampingFunction class has member variables set by its constructor, then
74  * these member variables must be passed in as the last arguments to the `check`
75  * function`. Each python function must take these same arguments as the
76  * trailing arguments.
77  */
78 template <class DampingFunctionType, class T, class... MemberArgs>
79 void check(std::unique_ptr<DampingFunctionType> in_gh_damping_function,
80  const std::string& python_function_prefix, const T& used_for_size,
81  const std::array<std::pair<double, double>, 1>& random_value_bounds,
82  const MemberArgs&... member_args) noexcept {
83  detail::check_impl(
86  DampingFunctionType::volume_dim,
87  typename DampingFunctionType::frame>>(
88  std::move(in_gh_damping_function)),
89  python_function_prefix, used_for_size, random_value_bounds,
90  member_args...);
91 }
92 
93 template <class DampingFunctionType, class T, class... MemberArgs>
94 void check(DampingFunctionType in_gh_damping_function,
95  const std::string& python_function_prefix, const T& used_for_size,
96  const std::array<std::pair<double, double>, 1>& random_value_bounds,
97  const MemberArgs&... member_args) noexcept {
98  detail::check_impl(
101  DampingFunctionType::volume_dim,
102  typename DampingFunctionType::frame>>(
103  std::make_unique<DampingFunctionType>(
104  std::move(in_gh_damping_function))),
105  python_function_prefix, used_for_size, random_value_bounds,
106  member_args...);
107 }
108 // @}
109 } // namespace TestHelpers::GeneralizedHarmonic::ConstraintDamping
std::string
utility
Literals.hpp
std::pair
TestingFramework.hpp
MakeWithRandomValues.hpp
tuple
pypp::call
R call(const std::string &module_name, const std::string &function_name, const Args &... t)
Calls a Python function from a module/file with given parameters.
Definition: Pypp.hpp:336
TestHelpers.hpp
TestHelpers::MathFunctions::check
void check(std::unique_ptr< MathFunctionType > in_math_function, const std::string &python_function_prefix, const T &used_for_size, const std::array< std::pair< double, double >, 1 > random_value_bounds, const MemberArgs &... member_args) noexcept
Test a MathFunction by comparing to python functions.
Definition: TestHelpers.hpp:139
MakeWithValue.hpp
std::array
memory
serialize_and_deserialize
T serialize_and_deserialize(const T &t)
Serializes and deserializes an object t of type T
Definition: TestHelpers.hpp:45
PupStlCpp11.hpp
Scalar
Tensor< T, Symmetry<>, index_list<> > Scalar
Definition: TypeAliases.hpp:21
Gsl.hpp
GeneralizedHarmonic::ConstraintDamping::DampingFunction
Base class defining interface for constraint damping functions.
Definition: DampingFunction.hpp:31
Tensor.hpp
pypp
Contains all functions for pypp.
Definition: CheckWithRandomValues.hpp:63
std::unique_ptr
string