Skip to content

Commit

Permalink
WIP more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nilsvu committed Nov 13, 2024
1 parent e4b94e5 commit 97d3701
Show file tree
Hide file tree
Showing 10 changed files with 126 additions and 57 deletions.
4 changes: 4 additions & 0 deletions src/Domain/CoordinateMaps/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,9 @@ target_link_libraries(
SphericalHarmonics
)

if (TARGET autodiff::autodiff)
target_link_libraries(${LIBRARY} PUBLIC autodiff::autodiff)
endif()

add_subdirectory(Python)
add_subdirectory(TimeDependent)
25 changes: 17 additions & 8 deletions src/Domain/CoordinateMaps/Interval.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#include "Domain/CoordinateMaps/Interval.hpp"

#include <autodiff/forward/dual.hpp>
#include <autodiff/reverse/var.hpp>
#include <cmath>
#include <optional>
#include <pup.h>
Expand Down Expand Up @@ -253,18 +255,25 @@ bool operator==(const CoordinateMaps::Interval& lhs,
// Explicit instantiations
#define DTYPE(data) BOOST_PP_TUPLE_ELEM(0, data)

#define INSTANTIATE(_, data) \
template std::array<tt::remove_cvref_wrap_t<DTYPE(data)>, 1> \
Interval::operator()(const std::array<DTYPE(data), 1>& source_coords) const; \
template tnsr::Ij<tt::remove_cvref_wrap_t<DTYPE(data)>, 1, Frame::NoFrame> \
Interval::jacobian(const std::array<DTYPE(data), 1>& source_coords) const; \
template tnsr::Ij<tt::remove_cvref_wrap_t<DTYPE(data)>, 1, Frame::NoFrame> \
Interval::inv_jacobian(const std::array<DTYPE(data), 1>& source_coords) \
#define INSTANTIATE(_, data) \
template std::array<tt::remove_cvref_wrap_t<DTYPE(data)>, 1> \
Interval::operator()(const std::array<DTYPE(data), 1>& source_coords) const;

#define INSTANTIATE_JAC(_, data) \
template tnsr::Ij<tt::remove_cvref_wrap_t<DTYPE(data)>, 1, Frame::NoFrame> \
Interval::jacobian(const std::array<DTYPE(data), 1>& source_coords) const; \
template tnsr::Ij<tt::remove_cvref_wrap_t<DTYPE(data)>, 1, Frame::NoFrame> \
Interval::inv_jacobian(const std::array<DTYPE(data), 1>& source_coords) \
const;

GENERATE_INSTANTIATIONS(INSTANTIATE, (double, DataVector,
std::reference_wrapper<const double>,
std::reference_wrapper<const DataVector>))
std::reference_wrapper<const DataVector>,
autodiff::dual, autodiff::var))
GENERATE_INSTANTIATIONS(INSTANTIATE_JAC,
(double, DataVector,
std::reference_wrapper<const double>,
std::reference_wrapper<const DataVector>))
#undef DTYPE
#undef INSTANTIATE
} // namespace domain::CoordinateMaps
55 changes: 33 additions & 22 deletions src/Domain/CoordinateMaps/Wedge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#include "Domain/CoordinateMaps/Wedge.hpp"

#include <autodiff/forward/dual.hpp>
#include <autodiff/reverse/var.hpp>
#include <climits>
#include <cmath>
#include <cstddef>
Expand Down Expand Up @@ -251,17 +253,17 @@ template <bool FuncIsXi, typename T>
tt::remove_cvref_wrap_t<T> Wedge<Dim>::get_cap_angular_function(
const T& lowercase_xi_or_eta) const {
constexpr auto cap_index = static_cast<size_t>(not FuncIsXi);
if (not with_equiangular_map_) {
return lowercase_xi_or_eta;
}
if (opening_angles_.has_value() and
opening_angles_distribution_.has_value()) {
return with_equiangular_map_
? tan(0.5 * opening_angles_.value()[cap_index]) *
tan(0.5 * opening_angles_distribution_.value()[cap_index] *
lowercase_xi_or_eta) /
tan(0.5 * opening_angles_distribution_.value()[cap_index])
: lowercase_xi_or_eta;
return tan(0.5 * opening_angles_.value()[cap_index]) *
tan(0.5 * opening_angles_distribution_.value()[cap_index] *
lowercase_xi_or_eta) /
tan(0.5 * opening_angles_distribution_.value()[cap_index]);
} else {
return with_equiangular_map_ ? tan(M_PI_4 * lowercase_xi_or_eta)
: lowercase_xi_or_eta;
return tan(M_PI_4 * lowercase_xi_or_eta);
}
}

