Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Strahlkorper construction from different frame #5871

Merged
merged 1 commit into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 0 additions & 38 deletions src/NumericalAlgorithms/SphericalHarmonics/Strahlkorper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,44 +65,6 @@ Strahlkorper<Frame>::Strahlkorper(const size_t l_max, const size_t m_max,
strahlkorper_coefs_.begin());
}

template <typename Frame>
Strahlkorper<Frame>::Strahlkorper(const size_t l_max, const size_t m_max,
const Strahlkorper& another_strahlkorper)
: l_max_(l_max),
m_max_(m_max),
ylm_(l_max, m_max),
center_(another_strahlkorper.center_),
strahlkorper_coefs_(another_strahlkorper.ylm_.prolong_or_restrict(
another_strahlkorper.strahlkorper_coefs_, ylm_)) {}

template <typename Frame>
Strahlkorper<Frame>::Strahlkorper(DataVector coefs,
const Strahlkorper& another_strahlkorper)
: l_max_(another_strahlkorper.l_max_),
m_max_(another_strahlkorper.m_max_),
ylm_(another_strahlkorper.ylm_),
center_(another_strahlkorper.center_),
strahlkorper_coefs_(std::move(coefs)) {
ASSERT(
strahlkorper_coefs_.size() == another_strahlkorper.ylm_.spectral_size(),
"Bad size " << strahlkorper_coefs_.size() << ", expected "
<< another_strahlkorper.ylm_.spectral_size());
}

template <typename Frame>
Strahlkorper<Frame>::Strahlkorper(DataVector coefs,
Strahlkorper&& another_strahlkorper)
: l_max_(another_strahlkorper.l_max_),
m_max_(another_strahlkorper.m_max_),
ylm_(std::move(another_strahlkorper.ylm_)),
// clang-tidy: do not std::move trivially constructable types
center_(std::move(another_strahlkorper.center_)), // NOLINT
strahlkorper_coefs_(std::move(coefs)) {
ASSERT(strahlkorper_coefs_.size() == ylm_.spectral_size(),
"Bad size " << strahlkorper_coefs_.size() << ", expected "
<< ylm_.spectral_size());
}

template <typename Frame>
void Strahlkorper<Frame>::pup(PUP::er& p) {
p | l_max_;
Expand Down
51 changes: 44 additions & 7 deletions src/NumericalAlgorithms/SphericalHarmonics/Strahlkorper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "NumericalAlgorithms/SphericalHarmonics/Spherepack.hpp"
#include "Options/String.hpp"
#include "Utilities/ForceInline.hpp"
#include "Utilities/StdArrayHelpers.hpp"

/// \cond
namespace PUP {
Expand Down Expand Up @@ -49,6 +50,10 @@ class Strahlkorper {

// Pup needs default constructor
Strahlkorper() = default;
Strahlkorper(const Strahlkorper&) = default;
Strahlkorper(Strahlkorper&&) = default;
Strahlkorper& operator=(const Strahlkorper&) = default;
Strahlkorper& operator=(Strahlkorper&&) = default;

/// Construct a sphere of radius `radius` with a given center.
Strahlkorper(size_t l_max, size_t m_max, double radius,
Expand Down Expand Up @@ -87,19 +92,51 @@ class Strahlkorper {
const ModalVector& spectral_coefficients,
std::array<double, 3> center);

/// Copies a Strahlkorper from another frame into this Strahlkorper.
///
/// \note The `OtherFrame` is ignored.
template <typename OtherFrame>
explicit Strahlkorper(const Strahlkorper<OtherFrame>& another_strahlkorper)
: l_max_(another_strahlkorper.l_max()),
m_max_(another_strahlkorper.m_max()),
ylm_(l_max_, m_max_),
center_(another_strahlkorper.expansion_center()),
strahlkorper_coefs_(another_strahlkorper.coefficients()) {}

/// Prolong or restrict another surface to the given `l_max` and `m_max`.
///
/// \note The `OtherFrame` is ignored.
template <typename OtherFrame>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For all of these, document that OtherFrame is ignored.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Arbitrary line] Do you want a frame-change constructor that doesn't do prolongation or similar?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Strahlkorper{coefs, other_strahlkorper} achieves that if you use the other_strahlkorper.coefficients() for the first argument. I don't really want to deal with the template gymnastics I'll have to do to avoid a conflict between the regular copy constructor, and a "copy constructor" for a different frame.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strahlkorper(const Strahlkorper&) = default;
Strahlkorper(Strahlkorper&&) = default;

template <typename OtherFrame>
Strahlkorper(const Strahlkorper<OtherFrame>& other) : /* ... */ {}

?

(Probably explicit.)

Strahlkorper(size_t l_max, size_t m_max,
const Strahlkorper& another_strahlkorper);
const Strahlkorper<OtherFrame>& another_strahlkorper)
: l_max_(l_max),
m_max_(m_max),
ylm_(l_max, m_max),
center_(another_strahlkorper.expansion_center()),
strahlkorper_coefs_(
another_strahlkorper.ylm_spherepack().prolong_or_restrict(
another_strahlkorper.coefficients(), ylm_)) {}

/// Construct a Strahlkorper from another Strahlkorper,
/// but explicitly specifying the coefficients.
/// Here coefficients are in the same storage scheme
/// as the `coefficients()` member function returns.
Strahlkorper(DataVector coefs, const Strahlkorper& another_strahlkorper);

/// Move-construct a Strahlkorper from another Strahlkorper,
/// explicitly specifying the coefficients.
Strahlkorper(DataVector coefs, Strahlkorper&& another_strahlkorper);
///
/// \note The `OtherFrame` is ignored.
template <typename OtherFrame>
Strahlkorper(DataVector coefs,
const Strahlkorper<OtherFrame>& another_strahlkorper)
: l_max_(another_strahlkorper.l_max()),
m_max_(another_strahlkorper.m_max()),
ylm_(another_strahlkorper.ylm_spherepack()),
center_(another_strahlkorper.expansion_center()),
strahlkorper_coefs_(std::move(coefs)) {
ASSERT(
strahlkorper_coefs_.size() ==
another_strahlkorper.ylm_spherepack().spectral_size(),
"Bad size " << strahlkorper_coefs_.size() << ", expected "
<< another_strahlkorper.ylm_spherepack().spectral_size());
}

/// Serialization for Charm++
// NOLINTNEXTLINE(google-runtime-references)
Expand Down Expand Up @@ -186,7 +223,7 @@ struct Strahlkorper {
using type = ylm::Strahlkorper<Frame>;
static constexpr Options::String help{"A star-shaped surface"};
};
} // namespace OptionTags
} // namespace OptionTags

