]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Make the special ReferenceCell objects objects, not enums.
authorWolfgang Bangerth <bangerth@colostate.edu>
Thu, 21 Jan 2021 01:38:12 +0000 (18:38 -0700)
committerWolfgang Bangerth <bangerth@colostate.edu>
Thu, 21 Jan 2021 01:59:28 +0000 (18:59 -0700)
include/deal.II/grid/reference_cell.h
include/deal.II/grid/tria_accessor.templates.h
include/deal.II/numerics/data_out_dof_data.templates.h
source/distributed/tria_base.cc
source/grid/grid_out.cc
source/grid/reference_cell.cc

index 636e671abb808e0f14a5197c2f02aef2baa779e4..8d6eb834630e4d9aa0fe9960d890a2effb19f1ff 100644 (file)
@@ -51,18 +51,15 @@ namespace ReferenceCell
   class Type
   {
   public:
-    enum CellKinds : std::uint8_t
-    {
-      Vertex  = 0,
-      Line    = 1,
-      Tri     = 2,
-      Quad    = 3,
-      Tet     = 4,
-      Pyramid = 5,
-      Wedge   = 6,
-      Hex     = 7,
-      Invalid = static_cast<std::uint8_t>(-1)
-    };
+    static const Type Vertex;
+    static const Type Line;
+    static const Type Tri;
+    static const Type Quad;
+    static const Type Tet;
+    static const Type Pyramid;
+    static const Type Wedge;
+    static const Type Hex;
+    static const Type Invalid;
 
     /**
      * Default constructor. Initialize this object as an invalid object.
@@ -72,7 +69,7 @@ namespace ReferenceCell
     /**
      * Constructor.
      */
-    Type(const CellKinds kind);
+    Type(const std::uint8_t kind);
 
     /**
      * Return the dimension of the reference cell represented by the current
@@ -155,13 +152,13 @@ namespace ReferenceCell
      * Operator for equality comparison.
      */
     bool
-    operator==(const CellKinds &type) const;
+    operator==(const std::uint8_t &type) const;
 
     /**
      * Operator for inequality comparison.
      */
     bool
-    operator!=(const CellKinds &type) const;
+    operator!=(const std::uint8_t &type) const;
 
     /**
      * Write and read the data of this object from a stream for the purpose
@@ -182,7 +179,7 @@ namespace ReferenceCell
     /**
      * The variable that stores what this object actually corresponds to.
      */
-    CellKinds kind;
+    std::uint8_t kind;
   };
 
 
@@ -193,7 +190,7 @@ namespace ReferenceCell
 
 
 
-  inline Type::Type(const CellKinds kind)
+  inline Type::Type(const std::uint8_t kind)
     : kind(kind)
   {}
 
@@ -223,7 +220,7 @@ namespace ReferenceCell
 
 
   inline bool
-  Type::operator==(const CellKinds &type) const
+  Type::operator==(const std::uint8_t &type) const
   {
     return kind == type;
   }
@@ -231,7 +228,7 @@ namespace ReferenceCell
 
 
   inline bool
-  Type::operator!=(const CellKinds &type) const
+  Type::operator!=(const std::uint8_t &type) const
   {
     return kind != type;
   }
@@ -242,12 +239,7 @@ namespace ReferenceCell
   inline void
   Type::serialize(Archive &archive, const unsigned int /*version*/)
   {
-    // Serialize the state as an 8-bit int. When saving the state, the
-    // last of the following 3 lines is a no-op. When loading, the first
-    // of these lines is a no-op.
-    std::uint8_t kind_as_int = static_cast<std::uint8_t>(kind);
-    archive &    kind_as_int;
-    kind = static_cast<CellKinds>(kind_as_int);
+    archive &kind;
   }
 
 
@@ -323,23 +315,18 @@ namespace ReferenceCell
   inline unsigned int
   Type::get_dimension() const
   {
-    switch (kind)
-      {
-        case Type::Vertex:
-          return 0;
-        case Type::Line:
-          return 1;
-        case Type::Tri:
-        case Type::Quad:
-          return 2;
-        case Type::Tet:
-        case Type::Pyramid:
-        case Type::Wedge:
-        case Type::Hex:
-          return 3;
-        default:
-          return numbers::invalid_unsigned_int;
-      }
+    if (*this == Vertex)
+      return 0;
+    else if (*this == Line)
+      return 1;
+    else if ((*this == Tri) || (*this == Quad))
+      return 2;
+    else if ((*this == Tet) || (*this == Pyramid) || (*this == Wedge) ||
+             (*this == Hex))
+      return 3;
+
+    Assert(false, ExcNotImplemented());
+    return numbers::invalid_unsigned_int;
   }
 
 
