dune-alugrid  3.0.0
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 #include <rpc/rpc.h>
7 
8 #include <dune/common/forloop.hh>
9 #include <dune/common/version.hh>
10 
11 #include <dune/grid/common/grid.hh>
12 #include <dune/grid/common/adaptcallback.hh>
13 #include <dune/grid/utility/persistentcontainer.hh>
14 
21 namespace Dune
22 {
23 
25  template <class GridImp>
27  {
29  template<int cd>
30  struct Codim
31  {
32  template<PartitionIteratorType pitype>
33  struct Partition
34  {
35  typedef typename GridImp::Traits::template Codim<cd>::template Partition<pitype>::LevelIterator Iterator;
36  };
37  };
38  };
39 
41  template <class GridImp>
43  {
45  template<int cd>
46  struct Codim
47  {
48  template<PartitionIteratorType pitype>
49  struct Partition
50  {
51  typedef typename GridImp::Traits::template Codim<cd>::
53  };
54  };
55  };
56 
57 
58 
63  template < class GridImp, class IteratorImp >
65  public IndexSet< GridImp, DefaultIndexSet <GridImp, IteratorImp>,
66  unsigned int
67 #if DUNE_VERSION_NEWER(DUNE_GRID,2,4)
68  , std::vector< GeometryType >
69 #endif
70  >
71  {
72  typedef GridImp GridType;
73  enum { dim = GridType::dimension };
74 
75  public:
76  enum { ncodim = GridType::dimension + 1 };
77 
79  typedef unsigned int IndexType;
81  typedef std::vector< GeometryType > Types;
82 
83  private:
85  typedef IteratorImp IteratorType ;
86 
87  public:
88  struct Index
89  {
90  int index_;
91  Index() : index_( -1 ) {}
92  int index() const { return index_; }
93  void set( const int index ) { index_ = index; }
94  };
95 
96  typedef PersistentContainer< GridType, Index > PersistentContainerType ;
97  typedef std::vector< PersistentContainerType* > PersistentContainerVectorType;
98 
99  private:
101 
102  template< int codim >
103  struct InsertEntity
104  {
105  static void apply ( const typename GridType::template Codim< 0 >::Entity &entity,
107  std::vector< int > &sizes )
108  {
109  PersistentContainerType &codimContainer = *(indexContainer[ codim ]);
110  if( codim == 0 )
111  {
112  Index &idx = codimContainer[ entity ];
113  if( idx.index() < 0 )
114  idx.set( sizes[ codim ]++ );
115  }
116  else
117  {
118 #if DUNE_VERSION_NEWER(DUNE_GRID,2,4)
119  const int subEntities = entity.subEntities( codim );
120 #else
121  const int subEntities = entity.template count< codim > ();
122 #endif
123  for( int i = 0; i < subEntities; ++i )
124  {
125  Index &idx = codimContainer( entity, i );
126  if( idx.index() < 0 )
127  idx.set( sizes[ codim ]++ );
128  }
129  }
130  }
131  };
132 
133  template <class EntityType, int codim>
134  struct EntitySpec
135  {
137  const EntityType & e,
138  int i )
139  {
140  // if the codimension equals that of the entity simply return the index
141  if( codim == EntityType::codimension )
142  return indexContainer[ e ].index();
143 
144  DUNE_THROW(NotImplemented,"subIndex for entities with codimension > 0 is not implemented");
145  return IndexType(-1);
146  }
147  };
148 
149  template <class EntityType>
150  struct EntitySpec<EntityType,0>
151  {
153  const EntityType & e,
154  int i )
155  {
156  alugrid_assert ( indexContainer( e, i ).index() >= 0 );
157  return indexContainer( e, i ).index();
158  }
159  };
160 
161  public:
164  using IndexSet<GridType, DefaultIndexSet>::subIndex;
165 
168  DefaultIndexSet( const GridType & grid ,
169  const IteratorType& begin,
170  const IteratorType& end,
171  const int level = -1 )
172  : grid_(grid),
173  indexContainers_( ncodim, (PersistentContainerType *) 0),
174  size_( ncodim, -1 ),
175  level_(level)
176  {
177  for( int codim=0; codim < ncodim; ++codim )
178  indexContainers_[ codim ] = new PersistentContainerType( grid, codim );
179 
180  calcNewIndex (begin, end);
181  }
182 
185  {
186  for( int codim=0; codim < ncodim; ++codim )
187  delete indexContainers_[ codim ];
188  }
189 
190  const PersistentContainerType& indexContainer( const size_t codim ) const
191  {
192  alugrid_assert ( codim < indexContainers_.size() );
193  alugrid_assert ( indexContainers_[ codim ] );
194  return *( indexContainers_[ codim ] );
195  }
196 
198  {
199  alugrid_assert ( codim < indexContainers_.size() );
200  alugrid_assert ( indexContainers_[ codim ] );
201  return *( indexContainers_[ codim ] );
202  }
203 
205  template<class EntityType>
206  IndexType index (const EntityType & en) const
207  {
208  enum { cd = EntityType::codimension };
209  // this must not be true for vertices
210  // therefore only check other codims
211 #ifdef ALUGRIDDEBUG
212  const int codim = cd;
213  alugrid_assert ( (codim == dim) ? (1) : ( level_ < 0 ) || (level_ == en.level() ));
214  alugrid_assert ( indexContainer( codim )[ en ].index() >= 0 );
215 #endif
216  return indexContainer( cd )[ en ].index();
217  }
218 
220  template<int cd>
221  IndexType index (const typename GridImp::template Codim<cd>::Entity& en) const
222  {
223  // this must not be true for vertices
224  // therefore only check other codims
225 #ifdef ALUGRIDDEBUG
226  const int codim = cd;
227  //const bool isLeaf = (codim == 0) ? en.isLeaf() : true ;
228  alugrid_assert ( (codim == dim) ? (true) : ( level_ < 0 ) || (level_ == en.level() ));
229  alugrid_assert ( indexContainer( cd )[ en ].index() >= 0 );
230 #endif
231  return indexContainer( cd )[ en ].index();
232  }
233 
236  template< int cc >
237  IndexType subIndex ( const typename std::remove_const< GridImp >::type::Traits::template Codim< cc >::Entity &e,
238  int i, unsigned int codim ) const
239  {
240  alugrid_assert ( (codim != 0) || (level_ < 0) || ( level_ == e.level() ) );
241  typedef typename std::remove_const< GridImp >::type::Traits::template Codim< cc >::Entity Entity;
242  return EntitySpec< Entity, cc >::subIndex( indexContainer( codim ), e, i );
243  }
244 
246  template<class EntityType>
247  bool contains (const EntityType& en) const
248  {
249  enum { cd = EntityType::codimension };
250  return (indexContainer( cd )[ en ].index() >= 0 );
251  }
252 
254  IndexType size ( int codim ) const
255  {
256  alugrid_assert ( codim >= 0 && codim <= GridType::dimension );
257  return size_[ codim ];
258  }
259 
262  IndexType size ( GeometryType type ) const
263  {
264  if( typeNotValid(type) ) return 0;
265  return size_[GridType::dimension-type.dim()];
266  }
267 
270  void calcNewIndex ( const IteratorType &begin, const IteratorType &end )
271  {
272  // resize arrays to new size
273  // and set size to zero
274  for( int cd = 0; cd < ncodim; ++cd )
275  {
276  indexContainer( cd ).resize( Index() );
277  indexContainer( cd ).fill( Index() );
278  size_[ cd ] = 0;
279  }
280 
281  // grid walk to setup index set
282  for( IteratorType it = begin; it != end; ++it )
283  {
284  alugrid_assert ( ( level_ < 0 ) ? it->isLeaf() : (it->level() == level_) );
285  const typename IteratorType::Entity &entity = *it;
286  ForLoop< InsertEntity, 0, dim >::apply( entity, indexContainers_, size_ );
287  }
288 
289  // remember the number of entity on level and cd = 0
290  for(int cd=0; cd<ncodim; ++cd)
291  {
292 #ifdef ALUGRIDDEBUG
293  const int gridSize = ( level_ < 0 ) ? grid_.size( cd ) : grid_.size( level_, cd);
294  const int mySize = size_[cd];
295  if( mySize > gridSize )
296  {
297  std::cout << "DefaultIndexSet[ " << level_ << " ]: " << mySize << " s | g " << gridSize << std::endl;
298  }
299  // this assertion currently fails for 3d conforming
300  // alugrid_assert ( ( grid_.conformingRefinement() && dim == 3 && level_ >= 0 ) ? true : (mySize <= gridSize) );
301 #endif
302  }
303  }
304 
306  const std::vector<GeometryType>& geomTypes (int codim) const
307  {
308  return grid_.geomTypes( codim );
309  }
310 
312  Types types( const int codim ) const
313  {
314  return geomTypes( codim );
315  }
316 
318  bool containsIndex ( const int cd, const int idx ) const
319  {
320  alugrid_assert ( (typename PersistentContainerType::Size)idx < indexContainer( cd ).size() );
321  return ((indexContainer( cd ).begin() + idx)->index() >= 0);
322  }
323 
324  private:
325  // return whether set has this type stored or not
326  bool typeNotValid (const GeometryType & type) const
327  {
328  int codim = GridType::dimension - type.dim();
329  const std::vector<GeometryType> & geomT = geomTypes(codim);
330  for(size_t i=0; i<geomT.size(); ++i) if(geomT[i] == type) return false;
331  return true;
332  }
333 
334  // grid this index set belongs to
335  const GridType& grid_;
336 
338  PersistentContainerVectorType indexContainers_;
339 
340  // number of entitys of each level an codim
341  std::vector< int > size_;
342 
343  // the level for which this index set is created
344  const int level_;
345  };
346 
347 } // namespace Dune
348 
349 #endif // #ifndef DUNE_ALUGRID_DEFAULTINDEXSETS_HH
Dune::DefaultIndexSet::calcNewIndex
void calcNewIndex(const IteratorType &begin, const IteratorType &end)
Definition: defaultindexsets.hh:270
Dune::DefaultIndexSet::PersistentContainerType
PersistentContainer< GridType, Index > PersistentContainerType
Definition: defaultindexsets.hh:96
Dune::DefaultIndexSet::indexContainer
const PersistentContainerType & indexContainer(const size_t codim) const
Definition: defaultindexsets.hh:190
Dune::DefaultIndexSet::DefaultIndexSet
DefaultIndexSet(const GridType &grid, const IteratorType &begin, const IteratorType &end, const int level=-1)
Definition: defaultindexsets.hh:168
Dune::DefaultIndexSet::Index::Index
Index()
Definition: defaultindexsets.hh:91
Dune::DefaultLevelIteratorTypes
LevelIterator tpyes for all codims and partition types.
Definition: defaultindexsets.hh:26
Dune::DefaultIndexSet
DefaultIndexSet creates an index set by using the grids persistent container an a given pair of itera...
Definition: defaultindexsets.hh:64
Dune::DefaultIndexSet::~DefaultIndexSet
~DefaultIndexSet()
desctructor deleting persistent containers
Definition: defaultindexsets.hh:184
Dune::DefaultLeafIteratorTypes::Codim
The types of the iterator.
Definition: defaultindexsets.hh:46
Dune::DefaultIndexSet::geomTypes
const std::vector< GeometryType > & geomTypes(int codim) const
deliver all geometry types used in this grid
Definition: defaultindexsets.hh:306
Dune::DefaultIndexSet::index
IndexType index(const EntityType &en) const
return LevelIndex of given entity
Definition: defaultindexsets.hh:206
Dune::DefaultLevelIteratorTypes::Codim::Partition::Iterator
GridImp::Traits::template Codim< cd >::template Partition< pitype >::LevelIterator Iterator
Definition: defaultindexsets.hh:35
Dune::DefaultLeafIteratorTypes
LeafIterator tpyes for all codims and partition types.
Definition: defaultindexsets.hh:42
Dune::DefaultIndexSet::Index::index_
int index_
Definition: defaultindexsets.hh:90
Dune::DefaultIndexSet::ncodim
Definition: defaultindexsets.hh:76
Dune::DefaultIndexSet::Types
std::vector< GeometryType > Types
type of geometry types
Definition: defaultindexsets.hh:81
Dune::DefaultIndexSet::PersistentContainerVectorType
std::vector< PersistentContainerType * > PersistentContainerVectorType
Definition: defaultindexsets.hh:97
Dune::DefaultIndexSet::contains
bool contains(const EntityType &en) const
returns true if this set provides an index for given entity
Definition: defaultindexsets.hh:247
alugrid_assert
#define alugrid_assert(EX)
Definition: alugrid_assert.hh:20
Dune::DefaultLevelIteratorTypes::Codim
The types.
Definition: defaultindexsets.hh:30
Dune::DefaultIndexSet::Index::index
int index() const
Definition: defaultindexsets.hh:92
Dune::DefaultIndexSet::size
IndexType size(GeometryType type) const
Definition: defaultindexsets.hh:262
Dune::DefaultIndexSet::subIndex
IndexType subIndex(const typename std::remove_const< GridImp >::type::Traits::template Codim< cc >::Entity &e, int i, unsigned int codim) const
Definition: defaultindexsets.hh:237
Dune::DefaultLeafIteratorTypes::Codim::Partition::Iterator
GridImp::Traits::template Codim< cd >::template Partition< pitype >::LeafIterator Iterator
Definition: defaultindexsets.hh:52
Dune::DefaultIndexSet::Index::set
void set(const int index)
Definition: defaultindexsets.hh:93
Dune::DefaultLeafIteratorTypes::Codim::Partition
Definition: defaultindexsets.hh:49
Dune::DefaultIndexSet::IndexType
unsigned int IndexType
type of index
Definition: defaultindexsets.hh:79
Dune::DefaultIndexSet::indexContainer
PersistentContainerType & indexContainer(const size_t codim)
Definition: defaultindexsets.hh:197
Dune::DefaultIndexSet::Index
Definition: defaultindexsets.hh:88
Dune::DefaultIndexSet::containsIndex
bool containsIndex(const int cd, const int idx) const
returns true if this set provides an index for given entity
Definition: defaultindexsets.hh:318
Dune
Definition: alu3dinclude.hh:79
Dune::DefaultIndexSet::size
IndexType size(int codim) const
return size of IndexSet for a given level and codim
Definition: defaultindexsets.hh:254
Dune::DefaultIndexSet::types
Types types(const int codim) const
deliver all geometry types used in this grid
Definition: defaultindexsets.hh:312
Dune::DefaultLevelIteratorTypes::Codim::Partition
Definition: defaultindexsets.hh:33
Dune::DefaultIndexSet::index
IndexType index(const typename GridImp::template Codim< cd >::Entity &en) const
return LevelIndex of given entity
Definition: defaultindexsets.hh:221