SpECTRE Documentation Coverage Report
Current view: top level - Evolution/Systems/GrMhd/GhValenciaDivClean/Actions - SetInitialData.hpp Hit Total Coverage
Commit: 1f2210958b4f38fdc0400907ee7c6d5af5111418 Lines: 4 39 10.3 %
Date: 2025-12-05 05:03:31
Legend: Lines: hit not hit

          Line data    Source code
       1           0 : // Distributed under the MIT License.
       2             : // See LICENSE.txt for details.
       3             : 
       4             : #pragma once
       5             : 
       6             : #include <cstddef>
       7             : #include <optional>
       8             : #include <string>
       9             : #include <variant>
      10             : 
      11             : #include "DataStructures/DataBox/DataBox.hpp"
      12             : #include "DataStructures/DataVector.hpp"
      13             : #include "DataStructures/Tensor/EagerMath/DeterminantAndInverse.hpp"
      14             : #include "DataStructures/Tensor/Tensor.hpp"
      15             : #include "Domain/Structure/ElementId.hpp"
      16             : #include "Evolution/DgSubcell/ActiveGrid.hpp"
      17             : #include "Evolution/DgSubcell/Tags/ActiveGrid.hpp"
      18             : #include "Evolution/DgSubcell/Tags/Coordinates.hpp"
      19             : #include "Evolution/DgSubcell/Tags/Jacobians.hpp"
      20             : #include "Evolution/DgSubcell/Tags/Mesh.hpp"
      21             : #include "Evolution/NumericInitialData.hpp"
      22             : #include "Evolution/Systems/GeneralizedHarmonic/Actions/SetInitialData.hpp"
      23             : #include "Evolution/Systems/GeneralizedHarmonic/GaugeSourceFunctions/SetPiAndPhiFromConstraints.hpp"
      24             : #include "Evolution/Systems/GeneralizedHarmonic/Tags.hpp"
      25             : #include "Evolution/Systems/GrMhd/ValenciaDivClean/Actions/NumericInitialData.hpp"
      26             : #include "IO/Importers/Actions/ReadVolumeData.hpp"
      27             : #include "IO/Importers/ElementDataReader.hpp"
      28             : #include "IO/Importers/Tags.hpp"
      29             : #include "NumericalAlgorithms/Spectral/Mesh.hpp"
      30             : #include "Options/String.hpp"
      31             : #include "Parallel/AlgorithmExecution.hpp"
      32             : #include "Parallel/GlobalCache.hpp"
      33             : #include "Parallel/Invoke.hpp"
      34             : #include "PointwiseFunctions/GeneralRelativity/SpatialMetric.hpp"
      35             : #include "PointwiseFunctions/GeneralRelativity/Tags.hpp"
      36             : #include "PointwiseFunctions/Hydro/EquationsOfState/EquationOfState.hpp"
      37             : #include "PointwiseFunctions/Hydro/Tags.hpp"
      38             : #include "PointwiseFunctions/InitialDataUtilities/InitialData.hpp"
      39             : #include "PointwiseFunctions/InitialDataUtilities/Tags/InitialData.hpp"
      40             : #include "Utilities/ErrorHandling/Error.hpp"
      41             : #include "Utilities/Gsl.hpp"
      42             : #include "Utilities/TMPL.hpp"
      43             : #include "Utilities/TaggedTuple.hpp"
      44             : 
      45             : /// \cond
      46             : namespace Tags {
      47             : struct Time;
      48             : }  // namespace Tags
      49             : /// \endcond
      50             : 
      51           1 : namespace grmhd::GhValenciaDivClean {
      52             : 
      53             : /*!
      54             :  * \brief Numeric initial data loaded from volume data files
      55             :  *
      56             :  * This class can be factory-created in the input file to start an evolution
      57             :  * from numeric initial data. It selects the set of GH variables to load from
      58             :  * the volume data file (ADM or GH variables), the set of hydro variables, and
      59             :  * allows to set constant values for some of the hydro variables. This class
      60             :  * mostly combines the `gh::NumericInitialData` and
      61             :  * `grmhd::ValenciaDivClean::NumericInitialData` classes.
      62             :  */
      63           1 : class NumericInitialData : public evolution::initial_data::InitialData,
      64             :                            public evolution::NumericInitialData {
      65             :  private:
      66           0 :   using GhNumericId = gh::NumericInitialData;
      67           0 :   using HydroNumericId = grmhd::ValenciaDivClean::NumericInitialData;
      68             : 
      69             :  public:
      70           0 :   using all_vars =
      71             :       tmpl::append<GhNumericId::all_vars, HydroNumericId::all_vars>;
      72             : 
      73           0 :   struct GhVariables : GhNumericId::Variables {};
      74           0 :   struct HydroVariables : HydroNumericId::Variables {};
      75             : 
      76           0 :   using options =
      77             :       tmpl::push_back<importers::ImporterOptions::tags_list, GhVariables,
      78             :                       HydroVariables, HydroNumericId::DensityCutoff>;
      79             : 
      80           0 :   static constexpr Options::String help =
      81             :       "Numeric initial data loaded from volume data files";
      82             : 
      83           0 :   NumericInitialData() = default;
      84           0 :   NumericInitialData(const NumericInitialData& rhs) = default;
      85           0 :   NumericInitialData& operator=(const NumericInitialData& rhs) = default;
      86           0 :   NumericInitialData(NumericInitialData&& /*rhs*/) = default;
      87           0 :   NumericInitialData& operator=(NumericInitialData&& /*rhs*/) = default;
      88           0 :   ~NumericInitialData() = default;
      89             : 
      90             :   /// \cond
      91             :   explicit NumericInitialData(CkMigrateMessage* msg);
      92             :   using PUP::able::register_constructor;
      93             :   WRAPPED_PUPable_decl_template(NumericInitialData);
      94             :   /// \endcond
      95             : 
      96           0 :   std::unique_ptr<evolution::initial_data::InitialData> get_clone()
      97             :       const override {
      98             :     return std::make_unique<NumericInitialData>(*this);
      99             :   }
     100             : 
     101           0 :   NumericInitialData(
     102             :       std::string file_glob, std::string subfile_name,
     103             :       std::variant<double, importers::ObservationSelector> observation_value,
     104             :       std::optional<double> observation_value_epsilon,
     105             :       bool enable_interpolation,
     106             :       typename GhNumericId::Variables::type gh_selected_variables,
     107             :       typename HydroNumericId::Variables::type hydro_selected_variables,
     108             :       double density_cutoff);
     109             : 
     110           0 :   const importers::ImporterOptions& importer_options() const {
     111             :     return gh_numeric_id_.importer_options();
     112             :   }
     113             : 
     114           0 :   const GhNumericId& gh_numeric_id() const { return gh_numeric_id_; }
     115             : 
     116           0 :   const HydroNumericId& hydro_numeric_id() const { return hydro_numeric_id_; }
     117             : 
     118           0 :   size_t volume_data_id() const;
     119             : 
     120             :   template <typename... AllTags>
     121           0 :   void select_for_import(
     122             :       const gsl::not_null<tuples::TaggedTuple<AllTags...>*> fields) const {
     123             :     gh_numeric_id_.select_for_import(fields);
     124             :     hydro_numeric_id_.select_for_import(fields);
     125             :   }
     126             : 
     127             :   template <typename... AllTags, size_t ThermodynamicDim>
     128           0 :   void set_initial_data(
     129             :       const gsl::not_null<tnsr::aa<DataVector, 3>*> spacetime_metric,
     130             :       const gsl::not_null<tnsr::aa<DataVector, 3>*> pi,
     131             :       const gsl::not_null<tnsr::iaa<DataVector, 3>*> phi,
     132             :       const gsl::not_null<Scalar<DataVector>*> rest_mass_density,
     133             :       const gsl::not_null<Scalar<DataVector>*> electron_fraction,
     134             :       const gsl::not_null<Scalar<DataVector>*> specific_internal_energy,
     135             :       const gsl::not_null<tnsr::I<DataVector, 3>*> spatial_velocity,
     136             :       const gsl::not_null<tnsr::I<DataVector, 3>*> magnetic_field,
     137             :       const gsl::not_null<Scalar<DataVector>*> div_cleaning_field,
     138             :       const gsl::not_null<Scalar<DataVector>*> lorentz_factor,
     139             :       const gsl::not_null<Scalar<DataVector>*> pressure,
     140             :       const gsl::not_null<Scalar<DataVector>*> temperature,
     141             :       const gsl::not_null<tuples::TaggedTuple<AllTags...>*> numeric_data,
     142             :       const Mesh<3>& mesh,
     143             :       const InverseJacobian<DataVector, 3, Frame::ElementLogical,
     144             :                             Frame::Inertial>& inv_jacobian,
     145             :       const EquationsOfState::EquationOfState<true, ThermodynamicDim>&
     146             :           equation_of_state) const {
     147             :     gh_numeric_id_.set_initial_data(spacetime_metric, pi, phi, numeric_data,
     148             :                                     mesh, inv_jacobian);
     149             :     const auto spatial_metric = gr::spatial_metric(*spacetime_metric);
     150             :     const auto inv_spatial_metric =
     151             :         determinant_and_inverse(spatial_metric).second;
     152             :     hydro_numeric_id_.set_initial_data(
     153             :         rest_mass_density, electron_fraction, specific_internal_energy,
     154             :         spatial_velocity, magnetic_field, div_cleaning_field, lorentz_factor,
     155             :         pressure, temperature, numeric_data, inv_spatial_metric,
     156             :         equation_of_state);
     157             :   }
     158             : 
     159           0 :   void pup(PUP::er& p) override;
     160             : 
     161           0 :   friend bool operator==(const NumericInitialData& lhs,
     162             :                          const NumericInitialData& rhs);
     163             : 
     164             :  private:
     165           0 :   GhNumericId gh_numeric_id_{};
     166           0 :   HydroNumericId hydro_numeric_id_{};
     167             : };
     168             : 
     169           0 : namespace Actions {
     170             : 
     171             : /*!
     172             :  * \brief Dispatch loading numeric initial data from files.
     173             :  *
     174             :  * Place this action before
     175             :  * grmhd::GhValenciaDivClean::Actions::SetNumericInitialData in the action list.
     176             :  * See importers::Actions::ReadAllVolumeDataAndDistribute for details, which is
     177             :  * invoked by this action.
     178             :  */
     179           1 : struct SetInitialData {
     180           0 :   using const_global_cache_tags =
     181             :       tmpl::list<evolution::initial_data::Tags::InitialData>;
     182             : 
     183             :   template <typename DbTagsList, typename... InboxTags, typename Metavariables,
     184             :             typename ArrayIndex, typename ActionList,
     185             :             typename ParallelComponent>
     186           0 :   static Parallel::iterable_action_return_t apply(
     187             :       db::DataBox<DbTagsList>& box,
     188             :       const tuples::TaggedTuple<InboxTags...>& /*inboxes*/,
     189             :       Parallel::GlobalCache<Metavariables>& cache,
     190             :       const ArrayIndex& array_index, const ActionList /*meta*/,
     191             :       const ParallelComponent* const parallel_component) {
     192             :     // Dispatch to the correct `apply` overload based on type of initial data
     193             :     using initial_data_classes =
     194             :         tmpl::at<typename Metavariables::factory_creation::factory_classes,
     195             :                  evolution::initial_data::InitialData>;
     196             :     return call_with_dynamic_type<Parallel::iterable_action_return_t,
     197             :                                   initial_data_classes>(
     198             :         &db::get<evolution::initial_data::Tags::InitialData>(box),
     199             :         [&box, &cache, &array_index,
     200             :          &parallel_component](const auto* const initial_data) {
     201             :           return apply(make_not_null(&box), *initial_data, cache, array_index,
     202             :                        parallel_component);
     203             :         });
     204             :   }
     205             : 
     206             :  private:
     207           0 :   static constexpr size_t Dim = 3;
     208             : 
     209             :   // Numeric initial data
     210             :   template <typename DbTagsList, typename Metavariables, typename ArrayIndex,
     211             :             typename ParallelComponent>
     212           0 :   static Parallel::iterable_action_return_t apply(
     213             :       const gsl::not_null<db::DataBox<DbTagsList>*> /*box*/,
     214             :       const NumericInitialData& initial_data,
     215             :       Parallel::GlobalCache<Metavariables>& cache,
     216             :       const ArrayIndex& array_index, const ParallelComponent* const /*meta*/) {
     217             :     // If we are using GH Numeric ID, then we don't have to set Pi and Phi since
     218             :     // we are reading them in. Also we only need to mutate this tag once so do
     219             :     // it on the first element.
     220             :     if (is_zeroth_element(array_index) and
     221             :         std::holds_alternative<gh::NumericInitialData::GhVars>(
     222             :             initial_data.gh_numeric_id().selected_variables())) {
     223             :       Parallel::mutate<gh::Tags::SetPiAndPhiFromConstraints,
     224             :                        gh::gauges::SetPiAndPhiFromConstraintsCacheMutator>(
     225             :           cache, false);
     226             :     }
     227             :     // Select the subset of the available variables that we want to read from
     228             :     // the volume data file
     229             :     tuples::tagged_tuple_from_typelist<db::wrap_tags_in<
     230             :         importers::Tags::Selected, NumericInitialData::all_vars>>
     231             :         selected_fields{};
     232             :     initial_data.select_for_import(make_not_null(&selected_fields));
     233             :     // Dispatch loading the variables from the volume data file
     234             :     // - Not using `ckLocalBranch` here to make sure the simple action
     235             :     //   invocation is asynchronous.
     236             :     auto& reader_component = Parallel::get_parallel_component<
     237             :         importers::ElementDataReader<Metavariables>>(cache);
     238             :     Parallel::simple_action<importers::Actions::ReadAllVolumeDataAndDistribute<
     239             :         3, NumericInitialData::all_vars, ParallelComponent>>(
     240             :         reader_component, initial_data.importer_options(),
     241             :         initial_data.volume_data_id(), std::move(selected_fields));
     242             :     return {Parallel::AlgorithmExecution::Continue, std::nullopt};
     243             :   }
     244             : 
     245             :   // "AnalyticData"-type initial data
     246             :   template <typename DbTagsList, typename InitialData, typename Metavariables,
     247             :             typename ArrayIndex, typename ParallelComponent>
     248           0 :   static Parallel::iterable_action_return_t apply(
     249             :       const gsl::not_null<db::DataBox<DbTagsList>*> box,
     250             :       const InitialData& initial_data,
     251             :       Parallel::GlobalCache<Metavariables>& /*cache*/,
     252             :       const ArrayIndex& /*array_index*/,
     253             :       const ParallelComponent* const /*meta*/) {
     254             :     // Get ADM + hydro variables from analytic data / solution
     255             :     const auto& [coords, mesh, inv_jacobian] = [&box]() {
     256             :       if constexpr (db::tag_is_retrievable_v<
     257             :                         evolution::dg::subcell::Tags::ActiveGrid,
     258             :                         db::DataBox<DbTagsList>>) {
     259             :         const bool on_subcell =
     260             :             db::get<evolution::dg::subcell::Tags::ActiveGrid>(*box) ==
     261             :             evolution::dg::subcell::ActiveGrid::Subcell;
     262             :         if (on_subcell) {
     263             :           return std::forward_as_tuple(
     264             :               db::get<evolution::dg::subcell::Tags::Coordinates<
     265             :                   Dim, Frame::Inertial>>(*box),
     266             :               db::get<evolution::dg::subcell::Tags::Mesh<Dim>>(*box),
     267             :               db::get<evolution::dg::subcell::fd::Tags::
     268             :                           InverseJacobianLogicalToInertial<Dim>>(*box));
     269             :         }
     270             :       }
     271             :       return std::forward_as_tuple(
     272             :           db::get<domain::Tags::Coordinates<Dim, Frame::Inertial>>(*box),
     273             :           db::get<domain::Tags::Mesh<Dim>>(*box),
     274             :           db::get<domain::Tags::InverseJacobian<Dim, Frame::ElementLogical,
     275             :                                                 Frame::Inertial>>(*box));
     276             :     }();
     277             :     auto vars = evolution::Initialization::initial_data(
     278             :         initial_data, coords, db::get<::Tags::Time>(*box),
     279             :         tmpl::append<tmpl::list<gr::Tags::SpatialMetric<DataVector, 3>,
     280             :                                 gr::Tags::Lapse<DataVector>,
     281             :                                 gr::Tags::Shift<DataVector, 3>,
     282             :                                 gr::Tags::ExtrinsicCurvature<DataVector, 3>>,
     283             :                      hydro::grmhd_tags<DataVector>>{});
     284             :     const auto& spatial_metric =
     285             :         get<gr::Tags::SpatialMetric<DataVector, 3>>(vars);
     286             :     const auto& lapse = get<gr::Tags::Lapse<DataVector>>(vars);
     287             :     const auto& shift = get<gr::Tags::Shift<DataVector, 3>>(vars);
     288             :     const auto& extrinsic_curvature =
     289             :         get<gr::Tags::ExtrinsicCurvature<DataVector, 3>>(vars);
     290             : 
     291             :     // Compute GH vars from ADM vars
     292             :     db::mutate<gr::Tags::SpacetimeMetric<DataVector, 3>,
     293             :                gh::Tags::Pi<DataVector, 3>, gh::Tags::Phi<DataVector, 3>>(
     294             :         &gh::initial_gh_variables_from_adm<3>, box, spatial_metric, lapse,
     295             :         shift, extrinsic_curvature, mesh, inv_jacobian);
     296             : 
     297             :     // Move hydro vars directly into the DataBox
     298             :     tmpl::for_each<hydro::grmhd_tags<DataVector>>(
     299             :         [&box, &vars](const auto tag_v) {
     300             :           using tag = tmpl::type_from<std::decay_t<decltype(tag_v)>>;
     301             :           Initialization::mutate_assign<tmpl::list<tag>>(
     302             :               box, std::move(get<tag>(vars)));
     303             :         });
     304             : 
     305             :     // No need to import numeric initial data, so we terminate the phase by
     306             :     // pausing the algorithm on this element
     307             :     return {Parallel::AlgorithmExecution::Pause, std::nullopt};
     308             :   }
     309             : };
     310             : 
     311             : /*!
     312             :  * \brief Receive numeric initial data loaded by
     313             :  * grmhd::GhValenciaDivClean::Actions::SetInitialData.
     314             :  *
     315             :  * Place this action in the action list after
     316             :  * grmhd::GhValenciaDivClean::Actions::SetInitialData to wait until the
     317             :  * data for this element has arrived, and then compute the GH variables and the
     318             :  * remaining primitive variables and store them in the DataBox to be used as
     319             :  * initial data.
     320             :  *
     321             :  * This action modifies the GH system tags (spacetime metric, pi, phi) and the
     322             :  * tags listed in `hydro::grmhd_tags` in the DataBox (i.e., the hydro
     323             :  * primitives). It does not modify conservative variables, so it relies on a
     324             :  * primitive-to-conservative update in the action list before the evolution can
     325             :  * start.
     326             :  *
     327             :  * \requires This action requires an equation of state, which is retrieved from
     328             :  * the DataBox as `hydro::Tags::GrmhdEquationOfState`.
     329             :  */
     330           1 : struct ReceiveNumericInitialData {
     331           0 :   static constexpr size_t Dim = 3;
     332           0 :   using inbox_tags =
     333             :       tmpl::list<importers::Tags::VolumeData<NumericInitialData::all_vars>>;
     334             : 
     335             :   template <typename DbTagsList, typename... InboxTags, typename Metavariables,
     336             :             typename ActionList, typename ParallelComponent>
     337           0 :   static Parallel::iterable_action_return_t apply(
     338             :       db::DataBox<DbTagsList>& box, tuples::TaggedTuple<InboxTags...>& inboxes,
     339             :       const Parallel::GlobalCache<Metavariables>& /*cache*/,
     340             :       const ElementId<Dim>& /*element_id*/, const ActionList /*meta*/,
     341             :       const ParallelComponent* const /*meta*/) {
     342             :     auto& inbox =
     343             :         tuples::get<importers::Tags::VolumeData<NumericInitialData::all_vars>>(
     344             :             inboxes);
     345             :     const auto& initial_data = dynamic_cast<const NumericInitialData&>(
     346             :         db::get<evolution::initial_data::Tags::InitialData>(box));
     347             :     const size_t volume_data_id = initial_data.volume_data_id();
     348             :     if (inbox.find(volume_data_id) == inbox.end()) {
     349             :       return {Parallel::AlgorithmExecution::Retry, std::nullopt};
     350             :     }
     351             :     auto numeric_data = std::move(inbox.extract(volume_data_id).mapped());
     352             : 
     353             :     const auto& [mesh, inv_jacobian] = [&box]() {
     354             :       if constexpr (db::tag_is_retrievable_v<
     355             :                         evolution::dg::subcell::Tags::ActiveGrid,
     356             :                         db::DataBox<DbTagsList>>) {
     357             :         const bool on_subcell =
     358             :             db::get<evolution::dg::subcell::Tags::ActiveGrid>(box) ==
     359             :             evolution::dg::subcell::ActiveGrid::Subcell;
     360             :         if (on_subcell) {
     361             :           return std::forward_as_tuple(
     362             :               db::get<evolution::dg::subcell::Tags::Mesh<Dim>>(box),
     363             :               db::get<evolution::dg::subcell::fd::Tags::
     364             :                           InverseJacobianLogicalToInertial<Dim>>(box));
     365             :         }
     366             :       }
     367             :       return std::forward_as_tuple(
     368             :           db::get<domain::Tags::Mesh<Dim>>(box),
     369             :           db::get<domain::Tags::InverseJacobian<Dim, Frame::ElementLogical,
     370             :                                                 Frame::Inertial>>(box));
     371             :     }();
     372             :     const auto& equation_of_state =
     373             :         db::get<hydro::Tags::GrmhdEquationOfState>(box);
     374             : 
     375             :     db::mutate<gr::Tags::SpacetimeMetric<DataVector, 3>,
     376             :                gh::Tags::Pi<DataVector, 3>, gh::Tags::Phi<DataVector, 3>,
     377             :                hydro::Tags::RestMassDensity<DataVector>,
     378             :                hydro::Tags::ElectronFraction<DataVector>,
     379             :                hydro::Tags::SpecificInternalEnergy<DataVector>,
     380             :                hydro::Tags::SpatialVelocity<DataVector, 3>,
     381             :                hydro::Tags::MagneticField<DataVector, 3>,
     382             :                hydro::Tags::DivergenceCleaningField<DataVector>,
     383             :                hydro::Tags::LorentzFactor<DataVector>,
     384             :                hydro::Tags::Pressure<DataVector>,
     385             :                hydro::Tags::Temperature<DataVector>>(
     386             :         [&initial_data, &numeric_data, &equation_of_state](
     387             :             const gsl::not_null<tnsr::aa<DataVector, 3>*> spacetime_metric,
     388             :             const gsl::not_null<tnsr::aa<DataVector, 3>*> pi,
     389             :             const gsl::not_null<tnsr::iaa<DataVector, 3>*> phi,
     390             :             const gsl::not_null<Scalar<DataVector>*> rest_mass_density,
     391             :             const gsl::not_null<Scalar<DataVector>*> electron_fraction,
     392             :             const gsl::not_null<Scalar<DataVector>*> specific_internal_energy,
     393             :             const gsl::not_null<tnsr::I<DataVector, 3>*> spatial_velocity,
     394             :             const gsl::not_null<tnsr::I<DataVector, 3>*> magnetic_field,
     395             :             const gsl::not_null<Scalar<DataVector>*> div_cleaning_field,
     396             :             const gsl::not_null<Scalar<DataVector>*> lorentz_factor,
     397             :             const gsl::not_null<Scalar<DataVector>*> pressure,
     398             :             const gsl::not_null<Scalar<DataVector>*> temperature,
     399             :             const auto& local_mesh, const auto& local_inv_jacobian) {
     400             :           initial_data.set_initial_data(
     401             :               spacetime_metric, pi, phi, rest_mass_density, electron_fraction,
     402             :               specific_internal_energy, spatial_velocity, magnetic_field,
     403             :               div_cleaning_field, lorentz_factor, pressure, temperature,
     404             :               make_not_null(&numeric_data), local_mesh, local_inv_jacobian,
     405             :               equation_of_state);
     406             :         },
     407             :         make_not_null(&box), mesh, inv_jacobian);
     408             : 
     409             :     return {Parallel::AlgorithmExecution::Continue, std::nullopt};
     410             :   }
     411             : };
     412             : 
     413             : }  // namespace Actions
     414             : 
     415             : }  // namespace grmhd::GhValenciaDivClean

Generated by: LCOV version 1.14