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

Generated by: LCOV version 1.14