template <typename Frame>
bool operator==(const Strahlkorper<Frame>& lhs,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,32 @@ void test_construct_from_options() {
Strahlkorper<Frame::Inertial>(6, 6, 4.5, {{1., 2., 3.}}));
}

void test_strahlkorper_from_other_strahlkorper() {
const Strahlkorper<Frame::Inertial> inertial_strahlkorper{
4_st, 1.2, std::array{1.0, 2.0, 3.0}};
Strahlkorper<Frame::Grid> grid_strahlkorper{inertial_strahlkorper};

const auto check_equal = [](const auto& s1, const auto& s2) {
CHECK(s1.coefficients() == s2.coefficients());
CHECK(s1.l_max() == s2.l_max());
CHECK(s1.m_max() == s2.m_max());
CHECK(s1.expansion_center() == s2.expansion_center());
};

check_equal(inertial_strahlkorper, grid_strahlkorper);

grid_strahlkorper = Strahlkorper<Frame::Grid>{
inertial_strahlkorper.coefficients(), inertial_strahlkorper};

check_equal(inertial_strahlkorper, grid_strahlkorper);

grid_strahlkorper =
Strahlkorper<Frame::Grid>{8_st, 8_st, inertial_strahlkorper};

check_equal(Strahlkorper<Frame::Grid>(8_st, 1.2, std::array{1.0, 2.0, 3.0}),
grid_strahlkorper);
}

} // namespace

SPECTRE_TEST_CASE("Unit.ApparentHorizonFinder.Strahlkorper",
Expand Down Expand Up @@ -225,11 +251,10 @@ SPECTRE_TEST_CASE("Unit.ApparentHorizonFinder.Strahlkorper",
return Strahlkorper<Frame::Inertial>(std::move(sk));
});
test_construct_from_options();
}

SPECTRE_TEST_CASE("Unit.ApparentHorizonFinder.Strahlkorper.Serialization",
"[ApparentHorizonFinder][Unit]") {
Strahlkorper<Frame::Inertial> s(4, 4, 2.0, {{1.0, 2.0, 3.0}});
test_serialization(s);
test_strahlkorper_from_other_strahlkorper();
{
Strahlkorper<Frame::Inertial> s(4, 4, 2.0, {{1.0, 2.0, 3.0}});
test_serialization(s);
}
}
} // namespace ylm
Original file line number Diff line number Diff line change
Expand Up @@ -1152,24 +1152,25 @@ SPECTRE_TEST_CASE("Unit.GrSurfaces.RadialDistance",
// Check cases where one has more resolution than the other
gr::surfaces::radial_distance(
make_not_null(&radial_dist), strahlkorper_a,
ylm::Strahlkorper(strahlkorper_b.l_max() - 1, strahlkorper_b.m_max() - 1,
strahlkorper_b));
ylm::Strahlkorper<Frame::Inertial>(strahlkorper_b.l_max() - 1,
strahlkorper_b.m_max() - 1,
strahlkorper_b));
CHECK_ITERABLE_APPROX(radial_dist, expected_radial_dist_a_minus_b);
gr::surfaces::radial_distance(
make_not_null(&radial_dist),
ylm::Strahlkorper(strahlkorper_b.l_max() - 1, strahlkorper_b.m_max() - 1,
strahlkorper_b),
strahlkorper_a);
gr::surfaces::radial_distance(make_not_null(&radial_dist),
ylm::Strahlkorper<Frame::Inertial>(
strahlkorper_b.l_max() - 1,
strahlkorper_b.m_max() - 1, strahlkorper_b),
strahlkorper_a);
CHECK_ITERABLE_APPROX(radial_dist, expected_radial_dist_b_minus_a);
gr::surfaces::radial_distance(
make_not_null(&radial_dist), strahlkorper_a,
ylm::Strahlkorper(strahlkorper_b.l_max(), strahlkorper_b.m_max() - 1,
strahlkorper_b));
ylm::Strahlkorper<Frame::Inertial>(
strahlkorper_b.l_max(), strahlkorper_b.m_max() - 1, strahlkorper_b));
CHECK_ITERABLE_APPROX(radial_dist, expected_radial_dist_a_minus_b);
gr::surfaces::radial_distance(
make_not_null(&radial_dist),
ylm::Strahlkorper(strahlkorper_b.l_max(), strahlkorper_b.m_max() - 1,
strahlkorper_b),
ylm::Strahlkorper<Frame::Inertial>(
strahlkorper_b.l_max(), strahlkorper_b.m_max() - 1, strahlkorper_b),
strahlkorper_a);
CHECK_ITERABLE_APPROX(radial_dist, expected_radial_dist_b_minus_a);

Expand Down
Loading