Skip to content

Commit

Permalink
Add function for the spacetime derivative of goth(g)
Browse files Browse the repository at this point in the history
  • Loading branch information
ncorsobh committed Dec 11, 2023
1 parent 7b3f85c commit c594f94
Show file tree
Hide file tree
Showing 6 changed files with 248 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/PointwiseFunctions/GeneralRelativity/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ spectre_target_sources(
Psi4Real.cpp
Ricci.cpp
Shift.cpp
SpacetimeDerivativeOfGothG.cpp
SpacetimeMetric.cpp
SpacetimeNormalOneForm.cpp
SpacetimeNormalVector.cpp
Expand Down Expand Up @@ -52,6 +53,7 @@ spectre_target_headers(
Psi4.hpp
Psi4Real.hpp
Shift.hpp
SpacetimeDerivativeOfGothG.hpp
SpacetimeMetric.hpp
SpacetimeNormalOneForm.hpp
SpacetimeNormalVector.hpp
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
// Distributed under the MIT License.
// See LICENSE.txt for details.

#include "PointwiseFunctions/GeneralRelativity/GeneralizedHarmonic/TimeDerivOfSpatialMetric.hpp"

#include "DataStructures/DataVector.hpp" // IWYU pragma: keep
#include "DataStructures/Tensor/Tensor.hpp" // IWYU pragma: keep

Check failure on line 7 in src/PointwiseFunctions/GeneralRelativity/SpacetimeDerivativeOfGothG.cpp

View workflow job for this annotation

GitHub Actions / Clang-tidy (Debug)

#includes are not sorted properly

Check failure on line 7 in src/PointwiseFunctions/GeneralRelativity/SpacetimeDerivativeOfGothG.cpp

View workflow job for this annotation

GitHub Actions / Clang-tidy (Release)

#includes are not sorted properly
#include "DataStructures/Tensor/Expressions/TensorExpression.hpp"
#include "DataStructures/Tensor/TypeAliases.hpp"
#include "DataStructures/VectorImpl.hpp"
#include "Utilities/ConstantExpressions.hpp"
#include "Utilities/ContainerHelpers.hpp"
#include "Utilities/GenerateInstantiations.hpp"
#include "Utilities/Gsl.hpp"

// IWYU pragma: no_forward_declare Tensor

namespace gr {
template <typename DataType, size_t SpatialDim, typename Frame>
void spacetime_deriv_of_goth_g(
gsl::not_null<tnsr::aBB<DataType, SpatialDim, Frame>*> da_goth_g,
const tnsr::AA<DataType, SpatialDim, Frame>& inverse_spacetime_metric,
const tnsr::abb<DataType, SpatialDim, Frame>& da_spacetime_metric,
const Scalar<DataType>& lapse,
const Scalar<DataType>& dt_lapse,
const tnsr::i<DataType, SpatialDim, Frame>& deriv_lapse,
const Scalar<DataType>& sqrt_det_spatial_metric,
const tnsr::a<DataType, SpatialDim, Frame>& da_det_spatial_metric) {
if (UNLIKELY(get_size(get<0, 0, 0>(*da_goth_g)) !=
get_size(get<0, 0, 0>(da_spacetime_metric)))) {
set_number_of_grid_points(da_goth_g, da_spacetime_metric);
}

const tnsr::a<DataType, SpatialDim, Frame> da_lapse{};
if constexpr (std::is_same_v<DataType, DataVector>) {
make_const_view(make_not_null(&da_lapse.get(0)), get(dt_lapse), 0,
get_size(get(dt_lapse)));
for (size_t i = 0; i < SpatialDim; ++i) {
make_const_view(make_not_null(&da_lapse.get(i + 1)), deriv_lapse.get(i),
0, get_size(deriv_lapse.get(0)));
}
}
else {
const_cast<double&>(da_lapse.get(0)) = get(dt_lapse);

Check failure on line 44 in src/PointwiseFunctions/GeneralRelativity/SpacetimeDerivativeOfGothG.cpp

View workflow job for this annotation

GitHub Actions / Clang-tidy (Debug)

do not use const_cast

Check failure on line 44 in src/PointwiseFunctions/GeneralRelativity/SpacetimeDerivativeOfGothG.cpp

View workflow job for this annotation

GitHub Actions / Clang-tidy (Release)

do not use const_cast
for (size_t i = 0; i < SpatialDim; ++i) {
const_cast<double&>(da_lapse.get(i + 1)) = deriv_lapse.get(i);

Check failure on line 46 in src/PointwiseFunctions/GeneralRelativity/SpacetimeDerivativeOfGothG.cpp

View workflow job for this annotation

GitHub Actions / Clang-tidy (Debug)

do not use const_cast

Check failure on line 46 in src/PointwiseFunctions/GeneralRelativity/SpacetimeDerivativeOfGothG.cpp

View workflow job for this annotation

GitHub Actions / Clang-tidy (Release)

do not use const_cast
}
}

tenex::evaluate<ti::a, ti::B, ti::C>(da_goth_g,
(da_lapse(ti::a) * sqrt_det_spatial_metric()
+ 0.5 * lapse() * da_det_spatial_metric(ti::a)
/ sqrt_det_spatial_metric()) * inverse_spacetime_metric(ti::B, ti::C)
- lapse() * sqrt_det_spatial_metric()
* inverse_spacetime_metric(ti::B, ti::D)
* inverse_spacetime_metric(ti::C, ti::E)
* da_spacetime_metric(ti::a, ti::d, ti::e));
}

template <typename DataType, size_t SpatialDim, typename Frame>
tnsr::aBB<DataType, SpatialDim, Frame> spacetime_deriv_of_goth_g(
const tnsr::AA<DataType, SpatialDim, Frame>& inverse_spacetime_metric,
const tnsr::abb<DataType, SpatialDim, Frame>& da_spacetime_metric,
const Scalar<DataType>& lapse,
const Scalar<DataType>& dt_lapse,
const tnsr::i<DataType, SpatialDim, Frame>& deriv_lapse,
const Scalar<DataType>& sqrt_det_spatial_metric,
const tnsr::a<DataType, SpatialDim, Frame>& da_det_spatial_metric) {
tnsr::aBB<DataType, SpatialDim, Frame> da_goth_g{};
gr::spacetime_deriv_of_goth_g(make_not_null(&da_goth_g),
inverse_spacetime_metric, da_spacetime_metric, lapse, dt_lapse,
deriv_lapse, sqrt_det_spatial_metric, da_det_spatial_metric);
return da_goth_g;
}
} // namespace gr

#define DIM(data) BOOST_PP_TUPLE_ELEM(0, data)
#define DTYPE(data) BOOST_PP_TUPLE_ELEM(1, data)
#define FRAME(data) BOOST_PP_TUPLE_ELEM(2, data)

#define INSTANTIATE(_, data) \
template void gr::spacetime_deriv_of_goth_g( \
const gsl::not_null<tnsr::aBB<DTYPE(data), DIM(data), FRAME(data)>*>\
da_goth_g, \
const tnsr::AA<DTYPE(data), DIM(data), FRAME(data)>& \
inverse_spacetime_metric, \
const tnsr::abb<DTYPE(data), DIM(data), FRAME(data)>& \
da_spacetime_metric, \
const Scalar<DTYPE(data)>& lapse, \
const Scalar<DTYPE(data)>& dt_lapse, \
const tnsr::i<DTYPE(data), DIM(data), FRAME(data)>& deriv_lapse, \
const Scalar<DTYPE(data)>& sqrt_det_spatial_metric, \
const tnsr::a<DTYPE(data), DIM(data), FRAME(data)>& \
da_det_spatial_metric); \
template tnsr::aBB<DTYPE(data), DIM(data), FRAME(data)> \
gr::spacetime_deriv_of_goth_g( \
const tnsr::AA<DTYPE(data), DIM(data), FRAME(data)>& \
inverse_spacetime_metric, \
const tnsr::abb<DTYPE(data), DIM(data), FRAME(data)>& \
da_spacetime_metric, \
const Scalar<DTYPE(data)>& lapse, \
const Scalar<DTYPE(data)>& dt_lapse, \
const tnsr::i<DTYPE(data), DIM(data), FRAME(data)>& deriv_lapse, \
const Scalar<DTYPE(data)>& sqrt_det_spatial_metric, \
const tnsr::a<DTYPE(data), DIM(data), FRAME(data)>& \
da_det_spatial_metric);

GENERATE_INSTANTIATIONS(INSTANTIATE, (1, 2, 3), (double, DataVector),
(Frame::Grid, Frame::Inertial))

#undef DIM
#undef DTYPE
#undef FRAME
#undef INSTANTIATE
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Distributed under the MIT License.
// See LICENSE.txt for details.

#pragma once

#include "DataStructures/DataBox/Prefixes.hpp"
#include "DataStructures/DataVector.hpp"
#include "DataStructures/Tensor/Tensor.hpp"
#include "Utilities/ContainerHelpers.hpp"
#include "Utilities/Gsl.hpp"
#include "Utilities/TMPL.hpp"

namespace gr {
/// @{
/*!
* \ingroup GeneralRelativityGroup
* \brief Computes spacetime derivative of
* \f$ \mathfrak{g}^{ab}\equiv (-g)^{1/2} g^{ab} \f$.
*
* \details Computes the spacetime derivative of
* \f$ \mathfrak{g}^{ab}\equiv (-g)^{1/2} g^{ab} \f$, defined
* in \cite Misner1973. Using \f$ (-g)^{1/2} = \alpha (\gamma)^{1/2} \f$, where
* \f$ \alpha \f$ is the lapse and \f$ \gamma \f$ is the determinant of the
* spatial metric (\cite BaumgarteShapiro), the derivative of
* \f$ \mathfrak{g}^{ab} \f$ expands out to
* \f{align}{
* \partial_c \mathfrak{g}^{ab} &= \left[\partial_c \alpha \gamma^{1/2}
* + \frac{1}{2} \alpha \partial_c \gamma \gamma^{-1/2} \right] g^{ab}
* - \alpha \gamma^{1/2} g^{ad} g^{be} \partial_c g_{de}.
* \f}
*/
template <typename DataType, size_t SpatialDim, typename Frame>
void spacetime_deriv_of_goth_g(
gsl::not_null<tnsr::aBB<DataType, SpatialDim, Frame>*> da_goth_g,
const tnsr::AA<DataType, SpatialDim, Frame>& inverse_spacetime_metric,
const tnsr::abb<DataType, SpatialDim, Frame>& da_spacetime_metric,
const Scalar<DataType>& lapse,
const Scalar<DataType>& dt_lapse,
const tnsr::i<DataType, SpatialDim, Frame>& deriv_lapse,
const Scalar<DataType>& sqrt_det_spatial_metric,
const tnsr::a<DataType, SpatialDim, Frame>& da_det_spatial_metric);

template <typename DataType, size_t SpatialDim, typename Frame>
tnsr::aBB<DataType, SpatialDim, Frame> spacetime_deriv_of_goth_g(
const tnsr::AA<DataType, SpatialDim, Frame>& inverse_spacetime_metric,
const tnsr::abb<DataType, SpatialDim, Frame>& da_spacetime_metric,
const Scalar<DataType>& lapse,
const Scalar<DataType>& dt_lapse,
const tnsr::i<DataType, SpatialDim, Frame>& deriv_lapse,
const Scalar<DataType>& sqrt_det_spatial_metric,
const tnsr::a<DataType, SpatialDim, Frame>& da_det_spatial_metric);
/// @}
} // namespace gr
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ set(LIBRARY_SOURCES
Test_ProjectionOperators.cpp
Test_Psi4.cpp
Test_Ricci.cpp
Test_SpacetimeDerivativeOfGothG.cpp
Test_Tags.cpp
Test_Transform.cpp
Test_WeylElectric.cpp
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Distributed under the MIT License.
# See LICENSE.txt for details.

import numpy as np

# Functions testing SpacetimeDerivativeOfGothG


def spacetime_deriv_of_goth_g(
inverse_spacetime_metric,
da_spacetime_metric,
lapse,
dt_lapse,
deriv_lapse,
sqrt_det_spatial_metric,
da_det_spatial_metric,
):
da_lapse = np.array([dt_lapse])
for d_lapse in deriv_lapse:
da_lapse = np.append(da_lapse, d_lapse)
return np.tensordot(
da_lapse * sqrt_det_spatial_metric
+ 0.5 * lapse * da_det_spatial_metric / sqrt_det_spatial_metric,
inverse_spacetime_metric,
axes=0,
) - lapse * sqrt_det_spatial_metric * np.einsum(
"im,jn,hmn",
inverse_spacetime_metric,
inverse_spacetime_metric,
da_spacetime_metric,
)


# End functions for testing SpacetimeDerivativeOfGothG
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Distributed under the MIT License.
// See LICENSE.txt for details.

#include "Framework/TestingFramework.hpp"

#include <cstddef>
#include <string>

#include "DataStructures/DataVector.hpp"
#include "DataStructures/Tensor/Tensor.hpp"
#include "Framework/CheckWithRandomValues.hpp"
#include "Framework/SetupLocalPythonEnvironment.hpp"
#include "Helpers/DataStructures/DataBox/TestHelpers.hpp"
#include "PointwiseFunctions/GeneralRelativity/SpacetimeDerivativeOfGothG.hpp"

namespace gr {

template <typename DataType, size_t SpatialDim>
void test_compute_spacetime_deriv_of_goth_g(const DataType& used_for_size) {
pypp::check_with_random_values<1>(
static_cast<tnsr::aBB<DataVector, SpatialDim, Frame::Inertial> (*)(
const tnsr::AA<DataType, SpatialDim, Frame::Inertial>&,
const tnsr::abb<DataType, SpatialDim, Frame::Inertial>&,
const Scalar<DataType>&,
const Scalar<DataType>&,
const tnsr::i<DataType, SpatialDim, Frame::Inertial>&,
const Scalar<DataType>&,
const tnsr::a<DataType, SpatialDim, Frame::Inertial>&)>(
&spacetime_deriv_of_goth_g<DataType, SpatialDim, Frame::Inertial>),
"SpacetimeDerivativeOfGothG", "spacetime_deriv_of_goth_g",
{{{0.0, 1.0}}}, used_for_size);
}

SPECTRE_TEST_CASE(
"Unit.PointwiseFunctions.GeneralRelativity.SpacetimeDerivativeOfGothG",
"[PointwiseFunctions][Unit]") {
pypp::SetupLocalPythonEnvironment local_python_env(
"PointwiseFunctions/GeneralRelativity/");
const DataVector used_for_size(5);
test_compute_spacetime_deriv_of_goth_g<DataVector, 1>(used_for_size);
test_compute_spacetime_deriv_of_goth_g<DataVector, 3>(used_for_size);
}

} // namespace gr

0 comments on commit c594f94

Please sign in to comment.