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/GlobalCache.hpp" 23 : #include "Parallel/Invoke.hpp" 24 : #include "ParallelAlgorithms/Amr/Actions/UpdateAmrDecision.hpp" 25 : #include "ParallelAlgorithms/Amr/Criteria/Criterion.hpp" 26 : #include "ParallelAlgorithms/Amr/Criteria/Tags/Criteria.hpp" 27 : #include "ParallelAlgorithms/Amr/Policies/EnforcePolicies.hpp" 28 : #include "ParallelAlgorithms/Amr/Policies/Policies.hpp" 29 : #include "ParallelAlgorithms/Amr/Policies/Tags.hpp" 30 : #include "ParallelAlgorithms/Amr/Projectors/Mesh.hpp" 31 : #include "Utilities/ErrorHandling/Error.hpp" 32 : #include "Utilities/Gsl.hpp" 33 : #include "Utilities/MakeArray.hpp" 34 : #include "Utilities/PrettyType.hpp" 35 : #include "Utilities/TMPL.hpp" 36 : 37 : /// \cond 38 : namespace tuples { 39 : template <class... Tags> 40 : class TaggedTuple; 41 : } // namespace tuples 42 : /// \endcond 43 : 44 : namespace detail { 45 : template <typename Criterion> 46 : struct get_tags { 47 : using type = typename Criterion::compute_tags_for_observation_box; 48 : }; 49 : 50 : } // namespace detail 51 : 52 : namespace amr::Actions { 53 : /// \brief Evaluates the refinement criteria in order to set the amr::Info of an 54 : /// Element and sends this information to the neighbors of the Element. 55 : /// 56 : /// DataBox: 57 : /// - Uses: 58 : /// * domain::Tags::Element<volume_dim> 59 : /// * amr::Tags::NeighborInfo<volume_dim> 60 : /// * amr::Criteria::Tags::Criteria (from GlobalCache) 61 : /// * amr::Tags::Policies (from GlobalCache) 62 : /// * any tags requested by the refinement criteria 63 : /// - Modifies: 64 : /// * amr::Tags::Info<volume_dim> 65 : /// 66 : /// Invokes: 67 : /// - UpdateAmrDecision on all neighboring Element%s 68 : /// 69 : /// \details 70 : /// - Evaluates each refinement criteria held by amr::Criteria::Tags::Criteria, 71 : /// and in each dimension selects the amr::Flag with the highest 72 : /// priority (i.e the highest integral value). 73 : /// - If necessary, changes the refinement decision in order to satisfy the 74 : /// amr::Policies 75 : /// - An Element that is splitting in one dimension is not allowed to join 76 : /// in another dimension. If this is requested by the refinement critiera, 77 : /// the decision to join is changed to do nothing 78 : /// - Checks if any neighbors have sent their AMR decision, and if so, calls 79 : /// amr:::update_amr_decision with the decision of each neighbor in 80 : /// order to see if the current decision needs to be updated 81 : /// - Sends the (possibly updated) decision to all of the neighboring Elements 82 1 : struct EvaluateRefinementCriteria { 83 : template <typename ParallelComponent, typename DbTagList, 84 : typename Metavariables> 85 0 : static void apply(db::DataBox<DbTagList>& box, 86 : Parallel::GlobalCache<Metavariables>& cache, 87 : const ElementId<Metavariables::volume_dim>& element_id) { 88 : constexpr size_t volume_dim = Metavariables::volume_dim; 89 : auto overall_decision = make_array<volume_dim>(amr::Flag::Undefined); 90 : 91 : using compute_tags = tmpl::remove_duplicates<tmpl::flatten<tmpl::transform< 92 : tmpl::at<typename Metavariables::factory_creation::factory_classes, 93 : Criterion>, 94 : detail::get_tags<tmpl::_1>>>>; 95 : auto observation_box = 96 : make_observation_box<compute_tags>(make_not_null(&box)); 97 : 98 : const auto& refinement_criteria = 99 : db::get<amr::Criteria::Tags::Criteria>(box); 100 : for (const auto& criterion : refinement_criteria) { 101 : auto decision = criterion->evaluate(observation_box, cache, element_id); 102 : for (size_t d = 0; d < volume_dim; ++d) { 103 : overall_decision[d] = std::max(overall_decision[d], decision[d]); 104 : } 105 : } 106 : 107 : const auto& policies = db::get<amr::Tags::Policies>(box); 108 : amr::enforce_policies(make_not_null(&overall_decision), policies, 109 : element_id, 110 : get<::domain::Tags::Mesh<volume_dim>>(box)); 111 : 112 : // An element cannot join if it is splitting in another dimension. 113 : // Update the flags now before sending to neighbors as each time 114 : // a flag is changed by UpdateAmrDecision, it sends the new flags 115 : // to its neighbors. So updating now will save some commmunication. 116 : amr::prevent_element_from_joining_while_splitting( 117 : make_not_null(&overall_decision)); 118 : 119 : // Check if we received any neighbor flags prior to determining our own 120 : // flags. If yes, then possible update our flags (e.g. sibling doesn't want 121 : // to join, maintain 2:1 balance, etc.) 122 : const auto& my_element = get<::domain::Tags::Element<volume_dim>>(box); 123 : const auto& my_neighbors_amr_info = 124 : get<amr::Tags::NeighborInfo<volume_dim>>(box); 125 : if (not my_neighbors_amr_info.empty()) { 126 : for (const auto& [neighbor_id, neighbor_amr_info] : 127 : my_neighbors_amr_info) { 128 : amr::update_amr_decision( 129 : make_not_null(&overall_decision), my_element, neighbor_id, 130 : neighbor_amr_info.flags, 131 : policies.enforce_two_to_one_balance_in_normal_direction()); 132 : } 133 : } 134 : 135 : const auto new_mesh = amr::projectors::new_mesh( 136 : get<::domain::Tags::Mesh<volume_dim>>(box), overall_decision, 137 : my_element, my_neighbors_amr_info); 138 : 139 : db::mutate<amr::Tags::Info<Metavariables::volume_dim>>( 140 : [&overall_decision, 141 : &new_mesh](const gsl::not_null<amr::Info<volume_dim>*> amr_info) { 142 : amr_info->flags = overall_decision; 143 : amr_info->new_mesh = new_mesh; 144 : }, 145 : make_not_null(&box)); 146 : 147 : auto& amr_element_array = 148 : Parallel::get_parallel_component<ParallelComponent>(cache); 149 : 150 : const amr::Info<Metavariables::volume_dim>& my_info = 151 : get<amr::Tags::Info<volume_dim>>(box); 152 : for (const auto& [direction, neighbors] : my_element.neighbors()) { 153 : (void)direction; 154 : for (const auto& neighbor_id : neighbors.ids()) { 155 : Parallel::simple_action<UpdateAmrDecision>( 156 : amr_element_array[neighbor_id], element_id, my_info); 157 : } 158 : } 159 : } 160 : }; 161 : } // namespace amr::Actions