LCOV - code coverage report
Current view: top level - src/geom - reference_elem.C (source / functions) Hit Total Coverage
Test: libMesh/libmesh: #4620 (a489da) with base 3f0912 Lines: 196 202 97.0 %
Date: 2026-11-19 19:20:12 Functions: 6 6 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : // The libMesh Finite Element Library.
       2             : // Copyright (C) 2002-2026 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
       3             : 
       4             : // This library is free software; you can redistribute it and/or
       5             : // modify it under the terms of the GNU Lesser General Public
       6             : // License as published by the Free Software Foundation; either
       7             : // version 2.1 of the License, or (at your option) any later version.
       8             : 
       9             : // This library is distributed in the hope that it will be useful,
      10             : // but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      12             : // Lesser General Public License for more details.
      13             : 
      14             : // You should have received a copy of the GNU Lesser General Public
      15             : // License along with this library; if not, write to the Free Software
      16             : // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
      17             : 
      18             : 
      19             : 
      20             : // Local includes
      21             : #include "libmesh/node.h"
      22             : #include "libmesh/elem.h"
      23             : #include "libmesh/reference_elem.h"
      24             : #include "libmesh/libmesh_singleton.h"
      25             : #include "libmesh/threads.h"
      26             : #include "libmesh/enum_to_string.h"
      27             : #include "libmesh/enum_elem_type.h"
      28             : #include "libmesh/fuzzy_equals.h"
      29             : 
      30             : // C++ includes
      31             : #include <map>
      32             : #include <sstream>
      33             : #include <memory> // std::unique_ptr
      34             : #include <cmath>  // std::sqrt, std::cbrt
      35             : #include <utility> // std::pair
      36             : #include <vector>
      37             : 
      38             : 
      39             : //-----------------------------------------------
      40             : // anonymous namespace for implementation details
      41             : namespace
      42             : {
      43             : using namespace libMesh;
      44             : 
      45             : namespace ElemDataStrings
      46             : {
      47             : // GCC 5.2.0 warns about overlength strings in the auto-generated
      48             : // reference_elem.data file.
      49             : #pragma GCC diagnostic ignored "-Woverlength-strings"
      50             : #include "reference_elem.data"
      51             : #pragma GCC diagnostic warning "-Woverlength-strings"
      52             : }
      53             : 
      54             : typedef Threads::spin_mutex InitMutex;
      55             : 
      56             : // Mutex for thread safety.
      57             : InitMutex init_mtx;
      58             : 
      59             : // map from ElemType to reference element file system object name
      60             : typedef std::map<ElemType, const char *> FileMapType;
      61             : FileMapType ref_elem_file;
      62             : Elem * ref_elem_map[INVALID_ELEM];
      63             : 
      64             : 
      65             : 
      66          14 : class SingletonCache : public libMesh::Singleton
      67             : {
      68             : public:
      69          72 :   virtual ~SingletonCache() = default;
      70             : 
      71             :   std::vector<std::unique_ptr<Node>> node_list;
      72             :   std::vector<std::unique_ptr<Elem>> elem_list;
      73             : };
      74             : 
      75             : // From [0], regarding the lifetime of the singleton_cache variable:
      76             : //
      77             : //     "All variables at namespace level, including the anonymous
      78             : //     namespace and function local static variable have static
      79             : //     storage duration unless they are declared thread_local."
      80             : //
      81             : // Variables with static storage duration are destroyed at the end of
      82             : // the program execution. From [1],
      83             : //
      84             : //     "If it is a pointer to the data which is static ... then like all
      85             : //     other dynamically allocated data, it will only be destructed when
      86             : //     you delete it.  There are two frequent solutions:
      87             : //     * use a smart pointer, which has a destructor which deletes it, or
      88             : //     * don't delete it; in most cases, there's really no reason to call the
      89             : //       destructor, and if you happen to use the instance in the destructors
      90             : //       of other static objects, you'll run into an order of destruction
      91             : //       problem."
      92             : //
      93             : // I tried making the singleton_cache a std::unique_ptr, but this resulted
      94             : // in a segfault during program shutdown which appeared to come from the
      95             : // single_cache unique_ptr's destructor. I didn't investigate whether the
      96             : // issue was caused by a double deletion or what, but it appears that the
      97             : // first suggestion above may not be valid in general.
      98             : //
      99             : // [0]: https://stackoverflow.com/questions/24342393/how-anonymous-namespaces-avoids-making-global-static-variable
     100             : // [1]: https://stackoverflow.com/questions/6850009/c-deleting-static-data
     101             : SingletonCache * singleton_cache = nullptr;
     102             : 
     103             : 
     104             : 
     105        1118 : void read_ref_elem (const ElemType type_in,
     106             :                     std::istream & in)
     107             : {
     108         364 :   libmesh_assert (singleton_cache != nullptr);
     109             : 
     110         364 :   std::string dummy;
     111             :   unsigned int n_elem, n_nodes, elem_type_read, nn;
     112             :   double x, y, z;
     113             : 
     114        1118 :   in >> dummy;
     115        1118 :   in >> n_elem;  /**/ std::getline (in, dummy); libmesh_assert_equal_to (n_elem, 1);
     116        1118 :   in >> n_nodes; /**/ std::getline (in, dummy);
     117        1118 :   in >> dummy;   /**/ std::getline (in, dummy);
     118        1118 :   in >> dummy;   /**/ std::getline (in, dummy);
     119        1118 :   in >> dummy;   /**/ std::getline (in, dummy);
     120        1118 :   in >> dummy;   /**/ std::getline (in, dummy);
     121        1118 :   in >> n_elem;  /**/ std::getline (in, dummy); libmesh_assert_equal_to (n_elem, 1);
     122             : 
     123         364 :   in >> elem_type_read;
     124             : 
     125         364 :   libmesh_assert_less (elem_type_read, INVALID_ELEM);
     126         364 :   libmesh_assert_equal_to (elem_type_read, static_cast<unsigned int>(type_in));
     127         364 :   libmesh_assert_equal_to (n_nodes, Elem::type_to_n_nodes_map[elem_type_read]);
     128             : 
     129             :   // Construct elem of appropriate type, store in the elem_list
     130        1482 :   auto & uelem = singleton_cache->elem_list.emplace_back(Elem::build(type_in));
     131             : 
     132             :   // We are expecting an identity map, so assert it!
     133       12986 :   for (unsigned int n=0; n<n_nodes; n++)
     134             :     {
     135        3864 :       in >> nn;
     136        3864 :       libmesh_assert_equal_to (n,nn);
     137             :     }
     138             : 
     139       12986 :   for (unsigned int n=0; n<n_nodes; n++)
     140             :     {
     141        3864 :       in >> x >> y >> z;
     142             : 
     143             :       auto & new_node =
     144       15732 :         singleton_cache->node_list.emplace_back(Node::build(x,y,z,n));
     145             : 
     146       15732 :       uelem->set_node(n, new_node.get());
     147             :     }
     148             : 
     149             :   // it is entirely possible we ran out of file or encountered
     150             :   // another error.  If so, throw an error.
     151        1482 :   libmesh_error_msg_if(!in, "ERROR while creating element singleton!");
     152             : 
     153             :   // Also store a pointer to the newly created Elem in the ref_elem_map array.
     154        1118 :   ref_elem_map[type_in] = uelem.get();
     155        1118 : }
     156             : 
     157             : 
     158             : 
     159      137621 : void init_ref_elem_table()
     160             : {
     161             :   // outside mutex - if this pointer is set, we can trust it.
     162      137621 :   if (singleton_cache != nullptr)
     163       34680 :     return;
     164             : 
     165             :   // playing with fire here - lock before touching shared
     166             :   // data structures
     167          14 :   InitMutex::scoped_lock lock(init_mtx);
     168             : 
     169             :   // inside mutex - pointer may have changed while waiting
     170             :   // for the lock to acquire, check it again.
     171          43 :   if (singleton_cache != nullptr)
     172           0 :     return;
     173             : 
     174             :   // OK, if we get here we have the lock and we are not
     175             :   // initialized.  populate singleton. Note that we do not
     176             :   // use a smart pointer to manage the singleton_cache variable
     177             :   // since variables with static storage duration are destroyed
     178             :   // automatically at the end of program execution.
     179          57 :   singleton_cache = new SingletonCache;
     180             : 
     181             :   // initialize the reference file table
     182             :   {
     183          14 :     ref_elem_file.clear();
     184             : 
     185             :     // 0D elements
     186          43 :     ref_elem_file[NODEELEM] = ElemDataStrings::one_nodeelem;
     187             : 
     188             :     // 1D elements
     189          43 :     ref_elem_file[EDGE2]    = ElemDataStrings::one_edge;
     190          43 :     ref_elem_file[EDGE3]    = ElemDataStrings::one_edge3;
     191          43 :     ref_elem_file[EDGE4]    = ElemDataStrings::one_edge4;
     192             : 
     193             :     // 2D elements
     194          43 :     ref_elem_file[TRI3]     = ElemDataStrings::one_tri;
     195          43 :     ref_elem_file[TRI6]     = ElemDataStrings::one_tri6;
     196          43 :     ref_elem_file[TRI7]     = ElemDataStrings::one_tri7;
     197             : 
     198          43 :     ref_elem_file[QUAD4]    = ElemDataStrings::one_quad;
     199          43 :     ref_elem_file[QUAD8]    = ElemDataStrings::one_quad8;
     200          43 :     ref_elem_file[QUAD9]    = ElemDataStrings::one_quad9;
     201          43 :     ref_elem_file[QUAD16]   = ElemDataStrings::one_quad16;
     202             : 
     203             :     // 3D elements
     204          43 :     ref_elem_file[HEX8]     = ElemDataStrings::one_hex;
     205          43 :     ref_elem_file[HEX20]    = ElemDataStrings::one_hex20;
     206          43 :     ref_elem_file[HEX27]    = ElemDataStrings::one_hex27;
     207             : 
     208          43 :     ref_elem_file[TET4]     = ElemDataStrings::one_tet;
     209          43 :     ref_elem_file[TET10]    = ElemDataStrings::one_tet10;
     210          43 :     ref_elem_file[TET14]    = ElemDataStrings::one_tet14;
     211             : 
     212          43 :     ref_elem_file[PRISM6]   = ElemDataStrings::one_prism;
     213          43 :     ref_elem_file[PRISM15]  = ElemDataStrings::one_prism15;
     214          43 :     ref_elem_file[PRISM18]  = ElemDataStrings::one_prism18;
     215          43 :     ref_elem_file[PRISM20]  = ElemDataStrings::one_prism20;
     216          43 :     ref_elem_file[PRISM21]  = ElemDataStrings::one_prism21;
     217             : 
     218          43 :     ref_elem_file[PYRAMID5] = ElemDataStrings::one_pyramid;
     219          43 :     ref_elem_file[PYRAMID13] = ElemDataStrings::one_pyramid13;
     220          43 :     ref_elem_file[PYRAMID14] = ElemDataStrings::one_pyramid14;
     221          43 :     ref_elem_file[PYRAMID18] = ElemDataStrings::one_pyramid18;
     222             : 
     223             :     // No entry here for C0POLYGON or C0POLYHEDRON - the reference
     224             :     // element there depends on the number of nodes and can't be
     225             :     // precomputed.
     226             :   }
     227             : 
     228             :   // Read'em
     229        1161 :   for (const auto & [elem_type, filename] : ref_elem_file)
     230             :     {
     231        2210 :       std::istringstream stream(filename);
     232        1118 :       read_ref_elem(elem_type, stream);
     233         390 :     }
     234             : }
     235             : 
     236             : 
     237             : // no reason to do this at startup -
     238             : // data structures will get initialized *if*
     239             : // ReferenceElem::get() is ever called.
     240             : // // Class to setup singleton data
     241             : // class ReferenceElemSetup : public Singleton::Setup
     242             : // {
     243             : //   void setup ()
     244             : //   {
     245             : //     init_ref_elem_table();
     246             : //   }
     247             : // } reference_elem_setup;
     248             : 
     249             : } // anonymous namespace
     250             : 
     251             : 
     252             : 
     253             : //----------------------------------------------------------------------------
     254             : // external API Implementation
     255             : namespace libMesh
     256             : {
     257             : namespace ReferenceElem
     258             : {
     259      137621 : const Elem & get (const ElemType type_in)
     260             : {
     261       34694 :   ElemType base_type = type_in;
     262             : 
     263             :   // For shell elements, use non shell type as the base type
     264      137621 :   if (type_in == TRISHELL3)
     265          88 :     base_type = TRI3;
     266             : 
     267      137445 :   if (type_in == QUADSHELL4)
     268        3428 :     base_type = QUAD4;
     269             : 
     270      130765 :   if (type_in == QUADSHELL8)
     271        6756 :     base_type = QUAD8;
     272             : 
     273      124109 :   if (type_in == QUADSHELL9)
     274         100 :     base_type = QUAD9;
     275             : 
     276      137621 :   init_ref_elem_table();
     277             : 
     278             :   // Throw an error if the user asked for an ElemType that we don't
     279             :   // have a reference element for.
     280      137621 :   libmesh_error_msg_if(ref_elem_map[base_type] == nullptr || type_in == INVALID_ELEM,
     281             :                        "No reference elem data available for ElemType " << type_in
     282             :                        << " = " << Utility::enum_to_string(type_in) << ".");
     283             : 
     284      137621 :   return *ref_elem_map[base_type];
     285             : }
     286             : 
     287             : std::pair<std::unique_ptr<Elem>, std::vector<std::unique_ptr<Node>>>
     288       72500 : ideal_target (const ElemType type)
     289             : {
     290             :   // Build target element
     291       90638 :   auto target_elem = Elem::build(type);
     292             : 
     293             :   // Volume of reference element
     294       72500 :   const auto ref_vol = target_elem->reference_elem()->volume();
     295             : 
     296             :   // Update the nodes of the target element, depending on type
     297       18138 :   const Real sqrt_2 = std::sqrt(Real(2));
     298       18138 :   const Real sqrt_3 = std::sqrt(Real(3));
     299       54414 :   std::vector<std::unique_ptr<Node>> owned_nodes;
     300             : 
     301       90638 :   const auto type_str = Utility::enum_to_string(type);
     302             : 
     303             :   // Elems deriving from Tri
     304       72500 :   if (type_str.compare(0, 3, "TRI") == 0)
     305             :     {
     306             : 
     307             :       // The target element will be an equilateral triangle with area equal to
     308             :       // the area of the reference element.
     309             : 
     310             :       // Equilateral triangle side length preserving area of the reference element
     311        1322 :       const auto side_length = std::sqrt(4. / sqrt_3 * ref_vol);
     312             : 
     313             :       // Define the nodal locations of the vertices
     314         332 :       const auto & s = side_length;
     315             :       //                                         x        y                  node_id
     316        1654 :       owned_nodes.emplace_back(Node::build(Point(0.,      0.),               0));
     317        1322 :       owned_nodes.emplace_back(Node::build(Point(s,       0.),               1));
     318        1654 :       owned_nodes.emplace_back(Node::build(Point(0.5 * s, 0.5 * sqrt_3 * s), 2));
     319             : 
     320        1322 :       switch (type)
     321             :         {
     322         170 :             case TRI3:
     323             :             case TRISHELL3: {
     324             :               // Nothing to do here, vertices already added above
     325         170 :               break;
     326             :             }
     327             : 
     328         647 :             case TRI6:
     329             :             case TRI7: {
     330             :               // Define the midpoint nodes of the equilateral triangle
     331             :               //                                         x         y                   node_id
     332         647 :               owned_nodes.emplace_back(Node::build(Point(0.50 * s, 0.00),              3));
     333         809 :               owned_nodes.emplace_back(Node::build(Point(0.75 * s, 0.25 * sqrt_3 * s), 4));
     334         809 :               owned_nodes.emplace_back(Node::build(Point(0.25 * s, 0.25 * sqrt_3 * s), 5));
     335             : 
     336         647 :               if (type == TRI7)
     337             :                 {
     338             :                   // Define the interior "bubble" node at the centroid
     339          80 :                   const auto & on = owned_nodes;
     340         480 :                   owned_nodes.emplace_back(Node::build(Point((*on[0] + *on[1] + *on[2]) / 3.), 6));
     341             :                 }
     342             : 
     343         162 :               break;
     344             :             }
     345             : 
     346           0 :           default:
     347           0 :             libmesh_error_msg("Unsupported triangular element: " << type_str);
     348             :             break;
     349             :         }
     350             :     } // if Tri
     351             : 
     352             :   // Elems deriving from Prism
     353       71178 :   else if (type_str.compare(0, 5, "PRISM") == 0)
     354             :     {
     355             : 
     356             :       // The target element will be a prism with an equilateral triangular
     357             :       // base with volume equal to the volume of the reference element.
     358             : 
     359             :       // For an equilateral triangular base with side length s, the
     360             :       // base area is s^2 * sqrt(3) / 4.
     361             :       // The prism height that will result in equal face areas is
     362             :       // s * sqrt(3) / 4. We choose s such that the target element has
     363             :       // the same volume as the reference element:
     364             :       // v = (s^2 * sqrt(3) / 4) * (s * sqrt(3) / 4) = 3 * s^3 / 4
     365             :       // --> s = (16 * v / 3)^(1/3)
     366             :       // I have no particular motivation for imposing equal face areas,
     367             :       // so this can be updated if a more `optimal` target prism is
     368             :       // identified.
     369             : 
     370             :       // Side length that preserves the volume of the reference element
     371          49 :       const auto side_length = std::cbrt(16. * ref_vol / 3.);
     372             :       // Prism height with the property that all faces have equal area
     373          49 :       const auto target_height = 0.25 * side_length * sqrt_3;
     374             : 
     375          14 :       const auto & s = side_length;
     376          14 :       const auto & h = target_height;
     377             :       //                                         x        y                 z    node_id
     378          63 :       owned_nodes.emplace_back(Node::build(Point(0.,      0.,               0.), 0));
     379          49 :       owned_nodes.emplace_back(Node::build(Point(s,       0.,               0.), 1));
     380          77 :       owned_nodes.emplace_back(Node::build(Point(0.5 * s, 0.5 * sqrt_3 * s, 0.), 2));
     381          63 :       owned_nodes.emplace_back(Node::build(Point(0.,      0.,               h),  3));
     382          63 :       owned_nodes.emplace_back(Node::build(Point(s,       0.,               h),  4));
     383          49 :       owned_nodes.emplace_back(Node::build(Point(0.5 * s, 0.5 * sqrt_3 * s, h),  5));
     384             : 
     385          49 :       if (type == PRISM15 || type == PRISM18 || type == PRISM20 || type == PRISM21)
     386             :         {
     387             :           // Define the edge midpoint nodes of the prism
     388          10 :           const auto & on = owned_nodes;
     389          45 :           owned_nodes.emplace_back(Node::build(Point((*on[0] + *on[1]) / 2.), 6));
     390          45 :           owned_nodes.emplace_back(Node::build(Point((*on[1] + *on[2]) / 2.), 7));
     391          45 :           owned_nodes.emplace_back(Node::build(Point((*on[2] + *on[0]) / 2.), 8));
     392          45 :           owned_nodes.emplace_back(Node::build(Point((*on[0] + *on[3]) / 2.), 9));
     393          45 :           owned_nodes.emplace_back(Node::build(Point((*on[1] + *on[4]) / 2.), 10));
     394          45 :           owned_nodes.emplace_back(Node::build(Point((*on[2] + *on[5]) / 2.), 11));
     395          45 :           owned_nodes.emplace_back(Node::build(Point((*on[3] + *on[4]) / 2.), 12));
     396          45 :           owned_nodes.emplace_back(Node::build(Point((*on[4] + *on[5]) / 2.), 13));
     397          45 :           owned_nodes.emplace_back(Node::build(Point((*on[5] + *on[3]) / 2.), 14));
     398             : 
     399          35 :           if (type == PRISM18 || type == PRISM20 || type == PRISM21)
     400             :             {
     401             :               // Define the rectangular face midpoint nodes of the prism
     402          36 :               owned_nodes.emplace_back(Node::build(Point((*on[0] + *on[1] + *on[3] + *on[4]) / 4.), 15));
     403          36 :               owned_nodes.emplace_back(Node::build(Point((*on[1] + *on[2] + *on[4] + *on[5]) / 4.), 16));
     404          36 :               owned_nodes.emplace_back(Node::build(Point((*on[0] + *on[2] + *on[3] + *on[5]) / 4.), 17));
     405             : 
     406          28 :               if (type == PRISM20 || type == PRISM21)
     407             :                 {
     408             :                   // Define the triangular face midpoint nodes of the prism
     409          27 :                   owned_nodes.emplace_back(Node::build(Point((*on[0] + *on[1] + *on[2]) / 3.), 18));
     410          27 :                   owned_nodes.emplace_back(Node::build(Point((*on[3] + *on[4] + *on[5]) / 3.), 19));
     411             : 
     412          21 :                   if (type == PRISM21)
     413             :                     // Define the interior point of the prism
     414          22 :                     owned_nodes.emplace_back(Node::build(Point((*on[9] + *on[10] + *on[11]) / 3.), 20));
     415             : 
     416             :                 }
     417          10 :             }
     418             :         }
     419             : 
     420          14 :       else if (type != PRISM6)
     421           0 :         libmesh_error_msg("Unsupported prism element: " << type_str);
     422             : 
     423             :     } // if Prism
     424             : 
     425             :   // Elems deriving from Pyramid
     426       71129 :   else if (type_str.compare(0, 7, "PYRAMID") == 0)
     427             :     {
     428             : 
     429             :       // The target element is a pyramid with an square base and
     430             :       // equilateral triangular sides with volume equal to the volume of the
     431             :       // reference element.
     432             : 
     433             :       // A pyramid with square base sidelength s and equilateral triangular
     434             :       // sides has height h = s / sqrt(2).
     435             :       // The volume is v = s^2 h / 3 = s^3 / ( 3 sqrt(2)).
     436             :       // Solving for s: s = (3 sqrt(2) v)^(1/3), where v is the volume of the
     437             :       // non-optimal reference element.
     438             : 
     439             :       // Side length that preserves the volume of the reference element
     440          35 :       const auto side_length = std::cbrt(3. * sqrt_2 * ref_vol);
     441             :       // Pyramid height with the property that all faces are equilateral triangles
     442          35 :       const auto target_height = side_length / sqrt_2;
     443             : 
     444          10 :       const auto & s = side_length;
     445          10 :       const auto & h = target_height;
     446             : 
     447             :       //                                         x        y        z    node_id
     448          45 :       owned_nodes.emplace_back(Node::build(Point(0.,      0.,      0.), 0));
     449          45 :       owned_nodes.emplace_back(Node::build(Point(s,       0.,      0.), 1));
     450          45 :       owned_nodes.emplace_back(Node::build(Point(s,       s,       0.), 2));
     451          35 :       owned_nodes.emplace_back(Node::build(Point(0.,      s,       0.), 3));
     452          45 :       owned_nodes.emplace_back(Node::build(Point(0.5 * s, 0.5 * s, h),  4));
     453             : 
     454          35 :       if (type == PYRAMID13 || type == PYRAMID14 || type == PYRAMID18)
     455             :         {
     456           8 :           const auto & on = owned_nodes;
     457             :           // Define the edge midpoint nodes of the pyramid
     458             : 
     459             :           // Base node to base node midpoint nodes
     460          36 :           owned_nodes.emplace_back(Node::build((*on[0] + *on[1]) / 2., 5));
     461          36 :           owned_nodes.emplace_back(Node::build((*on[1] + *on[2]) / 2., 6));
     462          36 :           owned_nodes.emplace_back(Node::build((*on[2] + *on[3]) / 2., 7));
     463          36 :           owned_nodes.emplace_back(Node::build((*on[3] + *on[0]) / 2., 8));
     464             : 
     465             :           // Base node to apex node midpoint nodes
     466          36 :           owned_nodes.emplace_back(Node::build(Point((*on[0] + *on[4]) / 2.), 9));
     467          36 :           owned_nodes.emplace_back(Node::build(Point((*on[1] + *on[4]) / 2.), 10));
     468          36 :           owned_nodes.emplace_back(Node::build(Point((*on[2] + *on[4]) / 2.), 11));
     469          36 :           owned_nodes.emplace_back(Node::build(Point((*on[3] + *on[4]) / 2.), 12));
     470             : 
     471          28 :           if (type == PYRAMID14 || type == PYRAMID18)
     472             :             {
     473             :               // Define the square face midpoint node of the pyramid
     474           9 :               owned_nodes.emplace_back(
     475          27 :                   Node::build(Point((*on[0] + *on[1] + *on[2] + *on[3]) / 4.), 13));
     476             : 
     477          21 :               if (type == PYRAMID18)
     478             :                 {
     479             :                   // Define the triangular face nodes
     480          18 :                   owned_nodes.emplace_back(Node::build(Point((*on[0] + *on[1] + *on[4]) / 3.), 14));
     481          18 :                   owned_nodes.emplace_back(Node::build(Point((*on[1] + *on[2] + *on[4]) / 3.), 15));
     482          18 :                   owned_nodes.emplace_back(Node::build(Point((*on[2] + *on[3] + *on[4]) / 3.), 16));
     483          22 :                   owned_nodes.emplace_back(Node::build(Point((*on[3] + *on[0] + *on[4]) / 3.), 17));
     484             :                 }
     485           8 :             }
     486             :         }
     487             : 
     488           7 :       else if (type != PYRAMID5)
     489           0 :         libmesh_error_msg("Unsupported pyramid element: " << type_str);
     490             : 
     491             :     } // if Pyramid
     492             : 
     493             :   // Elems deriving from Tet
     494       71094 :   else if (type_str.compare(0, 3, "TET") == 0)
     495             :     {
     496             : 
     497             :       // The ideal target element is a a regular tet with equilateral
     498             :       // triangles for all faces, with volume equal to the volume of the
     499             :       // reference element.
     500             : 
     501             :       // The volume of a tet is given by v = b * h / 3, where b is the area of
     502             :       // the base face and h is the height of the apex node. The area of an
     503             :       // equilateral triangle with side length s is b = sqrt(3) s^2 / 4.
     504             :       // For all faces to have side length s, the height of the apex node is
     505             :       // h = sqrt(2/3) * s. Then the volume is v = sqrt(2) * s^3 / 12.
     506             :       // Solving for s, the side length that will preserve the volume of the
     507             :       // reference element is s = (6 * sqrt(2) * v)^(1/3), where v is the volume
     508             :       // of the non-optimal reference element (i.e., a right tet).
     509             : 
     510             :       // Side length that preserves the volume of the reference element
     511       64540 :       const auto side_length = std::cbrt(6. * sqrt_2 * ref_vol);
     512             :       // tet height with the property that all faces are equilateral triangles
     513       64540 :       const auto target_height = sqrt_2 / sqrt_3 * side_length;
     514             : 
     515       16136 :       const auto & s = side_length;
     516       16136 :       const auto & h = target_height;
     517             : 
     518             :       // For regular tet
     519             :       //                                         x        y                z     node_id
     520       80676 :       owned_nodes.emplace_back(Node::build(Point(0.,      0.,               0.), 0));
     521       64540 :       owned_nodes.emplace_back(Node::build(Point(s,       0.,               0.), 1));
     522       80676 :       owned_nodes.emplace_back(Node::build(Point(0.5 * s, 0.5 * sqrt_3 * s, 0.), 2));
     523       80676 :       owned_nodes.emplace_back(Node::build(Point(0.5 * s, sqrt_3 / 6. * s,  h),  3));
     524             : 
     525       64540 :       if (type == TET10 || type == TET14)
     526             :         {
     527       10758 :           const auto & on = owned_nodes;
     528             :           // Define the edge midpoint nodes of the tet
     529             : 
     530             :           // Base node to base node midpoint nodes
     531       53787 :           owned_nodes.emplace_back(Node::build(Point((*on[0] + *on[1]) / 2.), 4));
     532       53787 :           owned_nodes.emplace_back(Node::build(Point((*on[1] + *on[2]) / 2.), 5));
     533       53787 :           owned_nodes.emplace_back(Node::build(Point((*on[2] + *on[0]) / 2.), 6));
     534             :           // Base node to apex node midpoint nodes
     535       53787 :           owned_nodes.emplace_back(Node::build(Point((*on[0] + *on[3]) / 2.), 7));
     536       53787 :           owned_nodes.emplace_back(Node::build(Point((*on[1] + *on[3]) / 2.), 8));
     537       53787 :           owned_nodes.emplace_back(Node::build(Point((*on[2] + *on[3]) / 2.), 9));
     538             : 
     539       43029 :           if (type == TET14)
     540             :             {
     541             :               // Define the face midpoint nodes of the tet
     542       26898 :               owned_nodes.emplace_back(Node::build(Point((*on[0] + *on[1] + *on[2]) / 3.), 10));
     543       26898 :               owned_nodes.emplace_back(Node::build(Point((*on[0] + *on[1] + *on[3]) / 3.), 11));
     544       26898 :               owned_nodes.emplace_back(Node::build(Point((*on[1] + *on[2] + *on[3]) / 3.), 12));
     545       32278 :               owned_nodes.emplace_back(Node::build(Point((*on[0] + *on[2] + *on[3]) / 3.), 13));
     546       10758 :             }
     547             :         }
     548             : 
     549       21511 :       else if (type != TET4)
     550           0 :         libmesh_error_msg("Unsupported tet element: " << type_str);
     551             : 
     552             :     } // if Tet
     553             : 
     554             :   // Set the target_elem equal to the reference elem
     555             :   else
     556      112577 :     for (const auto & node : target_elem->reference_elem()->node_ref_range())
     557      156685 :       owned_nodes.emplace_back(Node::build(node, node.id()));
     558             : 
     559             :   // Set nodes of target element
     560      786735 :   for (const auto & node_ptr : owned_nodes)
     561      892913 :     target_elem->set_node(node_ptr->id(), node_ptr.get());
     562             : 
     563       18138 :   libmesh_assert(relative_fuzzy_equals(target_elem->volume(), ref_vol, TOLERANCE));
     564             : 
     565       90638 :   return std::make_pair(std::move(target_elem), std::move(owned_nodes));
     566       36224 : }
     567             : } // namespace ReferenceElem
     568             : } // namespace libMesh

Generated by: LCOV version 1.14