3#ifndef DUNE_GEOMETRY_REFERENCEELEMENTIMPLEMENTATION_HH
4#define DUNE_GEOMETRY_REFERENCEELEMENTIMPLEMENTATION_HH
17#include <dune/common/hybridutilities.hh>
21#include <dune/geometry/referenceelement.hh>
38 template<
class ctype,
int dim >
39 class ReferenceElementContainer;
42 template<
class ctype,
int dim >
43 struct ReferenceElements;
50 using Dune::Impl::isPrism;
51 using Dune::Impl::isPyramid;
52 using Dune::Impl::baseTopologyId;
53 using Dune::Impl::prismConstruction;
54 using Dune::Impl::pyramidConstruction;
55 using Dune::Impl::numTopologies;
58 unsigned int size (
unsigned int topologyId,
int dim,
int codim );
69 unsigned int subTopologyId (
unsigned int topologyId,
int dim,
int codim,
unsigned int i );
76 void subTopologyNumbering (
unsigned int topologyId,
int dim,
int codim,
unsigned int i,
int subcodim,
77 unsigned int *beginOut,
unsigned int *endOut );
85 template<
class ct,
int cdim >
87 checkInside (
unsigned int topologyId,
int dim,
const FieldVector< ct, cdim > &x, ct tolerance, ct factor = ct( 1 ) )
89 assert( (dim >= 0) && (dim <= cdim) );
90 assert( topologyId < numTopologies( dim ) );
94 const ct baseFactor = (isPrism( topologyId, dim ) ? factor : factor - x[ dim-1 ]);
95 if( (x[ dim-1 ] > -tolerance) && (factor - x[ dim-1 ] > -tolerance) )
96 return checkInside< ct, cdim >( baseTopologyId( topologyId, dim ), dim-1, x, tolerance, baseFactor );
109 template<
class ct,
int cdim >
111 referenceCorners (
unsigned int topologyId,
int dim, FieldVector< ct, cdim > *corners )
113 assert( (dim >= 0) && (dim <= cdim) );
114 assert( topologyId < numTopologies( dim ) );
118 const unsigned int nBaseCorners
119 = referenceCorners( baseTopologyId( topologyId, dim ), dim-1, corners );
120 assert( nBaseCorners == size( baseTopologyId( topologyId, dim ), dim-1, dim-1 ) );
121 if( isPrism( topologyId, dim ) )
123 std::copy( corners, corners + nBaseCorners, corners + nBaseCorners );
124 for(
unsigned int i = 0; i < nBaseCorners; ++i )
125 corners[ i+nBaseCorners ][ dim-1 ] = ct( 1 );
126 return 2*nBaseCorners;
130 corners[ nBaseCorners ] = FieldVector< ct, cdim >( ct( 0 ) );
131 corners[ nBaseCorners ][ dim-1 ] = ct( 1 );
132 return nBaseCorners+1;
137 *corners = FieldVector< ct, cdim >( ct( 0 ) );
147 unsigned long referenceVolumeInverse (
unsigned int topologyId,
int dim );
150 inline ct referenceVolume (
unsigned int topologyId,
int dim )
152 return ct( 1 ) / ct( referenceVolumeInverse( topologyId, dim ) );
160 template<
class ct,
int cdim >
162 referenceOrigins (
unsigned int topologyId,
int dim,
int codim, FieldVector< ct, cdim > *origins )
164 assert( (dim >= 0) && (dim <= cdim) );
165 assert( topologyId < numTopologies( dim ) );
166 assert( (codim >= 0) && (codim <= dim) );
170 const unsigned int baseId = baseTopologyId( topologyId, dim );
171 if( isPrism( topologyId, dim ) )
173 const unsigned int n = (codim < dim ? referenceOrigins( baseId, dim-1, codim, origins ) : 0);
174 const unsigned int m = referenceOrigins( baseId, dim-1, codim-1, origins+n );
175 for(
unsigned int i = 0; i < m; ++i )
177 origins[ n+m+i ] = origins[ n+i ];
178 origins[ n+m+i ][ dim-1 ] = ct( 1 );
184 const unsigned int m = referenceOrigins( baseId, dim-1, codim-1, origins );
187 origins[ m ] = FieldVector< ct, cdim >( ct( 0 ) );
188 origins[ m ][ dim-1 ] = ct( 1 );
192 return m+referenceOrigins( baseId, dim-1, codim, origins+m );
197 origins[ 0 ] = FieldVector< ct, cdim >( ct( 0 ) );
207 template<
class ct,
int cdim,
int mydim >
209 referenceEmbeddings (
unsigned int topologyId,
int dim,
int codim,
210 FieldVector< ct, cdim > *origins,
211 FieldMatrix< ct, mydim, cdim > *jacobianTransposeds )
213 assert( (0 <= codim) && (codim <= dim) && (dim <= cdim) );
214 assert( (dim - codim <= mydim) && (mydim <= cdim) );
215 assert( topologyId < numTopologies( dim ) );
219 const unsigned int baseId = baseTopologyId( topologyId, dim );
220 if( isPrism( topologyId, dim ) )
222 const unsigned int n = (codim < dim ? referenceEmbeddings( baseId, dim-1, codim, origins, jacobianTransposeds ) : 0);
223 for(
unsigned int i = 0; i < n; ++i )
224 jacobianTransposeds[ i ][ dim-codim-1 ][ dim-1 ] = ct( 1 );
226 const unsigned int m = referenceEmbeddings( baseId, dim-1, codim-1, origins+n, jacobianTransposeds+n );
227 std::copy( origins+n, origins+n+m, origins+n+m );
228 std::copy( jacobianTransposeds+n, jacobianTransposeds+n+m, jacobianTransposeds+n+m );
229 for(
unsigned int i = 0; i < m; ++i )
230 origins[ n+m+i ][ dim-1 ] = ct( 1 );
236 const unsigned int m = referenceEmbeddings( baseId, dim-1, codim-1, origins, jacobianTransposeds );
239 origins[ m ] = FieldVector< ct, cdim >( ct( 0 ) );
240 origins[ m ][ dim-1 ] = ct( 1 );
241 jacobianTransposeds[ m ] = FieldMatrix< ct, mydim, cdim >( ct( 0 ) );
246 const unsigned int n = referenceEmbeddings( baseId, dim-1, codim, origins+m, jacobianTransposeds+m );
247 for(
unsigned int i = 0; i < n; ++i )
249 for(
int k = 0; k < dim-1; ++k )
250 jacobianTransposeds[ m+i ][ dim-codim-1 ][ k ] = -origins[ m+i ][ k ];
251 jacobianTransposeds[ m+i ][ dim-codim-1 ][ dim-1 ] = ct( 1 );
259 origins[ 0 ] = FieldVector< ct, cdim >( ct( 0 ) );
260 jacobianTransposeds[ 0 ] = FieldMatrix< ct, mydim, cdim >( ct( 0 ) );
261 for(
int k = 0; k < dim; ++k )
262 jacobianTransposeds[ 0 ][ k ][ k ] = ct( 1 );
272 template<
class ct,
int cdim >
274 referenceIntegrationOuterNormals (
unsigned int topologyId,
int dim,
275 const FieldVector< ct, cdim > *origins,
276 FieldVector< ct, cdim > *normals )
278 assert( (dim > 0) && (dim <= cdim) );
279 assert( topologyId < numTopologies( dim ) );
283 const unsigned int baseId = baseTopologyId( topologyId, dim );
284 if( isPrism( topologyId, dim ) )
286 const unsigned int numBaseFaces
287 = referenceIntegrationOuterNormals( baseId, dim-1, origins, normals );
289 for(
unsigned int i = 0; i < 2; ++i )
291 normals[ numBaseFaces+i ] = FieldVector< ct, cdim >( ct( 0 ) );
292 normals[ numBaseFaces+i ][ dim-1 ] = ct( 2*
int( i )-1 );
295 return numBaseFaces+2;
299 normals[ 0 ] = FieldVector< ct, cdim >( ct( 0 ) );
300 normals[ 0 ][ dim-1 ] = ct( -1 );
302 const unsigned int numBaseFaces
303 = referenceIntegrationOuterNormals( baseId, dim-1, origins+1, normals+1 );
304 for(
unsigned int i = 1; i <= numBaseFaces; ++i )
305 normals[ i ][ dim-1 ] = normals[ i ]*origins[ i ];
307 return numBaseFaces+1;
312 for(
unsigned int i = 0; i < 2; ++i )
314 normals[ i ] = FieldVector< ct, cdim >( ct( 0 ) );
315 normals[ i ][ 0 ] = ct( 2*
int( i )-1 );
322 template<
class ct,
int cdim >
324 referenceIntegrationOuterNormals (
unsigned int topologyId,
int dim,
325 FieldVector< ct, cdim > *normals )
327 assert( (dim > 0) && (dim <= cdim) );
329 FieldVector< ct, cdim > *origins
330 =
new FieldVector< ct, cdim >[ size( topologyId, dim, 1 ) ];
331 referenceOrigins( topologyId, dim, 1, origins );
333 const unsigned int numFaces
334 = referenceIntegrationOuterNormals( topologyId, dim, origins, normals );
335 assert( numFaces == size( topologyId, dim, 1 ) );
367 template<
class ctype_,
int dim >
368 class ReferenceElementImplementation
374 using ctype = ctype_;
377 using CoordinateField = ctype;
383 static constexpr int dimension = dim;
387 friend class Impl::ReferenceElementContainer< ctype, dim >;
389 struct SubEntityInfo;
391 template<
int codim >
struct CreateGeometries;
395 template<
int codim >
399 typedef AffineGeometry< ctype, dim-codim, dim > Geometry;
403 ReferenceElementImplementation (
const ReferenceElementImplementation& ) =
delete;
406 ReferenceElementImplementation& operator= (
const ReferenceElementImplementation& ) =
delete;
409 ReferenceElementImplementation () =
default;
415 int size (
int c )
const
417 assert( (c >= 0) && (c <= dim) );
418 return info_[ c ].size();
432 int size (
int i,
int c,
int cc )
const
434 assert( (i >= 0) && (i < size( c )) );
435 return info_[ c ][ i ].size( cc );
451 int subEntity (
int i,
int c,
int ii,
int cc )
const
453 assert( (i >= 0) && (i < size( c )) );
454 return info_[ c ][ i ].number( ii, cc );
467 assert( (i >= 0) && (i < size( c )) );
468 return info_[ c ][ i ].type();
472 const GeometryType &type ()
const {
return type( 0, 0 ); }
483 const Coordinate &position(
int i,
int c )
const
485 assert( (c >= 0) && (c <= dim) );
486 return baryCenters_[ c ][ i ];
496 bool checkInside (
const Coordinate &local )
const
498 const ctype tolerance = ctype( 64 ) * std::numeric_limits< ctype >::epsilon();
499 return Impl::template checkInside< ctype, dim >( type().
id(), dim, local, tolerance );
513 template<
int codim >
514 typename Codim< codim >::Geometry geometry (
int i )
const
516 return std::get< codim >( geometries_ )[ i ];
520 ctype volume ()
const
532 const Coordinate &integrationOuterNormal (
int face )
const
534 assert( (face >= 0) && (face <
int( integrationNormals_.size() )) );
535 return integrationNormals_[ face ];
539 void initialize (
unsigned int topologyId )
541 assert( topologyId < Impl::numTopologies( dim ) );
544 for(
int codim = 0; codim <= dim; ++codim )
546 const unsigned int size = Impl::size( topologyId, dim, codim );
547 info_[ codim ].resize( size );
548 for(
unsigned int i = 0; i < size; ++i )
549 info_[ codim ][ i ].initialize( topologyId, codim, i );
553 const unsigned int numVertices = size( dim );
554 baryCenters_[ dim ].resize( numVertices );
555 Impl::referenceCorners( topologyId, dim, &(baryCenters_[ dim ][ 0 ]) );
558 for(
int codim = 0; codim < dim; ++codim )
560 baryCenters_[ codim ].resize( size(codim) );
561 for(
int i = 0; i < size( codim ); ++i )
563 baryCenters_[ codim ][ i ] = Coordinate( ctype( 0 ) );
564 const unsigned int numCorners = size( i, codim, dim );
565 for(
unsigned int j = 0; j < numCorners; ++j )
566 baryCenters_[ codim ][ i ] += baryCenters_[ dim ][ subEntity( i, codim, j, dim ) ];
567 baryCenters_[ codim ][ i ] *= ctype( 1 ) / ctype( numCorners );
572 volume_ = Impl::template referenceVolume< ctype >( topologyId, dim );
577 integrationNormals_.resize( size( 1 ) );
578 Impl::referenceIntegrationOuterNormals( topologyId, dim, &(integrationNormals_[ 0 ]) );
585 template<
int... codim >
586 static std::tuple< std::vector< typename Codim< codim >::Geometry >... >
587 makeGeometryTable ( std::integer_sequence< int, codim... > );
590 typedef decltype( makeGeometryTable( std::make_integer_sequence< int, dim+1 >() ) ) GeometryTable;
595 std::vector< Coordinate > baryCenters_[ dim+1 ];
596 std::vector< Coordinate > integrationNormals_;
599 GeometryTable geometries_;
601 std::vector< SubEntityInfo > info_[ dim+1 ];
605 template<
class ctype,
int dim >
606 struct ReferenceElementImplementation< ctype, dim >::SubEntityInfo
609 : numbering_( nullptr )
611 std::fill( offset_.begin(), offset_.end(), 0 );
614 SubEntityInfo (
const SubEntityInfo &other )
615 : offset_( other.offset_ ),
618 numbering_ = allocate();
619 std::copy( other.numbering_, other.numbering_ + capacity(), numbering_ );
622 ~SubEntityInfo () { deallocate( numbering_ ); }
624 const SubEntityInfo &operator= (
const SubEntityInfo &other )
627 offset_ = other.offset_;
629 deallocate( numbering_ );
630 numbering_ = allocate();
631 std::copy( other.numbering_, other.numbering_ + capacity(), numbering_ );
636 int size (
int cc )
const
638 assert( (cc >= codim()) && (cc <= dim) );
639 return (offset_[ cc+1 ] - offset_[ cc ]);
642 int number (
int ii,
int cc )
const
644 assert( (ii >= 0) && (ii < size( cc )) );
645 return numbering_[ offset_[ cc ] + ii ];
650 void initialize (
unsigned int topologyId,
int codim,
unsigned int i )
652 const unsigned int subId = Impl::subTopologyId( topologyId, dim, codim, i );
656 for(
int cc = 0; cc <= codim; ++cc )
658 for(
int cc = codim; cc <= dim; ++cc )
659 offset_[ cc+1 ] = offset_[ cc ] + Impl::size( subId, dim-codim, cc-codim );
662 deallocate( numbering_ );
663 numbering_ = allocate();
664 for(
int cc = codim; cc <= dim; ++cc )
665 Impl::subTopologyNumbering( topologyId, dim, codim, i, cc-codim, numbering_+offset_[ cc ], numbering_+offset_[ cc+1 ] );
669 int codim ()
const {
return dim - type().dim(); }
671 unsigned int *allocate () {
return (capacity() != 0 ?
new unsigned int[ capacity() ] :
nullptr); }
672 void deallocate (
unsigned int *ptr ) {
delete[] ptr; }
673 unsigned int capacity ()
const {
return offset_[ dim+1 ]; }
676 unsigned int *numbering_;
677 std::array< unsigned int, dim+2 > offset_;
682 template<
class ctype,
int dim >
683 template<
int codim >
684 struct ReferenceElementImplementation< ctype, dim >::CreateGeometries
688 subRefElement(
const ReferenceElementImplementation< ctype, dim > &refElement,
int i, std::integral_constant< int, cc > )
694 subRefElement(
const ReferenceElementImplementation< ctype, dim > &refElement,
int i, std::integral_constant< int, 0 > )
700 static void apply (
const ReferenceElementImplementation< ctype, dim > &refElement, GeometryTable &geometries )
702 const int size = refElement.size( codim );
703 std::vector< FieldVector< ctype, dim > > origins( size );
704 std::vector< FieldMatrix< ctype, dim - codim, dim > > jacobianTransposeds( size );
705 Impl::referenceEmbeddings( refElement.type().id(), dim, codim, &(origins[ 0 ]), &(jacobianTransposeds[ 0 ]) );
707 std::get< codim >( geometries ).reserve( size );
708 for(
int i = 0; i < size; ++i )
710 typename Codim< codim >::Geometry geometry( subRefElement( refElement, i, std::integral_constant< int, codim >() ), origins[ i ], jacobianTransposeds[ i ] );
711 std::get< codim >( geometries ).push_back( geometry );
An implementation of the Geometry interface for affine geometries.
GeometryType
Type representing VTK's entity geometry types.
Definition: common.hh:178
Implements a matrix constructed from a given type representing a field and compile-time given number ...
Implements a vector constructed from a given type representing a field and a compile-time given size.
decltype(auto) apply(F &&f, ArgTuple &&args)
Apply function with arguments given as tuple.
Definition: apply.hh:58
#define DUNE_UNUSED_PARAMETER(parm)
A macro to mark intentionally unused function parameters with.
Definition: unused.hh:25
unspecified-type ReferenceElement
Returns the type of reference element for the argument type T.
Definition: referenceelements.hh:495
constexpr void forEach(Range &&range, F &&f)
Range based for loop.
Definition: hybridutilities.hh:308
Dune namespace.
Definition: alignedallocator.hh:10
typename Container::ReferenceElement ReferenceElement
The reference element type.
Definition: referenceelements.hh:186
static const ReferenceElement & general(const GeometryType &type)
get general reference elements
Definition: referenceelements.hh:196
A unique label for each type of element that can occur in a grid.
Traits for type conversions and type information.
Definition of the DUNE_UNUSED macro for the case that config.h is not available.