KASKADE 7 development version
integration.hh
Go to the documentation of this file.
1/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2/* */
3/* This file is part of the library KASKADE 7 */
4/* https://www.zib.de/research/projects/kaskade7-finite-element-toolbox */
5/* */
6/* Copyright (C) 2002-2019 Zuse Institute Berlin */
7/* */
8/* KASKADE 7 is distributed under the terms of the ZIB Academic License. */
9/* see $KASKADE/academic.txt */
10/* */
11/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
12
13#ifndef FEM_INTEGRATION_HH
14#define FEM_INTEGRATION_HH
15
22#include <dune/geometry/quadraturerules.hh>
23
24#include "fem/functionspace.hh"
25#include "fem/forEach.hh"
26
27namespace Kaskade
28{
30 namespace Integration_Detail
31 {
32 // C++ 14 has polymorphic lambdas..., but without method "order".
33 template <class Function, class Functor>
34 struct G
35 {
36 G(Function const& f_, Functor& g_)
37 : f(f_), g(g_) {}
38
39 template <class Cell, class QuadPoint, class Evaluator>
40 void operator()(Cell const& cell, QuadPoint const& qp, Evaluator const& eval) const
41 {
42 g(cell,qp,f.value(eval));
43 }
44
45 template <class Evaluator>
46 int order(Evaluator const& eval) const
47 {
48 return f.order(eval);
49 }
50
51 Function const& f;
52 Functor& g;
53 };
54
55 }
57
72 template <class Function, class Functor>
73 void forEachQuadPoint(Function const& f, Functor& accumulator)
74 {
75 Integration_Detail::G<Function,Functor> gWrapper(f,accumulator);
76 // this lambda would be great, but it doesn't provide method order
77 // auto gWrapper = [&] (auto const& cellPointer, auto const& quadPoint, auto w, auto const& eval)
78 // {
79 // accumulator(cellPointer,quadPoint,f.value(eval));
80 // };
81 forEachQP(gWrapper,f.space());
82 }
83
99 template <class Functor, class Space>
100 void forEachQP(Functor& g, Space const& space)
101 {
102 typedef typename Space::Grid Grid;
103 typedef typename Grid::LeafGridView GridView;
104 typedef typename Space::Evaluator Evaluator;
105
107
108 GridView const& gridView = space.grid().leafGridView();
109
110 Evaluator evaluator(space,&sfCache);
111 for(auto const& cell : elements(gridView))
112 {
113 evaluator.moveTo(cell);
114 Dune::GeometryType gt = cell.type();
115 auto const& qr = Dune::QuadratureRules<typename Grid::ctype,Grid::dimension>::rule(gt,g.order(evaluator));
116 evaluator.useQuadratureRule(qr,0);
117
118 size_t nQuadPos = qr.size();
119 for (size_t i=0; i<nQuadPos; ++i)
120 {
121 evaluator.evaluateAt(qr[i].position(),qr,i,0);
122 g(cell,qr[i],evaluator);
123 }
124 }
125 }
126
140 template <class Functor, class Function, class Space>
141 void forEachQuadPoint(Function const& f, Functor& g, Space& space)
142 {
143 typedef typename Space::Grid Grid;
144 typedef typename Grid::LeafGridView GridView;
145 typedef typename Space::Evaluator Evaluator;
146
148 SfCache sfCache;
149
150 // typedef typename IndexSet::template Codim<0>::template Partition<Dune::All_Partition> Entity;
151 // typedef typename Entity::Iterator CellIterator;
152 // IndexSet const& indexSet = space.grid().leafIndexSet();
153 typedef typename GridView::template Codim<0>::Iterator CellIterator ;
154 GridView const& gridView = f.space().grid().leafGridView() ;
155
156 Evaluator evaluator(space, &sfCache);
157
158 for (CellIterator ci=gridView.template begin<0,Dune::All_Partition>();
159 ci!=gridView.template end<0,Dune::All_Partition>(); ++ci)
160 {
161 evaluator.moveTo(*ci);
162
163 Dune::GeometryType gt = ci->type();
164 auto const& qr = Dune::QuadratureRules<typename Grid::ctype,Grid::dimension>::rule(gt,f.order(evaluator));
165
166 size_t nQuadPos = qr.size();
167 for (size_t i=0; i<nQuadPos; ++i)
168 g(typename GridView::template Codim<0>::EntityPointer(ci),qr[i],f.value(*ci,qr[i].position()));
169 }
170 }
171
182 template <class FEFunction>
183 typename FEFunction::ValueType integrateOverIntersection(FEFunction const& function,
184 typename FEFunction::Space::GridView::Intersection const& intersection,
185 typename FEFunction::Space::Evaluator& evaluator)
186 {
187 typename FEFunction::Space::Grid::template Codim<0>::Entity cell = intersection.inside(); //it is important to assign the temporary object to a local variable since the evaluator stores the address of the cell (as pointer) and dereferences it later
188 evaluator.moveTo(cell);
189 typename FEFunction::ValueType integral(0.0);
190 auto const& refElem = evaluator.shapeFunctions().referenceElement();
191 auto const& qr = Dune::QuadratureRules<typename FEFunction::Space::ctype,FEFunction::Space::dim-1>::rule(intersection.type(),function.order(evaluator));
192 int nQuadPos = qr.size();
193 for (int i=0; i<nQuadPos; ++i)
194 {
195 auto localPositionInCell = refElem.template geometry<1>(intersection.indexInInside()).global(qr[i].position());
196 evaluator.evaluateAt(localPositionInCell,qr,i,intersection.indexInInside());
197 integral += qr[i].weight()*intersection.geometry().integrationElement(qr[i].position())*function.value(evaluator);
198 }
199 return integral;
200 }
210 template <class FEFunction>
211 typename FEFunction::ValueType integrateOverBoundary(FEFunction const& function)
212 {
214 typename FEFunction::Space::Evaluator evaluator(function.space(),&sfCache);
215 typename FEFunction::Space::GridView const& gridView = function.space().gridView();
216 typename FEFunction::ValueType integral(0.0);
217 forEachBoundaryFace(gridView,[&](typename FEFunction::Space::GridView::Intersection const& intersection)
218 {
219 integral += integrateOverIntersection(function,intersection,evaluator);
220 });
221 return integral;
222 }
234 template <template <class, class> class DomainMapper, typename Scalar, typename GridView, int m>
235 typename FunctionSpaceElement<FEFunctionSpace<BoundaryMapper<DomainMapper, Scalar, GridView>>,m>::ValueType
237 {
240 typename FEFunction::Space::Evaluator evaluator(function.space(),&sfCache);
241 GridView const& gridView = function.space().gridView();
242 BoundaryMapper<DomainMapper, Scalar, GridView> const& mapper = function.space().mapper();
243 typename FEFunction::ValueType integral(0.0);
244 forEachBoundaryFace(gridView,[&](typename GridView::Intersection const& intersection) {
245 if(mapper.inDomain(intersection)) integral += integrateOverIntersection(function,intersection,evaluator);
246 });
247 return integral;
248 }
249
250
251 // ---------------------------------------------------------------------------------------------
252
253 template <typename Space>
255 {
256 typedef typename Space::Grid Grid;
257 typedef typename Space::GridView GridView;
258
259 typedef typename Grid::template Codim<0>::Entity Cell;
260 typedef Dune::QuadraturePoint<typename Grid::ctype,Grid::dimension> QuadPoint;
261 typedef typename Space::template Element<1>::type::ValueType ValueType;
262
263 public:
264 Integrator(): sum(0.0) {}
265
266 template <class CellPointer>
267 void operator()(CellPointer const& c, QuadPoint const& q, ValueType v)
268 {
269 v *= q.weight()*c->geometry().integrationElement(q.position());
270 sum += v;
271 }
272
273 ValueType getIntegral() { return sum; }
274 private:
275 ValueType sum;
276 };
277
287 template <typename Function,
288 class = typename Function::Space>
290 {
291 typename Function::ValueType sum(0.0);
292 auto integrator = [&](auto const& cell, auto const& qp, auto const& val)
293 {
294 sum += qp.weight()*cell.geometry().integrationElement(qp.position())*val;
295 };
296 forEachQuadPoint(f,integrator);
297 return sum;
298 }
299
300 template <class WeakFunctionView, class Space>
301 auto integral(WeakFunctionView const& f, Space const& space)
302 {
303 Integrator<Space> integrator;
304 forEachQuadPoint(f,integrator,space);
305 return integrator.getIntegral();
306 }
307
308 //---------------------------------------------------------------------
310 template <typename Space>
311 class [[deprecated("Use integral() functions directly. Wil be removed after 2019-12-31.")]] Integral
312 {
313 public:
314 template<typename Function>
316 {
317 return integral(f);
318 }
319
320 template<typename Function>
321 typename Function::ValueType operator()(Function const& f, typename Function::Space const& space)
322 {
323 return integral(f,space);
324 }
325 };
326
327
330 {
332 template<typename Function>
333 typename Function::Scalar id(Function const& f) const
334 {
336 return integral(f);
337 }
338
340 template<typename Function>
341 typename Function::Scalar operator()(Function const& f) const
342 {
343 return id(f);
344 }
345 };
346
347
348} // end of namespace Kaskade
349#endif //FEM_INTEGRATION_HH
Function is the interface for evaluatable functions .
Definition: concepts.hh:324
int order(Cell const &cell) const
unspecified Scalar
Definition: concepts.hh:330
ValueType value(Cell const &cell, Dune::FieldVector< typename Cell::Geometry::ctype, Cell::dimension > const &localCoordinate) const
A local to global mapper implementation for boundary spaces, with functions defined on the domain bou...
bool inDomain(typename GridView::Intersection const &face) const
A representation of a finite element function space defined over a domain covered by a grid.
A class for representing finite element functions.
Evaluation class for integrals.
Definition: integration.hh:312
Function::ValueType operator()(Function const &f, typename Function::Space const &space)
Definition: integration.hh:321
Function::ValueType operator()(Function const &f)
Definition: integration.hh:315
void operator()(CellPointer const &c, QuadPoint const &q, ValueType v)
Definition: integration.hh:267
ValueType getIntegral()
Definition: integration.hh:273
This class caches values and derivatives of shape functions at quadrature points.
A function that supports efficient evaluation via cell and local coordinate.
Definition: concepts.hh:602
FEFunctionSpace and FunctionSpaceElement and the like.
void forEachQuadPoint(Function const &f, Functor &accumulator)
Loops over the whole domain occupied by the grid and calls an accumulator for each integration point ...
Definition: integration.hh:73
Function::ValueType integral(Function const &f)
Evaluate integrals of finite element functions.
Definition: integration.hh:289
void forEachQP(Functor &g, Space const &space)
Loops over the whole domain occupied by the grid and calls the functor g for each integration point w...
Definition: integration.hh:100
FEFunction::ValueType integrateOverIntersection(FEFunction const &function, typename FEFunction::Space::GridView::Intersection const &intersection, typename FEFunction::Space::Evaluator &evaluator)
integrateOverIntersection computes the integral of an FE function over a grid intersection (face).
Definition: integration.hh:183
FEFunction::ValueType integrateOverBoundary(FEFunction const &function)
integrateOverBoundary computes the integral of an FE function over the whole boundary of the underlyi...
Definition: integration.hh:211
void forEachBoundaryFace(GridView const &gridView, Functor functor)
iterates over each boundary face and applies functor to face. Each boundary face is visited exactly o...
Definition: forEach.hh:88
typename GridView::template Codim< 0 >::Entity Cell
The type of cells (entities of codimension 0) in the grid view.
Definition: gridBasics.hh:35
Function::Scalar operator()(Function const &f) const
Evaluation of norm (??? that's not true)
Definition: integration.hh:341
Function::Scalar id(Function const &f) const
Evaluation of integral.
Definition: integration.hh:333