DUNE PDELab (2.7)

type.hh
Go to the documentation of this file.
1// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2// vi: set et ts=4 sw=2 sts=2:
3#ifndef DUNE_GEOMETRY_TYPE_HH
4#define DUNE_GEOMETRY_TYPE_HH
5
10#include <cassert>
11#include <cstdint>
12
13#include <string>
14
19#include <dune/common/unused.hh>
20
21namespace Dune
22{
23
24 // forward declaration needed for deprecated makeFromVertices
25 class GeometryType;
26 GeometryType geometryTypeFromVertexCount(unsigned int dim, unsigned int vertices);
27
28 namespace Impl
29 {
30
31 enum TopologyConstruction { pyramidConstruction = 0, prismConstruction = 1 };
32
33
34
35 // Basic Topology Types
36 // --------------------
37
38 struct Point
39 {
40 static const unsigned int dimension = 0;
41 static const unsigned int numCorners = 1;
42
43 static const unsigned int id = 0;
44
45 static std::string name () { return "p"; }
46 };
47
48
49 template< class BaseTopology >
50 struct Prism
51 {
52 static const unsigned int dimension = BaseTopology::dimension + 1;
53 static const unsigned int numCorners = 2 * BaseTopology::numCorners;
54
55 static const unsigned int id = BaseTopology::id | ((unsigned int)prismConstruction << (dimension-1));
56
57 static std::string name () { return BaseTopology::name() + "l"; }
58 };
59
60
61 template< class BaseTopology >
62 struct Pyramid
63 {
64 static const unsigned int dimension = BaseTopology::dimension + 1;
65 static const unsigned int numCorners = BaseTopology::numCorners + 1;
66
67 static const unsigned int id = BaseTopology::id | ((unsigned int)pyramidConstruction << (dimension-1));
68
69 static std::string name () { return BaseTopology::name() + "o"; }
70 };
71
72
73
74 // Properties of Topologies
75 // ------------------------
76
77 template< class Topology >
78 struct IsSimplex
79 : public std::integral_constant< bool, (Topology::id >> 1) == 0 >
80 {};
81
82 template< class Topology >
83 struct IsCube
84 : public std::integral_constant< bool, (Topology::id | 1) == (1 << Topology::dimension) - 1 >
85 {};
86
87
88
89 // Dynamic Topology Properties
90 // ---------------------------
91
100 inline static unsigned int numTopologies ( int dim ) noexcept
101 {
102 return (1u << dim);
103 }
104
116 inline bool static isPyramid ( unsigned int topologyId, int dim, int codim = 0 ) noexcept
117 {
118 assert( (dim > 0) && (topologyId < numTopologies( dim )) );
119 assert( (0 <= codim) && (codim < dim) );
120 return (((topologyId & ~1) & (1u << (dim-codim-1))) == 0);
121 }
122
134 inline static bool isPrism ( unsigned int topologyId, int dim, int codim = 0 ) noexcept
135 {
136 assert( (dim > 0) && (topologyId < numTopologies( dim )) );
137 assert( (0 <= codim) && (codim < dim) );
138 return (( (topologyId | 1) & (1u << (dim-codim-1))) != 0);
139 }
140
153 inline static bool isTopology ( TopologyConstruction construction, unsigned int topologyId, int dim, int codim = 0 ) noexcept
154 {
155 assert( (dim > 0) && (topologyId < numTopologies( dim )) );
156 assert( (0 <= codim) && (codim <= dim) );
157 return (codim >= (dim-1)) || (((topologyId >> (dim-codim-1)) & 1) == (unsigned int)construction);
158 }
159
167 inline static unsigned int baseTopologyId ( unsigned int topologyId, int dim, int codim = 1 ) noexcept
168 {
169 assert( (dim >= 0) && (topologyId < numTopologies( dim )) );
170 assert( (0 <= codim) && (codim <= dim) );
171 return topologyId & ((1u << (dim-codim)) - 1);
172 }
173
174
175
176 // SimplexTopology
177 // ---------------
178
179 template< unsigned int dim >
180 struct SimplexTopology
181 {
182 typedef Pyramid< typename SimplexTopology< dim-1 >::type > type;
183 };
184
185 template<>
186 struct SimplexTopology< 0 >
187 {
188 typedef Point type;
189 };
190
191
192
193 // CubeTopology
194 // ------------
195
196 template< unsigned int dim >
197 struct CubeTopology
198 {
199 typedef Prism< typename CubeTopology< dim-1 >::type > type;
200 };
201
202 template<>
203 struct CubeTopology< 0 >
204 {
205 typedef Point type;
206 };
207
208
209
210 // PyramidTopology
211 // ---------------
212
213 template< unsigned int dim >
214 struct PyramidTopology
215 {
216 typedef Pyramid< typename CubeTopology< dim-1 >::type > type;
217 };
218
219
220
221 // PrismTopology
222 // -------------
223
224 template< unsigned int dim >
225 struct PrismTopology
226 {
227 typedef Prism< typename SimplexTopology< dim-1 >::type > type;
228 };
229
230
231
232
233 // IfTopology
234 // ----------
235
236 template< template< class > class Operation, int dim, class Topology = Point >
237 struct IfTopology
238 {
239 template< class... Args >
240 static auto apply ( unsigned int topologyId, Args &&... args )
241 {
242 if( topologyId & 1 )
243 return IfTopology< Operation, dim-1, Prism< Topology > >::apply( topologyId >> 1, std::forward< Args >( args )... );
244 else
245 return IfTopology< Operation, dim-1, Pyramid< Topology > >::apply( topologyId >> 1, std::forward< Args >( args )... );
246 }
247 };
248
249 template< template< class > class Operation, class Topology >
250 struct IfTopology< Operation, 0, Topology >
251 {
252 template< class... Args >
253 static auto apply ( unsigned int topologyId, Args &&... args )
254 {
255 DUNE_UNUSED_PARAMETER( topologyId );
256 return Operation< Topology >::apply( std::forward< Args >( args )... );
257 }
258 };
259
260 } // namespace Impl
261
262
263
264 // GeometryType
265 // -------------
266
279 class GeometryType
280 {
281 public:
282
285 enum
286 BasicType {
287 simplex,
288 cube,
289 pyramid,
290 prism,
291 extended,
292 none
293 };
294
295 private:
296
298 unsigned char dim_;
299
301 bool none_;
302
304 unsigned int topologyId_;
305
306 // Internal type used for the Id. The exact nature of this type is kept
307 // as an implementation detail on purpose. We use a scoped enum here because scoped enums
308 // can be used as template parameters, but are not implicitly converted to other integral
309 // types by the compiler. That way, we avoid unfortunate implicit conversion chains, e.g.
310 // people trying to work with GlobalGeometryTypeIndex, but forgetting to actually call
311 // GlobalGeometryTypeIndex::index(gt) and just using gt directly.
312 enum class IdType : std::uint64_t
313 {};
314
315 public:
316
347 using Id = IdType;
348
356 constexpr operator Id() const
357 {
358 // recreate the exact storage layout that this class is using, making conversion
359 // extremely cheap
360 std::uint64_t id = dim_ | (std::uint64_t(none_) << 8) | (std::uint64_t(topologyId_) << 32);
361 return static_cast<Id>(id);
362 }
363
371 constexpr GeometryType(Id id)
372 : dim_(static_cast<std::uint64_t>(id) & 0xFF)
373 , none_(static_cast<std::uint64_t>(id) & 0x100)
374 , topologyId_(static_cast<std::uint64_t>(id) >> 32)
375 {}
376
379
381 constexpr GeometryType ()
382 : dim_(0), none_(true), topologyId_(0)
383 {}
384
385 DUNE_NO_DEPRECATED_BEGIN
387 GeometryType(BasicType basicType, unsigned int dim)
388 DUNE_DEPRECATED_MSG("The GeometryType constructor taking BasicType is deprecated and will be removed after DUNE 2.6")
389 : dim_(dim), none_((basicType == GeometryType::none) ? true : false), topologyId_(0)
390 {
391 if (dim < 2)
392 return;
393 switch( basicType )
394 {
395 case GeometryType::simplex :
396 topologyId_ = 0;
397 break;
398 case GeometryType::cube :
399 topologyId_ = ((1 << dim) - 1);
400 break;
401 case GeometryType::pyramid :
402 if (dim == 3)
403 topologyId_ = 0b0011;
404 else
405 DUNE_THROW( RangeError,
406 "Invalid basic geometry type: no pyramids for dimension " << dim << "." );
407 break;
408 case GeometryType::prism :
409 if (dim == 3)
410 topologyId_ = 0b0101;
411 else
412 DUNE_THROW( RangeError,
413 "Invalid basic geometry type: no prisms for dimension " << dim << "." );
414 break;
415 case GeometryType::none :
416 break;
417 default :
418 DUNE_THROW( RangeError,
419 "Invalid basic geometry type: " << basicType << " for dimension " << dim << "." );
420 }
421 }
422 DUNE_NO_DEPRECATED_END
423
430 constexpr GeometryType(unsigned int topologyId, unsigned int dim, bool none)
431 : dim_(dim), none_(none), topologyId_(topologyId)
432 {}
433
439 constexpr GeometryType(unsigned int topologyId, unsigned int dim)
440 : dim_(dim), none_(false), topologyId_(topologyId)
441 {}
442
453 template<class TopologyType,
454 class = Dune::void_t<decltype(TopologyType::dimension), decltype(TopologyType::id)>>
455 explicit GeometryType(TopologyType t)
456 : dim_(TopologyType::dimension), none_(false), topologyId_(TopologyType::id)
457 {
458 DUNE_UNUSED_PARAMETER(t);
459 }
460
462 DUNE_DEPRECATED_MSG("GeometryType(unsigned dim) is deprecated in DUNE 2.7, please use Dune::GeometryTypes::cube(dim) instead")
463 explicit GeometryType(unsigned int dim)
464 : dim_(dim), none_(false), topologyId_(0)
465 {
466 assert(dim < 2);
467 }
468
470 // We need this constructor for "int" and "unsigned int",
471 // because otherwise GeometryType(int) would try to call the
472 // generic GeometryType(TopologyType) constructor
473 DUNE_DEPRECATED_MSG("GeometryType(dim) is deprecated in DUNE 2.7, please use Dune::GeometryTypes::cube(dim) instead")
474 explicit GeometryType(int dim)
475 : dim_(dim), none_(false), topologyId_(0)
476 {
477 assert(dim < 2);
478 }
479
485
487 DUNE_DEPRECATED_MSG("makeVertex() is deprecated in DUNE 2.6, please use Dune::GeometryTypes::vertex instead")
488 void makeVertex() {
489 none_ = false;
490 dim_ = 0;
491 topologyId_ = 0;
492 }
493
495 DUNE_DEPRECATED_MSG("makeLine() is deprecated in DUNE 2.6, please use Dune::GeometryTypes::line instead")
496 void makeLine() {
497 none_ = false;
498 dim_ = 1;
499 topologyId_ = 0;
500 }
501
503 DUNE_DEPRECATED_MSG("makeTriangle() is deprecated in DUNE 2.6, please use Dune::GeometryTypes::triangle instead")
504 void makeTriangle() {
505 none_ = false;
506 dim_ = 2;
507 topologyId_ = 0;
508 }
509
511 DUNE_DEPRECATED_MSG("makeQuadrilateral() is deprecated in DUNE 2.6, please use Dune::GeometryTypes::quadrilateral instead")
512 void makeQuadrilateral() {
513 none_ = false;
514 dim_ = 2;
515 topologyId_ = 0b0011;
516 }
517
519 DUNE_DEPRECATED_MSG("makeTetrahedron() is deprecated in DUNE 2.6, please use Dune::GeometryTypes::tetrahedron instead")
520 void makeTetrahedron() {
521 none_ = false;
522 dim_ = 3;
523 topologyId_ = 0;
524 }
525
527 DUNE_DEPRECATED_MSG("makePyramid() is deprecated in DUNE 2.6, please use Dune::GeometryTypes::pyramid instead")
528 void makePyramid() {
529 none_ = false;
530 dim_ = 3;
531 topologyId_ = 0b0011;
532 }
533
535 DUNE_DEPRECATED_MSG("makePrism() is deprecated in DUNE 2.6, please use Dune::GeometryTypes::prism instead")
536 void makePrism() {
537 none_ = false;
538 dim_ = 3;
539 topologyId_ = 0b0101; // (1 << (dim_-1)) - 1;
540 }
541
543 DUNE_DEPRECATED_MSG("makeHexahedron() is deprecated in DUNE 2.6, please use Dune::GeometryTypes::hexahedron instead")
544 void makeHexahedron() {
545 none_ = false;
546 dim_ = 3;
547 topologyId_ = 0b0111;
548 }
549
551 DUNE_DEPRECATED_MSG("makeSimplex(dim) is deprecated in DUNE 2.6, please use Dune::GeometryTypes::simplex(dim) instead")
552 void makeSimplex(unsigned int dim) {
553 none_ = false;
554 dim_ = dim;
555 topologyId_ = 0;
556 }
557
559 DUNE_DEPRECATED_MSG("makeCube(dim) is deprecated in DUNE 2.6, please use Dune::GeometryTypes::cube(dim) instead")
560 void makeCube(unsigned int dim) {
561 none_ = false;
562 dim_ = dim;
563 topologyId_ = ((dim>1) ? ((1 << dim) - 1) : 0);
564 }
565
567 DUNE_DEPRECATED_MSG("makeNone(dim) is deprecated in DUNE 2.6, please use Dune::GeometryTypes::none(dim) instead")
568 void makeNone(unsigned int dim) {
569 none_ = true;
570 dim_ = dim;
571 topologyId_ = 0;
572 }
573
578 void makeFromVertices(unsigned int dim, unsigned int vertices) DUNE_DEPRECATED_MSG("Use the utility function geometryTypeFromVertexCount(...) instead.")
579 {
580 *this = geometryTypeFromVertexCount(dim, vertices);
581 return;
582 }
583
590 constexpr bool isVertex() const {
591 return dim_==0;
592 }
593
595 constexpr bool isLine() const {
596 return dim_==1;
597 }
598
600 constexpr bool isTriangle() const {
601 return ! none_ && dim_==2 && (topologyId_ | 1) == 0b0001;
602 }
603
605 constexpr bool isQuadrilateral() const {
606 return ! none_ && dim_==2 && (topologyId_ | 1) == 0b0011;
607 }
608
610 constexpr bool isTetrahedron() const {
611 return ! none_ && dim_==3 && (topologyId_ | 1) == 0b0001;
612 }
613
615 constexpr bool isPyramid() const {
616 return ! none_ && dim_==3 && (topologyId_ | 1) == 0b0011;
617 }
618
620 constexpr bool isPrism() const {
621 return ! none_ && dim_==3 && (topologyId_ | 1) == 0b0101;
622 }
623
625 constexpr bool isHexahedron() const {
626 return ! none_ && dim_==3 && (topologyId_ | 1) == 0b0111;
627 }
628
630 constexpr bool isSimplex() const {
631 return ! none_ && (topologyId_ | 1) == 1;
632 }
633
635 constexpr bool isCube() const {
636 return ! none_ && ((topologyId_ ^ ((1 << dim_)-1)) >> 1 == 0);
637 }
638
640 constexpr bool isNone() const {
641 return none_;
642 }
643
645 constexpr unsigned int dim() const {
646 return dim_;
647 }
648
650 constexpr unsigned int id() const {
651 return topologyId_;
652 }
653
662 constexpr bool operator==(const GeometryType& other) const {
663 return ( ( none_ == other.none_ )
664 && ( ( none_ == true )
665 || ( ( dim_ == other.dim_ )
666 && ( (topologyId_ >> 1) == (other.topologyId_ >> 1) )
667 )
668 )
669 );
670 }
671
673 constexpr bool operator!=(const GeometryType& other) const {
674 return ! ((*this)==other);
675 }
676
678 constexpr bool operator < (const GeometryType& other) const {
679 return ( ( none_ < other.none_ )
680 || ( !( other.none_ < none_ )
681 && ( ( dim_ < other.dim_ )
682 || ( (other.dim_ == dim_)
683 && ((topologyId_ >> 1) < (other.topologyId_ >> 1) )
684 )
685 )
686 )
687 );
688 }
689
692 };
693
695 inline std::ostream& operator<< (std::ostream& s, const GeometryType& a)
696 {
697 if (a.isSimplex())
698 {
699 s << "(simplex, " << a.dim() << ")";
700 return s;
701 }
702 if (a.isCube())
703 {
704 s << "(cube, " << a.dim() << ")";
705 return s;
706 }
707 if (a.isPyramid())
708 {
709 s << "(pyramid, 3)";
710 return s;
711 }
712 if (a.isPrism())
713 {
714 s << "(prism, 3)";
715 return s;
716 }
717 if (a.isNone())
718 {
719 s << "(none, " << a.dim() << ")";
720 return s;
721 }
722 s << "(other [" << a.id() << "], " << a.dim() << ")";
723 return s;
724 }
725
726 DUNE_NO_DEPRECATED_BEGIN
728 inline std::ostream& operator<< (std::ostream& s, GeometryType::BasicType type)
729 {
730 switch (type) {
731 case GeometryType::simplex :
732 s << "simplex";
733 break;
734 case GeometryType::cube :
735 s << "cube";
736 break;
737 case GeometryType::pyramid :
738 s << "pyramid";
739 break;
740 case GeometryType::prism :
741 s << "prism";
742 break;
743 case GeometryType::extended :
744 s << "other";
745 case GeometryType::none :
746 s << "none";
747 break;
748 default :
749 DUNE_THROW(Exception, "invalid GeometryType::BasicType");
750 }
751 return s;
752 }
753 DUNE_NO_DEPRECATED_END
754
755
757
761 namespace GeometryTypes {
762
764
767 inline constexpr GeometryType simplex(unsigned int dim)
768 {
769 return GeometryType(0,dim,false);
770 }
771
773
776 inline constexpr GeometryType cube(unsigned int dim)
777 {
778 return GeometryType(((dim>1) ? ((1 << dim) - 1) : 0),dim,false);
779 }
780
782
785 inline constexpr GeometryType none(unsigned int dim)
786 {
787 return GeometryType(0,dim,true);
788 }
789
790#ifndef __cpp_inline_variables
791 namespace {
792#endif
793
795
798 DUNE_INLINE_VARIABLE constexpr GeometryType vertex = GeometryType(0,0,false);
799
801
804 DUNE_INLINE_VARIABLE constexpr GeometryType line = GeometryType(0,1,false);
805
807
810 DUNE_INLINE_VARIABLE constexpr GeometryType triangle = simplex(2);
811
813
816 DUNE_INLINE_VARIABLE constexpr GeometryType quadrilateral = cube(2);
817
819
822 DUNE_INLINE_VARIABLE constexpr GeometryType tetrahedron = simplex(3);
823
825
828 DUNE_INLINE_VARIABLE constexpr GeometryType pyramid = GeometryType(0b0011,3,false);
829
831
834 DUNE_INLINE_VARIABLE constexpr GeometryType prism = GeometryType(0b0101,3,false);
835
837
840 DUNE_INLINE_VARIABLE constexpr GeometryType hexahedron = cube(3);
841
842#ifndef __cpp_inline_variables
843 }
844#endif
845
846 }
847
848
849} // namespace Dune
850
851// include utility header needed for deprecated makeFromVertices
852#include "utility/typefromvertexcount.hh"
853
854#endif // DUNE_GEOMETRY_TYPE_HH
Definition of the DUNE_DEPRECATED macro for the case that config.h is not available.
A few common exception classes.
Traits for type conversions and type information.
GeometryType
Type representing VTK's entity geometry types.
Definition: common.hh:180
Definitions of several macros that conditionally make C++ syntax available.
Dune namespace.
Definition: alignedallocator.hh:14
GeometryType geometryTypeFromVertexCount(unsigned int dim, unsigned int vertices)
Utitlity function to construct the correct geometry type given the dimension and the number of vertic...
Definition: typefromvertexcount.hh:15
Definition of the DUNE_UNUSED macro for the case that config.h is not available.
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Jul 15, 22:36, 2024)