Skip to content

Commit

Permalink
Merge pull request sxs-collaboration#5999 from wthrowe/TimeStepRequest
Browse files Browse the repository at this point in the history
Redesign step/slab size changing
  • Loading branch information
nilsdeppe authored Jul 11, 2024
2 parents 58b4e6f + 5061e65 commit bc19774
Show file tree
Hide file tree
Showing 75 changed files with 1,636 additions and 610 deletions.
4 changes: 2 additions & 2 deletions src/Evolution/Executables/Cce/CharacteristicExtractBase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include "NumericalAlgorithms/SpinWeightedSphericalHarmonics/SwshTags.hpp"
#include "Time/StepChoosers/Constant.hpp"
#include "Time/StepChoosers/ErrorControl.hpp"
#include "Time/StepChoosers/Increase.hpp"
#include "Time/StepChoosers/LimitIncrease.hpp"
#include "Utilities/TMPL.hpp"

template <bool EvolveCcm>
Expand Down Expand Up @@ -112,7 +112,7 @@ struct CharacteristicExtractDefaults {
tmpl::list<Cce::Tags::CauchyAngularCoords>>;
using cce_step_choosers =
tmpl::list<StepChoosers::Constant<StepChooserUse::LtsStep>,
StepChoosers::Increase<StepChooserUse::LtsStep>,
StepChoosers::LimitIncrease<StepChooserUse::LtsStep>,
StepChoosers::ErrorControl<StepChooserUse::LtsStep,
Tags::Variables<evolved_swsh_tags>,
swsh_vars_selector>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,8 @@
#include "Time/Actions/UpdateU.hpp"
#include "Time/ChangeSlabSize/Action.hpp"
#include "Time/ChangeSlabSize/Tags.hpp"
#include "Time/StepChoosers/Cfl.hpp"
#include "Time/StepChoosers/Constant.hpp"
#include "Time/StepChoosers/Factory.hpp"
#include "Time/StepChoosers/Increase.hpp"
#include "Time/StepChoosers/PreventRapidIncrease.hpp"
#include "Time/StepChoosers/StepChooser.hpp"
#include "Time/StepChoosers/StepToTimes.hpp"
#include "Time/Tags/StepperErrors.hpp"
#include "Time/Tags/Time.hpp"
#include "Time/Tags/TimeStepId.hpp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,8 @@
#include "Time/Actions/RecordTimeStepperData.hpp"
#include "Time/Actions/SelfStartActions.hpp"
#include "Time/Actions/UpdateU.hpp"
#include "Time/StepChoosers/Cfl.hpp"
#include "Time/StepChoosers/Constant.hpp"
#include "Time/StepChoosers/Factory.hpp"
#include "Time/StepChoosers/Increase.hpp"
#include "Time/StepChoosers/PreventRapidIncrease.hpp"
#include "Time/StepChoosers/StepChooser.hpp"
#include "Time/StepChoosers/StepToTimes.hpp"
#include "Time/Tags/Time.hpp"
#include "Time/TimeSequence.hpp"
#include "Time/TimeSteppers/Factory.hpp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,13 +221,8 @@
#include "Time/Actions/SelfStartActions.hpp" // IWYU pragma: keep
#include "Time/Actions/UpdateU.hpp"
#include "Time/ChangeSlabSize/Action.hpp"
#include "Time/StepChoosers/Cfl.hpp"
#include "Time/StepChoosers/Constant.hpp"
#include "Time/StepChoosers/Factory.hpp"
#include "Time/StepChoosers/Increase.hpp"
#include "Time/StepChoosers/PreventRapidIncrease.hpp"
#include "Time/StepChoosers/StepChooser.hpp"
#include "Time/StepChoosers/StepToTimes.hpp"
#include "Time/Tags/Time.hpp"
#include "Time/Tags/TimeStepId.hpp"
#include "Time/TimeSequence.hpp"
Expand Down
5 changes: 0 additions & 5 deletions src/Evolution/Executables/ScalarTensor/ScalarTensorBase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,8 @@
#include "Time/Actions/RecordTimeStepperData.hpp"
#include "Time/Actions/SelfStartActions.hpp"
#include "Time/Actions/UpdateU.hpp"
#include "Time/StepChoosers/Cfl.hpp"
#include "Time/StepChoosers/Constant.hpp"
#include "Time/StepChoosers/Factory.hpp"
#include "Time/StepChoosers/Increase.hpp"
#include "Time/StepChoosers/PreventRapidIncrease.hpp"
#include "Time/StepChoosers/StepChooser.hpp"
#include "Time/StepChoosers/StepToTimes.hpp"
#include "Time/Tags/Time.hpp"
#include "Time/TimeSequence.hpp"
#include "Time/TimeSteppers/Factory.hpp"
Expand Down
19 changes: 17 additions & 2 deletions src/Evolution/Initialization/Evolution.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "Parallel/Tags/ArrayIndex.hpp"
#include "ParallelAlgorithms/Amr/Protocols/Projector.hpp"
#include "Time/AdaptiveSteppingDiagnostics.hpp"
#include "Time/ChangeSlabSize/Tags.hpp"
#include "Time/ChooseLtsStepSize.hpp"
#include "Time/Slab.hpp"
#include "Time/StepChoosers/StepChooser.hpp"
Expand Down Expand Up @@ -114,7 +115,8 @@ struct TimeStepping {
/// Tags for items in the DataBox that are mutated by the apply function
using return_tags =
tmpl::list<::Tags::Next<::Tags::TimeStepId>, ::Tags::TimeStep,
::Tags::Next<::Tags::TimeStep>>;
::Tags::Next<::Tags::TimeStep>,
::Tags::ChangeSlabSize::SlabSizeGoal>;

/// Tags for mutable DataBox items that are either default initialized or
/// initialized by the apply function
Expand All @@ -130,6 +132,7 @@ struct TimeStepping {
static void apply(const gsl::not_null<TimeStepId*> next_time_step_id,
const gsl::not_null<TimeDelta*> time_step,
const gsl::not_null<TimeDelta*> next_time_step,
const gsl::not_null<double*> slab_size_goal,
const double initial_time_value,
const double initial_dt_value,
const double initial_slab_size,
Expand All @@ -141,13 +144,16 @@ struct TimeStepping {
time_runs_forward, time_stepper);
*time_step = choose_lts_step_size(initial_time, initial_dt_value);
*next_time_step = *time_step;
*slab_size_goal =
time_runs_forward ? initial_slab_size : -initial_slab_size;
}

/// Given the items fetched from a DataBox by the argument_tags, when not
/// using LTS, mutate the items in the DataBox corresponding to return_tags
static void apply(const gsl::not_null<TimeStepId*> next_time_step_id,
const gsl::not_null<TimeDelta*> time_step,
const gsl::not_null<TimeDelta*> next_time_step,
const gsl::not_null<double*> slab_size_goal,
const double initial_time_value,
const double initial_dt_value,
const double initial_slab_size,
Expand All @@ -159,6 +165,8 @@ struct TimeStepping {
time_runs_forward, time_stepper);
*time_step = (time_runs_forward ? 1 : -1) * initial_time.slab().duration();
*next_time_step = *time_step;
*slab_size_goal =
time_runs_forward ? initial_slab_size : -initial_slab_size;
}
};

Expand All @@ -168,7 +176,8 @@ struct ProjectTimeStepping : tt::ConformsTo<amr::protocols::Projector> {
using return_tags =
tmpl::list<::Tags::TimeStepId, ::Tags::Next<::Tags::TimeStepId>,
::Tags::TimeStep, ::Tags::Next<::Tags::TimeStep>, ::Tags::Time,
::Tags::AdaptiveSteppingDiagnostics>;
::Tags::AdaptiveSteppingDiagnostics,
::Tags::ChangeSlabSize::SlabSizeGoal>;
using argument_tags = tmpl::list<Parallel::Tags::ArrayIndex>;

static void apply(
Expand All @@ -179,6 +188,7 @@ struct ProjectTimeStepping : tt::ConformsTo<amr::protocols::Projector> {
const gsl::not_null<double*> /*time*/,
const gsl::not_null<AdaptiveSteppingDiagnostics*>
/*adaptive_stepping_diagnostics*/,
const gsl::not_null<double*> /*slab_size_goal*/,
const ElementId<Dim>& /*element_id*/,
const std::pair<Mesh<Dim>, Element<Dim>>& /*old_mesh_and_element*/) {
// Do not change anything for p-refinement
Expand All @@ -192,13 +202,15 @@ struct ProjectTimeStepping : tt::ConformsTo<amr::protocols::Projector> {
const gsl::not_null<double*> time,
const gsl::not_null<AdaptiveSteppingDiagnostics*>
adaptive_stepping_diagnostics,
const gsl::not_null<double*> slab_size_goal,
const ElementId<Dim>& element_id,
const tuples::TaggedTuple<Tags...>& parent_items) {
*time_step_id = get<::Tags::TimeStepId>(parent_items);
*next_time_step_id = get<::Tags::Next<::Tags::TimeStepId>>(parent_items);
*time_step = get<::Tags::TimeStep>(parent_items);
*next_time_step = get<::Tags::Next<::Tags::TimeStep>>(parent_items);
*time = get<::Tags::Time>(parent_items);
*slab_size_goal = get<::Tags::ChangeSlabSize::SlabSizeGoal>(parent_items);

// Since AdaptiveSteppingDiagnostics are reduced over all elements, we
// set the slab quantities to the same value over all children, and the
Expand Down Expand Up @@ -229,6 +241,7 @@ struct ProjectTimeStepping : tt::ConformsTo<amr::protocols::Projector> {
const gsl::not_null<double*> time,
const gsl::not_null<AdaptiveSteppingDiagnostics*>
adaptive_stepping_diagnostics,
const gsl::not_null<double*> slab_size_goal,
const ElementId<Dim>& /*element_id*/,
const std::unordered_map<ElementId<Dim>, tuples::TaggedTuple<Tags...>>&
children_items) {
Expand All @@ -248,6 +261,8 @@ struct ProjectTimeStepping : tt::ConformsTo<amr::protocols::Projector> {
*time_step = get<::Tags::TimeStep>(slowest_child_items);
*next_time_step = get<::Tags::Next<::Tags::TimeStep>>(slowest_child_items);
*time = get<::Tags::Time>(slowest_child_items);
*slab_size_goal =
get<::Tags::ChangeSlabSize::SlabSizeGoal>(slowest_child_items);
const auto& slowest_child_diagnostics =
get<::Tags::AdaptiveSteppingDiagnostics>(slowest_child_items);

Expand Down
42 changes: 23 additions & 19 deletions src/Time/Actions/ChangeStepSize.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "Time/ChooseLtsStepSize.hpp"
#include "Time/Tags/AdaptiveSteppingDiagnostics.hpp"
#include "Time/Tags/HistoryEvolvedVariables.hpp"
#include "Time/TimeStepRequestProcessor.hpp"
#include "Time/TimeSteppers/LtsTimeStepper.hpp"
#include "Utilities/ErrorHandling/Assert.hpp"
#include "Utilities/Gsl.hpp"
Expand Down Expand Up @@ -61,6 +62,20 @@ bool change_step_size(const gsl::not_null<db::DataBox<DbTags>*> box) {

const auto& time_step_id = db::get<Tags::TimeStepId>(*box);
ASSERT(time_step_id.substep() == 0, "Can't change step size on a substep.");

const auto& current_step = db::get<Tags::TimeStep>(*box);
const double last_step_size = current_step.value();

TimeStepRequestProcessor step_requests(time_step_id.time_runs_forward());
bool step_accepted = true;
for (const auto& step_chooser : step_choosers) {
const auto [step_request, step_choice_accepted] =
step_chooser->template desired_step<StepChoosersToUse>(last_step_size,
*box);
step_requests.process(step_request);
step_accepted = step_accepted and step_choice_accepted;
}

using history_tags = ::Tags::get_all_history_tags<DbTags>;
bool can_change_step_size = true;
tmpl::for_each<history_tags>([&box, &can_change_step_size, &time_stepper,
Expand All @@ -74,28 +89,14 @@ bool change_step_size(const gsl::not_null<db::DataBox<DbTags>*> box) {
time_stepper.can_change_step_size(time_step_id, history);
});
if (not can_change_step_size) {
step_requests.error_on_hard_limit(
current_step.value(),
(time_step_id.step_time() + current_step).value());
return true;
}

const auto& current_step = db::get<Tags::TimeStep>(*box);

const double last_step_size = std::abs(db::get<Tags::TimeStep>(*box).value());

// The step choosers return the magnitude of the desired step, so
// we always want the minimum requirement, but we have to negate
// the final answer if time is running backwards.
double desired_step = std::numeric_limits<double>::infinity();
bool step_accepted = true;
for (const auto& step_chooser : step_choosers) {
const auto [step_choice, step_choice_accepted] =
step_chooser->template desired_step<StepChoosersToUse>(
last_step_size, *box);
desired_step = std::min(desired_step, step_choice);
step_accepted = step_accepted and step_choice_accepted;
}
if (not current_step.is_positive()) {
desired_step = -desired_step;
}
const double desired_step = step_requests.step_size(
time_step_id.step_time().value(), current_step.value());

constexpr double smallest_relative_step_size = 1.0e-9;
if (abs(desired_step / current_step.slab().duration().value()) <
Expand Down Expand Up @@ -124,6 +125,9 @@ bool change_step_size(const gsl::not_null<db::DataBox<DbTags>*> box) {
// if step accepted, just proceed. Otherwise, change Time::Next and jump
// back to the first instance of `UpdateU`.
if (step_accepted) {
step_requests.error_on_hard_limit(
current_step.value(),
(time_step_id.step_time() + current_step).value());
return true;
} else {
db::mutate<Tags::Next<Tags::TimeStepId>, Tags::TimeStep>(
Expand Down
4 changes: 4 additions & 0 deletions src/Time/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ spectre_target_sources(
Time.cpp
TimeSequence.cpp
TimeStepId.cpp
TimeStepRequest.cpp
TimeStepRequestProcessor.cpp
Utilities.cpp
)

Expand All @@ -42,6 +44,8 @@ spectre_target_headers(
Time.hpp
TimeSequence.hpp
TimeStepId.hpp
TimeStepRequest.hpp
TimeStepRequestProcessor.hpp
Utilities.hpp
)

Expand Down
Loading

0 comments on commit bc19774

Please sign in to comment.