dune-fem  2.4.1-rc
gridfunctionadapter.hh
Go to the documentation of this file.
1 #ifndef DUNE_FEM_GRIDFUNCTIONADAPTER_HH
2 #define DUNE_FEM_GRIDFUNCTIONADAPTER_HH
3 
4 #include <dune/common/exceptions.hh>
5 
6 //- local includes
7 #include <dune/fem/version.hh>
10 
11 // for compatibility
13 
14 namespace Dune
15 {
16 
17  namespace Fem
18  {
19 
34  //- forward declaration
35  template <class FunctionImp, class GridPartImp>
37 
39  template <class FunctionImp, class GridPartImp>
41  {
42  typedef typename FunctionImp::FunctionSpaceType FunctionSpaceType;
43 
44  typedef typename FunctionSpaceType::RangeFieldType RangeFieldType;
45  typedef typename FunctionSpaceType::DomainFieldType DomainFieldType;
46  typedef typename FunctionSpaceType::RangeType RangeType;
47  typedef typename FunctionSpaceType::DomainType DomainType;
48  typedef typename FunctionSpaceType::JacobianRangeType JacobianRangeType;
49 
51 
52  typedef GridPartImp GridPartType;
53  typedef typename GridPartType :: GridType GridType;
54  typedef typename GridPartType :: template Codim<0> :: EntityType EntityType;
56  typedef typename GridPartType :: template Codim<0> :: IteratorType IteratorType;
58  typedef typename GridPartType :: IndexSetType IndexSetType;
59 
61  };
62 
63 
64 
65  // GridFunctionAdapter
66  // -------------------
67 
70  template< class FunctionImp, class GridPartImp >
72  : public Function< typename FunctionImp::FunctionSpaceType,
73  GridFunctionAdapter< FunctionImp, GridPartImp > >,
74  public HasLocalFunction
75  {
78 
79  // Make sure the function is not a discrete functon
80  static_assert( !(Dune::Conversion< FunctionImp, HasLocalFunction >::exists),
81  "FunctionType may not be a discrete function type." );
82 
83  public:
86 
88  typedef FunctionImp FunctionType;
89 
91  typedef GridPartImp GridPartType;
92 
95 
96  // type of discrete function space
98 
101 
103  typedef typename DiscreteFunctionSpaceType::DomainFieldType DomainFieldType ;
105  typedef typename DiscreteFunctionSpaceType::RangeFieldType RangeFieldType ;
107  typedef typename DiscreteFunctionSpaceType::DomainType DomainType ;
109  typedef typename DiscreteFunctionSpaceType::RangeType RangeType ;
111  typedef typename DiscreteFunctionSpaceType::JacobianRangeType JacobianRangeType;
112 
115 
116  private:
118 
119  public:
122 
123  // reference to function this local belongs to
124  GridFunctionAdapter ( const std::string &name,
125  const FunctionType &f,
126  const GridPartType &gridPart,
127  unsigned int order = DiscreteFunctionSpaceType::polynomialOrder )
128  : space_( gridPart, order ),
129  function_( f ),
130  name_( name )
131  {}
132 
133  // reference to function this local belongs to
134  GridFunctionAdapter ( const ThisType &other )
135  : space_( other.space_ ),
136  function_( other.function_ ),
137  name_( other.name_ )
138  {}
139 
141  void evaluate ( const DomainType &global, RangeType &result ) const
142  {
143  function_.evaluate( global, result );
144  }
145 
147  void jacobian ( const DomainType &global, JacobianRangeType &result ) const
148  {
149  function_.jacobian(global,result);
150  }
151 
153  LocalFunctionType localFunction ( const EntityType &entity )
154  {
155  return LocalFunctionType( entity, *this );
156  }
157 
159  const LocalFunctionType localFunction ( const EntityType &entity ) const
160  {
161  return LocalFunctionType( entity, *this );
162  }
163 
165  const std::string &name () const
166  {
167  return name_;
168  }
169 
171  const DiscreteFunctionSpaceType &space () const
172  {
173  return space_;
174  }
175 
176  const GridPartType &gridPart () const
177  {
178  return space().gridPart();
179  }
180 
181  protected:
182  DiscreteFunctionSpaceType space_;
183  const FunctionType& function_;
184  const std::string name_;
185  };
186 
187 
188 
189  // GridFunctionAdapter::LocalFunction
190  // ----------------------------------
191 
192  template< class Function, class GridPart >
194  {
195  typedef LocalFunction ThisType;
197 
198  public:
201 
203  typedef typename FunctionSpaceType::DomainFieldType DomainFieldType;
205  typedef typename FunctionSpaceType::RangeFieldType RangeFieldType;
207  static const int dimDomain = GridPart::GridType::dimensionworld;
209  static const int dimRange = FunctionSpaceType::dimRange;
210 
212  typedef typename FunctionSpaceType::DomainType DomainType;
214  typedef typename FunctionSpaceType::RangeType RangeType;
216  typedef typename FunctionSpaceType::JacobianRangeType JacobianRangeType;
218  typedef typename FunctionSpaceType::HessianRangeType HessianRangeType;
219 
221  typedef typename Traits::EntityType EntityType;
223  typedef typename EntityType::Geometry::LocalCoordinate LocalCoordinateType;
225  static const int dimLocal = LocalCoordinateType::dimension;
226 
228  LocalFunction ( const EntityType &entity, const DiscreteFunctionType &df )
229  : function_( &df.function_ ),
230  entity_( &entity ),
231  order_( df.space().order() )
232  {}
233 
234  LocalFunction ( const DiscreteFunctionType &df )
235  : function_( &df.function_ ),
236  entity_( 0 ),
237  order_( df.space().order() )
238  {}
239 
241  template< class PointType >
242  void evaluate ( const PointType &x, RangeType &ret ) const
243  {
244  const auto geometry = entity().geometry();
245  auto global = geometry.global( coordinate( x ) );
246  function().evaluate( global, ret );
247  }
248 
250  template< class PointType >
251  void jacobian ( const PointType &x, JacobianRangeType &ret ) const
252  {
253  const auto geometry = entity().geometry();
254  auto global = geometry.global( coordinate( x ) );
255  function().jacobian( global, ret );
256 
257  if( dimLocal != dimDomain )
258  {
259  // This computes the projection to the tangential space
260  // (i.e. the hyperplane this entity is contained in). This
261  // is done in a generic way by first projecting to the local
262  // tangential space of the reference elment, and then
263  // projecting back to the ambient space.
264 
265  const auto gjt = geometry.jacobianTransposed( coordinate( x ) );
266  const auto gjit = geometry.jacobianInverseTransposed( coordinate( x ) );
267 
268  FieldVector< RangeFieldType, dimLocal > tmp;
269  for( auto i = 0; i < dimRange; ++i )
270  {
271  gjit.mtv( ret[ i ], tmp );
272  gjt.mtv( tmp, ret[ i ] );
273  }
274  }
275  }
276 
278  template< class PointType >
279  void hessian ( const PointType &x, HessianRangeType &ret ) const
280  {
281  DUNE_THROW( NotImplemented, "Method hessian() not implemented yet" );
282  }
283 
285  template < class QuadratureType, class VectorType >
286  void evaluateQuadrature( const QuadratureType& quadrature, VectorType& values ) const
287  {
288  assert( values.size() == quadrature.nop() );
289  evaluateQuadratureImp( quadrature, values, values[ 0 ] );
290  }
291 
292  int order () const { return order_; }
293 
295  void init ( const EntityType &entity )
296  {
297  entity_ = &entity;
298  }
299 
300  const EntityType &entity () const
301  {
302  assert( entity_ );
303  return *entity_;
304  }
305 
306  protected:
307  template < class QuadratureType, class VectorType >
308  void evaluateQuadratureImp( const QuadratureType& quadrature, VectorType& values, const RangeType& ) const
309  {
310  const auto nop = quadrature.nop();
311  for( auto qp = 0; qp < nop; ++qp )
312  evaluate( quadrature[ qp ], values[ qp ] );
313  }
314 
315  template < class QuadratureType, class VectorType >
316  void evaluateQuadratureImp( const QuadratureType& quadrature, VectorType& values, const JacobianRangeType& ) const
317  {
318  const auto nop = quadrature.nop();
319  for( auto qp = 0; qp < nop; ++qp )
320  jacobian( quadrature[ qp ], values[ qp ] );
321  }
322 
323  const FunctionType &function () const
324  {
325  return *function_;
326  }
327 
329  const EntityType *entity_;
330  int order_;
331  };
332 
333 
334  namespace
335  {
336  template <class FunctionImp,class GridPartType,bool>
337  struct ConvertDFTypeHelper;
338 
339  template <class FunctionImp,class GridPartType>
340  struct ConvertDFTypeHelper<FunctionImp,GridPartType,true>
341  {
342  typedef ConvertDFTypeHelper<FunctionImp,GridPartType,true> ThisType;
343  enum {compatible = Dune::Conversion<GridPartType,typename FunctionImp::DiscreteFunctionSpaceType::GridPartType>::exists};
344  typedef FunctionImp FunctionType;
345  typedef typename FunctionType::DiscreteFunctionSpaceType DFSType;
346  ConvertDFTypeHelper(const std::string& name,const FunctionImp& func,const GridPartType& gp) :
347  func_(func)
348  {}
349  ConvertDFTypeHelper(const ConvertDFTypeHelper& other) :
350  func_(other.func_)
351  {}
352  const FunctionType& function() const
353  {
354  return func_;
355  }
356  const DFSType& space() const
357  {
358  return func_.space();
359  }
360  private:
361  const FunctionImp& func_;
362  };
363 
364  template <class FunctionImp,class GridPartType>
365  struct ConvertDFTypeHelper<FunctionImp,GridPartType,false>
366  : GridFunctionAdapter<FunctionImp,GridPartType>
367  {
368  typedef ConvertDFTypeHelper<FunctionImp,GridPartType,false> ThisType;
370  typedef BaseType FunctionType;
372  ConvertDFTypeHelper(const std::string& name,const FunctionImp& func,const GridPartType& gp) :
373  BaseType(name,func,gp)
374  {}
375  ConvertDFTypeHelper(const ConvertDFTypeHelper& other) :
376  BaseType(other)
377  {}
378  const FunctionType& function() const
379  {
380  return *this;
381  }
382  const DFSType& space() const
383  {
384  return BaseType::space();
385  }
386  };
387  }
388 
389  template< class FunctionImp, class GridPartImp >
391  : public Function< typename FunctionImp::FunctionSpaceType,
392  ConvertToGridFunction< FunctionImp, GridPartImp > >,
393  public HasLocalFunction
394  {
397  static const bool hasLocalFunction = Dune::Conversion< FunctionImp, HasLocalFunction >::exists;
398  typedef ConvertDFTypeHelper< FunctionImp, GridPartImp, hasLocalFunction > Helper;
399  typedef typename Helper::FunctionType ConvertedType;
400 
401  public:
402  typedef FunctionImp FunctionType;
403  typedef GridPartImp GridPartType;
404 
406  typedef typename ConvertedType::DiscreteFunctionSpaceType DiscreteFunctionSpaceType;
407  // type of discrete function space
408  typedef typename ConvertedType::FunctionSpaceType FunctionSpaceType;
409 
412 
414  typedef typename DiscreteFunctionSpaceType::DomainFieldType DomainFieldType ;
416  typedef typename DiscreteFunctionSpaceType::RangeFieldType RangeFieldType ;
418  typedef typename DiscreteFunctionSpaceType::DomainType DomainType ;
420  typedef typename DiscreteFunctionSpaceType::RangeType RangeType ;
422  typedef typename DiscreteFunctionSpaceType::JacobianRangeType JacobianRangeType;
423 
425  typedef typename GridPartType :: template Codim<0> :: EntityType EntityType;
426 
428  typedef typename ConvertedType::LocalFunctionType LocalFunctionType;
429 
431  ConvertToGridFunction ( const std::string &name,
432  const FunctionImp &function,
433  const GridPartType &gridPart )
434  : name_( name ),
435  helper_( name, function, gridPart )
436  {}
437 
438  ConvertToGridFunction ( const ThisType &other )
439  : name_( other.name_ ),
440  helper_( other.helper_ )
441  {}
442 
444  void evaluate ( const DomainType &global, RangeType &result ) const
445  {
446  helper_.function().evaluate(global,result);
447  }
448 
450  const LocalFunctionType localFunction( const EntityType &entity ) const
451  {
452  return helper_.function().localFunction(entity);
453  }
454 
456  LocalFunctionType localFunction( const EntityType &entity )
457  {
458  return helper_.function().localFunction(entity);
459  }
460 
462  const std::string &name() const
463  {
464  return name_;
465  }
466 
467  const DiscreteFunctionSpaceType &space() const
468  {
469  return helper_.space();
470  }
471 
472  private:
473  const std::string name_;
474  Helper helper_;
475  };
476 
477  template< class Function, class GridPart >
479  convertToGridFunction ( const std::string &name,
480  const Function &function,
481  const GridPart &gridPart )
482  {
483  return ConvertToGridFunction< Function, GridPart >( name, function, gridPart );
484  }
485 
486  } // namespace Fem
487 
488 } // namespace Dune
489 
491 
492 #endif // #ifndef DUNE_DISCRETEFUNCTIONADAPTER_HH
DiscreteFunctionSpaceType space_
Definition: gridfunctionadapter.hh:182
DiscreteFunctionSpaceType::DomainFieldType DomainFieldType
domain type (from function space)
Definition: gridfunctionadapter.hh:414
GridPartImp GridPartType
Definition: gridfunctionadapter.hh:52
LocalFunction(const DiscreteFunctionType &df)
Definition: gridfunctionadapter.hh:234
DiscreteFunctionSpaceType::DomainType DomainType
domain type (from function space)
Definition: gridfunctionadapter.hh:418
FunctionSpaceType::RangeFieldType RangeFieldType
range field type (from function space)
Definition: gridfunctionadapter.hh:205
FunctionSpaceType::JacobianRangeType JacobianRangeType
Definition: gridfunctionadapter.hh:48
FunctionSpaceType::DomainType DomainType
domain type (from function space)
Definition: gridfunctionadapter.hh:212
ConvertedType::FunctionSpaceType FunctionSpaceType
Definition: gridfunctionadapter.hh:408
LocalFunction LocalFunctionType
type of local function to export
Definition: gridfunctionadapter.hh:117
GridPartType::IndexSetType IndexSetType
type of IndexSet
Definition: gridfunctionadapter.hh:58
FunctionType::DiscreteFunctionSpaceType DFSType
Definition: gridfunctionadapter.hh:345
FunctionImp FunctionType
type of function
Definition: gridfunctionadapter.hh:88
EntityType::Geometry::LocalCoordinate LocalCoordinateType
local coordinate type
Definition: gridfunctionadapter.hh:223
FunctionSpaceType::DomainFieldType DomainFieldType
Definition: gridfunctionadapter.hh:45
void evaluate(const PointType &x, RangeType &ret) const
evaluate local function
Definition: gridfunctionadapter.hh:242
void evaluateQuadratureImp(const QuadratureType &quadrature, VectorType &values, const RangeType &) const
Definition: gridfunctionadapter.hh:308
int order() const
Definition: gridfunctionadapter.hh:292
FunctionSpaceType::RangeType RangeType
range type (from function space)
Definition: gridfunctionadapter.hh:214
void evaluateQuadratureImp(const QuadratureType &quadrature, VectorType &values, const JacobianRangeType &) const
Definition: gridfunctionadapter.hh:316
FunctionImp::FunctionSpaceType FunctionSpaceType
Definition: gridfunctionadapter.hh:42
LocalFunctionType localFunction(const EntityType &entity)
obtain a local function for an entity (read-write)
Definition: gridfunctionadapter.hh:456
DiscreteFunctionSpaceType::RangeFieldType RangeFieldType
range type (from function space)
Definition: gridfunctionadapter.hh:416
FunctionSpaceType::RangeType RangeType
Definition: gridfunctionadapter.hh:46
GridPartImp GridPartType
type of grid part
Definition: gridfunctionadapter.hh:91
const DiscreteFunctionSpaceType & space() const
obtain a reference to the corresponding DiscreteFunctionSpace
Definition: gridfunctionadapter.hh:171
void jacobian(const DomainType &global, JacobianRangeType &result) const
evaluate function on local coordinate local
Definition: gridfunctionadapter.hh:147
GridFunctionAdapter(const ThisType &other)
Definition: gridfunctionadapter.hh:134
DiscreteFunctionSpaceType::DomainFieldType DomainFieldType
domain type (from function space)
Definition: gridfunctionadapter.hh:103
DiscreteFunctionSpaceType::GridType GridType
type of grid
Definition: gridfunctionadapter.hh:100
DiscreteFunctionSpaceType::RangeType RangeType
range type (from function space)
Definition: gridfunctionadapter.hh:109
FunctionSpaceType::HessianRangeType HessianRangeType
hessian type (from function space)
Definition: gridfunctionadapter.hh:218
void jacobian(const PointType &x, JacobianRangeType &ret) const
jacobian of local function
Definition: gridfunctionadapter.hh:251
FunctionType::DiscreteFunctionSpaceType DFSType
Definition: gridfunctionadapter.hh:371
int order_
Definition: gridfunctionadapter.hh:330
GridPartType::GridType GridType
Definition: gridfunctionadapter.hh:53
void evaluate(const DomainType &global, RangeType &result) const
evaluate function on local coordinate local
Definition: gridfunctionadapter.hh:444
Traits::FunctionSpaceType FunctionSpaceType
function space type
Definition: gridfunctionadapter.hh:200
GridFunctionAdapter< FunctionImp, GridPartImp > DiscreteFunctionType
Definition: gridfunctionadapter.hh:60
void evaluate(const DomainType &global, RangeType &result) const
evaluate function on local coordinate local
Definition: gridfunctionadapter.hh:141
ConvertToGridFunction(const ThisType &other)
Definition: gridfunctionadapter.hh:438
traits of GridFunctionAdapter
Definition: gridfunctionadapter.hh:40
const FunctionType * function_
Definition: gridfunctionadapter.hh:328
FunctionSpaceType::RangeFieldType RangeFieldType
Definition: gridfunctionadapter.hh:44
const DiscreteFunctionSpaceType & space() const
Definition: gridfunctionadapter.hh:467
interface for local functions
Definition: localfunction.hh:41
const LocalFunctionType localFunction(const EntityType &entity) const
obtain a local function for an entity (read-write)
Definition: gridfunctionadapter.hh:159
const std::string name_
Definition: gridfunctionadapter.hh:184
void hessian(const PointType &x, HessianRangeType &ret) const
hessian of local function
Definition: gridfunctionadapter.hh:279
LocalFunctionType localFunction(const EntityType &entity)
obtain a local function for an entity (read-write)
Definition: gridfunctionadapter.hh:153
void evaluateQuadrature(const QuadratureType &quadrature, VectorType &values) const
evaluate function or jacobian of function for given quadrature
Definition: gridfunctionadapter.hh:286
DiscreteFunctionSpaceType::RangeFieldType RangeFieldType
range type (from function space)
Definition: gridfunctionadapter.hh:105
const FunctionType & function_
Definition: gridfunctionadapter.hh:183
FunctionImp FunctionType
Definition: gridfunctionadapter.hh:402
DiscreteFunctionSpaceType::DomainType DomainType
domain type (from function space)
Definition: gridfunctionadapter.hh:107
const EntityType * entity_
Definition: gridfunctionadapter.hh:329
Definition: coordinate.hh:4
FunctionSpaceType::DomainType DomainType
Definition: gridfunctionadapter.hh:47
FunctionSpaceType::DomainFieldType DomainFieldType
domain field type (from function space)
Definition: gridfunctionadapter.hh:203
ConvertDFTypeHelper< FunctionImp, GridPartType, false > ThisType
Definition: gridfunctionadapter.hh:368
Traits::FunctionSpaceType FunctionSpaceType
Definition: gridfunctionadapter.hh:97
void init(const EntityType &entity)
init local function
Definition: gridfunctionadapter.hh:295
GridFunctionAdapterTraits< FunctionImp, GridPartImp > Traits
type of traits
Definition: gridfunctionadapter.hh:81
GridFunctionAdapter(const std::string &name, const FunctionType &f, const GridPartType &gridPart, unsigned int order=DiscreteFunctionSpaceType::polynomialOrder)
Definition: gridfunctionadapter.hh:124
DiscreteFunctionSpaceAdapter< FunctionSpaceType, GridPartImp > DiscreteFunctionSpaceType
Definition: gridfunctionadapter.hh:50
LocalFunction(const EntityType &entity, const DiscreteFunctionType &df)
constructor initializing local function
Definition: gridfunctionadapter.hh:228
DiscreteFunctionSpaceType::GridType GridType
type of grid
Definition: gridfunctionadapter.hh:411
GridPartImp GridPartType
Definition: gridfunctionadapter.hh:403
DiscreteFunctionSpaceType::RangeType RangeType
range type (from function space)
Definition: gridfunctionadapter.hh:420
DiscreteFunctionSpaceType::JacobianRangeType JacobianRangeType
jacobian type (from function space)
Definition: gridfunctionadapter.hh:422
const LocalFunctionType localFunction(const EntityType &entity) const
obtain a local function for an entity (read-write)
Definition: gridfunctionadapter.hh:450
Definition: gridfunctionadapter.hh:193
ConvertToGridFunction(const std::string &name, const FunctionImp &function, const GridPartType &gridPart)
constructor
Definition: gridfunctionadapter.hh:431
GridPartType::template Codim< 0 >::EntityType EntityType
Definition: gridfunctionadapter.hh:54
FunctionSpaceType::JacobianRangeType JacobianRangeType
jacobian type (from function space)
Definition: gridfunctionadapter.hh:216
const std::string & name() const
obtain the name of the discrete function
Definition: gridfunctionadapter.hh:165
ConvertDFTypeHelper< FunctionImp, GridPartType, true > ThisType
Definition: gridfunctionadapter.hh:342
GridPartType::template Codim< 0 >::IteratorType IteratorType
type of iterator
Definition: gridfunctionadapter.hh:56
Definition: discretefunctionspace.hh:907
BaseType FunctionType
Definition: gridfunctionadapter.hh:370
const EntityType & entity() const
Definition: gridfunctionadapter.hh:300
ConvertedType::DiscreteFunctionSpaceType DiscreteFunctionSpaceType
type of discrete function space
Definition: gridfunctionadapter.hh:406
GridFunctionAdapter provides local functions for a Function.
Definition: gridfunctionadapter.hh:36
const GridPartType & gridPart() const
Definition: gridfunctionadapter.hh:176
Traits::DiscreteFunctionSpaceType DiscreteFunctionSpaceType
type of discrete function space
Definition: gridfunctionadapter.hh:94
Abstract class representing a function.
Definition: function.hh:43
FunctionImp FunctionType
Definition: gridfunctionadapter.hh:344
GridFunctionAdapter< FunctionImp, GridPartType > BaseType
Definition: gridfunctionadapter.hh:369
Traits::EntityType EntityType
type of codim 0 entity
Definition: gridfunctionadapter.hh:114
Definition: discretefunction.hh:55
ConvertedType::LocalFunctionType LocalFunctionType
type of local function to export
Definition: gridfunctionadapter.hh:428
const std::string & name() const
obtain the name of the discrete function
Definition: gridfunctionadapter.hh:462
GridPartType::template Codim< 0 >::EntityType EntityType
type of codim 0 entity
Definition: gridfunctionadapter.hh:425
GridPartType::GridType GridType
type of the grid
Definition: discretefunctionspace.hh:910
ConvertToGridFunction< Function, GridPart > convertToGridFunction(const std::string &name, const Function &function, const GridPart &gridPart)
Definition: gridfunctionadapter.hh:479
static const Point & coordinate(const Point &x)
Definition: coordinate.hh:11
DiscreteFunctionSpaceType::JacobianRangeType JacobianRangeType
jacobian type (from function space)
Definition: gridfunctionadapter.hh:111
Create Obejct that behaves like a discrete function space without to provide functions with the itera...
Definition: discretefunctionspace.hh:892
Definition: gridfunctionadapter.hh:390
Traits::EntityType EntityType
entity type
Definition: gridfunctionadapter.hh:221