sgrid.hh

Go to the documentation of this file.
00001 #ifndef DUNE_SGRID_HH
00002 #define DUNE_SGRID_HH
00003 
00004 #include <limits>
00005 #include <vector>
00006 
00007 #include <dune/common/fvector.hh>
00008 #include <dune/common/fmatrix.hh>
00009 #include <dune/common/stack.hh>
00010 #include <dune/grid/common/capabilities.hh>
00011 #include <dune/common/bigunsignedint.hh>
00012 #include <dune/common/collectivecommunication.hh>
00013 #include <dune/grid/common/grid.hh>
00014 #include <dune/grid/sgrid/numbering.hh>
00015 #include <dune/grid/common/indexidset.hh>
00016 
00022 namespace Dune {
00023 
00024 //************************************************************************
00028   typedef double sgrid_ctype; 
00029 
00030   // globally define the persistent index type
00031   const int sgrid_dim_bits = 24;   // bits for encoding each dimension
00032   const int sgrid_level_bits = 6;  // bits for encoding level number
00033   const int sgrid_codim_bits = 4;  // bits for encoding codimension
00034 
00035 //************************************************************************
00036 // forward declaration of templates
00037 
00038 template<int dim, int dimworld, class GridImp> class SGeometry;
00039 template<int codim, int dim, class GridImp> class SEntity;
00040 template<int codim, class GridImp> class SEntityPointer;
00041 template<int codim, PartitionIteratorType, class GridImp> class SLevelIterator;
00042 template<int dim, int dimworld> class SGrid;
00043 template<class GridImp> class SIntersectionIterator;
00044 template<class GridImp> class SHierarchicIterator;
00045 
00046 //************************************************************************
00077 template<int mydim, int cdim, class GridImp> 
00078 class SGeometry : public GeometryDefaultImplementation<mydim,cdim,GridImp,SGeometry>
00079 {
00080 public:
00082         typedef sgrid_ctype ctype;
00083 
00085         GeometryType type () const;             
00086 
00088         int corners () const;                  
00089 
00091         const FieldVector<sgrid_ctype, cdim>& operator[] (int i) const;
00092 
00094         FieldVector<sgrid_ctype, cdim> global (const FieldVector<sgrid_ctype, mydim>& local) const;
00095 
00097         FieldVector<sgrid_ctype, mydim> local (const FieldVector<sgrid_ctype, cdim>& global) const;
00098 
00100         bool checkInside (const FieldVector<sgrid_ctype, mydim>& local) const;
00101   
00121         sgrid_ctype integrationElement (const FieldVector<sgrid_ctype, mydim>& local) const;
00122 
00124         const FieldMatrix<sgrid_ctype,mydim,mydim>& jacobianInverseTransposed (const FieldVector<sgrid_ctype, mydim>& local) const;
00125 
00127         void print (std::ostream& ss, int indent) const;
00128 
00133         void make (FieldMatrix<sgrid_ctype,mydim+1,cdim>& __As);
00134 
00136         SGeometry () : builtinverse(false) {};
00137 
00138 private:
00139         FieldVector<sgrid_ctype, cdim> s;         
00140         FieldMatrix<sgrid_ctype,mydim,cdim> A;     
00141         array<FieldVector<sgrid_ctype, cdim>, 1<<mydim> c; 
00142         mutable FieldMatrix<sgrid_ctype,mydim,mydim> Jinv;       
00143         mutable bool builtinverse;
00144 };
00145 
00147 template<int cdim, class GridImp> 
00148 class SGeometry<0,cdim,GridImp> : public GeometryDefaultImplementation<0,cdim,GridImp,SGeometry>
00149 {
00150 public:
00152         typedef sgrid_ctype ctype;
00153 
00155         GeometryType type () const;             
00156 
00158         int corners () const;
00159 
00161         const FieldVector<sgrid_ctype, cdim>& operator[] (int i) const;
00162 
00164         void print (std::ostream& ss, int indent) const;
00165 
00167         void make (FieldMatrix<sgrid_ctype,1,cdim>& __As);
00168 
00170         FieldVector<sgrid_ctype, cdim> global (const FieldVector<sgrid_ctype, 0>& local) const { return this->operator[] (0); }
00171 
00173         FieldVector<sgrid_ctype, 0> local (const FieldVector<sgrid_ctype, cdim>& global) const { return FieldVector<sgrid_ctype,0> (0.0); } 
00174 
00196 
00197         SGeometry () {};
00198 
00204         sgrid_ctype integrationElement(const FieldVector<sgrid_ctype, 0>& local) const {
00205             return 1.;
00206         }
00207         
00209         bool checkInside (const FieldVector<sgrid_ctype, 0>& local) const 
00210         {
00211           return true; 
00212         }
00213   
00215         const FieldMatrix<sgrid_ctype,0,0>& jacobianInverseTransposed (const FieldVector<sgrid_ctype, 0>& local) const
00216         {
00217           static FieldMatrix<sgrid_ctype,0,0> dummy; 
00218           return dummy;
00219         }
00220 
00221 protected:
00222         FieldVector<sgrid_ctype, cdim> s;         
00223 };
00224 
00225 template <int mydim, int cdim, class GridImp>
00226 inline std::ostream& operator<< (std::ostream& s, SGeometry<mydim,cdim,GridImp>& e)
00227 {
00228         e.print(s,0);
00229         return s;
00230 }
00231 
00232 template<int mydim, int cdim, class GridImp>
00233 class SMakeableGeometry : public Geometry<mydim, cdim, GridImp, SGeometry>
00234 {
00235 public:
00236   SMakeableGeometry() :
00237     Geometry<mydim, cdim, GridImp, SGeometry>(SGeometry<mydim, cdim, GridImp>())
00238     {};
00239 
00240     void make (FieldMatrix<sgrid_ctype,mydim+1,cdim>& __As) { this->realGeometry.make(__As); }
00241 };
00242 
00243 //************************************************************************
00248 template<int codim, int dim, class GridImp>
00249 class SEntityBase {
00250   friend class SEntityPointer<codim,GridImp>;
00251   friend class SIntersectionIterator<GridImp>;
00252   enum { dimworld = GridImp::dimensionworld };
00253 public:
00254   typedef typename GridImp::template Codim<codim>::Geometry Geometry;
00255   typedef SMakeableGeometry<dim-codim, dimworld, const GridImp> MakeableGeometry;
00256   typedef typename GridImp::PersistentIndexType PersistentIndexType;
00257 
00259   int level () const
00260     {
00261       return l;
00262     }
00263   
00266   int index () const
00267     {
00268       return compressedIndex();
00269     }
00270   
00272   int globalIndex() const;
00273   
00275   const Geometry& geometry () const;
00276   
00278   SEntityBase (GridImp* _grid, int _l, int _id);
00279   SEntityBase ();
00280   void make (GridImp* _grid, int _l, int _id);
00281 
00283   void make (int _l, int _id);
00284 
00286   PersistentIndexType persistentIndex () const 
00287     { 
00288           if (codim!=dim)
00289                 {
00290                   // encode codim, this would actually not be necessary
00291           // because z is unique in codim
00292                   PersistentIndexType id(codim);
00293 
00294                   // encode level
00295                   id = id << sgrid_level_bits;
00296                   id = id+PersistentIndexType(l);
00297         
00298                   // encode coordinates
00299                   for (int i=dim-1; i>=0; i--)
00300                         {
00301                           id = id << sgrid_dim_bits;
00302                           id = id+PersistentIndexType(z[i]);
00303                         }
00304                   
00305                   return id;
00306                 }
00307           else
00308                 {
00309                   // determine min number of trailing zeroes
00310                   // consider that z is on the doubled grid !
00311                   int trailing = 1000;
00312                   for (int i=0; i<dim; i++)
00313                         {
00314                           // count trailing zeros
00315                           int zeros = 0;
00316                           for (int j=0; j<l; j++)
00317                                 if (z[i]&(1<<(j+1)))
00318                                   break;
00319                                 else
00320                                   zeros++;
00321                           trailing = std::min(trailing,zeros);
00322                         }
00323 
00324                   // determine the level of this vertex
00325                   int level = l-trailing;
00326 
00327                   // encode codim
00328                   PersistentIndexType id(dim);
00329 
00330                   // encode level
00331                   id = id << sgrid_level_bits;
00332                   id = id+PersistentIndexType(level);
00333         
00334                   // encode coordinates
00335                   for (int i=dim-1; i>=0; i--)
00336                         {
00337                           id = id << sgrid_dim_bits;
00338                           id = id+PersistentIndexType(z[i]>>trailing);
00339                         }
00340         
00341                   return id;
00342                 }
00343     }
00344 
00346   int compressedIndex () const
00347     {
00348                 return id;
00349     }
00350 
00352   int compressedLeafIndex () const
00353     {
00354           // codim != dim -> there are no copies of entities
00355           // maxlevel -> ids are fine
00356           if (codim<dim || l==grid->maxLevel())
00357                 return id;
00358           
00359           // this is a vertex which is not on the finest level
00360           // move coordinates up to maxlevel (multiply by 2 for each level
00361           array<int,dim> coord;
00362           for (int k=0; k<dim; k++)
00363                 coord[k] = z[k]*(1<<(grid->maxLevel()-l));
00364 
00365           // compute number with respect to maxLevel
00366           return grid->n(grid->maxLevel(),coord);
00367     }
00368 
00369 protected:
00370   // this is how we implement our elements
00371   GridImp* grid;         
00372   int l;                 
00373   int id;                
00374   array<int,dim> z; 
00375   mutable MakeableGeometry geo; 
00376   mutable bool builtgeometry;   
00377 };
00378 
00379 
00387 template<int codim, int dim, class GridImp> 
00388 class SEntity : public SEntityBase<codim,dim,GridImp>, 
00389                 public EntityDefaultImplementation<codim,dim,GridImp,SEntity>
00390 {
00391   enum { dimworld = GridImp::dimensionworld };
00392 public:
00393   typedef typename GridImp::template Codim<codim>::Geometry Geometry;
00394   typedef typename GridImp::template Codim<codim>::LevelIterator LevelIterator;
00395   typedef typename GridImp::template Codim<0>::LeafIntersectionIterator IntersectionIterator;
00396   typedef typename GridImp::template Codim<0>::HierarchicIterator HierarchicIterator;
00397   
00398   // disambiguate member functions with the same name in both bases
00400   int level () const {return SEntityBase<codim,dim,GridImp>::level();}
00401   
00403   int index () const {return SEntityBase<codim,dim,GridImp>::index();}
00404 
00406   const Geometry& geometry () const { return SEntityBase<codim,dim,GridImp>::geometry(); }
00407   
00409   PartitionType partitionType () const { return InteriorEntity; }
00410 
00411   // specific to SEntity
00413   SEntity (GridImp* _grid, int _l, int _id) : SEntityBase<codim,dim,GridImp>::SEntityBase(_grid,_l,_id) {};
00414 };
00415 
00442 template<int dim, class GridImp>
00443 class SEntity<0,dim,GridImp> : public SEntityBase<0,dim,GridImp>, 
00444                                public EntityDefaultImplementation<0,dim,GridImp,SEntity>
00445 {
00446   enum { dimworld = GridImp::dimensionworld };
00447 public:
00448   typedef typename GridImp::template Codim<0>::Geometry Geometry;
00449   typedef typename GridImp::template Codim<0>::LocalGeometry LocalGeometry;
00450   typedef SMakeableGeometry<dim, dimworld, const GridImp> MakeableGeometry;
00451   template <int cd>
00452   struct Codim
00453   {
00454     typedef typename GridImp::template Codim<cd>::EntityPointer EntityPointer;
00455   };
00456   typedef typename GridImp::template Codim<0>::EntityPointer EntityPointer;
00457   typedef typename GridImp::template Codim<0>::LeafIntersectionIterator IntersectionIterator;
00458   typedef typename GridImp::template Codim<0>::HierarchicIterator HierarchicIterator;
00459   typedef typename GridImp::PersistentIndexType PersistentIndexType;
00460 
00462   friend class SHierarchicIterator<GridImp>;
00463   
00464   // disambiguate member functions with the same name in both bases
00466   int level () const {return SEntityBase<0,dim,GridImp>::level();}
00467   
00470   int index () const {return SEntityBase<0,dim,GridImp>::index();}
00471   
00473   PartitionType partitionType () const { return InteriorEntity; }
00474 
00476   const Geometry& geometry () const {return SEntityBase<0,dim,GridImp>::geometry();}
00477 
00482   template<int cc> int count () const; 
00483 
00488   template<int cc> typename Codim<cc>::EntityPointer entity (int i) const;
00489 
00491   template<int cc>
00492   int subCompressedIndex (int i) const
00493   {
00494         if (cc==0) return this->compressedIndex();
00495         return this->grid->template getRealEntity<cc>(*entity<cc>(i)).compressedIndex();
00496   }
00497 
00499   template<int cc>
00500   int subCompressedLeafIndex (int i) const
00501   {
00502         if (cc==0) return this->compressedLeafIndex();
00503         return this->grid->template getRealEntity<cc>(*entity<cc>(i)).compressedLeafIndex();
00504   }
00505 
00507   template<int cc>
00508   PersistentIndexType subPersistentIndex (int i) const
00509   {
00510         if (cc==0) return this->persistentIndex();
00511         return this->grid->template getRealEntity<cc>(*entity<cc>(i)).persistentIndex();
00512   }
00513   
00521   IntersectionIterator ibegin () const;
00522   IntersectionIterator ileafbegin () const;
00523   IntersectionIterator ilevelbegin () const;
00525   IntersectionIterator iend () const;
00526   IntersectionIterator ileafend () const;
00527   IntersectionIterator ilevelend () const;
00528 
00534   EntityPointer father () const;
00535 
00537   bool isLeaf () const
00538     {
00539       return ( this->grid->maxLevel() == level() );
00540     }
00541 
00553   const LocalGeometry& geometryInFather () const;
00554 
00561   HierarchicIterator hbegin (int maxLevel) const;
00562 
00564   HierarchicIterator hend (int maxLevel) const;
00565 
00566   // members specific to SEntity
00568   SEntity (GridImp* _grid, int _l, int _id) : 
00569     SEntityBase<0,dim,GridImp>::SEntityBase(_grid,_l,_id)
00570     {
00571       built_father = false;
00572     }
00573 
00574   SEntity ()
00575     {
00576       built_father = false;
00577     }
00578 
00579   void make (GridImp* _grid, int _l, int _id)
00580     {
00581       SEntityBase<0,dim,GridImp>::make(_grid,_l,_id);
00582       built_father = false;
00583     }
00584 
00586   void make (int _l, int _id)
00587     {
00588       SEntityBase<0,dim,GridImp>::make(_l,_id);
00589       built_father = false;
00590     }
00591   
00592 private:
00593 
00594   mutable bool built_father;
00595   mutable int father_id;
00596     mutable SMakeableGeometry<dim,dim,const GridImp> in_father_local;
00597   void make_father() const;
00598 };
00599 
00609 template<int dim, class GridImp>
00610 class SEntity<dim,dim,GridImp> : public SEntityBase<dim,dim,GridImp>, 
00611                                  public EntityDefaultImplementation <dim,dim,GridImp,SEntity>
00612 {
00613   enum { dimworld = GridImp::dimensionworld };
00614 public:
00615   typedef typename GridImp::template Codim<dim>::Geometry Geometry;
00616   typedef typename GridImp::template Codim<0>::EntityPointer EntityPointer;
00617 
00618   // disambiguate member functions with the same name in both bases
00620   int level () const {return SEntityBase<dim,dim,GridImp>::level();}
00621 
00623   int index () const {return SEntityBase<dim,dim,GridImp>::index();}
00624   
00626   PartitionType partitionType () const { return InteriorEntity; }
00627 
00629   const Geometry& geometry () const {return SEntityBase<dim,dim,GridImp>::geometry();}
00630 
00631         // members specific to SEntity
00633         SEntity (GridImp* _grid, int _l, int _id) : SEntityBase<dim,dim,GridImp>::SEntityBase(_grid,_l,_id)
00634         {
00635                 built_father = false;
00636         }
00637 
00639         void make (int _l, int _id)
00640         {
00641                 SEntityBase<dim,dim,GridImp>::make(_l,_id);
00642                 built_father = false;
00643         }
00644 
00645 private:
00646   mutable bool built_father;
00647   mutable int father_id;
00648   mutable FieldVector<sgrid_ctype, dim> in_father_local;
00649   void make_father() const;
00650 };
00651 
00652 template<int codim, int dim, class GridImp>
00653 class SMakeableEntity :
00654   public GridImp::template Codim<codim>::Entity
00655 {
00656 public:
00657 
00658   SMakeableEntity(GridImp* _grid, int _l, int _id) :
00659     GridImp::template Codim<codim>::Entity (SEntity<codim, dim, GridImp>(_grid,_l,_id))
00660     {};
00661   SMakeableEntity(const SEntity<codim, dim, GridImp>& e) :
00662     GridImp::template Codim<codim>::Entity (e)
00663     {};
00664   void make (int _l, int _id) { this->realEntity.make(_l, _id); }
00665 };
00666 
00667 //************************************************************************
00675 struct SHierarchicStackElem {
00676   int l;
00677   int id;
00678   SHierarchicStackElem () : l(-1), id(-1) {}
00679   SHierarchicStackElem (int _l, int _id) {l=_l; id=_id;}
00680   bool operator== (const SHierarchicStackElem& s) const {return !operator!=(s);}
00681   bool operator!= (const SHierarchicStackElem& s) const {return l!=s.l || id!=s.id;}
00682 };
00683 
00684 template<class GridImp>
00685 class SHierarchicIterator :
00686   public Dune::SEntityPointer <0,GridImp>,
00687   public HierarchicIteratorDefaultImplementation <GridImp,SHierarchicIterator>
00688 {
00689   friend class SHierarchicIterator<const GridImp>;
00690   enum { dim = GridImp::dimension };
00691   enum { dimworld = GridImp::dimensionworld };
00692 public:
00693   typedef typename GridImp::template Codim<0>::Entity Entity;
00694   typedef typename GridImp::ctype ctype;
00695 
00697   void increment();
00698 
00705   SHierarchicIterator (GridImp* _grid,
00706                        const Dune::SEntity<0,GridImp::dimension,GridImp>& _e,
00707                        int _maxLevel, bool makeend) :
00708     Dune::SEntityPointer<0,GridImp>(_grid,_e.level(),_e.index())
00709     {
00710       // without sons, we are done
00711       // (the end iterator is equal to the calling iterator)
00712       if (makeend) return;
00713       
00714       // remember element where begin has been called
00715       orig_l = this->e.level();
00716       orig_id = _grid->template getRealEntity<0>(this->e).index();
00717       
00718       // push original element on stack
00719       SHierarchicStackElem originalElement(orig_l, orig_id);
00720       stack.push(originalElement);
00721       
00722       // compute maxLevel
00723       maxLevel = std::min(_maxLevel,this->grid->maxLevel());
00724       
00725       // ok, push all the sons as well
00726       push_sons(orig_l,orig_id);
00727       
00728       // and pop the first son
00729       increment();
00730     }
00731 
00732 private:
00733   int maxLevel;                
00734   int orig_l, orig_id;         
00735 
00736   FiniteStack<SHierarchicStackElem,GridImp::MAXL> stack;      
00737   
00738   void push_sons (int level, int fatherid); 
00739 };
00740 
00741 //************************************************************************
00748 template<class GridImp>
00749 class SIntersectionIterator :
00750   public IntersectionIteratorDefaultImplementation <GridImp,SIntersectionIterator>
00751 {
00752   enum { dim=GridImp::dimension };
00753   enum { dimworld=GridImp::dimensionworld };
00754 public:
00755   typedef typename GridImp::template Codim<0>::Entity Entity;
00756   typedef typename GridImp::template Codim<0>::EntityPointer EntityPointer;
00757   typedef typename GridImp::template Codim<1>::Geometry Geometry;
00758   typedef typename GridImp::template Codim<1>::LocalGeometry LocalGeometry;
00760   enum { dimension=dim };
00762   enum { dimensionworld=dimworld };
00764   typedef typename GridImp::ctype ctype;
00765 
00767   bool equals(const SIntersectionIterator<GridImp>& i) const;
00769   void increment();
00770 
00773   EntityPointer inside() const;
00774 
00777   EntityPointer outside() const;
00778   
00780   bool boundary () const;
00781 
00782   int boundaryId () const {
00783     if (boundary()) return count + 1;
00784     return 0;
00785   };
00787   bool neighbor () const;
00788 
00790   FieldVector<ctype, GridImp::dimensionworld> outerNormal (const FieldVector<ctype, GridImp::dimension-1>& local) const
00791     {
00792       return unitOuterNormal(local);
00793     }
00795   FieldVector<ctype, GridImp::dimensionworld> unitOuterNormal (const FieldVector<ctype, GridImp::dimension-1>& local) const
00796     {
00797       // while we are at it, compute normal direction
00798       FieldVector<sgrid_ctype, dimworld> normal(0.0);
00799       if (count%2)
00800         normal[count/2] =  1.0; // odd
00801       else
00802         normal[count/2] = -1.0; // even
00803       
00804       return normal; 
00805     }
00806 
00810   LocalGeometry& intersectionSelfLocal () const;
00814   LocalGeometry& intersectionNeighborLocal () const;
00818   Geometry& intersectionGlobal () const;
00820   int numberInSelf () const;
00822   int numberInNeighbor () const;
00823 
00825   SIntersectionIterator (GridImp* _grid, const SEntity<0,dim,GridImp >* _self, int _count);
00826 
00828   SIntersectionIterator& operator = (const SIntersectionIterator& it)
00829     {
00830       assert(grid == it.grid);
00831       
00832       /* Assert same Iterator Context */
00833       if (! self.equals(it.self))
00834         DUNE_THROW(GridError, "assignment of SIntersectionIterator "
00835                    << "with different inside Entity");
00836       if (partition != it.partition)
00837         DUNE_THROW(GridError, "assignment of SIntersectionIterator "
00838                    << "with different partition");
00839       // (zred == it.zred) if (self == it.self)
00840 
00841       /* Assign current position and update ne */
00842       ne = it.ne;
00843       count = it.count;
00844       make(count);
00845 
00846       return *this;
00847     }
00848   
00849 private:
00850   void make (int _count) const;                 
00851   void makeintersections () const;              
00852   const EntityPointer self;                     
00853   mutable EntityPointer ne;                     
00854   const GridImp * grid;                         
00855   const int partition;                          
00856   const array<int,dim> zred;                 
00857   mutable int count;                              
00858   mutable bool valid_count;                       
00859   mutable bool valid_nb;                          
00860   mutable bool is_on_boundary;                    
00861   mutable bool built_intersections;               
00862   mutable SMakeableGeometry<dim-1,dim,const GridImp> is_self_local;      
00863   mutable SMakeableGeometry<dim-1,dimworld,const GridImp> is_global;     
00864   mutable SMakeableGeometry<dim-1,dim,const GridImp> is_nb_local;        
00865 };
00866 
00867 //************************************************************************
00868 
00871 template<int codim, class GridImp>
00872 class SEntityPointer :
00873   public EntityPointerDefaultImplementation <codim, GridImp,
00874                                Dune::SEntityPointer<codim,GridImp> >
00875 {
00876   enum { dim = GridImp::dimension };
00877   friend class SIntersectionIterator<GridImp>;
00878 public:
00879   typedef SEntityPointer<codim,GridImp> Base;
00880   typedef typename GridImp::template Codim<codim>::Entity Entity;
00881 
00883   bool equals(const SEntityPointer<codim,GridImp>& i) const;
00885   Entity& dereference() const;
00887   int level () const;
00888 
00890   SEntityPointer (GridImp * _grid, int _l, int _id) :
00891     grid(_grid), l(_l), id(_id), e(_grid,_l,_id) { }
00892   
00894   SEntityPointer (const SEntity<codim,dim,GridImp> & _e) :
00895     grid(_e.grid), l(_e.l), id(_e.id), e(_e) { }
00896   
00897 protected:
00898   GridImp* grid;                 
00899   int l;                         
00900   mutable int id;                
00901   mutable SMakeableEntity<codim,dim,GridImp> e;  
00902 };
00903 
00904 //************************************************************************
00905 
00908 template<int codim, PartitionIteratorType pitype, class GridImp>
00909 class SLevelIterator :
00910   public Dune::SEntityPointer <codim,GridImp>,
00911   public LevelIteratorDefaultImplementation <codim,pitype,GridImp,SLevelIterator>
00912 {
00913   friend class SLevelIterator<codim, pitype,const GridImp>;
00914   enum { dim = GridImp::dimension };
00915 public:
00916   typedef typename GridImp::template Codim<codim>::Entity Entity;
00917 
00919   void increment();
00920 
00922   SLevelIterator (GridImp * _grid, int _l, int _id) :
00923     Dune::SEntityPointer<codim,GridImp>(_grid,_l,_id) {}
00924 };
00925 
00926 
00927 //========================================================================
00932 //========================================================================
00933 
00934 template <class GridImp> 
00935 struct SGridLevelIndexSetTypes
00936 {
00938   template<int cd>
00939   struct Codim
00940   {
00941         template<PartitionIteratorType pitype>
00942         struct Partition
00943         {
00944           typedef typename GridImp::Traits::template Codim<cd>::template Partition<pitype>::LevelIterator Iterator;
00945         };
00946   };
00947 };
00948 
00949 template<class GridImp>
00950 class SGridLevelIndexSet : public IndexSetDefaultImplementation<GridImp,SGridLevelIndexSet<GridImp>,SGridLevelIndexSetTypes<GridImp> >
00951 {
00952   typedef IndexSetDefaultImplementation<GridImp,SGridLevelIndexSet<GridImp>,SGridLevelIndexSetTypes<GridImp> > Base;
00953 public:
00954 
00956   SGridLevelIndexSet (const GridImp& g, int l) : grid(g), level(l)
00957   {
00958     // contains a single element type;
00959         for (int codim=0; codim<=GridImp::dimension; codim++)
00960           mytypes[codim].push_back(GeometryType(GeometryType::cube,GridImp::dimension-codim));
00961   }
00962 
00964   template<int cd>
00965   int index (const typename GridImp::Traits::template Codim<cd>::Entity& e) const 
00966   {
00967         return grid.template getRealEntity<cd>(e).compressedIndex(); 
00968   }
00969 
00971   template<int cc>
00972   int subIndex (const typename GridImp::Traits::template Codim<0>::Entity& e, int i) const
00973   {
00974         return grid.template getRealEntity<0>(e).template subCompressedIndex<cc>(i);
00975   }
00976 
00978   int size (GeometryType type) const
00979   {
00980       return grid.size(level,GridImp::dimension-type.dim());
00981   }
00982   
00984   int size (int codim) const
00985   {
00986     return Base::size(codim);
00987   }
00988 
00990   const std::vector<GeometryType>& geomTypes (int codim) const
00991   {
00992         return mytypes[codim];
00993   }
00994 
00996   template<int cd, PartitionIteratorType pitype>
00997   typename Base::template Codim<cd>::template Partition<pitype>::Iterator begin () const
00998   {
00999         return grid.template lbegin<cd,pitype>(level);
01000   }
01001   
01003   template<int cd, PartitionIteratorType pitype>
01004   typename Base::template Codim<cd>::template Partition<pitype>::Iterator end () const
01005   {
01006         return grid.template lend<cd,pitype>(level);
01007   }
01008 
01009 private:
01010   const GridImp& grid;
01011   int level;
01012   std::vector<GeometryType> mytypes[GridImp::dimension+1];
01013 };
01014 
01015 // Leaf Index Set
01016 
01017 template <class GridImp> 
01018 struct SGridLeafIndexSetTypes
01019 {
01021   template<int cd>
01022   struct Codim
01023   {
01024         template<PartitionIteratorType pitype>
01025         struct Partition
01026         {
01027           typedef typename GridImp::Traits::template Codim<cd>::template Partition<pitype>::LeafIterator Iterator;
01028         };
01029   };
01030 };
01031 
01032 template<class GridImp>
01033 class SGridLeafIndexSet : public IndexSetDefaultImplementation<GridImp,SGridLeafIndexSet<GridImp>,SGridLeafIndexSetTypes<GridImp> >
01034 {
01035   typedef IndexSetDefaultImplementation<GridImp,SGridLeafIndexSet<GridImp>,SGridLeafIndexSetTypes<GridImp> > Base;
01036     enum {dim = remove_const<GridImp>::type::dimension};
01037 public:
01038 
01040   SGridLeafIndexSet (const GridImp& g) : grid(g)
01041   {
01042     // contains a single element type;
01043         for (int codim=0; codim<=dim; codim++)
01044           mytypes[codim].push_back(GeometryType(GeometryType::cube,dim-codim));
01045   }
01046 
01048   /*
01049     We use the remove_const to extract the Type from the mutable class,
01050     because the const class is not instantiated yet.
01051   */
01052   template<int cd>
01053   int index (const typename remove_const<GridImp>::type::Traits::template Codim<cd>::Entity& e) const 
01054   {
01055         return grid.template getRealEntity<cd>(e).compressedLeafIndex(); 
01056   }
01057 
01059   /*
01060     We use the remove_const to extract the Type from the mutable class,
01061     because the const class is not instantiated yet.
01062   */
01063   template<int cc>
01064   int subIndex (const typename remove_const<GridImp>::type::Traits::template Codim<0>::Entity& e, int i) const
01065   {
01066         return grid.template getRealEntity<0>(e).template subCompressedLeafIndex<cc>(i);
01067   }
01068 
01070   int size (GeometryType type) const
01071   {
01072       return grid.size(grid.maxLevel(),GridImp::dimension-type.dim());
01073   }
01074 
01076   int size (int codim) const
01077   {
01078     return Base::size(codim);
01079   }
01080 
01082   const std::vector<GeometryType>& geomTypes (int codim) const
01083   {
01084         return mytypes[codim];
01085   }
01086 
01088   template<int cd, PartitionIteratorType pitype>
01089   typename Base::template Codim<cd>::template Partition<pitype>::Iterator begin () const
01090   {
01091         return grid.template leafbegin<cd,pitype>();
01092   }
01093   
01095   template<int cd, PartitionIteratorType pitype>
01096   typename Base::template Codim<cd>::template Partition<pitype>::Iterator end () const
01097   {
01098         return grid.template leafend<cd,pitype>();
01099   }
01100 
01101 private:
01102   const GridImp& grid;
01103   std::vector<GeometryType> mytypes[dim+1];
01104 };
01105 
01106 
01107 //========================================================================
01112 //========================================================================
01113 
01114 template<class GridImp>
01115 class SGridGlobalIdSet :
01116   public IdSetDefaultImplementation<GridImp,SGridGlobalIdSet<GridImp>, typename remove_const<GridImp>::type::PersistentIndexType>
01117   /*
01118     We used the remove_const to extract the Type from the mutable class,
01119     because the const class is not instantiated yet.
01120   */
01121 {
01122 public:
01124   /*
01125     We use the remove_const to extract the Type from the mutable class,
01126     because the const class is not instantiated yet.
01127   */
01128   typedef typename remove_const<GridImp>::type::PersistentIndexType IdType;
01129 
01131   SGridGlobalIdSet (const GridImp& g) : grid(g) {}
01132 
01134   /*
01135     We use the remove_const to extract the Type from the mutable class,
01136     because the const class is not instantiated yet.
01137   */
01138   template<int cd>
01139   IdType id (const typename remove_const<GridImp>::type::Traits::template Codim<cd>::Entity& e) const 
01140   {
01141           return grid.template getRealEntity<cd>(e).persistentIndex();
01142   }
01143 
01145   /*
01146     We use the remove_const to extract the Type from the mutable class,
01147     because the const class is not instantiated yet.
01148   */
01149   template<int cc>
01150   IdType subId (const typename remove_const<GridImp>::type::Traits::template Codim<0>::Entity& e, int i) const
01151   {
01152         return grid.template getRealEntity<0>(e).template subPersistentIndex<cc>(i);
01153   }
01154 
01155 private:
01156   const GridImp& grid;
01157 };
01158 
01159 
01160 template<int dim, int dimworld>
01161 struct SGridFamily
01162 {
01163   typedef GridTraits<dim,dimworld,Dune::SGrid<dim,dimworld>,
01164                      SGeometry,SEntity,
01165                      SEntityPointer,SLevelIterator,
01166                      SIntersectionIterator, // leaf  intersection iter 
01167                      SIntersectionIterator, // level intersection iter
01168                      SHierarchicIterator,
01169                      SLevelIterator,
01170                                          SGridLevelIndexSet<const SGrid<dim,dimworld> >,
01171                                          SGridLevelIndexSetTypes<const SGrid<dim,dimworld> >,
01172                                          SGridLeafIndexSet<const SGrid<dim,dimworld> >,
01173                                          SGridLeafIndexSetTypes<const SGrid<dim,dimworld> >,
01174                                          SGridGlobalIdSet<const SGrid<dim,dimworld> >,
01175                                          bigunsignedint<dim*sgrid_dim_bits+sgrid_level_bits+sgrid_codim_bits>,
01176                                          SGridGlobalIdSet<const SGrid<dim,dimworld> >,
01177                                          bigunsignedint<dim*sgrid_dim_bits+sgrid_level_bits+sgrid_codim_bits>, 
01178                                          CollectiveCommunication<Dune::SGrid<dim,dimworld> > > 
01179   Traits;
01180 };
01181 
01182 
01183 //************************************************************************
01237 template<int dim, int dimworld>
01238 class SGrid : public GridDefaultImplementation <dim,dimworld,sgrid_ctype,SGridFamily<dim,dimworld> >
01239 {
01240 public:
01241   typedef SGridFamily<dim,dimworld> GridFamily;
01242   typedef bigunsignedint<dim*sgrid_dim_bits+sgrid_level_bits+sgrid_codim_bits> PersistentIndexType;
01243 
01244   // need for friend declarations in entity
01245   typedef SGridLevelIndexSet<SGrid<dim,dimworld> > LevelIndexSetType;
01246   typedef SGridLeafIndexSet<SGrid<dim,dimworld> > LeafIndexSetType;
01247   typedef SGridGlobalIdSet<SGrid<dim,dimworld> > GlobalIdSetType;
01248 
01249   typedef typename SGridFamily<dim,dimworld>::Traits Traits;
01250 
01252   enum { MAXL=32 };
01253 
01255   typedef sgrid_ctype ctype;
01256 
01258   std::string name() const { return "SGrid"; };
01259 
01260   // constructors
01261 
01269   SGrid (const int* N_, const sgrid_ctype* H_);
01270   
01278   SGrid (const int* N_, const sgrid_ctype* L_, const sgrid_ctype* H_);
01279 
01289   SGrid (FieldVector<int,dim> N_, FieldVector<sgrid_ctype,dim> L_, FieldVector<sgrid_ctype,dim> H_);
01290 
01292   SGrid ();
01293 
01295   ~SGrid ();
01296 
01299   int maxLevel() const;
01300 
01302   template<int cd, PartitionIteratorType pitype>
01303   typename Traits::template Codim<cd>::template Partition<pitype>::LevelIterator lbegin (int level) const;
01304 
01306   template<int cd, PartitionIteratorType pitype>
01307   typename Traits::template Codim<cd>::template Partition<pitype>::LevelIterator lend (int level) const;
01308 
01310   template<int cd>
01311   typename Traits::template Codim<cd>::template Partition<All_Partition>::LevelIterator lbegin (int level) const
01312     {
01313       return lbegin<cd,All_Partition>(level);
01314     }
01315 
01317   template<int cd>
01318   typename Traits::template Codim<cd>::template Partition<All_Partition>::LevelIterator lend (int level) const
01319     {
01320       return lend<cd,All_Partition>(level);
01321     }
01322 
01324   template<int cd, PartitionIteratorType pitype>
01325   typename Traits::template Codim<cd>::template Partition<pitype>::LeafIterator leafbegin () const;
01326 
01328   template<int cd, PartitionIteratorType pitype>
01329   typename Traits::template Codim<cd>::template Partition<pitype>::LeafIterator leafend () const;
01330 
01332   template<int cd>
01333   typename Traits::template Codim<cd>::template Partition<All_Partition>::LeafIterator leafbegin () const
01334     {
01335       return leafbegin<cd,All_Partition>();
01336     }
01337 
01339   template<int cd>
01340   typename Traits::template Codim<cd>::template Partition<All_Partition>::LeafIterator leafend () const
01341     {
01342       return leafend<cd,All_Partition>();
01343     }
01344 
01356   template<class T, template<class> class P, int codim>
01357   void communicate (T& t, InterfaceType iftype, CommunicationDirection dir, int level)
01358   {
01359           // SGrid is sequential and has no periodic boundaries, so do nothing ...
01360           return;
01361   }
01362 
01364   int size (int level, int codim) const;
01365 
01367   int size (int codim) const
01368   {
01369         return size(maxLevel(),codim);
01370   }
01371 
01373   int size (int level, GeometryType type) const
01374   {
01375       return (type.isCube()) ? size(level,dim-type.dim()) : 0;
01376   }
01377 
01379   int size (GeometryType type) const
01380   {
01381         return size(maxLevel(),type);
01382   }
01383 
01384 
01386   int global_size (int codim) const;
01387 
01389   int overlapSize (int level, int codim)
01390   {
01391         return 0;
01392   }
01393 
01395   int ghostSize (int level, int codim)
01396   {
01397         return 0;
01398   }
01399 
01400   // these are all members specific to sgrid
01401 
01403   void globalRefine (int refCount);
01404 
01406     const array<int, dim>& dims(int level) const {
01407         return N[level];
01408     }
01409 
01411     const FieldVector<sgrid_ctype, dimworld>& lowerLeft() const {
01412         return low;
01413     }
01414 
01416     FieldVector<sgrid_ctype, dimworld> upperRight() const {
01417         return H;
01418     }
01419 
01421   bool adapt ()    
01422   {
01423         globalRefine(1);
01424         return true; 
01425   }
01426   
01427 
01429   FieldVector<sgrid_ctype, dimworld> pos (int level, array<int,dim>& z) const;
01430  
01432   int calc_codim (int level, const array<int,dim>& z) const;
01433 
01435   int n (int level, const array<int,dim>& z) const;
01436 
01438   array<int,dim> z (int level, int i, int codim) const;
01439         
01441   array<int,dim> compress (int level, const array<int,dim>& z) const; 
01442 
01444   array<int,dim> expand (int level, const array<int,dim>& r, int b) const; 
01445 
01449   int partition (int level, const array<int,dim>& z) const; 
01450 
01452   bool exists (int level, const array<int,dim>& zred) const;
01453 
01454 
01455   // The new index sets from DDM 11.07.2005
01456   const typename Traits::GlobalIdSet& globalIdSet() const
01457   {
01458         return *theglobalidset;
01459   }
01460   
01461   const typename Traits::LocalIdSet& localIdSet() const
01462   {
01463         return *theglobalidset;
01464   }
01465 
01466   const typename Traits::LevelIndexSet& levelIndexSet(int level) const
01467   {
01468         assert(level>=0 && level<=maxLevel());
01469         return *(indexsets[level]);
01470   }
01471 
01472   const typename Traits::LeafIndexSet& leafIndexSet() const
01473   {
01474         return *theleafindexset;
01475   }
01476 
01477   // dummy parallel functions
01478 
01479   template<class DataHandle>
01480   void communicate (DataHandle& data, InterfaceType iftype, CommunicationDirection dir, int level) const
01481   {
01482   }
01483 
01484   template<class DataHandle>
01485   void communicate (DataHandle& data, InterfaceType iftype, CommunicationDirection dir) const
01486   {
01487   }
01488 
01489   const CollectiveCommunication<SGrid>& comm () const
01490   {
01491         return ccobj;
01492   }
01493 
01495   int overlapSize (int level, int codim) const
01496   {
01497         return 0;
01498   }
01499 
01501   int overlapSize (int codim) const
01502   {
01503         return 0;
01504   }
01505 
01507   int ghostSize (int level, int codim) const
01508   {
01509         return 0;
01510   }
01511 
01513   int ghostSize (int codim) const
01514   {
01515         return 0;
01516   }
01517 
01518   SIntersectionIterator<const SGrid<dim, dimworld> >&
01519   getRealIntersectionIterator(typename Traits::LeafIntersectionIterator& it) 
01520   {
01521     return this->getRealImplementation(it);
01522   }
01523 
01524   const SIntersectionIterator<const SGrid<dim, dimworld> >&
01525   getRealIntersectionIterator(const typename Traits::LeafIntersectionIterator& it) const
01526   {
01527     return this->getRealImplementation(it);
01528   }
01529 
01530 
01531 private:
01532   /*
01533     Make associated classes friends to grant access to the real entity
01534   */
01535   friend class Dune::SGridLevelIndexSet<Dune::SGrid<dim,dimworld> >;
01536   friend class Dune::SGridLeafIndexSet<Dune::SGrid<dim,dimworld> >;
01537   friend class Dune::SGridGlobalIdSet<Dune::SGrid<dim,dimworld> >;
01538   friend class Dune::SIntersectionIterator<Dune::SGrid<dim,dimworld> >;
01539   friend class Dune::SHierarchicIterator<Dune::SGrid<dim,dimworld> >;
01540   friend class Dune::SEntity<0,dim,Dune::SGrid<dim,dimworld> >;
01541   
01542   friend class Dune::SGridLevelIndexSet<const Dune::SGrid<dim,dimworld> >;
01543   friend class Dune::SGridLeafIndexSet<const Dune::SGrid<dim,dimworld> >;
01544   friend class Dune::SGridGlobalIdSet<const Dune::SGrid<dim,dimworld> >;
01545   friend class Dune::SIntersectionIterator<const Dune::SGrid<dim,dimworld> >;
01546   friend class Dune::SHierarchicIterator<const Dune::SGrid<dim,dimworld> >;
01547   friend class Dune::SEntity<0,dim,const Dune::SGrid<dim,dimworld> >;
01548   
01549   template<int codim_, int dim_, class GridImp_, template<int,int,class> class EntityImp_>
01550   friend class Entity;
01551 
01552   /*
01553     private methods
01554    */
01555   template<int codim>
01556   SEntity<codim,dim,const SGrid<dim,dimworld> >& getRealEntity(typename Traits::template Codim<codim>::Entity& e )
01557   {
01558     return this->getRealImplementaion(e);
01559   }
01560   
01561   template<int codim>
01562   const SEntity<codim,dim,const SGrid<dim,dimworld> >& getRealEntity(const typename Traits::template Codim<codim>::Entity& e ) const
01563   {
01564     return this->getRealImplementation(e);
01565   }
01566   
01567   // diasable copy and assign
01568   SGrid(const SGrid &) {};
01569   SGrid & operator = (const SGrid &) { return *this; };
01570   // generate SGrid 
01571   void makeSGrid (const int* N_,  const sgrid_ctype* L_, const sgrid_ctype* H_);
01572 
01573   /*
01574     internal data
01575    */
01576   CollectiveCommunication<SGrid> ccobj;
01577 
01578   std::vector<SGridLevelIndexSet<const SGrid<dim,dimworld> >*> indexsets;
01579   SGridLeafIndexSet<const SGrid<dim,dimworld> > *theleafindexset;
01580   SGridGlobalIdSet<const SGrid<dim,dimworld> > *theglobalidset;
01581 
01582   int L;                          // number of levels in hierarchic mesh 0<=level<L
01583   FieldVector<sgrid_ctype, dim> low;     // lower left corner of the grid
01584   FieldVector<sgrid_ctype, dim> H;       // length of cube per direction
01585   array<int,dim> *N;                // number of elements per direction
01586   FieldVector<sgrid_ctype, dim> *h;      // mesh size per direction
01587   mutable CubeMapper<dim> *mapper;       // a mapper for each level
01588   
01589   // faster implemantation od subIndex 
01590   mutable array <int,dim> zrefStatic;     // for subIndex of SEntity
01591   mutable array <int,dim> zentityStatic;  // for subIndex of SEntity
01592 };
01593 
01594 namespace Capabilities
01595 {
01596 
01608   template<int dim, int dimw, int cdim>
01609   struct hasEntity< SGrid<dim,dimw>, cdim>
01610   {
01611     static const bool v = true;
01612   };
01613   
01617   template<int dim, int dimw>
01618   struct isLevelwiseConforming< SGrid<dim,dimw> >
01619   {
01620     static const bool v = true;
01621   };
01622 
01626   template<int dim, int dimw>
01627   struct isLeafwiseConforming< SGrid<dim,dimw> >
01628   {
01629     static const bool v = true;
01630   };
01631 
01635   template<int dim, int dimw>
01636   struct IsUnstructured< SGrid<dim,dimw> >
01637   {
01638     static const bool v = false;
01639   };
01640 
01644   template<int dim, int dimw>
01645   struct hasHangingNodes< SGrid<dim,dimw> >
01646   {
01647     static const bool v = false;
01648   };
01649   
01650 } // end namespace Capabilities
01651 
01652 } // end namespace Dune
01653 
01654 #include"sgrid/sgrid.cc"
01655 
01656 #endif

Generated on 6 Nov 2008 with Doxygen (ver 1.5.6) [logfile].