SpECTRE Documentation Coverage Report
Current view: top level - Evolution/Systems/ScalarAdvection/Subcell - TimeDerivative.hpp Hit Total Coverage
Commit: 1f2210958b4f38fdc0400907ee7c6d5af5111418 Lines: 1 3 33.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 <array>
       7             : #include <cstddef>
       8             : #include <optional>
       9             : #include <type_traits>
      10             : 
      11             : #include "DataStructures/DataBox/AsAccess.hpp"
      12             : #include "DataStructures/DataBox/DataBox.hpp"
      13             : #include "DataStructures/DataBox/MetavariablesTag.hpp"
      14             : #include "DataStructures/DataBox/PrefixHelpers.hpp"
      15             : #include "DataStructures/DataBox/Prefixes.hpp"
      16             : #include "DataStructures/DataVector.hpp"
      17             : #include "DataStructures/Tensor/Tensor.hpp"
      18             : #include "DataStructures/Variables.hpp"
      19             : #include "Domain/CoordinateMaps/Tags.hpp"
      20             : #include "Domain/Structure/Element.hpp"
      21             : #include "Domain/Tags.hpp"
      22             : #include "Evolution/BoundaryCorrection.hpp"
      23             : #include "Evolution/BoundaryCorrectionTags.hpp"
      24             : #include "Evolution/DgSubcell/CartesianFluxDivergence.hpp"
      25             : #include "Evolution/DgSubcell/ComputeBoundaryTerms.hpp"
      26             : #include "Evolution/DgSubcell/CorrectPackagedData.hpp"
      27             : #include "Evolution/DgSubcell/Tags/Coordinates.hpp"
      28             : #include "Evolution/DgSubcell/Tags/Jacobians.hpp"
      29             : #include "Evolution/DgSubcell/Tags/Mesh.hpp"
      30             : #include "Evolution/DgSubcell/Tags/OnSubcellFaces.hpp"
      31             : #include "Evolution/DiscontinuousGalerkin/Actions/PackageDataImpl.hpp"
      32             : #include "Evolution/DiscontinuousGalerkin/MortarTags.hpp"
      33             : #include "Evolution/Systems/ScalarAdvection/FiniteDifference/Reconstructor.hpp"
      34             : #include "Evolution/Systems/ScalarAdvection/FiniteDifference/Tags.hpp"
      35             : #include "Evolution/Systems/ScalarAdvection/Fluxes.hpp"
      36             : #include "Evolution/Systems/ScalarAdvection/Subcell/ComputeFluxes.hpp"
      37             : #include "Evolution/Systems/ScalarAdvection/System.hpp"
      38             : #include "NumericalAlgorithms/Spectral/Mesh.hpp"
      39             : #include "Utilities/CallWithDynamicType.hpp"
      40             : #include "Utilities/ErrorHandling/Assert.hpp"
      41             : #include "Utilities/Gsl.hpp"
      42             : #include "Utilities/MakeArray.hpp"
      43             : 
      44             : namespace ScalarAdvection::subcell {
      45             : /*!
      46             :  * \brief Compute the time derivative on the subcell grid using FD
      47             :  * reconstruction.
      48             :  *
      49             :  * The code makes the following unchecked assumptions:
      50             :  * - Assumes Cartesian coordinates with a diagonal Jacobian matrix
      51             :  * from the logical to the inertial frame
      52             :  * - Assumes the mesh is not moving (grid and inertial frame are the same)
      53             :  */
      54             : template <size_t Dim>
      55           1 : struct TimeDerivative {
      56             :   template <typename DbTagsList>
      57           0 :   static void apply(const gsl::not_null<db::DataBox<DbTagsList>*> box) {
      58             :     using metavariables =
      59             :         typename std::decay_t<decltype(db::get<Parallel::Tags::Metavariables>(
      60             :             *box))>;
      61             :     ASSERT((db::get<::domain::CoordinateMaps::Tags::CoordinateMap<
      62             :                 Dim, Frame::Grid, Frame::Inertial>>(*box))
      63             :                .is_identity(),
      64             :            "Do not yet support moving mesh with DG-subcell.");
      65             :     // subcell is currently not supported for external boundary elements
      66             :     const Element<Dim>& element = db::get<domain::Tags::Element<Dim>>(*box);
      67             :     ASSERT(element.external_boundaries().size() == 0,
      68             :            "Can't have external boundaries right now with subcell. ElementID "
      69             :                << element.id());
      70             : 
      71             :     using evolved_vars_tags = typename System<Dim>::variables_tag::tags_list;
      72             :     using fluxes_tags = typename Fluxes<Dim>::return_tags;
      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             : 
      85             :     const size_t num_reconstructed_pts =
      86             :         (subcell_mesh.extents(0) + 1) *
      87             :         subcell_mesh.extents().slice_away(0).product();
      88             : 
      89             :     const ScalarAdvection::fd::Reconstructor<Dim>& recons =
      90             :         db::get<ScalarAdvection::fd::Tags::Reconstructor<Dim>>(*box);
      91             : 
      92             :     const auto& boundary_correction =
      93             :         db::get<evolution::Tags::BoundaryCorrection>(*box);
      94             :     using derived_boundary_corrections =
      95             :         tmpl::at<typename metavariables::factory_creation::factory_classes,
      96             :                  evolution::BoundaryCorrection>;
      97             : 
      98             :     // Variables to store the boundary correction terms on FD subinterfaces
      99             :     std::array<Variables<evolved_vars_tags>, Dim> fd_boundary_corrections{};
     100             : 
     101             :     // package the data and compute the boundary correction
     102             :     tmpl::for_each<derived_boundary_corrections>([&boundary_correction,
     103             :                                                   &fd_boundary_corrections,
     104             :                                                   &box, &element,
     105             :                                                   &num_reconstructed_pts,
     106             :                                                   &recons, &subcell_mesh](
     107             :                                                      auto
     108             :                                                          derived_correction_v) {
     109             :       using derived_correction =
     110             :           tmpl::type_from<decltype(derived_correction_v)>;
     111             :       if (typeid(boundary_correction) == typeid(derived_correction)) {
     112             :         using dg_package_field_tags =
     113             :             typename derived_correction::dg_package_field_tags;
     114             :         using dg_package_data_temporary_tags =
     115             :             typename derived_correction::dg_package_data_temporary_tags;
     116             :         using dg_package_data_argument_tags =
     117             :             tmpl::append<evolved_vars_tags, fluxes_tags,
     118             :                          dg_package_data_temporary_tags>;
     119             : 
     120             :         // Variables that need to be reconstructed for dg_package_data()
     121             :         auto package_data_argvars_lower_face = make_array<Dim>(
     122             :             Variables<dg_package_data_argument_tags>(num_reconstructed_pts));
     123             :         auto package_data_argvars_upper_face = make_array<Dim>(
     124             :             Variables<dg_package_data_argument_tags>(num_reconstructed_pts));
     125             : 
     126             :         // Reconstruct the fields on interfaces
     127             :         call_with_dynamic_type<void, typename ScalarAdvection::fd::
     128             :                                          Reconstructor<Dim>::creatable_classes>(
     129             :             &recons,
     130             :             [&box, &package_data_argvars_lower_face,
     131             :              &package_data_argvars_upper_face](const auto& reconstructor) {
     132             :               db::apply<typename std::decay_t<decltype(
     133             :                   *reconstructor)>::reconstruction_argument_tags>(
     134             :                   [&package_data_argvars_lower_face,
     135             :                    &package_data_argvars_upper_face,
     136             :                    &reconstructor](const auto&... args) {
     137             :                     reconstructor->reconstruct(
     138             :                         make_not_null(&package_data_argvars_lower_face),
     139             :                         make_not_null(&package_data_argvars_upper_face),
     140             :                         args...);
     141             :                   },
     142             :                   *box);
     143             :             });
     144             : 
     145             :         // copy over the face values of the velocity field
     146             :         {
     147             :           using tag = ::ScalarAdvection::Tags::VelocityField<Dim>;
     148             :           const auto& velocity_on_face =
     149             :               db::get<evolution::dg::subcell::Tags::OnSubcellFaces<tag, Dim>>(
     150             :                   *box);
     151             :           for (size_t d = 0; d < Dim; ++d) {
     152             :             get<tag>(gsl::at(package_data_argvars_lower_face, d)) =
     153             :                 gsl::at(velocity_on_face, d);
     154             :             get<tag>(gsl::at(package_data_argvars_upper_face, d)) =
     155             :                 gsl::at(velocity_on_face, d);
     156             :           }
     157             :         }
     158             : 
     159             :         // Variables to store packaged data. Allocate outside of loop to
     160             :         // reduce allocations
     161             :         Variables<dg_package_field_tags> upper_packaged_data{
     162             :             num_reconstructed_pts};
     163             :         Variables<dg_package_field_tags> lower_packaged_data{
     164             :             num_reconstructed_pts};
     165             : 
     166             :         for (size_t dim = 0; dim < Dim; ++dim) {
     167             :           auto& vars_upper_face = gsl::at(package_data_argvars_upper_face, dim);
     168             :           auto& vars_lower_face = gsl::at(package_data_argvars_lower_face, dim);
     169             : 
     170             :           // Compute fluxes on each faces
     171             :           ScalarAdvection::subcell::compute_fluxes<Dim>(
     172             :               make_not_null(&vars_upper_face));
     173             :           ScalarAdvection::subcell::compute_fluxes<Dim>(
     174             :               make_not_null(&vars_lower_face));
     175             : 
     176             :           // Note that we use the sign convention on the normal vectors to
     177             :           // be compatible with DG.
     178             :           tnsr::i<DataVector, Dim, Frame::Inertial> lower_outward_conormal{
     179             :               num_reconstructed_pts, 0.0};
     180             :           lower_outward_conormal.get(dim) = 1.0;
     181             :           tnsr::i<DataVector, Dim, Frame::Inertial> upper_outward_conormal{
     182             :               num_reconstructed_pts, 0.0};
     183             :           upper_outward_conormal.get(dim) = -1.0;
     184             : 
     185             :           // Compute the packaged data
     186             :           evolution::dg::Actions::detail::dg_package_data<System<Dim>>(
     187             :               make_not_null(&upper_packaged_data),
     188             :               dynamic_cast<const derived_correction&>(boundary_correction),
     189             :               vars_upper_face, upper_outward_conormal, {std::nullopt}, *box,
     190             :               typename derived_correction::dg_package_data_volume_tags{},
     191             :               dg_package_data_argument_tags{});
     192             :           evolution::dg::Actions::detail::dg_package_data<System<Dim>>(
     193             :               make_not_null(&lower_packaged_data),
     194             :               dynamic_cast<const derived_correction&>(boundary_correction),
     195             :               vars_lower_face, lower_outward_conormal, {std::nullopt}, *box,
     196             :               typename derived_correction::dg_package_data_volume_tags{},
     197             :               dg_package_data_argument_tags{});
     198             : 
     199             :           // Now need to check if any of our neighbors are doing DG, because
     200             :           // if so then we need to use whatever boundary data they sent
     201             :           // instead of what we computed locally.
     202             :           //
     203             :           // Note: We could check this beforehand to avoid the extra work of
     204             :           // reconstruction and flux computations at the boundaries.
     205             :           evolution::dg::subcell::correct_package_data<true>(
     206             :               make_not_null(&lower_packaged_data),
     207             :               make_not_null(&upper_packaged_data), dim, element, subcell_mesh,
     208             :               db::get<evolution::dg::Tags::MortarData<Dim>>(*box), 0);
     209             : 
     210             :           // Compute the corrections on the faces. We only need to compute this
     211             :           // once because we can just flip the normal vectors then
     212             :           gsl::at(fd_boundary_corrections, dim)
     213             :               .initialize(num_reconstructed_pts);
     214             :           evolution::dg::subcell::compute_boundary_terms(
     215             :               make_not_null(&gsl::at(fd_boundary_corrections, dim)),
     216             :               dynamic_cast<const derived_correction&>(boundary_correction),
     217             :               upper_packaged_data, lower_packaged_data, db::as_access(*box),
     218             :               typename derived_correction::dg_boundary_terms_volume_tags{});
     219             :         }
     220             :       }
     221             :     });
     222             : 
     223             :     std::array<double, Dim> one_over_delta_xi{};
     224             :     {
     225             :       const tnsr::I<DataVector, Dim, Frame::ElementLogical>&
     226             :           cell_centered_logical_coords =
     227             :               db::get<evolution::dg::subcell::Tags::Coordinates<
     228             :                   Dim, Frame::ElementLogical>>(*box);
     229             : 
     230             :       for (size_t i = 0; i < Dim; ++i) {
     231             :         // Note: assumes isotropic extents
     232             :         gsl::at(one_over_delta_xi, i) =
     233             :             1.0 / (get<0>(cell_centered_logical_coords)[1] -
     234             :                    get<0>(cell_centered_logical_coords)[0]);
     235             :       }
     236             :     }
     237             : 
     238             :     // Now compute the actual time derivative
     239             :     using dt_variables_tag =
     240             :         db::add_tag_prefix<::Tags::dt, typename System<Dim>::variables_tag>;
     241             :     const size_t num_pts = subcell_mesh.number_of_grid_points();
     242             :     db::mutate<dt_variables_tag>(
     243             :         [&num_pts, &fd_boundary_corrections, &subcell_mesh, &one_over_delta_xi](
     244             :             const auto dt_vars_ptr,
     245             :             const InverseJacobian<DataVector, Dim, Frame::ElementLogical,
     246             :                                   Frame::Grid>&
     247             :                 cell_centered_logical_to_grid_inv_jacobian) {
     248             :           dt_vars_ptr->initialize(num_pts, 0.0);
     249             :           auto& dt_u =
     250             :               get<::Tags::dt<::ScalarAdvection::Tags::U>>(*dt_vars_ptr);
     251             : 
     252             :           for (size_t dim = 0; dim < Dim; ++dim) {
     253             :             Scalar<DataVector>& u_correction = get<::ScalarAdvection ::Tags::U>(
     254             :                 gsl::at(fd_boundary_corrections, dim));
     255             :             evolution::dg::subcell::add_cartesian_flux_divergence(
     256             :                 make_not_null(&get(dt_u)), gsl::at(one_over_delta_xi, dim),
     257             :                 cell_centered_logical_to_grid_inv_jacobian.get(dim, dim),
     258             :                 get(u_correction), subcell_mesh.extents(), dim);
     259             :           }
     260             :         },
     261             :         box,
     262             :         db::get<evolution::dg::subcell::fd::Tags::InverseJacobianLogicalToGrid<
     263             :             Dim>>(*box));
     264             :   }
     265             : };
     266             : }  // namespace ScalarAdvection::subcell

Generated by: LCOV version 1.14