dune-grid  2.2beta1
alugrid/3d/entityseed.hh
Go to the documentation of this file.
00001 #ifndef ALU3DGRID_ENTITYKEY_HH
00002 #define ALU3DGRID_ENTITYKEY_HH
00003 
00004 namespace Dune 
00005 {
00006 
00007 template<int cd, class GridImp>
00008 class ALU3dGridEntitySeed ;
00009 
00010 //**********************************************************************
00011 //
00012 // --ALU3dGridEntitySeed
00013 // --EntitySeed
00014 //**********************************************************************
00015 template< int codim, class GridImp >
00016 class ALU3dGridEntitySeedBase
00017 {
00018 protected:  
00019   typedef ALU3dGridEntitySeedBase< codim, GridImp > ThisType;
00020   enum { dim       = GridImp::dimension };
00021   enum { dimworld  = GridImp::dimensionworld };
00022 
00023    
00024   typedef typename GridImp::MPICommunicatorType Comm;
00025 
00026   friend class ALU3dGridEntity<codim,dim,GridImp>;
00027   friend class ALU3dGridEntity< 0,dim,GridImp>;
00028   friend class ALU3dGrid < GridImp::elementType, Comm >;
00029 
00030   typedef ALU3dImplTraits<GridImp::elementType, Comm > ImplTraits;
00031   typedef typename ImplTraits::template Codim<codim>::ImplementationType ImplementationType;
00032   typedef typename ImplTraits::template Codim<codim>::InterfaceType      HElementType;
00033   typedef typename ImplTraits::template Codim<codim>::EntitySeedType      KeyType ;
00034 
00035   typedef typename ImplTraits::BNDFaceType BNDFaceType;
00036   typedef typename ImplTraits::HBndSegType HBndSegType;
00037 
00038   template <int cd, class Key> 
00039   struct Bnd
00040   {
00041     static Key* toKey(const HBndSegType*) 
00042     {
00043       return (Key*) 0;
00044     }
00045     static HElementType* getItem(KeyType* key) 
00046     {
00047       return static_cast< HElementType* > ( key );
00048     }
00049     static bool isGhost(KeyType*) { return false; }
00050     static BNDFaceType* ghost( KeyType*  ) { return ( BNDFaceType* ) 0; }
00051   };
00052   template <class Key> 
00053   struct Bnd<0, Key>
00054   {
00055     static Key* toKey(const HBndSegType* ghostFace) 
00056     {
00057       return static_cast< KeyType* > (const_cast< BNDFaceType* >( static_cast<const BNDFaceType*> (ghostFace)));
00058     }
00059     static HElementType* getItem(KeyType* key) 
00060     {
00061       if( key ) 
00062       {
00063         if( key->isboundary() )
00064         {
00065           return ((static_cast< BNDFaceType* > ( key ))->getGhost().first);
00066         }
00067         else 
00068         {
00069           // we cannot cast to HElement here, since only the implementation is derived
00070           // from hasFace 
00071           return static_cast< HElementType * > (static_cast< ImplementationType* > (key));
00072         }
00073       }
00074       else 
00075         return static_cast< HElementType * > (0) ;
00076     }
00077     static bool isGhost(KeyType* key) { assert( key ); return key->isboundary(); }
00078     static BNDFaceType* ghost( KeyType* key ) { assert( key ); return (static_cast< BNDFaceType* > ( key )); }
00079   };
00080 public:
00081   static const int defaultValue = -1 ;
00082   static const int defaultTwist = 0 ;
00083 
00084   enum { codimension = codim };
00085 
00087   typedef typename GridImp::template Codim<codimension>::Entity   Entity;
00089   typedef MakeableInterfaceObject<Entity> EntityObject;
00090   typedef typename EntityObject :: ImplementationType EntityImp;
00091   
00093   typedef ThisType ALU3dGridEntitySeedType;
00094 
00096   typedef ALU3dGridEntitySeed<codimension,GridImp> EntitySeedImp;
00097   
00099   ~ALU3dGridEntitySeedBase() 
00100   {
00101 #ifndef NDEBUG 
00102     // clear pointer 
00103     clear();
00104 #endif
00105   }
00106 
00108   ALU3dGridEntitySeedBase();
00109 
00111   ALU3dGridEntitySeedBase(const HElementType& item);
00112 
00114   ALU3dGridEntitySeedBase(const HElementType* item, const HBndSegType* ghostFace );
00115 
00117   ALU3dGridEntitySeedBase(const HBndSegType& ghostFace );  
00118 
00120   //
00121   //  interface methods 
00122   //
00125   ALU3dGridEntitySeedBase(const ALU3dGridEntitySeedType & org);
00126   
00128   bool operator == (const ALU3dGridEntitySeedType& i) const
00129   {
00130     return equals( i );
00131   }
00132 
00134   bool operator != (const ALU3dGridEntitySeedType& i) const
00135   {
00136     return ! equals( i );
00137   }
00138 
00140   ThisType & operator = (const ThisType & org);
00141 
00143   //
00144   //  non-interface methods 
00145   //
00148   bool equals (const ALU3dGridEntitySeedType& i) const;
00149 
00151   void clear() 
00152   { 
00153     item_ = 0;
00154   }
00155 
00157   HElementType* item() const { return Bnd<codim,KeyType>::getItem( item_ ); }
00158 
00160   HElementType* interior() const 
00161   { 
00162     assert( ! isGhost() );
00163     return static_cast< HElementType * > (static_cast< ImplementationType* > (item_));
00164   }
00165 
00167   bool isGhost() const { return Bnd<codim,KeyType>::isGhost( item_ ); }
00168   BNDFaceType* ghost() const 
00169   {
00170     assert( isGhost() );
00171     return Bnd<codim,KeyType>::ghost( item_ ); 
00172   }
00173 
00174   KeyType* toKey(const HElementType* item)
00175   {
00176     return static_cast< KeyType* > (const_cast< ImplementationType* > (static_cast<const ImplementationType* > (item)));
00177   }
00178 
00179   void set(const HElementType& item)
00180   {
00181     item_ = toKey( &item );
00182   }
00183 
00184   KeyType* toKey( const HBndSegType* ghostFace )
00185   {
00186     return Bnd<codim,KeyType>::toKey( ghostFace );
00187   }
00188 
00189   void set(const HBndSegType& ghostFace)
00190   {
00191     item_ = toKey( &ghostFace );
00192   }
00193 
00194   int level () const { return defaultValue; }
00195   int twist () const { return defaultTwist; }
00196   int face  () const { return defaultValue; }
00197 
00198 protected:
00199   // pointer to item 
00200   mutable KeyType* item_;
00201 };
00202 
00203 template<int cd, class GridImp>
00204 class ALU3dGridEntitySeed : 
00205 public ALU3dGridEntitySeedBase<cd,GridImp>
00206 {
00207   typedef ALU3dGridEntitySeedBase<cd,GridImp> BaseType;
00208 
00209   typedef ALU3dGridEntitySeed <cd,GridImp> ThisType;
00210   enum { dim       = GridImp::dimension };
00211   enum { dimworld  = GridImp::dimensionworld };
00212     
00213   typedef typename GridImp::MPICommunicatorType Comm;
00214 
00215   friend class ALU3dGridEntity<cd,dim,GridImp>;
00216   friend class ALU3dGridEntity< 0,dim,GridImp>;
00217   friend class ALU3dGrid < GridImp::elementType, Comm >;
00218 
00219   typedef ALU3dImplTraits< GridImp::elementType, Comm > ImplTraits;
00220   typedef typename ImplTraits::template Codim<cd>::ImplementationType ImplementationType;
00221   typedef typename ImplTraits::template Codim<cd>::InterfaceType HElementType;
00222 
00223   typedef typename ImplTraits::BNDFaceType BNDFaceType;
00224   typedef ALU3dGridEntity<cd,dim,GridImp> ALU3dGridEntityType;
00225 
00226 public:
00227   using BaseType :: defaultValue ;
00228   using BaseType :: defaultTwist ;
00229 
00231   typedef typename GridImp::template Codim<cd>::Entity Entity;
00232   
00234   typedef ALU3dGridEntitySeed<cd,GridImp> ALU3dGridEntitySeedType;
00235   
00237   ALU3dGridEntitySeed(const ImplementationType & item) 
00238   {
00239     // this constructor should only be called by codim=0 entity keys
00240     assert( false );
00241     abort();
00242   }
00243 
00245   ALU3dGridEntitySeed(const HElementType & item,
00246                      const int level, 
00247                      const int twist = defaultTwist,
00248                      const int duneFace = defaultValue
00249                     );
00250 
00252   ALU3dGridEntitySeed() 
00253     : BaseType(), level_(defaultValue), twist_(defaultTwist), face_(defaultValue) {}
00254 
00256   ALU3dGridEntitySeed(const ALU3dGridEntityType& entity) 
00257     : ALU3dGridEntitySeedBase<cd,GridImp> (entity.getItem()),
00258    level_(entity.level()), twist_(defaultTwist), face_(defaultValue)
00259   {}
00260 
00262   ALU3dGridEntitySeed(const ALU3dGridEntitySeedType & org);
00263   
00265   ThisType & operator = (const ThisType & org);
00266 
00268   void clear();
00269 
00271   void set(const HElementType & item, const int level )
00272   {
00273     BaseType :: set( item );
00274     level_ = level ;
00275   }
00276 
00278   int level () const { return level_ ; }
00280   int twist () const { return twist_ ; }
00282   int face  () const { return face_ ; }
00283 
00284   using BaseType :: set ;
00285 
00286   bool operator == (const ALU3dGridEntitySeedType& i) const
00287   {
00288     return equals( i );
00289   }
00290 
00291   bool operator != (const ALU3dGridEntitySeedType& i) const
00292   {
00293     return ! equals( i );
00294   }
00295 
00297   bool equals (const ALU3dGridEntitySeedType& key) const
00298   {
00299     // only compare the item pointer, this is the real key 
00300     return BaseType :: equals( key ) && (level() == key.level()); 
00301   }
00302 
00303 protected:
00304   // level of entity 
00305   int level_; 
00306   // twist of face, for codim 1 only 
00307   int twist_;
00308   // face number, for codim 1 only 
00309   int face_;
00310 };
00311 
00315 template<class GridImp>
00316 class ALU3dGridEntitySeed<0,GridImp> : 
00317 public ALU3dGridEntitySeedBase<0,GridImp> 
00318 {
00319 protected:  
00320   typedef ALU3dGridEntitySeedBase<0,GridImp> BaseType;
00321 
00322   enum { cd = 0 };
00323   typedef ALU3dGridEntitySeed <cd,GridImp> ThisType;
00324   enum { dim       = GridImp::dimension };
00325   enum { dimworld  = GridImp::dimensionworld };
00326     
00327   typedef typename GridImp::MPICommunicatorType Comm;
00328 
00329   friend class ALU3dGridEntity<cd,dim,GridImp>;
00330   friend class ALU3dGridEntity< 0,dim,GridImp>;
00331   friend class ALU3dGrid < GridImp::elementType, Comm >;
00332 
00333   typedef ALU3dImplTraits<GridImp::elementType, Comm > ImplTraits;
00334   typedef typename ImplTraits::template Codim<cd>::ImplementationType ImplementationType;
00335   typedef typename ImplTraits::template Codim<cd>::InterfaceType      HElementType;
00336 
00337   typedef typename ImplTraits::BNDFaceType BNDFaceType;
00338   typedef typename ImplTraits::HBndSegType HBndSegType;
00339 
00340   typedef ALU3dGridEntity< 0,dim,GridImp> ALU3dGridEntityType ;
00341 
00342 public:
00343   using BaseType :: defaultValue ;
00344   using BaseType :: defaultTwist ;
00345 
00347   typedef typename GridImp::template Codim<cd>::Entity Entity;
00348   
00350   typedef ThisType ALU3dGridEntitySeedType;
00351   
00353   ALU3dGridEntitySeed() : BaseType() {} 
00354 
00356   ALU3dGridEntitySeed(const HElementType& item)
00357     : ALU3dGridEntitySeedBase<cd,GridImp> (item) {}
00358 
00360   ALU3dGridEntitySeed(const HElementType& item, int , int , int )
00361     : ALU3dGridEntitySeedBase<cd,GridImp> (item) {}
00362 
00364   ALU3dGridEntitySeed(const HBndSegType& ghostFace )  
00365     : ALU3dGridEntitySeedBase<cd,GridImp> ( ghostFace ) {}
00366 
00368   ALU3dGridEntitySeed(const ALU3dGridEntitySeedType & org)
00369     : ALU3dGridEntitySeedBase<cd,GridImp> (org) 
00370   {
00371   }
00372 };
00373 
00374 
00376 template <int cd, class GridImp> 
00377 inline std :: ostream &operator<< ( std :: ostream &out, 
00378                                     const ALU3dGridEntitySeed<cd,GridImp>& key)
00379 {
00380   out << key.item() << " " << key.level() << " " << key.twist() << " " << key.face();
00381   return out;
00382 }
00383 
00384 
00385 //*******************************************************************
00386 //
00387 //  Implementation 
00388 //
00389 //*******************************************************************
00390 template<int codim, class GridImp >
00391 inline ALU3dGridEntitySeedBase<codim,GridImp> :: 
00392 ALU3dGridEntitySeedBase()
00393   : item_( 0 )
00394 {
00395 }
00396 
00397 template<int codim, class GridImp >
00398 inline ALU3dGridEntitySeedBase<codim,GridImp> :: 
00399 ALU3dGridEntitySeedBase(const HElementType &item)
00400   : item_( toKey(&item) )
00401 {
00402 }
00403 
00404 template<int codim, class GridImp >
00405 inline ALU3dGridEntitySeedBase<codim,GridImp> :: 
00406 ALU3dGridEntitySeedBase(const HBndSegType& ghostFace )
00407   : item_( toKey(&ghostFace) )
00408 {
00409 }
00410 
00411 template<int codim, class GridImp >
00412 inline ALU3dGridEntitySeedBase<codim,GridImp> :: 
00413 ALU3dGridEntitySeedBase(const ALU3dGridEntitySeedType & org)
00414   : item_(org.item_)
00415 {
00416 }
00417 
00418 template<int codim, class GridImp >
00419 inline ALU3dGridEntitySeedBase<codim,GridImp> & 
00420 ALU3dGridEntitySeedBase<codim,GridImp> :: 
00421 operator = (const ALU3dGridEntitySeedType & org)
00422 {
00423   item_  = org.item_;
00424   return *this;
00425 }
00426 
00427 template<int codim, class GridImp >
00428 inline bool ALU3dGridEntitySeedBase<codim,GridImp>::
00429 equals (const ALU3dGridEntitySeedBase<codim,GridImp>& i) const 
00430 {
00431   // check equality of underlying items  
00432   return (item_ == i.item_);
00433 }
00434 
00436 //
00437 //  specialisation for higher codims 
00438 //  
00440 
00441 template<int codim, class GridImp >
00442 inline ALU3dGridEntitySeed<codim,GridImp> :: 
00443 ALU3dGridEntitySeed(const HElementType &item,
00444                    const int level,
00445                    const int twist,
00446                    const int duneFace ) 
00447   : ALU3dGridEntitySeedBase<codim,GridImp> (item) 
00448   , level_(level)
00449   , twist_ (twist)
00450   , face_(duneFace)
00451 {
00452   assert( (codim == 1) ? (face_ >= 0) : 1 );
00453 }
00454 
00455 template<int codim, class GridImp >
00456 inline ALU3dGridEntitySeed<codim,GridImp> :: 
00457 ALU3dGridEntitySeed(const ALU3dGridEntitySeedType & org)
00458   : ALU3dGridEntitySeedBase<codim,GridImp>(org) 
00459   , level_(org.level_)
00460   , twist_(org.twist_)
00461   , face_(org.face_)
00462 {
00463 }
00464 
00465 template<int codim, class GridImp >
00466 inline ALU3dGridEntitySeed<codim,GridImp> &
00467 ALU3dGridEntitySeed<codim,GridImp>:: 
00468 operator = (const ALU3dGridEntitySeedType & org)
00469 {
00470   // docu and cleanup 
00471   BaseType :: operator = ( org );
00472 
00473   // clone other stuff 
00474   level_ = org.level_;
00475   twist_ = org.twist_;
00476   face_  = org.face_;
00477   return *this;
00478 }
00479 
00480 template<int codim, class GridImp >
00481 inline void 
00482 ALU3dGridEntitySeed<codim,GridImp>::clear ()
00483 {
00484   BaseType :: clear();
00485   level_ = defaultValue ;
00486   twist_ = defaultTwist ;
00487   face_  = defaultValue ;
00488 }
00489 
00490 } // end namespace Dune 
00491 #endif