@@ -350,29 +337,26 @@ namespace ReferenceCell
   inline std::string
   Type::to_string() const
   {
-    switch (kind)
-      {
-        case Type::Vertex:
-          return "Vertex";
-        case Type::Line:
-          return "Line";
-        case Type::Tri:
-          return "Tri";
-        case Type::Quad:
-          return "Quad";
-        case Type::Tet:
-          return "Tet";
-        case Type::Pyramid:
-          return "Pyramid";
-        case Type::Wedge:
-          return "Wedge";
-        case Type::Hex:
-          return "Hex";
-        case Type::Invalid:
-          return "Invalid";
-        default:
-          Assert(false, ExcNotImplemented());
-      }
+    if (*this == Vertex)
+      return "Vertex";
+    else if (*this == Line)
+      return "Line";
+    else if (*this == Tri)
+      return "Tri";
+    else if (*this == Quad)
+      return "Quad";
+    else if (*this == Tet)
+      return "Tet";
+    else if (*this == Pyramid)
+      return "Pyramid";
+    else if (*this == Wedge)
+      return "Wedge";
+    else if (*this == Hex)
+      return "Hex";
+    else if (*this == Invalid)
+      return "Invalid";
+
+    Assert(false, ExcNotImplemented());
 
     return "Invalid";
   }
@@ -434,8 +418,8 @@ namespace ReferenceCell
     AssertIndexRange(n_vertices, 9);
     const auto X = Type::Invalid;
 
