1#ifndef DUNE_ALU3DGRID_FACTORY_HH
2#define DUNE_ALU3DGRID_FACTORY_HH
8#include <dune/common/shared_ptr.hh>
9#include <dune/common/parallel/mpihelper.hh>
10#include <dune/common/version.hh>
12#include <dune/geometry/referenceelements.hh>
14#include <dune/grid/common/gridfactory.hh>
15#include <dune/grid/common/boundaryprojection.hh>
25 template<
class ALUGr
id >
27 :
public GridFactoryInterface< ALUGrid >
30 typedef GridFactoryInterface< ALUGrid > BaseType;
51#if !DUNE_VERSION_NEWER(DUNE_GRID,3,0)
68 "ALU3dGridFactory supports only grids containing "
69 "tetrahedrons or hexahedrons exclusively." );
71 typedef Dune::BoundarySegmentWrapper< dimension, dimensionworld > BoundarySegmentWrapperType;
77 static const unsigned int boundaryId2d = 87;
83 typedef FieldVector< ctype, dimensionworld > VertexInputType;
87 typedef FieldVector< ctype, 3 > VertexType;
89 typedef std::vector< unsigned int > ElementType;
90 typedef std::array< unsigned int, numFaceCorners > FaceType;
94 typedef std::vector< std::pair< VertexType, GlobalIdType > > VertexVector;
95 typedef std::vector< ElementType > ElementVector;
96 typedef std::pair< FaceType, int > BndPair ;
97 typedef std::map< FaceType, int > BoundaryIdMap;
98 typedef std::vector< std::pair< BndPair, BndPair > > PeriodicBoundaryVector;
99 typedef std::pair< unsigned int, int > SubEntity;
100 typedef std::map< FaceType, SubEntity, FaceLess > FaceMap;
102 typedef std::map< FaceType, const DuneBoundaryProjectionType* > BoundaryProjectionMap;
103 typedef std::vector< const DuneBoundaryProjectionType* > BoundaryProjectionVector;
105 typedef std::vector< Transformation > FaceTransformationVector;
107 static void copy (
const std::initializer_list< unsigned int > &vertices, FaceType &faceId )
109 std::copy_n( vertices.begin(), faceId.size(), faceId.begin() );
112 static FaceType makeFace (
const std::vector< unsigned int > &vertices )
114 if( vertices.size() != (
dimension == 2 ? 2 : numFaceCorners) )
115 DUNE_THROW( GridError,
"Wrong number of face vertices passed: " << vertices.size() <<
"." );
121 copy( { 0, vertices[ 1 ]+1, vertices[ 0 ]+1 }, faceId );
123 copy( { 2*vertices[ 0 ], 2*vertices[ 1 ], 2*vertices[ 0 ]+1, 2*vertices[ 1 ]+1 }, faceId );
126 std::copy_n( vertices.begin(), numFaceCorners, faceId.begin() );
130 static BndPair makeBndPair (
const FaceType &face,
int id )
133 for(
unsigned int i = 0; i < numFaceCorners; ++i )
135 const unsigned int j = FaceTopologyMappingType::dune2aluVertex( i );
136 bndPair.first[ j ] = face[ i ];
144 virtual Grid* createGridObj( BoundaryProjectionVector* bndProjections,
const std::string& name )
const
146 return new Grid( communicator_, globalProjection_, bndProjections , name, realGrid_ );
156 bool removeGeneratedFile =
true );
169 virtual void insertVertex (
const VertexInputType &pos );
188 const std::vector< VertexId > &vertices );
202 insertBoundary (
const GeometryType &geometry,
const std::vector< VertexId > &faceVertices,
int boundaryId = 1 );
212 virtual void insertBoundary (
int element,
int face,
int boundaryId = 1 );
230 const std::vector< VertexId > &vertices,
250 const shared_ptr<BoundarySegment<dimension,dimensionworld> >& boundarySegment ) ;
275 Grid *
createGrid (
const bool addMissingBoundaries,
const std::string dgfName =
"" );
277 Grid *
createGrid (
const bool addMissingBoundaries,
bool temporary,
const std::string dgfName =
"" );
282 alugrid_assert( Grid::getRealImplementation( entity ).getIndex() <
int(ordering_.size()) );
283 return ordering_[ Grid::getRealImplementation( entity ).getIndex() ];
291 return Grid::getRealImplementation( entity ).getIndex()/2;
294 return Grid::getRealImplementation( entity ).getIndex() - 1;
296 return Grid::getRealImplementation( entity ).getIndex();
302 std::vector< unsigned int > vertices;
303#if DUNE_VERSION_NEWER(DUNE_GRID,2,4)
310 const Dune::ReferenceElement< double, dimension > &refElem =
311 Dune::ReferenceElements< double, dimension >::general( in.type() );
312 int faceNr = intersection.indexInInside();
313 const int vxSize = refElem.size( faceNr, 1,
dimension );
314 for (
int i=0;i<vxSize;++i)
316 int vxIdx = refElem.subEntity( faceNr, 1 , i ,
dimension);
317#if DUNE_VERSION_NEWER(DUNE_GRID,2,4)
318 vertices.push_back(
insertionIndex( in.template subEntity<dimension>(vxIdx) ) );
320 vertices.push_back(
insertionIndex( *(in.template subEntity<dimension>(vxIdx) ) ) );
324 FaceType faceId = makeFace( vertices );
325 std::sort( faceId.begin(), faceId.end() );
327 typename BoundaryIdMap::const_iterator pos = insertionOrder_.find( faceId );
328 if( pos != insertionOrder_.end() )
331 return std::numeric_limits<unsigned int>::max();
335 wasInserted (
const typename Grid::LeafIntersection &intersection )
const
337 return intersection.boundary() &&
338 (
insertionIndex(intersection) < std::numeric_limits<unsigned int>::max());
341 const std::vector<unsigned int>&
ordering ()
const {
return ordering_; }
344 void doInsertVertex (
const VertexInputType &pos,
const GlobalIdType globalId );
345 void doInsertBoundary (
int element,
int face,
int boundaryId );
350 return vertices_[ id ].second;
353 const VertexType &position (
const VertexId &
id )
const
356 return vertices_[ id ].first;
359 const VertexInputType inputPosition (
const VertexId &
id )
const
362 VertexType vertex = vertices_[ id ].first;
363 VertexInputType iVtx(0.);
369 void assertGeometryType(
const GeometryType &geometry );
370 static void generateFace (
const ElementType &element,
const int f, FaceType &face );
371 void generateFace (
const SubEntity &subEntity, FaceType &face )
const;
372 void correctElementOrientation ();
373 bool identifyFaces (
const Transformation &transformation,
const FaceType &key1,
const FaceType &key2,
const int defaultId );
374 void searchPeriodicNeighbor ( FaceMap &faceMap,
const typename FaceMap::iterator &pos,
const int defaultId );
375 void reinsertBoundary (
const FaceMap &faceMap,
const typename FaceMap::const_iterator &pos,
const int id );
376 void recreateBoundaryIds (
const int defaultId = 1 );
379 void sortElements(
const VertexVector& vertices,
const ElementVector& elements, std::vector< unsigned int >&
ordering );
383 VertexVector vertices_;
384 ElementVector elements_;
385 BoundaryIdMap boundaryIds_,insertionOrder_;
386 PeriodicBoundaryVector periodicBoundaries_;
388 BoundaryProjectionMap boundaryProjections_;
389 FaceTransformationVector faceTransformations_;
390 unsigned int numFacesInserted_;
392 const bool allowGridGeneration_;
393 bool foundGlobalIndex_ ;
397 typename SpaceFillingCurveOrderingType :: CurveType curveType_;
398 std::vector< unsigned int > ordering_;
403 template<
class ALUGr
id >
405 :
public std::binary_function< FaceType, FaceType, bool >
407 bool operator() (
const FaceType &a,
const FaceType &b )
const
409 for(
unsigned int i = 0; i < numFaceCorners; ++i )
411 if( a[ i ] != b[ i ] )
412 return (a[ i ] < b[ i ]);
419 template<
class ALUGr
id >
423 if( elementType ==
tetra )
425 if( !geometry.isSimplex() )
426 DUNE_THROW( GridError,
"Only simplex geometries can be inserted into "
427 "ALUGrid< 3, 3, simplex, refrule >." );
431 if( !geometry.isCube() )
432 DUNE_THROW( GridError,
"Only cube geometries can be inserted into "
433 "ALUGrid< 3, 3, cube, refrule >." );
440 template<
int dim,
int dimw, ALUGr
idElementType eltype, ALUGr
idRefinementType refinementtype ,
class Comm >
441 class GridFactory<
ALUGrid< dim, dimw, eltype, refinementtype, Comm > >
442 :
public ALU3dGridFactory< ALUGrid< dim, dimw, eltype, refinementtype, Comm > >
444 typedef GridFactory< ALUGrid< dim, dimw, eltype, refinementtype, Comm > >
ThisType;
460 :
BaseType( filename, communicator )
464 template<
class Gr
id >
468 template<
int dim,
int dimw, ALUGr
idElementType eltype, ALUGr
idRefinementType refinementtype ,
class Comm >
470 :
public ALU3dGridFactory< ALUGrid< dim, dimw, eltype, refinementtype, Comm > >
491 template<
class ALUGr
id >
493 ALU3dGridFactory< ALUGrid >
495 bool removeGeneratedFile )
497 globalProjection_ ( 0 ),
498 numFacesInserted_ ( 0 ),
500 allowGridGeneration_( rank_ == 0 ),
501 foundGlobalIndex_( false ),
502 communicator_( communicator ),
503 curveType_( SpaceFillingCurveOrderingType :: DefaultCurve )
506 template<
class ALUGr
id >
512 globalProjection_ ( 0 ),
513 numFacesInserted_ ( 0 ),
515 allowGridGeneration_( rank_ == 0 ),
516 foundGlobalIndex_( false ),
517 communicator_( communicator ),
518 curveType_( SpaceFillingCurveOrderingType :: DefaultCurve )
521 template<
class ALUGr
id >
527 globalProjection_ ( 0 ),
528 numFacesInserted_ ( 0 ),
529 realGrid_( realGrid ),
530 allowGridGeneration_( true ),
531 foundGlobalIndex_( false ),
532 communicator_( communicator ),
533 curveType_( SpaceFillingCurveOrderingType :: DefaultCurve )
536 template<
class ALUGr
id >
540 FaceType faceId = makeFace( vertices );
542 boundaryIds_.insert( makeBndPair( faceId, 1 ) );
544 std::sort( faceId.begin(), faceId.end() );
545 if( boundaryProjections_.find( faceId ) != boundaryProjections_.end() )
546 DUNE_THROW( GridError,
"Only one boundary projection can be attached to a face." );
548 boundaryProjections_[ faceId ] =
nullptr;
550 insertionOrder_.insert( std::make_pair( faceId, insertionOrder_.size() ) );
553 template<
class ALUGr
id >
557 FaceType faceId = makeFace( vertices );
559 boundaryIds_.insert( makeBndPair( faceId,
ALU3DSPACE ProcessorBoundary_t ) );
561 std::sort( faceId.begin(), faceId.end() );
562 boundaryProjections_[ faceId ] =
nullptr;
565 template<
class ALUGr
id >
568 const shared_ptr<BoundarySegment<dimension,dimensionworld> >& boundarySegment )
570 FaceType faceId = makeFace( vertices );
572 const size_t numVx = vertices.size();
574 if( elementType ==
tetra )
575 type.makeSimplex( dimension-1 );
577 type.makeCube( dimension-1 );
581 typedef FieldVector< double, dimensionworld > CoordType;
582 std::vector< CoordType > coords( numVx );
583 for(
size_t i = 0; i < numVx; ++i )
589 const VertexType &x = position( vertices[ i ] );
590 for(
unsigned int j = 0; j < dimensionworld; ++j )
591 coords[ i ][ j ] = x[ j ];
594 boundaryIds_.insert( makeBndPair( faceId, 1 ) );
596 std::sort( faceId.begin(), faceId.end() );
597 if( boundaryProjections_.find( faceId ) != boundaryProjections_.end() )
598 DUNE_THROW( GridError,
"Only one boundary projection can be attached to a face." );
600 BoundarySegmentWrapperType* prj
601 =
new BoundarySegmentWrapperType( type, coords, boundarySegment );
602 boundaryProjections_[ faceId ] = prj;
605 for(
size_t i = 0; i < numVx; ++i )
607 CoordType global = (*prj)( coords [ i ] );
608 if( (global - coords[ i ]).two_norm() > 1e-6 )
609 DUNE_THROW(GridError,
"BoundarySegment does not map face vertices to face vertices.");
613 insertionOrder_.insert( std::make_pair( faceId, insertionOrder_.size() ) );
617 template<
class ALUGr
id >
619 ::generateFace (
const SubEntity &subEntity, FaceType &face )
const
621 generateFace( elements_[ subEntity.first ], subEntity.second, face );
626#if COMPILE_ALUGRID_INLINE
#define ALU3DSPACE
Definition alu3dinclude.hh:24
Provides base classes for ALUGrid.
#define alugrid_assert(EX)
Definition alugrid_assert.hh:20
Definition alu3dinclude.hh:50
Definition alu3dinclude.hh:80
ALU3dGridElementType
Definition topology.hh:12
@ hexa
Definition topology.hh:12
@ tetra
Definition topology.hh:12
BaseType::ctype ctype
Definition alugrid.hh:48
@ dimension
Definition alugrid.hh:47
@ dimensionworld
Definition alugrid.hh:47
Factory class for ALUGrids.
Definition gridfactory.hh:28
unsigned int VertexId
Definition gridfactory.hh:56
static const unsigned int dimensionworld
Definition gridfactory.hh:40
virtual void insertVertex(const VertexInputType &pos)
insert a vertex into the coarse grid
Definition gridfactory.cc:38
Grid::MPICommunicatorType MPICommunicatorType
Definition gridfactory.hh:42
virtual unsigned int insertionIndex(const typename Codim< 0 >::Entity &entity) const
Definition gridfactory.hh:280
virtual bool wasInserted(const typename Grid::LeafIntersection &intersection) const
Definition gridfactory.hh:335
virtual void insertBoundarySegment(const std::vector< VertexId > &vertices, const shared_ptr< BoundarySegment< dimension, dimensionworld > > &boundarySegment)
insert a shaped boundary segment into the macro grid
Definition gridfactory.hh:567
void insertFaceTransformation(const WorldMatrix &matrix, const WorldVector &shift)
add a face transformation (for periodic identification)
Definition gridfactory.cc:233
virtual unsigned int insertionIndex(const typename Codim< dimension >::Entity &entity) const
Definition gridfactory.hh:287
ALU3dGridFactory(const MPICommunicatorType &communicator=Grid::defaultCommunicator(), bool removeGeneratedFile=true)
default constructor
Definition gridfactory.hh:494
ALUGrid Grid
Definition gridfactory.hh:33
const std::vector< unsigned int > & ordering() const
Definition gridfactory.hh:341
Transformation::WorldMatrix WorldMatrix
type of matrix from world coordinates to world coordinates
Definition gridfactory.hh:64
virtual void insertBoundarySegment(const std::vector< VertexId > &vertices)
insert a boundary segment into the macro grid
Definition gridfactory.hh:538
ALUGridTransformation< ctype, dimensionworld > Transformation
Definition gridfactory.hh:59
virtual void insertBoundary(const GeometryType &geometry, const std::vector< VertexId > &faceVertices, int boundaryId=1)
insert a boundary element into the coarse grid
Definition gridfactory.cc:151
void insertProcessBorder(int element, int face)
Definition gridfactory.hh:215
DuneBoundaryProjection< dimensionworld > DuneBoundaryProjectionType
type of boundary projection class
Definition gridfactory.hh:45
Transformation::WorldVector WorldVector
type of vector for world coordinates
Definition gridfactory.hh:62
virtual void insertProcessBorder(const std::vector< VertexId > &vertices)
Definition gridfactory.hh:555
ALU3dGridFactory(const std::string &filename, const MPICommunicatorType &communicator=Grid::defaultCommunicator())
constructor taking filename for temporary outfile
Definition gridfactory.hh:509
static const unsigned int dimension
Definition gridfactory.hh:39
virtual void insertElement(const GeometryType &geometry, const std::vector< VertexId > &vertices)
insert an element into the coarse grid
Definition gridfactory.cc:102
Grid * createGrid()
finalize the grid creation and hand over the grid
Definition gridfactory.cc:332
virtual void insertBoundaryProjection(const GeometryType &type, const std::vector< VertexId > &vertices, const DuneBoundaryProjectionType *projection)
insert a boundary projection into the macro grid
Definition gridfactory.cc:213
unsigned int GlobalIdType
Definition gridfactory.hh:57
virtual ~ALU3dGridFactory()
Destructor.
Definition gridfactory.cc:32
Grid::ctype ctype
Definition gridfactory.hh:35
ALU3dGridFactory(const bool verbose, const MPICommunicatorType &communicator)
constructor taking verbose flag
Definition gridfactory.hh:524
static const ALU3dGridElementType elementType
Definition gridfactory.hh:37
virtual unsigned int insertionIndex(const typename Grid::LeafIntersection &intersection) const
Definition gridfactory.hh:300
Definition 3d/grid.hh:137
Definition gridfactory.hh:49
Grid::template Codim< codim >::EntityPointer EntityPointer
Definition gridfactory.hh:52
Grid::template Codim< codim >::Entity Entity
Definition gridfactory.hh:50
Definition gridfactory.hh:406
Specialization of the generic GridFactory for ALUGrid.
Definition gridfactory.hh:443
GridFactory(const MPICommunicatorType &communicator=Grid::defaultCommunicator())
Default constructor.
Definition gridfactory.hh:453
GridFactory(const std::string &filename, const MPICommunicatorType &communicator=Grid::defaultCommunicator())
constructor taking filename
Definition gridfactory.hh:458
BaseType::Grid Grid
Definition gridfactory.hh:448
BaseType::MPICommunicatorType MPICommunicatorType
Definition gridfactory.hh:450
Definition gridfactory.hh:465
Definition gridfactory.hh:471
BaseType::MPICommunicatorType MPICommunicatorType
Definition gridfactory.hh:478
ReferenceGridFactory()
Default constructor.
Definition gridfactory.hh:481
BaseType::Grid Grid
Definition gridfactory.hh:476
Definition topology.hh:15
Definition topology.hh:40
Definition topology.hh:151
Definition transformation.hh:12
FieldMatrix< ctype, dimension, dimension > WorldMatrix
Definition transformation.hh:16
FieldVector< ctype, dimension > WorldVector
Definition transformation.hh:15