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