1#ifndef DUNE_ALU3DGRID_FACTORY_HH
2#define DUNE_ALU3DGRID_FACTORY_HH
12#include <dune/geometry/referenceelements.hh>
15#include <dune/grid/common/boundaryprojection.hh>
17#include <dune/alugrid/common/transformation.hh>
20#include <dune/alugrid/common/hsfc.hh>
25 template<
class ALUGr
id >
35 typedef typename Grid::ctype ctype;
37 static const ALU3dGridElementType elementType = Grid::elementType;
39 static const unsigned int dimension = Grid::dimension;
40 static const unsigned int dimensionworld = Grid::dimensionworld;
42 typedef typename Grid::MPICommunicatorType MPICommunicatorType;
47 typedef typename Grid::template Codim< codim >::Entity
Entity;
50 typedef unsigned int VertexId;
51 typedef unsigned int GlobalIdType;
53 typedef ALUGridTransformation< ctype, dimensionworld > Transformation;
63 typedef decltype(std::declval< Dune::GridFactoryInterface< Grid >* >()->
createGrid()) GridPtrType;
66 static_assert ( (elementType == tetra || elementType == hexa),
67 "ALU3dGridFactory supports only grids containing "
68 "tetrahedrons or hexahedrons exclusively." );
76 static const unsigned int numCorners = EntityCount< elementType >::numVertices;
77 static const unsigned int numFaces = EntityCount< elementType >::numFaces;
78 static const unsigned int numFaceCorners = EntityCount< elementType >::numVerticesPerFace;
85 typedef SpaceFillingCurveOrdering< VertexInputType > SpaceFillingCurveOrderingType;
90 typedef std::vector< unsigned int > ElementType;
91 typedef std::array< unsigned int, numFaceCorners > FaceType;
95 typedef std::vector< std::pair< VertexType, GlobalIdType > > VertexVector;
96 typedef std::vector< ElementType > ElementVector;
97 typedef std::pair< FaceType, int > BndPair ;
98 typedef std::map < FaceType, int > BoundaryIdMap;
99 typedef std::vector< std::pair< BndPair, BndPair > > PeriodicBoundaryVector;
100 typedef std::pair< unsigned int, int > SubEntity;
101 typedef std::map< FaceType, SubEntity, FaceLess > FaceMap;
103 typedef std::map< FaceType, const DuneBoundaryProjectionType* > BoundaryProjectionMap;
104 typedef std::vector< const DuneBoundaryProjectionType* > BoundaryProjectionVector;
106 typedef std::vector< Transformation > FaceTransformationVector;
108 static void copy (
const std::initializer_list< unsigned int > &vertices, FaceType &faceId )
110 std::copy_n( vertices.begin(), faceId.size(), faceId.begin() );
113 static FaceType makeFace (
const std::vector< unsigned int > &vertices )
115 if( vertices.size() != (dimension == 2 ? 2 : numFaceCorners) )
116 DUNE_THROW(
GridError,
"Wrong number of face vertices passed: " << vertices.size() <<
"." );
121 if( elementType == tetra )
122 copy( { 0, vertices[ 1 ]+1, vertices[ 0 ]+1 }, faceId );
123 else if( elementType == hexa )
124 copy( { 2*vertices[ 0 ], 2*vertices[ 1 ], 2*vertices[ 0 ]+1, 2*vertices[ 1 ]+1 }, faceId );
126 else if( dimension == 3 )
127 std::copy_n( vertices.begin(), numFaceCorners, faceId.begin() );
131 static BndPair makeBndPair (
const FaceType &face,
const int id )
134 for(
unsigned int i = 0; i < numFaceCorners; ++i )
137 bndPair.first[ j ] = face[ i ];
143 void markLongestEdge( std::vector< bool >& elementOrientation,
const bool resortElements =
true ) ;
144 void markLongestEdge();
148 virtual Grid* createGridObj(
const std::string& name )
const
150 ALU3DSPACE ProjectVertexPtrPair pv = std::make_pair( globalProjection_, surfaceProjection_ );
151 return new Grid( communicator_, pv, name, realGrid_ );
160 explicit ALU3dGridFactory (
const MPICommunicatorType &communicator = Grid::defaultCommunicator(),
161 bool removeGeneratedFile =
true );
165 const MPICommunicatorType &communicator = Grid::defaultCommunicator() );
193 const std::vector< VertexId > &vertices );
217 virtual void insertBoundary (
int element,
int face,
int boundaryId = 1 );
220 void insertProcessBorder (
int element,
int face )
235 const std::vector< VertexId > &vertices,
236 const DuneBoundaryProjectionType *projection );
246 insertProcessBorder (
const std::vector< VertexId >& vertices );
280 GridPtrType
createGrid (
const bool addMissingBoundaries,
const std::string dgfName =
"" );
282 GridPtrType
createGrid (
const bool addMissingBoundaries,
bool temporary,
const std::string dgfName =
"" );
287 alugrid_assert( entity.impl().getIndex() <
int(ordering_.size()) );
288 return ordering_[ entity.impl().getIndex() ];
294 if(dimension == 2 && elementType == hexa )
296 return entity.impl().getIndex()/2;
297 else if ( dimension == 2 && elementType == tetra )
299 return entity.impl().getIndex() - 1;
301 return entity.impl().getIndex();
306 return boundaryInsertionIndex( intersection.inside(), intersection.indexInInside() );
311 return boundaryInsertionIndex( intersection.inside(), intersection.indexInInside() );
324 const std::vector<unsigned int>& ordering ()
const {
return ordering_; }
336 return Communication(communicator_);
340 unsigned int boundaryInsertionIndex (
const typename Codim< 0 >::Entity &entity,
int face )
const
343 const int vxSize = refElem.size( face, 1, dimension );
344 std::vector< unsigned int > vertices( vxSize );
345 for(
int i = 0; i < vxSize; ++i )
346 vertices[ i ] =
insertionIndex( entity.template subEntity< dimension >( refElem.subEntity( face, 1, i, dimension ) ) );
348 FaceType faceId = makeFace( vertices );
349 std::sort( faceId.begin(), faceId.end() );
351 const auto pos = insertionOrder_.find( faceId );
355 void doInsertVertex (
const VertexInputType &pos,
const GlobalIdType globalId );
356 void doInsertBoundary (
int element,
int face,
int boundaryId );
358 GlobalIdType globalId (
const VertexId &
id )
const
360 alugrid_assert (
id < vertices_.size() );
361 return vertices_[ id ].second;
364 const VertexType &position (
const VertexId &
id )
const
366 alugrid_assert (
id < vertices_.size() );
367 return vertices_[ id ].first;
370 const VertexInputType inputPosition (
const VertexId &
id )
const
372 alugrid_assert (
id < vertices_.size() );
373 VertexType
vertex = vertices_[ id ].first;
374 VertexInputType iVtx(0.);
375 for(
unsigned i = 0 ; i < dimensionworld ; ++i)
380 void assertGeometryType(
const GeometryType &geometry );
381 static void generateFace (
const ElementType &element,
const int f, FaceType &face );
382 void generateFace (
const SubEntity &subEntity, FaceType &face )
const;
383 void correctElementOrientation ();
384 bool identifyFaces (
const Transformation &transformation,
const FaceType &key1,
const FaceType &key2,
const int defaultId );
385 void searchPeriodicNeighbor ( FaceMap &faceMap,
typename FaceMap::iterator &pos,
const int defaultId );
386 void reinsertBoundary (
const FaceMap &faceMap,
const typename FaceMap::const_iterator &pos,
const int id );
387 void recreateBoundaryIds (
const int defaultId = 1 );
390 void sortElements(
const VertexVector& vertices,
const ElementVector& elements, std::vector< unsigned int >& ordering );
394 VertexVector vertices_;
395 ElementVector elements_;
396 BoundaryIdMap boundaryIds_,insertionOrder_;
397 PeriodicBoundaryVector periodicBoundaries_;
398 ALU3DSPACE ProjectVertexPtr globalProjection_ ;
399 ALU3DSPACE ProjectVertexPtr surfaceProjection_ ;
400 BoundaryProjectionMap boundaryProjections_;
401 FaceTransformationVector faceTransformations_;
402 unsigned int numFacesInserted_;
404 const bool allowGridGeneration_;
405 bool foundGlobalIndex_ ;
407 MPICommunicatorType communicator_;
409 typename SpaceFillingCurveOrderingType :: CurveType curveType_;
410 std::vector< unsigned int > ordering_;
412 bool markLongestEdge_;
417 template<
class ALUGr
id >
418 struct ALU3dGridFactory< ALUGrid >::FaceLess
420 bool operator() (
const FaceType &a,
const FaceType &b )
const
422 for(
unsigned int i = 0; i < numFaceCorners; ++i )
424 if( a[ i ] != b[ i ] )
425 return (a[ i ] < b[ i ]);
432 template<
class ALUGr
id >
433 inline void ALU3dGridFactory< ALUGrid >
434 ::assertGeometryType(
const GeometryType &geometry )
436 if( elementType == tetra )
438 if( !geometry.isSimplex() )
439 DUNE_THROW( GridError,
"Only simplex geometries can be inserted into "
440 "ALUGrid< 3, 3, simplex, refrule >." << geometry );
444 if( !geometry.isCube() )
445 DUNE_THROW( GridError,
"Only cube geometries can be inserted into "
446 "ALUGrid< 3, 3, cube, refrule >." );
453 template<
int dim,
int dimw, ALUGr
idElementType eltype, ALUGr
idRefinementType refinementtype ,
class Comm >
455 :
public ALU3dGridFactory< ALUGrid< dim, dimw, eltype, refinementtype, Comm > >
463 typedef typename BaseType::MPICommunicatorType MPICommunicatorType;
466 explicit GridFactory (
const MPICommunicatorType &communicator = Grid::defaultCommunicator() )
471 template <
class MPIComm>
478 const MPICommunicatorType &communicator = Grid::defaultCommunicator() )
479 :
BaseType( filename, communicator )
483 template <
class MPIComm>
490 template<
class Gr
id >
491 class ReferenceGridFactory;
494 template<
int dim,
int dimw, ALUGr
idElementType eltype, ALUGr
idRefinementType refinementtype ,
class Comm >
495 class ReferenceGridFactory< ALUGrid< dim, dimw, eltype, refinementtype, Comm > >
496 :
public ALU3dGridFactory< ALUGrid< dim, dimw, eltype, refinementtype, Comm > >
498 typedef ReferenceGridFactory< ALUGrid< dim, dimw, eltype, refinementtype, Comm > > ThisType;
499 typedef ALU3dGridFactory< ALUGrid< dim, dimw, eltype, refinementtype, Comm > > BaseType;
502 typedef typename BaseType::Grid Grid;
504 typedef typename BaseType::MPICommunicatorType MPICommunicatorType;
507 ReferenceGridFactory()
508 : BaseType(false, Grid::defaultCommunicator() )
517 template<
class ALUGr
id >
521 bool removeGeneratedFile )
522 : rank_( ALU3dGridCommunications<
ALUGrid::dimension,
ALUGrid::dimensionworld, elementType, MPICommunicatorType >::getRank( communicator ) ),
523 globalProjection_ ( 0 ),
524 surfaceProjection_ ( 0 ),
525 numFacesInserted_ ( 0 ),
527 allowGridGeneration_( rank_ == 0 ),
528 foundGlobalIndex_( false ),
529 communicator_( communicator ),
530 curveType_( SpaceFillingCurveOrderingType :: DefaultCurve ),
531 markLongestEdge_(
ALUGrid::dimension == 2 )
533 BoundarySegmentWrapperType::registerFactory();
534 ALUProjectionType::registerFactory();
537 template<
class ALUGr
id >
541 const MPICommunicatorType &communicator )
542 : rank_( ALU3dGridCommunications<
ALUGrid::dimension,
ALUGrid::dimensionworld, elementType, MPICommunicatorType >::getRank( communicator ) ),
543 globalProjection_ ( 0 ),
544 surfaceProjection_ ( 0 ),
545 numFacesInserted_ ( 0 ),
547 allowGridGeneration_( rank_ == 0 ),
548 foundGlobalIndex_( false ),
549 communicator_( communicator ),
550 curveType_( SpaceFillingCurveOrderingType :: DefaultCurve ),
551 markLongestEdge_(
ALUGrid::dimension == 2 )
553 BoundarySegmentWrapperType::registerFactory();
554 ALUProjectionType::registerFactory();
557 template<
class ALUGr
id >
561 const MPICommunicatorType &communicator )
562 : rank_( ALU3dGridCommunications<
ALUGrid::dimension,
ALUGrid::dimensionworld, elementType, MPICommunicatorType >::getRank( communicator ) ),
563 globalProjection_ ( 0 ),
564 surfaceProjection_ ( 0 ),
565 numFacesInserted_ ( 0 ),
566 realGrid_( realGrid ),
567 allowGridGeneration_( true ),
568 foundGlobalIndex_( false ),
569 communicator_( communicator ),
570 curveType_( SpaceFillingCurveOrderingType :: DefaultCurve ),
571 markLongestEdge_(
ALUGrid::dimension == 2 )
573 BoundarySegmentWrapperType::registerFactory();
574 ALUProjectionType::registerFactory();
577 template<
class ALUGr
id >
581 FaceType faceId = makeFace( vertices );
583 boundaryIds_.insert( makeBndPair( faceId, 1 ) );
585 std::sort( faceId.begin(), faceId.end() );
586 if( boundaryProjections_.find( faceId ) != boundaryProjections_.end() )
589 boundaryProjections_[ faceId ] =
nullptr;
591 insertionOrder_.insert( std::make_pair( faceId, insertionOrder_.size() ) );
594 template<
class ALUGr
id >
598 FaceType faceId = makeFace( vertices );
600 boundaryIds_.insert( makeBndPair( faceId, ALU3DSPACE ProcessorBoundary_t ) );
602 std::sort( faceId.begin(), faceId.end() );
603 boundaryProjections_[ faceId ] =
nullptr;
606 template<
class ALUGr
id >
611 const std::size_t numVx = vertices.size();
620 std::vector< CoordType > coords( numVx );
621 for( std::size_t i = 0; i < numVx; ++i )
624 const std::size_t vtx = (dimension == 2 ? (elementType == tetra ? vertices[ i ] + 1 : 2 * vertices[ i ]) : vertices[ i ]);
627 alugrid_assert ( vertices_.size() > vtx );
630 std::copy_n( position( vtx ).begin(), dimensionworld, coords[ i ].begin() );
636 for( std::size_t i = 0; i < numVx; ++i )
638 CoordType global = (*prj)( coords [ i ] );
639 if( (global - coords[ i ]).two_norm() > 1e-6 )
643 FaceType faceId = makeFace( vertices );
645 boundaryIds_.insert( makeBndPair( faceId, 1 ) );
647 std::sort( faceId.begin(), faceId.end() );
648 if( boundaryProjections_.find( faceId ) != boundaryProjections_.end() )
651 boundaryProjections_[ faceId ] = prj.release();
653 insertionOrder_.insert( std::make_pair( faceId, insertionOrder_.size() ) );
657 template<
class ALUGr
id >
659 ::generateFace (
const SubEntity &subEntity, FaceType &face )
const
661 generateFace( elements_[ subEntity.first ], subEntity.second, face );
666#if COMPILE_ALUGRID_INLINE
667 #include "gridfactory.cc"
Provides base classes for ALUGrid.
Factory class for ALUGrids.
Definition: gridfactory.hh:28
Communication comm() const
Return the Communication used by the grid factory.
Definition: gridfactory.hh:334
GridPtrType createGrid()
finalize the grid creation and hand over the grid
Definition: gridfactory.cc:476
virtual void insertVertex(const VertexInputType &pos)
insert a vertex into the coarse grid
Definition: gridfactory.cc:34
virtual unsigned int insertionIndex(const typename Codim< 0 >::Entity &entity) const
obtain an element's insertion index
Definition: gridfactory.hh:285
void insertFaceTransformation(const WorldMatrix &matrix, const WorldVector &shift)
add a face transformation (for periodic identification)
Definition: gridfactory.cc:242
virtual unsigned int insertionIndex(const typename Codim< dimension >::Entity &entity) const
obtain a vertex' insertion index
Definition: gridfactory.hh:292
ALU3dGridFactory(const MPICommunicatorType &communicator=Grid::defaultCommunicator(), bool removeGeneratedFile=true)
default constructor
Definition: gridfactory.hh:520
Transformation::WorldMatrix WorldMatrix
type of matrix from world coordinates to world coordinates
Definition: gridfactory.hh:58
virtual void insertBoundarySegment(const std::vector< VertexId > &vertices)
insert a boundary segment into the macro grid
Definition: gridfactory.hh:579
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:148
Transformation::WorldVector WorldVector
type of vector for world coordinates
Definition: gridfactory.hh:56
ALU3dGridFactory(const std::string &filename, const MPICommunicatorType &communicator=Grid::defaultCommunicator())
constructor taking filename for temporary outfile
Definition: gridfactory.hh:540
virtual void insertElement(const GeometryType &geometry, const std::vector< VertexId > &vertices)
insert an element into the coarse grid
Definition: gridfactory.cc:98
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:221
virtual ~ALU3dGridFactory()
Destructor.
Definition: gridfactory.cc:28
void setLongestEdgeFlag(bool flag=true)
set longest edge marking for biscetion grids (default is off)
Definition: gridfactory.hh:328
ALU3dGridFactory(const bool verbose, const MPICommunicatorType &communicator)
constructor taking verbose flag
Definition: gridfactory.hh:560
virtual void insertBoundarySegment(const std::vector< VertexId > &vertices, const std::shared_ptr< BoundarySegment< dimension, dimensionworld > > &boundarySegment)
insert a shaped boundary segment into the macro grid
Definition: gridfactory.hh:608
ALUGrid boundary projection implementation DuneBndProjection has to fulfil the DuneBoundaryProjection...
Definition: bndprojection.hh:14
unstructured parallel implementation of the DUNE grid interface
Definition: alugrid.hh:32
Definition: boundaryprojection.hh:133
Definition: topology.hh:40
Wrapper class for entities.
Definition: entity.hh:66
Definition: topology.hh:151
static int dune2aluVertex(int index)
Maps vertex index from Dune onto ALU3dGrid reference face.
Definition: topology.cc:497
Unique label for each type of entities that can occur in DUNE grids.
Definition: type.hh:125
Base class for exceptions in Dune grid modules.
Definition: exceptions.hh:20
Provide a generic factory class for unstructured grids.
Definition: gridfactory.hh:73
Specialization of the generic GridFactory for ALUGrid.
Definition: gridfactory.hh:456
GridFactory(const MPICommunicatorType &communicator=Grid::defaultCommunicator())
Default constructor.
Definition: gridfactory.hh:466
GridFactory(const std::string &filename, const MPIComm &)
constructor taking filename and ignoring MPIComm
Definition: gridfactory.hh:484
GridFactory(const MPIComm &)
Default constructor ignoring MPIComm.
Definition: gridfactory.hh:472
GridFactory(const std::string &filename, const MPICommunicatorType &communicator=Grid::defaultCommunicator())
constructor taking filename
Definition: gridfactory.hh:477
Provide a generic factory class for unstructured grids.
Definition: gridfactory.hh:314
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:456
detected_or_fallback_t< DeprecatedCollectiveCommunication_t, Communication_t, typename GridFamily::Traits > Communication
A type that is a model of Dune::Communication. It provides a portable way for communication on the se...
Definition: grid.hh:525
GridFamily::Traits::LevelIntersection LevelIntersection
A type that is a model of Dune::Intersection, an intersections of two codimension 1 of two codimensio...
Definition: grid.hh:461
Provide a generic factory class for unstructured grids.
#define DUNE_THROW(E, m)
Definition: exceptions.hh:218
constexpr GeometryType cube(unsigned int dim)
Returns a GeometryType representing a hypercube of dimension dim.
Definition: type.hh:472
constexpr GeometryType simplex(unsigned int dim)
Returns a GeometryType representing a simplex of dimension dim.
Definition: type.hh:463
constexpr GeometryType vertex
GeometryType representing a vertex.
Definition: type.hh:506
auto max(ADLTag< 0 >, const V &v1, const V &v2)
implements binary Simd::max()
Definition: defaults.hh:81
Helpers for dealing with MPI.
Dune namespace.
Definition: alignedallocator.hh:13
Base class for classes implementing geometries of boundary segments.
Definition: boundarysegment.hh:94
Static tag representing a codimension.
Definition: dimension.hh:24
Interface class for vertex projection at the boundary.
Definition: boundaryprojection.hh:33
static const ReferenceElement & general(const GeometryType &type)
get general reference elements
Definition: referenceelements.hh:198