-    static constexpr std::array<std::array<ReferenceCell::Type::CellKinds, 9>,
-                                4>
+    static const std::array<std::array<ReferenceCell::Type, 9>,
+                            4>
       table = {
         {// dim 0
          {{X, Type::Vertex, X, X, X, X, X, X, X}},
index 055cb2b8917b5acb9cd4053760930be92701f786..9a4646649dbff52051d7502b9f1c4acd3f1508d2 100644 (file)
@@ -54,74 +54,68 @@ namespace internal
                                            GeometryInfo<dim>::vertices_per_cell>
         vertices)
     {
-      switch (ReferenceCell::n_vertices_to_type(dim, vertices.size()))
-        {
-          case ReferenceCell::Type::Line:
-            // Return the distance between the two vertices
-            return (vertices[1] - vertices[0]).norm();
-
-          case ReferenceCell::Type::Tri:
-            // Return the longest of the three edges
-            return std::max({(vertices[1] - vertices[0]).norm(),
-                             (vertices[2] - vertices[1]).norm(),
-                             (vertices[2] - vertices[0]).norm()});
-
-          case ReferenceCell::Type::Quad:
-            // Return the longer one of the two diagonals of the quadrilateral
-            return std::max({(vertices[3] - vertices[0]).norm(),
-                             (vertices[2] - vertices[1]).norm()});
-
-          case ReferenceCell::Type::Tet:
-            // Return the longest of the six edges of the tetrahedron
-            return std::max({(vertices[1] - vertices[0]).norm(),
-                             (vertices[2] - vertices[0]).norm(),
-                             (vertices[2] - vertices[1]).norm(),
-                             (vertices[3] - vertices[0]).norm(),
-                             (vertices[3] - vertices[1]).norm(),
-                             (vertices[3] - vertices[2]).norm()});
-
-          case ReferenceCell::Type::Pyramid:
-            // Return ...
-            return std::max({// the longest diagonal of the quadrilateral base
-                             // of the pyramid or ...
-                             (vertices[3] - vertices[0]).norm(),
-                             (vertices[2] - vertices[1]).norm(),
-                             // the longest edge connected with the apex of the
-                             // pyramid
-                             (vertices[4] - vertices[0]).norm(),
-                             (vertices[4] - vertices[1]).norm(),
-                             (vertices[4] - vertices[2]).norm(),
-                             (vertices[4] - vertices[3]).norm()});
-
-          case ReferenceCell::Type::Wedge:
-            // Return ...
-            return std::max({// the longest of the 2*3=6 diagonals of the three
-                             // quadrilateral sides of the wedge or ...
-                             (vertices[4] - vertices[0]).norm(),
-                             (vertices[3] - vertices[1]).norm(),
-                             (vertices[5] - vertices[1]).norm(),
-                             (vertices[4] - vertices[2]).norm(),
-                             (vertices[5] - vertices[0]).norm(),
-                             (vertices[3] - vertices[2]).norm(),
-                             // the longest of the 3*2=6 edges of the two
-                             // triangular faces of the wedge
-                             (vertices[1] - vertices[0]).norm(),
-                             (vertices[2] - vertices[1]).norm(),
-                             (vertices[2] - vertices[0]).norm(),
-                             (vertices[4] - vertices[3]).norm(),
-                             (vertices[5] - vertices[4]).norm(),
-                             (vertices[5] - vertices[3]).norm()});
-
-          case ReferenceCell::Type::Hex:
-            // Return the longest of the four diagonals of the hexahedron
-            return std::max({(vertices[7] - vertices[0]).norm(),
-                             (vertices[6] - vertices[1]).norm(),
-                             (vertices[2] - vertices[5]).norm(),
-                             (vertices[3] - vertices[4]).norm()});
-          default:
-            Assert(false, ExcNotImplemented());
-            return -1e10;
-        }
+      const ReferenceCell::Type reference_cell_type =
+        ReferenceCell::n_vertices_to_type(dim, vertices.size());
+
+      if (reference_cell_type == ReferenceCell::Type::Line)
+        // Return the distance between the two vertices
+        return (vertices[1] - vertices[0]).norm();
+      else if (reference_cell_type == ReferenceCell::Type::Tri)
+        // Return the longest of the three edges
+        return std::max({(vertices[1] - vertices[0]).norm(),
+                         (vertices[2] - vertices[1]).norm(),
+                         (vertices[2] - vertices[0]).norm()});
+      else if (reference_cell_type == ReferenceCell::Type::Quad)
+        // Return the longer one of the two diagonals of the quadrilateral
+        return std::max({(vertices[3] - vertices[0]).norm(),
+                         (vertices[2] - vertices[1]).norm()});
+      else if (reference_cell_type == ReferenceCell::Type::Tet)
+        // Return the longest of the six edges of the tetrahedron
+        return std::max({(vertices[1] - vertices[0]).norm(),
+                         (vertices[2] - vertices[0]).norm(),
+                         (vertices[2] - vertices[1]).norm(),
+                         (vertices[3] - vertices[0]).norm(),
+                         (vertices[3] - vertices[1]).norm(),
+                         (vertices[3] - vertices[2]).norm()});
+      else if (reference_cell_type == ReferenceCell::Type::Pyramid)
+        // Return ...
+        return std::max({// the longest diagonal of the quadrilateral base
+                         // of the pyramid or ...
+                         (vertices[3] - vertices[0]).norm(),
+                         (vertices[2] - vertices[1]).norm(),
+                         // the longest edge connected with the apex of the
+                         // pyramid
+                         (vertices[4] - vertices[0]).norm(),
+                         (vertices[4] - vertices[1]).norm(),
+                         (vertices[4] - vertices[2]).norm(),
+                         (vertices[4] - vertices[3]).norm()});
+      else if (reference_cell_type == ReferenceCell::Type::Wedge)
+        // Return ...
+        return std::max({// the longest of the 2*3=6 diagonals of the three
+                         // quadrilateral sides of the wedge or ...
+                         (vertices[4] - vertices[0]).norm(),
+                         (vertices[3] - vertices[1]).norm(),
+                         (vertices[5] - vertices[1]).norm(),
+                         (vertices[4] - vertices[2]).norm(),
+                         (vertices[5] - vertices[0]).norm(),
+                         (vertices[3] - vertices[2]).norm(),
+                         // the longest of the 3*2=6 edges of the two
+                         // triangular faces of the wedge
+                         (vertices[1] - vertices[0]).norm(),
+                         (vertices[2] - vertices[1]).norm(),
+                         (vertices[2] - vertices[0]).norm(),
+                         (vertices[4] - vertices[3]).norm(),
+                         (vertices[5] - vertices[4]).norm(),
+                         (vertices[5] - vertices[3]).norm()});
+      else if (reference_cell_type == ReferenceCell::Type::Hex)
+        // Return the longest of the four diagonals of the hexahedron
+        return std::max({(vertices[7] - vertices[0]).norm(),
+                         (vertices[6] - vertices[1]).norm(),
+                         (vertices[2] - vertices[5]).norm(),
+                         (vertices[3] - vertices[4]).norm()});
+
+      Assert(false, ExcNotImplemented());
+      return -1e10;
     }
   } // namespace TriaAccessorImplementation
 } // namespace internal
