Finite Volume Solver  prototype
A framework to build finite volume solvers for the AG Klein at the Freie Universität Berlin.
ViewFArrayBox.hpp
Go to the documentation of this file.
1 // Copyright (c) 2019 Maikel Nadolski
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 // SOFTWARE.
20 
21 #ifndef FUB_AMREX_FARRAYBOX_HPP
22 #define FUB_AMREX_FARRAYBOX_HPP
23 
24 #include "fub/Equation.hpp"
25 #include "fub/PatchDataView.hpp"
26 #include "fub/State.hpp"
28 
29 #include <AMReX_BaseFab.H>
30 #include <AMReX_FArrayBox.H>
31 #include <AMReX_RealBox.H>
32 
33 namespace fub {
34 template <>
35 ::amrex::IntVect GetOptionOr(const ProgramOptions& map, const std::string& name,
36  const ::amrex::IntVect& value);
37 
38 template <>
39 ::amrex::Box GetOptionOr(const ProgramOptions& map, const std::string& name,
40  const ::amrex::Box& value);
41 
42 template <>
43 ::amrex::RealBox GetOptionOr(const ProgramOptions& map, const std::string& name,
44  const ::amrex::RealBox& value);
45 
46 /// \brief The amrex namespace
47 namespace amrex {
48 
49 std::array<std::ptrdiff_t, AMREX_SPACEDIM> AsArray(const ::amrex::IntVect& vec);
50 
51 template <int Rank> IndexBox<Rank> AsIndexBox(const ::amrex::Box& box) {
52  const std::array<std::ptrdiff_t, AMREX_SPACEDIM> lower =
53  AsArray(box.smallEnd());
54  std::array<std::ptrdiff_t, AMREX_SPACEDIM> upper = AsArray(box.bigEnd());
55  std::transform(upper.begin(), upper.end(), upper.begin(),
56  [](std::ptrdiff_t i) { return i + 1; });
57 
58  if constexpr (AMREX_SPACEDIM != Rank) {
59  constexpr std::size_t sRank = static_cast<std::size_t>(Rank);
60  std::array<std::ptrdiff_t, sRank> lower1;
61  std::array<std::ptrdiff_t, sRank> upper1;
62  std::copy_n(lower.begin(), Rank, lower1.begin());
63  std::copy_n(upper.begin(), Rank, upper1.begin());
64  return IndexBox<Rank>{lower1, upper1};
65  } else {
66  return IndexBox<Rank>{lower, upper};
67  }
68 }
69 
70 /// Creates a mdspan which views all components of a mutable Fab.
71 ///
72 /// \param[in] fab The Fab which owns the data.
73 template <typename T>
74 mdspan<T, AMREX_SPACEDIM + 1> MakeMdSpan(::amrex::BaseFab<T>& fab) {
76  ::amrex::IntVect length{fab.box().length()};
77  for (int i = 0; i < AMREX_SPACEDIM; ++i) {
78  extents[static_cast<std::size_t>(i)] = length[i];
79  }
80  extents[AMREX_SPACEDIM] = fab.nComp();
81  return mdspan<T, AMREX_SPACEDIM + 1>{fab.dataPtr(), extents};
82 }
83 
84 template <typename T>
86 MakePatchDataView(::amrex::BaseFab<T>& fab) {
88  Index<AMREX_SPACEDIM> lower = AsArray(fab.box().smallEnd());
89  Index<AMREX_SPACEDIM + 1> lower_comp{};
90  std::copy_n(lower.begin(), AMREX_SPACEDIM, lower_comp.begin());
91  return PatchDataView<T, AMREX_SPACEDIM + 1>(mdspan, lower_comp);
92 }
93 
94 /// Creates a mdspan which views the specified component of a mutable Fab.
95 ///
96 /// \param[in] fab The Fab which owns the data.
97 template <typename T>
98 mdspan<T, AMREX_SPACEDIM> MakeMdSpan(::amrex::BaseFab<T>& fab, int component) {
100  ::amrex::IntVect length{fab.box().length()};
101  for (int i = 0; i < AMREX_SPACEDIM; ++i) {
102  extents[static_cast<std::size_t>(i)] = length[i];
103  }
104  return mdspan<T, AMREX_SPACEDIM>{fab.dataPtr(component), extents};
105 }
106 
107 template <typename T>
109  int component) {
111  Index<AMREX_SPACEDIM> lower = AsArray(fab.box().smallEnd());
113 }
114 
115 /// Creates a mdspan which views all components of a const Fab.
116 ///
117 /// \param[in] fab The Fab which owns the data.
118 template <typename T>
119 mdspan<const T, AMREX_SPACEDIM + 1> MakeMdSpan(const ::amrex::BaseFab<T>& fab) {
121  ::amrex::IntVect length{fab.box().length()};
122  for (int i = 0; i < AMREX_SPACEDIM; ++i) {
123  extents[static_cast<std::size_t>(i)] = length[i];
124  }
125  extents[static_cast<std::size_t>(AMREX_SPACEDIM)] = fab.nComp();
126  return mdspan<const T, AMREX_SPACEDIM + 1>{fab.dataPtr(), extents};
127 }
128 
129 template <typename T>
131 MakePatchDataView(const ::amrex::BaseFab<T>& fab) {
133  Index<AMREX_SPACEDIM> lower = AsArray(fab.box().smallEnd());
134  Index<AMREX_SPACEDIM + 1> lower_comp;
135  std::copy_n(lower.begin(), AMREX_SPACEDIM, lower_comp.begin());
136  lower_comp[AMREX_SPACEDIM] = 0;
138 }
139 
140 /// Creates a mdspan which views the specified component of a const Fab.
141 ///
142 /// \param[in] fab The Fab which owns the data.
143 template <typename T>
144 mdspan<const T, AMREX_SPACEDIM> MakeMdSpan(const ::amrex::BaseFab<T>& fab,
145  int component) {
147  ::amrex::IntVect length{fab.box().length()};
148  for (int i = 0; i < AMREX_SPACEDIM; ++i) {
149  extents[static_cast<std::size_t>(i)] = length[i];
150  }
151  return mdspan<const T, AMREX_SPACEDIM>{fab.dataPtr(component), extents};
152 }
153 
154 template <typename T>
156 MakePatchDataView(const ::amrex::BaseFab<T>& fab, int component) {
158  Index<AMREX_SPACEDIM> lower = AsArray(fab.box().smallEnd());
160 }
161 
162 template <typename T>
164 MakePatchDataView(const ::amrex::BaseFab<T>& fab, int component,
165  const ::amrex::Box& box) {
167  Index<AMREX_SPACEDIM> lower = AsArray(fab.box().smallEnd());
169  .Subview(AsIndexBox<AMREX_SPACEDIM>(box));
170 }
171 
172 template <typename T>
174 MakePatchDataView(::amrex::BaseFab<T>& fab, int component,
175  const ::amrex::Box& box) {
176  mdspan<T, AMREX_SPACEDIM> mdspan = MakeMdSpan(fab, component);
177  Index<AMREX_SPACEDIM> lower = AsArray(fab.box().smallEnd());
179  .Subview(AsIndexBox<AMREX_SPACEDIM>(box));
180 }
181 
182 template <typename State> struct MakeViewImpl {
183  using Equation = typename State::Equation;
185  using ValueType = typename State::ValueType;
186 
187  static constexpr int Rank = Equation::Rank();
188  static constexpr std::size_t sRank = static_cast<std::size_t>(Rank);
189 
191  const Equation& equation) {
192  const auto depths = ::fub::Depths(equation, Type<State>{});
193  int counter = 0;
194  const Index<AMREX_SPACEDIM + 1>& origin = fab.Origin();
195  auto transform = overloaded{
196  [&](int n_comps) {
198  Index<Rank + 1> e{};
199  std::copy_n(efab.begin(), Rank, e.begin());
200  e[sRank] = n_comps;
202  index[static_cast<std::size_t>(AMREX_SPACEDIM)] = counter;
203  Index<Rank + 1> this_origin{};
204  std::copy_n(origin.begin(), Rank, this_origin.begin());
205  // this_origin[sRank] = counter;
206  counter += n_comps;
207  mdspan<ValueType, sRank + 1> mds(&fab.MdSpan()(index), e);
208  return PatchDataView<ValueType, Rank + 1>(mds, this_origin);
209  },
210  [&](const ScalarDepth&) {
212  Index<Rank> e{};
213  std::copy_n(efab.begin(), Rank, e.begin());
215  index[AMREX_SPACEDIM] = counter;
216  Index<Rank> this_origin{};
217  std::copy_n(origin.begin(), Rank, this_origin.begin());
218  counter += 1;
219  mdspan<ValueType, sRank> mds(&fab.MdSpan()(index), e);
220  return PatchDataView<ValueType, Rank>(mds, this_origin);
221  }};
222  State pd_views{};
224  [&](auto& pdv, const auto& depth) { pdv = transform(depth); }, pd_views,
225  depths);
226  return pd_views;
227  }
228 };
229 
230 template <typename State, typename T, typename Equation>
232  const Equation& equation) {
233  MakeViewImpl<State> make_view{};
234  return make_view(fab, equation);
235 }
236 
237 template <typename State, typename Equation>
238 auto MakeView(::amrex::FArrayBox& fab, const Equation& equation) {
239  return MakeView<BasicView<State>>(MakePatchDataView(fab), equation);
240 }
241 
242 template <typename State, typename Equation>
243 auto MakeView(const ::amrex::FArrayBox& fab, const Equation& equation) {
244  return MakeView<BasicView<State>>(MakePatchDataView(fab), equation);
245 }
246 
247 template <typename State, typename Equation>
248 auto MakeView(::amrex::FArrayBox& fab, const Equation& eq,
249  const IndexBox<Equation::Rank()>& box) {
250  return Subview(MakeView<State>(fab, eq), box);
251 }
252 
253 template <typename State, typename Equation>
254 auto MakeView(const ::amrex::FArrayBox& fab, const Equation& eq,
255  const IndexBox<Equation::Rank()>& box) {
256  return Subview(MakeView<State>(fab, eq), box);
257 }
258 
259 template <typename State, typename FAB, typename Equation>
260 auto MakeView(FAB&& fab, const Equation& eq, const ::amrex::Box& box) {
261  return MakeView<State>(std::forward<FAB>(fab), eq,
262  AsIndexBox<Equation::Rank()>(box));
263 }
264 
265 std::array<::amrex::Box, 2>
267  const ::amrex::Box& face_validbox,
268  int stencil_width, Direction dir);
269 
270 } // namespace amrex
271 } // namespace fub
272 
273 #endif
Definition: mdspan.hpp:573
An extents object defines a multidimensional index space which is the Cartesian product of integers e...
Definition: mdspan.hpp:208
std::array<::amrex::Box, 2 > GetCellsAndFacesInStencilRange(const ::amrex::Box &cell_tilebox, const ::amrex::Box &face_validbox, int stencil_width, Direction dir)
IndexBox< Rank > AsIndexBox(const ::amrex::Box &box)
Definition: ViewFArrayBox.hpp:51
std::array< std::ptrdiff_t, AMREX_SPACEDIM > AsArray(const ::amrex::IntVect &vec)
auto MakeView(const PatchDataView< T, AMREX_SPACEDIM+1 > &fab, const Equation &equation)
Definition: ViewFArrayBox.hpp:231
mdspan< T, AMREX_SPACEDIM+1 > MakeMdSpan(::amrex::BaseFab< T > &fab)
Creates a mdspan which views all components of a mutable Fab.
Definition: ViewFArrayBox.hpp:74
PatchDataView< T, AMREX_SPACEDIM+1 > MakePatchDataView(::amrex::BaseFab< T > &fab)
Definition: ViewFArrayBox.hpp:86
std::decay_t< decltype(std::declval< T >().GetEquation())> Equation
A template typedef to detect the member function.
Definition: Meta.hpp:59
decltype(::fub::Depths(std::declval< typename T::Equation const & >(), Type< T >{})) Depths
Definition: State.hpp:189
The fub namespace.
Definition: AnyBoundaryCondition.hpp:31
View< State > Subview(const BasicView< State, Layout, Rank > &state, const IndexBox< Rank > &box)
Definition: Equation.hpp:86
void ForEachVariable(F function, Ts &&... states)
Definition: State.hpp:89
Direction
This is a type safe type to denote a dimensional split direction.
Definition: Direction.hpp:30
std::array< std::ptrdiff_t, static_cast< std::size_t >(Rank)> Index
Definition: PatchDataView.hpp:34
T GetOptionOr(const ProgramOptions &map, const std::string &name, const T &value)
Definition: ProgramOptions.hpp:48
IndexBox< Rank > Box(const BasicView< State, Layout, Rank > &view)
Definition: State.hpp:486
std::ptrdiff_t index
Definition: type_traits.hpp:179
std::map< std::string, pybind11::object > ProgramOptions
Definition: ProgramOptions.hpp:40
Definition: PatchDataView.hpp:56
Definition: PatchDataView.hpp:201
dynamic_extents< sRank > Extents() const noexcept
Definition: PatchDataView.hpp:214
const mdspan< T, sRank, Layout > & MdSpan() const noexcept
Definition: PatchDataView.hpp:206
PatchDataView< T, R, layout_stride > Subview(const IndexBox< R > &box) const
Definition: PatchDataView.hpp:245
const std::array< std::ptrdiff_t, sRank > & Origin() const noexcept
Definition: PatchDataView.hpp:210
This type is used to tag scalar quantities.
Definition: State.hpp:109
Definition: State.hpp:162
Definition: ViewFArrayBox.hpp:182
State operator()(const PatchDataView< ValueType, AMREX_SPACEDIM+1 > &fab, const Equation &equation)
Definition: ViewFArrayBox.hpp:190
static constexpr std::size_t sRank
Definition: ViewFArrayBox.hpp:188
static constexpr int Rank
Definition: ViewFArrayBox.hpp:187
typename State::Equation Equation
Definition: ViewFArrayBox.hpp:183
typename State::ValueType ValueType
Definition: ViewFArrayBox.hpp:185
meta::Depths< State > Depths
Definition: ViewFArrayBox.hpp:184
Definition: type_traits.hpp:291