Expand Down Expand Up @@ -510,11 +512,14 @@ std::array<tt::remove_cvref_wrap_t<T>, Dim> Wedge<Dim>::operator()(
const bool zero_offset = not cube_half_length_.has_value();

std::array<ReturnType, Dim> physical_coords{};
physical_coords[radial_coord] =
zero_offset ? generalized_z
: generalized_z * (1.0 - rotated_focus[radial_coord] /
cube_half_length_.value()) +
rotated_focus[radial_coord];
if (zero_offset) {
physical_coords[radial_coord] = generalized_z;
} else {
physical_coords[radial_coord] =
generalized_z *
(1.0 - rotated_focus[radial_coord] / cube_half_length_.value()) +
rotated_focus[radial_coord];
}
if (zero_offset) {
physical_coords[polar_coord] = generalized_z * cap[0];
} else {
Expand Down Expand Up @@ -803,7 +808,6 @@ Wedge<Dim>::inv_jacobian(const std::array<T, Dim>& source_coords) const {
xi *= 0.5;
}


std::array<ReturnType, Dim - 1> cap{};
std::array<ReturnType, Dim - 1> cap_deriv{};
cap[0] = get_cap_angular_function<true>(xi);
Expand Down Expand Up @@ -1020,18 +1024,25 @@ bool operator!=(const Wedge<Dim>& lhs, const Wedge<Dim>& rhs) {
#define INSTANTIATE_DTYPE(_, data) \
template std::array<tt::remove_cvref_wrap_t<DTYPE(data)>, DIM(data)> \
Wedge<DIM(data)>::operator()( \
const std::array<DTYPE(data), DIM(data)>& source_coords) const; \
template tnsr::Ij<tt::remove_cvref_wrap_t<DTYPE(data)>, DIM(data), \
Frame::NoFrame> \
Wedge<DIM(data)>::jacobian( \
const std::array<DTYPE(data), DIM(data)>& source_coords) const; \
template tnsr::Ij<tt::remove_cvref_wrap_t<DTYPE(data)>, DIM(data), \
Frame::NoFrame> \
Wedge<DIM(data)>::inv_jacobian( \
const std::array<DTYPE(data), DIM(data)>& source_coords) const;

#define INSTANTIATE_DTYPE_JAC(_, data) \
template tnsr::Ij<tt::remove_cvref_wrap_t<DTYPE(data)>, DIM(data), \
Frame::NoFrame> \
Wedge<DIM(data)>::jacobian( \
const std::array<DTYPE(data), DIM(data)>& source_coords) const; \
template tnsr::Ij<tt::remove_cvref_wrap_t<DTYPE(data)>, DIM(data), \
Frame::NoFrame> \
Wedge<DIM(data)>::inv_jacobian( \
const std::array<DTYPE(data), DIM(data)>& source_coords) const;

GENERATE_INSTANTIATIONS(INSTANTIATE_DIM, (2, 3))
GENERATE_INSTANTIATIONS(INSTANTIATE_DTYPE, (2, 3),
(double, DataVector,
std::reference_wrapper<const double>,
std::reference_wrapper<const DataVector>,
autodiff::dual, autodiff::var))
GENERATE_INSTANTIATIONS(INSTANTIATE_DTYPE_JAC, (2, 3),
(double, DataVector,
std::reference_wrapper<const double>,
std::reference_wrapper<const DataVector>))
Expand Down
5 changes: 4 additions & 1 deletion src/Domain/Structure/OrientationMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#include "Domain/Structure/OrientationMap.hpp"

#include <autodiff/forward/dual.hpp>
#include <autodiff/reverse/var.hpp>
#include <functional>
#include <ostream>
#include <pup.h>
Expand Down Expand Up @@ -290,7 +292,8 @@ GENERATE_INSTANTIATIONS(INSTANTIATION, (1, 2, 3),
std::reference_wrapper<double> const,
std::reference_wrapper<DataVector> const,
std::reference_wrapper<const double> const,
std::reference_wrapper<const DataVector> const))
std::reference_wrapper<const DataVector> const,
autodiff::dual, autodiff::var))

