SpECTRE Documentation Coverage Report
Current view: top level - ParallelAlgorithms/Amr/Events - RefineMesh.hpp Hit Total Coverage
Commit: a8efe75339f4781ca06d43fed14c40144d5e8a08 Lines: 2 13 15.4 %
Date: 2024-10-17 21:19:21
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 <algorithm>
       7             : #include <array>
       8             : #include <cstddef>
       9             : #include <typeinfo>
      10             : #include <utility>
      11             : 
      12             : #include "DataStructures/DataBox/DataBox.hpp"
      13             : #include "DataStructures/DataBox/ObservationBox.hpp"
      14             : #include "Domain/Amr/Flag.hpp"
      15             : #include "Domain/Structure/ElementId.hpp"
      16             : #include "Domain/Tags.hpp"
      17             : #include "IO/Logging/Tags.hpp"
      18             : #include "IO/Logging/Verbosity.hpp"
      19             : #include "Options/String.hpp"
      20             : #include "Parallel/GlobalCache.hpp"
      21             : #include "Parallel/Printf/Printf.hpp"
      22             : #include "ParallelAlgorithms/Amr/Criteria/Criterion.hpp"
      23             : #include "ParallelAlgorithms/Amr/Criteria/Tags/Criteria.hpp"
      24             : #include "ParallelAlgorithms/Amr/Projectors/Mesh.hpp"
      25             : #include "ParallelAlgorithms/Amr/Tags.hpp"
      26             : #include "ParallelAlgorithms/EventsAndTriggers/Event.hpp"
      27             : #include "Utilities/Algorithm.hpp"
      28             : #include "Utilities/ErrorHandling/Error.hpp"
      29             : #include "Utilities/Gsl.hpp"
      30             : #include "Utilities/MakeArray.hpp"
      31             : #include "Utilities/Serialization/CharmPupable.hpp"
      32             : #include "Utilities/TMPL.hpp"
      33             : 
      34             : namespace PUP {
      35             : class er;
      36             : }  // namespace PUP
      37             : 
      38             : namespace amr::Events {
      39             : namespace detail {
      40             : template <typename Criterion>
      41             : struct get_tags {
      42             :   using type = typename Criterion::compute_tags_for_observation_box;
      43             : };
      44             : 
      45             : }  // namespace detail
      46             : /// \ingroup AmrGroup
      47             : /// \brief Performs p-refinement on the domain
      48             : ///
      49             : /// \details
      50             : /// - Loops over all refinement criteria specified in the
      51             : ///   input file, ignoring any requests to join or split the Element.
      52             : ///   If no valid p-refinement decision is requested, no change is
      53             : ///   made to the Element.
      54             : /// - Updates the Mesh and all return tags of Metavariables::amr::projectors
      55             : ///
      56             : /// \warning This does not communicate the new Mesh to its neighbors, nor does
      57             : /// it update ::domain::Tags::NeighborMesh
      58           1 : class RefineMesh : public Event {
      59             :  public:
      60             :   /// \cond
      61             :   explicit RefineMesh(CkMigrateMessage* m);
      62             :   using PUP::able::register_constructor;
      63             :   WRAPPED_PUPable_decl_template(RefineMesh);  // NOLINT
      64             :   /// \endcond
      65             : 
      66           0 :   using options = tmpl::list<>;
      67           0 :   static constexpr Options::String help = {"Perform p-refinement"};
      68             : 
      69           0 :   RefineMesh();
      70             : 
      71           0 :   using compute_tags_for_observation_box = tmpl::list<>;
      72           0 :   using return_tags = tmpl::list<::Tags::DataBox>;
      73           0 :   using argument_tags = tmpl::list<>;
      74             : 
      75             :   template <typename DbTags, typename Metavariables, typename Component>
      76           0 :   void operator()(const gsl::not_null<db::DataBox<DbTags>*> box,
      77             :                   Parallel::GlobalCache<Metavariables>& cache,
      78             :                   const ElementId<Metavariables::volume_dim>& element_id,
      79             :                   const Component* const /*meta*/,
      80             :                   const ObservationValue& /*observation_value*/) const {
      81             :     // Evaluate AMR refinement criteria
      82             :     // NOTE: This evaluates all criteria.  In the future this could be
      83             :     // restricted to evaluate only those criteria that do p-refinement that can
      84             :     // be evaluated locally (i.e. do not need neighbor information that is
      85             :     // already in the DataBox)
      86             :     constexpr size_t volume_dim = Metavariables::volume_dim;
      87             :     auto overall_decision = make_array<volume_dim>(amr::Flag::Undefined);
      88             : 
      89             :     using compute_tags = tmpl::remove_duplicates<tmpl::flatten<tmpl::transform<
      90             :         tmpl::at<typename Metavariables::factory_creation::factory_classes,
      91             :                  Criterion>,
      92             :         detail::get_tags<tmpl::_1>>>>;
      93             :     auto observation_box = make_observation_box<compute_tags>(box);
      94             : 
      95             :     const auto& refinement_criteria =
      96             :         db::get<amr::Criteria::Tags::Criteria>(*box);
      97             :     for (const auto& criterion : refinement_criteria) {
      98             :       auto decision = criterion->evaluate(observation_box, cache, element_id);
      99             :       for (size_t d = 0; d < volume_dim; ++d) {
     100             :         // Ignore h-refinement decisions
     101             :         if (decision[d] == amr::Flag::Split and
     102             :             decision[d] == amr::Flag::Join) {
     103             :           ERROR("The criterion '" << typeid(*criterion).name()
     104             :                                   << "' requested h-refinement, but RefineMesh "
     105             :                                      "only works for p-refinement.");
     106             :         } else {
     107             :           overall_decision[d] = std::max(overall_decision[d], decision[d]);
     108             :         }
     109             :       }
     110             :     }
     111             :     // If no refinement criteria requested p-refinement, then set flag to
     112             :     // do nothing
     113             :     for (size_t d = 0; d < volume_dim; ++d) {
     114             :       if (overall_decision[d] == amr::Flag::Undefined) {
     115             :         overall_decision[d] = amr::Flag::DoNothing;
     116             :       }
     117             :     }
     118             : 
     119             :     // Now p-refine
     120             :     const auto old_mesh_and_element =
     121             :         std::make_pair(db::get<::domain::Tags::Mesh<volume_dim>>(*box),
     122             :                        db::get<::domain::Tags::Element<volume_dim>>(*box));
     123             :     const auto& old_mesh = old_mesh_and_element.first;
     124             :     const auto& verbosity =
     125             :         db::get<logging::Tags::Verbosity<amr::OptionTags::AmrGroup>>(*box);
     126             : 
     127             :     if (alg::any_of(overall_decision, [](amr::Flag flag) {
     128             :           return (flag == amr::Flag::IncreaseResolution or
     129             :                   flag == amr::Flag::DecreaseResolution);
     130             :         })) {
     131             :       db::mutate<::domain::Tags::Mesh<volume_dim>>(
     132             :           [&old_mesh,
     133             :            &overall_decision](const gsl::not_null<Mesh<volume_dim>*> mesh) {
     134             :             *mesh = amr::projectors::mesh(old_mesh, overall_decision);
     135             :           },
     136             :           box);
     137             : 
     138             :       if (verbosity >= Verbosity::Debug) {
     139             :         Parallel::printf(
     140             :             "Increasing order of element %s: %s -> %s\n", element_id,
     141             :             old_mesh.extents(),
     142             :             db::get<::domain::Tags::Mesh<volume_dim>>(*box).extents());
     143             :       }
     144             : 
     145             :       // Run the projectors.
     146             :       tmpl::for_each<typename Metavariables::amr::projectors>(
     147             :           [&box, &old_mesh_and_element](auto projector_v) {
     148             :             using projector = typename decltype(projector_v)::type;
     149             :             try {
     150             :               db::mutate_apply<projector>(box, old_mesh_and_element);
     151             :             } catch (std::exception& e) {
     152             :               ERROR("Error in AMR projector '"
     153             :                     << pretty_type::get_name<projector>() << "':\n"
     154             :                     << e.what());
     155             :             }
     156             :           });
     157             :     }
     158             :   }
     159             : 
     160           0 :   using is_ready_argument_tags = tmpl::list<>;
     161             : 
     162             :   template <typename Metavariables, typename ArrayIndex, typename Component>
     163           0 :   bool is_ready(Parallel::GlobalCache<Metavariables>& /*cache*/,
     164             :                 const ArrayIndex& /*array_index*/,
     165             :                 const Component* const /*meta*/) const {
     166             :     return true;
     167             :   }
     168             : 
     169           1 :   bool needs_evolved_variables() const override { return true; }
     170             : 
     171             :   // NOLINTNEXTLINE(google-runtime-references)
     172           0 :   void pup(PUP::er& p) override;
     173             : };
     174             : }  // namespace amr::Events

Generated by: LCOV version 1.14