AMDiS  0.3
The Adaptive Multi-Dimensional Simulation Toolbox
ContextGeometry.hpp
1 #pragma once
2 
3 #include <optional>
4 #include <type_traits>
5 
6 #include <dune/common/typetraits.hh>
7 #include <dune/geometry/type.hh>
8 
9 namespace AMDiS
10 {
11  namespace Impl
12  {
13  template <class E, class = std::void_t<>>
14  struct ContextTypes
15  {
16  using Entity = E;
17  using LocalGeometry = typename E::Geometry;
18  };
19 
20  // specialization for intersections
21  template <class I>
22  struct ContextTypes<I, std::void_t<decltype(std::declval<I>().inside())>>
23  {
24  using Entity = typename I::Entity;
25  using LocalGeometry = typename I::LocalGeometry;
26  };
27 
28  } // end namespace Impl
29 
30 
32 
42  template <class LC>
44  {
45  using ContextType = Impl::ContextTypes<LC>;
46 
47  public:
48  using LocalContext = LC;
49  using Element = typename ContextType::Entity;
50  using Geometry = typename Element::Geometry;
51  using LocalGeometry = typename ContextType::LocalGeometry;
52 
53  using IsEntity = std::is_same<Element, LocalContext>;
54 
55  enum {
56  dim = Geometry::mydimension, //< the dimension of the grid element
57  dow = Geometry::coorddimension //< the dimension of the world
58  };
59 
61  ContextGeometry(LC const& localContext, Element const& element, Geometry const& geometry)
62  : localContext_(&localContext)
63  , element_(&element)
64  , geometry_(&geometry)
65  {}
66 
67  // Prevent from storing pointer to rvalue-reference
68  ContextGeometry(LC const& localContext, Element const& element, Geometry&& geometry) = delete;
69 
70  public:
72  Element const& element() const
73  {
74  return *element_;
75  }
76 
78  LocalContext const& localContext() const
79  {
80  return *localContext_;
81  }
82 
84  Geometry const& geometry() const
85  {
86  return *geometry_;
87  }
88 
90  LocalGeometry const& localGeometry() const
91  {
92  return localGeometry_impl(IsEntity{});
93  }
94 
95 
96  public:
97 
99  template <class Coordinate>
100  decltype(auto) local(Coordinate const& p) const
101  {
102  return local_impl(p, IsEntity{});
103  }
104 
106  template <class Coordinate>
107  decltype(auto) global(Coordinate const& p) const
108  {
109  return geometry_->global(p);
110  }
111 
113  Dune::GeometryType type() const
114  {
115  return localContext_->type();
116  }
117 
120  template <class Coordinate>
121  auto integrationElement(Coordinate const& p) const
122  {
123  return localGeometry().integrationElement(p);
124  }
125 
126  private: // implementation detail
127 
128  // position for elements
129  template <class Coordinate>
130  Coordinate const& local_impl(Coordinate const& p, std::true_type) const
131  {
132  return p;
133  }
134 
135  // position for intersection
136  template <class Coordinate>
137  auto local_impl(Coordinate const& p, std::false_type) const
138  {
139  return localGeometry().global(p);
140  }
141 
142  // local-geometry is the same as geometry
143  Geometry const& localGeometry_impl(std::true_type) const
144  {
145  return *geometry_;
146  }
147 
148  // local-geometry of intersection in inside element
149  LocalGeometry const& localGeometry_impl(std::false_type) const
150  {
151  if (!localGeometry_)
152  localGeometry_.emplace(localContext_->geometryInInside());
153 
154  return *localGeometry_;
155  }
156 
157  private:
158  LocalContext const* localContext_;
159  Element const* element_;
160  Geometry const* geometry_;
161 
162  // The localGeometry may be constructed only if needed
163  mutable std::optional<LocalGeometry> localGeometry_;
164  };
165 
166 
167  template <class GV>
169  {
170  public:
171  using GridView = GV;
172  using Element = typename GV::template Codim<0>::Entity;
173  using Geometry = typename Element::Geometry;
174 
175  enum {
176  dim = GridView::dimension, //< the dimension of the grid element
177  dow = GridView::dimensionworld //< the dimension of the world
178  };
179 
181  template <class E, class G>
182  GlobalContext(GV const& gridView, E&& element, G&& geometry)
183  : gridView_(gridView)
184  , element_(Dune::wrap_or_move(FWD(element)))
185  , geometry_(Dune::wrap_or_move(FWD(geometry)))
186  {}
187 
188  public:
190  GridView const& gridView() const
191  {
192  return gridView_;
193  }
194 
196  Element const& element() const
197  {
198  return *element_;
199  }
200 
202  Geometry const& geometry() const
203  {
204  return *geometry_;
205  }
206 
207  private:
208  GridView gridView_;
209  std::shared_ptr<Element const> element_;
210  std::shared_ptr<Geometry const> geometry_;
211  };
212 
213 } // end namespace AMDiS
LocalGeometry const & localGeometry() const
Return the geometry of the element, or geometryInInside of the intersection.
Definition: ContextGeometry.hpp:90
GridView const & gridView() const
Return the GridView this context is bound to.
Definition: ContextGeometry.hpp:190
Definition: ContextGeometry.hpp:168
Definition: AdaptiveGrid.hpp:373
Element const & element() const
Return the bound element (entity of codim 0)
Definition: ContextGeometry.hpp:72
Definition: FieldMatVec.hpp:12
Definition: AdaptBase.hpp:6
GlobalContext(GV const &gridView, E &&element, G &&geometry)
Constructor. Stores a copy of gridView and a pointer to element and geometry.
Definition: ContextGeometry.hpp:182
Geometry const & geometry() const
Return the geometry of the Element.
Definition: ContextGeometry.hpp:202
auto integrationElement(Coordinate const &p) const
Definition: ContextGeometry.hpp:121
Geometry const & geometry() const
Return the geometry of the Element.
Definition: ContextGeometry.hpp:84
Wrapper class for element and geometry.
Definition: ContextGeometry.hpp:43
LocalContext const & localContext() const
Return the LocalContext, either the element or an intersection.
Definition: ContextGeometry.hpp:78
Dune::GeometryType type() const
Return the geometry-type of the localContext.
Definition: ContextGeometry.hpp:113
Element const & element() const
Return the bound element (entity of codim 0)
Definition: ContextGeometry.hpp:196
ContextGeometry(LC const &localContext, Element const &element, Geometry const &geometry)
Constructor. Stores pointer to localContext, element, and geometry.
Definition: ContextGeometry.hpp:61