SpECTRE Documentation Coverage Report
Current view: top level - ParallelAlgorithms/Amr/Criteria - TruncationError.hpp Coverage Total Hit
Commit: 0d1d717af84a81fb1284a20c4013708fe85cc2f0 Lines: 3.4 % 29 1
Test Date: 2026-10-22 17:29:42
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 <pup.h>
       9              : #include <string>
      10              : #include <vector>
      11              : 
      12              : #include "DataStructures/DataBox/DataBox.hpp"
      13              : #include "DataStructures/DataBox/DataBoxTag.hpp"
      14              : #include "DataStructures/DataBox/ValidateSelection.hpp"
      15              : #include "DataStructures/DataVector.hpp"
      16              : #include "DataStructures/Tensor/Tensor.hpp"
      17              : #include "Domain/Amr/Flag.hpp"
      18              : #include "Domain/Tags.hpp"
      19              : #include "NumericalAlgorithms/Spectral/Mesh.hpp"
      20              : #include "Options/Context.hpp"
      21              : #include "Options/ParseError.hpp"
      22              : #include "Options/String.hpp"
      23              : #include "ParallelAlgorithms/Amr/Criteria/Criterion.hpp"
      24              : #include "ParallelAlgorithms/Amr/Criteria/Type.hpp"
      25              : #include "Utilities/Algorithm.hpp"
      26              : #include "Utilities/TMPL.hpp"
      27              : 
      28              : /// \cond
      29              : template <size_t>
      30              : class ElementId;
      31              : /// \endcond
      32              : 
      33              : namespace amr::Criteria {
      34              : 
      35              : namespace TruncationError_detail {
      36              : /*!
      37              :  * \brief Apply the truncation error criterion to a single tensor component
      38              :  *
      39              :  * The `result` is the current decision in each dimension based on the previous
      40              :  * tensor components. This function will update the flags if necessary. It takes
      41              :  * the "max" of the current and new flags, where the "highest" flag is
      42              :  * `Flag::IncreaseResolution`, followed by `Flag::DoNothing`, and then
      43              :  * `Flag::DecreaseResolution`.
      44              :  */
      45              : template <typename ValueType, size_t Dim>
      46              : void max_over_components(
      47              :     gsl::not_null<std::array<Flag, Dim>*> result,
      48              :     const gsl::not_null<std::array<DataVector, Dim>*> power_monitors_buffer,
      49              :     const ValueType& tensor_component, const Mesh<Dim>& mesh,
      50              :     double target_abs_truncation_error, double target_rel_truncation_error);
      51              : }  // namespace TruncationError_detail
      52              : 
      53              : /*!
      54              :  * \brief Refine the grid towards the target truncation error
      55              :  *
      56              :  * - If any tensor component has a truncation error above the target value, the
      57              :  *   element will be p-refined.
      58              :  * - If all tensor components still satisfy the target even with one mode
      59              :  *   removed, the element will be p-coarsened.
      60              :  *
      61              :  * For details on how the truncation error is computed see
      62              :  * `PowerMonitors::truncation_error`.
      63              :  *
      64              :  * \tparam Dim Spatial dimension of the grid
      65              :  * \tparam TensorTags List of tags of the tensors to be monitored
      66              :  */
      67              : template <size_t Dim, typename TensorTags>
      68            1 : class TruncationError : public Criterion {
      69              :  public:
      70            0 :   struct VariablesToMonitor {
      71            0 :     using type = std::vector<std::string>;
      72            0 :     static constexpr Options::String help = {
      73              :         "The tensors to monitor the truncation error of."};
      74            0 :     static size_t lower_bound_on_size() { return 1; }
      75              :   };
      76            0 :   struct AbsoluteTargetTruncationError {
      77            0 :     static std::string name() { return "AbsoluteTarget"; }
      78            0 :     using type = double;
      79            0 :     static type lower_bound() { return 0.0; }
      80            0 :     static constexpr Options::String help = {
      81              :         "The absolute target truncation error."};
      82              :   };
      83            0 :   struct RelativeTargetTruncationError {
      84            0 :     static std::string name() { return "RelativeTarget"; }
      85            0 :     using type = double;
      86            0 :     static type lower_bound() { return 0.0; }
      87            0 :     static constexpr Options::String help = {
      88              :         "The relative target truncation error."};
      89              :   };
      90              : 
      91            0 :   using options = tmpl::list<VariablesToMonitor, AbsoluteTargetTruncationError,
      92              :                              RelativeTargetTruncationError>;
      93              : 
      94            0 :   static constexpr Options::String help = {
      95              :       "Refine the grid towards the target truncation error"};
      96              : 
      97            0 :   TruncationError() = default;
      98              : 
      99            0 :   TruncationError(std::vector<std::string> vars_to_monitor,
     100              :                   double target_abs_truncation_error,
     101              :                   double target_rel_truncation_error,
     102              :                   const Options::Context& context = {});
     103              : 
     104              :   /// \cond
     105              :   explicit TruncationError(CkMigrateMessage* msg);
     106              :   using PUP::able::register_constructor;
     107              :   WRAPPED_PUPable_decl_template(TruncationError);  // NOLINT
     108              :   /// \endcond
     109              : 
     110            0 :   Type type() override { return Type::p; }
     111              : 
     112            0 :   std::string observation_name() override { return "TruncationError"; }
     113              : 
     114            0 :   using compute_tags_for_observation_box = tmpl::list<>;
     115              : 
     116            0 :   using argument_tags = tmpl::list<::Tags::DataBox>;
     117              : 
     118              :   template <typename DbTagsList, typename Metavariables>
     119            0 :   std::array<Flag, Dim> operator()(const db::DataBox<DbTagsList>& box,
     120              :                                    Parallel::GlobalCache<Metavariables>& cache,
     121              :                                    const ElementId<Dim>& element_id) const;
     122              : 
     123            0 :   void pup(PUP::er& p) override;
     124              : 
     125              :  private:
     126            0 :   std::vector<std::string> vars_to_monitor_{};
     127            0 :   double target_abs_truncation_error_{};
     128            0 :   double target_rel_truncation_error_{};
     129              : };
     130              : 
     131              : // Out-of-line definitions
     132              : /// \cond
     133              : 
     134              : template <size_t Dim, typename TensorTags>
     135              : TruncationError<Dim, TensorTags>::TruncationError(
     136              :     std::vector<std::string> vars_to_monitor,
     137              :     const double target_abs_truncation_error,
     138              :     const double target_rel_truncation_error, const Options::Context& context)
     139              :     : vars_to_monitor_(std::move(vars_to_monitor)),
     140              :       target_abs_truncation_error_(target_abs_truncation_error),
     141              :       target_rel_truncation_error_(target_rel_truncation_error) {
     142              :   db::validate_selection<TensorTags>(vars_to_monitor_, context);
     143              : }
     144              : 
     145              : template <size_t Dim, typename TensorTags>
     146              : TruncationError<Dim, TensorTags>::TruncationError(CkMigrateMessage* msg)
     147              :     : Criterion(msg) {}
     148              : 
     149              : template <size_t Dim, typename TensorTags>
     150              : template <typename DbTagsList, typename Metavariables>
     151              : std::array<Flag, Dim> TruncationError<Dim, TensorTags>::operator()(
     152              :     const db::DataBox<DbTagsList>& box,
     153              :     Parallel::GlobalCache<Metavariables>& /*cache*/,
     154              :     const ElementId<Dim>& /*element_id*/) const {
     155              :   auto result = make_array<Dim>(Flag::Undefined);
     156              :   const auto& mesh = db::get<domain::Tags::Mesh<Dim>>(box);
     157              :   std::array<DataVector, Dim> power_monitors_buffer{};
     158              :   // Check all tensors and all tensor components in turn
     159              :   tmpl::for_each<TensorTags>(
     160              :       [&result, &box, &mesh, &power_monitors_buffer, this](const auto tag_v) {
     161              :         // Stop if we have already decided to refine every dimension
     162              :         if (result == make_array<Dim>(Flag::IncreaseResolution)) {
     163              :           return;
     164              :         }
     165              :         using tag = tmpl::type_from<std::decay_t<decltype(tag_v)>>;
     166              :         const std::string tag_name = db::tag_name<tag>();
     167              :         // Skip if this tensor is not being monitored
     168              :         if (not alg::found(vars_to_monitor_, tag_name)) {
     169              :           return;
     170              :         }
     171              :         const auto& tensor = db::get<tag>(box);
     172              :         for (const auto& tensor_component : tensor) {
     173              :           TruncationError_detail::max_over_components(
     174              :               make_not_null(&result), make_not_null(&power_monitors_buffer),
     175              :               tensor_component, mesh, target_abs_truncation_error_,
     176              :               target_rel_truncation_error_);
     177              :         }
     178              :       });
     179              :   return result;
     180              : }
     181              : 
     182              : template <size_t Dim, typename TensorTags>
     183              : void TruncationError<Dim, TensorTags>::pup(PUP::er& p) {
     184              :   p | vars_to_monitor_;
     185              :   p | target_abs_truncation_error_;
     186              :   p | target_rel_truncation_error_;
     187              : }
     188              : 
     189              : template <size_t Dim, typename TensorTags>
     190              : PUP::able::PUP_ID TruncationError<Dim, TensorTags>::my_PUP_ID = 0;  // NOLINT
     191              : /// \endcond
     192              : 
     193              : }  // namespace amr::Criteria
        

Generated by: LCOV version 2.0-1