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 <tuple> 10 : #include <utility> 11 : 12 : #include "DataStructures/DataBox/DataBox.hpp" 13 : #include "DataStructures/DataBox/ObservationBox.hpp" 14 : #include "DataStructures/DataBox/TagTraits.hpp" 15 : #include "Domain/Amr/Flag.hpp" 16 : #include "Domain/Amr/Helpers.hpp" 17 : #include "Domain/Amr/Tags/Flags.hpp" 18 : #include "Domain/Structure/Element.hpp" 19 : #include "Domain/Structure/ElementId.hpp" 20 : #include "Domain/Structure/Neighbors.hpp" 21 : #include "Domain/Tags.hpp" 22 : #include "Parallel/ArrayCollection/IsDgElementCollection.hpp" 23 : #include "Parallel/ArrayCollection/SimpleActionOnElement.hpp" 24 : #include "Parallel/GlobalCache.hpp" 25 : #include "Parallel/Invoke.hpp" 26 : #include "Parallel/Tags/Section.hpp" 27 : #include "ParallelAlgorithms/Actions/GetItemFromDistributedObject.hpp" 28 : #include "ParallelAlgorithms/Amr/Actions/UpdateAmrDecision.hpp" 29 : #include "ParallelAlgorithms/Amr/Criteria/Criterion.hpp" 30 : #include "ParallelAlgorithms/Amr/Criteria/Tags/Criteria.hpp" 31 : #include "ParallelAlgorithms/Amr/Policies/EnforcePolicies.hpp" 32 : #include "ParallelAlgorithms/Amr/Policies/Policies.hpp" 33 : #include "ParallelAlgorithms/Amr/Policies/Tags.hpp" 34 : #include "ParallelAlgorithms/Amr/Projectors/Mesh.hpp" 35 : #include "ParallelAlgorithms/Amr/Tags.hpp" 36 : #include "Utilities/ErrorHandling/Assert.hpp" 37 : #include "Utilities/ErrorHandling/Error.hpp" 38 : #include "Utilities/Gsl.hpp" 39 : #include "Utilities/MakeArray.hpp" 40 : #include "Utilities/PrettyType.hpp" 41 : #include "Utilities/TMPL.hpp" 42 : 43 : /// \cond 44 : namespace tuples { 45 : template <class... Tags> 46 : class TaggedTuple; 47 : } // namespace tuples 48 : /// \endcond 49 : 50 : namespace detail { 51 : template <typename Criterion> 52 : struct get_tags { 53 : using type = typename Criterion::compute_tags_for_observation_box; 54 : }; 55 : 56 : } // namespace detail 57 : 58 : namespace amr::Actions { 59 : /// \brief Evaluates the refinement criteria in order to set the amr::Info of an 60 : /// Element and sends this information to the neighbors of the Element. 61 : /// 62 : /// DataBox: 63 : /// - Uses: 64 : /// * domain::Tags::Element<volume_dim> 65 : /// * amr::Tags::NeighborInfo<volume_dim> 66 : /// * amr::Criteria::Tags::Criteria (from GlobalCache) 67 : /// * amr::Tags::Policies (from GlobalCache) 68 : /// * any tags requested by the refinement criteria 69 : /// - Modifies: 70 : /// * amr::Tags::Info<volume_dim> 71 : /// 72 : /// Invokes: 73 : /// - UpdateAmrDecision on all neighboring Element%s 74 : /// 75 : /// \details 76 : /// - Evaluates each refinement criteria held by amr::Criteria::Tags::Criteria, 77 : /// and in each dimension selects the amr::Flag with the highest 78 : /// priority (i.e the highest integral value). If 79 : /// Metavariables::amr::p_refine_only_in_event is true, only h-refinement 80 : /// criteria will be evaluated 81 : /// - If necessary, changes the refinement decision in order to satisfy the 82 : /// amr::Policies 83 : /// - An Element that is splitting in one dimension is not allowed to join 84 : /// in another dimension. If this is requested by the refinement critiera, 85 : /// the decision to join is changed to do nothing 86 : /// - Checks if any neighbors have sent their AMR decision, and if so, calls 87 : /// amr:::update_amr_decision with the decision of each neighbor in 88 : /// order to see if the current decision needs to be updated 89 : /// - Sends the (possibly updated) decision to all of the neighboring Elements 90 1 : struct EvaluateRefinementCriteria { 91 : template <typename ParallelComponent, typename DbTagList, 92 : typename Metavariables, size_t Dim> 93 0 : static void apply(db::DataBox<DbTagList>& box, 94 : Parallel::GlobalCache<Metavariables>& cache, 95 : const ElementId<Dim>& element_id) { 96 : if constexpr (Metavariables::amr::keep_coarse_grids) { 97 : // Only evaluate the criteria on the finest grid. The other elements keep 98 : // their flags as Undefined and will therefore be skipped by AdjustDomain. 99 : const bool is_finest_grid = 100 : db::get<amr::Tags::ChildIds<Dim>>(box).empty(); 101 : if (not is_finest_grid) { 102 : return; 103 : } 104 : } 105 : 106 : constexpr size_t volume_dim = Metavariables::volume_dim; 107 : auto overall_decision = make_array<volume_dim>(amr::Flag::Undefined); 108 : 109 : using compute_tags = tmpl::remove_duplicates<tmpl::flatten<tmpl::transform< 110 : tmpl::at<typename Metavariables::factory_creation::factory_classes, 111 : Criterion>, 112 : detail::get_tags<tmpl::_1>>>>; 113 : auto observation_box = 114 : make_observation_box<compute_tags>(make_not_null(&box)); 115 : 116 : const auto& refinement_criteria = 117 : db::get<amr::Criteria::Tags::Criteria>(box); 118 : for (const auto& criterion : refinement_criteria) { 119 : if (Metavariables::amr::p_refine_only_in_event and 120 : criterion->type() == amr::Criteria::Type::p) { 121 : continue; 122 : } 123 : auto decision = criterion->evaluate(observation_box, cache, element_id); 124 : if constexpr (Metavariables::amr::p_refine_only_in_event) { 125 : ASSERT(alg::none_of(decision, 126 : [](amr::Flag flag) { 127 : return flag == amr::Flag::IncreaseResolution or 128 : flag == amr::Flag::DecreaseResolution; 129 : }), 130 : "The criterion '" 131 : << typeid(*criterion).name() 132 : << "' requested p-refinement, but claims to be " 133 : "for h-refinement."); 134 : } 135 : for (size_t d = 0; d < volume_dim; ++d) { 136 : overall_decision[d] = std::max(overall_decision[d], decision[d]); 137 : } 138 : } 139 : 140 : // If no refinement criteria were called, then set flag to do nothing 141 : for (size_t d = 0; d < volume_dim; ++d) { 142 : if (overall_decision[d] == amr::Flag::Undefined) { 143 : overall_decision[d] = amr::Flag::DoNothing; 144 : } 145 : } 146 : 147 : const auto& policies = db::get<amr::Tags::Policies>(box); 148 : amr::enforce_policies(make_not_null(&overall_decision), policies, 149 : element_id, 150 : get<::domain::Tags::Mesh<volume_dim>>(box)); 151 : 152 : // An element cannot join if it is splitting in another dimension. 153 : // Update the flags now before sending to neighbors as each time 154 : // a flag is changed by UpdateAmrDecision, it sends the new flags 155 : // to its neighbors. So updating now will save some commmunication. 156 : amr::prevent_element_from_joining_while_splitting( 157 : make_not_null(&overall_decision)); 158 : 159 : // Check if we received any neighbor flags prior to determining our own 160 : // flags. If yes, then possible update our flags (e.g. sibling doesn't want 161 : // to join, maintain 2:1 balance, etc.) 162 : const auto& my_element = get<::domain::Tags::Element<volume_dim>>(box); 163 : const auto& my_neighbors_amr_info = 164 : get<amr::Tags::NeighborInfo<volume_dim>>(box); 165 : if (not my_neighbors_amr_info.empty()) { 166 : for (const auto& [neighbor_id, neighbor_amr_info] : 167 : my_neighbors_amr_info) { 168 : amr::update_amr_decision( 169 : make_not_null(&overall_decision), my_element, neighbor_id, 170 : neighbor_amr_info.flags, 171 : policies.enforce_two_to_one_balance_in_normal_direction()); 172 : } 173 : } 174 : 175 : const auto new_mesh = amr::projectors::new_mesh( 176 : get<::domain::Tags::Mesh<volume_dim>>(box), overall_decision, 177 : my_element, my_neighbors_amr_info); 178 : 179 : db::mutate<amr::Tags::Info<Metavariables::volume_dim>>( 180 : [&overall_decision, 181 : &new_mesh](const gsl::not_null<amr::Info<volume_dim>*> amr_info) { 182 : amr_info->flags = overall_decision; 183 : amr_info->new_mesh = new_mesh; 184 : }, 185 : make_not_null(&box)); 186 : 187 : auto& amr_element_array = 188 : Parallel::get_parallel_component<ParallelComponent>(cache); 189 : 190 : const amr::Info<Metavariables::volume_dim>& my_info = 191 : get<amr::Tags::Info<volume_dim>>(box); 192 : for (const auto& [direction, neighbors] : my_element.neighbors()) { 193 : (void)direction; 194 : for (const auto& neighbor_id : neighbors.ids()) { 195 : if constexpr (Parallel::is_dg_element_collection_v<ParallelComponent>) { 196 : const auto neighbor_location = static_cast<int>( 197 : Parallel::local_synchronous_action< 198 : Parallel::Actions::GetItemFromDistributedOject< 199 : Parallel::Tags::ElementLocations<volume_dim>>>( 200 : Parallel::get_parallel_component<ParallelComponent>(cache)) 201 : ->at(neighbor_id)); 202 : Parallel::threaded_action<Parallel::Actions::SimpleActionOnElement< 203 : UpdateAmrDecision, true>>(amr_element_array[neighbor_location], 204 : neighbor_id, element_id, my_info); 205 : } else { 206 : Parallel::simple_action<UpdateAmrDecision>( 207 : amr_element_array[neighbor_id], element_id, my_info); 208 : } 209 : } 210 : } 211 : } 212 : }; 213 : } // namespace amr::Actions