DUNE PDELab (git)

raviartthomasbasis.hh
1// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2// vi: set et ts=4 sw=2 sts=2:
3#ifndef DUNE_FUNCTIONS_FUNCTIONSPACEBASES_RAVIARTTHOMASBASIS_HH
4#define DUNE_FUNCTIONS_FUNCTIONSPACEBASES_RAVIARTTHOMASBASIS_HH
5
6#include <array>
8
11
12#include <dune/localfunctions/common/localfiniteelementvariant.hh>
13#include <dune/localfunctions/raviartthomas.hh>
14#include <dune/localfunctions/raviartthomas/raviartthomas0cube2d.hh>
15#include <dune/localfunctions/raviartthomas/raviartthomas0cube3d.hh>
16#include <dune/localfunctions/raviartthomas/raviartthomas02d.hh>
17#include <dune/localfunctions/raviartthomas/raviartthomas03d.hh>
18#include <dune/localfunctions/raviartthomas/raviartthomas1cube2d.hh>
19#include <dune/localfunctions/raviartthomas/raviartthomas1cube3d.hh>
20#include <dune/localfunctions/raviartthomas/raviartthomas12d.hh>
21#include <dune/localfunctions/raviartthomas/raviartthomas2cube2d.hh>
22
23#include <dune/functions/functionspacebases/globalvaluedlocalfiniteelement.hh>
24#include <dune/functions/functionspacebases/nodes.hh>
25#include <dune/functions/functionspacebases/defaultglobalbasis.hh>
26#include <dune/functions/functionspacebases/leafprebasismixin.hh>
27
28namespace Dune {
29namespace Functions {
30
31namespace Impl {
32
33 template<int dim, typename D, typename R, std::size_t k>
34 struct RaviartThomasSimplexLocalInfo
35 {
36 // Dummy type, must be something that we can have a std::unique_ptr to
37 using FiniteElement = void*;
38 };
39
40 template<typename D, typename R>
41 struct RaviartThomasSimplexLocalInfo<2,D,R,0>
42 {
43 using FiniteElement = RT02DLocalFiniteElement<D,R>;
44 };
45
46 template<typename D, typename R>
47 struct RaviartThomasSimplexLocalInfo<2,D,R,1>
48 {
49 using FiniteElement = RT12DLocalFiniteElement<D,R>;
50 };
51
52 template<typename D, typename R>
53 struct RaviartThomasSimplexLocalInfo<3,D,R,0>
54 {
55 using FiniteElement = RT03DLocalFiniteElement<D,R>;
56 };
57
58 template<int dim, typename D, typename R, std::size_t k>
59 struct RaviartThomasCubeLocalInfo
60 {
61 // Dummy type, must be something that we can have a std::unique_ptr to
62 using FiniteElement = void*;
63 };
64
65 template<typename D, typename R>
66 struct RaviartThomasCubeLocalInfo<2,D,R,0>
67 {
68 using FiniteElement = RT0Cube2DLocalFiniteElement<D,R>;
69 };
70
71 template<typename D, typename R>
72 struct RaviartThomasCubeLocalInfo<2,D,R,1>
73 {
74 using FiniteElement = RT1Cube2DLocalFiniteElement<D,R>;
75 };
76
77 template<typename D, typename R>
78 struct RaviartThomasCubeLocalInfo<2,D,R,2>
79 {
80 using FiniteElement = RT2Cube2DLocalFiniteElement<D,R>;
81 };
82
83 template<typename D, typename R>
84 struct RaviartThomasCubeLocalInfo<3,D,R,0>
85 {
86 using FiniteElement = RT0Cube3DLocalFiniteElement<D,R>;
87 };
88
89 template<typename D, typename R>
90 struct RaviartThomasCubeLocalInfo<3,D,R,1>
91 {
92 using FiniteElement = RT1Cube3DLocalFiniteElement<D,R>;
93 };
94
95 template<typename GV, int dim, typename R, std::size_t k>
96 class RaviartThomasLocalFiniteElementMap
97 {
98 using D = typename GV::ctype;
99 constexpr static bool hasFixedElementType = Capabilities::hasSingleGeometryType<typename GV::Grid>::v;
100
101 using CubeFiniteElement = typename RaviartThomasCubeLocalInfo<dim, D, R, k>::FiniteElement;
102 using SimplexFiniteElement = typename RaviartThomasSimplexLocalInfo<dim, D, R, k>::FiniteElement;
103
104 public:
105
106 using T = LocalBasisTraits<D, dim, FieldVector<D,dim>, R, dim, FieldVector<R,dim>, FieldMatrix<D,dim,dim> >;
107
108 constexpr static unsigned int topologyId = Capabilities::hasSingleGeometryType<typename GV::Grid>::topologyId; // meaningless if hasFixedElementType is false
109 constexpr static GeometryType type = GeometryType(topologyId, GV::dimension);
110
111 using FiniteElement = std::conditional_t<hasFixedElementType,
112 std::conditional_t<type.isCube(),CubeFiniteElement,SimplexFiniteElement>,
113 LocalFiniteElementVariant<CubeFiniteElement, SimplexFiniteElement> >;
114
115 // Each element facet can have its orientation reversed, hence there are
116 // 2^#facets different variants.
117 static std::size_t numVariants(GeometryType type)
118 {
119 auto numFacets = referenceElement<D,dim>(type).size(1);
120 return power(2,numFacets);
121 }
122
123 RaviartThomasLocalFiniteElementMap(const GV& gv)
124 : elementMapper_(gv, mcmgElementLayout()),
125 orient_(gv.size(0))
126 {
127 if constexpr (hasFixedElementType)
128 {
129 variants_.resize(numVariants(type));
130 for (size_t i = 0; i < numVariants(type); i++)
131 variants_[i] = FiniteElement(i);
132 }
133 else
134 {
135 // for mixed grids add offset for cubes
136 variants_.resize(numVariants(GeometryTypes::simplex(dim)) + numVariants(GeometryTypes::cube(dim)));
137 for (size_t i = 0; i < numVariants(GeometryTypes::simplex(dim)); i++)
138 variants_[i] = SimplexFiniteElement(i);
139 for (size_t i = 0; i < numVariants(GeometryTypes::cube(dim)); i++)
140 variants_[i + numVariants(GeometryTypes::simplex(dim))] = CubeFiniteElement(i);
141 }
142
143 for(const auto& cell : elements(gv))
144 {
145 unsigned int myId = elementMapper_.index(cell);
146 orient_[myId] = 0;
147
148 for (const auto& intersection : intersections(gv,cell))
149 {
150 if (intersection.neighbor() && (elementMapper_.index(intersection.outside()) > myId))
151 orient_[myId] |= (1 << intersection.indexInInside());
152 }
153
154 // for mixed grids add offset for cubes
155 if constexpr (!hasFixedElementType)
156 if (cell.type().isCube())
157 orient_[myId] += numVariants(GeometryTypes::simplex(dim));
158 }
159 }
160
161 template<class EntityType>
162 const FiniteElement& find(const EntityType& e) const
163 {
164 return variants_[orient_[elementMapper_.index(e)]];
165 }
166
167 private:
168 std::vector<FiniteElement> variants_;
170 std::vector<unsigned char> orient_;
171 };
172
173
174} // namespace Impl
175
176
177// *****************************************************************************
178// This is the reusable part of the basis. It contains
179//
180// RaviartThomasPreBasis
181// RaviartThomasNode
182//
183// The pre-basis allows to create the others and is the owner of possible shared
184// state. These components do _not_ depend on the global basis and local view
185// and can be used without a global basis.
186// *****************************************************************************
187
188template<typename GV, int k>
189class RaviartThomasNode;
190
191template<typename GV, int k>
192class RaviartThomasPreBasis :
193 public LeafPreBasisMixin< RaviartThomasPreBasis<GV,k> >
194{
195 static const int dim = GV::dimension;
196 using FiniteElementMap = typename Impl::RaviartThomasLocalFiniteElementMap<GV, dim, double, k>;
197
198public:
199
201 using GridView = GV;
202 using size_type = std::size_t;
203
204 using Node = RaviartThomasNode<GV, k>;
205
207 RaviartThomasPreBasis(const GridView& gv) :
208 gridView_(gv),
209 finiteElementMap_(gv)
210 {
211 // Currently there are some unresolved bugs with hybrid grids and higher order Raviart-Thomas elements
212 if (gv.indexSet().types(0).size() > 1 and k>0)
213 DUNE_THROW(Dune::NotImplemented, "Raviart-Thomas basis with index k>0 is only implemented for grids with a single element type");
214
215 for(auto type : gv.indexSet().types(0))
216 if (!type.isSimplex() && !type.isCube())
217 DUNE_THROW(Dune::NotImplemented, "Raviart-Thomas elements are only implemented for grids with simplex or cube elements.");
218
219 GeometryType type = gv.template begin<0>()->type();
220 const static int dofsPerElement = type.isCube() ? ((dim == 2) ? k*(k+1)*dim : k*(k+1)*(k+1)*dim) : k*dim;
221 const static int dofsPerFace = type.isCube() ? (dim-2)*2*k+k+1 : (dim-1)*k+1 ;
222
223 dofsPerCodim_ = {{dofsPerElement, dofsPerFace}};
224 }
225
226 void initializeIndices()
227 {
228 codimOffset_[0] = 0;
229 codimOffset_[1] = codimOffset_[0] + dofsPerCodim_[0] * gridView_.size(0);
230 }
231
234 const GridView& gridView() const
235 {
236 return gridView_;
237 }
238
239 /* \brief Update the stored grid view, to be called if the grid has changed */
240 void update (const GridView& gv)
241 {
242 gridView_ = gv;
243 }
244
248 Node makeNode() const
249 {
250 return Node{&finiteElementMap_};
251 }
252
253 size_type dimension() const
254 {
255 return dofsPerCodim_[0] * gridView_.size(0) + dofsPerCodim_[1] * gridView_.size(1);
256 }
257
258 size_type maxNodeSize() const
259 {
260 size_type result = 0;
261 for (auto&& type : gridView_.indexSet().types(0))
262 {
263 size_t numFaces = ReferenceElements<double,dim>::general(type).size(1);
264 const static int dofsPerElement = type.isCube() ? ((dim == 2) ? k*(k+1)*dim : k*(k+1)*(k+1)*dim) : k*dim;
265 const static int dofsPerFace = type.isCube() ? (dim-2)*2*k+k+1 : (dim-1)*k+1 ;
266 result = std::max(result, dofsPerElement + dofsPerFace * numFaces);
267 }
268
269 return result;
270 }
271
277 template<typename It>
278 It indices(const Node& node, It it) const
279 {
280 const auto& gridIndexSet = gridView().indexSet();
281 const auto& element = node.element();
282
283 // throw if Element is not of predefined type
284 if (not(element.type().isCube()) and not(element.type().isSimplex()))
285 DUNE_THROW(Dune::NotImplemented, "RaviartThomasBasis only implemented for cube and simplex elements.");
286
287 for(std::size_t i=0, end=node.size(); i<end; ++i, ++it)
288 {
289 Dune::LocalKey localKey = node.finiteElement().localCoefficients().localKey(i);
290
291 // The dimension of the entity that the current dof is related to
292 size_t subentity = localKey.subEntity();
293 size_t codim = localKey.codim();
294
295 if (not(codim==0 or codim==1))
296 DUNE_THROW(Dune::NotImplemented, "Grid contains elements not supported for the RaviartThomasBasis");
297
298 *it = { codimOffset_[codim] +
299 dofsPerCodim_[codim] * gridIndexSet.subIndex(element, subentity, codim) + localKey.index() };
300 }
301
302 return it;
303 }
304
305protected:
306 GridView gridView_;
307 std::array<size_t,dim+1> codimOffset_;
308 FiniteElementMap finiteElementMap_;
309 // Number of dofs per entity type depending on the entity's codimension and type
310 std::array<int,dim+1> dofsPerCodim_;
311};
312
313
314
315template<typename GV, int k>
316class RaviartThomasNode :
317 public LeafBasisNode
318{
319 static const int dim = GV::dimension;
320
321public:
322
323 using size_type = std::size_t;
324 using Element = typename GV::template Codim<0>::Entity;
325 using FiniteElementMap = typename Impl::RaviartThomasLocalFiniteElementMap<GV, dim, double, k>;
326 using FiniteElement = Impl::GlobalValuedLocalFiniteElement<Impl::ContravariantPiolaTransformator,
327 typename FiniteElementMap::FiniteElement,
328 Element>;
329
330 RaviartThomasNode(const FiniteElementMap* finiteElementMap) :
331 element_(nullptr),
332 finiteElementMap_(finiteElementMap)
333 { }
334
336 const Element& element() const
337 {
338 return *element_;
339 }
340
345 const FiniteElement& finiteElement() const
346 {
347 return finiteElement_;
348 }
349
351 void bind(const Element& e)
352 {
353 element_ = &e;
354 finiteElement_.bind((finiteElementMap_->find(*element_)), e);
355 this->setSize(finiteElement_.size());
356 }
357
358protected:
359
360 FiniteElement finiteElement_;
361 const Element* element_;
362 const FiniteElementMap* finiteElementMap_;
363};
364
365namespace BasisFactory {
366
374template<std::size_t k>
376{
377 return [](const auto& gridView) {
378 return RaviartThomasPreBasis<std::decay_t<decltype(gridView)>, k>(gridView);
379 };
380}
381
382} // end namespace BasisFactory
383
384
385
386// *****************************************************************************
387// This is the actual global basis implementation based on the reusable parts.
388// *****************************************************************************
389
397template<typename GV, int k>
398using RaviartThomasBasis = DefaultGlobalBasis<RaviartThomasPreBasis<GV, k> >;
399
400} // end namespace Functions
401} // end namespace Dune
402
403
404#endif // DUNE_FUNCTIONS_FUNCTIONSPACEBASES_RAVIARTTHOMASBASIS_HH
Describe position of one degree of freedom.
Definition: localkey.hh:24
constexpr unsigned int index() const noexcept
Return offset within subentity.
Definition: localkey.hh:70
constexpr unsigned int codim() const noexcept
Return codim of associated entity.
Definition: localkey.hh:63
constexpr unsigned int subEntity() const noexcept
Return number of associated subentity.
Definition: localkey.hh:56
Implementation class for a multiple codim and multiple geometry type mapper.
Definition: mcmgmapper.hh:129
Default exception for dummy implementations.
Definition: exceptions.hh:263
A set of traits classes to store static information about grid implementation.
A few common exception classes.
GeometryType
Type representing VTK's entity geometry types.
Definition: common.hh:132
#define DUNE_THROW(E, m)
Definition: exceptions.hh:218
auto raviartThomas()
Create a pre-basis factory that can create a Raviart-Thomas pre-basis.
Definition: raviartthomasbasis.hh:375
constexpr GeometryType cube(unsigned int dim)
Returns a GeometryType representing a hypercube of dimension dim.
Definition: type.hh:462
constexpr GeometryType simplex(unsigned int dim)
Returns a GeometryType representing a simplex of dimension dim.
Definition: type.hh:453
constexpr auto max
Function object that returns the greater of the given values.
Definition: hybridutilities.hh:484
MCMGLayout mcmgElementLayout()
layout for elements (codim-0 entities)
Definition: mcmgmapper.hh:97
Mapper for multiple codim and multiple geometry types.
Dune namespace.
Definition: alignedallocator.hh:13
constexpr std::integral_constant< std::size_t, sizeof...(II)> size(std::integer_sequence< T, II... >)
Return the size of the sequence.
Definition: integersequence.hh:75
constexpr Base power(Base m, Exponent p)
Power method for integer exponents.
Definition: math.hh:75
static const ReferenceElement & general(const GeometryType &type)
get general reference elements
Definition: referenceelements.hh:156
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Jul 15, 22:36, 2024)