index efa7446471772c16b7f8ac36712f42671f5eac0b..9e56b71f62ecdfa66bb9d392dfc08b4f41bb0293 100644 (file)
@@ -114,27 +114,24 @@ namespace internal
 
           for (const auto &fe : finite_elements)
             for (unsigned int i = 0; i < fe->size(); ++i)
-              switch ((*fe)[i].reference_cell_type())
-                {
-                  case ReferenceCell::Type::Vertex:
-                  case ReferenceCell::Type::Line:
-                  case ReferenceCell::Type::Quad:
-                  case ReferenceCell::Type::Hex:
-                    needs_hypercube_setup |= true;
-                    break;
-                  case ReferenceCell::Type::Tri:
-                  case ReferenceCell::Type::Tet:
-                    needs_simplex_setup |= true;
-                    break;
-                  case ReferenceCell::Type::Wedge:
-                    needs_wedge_setup |= true;
-                    break;
-                  case ReferenceCell::Type::Pyramid:
-                    needs_pyramid_setup |= true;
-                    break;
-                  default:
-                    Assert(false, ExcNotImplemented());
-                }
+              {
+                const auto reference_cell_type = (*fe)[i].reference_cell_type();
+
+                if ((reference_cell_type == ReferenceCell::Type::Vertex) ||
+                    (reference_cell_type == ReferenceCell::Type::Line) ||
+                    (reference_cell_type == ReferenceCell::Type::Quad) ||
+                    (reference_cell_type == ReferenceCell::Type::Hex))
+                  needs_hypercube_setup |= true;
+                else if ((reference_cell_type == ReferenceCell::Type::Tri) ||
+                         (reference_cell_type == ReferenceCell::Type::Tet))
+                  needs_simplex_setup |= true;
+                else if (reference_cell_type == ReferenceCell::Type::Wedge)
+                  needs_wedge_setup |= true;
+                else if (reference_cell_type == ReferenceCell::Type::Pyramid)
+                  needs_pyramid_setup |= true;
+                else
+                  Assert(false, ExcNotImplemented());
+              }
 
           std::unique_ptr<dealii::Quadrature<dim>> quadrature_simplex;
           std::unique_ptr<dealii::Quadrature<dim>> quadrature_hypercube;
@@ -201,27 +198,30 @@ namespace internal
                   dealii::hp::QCollection<dim> quadrature;
 
                   for (unsigned int j = 0; j < finite_elements[i]->size(); ++j)
-                    switch ((*finite_elements[i])[j].reference_cell_type())
-                      {
-                        case ReferenceCell::Type::Vertex:
-                        case ReferenceCell::Type::Line:
-                        case ReferenceCell::Type::Quad:
-                        case ReferenceCell::Type::Hex:
-                          quadrature.push_back(*quadrature_hypercube);
-                          break;
-                        case ReferenceCell::Type::Tri:
-                        case ReferenceCell::Type::Tet:
-                          quadrature.push_back(*quadrature_simplex);
-                          break;
-                        case ReferenceCell::Type::Wedge:
-                          quadrature.push_back(*quadrature_wedge);
-                          break;
-                        case ReferenceCell::Type::Pyramid:
-                          quadrature.push_back(*quadrature_pyramid);
-                          break;
-                        default:
-                          Assert(false, ExcNotImplemented());
-                      }
+                    {
+                      const auto reference_cell_type =
+                        (*finite_elements[i])[j].reference_cell_type();
+
+                      if ((reference_cell_type ==
+                           ReferenceCell::Type::Vertex) ||
+                          (reference_cell_type == ReferenceCell::Type::Line) ||
+                          (reference_cell_type == ReferenceCell::Type::Quad) ||
+                          (reference_cell_type == ReferenceCell::Type::Hex))
+                        quadrature.push_back(*quadrature_hypercube);
+                      else if ((reference_cell_type ==
+                                ReferenceCell::Type::Tri) ||
+                               (reference_cell_type ==
+                                ReferenceCell::Type::Tet))
+                        quadrature.push_back(*quadrature_simplex);
+                      else if (reference_cell_type ==
+                               ReferenceCell::Type::Wedge)
+                        quadrature.push_back(*quadrature_wedge);
+                      else if (reference_cell_type ==
+                               ReferenceCell::Type::Pyramid)
+                        quadrature.push_back(*quadrature_pyramid);
+                      else
+                        Assert(false, ExcNotImplemented());
+                    }
 
                   x_fe_values[i] =
                     std::make_shared<dealii::hp::FEValues<dim, spacedim>>(
index c605c37f04a45524abd7ab4b5f9f9f181794cd32..a5cb1085134908fbae5b009f76724aca65796171 100644 (file)
@@ -309,8 +309,7 @@ namespace parallel
     // transform back and store result
     this->reference_cell_types.clear();
     for (const auto &i : reference_cell_types_ui)
-      this->reference_cell_types.emplace_back(
-        static_cast<ReferenceCell::Type::CellKinds>(i));
+      this->reference_cell_types.emplace_back(static_cast<std::uint8_t>(i));
   }
 
 
