SpECTRE Documentation Coverage Report
Current view: top level - Evolution/Systems/Burgers/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/Structure/Element.hpp"
      20             : #include "Domain/Tags.hpp"
      21             : #include "Evolution/BoundaryCorrection.hpp"
      22             : #include "Evolution/BoundaryCorrectionTags.hpp"
      23             : #include "Evolution/DgSubcell/CartesianFluxDivergence.hpp"
      24             : #include "Evolution/DgSubcell/ComputeBoundaryTerms.hpp"
      25             : #include "Evolution/DgSubcell/CorrectPackagedData.hpp"
      26             : #include "Evolution/DgSubcell/SubcellOptions.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/DiscontinuousGalerkin/Actions/PackageDataImpl.hpp"
      31             : #include "Evolution/DiscontinuousGalerkin/MortarTags.hpp"
      32             : #include "Evolution/Systems/Burgers/FiniteDifference/BoundaryConditionGhostData.hpp"
      33             : #include "Evolution/Systems/Burgers/FiniteDifference/Reconstructor.hpp"
      34             : #include "Evolution/Systems/Burgers/FiniteDifference/Tags.hpp"
      35             : #include "Evolution/Systems/Burgers/Fluxes.hpp"
      36             : #include "Evolution/Systems/Burgers/Subcell/ComputeFluxes.hpp"
      37             : #include "Evolution/Systems/Burgers/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 Burgers::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           1 : struct TimeDerivative {
      55             :   template <typename DbTagsList>
      56           0 :   static void apply(const gsl::not_null<db::DataBox<DbTagsList>*> box) {
      57             :     using metavariables =
      58             :         typename std::decay_t<decltype(db::get<Parallel::Tags::Metavariables>(
      59             :             *box))>;
      60             :     ASSERT((db::get<::domain::CoordinateMaps::Tags::CoordinateMap<
      61             :                 1, Frame::Grid, Frame::Inertial>>(*box))
      62             :                .is_identity(),
      63             :            "Do not yet support moving mesh with DG-subcell.");
      64             :     const Element<1>& element = db::get<domain::Tags::Element<1>>(*box);
      65             : 
      66             :     const bool element_is_interior = element.external_boundaries().size() == 0;
      67             :     constexpr bool subcell_enabled_at_external_boundary =
      68             :         metavariables::SubcellOptions::subcell_enabled_at_external_boundary;
      69             : 
      70             :     ASSERT(element_is_interior or subcell_enabled_at_external_boundary,
      71             :            "Subcell time derivative is called at a boundary element while "
      72             :            "using subcell is disabled at external boundaries."
      73             :            "ElementID "
      74             :                << element.id());
      75             : 
      76             :     using evolved_vars_tags = typename System::variables_tag::tags_list;
      77             :     using fluxes_tags = typename Fluxes::return_tags;
      78             : 
      79             :     // The copy of Mesh is intentional to avoid a GCC-7 internal compiler error.
      80             :     const Mesh<1> subcell_mesh =
      81             :         db::get<evolution::dg::subcell::Tags::Mesh<1>>(*box);
      82             :     const size_t num_reconstructed_pts =
      83             :         subcell_mesh.number_of_grid_points() + 1;
      84             : 
      85             :     const Burgers::fd::Reconstructor& recons =
      86             :         db::get<Burgers::fd::Tags::Reconstructor>(*box);
      87             : 
      88             :     const auto& boundary_correction =
      89             :         db::get<evolution::Tags::BoundaryCorrection>(*box);
      90             :     using derived_boundary_corrections =
      91             :         tmpl::at<typename metavariables::factory_creation::factory_classes,
      92             :                  evolution::BoundaryCorrection>;
      93             :     // Variables to store the boundary correction terms on FD subinterfaces
      94             :     std::array<Variables<evolved_vars_tags>, 1> fd_boundary_corrections{};
      95             : 
      96             :     // If the element has external boundaries and subcell is enabled for
      97             :     // boundary elements, compute FD ghost data with a given boundary condition.
      98             :     if constexpr (subcell_enabled_at_external_boundary) {
      99             :       if (element.external_boundaries().size() != 0) {
     100             :         fd::BoundaryConditionGhostData::apply(box, element, recons);
     101             :       }
     102             :     }
     103             : 
     104             :     // package the data and compute the boundary correction
     105             :     tmpl::for_each<derived_boundary_corrections>(
     106             :         [&boundary_correction, &box, &element, &fd_boundary_corrections,
     107             :          &num_reconstructed_pts, &recons,
     108             :          &subcell_mesh](auto 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_argument_tags =
     115             :                 tmpl::append<evolved_vars_tags, fluxes_tags>;
     116             : 
     117             :             // Variables that need to be reconstructed for dg_package_data()
     118             :             auto package_data_argvars_lower_face =
     119             :                 make_array<1>(Variables<dg_package_data_argument_tags>(
     120             :                     num_reconstructed_pts));
     121             :             auto package_data_argvars_upper_face =
     122             :                 make_array<1>(Variables<dg_package_data_argument_tags>(
     123             :                     num_reconstructed_pts));
     124             : 
     125             :             // Reconstruct the fields on interfaces
     126             :             call_with_dynamic_type<
     127             :                 void, typename Burgers::fd::Reconstructor::creatable_classes>(
     128             :                 &recons,
     129             :                 [&box, &package_data_argvars_lower_face,
     130             :                  &package_data_argvars_upper_face](const auto& reconstructor) {
     131             :                   db::apply<typename std::decay_t<decltype(
     132             :                       *reconstructor)>::reconstruction_argument_tags>(
     133             :                       [&package_data_argvars_lower_face,
     134             :                        &package_data_argvars_upper_face,
     135             :                        &reconstructor](const auto&... args) {
     136             :                         reconstructor->reconstruct(
     137             :                             make_not_null(&package_data_argvars_lower_face),
     138             :                             make_not_null(&package_data_argvars_upper_face),
     139             :                             args...);
     140             :                       },
     141             :                       *box);
     142             :                 });
     143             : 
     144             :             // Variables to store packaged data. Allocate outside of loop to
     145             :             // reduce allocations
     146             :             Variables<dg_package_field_tags> upper_packaged_data{
     147             :                 num_reconstructed_pts};
     148             :             Variables<dg_package_field_tags> lower_packaged_data{
     149             :                 num_reconstructed_pts};
     150             : 
     151             :             auto& vars_upper_face = gsl::at(package_data_argvars_upper_face, 0);
     152             :             auto& vars_lower_face = gsl::at(package_data_argvars_lower_face, 0);
     153             : 
     154             :             // Compute fluxes on each faces
     155             :             Burgers::subcell::compute_fluxes(make_not_null(&vars_upper_face));
     156             :             Burgers::subcell::compute_fluxes(make_not_null(&vars_lower_face));
     157             : 
     158             :             // Note that we use the sign convention on the normal vectors to
     159             :             // be compatible with DG.
     160             :             tnsr::i<DataVector, 1, Frame::Inertial> lower_outward_conormal{
     161             :                 num_reconstructed_pts, 0.0};
     162             :             lower_outward_conormal.get(0) = 1.0;
     163             :             tnsr::i<DataVector, 1, Frame::Inertial> upper_outward_conormal{
     164             :                 num_reconstructed_pts, 0.0};
     165             :             upper_outward_conormal.get(0) = -1.0;
     166             : 
     167             :             // Compute the packaged data
     168             :             evolution::dg::Actions::detail::dg_package_data<System>(
     169             :                 make_not_null(&upper_packaged_data),
     170             :                 dynamic_cast<const derived_correction&>(boundary_correction),
     171             :                 vars_upper_face, upper_outward_conormal, {std::nullopt}, *box,
     172             :                 typename derived_correction::dg_package_data_volume_tags{},
     173             :                 dg_package_data_argument_tags{});
     174             :             evolution::dg::Actions::detail::dg_package_data<System>(
     175             :                 make_not_null(&lower_packaged_data),
     176             :                 dynamic_cast<const derived_correction&>(boundary_correction),
     177             :                 vars_lower_face, lower_outward_conormal, {std::nullopt}, *box,
     178             :                 typename derived_correction::dg_package_data_volume_tags{},
     179             :                 dg_package_data_argument_tags{});
     180             : 
     181             :             // Now need to check if any of our neighbors are doing DG, because
     182             :             // if so then we need to use whatever boundary data they sent
     183             :             // instead of what we computed locally.
     184             :             //
     185             :             // Note: We could check this beforehand to avoid the extra work of
     186             :             // reconstruction and flux computations at the boundaries.
     187             :             evolution::dg::subcell::correct_package_data<true>(
     188             :                 make_not_null(&lower_packaged_data),
     189             :                 make_not_null(&upper_packaged_data), 0, element, subcell_mesh,
     190             :                 db::get<evolution::dg::Tags::MortarData<1>>(*box), 0);
     191             : 
     192             :             // Compute the corrections on the faces. We only need to compute
     193             :             // this once because we can just flip the normal vectors then
     194             :             gsl::at(fd_boundary_corrections, 0)
     195             :                 .initialize(num_reconstructed_pts);
     196             :             evolution::dg::subcell::compute_boundary_terms(
     197             :                 make_not_null(&gsl::at(fd_boundary_corrections, 0)),
     198             :                 dynamic_cast<const derived_correction&>(boundary_correction),
     199             :                 upper_packaged_data, lower_packaged_data, db::as_access(*box),
     200             :                 typename derived_correction::dg_boundary_terms_volume_tags{});
     201             :           }
     202             :         });
     203             : 
     204             :     std::array<double, 1> one_over_delta_xi{};
     205             :     {
     206             :       const tnsr::I<DataVector, 1, Frame::ElementLogical>&
     207             :           cell_centered_logical_coords =
     208             :               db::get<evolution::dg::subcell::Tags::Coordinates<
     209             :                   1, Frame::ElementLogical>>(*box);
     210             : 
     211             :       gsl::at(one_over_delta_xi, 0) =
     212             :           1.0 / (get<0>(cell_centered_logical_coords)[1] -
     213             :                  get<0>(cell_centered_logical_coords)[0]);
     214             :     }
     215             : 
     216             :     // Now compute the actual time derivative
     217             :     using dt_variables_tag =
     218             :         db::add_tag_prefix<::Tags::dt, typename System::variables_tag>;
     219             :     const size_t num_pts = subcell_mesh.number_of_grid_points();
     220             :     db::mutate<dt_variables_tag>(
     221             :         [&num_pts, &fd_boundary_corrections, &subcell_mesh, &one_over_delta_xi](
     222             :             const auto dt_vars_ptr,
     223             :             const InverseJacobian<DataVector, 1, Frame::ElementLogical,
     224             :                                   Frame::Grid>&
     225             :                 cell_centered_logical_to_grid_inv_jacobian) {
     226             :           dt_vars_ptr->initialize(num_pts, 0.0);
     227             :           auto& dt_u = get<::Tags::dt<::Burgers::Tags::U>>(*dt_vars_ptr);
     228             : 
     229             :           Scalar<DataVector>& u_correction =
     230             :               get<::Burgers::Tags::U>(gsl::at(fd_boundary_corrections, 0));
     231             :           evolution::dg::subcell::add_cartesian_flux_divergence(
     232             :               make_not_null(&get(dt_u)), gsl::at(one_over_delta_xi, 0),
     233             :               cell_centered_logical_to_grid_inv_jacobian.get(0, 0),
     234             :               get(u_correction), subcell_mesh.extents(), 0);
     235             :         },
     236             :         box,
     237             :         db::get<
     238             :             evolution::dg::subcell::fd::Tags::InverseJacobianLogicalToGrid<1>>(
     239             :             *box));
     240             :   }
     241             : };
     242             : }  // namespace Burgers::subcell

Generated by: LCOV version 1.14