Dune Core Modules (2.9.0)

geometry.hh
1#ifndef DUNE_ALU3DGRIDGEOMETRY_HH
2#define DUNE_ALU3DGRIDGEOMETRY_HH
3
4// System includes
5#include <memory>
6#include <type_traits>
7
8// Dune includes
9#include <dune/common/math.hh>
10
12
13// Local includes
14#include "alu3dinclude.hh"
15#include "topology.hh"
16#include "mappings.hh"
17#include <dune/alugrid/common/memory.hh>
18
19namespace Dune
20{
21
22 // Forward declarations
23 template<int cd, int dim, class GridImp>
24 class ALU3dGridEntity;
25 template<int cd, class GridImp >
26 class ALU3dGridEntityPointer;
27 template<int mydim, int coorddim, class GridImp>
28 class ALU3dGridGeometry;
29 template< int dim, int dimworld, ALU3dGridElementType, class >
30 class ALU3dGrid;
31 class BilinearSurfaceMapping;
32 class TrilinearMapping;
33
34 template< class GridImp >
35 class ALU3dGridIntersectionIterator;
36
37 template <int cdim>
38 class MyALUGridGeometryImplementation
39 {
40 public:
41 typedef FieldVector<alu3d_ctype, cdim> CoordinateVectorType;
42
43 static const signed char invalid = -1; // means geometry is not meaningful
44 static const signed char updated = 0; // means the point values have been set
45 static const signed char buildmapping = 1; // means updated and mapping was build
46
47 template <int dim, int corners, class Mapping>
48 class GeometryImplBase
49 {
50 private:
51 // prohibited due to reference counting
52 GeometryImplBase( const GeometryImplBase& );
53
54 protected:
56 static const int corners_ = corners ;
57
59 typedef FieldMatrix<alu3d_ctype, corners , cdim> CoordinateMatrixType;
60
61 // select coordinate storage for coord_ (pointer for dim == 3)
62 typedef typename std::conditional<
63 dim == 3,
64 std::unique_ptr< CoordinateMatrixType >,
65 CoordinateMatrixType >:: type CoordinateStorageType;
66
68 typedef Mapping MappingType;
69
71 CoordinateStorageType coord_ ;
72
74 MappingType map_;
75
77 double volume_ ;
78
80 signed char status_ ;
81
82 public:
84 unsigned int refCount_;
85
87 GeometryImplBase()
88 : coord_(),
89 map_(),
90 volume_( 1.0 )
91 {
92 invalidate();
93 }
94
95 // copy coordinate vector from field vector or alu3d_ctype[cdim]
96 template <class CoordPtrType>
97 static inline void copy(const CoordPtrType& p,
98 CoordinateVectorType& c)
99 {
100 // we have either 2d or 3d vectors
101 alugrid_assert( cdim > 1 );
102 c[0] = p[0];
103 c[1] = p[1];
104 if( cdim > 2 )
105 c[2] = p[2];
106 }
107
108 template <class CoordPtrType>
109 void update(const CoordPtrType&,
110 const CoordPtrType&,
111 const CoordPtrType&,
112 const CoordPtrType&,
113 const CoordPtrType&,
114 const CoordPtrType&,
115 const CoordPtrType&,
116 const CoordPtrType& ) const
117 {
118 DUNE_THROW(InvalidStateException,"This method should not be called!");
119 }
120
121 template <class CoordPtrType>
122 void update(const CoordPtrType&,
123 const CoordPtrType&,
124 const CoordPtrType&,
125 const CoordPtrType& ) const
126 {
127 DUNE_THROW(InvalidStateException,"This method should not be called!");
128 }
129
130 template <class CoordPtrType>
131 void update(const CoordPtrType&,
132 const CoordPtrType&,
133 const CoordPtrType& ) const
134 {
135 DUNE_THROW(InvalidStateException,"This method should not be called!");
136 }
137
138 template <class CoordPtrType>
139 void update(const CoordPtrType&,
140 const CoordPtrType& ) const
141 {
142 DUNE_THROW(InvalidStateException,"This method should not be called!");
143 }
144
145 template <class CoordPtrType>
146 void update(const CoordPtrType& ) const
147 {
148 DUNE_THROW(InvalidStateException,"This method should not be called!");
149 }
150
151 // update geometry in father coordinates (default impl)
152 template <class GeometryImp>
153 inline void updateInFather(const GeometryImp &fatherGeom ,
154 const GeometryImp &myGeom)
155 {
156 // this version is only for the 2d elements
157 alugrid_assert( dim == 2 );
158
159 // compute the local coordinates in father refelem
160 for(int i=0; i < myGeom.corners() ; ++i)
161 {
162 // calculate coordinate
163 coord_[i] = fatherGeom.local( myGeom.corner( i ) );
164
165 // to avoid rounding errors
166 for(int j=0; j<cdim; ++j)
167 {
168 if ( coord_[i][j] < 1e-16) coord_[i][j] = 0.0;
169 }
170 }
171
172 status_ = updated ;
173 }
174
175 // set status to invalid
176 void invalidate () { status_ = invalid ; }
177
178 // return true if geometry is valid
179 bool valid () const { return status_ != invalid ; }
180
181 // set volume
182 void setVolume( const double volume ) { volume_ = volume ; }
183
184 // return volume
185 double volume() const { return volume_; }
186 };
187
189 template <int dummy, int dim,
190 ALU3dGridElementType eltype> class GeometryImpl;
191 public:
192 // geometry implementation for edges and vertices
193 template <int dummy, int dim, ALU3dGridElementType eltype>
194 class GeometryImpl : public GeometryImplBase< dim, dim+1, LinearMapping<cdim, dim> >
195 {
196 protected:
197 typedef GeometryImplBase< dim, dim+1, LinearMapping<cdim, dim> > BaseType;
198
199 using BaseType :: corners_ ;
200 using BaseType :: copy ;
201 using BaseType :: coord_ ;
202 using BaseType :: map_ ;
203 using BaseType :: status_ ;
204
205 typedef typename BaseType :: MappingType MappingType ;
206 public:
207 using BaseType :: update ;
208 using BaseType :: valid ;
209
210 // return coordinate vector
211 inline const CoordinateVectorType& operator [] (const int i) const
212 {
213 alugrid_assert ( valid() );
214 alugrid_assert ( i>=0 && i<corners_ );
215 return coord_[i];
216 }
217
218 inline MappingType& mapping()
219 {
220 alugrid_assert ( valid() );
221 if( status_ == buildmapping ) return map_;
222
223 map_.buildMapping( coord_[0] );
224 status_ = buildmapping ;
225 return map_;
226 }
227
228 // update vertex
229 template <class CoordPtrType>
230 inline void update(const CoordPtrType& p0)
231 {
232 alugrid_assert ( corners_ == 1 );
233 copy( p0, coord_[0] );
234 // we need to update the mapping
235 status_ = updated ;
236 }
237 };
238
239 // geometry implementation for edges and vertices
240 template <int dummy, ALU3dGridElementType eltype>
241 class GeometryImpl<dummy,1,eltype>
242 : public GeometryImplBase< 1, 2, LinearMapping<cdim, 1> >
243 {
244 protected:
245 enum { dim = 1 };
246 typedef GeometryImplBase< dim, dim+1, LinearMapping<cdim, dim> > BaseType;
247
248 using BaseType :: corners_ ;
249 using BaseType :: copy ;
250 using BaseType :: coord_ ;
251 using BaseType :: map_ ;
252 using BaseType :: status_ ;
253
254 typedef typename BaseType :: MappingType MappingType;
255 public:
256 using BaseType :: update ;
257 using BaseType :: valid ;
258
259 // return coordinate vector
260 inline const CoordinateVectorType& operator [] (const int i) const
261 {
262 alugrid_assert ( valid() );
263 alugrid_assert ( i>=0 && i<corners_ );
264 return coord_[i];
265 }
266
267 inline MappingType& mapping()
268 {
269 alugrid_assert ( valid() );
270 if( status_ == buildmapping ) return map_;
271
272 map_.buildMapping( coord_[0], coord_[1] );
273 status_ = buildmapping ;
274 return map_;
275 }
276
277 // update edge
278 template <class CoordPtrType>
279 inline void update(const CoordPtrType& p0,
280 const CoordPtrType& p1)
281 {
282 alugrid_assert ( corners_ == 2 );
283 copy( p0, coord_[0] );
284 copy( p1, coord_[1] );
285 status_ = updated;
286 }
287 };
288
289 // geom impl for simplex faces (triangles)
290 template <int dummy>
291 class GeometryImpl<dummy, 2, tetra>
292 : public GeometryImplBase< 2, 3, LinearMapping<cdim, 2> >
293 {
294 protected:
295 // dim = 2, corners = 3
296 typedef GeometryImplBase< 2, 3, LinearMapping<cdim, 2> > BaseType;
297
298 using BaseType :: corners_ ;
299 using BaseType :: copy ;
300 using BaseType :: coord_ ;
301 using BaseType :: map_ ;
302 using BaseType :: status_ ;
303
304 typedef typename BaseType :: MappingType MappingType ;
305 public:
306 using BaseType :: update ;
307 using BaseType :: valid ;
308
309 // return coordinate vector
310 inline const CoordinateVectorType& operator [] (const int i) const
311 {
312 alugrid_assert ( valid() );
313 alugrid_assert ( i>=0 && i<corners_ );
314 return coord_[i];
315 }
316
317 // update geometry coordinates
318 template <class CoordPtrType>
319 inline void update(const CoordPtrType& p0,
320 const CoordPtrType& p1,
321 const CoordPtrType& p2)
322 {
323 copy(p0, coord_[0] );
324 copy(p1, coord_[1] );
325 copy(p2, coord_[2] );
326 status_ = updated;
327 }
328
329 // return mapping (always up2date)
330 inline MappingType& mapping()
331 {
332 alugrid_assert ( valid() );
333 if( status_ == buildmapping ) return map_;
334
335 map_.buildMapping( coord_[0], coord_[1], coord_[2] );
336 status_ = buildmapping ;
337 return map_;
338 }
339 };
340
342 //
343 // hexa specializations
344 //
346
347 // geom impl for quadrilaterals (also hexa faces)
348 template <int dummy>
349 class GeometryImpl<dummy, 2, hexa>
350 : public GeometryImplBase< 2, 4, BilinearMapping< cdim > >
351 {
352 protected:
353 // dim = 2, corners = 4
354 typedef GeometryImplBase< 2, 4, BilinearMapping< cdim > > BaseType;
355
356 using BaseType :: corners_ ;
357 using BaseType :: copy ;
358 using BaseType :: coord_ ;
359 using BaseType :: map_ ;
360 using BaseType :: status_ ;
361
362 typedef typename BaseType :: MappingType MappingType ;
363 public:
364 using BaseType :: update ;
365 using BaseType :: valid ;
366
367 // return coordinate vector
368 inline const CoordinateVectorType& operator [] (const int i) const
369 {
370 alugrid_assert ( valid() );
371 alugrid_assert ( i>=0 && i<corners_ );
372 return coord_[i];
373 }
374
375 // update geometry coordinates
376 template <class CoordPtrType>
377 inline void update(const CoordPtrType& p0,
378 const CoordPtrType& p1,
379 const CoordPtrType& p2,
380 const CoordPtrType& p3)
381 {
382 copy(p0, coord_[0] );
383 copy(p1, coord_[1] );
384 copy(p2, coord_[2] );
385 copy(p3, coord_[3] );
386 status_ = updated;
387 }
388
389 // return mapping (always up2date)
390 inline MappingType& mapping()
391 {
392 alugrid_assert ( valid() );
393 if( status_ == buildmapping ) return map_;
394
395 map_.buildMapping( coord_[0], coord_[1], coord_[2], coord_[3] );
396 status_ = buildmapping ;
397 return map_;
398 }
399 };
400
401 // geometry impl for hexahedrons
402 template <int dummy>
403 class GeometryImpl<dummy,3, hexa>
404 : public GeometryImplBase< 3, 8, TrilinearMapping >
405 {
406 protected:
407 // dim = 3, corners = 8
408 typedef GeometryImplBase< 3, 8, TrilinearMapping > BaseType;
409
410 using BaseType :: corners_ ;
411 using BaseType :: copy ;
412 using BaseType :: coord_ ;
413 using BaseType :: map_ ;
414 using BaseType :: status_ ;
415
416 typedef typename BaseType :: MappingType MappingType ;
417 typedef typename BaseType :: CoordinateMatrixType CoordinateMatrixType;
418
419 typedef alu3d_ctype CoordPtrType[cdim];
420
421 // coordinate pointer vector
422 const alu3d_ctype* coordPtr_[ corners_ ];
423 public:
424 using BaseType :: update ;
425 using BaseType :: valid ;
426
428 GeometryImpl() : BaseType()
429 {
430 // set initialize coord pointers
431 for( int i=0; i<corners_; ++i )
432 coordPtr_[ i ] = 0;
433 }
434
435 const alu3d_ctype* point( const int i ) const
436 {
437 alugrid_assert ( valid() );
438 alugrid_assert ( i>=0 && i<corners_ );
439 alugrid_assert ( coordPtr_[i] );
440 return coordPtr_[ i ];
441 }
442
443 // return coordinates
444 inline CoordinateVectorType operator [] (const int i) const
445 {
446 CoordinateVectorType coord ;
447 copy( point( i ), coord );
448 return coord ;
449 }
450
451 // update geometry coordinates
452 inline void update(const CoordPtrType& p0,
453 const CoordPtrType& p1,
454 const CoordPtrType& p2,
455 const CoordPtrType& p3,
456 const CoordPtrType& p4,
457 const CoordPtrType& p5,
458 const CoordPtrType& p6,
459 const CoordPtrType& p7)
460 {
461 coordPtr_[0] = &p0[ 0 ];
462 coordPtr_[1] = &p1[ 0 ];
463 coordPtr_[2] = &p2[ 0 ];
464 coordPtr_[3] = &p3[ 0 ];
465 coordPtr_[4] = &p4[ 0 ];
466 coordPtr_[5] = &p5[ 0 ];
467 coordPtr_[6] = &p6[ 0 ];
468 coordPtr_[7] = &p7[ 0 ];
469 status_ = updated;
470 }
471
472 // update geometry in father coordinates
473 template <class GeometryImp>
474 inline void updateInFather(const GeometryImp &fatherGeom ,
475 const GeometryImp &myGeom)
476 {
477 if( ! coord_ )
478 {
479 coord_.reset( new CoordinateMatrixType() );
480 }
481
482 CoordinateMatrixType& coord = *coord_;
483 // compute the local coordinates in father refelem
484 for(int i=0; i < myGeom.corners() ; ++i)
485 {
486 // calculate coordinate
487 coord[i] = fatherGeom.local( myGeom.corner( i ) );
488
489 // set pointer
490 coordPtr_[i] = (&(coord[i][0]));
491
492 // to avoid rounding errors
493 for(int j=0; j<cdim; ++j)
494 {
495 if ( coord[i][j] < 1e-16) coord[i][j] = 0.0;
496 }
497 }
498
499 status_ = updated ;
500 }
501
502 // return mapping (always up2date)
503 inline MappingType& mapping()
504 {
505 alugrid_assert ( valid() );
506 if( status_ == buildmapping ) return map_;
507
508 map_.buildMapping( point( 0 ), point( 1 ), point( 2 ), point( 3 ),
509 point( 4 ), point( 5 ), point( 6 ), point( 7 ) );
510
511 status_ = buildmapping;
512 return map_;
513 }
514
515 // set status to invalid
516 void invalidate () { status_ = invalid ; }
517
518 // return true if geometry is valid
519 bool valid () const { return status_ != invalid ; }
520 };
521
522
523 // geometry impl for hexahedrons
524 template <int dummy>
525 class GeometryImpl<dummy,3, tetra>
526 : public GeometryImplBase< 3, 4, LinearMapping<cdim, cdim> >
527 {
528 // dim = 3, corners = 8
529 typedef GeometryImplBase< 3, 4, LinearMapping<cdim, cdim> > BaseType;
530
531 using BaseType :: corners_ ;
532 using BaseType :: copy ;
533 using BaseType :: coord_ ;
534 using BaseType :: map_ ;
535 using BaseType :: status_ ;
536
537 typedef typename BaseType :: MappingType MappingType ;
538 typedef typename BaseType :: CoordinateMatrixType CoordinateMatrixType;
539
540 typedef alu3d_ctype CoordPtrType[cdim];
541
542 // coordinate pointer vector
543 const alu3d_ctype* coordPtr_[ corners_ ];
544 public:
545 using BaseType :: update ;
546 using BaseType :: valid ;
547
548 // default constructor
549 GeometryImpl() : BaseType()
550 {
551 // set initialize coord pointers
552 for( int i=0; i<corners_; ++i )
553 coordPtr_[ i ] = 0;
554 }
555
556 const alu3d_ctype* point( const int i ) const
557 {
558 alugrid_assert ( valid() );
559 alugrid_assert ( i>=0 && i<corners_ );
560 alugrid_assert ( coordPtr_[ i ] );
561 return coordPtr_[ i ];
562 }
563
564 // return coordinate vector
565 inline CoordinateVectorType operator [] (const int i) const
566 {
567 CoordinateVectorType coord ;
568 copy( point( i ), coord );
569 return coord ;
570 }
571
572 // update geometry coordinates
573 inline void update(const CoordPtrType& p0,
574 const CoordPtrType& p1,
575 const CoordPtrType& p2,
576 const CoordPtrType& p3)
577 {
578 coordPtr_[0] = &p0[ 0 ];
579 coordPtr_[1] = &p1[ 0 ];
580 coordPtr_[2] = &p2[ 0 ];
581 coordPtr_[3] = &p3[ 0 ];
582 status_ = updated;
583 }
584
585 // update geometry in father coordinates
586 template <class GeometryImp>
587 inline void updateInFather(const GeometryImp &fatherGeom ,
588 const GeometryImp & myGeom)
589 {
590 if( ! coord_ )
591 {
592 coord_.reset(new CoordinateMatrixType());
593 }
594
595 CoordinateMatrixType& coord = *coord_;
596 // compute the local coordinates in father refelem
597 for(int i=0; i < myGeom.corners() ; ++i)
598 {
599 // calculate coordinate
600 coord[i] = fatherGeom.local( myGeom.corner( i ) );
601
602 // set pointer
603 coordPtr_[i] = (&(coord[i][0]));
604
605 // to avoid rounding errors
606 for(int j=0; j<cdim; ++j)
607 {
608 if ( coord[i][j] < 1e-16) coord[i][j] = 0.0;
609 }
610 }
611
612 status_ = updated;
613 }
614
615 // return mapping (always up2date)
616 inline MappingType& mapping()
617 {
618 alugrid_assert ( valid() );
619 if( status_ == buildmapping ) return map_;
620
621 map_.buildMapping( point( 0 ), point( 1 ), point( 2 ), point( 3 ) );
622
623 status_ = buildmapping;
624 return map_;
625 }
626 };
627 }; // end of class ALUGridGeometryImplementation
628
629 template <int mydim, int cdim, class GridImp>
630 class ALU3dGridGeometry :
631 public GeometryDefaultImplementation<mydim, cdim, GridImp, ALU3dGridGeometry>
632 {
633 static const ALU3dGridElementType elementType = GridImp::elementType;
634
635 typedef typename GridImp::MPICommunicatorType Comm;
636
637 //friend class ALU3dGridIntersectionIterator<GridImp>;
638 typedef ALU3dImplTraits< elementType, Comm > ALU3dImplTraitsType ;
639
640 public:
641 typedef typename ALU3dImplTraitsType::IMPLElementType IMPLElementType;
642 typedef typename ALU3dImplTraitsType::GEOFaceType GEOFaceType;
643 typedef typename ALU3dImplTraitsType::GEOEdgeType GEOEdgeType;
644 typedef typename ALU3dImplTraitsType::GEOVertexType GEOVertexType;
645
646 // interface types
647 typedef typename ALU3dImplTraitsType::HFaceType HFaceType;
648 typedef typename ALU3dImplTraitsType::HEdgeType HEdgeType;
649 typedef typename ALU3dImplTraitsType::VertexType VertexType;
650
651
652 typedef ElementTopologyMapping<elementType> ElementTopo;
653 typedef FaceTopologyMapping<elementType> FaceTopo;
654
655 static const int corners_ = (elementType == hexa) ? Dune::power( int(2) , int((mydim> -1) ? mydim : 0 ) ) : mydim+1;
656
657 // type of specialized geometry implementation
658 typedef typename MyALUGridGeometryImplementation<cdim> ::
659 template GeometryImpl<0, mydim, elementType > GeometryImplType;
660
661 public:
662 typedef typename GridImp :: ctype ctype;
663
665 typedef FieldVector<ctype, mydim> LocalCoordinate;
666
668 typedef FieldVector<ctype, cdim > GlobalCoordinate;
669
671 typedef FieldMatrix<ctype,cdim,mydim> JacobianInverseTransposed;
672
674 typedef FieldMatrix< ctype, mydim, cdim > JacobianTransposed;
675
676 // type of coordinate matrix for faces
677 typedef FieldMatrix<ctype,
678 GridImp::dimension == 3 ? EntityCount< elementType > :: numVerticesPerFace : 2 , cdim> FaceCoordinatesType;
679
682 GeometryType type () const;
683
685 int corners () const;
686
688 GlobalCoordinate corner (int i) const;
689
692 GlobalCoordinate global (const LocalCoordinate& local) const;
693
696 LocalCoordinate local (const GlobalCoordinate& global) const;
697
699 ctype integrationElement (const LocalCoordinate& local) const;
700
703 const JacobianInverseTransposed &jacobianInverseTransposed (const LocalCoordinate& local) const;
704
706 const JacobianTransposed& jacobianTransposed (const LocalCoordinate& local) const;
707
709 bool affine () const;
710
712 ctype volume () const;
713
714 //***********************************************************************
716 //***********************************************************************
718 bool buildGeom(const IMPLElementType & item);
719 bool buildGeom(const HFaceType & item, int twist);
720 bool buildGeom(const HEdgeType & item, int twist);
721 bool buildGeom(const VertexType & item, int twist);
722
723 // this method is used by the intersection iterator
724 bool buildGeom(const FaceCoordinatesType& coords);
725
726 // this method is used by the intersection iterator
727 template <class coord_t>
728 bool buildGeom(const coord_t& p0,
729 const coord_t& p1,
730 const coord_t& p2,
731 const coord_t& p3);
732
733 // this method is used by the intersection iterator
734 template <class coord_t>
735 bool buildGeom(const coord_t& p0,
736 const coord_t& p1,
737 const coord_t& p2);
738
739 // this method is used by the intersection iterator
740 template <class coord_t>
741 bool buildGeom(const coord_t& p0,
742 const coord_t& p1);
743
745 template <class Geometry>
746 bool buildGeomInFather(const Geometry &fatherGeom , const Geometry &myGeom);
747
750 void print (std::ostream& ss) const;
751
753 void invalidate () { geoImplPtr_.invalidate(); }
754
756 bool valid () const { return geoImpl().valid(); }
757
758 protected:
759 // return reference to geometry implementation
760 GeometryImplType& geoImpl() const
761 {
762 return *geoImplPtr_;
763 }
764
765 // proxy object holding GeometryImplType* with reference counting
766 mutable ALU3DSPACE SharedPointer< GeometryImplType > geoImplPtr_;
767 };
768
769} // namespace Dune
770
771#include "geometry_imp.cc"
772
773#endif
vector space out of a tensor product of fields.
Definition: fvector.hh:95
general type of geometry implementation
Definition: geometry.hh:195
Different resources needed by all grid implementations.
GeometryType
Type representing VTK's entity geometry types.
Definition: common.hh:132
topology of a Cartesian grid
#define DUNE_THROW(E, m)
Definition: exceptions.hh:218
Some useful basic math stuff.
Dune namespace.
Definition: alignedallocator.hh:13
constexpr Base power(Base m, Exponent p)
Power method for integer exponents.
Definition: math.hh:75
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Jul 15, 22:36, 2024)