index fadc5d4fb5d26735e307e6648ad427d65a45ed39..ed8ac97d70145bef1ae140d77983f50e1348efb7 100644 (file)
@@ -3218,29 +3218,25 @@ GridOut::write_vtk(const Triangulation<dim, spacedim> &tria,
         for (const unsigned int i : cell->vertex_indices())
           {
             out << ' ';
-            switch (cell->reference_cell_type())
+            const auto reference_cell_type = cell->reference_cell_type();
+
+            if ((reference_cell_type == ReferenceCell::Type::Vertex) ||
+                (reference_cell_type == ReferenceCell::Type::Line) ||
+                (reference_cell_type == ReferenceCell::Type::Quad) ||
+                (reference_cell_type == ReferenceCell::Type::Hex))
+              out << cell->vertex_index(GeometryInfo<dim>::ucd_to_deal[i]);
+            else if ((reference_cell_type == ReferenceCell::Type::Tri) ||
+                     (reference_cell_type == ReferenceCell::Type::Tet) ||
+                     (reference_cell_type == ReferenceCell::Type::Wedge))
+              out << cell->vertex_index(i);
+            else if (reference_cell_type == ReferenceCell::Type::Pyramid)
               {
-                case ReferenceCell::Type::Vertex:
-                case ReferenceCell::Type::Line:
-                case ReferenceCell::Type::Quad:
-                case ReferenceCell::Type::Hex:
-                  out << cell->vertex_index(GeometryInfo<dim>::ucd_to_deal[i]);
-                  break;
-                case ReferenceCell::Type::Tri:
-                case ReferenceCell::Type::Tet:
-                case ReferenceCell::Type::Wedge:
-                  out << cell->vertex_index(i);
-                  break;
-                case ReferenceCell::Type::Pyramid:
-                  {
-                    static const std::array<unsigned int, 5> permutation_table{
-                      {0, 1, 3, 2, 4}};
-                    out << cell->vertex_index(permutation_table[i]);
-                    break;
-                  }
-                default:
-                  Assert(false, ExcNotImplemented());
+                static const std::array<unsigned int, 5> permutation_table{
+                  {0, 1, 3, 2, 4}};
+                out << cell->vertex_index(permutation_table[i]);
               }
+            else
+              Assert(false, ExcNotImplemented());
           }
         out << '\n';
       }
index d7b2e33f1ab849a66f524d500d0e66875f4b4074..9e473a05923de5f9ebc4483ecb659ba5e8aa4ce4 100644 (file)
@@ -32,6 +32,17 @@ DEAL_II_NAMESPACE_OPEN
 
 namespace ReferenceCell
 {
+  const Type Type::Vertex  = Type(0);
+  const Type Type::Line    = Type(1);
+  const Type Type::Tri     = Type(2);
+  const Type Type::Quad    = Type(3);
+  const Type Type::Tet     = Type(4);
+  const Type Type::Pyramid = Type(5);
+  const Type Type::Wedge   = Type(6);
+  const Type Type::Hex     = Type(7);
+  const Type Type::Invalid = Type(static_cast<std::uint8_t>(-1));
+
+
   template <int dim, int spacedim>
   void
   make_triangulation(const Type &                  reference_cell,

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.