#undef INSTANTIATION
#undef DTYPE
Expand Down
26 changes: 0 additions & 26 deletions tests/Unit/Domain/CoordinateMaps/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,8 @@
set(LIBRARY "Test_CoordinateMaps")

set(LIBRARY_SOURCES
Test_Affine.cpp
Test_BulgedCube.cpp
Test_Composition.cpp
Test_CoordinateMap.cpp
Test_CylindricalEndcap.cpp
Test_CylindricalFlatEndcap.cpp
Test_CylindricalFlatSide.cpp
Test_CylindricalSide.cpp
Test_DiscreteRotation.cpp
Test_Distribution.cpp
Test_EquatorialCompression.cpp
Test_Equiangular.cpp
Test_Frustum.cpp
Test_Identity.cpp
Test_Interval.cpp
Test_KerrHorizonConforming.cpp
Test_ProductMaps.cpp
Test_Rotation.cpp
Test_SpecialMobius.cpp
Test_Tags.cpp
Test_TimeDependentHelpers.cpp
Test_UniformCylindricalEndcap.cpp
Test_UniformCylindricalFlatEndcap.cpp
Test_UniformCylindricalSide.cpp
Test_Wedge2D.cpp
Test_Wedge3D.cpp
)

add_test_library(${LIBRARY} "${LIBRARY_SOURCES}")
Expand All @@ -49,5 +25,3 @@ target_link_libraries(
Spectral
Utilities
)

add_subdirectory(TimeDependent)
1 change: 1 addition & 0 deletions tests/Unit/Domain/CoordinateMaps/Test_Interval.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ void test_coordinate_map(
test_inverse_map(map, random_point);
test_jacobian(map, random_point);
test_inv_jacobian(map, random_point);
test_jacobian_autodiff(map, random_point);
}
const std::array<double, 1> point_xA{{xA}};
const std::array<double, 1> point_xB{{xB}};
Expand Down
1 change: 1 addition & 0 deletions tests/Unit/Elliptic/Systems/Punctures/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ add_test_library(${LIBRARY} "${LIBRARY_SOURCES}")
target_link_libraries(
${LIBRARY}
PRIVATE
autodiff::autodiff
DataStructuresHelpers
Punctures
)
Expand Down
31 changes: 31 additions & 0 deletions tests/Unit/Elliptic/Systems/Punctures/Test_Equations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
#include "Framework/TestingFramework.hpp"

#include <array>
#include <autodiff/forward/real.hpp>
#include <cstddef>
#include <string>

#include "DataStructures/DataVector.hpp"
#include "DataStructures/DynamicVector.hpp"
#include "Elliptic/Protocols/FirstOrderSystem.hpp"
#include "Elliptic/Systems/Punctures/FirstOrderSystem.hpp"
#include "Elliptic/Systems/Punctures/Sources.hpp"
Expand All @@ -18,6 +20,31 @@

