SpECTRE Documentation Coverage Report
Current view: top level - Evolution/Systems/ScalarTensor/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 <pup.h>
       9             : #include <string>
      10             : #include <variant>
      11             : 
      12             : #include "DataStructures/DataBox/DataBox.hpp"
      13             : #include "DataStructures/DataVector.hpp"
      14             : #include "DataStructures/Tensor/EagerMath/DeterminantAndInverse.hpp"
      15             : #include "DataStructures/Tensor/Tensor.hpp"
      16             : #include "Domain/Structure/ElementId.hpp"
      17             : #include "Domain/Tags.hpp"
      18             : #include "Evolution/Initialization/InitialData.hpp"
      19             : #include "Evolution/NumericInitialData.hpp"
      20             : #include "Evolution/Systems/CurvedScalarWave/Actions/SetInitialData.hpp"
      21             : #include "Evolution/Systems/CurvedScalarWave/Tags.hpp"
      22             : #include "Evolution/Systems/GeneralizedHarmonic/Actions/SetInitialData.hpp"
      23             : #include "Evolution/Systems/GeneralizedHarmonic/Tags.hpp"
      24             : #include "IO/Importers/Actions/ReadVolumeData.hpp"
      25             : #include "IO/Importers/ElementDataReader.hpp"
      26             : #include "IO/Importers/Tags.hpp"
      27             : #include "NumericalAlgorithms/LinearOperators/PartialDerivatives.hpp"
      28             : #include "NumericalAlgorithms/Spectral/Mesh.hpp"
      29             : #include "Options/String.hpp"
      30             : #include "Parallel/AlgorithmExecution.hpp"
      31             : #include "Parallel/GlobalCache.hpp"
      32             : #include "Parallel/Invoke.hpp"
      33             : #include "PointwiseFunctions/GeneralRelativity/SpatialMetric.hpp"
      34             : #include "PointwiseFunctions/GeneralRelativity/Tags.hpp"
      35             : #include "PointwiseFunctions/InitialDataUtilities/InitialData.hpp"
      36             : #include "PointwiseFunctions/InitialDataUtilities/Tags/InitialData.hpp"
      37             : #include "Utilities/CallWithDynamicType.hpp"
      38             : #include "Utilities/ErrorHandling/Error.hpp"
      39             : #include "Utilities/Gsl.hpp"
      40             : #include "Utilities/Serialization/CharmPupable.hpp"
      41             : #include "Utilities/TMPL.hpp"
      42             : #include "Utilities/TaggedTuple.hpp"
      43             : 
      44           1 : namespace ScalarTensor {
      45             : 
      46             : /*!
      47             :  * \brief Numeric initial data loaded from volume data files
      48             :  */
      49           1 : class NumericInitialData : public evolution::initial_data::InitialData,
      50             :                            public evolution::NumericInitialData {
      51             :  private:
      52           0 :   using GhNumericId = gh::NumericInitialData;
      53           0 :   using ScalarNumericId = CurvedScalarWave::NumericInitialData;
      54             : 
      55             :  public:
      56           0 :   using all_vars =
      57             :       tmpl::append<GhNumericId::all_vars, ScalarNumericId::all_vars>;
      58             : 
      59           0 :   struct GhVariables : GhNumericId::Variables {};
      60           0 :   struct ScalarVariables : ScalarNumericId::Variables {};
      61             : 
      62           0 :   using options = tmpl::push_back<importers::ImporterOptions::tags_list,
      63             :                                   GhVariables, ScalarVariables>;
      64             : 
      65           0 :   static constexpr Options::String help =
      66             :       "Numeric initial data for the Scalar Tensor system loaded from volume "
      67             :       "data files";
      68             : 
      69           0 :   NumericInitialData() = default;
      70           0 :   NumericInitialData(const NumericInitialData& rhs) = default;
      71           0 :   NumericInitialData& operator=(const NumericInitialData& rhs) = default;
      72           0 :   NumericInitialData(NumericInitialData&& /*rhs*/) = default;
      73           0 :   NumericInitialData& operator=(NumericInitialData&& /*rhs*/) = default;
      74           0 :   ~NumericInitialData() override = default;
      75             : 
      76             :   /// \cond
      77             :   explicit NumericInitialData(CkMigrateMessage* msg);
      78             :   using PUP::able::register_constructor;
      79             :   WRAPPED_PUPable_decl_template(NumericInitialData);
      80             :   /// \endcond
      81             : 
      82           0 :   std::unique_ptr<evolution::initial_data::InitialData> get_clone()
      83             :       const override {
      84             :     return std::make_unique<NumericInitialData>(*this);
      85             :   }
      86             : 
      87           0 :   NumericInitialData(
      88             :       std::string file_glob, std::string subfile_name,
      89             :       std::variant<double, importers::ObservationSelector> observation_value,
      90             :       std::optional<double> observation_value_epsilon,
      91             :       bool enable_interpolation,
      92             :       typename GhNumericId::Variables::type gh_selected_variables,
      93             :       typename ScalarNumericId::Variables::type hydro_selected_variables);
      94             : 
      95           0 :   const importers::ImporterOptions& importer_options() const {
      96             :     return gh_numeric_id_.importer_options();
      97             :   }
      98             : 
      99           0 :   const GhNumericId& gh_numeric_id() const { return gh_numeric_id_; }
     100             : 
     101           0 :   const ScalarNumericId& scalar_numeric_id() const {
     102             :     return scalar_numeric_id_;
     103             :   }
     104             : 
     105           0 :   size_t volume_data_id() const;
     106             : 
     107             :   template <typename... AllTags>
     108           0 :   void select_for_import(
     109             :       const gsl::not_null<tuples::TaggedTuple<AllTags...>*> fields) const {
     110             :     gh_numeric_id_.select_for_import(fields);
     111             :     scalar_numeric_id_.select_for_import(fields);
     112             :   }
     113             : 
     114             :   template <typename... AllTags>
     115           0 :   void set_initial_data(
     116             :       const gsl::not_null<tnsr::aa<DataVector, 3>*> spacetime_metric,
     117             :       const gsl::not_null<tnsr::aa<DataVector, 3>*> pi,
     118             :       const gsl::not_null<tnsr::iaa<DataVector, 3>*> phi,
     119             :       const gsl::not_null<Scalar<DataVector>*> psi_scalar,
     120             :       const gsl::not_null<Scalar<DataVector>*> pi_scalar,
     121             :       const gsl::not_null<tnsr::i<DataVector, 3>*> phi_scalar,
     122             :       const gsl::not_null<tuples::TaggedTuple<AllTags...>*> numeric_data,
     123             :       const Mesh<3>& mesh,
     124             :       const InverseJacobian<DataVector, 3, Frame::ElementLogical,
     125             :                             Frame::Inertial>& inv_jacobian) const {
     126             :     gh_numeric_id_.set_initial_data(spacetime_metric, pi, phi, numeric_data,
     127             :                                     mesh, inv_jacobian);
     128             :     scalar_numeric_id_.set_initial_data(psi_scalar, pi_scalar, phi_scalar,
     129             :                                         numeric_data);
     130             :   }
     131             : 
     132           0 :   void pup(PUP::er& p) override;
     133             : 
     134           0 :   friend bool operator==(const NumericInitialData& lhs,
     135             :                          const NumericInitialData& rhs);
     136             : 
     137             :  private:
     138           0 :   GhNumericId gh_numeric_id_{};
     139           0 :   ScalarNumericId scalar_numeric_id_{};
     140             : };
     141             : 
     142           0 : namespace Actions {
     143             : 
     144             : /*!
     145             :  * \brief Dispatch loading numeric initial data from files.
     146             :  *
     147             :  * Place this action before
     148             :  * ScalarTensor::Actions::SetNumericInitialData in the action list.
     149             :  * See importers::Actions::ReadAllVolumeDataAndDistribute for details, which is
     150             :  * invoked by this action.
     151             :  */
     152           1 : struct SetInitialData {
     153           0 :   using const_global_cache_tags =
     154             :       tmpl::list<evolution::initial_data::Tags::InitialData>;
     155             : 
     156             :   template <typename DbTagsList, typename... InboxTags, typename Metavariables,
     157             :             typename ArrayIndex, typename ActionList,
     158             :             typename ParallelComponent>
     159           0 :   static Parallel::iterable_action_return_t apply(
     160             :       db::DataBox<DbTagsList>& box,
     161             :       const tuples::TaggedTuple<InboxTags...>& /*inboxes*/,
     162             :       Parallel::GlobalCache<Metavariables>& cache,
     163             :       const ArrayIndex& array_index, const ActionList /*meta*/,
     164             :       const ParallelComponent* const parallel_component) {
     165             :     // Dispatch to the correct `apply` overload based on type of initial data
     166             :     using initial_data_classes =
     167             :         tmpl::at<typename Metavariables::factory_creation::factory_classes,
     168             :                  evolution::initial_data::InitialData>;
     169             :     return call_with_dynamic_type<Parallel::iterable_action_return_t,
     170             :                                   initial_data_classes>(
     171             :         &db::get<evolution::initial_data::Tags::InitialData>(box),
     172             :         [&box, &cache, &array_index,
     173             :          &parallel_component](const auto* const initial_data) {
     174             :           return apply(make_not_null(&box), *initial_data, cache, array_index,
     175             :                        parallel_component);
     176             :         });
     177             :   }
     178             : 
     179             :  private:
     180           0 :   static constexpr size_t Dim = 3;
     181             : 
     182             :   // Numeric initial data
     183             :   template <typename DbTagsList, typename Metavariables, typename ArrayIndex,
     184             :             typename ParallelComponent>
     185           0 :   static Parallel::iterable_action_return_t apply(
     186             :       const gsl::not_null<db::DataBox<DbTagsList>*> /*box*/,
     187             :       const NumericInitialData& initial_data,
     188             :       Parallel::GlobalCache<Metavariables>& cache,
     189             :       const ArrayIndex& array_index, const ParallelComponent* const /*meta*/) {
     190             :     // If we are using GH Numeric ID, then we don't have to set Pi and Phi since
     191             :     // we are reading them in. Also we only need to mutate this tag once so do
     192             :     // it on the first element.
     193             :     if (is_zeroth_element(array_index) and
     194             :         std::holds_alternative<gh::NumericInitialData::GhVars>(
     195             :             initial_data.gh_numeric_id().selected_variables())) {
     196             :       Parallel::mutate<gh::Tags::SetPiAndPhiFromConstraints,
     197             :                        gh::gauges::SetPiAndPhiFromConstraintsCacheMutator>(
     198             :           cache, false);
     199             :     }
     200             :     // Select the subset of the available variables that we want to read from
     201             :     // the volume data file
     202             :     tuples::tagged_tuple_from_typelist<db::wrap_tags_in<
     203             :         importers::Tags::Selected, NumericInitialData::all_vars>>
     204             :         selected_fields{};
     205             :     initial_data.select_for_import(make_not_null(&selected_fields));
     206             :     // Dispatch loading the variables from the volume data file
     207             :     // - Not using `ckLocalBranch` here to make sure the simple action
     208             :     //   invocation is asynchronous.
     209             :     auto& reader_component = Parallel::get_parallel_component<
     210             :         importers::ElementDataReader<Metavariables>>(cache);
     211             :     Parallel::simple_action<importers::Actions::ReadAllVolumeDataAndDistribute<
     212             :         3, NumericInitialData::all_vars, ParallelComponent>>(
     213             :         reader_component, initial_data.importer_options(),
     214             :         initial_data.volume_data_id(), std::move(selected_fields));
     215             :     return {Parallel::AlgorithmExecution::Continue, std::nullopt};
     216             :   }
     217             : 
     218             :   // "AnalyticData"-type initial data
     219             :   template <typename DbTagsList, typename InitialData, typename Metavariables,
     220             :             typename ArrayIndex, typename ParallelComponent>
     221           0 :   static Parallel::iterable_action_return_t apply(
     222             :       const gsl::not_null<db::DataBox<DbTagsList>*> box,
     223             :       const InitialData& initial_data,
     224             :       Parallel::GlobalCache<Metavariables>& /*cache*/,
     225             :       const ArrayIndex& /*array_index*/,
     226             :       const ParallelComponent* const /*meta*/) {
     227             :     // Get ADM + scalar variables from analytic data / solution
     228             :     const auto& [coords, mesh, inv_jacobian] = [&box]() {
     229             :       return std::forward_as_tuple(
     230             :           db::get<domain::Tags::Coordinates<Dim, Frame::Inertial>>(*box),
     231             :           db::get<domain::Tags::Mesh<Dim>>(*box),
     232             :           db::get<domain::Tags::InverseJacobian<Dim, Frame::ElementLogical,
     233             :                                                 Frame::Inertial>>(*box));
     234             :     }();
     235             :     auto vars = evolution::Initialization::initial_data(
     236             :         initial_data, coords, db::get<::Tags::Time>(*box),
     237             :         tmpl::append<tmpl::list<gr::Tags::SpatialMetric<DataVector, 3>,
     238             :                                 gr::Tags::Lapse<DataVector>,
     239             :                                 gr::Tags::Shift<DataVector, 3>,
     240             :                                 gr::Tags::ExtrinsicCurvature<DataVector, 3>>,
     241             :                      // Don't use the scalar gradient
     242             :                      tmpl::list<CurvedScalarWave::Tags::Psi,
     243             :                                 CurvedScalarWave::Tags::Pi>>{});
     244             :     const auto& spatial_metric =
     245             :         get<gr::Tags::SpatialMetric<DataVector, 3>>(vars);
     246             :     const auto& lapse = get<gr::Tags::Lapse<DataVector>>(vars);
     247             : 
     248             :     const auto& shift = get<gr::Tags::Shift<DataVector, 3>>(vars);
     249             : 
     250             :     const auto& extrinsic_curvature =
     251             :         get<gr::Tags::ExtrinsicCurvature<DataVector, 3>>(vars);
     252             : 
     253             :     // Compute GH vars from ADM vars
     254             :     db::mutate<gr::Tags::SpacetimeMetric<DataVector, 3>,
     255             :                gh::Tags::Pi<DataVector, 3>, gh::Tags::Phi<DataVector, 3>>(
     256             :         &gh::initial_gh_variables_from_adm<3>, box, spatial_metric, lapse,
     257             :         shift, extrinsic_curvature, mesh, inv_jacobian);
     258             : 
     259             :     // Move scalar variables and compute gradient
     260             :     db::mutate<CurvedScalarWave::Tags::Psi, CurvedScalarWave::Tags::Pi,
     261             :                CurvedScalarWave::Tags::Phi<3>>(
     262             :         [&vars](const gsl::not_null<Scalar<DataVector>*> psi_scalar,
     263             :                 const gsl::not_null<Scalar<DataVector>*> pi_scalar,
     264             :                 const gsl::not_null<tnsr::i<DataVector, 3>*> phi_scalar,
     265             :                 const Mesh<3>& local_mesh,
     266             :                 const InverseJacobian<DataVector, 3_st, Frame::ElementLogical,
     267             :                                       Frame::Inertial>& local_inv_jacobian) {
     268             :           *psi_scalar = std::move(get<CurvedScalarWave::Tags::Psi>(vars));
     269             :           *pi_scalar = std::move(get<CurvedScalarWave::Tags::Pi>(vars));
     270             :           // Set Phi to the numerical spatial derivative of the scalar
     271             :           partial_derivative(phi_scalar, *psi_scalar, local_mesh,
     272             :                              local_inv_jacobian);
     273             :         },
     274             :         box, mesh, inv_jacobian);
     275             : 
     276             :     // No need to import numeric initial data, so we terminate the phase by
     277             :     // pausing the algorithm on this element
     278             :     return {Parallel::AlgorithmExecution::Pause, std::nullopt};
     279             :   }
     280             : };
     281             : 
     282             : /*!
     283             :  * \brief Receive numeric initial data loaded by
     284             :  * ScalarTensor::Actions::SetInitialData.
     285             :  */
     286           1 : struct ReceiveNumericInitialData {
     287           0 :   static constexpr size_t Dim = 3;
     288           0 :   using inbox_tags =
     289             :       tmpl::list<importers::Tags::VolumeData<NumericInitialData::all_vars>>;
     290             : 
     291             :   template <typename DbTagsList, typename... InboxTags, typename Metavariables,
     292             :             typename ActionList, typename ParallelComponent>
     293           0 :   static Parallel::iterable_action_return_t apply(
     294             :       db::DataBox<DbTagsList>& box, tuples::TaggedTuple<InboxTags...>& inboxes,
     295             :       const Parallel::GlobalCache<Metavariables>& /*cache*/,
     296             :       const ElementId<Dim>& /*element_id*/, const ActionList /*meta*/,
     297             :       const ParallelComponent* const /*meta*/) {
     298             :     auto& inbox =
     299             :         tuples::get<importers::Tags::VolumeData<NumericInitialData::all_vars>>(
     300             :             inboxes);
     301             :     const auto& initial_data = dynamic_cast<const NumericInitialData&>(
     302             :         db::get<evolution::initial_data::Tags::InitialData>(box));
     303             :     const size_t volume_data_id = initial_data.volume_data_id();
     304             :     if (inbox.find(volume_data_id) == inbox.end()) {
     305             :       return {Parallel::AlgorithmExecution::Retry, std::nullopt};
     306             :     }
     307             :     auto numeric_data = std::move(inbox.extract(volume_data_id).mapped());
     308             : 
     309             :     const auto& mesh = db::get<domain::Tags::Mesh<Dim>>(box);
     310             :     const auto& inv_jacobian =
     311             :         db::get<domain::Tags::InverseJacobian<Dim, Frame::ElementLogical,
     312             :                                               Frame::Inertial>>(box);
     313             : 
     314             :     db::mutate<gr::Tags::SpacetimeMetric<DataVector, 3>,
     315             :                gh::Tags::Pi<DataVector, 3>, gh::Tags::Phi<DataVector, 3>,
     316             :                CurvedScalarWave::Tags::Psi, CurvedScalarWave::Tags::Pi,
     317             :                CurvedScalarWave::Tags::Phi<3>>(
     318             :         [&initial_data, &numeric_data, &mesh, &inv_jacobian](
     319             :             const gsl::not_null<tnsr::aa<DataVector, 3>*> spacetime_metric,
     320             :             const gsl::not_null<tnsr::aa<DataVector, 3>*> pi,
     321             :             const gsl::not_null<tnsr::iaa<DataVector, 3>*> phi,
     322             :             const gsl::not_null<Scalar<DataVector>*> psi_scalar,
     323             :             const gsl::not_null<Scalar<DataVector>*> pi_scalar,
     324             :             const gsl::not_null<tnsr::i<DataVector, 3>*> phi_scalar) {
     325             :           initial_data.set_initial_data(spacetime_metric, pi, phi,
     326             : 
     327             :                                         psi_scalar, pi_scalar, phi_scalar,
     328             : 
     329             :                                         make_not_null(&numeric_data), mesh,
     330             :                                         inv_jacobian);
     331             :         },
     332             :         make_not_null(&box));
     333             : 
     334             :     return {Parallel::AlgorithmExecution::Continue, std::nullopt};
     335             :   }
     336             : };
     337             : 
     338             : }  // namespace Actions
     339             : 
     340             : }  // namespace ScalarTensor

Generated by: LCOV version 1.14