Finite Volume Solver  prototype
A framework to build finite volume solvers for the AG Klein at the Freie Universität Berlin.
SAMRAI/PatchHierarchy.hpp
Go to the documentation of this file.
1 // Copyright (c) 2019 Maikel Nadolski
2 // Copyright (c) 2019 Patrick Denzler
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to deal
6 // in the Software without restriction, including without limitation the rights
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 // copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 // SOFTWARE.
21 
22 #ifndef FUB_SAMRAI_PATCH_HIERARCHY_HPP
23 #define FUB_SAMRAI_PATCH_HIERARCHY_HPP
24 
26 
27 #include "fub/Duration.hpp"
28 #include "fub/counter/CounterRegistry.hpp"
29 #include "fub/ext/uuid.hpp"
30 
31 #include <SAMRAI/geom/CartesianGridGeometry.h>
32 #include <SAMRAI/geom/CartesianPatchGeometry.h>
33 #include <SAMRAI/hier/PatchHierarchy.h>
34 
35 namespace fub::samrai {
36 
37 SAMRAI::hier::ComponentSelector
38 SelectComponents(const SAMRAI::hier::PatchDescriptor& desc);
39 SAMRAI::hier::ComponentSelector SelectComponents(span<const int> data_ids);
40 
42  SAMRAI::hier::IntVector refine_ratio;
44 };
45 
46 template <std::size_t Rank> struct CoordinateRange {
47  std::array<double, Rank> lower;
48  std::array<double, Rank> upper;
49 };
50 
51 template <typename I, std::size_t Rank>
52 std::enable_if_t<std::is_integral_v<I>,
53  std::shared_ptr<SAMRAI::geom::CartesianGridGeometry>>
54 MakeCartesianGridGeometry(const std::array<I, Rank>& n_cells,
55  const CoordinateRange<Rank>& coordinates) {
56  const double* x_lo = coordinates.lower.data();
57  const double* x_up = coordinates.upper.data();
58  const SAMRAI::hier::Index idx_up = std::apply(
59  [](auto... is) { return SAMRAI::hier::Index(int(is - 1)...); }, n_cells);
60  const SAMRAI::tbox::Dimension dim(Rank);
61  const SAMRAI::hier::Box domain_box(SAMRAI::hier::Index::getZeroIndex(dim),
62  idx_up, SAMRAI::hier::BlockId(0));
63  SAMRAI::hier::BoxContainer domain{domain_box};
64  return std::make_shared<SAMRAI::geom::CartesianGridGeometry>(
65  MakeUniqueName(), x_lo, x_up, domain);
66 }
67 
68 /// \ingroup PatchHierarchy
70 public:
71  /// \brief Constructs a PatchHierarchy object which is capable of holding data
72  /// described by the secified data description on given geometry extents.
73  template <typename Equation>
74  PatchHierarchy(const Equation& eq,
75  std::shared_ptr<SAMRAI::geom::CartesianGridGeometry> geom,
76  PatchHierarchyOptions hier_opts);
77 
79  std::shared_ptr<SAMRAI::geom::CartesianGridGeometry> geom,
80  PatchHierarchyOptions hier_opts);
81 
82  PatchHierarchy(PatchHierarchy&& ph) = default;
84 
87  PatchHierarchy tmp(ph);
88  std::swap(*this, tmp);
89  return *this;
90  }
91 
92  /// \brief Return some additional patch hierarchy options.
93  [[nodiscard]] const PatchHierarchyOptions& GetOptions() const noexcept;
94 
95  [[nodiscard]] int GetMaxNumberOfLevels() const noexcept;
96  [[nodiscard]] int GetNumberOfLevels() const noexcept;
97 
98  [[nodiscard]] const DataDescription& GetDataDescription() const noexcept;
99 
100  [[nodiscard]] const std::shared_ptr<SAMRAI::hier::PatchHierarchy>&
101  GetNative() const noexcept;
102 
103  [[nodiscard]] const SAMRAI::geom::CartesianGridGeometry&
104  GetGeometry(int level) const noexcept;
105 
106  [[nodiscard]] span<const int> GetDataIds() const noexcept;
107 
108  [[nodiscard]] std::shared_ptr<SAMRAI::hier::PatchLevel>
109  GetPatchLevel(int level) const;
110 
111  [[nodiscard]] const std::shared_ptr<CounterRegistry>&
112  GetCounterRegistry() const noexcept;
113 
114  [[nodiscard]] std::ptrdiff_t GetCycles(int level = 0) const;
115  [[nodiscard]] Duration GetTimePoint(int level = 0) const;
116 
117  void SetCycles(std::ptrdiff_t cycles, int level);
118  void SetTimePoint(Duration time_point, int level);
119 
120 private:
121  std::shared_ptr<SAMRAI::hier::PatchHierarchy> hierarchy_;
124  std::vector<std::ptrdiff_t> cycles_;
125  std::vector<Duration> time_points_;
126  std::shared_ptr<CounterRegistry> counter_registry_;
127 };
128 
129 template <typename Equation>
131  const Equation& eq,
132  std::shared_ptr<SAMRAI::geom::CartesianGridGeometry> geom,
133  PatchHierarchyOptions hier_opts)
134  : PatchHierarchy(RegisterVariables(eq), std::move(geom),
135  std::move(hier_opts)) {}
136 
137 template <typename... Is>
138 std::array<double, sizeof...(Is)>
139 GetCellCenter(const SAMRAI::geom::CartesianGridGeometry& geom, Is... is) {
140  const double* dx = geom.getDx();
141  const double* xlo = geom.getXLower();
142  std::array<std::common_type_t<Is...>, sizeof...(Is)> index{
143  std::common_type_t<Is...>(is)...};
144  std::array<double, sizeof...(Is)> x;
145  for (std::size_t d = 0; d < sizeof...(Is); ++d) {
146  x[d] = xlo[d] + 0.5 * dx[d] + index[d] * dx[d];
147  }
148  return x;
149 }
150 
151 } // namespace fub::samrai
152 
153 #endif
Definition: SAMRAI/PatchHierarchy.hpp:69
std::shared_ptr< SAMRAI::hier::PatchHierarchy > hierarchy_
Definition: SAMRAI/PatchHierarchy.hpp:121
PatchHierarchy & operator=(PatchHierarchy &&ph)=default
const SAMRAI::geom::CartesianGridGeometry & GetGeometry(int level) const noexcept
PatchHierarchy(PatchHierarchy &&ph)=default
const std::shared_ptr< SAMRAI::hier::PatchHierarchy > & GetNative() const noexcept
int GetNumberOfLevels() const noexcept
std::shared_ptr< SAMRAI::hier::PatchLevel > GetPatchLevel(int level) const
PatchHierarchy & operator=(const PatchHierarchy &ph)
Definition: SAMRAI/PatchHierarchy.hpp:86
Duration GetTimePoint(int level=0) const
const PatchHierarchyOptions & GetOptions() const noexcept
Return some additional patch hierarchy options.
span< const int > GetDataIds() const noexcept
std::vector< Duration > time_points_
Definition: SAMRAI/PatchHierarchy.hpp:125
std::vector< std::ptrdiff_t > cycles_
Definition: SAMRAI/PatchHierarchy.hpp:124
const DataDescription & GetDataDescription() const noexcept
void SetCycles(std::ptrdiff_t cycles, int level)
DataDescription data_desc_
Definition: SAMRAI/PatchHierarchy.hpp:122
std::shared_ptr< CounterRegistry > counter_registry_
Definition: SAMRAI/PatchHierarchy.hpp:126
int GetMaxNumberOfLevels() const noexcept
std::ptrdiff_t GetCycles(int level=0) const
PatchHierarchy(const Equation &eq, std::shared_ptr< SAMRAI::geom::CartesianGridGeometry > geom, PatchHierarchyOptions hier_opts)
Constructs a PatchHierarchy object which is capable of holding data described by the secified data de...
Definition: SAMRAI/PatchHierarchy.hpp:130
PatchHierarchy(const PatchHierarchy &ph)
const std::shared_ptr< CounterRegistry > & GetCounterRegistry() const noexcept
PatchHierarchyOptions options_
Definition: SAMRAI/PatchHierarchy.hpp:123
void SetTimePoint(Duration time_point, int level)
PatchHierarchy(DataDescription dd, std::shared_ptr< SAMRAI::geom::CartesianGridGeometry > geom, PatchHierarchyOptions hier_opts)
A span is a view over a contiguous sequence of objects, the storage of which is owned by some other o...
Definition: span.hpp:81
std::decay_t< decltype(std::declval< T >().GetEquation())> Equation
A template typedef to detect the member function.
Definition: Meta.hpp:59
Definition: CartesianPatchHierarchy.hpp:41
SAMRAI::hier::ComponentSelector SelectComponents(const SAMRAI::hier::PatchDescriptor &desc)
std::enable_if_t< std::is_integral_v< I >, std::shared_ptr< SAMRAI::geom::CartesianGridGeometry > > MakeCartesianGridGeometry(const std::array< I, Rank > &n_cells, const CoordinateRange< Rank > &coordinates)
Definition: SAMRAI/PatchHierarchy.hpp:54
DataDescription RegisterVariables(const Equation &equation, std::string prefix=std::string())
This function registers all neccessary variables and contexts with SAMRAI.
Definition: RegisterVariables.hpp:102
std::array< double, sizeof...(Is)> GetCellCenter(const SAMRAI::geom::CartesianGridGeometry &geom, Is... is)
Definition: SAMRAI/PatchHierarchy.hpp:139
std::string MakeUniqueName()
Definition: uuid.hpp:31
std::chrono::duration< double > Duration
Definition: Duration.hpp:31
std::array< std::ptrdiff_t, static_cast< std::size_t >(Rank)> Index
Definition: PatchDataView.hpp:34
IndexBox< Rank > Box(const BasicView< State, Layout, Rank > &view)
Definition: State.hpp:486
std::ptrdiff_t index
Definition: type_traits.hpp:179
Definition: SAMRAI/PatchHierarchy.hpp:46
std::array< double, Rank > lower
Definition: SAMRAI/PatchHierarchy.hpp:47
std::array< double, Rank > upper
Definition: SAMRAI/PatchHierarchy.hpp:48
Definition: RegisterVariables.hpp:35
Definition: SAMRAI/PatchHierarchy.hpp:41
SAMRAI::hier::IntVector refine_ratio
Definition: SAMRAI/PatchHierarchy.hpp:42
int max_number_of_levels
Definition: SAMRAI/PatchHierarchy.hpp:43