dune-alugrid  2.6-git
defaultindexsets.hh
Go to the documentation of this file.
1 #ifndef DUNE_ALUGRID_DEFAULTINDEXSETS_HH
2 #define DUNE_ALUGRID_DEFAULTINDEXSETS_HH
3 
4 #include <type_traits>
5 #include <vector>
6 
7 #include <dune/common/version.hh>
8 
9 #include <dune/grid/common/grid.hh>
10 #include <dune/grid/common/adaptcallback.hh>
11 #include <dune/grid/utility/persistentcontainer.hh>
12 
19 namespace Dune
20 {
21 
23  template <class GridImp>
25  {
27  template<int cd>
28  struct Codim
29  {
30  template<PartitionIteratorType pitype>
31  struct Partition
32  {
33  typedef typename GridImp::Traits::template Codim<cd>::template Partition<pitype>::LevelIterator Iterator;
34  };
35  };
36  };
37 
39  template <class GridImp>
41  {
43  template<int cd>
44  struct Codim
45  {
46  template<PartitionIteratorType pitype>
47  struct Partition
48  {
49  typedef typename GridImp::Traits::template Codim<cd>::
51  };
52  };
53  };
54 
55 
56 
61  template < class GridImp, class IteratorImp >
63  public IndexSet< GridImp, DefaultIndexSet <GridImp, IteratorImp>,
64  unsigned int, std::vector< GeometryType > >
65  {
66  typedef GridImp GridType;
67  enum { dim = GridType::dimension };
68 
69  public:
70  enum { ncodim = GridType::dimension + 1 };
71 
73  typedef unsigned int IndexType;
75  typedef std::vector< GeometryType > Types;
76 
77  private:
79  typedef IteratorImp IteratorType ;
80 
81  public:
82  struct Index
83  {
84  int index_;
85  Index() : index_( -1 ) {}
86  int index() const { return index_; }
87  void set( const int index ) { index_ = index; }
88  };
89 
90  typedef PersistentContainer< GridType, Index > PersistentContainerType ;
91  typedef std::vector< PersistentContainerType* > PersistentContainerVectorType;
92 
93  private:
95 
96  template< int codim >
97  struct InsertEntityLoop
98  {
99  // determine next codim with a sealing of the grid's dimension
100  static const int nextCodim = codim == dim ? dim : codim + 1;
101 
102  static void apply ( const typename GridType::template Codim< 0 >::Entity &entity,
104  std::vector< int > &sizes )
105  {
106  PersistentContainerType &codimContainer = *(indexContainer[ codim ]);
107  if( codim == 0 )
108  {
109  Index &idx = codimContainer[ entity ];
110  if( idx.index() < 0 )
111  idx.set( sizes[ codim ]++ );
112  }
113  else
114  {
115  const int subEntities = entity.subEntities( codim );
116  for( int i = 0; i < subEntities; ++i )
117  {
118  Index &idx = codimContainer( entity, i );
119  if( idx.index() < 0 )
120  idx.set( sizes[ codim ]++ );
121  }
122  }
123 
124  if( codim < dim )
125  {
126  // call next codim, dim will end this loop
127  InsertEntityLoop< nextCodim >::apply( entity, indexContainer, sizes );
128  }
129  }
130  };
131 
132  template <class EntityType, int codim>
133  struct EntitySpec
134  {
136  const EntityType & e,
137  int i )
138  {
139  // if the codimension equals that of the entity simply return the index
140  if( codim == EntityType::codimension )
141  return indexContainer[ e ].index();
142 
143  DUNE_THROW(NotImplemented,"subIndex for entities with codimension > 0 is not implemented");
144  return IndexType(-1);
145  }
146  };
147 
148  template <class EntityType>
149  struct EntitySpec<EntityType,0>
150  {
152  const EntityType & e,
153  int i )
154  {
155  alugrid_assert ( indexContainer( e, i ).index() >= 0 );
156  return indexContainer( e, i ).index();
157  }
158  };
159 
160  public:
163  using IndexSet<GridType, DefaultIndexSet>::subIndex;
164 
167  DefaultIndexSet( const GridType & grid ,
168  const IteratorType& begin,
169  const IteratorType& end,
170  const int level = -1 )
171  : grid_(grid),
172  indexContainers_( ncodim, (PersistentContainerType *) 0),
173  size_( ncodim, -1 ),
174  level_(level)
175  {
176  for( int codim=0; codim < ncodim; ++codim )
177  indexContainers_[ codim ] = new PersistentContainerType( grid, codim );
178 
179  calcNewIndex (begin, end);
180  }
181 
184  {
185  for( int codim=0; codim < ncodim; ++codim )
186  delete indexContainers_[ codim ];
187  }
188 
189  const PersistentContainerType& indexContainer( const size_t codim ) const
190  {
191  alugrid_assert ( codim < indexContainers_.size() );
192  alugrid_assert ( indexContainers_[ codim ] );
193  return *( indexContainers_[ codim ] );
194  }
195 
197  {
198  alugrid_assert ( codim < indexContainers_.size() );
199  alugrid_assert ( indexContainers_[ codim ] );
200  return *( indexContainers_[ codim ] );
201  }
202 
204  template<class EntityType>
205  IndexType index (const EntityType & en) const
206  {
207  enum { cd = EntityType::codimension };
208  // this must not be true for vertices
209  // therefore only check other codims
210 #ifdef ALUGRIDDEBUG
211  const int codim = cd;
212  alugrid_assert ( (codim == dim) ? (1) : ( level_ < 0 ) || (level_ == en.level() ));
213  alugrid_assert ( indexContainer( codim )[ en ].index() >= 0 );
214 #endif
215  return indexContainer( cd )[ en ].index();
216  }
217 
219  template<int cd>
220  IndexType index (const typename GridImp::template Codim<cd>::Entity& en) const
221  {
222  // this must not be true for vertices
223  // therefore only check other codims
224 #ifdef ALUGRIDDEBUG
225  const int codim = cd;
226  //const bool isLeaf = (codim == 0) ? en.isLeaf() : true ;
227  alugrid_assert ( (codim == dim) ? (true) : ( level_ < 0 ) || (level_ == en.level() ));
228  alugrid_assert ( indexContainer( cd )[ en ].index() >= 0 );
229 #endif
230  return indexContainer( cd )[ en ].index();
231  }
232 
235  template< int cc >
236  IndexType subIndex ( const typename std::remove_const< GridImp >::type::Traits::template Codim< cc >::Entity &e,
237  int i, unsigned int codim ) const
238  {
239  alugrid_assert ( (codim != 0) || (level_ < 0) || ( level_ == e.level() ) );
240  typedef typename std::remove_const< GridImp >::type::Traits::template Codim< cc >::Entity Entity;
241  return EntitySpec< Entity, cc >::subIndex( indexContainer( codim ), e, i );
242  }
243 
245  template<class EntityType>
246  bool contains (const EntityType& en) const
247  {
248  enum { cd = EntityType::codimension };
249  return (indexContainer( cd )[ en ].index() >= 0 );
250  }
251 
253  IndexType size ( int codim ) const
254  {
255  alugrid_assert ( codim >= 0 && codim <= GridType::dimension );
256  return size_[ codim ];
257  }
258 
261  IndexType size ( GeometryType type ) const
262  {
263  if( typeNotValid(type) ) return 0;
264  return size_[GridType::dimension-type.dim()];
265  }
266 
269  void calcNewIndex ( const IteratorType &begin, const IteratorType &end )
270  {
271  // resize arrays to new size
272  // and set size to zero
273  for( int cd = 0; cd < ncodim; ++cd )
274  {
275  indexContainer( cd ).resize( Index() );
276  indexContainer( cd ).fill( Index() );
277  size_[ cd ] = 0;
278  }
279 
280  // grid walk to setup index set
281  for( IteratorType it = begin; it != end; ++it )
282  {
283  const typename IteratorType::Entity &entity = *it;
284  alugrid_assert ( ( level_ < 0 ) ? entity.isLeaf() : (entity.level() == level_) );
285  InsertEntityLoop< 0 >::apply( entity, indexContainers_, size_ );
286  }
287 
288  // remember the number of entity on level and cd = 0
289  for(int cd=0; cd<ncodim; ++cd)
290  {
291 #ifdef ALUGRIDDEBUG
292  const int gridSize = ( level_ < 0 ) ? grid_.size( cd ) : grid_.size( level_, cd);
293  const int mySize = size_[cd];
294  if( mySize > gridSize )
295  {
296  std::cout << "DefaultIndexSet[ " << level_ << " ]: " << mySize << " s | g " << gridSize << std::endl;
297  }
298  // this assertion currently fails for 3d conforming
299  // alugrid_assert ( ( grid_.conformingRefinement() && dim == 3 && level_ >= 0 ) ? true : (mySize <= gridSize) );
300 #endif
301  }
302  }
303 
305  const std::vector<GeometryType>& geomTypes (int codim) const
306  {
307  return grid_.geomTypes( codim );
308  }
309 
311  Types types( const int codim ) const
312  {
313  return geomTypes( codim );
314  }
315 
317  bool containsIndex ( const int cd, const int idx ) const
318  {
319  alugrid_assert ( (typename PersistentContainerType::Size)idx < indexContainer( cd ).size() );
320  return ((indexContainer( cd ).begin() + idx)->index() >= 0);
321  }
322 
323  private:
324  // return whether set has this type stored or not
325  bool typeNotValid (const GeometryType & type) const
326  {
327  int codim = GridType::dimension - type.dim();
328  const std::vector<GeometryType> & geomT = geomTypes(codim);
329  for(size_t i=0; i<geomT.size(); ++i) if(geomT[i] == type) return false;
330  return true;
331  }
332 
333  // grid this index set belongs to
334  const GridType& grid_;
335 
337  PersistentContainerVectorType indexContainers_;
338 
339  // number of entitys of each level an codim
340  std::vector< int > size_;
341 
342  // the level for which this index set is created
343  const int level_;
344  };
345 
346 
350  template <class Grid>
352  {
353  public:
355  typedef int IndexType;
356 
357  public:
358  struct Index
359  {
361  Index() : index_( -1 ) {}
362  int index() const { return index_; }
363  void set( const int index ) { index_ = index; }
364  };
365 
367  typedef std::vector< Index > SegmentIndexVectorType;
368  protected:
371 
372  public:
374  : segmentIndex_(),
375  numSegments_( -1 )
376  {
377  }
378 
380  IndexType index ( const int segmentId ) const
381  {
382  alugrid_assert( valid() );
383  alugrid_assert( segmentId < int(segmentIndex_.size() ) );
384  alugrid_assert( segmentIndex_[ segmentId ].index() >= 0 );
385  return segmentIndex_[ segmentId ].index();
386  }
387 
388  IndexType size() const
389  {
390  alugrid_assert( valid() );
391  return numSegments_;
392  }
393 
396  template <class GridViewType>
397  void update( const GridViewType& gridView )
398  {
399  numSegments_ = 0 ;
400  segmentIndex_.clear();
401 
402  const auto end = gridView.template end<0, Interior_Partition> ();
403  for( auto it = gridView.template begin<0, Interior_Partition> (); it != end; ++ it )
404  {
405  const auto& entity = *it;
406  const auto endi = gridView.iend( entity );
407  for( auto i = gridView.ibegin( entity ); i != endi; ++i )
408  {
409  const auto& intersection = *i;
410  if( intersection.boundary() )
411  {
412  const int id = Grid::getRealImplementation( intersection ).segmentId();
413  if( int(segmentIndex_.size()) <= id )
414  segmentIndex_.resize( id+1 );
415  if( segmentIndex_[ id ].index() < 0 )
416  segmentIndex_[ id ].set( numSegments_ ++ );
417  }
418  }
419  }
420 
421  // if segment index is consecutive use identity
422  if( numSegments_ == int(segmentIndex_.size()) )
423  {
424  for( int i=0; i<numSegments_; ++ i )
425  {
426  segmentIndex_[ i ].set( i );
427  }
428  }
429  }
430 
431  bool valid () const { return numSegments_ >= 0; }
432  void invalidate () { numSegments_ = -1; }
433  };
434 
435 } // namespace Dune
436 
437 #endif // #ifndef DUNE_ALUGRID_DEFAULTINDEXSETS_HH
#define alugrid_assert(EX)
Definition: alugrid_assert.hh:20
Definition: alu3dinclude.hh:80
LevelIterator tpyes for all codims and partition types.
Definition: defaultindexsets.hh:25
The types.
Definition: defaultindexsets.hh:29
Definition: defaultindexsets.hh:32
GridImp::Traits::template Codim< cd >::template Partition< pitype >::LevelIterator Iterator
Definition: defaultindexsets.hh:33
LeafIterator tpyes for all codims and partition types.
Definition: defaultindexsets.hh:41
The types of the iterator.
Definition: defaultindexsets.hh:45
Definition: defaultindexsets.hh:48
GridImp::Traits::template Codim< cd >::template Partition< pitype >::LeafIterator Iterator
Definition: defaultindexsets.hh:50
DefaultIndexSet creates an index set by using the grids persistent container an a given pair of itera...
Definition: defaultindexsets.hh:65
IndexType size(GeometryType type) const
Definition: defaultindexsets.hh:261
std::vector< PersistentContainerType * > PersistentContainerVectorType
Definition: defaultindexsets.hh:91
std::vector< GeometryType > Types
type of geometry types
Definition: defaultindexsets.hh:75
Types types(const int codim) const
deliver all geometry types used in this grid
Definition: defaultindexsets.hh:311
IndexType size(int codim) const
return size of IndexSet for a given level and codim
Definition: defaultindexsets.hh:253
~DefaultIndexSet()
desctructor deleting persistent containers
Definition: defaultindexsets.hh:183
const std::vector< GeometryType > & geomTypes(int codim) const
deliver all geometry types used in this grid
Definition: defaultindexsets.hh:305
IndexType index(const typename GridImp::template Codim< cd >::Entity &en) const
return LevelIndex of given entity
Definition: defaultindexsets.hh:220
PersistentContainer< GridType, Index > PersistentContainerType
Definition: defaultindexsets.hh:90
bool contains(const EntityType &en) const
returns true if this set provides an index for given entity
Definition: defaultindexsets.hh:246
const PersistentContainerType & indexContainer(const size_t codim) const
Definition: defaultindexsets.hh:189
void calcNewIndex(const IteratorType &begin, const IteratorType &end)
Definition: defaultindexsets.hh:269
unsigned int IndexType
type of index
Definition: defaultindexsets.hh:73
bool containsIndex(const int cd, const int idx) const
returns true if this set provides an index for given entity
Definition: defaultindexsets.hh:317
PersistentContainerType & indexContainer(const size_t codim)
Definition: defaultindexsets.hh:196
@ ncodim
Definition: defaultindexsets.hh:70
IndexType index(const EntityType &en) const
return LevelIndex of given entity
Definition: defaultindexsets.hh:205
IndexType subIndex(const typename std::remove_const< GridImp >::type::Traits::template Codim< cc >::Entity &e, int i, unsigned int codim) const
Definition: defaultindexsets.hh:236
DefaultIndexSet(const GridType &grid, const IteratorType &begin, const IteratorType &end, const int level=-1)
Definition: defaultindexsets.hh:167
Definition: defaultindexsets.hh:83
void set(const int index)
Definition: defaultindexsets.hh:87
int index() const
Definition: defaultindexsets.hh:86
int index_
Definition: defaultindexsets.hh:84
Index()
Definition: defaultindexsets.hh:85
DefaultBoundarySegmentIndexSet creates an index set for the macro boundary segments.
Definition: defaultindexsets.hh:352
int numSegments_
Definition: defaultindexsets.hh:370
IndexType size() const
Definition: defaultindexsets.hh:388
SegmentIndexVectorType segmentIndex_
Definition: defaultindexsets.hh:369
int IndexType
type of index
Definition: defaultindexsets.hh:355
void invalidate()
Definition: defaultindexsets.hh:432
DefaultBoundarySegmentIndexSet()
Definition: defaultindexsets.hh:373
void update(const GridViewType &gridView)
Definition: defaultindexsets.hh:397
std::vector< Index > SegmentIndexVectorType
type of geometry types
Definition: defaultindexsets.hh:367
IndexType index(const int segmentId) const
return LevelIndex of given entity
Definition: defaultindexsets.hh:380
bool valid() const
Definition: defaultindexsets.hh:431
Definition: defaultindexsets.hh:359
void set(const int index)
Definition: defaultindexsets.hh:363
IndexType index_
Definition: defaultindexsets.hh:360
int index() const
Definition: defaultindexsets.hh:362
Index()
Definition: defaultindexsets.hh:361