Skip to content

Commit

Permalink
WIP use autodiff to test punctures linearization
Browse files Browse the repository at this point in the history
  • Loading branch information
nilsvu committed Nov 13, 2024
1 parent 090df8b commit 2565c3d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
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
25 changes: 25 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,25 @@

namespace {

template <typename DataType, typename VarDataType>
VarDataType puncture_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(
&puncture_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 +51,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

0 comments on commit 2565c3d

Please sign in to comment.