Skip to content

Commit

Permalink
Merge pull request #6264 from nikwit/update-functions-of-time
Browse files Browse the repository at this point in the history
Add `UpdateFunctionsOfTime` action
  • Loading branch information
knelli2 authored Nov 17, 2024
2 parents ceeae05 + 740bd18 commit c05c009
Show file tree
Hide file tree
Showing 13 changed files with 460 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ spectre_target_headers(
ReceiveElementData.hpp
SendToElements.hpp
UpdateAcceleration.hpp
UpdateFunctionsOfTime.hpp
)
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ struct InitializeEvolvedVariables {

using simple_tags =
tmpl::list<variables_tag, dt_variables_tag, Tags::CurrentIteration,
Tags::ExpirationTime, Tags::WorldtubeRadius,
::Tags::HistoryEvolvedVariables<variables_tag>>;
using return_tags = simple_tags;

Expand All @@ -41,7 +42,8 @@ struct InitializeEvolvedVariables {
using mutable_global_cache_tags = tmpl::list<>;
using simple_tags_from_options = tmpl::list<Tags::InitialPositionAndVelocity>;
using argument_tags = tmpl::list<::Tags::TimeStepper<TimeStepper>,
Tags::InitialPositionAndVelocity>;
Tags::InitialPositionAndVelocity,
::Tags::Time, Tags::ExcisionSphere<Dim>>;
static void apply(
const gsl::not_null<Variables<
tmpl::list<Tags::EvolvedPosition<Dim>, Tags::EvolvedVelocity<Dim>>>*>
Expand All @@ -51,11 +53,20 @@ struct InitializeEvolvedVariables {
::Tags::dt<Tags::EvolvedVelocity<Dim>>>>*>
dt_evolved_vars,
const gsl::not_null<size_t*> current_iteration,
const gsl::not_null<double*> expiration_time,
const gsl::not_null<double*> worldtube_radius,
const gsl::not_null<::Tags::HistoryEvolvedVariables<variables_tag>::type*>
time_stepper_history,
const TimeStepper& time_stepper,
const std::array<tnsr::I<double, Dim>, 2>& initial_pos_and_vel) {
const std::array<tnsr::I<double, Dim>, 2>& initial_pos_and_vel,
const double initial_time, const ExcisionSphere<Dim>& excision_sphere) {
*current_iteration = 0;

// the functions of time should be ready during the first step but not the
// second. We choose the arbitrary value of 1e-10 here to ensure this.
*expiration_time = initial_time + 1e-10;
*worldtube_radius = excision_sphere.radius();

const size_t starting_order =
time_stepper.number_of_past_steps() == 0 ? time_stepper.order() : 1;
*time_stepper_history =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ struct ReceiveElementData {
for (const auto& [_, element_ylm_coefs] : inbox.at(time_step_id)) {
external_ylm_coefs += element_ylm_coefs;
}
const double wt_radius = db::get<Tags::ExcisionSphere<Dim>>(box).radius();
const double wt_radius = db::get<Tags::WorldtubeRadius>(box);
external_ylm_coefs /= wt_radius * wt_radius;

DataVector& psi_ylm_coefs =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
// Distributed under the MIT License.
// See LICENSE.txt for details.

#pragma once

#include <cstddef>
#include <memory>
#include <unordered_map>
#include <utility>

#include "ControlSystem/UpdateFunctionOfTime.hpp"
#include "DataStructures/DataBox/DataBox.hpp"
#include "DataStructures/DataBox/Prefixes.hpp"
#include "Domain/Creators/Tags/Domain.hpp"
#include "Evolution/Systems/CurvedScalarWave/Worldtube/Inboxes.hpp"
#include "Evolution/Systems/CurvedScalarWave/Worldtube/RadiusFunctions.hpp"
#include "Evolution/Systems/CurvedScalarWave/Worldtube/Tags.hpp"
#include "Evolution/Systems/CurvedScalarWave/Worldtube/Worldtube.hpp"
#include "Parallel/AlgorithmExecution.hpp"
#include "Parallel/GlobalCache.hpp"
#include "ParallelAlgorithms/EventsAndTriggers/Tags.hpp"
#include "ParallelAlgorithms/Initialization/MutateAssign.hpp"
#include "Time/Tags/TimeStepId.hpp"
#include "Time/TimeStepId.hpp"
#include "Time/TimeSteppers/TimeStepper.hpp"
#include "Utilities/Gsl.hpp"
#include "Utilities/TMPL.hpp"

namespace control_system {
// forward declare
struct UpdateMultipleFunctionsOfTime;
} // namespace control_system

namespace CurvedScalarWave::Worldtube::Actions {

/*!
* \brief Updates the functions of time according to the motion of the
* worldtube.
*
* \details We demand that the scalar charge is always in the center of the
* worldtube and therefore deform the grid to the track the particle's motion.
* In addition, the worldtube and black hole excision sphere radii are adjusted
* according to smooth_broken_power_law.
*/
struct UpdateFunctionsOfTime {
template <typename DbTagsList, typename... InboxTags, typename Metavariables,
typename ArrayIndex, typename ActionList,
typename ParallelComponent>
static Parallel::iterable_action_return_t apply(
db::DataBox<DbTagsList>& box,
tuples::TaggedTuple<InboxTags...>& /*inboxes*/,
Parallel::GlobalCache<Metavariables>& cache,
const ArrayIndex& /*array_index*/, ActionList /*meta*/,
const ParallelComponent* const /*meta*/) {
const double current_expiration_time = db::get<Tags::ExpirationTime>(box);
const double time = db::get<::Tags::Time>(box);
const auto& particle_pos_vel =
db::get<Tags::ParticlePositionVelocity<3>>(box);
const double& x = get<0>(particle_pos_vel[0]);
const double& y = get<1>(particle_pos_vel[0]);
const double& xdot = get<0>(particle_pos_vel[1]);
const double& ydot = get<1>(particle_pos_vel[1]);
const double r = hypot(x, y);
const double radial_vel = (xdot * x + ydot * y) / r;
const auto& excision_sphere = db::get<Tags::ExcisionSphere<3>>(box);
const double grid_radius_particle =
get(magnitude(excision_sphere.center()));

const DataVector angular_update{atan2(y, x),
(x * ydot - y * xdot) / square(r)};
const DataVector expansion_update{r / grid_radius_particle,
radial_vel / grid_radius_particle};

const auto& wt_radius_params =
db::get<Tags::WorldtubeRadiusParameters>(box);
const auto& bh_radius_params =
db::get<Tags::BlackHoleRadiusParameters>(box);
const double wt_radius_grid = excision_sphere.radius();
const double wt_radius_inertial =
smooth_broken_power_law(r, wt_radius_params[0], wt_radius_params[1],
wt_radius_params[2], wt_radius_params[3]);
const double wt_radius_derivative = smooth_broken_power_law_derivative(
r, wt_radius_params[0], wt_radius_params[1], wt_radius_params[2],
wt_radius_params[3]);
const double wt_radius_time_derivative = wt_radius_derivative * radial_vel;

const auto& bh_excision_sphere =
db::get<domain::Tags::Domain<3>>(box).excision_spheres().at(
"ExcisionSphereB");
const double bh_excision_radius_grid = bh_excision_sphere.radius();
const double bh_excision_radius_inertial =
smooth_broken_power_law(r, bh_radius_params[0], bh_radius_params[1],
bh_radius_params[2], bh_radius_params[3]);
const double bh_excision_radius_derivative =
smooth_broken_power_law_derivative(
r, bh_radius_params[0], bh_radius_params[1], bh_radius_params[2],
bh_radius_params[3]);
const double bh_excision_radius_time_derivative =
bh_excision_radius_derivative * radial_vel;

const double sqrt_4_pi = sqrt(4. * M_PI);
// The expansion map has already stretched the excision spheres and we need
// to account for that.
const DataVector size_a_update{
sqrt_4_pi * (wt_radius_grid - wt_radius_inertial / expansion_update[0]),
sqrt_4_pi *
(wt_radius_inertial * expansion_update[1] -
wt_radius_time_derivative * expansion_update[0]) /
square(expansion_update[0])};

const DataVector size_b_update{
sqrt_4_pi * (bh_excision_radius_grid -
bh_excision_radius_inertial / expansion_update[0]),
sqrt_4_pi *
(bh_excision_radius_inertial * expansion_update[1] -
bh_excision_radius_time_derivative * expansion_update[0]) /
square(expansion_update[0])};

// we set the new expiration time to 1/100th of a time step next to the
// current time. This is small enough that it can handle rapid time step
// decreases but large enough to avoid floating point precision issues.
const double new_expiration_time =
time +
0.01 * (db::get<::Tags::Next<::Tags::TimeStepId>>(box).substep_time() -
time);

db::mutate<Tags::ExpirationTime>(
[&new_expiration_time](const auto expiration_time) {
*expiration_time = new_expiration_time;
},
make_not_null(&box));
db::mutate<Tags::WorldtubeRadius>(
[&wt_radius_inertial](const auto wt_radius) {
*wt_radius = wt_radius_inertial;
},
make_not_null(&box));
std::unordered_map<std::string, std::pair<DataVector, double>>
all_updates{};
all_updates["Rotation"] =
std::make_pair(angular_update, new_expiration_time);
all_updates["Expansion"] =
std::make_pair(expansion_update, new_expiration_time);
all_updates["SizeA"] = std::make_pair(size_a_update, new_expiration_time);
all_updates["SizeB"] = std::make_pair(size_b_update, new_expiration_time);

Parallel::mutate<::domain::Tags::FunctionsOfTime,
control_system::UpdateMultipleFunctionsOfTime>(
cache, current_expiration_time, all_updates);

return {Parallel::AlgorithmExecution::Continue, std::nullopt};
}
};
} // namespace CurvedScalarWave::Worldtube::Actions
15 changes: 15 additions & 0 deletions src/Evolution/Systems/CurvedScalarWave/Worldtube/Tags.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,21 @@ struct CurrentIteration : db::SimpleTag {
using type = size_t;
};

/*!
* \brief The current expiration time of the functions of time which are
* controlled by the worldtube singleton.
*/
struct ExpirationTime : db::SimpleTag {
using type = double;
};

/*!
* \brief The current worldtube radius held by the singleton.
*/
struct WorldtubeRadius : db::SimpleTag {
using type = double;
};

/*!
* \brief The initial position and velocity of the scalar charge in inertial
* coordinates.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ set(LIBRARY_SOURCES
SingletonActions/Test_InitializeEvolvedVariables.cpp
SingletonActions/Test_ObserveWorldtubeSolution.cpp
SingletonActions/Test_UpdateAcceleration.cpp
SingletonActions/Test_UpdateFunctionsOfTime.cpp
)

add_test_library(${LIBRARY} "${LIBRARY_SOURCES}")

target_link_libraries(
${LIBRARY}
PRIVATE
ControlSystem
CurvedScalarWave
CurvedScalarWaveHelpers
DataStructures
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ struct MockMetavariables {
using const_global_cache_tags = tmpl::list<
domain::Tags::Domain<Dim>,
CurvedScalarWave::Tags::BackgroundSpacetime<gr::Solutions::KerrSchild>,
Tags::ExcisionSphere<Dim>, Tags::ExpansionOrder, Tags::MaxIterations,
Tags::Charge, Tags::Mass>;
Tags::ExcisionSphere<Dim>, Tags::WorldtubeRadius, Tags::ExpansionOrder,
Tags::MaxIterations, Tags::Charge, Tags::Mass>;
};

void test_iterations(const size_t max_iterations) {
Expand Down Expand Up @@ -166,10 +166,15 @@ void test_iterations(const size_t max_iterations) {
tuples::TaggedTuple<
domain::Tags::Domain<Dim>,
CurvedScalarWave::Tags::BackgroundSpacetime<gr::Solutions::KerrSchild>,
Tags::ExcisionSphere<Dim>, Tags::ExpansionOrder, Tags::MaxIterations,
Tags::Charge, Tags::Mass>
tuple_of_opts{shell.create_domain(), kerr_schild, excision_sphere,
expansion_order, max_iterations, charge,
Tags::ExcisionSphere<Dim>, Tags::WorldtubeRadius, Tags::ExpansionOrder,
Tags::MaxIterations, Tags::Charge, Tags::Mass>
tuple_of_opts{shell.create_domain(),
kerr_schild,
excision_sphere,
excision_sphere.radius(),
expansion_order,
max_iterations,
charge,
std::make_optional(mass)};
ActionTesting::MockRuntimeSystem<metavars> runner{std::move(tuple_of_opts)};
const auto element_ids = initial_element_ids(initial_refinements);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ struct MockMetavariables {
using dg_element_array = MockElementArray<MockMetavariables>;
using const_global_cache_tags =
tmpl::list<domain::Tags::Domain<Dim>, Tags::ExcisionSphere<Dim>,
Tags::ExpansionOrder, Tags::MaxIterations, Tags::Charge,
Tags::Mass>;
Tags::WorldtubeRadius, Tags::ExpansionOrder,
Tags::MaxIterations, Tags::Charge, Tags::Mass>;
};

// This test checks that `SendToWorldtube` integrates the regular field on the
Expand Down Expand Up @@ -165,10 +165,11 @@ SPECTRE_TEST_CASE("Unit.CurvedScalarWave.Worldtube.SendToWorldtube", "[Unit]") {
const auto& initial_extents = shell.initial_extents();
// self force and therefore iterative scheme is turned off
tuples::TaggedTuple<domain::Tags::Domain<Dim>, Tags::ExcisionSphere<Dim>,
Tags::ExpansionOrder, Tags::MaxIterations, Tags::Charge,
Tags::Mass>
Tags::WorldtubeRadius, Tags::ExpansionOrder,
Tags::MaxIterations, Tags::Charge, Tags::Mass>
tuple_of_opts{shell.create_domain(),
excision_sphere,
excision_sphere.radius(),
expansion_order,
static_cast<size_t>(0),
0.1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "Evolution/Systems/CurvedScalarWave/Worldtube/SingletonActions/InitializeEvolvedVariables.hpp"
#include "Framework/TestingFramework.hpp"
#include "Time/Tags/HistoryEvolvedVariables.hpp"
#include "Time/Tags/Time.hpp"
#include "Time/Tags/TimeStepper.hpp"
#include "Time/TimeSteppers/AdamsBashforth.hpp"
#include "Time/TimeSteppers/TimeStepper.hpp"
Expand All @@ -19,21 +20,30 @@ SPECTRE_TEST_CASE(
tmpl::list<Tags::EvolvedPosition<3>, Tags::EvolvedVelocity<3>>>;
using dt_variables_tag = db::add_tag_prefix<::Tags::dt, variables_tag>;
const tnsr::I<double, 3> initial_pos{{1., 2., 3.}};
const tnsr::I<double, 3, Frame::Grid> initial_excision_pos{{1., 2., 3.}};
const tnsr::I<double, 3> initial_vel{{4., 5., 6.}};
const size_t current_iteration = 77;
const double expiration_time = 1234.;
const double initial_time = 0.;
const double initial_wt_radius = 12345.;
const double wt_radius = 2.;
const ExcisionSphere<3> excision_sphere(wt_radius, initial_excision_pos, {});
auto box =
db::create<db::AddSimpleTags<
variables_tag, dt_variables_tag,
::Tags::HistoryEvolvedVariables<variables_tag>,
::Tags::ConcreteTimeStepper<TimeStepper>,
Tags::InitialPositionAndVelocity, Tags::CurrentIteration>,
Tags::InitialPositionAndVelocity, Tags::CurrentIteration,
Tags::ExpirationTime, Tags::WorldtubeRadius, ::Tags::Time,
Tags::ExcisionSphere<3>>,
time_stepper_ref_tags<TimeStepper>>(
variables_tag::type{}, dt_variables_tag::type{},
TimeSteppers::History<variables_tag::type>{},
static_cast<std::unique_ptr<TimeStepper>>(
std::make_unique<TimeSteppers::AdamsBashforth>(4)),
std::array<tnsr::I<double, 3>, 2>{{initial_pos, initial_vel}},
current_iteration);
current_iteration, expiration_time, initial_wt_radius, initial_time,
excision_sphere);

db::mutate_apply<Initialization::InitializeEvolvedVariables>(
make_not_null(&box));
Expand All @@ -47,6 +57,8 @@ SPECTRE_TEST_CASE(
CHECK(db::get<::Tags::HistoryEvolvedVariables<variables_tag>>(box) ==
TimeSteppers::History<variables_tag::type>(1));
CHECK(get<Tags::CurrentIteration>(box) == 0);
CHECK(get<Tags::ExpirationTime>(box) == initial_time + 1e-10);
CHECK(get<Tags::WorldtubeRadius>(box) == wt_radius);
}
} // namespace
} // namespace CurvedScalarWave::Worldtube
Loading

0 comments on commit c05c009

Please sign in to comment.