ExpandOverBlocks.hpp
1 // Distributed under the MIT License.
2 // See LICENSE.txt for details.
3 
4 #pragma once
5 
6 #include <array>
7 #include <cstddef>
8 #include <string>
9 #include <unordered_map>
10 #include <unordered_set>
11 #include <vector>
12 
13 namespace domain {
14 /*!
15  * \brief Produce a distribution of type `T` over all blocks and dimensions in
16  * the domain, based on values `T` of variable isotropy and homogeneity.
17  *
18  * This class is useful to option-create values for e.g. the initial refinement
19  * level or initial number of grid points for domain creators. It can be used
20  * with `std::visit` and a `std::variant` with (a subset of) these types:
21  *
22  * - `T`: Repeat over all blocks and dimensions (isotropic and homogeneous).
23  * - `std::array<T, Dim>`: Repeat over all blocks (homogeneous).
24  * - `std::vector<std::array<T, Dim>>>`: Only check if the size matches the
25  * number of blocks, throwing a `std::length_error` if it doesn't.
26  * - `std::unordered_map<std::string, std::array<T, Dim>>`: Map block names, or
27  * names of block groups, to values. The map must cover all blocks once the
28  * groups are expanded. To use this option you must pass the list of block
29  * names and groups to the constructor.
30  *
31  * Note that the call-operators `throw` when they encounter errors, such as
32  * mismatches in the number of blocks. The exceptions can be used to output
33  * user-facing error messages in an option-parsing context.
34  *
35  * Here's an example for using this class:
36  *
37  * \snippet Test_ExpandOverBlocks.cpp expand_over_blocks_example
38  *
39  * Here's an example using block names and groups:
40  *
41  * \snippet Test_ExpandOverBlocks.cpp expand_over_blocks_named_example
42  *
43  * \tparam T The type distributed over the domain
44  * \tparam Dim The number of spatial dimensions
45  */
46 template <typename T, size_t Dim>
48  ExpandOverBlocks(size_t num_blocks) noexcept;
50  std::vector<std::string> block_names,
52  block_groups = {}) noexcept;
53 
54  /// Repeat over all blocks and dimensions (isotropic and homogeneous)
55  std::vector<std::array<T, Dim>> operator()(const T& value) const;
56 
57  /// Repeat over all blocks (homogeneous)
59  const std::array<T, Dim>& value) const;
60 
61  /// Only check if the size matches the number of blocks, throwing a
62  /// `std::length_error` if it doesn't
64  std::vector<std::array<T, Dim>> value) const;
65 
66  /// Map block names, or names of block groups, to values. The map must cover
67  /// all blocks once the groups are expanded. To use this option you must pass
68  /// the list of block names and groups to the constructor. Here's an example:
69  ///
70  /// \snippet Test_ExpandOverBlocks.cpp expand_over_blocks_named_example
73 
74  private:
75  size_t num_blocks_;
76  std::vector<std::string> block_names_;
78  block_groups_;
79 };
80 } // namespace domain
std::string
unordered_set
vector
domain::ExpandOverBlocks::operator()
std::vector< std::array< T, Dim > > operator()(const T &value) const
Repeat over all blocks and dimensions (isotropic and homogeneous)
cstddef
array
domain::ExpandOverBlocks
Produce a distribution of type T over all blocks and dimensions in the domain, based on values T of v...
Definition: ExpandOverBlocks.hpp:47
unordered_map
string