dune-fem  2.4.1-rc
padaptivespace/generic.hh
Go to the documentation of this file.
1 #ifndef DUNE_FEM_SPACE_PADAPTIVE_GENERIC_HH
2 #define DUNE_FEM_SPACE_PADAPTIVE_GENERIC_HH
3 
4 #include <cassert>
5 #include <list>
6 #include <vector>
7 
8 #include <dune/common/math.hh>
9 
19 
20 
21 namespace Dune
22 {
23 
24  namespace Fem
25  {
26 
27  // GenericDiscreteFunctionSpace
28  // ----------------------------
29 
36  template< class Traits >
38  : public DiscreteFunctionSpaceDefault< Traits >
39  {
42 
43  public:
46 
48 
50  typedef typename BaseType::GridType GridType;
53  typedef typename IteratorType::Entity EntityType;
55 
58 
60 
61  static const int polynomialOrder = Traits::polynomialOrder;
62 
63  protected:
64  // single type for shape function sets of all polynomial orders
66  // storage for scalar shape function set per polynomial order
68  // factory for shape function set of static order
69  template< int pOrd >
71  {
72  typedef typename Traits::template ScalarShapeFunctionSetFactory< pOrd >::Type Type;
73  };
74 
75  public:
76 
79 
80  protected:
81  template< int pOrd >
82  struct Initialize;
83 
84  // type of intermediate storage
86 
89  {
91  virtual bool equals( void * ) const = 0 ;
92  virtual void adaptFunction( IntermediateStorageFunctionType &tmp ) = 0;
93 
94  protected:
96  };
97 
98  template < class DF, class LocalInterpolation >
99  class PAdaptiveDiscreteFunctionEntry;
100 
101  typedef std::list< PAdaptiveDiscreteFunctionEntryInterface * > PAdaptiveDiscreteFunctionListType;
102  typedef typename PAdaptiveDiscreteFunctionListType::iterator DFListIteratorType;
103 
104  // type of DoF manager
106 
107  public:
109  typedef int IdentifierType;
111  static const IdentifierType id = 665;
112 
113  using BaseType::asImp;
114  using BaseType::gridPart;
115 
123  const InterfaceType commInterface,
124  const CommunicationDirection commDirection )
125  : BaseType( gridPart, commInterface, commDirection ),
126  scalarShapeFunctionSets_( polynomialOrder+1 ),
127  compiledLocalKeys_( polynomialOrder+1 ),
129  {}
130 
131  protected:
132  // copy constructor needed for p-adaptation
134  : BaseType( other.gridPart_, other.commInterface_, other.commDirection_ ),
135  scalarShapeFunctionSets_( polynomialOrder+1 ),
136  compiledLocalKeys_( polynomialOrder+1 ),
137  blockMapper_( initialize( &other.blockMapper() ) )
138  {}
139 
140  public:
141  // Destructor (freeing base functions pointers and block mapper)
143  {
144  assert( dfList_.empty() );
145  delete blockMapper_;
146  }
147 
148 
150  // Interface methods //
152 
154  inline DFSpaceIdentifier type () const { return GenericSpace_id; }
155 
157  BasisFunctionSetType basisFunctionSet ( const EntityType &entity ) const
158  {
159  return BasisFunctionSetType( entity, shapeFunctionSet( entity ) );
160  }
161 
163  inline bool continuous () const { return Traits::continuousSpace; }
164 
166  inline int order () const { return polynomialOrder; }
167 
169  inline int order (const typename BaseType::EntityType &entity) const
170  {
171  return blockMapper().polynomOrder( entity );
172  }
173 
175  inline bool multipleBaseFunctionSets () const { return (polynomialOrder > 1); }
176 
178  BlockMapperType &blockMapper () const
179  {
180  assert( blockMapper_ );
181  return *blockMapper_;
182  }
183 
184 
186  // Non-interface methods //
188 
196  ShapeFunctionSetType shapeFunctionSet ( const EntityType &entity ) const
197  {
198  return shapeFunctionSet( entity.type(), order( entity ) );
199  }
200 
209  ShapeFunctionSetType shapeFunctionSet ( const GeometryType &type, const int order = polynomialOrder ) const
210  {
212  }
213 
223  template< class EntityType >
224  inline const CompiledLocalKeyType &compiledLocalKey ( const EntityType &entity ) const
225  {
226  return compiledLocalKey( entity.type(), order( entity ) );
227  }
228 
239  inline const CompiledLocalKeyType &compiledLocalKey ( const GeometryType type, const int order = polynomialOrder ) const
240  {
241  return compiledLocalKeys_[ order ][ type ];
242  }
243 
244 
246  // Adaptive interface methods //
248 
255  template< class Vector >
256  void adapt ( const Vector &polynomialOrders, const int polOrderShift = 0 ) const
257  {
258  typedef typename IteratorType::Entity EntityType ;
259  const IteratorType endit = this->end();
260 
261  // create a copy of this space (to be improved)
262  DiscreteFunctionSpaceType oldSpace( asImp() );
263 
264  //std::cout << "Old space size = " << oldSpace.size() << std::endl;
265 
266  // set new polynomial order for space
267  for( IteratorType it = this->begin(); it != endit; ++it )
268  {
269  const EntityType &entity = *it;
270  const int polOrder = polynomialOrders[ this->indexSet().index( entity ) ] + polOrderShift ;
271  blockMapper().setPolynomOrder( entity, polOrder );
272  }
273 
274  // adjust mapper
275  blockMapper().adapt();
276 
277  //std::cout << "New space size = " << blockMapper().size() << std::endl;
278  //for(size_t i=0; i<polynomialOrders.size(); ++i)
279  // std::cout << " " << polynomialOrders[ i ] << std::endl;
280 
281  // Adapt space and then discrete functions
282  IntermediateStorageFunctionType tmp( "padapt-temp", oldSpace );
283  //std::cout << "created tmp with size = " << oldSpace.size() << " " << tmp.space().size() << std::endl;
284 
285  const DFListIteratorType endDF = dfList_.end();
286  for( DFListIteratorType it = dfList_.begin(); it != endDF; ++it )
287  {
288  (*it)->adaptFunction( tmp );
289  }
290 
291  DofManagerType &dm = DofManagerType::instance( this->grid() );
292  // resize discrete functions (only functions belonging
293  // to this space will be affected ), for convenience
294  dm.resize();
295  dm.compress();
296 
297  //std::cout <<"This spaces size = " << this->size() << std::endl;
298  //std::cout << std::endl;
299  }
300 
301 
302  protected:
303  // initialize space and create block mapper
304  BlockMapperType *initialize ( const BlockMapperType *otherMapper = 0 )
305  {
306  const IndexSetType &indexSet = gridPart().indexSet();
307 
308  AllGeomTypes< IndexSetType, GridType > allGeometryTypes( indexSet );
309  const std::vector< GeometryType > &geometryTypes
310  = allGeometryTypes.geomTypes( 0 );
311 
312  for( unsigned int i = 0; i < geometryTypes.size(); ++i )
313  {
314  ForLoop< Initialize, 1, polynomialOrder >::
315  apply( scalarShapeFunctionSets_, compiledLocalKeys_, geometryTypes[ i ] );
316  }
317 
318  if( otherMapper )
319  {
320  // make a copy of the other block mapper
321  return new BlockMapperType( *otherMapper, compiledLocalKeys_ );
322  }
323  else
324  {
325  // create new block mapper, this mapper is unique for each space since
326  // the polynomial degrees might be different for each element
328  }
329  }
330 
331  template< class DiscreteFunction >
332  DFListIteratorType searchFunction ( const DiscreteFunction &df ) const
333  {
334  assert( &df.space() == this );
335  const DFListIteratorType endDF = dfList_.end();
336  for( DFListIteratorType it = dfList_.begin(); it != endDF; ++it )
337  {
338  if( (*it)->equals( (void *) &df ) )
339  return it;
340  }
341  return endDF;
342  }
343 
344  public:
345  template< class DiscreteFunction >
346  void removeFunction ( const DiscreteFunction &df ) const
347  {
348  DFListIteratorType it = searchFunction( df );
349  if( it != dfList_.end() )
350  {
351  delete (*it);
352  dfList_.erase( it );
353  }
354  }
355 
356  protected:
357  // storage for base function sets
358  std::vector< ScalarShapeFunctionSetStorageType > scalarShapeFunctionSets_;
359  // storage for compiled local keys
360  std::vector< LocalKeyStorageType > compiledLocalKeys_;
361 
362  // corresponding mapper
363  BlockMapperType *blockMapper_;
364  // list of registered discrete functions
365  mutable PAdaptiveDiscreteFunctionListType dfList_;
366  };
367 
368 
369 
370  // Implementation of GenericDiscreteFunctionSpace::Initialize
371  // ----------------------------------------------------------
372 
373  template< class Traits >
374  template <int pOrd>
375  struct GenericDiscreteFunctionSpace< Traits >::Initialize
376  {
378  {
379  static CompiledLocalKeyType *createObject ( const GeometryType &type )
380  {
381  return new CompiledLocalKeyType( type, pOrd );
382  }
383  static void deleteObject ( CompiledLocalKeyType *obj )
384  {
385  delete obj;
386  }
387  };
388 
389  static void apply ( std::vector< ScalarShapeFunctionSetStorageType > &scalarShapeFunctionSets,
390  std::vector< LocalKeyStorageType > &compiledLocalKeys,
391  const GeometryType &type )
392  {
393  typedef typename ScalarShapeFunctionSetFactory< pOrd >::Type ScalarShapeFunctionSetFactoryType;
395  scalarShapeFunctionSets[ pOrd ].template insert< SingletonProviderType >( type );
396 
397  typedef SingletonList< GeometryType, CompiledLocalKeyType, CompiledLocalKeyFactory > CompiledLocalKeySingletonProviderType;
398  compiledLocalKeys[ pOrd ].template insert< CompiledLocalKeySingletonProviderType >( type );
399  }
400  };
401 
402 
403 
404  // Implementation of GenericDiscreteFunctionSpace::PAdaptiveDiscreteFunctionEntry
405  // ------------------------------------------------------------------------------
406 
407  template< class Traits >
408  template < class DF, class LocalInterpolation >
411  {
412  DF &df_;
413  DofManagerType &dm_;
414 
415  public:
417  : df_( df ),
418  dm_( DofManagerType::instance( df.space().grid() ) )
419  {}
420 
421  virtual bool equals ( void *ptr ) const { return (((void *) &df_) == ptr ); }
422 
424  {
425  //const int oldSize = tmp.space().size() ;
426  //const int newSize = df_.space().size() ;
427 
428  //std::cout << " Start adaptFct: old size " << tmp.space().size()
429  // << " new size " << df_.space().size() << std::endl;
430 
431  typedef typename IntermediateStorageFunctionType::DofIteratorType TmpIteratorType;
432  typedef typename DF::DofIteratorType DFIteratorType;
433 
434  // copy dof to temporary storage
435  DFIteratorType dfit = df_.dbegin();
436  const TmpIteratorType endtmp = tmp.dend();
437  for( TmpIteratorType it = tmp.dbegin(); it != endtmp; ++it, ++dfit )
438  {
439  assert( dfit != df_.dend() );
440  *it = *dfit;
441  }
442  assert( dfit == df_.dend() );
443 
444  // adjust size of discrete function
445  df_.resize();
446 
447  //std::cout << " End adaptFct: old size " << tmp.space().size()
448  // << " new size " << df_.space().size() << std::endl;
449 
450  // interpolate to new space, this can be a
451  // Lagrange interpolation or a L2 projection
452  LocalInterpolation::apply( tmp, df_ );
453  }
454  };
455 
456  } // namespace Fem
457 
458 } // Dune namespace
459 
460 #endif // #ifndef DUNE_FEM_SPACE_PADAPTIVE_GENERIC_HH
const IndexSetType & indexSet() const
Get a reference to the associated index set.
Definition: discretefunctionspace.hh:720
BaseType::IteratorType IteratorType
Definition: padaptivespace/generic.hh:52
Definition: selectcaching.hh:28
PAdaptiveDiscreteFunctionListType::iterator DFListIteratorType
Definition: padaptivespace/generic.hh:102
BaseType::GridType GridType
Definition: discretefunctionspace.hh:610
BlockMapperType * blockMapper_
Definition: padaptivespace/generic.hh:363
BaseType::GridPartType GridPartType
Definition: padaptivespace/generic.hh:49
void removeFunction(const DiscreteFunction &df) const
Definition: padaptivespace/generic.hh:346
Definition: padaptivespace/lagrange.hh:43
BlockMapperType * initialize(const BlockMapperType *otherMapper=0)
Definition: padaptivespace/generic.hh:304
BlockMapperType & blockMapper() const
get a reference to the block mapper
Definition: padaptivespace/generic.hh:178
const CommunicationDirection commDirection_
Definition: discretefunctionspace.hh:651
BaseType::EntityType EntityType
Definition: discretefunctionspace.hh:613
BaseType::FunctionSpaceType FunctionSpaceType
Definition: padaptivespace/generic.hh:47
BaseType::BlockMapperType BlockMapperType
Definition: padaptivespace/generic.hh:59
BasisFunctionSetType basisFunctionSet(const EntityType &entity) const
get basis function set for given entity
Definition: padaptivespace/generic.hh:157
Traits::template ScalarShapeFunctionSetFactory< pOrd >::Type Type
Definition: padaptivespace/generic.hh:72
GenericDiscreteFunctionSpace(GridPartType &gridPart, const InterfaceType commInterface, const CommunicationDirection commDirection)
constructor
Definition: padaptivespace/generic.hh:122
A vector valued function space.
Definition: functionspace.hh:16
BaseType::IndexSetType IndexSetType
Definition: padaptivespace/generic.hh:51
AdaptiveDiscreteFunction< DiscreteFunctionSpaceType > IntermediateStorageFunctionType
Definition: padaptivespace/generic.hh:82
PAdaptiveDiscreteFunctionEntryInterface()
Definition: padaptivespace/generic.hh:95
DFSpaceIdentifier type() const
return type identifier of discrete function space
Definition: padaptivespace/generic.hh:154
virtual ~PAdaptiveDiscreteFunctionEntryInterface()
Definition: padaptivespace/generic.hh:90
ShapeFunctionSetType shapeFunctionSet(const EntityType &entity) const
return shape function set for given entity
Definition: padaptivespace/generic.hh:196
ConstDofIteratorType dbegin() const
Obtain the constant iterator pointing to the first dof.
Definition: discretefunction.hh:746
Singleton list for key/object pairs.
Definition: singletonlist.hh:49
Definition: shapefunctionset/vectorial.hh:428
BaseType::IndexSetType IndexSetType
Definition: discretefunctionspace.hh:611
DFSpaceIdentifier
enumerator for identification of spaces
Definition: discretefunctionspace.hh:88
BaseType::DiscreteFunctionSpaceType DiscreteFunctionSpaceType
Definition: padaptivespace/generic.hh:45
BaseType::GridType GridType
Definition: padaptivespace/generic.hh:50
Definition: adaptivefunction/adaptivefunction.hh:34
void compress()
Compress all data that is hold by this dofmanager.
Definition: dofmanager.hh:1106
Please doc me.
Definition: padaptivespace/generic.hh:37
Definition: datacollector.hh:45
Traits::DofIteratorType DofIteratorType
type of the dof iterator
Definition: discretefunction.hh:591
Traits::BasisFunctionSetType BasisFunctionSetType
type of basis function set of this space
Definition: discretefunctionspace.hh:179
DFListIteratorType searchFunction(const DiscreteFunction &df) const
Definition: padaptivespace/generic.hh:332
DofManager< GridType > DofManagerType
Definition: padaptivespace/generic.hh:105
static void apply(std::vector< ScalarShapeFunctionSetStorageType > &scalarShapeFunctionSets, std::vector< LocalKeyStorageType > &compiledLocalKeys, const GeometryType &type)
Definition: padaptivespace/generic.hh:389
IteratorType::Entity EntityType
Definition: padaptivespace/generic.hh:53
Traits::BlockMapperType BlockMapperType
type of block mapper of this space
Definition: discretefunctionspace.hh:181
ShapeFunctionSetType shapeFunctionSet(const GeometryType &type, const int order=polynomialOrder) const
return shape unique function set for geometry type
Definition: padaptivespace/generic.hh:209
BaseSetLocalKeyStorage< CompiledLocalKeyType > LocalKeyStorageType
Definition: padaptivespace/generic.hh:78
bool multipleBaseFunctionSets() const
this space has more than one base function set
Definition: padaptivespace/generic.hh:175
GridPartType & gridPart_
Definition: discretefunctionspace.hh:631
bool continuous() const
returns true if the space contains only globally continuous functions
Definition: padaptivespace/generic.hh:163
const DiscreteFunctionSpaceType & asImp() const
Definition: discretefunctionspace.hh:546
BaseSetLocalKeyStorage< ScalarShapeFunctionSetType > ScalarShapeFunctionSetStorageType
Definition: padaptivespace/generic.hh:67
IteratorType begin() const
get iterator pointing to the first entity of the associated grid partition
Definition: discretefunctionspace.hh:736
const CompiledLocalKeyType & compiledLocalKey(const GeometryType type, const int order=polynomialOrder) const
provide access to the compiled local keys for a geometry type and polynomial order ...
Definition: padaptivespace/generic.hh:239
Traits::ScalarShapeFunctionSetType ScalarShapeFunctionSetType
Definition: padaptivespace/generic.hh:65
BaseType::IntersectionType IntersectionType
Definition: padaptivespace/generic.hh:54
Definition: coordinate.hh:4
static CompiledLocalKeyType * createObject(const GeometryType &type)
Definition: padaptivespace/generic.hh:379
BaseType::IteratorType IteratorType
Definition: discretefunctionspace.hh:612
PAdaptiveDiscreteFunctionListType dfList_
Definition: padaptivespace/generic.hh:365
int IdentifierType
type of identifier for this discrete function space
Definition: padaptivespace/generic.hh:109
IteratorType end() const
get iterator pointing behind the last entity of the associated grid partition
Definition: discretefunctionspace.hh:746
void resize()
Resize index sets and memory due to what the mapper has as new size.
Definition: dofmanager.hh:1053
PAdaptiveDiscreteFunctionEntry(DF &df)
Definition: padaptivespace/generic.hh:416
BaseType::GridPartType GridPartType
Definition: discretefunctionspace.hh:609
interface for list of p-adaptive functions
Definition: padaptivespace/generic.hh:88
ThisType GenericDiscreteFunctionSpaceType
Definition: padaptivespace/generic.hh:44
GenericDiscreteFunctionSpace(const GenericDiscreteFunctionSpace &other)
Definition: padaptivespace/generic.hh:133
void adapt(const Vector &polynomialOrders, const int polOrderShift=0) const
p adaptation
Definition: padaptivespace/generic.hh:256
Provides a proxy class for pointers to a shape function set.
Traits::CompiledLocalKeyType CompiledLocalKeyType
Definition: padaptivespace/generic.hh:77
default implementation uses method geomTypes of given index set. Used in DiscreteFunctionSpaces.
Definition: allgeomtypes.hh:89
const InterfaceType commInterface_
Definition: discretefunctionspace.hh:650
const CompiledLocalKeyType & compiledLocalKey(const EntityType &entity) const
provide access to the compiled local keys for an entity
Definition: padaptivespace/generic.hh:224
std::list< PAdaptiveDiscreteFunctionEntryInterface * > PAdaptiveDiscreteFunctionListType
Definition: padaptivespace/generic.hh:99
int order(const typename BaseType::EntityType &entity) const
get global order of space
Definition: padaptivespace/generic.hh:169
~GenericDiscreteFunctionSpace()
Definition: padaptivespace/generic.hh:142
This is the class with default implementations for discrete function. The methods not marked with hav...
Definition: discretefunctionspace.hh:595
virtual void adaptFunction(IntermediateStorageFunctionType &tmp)
Definition: padaptivespace/generic.hh:423
GridPartType::IntersectionType IntersectionType
type of the intersections
Definition: discretefunctionspace.hh:202
std::vector< LocalKeyStorageType > compiledLocalKeys_
Definition: padaptivespace/generic.hh:360
GridPartType & gridPart() const
Definition: discretefunctionspace.hh:714
const std::vector< GeometryType > & geomTypes(unsigned int codim) const
returns vector with geometry tpyes this index set has indices for
Definition: allgeomtypes.hh:145
static const int polynomialOrder
Definition: padaptivespace/generic.hh:61
Traits::DiscreteFunctionSpaceType DiscreteFunctionSpaceType
Definition: discretefunctionspace.hh:607
int order() const
get global order of space
Definition: padaptivespace/generic.hh:166
static void deleteObject(CompiledLocalKeyType *obj)
Definition: padaptivespace/generic.hh:383
const GridType & grid() const
get reference to grid this discrete function space belongs to
Definition: discretefunctionspace.hh:702
ConstDofIteratorType dend() const
Obtain the constant iterator pointing to the last dof.
Definition: discretefunction.hh:758
Definition: lagrangepoints.hh:461
std::vector< ScalarShapeFunctionSetStorageType > scalarShapeFunctionSets_
Definition: padaptivespace/generic.hh:358
static ThisType & instance(const GridType &grid)
obtain a reference to the DofManager for a given grid
Definition: dofmanager.hh:1319
virtual bool equals(void *ptr) const
Definition: padaptivespace/generic.hh:421
id for Generic Space
Definition: discretefunctionspace.hh:94
BaseType::BasisFunctionSetType BasisFunctionSetType
Definition: padaptivespace/generic.hh:57
Traits::ShapeFunctionSetType ShapeFunctionSetType
Definition: padaptivespace/generic.hh:56