SpECTRE Documentation Coverage Report
Current view: top level - Evolution/Systems/NewtonianEuler/Subcell - TimeDerivative.hpp Hit Total Coverage
Commit: 3528f39684ab2ee5d689cee48331779e729b0a07 Lines: 1 4 25.0 %
Date: 2024-02-27 07:22:14
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 <array>
       7             : #include <cstddef>
       8             : #include <optional>
       9             : #include <type_traits>
      10             : 
      11             : #include "DataStructures/DataBox/DataBox.hpp"
      12             : #include "DataStructures/DataBox/PrefixHelpers.hpp"
      13             : #include "DataStructures/DataBox/Prefixes.hpp"
      14             : #include "DataStructures/DataVector.hpp"
      15             : #include "DataStructures/Tensor/Tensor.hpp"
      16             : #include "DataStructures/Variables.hpp"
      17             : #include "Domain/CoordinateMaps/Tags.hpp"
      18             : #include "Domain/Structure/Element.hpp"
      19             : #include "Domain/Tags.hpp"
      20             : #include "Evolution/BoundaryCorrectionTags.hpp"
      21             : #include "Evolution/DgSubcell/CartesianFluxDivergence.hpp"
      22             : #include "Evolution/DgSubcell/ComputeBoundaryTerms.hpp"
      23             : #include "Evolution/DgSubcell/CorrectPackagedData.hpp"
      24             : #include "Evolution/DgSubcell/Tags/Coordinates.hpp"
      25             : #include "Evolution/DgSubcell/Tags/Jacobians.hpp"
      26             : #include "Evolution/DgSubcell/Tags/Mesh.hpp"
      27             : #include "Evolution/DgSubcell/Tags/OnSubcellFaces.hpp"
      28             : #include "Evolution/DiscontinuousGalerkin/Actions/NormalCovectorAndMagnitude.hpp"
      29             : #include "Evolution/DiscontinuousGalerkin/Actions/PackageDataImpl.hpp"
      30             : #include "Evolution/DiscontinuousGalerkin/MortarTags.hpp"
      31             : #include "Evolution/Systems/NewtonianEuler/BoundaryCorrections/BoundaryCorrection.hpp"
      32             : #include "Evolution/Systems/NewtonianEuler/FiniteDifference/Reconstructor.hpp"
      33             : #include "Evolution/Systems/NewtonianEuler/FiniteDifference/Tag.hpp"
      34             : #include "Evolution/Systems/NewtonianEuler/Fluxes.hpp"
      35             : #include "Evolution/Systems/NewtonianEuler/Sources.hpp"
      36             : #include "Evolution/Systems/NewtonianEuler/Subcell/ComputeFluxes.hpp"
      37             : #include "Evolution/Systems/NewtonianEuler/System.hpp"
      38             : #include "NumericalAlgorithms/Spectral/Mesh.hpp"
      39             : #include "Parallel/Tags/Metavariables.hpp"
      40             : #include "PointwiseFunctions/Hydro/Tags.hpp"
      41             : #include "Utilities/CallWithDynamicType.hpp"
      42             : #include "Utilities/ErrorHandling/Assert.hpp"
      43             : #include "Utilities/Gsl.hpp"
      44             : #include "Utilities/TMPL.hpp"
      45             : 
      46             : namespace NewtonianEuler::subcell {
      47             : /*!
      48             :  * \brief Compute the time derivative on the subcell grid using FD
      49             :  * reconstruction.
      50             :  *
      51             :  * The code makes the following unchecked assumptions:
      52             :  * - Assumes Cartesian coordinates with a diagonal Jacobian matrix
      53             :  * from the logical to the inertial frame
      54             :  * - Assumes the mesh is not moving (grid and inertial frame are the same)
      55             :  */
      56             : template <size_t Dim>
      57           1 : struct TimeDerivative {
      58             :   template <typename DbTagsList>
      59           0 :   static void apply(const gsl::not_null<db::DataBox<DbTagsList>*> box) {
      60             :     using metavariables = typename std::decay_t<decltype(
      61             :         db::get<Parallel::Tags::Metavariables>(*box))>;
      62             :     using system = typename metavariables::system;
      63             :     using evolved_vars_tag = typename system::variables_tag;
      64             :     using evolved_vars_tags = typename evolved_vars_tag::tags_list;
      65             :     using prim_tags = typename system::primitive_variables_tag::tags_list;
      66             :     using fluxes_tags = db::wrap_tags_in<::Tags::Flux, evolved_vars_tags,
      67             :                                          tmpl::size_t<Dim>, Frame::Inertial>;
      68             : 
      69             :     ASSERT((db::get<::domain::CoordinateMaps::Tags::CoordinateMap<
      70             :                 Dim, Frame::Grid, Frame::Inertial>>(*box))
      71             :                .is_identity(),
      72             :            "Do not yet support moving mesh with DG-subcell.");
      73             : 
      74             :     // The copy of Mesh is intentional to avoid a GCC-7 internal compiler error.
      75             :     const Mesh<Dim> subcell_mesh =
      76             :         db::get<evolution::dg::subcell::Tags::Mesh<Dim>>(*box);
      77             :     ASSERT(
      78             :         subcell_mesh == Mesh<Dim>(subcell_mesh.extents(0),
      79             :                                   subcell_mesh.basis(0),
      80             :                                   subcell_mesh.quadrature(0)),
      81             :         "The subcell/FD mesh must be isotropic for the FD time derivative but "
      82             :         "got "
      83             :             << subcell_mesh);
      84             :     const size_t num_pts = subcell_mesh.number_of_grid_points();
      85             :     const size_t reconstructed_num_pts =
      86             :         (subcell_mesh.extents(0) + 1) *
      87             :         subcell_mesh.extents().slice_away(0).product();
      88             : 
      89             :     const tnsr::I<DataVector, Dim, Frame::ElementLogical>&
      90             :         cell_centered_logical_coords =
      91             :             db::get<evolution::dg::subcell::Tags::Coordinates<
      92             :                 Dim, Frame::ElementLogical>>(*box);
      93             :     std::array<double, Dim> one_over_delta_xi{};
      94             :     for (size_t i = 0; i < Dim; ++i) {
      95             :       // Note: assumes isotropic extents
      96             :       gsl::at(one_over_delta_xi, i) =
      97             :           1.0 / (get<0>(cell_centered_logical_coords)[1] -
      98             :                  get<0>(cell_centered_logical_coords)[0]);
      99             :     }
     100             : 
     101             :     const NewtonianEuler::fd::Reconstructor<Dim>& recons =
     102             :         db::get<NewtonianEuler::fd::Tags::Reconstructor<Dim>>(*box);
     103             : 
     104             :     const Element<Dim>& element = db::get<domain::Tags::Element<Dim>>(*box);
     105             :     ASSERT(element.external_boundaries().size() == 0,
     106             :            "Can't have external boundaries right now with subcell. ElementID "
     107             :                << element.id());
     108             : 
     109             :     // Now package the data and compute the correction
     110             :     const auto& boundary_correction =
     111             :         db::get<evolution::Tags::BoundaryCorrection<system>>(*box);
     112             :     using derived_boundary_corrections =
     113             :         typename std::decay_t<decltype(boundary_correction)>::creatable_classes;
     114             :     std::array<Variables<evolved_vars_tags>, Dim> boundary_corrections{};
     115             :     tmpl::for_each<derived_boundary_corrections>([&boundary_correction,
     116             :                                                   &reconstructed_num_pts,
     117             :                                                   &recons, &box, &element,
     118             :                                                   &subcell_mesh,
     119             :                                                   &boundary_corrections](
     120             :                                                      auto
     121             :                                                          derived_correction_v) {
     122             :       using DerivedCorrection = tmpl::type_from<decltype(derived_correction_v)>;
     123             :       if (typeid(boundary_correction) == typeid(DerivedCorrection)) {
     124             :         using dg_package_data_temporary_tags =
     125             :             typename DerivedCorrection::dg_package_data_temporary_tags;
     126             :         using dg_package_data_argument_tags =
     127             :             tmpl::append<evolved_vars_tags, prim_tags, fluxes_tags,
     128             :                          dg_package_data_temporary_tags>;
     129             :         // Computed prims and cons on face via reconstruction
     130             :         auto package_data_argvars_lower_face = make_array<Dim>(
     131             :             Variables<dg_package_data_argument_tags>(reconstructed_num_pts));
     132             :         auto package_data_argvars_upper_face = make_array<Dim>(
     133             :             Variables<dg_package_data_argument_tags>(reconstructed_num_pts));
     134             : 
     135             :         // Reconstruct data to the face
     136             :         call_with_dynamic_type<void, typename NewtonianEuler::fd::Reconstructor<
     137             :                                          Dim>::creatable_classes>(
     138             :             &recons,
     139             :             [&box, &package_data_argvars_lower_face,
     140             :              &package_data_argvars_upper_face](const auto& reconstructor) {
     141             :               db::apply<typename std::decay_t<decltype(
     142             :                   *reconstructor)>::reconstruction_argument_tags>(
     143             :                   [&package_data_argvars_lower_face,
     144             :                    &package_data_argvars_upper_face,
     145             :                    &reconstructor](const auto&... args) {
     146             :                     reconstructor->reconstruct(
     147             :                         make_not_null(&package_data_argvars_lower_face),
     148             :                         make_not_null(&package_data_argvars_upper_face),
     149             :                         args...);
     150             :                   },
     151             :                   *box);
     152             :             });
     153             : 
     154             :         using dg_package_field_tags =
     155             :             typename DerivedCorrection::dg_package_field_tags;
     156             :         // Allocated outside for loop to reduce allocations
     157             :         Variables<dg_package_field_tags> upper_packaged_data{
     158             :             reconstructed_num_pts};
     159             :         Variables<dg_package_field_tags> lower_packaged_data{
     160             :             reconstructed_num_pts};
     161             : 
     162             :         // Compute fluxes on faces
     163             :         for (size_t i = 0; i < Dim; ++i) {
     164             :           auto& vars_upper_face = gsl::at(package_data_argvars_upper_face, i);
     165             :           auto& vars_lower_face = gsl::at(package_data_argvars_lower_face, i);
     166             :           NewtonianEuler::subcell::compute_fluxes<Dim>(
     167             :               make_not_null(&vars_upper_face));
     168             :           NewtonianEuler::subcell::compute_fluxes<Dim>(
     169             :               make_not_null(&vars_lower_face));
     170             : 
     171             :           tnsr::i<DataVector, Dim, Frame::Inertial> lower_outward_conormal{
     172             :               reconstructed_num_pts, 0.0};
     173             :           lower_outward_conormal.get(i) = 1.0;
     174             : 
     175             :           tnsr::i<DataVector, Dim, Frame::Inertial> upper_outward_conormal{
     176             :               reconstructed_num_pts, 0.0};
     177             :           upper_outward_conormal.get(i) = -1.0;
     178             : 
     179             :           // Compute the packaged data
     180             :           using dg_package_data_projected_tags = tmpl::append<
     181             :               evolved_vars_tags, fluxes_tags, dg_package_data_temporary_tags,
     182             :               typename DerivedCorrection::dg_package_data_primitive_tags>;
     183             :           evolution::dg::Actions::detail::dg_package_data<system>(
     184             :               make_not_null(&upper_packaged_data),
     185             :               dynamic_cast<const DerivedCorrection&>(boundary_correction),
     186             :               vars_upper_face, upper_outward_conormal, {std::nullopt}, *box,
     187             :               typename DerivedCorrection::dg_package_data_volume_tags{},
     188             :               dg_package_data_projected_tags{});
     189             : 
     190             :           evolution::dg::Actions::detail::dg_package_data<system>(
     191             :               make_not_null(&lower_packaged_data),
     192             :               dynamic_cast<const DerivedCorrection&>(boundary_correction),
     193             :               vars_lower_face, lower_outward_conormal, {std::nullopt}, *box,
     194             :               typename DerivedCorrection::dg_package_data_volume_tags{},
     195             :               dg_package_data_projected_tags{});
     196             : 
     197             :           // Now need to check if any of our neighbors are doing DG,
     198             :           // because if so then we need to use whatever boundary data
     199             :           // they sent instead of what we computed locally.
     200             :           //
     201             :           // Note: We could check this beforehand to avoid the extra
     202             :           // work of reconstruction and flux computations at the
     203             :           // boundaries.
     204             :           evolution::dg::subcell::correct_package_data<true>(
     205             :               make_not_null(&lower_packaged_data),
     206             :               make_not_null(&upper_packaged_data), i, element, subcell_mesh,
     207             :               db::get<evolution::dg::Tags::MortarData<Dim>>(*box), 0);
     208             : 
     209             :           // Compute the corrections on the faces. We only need to
     210             :           // compute this once because we can just flip the normal
     211             :           // vectors then
     212             :           gsl::at(boundary_corrections, i).initialize(reconstructed_num_pts);
     213             :           evolution::dg::subcell::compute_boundary_terms(
     214             :               make_not_null(&gsl::at(boundary_corrections, i)),
     215             :               dynamic_cast<const DerivedCorrection&>(boundary_correction),
     216             :               upper_packaged_data, lower_packaged_data);
     217             :         }
     218             :       }
     219             :     });
     220             : 
     221             :     // Now compute the actual time derivatives.
     222             :     using dt_variables_tag = db::add_tag_prefix<::Tags::dt, evolved_vars_tag>;
     223             :     using source_argument_tags = tmpl::conditional_t<
     224             :         metavariables::has_source_terms,
     225             :         tmpl::replace<
     226             :             tmpl::push_front<
     227             :                 typename metavariables::source_term_type::argument_tags,
     228             :                 Tags::SourceTerm<typename metavariables::initial_data>>,
     229             :             ::domain::Tags::Coordinates<Dim, Frame::Inertial>,
     230             :             ::evolution::dg::subcell::Tags::Coordinates<Dim, Frame::Inertial>>,
     231             :         tmpl::list<>>;
     232             :     db::mutate_apply<tmpl::list<dt_variables_tag>, source_argument_tags>(
     233             :         [&num_pts, &boundary_corrections, &subcell_mesh, &one_over_delta_xi,
     234             :          &cell_centered_logical_to_grid_inv_jacobian =
     235             :              db::get<evolution::dg::subcell::fd::Tags::
     236             :                          InverseJacobianLogicalToGrid<Dim>>(*box)](
     237             :             const auto dt_vars_ptr, const auto&... source_args) {
     238             :           dt_vars_ptr->initialize(num_pts, 0.0);
     239             :           using MassDensityCons = NewtonianEuler::Tags::MassDensityCons;
     240             :           using MomentumDensity = NewtonianEuler::Tags::MomentumDensity<Dim>;
     241             :           using EnergyDensity = NewtonianEuler::Tags::EnergyDensity;
     242             : 
     243             :           auto& dt_mass = get<::Tags::dt<MassDensityCons>>(*dt_vars_ptr);
     244             :           auto& dt_momentum = get<::Tags::dt<MomentumDensity>>(*dt_vars_ptr);
     245             :           auto& dt_energy = get<::Tags::dt<EnergyDensity>>(*dt_vars_ptr);
     246             : 
     247             :           if constexpr (metavariables::has_source_terms) {
     248             :             sources_impl(
     249             :                 std::make_tuple(make_not_null(&dt_mass),
     250             :                                 make_not_null(&dt_momentum),
     251             :                                 make_not_null(&dt_energy)),
     252             :                 typename metavariables::source_term_type::sourced_variables{},
     253             :                 source_args...);
     254             :           }
     255             : 
     256             :           for (size_t dim = 0; dim < Dim; ++dim) {
     257             :             Scalar<DataVector>& mass_density_correction =
     258             :                 get<MassDensityCons>(gsl::at(boundary_corrections, dim));
     259             :             Scalar<DataVector>& energy_density_correction =
     260             :                 get<EnergyDensity>(gsl::at(boundary_corrections, dim));
     261             :             tnsr::I<DataVector, Dim, Frame::Inertial>&
     262             :                 momentum_density_correction =
     263             :                     get<MomentumDensity>(gsl::at(boundary_corrections, dim));
     264             :             evolution::dg::subcell::add_cartesian_flux_divergence(
     265             :                 make_not_null(&get(dt_mass)), gsl::at(one_over_delta_xi, dim),
     266             :                 cell_centered_logical_to_grid_inv_jacobian.get(dim, dim),
     267             :                 get(mass_density_correction), subcell_mesh.extents(), dim);
     268             :             evolution::dg::subcell::add_cartesian_flux_divergence(
     269             :                 make_not_null(&get(dt_energy)), gsl::at(one_over_delta_xi, dim),
     270             :                 cell_centered_logical_to_grid_inv_jacobian.get(dim, dim),
     271             :                 get(energy_density_correction), subcell_mesh.extents(), dim);
     272             :             for (size_t d = 0; d < Dim; ++d) {
     273             :               evolution::dg::subcell::add_cartesian_flux_divergence(
     274             :                   make_not_null(&dt_momentum.get(d)),
     275             :                   gsl::at(one_over_delta_xi, dim),
     276             :                   cell_centered_logical_to_grid_inv_jacobian.get(dim, dim),
     277             :                   momentum_density_correction.get(d), subcell_mesh.extents(),
     278             :                   dim);
     279             :             }
     280             :           }
     281             :         },
     282             :         box);
     283             :   }
     284             : 
     285             :  private:
     286             :   template <typename SourceTerm, typename... SourceTermArgs,
     287             :             typename... SourcedVars>
     288           0 :   static void sources_impl(std::tuple<gsl::not_null<Scalar<DataVector>*>,
     289             :                                       gsl::not_null<tnsr::I<DataVector, Dim>*>,
     290             :                                       gsl::not_null<Scalar<DataVector>*>>
     291             :                                dt_vars,
     292             :                            tmpl::list<SourcedVars...> /*meta*/,
     293             :                            const SourceTerm& source,
     294             :                            const SourceTermArgs&... source_term_args) {
     295             :     using dt_vars_list =
     296             :         tmpl::list<Tags::MassDensityCons, Tags::MomentumDensity<Dim>,
     297             :                    Tags::EnergyDensity>;
     298             : 
     299             :     source.apply(
     300             :         std::get<tmpl::index_of<dt_vars_list, SourcedVars>::value>(dt_vars)...,
     301             :         source_term_args...);
     302             :   }
     303             : };
     304             : }  // namespace NewtonianEuler::subcell

Generated by: LCOV version 1.14