AMDiS  0.1
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 boundarySegementIndex 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 
76  void setBoxBoundary(std::array<BoundaryType, 2*dow> const& ids)
77  {
78  auto gv = grid_->leafGridView();
79  for (auto const& e : elements(gv))
80  {
81  for (auto const& segment : intersections(gv,e)) {
82  if (!segment.boundary())
83  continue;
84 
85  auto n = segment.centerUnitOuterNormal();
86  auto index = segment.boundarySegmentIndex();
87 
88  for (int i = 0; i < dow; ++i) {
89  if (n[i] < -0.5)
90  boundaryIds_[index] = ids[2*i];
91  else if (n[i] > 0.5)
92  boundaryIds_[index] = ids[2*i+1];
93  }
94  }
95  }
96  }
97 
98 
100  template <class Indicator,
101  REQUIRES(Concepts::Functor<Indicator, int(Domain)>) >
102  void setIndicator(Indicator const& indicator)
103  {
104  auto gv = grid_->leafGridView();
105  for (auto const& e : elements(gv))
106  {
107  for (auto const& segment : intersections(gv,e)) {
108  if (!segment.boundary())
109  continue;
110 
111  auto index = segment.boundarySegmentIndex();
112  boundaryIds_[index] = indicator(segment.geometry().center());
113  }
114  }
115  }
116 
117 
119  template <class Predicate,
120  REQUIRES(Concepts::Functor<Predicate, bool(Domain)>) >
121  void setPredicate(Predicate const& pred, BoundaryType id)
122  {
123  auto gv = grid_->leafGridView();
124  for (auto const& e : elements(gv))
125  {
126  for (auto const& segment : intersections(gv,e)) {
127  if (!segment.boundary())
128  continue;
129 
130  auto index = segment.boundarySegmentIndex();
131  if (pred(segment.geometry().center()))
132  boundaryIds_[index] = id;
133  }
134  }
135  }
136 
137 
138  template <class I>
139  using HasBoundaryId = decltype(std::declval<I>().boundaryId());
140 
143  {
144  if (!Dune::Std::is_detected<HasBoundaryId, Segment>::value)
145  return;
146 
147  auto gv = grid_->leafGridView();
148  for (auto const& e : elements(gv))
149  {
150  for (auto const& segment : intersections(gv,e)) {
151  if (!segment.boundary())
152  continue;
153 
154  if constexpr (Dune::Std::is_detected<HasBoundaryId, Segment>::value) {
155  auto index = segment.boundarySegmentIndex();
156  boundaryIds_[index] = segment.boundaryId();
157  }
158  }
159  }
160  }
161 
162  template <class I>
163  void setBoundaryIds(std::vector<I> const& ids)
164  {
165  test_exit(ids.size() == boundaryIds_.size(), "Number of boundary IDs does not match!");
166  std::copy(ids.begin(), ids.end(), boundaryIds_.begin());
167  }
168 
169  private:
170  std::shared_ptr<Grid> grid_;
171  using Super::boundaryIds_;
172  };
173 
174 } // end namespace AMDiS
constexpr bool Functor
A Functor is a function F with signature Signature.
Definition: Concepts.hpp:134
void setPredicate(Predicate const &pred, BoundaryType id)
Set id for all boundary intersections with pred(center) == true.
Definition: BoundaryManager.hpp:121
constexpr bool Predicate
A predicate is a function that returns a boolean.
Definition: Concepts.hpp:142
Contains all classes needed for solving linear and non linear equation systems.
Definition: AdaptBase.hpp:6
void setIndicator(Indicator const &indicator)
Set indicator(center) for all boundary intersections.
Definition: BoundaryManager.hpp:102
void setBoxBoundary(std::array< BoundaryType, 2 *dow > const &ids)
Set boundary ids [left,right, front,back, bottom,top] for cube domains.
Definition: BoundaryManager.hpp:76
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:142
Definition: BoundaryManager.hpp:14
Manage boundary ids of boundary segments in a grid.
Definition: BoundaryManager.hpp:52
void test_exit(bool condition, std::string const &str, Args &&... args)
test for condition and in case of failure print message and exit
Definition: Output.hpp:163