4 #ifndef DUNE_ALBERTA_GRIDFACTORY_HH
5 #define DUNE_ALBERTA_GRIDFACTORY_HH
16 #include <dune/common/array.hh>
18 #include <dune/geometry/referenceelements.hh>
48 template<
int dim,
int dimworld >
69 typedef FieldMatrix< ctype, dimensionworld, dimensionworld >
WorldMatrix;
84 static const int numVertices
93 typedef array< unsigned int, dimension > FaceId;
94 typedef std::map< FaceId, size_t > BoundaryMap;
96 class ProjectionFactory;
100 static const bool supportsBoundaryIds =
true;
102 static const bool supportPeriodicity = MacroData::supportPeriodicity;
106 : globalProjection_( (const DuneProjection *) 0 )
119 macroData_.insertVertex( pos );
128 const std::vector< unsigned int > &vertices )
131 DUNE_THROW(
AlbertaError,
"Inserting element of wrong dimension: " << type.dim() );
132 if( !type.isSimplex() )
133 DUNE_THROW(
AlbertaError,
"Alberta supports only simplices." );
135 if( vertices.size() != (size_t)numVertices )
136 DUNE_THROW(
AlbertaError,
"Wrong number of vertices passed: " << vertices.size() <<
"." );
138 int array[ numVertices ];
139 for(
int i = 0; i < numVertices; ++i )
140 array[ i ] = vertices[ numberingMap_.alberta2dune( dimension, i ) ];
141 macroData_.insertElement( array );
156 if( (
id <= 0) || (
id > 127) )
157 DUNE_THROW(
AlbertaError,
"Invalid boundary id: " <<
id <<
"." );
158 macroData_.boundaryId( element, numberingMap_.dune2alberta( 1, face ) ) =
id;
173 const std::vector< unsigned int > &vertices,
174 const DuneProjection *projection )
176 if( (
int)type.dim() != dimension-1 )
177 DUNE_THROW(
AlbertaError,
"Inserting boundary face of wrong dimension: " << type.dim() );
178 if( !type.isSimplex() )
179 DUNE_THROW(
AlbertaError,
"Alberta supports only simplices." );
182 if( vertices.size() != faceId.size() )
183 DUNE_THROW(
AlbertaError,
"Wrong number of face vertices passed: " << vertices.size() <<
"." );
184 for(
size_t i = 0; i < faceId.size(); ++i )
185 faceId[ i ] = vertices[ i ];
186 std::sort( faceId.begin(), faceId.end() );
188 typedef std::pair< typename BoundaryMap::iterator, bool > InsertResult;
189 const InsertResult result = boundaryMap_.insert( std::make_pair( faceId, boundaryProjections_.size() ) );
191 DUNE_THROW(
GridError,
"Only one boundary projection can be attached to a face." );
192 boundaryProjections_.push_back( DuneProjectionPtr( projection ) );
206 if( globalProjection_ )
207 DUNE_THROW(
GridError,
"Only one global boundary projection can be attached to a grid." );
208 globalProjection_ = DuneProjectionPtr( projection );
219 typedef typename GenericGeometry::SimplexTopology< dimension-1 >::type Topology;
220 insertBoundaryProjection(
GeometryType( Topology() ), vertices, 0 );
230 const shared_ptr< BoundarySegment > &boundarySegment )
232 const ReferenceElement< ctype, dimension-1 > &refSimplex
235 if( !boundarySegment )
236 DUNE_THROW(
GridError,
"Trying to insert null as a boundary segment." );
237 if( (
int)vertices.size() != refSimplex.size( dimension-1 ) )
238 DUNE_THROW(
GridError,
"Wrong number of face vertices passed: " << vertices.size() <<
"." );
240 std::vector< WorldVector > coords( refSimplex.size( dimension-1 ) );
244 for(
int j = 0; j < dimensionworld; ++j )
245 coords[ i ][ j ] = x[ j ];
246 if( ((*boundarySegment)( refSimplex.position( i, dimension-1 ) ) - coords[ i ]).two_norm() > 1e-6 )
247 DUNE_THROW(
GridError,
"Boundary segment does not interpolate the corners." );
251 const DuneProjection *prj =
new BoundarySegmentWrapper( gt, coords, boundarySegment );
252 insertBoundaryProjection( gt, vertices, prj );
268 void insertFaceTransformation (
const WorldMatrix &matrix,
const WorldVector &shift );
280 macroData_.markLongestEdge();
297 macroData_.finalize();
298 if( macroData_.elementCount() == 0 )
299 DUNE_THROW(
GridError,
"Cannot create empty AlbertaGrid." );
302 assert( macroData_.checkNeighbors() );
303 macroData_.checkCycles();
304 ProjectionFactory projectionFactory( *
this );
305 return new Grid( macroData_, projectionFactory );
325 template< GrapeIOFileFormatType type >
326 bool write (
const std::string &filename )
328 static_assert( type !=
pgm,
"AlbertaGridFactory: writing pgm format is not supported." );
329 macroData_.finalize();
332 assert( macroData_.checkNeighbors() );
333 return macroData_.write( filename, (type ==
xdr) );
344 virtual bool write (
const std::string &filename )
346 return write< ascii >( filename );
352 return insertionIndex( Grid::getRealImplementation( entity ).elementInfo() );
358 const int elIndex =
insertionIndex( Grid::getRealImplementation( entity ).elementInfo() );
359 const typename MacroData::ElementId &elementId = macroData_.element( elIndex );
360 return elementId[ Grid::getRealImplementation( entity ).subEntity() ];
366 const Grid &grid = Grid::getRealImplementation( intersection ).grid();
367 const ElementInfo &elementInfo = Grid::getRealImplementation( intersection ).elementInfo();
368 const int face = grid.
generic2alberta( 1, intersection.indexInInside() );
379 unsigned int insertionIndex (
const ElementInfo &elementInfo )
const;
380 unsigned int insertionIndex (
const ElementInfo &elementInfo,
const int face )
const;
382 FaceId faceId (
const ElementInfo &elementInfo,
const int face )
const;
384 MacroData macroData_;
385 NumberingMap numberingMap_;
386 DuneProjectionPtr globalProjection_;
387 BoundaryMap boundaryMap_;
388 std::vector< DuneProjectionPtr > boundaryProjections_;
392 template<
int dim,
int dimworld >
395 macroData_.release();
399 template<
int dim,
int dimworld >
405 for(
int i = 0; i < dimworld; ++i )
406 for(
int j = 0; j < dimworld; ++j )
408 const ctype delta = (i == j ?
ctype( 1 ) : ctype( 0 ));
409 const ctype epsilon = (8*dimworld)*std::numeric_limits< ctype >::epsilon();
411 if(
std::abs( matrix[ i ] * matrix[ j ] - delta ) > epsilon )
414 "Matrix of face transformation is not orthogonal." );
420 for(
int i = 0; i < dimworld; ++i )
421 for(
int j = 0; j < dimworld; ++j )
422 M[ i ][ j ] = matrix[ i ][ j ];
426 for(
int i = 0; i < dimworld; ++i )
430 macroData_.insertWallTrafo( M, t );
434 template<
int dim,
int dimworld >
437 ::insertionIndex (
const ElementInfo &elementInfo )
const
439 const MacroElement ¯oElement = elementInfo.macroElement();
440 const unsigned int index = macroElement.index;
443 const typename MacroData::ElementId &elementId = macroData_.element( index );
444 for(
int i = 0; i <= dimension; ++i )
448 for(
int j = 0; j < dimensionworld; ++j )
450 if( x[ j ] != y[ j ] )
451 DUNE_THROW(
GridError,
"Vertex in macro element does not coincide with same vertex in macro data structure." );
454 #endif // #ifndef NDEBUG
460 template<
int dim,
int dimworld >
462 GridFactory< AlbertaGrid< dim, dimworld > >
463 ::insertionIndex (
const ElementInfo &elementInfo,
const int face )
const
465 typedef typename BoundaryMap::const_iterator Iterator;
466 const Iterator it = boundaryMap_.find( faceId( elementInfo, face ) );
467 if( it != boundaryMap_.end() )
474 template<
int dim,
int dimworld >
475 inline typename GridFactory< AlbertaGrid< dim, dimworld > >::FaceId
476 GridFactory< AlbertaGrid< dim, dimworld > >
477 ::faceId (
const ElementInfo &elementInfo,
const int face )
const
479 const unsigned int index = insertionIndex( elementInfo );
480 const typename MacroData::ElementId &elementId = macroData_.element( index );
483 for(
size_t i = 0; i < faceId.size(); ++i )
485 const int k = Alberta::MapVertices< dimension, 1 >::apply( face, i );
486 faceId[ i ] = elementId[ k ];
488 std::sort( faceId.begin(), faceId.end() );
497 template<
int dim,
int dimworld >
501 typedef ProjectionFactory This;
513 : gridFactory_( gridFactory )
518 if( gridFactory().globalProjection_ )
521 const unsigned int index = gridFactory().insertionIndex( elementInfo, face );
523 return bool( gridFactory().boundaryProjections_[ index ] );
530 return bool( gridFactory().globalProjection_ );
533 Projection
projection (
const ElementInfo &elementInfo,
const int face )
const
535 const unsigned int index = gridFactory().insertionIndex( elementInfo, face );
538 const DuneProjectionPtr &projection = gridFactory().boundaryProjections_[ index ];
540 return Projection( projection );
543 assert( gridFactory().globalProjection_ );
544 return Projection( gridFactory().globalProjection_ );
547 Projection
projection (
const ElementInfo &elementInfo )
const
549 assert( gridFactory().globalProjection_ );
550 return Projection( gridFactory().globalProjection_ );
564 #endif // #if HAVE_ALBERTA
566 #endif // #ifndef DUNE_ALBERTA_GRIDFACTORY_HH
bool hasProjection(const ElementInfo &elementInfo, const int face) const
Definition: albertagrid/gridfactory.hh:516
virtual unsigned int insertionIndex(const typename Codim< 0 >::Entity &entity) const
obtain an element's insertion index
Definition: common/gridfactory.hh:179
virtual void insertBoundaryProjection(const DuneProjection *projection)
insert a global (boundary) projection into the macro grid
Definition: albertagrid/gridfactory.hh:204
The dimension of the world the grid lives in.
Definition: common/grid.hh:408
Provide a generic factory class for unstructured grids.
Definition: common/gridfactory.hh:263
Interface class for vertex projection at the boundary.
Definition: boundaryprojection.hh:23
GridFamily::ctype ctype
Definition: agrid.hh:175
Projection projection(const ElementInfo &elementInfo) const
Definition: albertagrid/gridfactory.hh:547
GridFactory()
Definition: albertagrid/gridfactory.hh:105
Provide a generic factory class for unstructured grids.
Definition: common/gridfactory.hh:73
ALBERTA REAL_D GlobalVector
Definition: misc.hh:47
virtual void insertBoundarySegment(const std::vector< unsigned int > &vertices, const shared_ptr< BoundarySegment > &boundarySegment)
insert a shaped boundary segment into the macro grid
Definition: albertagrid/gridfactory.hh:229
static const int dimension
dimension of the grid
Definition: common/gridfactory.hh:78
DuneBoundaryProjection< dimensionworld > DuneProjection
Definition: albertagrid/gridfactory.hh:71
virtual void insertElement(const GeometryType &type, const std::vector< unsigned int > &vertices)
insert an element into the macro grid
Definition: albertagrid/gridfactory.hh:127
void abs(const DofVectorPointer< int > &dofVector)
Definition: dofvector.hh:326
ProjectionFactory(const GridFactory &gridFactory)
Definition: albertagrid/gridfactory.hh:512
virtual void insertBoundarySegment(const std::vector< unsigned int > &vertices)
insert a boundary segment into the macro grid
Definition: albertagrid/gridfactory.hh:217
virtual bool wasInserted(const typename Grid::LeafIntersection &intersection) const
Definition: albertagrid/gridfactory.hh:373
Grid::template Codim< codim >::Entity Entity
Definition: albertagrid/gridfactory.hh:78
Base class for classes implementing geometries of boundary segments.
Definition: boundarysegment.hh:29
Definition: grapedataioformattypes.hh:16
ALBERTA REAL Real
Definition: misc.hh:45
Definition: grapedataioformattypes.hh:18
provides the AlbertaGrid class
int generic2alberta(int codim, int i) const
Definition: agrid.hh:559
void markLongestEdge()
mark the longest edge as refinemet edge
Definition: albertagrid/gridfactory.hh:278
virtual unsigned int insertionIndex(const typename Grid::LeafIntersection &intersection) const
Definition: albertagrid/gridfactory.hh:364
int max(const DofVectorPointer< int > &dofVector)
Definition: dofvector.hh:335
Base::Projection Projection
Definition: albertagrid/gridfactory.hh:507
Base class for exceptions in Dune grid modules.
Definition: exceptions.hh:16
virtual void insertBoundaryProjection(const GeometryType &type, const std::vector< unsigned int > &vertices, const DuneProjection *projection)
insert a boundary projection into the macro grid
Definition: albertagrid/gridfactory.hh:172
FieldVector< ctype, dimensionworld > WorldVector
type of vector for world coordinates
Definition: albertagrid/gridfactory.hh:67
GridFamily::Traits::LeafIntersection LeafIntersection
A type that is a model of Dune::Intersection, an intersections of two codimension 1 of two codimensio...
Definition: common/grid.hh:486
[ provides Dune::Grid ]
Definition: agrid.hh:137
Grid::ctype ctype
type of (scalar) coordinates
Definition: albertagrid/gridfactory.hh:59
Provide a generic factory class for unstructured grids.
const GridFactory & gridFactory() const
Definition: albertagrid/gridfactory.hh:553
Definition: macroelement.hh:20
Definition: alugrid/common/declaration.hh:18
virtual bool write(const std::string &filename)
write out the macro triangulation in native grid file format
Definition: albertagrid/gridfactory.hh:344
virtual void insertBoundary(int element, int face, int id)
mark a face as boundary (and assign a boundary id)
Definition: albertagrid/gridfactory.hh:154
Projection projection(const ElementInfo &elementInfo, const int face) const
Definition: albertagrid/gridfactory.hh:533
virtual unsigned int insertionIndex(const typename Codim< dimension >::Entity &entity) const
obtain a vertex' insertion index
Definition: albertagrid/gridfactory.hh:356
Definition: albertagrid/projection.hh:25
virtual void insertVertex(const WorldVector &pos)
insert a vertex into the macro grid
Definition: albertagrid/gridfactory.hh:117
specialization of the generic GridFactory for AlbertaGrid
Definition: albertagrid/gridfactory.hh:49
Definition: albertagrid/projection.hh:33
FieldMatrix< ctype, dimensionworld, dimensionworld > WorldMatrix
type of matrix from world coordinates to world coordinates
Definition: albertagrid/gridfactory.hh:69
Dune::shared_ptr< const DuneProjection > DuneProjectionPtr
Definition: albertagrid/gridfactory.hh:72
AlbertaGrid< dim, dimworld > Grid
type of grid this factory is for
Definition: albertagrid/gridfactory.hh:56
Base::ElementInfo ElementInfo
Definition: albertagrid/gridfactory.hh:508
virtual unsigned int insertionIndex(const typename Codim< 0 >::Entity &entity) const
obtain an element's insertion index
Definition: albertagrid/gridfactory.hh:350
ALBERTA REAL_DD GlobalMatrix
Definition: misc.hh:48
static void destroyGrid(Grid *grid)
destroy a grid previously obtain from this factory
Definition: albertagrid/gridfactory.hh:312
bool hasProjection(const ElementInfo &elementInfo) const
Definition: albertagrid/gridfactory.hh:528
The dimension of the grid.
Definition: common/grid.hh:402
Projection::Projection DuneProjection
Definition: albertagrid/gridfactory.hh:510
Dune::BoundarySegment< dimension, dimensionworld > BoundarySegment
Definition: albertagrid/gridfactory.hh:73
Include standard header files.
Definition: agrid.hh:59
Grid * createGrid()
finalize grid creation and hand over the grid
Definition: albertagrid/gridfactory.hh:295
Definition: boundaryprojection.hh:65
GeometryType
Type representing VTK's entity geometry types.
Definition: common.hh:178
bool write(const std::string &filename)
write out the macro triangulation in native grid file format
Definition: albertagrid/gridfactory.hh:326