Line data Source code
1 0 : // Distributed under the MIT License.
2 : // See LICENSE.txt for details.
3 :
4 : #pragma once
5 :
6 : #include <optional>
7 : #include <type_traits>
8 :
9 : #include "DataStructures/DataBox/DataBox.hpp"
10 : #include "Parallel/Reduction.hpp"
11 : #include "Parallel/Tags/Section.hpp"
12 : #include "Utilities/ErrorHandling/Assert.hpp"
13 : #include "Utilities/Gsl.hpp"
14 :
15 : namespace Parallel {
16 :
17 : /*!
18 : * \brief Retrieve the section that the element belongs to, or
19 : * `Parallel::no_section()` if `SectionIdTag` is `void`.
20 : *
21 : * This function is useful to support sections in parallel algorithms. Specify
22 : * the `SectionIdTag` template parameter to retrieve the associated section, or
23 : * set it to `void` when the parallel algorithm runs over all elements of the
24 : * parallel component. See `Parallel::Section` for details on sections.
25 : *
26 : * Only call this function on elements that are part of a section. In case not
27 : * all elements are part of a section with the `SectionIdTag`, make sure to skip
28 : * those elements before calling this function.
29 : */
30 : template <typename ParallelComponent, typename SectionIdTag,
31 : typename DbTagsList>
32 1 : auto& get_section(
33 : [[maybe_unused]] const gsl::not_null<db::DataBox<DbTagsList>*> box) {
34 : if constexpr (std::is_same_v<SectionIdTag, void>) {
35 : return Parallel::no_section();
36 : } else {
37 : std::optional<Parallel::Section<ParallelComponent, SectionIdTag>>& section =
38 : db::get_mutable_reference<
39 : Parallel::Tags::Section<ParallelComponent, SectionIdTag>>(box);
40 : ASSERT(section.has_value(),
41 : "Call 'get_section' only on elements that belong to a section. This "
42 : "is probably a bug, because the other elements should presumably be "
43 : "skipped. The section is: "
44 : << db::tag_name<SectionIdTag>());
45 : return *section;
46 : }
47 : }
48 :
49 : } // namespace Parallel
|