4#ifndef DUNE_ALU3DGRID_FACTORY_HH
5#define DUNE_ALU3DGRID_FACTORY_HH
15#include <dune/geometry/referenceelements.hh>
18#include <dune/grid/common/boundaryprojection.hh>
20#include <dune/grid/alugrid/common/transformation.hh>
27 template<
class ALUGr
id >
28 class ALU3dGridFactory
29 :
public GridFactoryInterface< ALUGrid >
31 typedef ALU3dGridFactory< ALUGrid > ThisType;
32 typedef GridFactoryInterface< ALUGrid > BaseType;
39 static const ALU3dGridElementType elementType = Grid::elementType;
44 typedef typename Grid::MPICommunicatorType MPICommunicatorType;
47 typedef DuneBoundaryProjection< 3 > DuneBoundaryProjectionType;
52 typedef typename Grid::template Codim< codim >::Entity Entity;
55 typedef unsigned int VertexId;
57 typedef ALUGridTransformation< ctype, dimensionworld > Transformation;
60 typedef typename Transformation::WorldVector WorldVector;
62 typedef typename Transformation::WorldMatrix WorldMatrix;
66 "ALU3dGridFactory supports only grids containing "
67 "tetrahedrons or hexahedrons exclusively." );
71 static const unsigned int numCorners = EntityCount< elementType >::numVertices;
72 static const unsigned int numFaces = EntityCount< elementType >::numFaces;
73 static const unsigned int numFaceCorners = EntityCount< elementType >::numVerticesPerFace;
75 typedef ElementTopologyMapping< elementType > ElementTopologyMappingType;
76 typedef FaceTopologyMapping< elementType > FaceTopologyMappingType;
78 typedef FieldVector< ctype, dimensionworld > VertexType;
79 typedef std::vector< unsigned int > ElementType;
80 typedef array< unsigned int, numFaceCorners > FaceType;
84 typedef std::vector< std::pair< VertexType, size_t > > VertexVector;
85 typedef std::vector< ElementType > ElementVector;
86 typedef std::pair< FaceType, int > BndPair ;
87 typedef std::map< FaceType, int > BoundaryIdMap;
88 typedef std::vector< std::pair< BndPair, BndPair > > PeriodicBoundaryVector;
89 typedef std::pair< unsigned int, int > SubEntity;
90 typedef std::map< FaceType, SubEntity, FaceLess > FaceMap;
92 typedef std::map< FaceType, const DuneBoundaryProjectionType* > BoundaryProjectionMap;
93 typedef std::vector< const DuneBoundaryProjectionType* > BoundaryProjectionVector;
95 typedef std::vector< Transformation > FaceTransformationVector;
98 void copyAndSort (
const std::vector< unsigned int > &vertices, FaceType &faceId )
const
100 std::vector<unsigned int> tmp( vertices );
101 std::sort( tmp.begin(), tmp.end() );
104 for(
size_t i = 0; i < faceId.size(); ++i ) faceId[ i ] = tmp[ i ];
109 virtual Grid* createGridObj( BoundaryProjectionVector* bndProjections,
const std::string& name )
const
111 return ( allowGridGeneration_ ) ?
112 new Grid( communicator_, globalProjection_, bndProjections , name, realGrid_ ) :
113 new Grid( communicator_ );
118 explicit ALU3dGridFactory (
const MPICommunicatorType &communicator = Grid::defaultCommunicator(),
119 bool removeGeneratedFile =
true );
122 explicit ALU3dGridFactory (
const std::string &filename,
123 const MPICommunicatorType &communicator = Grid::defaultCommunicator() );
126 explicit ALU3dGridFactory (
const bool verbose,
const MPICommunicatorType &communicator );
129 virtual ~ALU3dGridFactory ();
135 virtual void insertVertex (
const VertexType &pos );
138 VertexId insertVertex (
const VertexType &pos,
const size_t globalId );
149 insertElement (
const GeometryType &geometry,
150 const std::vector< VertexId > &vertices );
163 insertBoundary (
const GeometryType &geometry,
164 const std::vector< VertexId > &faceVertices,
173 virtual void insertBoundary (
const int element,
const int face,
const int id );
176 void insertProcessBorder (
const int element,
const int face )
178 insertBoundary( element, face, ALU3DSPACE ProcessorBoundary_t );
190 insertBoundaryProjection (
const GeometryType &type,
191 const std::vector< VertexId > &vertices,
192 const DuneBoundaryProjectionType *projection );
199 insertBoundarySegment (
const std::vector< VertexId >& vertices ) ;
207 insertBoundarySegment (
const std::vector< VertexId >& vertices,
208 const shared_ptr<BoundarySegment<3,3> >& boundarySegment ) ;
214 virtual void insertBoundaryProjection (
const DuneBoundaryProjectionType& bndProjection );
225 void insertFaceTransformation (
const WorldMatrix &matrix,
const WorldVector &shift );
233 Grid *createGrid (
const bool addMissingBoundaries,
const std::string dgfName =
"" );
235 Grid *createGrid (
const bool addMissingBoundaries,
bool temporary,
const std::string dgfName =
"" );
238 insertionIndex (
const typename Codim< 0 >::Entity &entity )
const
240 return Grid::getRealImplementation( entity ).getIndex();
243 insertionIndex (
const typename Codim< dimension >::Entity &entity )
const
245 return Grid::getRealImplementation( entity ).getIndex();
250 return intersection.boundarySegmentIndex();
255 return intersection.boundary() &&
256 ( insertionIndex(intersection) < numFacesInserted_ );
260 size_t globalId (
const VertexId &
id )
const
262 assert(
id < vertices_.size() );
263 return vertices_[ id ].second;
266 const VertexType &position (
const VertexId &
id )
const
268 assert(
id < vertices_.size() );
269 return vertices_[ id ].first;
272 void assertGeometryType(
const GeometryType &geometry );
273 static void generateFace (
const ElementType &element,
const int f, FaceType &face );
274 void generateFace (
const SubEntity &subEntity, FaceType &face )
const;
275 void correctElementOrientation ();
276 bool identifyFaces (
const Transformation &transformation,
const FaceType &key1,
const FaceType &key2,
const int defaultId );
277 void searchPeriodicNeighbor ( FaceMap &faceMap,
const typename FaceMap::iterator &pos,
const int defaultId );
278 void reinsertBoundary (
const FaceMap &faceMap,
const typename FaceMap::const_iterator &pos,
const int id );
279 void recreateBoundaryIds (
const int defaultId = 1 );
283 VertexVector vertices_;
284 ElementVector elements_;
285 BoundaryIdMap boundaryIds_;
286 PeriodicBoundaryVector periodicBoundaries_;
287 const DuneBoundaryProjectionType* globalProjection_ ;
288 BoundaryProjectionMap boundaryProjections_;
289 FaceTransformationVector faceTransformations_;
290 unsigned int numFacesInserted_;
292 const bool allowGridGeneration_;
294 MPICommunicatorType communicator_;
299 template<
class ALUGr
id >
300 struct ALU3dGridFactory< ALUGrid >::FaceLess
301 :
public std::binary_function< FaceType, FaceType, bool >
303 bool operator() (
const FaceType &a,
const FaceType &b )
const
305 for(
unsigned int i = 0; i < numFaceCorners; ++i )
307 if( a[ i ] != b[ i ] )
308 return (a[ i ] < b[ i ]);
315 template<
class ALUGr
id >
316 inline void ALU3dGridFactory< ALUGrid >
317 ::assertGeometryType(
const GeometryType &geometry )
319 if( elementType == tetra )
321 if( !geometry.isSimplex() )
322 DUNE_THROW( GridError,
"Only simplex geometries can be inserted into "
323 "ALUSimplexGrid< 3, 3 >." );
327 if( !geometry.isCube() )
328 DUNE_THROW( GridError,
"Only cube geometries can be inserted into "
329 "ALUCubeGrid< 3, 3 >." );
340 class GridFactory< ALUSimplexGrid< 3, dimw > >
341 :
public ALU3dGridFactory< ALUSimplexGrid< 3, dimw > >
343 typedef GridFactory< ALUSimplexGrid< 3, dimw > > ThisType;
344 typedef ALU3dGridFactory< ALUSimplexGrid< 3, dimw > > BaseType;
347 typedef typename BaseType::Grid Grid;
349 typedef typename BaseType::MPICommunicatorType MPICommunicatorType;
352 explicit GridFactory (
const MPICommunicatorType &communicator = Grid::defaultCommunicator() )
353 : BaseType( communicator )
358 const MPICommunicatorType &communicator = Grid::defaultCommunicator() )
359 : BaseType( filename, communicator )
363 template<
class,
class,
int >
friend class ALULocalGeometryStorage;
366 const MPICommunicatorType &communicator = Grid::defaultCommunicator() )
367 : BaseType( realGrid, communicator )
379 class GridFactory< ALUCubeGrid< 3, dimw > >
380 :
public ALU3dGridFactory< ALUCubeGrid< 3, dimw > >
382 typedef GridFactory< ALUCubeGrid< 3, dimw > > ThisType;
383 typedef ALU3dGridFactory< ALUCubeGrid< 3, dimw > > BaseType;
386 typedef typename BaseType::Grid Grid;
388 typedef typename BaseType::MPICommunicatorType MPICommunicatorType;
391 explicit GridFactory (
const MPICommunicatorType &communicator = Grid::defaultCommunicator() )
392 : BaseType( communicator )
397 const MPICommunicatorType &communicator = Grid::defaultCommunicator() )
398 : BaseType( filename, communicator )
402 template<
class,
class,
int >
friend class ALULocalGeometryStorage;
405 const MPICommunicatorType &communicator = Grid::defaultCommunicator() )
406 : BaseType( realGrid, communicator )
414 template<ALUGr
idElementType eltype, ALUGr
idRefinementType refinementtype ,
class Comm >
415 class GridFactory< ALUGrid< 3, 3, eltype, refinementtype, Comm > >
416 :
public ALU3dGridFactory< ALUGrid< 3, 3, eltype, refinementtype, Comm > >
418 typedef GridFactory< ALUGrid< 3, 3, eltype, refinementtype, Comm > > ThisType;
419 typedef ALU3dGridFactory< ALUGrid< 3, 3, eltype, refinementtype, Comm > > BaseType;
422 typedef typename BaseType::Grid Grid;
424 typedef typename BaseType::MPICommunicatorType MPICommunicatorType;
427 explicit GridFactory (
const MPICommunicatorType &communicator = Grid::defaultCommunicator() )
428 : BaseType( communicator )
433 const MPICommunicatorType &communicator = Grid::defaultCommunicator() )
434 : BaseType( filename, communicator )
438 template<
class,
class,
int >
friend class ALULocalGeometryStorage;
441 const MPICommunicatorType &communicator = Grid::defaultCommunicator() )
442 : BaseType( realGrid, communicator )
451 template<
class ALUGr
id >
453 ALU3dGridFactory< ALUGrid >
454 :: ALU3dGridFactory (
const MPICommunicatorType &communicator,
455 bool removeGeneratedFile )
456 : rank_( ALU3dGridCommunications< elementType, MPICommunicatorType >::getRank( communicator ) ),
457 globalProjection_ ( 0 ),
458 numFacesInserted_ ( 0 ),
460 allowGridGeneration_( rank_ == 0 ),
461 communicator_( communicator )
464 template<
class ALUGr
id >
466 ALU3dGridFactory< ALUGrid >
467 :: ALU3dGridFactory (
const std::string &filename,
468 const MPICommunicatorType &communicator )
469 : rank_( ALU3dGridCommunications< elementType, MPICommunicatorType >::getRank( communicator ) ),
470 globalProjection_ ( 0 ),
471 numFacesInserted_ ( 0 ),
473 allowGridGeneration_( rank_ == 0 ),
474 communicator_( communicator )
477 template<
class ALUGr
id >
479 ALU3dGridFactory< ALUGrid >
480 :: ALU3dGridFactory (
const bool realGrid,
481 const MPICommunicatorType &communicator )
482 : rank_( ALU3dGridCommunications< elementType, MPICommunicatorType >::getRank( communicator ) ),
483 globalProjection_ ( 0 ),
484 numFacesInserted_ ( 0 ),
485 realGrid_( realGrid ),
486 allowGridGeneration_( true ),
487 communicator_( communicator )
490 template<
class ALUGr
id >
491 inline void ALU3dGridFactory< ALUGrid > ::
492 insertBoundarySegment (
const std::vector< unsigned int >& vertices )
495 copyAndSort( vertices, faceId );
497 if( vertices.size() != numFaceCorners )
498 DUNE_THROW( GridError,
"Wrong number of face vertices passed: " << vertices.size() <<
"." );
500 if( boundaryProjections_.find( faceId ) != boundaryProjections_.end() )
501 DUNE_THROW( GridError,
"Only one boundary projection can be attached to a face." );
504 boundaryProjections_[ faceId ] = 0;
507 for(
unsigned int i = 0; i < numFaceCorners; ++i )
509 const unsigned int j = FaceTopologyMappingType::dune2aluVertex( i );
510 boundaryId.first[ j ] = vertices[ i ];
512 boundaryId.second = 1;
513 boundaryIds_.insert( boundaryId );
516 template<
class ALUGr
id >
517 inline void ALU3dGridFactory< ALUGrid > ::
518 insertBoundarySegment (
const std::vector< unsigned int >& vertices,
519 const shared_ptr<BoundarySegment<3,3> >& boundarySegment )
522 copyAndSort( vertices, faceId );
524 if( vertices.size() != numFaceCorners )
525 DUNE_THROW( GridError,
"Wrong number of face vertices passed: " << vertices.size() <<
"." );
527 if( boundaryProjections_.find( faceId ) != boundaryProjections_.end() )
528 DUNE_THROW( GridError,
"Only one boundary projection can be attached to a face." );
530 const size_t numVx = vertices.size();
533 type.makeSimplex( dimension-1 );
535 type.makeCube( dimension-1 );
539 typedef FieldVector< double, dimensionworld > CoordType;
540 std::vector< CoordType > coords( numVx );
541 for(
size_t i = 0; i < numVx; ++i )
544 assert( vertices_.size() > vertices[ i ] );
547 const VertexType &x = position( vertices[ i ] );
548 for(
unsigned int j = 0; j < dimensionworld; ++j )
549 coords[ i ][ j ] = x[ j ];
552 BoundarySegmentWrapperType* prj
553 =
new BoundarySegmentWrapperType( type, coords, boundarySegment );
554 boundaryProjections_[ faceId ] = prj;
557 for(
size_t i = 0; i < numVx; ++i )
559 CoordType global = (*prj)( coords [ i ] );
560 if( (global - coords[ i ]).two_norm() > 1e-6 )
561 DUNE_THROW(GridError,
"BoundarySegment does not map face vertices to face vertices.");
566 for(
unsigned int i = 0; i < numFaceCorners; ++i )
568 const unsigned int j = FaceTopologyMappingType::dune2aluVertex( i );
569 boundaryId.first[ j ] = vertices[ i ];
571 boundaryId.second = 1;
572 boundaryIds_.insert( boundaryId );
576 template<
class ALUGr
id >
577 inline void ALU3dGridFactory< ALUGrid >
578 ::generateFace (
const SubEntity &subEntity, FaceType &face )
const
580 generateFace( elements_[ subEntity.first ], subEntity.second, face );
587#if COMPILE_ALUGRID_INLINE
588 #include "alu3dgridfactory.cc"
Provides base classes for ALUGrid.
Fallback implementation of the std::array class (a static array)
Definition: boundaryprojection.hh:67
GridFactory()
Default constructor.
Definition: gridfactory.hh:274
@ dimension
The dimension of the grid.
Definition: grid.hh:400
GridFamily::Traits::LeafIntersection LeafIntersection
A type that is a model of Dune::Intersection, an intersections of two codimension 1 of two codimensio...
Definition: grid.hh:484
@ dimensionworld
The dimension of the world the grid lives in.
Definition: grid.hh:406
ct ctype
Define type used for coordinates in grid module.
Definition: grid.hh:546
Provide a generic factory class for unstructured grids.
GeometryType
Type representing VTK's entity geometry types.
Definition: common.hh:178
#define dune_static_assert(COND, MSG)
Helper template so that compilation fails if condition is not true.
Definition: static_assert.hh:79
#define DUNE_THROW(E, m)
Definition: exceptions.hh:244
Dune namespace.
Definition: alignment.hh:14
Helpers for dealing with MPI.