4 #ifndef DUNE_ALBERTA_TREEITERATOR_HH
5 #define DUNE_ALBERTA_TREEITERATOR_HH
7 #include <dune/common/hybridutilities.hh>
8 #include <dune/common/std/utility.hh>
9 #include <dune/common/typetraits.hh>
30 template<
int dim,
int dimworld >
31 class AlbertaMarkerVector
33 typedef AlbertaMarkerVector< dim, dimworld > This;
35 typedef AlbertaGrid< dim, dimworld > Grid;
41 typedef Alberta::HierarchyDofNumbering< dimension > DofNumbering;
42 typedef Alberta::ElementInfo< dimension > ElementInfo;
45 struct NoMarkSubEntities;
47 struct MarkSubEntities;
51 explicit AlbertaMarkerVector (
const DofNumbering &dofNumbering )
52 : dofNumbering_( dofNumbering )
54 for(
int codim = 0; codim <= dimension; ++codim )
58 AlbertaMarkerVector (
const This &other )
59 : dofNumbering_( other.dofNumbering_ )
61 for(
int codim = 0; codim <= dimension; ++codim )
65 ~AlbertaMarkerVector ()
71 This &operator= (
const This & );
76 bool subEntityOnElement (
const ElementInfo &elementInfo,
int subEntity )
const;
78 template<
int firstCodim,
class Iterator >
79 void markSubEntities (
const Iterator &begin,
const Iterator &end );
83 for(
int codim = 0; codim <= dimension; ++codim )
85 if( marker_[ codim ] != 0 )
86 delete[] marker_[ codim ];
94 return (marker_[ dimension ] != 0);
98 void print ( std::ostream &out = std::cout )
const;
101 const DofNumbering &dofNumbering_;
102 int *marker_[ dimension+1 ];
110 template<
int dim,
int dimworld >
112 struct AlbertaMarkerVector< dim, dimworld >::NoMarkSubEntities
114 template<
int firstCodim,
class Iterator >
115 static void mark (
const DofNumbering &dofNumbering,
int *(&marker)[ dimension + 1 ],
116 const Iterator &begin,
const Iterator &end )
125 template<
int dim,
int dimworld >
127 struct AlbertaMarkerVector< dim, dimworld >::MarkSubEntities
129 template<
int codim >
132 static const int numSubEntities = Alberta::NumSubEntities< dimension, codim >::value;
134 typedef Alberta::ElementInfo< dimension > ElementInfo;
136 static void apply (
const DofNumbering &dofNumbering,
137 int *(&marker)[ dimension + 1 ],
138 const ElementInfo &elementInfo )
140 int *array = marker[ codim ];
142 const int index = dofNumbering( elementInfo, 0, 0 );
143 for(
int i = 0; i < numSubEntities; ++i )
145 int &mark = array[ dofNumbering( elementInfo, codim, i ) ];
146 mark = std::max( index, mark );
151 template<
int firstCodim,
class Iterator >
152 static void mark (
const DofNumbering &dofNumbering,
int *(&marker)[ dimension + 1 ],
153 const Iterator &begin,
const Iterator &end )
155 for(
int codim = firstCodim; codim <= dimension; ++codim )
157 const int size = dofNumbering.size( codim );
158 marker[ codim ] =
new int[ size ];
160 int *array = marker[ codim ];
161 for(
int i = 0; i < size; ++i )
165 for( Iterator it = begin; it != end; ++it )
167 const ElementInfo &elementInfo = it->impl().elementInfo();
168 Hybrid::forEach( Std::make_index_sequence< dimension+1-firstCodim >{},
169 [ & ](
auto i ){ Codim< i+firstCodim >::apply( dofNumbering, marker, elementInfo ); } );
182 template<
int codim,
class Gr
idImp,
bool leafIterator >
183 class AlbertaGridTreeIterator
185 typedef AlbertaGridTreeIterator< codim, GridImp, leafIterator > This;
188 static const int dimension = GridImp::dimension;
189 static const int codimension = codim;
190 static const int dimensionworld = GridImp::dimensionworld;
193 friend class AlbertaGrid< dimension, dimensionworld >;
195 static const int numSubEntities
196 = Alberta::NumSubEntities< dimension, codimension >::value;
199 typedef Alberta::MeshPointer< dimension > MeshPointer;
200 typedef typename MeshPointer::MacroIterator MacroIterator;
202 typedef typename GridImp::template Codim< codim >::Entity Entity;
203 typedef MakeableInterfaceObject< Entity > EntityObject;
204 typedef typename EntityObject::ImplementationType EntityImp;
205 typedef typename EntityImp::ElementInfo ElementInfo;
207 typedef AlbertaMarkerVector< dimension, dimensionworld > MarkerVector;
209 AlbertaGridTreeIterator ();
212 AlbertaGridTreeIterator (
const This &other );
215 This &operator= (
const This &other );
218 AlbertaGridTreeIterator (
const GridImp &grid,
int travLevel );
221 AlbertaGridTreeIterator (
const GridImp &grid,
222 const MarkerVector *marker,
226 bool equals (
const This &other )
const
228 return entity_.impl().equals( other.entity_.impl() );
232 Entity &dereference ()
const
240 return entity_.impl().level();
248 const GridImp &grid ()
const
250 return entity_.impl().grid();
254 void nextElement ( ElementInfo &elementInfo );
255 void nextElementStop (ElementInfo &elementInfo );
256 bool stopAtElement (
const ElementInfo &elementInfo )
const;
258 void goNext ( ElementInfo &elementInfo );
259 void goNext (
const std::integral_constant< int, 0 > cdVariable,
260 ElementInfo &elementInfo );
261 void goNext (
const std::integral_constant< int, 1 > cdVariable,
262 ElementInfo &elementInfo );
264 void goNext (
const std::integral_constant< int, cd > cdVariable,
265 ElementInfo &elementInfo );
267 mutable Entity entity_;
275 MacroIterator macroIterator_;
278 const MarkerVector *marker_;
286 template<
int dim,
int dimworld >
287 template<
int codim >
288 inline bool AlbertaMarkerVector< dim, dimworld >
289 ::subEntityOnElement (
const ElementInfo &elementInfo,
int subEntity )
const
291 assert( marker_[ codim ] != 0 );
293 const int subIndex = dofNumbering_( elementInfo, codim, subEntity );
294 const int markIndex = marker_[ codim ][ subIndex ];
295 assert( (markIndex >= 0) );
297 const int index = dofNumbering_( elementInfo, 0, 0 );
298 return (markIndex == index);
302 template<
int dim,
int dimworld >
303 template<
int firstCodim,
class Iterator >
304 inline void AlbertaMarkerVector< dim, dimworld >
305 ::markSubEntities (
const Iterator &begin,
const Iterator &end )
308 std::conditional< (firstCodim <= dimension), MarkSubEntities<true>, NoMarkSubEntities<false> >::type
309 ::template mark< firstCodim, Iterator >( dofNumbering_, marker_, begin, end );
313 template<
int dim,
int dimworld >
314 inline void AlbertaMarkerVector< dim, dimworld >::print ( std::ostream &out )
const
316 for(
int codim = 1; codim <= dimension; ++codim )
318 int *marker = marker_[ codim ];
321 const int size = dofNumbering_.size( codim );
323 out <<
"Codimension " << codim <<
" (" << size <<
" entries)" << std::endl;
324 for(
int i = 0; i < size; ++i )
325 out <<
"subentity " << i <<
" visited on Element " << marker[ i ] << std::endl;
335 template<
int codim,
class Gr
idImp,
bool leafIterator >
336 inline AlbertaGridTreeIterator< codim, GridImp, leafIterator >
337 ::AlbertaGridTreeIterator ()
345 template<
int codim,
class Gr
idImp,
bool leafIterator >
346 inline AlbertaGridTreeIterator< codim, GridImp, leafIterator >
347 ::AlbertaGridTreeIterator (
const GridImp &grid,
348 const MarkerVector *marker,
350 : entity_( EntityImp( grid ) ),
352 subEntity_( (codim == 0 ? 0 : -1) ),
353 macroIterator_( grid.meshPointer().begin() ),
356 ElementInfo elementInfo = *macroIterator_;
357 nextElementStop( elementInfo );
359 goNext( elementInfo );
361 entity_.impl().setElement( elementInfo, subEntity_ );
366 template<
int codim,
class Gr
idImp,
bool leafIterator >
367 inline AlbertaGridTreeIterator< codim, GridImp, leafIterator >
368 ::AlbertaGridTreeIterator (
const GridImp &grid,
370 : entity_( EntityImp( grid ) ),
373 macroIterator_( grid.meshPointer().end() ),
379 template<
int codim,
class Gr
idImp,
bool leafIterator >
380 inline AlbertaGridTreeIterator< codim, GridImp, leafIterator >
381 ::AlbertaGridTreeIterator(
const This &other )
382 : entity_( other.entity_ ),
383 level_( other.level_ ),
384 subEntity_( other.subEntity_ ),
385 macroIterator_( other.macroIterator_ ),
386 marker_( other.marker_ )
391 template<
int codim,
class Gr
idImp,
bool leafIterator >
392 inline typename AlbertaGridTreeIterator< codim, GridImp, leafIterator >::This &
393 AlbertaGridTreeIterator< codim, GridImp, leafIterator >::operator= (
const This &other )
395 entity_ = other.entity_;
396 level_ = other.level_;
397 subEntity_ = other.subEntity_;
398 macroIterator_ = other.macroIterator_;
399 marker_ = other.marker_;
405 template<
int codim,
class Gr
idImp,
bool leafIterator >
406 inline void AlbertaGridTreeIterator< codim, GridImp, leafIterator >::increment ()
408 ElementInfo elementInfo = entity_.impl().elementInfo_;
409 goNext ( elementInfo );
411 entity_.impl().setElement( elementInfo, subEntity_ );
415 template<
int codim,
class Gr
idImp,
bool leafIterator >
416 inline void AlbertaGridTreeIterator< codim, GridImp, leafIterator >
417 ::nextElement ( ElementInfo &elementInfo )
419 if( elementInfo.isLeaf() || (elementInfo.level() >= level_) )
421 while( (elementInfo.level() > 0) && (elementInfo.indexInFather() == 1) )
422 elementInfo = elementInfo.father();
423 if( elementInfo.level() == 0 )
426 elementInfo = *macroIterator_;
429 elementInfo = elementInfo.father().child( 1 );
432 elementInfo = elementInfo.child( 0 );
436 template<
int codim,
class Gr
idImp,
bool leafIterator >
437 inline void AlbertaGridTreeIterator< codim, GridImp, leafIterator >
438 ::nextElementStop ( ElementInfo &elementInfo )
440 while( !(!elementInfo || stopAtElement( elementInfo )) )
441 nextElement( elementInfo );
445 template<
int codim,
class Gr
idImp,
bool leafIterator >
446 inline bool AlbertaGridTreeIterator< codim, GridImp, leafIterator >
447 ::stopAtElement (
const ElementInfo &elementInfo )
const
451 return (leafIterator ? elementInfo.isLeaf() : (level_ == elementInfo.level()));
455 template<
int codim,
class Gr
idImp,
bool leafIterator >
456 inline void AlbertaGridTreeIterator< codim, GridImp, leafIterator >
457 ::goNext ( ElementInfo &elementInfo )
459 std::integral_constant< int, codim > codimVariable;
460 goNext( codimVariable, elementInfo );
463 template<
int codim,
class Gr
idImp,
bool leafIterator >
464 inline void AlbertaGridTreeIterator< codim, GridImp, leafIterator >
465 ::goNext (
const std::integral_constant< int, 0 > cdVariable,
466 ElementInfo &elementInfo )
468 assert( stopAtElement( elementInfo ) );
470 nextElement( elementInfo );
471 nextElementStop( elementInfo );
474 template<
int codim,
class Gr
idImp,
bool leafIterator >
475 inline void AlbertaGridTreeIterator< codim, GridImp, leafIterator >
476 ::goNext (
const std::integral_constant< int, 1 > cdVariable,
477 ElementInfo &elementInfo )
479 assert( stopAtElement( elementInfo ) );
482 if( subEntity_ >= numSubEntities )
485 nextElement( elementInfo );
486 nextElementStop( elementInfo );
493 const int face = (dimension == 1 ? (numSubEntities-1)-subEntity_ : subEntity_);
495 const ALBERTA EL *neighbor = elementInfo.elInfo().neigh[ face ];
496 if( (neighbor != NULL) && !elementInfo.isBoundary( face ) )
499 const int elIndex = grid().dofNumbering() ( elementInfo, 0, 0 );
500 const int nbIndex = grid().dofNumbering() ( neighbor, 0, 0 );
501 if( elIndex < nbIndex )
502 goNext( cdVariable, elementInfo );
509 assert( marker_ != 0 );
510 if( !marker_->template subEntityOnElement< 1 >( elementInfo, subEntity_ ) )
511 goNext( cdVariable, elementInfo );
515 template<
int codim,
class Gr
idImp,
bool leafIterator >
517 inline void AlbertaGridTreeIterator< codim, GridImp, leafIterator >
518 ::goNext (
const std::integral_constant< int, cd > cdVariable,
519 ElementInfo &elementInfo )
521 assert( stopAtElement( elementInfo ) );
524 if( subEntity_ >= numSubEntities )
527 nextElement( elementInfo );
528 nextElementStop( elementInfo );
533 assert( marker_ != 0 );
534 if( !marker_->template subEntityOnElement< cd >( elementInfo, subEntity_ ) )
535 goNext( cdVariable, elementInfo );
540 #endif // #if HAVE_ALBERTA
542 #endif // #ifndef DUNE_ALBERTA_TREEITERATOR_HH