dune-grid  2.2.1
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 >
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 
78  typedef ALU3dGridFaceInfo< GridImp::elementType, Comm > FaceInfoType;
79  typedef typename std::auto_ptr< FaceInfoType > FaceInfoPointer;
80 
81  typedef typename SelectType<
82  tetra == GridImp::elementType,
83  ALU3dGridGeometricFaceInfoTetra< Comm >,
84  ALU3dGridGeometricFaceInfoHexa< Comm > >::Type GeometryInfoType;
85 
86  typedef ElementTopologyMapping<GridImp::elementType> ElementTopo;
87  typedef FaceTopologyMapping<GridImp::elementType> FaceTopo;
88 
89  enum { numFaces = EntityCount<GridImp::elementType>::numFaces };
90  enum { numVerticesPerFace =
91  EntityCount<GridImp::elementType>::numVerticesPerFace };
92  enum { numVertices = EntityCount<GridImp::elementType>::numVertices };
93 
94  typedef ALU3dGridIntersectionIterator<GridImp> ThisType;
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 
324  typedef ALU3dGridFaceInfo< GridImp::elementType, Comm > FaceInfoType;
325  typedef typename std::auto_ptr< FaceInfoType > FaceInfoPointer;
326 
327  typedef typename SelectType<
328  tetra == GridImp::elementType,
329  ALU3dGridGeometricFaceInfoTetra< Comm >,
330  ALU3dGridGeometricFaceInfoHexa< Comm > >::Type GeometryInfoType;
331 
332  typedef ElementTopologyMapping<GridImp::elementType> ElementTopo;
333  typedef FaceTopologyMapping<GridImp::elementType> FaceTopo;
334 
335  enum { numFaces = EntityCount<GridImp::elementType>::numFaces };
336  enum { numVerticesPerFace =
337  EntityCount<GridImp::elementType>::numVerticesPerFace };
338  enum { numVertices = EntityCount<GridImp::elementType>::numVertices };
339 
340  typedef ALU3dGridIntersectionIterator<GridImp> BaseType;
341  typedef ALU3dGridLevelIntersectionIterator<GridImp> ThisType;
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 public:
425  typedef typename InternalIteratorType :: val_t val_t;
426 
427  // here the items level will do
428  template <class GridImp, int codim>
429  class GetLevel
430  {
431  public:
432  template <class ItemType>
433  static int getLevel(const GridImp & grid, const ItemType & item, int level )
434  {
435  assert( & item );
436  return (level < 0) ? item.level() : level;
437  }
438  };
439 
440  // level is not needed for codim = 0
441  template <class GridImp>
442  class GetLevel<GridImp,0>
443  {
444  public:
445  template <class ItemType>
446  static int getLevel(const GridImp & grid, const ItemType & item, int level )
447  {
448  return level;
449  }
450  };
451 
452  template <class GridImp>
453  class GetLevel<GridImp,3>
454  {
455  public:
456  template <class ItemType>
457  static int getLevel(const GridImp & grid, const ItemType & item, int level)
458  {
459  return (level < 0) ? grid.getLevelOfLeafVertex(item) : level;
460  }
461  };
462 
463 protected:
464  // set iterator to first item
465  template <class GridImp, class IteratorImp>
466  void firstItem(const GridImp & grid, IteratorImp & it, int level )
467  {
468  InternalIteratorType & iter = it.internalIterator();
469  iter.first();
470  if( ! iter.done() )
471  {
472  assert( iter.size() > 0 );
473  setItem(grid,it,iter,level);
474  }
475  else
476  {
477  it.removeIter();
478  }
479  }
480 
481  // set the iterators entity to actual item
482  template <class GridImp, class IteratorImp>
483  void setItem (const GridImp & grid, IteratorImp & it, InternalIteratorType & iter, int level)
484  {
485  enum { codim = IteratorImp :: codimension };
486  val_t & item = iter.item();
487  assert( item.first || item.second );
488  if( item.first )
489  {
490  it.updateEntityPointer( item.first ,
491  GetLevel<GridImp,codim>::getLevel(grid, *(item.first) , level) );
492  }
493  else
494  it.updateGhostPointer( *item.second );
495  }
496 
497  // increment iterator
498  template <class GridImp, class IteratorImp>
499  void incrementIterator(const GridImp & grid, IteratorImp & it, int level)
500  {
501  // if iter_ is zero, then end iterator
502  InternalIteratorType & iter = it.internalIterator();
503 
504  iter.next();
505 
506  if(iter.done())
507  {
508  it.removeIter();
509  return ;
510  }
511 
512  setItem(grid,it,iter,level);
513  return ;
514  }
515 };
516 
517 //**********************************************************************
518 //
519 // --ALU3dGridLevelIterator
520 // --LevelIterator
524 template<int cd, PartitionIteratorType pitype, class GridImp>
525 class ALU3dGridLevelIterator
526 : public ALU3dGridEntityPointer< cd, GridImp >,
527  public ALU3dGridTreeIterator< ALU3DSPACE ALU3dGridLevelIteratorWrapper< cd, pitype, typename GridImp::MPICommunicatorType > >
528 {
529  enum { dim = GridImp::dimension };
530  enum { dimworld = GridImp::dimensionworld };
531 
532  typedef typename GridImp::MPICommunicatorType Comm;
533 
534  friend class ALU3dGridEntity<3,dim,GridImp>;
535  friend class ALU3dGridEntity<2,dim,GridImp>;
536  friend class ALU3dGridEntity<1,dim,GridImp>;
537  friend class ALU3dGridEntity<0,dim,GridImp>;
538  friend class ALU3dGrid< GridImp::elementType, Comm >;
539 
540  friend class ALU3dGridTreeIterator< ALU3DSPACE ALU3dGridLevelIteratorWrapper< cd, pitype, Comm > >;
541 
542 public:
543  typedef typename GridImp::GridObjectFactoryType FactoryType;
544 
545  typedef typename GridImp::template Codim<cd>::Entity Entity;
547 
549  typedef ALU3dGridLevelIterator<cd,pitype,GridImp> ThisType;
550  // the wrapper for the original iterator of the ALU3dGrid
551  typedef typename ALU3DSPACE ALU3dGridLevelIteratorWrapper< cd, pitype, Comm > IteratorType;
553  typedef typename ALU3DSPACE IteratorElType< cd, Comm >::val_t val_t;
554 
556  ALU3dGridLevelIterator(const FactoryType& factory, int level, bool);
557 
559  ALU3dGridLevelIterator(const FactoryType& factory, int level);
560 
562  ALU3dGridLevelIterator(const ThisType & org);
563 
564  // destructor
566 
568  void increment ();
569 
571  Entity & dereference () const;
572 
574  void releaseEntity () {}
575 
577  ThisType & operator = (const ThisType & org);
578 private:
580  void assign (const ThisType & org);
581 
582  // actual level
583  int level_;
584 
585  // the internal iterator
586  IteratorType * iter_ ;
587 
588  // deletes iter_
589  void removeIter ();
590 
591  IteratorType & internalIterator ()
592  {
593  assert( iter_ );
594  return *iter_;
595  }
596 };
597 
598 //********************************************************************
599 //
600 // --ALU3dGridLeafIterator
601 // --LeafIterator
602 //
603 //********************************************************************
605 template<int cdim, PartitionIteratorType pitype, class GridImp>
606 class ALU3dGridLeafIterator
607 : public ALU3dGridEntityPointer< cdim, GridImp >,
608  public ALU3dGridTreeIterator< ALU3DSPACE ALU3dGridLeafIteratorWrapper< cdim, pitype, typename GridImp::MPICommunicatorType > >
609 {
610  enum { dim = GridImp :: dimension };
611 
612  friend class ALU3dGridEntity<cdim,dim,GridImp>;
613  enum { codim = cdim };
614 
615  typedef typename GridImp::MPICommunicatorType Comm;
616 
617 public:
618  typedef typename GridImp::GridObjectFactoryType FactoryType;
619 
620  typedef typename GridImp::template Codim<cdim>::Entity Entity;
621 
622  typedef typename ALU3DSPACE ALU3dGridLeafIteratorWrapper< cdim, pitype, Comm > IteratorType ;
624 
626  typedef typename ALU3DSPACE IteratorElType< cdim, Comm >::val_t val_t;
627 
629 
631  ALU3dGridLeafIterator(const FactoryType& factory, int level);
632 
634  ALU3dGridLeafIterator(const FactoryType& factory, int level , bool isBegin);
635 
637  ALU3dGridLeafIterator(const ThisType & org);
638 
641 
643  void increment ();
644 
646  Entity & dereference () const;
647 
649  void releaseEntity () {}
650 
652  ThisType & operator = (const ThisType & org);
653 
654 private:
655  // the internal iterator
656  IteratorType * iter_;
657 
658  // max level for iteration
659  int walkLevel_ ;
660 
662  void assign (const ThisType & org);
663 
664  // deletes iter_
665  void removeIter () ;
666 
667  // return reference to iter_
668  InternalIteratorType & internalIterator ()
669  {
670  assert( iter_ );
671  return *iter_;
672  }
673 };
674 
675 // - HierarchicIteraor
676 // --HierarchicIterator
677 template<class GridImp>
678 class ALU3dGridHierarchicIterator
679 : public ALU3dGridEntityPointer<0,GridImp>
680 // public HierarchicIteratorDefaultImplementation <GridImp,ALU3dGridHierarchicIterator>
681 {
682  typedef ALU3dGridHierarchicIterator<GridImp> ThisType;
683  enum { dim = GridImp::dimension };
684 
685  typedef typename GridImp::MPICommunicatorType Comm;
686 
687  typedef ALU3dImplTraits< GridImp::elementType, Comm > ImplTraits;
688  typedef typename ImplTraits::HElementType HElementType;
689  typedef typename ImplTraits::HBndSegType HBndSegType;
690 
691  template < class PointerType, class CommT >
692  class GhostElementStorage;
693 
695  template < class PointerType >
696  class GhostElementStorage< PointerType, No_Comm >
697  {
698  public:
699  GhostElementStorage() {}
700  explicit GhostElementStorage( const PointerType& ) {}
701  PointerType& operator * () { PointerType* p = 0; assert( false ); abort(); return *p; }
702  const PointerType* ghost () const { return 0; }
703  PointerType* nextGhost () const { return 0; }
704  PointerType* operator -> () const { return 0; }
705  bool operator != (const PointerType* ) const { return false; }
706  bool operator ! () const { return true ; }
707  GhostElementStorage& operator= (const GhostElementStorage& ) { return *this; }
708  GhostElementStorage& operator= (const PointerType* ) { return *this; }
709  bool valid () const { return false; }
710  };
711 
712 #if ALU3DGRID_PARALLEL
713 
714  template < class PointerType >
715  class GhostElementStorage< PointerType, MPI_Comm >
716  {
717  private:
718  // pointers to ghost and current ghost
719  const HBndSegType * ghost_;
720  HBndSegType * nextGhost_;
721  public:
722  GhostElementStorage() : ghost_( 0 ), nextGhost_( 0 ) {}
723  explicit GhostElementStorage( const PointerType& gh ) : ghost_( &gh ), nextGhost_( 0 ) {}
724  GhostElementStorage( const GhostElementStorage& org )
725  : ghost_( org.ghost_ ), nextGhost_( org.nextGhost_ ) {}
726 
727  PointerType& operator * () { assert( nextGhost_ ); return *nextGhost_; }
728  const PointerType* ghost () const { return ghost_; }
729  PointerType* nextGhost () const { return nextGhost_; }
730  PointerType* operator -> () { return nextGhost_; }
731  bool operator != (const PointerType* p ) const { return (nextGhost_ != p); }
732  bool operator ! () const { return nextGhost_ == 0; }
733  GhostElementStorage& operator= (const GhostElementStorage& org)
734  {
735  ghost_ = org.ghost_;
736  nextGhost_ = org.nextGhost_;
737  return *this;
738  }
739  GhostElementStorage& operator= (PointerType* p)
740  {
741  nextGhost_ = p;
742  return *this;
743  }
744  bool valid () const { return (ghost_ != 0); }
745  };
746 #endif
747 
748 public:
749  typedef typename GridImp::GridObjectFactoryType FactoryType;
750 
751  typedef typename GridImp::template Codim<0>::Entity Entity;
752  typedef typename GridImp::ctype ctype;
753 
756  const HElementType & elem,
757  int maxlevel, bool end );
758 
761  const HBndSegType& ghost,
762  int maxlevel,
763  bool end);
764 
767 
769  void increment();
770 
772  Entity & dereference () const;
773 
775  void releaseEntity () {}
776 
778  ThisType & operator = (const ThisType & org);
779 
780 private:
781  // assign iterator
782  void assign(const ThisType & org);
783 
785  int getLevel(const HElementType* item) const;
786 
788  int getLevel(const HBndSegType* face) const;
789 
790  // go to next valid element
791  template <class HItemType>
792  HItemType* goNextElement (const HItemType* startElem, HItemType * oldEl);
793 
795  const HElementType * elem_;
796 
797  // pointers to ghost and current ghost
798  GhostElementStorage< HBndSegType, Comm > ghostElem_;
799 
801  int maxlevel_;
802 };
803 
804 
805 } // end namespace Dune
806 
807 #include "iterator_imp.cc"
808 
809 #endif