namespace {

// template <typename DataType, typename VarDataType>
// void add_sources(const gsl::not_null<VarDataType*> puncture_equation,
// const DataType& alpha, const DataType& beta,
// const VarDataType& field) {
// *puncture_equation -= beta / pow<7>(alpha * (field + 1.) + 1.);
// }
template <typename DataType, typename VarDataType>
VarDataType add_sources(const Scalar<DataType>& alpha,
const Scalar<DataType>& beta,
const Scalar<VarDataType>& field) {
return get(beta) / pow<7>(get(alpha) * (get(field) + 1.) + 1.);
}

void add_linearized_sources_autodiff(
const gsl::not_null<Scalar<DataVector>*> linearized_puncture_equation,
const Scalar<DataVector>& alpha, const Scalar<DataVector>& beta,
const Scalar<DataVector>& field,
const Scalar<DataVector>& field_correction) {
Scalar<blaze::DynamicVector<autodiff::real>> field_dual(get(field));
const auto dS_du = autodiff::derivative(
&add_sources<DataVector, blaze::DynamicVector<autodiff::real>>,
autodiff::wrt(get(field_dual)), autodiff::at(alpha, beta, field_dual));
get(*linearized_puncture_equation) -= dS_du * get(field_correction);
}

void test_equations(const DataVector& used_for_size) {
const double eps = 1.e-12;
const auto seed = std::random_device{}();
Expand All @@ -30,6 +57,10 @@ void test_equations(const DataVector& used_for_size) {
&Punctures::add_linearized_sources, "Equations", {"linearized_sources"},
{{{-1., 1.}, {-1., 1.}, {-1., 1.}, {-1., 1.}}}, used_for_size, eps, seed,
fill_result_tensors);
pypp::check_with_random_values<4>(
&add_linearized_sources_autodiff, "Equations", {"linearized_sources"},
{{{-1., 1.}, {-1., 1.}, {-1., 1.}, {-1., 1.}}}, used_for_size, eps, seed,
fill_result_tensors);
}

void test_computers(const DataVector& used_for_size) {
Expand Down
1 change: 1 addition & 0 deletions tests/Unit/Helpers/Domain/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ add_spectre_library(${LIBRARY} ${SPECTRE_TEST_LIBS_TYPE} ${LIBRARY_SOURCES})
target_link_libraries(
${LIBRARY}
INTERFACE
autodiff::autodiff
Boost::boost
CoordinateMaps
FunctionsOfTime
Expand Down
34 changes: 34 additions & 0 deletions tests/Unit/Helpers/Domain/CoordinateMaps/TestMapHelpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include <algorithm>
#include <array>
#include <autodiff/reverse/var.hpp>
#include <cmath>
#include <functional>
#include <limits>
Expand Down Expand Up @@ -120,6 +121,36 @@ void test_jacobian(const Map& map,
}
}

template <typename Map>
void test_jacobian_autodiff(const Map& map,
const std::array<double, Map::dim>& test_point) {
INFO("Test Jacobian with autodiff");
CAPTURE(test_point);
constexpr size_t Dim = Map::dim;
// Use reverse mode because we want the full Jacobian (all derivatives)
std::array<autodiff::var, Dim> x{};
for (size_t i = 0; i < Dim; ++i) {
gsl::at(x, i) = gsl::at(test_point, i);
}
const auto y = map(x);
const auto jacobian = map.jacobian(test_point);
for (size_t i = 0; i < Dim; ++i) {
std::array<double, Dim> deriv_i{};
if constexpr (Dim == 1) {
deriv_i = autodiff::derivatives(gsl::at(y, i), autodiff::wrt(x[0]));
} else if constexpr (Dim == 2) {
deriv_i = autodiff::derivatives(gsl::at(y, i), autodiff::wrt(x[0], x[1]));
} else if constexpr (Dim == 3) {
deriv_i =
autodiff::derivatives(gsl::at(y, i), autodiff::wrt(x[0], x[1], x[2]));
}
for (size_t j = 0; j < Dim; ++j) {
INFO("i: " << i << " j: " << j);
CHECK(jacobian.get(i, j) == approx(gsl::at(deriv_i, j)));
}
}
}

template <typename Map>
void test_jacobian(
const Map& map, const std::array<double, Map::dim>& test_point,
Expand Down Expand Up @@ -586,16 +617,19 @@ void test_suite_for_map_on_unit_cube(const Map& map) {
test_coordinate_map_argument_types(map_to_test, origin);

test_jacobian(map_to_test, origin);
test_jacobian_autodiff(map_to_test, origin);
test_inv_jacobian(map_to_test, origin);
test_inverse_map(map_to_test, origin);

for (VolumeCornerIterator<Map::dim> vci{}; vci; ++vci) {
test_jacobian(map_to_test, vci.coords_of_corner());
test_jacobian_autodiff(map_to_test, vci.coords_of_corner());
test_inv_jacobian(map_to_test, vci.coords_of_corner());
test_inverse_map(map_to_test, vci.coords_of_corner());
}

test_jacobian(map_to_test, random_point);
test_jacobian_autodiff(map_to_test, random_point);
test_inv_jacobian(map_to_test, random_point);
test_inverse_map(map_to_test, random_point);
};
Expand Down

0 comments on commit 97d3701

Please sign in to comment.