DUNE-FEM (unstable)

gridfactory.hh
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file LICENSE.md in module root
2// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
3// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
4// vi: set et ts=4 sw=2 sts=2:
5
6#ifndef DUNE_ALBERTA_GRIDFACTORY_HH
7#define DUNE_ALBERTA_GRIDFACTORY_HH
8
14#include <algorithm>
15#include <array>
16#include <limits>
17#include <map>
18#include <memory>
19
20#include <dune/geometry/referenceelements.hh>
21
23
25
26#if HAVE_ALBERTA
27
28namespace Dune
29{
30
48 template< int dim, int dimworld >
49 class GridFactory< AlbertaGrid< dim, dimworld > >
50 : public GridFactoryInterface< AlbertaGrid< dim, dimworld > >
51 {
53
54 public:
57
59 typedef typename Grid::ctype ctype;
60
62 static const int dimension = Grid::dimension;
64 static const int dimensionworld = Grid::dimensionworld;
65
70
72 typedef std::shared_ptr< const DuneProjection > DuneProjectionPtr;
74
75 template< int codim >
76 struct Codim
77 {
78 typedef typename Grid::template Codim< codim >::Entity Entity;
79 };
80
81 private:
83
84 static const int numVertices
85 = Alberta::NumSubEntities< dimension, dimension >::value;
86
87 typedef Alberta::MacroElement< dimension > MacroElement;
88 typedef Alberta::ElementInfo< dimension > ElementInfo;
89 typedef Alberta::MacroData< dimension > MacroData;
90 typedef Alberta::NumberingMap< dimension, Alberta::Dune2AlbertaNumbering > NumberingMap;
91 typedef Alberta::DuneBoundaryProjection< dimension > Projection;
92
93 typedef std::array< unsigned int, dimension > FaceId;
94 typedef std::map< FaceId, size_t > BoundaryMap;
95
96 class ProjectionFactory;
97
98 public:
100 static const bool supportsBoundaryIds = true;
102 static const bool supportPeriodicity = MacroData::supportPeriodicity;
103
106 : globalProjection_( (const DuneProjection *) 0 )
107 {
108 macroData_.create();
109 }
110
111 virtual ~GridFactory ();
112
117 virtual void insertVertex ( const WorldVector &pos )
118 {
119 macroData_.insertVertex( pos );
120 }
121
127 virtual void insertElement ( const GeometryType &type,
128 const std::vector< unsigned int > &vertices )
129 {
130 if( (int)type.dim() != dimension )
131 DUNE_THROW( AlbertaError, "Inserting element of wrong dimension: " << type.dim() );
132 if( !type.isSimplex() )
133 DUNE_THROW( AlbertaError, "Alberta supports only simplices." );
134
135 if( vertices.size() != (size_t)numVertices )
136 DUNE_THROW( AlbertaError, "Wrong number of vertices passed: " << vertices.size() << "." );
137
138 int array[ numVertices ];
139 for( int i = 0; i < numVertices; ++i )
140 array[ i ] = vertices[ numberingMap_.alberta2dune( dimension, i ) ];
141 macroData_.insertElement( array );
142 }
143
154 virtual void insertBoundary ( int element, int face, int id )
155 {
156 if( (id <= 0) || (id > 127) )
157 DUNE_THROW( AlbertaError, "Invalid boundary id: " << id << "." );
158 macroData_.boundaryId( element, numberingMap_.dune2alberta( 1, face ) ) = id;
159 }
160
171 virtual void
173 const std::vector< unsigned int > &vertices,
174 const DuneProjection *projection )
175 {
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." );
180
181 FaceId faceId;
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() );
187
188 typedef std::pair< typename BoundaryMap::iterator, bool > InsertResult;
189 const InsertResult result = boundaryMap_.insert( std::make_pair( faceId, boundaryProjections_.size() ) );
190 if( !result.second )
191 DUNE_THROW( GridError, "Only one boundary projection can be attached to a face." );
192 boundaryProjections_.push_back( DuneProjectionPtr( projection ) );
193 }
194
195
204 virtual void insertBoundaryProjection ( const DuneProjection *projection )
205 {
206 if( globalProjection_ )
207 DUNE_THROW( GridError, "Only one global boundary projection can be attached to a grid." );
208 globalProjection_ = DuneProjectionPtr( projection );
209 }
210
216 virtual void
217 insertBoundarySegment ( const std::vector< unsigned int >& vertices )
218 {
219 insertBoundaryProjection( GeometryTypes::simplex( dimension-1 ), vertices, 0 );
220 }
221
227 virtual void
228 insertBoundarySegment ( const std::vector< unsigned int > &vertices,
229 const std::shared_ptr< BoundarySegment > &boundarySegment )
230 {
232
233 if( !boundarySegment )
234 DUNE_THROW( GridError, "Trying to insert null as a boundary segment." );
235 if( (int)vertices.size() != refSimplex.size( dimension-1 ) )
236 DUNE_THROW( GridError, "Wrong number of face vertices passed: " << vertices.size() << "." );
237
238 std::vector< WorldVector > coords( refSimplex.size( dimension-1 ) );
239 for( int i = 0; i < dimension; ++i )
240 {
241 Alberta::GlobalVector &x = macroData_.vertex( vertices[ i ] );
242 for( int j = 0; j < dimensionworld; ++j )
243 coords[ i ][ j ] = x[ j ];
244 if( ((*boundarySegment)( refSimplex.position( i, dimension-1 ) ) - coords[ i ]).two_norm() > 1e-6 )
245 DUNE_THROW( GridError, "Boundary segment does not interpolate the corners." );
246 }
247
248 const GeometryType gt = refSimplex.type( 0, 0 );
249 const DuneProjection *prj = new BoundarySegmentWrapper( gt, coords, boundarySegment );
250 insertBoundaryProjection( gt, vertices, prj );
251 }
252
266 void insertFaceTransformation ( const WorldMatrix &matrix, const WorldVector &shift );
267
277 {
278 macroData_.markLongestEdge();
279 }
280
293 std::unique_ptr<Grid> createGrid ()
294 {
295 macroData_.finalize();
296 if( macroData_.elementCount() == 0 )
297 DUNE_THROW( GridError, "Cannot create empty AlbertaGrid." );
298 if( dimension < 3 )
299 macroData_.setOrientation( Alberta::Real( 1 ) );
300 assert( macroData_.checkNeighbors() );
301 macroData_.checkCycles();
302 ProjectionFactory projectionFactory( *this );
303 return std::make_unique<Grid>( macroData_, projectionFactory );
304 }
305
310 static void destroyGrid ( Grid *grid )
311 {
312 delete grid;
313 }
314
321 bool write ( const std::string &filename )
322 {
323 macroData_.finalize();
324 if( dimension < 3 )
325 macroData_.setOrientation( Alberta::Real( 1 ) );
326 assert( macroData_.checkNeighbors() );
327 return macroData_.write( filename, false );
328 }
329
330 virtual unsigned int
331 insertionIndex ( const typename Codim< 0 >::Entity &entity ) const
332 {
333 return insertionIndex( entity.impl().elementInfo() );
334 }
335
336 virtual unsigned int
337 insertionIndex ( const typename Codim< dimension >::Entity &entity ) const
338 {
339 const int elIndex = insertionIndex( entity.impl().elementInfo() );
340 const typename MacroData::ElementId &elementId = macroData_.element( elIndex );
341 return elementId[ entity.impl().subEntity() ];
342 }
343
344 virtual unsigned int
345 insertionIndex ( const typename Grid::LeafIntersection &intersection ) const
346 {
347 const Grid &grid = intersection.impl().grid();
348 const ElementInfo &elementInfo = intersection.impl().elementInfo();
349 const int face = grid.generic2alberta( 1, intersection.indexInInside() );
350 return insertionIndex( elementInfo, face );
351 }
352
353 virtual bool
354 wasInserted ( const typename Grid::LeafIntersection &intersection ) const
355 {
356 return (insertionIndex( intersection ) < std::numeric_limits< unsigned int >::max());
357 }
358
359 private:
360 unsigned int insertionIndex ( const ElementInfo &elementInfo ) const;
361 unsigned int insertionIndex ( const ElementInfo &elementInfo, const int face ) const;
362
363 FaceId faceId ( const ElementInfo &elementInfo, const int face ) const;
364
365 MacroData macroData_;
366 NumberingMap numberingMap_;
367 DuneProjectionPtr globalProjection_;
368 BoundaryMap boundaryMap_;
369 std::vector< DuneProjectionPtr > boundaryProjections_;
370 };
371
372
373 template< int dim, int dimworld >
374 GridFactory< AlbertaGrid< dim, dimworld > >::~GridFactory ()
375 {
376 macroData_.release();
377 }
378
379
380 template< int dim, int dimworld >
381 inline void
382 GridFactory< AlbertaGrid< dim, dimworld > >
383 ::insertFaceTransformation ( const WorldMatrix &matrix, const WorldVector &shift )
384 {
385 // make sure the matrix is orthogonal
386 for( int i = 0; i < dimworld; ++i )
387 for( int j = 0; j < dimworld; ++j )
388 {
389 const ctype delta = (i == j ? ctype( 1 ) : ctype( 0 ));
390 const ctype epsilon = (8*dimworld)*std::numeric_limits< ctype >::epsilon();
391
392 if( std::abs( matrix[ i ] * matrix[ j ] - delta ) > epsilon )
393 {
394 DUNE_THROW( AlbertaError,
395 "Matrix of face transformation is not orthogonal." );
396 }
397 }
398
399 // copy matrix
400 Alberta::GlobalMatrix M;
401 for( int i = 0; i < dimworld; ++i )
402 for( int j = 0; j < dimworld; ++j )
403 M[ i ][ j ] = matrix[ i ][ j ];
404
405 // copy shift
406 Alberta::GlobalVector t;
407 for( int i = 0; i < dimworld; ++i )
408 t[ i ] = shift[ i ];
409
410 // insert into ALBERTA macro data
411 macroData_.insertWallTrafo( M, t );
412 }
413
414
415 template< int dim, int dimworld >
416 inline unsigned int
418 ::insertionIndex ( const ElementInfo &elementInfo ) const
419 {
420 const MacroElement &macroElement = elementInfo.macroElement();
421 const unsigned int index = macroElement.index;
422
423#ifndef NDEBUG
424 const typename MacroData::ElementId &elementId = macroData_.element( index );
425 for( int i = 0; i <= dimension; ++i )
426 {
427 const Alberta::GlobalVector &x = macroData_.vertex( elementId[ i ] );
428 const Alberta::GlobalVector &y = macroElement.coordinate( i );
429 for( int j = 0; j < dimensionworld; ++j )
430 {
431 if( x[ j ] != y[ j ] )
432 DUNE_THROW( GridError, "Vertex in macro element does not coincide with same vertex in macro data structure." );
433 }
434 }
435#endif // #ifndef NDEBUG
436
437 return index;
438 }
439
440
441 template< int dim, int dimworld >
442 inline unsigned int
443 GridFactory< AlbertaGrid< dim, dimworld > >
444 ::insertionIndex ( const ElementInfo &elementInfo, const int face ) const
445 {
446 typedef typename BoundaryMap::const_iterator Iterator;
447 const Iterator it = boundaryMap_.find( faceId( elementInfo, face ) );
448 if( it != boundaryMap_.end() )
449 return it->second;
450 else
452 }
453
454
455 template< int dim, int dimworld >
456 inline typename GridFactory< AlbertaGrid< dim, dimworld > >::FaceId
457 GridFactory< AlbertaGrid< dim, dimworld > >
458 ::faceId ( const ElementInfo &elementInfo, const int face ) const
459 {
460 const unsigned int index = insertionIndex( elementInfo );
461 const typename MacroData::ElementId &elementId = macroData_.element( index );
462
463 FaceId faceId;
464 for( size_t i = 0; i < faceId.size(); ++i )
465 {
466 const int k = Alberta::MapVertices< dimension, 1 >::apply( face, i );
467 faceId[ i ] = elementId[ k ];
468 }
469 std::sort( faceId.begin(), faceId.end() );
470 return faceId;
471 }
472
473
474
475 // GridFactory::ProjectionFactory
476 // ------------------------------
477
478 template< int dim, int dimworld >
479 class GridFactory< AlbertaGrid< dim, dimworld > >::ProjectionFactory
480 : public Alberta::ProjectionFactory< Alberta::DuneBoundaryProjection< dim >, ProjectionFactory >
481 {
482 typedef ProjectionFactory This;
483 typedef Alberta::ProjectionFactory< Alberta::DuneBoundaryProjection< dim >, ProjectionFactory > Base;
484
485 typedef typename Dune::GridFactory< AlbertaGrid< dim, dimworld > > Factory;
486
487 public:
488 typedef typename Base::Projection Projection;
489 typedef typename Base::ElementInfo ElementInfo;
490
491 typedef typename Projection::Projection DuneProjection;
492
493 ProjectionFactory( const GridFactory &gridFactory )
494 : gridFactory_( gridFactory )
495 {}
496
497 bool hasProjection ( const ElementInfo &elementInfo, const int face ) const
498 {
499 if( gridFactory().globalProjection_ )
500 return true;
501
502 const unsigned int index = gridFactory().insertionIndex( elementInfo, face );
504 return bool( gridFactory().boundaryProjections_[ index ] );
505 else
506 return false;
507 }
508
509 bool hasProjection ( const ElementInfo & /* elementInfo */ ) const
510 {
511 return bool( gridFactory().globalProjection_ );
512 }
513
514 Projection projection ( const ElementInfo &elementInfo, const int face ) const
515 {
516 const unsigned int index = gridFactory().insertionIndex( elementInfo, face );
518 {
519 const DuneProjectionPtr &projection = gridFactory().boundaryProjections_[ index ];
520 if( projection )
521 return Projection( projection );
522 }
523
524 assert( gridFactory().globalProjection_ );
525 return Projection( gridFactory().globalProjection_ );
526 };
527
528 Projection projection ( const ElementInfo & /* elementInfo */ ) const
529 {
530 assert( gridFactory().globalProjection_ );
531 return Projection( gridFactory().globalProjection_ );
532 };
533
534 const GridFactory &gridFactory () const
535 {
536 return gridFactory_;
537 }
538
539 private:
540 const GridFactory &gridFactory_;
541 };
542
543}
544
545#endif // #if HAVE_ALBERTA
546
547#endif // #ifndef DUNE_ALBERTA_GRIDFACTORY_HH
provides the AlbertaGrid class
[ provides Dune::Grid ]
Definition: agrid.hh:109
Definition: boundaryprojection.hh:133
Wrapper class for entities.
Definition: entity.hh:66
A dense n x m matrix.
Definition: fmatrix.hh:117
vector space out of a tensor product of fields.
Definition: fvector.hh:91
Unique label for each type of entities that can occur in DUNE grids.
Definition: type.hh:114
constexpr unsigned int dim() const
Return dimension of the type.
Definition: type.hh:360
constexpr bool isSimplex() const
Return true if entity is a simplex of any dimension.
Definition: type.hh:319
Base class for exceptions in Dune grid modules.
Definition: exceptions.hh:20
Provide a generic factory class for unstructured grids.
Definition: gridfactory.hh:70
virtual unsigned int insertionIndex(const typename Codim< 0 >::Entity &entity) const
obtain an element's insertion index
Definition: gridfactory.hh:181
static const int dimension
dimension of the grid
Definition: gridfactory.hh:74
virtual bool wasInserted(const typename GridType::LeafIntersection &intersection) const
determine whether an intersection was inserted
Definition: gridfactory.hh:247
specialization of the generic GridFactory for AlbertaGrid
Definition: gridfactory.hh:51
FieldVector< ctype, dimensionworld > WorldVector
type of vector for world coordinates
Definition: gridfactory.hh:67
std::unique_ptr< Grid > createGrid()
finalize grid creation and hand over the grid
Definition: gridfactory.hh:293
virtual unsigned int insertionIndex(const typename Codim< 0 >::Entity &entity) const
obtain an element's insertion index
Definition: gridfactory.hh:331
AlbertaGrid< dim, dimworld > Grid
type of grid this factory is for
Definition: gridfactory.hh:56
virtual void insertBoundaryProjection(const GeometryType &type, const std::vector< unsigned int > &vertices, const DuneProjection *projection)
insert a boundary projection into the macro grid
Definition: gridfactory.hh:172
FieldMatrix< ctype, dimensionworld, dimensionworld > WorldMatrix
type of matrix from world coordinates to world coordinates
Definition: gridfactory.hh:69
virtual void insertElement(const GeometryType &type, const std::vector< unsigned int > &vertices)
insert an element into the macro grid
Definition: gridfactory.hh:127
void markLongestEdge()
mark the longest edge as refinemet edge
Definition: gridfactory.hh:276
Grid::ctype ctype
type of (scalar) coordinates
Definition: gridfactory.hh:59
virtual void insertVertex(const WorldVector &pos)
insert a vertex into the macro grid
Definition: gridfactory.hh:117
virtual void insertBoundary(int element, int face, int id)
mark a face as boundary (and assign a boundary id)
Definition: gridfactory.hh:154
GridFactory()
Definition: gridfactory.hh:105
static void destroyGrid(Grid *grid)
destroy a grid previously obtain from this factory
Definition: gridfactory.hh:310
virtual unsigned int insertionIndex(const typename Codim< dimension >::Entity &entity) const
obtain a vertex' insertion index
Definition: gridfactory.hh:337
bool write(const std::string &filename)
write out the macro triangulation in native grid file format
Definition: gridfactory.hh:321
virtual void insertBoundarySegment(const std::vector< unsigned int > &vertices, const std::shared_ptr< BoundarySegment > &boundarySegment)
insert a shaped boundary segment into the macro grid
Definition: gridfactory.hh:228
virtual void insertBoundaryProjection(const DuneProjection *projection)
insert a global (boundary) projection into the macro grid
Definition: gridfactory.hh:204
virtual void insertBoundarySegment(const std::vector< unsigned int > &vertices)
insert a boundary segment into the macro grid
Definition: gridfactory.hh:217
Provide a generic factory class for unstructured grids.
Definition: gridfactory.hh:275
GridFactory()
Default constructor.
Definition: gridfactory.hh:291
Grid abstract base class.
Definition: grid.hh:375
static constexpr int dimension
The dimension of the grid.
Definition: grid.hh:387
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
static constexpr int dimensionworld
The dimension of the world the grid lives in.
Definition: grid.hh:390
Provide a generic factory class for unstructured grids.
#define DUNE_THROW(E, m)
Definition: exceptions.hh:218
bool gt(const T &first, const T &second, typename EpsilonType< T >::Type epsilon)
test if first greater than second
Definition: float_cmp.cc:158
constexpr GeometryType simplex(unsigned int dim)
Returns a GeometryType representing a simplex of dimension dim.
Definition: type.hh:453
constexpr auto max
Function object that returns the greater of the given values.
Definition: hybridutilities.hh:484
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 & simplex()
get simplex reference elements
Definition: referenceelements.hh:162
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Nov 21, 23:30, 2024)