dune-grid  2.2.0
alu2dgridfactory.hh
Go to the documentation of this file.
00001 // -*- tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
00002 // vi: set et ts=8 sw=2 sts=2:
00003 
00004 #ifndef DUNE_ALU2DGRID_FACTORY_HH
00005 #define DUNE_ALU2DGRID_FACTORY_HH
00006 
00007 #ifdef ENABLE_ALUGRID
00008 
00009 #include <dune/common/array.hh>
00010 #include <dune/common/mpihelper.hh>
00011 
00012 #include <dune/geometry/referenceelements.hh>
00013 
00014 #include <dune/grid/common/gridfactory.hh>
00015 
00016 #include <dune/grid/alugrid/common/transformation.hh>
00017 #include <dune/grid/alugrid/2d/grid.hh>
00018 
00019 namespace Dune
00020 {
00021 
00023   template< class GridImp >
00024   class ALU2dGridFactory
00025   : public GridFactoryInterface< GridImp >
00026   {
00027   public:
00028     typedef GridImp  Grid; 
00029 
00030     static const int dimension = Grid::dimension;
00031     static const int dimensionworld = Grid::dimensionworld;
00032 
00034     typedef DuneBoundaryProjection< dimensionworld >  DuneBoundaryProjectionType;
00035 
00036     template< int codim >
00037     struct Codim
00038     {
00039       typedef typename Grid::template Codim< codim >::Entity Entity;
00040     };
00041 
00042   private:
00043     typedef Dune::BoundarySegmentWrapper<2, dimensionworld> BoundarySegmentWrapperType;
00044 
00045     typedef ALU2dGridFactory< Grid > ThisType;
00046     typedef GridFactoryInterface< Grid > BaseType;
00047 
00048     typedef typename Grid::ctype ctype;
00049 
00050     static const ALU2DSPACE ElementType elementType = Grid::elementType;
00051     static const unsigned int numFaceCorners = 2;
00052     
00053     typedef FieldVector< ctype, dimensionworld > VertexType;
00054     typedef std::vector< unsigned int > ElementType;
00055     typedef array< unsigned int, numFaceCorners > FaceType;
00056 
00057     static const int periodicBndId = ALU2dImplTraits< dimensionworld, elementType >::HBndElType::general_periodic;
00058 
00059   public:
00060     typedef ALUGridTransformation< ctype, dimensionworld > Transformation;
00061 
00063     typedef typename Transformation::WorldVector WorldVector;
00065     typedef typename Transformation::WorldMatrix WorldMatrix;
00066 
00067   private:
00068     struct FaceLess;
00069 
00070     typedef std::vector< VertexType > VertexVector;
00071     typedef std::vector< ElementType > ElementVector;
00072     typedef std::vector< std::pair< FaceType, int > > BoundaryIdVector;
00073 
00074     typedef std::map< FaceType, const DuneBoundaryProjectionType* > BoundaryProjectionMap;
00075     typedef std::vector< const DuneBoundaryProjectionType* > BoundaryProjectionVector;
00076 
00077     typedef std::pair< unsigned int, int > SubEntity;
00078     typedef std::map< FaceType, SubEntity, FaceLess > FaceMap;
00079     typedef std::vector< Transformation > FaceTransformationVector;
00080     typedef std::map< FaceType, unsigned int, FaceLess > PeriodicNeighborMap;
00081 
00082     // copy vertex numbers and store smalled #dimension ones 
00083     void copyAndSort(const std::vector<unsigned int>& vertices, FaceType& faceId) const
00084     {
00085       std::vector<unsigned int> tmp( vertices );
00086       std::sort( tmp.begin(), tmp.end() );
00087 
00088       // copy only the first dimension vertices (enough for key)
00089       for( size_t i = 0; i < faceId.size(); ++i ) faceId[ i ] = tmp[ i ];
00090     }
00091 
00092   public:
00094     explicit ALU2dGridFactory ( bool removeGeneratedFile = true );
00095 
00097     explicit ALU2dGridFactory ( const std::string &filename );
00098 
00100     virtual ~ALU2dGridFactory ();
00101 
00106     virtual void insertVertex ( const VertexType &pos );
00107 
00116     virtual void
00117     insertElement ( const GeometryType &geometry,
00118                     const std::vector< unsigned int > &vertices );
00119 
00130     virtual void
00131     insertBoundary ( const GeometryType &geometry,
00132                      const std::vector< unsigned int > &faceVertices,
00133                      const int id );
00134 
00141     virtual void insertBoundary ( const int element, const int face, const int id );
00142 
00151     virtual void
00152     insertBoundaryProjection ( const GeometryType &type,
00153                                const std::vector< unsigned int > &vertices,
00154                                const DuneBoundaryProjectionType *projection );
00155 
00160     virtual void
00161     insertBoundarySegment ( const std::vector< unsigned int >& vertices ) ;
00162 
00168     virtual void
00169     insertBoundarySegment ( const std::vector< unsigned int >& vertices,
00170                             const shared_ptr<BoundarySegment<2,dimensionworld> >& boundarySegment ) ;
00171 
00176     virtual void insertBoundaryProjection ( const DuneBoundaryProjectionType& bndProjection );
00177 
00187     void insertFaceTransformation ( const WorldMatrix &matrix, const WorldVector &shift );
00188 
00189     virtual unsigned int
00190     insertionIndex ( const typename Codim< 0 >::Entity &entity ) const
00191     {
00192       return Grid::getRealImplementation( entity ).getIndex();
00193     }
00194 
00195     virtual unsigned int
00196     insertionIndex ( const typename Codim< dimension >::Entity &entity ) const
00197     {
00198       return Grid::getRealImplementation( entity ).getIndex();
00199     }
00200 
00201     virtual unsigned int
00202     insertionIndex ( const typename Grid::LeafIntersection &intersection ) const
00203     {
00204       return intersection.boundarySegmentIndex();
00205     }
00206 
00207     virtual bool
00208     wasInserted ( const typename Grid::LeafIntersection &intersection ) const
00209     {
00210       return intersection.boundary() &&
00211         ( insertionIndex(intersection) < numFacesInserted_ );
00212     }
00213 
00218     Grid *createGrid ();
00219 
00220     Grid *createGrid ( const bool addMissingBoundaries, const std::string dgfName = "" );
00221 
00222     Grid *createGrid ( const bool addMissingBoundaries, bool temporary, const std::string dgfName = "" );
00223 
00224     void setTolerance ( const ctype &epsilon ) { epsilon_ = epsilon; }
00225 
00226   protected:  
00228     virtual Grid* createGridObj( const bool temporary,
00229                                  const std::string& filename,
00230                                  std::istream& inFile,
00231                                  BoundaryProjectionVector* bndProjections )
00232     {
00233       return ( temporary ) ? 
00234             new Grid( filename, inFile, globalProjection_, bndProjections, grdVerbose_ ) :
00235             new Grid( filename, globalProjection_ , bndProjections, grdVerbose_ );
00236     }
00237 
00242     void setVerbosity( const bool verbose ) { grdVerbose_ = verbose; }
00243 
00244   private:
00245     static void generateFace ( const ElementType &element, const int f, FaceType &face );
00246     void correctElementOrientation ();
00247     typename FaceMap::const_iterator findPeriodicNeighbor( const FaceMap &faceMap, const FaceType &key ) const;
00248     void reinsertBoundary ( const FaceMap &faceMap, const typename FaceMap::const_iterator &pos, const int id );
00249     void recreateBoundaryIds ( const int defaultId = 1 );
00250 
00251     VertexVector vertices_;
00252     ElementVector elements_;
00253     BoundaryIdVector boundaryIds_;
00254     const DuneBoundaryProjectionType* globalProjection_ ;
00255     BoundaryProjectionMap boundaryProjections_;
00256     unsigned int numFacesInserted_;
00257     bool grdVerbose_;
00258     FaceTransformationVector faceTransformations_;
00259     PeriodicNeighborMap periodicNeighborMap_;
00260     ctype epsilon_;
00261   };
00262 
00263 
00264   template< class GridImp >
00265   struct ALU2dGridFactory< GridImp >::FaceLess
00266   : public std::binary_function< FaceType, FaceType, bool >
00267   {
00268     bool operator() ( const FaceType &a, const FaceType &b ) const
00269     {
00270       for( unsigned int i = 0; i < numFaceCorners; ++i )
00271       {
00272         if( a[ i ] != b[ i ] )
00273           return (a[ i ] < b[ i ]);
00274       }
00275       return false;
00276     }
00277   };
00278 
00279 
00283   template<int dimw>
00284   class GridFactory< ALUConformGrid<2,dimw> >
00285   : public ALU2dGridFactory<ALUConformGrid<2, dimw> >
00286   {
00287   public:
00288     typedef ALUConformGrid< 2, dimw > Grid;
00289 
00290   protected:
00291     typedef GridFactory ThisType;
00292     typedef ALU2dGridFactory< Grid > BaseType;
00293 
00294   public:
00296     explicit GridFactory ( )
00297     : BaseType( )
00298     {}
00299 
00301     GridFactory ( const std::string &filename )
00302     : BaseType( filename )
00303     {}
00304 
00306     GridFactory ( const bool verbose )
00307     : BaseType( )
00308     {
00309       this->setVerbosity( verbose );
00310     }
00311   };
00315   template<int dimw>
00316   class GridFactory< ALUSimplexGrid<2,dimw> >
00317   : public ALU2dGridFactory<ALUSimplexGrid<2, dimw> >
00318   {
00319   public:
00320     typedef ALUSimplexGrid< 2, dimw > Grid;
00321 
00322   protected:
00323     typedef GridFactory ThisType;
00324     typedef ALU2dGridFactory< Grid > BaseType;
00325 
00326   public:
00328     explicit GridFactory ( )
00329     : BaseType( )
00330     {}
00331 
00333     GridFactory ( const std::string &filename )
00334     : BaseType( filename )
00335     {}
00336 
00341     GridFactory ( const bool verbose )
00342     : BaseType( )
00343     {
00344       this->setVerbosity( verbose );
00345     }
00346   };
00347 
00351   template<int dimw>
00352   class GridFactory< ALUCubeGrid<2,dimw> >
00353   : public ALU2dGridFactory<ALUCubeGrid<2,dimw> >
00354   {
00355   public:
00356     typedef ALUCubeGrid< 2, dimw > Grid;
00357 
00358   protected:
00359     typedef GridFactory ThisType;
00360     typedef ALU2dGridFactory< Grid > BaseType;
00361 
00362   public:
00364     explicit GridFactory ( )
00365     : BaseType( )
00366     {}
00367 
00369     GridFactory ( const std::string &filename )
00370     : BaseType( filename )
00371     {}
00372 
00377     GridFactory ( const bool verbose )
00378     : BaseType( )
00379     {
00380       this->setVerbosity( verbose );
00381     }
00382   };
00383 
00388   template<int dimw, ALUGridElementType eltype, ALUGridRefinementType refinementtype, class Comm>
00389   class GridFactory< ALUGrid<2, dimw, eltype, refinementtype, Comm > >
00390   : public ALU2dGridFactory< ALUGrid<2, dimw, eltype, refinementtype, Comm > >
00391   {
00392   public:
00393     typedef ALUGrid<2, dimw, eltype, refinementtype, Comm > Grid;
00394 
00395   protected:  
00396     typedef GridFactory ThisType;
00397     typedef ALU2dGridFactory< Grid > BaseType;
00398 
00399   public:
00401     explicit GridFactory ( )
00402     : BaseType( )
00403     {}
00404 
00406     GridFactory ( const std::string &filename )
00407     : BaseType( filename )
00408     {}
00409 
00414     GridFactory ( const bool verbose )
00415     : BaseType( )
00416     {
00417       this->setVerbosity( verbose );
00418     }
00419   };
00420 
00421 
00422 
00423   // Inline Implementations
00424   // ----------------------
00425 
00426   template< class GridImp >
00427   inline ALU2dGridFactory< GridImp >::ALU2dGridFactory ( bool removeGeneratedFile )
00428   : globalProjection_ ( 0 ),
00429     numFacesInserted_ ( 0 ),
00430     grdVerbose_( true ),
00431     epsilon_( 1e-8 )
00432   {}
00433 
00434 
00435   template< class GridImp >
00436   inline ALU2dGridFactory< GridImp >::ALU2dGridFactory ( const std::string &filename )
00437   : globalProjection_ ( 0 ),
00438     numFacesInserted_ ( 0 ),
00439     grdVerbose_( true ),
00440     epsilon_( 1e-8 )
00441   {}
00442 
00443 
00444   template< class GridImp >
00445   inline ALU2dGridFactory< GridImp >::~ALU2dGridFactory ()
00446   {}
00447 
00448 
00449   template< class GridImp >
00450   inline GridImp *ALU2dGridFactory< GridImp >::createGrid ()
00451   {
00452     return createGrid( true, true, "" );
00453   }
00454 
00455 
00456   template< class GridImp >
00457   inline GridImp *ALU2dGridFactory< GridImp >
00458     ::createGrid ( const bool addMissingBoundaries, const std::string dgfName )
00459   {
00460     return createGrid( addMissingBoundaries, true, dgfName );
00461   }
00462 
00463 }
00464 
00465 #endif // #ifdef ENABLE_ALUGRID
00466 
00467 #endif // #ifndef DUNE_ALU2DGRID_FACTORY_HH