1 #ifndef DUNE_FEM_GENERICDOFMAPPER_HH 2 #define DUNE_FEM_GENERICDOFMAPPER_HH 4 #if HAVE_DUNE_LOCALFUNCTIONS 8 #include <dune/geometry/referenceelements.hh> 10 #include <dune/grid/common/indexidset.hh> 12 #include <dune/localfunctions/common/localkey.hh> 27 template<
class Gr
idPart,
class LocalCoefficientsMap >
28 class GenericDofMapper;
30 template<
class Gr
idPart,
class LocalCoefficientsMap >
31 class GenericDofMapIterator;
38 template<
class Gr
idPart,
class LocalCoefficientsMap >
39 struct GenericDofMapperTraits
41 typedef GenericDofMapper< GridPart, LocalCoefficientsMap > DofMapperType;
43 typedef typename GridPart::template Codim< 0 >::EntityType EntityType;
45 typedef GenericDofMapIterator< GridPart, LocalCoefficientsMap > DofMapIteratorType;
53 template<
class Gr
idPart,
class LocalCoefficientsMap >
54 class GenericDofMapper
55 :
public DofMapper< GenericDofMapperTraits< GridPart, LocalCoefficientsMap > >
57 typedef GenericDofMapper< GridPart, LocalCoefficientsMap > ThisType;
58 typedef DofMapper< GenericDofMapperTraits< GridPart, LocalCoefficientsMap > > BaseType;
60 friend class GenericDofMapIterator< GridPart, LocalCoefficientsMap >;
63 typedef typename BaseType::EntityType EntityType;
64 typedef typename BaseType::DofMapIteratorType DofMapIteratorType;
66 typedef GridPart GridPartType;
67 typedef LocalCoefficientsMap LocalCoefficientsMapType;
69 typedef typename LocalCoefficientsMapType::LocalCoefficientsType LocalCoefficientsType;
74 unsigned int subEntity;
75 unsigned int topologyId;
76 unsigned int blockIdx;
83 typedef typename std::vector< SubEntityInfo >::const_iterator Iterator;
85 Iterator begin()
const 87 return subEntityInfo.begin();
92 return subEntityInfo.end();
95 unsigned int localDofPermutation(
unsigned int i )
const 97 assert( i < localDof.size() );
101 unsigned int size ()
const 106 unsigned int numDofs;
107 std::vector< unsigned int > localDof;
108 std::vector< SubEntityInfo > subEntityInfo;
112 typedef typename GridPartType::IndexSetType IndexSetType;
113 typedef Fem::DofManager< typename GridPartType::GridType > DofManagerType;
118 unsigned int topologyId;
119 unsigned int numDofs;
121 unsigned int oldOffset;
123 Block (
const unsigned int cd,
const unsigned int tid,
const unsigned int nDofs )
124 : codim( cd ), topologyId( tid ), numDofs( nDofs )
128 template<
int topologyId >
132 static const int dimension = GridPartType::GridType::dimension;
133 static const unsigned int numTopologies = (1 << dimension);
135 GenericDofMapper (
const GridPartType &gridPart,
136 const LocalCoefficientsMapType &localCoefficientsMap );
140 dofManager_.removeIndexSet( *
this );
143 const IndexSetType &indexSet ()
const 148 DofMapIteratorType begin (
const EntityType &entity )
const 150 typename DofMapIteratorType::Begin begin;
151 return DofMapIteratorType( *
this, entity, begin );
154 DofMapIteratorType end (
const EntityType &entity )
const 156 typename DofMapIteratorType::End end;
157 return DofMapIteratorType( *
this, entity, end );
160 const MapInfo &mapInfo (
const EntityType &entity )
const;
162 template<
class Functor >
163 void mapEach (
const EntityType &element, Functor functor )
const;
165 void map (
const EntityType &element, std::vector< std::size_t > &indices )
const;
167 int mapToGlobal(
const EntityType &entity,
const int localDof )
const 169 static std::vector< unsigned int > indices;
170 map( entity, indices );
171 return indices[ localDof ];
174 template<
class Entity,
class Functor >
175 void mapEachEntityDof (
const Entity &entity, Functor f )
const;
177 unsigned int size ()
const 182 int maxNumDofs ()
const 187 int numDofs (
const EntityType &entity )
const 189 return mapInfo( entity ).numDofs;
192 template<
class Entity >
193 int numEntityDofs (
const Entity &entity )
const 195 const int codim = Entity::codimension;
196 const unsigned int topologyId = entity.type().id();
197 const int blockIndex = blockIndex_[ codim ][ topologyId >> 1 ];
198 return (blockIndex >= 0 ? blocks_[ blockIndex ].numDofs : 0);
201 bool contains (
const int codim )
const;
203 const int numBlocks ()
const 205 return blocks_.size();
208 int offSet (
const int blockIdx )
const 210 assert( (blockIdx >= 0) && (blockIdx < numBlocks()) );
211 return blocks_[ blockIdx ].offset;
214 int oldOffSet (
const int blockIdx )
const 216 assert( (blockIdx >= 0) && (blockIdx < numBlocks()) );
217 return blocks_[ blockIdx ].oldOffset;
220 int numberOfHoles (
const int blockIdx )
const;
221 int oldIndex (
const int hole,
const int blockIdx )
const;
222 int newIndex (
const int hole,
const int blockIdx )
const;
224 bool consecutive ()
const 229 bool fixedDataSize(
const int codim )
const;
234 template<
class Entity >
235 void insertEntity (
const Entity &entity )
240 template<
class Entity >
241 void removeEntity (
const Entity &entity )
256 GenericDofMapper (
const ThisType & );
257 ThisType &operator= (
const ThisType & );
261 template<
class Topology >
262 void build (
const LocalCoefficientsType &localCoefficients,
265 template<
class Topology >
268 DofManagerType &dofManager_;
269 const IndexSetType &indexSet_;
270 const LocalCoefficientsMapType &localCoefficientsMap_;
271 std::vector< MapInfo > mapInfo_[ numTopologies ];
272 std::vector< Block > blocks_;
273 unsigned int maxNumDofs_;
275 std::vector< int > blockIndex_[ dimension+1 ];
279 template<
class Gr
idPart,
class LocalCoefficientsMap >
280 inline GenericDofMapper< GridPart, LocalCoefficientsMap >
281 ::GenericDofMapper (
const GridPartType &gridPart,
282 const LocalCoefficientsMapType &localCoefficientsMap )
283 : dofManager_( DofManagerType::instance( gridPart.grid() ) ),
284 indexSet_( gridPart.indexSet() ),
285 localCoefficientsMap_( localCoefficientsMap ),
288 for(
int codim = 0; codim <= dimension; ++codim )
290 const int subdimension = dimension-codim;
293 blockIndex_[ codim ].resize( subdimension > 0 ? 1 << (subdimension-1) : 1, -1 );
295 ForLoop< Build, 0, numTopologies-1 >::apply( *
this );
297 dofManager_.addIndexSet( *
this );
301 template<
class Gr
idPart,
class LocalCoefficientsMap >
303 GenericDofMapper< GridPart, LocalCoefficientsMap >::contains (
const int codim )
const 305 typedef typename std::vector< int >::const_iterator Iterator;
306 assert( (codim >= 0) && (codim <= dimension) );
308 bool contains =
false;
309 const Iterator end = blockIndex_[ codim ].end();
310 for( Iterator it = blockIndex_[ codim ].begin(); it != end; ++it )
311 contains |= (*it >= 0);
316 template<
class Gr
idPart,
class LocalCoefficientsMap >
317 const typename GenericDofMapper< GridPart, LocalCoefficientsMap >::MapInfo &
318 GenericDofMapper< GridPart, LocalCoefficientsMap >::mapInfo (
const EntityType &entity )
const 320 const unsigned int topologyId = entity.type().id();
321 const unsigned int i = localCoefficientsMap_( entity );
322 assert( i <= mapInfo_[ topologyId ].size() );
323 return mapInfo_[ topologyId ][ i ];
327 template<
class Gr
idPart,
class LocalCoefficientsMap >
328 template<
class Functor >
330 GenericDofMapper< GridPart, LocalCoefficientsMap >
331 ::mapEach (
const EntityType &entity, Functor functor )
const 333 typedef typename std::vector< SubEntityInfo >::const_iterator Iterator;
335 const MapInfo &info = mapInfo( entity );
337 const unsigned int *localDof = &(info.localDof[ 0 ]);
339 const Iterator end = info.subEntityInfo.end();
340 for( Iterator it = info.subEntityInfo.begin(); it != end; ++it )
342 const unsigned int index = indexSet().subIndex( entity, it->subEntity, it->codim );
343 const unsigned int offset = it->offset + index*it->numDofs;
344 for(
unsigned int j = 0; j < it->numDofs; ++j )
345 functor( *(localDof++), offset + j );
350 template<
class Gr
idPart,
class LocalCoefficientsMap >
352 GenericDofMapper< GridPart, LocalCoefficientsMap >
353 ::map (
const EntityType &element, std::vector< std::size_t > &indices )
const 355 indices.resize( numDofs( element ) );
356 mapEach( element, Fem::AssignFunctor< std::vector< std::size_t > >( indices ) );
360 template<
class Gr
idPart,
class LocalCoefficientsMap >
361 template<
class Entity,
class Functor >
363 GenericDofMapper< GridPart, LocalCoefficientsMap >
364 ::mapEachEntityDof (
const Entity &entity, Functor f )
const 366 const int codim = Entity::codimension;
367 const unsigned int topologyId = entity.type().id();
368 const int blockIndex = blockIndex_[ codim ][ topologyId >> 1 ];
369 assert( blockIndex >= 0 );
371 const Block &block = blocks_[ blockIndex ];
372 const unsigned int baseIndex = block.offset + block.numDofs*indexSet_.index( entity );
373 for(
unsigned int index = baseIndex; index != baseIndex + block.numDofs; ++index )
378 template<
class Gr
idPart,
class LocalCoefficientsMap >
380 GenericDofMapper< GridPart, LocalCoefficientsMap >::update ()
383 const unsigned int numBlocks = blocks_.size();
384 for(
unsigned int i = 0; i < numBlocks; ++i )
386 Block &block = blocks_[ i ];
388 unsigned int idxSize = 0;
389 const GeometryType type( block.topologyId, dimension - block.codim);
391 idxSize = indexSet().size( type );
392 block.oldOffset = block.offset;
393 block.offset = size_;
394 size_ += idxSize * block.numDofs;
397 for(
unsigned int topologyId = 0; topologyId < numTopologies; ++topologyId )
399 typedef typename std::vector< MapInfo >::iterator Iterator;
400 const Iterator mend = mapInfo_[ topologyId ].end();
401 for( Iterator mit = mapInfo_[ topologyId ].begin(); mit != mend; ++mit )
403 typedef typename std::vector< SubEntityInfo >::iterator Iterator;
404 const Iterator send = mit->subEntityInfo.end();
405 for( Iterator sit = mit->subEntityInfo.begin(); sit != send; ++sit )
406 sit->offset = blocks_[ sit->blockIdx ].offset;
412 template<
class Gr
idPart,
class LocalCoefficientsMap >
414 GenericDofMapper< GridPart, LocalCoefficientsMap >::numberOfHoles (
const int blockIdx )
const 416 assert( (blockIdx >= 0) && (blockIdx < numBlocks()) );
417 const Block &block = blocks_[ blockIdx ];
418 return block.numDofs * indexSet().numberOfHoles( block.codim );
422 template<
class Gr
idPart,
class LocalCoefficientsMap >
424 GenericDofMapper< GridPart, LocalCoefficientsMap >
425 ::oldIndex (
const int hole,
const int blockIdx )
const 427 assert( (hole >= 0) && (hole < numberOfHoles( blockIdx )) );
428 const Block &block = blocks_[ blockIdx ];
429 const int numDofs = block.numDofs;
430 return block.offset + numDofs * indexSet().oldIndex( hole / numDofs, block.codim ) + (hole % numDofs);
434 template<
class Gr
idPart,
class LocalCoefficientsMap >
436 GenericDofMapper< GridPart, LocalCoefficientsMap >
437 ::newIndex (
const int hole,
const int blockIdx )
const 439 assert( (hole >= 0) && (hole < numberOfHoles( blockIdx )) );
440 const Block &block = blocks_[ blockIdx ];
441 const int numDofs = block.numDofs;
442 return block.offset + numDofs * indexSet().newIndex( hole / numDofs, block.codim ) + (hole % numDofs);
446 template<
class Gr
idPart,
class LocalCoefficientsMap >
448 GenericDofMapper< GridPart, LocalCoefficientsMap >::fixedDataSize(
const int codim )
const 450 typedef typename std::vector< int >::const_iterator Iterator;
451 assert( (codim >= 0) && (codim <= dimension) );
453 Iterator begin = blockIndex_[ codim ].begin();
454 const Iterator end = blockIndex_[ codim ].end();
458 unsigned int numDofs = (*begin >= 0 ? blocks_[ *begin ].numDofs : 0);
459 bool fixedSize =
true;
460 for( Iterator it = begin++; it != end; ++it )
461 fixedSize &= (numDofs == (*it >= 0 ? blocks_[ *it ].numDofs : 0));
466 template<
class Gr
idPart,
class LocalCoefficientsMap >
467 template<
class Topology >
469 GenericDofMapper< GridPart, LocalCoefficientsMap >
470 ::build (
const LocalCoefficientsType &localCoefficients, MapInfo &mapInfo )
472 const Dune::ReferenceElement< void, dimension > &refElement
473 = Dune::ReferenceElements< void, dimension >::general( GeometryType( Topology() ) );
475 mapInfo.numDofs = localCoefficients.size();
476 mapInfo.localDof.resize( mapInfo.numDofs );
477 maxNumDofs_ =
std::max( maxNumDofs_, mapInfo.numDofs );
480 GenericGeometry::SubTopologyMapper< Topology > mapper;
481 std::vector< unsigned int > counts( mapper.size(), (
unsigned int)0 );
482 for(
unsigned int i = 0; i < mapInfo.numDofs; ++i )
484 const LocalKey &key = localCoefficients.localKey( i );
485 ++counts[ mapper( key.codim(), key.subEntity() ) ];
489 for(
int codim = 0; codim <= dimension; ++codim )
491 const unsigned int codimSize = refElement.size( codim );
492 for(
unsigned int subEntity = 0; subEntity < codimSize; ++subEntity )
494 const unsigned int topologyId = refElement.type( subEntity, codim ).id();
496 int &blockIdx = blockIndex_[ codim ][ topologyId >> 1 ];
497 const unsigned int numDofs = counts[ mapper( codim, subEntity ) ];
502 blockIdx = blocks_.size();
503 blocks_.push_back( Block( codim, topologyId, numDofs ) );
508 else if( numDofs != (blockIdx >= 0 ? blocks_[ blockIdx ].numDofs : 0) )
509 DUNE_THROW( InvalidStateException,
"Inconsistent LocalCoefficients." );
513 SubEntityInfo subEntityInfo;
514 subEntityInfo.codim = codim;
515 subEntityInfo.subEntity = subEntity;
516 subEntityInfo.topologyId = topologyId;
517 subEntityInfo.blockIdx = blockIdx;
518 subEntityInfo.numDofs = numDofs;
519 mapInfo.subEntityInfo.push_back( subEntityInfo );
525 for(
unsigned int i = 0; i < mapInfo.numDofs; ++i )
527 typedef typename std::vector< SubEntityInfo >::iterator Iterator;
528 const LocalKey &key = localCoefficients.localKey( i );
530 unsigned int *localDof = &(mapInfo.localDof[ 0 ]);
531 const Iterator end = mapInfo.subEntityInfo.end();
532 for( Iterator it = mapInfo.subEntityInfo.begin();
true; ++it )
536 std::cerr <<
"Error: (subEntity = " << key.subEntity()
537 <<
", codim = " << key.codim()
538 <<
") not found in subEntityInfo" << std::endl;
539 std::cerr <<
"SubEntityInfo contains:" << std::endl;
540 for( it = mapInfo.subEntityInfo.begin(); it != end; ++it )
542 std::cerr <<
" (subEntity = " << it->subEntity
543 <<
", codim = " << it->codim <<
")" << std::endl;
548 if( (it->codim == key.codim()) && (it->subEntity == key.subEntity()) )
550 *(localDof + key.index()) = i;
553 localDof += it->numDofs;
559 template<
class Gr
idPart,
class LocalCoefficientsMap >
560 template<
class Topology >
562 GenericDofMapper< GridPart, LocalCoefficientsMap >::build ()
564 const unsigned int size = localCoefficientsMap_.template size< Topology >();
565 mapInfo_[ Topology::id ].resize( size );
566 for(
unsigned int i = 0; i < size; ++i )
568 MapInfo &mapInfo = mapInfo_[ Topology::id ][ i ];
569 const LocalCoefficientsType &localCoefficients
570 = localCoefficientsMap_.template localCoefficients< Topology >( i );
571 build< Topology >( localCoefficients, mapInfo );
577 template<
class Gr
idPart,
class LocalCoefficientsMap >
578 template<
int topologyId >
579 struct GenericDofMapper< GridPart, LocalCoefficientsMap >::Build
581 typedef typename GenericGeometry::Topology< topologyId, dimension >::type Topology;
583 static void apply ( ThisType &dofMapper )
585 dofMapper.template build< Topology >();
594 template<
class Gr
idPart,
class LocalCoefficientsMap >
595 class GenericDofMapIterator
597 typedef GenericDofMapIterator< GridPart, LocalCoefficientsMap > ThisType;
600 typedef GenericDofMapper< GridPart, LocalCoefficientsMap > DofMapperType;
602 typedef typename DofMapperType::EntityType EntityType;
603 typedef typename DofMapperType::IndexSetType IndexSetType;
605 typedef std::integral_constant<int, 0 > Begin;
606 typedef std::integral_constant<int, 1 > End;
609 typedef typename DofMapperType::SubEntityInfo SubEntityInfo;
610 typedef typename DofMapperType::MapInfo MapInfo;
611 typedef typename std::vector< SubEntityInfo >::const_iterator SubEntityInfoIterator;
614 GenericDofMapIterator (
const DofMapperType &dofMapper,
615 const EntityType &entity,
617 : indexSet_( dofMapper.indexSet() ),
619 mapInfo_( dofMapper.mapInfo( entity ) ),
620 subEntityInfoIt_( mapInfo_.subEntityInfo.begin() ),
621 localDof_( &(mapInfo_.localDof[ 0 ]) )
623 initSubEntity ( subEntityInfoIt_ );
626 GenericDofMapIterator (
const DofMapperType &dofMapper,
627 const EntityType &entity,
629 : indexSet_( dofMapper.indexSet() ),
631 mapInfo_( dofMapper.mapInfo( entity ) ),
632 subEntityInfoIt_( mapInfo_.subEntityInfo.end() ),
633 localDof_( &(mapInfo_.localDof[ mapInfo_.numDofs ]) )
636 ThisType &operator++ ()
640 if( dof_ >= subEntityInfoIt_->numDofs )
641 initSubEntity( ++subEntityInfoIt_ );
645 bool operator== (
const ThisType &other )
const 647 return (localDof_ == other.localDof_);
650 bool operator!= (
const ThisType &other )
const 652 return (localDof_ != other.localDof_);
662 return offset_ + dof_;
666 void initSubEntity (
const SubEntityInfoIterator &it )
668 if( it != mapInfo_.subEntityInfo.end() )
670 assert( it->numDofs > 0 );
672 const unsigned int index = indexSet_.subIndex( entity_, it->subEntity, it->codim );
673 offset_ = it->offset + index * it->numDofs;
678 const IndexSetType &indexSet_;
679 const EntityType &entity_;
680 const MapInfo &mapInfo_;
681 SubEntityInfoIterator subEntityInfoIt_;
682 const unsigned int *localDof_;
691 #endif // #if HAVE_DUNE_LOCALFUNCTIONS 693 #endif // #ifndef DUNE_FEM_GENERICDOFMAPPER_HH
static constexpr T max(T a)
Definition: utility.hh:65
bool operator!=(const Double &a, const Double &b)
Definition: double.hh:629
bool operator==(const Double &a, const Double &b)
Definition: double.hh:589
Definition: coordinate.hh:4