Loading [MathJax]/extensions/tex2jax.js
Finite Volume Solver  prototype
A framework to build finite volume solvers for the AG Klein at the Freie Universität Berlin.
•All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
SAMRAI/FluxMethodAdapter.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_SAMRAI_FLUX_METHOD_HPP
22 #define FUB_SAMRAI_FLUX_METHOD_HPP
23 
25 #include "fub/SAMRAI/ViewPatch.hpp"
26 
27 #include <limits>
28 #include <numeric>
29 
30 namespace fub::samrai {
31 /// This is a wrapper class which dispatches a given base method object and
32 /// dispatches SAMRAI typed patches.
33 ///
34 /// The base method is expected to act on View objects of equation states.
35 template <typename Tag, typename BaseMethod>
36 class FluxMethodAdapter : private BaseMethod {
37 public:
38  using Equation =
39  std::decay_t<decltype(std::declval<const BaseMethod&>().GetEquation())>;
42 
43  FluxMethodAdapter(Tag, const BaseMethod& base) : BaseMethod(base) {}
44  FluxMethodAdapter(Tag, BaseMethod&& base) : BaseMethod(std::move(base)) {}
45 
46  using BaseMethod::GetEquation;
47 
48  using BaseMethod::GetStencilWidth;
49 
50  /// Extracts the state variables patch data views including its ghost cells
51  /// and compute a stable time step size in specified direction and refinement
52  /// level.
53  ///
54  /// \pre This method expects the ghost cells to be filled with proper boundary
55  /// data.
56  Duration ComputeStableDt(IntegratorContext& context, int level,
57  Direction dir);
58 
59  void ComputeNumericFluxes(IntegratorContext& context, int level, Duration dt,
60  Direction dir);
61 
62  void ComputeNumericFluxes(span<SAMRAI::pdat::SideData<double>*> fluxes,
63  span<SAMRAI::pdat::CellData<double> const*> cells,
64  double dx, Duration dt, Direction dir);
65 
66  Duration ComputeStableDt(span<SAMRAI::pdat::CellData<double> const*> data,
67  double dx, Direction dir);
68 };
69 
70 template <typename Tag, typename BaseMethod>
73  int level, Direction dir) {
74  const SAMRAI::hier::PatchLevel& patch_level = context.GetPatchLevel(level);
75  span<const int> data_ids = context.GetScratchIds();
76  const SAMRAI::geom::CartesianGridGeometry& geom = context.GetGeometry(level);
77  const int dir_value = static_cast<int>(dir);
78  const double dx = geom.getDx()[dir_value];
79  std::vector<const SAMRAI::pdat::CellData<double>*> patch_data(
80  data_ids.size());
81  return std::accumulate(patch_level.begin(), patch_level.end(),
82  Duration(std::numeric_limits<double>::max()),
83  [&](Duration min_dt, auto&& patch) {
84  GetPatchData(span{patch_data}, *patch, data_ids);
85  return std::min(
86  min_dt, ComputeStableDt(patch_data, dx, dir));
87  });
88 }
89 
90 template <typename Tag, typename BaseMethod>
92  span<SAMRAI::pdat::CellData<double> const*> data, double dx,
93  Direction dir) {
94  constexpr int Rank = Equation::Rank();
95  auto states =
96  MakeView<const Complete>(data, BaseMethod::GetEquation(),
97  AsIndexBox<Rank>(data[0]->getGhostBox()));
98  return Duration(BaseMethod::ComputeStableDt(Tag(), states, dx, dir));
99 }
100 
101 template <typename Tag, typename BaseMethod>
103  fub::samrai::IntegratorContext& context, int level, fub::Duration dt,
104  fub::Direction dir) {
105  const SAMRAI::geom::CartesianGridGeometry& geom = context.GetGeometry(level);
106  const int dir_value = static_cast<int>(dir);
107  const double dx = geom.getDx()[dir_value];
108  const SAMRAI::hier::PatchLevel& patch_level = context.GetPatchLevel(level);
109  span<const int> scratch_ids = context.GetScratchIds();
110  std::vector<const SAMRAI::pdat::CellData<double>*> scratch_data(
111  scratch_ids.size());
112  span<const int> flux_ids = context.GetFluxIds();
113  std::vector<SAMRAI::pdat::SideData<double>*> flux_data(flux_ids.size());
114  for (std::shared_ptr<SAMRAI::hier::Patch> patch : patch_level) {
115  GetPatchData(span{flux_data}, *patch, flux_ids);
116  GetPatchData(span{scratch_data}, *patch, scratch_ids);
117  ComputeNumericFluxes(flux_data, scratch_data, dx, dt, dir);
118  }
119 }
120 
121 template <typename Tag, typename BaseMethod>
123  span<SAMRAI::pdat::SideData<double>*> fluxes,
124  span<SAMRAI::pdat::CellData<double> const*> cells, double dx, Duration dt,
125  Direction dir) {
126  constexpr int Rank = Equation::Rank();
127  auto cell_box = AsIndexBox<Rank>(cells[0]->getGhostBox());
128  const int gcw = GetStencilWidth();
129  auto face_box = Shrink(cell_box, dir, {gcw, gcw});
130  auto flux_view = MakeView<Conservative>(fluxes, GetEquation(), dir, face_box);
131  auto scratch_view = MakeView<const Complete>(cells, GetEquation(), cell_box);
132  BaseMethod::ComputeNumericFluxes(flux_view, scratch_view, dt, dx, dir);
133 }
134 
135 } // namespace fub::samrai
136 
137 #endif
This is a wrapper class which dispatches a given base method object and dispatches SAMRAI typed patch...
Definition: SAMRAI/FluxMethodAdapter.hpp:36
std::decay_t< decltype(std::declval< const BaseMethod & >().GetEquation())> Equation
Definition: SAMRAI/FluxMethodAdapter.hpp:39
void ComputeNumericFluxes(IntegratorContext &context, int level, Duration dt, Direction dir)
Definition: SAMRAI/FluxMethodAdapter.hpp:102
Duration ComputeStableDt(IntegratorContext &context, int level, Direction dir)
Extracts the state variables patch data views including its ghost cells and compute a stable time ste...
Definition: SAMRAI/FluxMethodAdapter.hpp:72
FluxMethodAdapter(Tag, BaseMethod &&base)
Definition: SAMRAI/FluxMethodAdapter.hpp:44
FluxMethodAdapter(Tag, const BaseMethod &base)
Definition: SAMRAI/FluxMethodAdapter.hpp:43
This class is used by the HypebrolicSplitLevelIntegrator and delegates AMR related tasks to the AMReX...
Definition: SAMRAI/IntegratorContext.hpp:52
span< const int > GetScratchIds() const
Returns the MultiFab associated with level data on the specifed level number.
const SAMRAI::geom::CartesianGridGeometry & GetGeometry(int level) const
Returns the geometry object for the specified refinement level.
SAMRAI::hier::PatchLevel & GetPatchLevel(int level)
Returns the MultiFab associated with level data on the specifed level number.
span< const int > GetFluxIds() const
Returns the MultiFab associated with level data on the specifed level number.
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
constexpr index_type size() const noexcept
Returns the number of elements in the span.
Definition: span.hpp:392
Definition: CartesianPatchHierarchy.hpp:41
std::enable_if_t< std::is_pointer_v< PatchData >, void > GetPatchData(span< PatchData > patch_datas, SAMRAI::hier::Patch &patch, span< const int > data_ids)
Definition: ViewPatch.hpp:100
auto Shrink(const layout_left::mapping< Extent > &layout, Direction dir, std::ptrdiff_t n=1)
Definition: Equation.hpp:78
std::chrono::duration< double > Duration
Definition: Duration.hpp:31
Direction
This is a type safe type to denote a dimensional split direction.
Definition: Direction.hpp:30