Skip to content

Commit

Permalink
Address LK comments in code review
Browse files Browse the repository at this point in the history
  • Loading branch information
Francois Foucart committed Jan 27, 2025
1 parent 1c22afe commit 715ddc1
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
#include "Evolution/Initialization/Limiter.hpp"
#include "Evolution/Initialization/SetVariables.hpp"
#include "Evolution/Particles/MonteCarlo/Actions/InitializeMonteCarlo.hpp"
#include "Evolution/Particles/MonteCarlo/Actions/Labels.hpp"
#include "Evolution/Particles/MonteCarlo/Actions/TimeStepActions.hpp"
#include "Evolution/Particles/MonteCarlo/Actions/TriggerMonteCarloEvolution.hpp"
#include "Evolution/Particles/MonteCarlo/GhostZoneCommunication.hpp"
#include "Evolution/Particles/MonteCarlo/GhostZoneCommunicationTags.hpp"
#include "Evolution/Particles/MonteCarlo/System.hpp"
Expand Down Expand Up @@ -173,8 +175,7 @@ struct EvolutionMetavars {
using initialization_actions = tmpl::list<
Initialization::Actions::InitializeItems<
Initialization::TimeStepping<EvolutionMetavars, TimeStepperBase>,
evolution::dg::Initialization::Domain<volume_dim> //,
>,
evolution::dg::Initialization::Domain<volume_dim>>,
Initialization::Actions::AddSimpleTags<
evolution::dg::BackgroundGrVars<system, EvolutionMetavars, true>>,
evolution::dg::subcell::Actions::SetSubcellGrid<volume_dim, system,
Expand Down Expand Up @@ -202,10 +203,11 @@ struct EvolutionMetavars {
tmpl::list<
Actions::AdvanceTime,
evolution::Actions::RunEventsAndTriggers<false>,
Actions::AdvanceTime,
evolution::Actions::RunEventsAndTriggers<false>,
Actions::AdvanceTime,
evolution::Actions::RunEventsAndTriggers<false>,
// Monte-Carlo is only triggered at the end of a full time
// step
Particles::MonteCarlo::Actions::TriggerMonteCarloEvolution,
Actions::Label<
Particles::MonteCarlo::Actions::Labels::BeginMonteCarlo>,
Particles::MonteCarlo::Actions::SendDataForMcCommunication<
volume_dim,
// No local time stepping
Expand All @@ -223,6 +225,8 @@ struct EvolutionMetavars {
Particles::MonteCarlo::Actions::ReceiveDataForMcCommunication<
volume_dim,
Particles::MonteCarlo::CommunicationStep::PostStep>,
Actions::Label<
Particles::MonteCarlo::Actions::Labels::EndMonteCarlo>,
PhaseControl::Actions::ExecutePhaseChange>>>>;

struct registration
Expand Down
2 changes: 2 additions & 0 deletions src/Evolution/Particles/MonteCarlo/Actions/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ spectre_target_headers(
INCLUDE_DIRECTORY ${CMAKE_SOURCE_DIR}/src
HEADERS
InitializeMonteCarlo.hpp
Labels.hpp
TimeStepActions.hpp
TriggerMonteCarloEvolution.hpp
)
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include "DataStructures/DataBox/DataBox.hpp"
#include "Domain/Structure/Element.hpp"
#include "Domain/Tags.hpp"
#include "Evolution/DgSubcell/ActiveGrid.hpp"
#include "Evolution/DgSubcell/Tags/ActiveGrid.hpp"
#include "Evolution/DgSubcell/Tags/Coordinates.hpp"
#include "Evolution/DgSubcell/Tags/Mesh.hpp"
#include "Evolution/Initialization/InitialData.hpp"
Expand Down Expand Up @@ -76,7 +78,8 @@ struct InitializeMCTags {
Particles::MonteCarlo::Tags::CellLightCrossingTime<DataVector>,
hydro_variables_tag,
Particles::MonteCarlo::Tags::MortarDataTag<dim>,
Particles::MonteCarlo::Tags::McGhostZoneDataTag<dim>>;
Particles::MonteCarlo::Tags::McGhostZoneDataTag<dim>,
evolution::dg::subcell::Tags::ActiveGrid>;

using compute_tags = tmpl::list<>;

Expand All @@ -89,6 +92,10 @@ struct InitializeMCTags {
const Parallel::GlobalCache<Metavariables>& /*cache*/,
const ArrayIndex& /*array_index*/, ActionList /*meta*/,
const ParallelComponent* const /*meta*/) {
if (db::get<evolution::dg::subcell::Tags::ActiveGrid>(box) !=
evolution::dg::subcell::ActiveGrid::Subcell) {
ERROR("MC requires all elements to use Subcell");
}
const size_t num_grid_points =
db::get<evolution::dg::subcell::Tags::Mesh<dim>>(box)
.number_of_grid_points();
Expand All @@ -98,12 +105,9 @@ struct InitializeMCTags {
using HydroVars = typename hydro_variables_tag::type;
call_with_dynamic_type<void, derived_classes>(
&db::get<evolution::initial_data::Tags::InitialData>(box),
[&box](const auto* const data_or_solution) {
[&box, &num_grid_points](const auto* const data_or_solution) {
static constexpr size_t dim = System::volume_dim;
const double initial_time = db::get<::Tags::Time>(box);
const size_t num_grid_points =
db::get<evolution::dg::subcell::Tags::Mesh<dim>>(box)
.number_of_grid_points();
const auto& inertial_coords = db::get<
evolution::dg::subcell::Tags::Coordinates<dim, Frame::Inertial>>(
box);
Expand All @@ -121,8 +125,7 @@ struct InitializeMCTags {
tmpl::list<Particles::MonteCarlo::Tags::PacketsOnElement>>(
make_not_null(&box), std::move(all_packets));

const unsigned long seed =
std::random_device{}(); // static_cast<unsigned long>(time(NULL));
const unsigned long seed = std::random_device{}();
typename Particles::MonteCarlo::Tags::RandomNumberGenerator::type rng(seed);

Initialization::mutate_assign<
Expand Down
12 changes: 12 additions & 0 deletions src/Evolution/Particles/MonteCarlo/Actions/Labels.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Distributed under the MIT License.
// See LICENSE.txt for details.

#pragma once

/// Labels used to navigate the action list when using MC
namespace Particles::MonteCarlo::Actions::Labels {
/// Beginning of the MC algorithm
struct BeginMonteCarlo {};
/// End of the MC algorithm
struct EndMonteCarlo {};
} // namespace Particles::MonteCarlo::Actions::Labels
16 changes: 3 additions & 13 deletions src/Evolution/Particles/MonteCarlo/Actions/TimeStepActions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "Evolution/Particles/MonteCarlo/TemplatedLocalFunctions.hpp"
#include "Parallel/AlgorithmExecution.hpp"
#include "Parallel/GlobalCache.hpp"
#include "PointwiseFunctions/GeneralRelativity/DerivativeSpatialMetric.hpp"
#include "PointwiseFunctions/GeneralRelativity/Tags.hpp"
#include "PointwiseFunctions/Hydro/EquationsOfState/EquationOfState.hpp"
#include "PointwiseFunctions/Hydro/Tags.hpp"
Expand Down Expand Up @@ -134,19 +135,8 @@ struct TimeStepMutator {

tnsr::iJJ<DataVector, 3, Frame::Inertial> d_inv_spatial_metric =
make_with_value<tnsr::iJJ<DataVector, 3, Frame::Inertial>>(lapse, 0.0);
for (size_t i = 0; i < 3; i++) {
for (size_t j = i; j < 3; j++) {
for (size_t k = 0; k < 3; k++) {
for (size_t l = 0; l < 3; l++) {
for (size_t m = 0; m < 3; m++) {
d_inv_spatial_metric.get(k, i, j) -=
inv_spatial_metric.get(i, l) * inv_spatial_metric.get(j, m) *
d_spatial_metric.get(k, l, m);
}
}
}
}
}
gr::deriv_inverse_spatial_metric(make_not_null(&d_inv_spatial_metric),
inv_spatial_metric, d_spatial_metric);

TemplatedLocalFunctions<EnergyBins, NeutrinoSpecies> templated_functions;
templated_functions.take_time_step_on_element(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Distributed under the MIT License.
// See LICENSE.txt for details.

#pragma once

#include <cstddef>
#include <optional>
#include <tuple>
#include <utility>

#include "DataStructures/DataBox/DataBox.hpp"
#include "Evolution/Particles/MonteCarlo/Actions/Labels.hpp"
#include "Parallel/AlgorithmExecution.hpp"
#include "ParallelAlgorithms/Actions/Goto.hpp"
#include "Time/Tags/TimeStepId.hpp"
#include "Time/Time.hpp"
#include "Time/TimeStepId.hpp"
#include "Utilities/TMPL.hpp"

/// \cond
namespace Parallel {
template <typename Metavariables>
class GlobalCache;
} // namespace Parallel
namespace tuples {
template <typename...>
class TaggedTuple;
} // namespace tuples
/// \endcond

namespace Particles::MonteCarlo::Actions {
/*!
* \brief Goes to `Labels::BeginMonteCarlo` or `Labels::EndMonteCarlo` depending
* on whether we are at the end of a full time step or at an intermediate step
* of the timestepping algorithm.
*
* GlobalCache: nothing
*
* DataBox:
* - Uses:
* -
*/
struct TriggerMonteCarloEvolution {
template <typename DbTagsList, typename... InboxTags, typename Metavariables,
typename ArrayIndex, typename ActionList,
typename ParallelComponent>
static Parallel::iterable_action_return_t apply(
db::DataBox<DbTagsList>& box,
const tuples::TaggedTuple<InboxTags...>& /*inboxes*/,
const Parallel::GlobalCache<Metavariables>& /*cache*/,
const ArrayIndex& /*array_index*/, ActionList /*meta*/,
const ParallelComponent* const /*meta*/) {
const auto& next_time_id = db::get<::Tags::Next<::Tags::TimeStepId>>(box);
// We only run MC if we are at the beginning of a full time step
const bool trigger_mc = (next_time_id.substep() == 0);
// Note: we jump to the `Label+1` because the label actions don't do
// anything anyway
if (trigger_mc) {
const size_t mc_index =
tmpl::index_of<ActionList,
::Actions::Label<Labels::BeginMonteCarlo>>::value +
1;
return {Parallel::AlgorithmExecution::Continue, mc_index};
} else {
const size_t post_mc_index =
tmpl::index_of<ActionList,
::Actions::Label<Labels::EndMonteCarlo>>::value +
1;
return {Parallel::AlgorithmExecution::Continue, post_mc_index};
}
}
};
} // namespace Particles::MonteCarlo::Actions
4 changes: 0 additions & 4 deletions src/Evolution/Particles/MonteCarlo/GhostZoneCommunication.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -321,8 +321,6 @@ struct ReceiveDataForMcCommunication {
const DataVector& received_data_direction =
received_data[directional_element_id]
.ghost_zone_hydro_variables;
// REQUIRE(received_data[directional_element_id]
// .packets_entering_this_element == std::nullopt);
if (mortar_data->rest_mass_density[directional_element_id] ==
std::nullopt) {
continue;
Expand Down Expand Up @@ -378,8 +376,6 @@ struct ReceiveDataForMcCommunication {
received_data[directional_element_id]
.packets_entering_this_element;
// Temporary: currently no data for coupling to the fluid
// REQUIRE(received_data[directional_element_id]
// .ghost_zone_hydro_variables.size() == 0);
if (received_data_packets == std::nullopt) {
continue;
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/Evolution/Particles/MonteCarlo/System.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ struct System {
using spacetime_variables_tag =
::Tags::Variables<gr::tags_for_hydro<volume_dim, DataVector>>;
using flux_spacetime_variables_tag = ::Tags::Variables<tmpl::list<>>;
// Hydro tags needed for background metric
// Hydro tags needed for background fluid
using hydro_variables_tag = ::Tags::Variables<hydro::grmhd_tags<DataVector>>;
using primitive_variables_tag = hydro_variables_tag;

Expand Down

0 comments on commit 715ddc1

Please sign in to comment.