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;
65 static_assert((elementType == tetra || elementType == hexa),
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 "ALUGrid< 3, 3 , simplex, (non)conforming>." );
327 if( !geometry.isCube() )
328 DUNE_THROW( GridError,
"Only cube geometries can be inserted into "
329 "ALUGrid< 3, 3, cube, nonconforming >." );
338 template<ALUGr
idElementType eltype, ALUGr
idRefinementType refinementtype ,
class Comm >
339 class GridFactory< ALUGrid< 3, 3, eltype, refinementtype, Comm > >
340 :
public ALU3dGridFactory< ALUGrid< 3, 3, eltype, refinementtype, Comm > >
342 typedef GridFactory< ALUGrid< 3, 3, eltype, refinementtype, Comm > > ThisType;
343 typedef ALU3dGridFactory< ALUGrid< 3, 3, eltype, refinementtype, Comm > > BaseType;
346 typedef typename BaseType::Grid Grid;
348 typedef typename BaseType::MPICommunicatorType MPICommunicatorType;
351 explicit GridFactory (
const MPICommunicatorType &communicator = Grid::defaultCommunicator() )
352 : BaseType( communicator )
357 const MPICommunicatorType &communicator = Grid::defaultCommunicator() )
358 : BaseType( filename, communicator )
362 template<
class,
class,
int >
friend class ALULocalGeometryStorage;
365 const MPICommunicatorType &communicator = Grid::defaultCommunicator() )
366 : BaseType( realGrid, communicator )
375 template<
class ALUGr
id >
377 ALU3dGridFactory< ALUGrid >
378 :: ALU3dGridFactory (
const MPICommunicatorType &communicator,
379 bool removeGeneratedFile )
380 : rank_( ALU3dGridCommunications< elementType, MPICommunicatorType >::getRank( communicator ) ),
381 globalProjection_ ( 0 ),
382 numFacesInserted_ ( 0 ),
384 allowGridGeneration_( rank_ == 0 ),
385 communicator_( communicator )
388 template<
class ALUGr
id >
390 ALU3dGridFactory< ALUGrid >
391 :: ALU3dGridFactory (
const std::string &filename,
392 const MPICommunicatorType &communicator )
393 : rank_( ALU3dGridCommunications< elementType, MPICommunicatorType >::getRank( communicator ) ),
394 globalProjection_ ( 0 ),
395 numFacesInserted_ ( 0 ),
397 allowGridGeneration_( rank_ == 0 ),
398 communicator_( communicator )
401 template<
class ALUGr
id >
403 ALU3dGridFactory< ALUGrid >
404 :: ALU3dGridFactory (
const bool realGrid,
405 const MPICommunicatorType &communicator )
406 : rank_( ALU3dGridCommunications< elementType, MPICommunicatorType >::getRank( communicator ) ),
407 globalProjection_ ( 0 ),
408 numFacesInserted_ ( 0 ),
409 realGrid_( realGrid ),
410 allowGridGeneration_( true ),
411 communicator_( communicator )
414 template<
class ALUGr
id >
415 inline void ALU3dGridFactory< ALUGrid > ::
416 insertBoundarySegment (
const std::vector< unsigned int >&
vertices )
421 if(
vertices.size() != numFaceCorners )
422 DUNE_THROW( GridError,
"Wrong number of face vertices passed: " <<
vertices.size() <<
"." );
424 if( boundaryProjections_.find( faceId ) != boundaryProjections_.end() )
425 DUNE_THROW( GridError,
"Only one boundary projection can be attached to a face." );
428 boundaryProjections_[ faceId ] = 0;
431 for(
unsigned int i = 0; i < numFaceCorners; ++i )
433 const unsigned int j = FaceTopologyMappingType::dune2aluVertex( i );
434 boundaryId.first[ j ] =
vertices[ i ];
436 boundaryId.second = 1;
437 boundaryIds_.insert( boundaryId );
440 template<
class ALUGr
id >
441 inline void ALU3dGridFactory< ALUGrid > ::
442 insertBoundarySegment (
const std::vector< unsigned int >&
vertices,
443 const shared_ptr<BoundarySegment<3,3> >& boundarySegment )
448 if(
vertices.size() != numFaceCorners )
449 DUNE_THROW( GridError,
"Wrong number of face vertices passed: " <<
vertices.size() <<
"." );
451 if( boundaryProjections_.find( faceId ) != boundaryProjections_.end() )
452 DUNE_THROW( GridError,
"Only one boundary projection can be attached to a face." );
454 const size_t numVx =
vertices.size();
457 type.makeSimplex( dimension-1 );
459 type.makeCube( dimension-1 );
463 typedef FieldVector< double, dimensionworld > CoordType;
464 std::vector< CoordType > coords( numVx );
465 for(
size_t i = 0; i < numVx; ++i )
468 assert( vertices_.size() >
vertices[ i ] );
471 const VertexType &x = position(
vertices[ i ] );
472 for(
unsigned int j = 0; j < dimensionworld; ++j )
473 coords[ i ][ j ] = x[ j ];
476 BoundarySegmentWrapperType* prj
477 =
new BoundarySegmentWrapperType( type, coords, boundarySegment );
478 boundaryProjections_[ faceId ] = prj;
481 for(
size_t i = 0; i < numVx; ++i )
483 CoordType global = (*prj)( coords [ i ] );
484 if( (global - coords[ i ]).two_norm() > 1e-6 )
485 DUNE_THROW(GridError,
"BoundarySegment does not map face vertices to face vertices.");
490 for(
unsigned int i = 0; i < numFaceCorners; ++i )
492 const unsigned int j = FaceTopologyMappingType::dune2aluVertex( i );
493 boundaryId.first[ j ] =
vertices[ i ];
495 boundaryId.second = 1;
496 boundaryIds_.insert( boundaryId );
500 template<
class ALUGr
id >
501 inline void ALU3dGridFactory< ALUGrid >
502 ::generateFace (
const SubEntity &subEntity, FaceType &face )
const
504 generateFace( elements_[ subEntity.first ], subEntity.second, face );
511#if COMPILE_ALUGRID_INLINE
512 #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
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:486
@ dimensionworld
The dimension of the world the grid lives in.
Definition: grid.hh:408
@ dimension
The dimension of the grid.
Definition: grid.hh:402
ct ctype
Define type used for coordinates in grid module.
Definition: grid.hh:548
Provide a generic factory class for unstructured grids.
GeometryType
Type representing VTK's entity geometry types.
Definition: common.hh:178
#define DUNE_THROW(E, m)
Definition: exceptions.hh:243
IteratorRange<... > vertices(const GV &gv)
Iterates over all vertices (entities with dimension 0) of a GridView.
Helpers for dealing with MPI.
Dune namespace.
Definition: alignment.hh:10