Line data Source code
1 0 : // Distributed under the MIT License.
2 : // See LICENSE.txt for details.
3 :
4 : #pragma once
5 :
6 : #include <boost/rational.hpp>
7 : #include <cstddef>
8 : #include <vector>
9 :
10 : #include "DataStructures/DataBox/DataBox.hpp"
11 : #include "Domain/Amr/Helpers.hpp"
12 : #include "Domain/Structure/ElementId.hpp"
13 : #include "IO/Logging/Tags.hpp"
14 : #include "IO/Logging/Verbosity.hpp"
15 : #include "Parallel/AlgorithmExecution.hpp"
16 : #include "Parallel/ArrayCollection/IsDgElementCollection.hpp"
17 : #include "Parallel/GetSection.hpp"
18 : #include "Parallel/GlobalCache.hpp"
19 : #include "Parallel/Printf/Printf.hpp"
20 : #include "Parallel/Reduction.hpp"
21 : #include "ParallelAlgorithms/Amr/Actions/Component.hpp"
22 : #include "ParallelAlgorithms/Amr/Actions/RunAmrDiagnostics.hpp"
23 : #include "ParallelAlgorithms/Amr/Tags.hpp"
24 : #include "Utilities/GetOutput.hpp"
25 : #include "Utilities/Serialization/PupBoost.hpp"
26 : #include "Utilities/StdHelpers.hpp"
27 :
28 : namespace amr::Actions {
29 :
30 : /// \brief Send AMR diagnostics about Element%s to amr::Component
31 : ///
32 : /// Sends the following:
33 : /// - The fraction of a Block volume (in the logical coordinate frame) covered
34 : /// by the Element
35 : /// - One, in order to count the number of Element%s
36 : /// - The number of grid points
37 : /// - The refinement level in each logical dimension
38 : /// - The number of grid points in each logical dimension
39 : ///
40 : /// The information is sent to amr::Component which runs the action
41 : /// amr::Actions::RunAmrDiagnostics after all Element%s have contributed to the
42 : /// reduction
43 1 : struct SendAmrDiagnostics {
44 0 : using const_global_cache_tags =
45 : tmpl::list<logging::Tags::Verbosity<amr::OptionTags::AmrGroup>>;
46 :
47 0 : using ReductionData = Parallel::ReductionData<
48 : // Grid index
49 : Parallel::ReductionDatum<size_t, funcl::AssertEqual<>>,
50 : // fraction of Block volume
51 : Parallel::ReductionDatum<boost::rational<size_t>, funcl::Plus<>>,
52 : // number of elements
53 : Parallel::ReductionDatum<size_t, funcl::Plus<>>,
54 : // number of grid points
55 : Parallel::ReductionDatum<size_t, funcl::Plus<>>,
56 : // average refinement level by dimension
57 : Parallel::ReductionDatum<
58 : std::vector<double>, funcl::ElementWise<funcl::Plus<>>,
59 : funcl::ElementWise<funcl::Divides<>>, std::index_sequence<2>>,
60 : // average number of grid points by dimension
61 : Parallel::ReductionDatum<
62 : std::vector<double>, funcl::ElementWise<funcl::Plus<>>,
63 : funcl::ElementWise<funcl::Divides<>>, std::index_sequence<2>>>;
64 :
65 : template <typename DbTagList, typename... InboxTags, typename Metavariables,
66 : size_t Dim, typename ActionList, typename ParallelComponent>
67 0 : static Parallel::iterable_action_return_t apply(
68 : db::DataBox<DbTagList>& box,
69 : tuples::TaggedTuple<InboxTags...>& /*inboxes*/,
70 : const Parallel::GlobalCache<Metavariables>& cache,
71 : const ElementId<Dim>& element_id, const ActionList /*meta*/,
72 : const ParallelComponent* /*meta*/) {
73 : if constexpr (Parallel::is_dg_element_collection_v<ParallelComponent>) {
74 : ERROR("Reductions are not yet implemented for nodegroup arrays.");
75 : } else {
76 : const auto& mesh = db::get<::domain::Tags::Mesh<Dim>>(box);
77 : const auto& my_proxy =
78 : Parallel::get_parallel_component<ParallelComponent>(
79 : cache)[element_id];
80 : const auto& target_proxy =
81 : Parallel::get_parallel_component<amr::Component<Metavariables>>(
82 : cache);
83 : std::vector<double> refinement_levels_by_dim(Dim);
84 : std::vector<double> extents_by_dim(Dim);
85 : const auto refinement_levels = element_id.refinement_levels();
86 : for (size_t d = 0; d < Dim; ++d) {
87 : refinement_levels_by_dim[d] = gsl::at(refinement_levels, d);
88 : extents_by_dim[d] = mesh.extents(d);
89 : }
90 : if (db::get<logging::Tags::Verbosity<amr::OptionTags::AmrGroup>>(box) >=
91 : Verbosity::Debug) {
92 : Parallel::printf("%s h-refinement %s, p-refinement %s\n",
93 : get_output(element_id), get_output(refinement_levels),
94 : get_output(mesh.extents()));
95 : if constexpr (Metavariables::amr::keep_coarse_grids) {
96 : const auto& parent_id = db::get<amr::Tags::ParentId<Dim>>(box);
97 : const auto& child_ids = db::get<amr::Tags::ChildIds<Dim>>(box);
98 : Parallel::printf("%s parent %s, children %s\n",
99 : get_output(element_id), get_output(parent_id),
100 : get_output(child_ids));
101 : }
102 : }
103 : auto& grid_index_section = Parallel::get_section<
104 : ParallelComponent,
105 : tmpl::conditional_t<Metavariables::amr::keep_coarse_grids,
106 : amr::Tags::GridIndex, void>>(make_not_null(&box));
107 : Parallel::contribute_to_reduction<amr::Actions::RunAmrDiagnostics>(
108 : ReductionData{element_id.grid_index(),
109 : amr::fraction_of_block_volume(element_id), 1,
110 : mesh.number_of_grid_points(), refinement_levels_by_dim,
111 : extents_by_dim},
112 : my_proxy, target_proxy, make_not_null(&grid_index_section));
113 : }
114 : return {Parallel::AlgorithmExecution::Continue, std::nullopt};
115 : }
116 : };
117 : } // namespace amr::Actions
|