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/grid/common/genericreferenceelements.hh>
00013 #include <dune/grid/common/gridfactory.hh>
00014 
00015 #include <dune/grid/alugrid/common/transformation.hh>
00016 #include <dune/grid/alugrid/2d/grid.hh>
00017 
00018 namespace Dune
00019 {
00020 
00022   template< template< int, int > class ALUGrid, int dimw >
00023   class ALU2dGridFactory
00024   : public GridFactoryInterface< ALUGrid< 2, dimw > >
00025   {
00026   public:
00027     typedef ALUGrid< 2, dimw > Grid;
00028 
00030     typedef DuneBoundaryProjection< dimw >  DuneBoundaryProjectionType;
00031 
00032     template< int codim >
00033     struct Codim
00034     {
00035       typedef typename Grid::template Codim< codim >::Entity Entity;
00036     };
00037 
00038   private:
00039     typedef Dune::BoundarySegmentWrapper<2, dimw> BoundarySegmentWrapperType;
00040 
00041     typedef ALU2dGridFactory< ALUGrid,dimw > ThisType;
00042     typedef GridFactoryInterface< Grid > BaseType;
00043 
00044     typedef typename Grid::ctype ctype;
00045 
00046     static const int dimension = Grid::dimension;
00047     static const int dimensionworld = Grid::dimensionworld;
00048 
00049     static const ALU2DSPACE ElementType elementType = Grid::elementType;
00050     static const unsigned int numFaceCorners = 2;
00051     
00052     typedef FieldVector< ctype, dimensionworld > VertexType;
00053     typedef std::vector< unsigned int > ElementType;
00054     typedef array< unsigned int, numFaceCorners > FaceType;
00055 
00056     static const int periodicBndId = ALU2dImplTraits< dimensionworld, elementType >::HBndElType::general_periodic;
00057 
00058   public:
00059     typedef ALUGridTransformation< ctype, dimensionworld > Transformation;
00060 
00062     typedef typename Transformation::WorldVector WorldVector;
00064     typedef typename Transformation::WorldMatrix WorldMatrix;
00065 
00066   private:
00067     struct FaceLess;
00068 
00069     typedef std::vector< VertexType > VertexVector;
00070     typedef std::vector< ElementType > ElementVector;
00071     typedef std::vector< std::pair< FaceType, int > > BoundaryIdVector;
00072 
00073     typedef std::map< FaceType, const DuneBoundaryProjectionType* > BoundaryProjectionMap;
00074     typedef std::vector< const DuneBoundaryProjectionType* > BoundaryProjectionVector;
00075 
00076     typedef std::pair< unsigned int, int > SubEntity;
00077     typedef std::map< FaceType, SubEntity, FaceLess > FaceMap;
00078     typedef std::vector< Transformation > FaceTransformationVector;
00079     typedef std::map< FaceType, unsigned int, FaceLess > PeriodicNeighborMap;
00080 
00081     // copy vertex numbers and store smalled #dimension ones 
00082     void copyAndSort(const std::vector<unsigned int>& vertices, FaceType& faceId) const
00083     {
00084       std::vector<unsigned int> tmp( vertices );
00085       std::sort( tmp.begin(), tmp.end() );
00086 
00087       // copy only the first dimension vertices (enough for key)
00088       for( size_t i = 0; i < faceId.size(); ++i ) faceId[ i ] = tmp[ i ];
00089     }
00090 
00091   public:
00093     explicit ALU2dGridFactory ( bool removeGeneratedFile = true );
00094 
00096     explicit ALU2dGridFactory ( const std::string &filename );
00097 
00099     virtual ~ALU2dGridFactory ();
00100 
00105     virtual void insertVertex ( const VertexType &pos );
00106 
00115     virtual void
00116     insertElement ( const GeometryType &geometry,
00117                     const std::vector< unsigned int > &vertices );
00118 
00129     virtual void
00130     insertBoundary ( const GeometryType &geometry,
00131                      const std::vector< unsigned int > &faceVertices,
00132                      const int id );
00133 
00140     virtual void insertBoundary ( const int element, const int face, const int id );
00141 
00150     virtual void
00151     insertBoundaryProjection ( const GeometryType &type,
00152                                const std::vector< unsigned int > &vertices,
00153                                const DuneBoundaryProjectionType *projection );
00154 
00159     virtual void
00160     insertBoundarySegment ( const std::vector< unsigned int >& vertices ) ;
00161 
00167     virtual void
00168     insertBoundarySegment ( const std::vector< unsigned int >& vertices,
00169                             const shared_ptr<BoundarySegment<2,dimw> >& boundarySegment ) ;
00170 
00175     virtual void insertBoundaryProjection ( const DuneBoundaryProjectionType& bndProjection );
00176 
00186     void insertFaceTransformation ( const WorldMatrix &matrix, const WorldVector &shift );
00187 
00188     virtual unsigned int
00189     insertionIndex ( const typename Codim< 0 >::Entity &entity ) const
00190     {
00191       return Grid::getRealImplementation( entity ).getIndex();
00192     }
00193 
00194     virtual unsigned int
00195     insertionIndex ( const typename Codim< dimension >::Entity &entity ) const
00196     {
00197       return Grid::getRealImplementation( entity ).getIndex();
00198     }
00199 
00200     virtual unsigned int
00201     insertionIndex ( const typename Grid::LeafIntersection &intersection ) const
00202     {
00203       return intersection.boundarySegmentIndex();
00204     }
00205 
00206     virtual bool
00207     wasInserted ( const typename Grid::LeafIntersection &intersection ) const
00208     {
00209       return intersection.boundary() &&
00210         ( insertionIndex(intersection) < numFacesInserted_ );
00211     }
00212 
00217     Grid *createGrid ();
00218 
00219     Grid *createGrid ( const bool addMissingBoundaries, const std::string dgfName = "" );
00220 
00221     Grid *createGrid ( const bool addMissingBoundaries, bool temporary, const std::string dgfName = "" );
00222 
00223     void setTolerance ( const ctype &epsilon ) { epsilon_ = epsilon; }
00224 
00225   protected:  
00230     void setVerbosity( const bool verbose ) { grdVerbose_ = verbose; }
00231 
00232   private:
00233     static std::string temporaryFileName (const std::string& dgfName );
00234     static void generateFace ( const ElementType &element, const int f, FaceType &face );
00235     void correctElementOrientation ();
00236     typename FaceMap::const_iterator findPeriodicNeighbor( const FaceMap &faceMap, const FaceType &key ) const;
00237     void reinsertBoundary ( const FaceMap &faceMap, const typename FaceMap::const_iterator &pos, const int id );
00238     void recreateBoundaryIds ( const int defaultId = 1 );
00239 
00240     VertexVector vertices_;
00241     ElementVector elements_;
00242     BoundaryIdVector boundaryIds_;
00243     const DuneBoundaryProjectionType* globalProjection_ ;
00244     BoundaryProjectionMap boundaryProjections_;
00245     unsigned int numFacesInserted_;
00246     bool grdVerbose_;
00247     FaceTransformationVector faceTransformations_;
00248     PeriodicNeighborMap periodicNeighborMap_;
00249     ctype epsilon_;
00250   };
00251 
00252 
00253   template< template< int, int > class ALUGrid, int dimw >
00254   struct ALU2dGridFactory< ALUGrid, dimw >::FaceLess
00255   : public std::binary_function< FaceType, FaceType, bool >
00256   {
00257     bool operator() ( const FaceType &a, const FaceType &b ) const
00258     {
00259       for( unsigned int i = 0; i < numFaceCorners; ++i )
00260       {
00261         if( a[ i ] != b[ i ] )
00262           return (a[ i ] < b[ i ]);
00263       }
00264       return false;
00265     }
00266   };
00267 
00268 
00272   template<int dimw>
00273   class GridFactory< ALUConformGrid<2,dimw> >
00274   : public ALU2dGridFactory<ALUConformGrid, dimw>
00275   {
00276     typedef GridFactory ThisType;
00277     typedef ALU2dGridFactory<ALUConformGrid,dimw> BaseType;
00278 
00279   public:
00280     typedef ALUConformGrid< 2, dimw > Grid;
00281 
00282   public:
00284     explicit GridFactory ( )
00285     : BaseType( )
00286     {}
00287 
00289     GridFactory ( const std::string &filename )
00290     : BaseType( filename )
00291     {}
00292 
00294     GridFactory ( const bool verbose )
00295     : BaseType( )
00296     {
00297       this->setVerbosity( verbose );
00298     }
00299   };
00303   template<int dimw>
00304   class GridFactory< ALUSimplexGrid<2,dimw> >
00305   : public ALU2dGridFactory<ALUSimplexGrid,dimw>
00306   {
00307     typedef GridFactory ThisType;
00308     typedef ALU2dGridFactory<ALUSimplexGrid,dimw> BaseType;
00309 
00310   public:
00311     typedef ALUSimplexGrid< 2, dimw > Grid;
00312 
00313   public:
00315     explicit GridFactory ( )
00316     : BaseType( )
00317     {}
00318 
00320     GridFactory ( const std::string &filename )
00321     : BaseType( filename )
00322     {}
00323 
00325     GridFactory ( const bool verbose )
00326     : BaseType( )
00327     {
00328       this->setVerbosity( verbose );
00329     }
00330   };
00331 
00335   template<int dimw>
00336   class GridFactory< ALUCubeGrid<2,dimw> >
00337   : public ALU2dGridFactory<ALUCubeGrid,dimw>
00338   {
00339     typedef GridFactory ThisType;
00340     typedef ALU2dGridFactory<ALUCubeGrid,dimw> BaseType;
00341 
00342   public:
00343     typedef ALUCubeGrid< 2, dimw > Grid;
00344 
00345   public:
00347     explicit GridFactory ( )
00348     : BaseType( )
00349     {}
00350 
00352     GridFactory ( const std::string &filename )
00353     : BaseType( filename )
00354     {}
00355 
00357     GridFactory ( const bool verbose )
00358     : BaseType( )
00359     {
00360       this->setVerbosity( verbose );
00361     }
00362   };
00363 
00364 
00365 
00366   // Inline Implementations
00367   // ----------------------
00368 
00369   template< template< int, int > class ALUGrid, int dimw >
00370   inline ALU2dGridFactory< ALUGrid, dimw >::ALU2dGridFactory ( bool removeGeneratedFile )
00371   : globalProjection_ ( 0 ),
00372     numFacesInserted_ ( 0 ),
00373     grdVerbose_( true ),
00374     epsilon_( 1e-8 )
00375   {}
00376 
00377 
00378   template< template< int, int > class ALUGrid, int dimw >
00379   inline ALU2dGridFactory< ALUGrid, dimw >::ALU2dGridFactory ( const std::string &filename )
00380   : globalProjection_ ( 0 ),
00381     numFacesInserted_ ( 0 ),
00382     grdVerbose_( true ),
00383     epsilon_( 1e-8 )
00384   {}
00385 
00386 
00387   template< template< int, int > class ALUGrid, int dimw >
00388   inline ALU2dGridFactory< ALUGrid, dimw >::~ALU2dGridFactory ()
00389   {}
00390 
00391 
00392   template< template< int, int > class ALUGrid, int dimw >
00393   inline ALUGrid< 2, dimw > *ALU2dGridFactory< ALUGrid, dimw >::createGrid ()
00394   {
00395     return createGrid( true, true, "" );
00396   }
00397 
00398 
00399   template< template< int, int > class ALUGrid, int dimw >
00400   inline ALUGrid< 2, dimw > *ALU2dGridFactory< ALUGrid, dimw >
00401     ::createGrid ( const bool addMissingBoundaries, const std::string dgfName )
00402   {
00403     return createGrid( addMissingBoundaries, true, dgfName );
00404   }
00405 
00406 }
00407 
00408 #endif // #ifdef ENABLE_ALUGRID
00409 
00410 #endif // #ifndef DUNE_ALU2DGRID_FACTORY_HH

Generated on Fri Apr 29 2011 with Doxygen (ver 1.7.1) [doxygen-log,error-log].