3d/iterator.hh

00001 #ifndef DUNE_ALU3DGRIDITERATOR_HH
00002 #define DUNE_ALU3DGRIDITERATOR_HH
00003 
00004 // System includes
00005 
00006 // Dune includes
00007 #include <dune/grid/common/grid.hh>
00008 #include <dune/grid/common/intersectioniteratorwrapper.hh>
00009 
00010 // Local includes
00011 #include "alu3dinclude.hh"
00012 #include "topology.hh"
00013 #include "faceutility.hh"
00014 #include "alu3diterators.hh"
00015 #include "memory.hh"
00016 
00017 namespace Dune {
00018   // Forward declarations
00019   template<int cd, int dim, class GridImp> 
00020   class ALU3dGridEntity;
00021   template<int cd, PartitionIteratorType pitype, class GridImp >
00022   class ALU3dGridLevelIterator;
00023   template<int cd, class GridImp > 
00024   class ALU3dGridEntityPointer;
00025   template<int mydim, int coorddim, class GridImp>
00026   class ALU3dGridGeometry;
00027   template<class GridImp> 
00028   class ALU3dGridHierarchicIterator;
00029   template<class GridImp>
00030   class ALU3dGridIntersectionIterator;
00031   template<int codim, PartitionIteratorType pitype, class GridImp>
00032   class ALU3dGridLeafIterator;
00033   template<int dim, int dimworld, ALU3dGridElementType elType> 
00034   class ALU3dGrid;
00035   template <ALU3dGridElementType type>
00036   class ALU3dGridFaceInfo;
00037   template <ALU3dGridElementType elType>
00038   class ALU3dGridGeometricFaceInfo;
00039 
00040 //**********************************************************************
00041 //
00042 // --ALU3dGridIntersectionIterator
00043 // --IntersectionIterator
00051 template<class GridImp>
00052 class ALU3dGridIntersectionIterator  
00053 : public IntersectionIteratorDefaultImplementation <GridImp,ALU3dGridIntersectionIterator>
00054 {
00055   enum { dim       = GridImp::dimension };
00056   enum { dimworld  = GridImp::dimensionworld };
00057     
00058   typedef ALU3dImplTraits<GridImp::elementType> ImplTraits;
00059   typedef typename ImplTraits::GEOElementType GEOElementType;
00060   typedef typename ImplTraits::IMPLElementType IMPLElementType;
00061   typedef typename ImplTraits::GEOFaceType GEOFaceType;
00062   typedef typename ImplTraits::NeighbourPairType NeighbourPairType;
00063   typedef typename ImplTraits::PLLBndFaceType PLLBndFaceType;
00064   typedef typename ImplTraits::BNDFaceType BNDFaceType;
00065 
00066   typedef ALU3dGridFaceInfo<GridImp::elementType> FaceInfoType;
00067   typedef typename std::auto_ptr<FaceInfoType> FaceInfoPointer;
00068 
00069   typedef typename SelectType<
00070     SameType<Int2Type<tetra>, Int2Type<GridImp::elementType> >::value,
00071     ALU3dGridGeometricFaceInfoTetra,
00072     ALU3dGridGeometricFaceInfoHexa
00073                    >::Type GeometryInfoType;
00074       
00075   typedef ElementTopologyMapping<GridImp::elementType> ElementTopo;
00076   typedef FaceTopologyMapping<GridImp::elementType> FaceTopo;
00077 
00078   enum { numFaces = EntityCount<GridImp::elementType>::numFaces };
00079   enum { numVerticesPerFace = 
00080          EntityCount<GridImp::elementType>::numVerticesPerFace };
00081   enum { numVertices = EntityCount<GridImp::elementType>::numVertices };
00082 
00083   typedef ALU3dGridIntersectionIterator<GridImp> ThisType;
00084   
00085   friend class ALU3dGridEntity<0,dim,GridImp>;
00086   friend class IntersectionIteratorWrapper<GridImp,ThisType>;
00087 
00088 protected:  
00089   enum IntersectionIteratorType { IntersectionLeaf , IntersectionLevel, IntersectionBoth }; 
00090 
00091 public:
00092   typedef typename GridImp::template Codim<0>::Entity Entity;
00093   typedef typename GridImp::template Codim<1>::Geometry Geometry;
00094   typedef typename GridImp::template Codim<1>::LocalGeometry LocalGeometry;
00095   
00096   typedef ALU3dGridGeometry<dim-1,dimworld,GridImp> GeometryImp;
00097   typedef MakeableInterfaceObject<Geometry> GeometryObject;
00098   
00099   typedef FieldVector<alu3d_ctype, dimworld> NormalType;
00100   typedef ALU3dGridEntityPointer<0,GridImp> EntityPointer;
00101 
00102   typedef ALUMemoryProvider< ThisType > StorageType;
00103   
00106   ALU3dGridIntersectionIterator(const GridImp & grid, 
00107                                 ALU3DSPACE HElementType *el,
00108                                 int wLevel,bool end=false);
00109   
00110   ALU3dGridIntersectionIterator(const GridImp & grid,int wLevel);
00111   
00113   ALU3dGridIntersectionIterator(const ALU3dGridIntersectionIterator<GridImp> & org);
00114 
00116   void assign(const ALU3dGridIntersectionIterator<GridImp> & org);
00117 
00119   bool equals (const ALU3dGridIntersectionIterator<GridImp> & i) const;
00120 
00122   void increment ();
00123 
00125   EntityPointer outside() const;
00126 
00128   EntityPointer inside() const;
00129 
00131   bool boundary () const;
00132 
00134   bool neighbor () const; 
00135 
00137   bool levelNeighbor () const; 
00138 
00140   bool leafNeighbor () const; 
00141 
00143   int boundaryId () const; 
00144       
00149   const LocalGeometry & intersectionSelfLocal () const;
00150 
00155   const Geometry & intersectionGlobal () const;
00156 
00159   int numberInSelf () const;
00160 
00164   const LocalGeometry & intersectionNeighborLocal () const;
00165 
00168   int numberInNeighbor () const;
00169  
00171   int twistInSelf() const;
00172 
00174   int twistInNeighbor() const;
00175 
00178   NormalType & unitOuterNormal (const FieldVector<alu3d_ctype, dim-1>& local) const ;
00179   
00182   NormalType & outerNormal (const FieldVector<alu3d_ctype, dim-1>& local) const;
00183 
00186   NormalType & integrationOuterNormal (const FieldVector<alu3d_ctype, dim-1>& local) const;
00187 
00189   int level () const;
00190 
00192   bool conforming () const 
00193   {
00194     return (connector_.conformanceState() == FaceInfoType::CONFORMING);  
00195   }
00196 
00197 protected:
00198   // set interator to end iterator 
00199   void done () ;
00200   
00201   void outputElementInfo() const;
00202 
00203   void outputFaceInfo() const;
00204 
00205   template <typename T>
00206   void printToScreen(int duneIdx, int aluIdx, 
00207                      const T& info) const;
00208   
00209   void printToScreen(int duneIdx, int aluIdx) const;
00210 
00211   // used in printToScreen
00212   NormalType convert2FV(const alu3d_ctype (&p)[3]) const;
00213 
00214   // reset IntersectionIterator to first neighbour 
00215   void setFirstItem(const ALU3DSPACE HElementType & elem, int wLevel);
00216 
00217   // reset IntersectionIterator to first neighbour 
00218   template <class EntityType>
00219   void first(const EntityType & en, int wLevel);
00220 
00221   // set new face
00222   void setNewFace(const GEOFaceType& newFace);
00223 
00224   void buildLocalGeometries() const;
00225   
00226   void buildGlobalGeometry() const;
00227 
00228   // get the face corresponding to the index
00229   const typename ALU3dImplTraits<tetra>::GEOFaceType*
00230   getFace(const ALU3DSPACE GEOTetraElementType& elem, int index) const;
00231 
00232   const typename ALU3dImplTraits<hexa>::GEOFaceType* 
00233   getFace(const ALU3DSPACE GEOHexaElementType& elem, int index) const;
00234 
00237   mutable FaceInfoType      connector_;
00238   mutable GeometryInfoType  geoProvider_; // need to initialise
00239 
00240   // reference to grid 
00241   const GridImp & grid_;
00242   
00244   const IMPLElementType* item_;  
00245 
00246   //mutable int nFaces_;
00247   //mutable int walkLevel_;
00248   mutable int index_;
00249 
00250   mutable bool generatedGlobalGeometry_;
00251   mutable bool generatedLocalGeometries_;
00252 
00253   mutable GeometryObject intersectionGlobal_;
00254   mutable GeometryImp &  intersectionGlobalImp_;
00255   mutable GeometryObject intersectionSelfLocal_;
00256   mutable GeometryImp &  intersectionSelfLocalImp_;
00257   mutable GeometryObject intersectionNeighborLocal_;
00258   mutable GeometryImp &  intersectionNeighborLocalImp_;
00259 
00260   // unit outer normal
00261   mutable NormalType unitOuterNormal_;
00262 
00263   // true if end iterator 
00264   bool done_;
00265 };
00266 
00267 template<class GridImp>
00268 class ALU3dGridLevelIntersectionIterator : 
00269 public ALU3dGridIntersectionIterator<GridImp>
00270 {
00271   enum { dim       = GridImp::dimension };
00272   enum { dimworld  = GridImp::dimensionworld };
00273     
00274   typedef ALU3dImplTraits<GridImp::elementType> ImplTraits;
00275   typedef typename ImplTraits::GEOElementType GEOElementType;
00276   typedef typename ImplTraits::IMPLElementType IMPLElementType;
00277   typedef typename ImplTraits::GEOFaceType GEOFaceType;
00278   typedef typename ImplTraits::NeighbourPairType NeighbourPairType;
00279   typedef typename ImplTraits::PLLBndFaceType PLLBndFaceType;
00280   typedef typename ImplTraits::BNDFaceType BNDFaceType;
00281 
00282   typedef ALU3dGridFaceInfo<GridImp::elementType> FaceInfoType;
00283   typedef typename std::auto_ptr<FaceInfoType> FaceInfoPointer;
00284 
00285   typedef typename SelectType<
00286     SameType<Int2Type<tetra>, Int2Type<GridImp::elementType> >::value,
00287     ALU3dGridGeometricFaceInfoTetra,
00288     ALU3dGridGeometricFaceInfoHexa
00289                    >::Type GeometryInfoType;
00290       
00291   typedef ElementTopologyMapping<GridImp::elementType> ElementTopo;
00292   typedef FaceTopologyMapping<GridImp::elementType> FaceTopo;
00293 
00294   enum { numFaces = EntityCount<GridImp::elementType>::numFaces };
00295   enum { numVerticesPerFace = 
00296          EntityCount<GridImp::elementType>::numVerticesPerFace };
00297   enum { numVertices = EntityCount<GridImp::elementType>::numVertices };
00298 
00299   typedef ALU3dGridLevelIntersectionIterator<GridImp> ThisType;
00300 
00301   friend class ALU3dGridEntity<0,dim,GridImp>;
00302   friend class IntersectionIteratorWrapper<GridImp,ThisType>;
00303 
00304 public:
00305   typedef ALUMemoryProvider< ThisType > StorageType;
00306 
00309   ALU3dGridLevelIntersectionIterator(const GridImp & grid, 
00310                                 ALU3DSPACE HElementType *el,
00311                                 int wLevel,bool end=false);
00312   
00313   ALU3dGridLevelIntersectionIterator(const GridImp & grid,int wLevel);
00314   
00316   ALU3dGridLevelIntersectionIterator(const ThisType & org);
00317 
00319   void assign(const ThisType & org);
00320 
00322   void increment ();
00323 
00324   // reset IntersectionIterator to first neighbour 
00325   template <class EntityType>
00326   void first(const EntityType & en, int wLevel);
00327 
00329   bool neighbor () const; 
00330 
00332   bool levelNeighbor () const; 
00333 
00335   bool leafNeighbor () const; 
00336 
00338   bool conforming () const 
00339   {
00340     assert( this->connector_.conformanceState() == FaceInfoType::CONFORMING );  
00341     return true; 
00342   }
00343 private:  
00344   // set new face
00345   void setNewFace(const GEOFaceType& newFace);
00346 
00347   // reset IntersectionIterator to first neighbour 
00348   void setFirstItem(const ALU3DSPACE HElementType & elem, int wLevel);
00349 
00350   bool levelNeighbor_; 
00351   bool isLeafItem_; 
00352 };
00353 
00355 //
00356 //  --IterationImpl
00357 //
00359 template <class InternalIteratorType > 
00360 struct ALU3dGridTreeIterator 
00361 {
00362   typedef typename InternalIteratorType :: val_t val_t;
00363 
00364   // here the items level will do 
00365   template <class GridImp, int codim> 
00366   struct GetLevel 
00367   {
00368     template <class ItemType> 
00369     static int getLevel(const GridImp & grid, const ItemType & item, int level )
00370     {
00371       assert( & item );
00372       return (level < 0) ? item.level() : level;
00373     }
00374   };
00375  
00376   // level is not needed for codim = 0 
00377   template <class GridImp> 
00378   struct GetLevel<GridImp,0>
00379   {
00380     template <class ItemType> 
00381     static int getLevel(const GridImp & grid, const ItemType & item, int level )
00382     {
00383       return level;
00384     }
00385   };
00386   
00387   template <class GridImp> 
00388   struct GetLevel<GridImp,3>
00389   {
00390     template <class ItemType> 
00391     static int getLevel(const GridImp & grid, const ItemType & item, int level)
00392     {
00393       return (level < 0) ? grid.getLevelOfLeafVertex(item) : level;
00394     }
00395   };
00396   
00397 protected: 
00398   // set iterator to first item 
00399   template <class GridImp, class IteratorImp> 
00400   void firstItem(const GridImp & grid, IteratorImp & it, int level ) 
00401   {
00402     InternalIteratorType & iter = it.internalIterator();
00403     iter.first(); 
00404     if( ! iter.done() )
00405     {
00406       assert( iter.size() > 0 );
00407       setItem(grid,it,iter,level);
00408     } 
00409     else 
00410     {
00411       it.removeIter();
00412     }
00413   } 
00414 
00415   // set the iterators entity to actual item 
00416   template <class GridImp, class IteratorImp>
00417   void setItem (const GridImp & grid, IteratorImp & it, InternalIteratorType & iter, int level)
00418   {
00419     enum { codim = IteratorImp :: codimension };
00420     val_t & item = iter.item();
00421     assert( item.first || item.second );
00422     if( item.first )
00423     {
00424       it.updateEntityPointer( item.first , 
00425           GetLevel<GridImp,codim>::getLevel(grid, *(item.first) , level) ); 
00426     }
00427     else
00428       it.updateGhostPointer( *item.second );
00429   }
00430 
00431   // increment iterator 
00432   template <class GridImp, class IteratorImp>
00433   void incrementIterator(const GridImp & grid, IteratorImp & it, int level) 
00434   {
00435     // if iter_ is zero, then end iterator 
00436     InternalIteratorType & iter = it.internalIterator();
00437 
00438     iter.next();
00439 
00440     if(iter.done())
00441     {
00442       it.removeIter();
00443       return ;
00444     }
00445 
00446     setItem(grid,it,iter,level);
00447     return ;
00448   }
00449 };
00450 
00451 //**********************************************************************
00452 //
00453 // --ALU3dGridLevelIterator
00454 // --LevelIterator
00458 template<int cd, PartitionIteratorType pitype, class GridImp>
00459 class ALU3dGridLevelIterator : 
00460 public ALU3dGridEntityPointer <cd,GridImp> ,
00461 public LevelIteratorDefaultImplementation <cd,pitype,GridImp,ALU3dGridLevelIterator> , 
00462 public ALU3dGridTreeIterator< ALU3DSPACE ALU3dGridLevelIteratorWrapper<cd,pitype> > 
00463 {
00464   enum { dim       = GridImp::dimension };
00465   enum { dimworld  = GridImp::dimensionworld };
00466 
00467   friend class ALU3dGridEntity<3,dim,GridImp>;
00468   friend class ALU3dGridEntity<2,dim,GridImp>;
00469   friend class ALU3dGridEntity<1,dim,GridImp>;
00470   friend class ALU3dGridEntity<0,dim,GridImp>;
00471   friend class ALU3dGrid < dim , dimworld, GridImp::elementType >;
00472 
00473   friend class ALU3dGridTreeIterator< ALU3DSPACE ALU3dGridLevelIteratorWrapper<cd,pitype> > ;
00474 public:
00475   typedef typename GridImp::template Codim<cd>::Entity Entity;
00476   typedef ALU3DSPACE VertexListType VertexListType; 
00477     
00479   typedef ALU3dGridLevelIterator<cd,pitype,GridImp> ThisType;
00480   // the wrapper for the original iterator of the ALU3dGrid  
00481   typedef typename ALU3DSPACE ALU3dGridLevelIteratorWrapper<cd,pitype> IteratorType; 
00482   typedef IteratorType InternalIteratorType; 
00483   typedef typename ALU3DSPACE IteratorElType<cd>::val_t val_t;
00484  
00486   ALU3dGridLevelIterator(const GridImp & grid, int level, bool);
00487   
00489   ALU3dGridLevelIterator(const GridImp & grid, int level);
00490   
00492   ALU3dGridLevelIterator(const ThisType & org);
00493   
00494   // destructor 
00495   ~ALU3dGridLevelIterator();
00496 
00498   void increment ();
00499 
00501   Entity & dereference () const;
00502 
00504   ThisType & operator = (const ThisType & org);
00505 private:
00507   void assign (const ThisType & org);
00508 
00509   // actual level
00510   int level_;
00511 
00512   // the internal iterator 
00513   IteratorType * iter_ ;
00514   
00515   // deletes iter_ 
00516   void removeIter ();
00517 
00518   IteratorType & internalIterator () 
00519   { 
00520     assert( iter_ ); 
00521     return *iter_; 
00522   }
00523 };
00524 
00525 //********************************************************************
00526 //
00527 //  --ALU3dGridLeafIterator 
00528 //  --LeafIterator 
00529 //
00530 //********************************************************************
00532 template<int cdim, PartitionIteratorType pitype, class GridImp>
00533 class ALU3dGridLeafIterator :
00534   public LeafIteratorDefaultImplementation<cdim, pitype, GridImp, ALU3dGridLeafIterator>,
00535   public ALU3dGridEntityPointer<cdim,GridImp> , 
00536   public ALU3dGridTreeIterator< ALU3DSPACE ALU3dGridLeafIteratorWrapper<cdim, pitype> > 
00537 {
00538   enum { dim = GridImp :: dimension };
00539   
00540   friend class ALU3dGridEntity<cdim,dim,GridImp>;
00541   enum { codim = cdim }; 
00542 
00543   friend class ALU3dGridTreeIterator< ALU3DSPACE ALU3dGridLeafIteratorWrapper<cdim, pitype> > ;
00544   
00545 public:
00546   typedef typename GridImp::template Codim<cdim>::Entity Entity;
00547 
00548   // the wrapper for the original iterator of the ALU3dGrid  
00549   typedef typename ALU3DSPACE ALU3dGridLeafIteratorWrapper<cdim, pitype> IteratorType; 
00550   typedef IteratorType InternalIteratorType;
00551   typedef typename ALU3DSPACE IteratorElType<cdim>::val_t val_t;
00552   
00553   typedef ALU3dGridLeafIterator<cdim, pitype, GridImp> ThisType;
00554 
00556   ALU3dGridLeafIterator(const GridImp & grid, int level);
00557 
00559   ALU3dGridLeafIterator(const GridImp & grid, int level , bool isBegin);
00560 
00562   ALU3dGridLeafIterator(const ThisType & org);
00563   
00565   ~ALU3dGridLeafIterator();
00566 
00568   void increment ();
00569 
00571   Entity & dereference () const;
00572 
00574   ThisType & operator = (const ThisType & org);
00575   
00576 private:
00577   // the internal iterator 
00578   IteratorType * iter_;
00579 
00581   void assign (const ThisType & org);
00582   
00583   // deletes iter_  
00584   void removeIter () ;
00585 
00586   // return reference to iter_ 
00587   InternalIteratorType & internalIterator () 
00588   { 
00589     assert( iter_ );
00590     return *iter_;
00591   }
00592 };
00593 
00594 // - HierarchicIteraor
00595 // --HierarchicIterator
00596 template<class GridImp>
00597 class ALU3dGridHierarchicIterator :
00598 public ALU3dGridEntityPointer<0,GridImp> ,
00599 public HierarchicIteratorDefaultImplementation <GridImp,ALU3dGridHierarchicIterator>
00600 {
00601   enum { dim = GridImp::dimension };
00602   typedef ALU3dGridHierarchicIterator<GridImp> ThisType;
00603 public:
00604   typedef typename GridImp::template Codim<0>::Entity Entity;
00605   typedef typename GridImp::ctype ctype;
00606 
00608   ALU3dGridHierarchicIterator(const GridImp &grid,
00609       const ALU3DSPACE HElementType & elem, int maxlevel, bool end=false);
00610   
00612   ALU3dGridHierarchicIterator(const ALU3dGridHierarchicIterator<GridImp> &org);
00613     
00615   void increment();
00616 
00618   Entity & dereference () const;
00619 
00621   ThisType & operator = (const ThisType & org);
00622   
00623 private:
00624   // go to next valid element 
00625   ALU3DSPACE HElementType * goNextElement (ALU3DSPACE HElementType * oldEl);
00626   
00628   const ALU3DSPACE HElementType * elem_;
00629 
00631   int maxlevel_; 
00632 };
00633 
00634 
00635 } // end namespace Dune
00636 
00637 #include "iterator_imp.cc"
00638 
00639 #endif

Generated on 12 Dec 2007 with Doxygen (ver 1.5.1)