Finite Volume Solver  prototype
A framework to build finite volume solvers for the AG Klein at the Freie Universität Berlin.
ReflectiveBoundary2.hpp
Go to the documentation of this file.
1 // Copyright (c) 2020 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_CUTCELL_BOUNDARY_CONDITION_REFLECTIVE_BOUNDARY2_HPP
22 #define FUB_AMREX_CUTCELL_BOUNDARY_CONDITION_REFLECTIVE_BOUNDARY2_HPP
23 
25 
26 #include "fub/AMReX/ForEachFab.hpp"
28 #include "fub/Execution.hpp"
30 
31 namespace fub::amrex::cutcell {
32 
33 /// \ingroup BoundaryCondition
34 ///
35 /// \brief This boundary condition provides wall boundary which acts only on a
36 /// specified subset of ghost cells.
37 template <typename Tag, typename Equation> class ReflectiveBoundary2 {
38 public:
39  /// \brief Constructs the boundary condition with respective execution tag.
40  ReflectiveBoundary2(Tag, const Equation& equation, Direction dir, int side,
41  const ::amrex::Box& boundary_section);
42 
43  /// \brief Delegates the construction to the tag constructor.
44  ReflectiveBoundary2(const Equation& equation, Direction dir, int side,
45  const ::amrex::Box& boundary_section)
46  : ReflectiveBoundary2(Tag(), equation, dir, side, boundary_section) {}
47 
48  /// \brief Fill the boundary section with reflected states. The reflected
49  /// state is taken from a mirrored index by given direction and side.
50  void FillBoundary(::amrex::MultiFab& mf, const GriddingAlgorithm& gridding,
51  int level);
52 
53  /// \brief Conditionally fill the boundary section with reflected states, if
54  /// dir == dir_. The reflected state is taken from a mirrored index by given
55  /// direction and side.
56  void FillBoundary(::amrex::MultiFab& mf, const GriddingAlgorithm& gridding,
57  int level, Direction dir);
58 
59 private:
60  /// \brief The equation defines how to reflect states.
61  ///
62  /// This needs to be Local'ized because the implementation might use OpenMP.
65  int side_;
67 };
68 
69 template <typename Tag, typename Equation>
71  Tag, const Equation& equation, Direction dir, int side,
72  const ::amrex::Box& boundary_section)
73  : equation_{Local<Tag, Equation>{equation}}, dir_{dir}, side_{side},
74  boundary_section_{boundary_section} {}
75 
76 template <typename Tag, typename Equation>
78  ::amrex::MultiFab& mf, const GriddingAlgorithm& gridding, int level,
79  Direction dir) {
80  if (dir == dir_) {
81  FillBoundary(mf, gridding, level);
82  }
83 }
84 
85 template <typename Tag, typename Equation>
87  ::amrex::MultiFab& mf, const GriddingAlgorithm& grid, int level) {
88  static constexpr int Rank = Equation::Rank();
89  static constexpr std::size_t sRank = static_cast<std::size_t>(Rank);
90  const Eigen::Matrix<double, Rank, 1> unit = UnitVector<Rank>(dir_);
91  const ::amrex::MultiFab& alphas =
92  grid.GetPatchHierarchy().GetEmbeddedBoundary(level)->getVolFrac();
93  ForEachFab(Tag(), mf, [&](const ::amrex::MFIter& mfi) {
94  Complete<Equation> state(*equation_);
95  Complete<Equation> reflected(*equation_);
96  Complete<Equation> zeros(*equation_);
97  ::amrex::FArrayBox& fab = mf[mfi];
98  const ::amrex::FArrayBox& alpha = alphas[mfi];
99  ::amrex::Box box_to_fill = mfi.growntilebox() & boundary_section_;
100  if (!box_to_fill.isEmpty()) {
101  auto states =
102  MakeView<Complete<Equation>>(fab, *equation_, mfi.growntilebox());
103  auto box = AsIndexBox<Rank>(box_to_fill);
104  ForEachIndex(box, [&](auto... is) {
105  std::array<std::ptrdiff_t, sRank> dest{is...};
106  std::array<std::ptrdiff_t, sRank> src =
107  ReflectIndex(dest, box, dir_, side_);
108  ::amrex::IntVect iv{
109  AMREX_D_DECL(int(src[0]), int(src[1]), int(src[2]))};
110  ::amrex::IntVect dest_iv{
111  AMREX_D_DECL(int(dest[0]), int(dest[1]), int(dest[2]))};
112  if (alpha(dest_iv) > 0.0 && alpha(iv) > 0.0) {
113  Load(state, states, src);
114  FUB_ASSERT(state.density > 0.0);
115  Reflect(reflected, state, unit, *equation_);
116  Store(states, reflected, dest);
117  } else {
118  Store(states, zeros, dest);
119  }
120  });
121  }
122  });
123 }
124 
125 template <typename Tag, typename Equation>
128 
129 template <typename Equation>
132 
133 } // namespace fub::amrex::cutcell
134 
135 #endif
#define FUB_ASSERT(x)
Definition: assert.hpp:39
This class modifies and initializes a cutcell::PatchLevel in a cutcell::PatchHierarchy.
Definition: AMReX/cutcell/GriddingAlgorithm.hpp:56
const PatchHierarchy & GetPatchHierarchy() const noexcept
This boundary condition provides wall boundary which acts only on a specified subset of ghost cells.
Definition: ReflectiveBoundary2.hpp:37
Direction dir_
Definition: ReflectiveBoundary2.hpp:64
ReflectiveBoundary2(const Equation &equation, Direction dir, int side, const ::amrex::Box &boundary_section)
Delegates the construction to the tag constructor.
Definition: ReflectiveBoundary2.hpp:44
Local< Tag, Equation > equation_
The equation defines how to reflect states.
Definition: ReflectiveBoundary2.hpp:63
int side_
Definition: ReflectiveBoundary2.hpp:65
::amrex::Box boundary_section_
Definition: ReflectiveBoundary2.hpp:66
ReflectiveBoundary2(Tag, const Equation &equation, Direction dir, int side, const ::amrex::Box &boundary_section)
Constructs the boundary condition with respective execution tag.
Definition: ReflectiveBoundary2.hpp:70
void FillBoundary(::amrex::MultiFab &mf, const GriddingAlgorithm &gridding, int level)
Fill the boundary section with reflected states.
Definition: ReflectiveBoundary2.hpp:86
void ForEachFab(Tag, const ::amrex::FabArrayBase &fabarray, F function)
Iterate through all local FArrayBox objects in a MultiFab.
Definition: ForEachFab.hpp:34
Definition: FillCutCellData.hpp:30
ReflectiveBoundary2(const Equation &, Direction, int, const ::amrex::Box &) -> ReflectiveBoundary2< execution::SequentialTag, Equation >
void ForEachIndex(const ::amrex::Box &box, F function)
Definition: ForEachIndex.hpp:29
std::decay_t< decltype(std::declval< T >().GetEquation())> Equation
A template typedef to detect the member function.
Definition: Meta.hpp:59
Index< 1 > ReflectIndex(Index< 1 > i, const IndexBox< 1 > &domain, Direction dir, int side)
void Load(State &state, const BasicView< const State, Layout, Rank > &view, const std::array< std::ptrdiff_t, State::Equation::Rank()> &index)
Definition: State.hpp:640
Direction
This is a type safe type to denote a dimensional split direction.
Definition: Direction.hpp:30
void Reflect(Complete< IdealGasMix< 1 >> &reflected, const Complete< IdealGasMix< 1 >> &state, const Eigen::Matrix< double, 1, 1 > &normal, const IdealGasMix< 1 > &gas)
void Store(const BasicView< Conservative< Eq >, Layout, Eq::Rank()> &view, const Conservative< Eq > &state, const std::array< std::ptrdiff_t, Eq::Rank()> &index)
Definition: State.hpp:663
IndexBox< Rank > Box(const BasicView< State, Layout, Rank > &view)
Definition: State.hpp:486
typename detail::LocalType< Tag, T >::type Local
Definition: Execution.hpp:56