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 : // Evaluate criteria on blocks that have AMR enabled, and "do nothing" on 110 : // the other blocks. 111 : // Note that blocks that do not have AMR enabled can still be refined to 112 : // satisfy 2:1 balance. When this happens, they won't coarsen back since "do 113 : // nothing" has higher priority than coarsening. This behavior can be 114 : // changed if needed. 115 : const auto& amr_blocks = db::get<amr::Tags::AmrBlocks<volume_dim>>(box); 116 : if (not amr_blocks.has_value() or 117 : amr_blocks->contains(element_id.block_id())) { 118 : using compute_tags = 119 : tmpl::remove_duplicates<tmpl::flatten<tmpl::transform< 120 : tmpl::at< 121 : typename Metavariables::factory_creation::factory_classes, 122 : Criterion>, 123 : detail::get_tags<tmpl::_1>>>>; 124 : auto observation_box = 125 : make_observation_box<compute_tags>(make_not_null(&box)); 126 : 127 : const auto& refinement_criteria = 128 : db::get<amr::Criteria::Tags::Criteria>(box); 129 : for (const auto& criterion : refinement_criteria) { 130 : if (Metavariables::amr::p_refine_only_in_event and 131 : criterion->type() == amr::Criteria::Type::p) { 132 : continue; 133 : } 134 : auto decision = criterion->evaluate(observation_box, cache, element_id); 135 : if constexpr (Metavariables::amr::p_refine_only_in_event) { 136 : ASSERT(alg::none_of(decision, 137 : [](amr::Flag flag) { 138 : return flag == amr::Flag::IncreaseResolution or 139 : flag == amr::Flag::DecreaseResolution; 140 : }), 141 : "The criterion '" 142 : << typeid(*criterion).name() 143 : << "' requested p-refinement, but claims to be " 144 : "for h-refinement."); 145 : } 146 : for (size_t d = 0; d < volume_dim; ++d) { 147 : overall_decision[d] = std::max(overall_decision[d], decision[d]); 148 : } 149 : } 150 : } // amr_blocks.contains(element_id.block_id()) 151 : 152 : // If no refinement criteria were called, then set flag to do nothing 153 : for (size_t d = 0; d < volume_dim; ++d) { 154 : if (overall_decision[d] == amr::Flag::Undefined) { 155 : overall_decision[d] = amr::Flag::DoNothing; 156 : } 157 : } 158 : 159 : const auto& policies = db::get<amr::Tags::Policies>(box); 160 : amr::enforce_policies(make_not_null(&overall_decision), policies, 161 : element_id, 162 : get<::domain::Tags::Mesh<volume_dim>>(box)); 163 : 164 : // An element cannot join if it is splitting in another dimension. 165 : // Update the flags now before sending to neighbors as each time 166 : // a flag is changed by UpdateAmrDecision, it sends the new flags 167 : // to its neighbors. So updating now will save some commmunication. 168 : amr::prevent_element_from_joining_while_splitting( 169 : make_not_null(&overall_decision)); 170 : 171 : // Check if we received any neighbor flags prior to determining our own 172 : // flags. If yes, then possible update our flags (e.g. sibling doesn't want 173 : // to join, maintain 2:1 balance, etc.) 174 : const auto& my_element = get<::domain::Tags::Element<volume_dim>>(box); 175 : const auto& my_neighbors_amr_info = 176 : get<amr::Tags::NeighborInfo<volume_dim>>(box); 177 : if (not my_neighbors_amr_info.empty()) { 178 : for (const auto& [neighbor_id, neighbor_amr_info] : 179 : my_neighbors_amr_info) { 180 : amr::update_amr_decision( 181 : make_not_null(&overall_decision), my_element, neighbor_id, 182 : neighbor_amr_info.flags, 183 : policies.enforce_two_to_one_balance_in_normal_direction()); 184 : } 185 : } 186 : 187 : const auto new_mesh = amr::projectors::new_mesh( 188 : get<::domain::Tags::Mesh<volume_dim>>(box), overall_decision, 189 : my_element, my_neighbors_amr_info); 190 : 191 : db::mutate<amr::Tags::Info<Metavariables::volume_dim>>( 192 : [&overall_decision, 193 : &new_mesh](const gsl::not_null<amr::Info<volume_dim>*> amr_info) { 194 : amr_info->flags = overall_decision; 195 : amr_info->new_mesh = new_mesh; 196 : }, 197 : make_not_null(&box)); 198 : 199 : auto& amr_element_array = 200 : Parallel::get_parallel_component<ParallelComponent>(cache); 201 : 202 : const amr::Info<Metavariables::volume_dim>& my_info = 203 : get<amr::Tags::Info<volume_dim>>(box); 204 : for (const auto& [direction, neighbors] : my_element.neighbors()) { 205 : (void)direction; 206 : for (const auto& neighbor_id : neighbors.ids()) { 207 : if constexpr (Parallel::is_dg_element_collection_v<ParallelComponent>) { 208 : const auto neighbor_location = static_cast<int>( 209 : Parallel::local_synchronous_action< 210 : Parallel::Actions::GetItemFromDistributedOject< 211 : Parallel::Tags::ElementLocations<volume_dim>>>( 212 : Parallel::get_parallel_component<ParallelComponent>(cache)) 213 : ->at(neighbor_id)); 214 : Parallel::threaded_action<Parallel::Actions::SimpleActionOnElement< 215 : UpdateAmrDecision, true>>(amr_element_array[neighbor_location], 216 : neighbor_id, element_id, my_info); 217 : } else { 218 : Parallel::simple_action<UpdateAmrDecision>( 219 : amr_element_array[neighbor_id], element_id, my_info); 220 : } 221 : } 222 : } 223 : } 224 : }; 225 : } // namespace amr::Actions