dune-grid  2.2.0
alugrid/3d/iterator.hh
Go to the documentation of this file.
1 // -*- tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=8 sw=2 sts=2:
3 
4 #ifndef DUNE_ALU3DGRIDITERATOR_HH
5 #define DUNE_ALU3DGRIDITERATOR_HH
6 
7 // System includes
8 
9 // Dune includes
10 #include <dune/grid/common/grid.hh>
13 
14 // Local includes
15 #include "alu3dinclude.hh"
16 #include "topology.hh"
17 #include "faceutility.hh"
18 #include "alu3diterators.hh"
19 
20 namespace Dune {
21  // Forward declarations
22  template<int cd, int dim, class GridImp>
23  class ALU3dGridEntity;
24  template<int cd, PartitionIteratorType pitype, class GridImp >
25  class ALU3dGridLevelIterator;
26  template<int cd, class GridImp >
27  class ALU3dGridEntityPointer;
28  template<int mydim, int coorddim, class GridImp>
29  class ALU3dGridGeometry;
30  template<class GridImp>
31  class ALU3dGridHierarchicIterator;
32  template<class GridImp>
33  class ALU3dGridIntersectionIterator;
34  template<int codim, PartitionIteratorType pitype, class GridImp>
35  class ALU3dGridLeafIterator;
36  template< ALU3dGridElementType, class >
37  class ALU3dGrid;
38  template< ALU3dGridElementType, class >
39  class ALU3dGridFaceInfo;
40  template< ALU3dGridElementType, class >
41  class ALU3dGridGeometricFaceInfo;
42 
43 //**********************************************************************
44 //
45 // --ALU3dGridIntersectionIterator
46 // --IntersectionIterator
54 template<class GridImp>
56 //: public IntersectionIteratorDefaultImplementation <GridImp,ALU3dGridIntersectionIterator>
57 {
58  enum { dim = GridImp::dimension };
59  enum { dimworld = GridImp::dimensionworld };
60 
61  typedef typename GridImp::MPICommunicatorType Comm;
62 
63  typedef ALU3dImplTraits< GridImp::elementType, Comm > ImplTraits;
64 
65  typedef typename ImplTraits::HElementType HElementType ;
66  typedef typename ImplTraits::HBndSegType HBndSegType;
67  typedef typename ImplTraits::GEOElementType GEOElementType;
68  typedef typename ImplTraits::IMPLElementType IMPLElementType;
69  typedef typename ImplTraits::GEOFaceType GEOFaceType;
70  typedef typename ImplTraits::NeighbourPairType NeighbourPairType;
71  typedef typename ImplTraits::BNDFaceType BNDFaceType;
72 
73  typedef typename ALU3dImplTraits< tetra, Comm >::GEOElementType GEOTetraElementType;
74  typedef typename ALU3dImplTraits< hexa, Comm >::GEOElementType GEOHexaElementType;
75  typedef typename ALU3dImplTraits< tetra, Comm >::BNDFaceType GEOTriangleBndType;
76  typedef typename ALU3dImplTraits< hexa, Comm >::BNDFaceType GEOQuadBndType;
77 
79  typedef typename std::auto_ptr< FaceInfoType > FaceInfoPointer;
80 
81  typedef typename SelectType<
82  tetra == GridImp::elementType,
84  ALU3dGridGeometricFaceInfoHexa< Comm > >::Type GeometryInfoType;
85 
88 
90  enum { numVerticesPerFace =
93 
95 
96  friend class ALU3dGridEntity<0,dim,GridImp>;
97  friend class IntersectionIteratorWrapper<GridImp,ThisType>;
98 
99 protected:
101 
102  typedef typename GridImp::Traits::template Codim< 1 >::GeometryImpl GeometryImpl;
103  typedef typename GridImp::Traits::template Codim< 1 >::LocalGeometryImpl LocalGeometryImpl;
104 
105 public:
106  typedef typename GridImp::GridObjectFactoryType FactoryType;
107 
108  typedef typename GridImp::template Codim<0>::Entity Entity;
109  typedef typename GridImp::template Codim<1>::Geometry Geometry;
110  typedef typename GridImp::template Codim<1>::LocalGeometry LocalGeometry;
111 
115 
116  typedef FieldVector<alu3d_ctype, dimworld> NormalType;
118 
120 
124  HElementType *el,
125  int wLevel,bool end=false);
126 
127  ALU3dGridIntersectionIterator(const FactoryType& factory, int wLevel);
128 
131 
134 
135  const Intersection &dereference () const
136  {
137  return reinterpret_cast< const Intersection & >( *this );
138  }
139 
141  bool equals (const ALU3dGridIntersectionIterator<GridImp> & i) const;
142 
144  void increment ();
145 
147  EntityPointer outside() const;
148 
150  EntityPointer inside() const;
151 
153  bool boundary () const;
154 
156  bool neighbor () const;
157 
159  bool levelNeighbor () const;
160 
162  bool leafNeighbor () const;
163 
165  int boundaryId () const;
166 
168  size_t boundarySegmentIndex() const;
169 
175 
180  Geometry geometry () const;
181 
183  GeometryType type () const;
184 
187  int indexInInside () const;
188 
193 
196  int indexInOutside () const;
197 
199  int twistInSelf() const { return twistInInside(); }
200 
202  int twistInNeighbor() const { return twistInOutside(); }
203 
205  int twistInInside() const;
206 
208  int twistInOutside() const;
209 
212  NormalType & unitOuterNormal (const FieldVector<alu3d_ctype, dim-1>& local) const ;
213 
216  NormalType & outerNormal (const FieldVector<alu3d_ctype, dim-1>& local) const;
217 
220  NormalType & integrationOuterNormal (const FieldVector<alu3d_ctype, dim-1>& local) const;
221 
223  int level () const;
224 
225  int outsideLevel () const { return connector_.outsideLevel(); }
226 
228  bool conforming () const
229  {
231  }
232 
234  const GEOFaceType& getItem() const { return connector_.face(); }
235 
236 protected:
237  // set interator to end iterator
238  void done () ;
239  template< class EntityType > void done ( const EntityType &en ) { done(); }
240 
241  // reset IntersectionIterator to first neighbour
242  void setFirstItem(const HElementType & elem, int wLevel);
243 
244  // reset IntersectionIterator to first neighbour
245  void setInteriorItem(const HElementType & elem,
246  const BNDFaceType& bnd, int wLevel);
247 
248  // reset IntersectionIterator to first neighbour
249  template <class EntityType>
250  void first(const EntityType & en, int wLevel);
251 
252  // set new face
253  void setNewFace(const GEOFaceType& newFace);
254 
255 private:
256  // set new face (only LeafIntersectionIterator)
257  void setGhostFace(const GEOFaceType& newFace);
258 
259 protected:
260  // generate local geometries
261  void buildLocalGeometries() const;
262 
263  // get the face corresponding to the index
265  getFace ( const GEOTriangleBndType &bnd, int index ) const;
266 
267  // get the face corresponding to the index
269  getFace ( const GEOQuadBndType &bnd, int index ) const;
270 
271  // get the face corresponding to the index
273  getFace ( const GEOTetraElementType &elem, int index ) const;
274 
276  getFace ( const GEOHexaElementType &elem, int index ) const;
277 
281  mutable GeometryInfoType geoProvider_; // need to initialise
282 
283  // reference to factory
285 
287  const IMPLElementType* item_;
288 
290  const BNDFaceType* ghost_;
291 
292  mutable int innerLevel_;
293  mutable int index_;
294 
298 
299  // unit outer normal
301 
302  // true if end iterator
303  bool done_;
304 };
305 
306 template<class GridImp>
308 public ALU3dGridIntersectionIterator<GridImp>
309 {
310  enum { dim = GridImp::dimension };
311  enum { dimworld = GridImp::dimensionworld };
312 
313  typedef typename GridImp::MPICommunicatorType Comm;
314 
315  typedef ALU3dImplTraits< GridImp::elementType, Comm > ImplTraits;
316 
317  typedef typename ImplTraits::HElementType HElementType ;
318  typedef typename ImplTraits::GEOElementType GEOElementType;
319  typedef typename ImplTraits::IMPLElementType IMPLElementType;
320  typedef typename ImplTraits::GEOFaceType GEOFaceType;
321  typedef typename ImplTraits::NeighbourPairType NeighbourPairType;
322  typedef typename ImplTraits::BNDFaceType BNDFaceType;
323 
325  typedef typename std::auto_ptr< FaceInfoType > FaceInfoPointer;
326 
327  typedef typename SelectType<
328  tetra == GridImp::elementType,
330  ALU3dGridGeometricFaceInfoHexa< Comm > >::Type GeometryInfoType;
331 
334 
336  enum { numVerticesPerFace =
338  enum { numVertices = EntityCount<GridImp::elementType>::numVertices };
339 
342 
343  friend class ALU3dGridEntity<0,dim,GridImp>;
344  friend class IntersectionIteratorWrapper<GridImp,ThisType>;
345 protected:
346  using BaseType :: item_;
347  using BaseType :: ghost_;
349  using BaseType :: index_;
352  using BaseType :: factory_;
353  using BaseType :: done_;
354  using BaseType :: boundary;
355  using BaseType :: done ;
356  using BaseType :: getFace;
357  using BaseType :: neighbor ;
358 
359 public:
360  typedef typename GridImp::GridObjectFactoryType FactoryType;
361 
362  //typedef Dune :: Intersection< const GridImp, ThisType >
364 
368  HElementType *el,
369  int wLevel,bool end=false);
370 
371  ALU3dGridLevelIntersectionIterator(const FactoryType& factory, int wLevel);
372 
375 
377  void assign(const ThisType & org);
378 
380  void increment ();
381 
382  // reset IntersectionIterator to first neighbour
383  template <class EntityType>
384  void first(const EntityType & en, int wLevel);
385 
387  bool neighbor () const;
388 
390  bool levelNeighbor () const;
391 
393  bool leafNeighbor () const;
394 
396  bool conforming () const
397  {
398  assert( !neighbor() || this->connector_.conformanceState() == FaceInfoType::CONFORMING );
399  return true;
400  }
401 private:
402  // set new face
403  void setNewFace(const GEOFaceType& newFace);
404 
405  // reset IntersectionIterator to first neighbour
406  void setFirstItem(const HElementType & elem, int wLevel);
407 
408  // reset IntersectionIterator to first neighbour
409  void setInteriorItem(const HElementType & elem,
410  const BNDFaceType& bnd, int wLevel);
411 
412  bool levelNeighbor_;
413  bool isLeafItem_;
414 };
415 
417 //
418 // --IterationImpl
419 //
421 template <class InternalIteratorType >
423 {
424  typedef typename InternalIteratorType :: val_t val_t;
425 
426  // here the items level will do
427  template <class GridImp, int codim>
428  struct GetLevel
429  {
430  template <class ItemType>
431  static int getLevel(const GridImp & grid, const ItemType & item, int level )
432  {
433  assert( & item );
434  return (level < 0) ? item.level() : level;
435  }
436  };
437 
438  // level is not needed for codim = 0
439  template <class GridImp>
440  struct GetLevel<GridImp,0>
441  {
442  template <class ItemType>
443  static int getLevel(const GridImp & grid, const ItemType & item, int level )
444  {
445  return level;
446  }
447  };
448 
449  template <class GridImp>
450  struct GetLevel<GridImp,3>
451  {
452  template <class ItemType>
453  static int getLevel(const GridImp & grid, const ItemType & item, int level)
454  {
455  return (level < 0) ? grid.getLevelOfLeafVertex(item) : level;
456  }
457  };
458 
459 protected:
460  // set iterator to first item
461  template <class GridImp, class IteratorImp>
462  void firstItem(const GridImp & grid, IteratorImp & it, int level )
463  {
464  InternalIteratorType & iter = it.internalIterator();
465  iter.first();
466  if( ! iter.done() )
467  {
468  assert( iter.size() > 0 );
469  setItem(grid,it,iter,level);
470  }
471  else
472  {
473  it.removeIter();
474  }
475  }
476 
477  // set the iterators entity to actual item
478  template <class GridImp, class IteratorImp>
479  void setItem (const GridImp & grid, IteratorImp & it, InternalIteratorType & iter, int level)
480  {
481  enum { codim = IteratorImp :: codimension };
482  val_t & item = iter.item();
483  assert( item.first || item.second );
484  if( item.first )
485  {
486  it.updateEntityPointer( item.first ,
487  GetLevel<GridImp,codim>::getLevel(grid, *(item.first) , level) );
488  }
489  else
490  it.updateGhostPointer( *item.second );
491  }
492 
493  // increment iterator
494  template <class GridImp, class IteratorImp>
495  void incrementIterator(const GridImp & grid, IteratorImp & it, int level)
496  {
497  // if iter_ is zero, then end iterator
498  InternalIteratorType & iter = it.internalIterator();
499 
500  iter.next();
501 
502  if(iter.done())
503  {
504  it.removeIter();
505  return ;
506  }
507 
508  setItem(grid,it,iter,level);
509  return ;
510  }
511 };
512 
513 //**********************************************************************
514 //
515 // --ALU3dGridLevelIterator
516 // --LevelIterator
520 template<int cd, PartitionIteratorType pitype, class GridImp>
522 : public ALU3dGridEntityPointer< cd, GridImp >,
523  public ALU3dGridTreeIterator< ALU3DSPACE ALU3dGridLevelIteratorWrapper< cd, pitype, typename GridImp::MPICommunicatorType > >
524 {
525  enum { dim = GridImp::dimension };
526  enum { dimworld = GridImp::dimensionworld };
527 
528  typedef typename GridImp::MPICommunicatorType Comm;
529 
530  friend class ALU3dGridEntity<3,dim,GridImp>;
531  friend class ALU3dGridEntity<2,dim,GridImp>;
532  friend class ALU3dGridEntity<1,dim,GridImp>;
533  friend class ALU3dGridEntity<0,dim,GridImp>;
534  friend class ALU3dGrid< GridImp::elementType, Comm >;
535 
536  friend class ALU3dGridTreeIterator< ALU3DSPACE ALU3dGridLevelIteratorWrapper< cd, pitype, Comm > >;
537 
538 public:
539  typedef typename GridImp::GridObjectFactoryType FactoryType;
540 
541  typedef typename GridImp::template Codim<cd>::Entity Entity;
543 
545  typedef ALU3dGridLevelIterator<cd,pitype,GridImp> ThisType;
546  // the wrapper for the original iterator of the ALU3dGrid
547  typedef typename ALU3DSPACE ALU3dGridLevelIteratorWrapper< cd, pitype, Comm > IteratorType;
549  typedef typename ALU3DSPACE IteratorElType< cd, Comm >::val_t val_t;
550 
552  ALU3dGridLevelIterator(const FactoryType& factory, int level, bool);
553 
555  ALU3dGridLevelIterator(const FactoryType& factory, int level);
556 
558  ALU3dGridLevelIterator(const ThisType & org);
559 
560  // destructor
562 
564  void increment ();
565 
567  Entity & dereference () const;
568 
570  void releaseEntity () {}
571 
573  ThisType & operator = (const ThisType & org);
574 private:
576  void assign (const ThisType & org);
577 
578  // actual level
579  int level_;
580 
581  // the internal iterator
582  IteratorType * iter_ ;
583 
584  // deletes iter_
585  void removeIter ();
586 
587  IteratorType & internalIterator ()
588  {
589  assert( iter_ );
590  return *iter_;
591  }
592 };
593 
594 //********************************************************************
595 //
596 // --ALU3dGridLeafIterator
597 // --LeafIterator
598 //
599 //********************************************************************
601 template<int cdim, PartitionIteratorType pitype, class GridImp>
603 : public ALU3dGridEntityPointer< cdim, GridImp >,
604  public ALU3dGridTreeIterator< ALU3DSPACE ALU3dGridLeafIteratorWrapper< cdim, pitype, typename GridImp::MPICommunicatorType > >
605 {
606  enum { dim = GridImp :: dimension };
607 
608  friend class ALU3dGridEntity<cdim,dim,GridImp>;
609  enum { codim = cdim };
610 
611  typedef typename GridImp::MPICommunicatorType Comm;
612 
613 public:
614  typedef typename GridImp::GridObjectFactoryType FactoryType;
615 
616  typedef typename GridImp::template Codim<cdim>::Entity Entity;
617 
618  typedef typename ALU3DSPACE ALU3dGridLeafIteratorWrapper< cdim, pitype, Comm > IteratorType ;
620 
622  typedef typename ALU3DSPACE IteratorElType< cdim, Comm >::val_t val_t;
623 
625 
627  ALU3dGridLeafIterator(const FactoryType& factory, int level);
628 
630  ALU3dGridLeafIterator(const FactoryType& factory, int level , bool isBegin);
631 
633  ALU3dGridLeafIterator(const ThisType & org);
634 
637 
639  void increment ();
640 
642  Entity & dereference () const;
643 
645  void releaseEntity () {}
646 
648  ThisType & operator = (const ThisType & org);
649 
650 private:
651  // the internal iterator
652  IteratorType * iter_;
653 
654  // max level for iteration
655  int walkLevel_ ;
656 
658  void assign (const ThisType & org);
659 
660  // deletes iter_
661  void removeIter () ;
662 
663  // return reference to iter_
664  InternalIteratorType & internalIterator ()
665  {
666  assert( iter_ );
667  return *iter_;
668  }
669 };
670 
671 // - HierarchicIteraor
672 // --HierarchicIterator
673 template<class GridImp>
675 : public ALU3dGridEntityPointer<0,GridImp>
676 // public HierarchicIteratorDefaultImplementation <GridImp,ALU3dGridHierarchicIterator>
677 {
679  enum { dim = GridImp::dimension };
680 
681  typedef typename GridImp::MPICommunicatorType Comm;
682 
683  typedef ALU3dImplTraits< GridImp::elementType, Comm > ImplTraits;
684  typedef typename ImplTraits::HElementType HElementType;
685  typedef typename ImplTraits::HBndSegType HBndSegType;
686 
687  template < class PointerType, class CommT >
688  class GhostElementStorage;
689 
691  template < class PointerType >
692  struct GhostElementStorage< PointerType, No_Comm >
693  {
694  GhostElementStorage() {}
695  explicit GhostElementStorage( const PointerType& ) {}
696  PointerType& operator * () { PointerType* p = 0; assert( false ); abort(); return *p; }
697  const PointerType* ghost () const { return 0; }
698  PointerType* nextGhost () const { return 0; }
699  PointerType* operator -> () const { return 0; }
700  bool operator != (const PointerType* ) const { return false; }
701  bool operator ! () const { return true ; }
702  GhostElementStorage& operator= (const GhostElementStorage& ) { return *this; }
703  GhostElementStorage& operator= (const PointerType* ) { return *this; }
704  bool valid () const { return false; }
705  };
706 
707 #if ALU3DGRID_PARALLEL
708 
709  template < class PointerType >
710  struct GhostElementStorage< PointerType, MPI_Comm >
711  {
712  private:
713  // pointers to ghost and current ghost
714  const HBndSegType * ghost_;
715  HBndSegType * nextGhost_;
716  public:
717  GhostElementStorage() : ghost_( 0 ), nextGhost_( 0 ) {}
718  explicit GhostElementStorage( const PointerType& gh ) : ghost_( &gh ), nextGhost_( 0 ) {}
719  GhostElementStorage( const GhostElementStorage& org )
720  : ghost_( org.ghost_ ), nextGhost_( org.nextGhost_ ) {}
721 
722  PointerType& operator * () { assert( nextGhost_ ); return *nextGhost_; }
723  const PointerType* ghost () const { return ghost_; }
724  PointerType* nextGhost () const { return nextGhost_; }
725  PointerType* operator -> () { return nextGhost_; }
726  bool operator != (const PointerType* p ) const { return (nextGhost_ != p); }
727  bool operator ! () const { return nextGhost_ == 0; }
728  GhostElementStorage& operator= (const GhostElementStorage& org)
729  {
730  ghost_ = org.ghost_;
731  nextGhost_ = org.nextGhost_;
732  return *this;
733  }
734  GhostElementStorage& operator= (PointerType* p)
735  {
736  nextGhost_ = p;
737  return *this;
738  }
739  bool valid () const { return (ghost_ != 0); }
740  };
741 #endif
742 
743 public:
744  typedef typename GridImp::GridObjectFactoryType FactoryType;
745 
746  typedef typename GridImp::template Codim<0>::Entity Entity;
747  typedef typename GridImp::ctype ctype;
748 
751  const HElementType & elem,
752  int maxlevel, bool end );
753 
756  const HBndSegType& ghost,
757  int maxlevel,
758  bool end);
759 
762 
764  void increment();
765 
767  Entity & dereference () const;
768 
770  void releaseEntity () {}
771 
773  ThisType & operator = (const ThisType & org);
774 
775 private:
776  // assign iterator
777  void assign(const ThisType & org);
778 
780  int getLevel(const HElementType* item) const;
781 
783  int getLevel(const HBndSegType* face) const;
784 
785  // go to next valid element
786  template <class HItemType>
787  HItemType* goNextElement (const HItemType* startElem, HItemType * oldEl);
788 
790  const HElementType * elem_;
791 
792  // pointers to ghost and current ghost
793  GhostElementStorage< HBndSegType, Comm > ghostElem_;
794 
796  int maxlevel_;
797 };
798 
799 
800 } // end namespace Dune
801 
802 #include "iterator_imp.cc"
803 
804 #endif