dune-fem  2.6-git
const.hh
Go to the documentation of this file.
1 #ifndef DUNE_FEM_FUNCTION_LOCALFUNCTION_CONST_HH
2 #define DUNE_FEM_FUNCTION_LOCALFUNCTION_CONST_HH
3 
4 #include <algorithm>
5 #include <type_traits>
6 #include <utility>
7 
8 #include <dune/common/dynvector.hh>
9 
12 
13 namespace Dune
14 {
15 
16  namespace Fem
17  {
18 
19  // External Forward Declerations
20  // -----------------------------
21 
22  template< class >
23  struct DiscreteFunctionTraits;
24 
25  class HasLocalFunction;
26  class IsDiscreteFunction;
27  struct BindableFunction;
28 
29  // BasicConstLocalFunction
30  // -----------------------
31 
32  template < class BasisFunctionSet, class LocalDofVector >
34  : public LocalFunction< BasisFunctionSet, LocalDofVector >
35  {
38 
39  public:
41  typedef typename BaseType::DofType DofType;
42 
45 
48 
51 
53  typedef typename BaseType::SizeType SizeType;
54 
57 
59 
61 
64  {}
65 
67 
70  {}
71 
72  BasicConstLocalFunction ( const BaseType &other ) : BaseType( other ) {}
73 
74  BasicConstLocalFunction ( const ThisType &other ) : BaseType( static_cast<const BaseType &>( other ) ) {}
75  BasicConstLocalFunction ( ThisType && other ) : BaseType( static_cast<BaseType&&>(other) ) {}
76 
77  const DofType &operator[] ( SizeType i ) const { return static_cast< const BaseType & >( *this )[ i ]; }
78  const DofType &operator[] ( SizeType i ) { return static_cast< const BaseType & >( *this )[ i ]; }
79 
81 
82  protected:
83  using BaseType::clear;
84  using BaseType::assign;
85  using BaseType::operator +=;
86  using BaseType::operator -=;
87  using BaseType::axpy;
88  };
89 
107  template< class DiscreteFunction >
109  : public BasicConstLocalFunction<
110  typename DiscreteFunctionTraits< std::remove_const_t< DiscreteFunction > >::DiscreteFunctionSpaceType::BasisFunctionSetType,
111  Dune::DynamicVector< typename DiscreteFunctionTraits< std::remove_const_t< DiscreteFunction > >::DofType,
112  typename DiscreteFunctionTraits< std::remove_const_t< DiscreteFunction > >::LocalDofVectorAllocatorType
113  :: template rebind< typename DiscreteFunctionTraits< std::remove_const_t< DiscreteFunction > > ::DofType > ::other > >
114  {
116  typedef BasicConstLocalFunction< typename DiscreteFunctionTraits< std::remove_const_t< DiscreteFunction > >::DiscreteFunctionSpaceType::BasisFunctionSetType,
117  Dune::DynamicVector< typename DiscreteFunctionTraits< std::remove_const_t< DiscreteFunction > >::DofType,
118  typename DiscreteFunctionTraits< std::remove_const_t< DiscreteFunction > > :: LocalDofVectorAllocatorType
119  :: template rebind< typename DiscreteFunctionTraits< std::remove_const_t< DiscreteFunction > >::DofType >::other > >
120  BaseType;
121 
122  public:
123  typedef std::remove_const_t< DiscreteFunction > DiscreteFunctionType;
124  typedef typename DiscreteFunctionType::DiscreteFunctionSpaceType DiscreteFunctionSpaceType;
125 
127 
128  typedef typename BaseType::DofType DofType;
132  typedef typename BaseType::RangeType RangeType;
135 
150  : BaseType( LocalDofVectorType( df.localDofVectorAllocator() ) ),
151  discreteFunction_( &df )
152  {
153  }
154 
156  ConstLocalDiscreteFunction ( const typename DiscreteFunctionType::LocalFunctionType &localFunction )
157  : BaseType( localFunction.basisFunctionSet(), LocalDofVectorType( localFunction.size(), localFunction.discreteFunction().localDofVectorAllocator() ) ),
158  discreteFunction_( &localFunction.discreteFunction() )
159  {
160  std::copy( localFunction.localDofVector().begin(), localFunction.localDofVector().end(), localDofVector().begin() );
161  }
162 
177  : BaseType( df.space().basisFunctionSet( entity ), LocalDofVectorType( df.localDofVectorAllocator() ) ),
178  discreteFunction_( &df )
179  {
180  discreteFunction().getLocalDofs( entity, localDofVector() );
181  }
182 
185  : BaseType( static_cast<const BaseType &>( other ) ),
187  {}
188 
191  : BaseType( static_cast< BaseType &&>( other ) ),
193  {}
194 
196 
197  using BaseType::evaluate;
198  using BaseType::jacobian;
199  using BaseType::hessian;
200 
206  template< class Point >
207  RangeType evaluate ( const Point &p ) const
208  {
209  RangeType val;
210  evaluate( p, val );
211  return val;
212  }
213 
222  template< class Point >
223  JacobianRangeType jacobian ( const Point &p ) const
224  {
225  JacobianRangeType jac;
226  jacobian( p, jac );
227  return jac;
228  }
229 
238  template< class Point >
239  HessianRangeType hessian ( const Point &p ) const
240  {
242  hessian( p, h );
243  return h;
244  }
245 
247  void init ( const EntityType &entity )
248  {
250  discreteFunction().getLocalDofs( entity, localDofVector() );
251  }
252 
253  void bind ( const EntityType &entity ) { init( entity ); }
254  void unbind () {}
255 
257  const GridFunctionType &gridFunction() const { return discreteFunction(); }
258 
259  protected:
261  };
262 
263 
264 
265  // ConstLocalFunction
266  // ------------------
267 
268  namespace Impl
269  {
270 
271  template< class GF, class = void >
272  struct ConstLocalFunction;
273 
274  template< class GF >
275  struct ConstLocalFunction< GF, std::enable_if_t< std::is_base_of< Fem::HasLocalFunction, GF >::value && std::is_base_of< Fem::IsDiscreteFunction, GF >::value > >
276  {
278  };
279 
280  template< class GF >
281  struct ConstLocalFunction< GF, std::enable_if_t< std::is_base_of< Fem::HasLocalFunction, GF >::value && !std::is_base_of< Fem::IsDiscreteFunction, GF >::value && std::is_class< typename GF::LocalFunctionType >::value > >
282  {
283  struct Type
284  : public GF::LocalFunctionType
285  {
286  typedef GF GridFunctionType;
287  typedef typename GridFunctionType::LocalFunctionType::EntityType EntityType;
288 
289  typedef typename GF::LocalFunctionType::RangeType RangeType;
290  typedef typename GF::LocalFunctionType::JacobianRangeType JacobianRangeType;
291  typedef typename GF::LocalFunctionType::HessianRangeType HessianRangeType;
292 
293  explicit Type ( const GridFunctionType &gridFunction )
294  : GridFunctionType::LocalFunctionType( gridFunction ),
295  gridFunction_( gridFunction )
296  {}
297 
298  using GF::LocalFunctionType::evaluate;
299  using GF::LocalFunctionType::jacobian;
300  using GF::LocalFunctionType::hessian;
301  using GF::LocalFunctionType::init;
302  using GF::LocalFunctionType::entity;
303 
305  template< class Point >
306  RangeType evaluate ( const Point &p ) const
307  {
308  RangeType val;
309  evaluate( p, val );
310  return val;
311  }
312 
314  template< class Point >
315  JacobianRangeType jacobian ( const Point &p ) const
316  {
317  JacobianRangeType jac;
318  jacobian( p, jac );
319  return jac;
320  }
321 
323  template< class Point >
324  HessianRangeType hessian ( const Point &p ) const
325  {
326  HessianRangeType h;
327  hessian( p, h );
328  return h;
329  }
330 
331  void bind ( const EntityType &entity ) { init( entity ); }
332  void unbind () {}
333 
334  const GridFunctionType &gridFunction () const { return gridFunction_; }
335 
336  private:
337  const GridFunctionType &gridFunction_;
338  };
339  };
340 
341  template< class GF >
342  struct ConstLocalFunction< GF, std::enable_if_t< std::is_base_of< Fem::BindableFunction, GF >::value && !std::is_base_of< Fem::IsDiscreteFunction, GF >::value > >
343  {
344  struct Type
345  {
346  typedef GF GridFunctionType;
347  typedef typename GF::EntityType EntityType;
348  typedef typename GF::RangeFieldType RangeFieldType;
349  typedef typename GF::RangeType RangeType;
350  typedef typename GF::JacobianRangeType JacobianRangeType;
351  typedef typename GF::HessianRangeType HessianRangeType;
352 
353  explicit Type ( const GridFunctionType &gridFunction )
354  : gridFunction_( gridFunction )
355  {}
356 
357  template <class Point>
358  void evaluate(const Point &x, RangeType &ret) const
359  {
360  gridFunction().evaluate(x,ret);
361  }
362  template <class Point>
363  void jacobian(const Point &x, JacobianRangeType &ret) const
364  {
365  gridFunction().jacobian(x,ret);
366  }
367  template <class Point>
368  void hessian(const Point &x, HessianRangeType &ret) const
369  {
370  gridFunction().hessian(x,ret);
371  }
372  unsigned int order() const { return gridFunction().order(); }
373 
375  template< class Point >
376  RangeType evaluate ( const Point &p ) const
377  {
378  RangeType val;
379  evaluate( p, val );
380  return val;
381  }
382 
384  template< class Point >
385  JacobianRangeType jacobian ( const Point &p ) const
386  {
387  JacobianRangeType jac;
388  jacobian( p, jac );
389  return jac;
390  }
391 
393  template< class Point >
394  HessianRangeType hessian ( const Point &p ) const
395  {
396  HessianRangeType h;
397  hessian( p, h );
398  return h;
399  }
400 
401  template< class Quadrature, class ... Vectors >
402  void evaluateQuadrature ( const Quadrature &quad, Vectors & ... values ) const
403  {
404  static_assert( sizeof...( Vectors ) > 0, "evaluateQuadrature needs to be called with at least one vector." );
405  std::ignore = std::make_tuple( ( evaluateQuadrature( quad, values ), 1 ) ... );
406  }
407 
408  template< class Quadrature, class Vector >
409  auto evaluateQuadrature ( const Quadrature &quad, Vector &v ) const
410  -> std::enable_if_t< std::is_same< std::decay_t< decltype(v[ 0 ]) >, RangeType >::value >
411  {
412  for( const auto qp : quad )
413  v[ qp.index() ] = evaluate( qp );
414  }
415 
416  template< class Quadrature, class Vector >
417  auto evaluateQuadrature ( const Quadrature &quad, Vector &v ) const
418  -> std::enable_if_t< std::is_same< std::decay_t< decltype(v[ 0 ]) >, JacobianRangeType >::value >
419  {
420  for( const auto qp : quad )
421  v[ qp.index() ] = jacobian( qp );
422  }
423 
424  template< class Quadrature, class Vector >
425  auto evaluateQuadrature ( const Quadrature &quad, Vector &v ) const
426  -> std::enable_if_t< std::is_same< std::decay_t< decltype(v[ 0 ]) >, HessianRangeType >::value >
427  {
428  for( const auto qp : quad )
429  v[ qp.index() ] = hessian( qp );
430  }
431 
432  void bind ( const EntityType &entity ) { gridFunction_.bind( entity ); }
433  void unbind () { gridFunction().unbind(); }
434 
435  const EntityType& entity() const
436  {
437  return gridFunction_.entity();
438  }
439 
440  const GridFunctionType &gridFunction () const { return gridFunction_; }
441 
442  private:
443  GridFunctionType &gridFunction () { return gridFunction_; }
444  GridFunctionType gridFunction_;
445  };
446  };
447  } // namespace Impl
448 
449 
450  template< class GridFunction >
451  using ConstLocalFunction = typename Impl::ConstLocalFunction< GridFunction >::Type;
452 
453  } // namespace Fem
454 
455 } // namespace Dune
456 
457 #endif // #ifndef DUNE_FEM_FUNCTION_LOCALFUNCTION_CONST_HH
Definition: bindguard.hh:11
typename Impl::ConstLocalFunction< GridFunction >::Type ConstLocalFunction
Definition: const.hh:451
Definition: explicitfieldvector.hh:75
Traits class for a DiscreteFunction.
Definition: common/discretefunction.hh:60
Definition: const.hh:35
BasicConstLocalFunction(const BasisFunctionSetType &basisFunctionSet, LocalDofVectorType &&localDofVector)
Definition: const.hh:68
BasicConstLocalFunction(const BasisFunctionSetType &basisFunctionSet, const LocalDofVectorType &localDofVector)
Definition: const.hh:62
BasicConstLocalFunction(const BaseType &other)
Definition: const.hh:72
BasicConstLocalFunction(ThisType &&other)
Definition: const.hh:75
BaseType::SizeType SizeType
type of SizeType
Definition: const.hh:53
const DofType & operator[](SizeType i) const
Definition: const.hh:77
BaseType ::LocalDofVectorType LocalDofVectorType
type of LocalDofVector
Definition: const.hh:50
const LocalDofVectorType & localDofVector() const
return const reference to local Dof Vector
Definition: localfunction.hh:396
BasicConstLocalFunction(const BasisFunctionSetType &basisFunctionSet)
Definition: const.hh:58
BaseType ::BasisFunctionSetType BasisFunctionSetType
type of BasisFunctionSet
Definition: const.hh:47
BasicConstLocalFunction(LocalDofVectorType &&localDofVector)
Definition: const.hh:66
BasicConstLocalFunction(const ThisType &other)
Definition: const.hh:74
BasicConstLocalFunction(const LocalDofVectorType &localDofVector)
Definition: const.hh:60
BaseType::DofType DofType
type of Dof
Definition: const.hh:41
BasicConstLocalFunction()
default ctor
Definition: const.hh:56
BaseType ::EntityType EntityType
type of Entity
Definition: const.hh:44
A constant local function carrying values for one entity.
Definition: const.hh:114
const GridFunctionType & gridFunction() const
Definition: const.hh:257
BaseType::HessianRangeType HessianRangeType
Definition: const.hh:134
BaseType::RangeType RangeType
Definition: const.hh:132
ConstLocalDiscreteFunction(ThisType &&other)
move constructor
Definition: const.hh:190
const DiscreteFunctionType & discreteFunction() const
Definition: const.hh:256
ConstLocalDiscreteFunction(const DiscreteFunctionType &df)
constructor creating a local function without binding it to an entity
Definition: const.hh:149
RangeType evaluate(const Point &p) const
evaluate the local function
Definition: const.hh:207
const DiscreteFunctionType * discreteFunction_
Definition: const.hh:260
std::remove_const_t< DiscreteFunction > DiscreteFunctionType
Definition: const.hh:123
void init(const EntityType &entity)
interface for local functions :: init
Definition: const.hh:247
ConstLocalDiscreteFunction(const ThisType &other)
copy constructor
Definition: const.hh:184
BaseType::LocalDofVectorType LocalDofVectorType
Definition: const.hh:131
ConstLocalDiscreteFunction(const DiscreteFunctionType &df, const EntityType &entity)
constructor creating a local function and binding it to an entity
Definition: const.hh:176
const LocalDofVectorType & localDofVector() const
return const reference to local Dof Vector
Definition: localfunction.hh:396
DiscreteFunctionType GridFunctionType
Definition: const.hh:126
BaseType::DofType DofType
Definition: const.hh:128
DiscreteFunctionType::DiscreteFunctionSpaceType DiscreteFunctionSpaceType
Definition: const.hh:124
ConstLocalDiscreteFunction(const typename DiscreteFunctionType::LocalFunctionType &localFunction)
cast a MutableLocalFunction into this one !!! expensive !!!
Definition: const.hh:156
BaseType::BasisFunctionSetType BasisFunctionSetType
Definition: const.hh:130
BaseType::JacobianRangeType JacobianRangeType
Definition: const.hh:133
void bind(const EntityType &entity)
Definition: const.hh:253
HessianRangeType hessian(const Point &p) const
evaluate Hessian of the local function
Definition: const.hh:239
BaseType::EntityType EntityType
Definition: const.hh:129
JacobianRangeType jacobian(const Point &p) const
evaluate Jacobian of the local function
Definition: const.hh:223
void unbind()
Definition: const.hh:254
interface for local functions
Definition: localfunction.hh:44
void evaluate(const PointType &x, RangeType &ret) const
evaluate the local function
Definition: localfunction.hh:307
ThisType & axpy(const RangeFieldType s, const LocalFunction< BasisFunctionSet, T > &other)
add a multiple of another local function to this one
Definition: localfunction.hh:204
BasisFunctionSet BasisFunctionSetType
type of basis function set
Definition: localfunction.hh:49
void hessian(const PointType &x, HessianRangeType &ret) const
evaluate Hessian of the local function
Definition: localfunction.hh:335
void init(const BasisFunctionSetType &basisFunctionSet)
Definition: localfunction.hh:295
FunctionSpaceType::RangeType RangeType
type of range vectors, i.e., type of function values
Definition: localfunction.hh:73
const BasisFunctionSetType & basisFunctionSet() const
obtain the basis function set for this local function
Definition: localfunction.hh:286
const LocalDofVectorType & localDofVector() const
return const reference to local Dof Vector
Definition: localfunction.hh:396
void jacobian(const PointType &x, JacobianRangeType &ret) const
evaluate Jacobian of the local function
Definition: localfunction.hh:321
FunctionSpaceType::JacobianRangeType JacobianRangeType
type of the Jacobian, i.e., type of evaluated Jacobian matrix
Definition: localfunction.hh:75
void clear()
set all DoFs to zero
Definition: localfunction.hh:172
BasisFunctionSetType::EntityType EntityType
type of the entity, the local function lives on is given by the space
Definition: localfunction.hh:61
void assign(const LocalFunction< BasisFunctionSet, T > &other)
assign all DoFs of this local function
Definition: localfunction.hh:166
LocalDofVector LocalDofVectorType
type of local Dof Vector
Definition: localfunction.hh:52