AMDiS  2.10
The Adaptive Multi-Dimensional Simulation Toolbox
BoundaryManager.hpp
1 #pragma once
2 
3 #include <memory>
4 #include <vector>
5 
6 #include <dune/common/hybridutilities.hh>
7 #include <dune/common/std/type_traits.hh>
8 
9 #include <amdis/Boundary.hpp>
10 #include <amdis/common/Concepts.hpp>
11 
12 namespace AMDiS
13 {
15  {
16  public:
17  BoundaryManagerBase(std::size_t numBoundarySegments)
18  : boundaryIds_(numBoundarySegments, BoundaryType{1})
19  {}
20 
22  template <class Intersection>
23  BoundaryType boundaryId(Intersection const& intersection) const
24  {
25  return boundaryIds_[intersection.boundarySegmentIndex()];
26  }
27 
28  std::vector<BoundaryType> const& boundaryIds() const
29  {
30  return boundaryIds_;
31  }
32 
33  protected:
34  std::vector<BoundaryType> boundaryIds_; // maps a boundarySegmentIndex to an ID
35  };
36 
37 
39 
51  template <class G>
53  : public BoundaryManagerBase
54  {
55  using Super = BoundaryManagerBase;
56 
57  public:
58  using Grid = G;
59  enum { dim = Grid::dimension };
60  enum { dow = Grid::dimensionworld };
61 
62  using Segment = typename Grid::LevelGridView::Intersection;
63  using Domain = typename Grid::template Codim<0>::Geometry::GlobalCoordinate;
64 
65  public:
68  BoundaryManager(std::shared_ptr<G> const& grid)
69  : Super(grid->numBoundarySegments())
70  , grid_(grid)
71  {
72  setBoundaryId();
73  }
74 
75  BoundaryManager(G& grid)
76  : BoundaryManager{Dune::stackobject_to_shared_ptr(grid)}
77  {}
78 
79 
81  void setBoxBoundary(std::array<BoundaryType, 2*dow> const& ids)
82  {
83  auto gv = grid_->leafGridView();
84  for (auto const& e : elements(gv))
85  {
86  for (auto const& segment : intersections(gv,e)) {
87  if (!segment.boundary())
88  continue;
89 
90  auto n = segment.centerUnitOuterNormal();
91  auto index = segment.boundarySegmentIndex();
92 
93  for (int i = 0; i < dow; ++i) {
94  if (n[i] < -0.5)
95  boundaryIds_[index] = ids[2*i];
96  else if (n[i] > 0.5)
97  boundaryIds_[index] = ids[2*i+1];
98  }
99  }
100  }
101  }
102 
103 
105  template <class Indicator,
106  REQUIRES(Concepts::Functor<Indicator, int(Domain)>) >
107  void setIndicator(Indicator const& indicator)
108  {
109  auto gv = grid_->leafGridView();
110  for (auto const& e : elements(gv))
111  {
112  for (auto const& segment : intersections(gv,e)) {
113  if (!segment.boundary())
114  continue;
115 
116  auto index = segment.boundarySegmentIndex();
117  boundaryIds_[index] = indicator(segment.geometry().center());
118  }
119  }
120  }
121 
122 
124  template <class Predicate,
125  REQUIRES(Concepts::Functor<Predicate, bool(Domain)>) >
126  void setPredicate(Predicate const& pred, BoundaryType id)
127  {
128  auto gv = grid_->leafGridView();
129  for (auto const& e : elements(gv))
130  {
131  for (auto const& segment : intersections(gv,e)) {
132  if (!segment.boundary())
133  continue;
134 
135  auto index = segment.boundarySegmentIndex();
136  if (pred(segment.geometry().center()))
137  boundaryIds_[index] = id;
138  }
139  }
140  }
141 
142 
143  template <class I>
144  using HasBoundaryId = decltype(std::declval<I>().boundaryId());
145 
148  {
149  if (!Dune::Std::is_detected<HasBoundaryId, Segment>::value)
150  return;
151 
152  auto gv = grid_->leafGridView();
153  for (auto const& e : elements(gv))
154  {
155  for (auto const& segment : intersections(gv,e)) {
156  if (!segment.boundary())
157  continue;
158 
159  if constexpr (Dune::Std::is_detected<HasBoundaryId, Segment>::value) {
160  auto index = segment.boundarySegmentIndex();
161  boundaryIds_[index] = segment.boundaryId();
162  }
163  }
164  }
165  }
166 
167  template <class I>
168  void setBoundaryIds(std::vector<I> const& ids)
169  {
170  test_exit(ids.size() == boundaryIds_.size(), "Number of boundary IDs does not match!");
171  std::copy(ids.begin(), ids.end(), boundaryIds_.begin());
172  }
173 
174  private:
175  std::shared_ptr<Grid> grid_;
176  using Super::boundaryIds_;
177  };
178 
179 } // end namespace AMDiS
constexpr bool Functor
A Functor is a function F with signature Signature.
Definition: Concepts.hpp:133
void setPredicate(Predicate const &pred, BoundaryType id)
Set id for all boundary intersections with pred(center) == true.
Definition: BoundaryManager.hpp:126
constexpr bool Predicate
A predicate is a function that returns a boolean.
Definition: Concepts.hpp:141
Definition: AdaptBase.hpp:6
void setIndicator(Indicator const &indicator)
Set indicator(center) for all boundary intersections.
Definition: BoundaryManager.hpp:107
void setBoxBoundary(std::array< BoundaryType, 2 *dow > const &ids)
Set boundary ids [left,right, front,back, bottom,top] for cube domains.
Definition: BoundaryManager.hpp:81
BoundaryType boundaryId(Intersection const &intersection) const
Return the stored boundary id for the given intersection.
Definition: BoundaryManager.hpp:23
BoundaryManager(std::shared_ptr< G > const &grid)
Definition: BoundaryManager.hpp:68
void setBoundaryId()
Set boundary ids as stored in the grid, e.g. read by grid reader.
Definition: BoundaryManager.hpp:147
Definition: BoundaryManager.hpp:14
Manage boundary ids of boundary segments in a grid.
Definition: BoundaryManager.hpp:52