alu3dgridfactory.hh

00001 #ifndef DUNE_ALU3DGRID_FACTORY_HH
00002 #define DUNE_ALU3DGRID_FACTORY_HH
00003 
00004 #ifdef ENABLE_ALUGRID
00005 
00006 #include <dune/common/fixedarray.hh>
00007 #include <dune/common/mpihelper.hh>
00008 
00009 #include <dune/grid/common/gridfactory.hh>
00010 #include <dune/grid/alugrid/3d/alugrid.hh>
00011 #include <dune/grid/io/file/dgfparser/entitykey.hh>
00012 
00013 namespace Dune
00014 {
00015 
00017   template< template< int, int > class ALUGrid >
00018   class ALU3dGridFactory
00019   : public GridFactoryInterface< ALUGrid< 3, 3 > >
00020   {
00021     typedef ALU3dGridFactory< ALUGrid > ThisType;
00022     typedef GridFactoryInterface< ALUGrid< 3, 3 > > BaseType;
00023 
00024   public:
00025     typedef ALUGrid< 3, 3 > GridType;
00026 
00027     typedef MPIHelper :: MPICommunicator MPICommunicatorType;
00028 
00029   private:
00030     typedef typename GridType :: ctype ctype;
00031 
00032     static const ALU3dGridElementType elementType = GridType :: elementType;
00033 
00034     dune_static_assert( (elementType == tetra || elementType == hexa),
00035                         "ALU3dGridFactory supports only grids containing "
00036                         "tetrahedrons or hexahedrons exclusively." );
00037 
00038     static const unsigned int dimension = GridType :: dimension;
00039     static const unsigned int dimensionworld = GridType :: dimensionworld;
00040 
00041     static const unsigned int numCorners
00042       = EntityCount< elementType > :: numVertices;
00043     static const unsigned int numFaceCorners
00044       = EntityCount< elementType > :: numVerticesPerFace;
00045     
00046     typedef ElementTopologyMapping< elementType > ElementTopologyMappingType;
00047     typedef FaceTopologyMapping< elementType > FaceTopologyMappingType;
00048 
00049     typedef FieldVector< ctype, dimensionworld > VertexType;
00050     typedef std::vector< unsigned int > ElementType;
00051     typedef array< unsigned int, numFaceCorners > FaceType;
00052 
00053   private:
00054     typedef std::vector< VertexType > VertexVector;
00055     typedef std::vector< ElementType > ElementVector;
00056     typedef std::vector< std::pair< FaceType, int > > BoundaryIdVector;
00057 
00058     const std::string filename_; 
00059     MPICommunicatorType communicator_;
00060     bool removeGeneratedFile_;
00061 #if ALU3DGRID_PARALLEL
00062     int rank_;
00063 #endif
00064     VertexVector vertices_;
00065     ElementVector elements_;
00066     BoundaryIdVector boundaryIds_;
00067 
00068   public:
00070     explicit ALU3dGridFactory ( const MPICommunicatorType &communicator
00071                                   = MPIHelper :: getCommunicator(),
00072                                 bool removeGeneratedFile = true );
00073 
00075     explicit ALU3dGridFactory ( const std::string &filename, 
00076                                 const MPICommunicatorType &communicator
00077                                   = MPIHelper :: getCommunicator() );
00078 
00080     virtual ~ALU3dGridFactory ();
00081 
00086     virtual void insertVertex ( const VertexType &pos );
00087 
00096     virtual void
00097     insertElement ( const GeometryType &geometry,
00098                     const std::vector< unsigned int > &vertices );
00099 
00110     virtual void
00111     insertBoundary ( const GeometryType &geometry,
00112                      const std::vector< unsigned int > &faceVertices,
00113                      const int id );
00114 
00115     void insertBoundary ( const GeometryType &geometry,
00116                           const DGFEntityKey< unsigned int > &key,
00117                           const int id );
00118 
00123     GridType *createGrid ();
00124 
00125     GridType *createGrid ( const bool addMissingBoundaries );
00126 
00127   private:
00128     template< class T >
00129     static void exchange ( T &x, T &y );
00130 
00131     void assertGeometryType( const GeometryType &geometry );
00132     static std::string temporaryFileName ();
00133     void correctElementOrientation ();
00134     void recreateBoundaryIds ( const int defaultId = 1 );
00135   };
00136 
00137 
00138   template< template< int, int > class ALUGrid >
00139   template< class T >
00140   inline void ALU3dGridFactory< ALUGrid >::exchange ( T &x, T &y )
00141   {
00142     T dummy = x; x = y; y = dummy;
00143   }
00144 
00145 
00146   template< template< int, int > class ALUGrid >
00147   inline void ALU3dGridFactory< ALUGrid >
00148     :: assertGeometryType( const GeometryType &geometry )
00149   {
00150     if( elementType == tetra )
00151     {
00152       if( !geometry.isSimplex() )
00153         DUNE_THROW( GridError, "Only simplex geometries can be inserted into "
00154                                "ALUSimplexGrid< 3, 3 >." );
00155     }
00156     else
00157     {
00158       if( !geometry.isCube() )
00159         DUNE_THROW( GridError, "Only cube geometries can be inserted into "
00160                                "ALUCubeGrid< 3, 3 >." );
00161     }
00162   }
00163   
00164 
00167   template<>
00168   class GridFactory< ALUSimplexGrid< 3, 3 > >
00169   : public ALU3dGridFactory< ALUSimplexGrid >
00170   {
00171     typedef GridFactory< ALUSimplexGrid< 3, 3 > > ThisType;
00172     typedef ALU3dGridFactory< ALUSimplexGrid > BaseType;
00173 
00174   public:
00175     typedef ALUSimplexGrid< 3, 3 > GridType;
00176 
00177     typedef MPIHelper :: MPICommunicator MPICommunicatorType;
00178 
00179   public:
00181     explicit GridFactory ( const MPICommunicatorType &communicator
00182                              = MPIHelper :: getCommunicator() )
00183     : BaseType( communicator )
00184     {}
00185 
00187     GridFactory ( const std::string filename, 
00188                   const MPICommunicatorType &communicator
00189                          = MPIHelper :: getCommunicator() )
00190     : BaseType( filename, communicator )
00191     {}
00192   };
00193 
00194 
00195 
00198   template<>
00199   class GridFactory< ALUCubeGrid< 3, 3 > >
00200   : public ALU3dGridFactory< ALUCubeGrid >
00201   {
00202     typedef GridFactory< ALUCubeGrid< 3, 3 > > ThisType;
00203     typedef ALU3dGridFactory< ALUCubeGrid > BaseType;
00204 
00205   public:
00206     typedef ALUCubeGrid< 3, 3 > GridType;
00207 
00208     typedef MPIHelper :: MPICommunicator MPICommunicatorType;
00209 
00210   public:
00212     explicit GridFactory ( const MPICommunicatorType &communicator
00213                              = MPIHelper :: getCommunicator() )
00214     : BaseType( communicator )
00215     {}
00216     
00218     GridFactory ( const std::string filename, 
00219                   const MPICommunicatorType &communicator
00220                          = MPIHelper :: getCommunicator() )
00221     : BaseType( filename, communicator )
00222     {}
00223   };
00224 
00225 }
00226 
00227 // This include is nasty, but we cannot incorporate 'alu3dgridfactory.cc' into
00228 // the lib before HAVE_MPI behaves predictable
00229 #include "alu3dgridfactory.cc"
00230 
00231 #endif // #ifdef ENABLE_ALUGRID
00232 
00233 #endif

Generated on Tue Jul 28 22:28:14 2009 for dune-grid by  doxygen 1.5.6