AMDiS 2.11-git
The Adaptive Multi-Dimensional Simulation Toolbox
 
Loading...
Searching...
No Matches
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
12namespace 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 {
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 SegmentImpl = typename Grid::LevelGridView::Intersection::Implementation;
64 using Domain = typename Grid::template Codim<0>::Geometry::GlobalCoordinate;
65
66 public:
69 BoundaryManager(std::shared_ptr<G> const& grid)
70 : Super(grid->numBoundarySegments())
71 , grid_(grid)
72 {
74 }
75
76 BoundaryManager(G& grid)
77 : BoundaryManager{Dune::stackobject_to_shared_ptr(grid)}
78 {}
79
80
82 void setBoxBoundary(std::array<BoundaryType, 2*dow> const& ids)
83 {
84 auto gv = grid_->leafGridView();
85 for (auto const& e : elements(gv))
86 {
87 for (auto const& segment : intersections(gv,e)) {
88 if (!segment.boundary())
89 continue;
90
91 auto n = segment.centerUnitOuterNormal();
92 auto index = segment.boundarySegmentIndex();
93
94 for (int i = 0; i < dow; ++i) {
95 if (n[i] < -0.5)
96 boundaryIds_[index] = ids[2*i];
97 else if (n[i] > 0.5)
98 boundaryIds_[index] = ids[2*i+1];
99 }
100 }
101 }
102 }
103
104
106 template <class Indicator,
107 REQUIRES(Concepts::Functor<Indicator, int(Domain)>) >
108 void setIndicator(Indicator const& indicator)
109 {
110 auto gv = grid_->leafGridView();
111 for (auto const& e : elements(gv))
112 {
113 for (auto const& segment : intersections(gv,e)) {
114 if (!segment.boundary())
115 continue;
116
117 auto index = segment.boundarySegmentIndex();
118 boundaryIds_[index] = indicator(segment.geometry().center());
119 }
120 }
121 }
122
123
125 template <class Predicate,
126 REQUIRES(Concepts::Functor<Predicate, bool(Domain)>) >
127 void setPredicate(Predicate const& pred, BoundaryType id)
128 {
129 auto gv = grid_->leafGridView();
130 for (auto const& e : elements(gv))
131 {
132 for (auto const& segment : intersections(gv,e)) {
133 if (!segment.boundary())
134 continue;
135
136 auto index = segment.boundarySegmentIndex();
137 if (pred(segment.geometry().center()))
138 boundaryIds_[index] = id;
139 }
140 }
141 }
142
143
144 template <class I>
145 using HasBoundaryId = decltype(std::declval<I>().boundaryId());
146
149 {
150 if (!Dune::Std::is_detected<HasBoundaryId, SegmentImpl>::value)
151 return;
152
153 auto gv = grid_->leafGridView();
154 for (auto const& e : elements(gv))
155 {
156 for (auto const& segment : intersections(gv,e)) {
157 if (!segment.boundary())
158 continue;
159
160 if constexpr (Dune::Std::is_detected<HasBoundaryId, SegmentImpl>::value) {
161 auto index = segment.boundarySegmentIndex();
162 boundaryIds_[index] = segment.impl().boundaryId();
163 }
164 }
165 }
166 }
167
168 template <class I>
169 void setBoundaryIds(std::vector<I> const& ids)
170 {
171 test_exit(ids.size() == boundaryIds_.size(), "Number of boundary IDs does not match!");
172 std::copy(ids.begin(), ids.end(), boundaryIds_.begin());
173 }
174
175 private:
176 std::shared_ptr<Grid> grid_;
177 using Super::boundaryIds_;
178 };
179
180} // end namespace AMDiS
Definition BoundaryManager.hpp:15
BoundaryType boundaryId(Intersection const &intersection) const
Return the stored boundary id for the given intersection.
Definition BoundaryManager.hpp:23
Manage boundary ids of boundary segments in a grid.
Definition BoundaryManager.hpp:54
void setBoundaryId()
Set boundary ids as stored in the grid, e.g. read by grid reader.
Definition BoundaryManager.hpp:148
BoundaryManager(std::shared_ptr< G > const &grid)
Definition BoundaryManager.hpp:69
void setIndicator(Indicator const &indicator)
Set indicator(center) for all boundary intersections.
Definition BoundaryManager.hpp:108
void setPredicate(Predicate const &pred, BoundaryType id)
Set id for all boundary intersections with pred(center) == true.
Definition BoundaryManager.hpp:127
void setBoxBoundary(std::array< BoundaryType, 2 *dow > const &ids)
Set boundary ids [left,right, front,back, bottom,top] for cube domains.
Definition BoundaryManager.hpp:82
constexpr bool Functor
A Functor is a function F with signature Signature.
Definition Concepts.hpp:133