LCOV - code coverage report
Current view: top level - src/geom - elem.C (source / functions) Hit Total Coverage
Test: libMesh/libmesh: #4620 (a489da) with base 3f0912 Lines: 1240 1592 77.9 %
Date: 2026-11-19 19:20:12 Functions: 81 111 73.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             : // Local includes
      20             : #include "libmesh/elem.h"
      21             : 
      22             : #include "libmesh/boundary_info.h"
      23             : #include "libmesh/fe_type.h"
      24             : #include "libmesh/fe_interface.h"
      25             : #include "libmesh/tensor_value.h"
      26             : #include "libmesh/node_elem.h"
      27             : #include "libmesh/edge_edge2.h"
      28             : #include "libmesh/edge_edge3.h"
      29             : #include "libmesh/edge_edge4.h"
      30             : #include "libmesh/edge_inf_edge2.h"
      31             : #include "libmesh/face_c0polygon.h"
      32             : #include "libmesh/face_tri3.h"
      33             : #include "libmesh/face_tri3_subdivision.h"
      34             : #include "libmesh/face_tri3_shell.h"
      35             : #include "libmesh/face_tri6.h"
      36             : #include "libmesh/face_tri7.h"
      37             : #include "libmesh/face_quad4.h"
      38             : #include "libmesh/face_quad4_shell.h"
      39             : #include "libmesh/face_quad8.h"
      40             : #include "libmesh/face_quad8_shell.h"
      41             : #include "libmesh/face_quad9.h"
      42             : #include "libmesh/face_quad9_shell.h"
      43             : #include "libmesh/face_quad16.h"
      44             : #include "libmesh/face_inf_quad4.h"
      45             : #include "libmesh/face_inf_quad6.h"
      46             : #include "libmesh/cell_tet4.h"
      47             : #include "libmesh/cell_tet10.h"
      48             : #include "libmesh/cell_tet14.h"
      49             : #include "libmesh/cell_hex8.h"
      50             : #include "libmesh/cell_hex20.h"
      51             : #include "libmesh/cell_hex27.h"
      52             : #include "libmesh/cell_inf_hex8.h"
      53             : #include "libmesh/cell_inf_hex16.h"
      54             : #include "libmesh/cell_inf_hex18.h"
      55             : #include "libmesh/cell_prism6.h"
      56             : #include "libmesh/cell_prism15.h"
      57             : #include "libmesh/cell_prism18.h"
      58             : #include "libmesh/cell_prism20.h"
      59             : #include "libmesh/cell_prism21.h"
      60             : #include "libmesh/cell_inf_prism6.h"
      61             : #include "libmesh/cell_inf_prism12.h"
      62             : #include "libmesh/cell_pyramid5.h"
      63             : #include "libmesh/cell_pyramid13.h"
      64             : #include "libmesh/cell_pyramid14.h"
      65             : #include "libmesh/cell_pyramid18.h"
      66             : #include "libmesh/fe_base.h"
      67             : #include "libmesh/mesh_base.h"
      68             : #include "libmesh/quadrature_nodal.h"
      69             : #include "libmesh/quadrature_gauss.h"
      70             : #include "libmesh/remote_elem.h"
      71             : #include "libmesh/reference_elem.h"
      72             : #include "libmesh/enum_to_string.h"
      73             : #include "libmesh/threads.h"
      74             : #include "libmesh/enum_elem_quality.h"
      75             : #include "libmesh/enum_io_package.h"
      76             : #include "libmesh/enum_order.h"
      77             : #include "libmesh/elem_internal.h"
      78             : 
      79             : #ifdef LIBMESH_ENABLE_PERIODIC
      80             : #include "libmesh/mesh.h"
      81             : #include "libmesh/periodic_boundaries.h"
      82             : #endif
      83             : 
      84             : 
      85             : // C++ includes
      86             : #include <algorithm> // for std::sort
      87             : #include <array>
      88             : #include <iterator>  // for std::ostream_iterator
      89             : #include <sstream>
      90             : #include <limits>    // for std::numeric_limits<>
      91             : #include <cmath>     // for std::sqrt()
      92             : #include <memory>
      93             : #include <regex>     // for exceptions in volume()
      94             : 
      95             : 
      96             : namespace libMesh
      97             : {
      98             : 
      99             : Threads::spin_mutex parent_indices_mutex;
     100             : Threads::spin_mutex parent_bracketing_nodes_mutex;
     101             : 
     102             : // Initialize static member variables
     103             : const unsigned int Elem::type_to_dim_map [] =
     104             :   {
     105             :     1,  // EDGE2
     106             :     1,  // EDGE3
     107             :     1,  // EDGE4
     108             : 
     109             :     2,  // TRI3
     110             :     2,  // TRI6
     111             : 
     112             :     2,  // QUAD4
     113             :     2,  // QUAD8
     114             :     2,  // QUAD9
     115             : 
     116             :     3,  // TET4
     117             :     3,  // TET10
     118             : 
     119             :     3,  // HEX8
     120             :     3,  // HEX20
     121             :     3,  // HEX27
     122             : 
     123             :     3,  // PRISM6
     124             :     3,  // PRISM15
     125             :     3,  // PRISM18
     126             : 
     127             :     3,  // PYRAMID5
     128             :     3,  // PYRAMID13
     129             :     3,  // PYRAMID14
     130             : 
     131             :     1,  // INFEDGE2
     132             : 
     133             :     2,  // INFQUAD4
     134             :     2,  // INFQUAD6
     135             : 
     136             :     3,  // INFHEX8
     137             :     3,  // INFHEX16
     138             :     3,  // INFHEX18
     139             : 
     140             :     3,  // INFPRISM6
     141             :     3,  // INFPRISM12
     142             : 
     143             :     0,  // NODEELEM
     144             : 
     145             :     0,  // REMOTEELEM
     146             : 
     147             :     2,  // TRI3SUBDIVISION
     148             :     2,  // TRISHELL3
     149             :     2,  // QUADSHELL4
     150             :     2,  // QUADSHELL8
     151             : 
     152             :     2,  // TRI7
     153             :     3,  // TET14
     154             :     3,  // PRISM20
     155             :     3,  // PRISM21
     156             :     3,  // PYRAMID18
     157             : 
     158             :     2,  // QUADSHELL9
     159             : 
     160             :     2,  // C0POLYGON
     161             :     3,  // C0POLYHEDRON
     162             : 
     163             :     2,  // QUAD16
     164             :   };
     165             : 
     166             : const unsigned int Elem::max_n_nodes;
     167             : 
     168             : const unsigned int Elem::type_to_n_sides_map [] =
     169             :   {
     170             :     2,  // EDGE2
     171             :     2,  // EDGE3
     172             :     2,  // EDGE4
     173             : 
     174             :     3,  // TRI3
     175             :     3,  // TRI6
     176             : 
     177             :     4,  // QUAD4
     178             :     4,  // QUAD8
     179             :     4,  // QUAD9
     180             : 
     181             :     4,  // TET4
     182             :     4,  // TET10
     183             : 
     184             :     6,  // HEX8
     185             :     6,  // HEX20
     186             :     6,  // HEX27
     187             : 
     188             :     5,  // PRISM6
     189             :     5,  // PRISM15
     190             :     5,  // PRISM18
     191             : 
     192             :     5,  // PYRAMID5
     193             :     5,  // PYRAMID13
     194             :     5,  // PYRAMID14
     195             : 
     196             :     2,  // INFEDGE2
     197             : 
     198             :     3,  // INFQUAD4
     199             :     3,  // INFQUAD6
     200             : 
     201             :     5,  // INFHEX8
     202             :     5,  // INFHEX16
     203             :     5,  // INFHEX18
     204             : 
     205             :     4,  // INFPRISM6
     206             :     4,  // INFPRISM12
     207             : 
     208             :     0,  // NODEELEM
     209             : 
     210             :     0,  // REMOTEELEM
     211             : 
     212             :     3,  // TRI3SUBDIVISION
     213             :     3,  // TRISHELL3
     214             :     4,  // QUADSHELL4
     215             :     4,  // QUADSHELL8
     216             : 
     217             :     3,  // TRI7
     218             :     4,  // TET14
     219             :     5,  // PRISM20
     220             :     5,  // PRISM21
     221             :     5,  // PYRAMID18
     222             : 
     223             :     4,  // QUADSHELL9
     224             : 
     225             :     invalid_uint,  // C0POLYGON
     226             :     invalid_uint,  // C0POLYHEDRON
     227             : 
     228             :     4,  // QUAD16
     229             :   };
     230             : 
     231             : const unsigned int Elem::type_to_n_edges_map [] =
     232             :   {
     233             :     0,  // EDGE2
     234             :     0,  // EDGE3
     235             :     0,  // EDGE4
     236             : 
     237             :     3,  // TRI3
     238             :     3,  // TRI6
     239             : 
     240             :     4,  // QUAD4
     241             :     4,  // QUAD8
     242             :     4,  // QUAD9
     243             : 
     244             :     6,  // TET4
     245             :     6,  // TET10
     246             : 
     247             :     12, // HEX8
     248             :     12, // HEX20
     249             :     12, // HEX27
     250             : 
     251             :     9,  // PRISM6
     252             :     9,  // PRISM15
     253             :     9,  // PRISM18
     254             : 
     255             :     8,  // PYRAMID5
     256             :     8,  // PYRAMID13
     257             :     8,  // PYRAMID14
     258             : 
     259             :     0,  // INFEDGE2
     260             : 
     261             :     3,  // INFQUAD4
     262             :     3,  // INFQUAD6
     263             : 
     264             :     8,  // INFHEX8
     265             :     8,  // INFHEX16
     266             :     8,  // INFHEX18
     267             : 
     268             :     6,  // INFPRISM6
     269             :     6,  // INFPRISM12
     270             : 
     271             :     0,  // NODEELEM
     272             : 
     273             :     0,  // REMOTEELEM
     274             : 
     275             :     3,  // TRI3SUBDIVISION
     276             :     3,  // TRISHELL3
     277             :     4,  // QUADSHELL4
     278             :     4,  // QUADSHELL8
     279             : 
     280             :     3,  // TRI7
     281             :     6,  // TET14
     282             :     9,  // PRISM20
     283             :     9,  // PRISM21
     284             :     8,  // PYRAMID18
     285             : 
     286             :     4,  // QUADSHELL9
     287             : 
     288             :     invalid_uint,  // C0POLYGON
     289             :     invalid_uint,  // C0POLYHEDRON
     290             : 
     291             :     4,   // QUAD16
     292             :   };
     293             : 
     294             : const Order Elem::type_to_default_order_map [] =
     295             :   {
     296             :     FIRST,    // EDGE2
     297             :     SECOND,   // EDGE3
     298             :     THIRD,    // EDGE4
     299             : 
     300             :     FIRST,    // TRI3
     301             :     SECOND,   // TRI6
     302             : 
     303             :     FIRST,    // QUAD4
     304             :     SECOND,   // QUAD8
     305             :     SECOND,   // QUAD9
     306             : 
     307             :     FIRST,    // TET4
     308             :     SECOND,   // TET10
     309             : 
     310             :     FIRST,    // HEX8
     311             :     SECOND,   // HEX20
     312             :     SECOND,   // HEX27
     313             : 
     314             :     FIRST,    // PRISM6
     315             :     SECOND,   // PRISM15
     316             :     SECOND,   // PRISM18
     317             : 
     318             :     FIRST,    // PYRAMID5
     319             :     SECOND,   // PYRAMID13
     320             :     SECOND,   // PYRAMID14
     321             : 
     322             :     FIRST,    // INFEDGE2
     323             : 
     324             :     FIRST,    // INFQUAD4
     325             :     SECOND,   // INFQUAD6
     326             : 
     327             :     FIRST,    // INFHEX8
     328             :     SECOND,   // INFHEX16
     329             :     SECOND,   // INFHEX18
     330             : 
     331             :     FIRST,    // INFPRISM6
     332             :     SECOND,   // INFPRISM12
     333             : 
     334             :     CONSTANT, // NODEELEM
     335             : 
     336             :     INVALID_ORDER, // REMOTEELEM
     337             : 
     338             :     FIRST,    // TRI3SUBDIVISION
     339             :     FIRST,    // TRISHELL3
     340             :     FIRST,    // QUADSHELL4
     341             :     SECOND,   // QUADSHELL8
     342             : 
     343             :     THIRD,    // TRI7
     344             :     THIRD,    // TET14
     345             :     THIRD,    // PRISM20
     346             :     THIRD,    // PRISM21
     347             :     THIRD,    // PYRAMID18
     348             : 
     349             :     SECOND,   // QUADSHELL9
     350             : 
     351             :     FIRST,    // C0POLYGON
     352             :     FIRST,    // C0POLYHEDRON
     353             : 
     354             :     THIRD,   // QUAD16
     355             :   };
     356             : 
     357             : // ------------------------------------------------------------
     358             : // Elem class member functions
     359     2314739 : std::unique_ptr<Elem> Elem::disconnected_clone() const
     360             : {
     361     2314739 :   std::unique_ptr<Elem> returnval;
     362             : 
     363     2314739 :   switch (this->type())
     364             :     {
     365        8395 :     case C0POLYGON:
     366        8395 :       returnval = std::make_unique<C0Polygon>(this->n_sides());
     367        8395 :       break;
     368             : 
     369     2306344 :     default:
     370     2912300 :       returnval = Elem::build(this->type());
     371             :     }
     372             : 
     373     2314739 :   returnval->set_id() = this->id();
     374             : #ifdef LIBMESH_ENABLE_UNIQUE_ID
     375     2314739 :   if (this->valid_unique_id())
     376     1702002 :     returnval->set_unique_id(this->unique_id());
     377             : #endif
     378             : 
     379     2314739 :   const auto n_elem_ints = this->n_extra_integers();
     380     2314739 :   returnval->add_extra_integers(n_elem_ints);
     381     2321688 :   for (unsigned int i = 0; i != n_elem_ints; ++i)
     382        6949 :     returnval->set_extra_integer(i, this->get_extra_integer(i));
     383             : 
     384     2017717 :   returnval->inherit_data_from(*this);
     385             : 
     386     2314739 :   return returnval;
     387           0 : }
     388             : 
     389             : 
     390             : 
     391     6037887 : std::unique_ptr<Elem> Elem::build(const ElemType type,
     392             :                                   Elem * p)
     393             : {
     394     6037887 :   switch (type)
     395             :     {
     396             :       // 0D elements
     397       29865 :     case NODEELEM:
     398       29865 :       return std::make_unique<NodeElem>(p);
     399             : 
     400             :       // 1D elements
     401       65035 :     case EDGE2:
     402       65035 :       return std::make_unique<Edge2>(p);
     403       43670 :     case EDGE3:
     404       43670 :       return std::make_unique<Edge3>(p);
     405        6206 :     case EDGE4:
     406        6206 :       return std::make_unique<Edge4>(p);
     407             : 
     408             :       // 2D elements
     409      600620 :     case TRI3:
     410      600620 :       return std::make_unique<Tri3>(p);
     411        9326 :     case TRISHELL3:
     412        9326 :       return std::make_unique<TriShell3>(p);
     413        8132 :     case TRI3SUBDIVISION:
     414        8132 :       return std::make_unique<Tri3Subdivision>(p);
     415      143161 :     case TRI6:
     416      143161 :       return std::make_unique<Tri6>(p);
     417       62563 :     case TRI7:
     418       62563 :       return std::make_unique<Tri7>(p);
     419      770838 :     case QUAD4:
     420      770838 :       return std::make_unique<Quad4>(p);
     421       22696 :     case QUADSHELL4:
     422       22696 :       return std::make_unique<QuadShell4>(p);
     423       27767 :     case QUAD8:
     424       27767 :       return std::make_unique<Quad8>(p);
     425       21854 :     case QUADSHELL8:
     426       21854 :       return std::make_unique<QuadShell8>(p);
     427      475023 :     case QUAD9:
     428      475023 :       return std::make_unique<Quad9>(p);
     429        8451 :     case QUADSHELL9:
     430        8451 :       return std::make_unique<QuadShell9>(p);
     431        1421 :     case QUAD16:
     432        1421 :       return std::make_unique<Quad16>(p);
     433             : 
     434             :     // Well, a hexagon is *a* polygon...
     435        4360 :     case C0POLYGON:
     436        4360 :       return std::make_unique<C0Polygon>(6, p);
     437             : 
     438             :     // Building a polyhedron can't currently be done without creating
     439             :     // its nodes first
     440           0 :     case C0POLYHEDRON:
     441           0 :       libmesh_not_implemented_msg
     442             :         ("Polyhedra cannot be built via Elem::build()");
     443             : 
     444             :       // 3D elements
     445     1714153 :     case TET4:
     446     1714153 :       return std::make_unique<Tet4>(p);
     447      344110 :     case TET10:
     448      344110 :       return std::make_unique<Tet10>(p);
     449      560765 :     case TET14:
     450      560765 :       return std::make_unique<Tet14>(p);
     451      448017 :     case HEX8:
     452      448017 :       return std::make_unique<Hex8>(p);
     453       19236 :     case HEX20:
     454       19236 :       return std::make_unique<Hex20>(p);
     455      225765 :     case HEX27:
     456      225765 :       return std::make_unique<Hex27>(p);
     457       98370 :     case PRISM6:
     458       98370 :       return std::make_unique<Prism6>(p);
     459       25733 :     case PRISM15:
     460       25733 :       return std::make_unique<Prism15>(p);
     461       84679 :     case PRISM18:
     462       84679 :       return std::make_unique<Prism18>(p);
     463       17162 :     case PRISM20:
     464       17162 :       return std::make_unique<Prism20>(p);
     465       35701 :     case PRISM21:
     466       35701 :       return std::make_unique<Prism21>(p);
     467       70107 :     case PYRAMID5:
     468       70107 :       return std::make_unique<Pyramid5>(p);
     469       27918 :     case PYRAMID13:
     470       27918 :       return std::make_unique<Pyramid13>(p);
     471       28222 :     case PYRAMID14:
     472       28222 :       return std::make_unique<Pyramid14>(p);
     473       25659 :     case PYRAMID18:
     474       25659 :       return std::make_unique<Pyramid18>(p);
     475             : 
     476             : #ifdef LIBMESH_ENABLE_INFINITE_ELEMENTS
     477             :       // 1D infinite elements
     478           0 :     case INFEDGE2:
     479           0 :       return std::make_unique<InfEdge2>(p);
     480             : 
     481             :       // 2D infinite elements
     482         243 :     case INFQUAD4:
     483         243 :       return std::make_unique<InfQuad4>(p);
     484         243 :     case INFQUAD6:
     485         243 :       return std::make_unique<InfQuad6>(p);
     486             : 
     487             :       // 3D infinite elements
     488        4417 :     case INFHEX8:
     489        4417 :       return std::make_unique<InfHex8>(p);
     490         355 :     case INFHEX16:
     491         355 :       return std::make_unique<InfHex16>(p);
     492        2386 :     case INFHEX18:
     493        2386 :       return std::make_unique<InfHex18>(p);
     494         355 :     case INFPRISM6:
     495         355 :       return std::make_unique<InfPrism6>(p);
     496        3303 :     case INFPRISM12:
     497        3303 :       return std::make_unique<InfPrism12>(p);
     498             : #endif
     499             : 
     500           0 :     default:
     501           0 :       libmesh_error_msg("ERROR: Undefined element type == " << Utility::enum_to_string(type));
     502             :     }
     503             : }
     504             : 
     505             : 
     506             : 
     507      675488 : std::unique_ptr<Elem> Elem::build_with_id (const ElemType type,
     508             :                                            dof_id_type id)
     509             : {
     510             :   // Call the other build() method with nullptr parent, then set the
     511             :   // required id.
     512      675488 :   auto temp = Elem::build(type, nullptr);
     513      191935 :   temp->set_id(id);
     514      675488 :   return temp;
     515             : }
     516             : 
     517             : 
     518             : 
     519      136879 : const Elem * Elem::reference_elem () const
     520             : {
     521      136879 :   return &(ReferenceElem::get(this->type()));
     522             : }
     523             : 
     524             : 
     525             : 
     526       27504 : Point Elem::true_centroid() const
     527             : {
     528             :   // The base class implementation builds a finite element of the correct
     529             :   // order and computes the centroid, c=(cx, cy, cz), where:
     530             :   //
     531             :   // [cx]            [\int x dV]
     532             :   // [cy] := (1/V) * [\int y dV]
     533             :   // [cz]            [\int z dV]
     534             :   //
     535             :   // using quadrature. Note that we can expand "x" in the FE space as:
     536             :   //
     537             :   // x = \sum_i x_i \phi_i
     538             :   //
     539             :   // where x_i are the nodal positions of the element and \phi_i are the
     540             :   // associated Lagrange shape functions. This allows us to write the
     541             :   // integrals above as e.g.:
     542             :   //
     543             :   // \int x dV = \sum_i x_i \int \phi_i dV
     544             :   //
     545             :   // Defining:
     546             :   //
     547             :   // V_i := \int \phi_i dV
     548             :   //
     549             :   // we then have:
     550             :   //
     551             :   // [cx]           [\sum_i x_i V_i]
     552             :   // [cy] = (1/V) * [\sum_i y_i V_i]
     553             :   // [cz]           [\sum_i z_i V_i]
     554             :   //
     555             :   // where:
     556             :   // V = \sum_i V_i
     557             :   //
     558             :   // Derived element types can overload this method to compute
     559             :   // the centroid more efficiently when possible.
     560             : 
     561             :   // If this Elem has an elevated p_level, then we need to generate a
     562             :   // barebones copy of it with zero p_level and call true_centroid()
     563             :   // on that instead.  This workaround allows us to avoid issues with
     564             :   // calling FE::reinit() with a default_order() FEType, and then
     565             :   // having that order incorrectly boosted by p_level.
     566       27504 :   if (this->p_level())
     567             :     {
     568           9 :       auto elem_copy = this->disconnected_clone();
     569             : #ifdef LIBMESH_ENABLE_AMR
     570           7 :       elem_copy->set_p_level(0);
     571             : #endif
     572             : 
     573             :       // Set node pointers
     574         147 :       for (auto n : this->node_index_range())
     575         140 :         elem_copy->set_node(n, _nodes[n]);
     576             : 
     577           7 :       return elem_copy->true_centroid();
     578           3 :     }
     579             : 
     580       27497 :   const FEFamily mapping_family = FEMap::map_fe_type(*this);
     581       27497 :   const FEType fe_type(this->default_order(), mapping_family);
     582             : 
     583             :   // Build FE and attach quadrature rule.  The default quadrature rule
     584             :   // integrates the mass matrix exactly, thus it is overkill to
     585             :   // integrate the basis functions, but this is convenient.
     586       34392 :   std::unique_ptr<FEBase> fe = FEBase::build(this->dim(), fe_type);
     587       34392 :   QGauss qrule (this->dim(), fe_type.default_quadrature_order());
     588       27497 :   fe->attach_quadrature_rule(&qrule);
     589             : 
     590             :   // Pre-request required data
     591       27497 :   const auto & JxW = fe->get_JxW();
     592        6895 :   const auto & phi = fe->get_phi();
     593             : 
     594             :   // Re-compute element-specific values
     595       27497 :   fe->reinit(this);
     596             : 
     597             :   // Number of basis functions
     598       13790 :   auto N = phi.size();
     599        6895 :   libmesh_assert_equal_to(N, this->n_nodes());
     600             : 
     601             :   // Compute V_i
     602       27497 :   std::vector<Real> V(N);
     603      846010 :   for (auto qp : index_range(JxW))
     604    12607873 :     for (auto i : make_range(N))
     605    20634305 :       V[i] += JxW[qp] * phi[i][qp];
     606             : 
     607             :   // Compute centroid
     608        6895 :   Point cp;
     609        6895 :   Real vol = 0.;
     610             : 
     611      390009 :   for (auto i : make_range(N))
     612             :     {
     613      362972 :       cp += this->point(i) * V[i];
     614      362512 :       vol += V[i];
     615             :     }
     616             : 
     617        6895 :   return cp / vol;
     618       13707 : }
     619             : 
     620   161036580 : Point Elem::vertex_average() const
     621             : {
     622    42235895 :   Point cp;
     623             : 
     624   161036580 :   const auto n_vertices = this->n_vertices();
     625             : 
     626   964695966 :   for (unsigned int n=0; n<n_vertices; n++)
     627   421072568 :     cp.add (this->point(n));
     628             : 
     629   161036580 :   return (cp /= static_cast<Real>(n_vertices));
     630             : }
     631             : 
     632             : 
     633             : 
     634      679376 : Real Elem::hmin() const
     635             : {
     636             :   // Avoid calling a virtual a lot of times
     637      679376 :   const auto n_vertices = this->n_vertices();
     638             : 
     639             :   // Special case for NodeElem
     640      679376 :   if (!n_vertices)
     641           0 :     return 0;
     642             : 
     643      679376 :   Real h_min=std::numeric_limits<Real>::max();
     644             : 
     645     3249114 :   for (unsigned int n_outer=0; n_outer<n_vertices; n_outer++)
     646     6370728 :     for (unsigned int n_inner=n_outer+1; n_inner<n_vertices; n_inner++)
     647             :       {
     648     2028904 :         const auto diff = (this->point(n_outer) - this->point(n_inner));
     649             : 
     650     4700080 :         h_min = std::min(h_min, diff.norm_sq());
     651             :       }
     652             : 
     653      679376 :   return std::sqrt(h_min);
     654             : }
     655             : 
     656             : 
     657             : 
     658    21745486 : Real Elem::hmax() const
     659             : {
     660    21745486 :   Real h_max=0;
     661             : 
     662             :   // Avoid calling a virtual a lot of times
     663    21745486 :   const auto n_vertices = this->n_vertices();
     664             : 
     665   119963315 :   for (unsigned int n_outer=0; n_outer<n_vertices; n_outer++)
     666   313872485 :     for (unsigned int n_inner=n_outer+1; n_inner<n_vertices; n_inner++)
     667             :       {
     668   121680302 :         const auto diff = (this->point(n_outer) - this->point(n_inner));
     669             : 
     670   247728944 :         h_max = std::max(h_max, diff.norm_sq());
     671             :       }
     672             : 
     673    28665999 :   return std::sqrt(h_max);
     674             : }
     675             : 
     676             : 
     677             : 
     678       88048 : Real Elem::length(const unsigned int n1,
     679             :                   const unsigned int n2) const
     680             : {
     681       22016 :   libmesh_assert_less ( n1, this->n_vertices() );
     682       22016 :   libmesh_assert_less ( n2, this->n_vertices() );
     683             : 
     684      110064 :   return (this->point(n1) - this->point(n2)).norm();
     685             : }
     686             : 
     687             : 
     688             : 
     689           0 : dof_id_type Elem::key () const
     690             : {
     691           0 :   const unsigned short n_n = this->n_nodes();
     692             : 
     693             :   std::array<dof_id_type, Elem::max_n_nodes> node_ids;
     694             : 
     695           0 :   for (unsigned short n=0; n != n_n; ++n)
     696           0 :     node_ids[n] = this->node_id(n);
     697             : 
     698             :   // Always sort, so that different local node numberings hash to the
     699             :   // same value.
     700           0 :   std::sort (node_ids.begin(), node_ids.begin()+n_n);
     701             : 
     702           0 :   return Utility::hashword(node_ids.data(), n_n);
     703             : }
     704             : 
     705             : 
     706             : 
     707    11414110 : bool Elem::operator == (const Elem & rhs) const
     708             : {
     709             :   // If the elements aren't the same type, they aren't equal
     710    11414110 :   if (this->type() != rhs.type())
     711           8 :     return false;
     712             : 
     713    11414092 :   const unsigned short n_n = this->n_nodes();
     714     5257472 :   libmesh_assert_equal_to(n_n, rhs.n_nodes());
     715             : 
     716             :   // Make two sorted arrays of global node ids and compare them for
     717             :   // equality.
     718             :   std::array<dof_id_type, Elem::max_n_nodes> this_ids, rhs_ids;
     719             : 
     720    41979132 :   for (unsigned short n = 0; n != n_n; n++)
     721             :     {
     722    30565040 :       this_ids[n] = this->node_id(n);
     723    37706366 :       rhs_ids[n] = rhs.node_id(n);
     724             :     }
     725             : 
     726             :   // Sort the vectors to rule out different local node numberings.
     727    11414092 :   std::sort(this_ids.begin(), this_ids.begin()+n_n);
     728    11414092 :   std::sort(rhs_ids.begin(), rhs_ids.begin()+n_n);
     729             : 
     730             :   // If the node ids match, the elements are equal!
     731    41979132 :   for (unsigned short n = 0; n != n_n; ++n)
     732    30565040 :     if (this_ids[n] != rhs_ids[n])
     733           0 :       return false;
     734     5257472 :   return true;
     735             : }
     736             : 
     737             : 
     738             : 
     739     3150014 : bool Elem::topologically_equal (const Elem & rhs) const
     740             : {
     741             :   // If the elements aren't the same type, they aren't equal
     742     3150014 :   if (this->type() != rhs.type())
     743           0 :     return false;
     744             : 
     745     3113258 :   libmesh_assert_equal_to(this->n_nodes(), rhs.n_nodes());
     746             : 
     747    25503386 :   for (auto n : make_range(this->n_nodes()))
     748    22620116 :     if (this->node_id(n) != rhs.node_id(n))
     749           0 :       return false;
     750             : 
     751    15832445 :   for (auto neigh : make_range(this->n_neighbors()))
     752             :     {
     753    12740018 :       if (!this->neighbor_ptr(neigh))
     754             :         {
     755      982573 :           if (rhs.neighbor_ptr(neigh))
     756           6 :             return false;
     757      962597 :           continue;
     758             :         }
     759    23350533 :       if (!rhs.neighbor_ptr(neigh) ||
     760    11640660 :           this->neighbor_ptr(neigh)->id() !=
     761    11640660 :           rhs.neighbor_ptr(neigh)->id())
     762           6 :         return false;
     763             :     }
     764             : 
     765     3164787 :   if (this->parent())
     766             :     {
     767     1654248 :       if (!rhs.parent())
     768           0 :         return false;
     769     1653948 :       if (this->parent()->id() != rhs.parent()->id())
     770           0 :         return false;
     771             :     }
     772     1510539 :   else if (rhs.parent())
     773           0 :     return false;
     774             : 
     775     3149993 :   if (this->interior_parent())
     776             :     {
     777        4640 :       if (!rhs.interior_parent())
     778           0 :         return false;
     779        4640 :       if (this->interior_parent()->id() !=
     780        4640 :           rhs.interior_parent()->id())
     781           0 :         return false;
     782             :     }
     783     3145353 :   else if (rhs.interior_parent())
     784           0 :     return false;
     785             : 
     786     3113252 :   return true;
     787             : }
     788             : 
     789             : 
     790             : 
     791           0 : bool Elem::is_semilocal(const processor_id_type my_pid) const
     792             : {
     793           0 :   std::set<const Elem *> point_neighbors;
     794             : 
     795           0 :   this->find_point_neighbors(point_neighbors);
     796             : 
     797           0 :   for (const auto & elem : point_neighbors)
     798           0 :     if (elem->processor_id() == my_pid)
     799           0 :       return true;
     800             : 
     801           0 :   return false;
     802             : }
     803             : 
     804             : 
     805             : 
     806        2499 : unsigned int Elem::which_side_am_i (const Elem * e) const
     807             : {
     808         708 :   libmesh_assert(e);
     809             : 
     810        2499 :   const unsigned int ns = this->n_sides();
     811        2499 :   const unsigned int nn = this->n_nodes();
     812             : 
     813        2499 :   const unsigned int en = e->n_nodes();
     814             : 
     815             :   // e might be on any side until proven otherwise
     816        3207 :   std::vector<bool> might_be_side(ns, true);
     817             : 
     818        9938 :   for (unsigned int i=0; i != en; ++i)
     819             :     {
     820        9547 :       Point side_point = e->point(i);
     821        2108 :       unsigned int local_node_id = libMesh::invalid_uint;
     822             : 
     823             :       // Look for a node of this that's contiguous with node i of
     824             :       // e. Note that the exact floating point comparison of Point
     825             :       // positions is intentional, see the class documentation for
     826             :       // this function.
     827       74216 :       for (unsigned int j=0; j != nn; ++j)
     828       37848 :         if (this->point(j) == side_point)
     829        2108 :           local_node_id = j;
     830             : 
     831             :       // If a node of e isn't contiguous with some node of this, then
     832             :       // e isn't a side of this.
     833        7439 :       if (local_node_id == libMesh::invalid_uint)
     834           0 :         return libMesh::invalid_uint;
     835             : 
     836             :       // If a node of e isn't contiguous with some node on side s of
     837             :       // this, then e isn't on side s.
     838       37137 :       for (unsigned int s=0; s != ns; ++s)
     839       29698 :         if (!this->is_node_on_side(local_node_id, s))
     840        9816 :           might_be_side[s] = false;
     841             :     }
     842             : 
     843        6810 :   for (unsigned int s=0; s != ns; ++s)
     844        8741 :     if (might_be_side[s])
     845             :       {
     846             : #ifdef DEBUG
     847        1593 :         for (unsigned int s2=s+1; s2 < ns; ++s2)
     848         885 :           libmesh_assert (!might_be_side[s2]);
     849             : #endif
     850        2499 :         return s;
     851             :       }
     852             : 
     853             :   // Didn't find any matching side
     854           0 :   return libMesh::invalid_uint;
     855             : }
     856             : 
     857             : 
     858             : 
     859     7907621 : bool Elem::contains_vertex_of(const Elem * e, bool mesh_connection) const
     860             : {
     861             :   // Our vertices are the first numbered nodes
     862     7907621 :   const unsigned int nv = e->n_vertices();
     863     7907621 :   const unsigned int my_nv = this->n_vertices();
     864             : 
     865             :   // Check for vertex-to-vertex containment first; contains_point() is
     866             :   // expensive
     867    35281957 :   for (auto n : make_range(nv))
     868             :     {
     869    17165593 :       const Node * vertex = e->node_ptr(n);
     870   227364736 :       for (auto my_n : make_range(my_nv))
     871   199990400 :         if (&this->node_ref(my_n) == vertex)
     872     1313581 :           return true;
     873             :     }
     874             : 
     875             :   // If e is in our mesh, then we might be done testing
     876     3034028 :   if (mesh_connection)
     877             :     {
     878     3034028 :       const unsigned int l = this->level();
     879     3034028 :       const unsigned int el = e->level();
     880             : 
     881     3034028 :       if (l >= el)
     882      806278 :         return false;
     883             : 
     884             :       // We could also return false for l==el-1 iff we knew we had no
     885             :       // triangular faces, but we don't have an API to check that.
     886             :     }
     887             : 
     888             :   // Our vertices are the first numbered nodes
     889       11844 :   for (auto n : make_range(nv))
     890       12412 :     if (this->contains_point(e->point(n)))
     891           0 :       return true;
     892        1632 :   return false;
     893             : }
     894             : 
     895             : 
     896             : 
     897           0 : bool Elem::contains_edge_of(const Elem * e) const
     898             : {
     899           0 :   unsigned int num_contained_edges = 0;
     900             : 
     901             :   // Our vertices are the first numbered nodes
     902           0 :   for (auto n : make_range(e->n_vertices()))
     903             :     {
     904           0 :       if (this->contains_point(e->point(n)))
     905             :         {
     906           0 :           num_contained_edges++;
     907           0 :           if (num_contained_edges>=2)
     908             :             {
     909           0 :               return true;
     910             :             }
     911             :         }
     912             :     }
     913           0 :   return false;
     914             : }
     915             : 
     916             : 
     917             : 
     918      198194 : void Elem::find_point_neighbors(const Point & p,
     919             :                                 std::set<const Elem *> & neighbor_set) const
     920             : {
     921       51820 :   libmesh_assert(this->contains_point(p));
     922       51820 :   libmesh_assert(this->active());
     923             : 
     924       51820 :   neighbor_set.clear();
     925      198194 :   neighbor_set.insert(this);
     926             : 
     927      103640 :   std::set<const Elem *> untested_set, next_untested_set;
     928      198194 :   untested_set.insert(this);
     929             : 
     930             : #ifdef LIBMESH_ENABLE_AMR
     931      103640 :   std::vector<const Elem *> active_neighbor_children;
     932             : #endif // #ifdef LIBMESH_ENABLE_AMR
     933             : 
     934      593163 :   while (!untested_set.empty())
     935             :     {
     936             :       // Loop over all the elements in the patch that haven't already
     937             :       // been tested
     938      836686 :       for (const auto & elem : untested_set)
     939     2408114 :           for (auto current_neighbor : elem->neighbor_ptr_range())
     940             :             {
     941     2281442 :               if (current_neighbor &&
     942     1725006 :                   current_neighbor != remote_elem)    // we have a real neighbor on this side
     943             :                 {
     944      429806 :                   if (current_neighbor->active())                // ... if it is active
     945             :                     {
     946      425024 :                       auto it = neighbor_set.lower_bound(current_neighbor);
     947     2983818 :                       if ((it == neighbor_set.end() || *it != current_neighbor) &&
     948     1324111 :                           current_neighbor->contains_point(p))   // ... don't have and touches p
     949             :                         {
     950             :                           // Add it and test it
     951      179205 :                           next_untested_set.insert(current_neighbor);
     952      241532 :                           neighbor_set.emplace_hint(it, current_neighbor);
     953             :                         }
     954             :                     }
     955             : #ifdef LIBMESH_ENABLE_AMR
     956             :                   else                                 // ... the neighbor is *not* active,
     957             :                     {                                  // ... so add *all* neighboring
     958             :                                                        // active children that touch p
     959        4782 :                       active_neighbor_children.clear();
     960             :                       current_neighbor->active_family_tree_by_neighbor
     961       15059 :                         (active_neighbor_children, elem);
     962             : 
     963       45491 :                       for (const auto & current_child : active_neighbor_children)
     964             :                         {
     965        9564 :                           auto it = neighbor_set.lower_bound(current_child);
     966       59351 :                           if ((it == neighbor_set.end() || *it != current_child) &&
     967       28919 :                               current_child->contains_point(p))
     968             :                             {
     969             :                               // Add it and test it
     970        1377 :                               next_untested_set.insert(current_child);
     971        1991 :                               neighbor_set.emplace_hint(it, current_child);
     972             :                             }
     973             :                         }
     974             :                     }
     975             : #endif // #ifdef LIBMESH_ENABLE_AMR
     976             :                 }
     977             :             }
     978      102942 :       untested_set.swap(next_untested_set);
     979      102942 :       next_untested_set.clear();
     980             :     }
     981      198194 : }
     982             : 
     983             : 
     984             : 
     985       57474 : void Elem::find_point_neighbors(std::set<const Elem *> & neighbor_set) const
     986             : {
     987       57474 :   this->find_point_neighbors(neighbor_set, this);
     988       57474 : }
     989             : 
     990             : 
     991             : 
     992       57474 : void Elem::find_point_neighbors(std::set<const Elem *> & neighbor_set,
     993             :                                 const Elem * start_elem) const
     994             : {
     995       57474 :   ElemInternal::find_point_neighbors(this, neighbor_set, start_elem);
     996       57474 : }
     997             : 
     998             : 
     999             : 
    1000           0 : void Elem::find_point_neighbors(std::set<Elem *> & neighbor_set,
    1001             :                                 Elem * start_elem)
    1002             : {
    1003           0 :   ElemInternal::find_point_neighbors(this, neighbor_set, start_elem);
    1004           0 : }
    1005             : 
    1006             : 
    1007             : 
    1008        1120 : void Elem::find_edge_neighbors(const Point & p1,
    1009             :                                const Point & p2,
    1010             :                                std::set<const Elem *> & neighbor_set) const
    1011             : {
    1012             :   // Simple but perhaps suboptimal code: find elements containing the
    1013             :   // first point, then winnow this set down by removing elements which
    1014             :   // don't also contain the second point
    1015             : 
    1016         280 :   libmesh_assert(this->contains_point(p2));
    1017        1120 :   this->find_point_neighbors(p1, neighbor_set);
    1018             : 
    1019         280 :   std::set<const Elem *>::iterator        it = neighbor_set.begin();
    1020         280 :   const std::set<const Elem *>::iterator end = neighbor_set.end();
    1021             : 
    1022        3292 :   while (it != end)
    1023             :     {
    1024             :       // As of C++11, set::erase returns an iterator to the element
    1025             :       // following the erased element, or end.
    1026        2172 :       if (!(*it)->contains_point(p2))
    1027         789 :         it = neighbor_set.erase(it);
    1028             :       else
    1029         280 :         ++it;
    1030             :     }
    1031        1120 : }
    1032             : 
    1033             : 
    1034             : 
    1035           0 : void Elem::find_edge_neighbors(std::set<const Elem *> & neighbor_set) const
    1036             : {
    1037           0 :   neighbor_set.clear();
    1038           0 :   neighbor_set.insert(this);
    1039             : 
    1040           0 :   std::set<const Elem *> untested_set, next_untested_set;
    1041           0 :   untested_set.insert(this);
    1042             : 
    1043           0 :   while (!untested_set.empty())
    1044             :     {
    1045             :       // Loop over all the elements in the patch that haven't already
    1046             :       // been tested
    1047           0 :       for (const auto & elem : untested_set)
    1048             :         {
    1049           0 :           for (auto current_neighbor : elem->neighbor_ptr_range())
    1050             :             {
    1051           0 :               if (current_neighbor &&
    1052           0 :                   current_neighbor != remote_elem)    // we have a real neighbor on this side
    1053             :                 {
    1054           0 :                   if (current_neighbor->active())                // ... if it is active
    1055             :                     {
    1056           0 :                       if (this->contains_edge_of(current_neighbor) // ... and touches us
    1057           0 :                           || current_neighbor->contains_edge_of(this))
    1058             :                         {
    1059             :                           // Make sure we'll test it
    1060           0 :                           if (!neighbor_set.count(current_neighbor))
    1061           0 :                             next_untested_set.insert (current_neighbor);
    1062             : 
    1063             :                           // And add it
    1064           0 :                           neighbor_set.insert (current_neighbor);
    1065             :                         }
    1066             :                     }
    1067             : #ifdef LIBMESH_ENABLE_AMR
    1068             :                   else                                 // ... the neighbor is *not* active,
    1069             :                     {                                  // ... so add *all* neighboring
    1070             :                                                        // active children
    1071           0 :                       std::vector<const Elem *> active_neighbor_children;
    1072             : 
    1073             :                       current_neighbor->active_family_tree_by_neighbor
    1074           0 :                         (active_neighbor_children, elem);
    1075             : 
    1076           0 :                       for (const auto & current_child : active_neighbor_children)
    1077           0 :                         if (this->contains_edge_of(current_child) || current_child->contains_edge_of(this))
    1078             :                           {
    1079             :                             // Make sure we'll test it
    1080           0 :                             if (!neighbor_set.count(current_child))
    1081           0 :                               next_untested_set.insert (current_child);
    1082             : 
    1083           0 :                             neighbor_set.insert (current_child);
    1084             :                           }
    1085             :                     }
    1086             : #endif // #ifdef LIBMESH_ENABLE_AMR
    1087             :                 }
    1088             :             }
    1089             :         }
    1090           0 :       untested_set.swap(next_untested_set);
    1091           0 :       next_untested_set.clear();
    1092             :     }
    1093           0 : }
    1094             : 
    1095             : 
    1096             : 
    1097        1782 : void Elem::find_interior_neighbors(std::set<const Elem *> & neighbor_set) const
    1098             : {
    1099        1782 :   ElemInternal::find_interior_neighbors(this, neighbor_set);
    1100        1782 : }
    1101             : 
    1102             : 
    1103             : 
    1104        2216 : void Elem::find_interior_neighbors(std::set<Elem *> & neighbor_set)
    1105             : {
    1106        2216 :   ElemInternal::find_interior_neighbors(this, neighbor_set);
    1107        2216 : }
    1108             : 
    1109             : 
    1110             : 
    1111    16047022 : const Elem * Elem::interior_parent () const
    1112             : {
    1113             :   // interior parents make no sense for full-dimensional elements.
    1114    16047022 :   if (this->dim() >= LIBMESH_DIM)
    1115     3303973 :       return nullptr;
    1116             : 
    1117             :   // they USED TO BE only good for level-0 elements, but we now
    1118             :   // support keeping interior_parent() valid on refined boundary
    1119             :   // elements.
    1120             :   // if (this->level() != 0)
    1121             :   // return this->parent()->interior_parent();
    1122             : 
    1123             :   // We store the interior_parent pointer after both the parent
    1124             :   // neighbor and neighbor pointers
    1125    12130668 :   Elem * interior_p = _elemlinks[1+this->n_sides()];
    1126             : 
    1127             :   // If we have an interior_parent, we USED TO assume it was a
    1128             :   // one-higher-dimensional interior element, but we now allow e.g.
    1129             :   // edge elements to have a 3D interior_parent with no
    1130             :   // intermediate 2D element.
    1131             :   // libmesh_assert (!interior_p ||
    1132             :   //                interior_p->dim() == (this->dim()+1));
    1133     5567017 :   libmesh_assert (!interior_p ||
    1134             :                   (interior_p == remote_elem) ||
    1135             :                   (interior_p->dim() > this->dim()));
    1136             : 
    1137             :   // If an element in a multi-dimensional mesh has an interior_parent
    1138             :   // link, it should be at our level or coarser, just like a neighbor
    1139             :   // link.  Our collect_families() code relies on this, but it might
    1140             :   // be tempting for users to manually assign something that breaks
    1141             :   // it.
    1142             :   //
    1143             :   // However, we *also* create temporary side elements, and we don't
    1144             :   // bother with creating ancestors for those, so they can be at level
    1145             :   // 0 even when they're sides of non-level-0 elements.
    1146     5567017 :   libmesh_assert (!interior_p ||
    1147             :                   (interior_p->level() <= this->level()) ||
    1148             :                   (this->level() == 0 &&
    1149             :                    this->id() == DofObject::invalid_id));
    1150             : 
    1151    12130668 :   return interior_p;
    1152             : }
    1153             : 
    1154             : 
    1155             : 
    1156    12468487 : Elem * Elem::interior_parent ()
    1157             : {
    1158             :   // See the const version for comments
    1159    12468487 :   if (this->dim() >= LIBMESH_DIM)
    1160     1136824 :       return nullptr;
    1161             : 
    1162    10181011 :   Elem * interior_p = _elemlinks[1+this->n_sides()];
    1163             : 
    1164     5363835 :   libmesh_assert (!interior_p ||
    1165             :                   (interior_p == remote_elem) ||
    1166             :                   (interior_p->dim() > this->dim()));
    1167             : 
    1168    10181011 :   return interior_p;
    1169             : }
    1170             : 
    1171             : 
    1172             : 
    1173  1528553273 : void Elem::set_interior_parent (Elem * p)
    1174             : {
    1175             :   // interior parents make no sense for full-dimensional elements.
    1176   474764709 :   libmesh_assert (!p ||
    1177             :                   this->dim() < LIBMESH_DIM);
    1178             : 
    1179             :   // If we have an interior_parent, we USED TO assume it was a
    1180             :   // one-higher-dimensional interior element, but we now allow e.g.
    1181             :   // edge elements to have a 3D interior_parent with no
    1182             :   // intermediate 2D element.
    1183             :   // libmesh_assert (!p ||
    1184             :   //                 p->dim() == (this->dim()+1));
    1185   474764709 :   libmesh_assert (!p ||
    1186             :                   (p == remote_elem) ||
    1187             :                   (p->dim() > this->dim()));
    1188             : 
    1189  1528553273 :   _elemlinks[1+this->n_sides()] = p;
    1190  1528553273 : }
    1191             : 
    1192             : 
    1193             : 
    1194             : #ifdef LIBMESH_ENABLE_PERIODIC
    1195             : 
    1196      786435 : Elem * Elem::topological_neighbor (const unsigned int i,
    1197             :                                    MeshBase & mesh,
    1198             :                                    const PointLocatorBase & point_locator,
    1199             :                                    const PeriodicBoundaries * pb)
    1200             : {
    1201      488472 :   libmesh_assert_less (i, this->n_neighbors());
    1202             : 
    1203      687080 :   Elem * neighbor_i = this->neighbor_ptr(i);
    1204      786435 :   if (neighbor_i != nullptr)
    1205      476930 :     return neighbor_i;
    1206             : 
    1207       22252 :   if (pb)
    1208             :     {
    1209             :       // Since the neighbor is nullptr it must be on a boundary. We need
    1210             :       // see if this is a periodic boundary in which case it will have a
    1211             :       // topological neighbor
    1212       11542 :       std::vector<boundary_id_type> bc_ids;
    1213       22252 :       mesh.get_boundary_info().boundary_ids(this, cast_int<unsigned short>(i), bc_ids);
    1214       22252 :       for (const auto & id : bc_ids)
    1215       22252 :         if (pb->boundary(id))
    1216             :           {
    1217             :             // Since the point locator inside of periodic boundaries
    1218             :             // returns a const pointer we will retrieve the proper
    1219             :             // pointer directly from the mesh object.
    1220       22252 :             const Elem * const cn = pb->neighbor(id, point_locator, this, i);
    1221       11542 :             neighbor_i = const_cast<Elem *>(cn);
    1222             : 
    1223             :             // Since coarse elements do not have more refined
    1224             :             // neighbors we need to make sure that we don't return one
    1225             :             // of these types of neighbors.
    1226       22252 :             if (neighbor_i)
    1227       24573 :               while (level() < neighbor_i->level())
    1228        2026 :                 neighbor_i = neighbor_i->parent();
    1229       11542 :             return neighbor_i;
    1230             :           }
    1231             :     }
    1232             : 
    1233           0 :   return nullptr;
    1234             : }
    1235             : 
    1236             : 
    1237             : 
    1238       34414 : const Elem * Elem::topological_neighbor (const unsigned int i,
    1239             :                                          const MeshBase & mesh,
    1240             :                                          const PointLocatorBase & point_locator,
    1241             :                                          const PeriodicBoundaries * pb) const
    1242             : {
    1243       20210 :   libmesh_assert_less (i, this->n_neighbors());
    1244             : 
    1245       31594 :   const Elem * neighbor_i = this->neighbor_ptr(i);
    1246       34414 :   if (neighbor_i != nullptr)
    1247       11131 :     return neighbor_i;
    1248             : 
    1249       18161 :   if (pb)
    1250             :     {
    1251             :       // Since the neighbor is nullptr it must be on a boundary. We need
    1252             :       // see if this is a periodic boundary in which case it will have a
    1253             :       // topological neighbor
    1254        9079 :       std::vector<boundary_id_type> bc_ids;
    1255       18161 :       mesh.get_boundary_info().boundary_ids(this, cast_int<unsigned short>(i), bc_ids);
    1256       19019 :       for (const auto & id : bc_ids)
    1257       16545 :         if (pb->boundary(id))
    1258             :           {
    1259       15687 :             neighbor_i = pb->neighbor(id, point_locator, this, i);
    1260             : 
    1261             :             // Since coarse elements do not have more refined
    1262             :             // neighbors we need to make sure that we don't return one
    1263             :             // of these types of neighbors.
    1264       15687 :             if (neighbor_i)
    1265       17670 :               while (level() < neighbor_i->level())
    1266        1832 :                 neighbor_i = neighbor_i->parent();
    1267        8221 :             return neighbor_i;
    1268             :           }
    1269             :     }
    1270             : 
    1271         858 :   return nullptr;
    1272             : }
    1273             : 
    1274             : 
    1275       20064 : bool Elem::has_topological_neighbor (const Elem * elem,
    1276             :                                      const MeshBase & mesh,
    1277             :                                      const PointLocatorBase & point_locator,
    1278             :                                      const PeriodicBoundaries * pb) const
    1279             : {
    1280             :   // First see if this is a normal "interior" neighbor
    1281       20064 :   if (has_neighbor(elem))
    1282           0 :     return true;
    1283             : 
    1284       20114 :   for (auto n : this->side_index_range())
    1285       20114 :     if (this->topological_neighbor(n, mesh, point_locator, pb))
    1286       14000 :       return true;
    1287             : 
    1288           0 :   return false;
    1289             : }
    1290             : 
    1291             : 
    1292             : #endif
    1293             : 
    1294             : #ifndef NDEBUG
    1295             : 
    1296           0 : void Elem::libmesh_assert_valid_node_pointers() const
    1297             : {
    1298           0 :   libmesh_assert(this->valid_id());
    1299           0 :   for (auto n : this->node_index_range())
    1300             :     {
    1301           0 :       libmesh_assert(this->node_ptr(n));
    1302           0 :       libmesh_assert(this->node_ptr(n)->valid_id());
    1303             :     }
    1304           0 : }
    1305             : 
    1306             : 
    1307             : 
    1308     4283670 : void Elem::libmesh_assert_valid_neighbors() const
    1309             : {
    1310    21258426 :   for (auto n : this->side_index_range())
    1311             :     {
    1312    16974756 :       const Elem * neigh = this->neighbor_ptr(n);
    1313             : 
    1314             :       // Any element might have a remote neighbor; checking
    1315             :       // to make sure that's not inaccurate is tough.
    1316    16974756 :       if (neigh == remote_elem)
    1317       13947 :         continue;
    1318             : 
    1319    16960809 :       if (neigh)
    1320             :         {
    1321             :           // Only subactive elements have subactive neighbors
    1322    15877538 :           libmesh_assert (this->subactive() || !neigh->subactive());
    1323             : 
    1324    15877538 :           const Elem * elem = this;
    1325             : 
    1326             :           // If we're subactive but our neighbor isn't, its
    1327             :           // return neighbor link will be to our first active
    1328             :           // ancestor OR to our inactive ancestor of the same
    1329             :           // level as neigh,
    1330    15877538 :           if (this->subactive() && !neigh->subactive())
    1331             :             {
    1332       90744 :               for (elem = this; !elem->active();
    1333       45372 :                    elem = elem->parent())
    1334       45372 :                 libmesh_assert(elem);
    1335             :             }
    1336             :           else
    1337             :             {
    1338    15832166 :               unsigned int rev = neigh->which_neighbor_am_i(elem);
    1339    15832166 :               libmesh_assert_less (rev, neigh->n_neighbors());
    1340             : 
    1341    15832166 :               if (this->subactive() && !neigh->subactive())
    1342             :                 {
    1343           0 :                   while (neigh->neighbor_ptr(rev) != elem)
    1344             :                     {
    1345           0 :                       libmesh_assert(elem->parent());
    1346           0 :                       elem = elem->parent();
    1347             :                     }
    1348             :                 }
    1349             :               else
    1350             :                 {
    1351    15832166 :                   const Elem * nn = neigh->neighbor_ptr(rev);
    1352    15832166 :                   libmesh_assert(nn);
    1353             : 
    1354    16260550 :                   for (; elem != nn; elem = elem->parent())
    1355      428384 :                     libmesh_assert(elem);
    1356             :                 }
    1357             :             }
    1358             :         }
    1359             :       // If we don't have a neighbor and we're not subactive, our
    1360             :       // ancestors shouldn't have any neighbors in this same
    1361             :       // direction.
    1362     1083271 :       else if (!this->subactive())
    1363             :         {
    1364     1066883 :           const Elem * my_parent = this->parent();
    1365     1451177 :           if (my_parent &&
    1366             :               // A parent with a different dimension isn't really one of
    1367             :               // our ancestors, it means we're on a boundary mesh and this
    1368             :               // is an interior mesh element for which we're on a side.
    1369             :               // Nothing to test for in that case.
    1370      384294 :               (my_parent->dim() == this->dim()))
    1371      384294 :             libmesh_assert (!my_parent->neighbor_ptr(n));
    1372             :         }
    1373             :     }
    1374     4283670 : }
    1375             : 
    1376             : #endif // !NDEBUG
    1377             : 
    1378             : 
    1379             : 
    1380      116411 : void Elem::make_links_to_me_local(unsigned int n, unsigned int nn)
    1381             : {
    1382       77630 :   Elem * neigh = this->neighbor_ptr(n);
    1383             : 
    1384             :   // Don't bother calling this function unless it's necessary
    1385       38815 :   libmesh_assert(neigh);
    1386       38815 :   libmesh_assert(!neigh->is_remote());
    1387             : 
    1388      116411 :   const unsigned int this_level = this->level();
    1389      116411 :   const unsigned int neigh_level = neigh->level();
    1390             : 
    1391             :   // We never have neighbors more refined than us
    1392       38815 :   libmesh_assert_less_equal (neigh_level, this_level);
    1393             : 
    1394             :   // We never have subactive neighbors of non subactive elements
    1395       38815 :   libmesh_assert(!neigh->subactive() || this->subactive());
    1396             : 
    1397             :   // If we have a neighbor less refined than us then it must not
    1398             :   // have any more refined descendants we could have pointed to
    1399             :   // instead.
    1400       38815 :   libmesh_assert((neigh_level == this_level) ||
    1401             :                  (neigh->active() && !this->subactive()) ||
    1402             :                  (!neigh->has_children() && this->subactive()));
    1403             : 
    1404             :   // What side of neigh are we on?  nn.
    1405             :   //
    1406             :   // We can't use the usual Elem method because we're in the middle of
    1407             :   // restoring topology.  We can't compare side_ptr nodes because
    1408             :   // users want to abuse neighbor_ptr to point to
    1409             :   // not-technically-neighbors across mesh slits.  We can't compare
    1410             :   // node locations because users want to move those
    1411             :   // not-technically-neighbors until they're
    1412             :   // not-even-geometrically-neighbors.
    1413             : 
    1414             :   // Find any elements that might need to point to elem
    1415       77630 :   std::vector<Elem *> neigh_family;
    1416             : #ifdef LIBMESH_ENABLE_AMR
    1417      116411 :   neigh->total_family_tree_by_side(neigh_family, nn);
    1418             : #else
    1419             :   neigh_family.push_back(neigh);
    1420             : #endif
    1421             : 
    1422             :   // Pull objects out of the loop to reduce heap operations
    1423      116411 :   std::unique_ptr<Elem> my_side, neigh_side;
    1424             : 
    1425             :   // And point them to elem
    1426      269962 :   for (auto & neigh_family_member : neigh_family)
    1427             :     {
    1428             :       // Only subactive elements point to other subactive elements
    1429      153551 :       const bool member_subactive = neigh_family_member->subactive();
    1430      153551 :       if (this->subactive() && !member_subactive)
    1431       12380 :         continue;
    1432             : 
    1433             :       // neigh (and possibly some of its family) might be at a lower
    1434             :       // level than us, with a neighbor at that lower level.  We
    1435             :       // would exit early if neigh was at a lower level, except we do
    1436             :       // want to handle neighbor links from subactive descendents.
    1437      153551 :       const unsigned int member_level = neigh_family_member->level();
    1438      153551 :       if (member_level < this_level)
    1439           0 :         continue;
    1440             : 
    1441             :       // Ideally, the neighbor link ought to either be correct
    1442             :       // already or ought to be to remote_elem.
    1443             :       //
    1444             :       // However, if we're redistributing a newly created elem,
    1445             :       // after an AMR step but before find_neighbors has fixed up
    1446             :       // neighbor links, we might have an out of date neighbor
    1447             :       // link to elem's parent instead.
    1448             : #ifdef LIBMESH_ENABLE_AMR
    1449       51195 :       libmesh_assert((neigh_family_member->neighbor_ptr(nn) &&
    1450             :                       (neigh_family_member->neighbor_ptr(nn)->active() ||
    1451             :                        this->is_ancestor_of(neigh_family_member->neighbor_ptr(nn)) ||
    1452             :                        neigh_family_member->neighbor_ptr(nn)->is_ancestor_of(this))) ||
    1453             :                      (neigh_family_member->neighbor_ptr(nn) == remote_elem) ||
    1454             :                      ((this->refinement_flag() == JUST_REFINED) &&
    1455             :                       (this->parent() != nullptr) &&
    1456             :                       (neigh_family_member->neighbor_ptr(nn) == this->parent())));
    1457             : #else
    1458             :       libmesh_assert((neigh_family_member->neighbor_ptr(nn) == this) ||
    1459             :                      (neigh_family_member->neighbor_ptr(nn) == remote_elem));
    1460             : #endif
    1461             : 
    1462             :       // Some of neigh's family might be at a higher (finer) level
    1463             :       // than us, and might need to point back to one of our children
    1464             :       // rather than to us.
    1465      153551 :       if (member_level > this_level)
    1466             :         {
    1467       37140 :           if (this->ancestor())
    1468       24760 :             continue;
    1469             : 
    1470           0 :           if (member_subactive && this->has_children())
    1471           0 :             continue;
    1472             :         }
    1473             : 
    1474             :       // If neigh is at a coarser level than us, some of neigh's
    1475             :       // subactive family just won't line up with us!
    1476      116411 :       if (neigh_level < this_level &&
    1477           0 :           member_level > neigh_level)
    1478             :         {
    1479           0 :           libmesh_assert(member_subactive);
    1480             : 
    1481           0 :           this->side_ptr(my_side, n);
    1482           0 :           neigh_family_member->side_ptr(neigh_side, nn);
    1483             : 
    1484           0 :           if (*my_side != *neigh_side)
    1485           0 :             continue;
    1486             :         }
    1487             : 
    1488      116411 :       neigh_family_member->set_neighbor(nn, this);
    1489             :     }
    1490      155192 : }
    1491             : 
    1492             : 
    1493        5474 : void Elem::make_links_to_me_remote()
    1494             : {
    1495        1821 :   libmesh_assert_not_equal_to (this, remote_elem);
    1496             : 
    1497             :   // We need to have handled any children first
    1498             : #if defined(LIBMESH_ENABLE_AMR) && defined(DEBUG)
    1499        1821 :   if (this->has_children())
    1500         268 :     for (auto & child : this->child_ref_range())
    1501         208 :       libmesh_assert_equal_to (&child, remote_elem);
    1502             : #endif
    1503             : 
    1504             :   // Remotify any neighbor links
    1505       29044 :   for (auto neigh : this->neighbor_ptr_range())
    1506             :     {
    1507       23570 :       if (neigh && neigh != remote_elem)
    1508             :         {
    1509             :           // My neighbor should never be more refined than me; my real
    1510             :           // neighbor would have been its parent in that case.
    1511        3937 :           libmesh_assert_greater_equal (this->level(), neigh->level());
    1512             : 
    1513       23666 :           if (this->level() == neigh->level() &&
    1514        7896 :               neigh->has_neighbor(this))
    1515             :             {
    1516             : #ifdef LIBMESH_ENABLE_AMR
    1517             :               // My neighbor may have descendants which also consider me a
    1518             :               // neighbor
    1519        7874 :               std::vector<Elem *> family;
    1520       11833 :               neigh->total_family_tree_by_neighbor (family, this);
    1521             : 
    1522             :               // FIXME - There's a lot of ugly const_casts here; we
    1523             :               // may want to make remote_elem non-const
    1524       23666 :               for (auto & n : family)
    1525             :                 {
    1526        3937 :                   libmesh_assert (n);
    1527       11833 :                   if (n == remote_elem)
    1528           0 :                     continue;
    1529       11833 :                   unsigned int my_s = n->which_neighbor_am_i(this);
    1530        3937 :                   libmesh_assert_less (my_s, n->n_neighbors());
    1531        3937 :                   libmesh_assert_equal_to (n->neighbor_ptr(my_s), this);
    1532       11833 :                   n->set_neighbor(my_s, const_cast<RemoteElem *>(remote_elem));
    1533             :                 }
    1534             : #else
    1535             :               unsigned int my_s = neigh->which_neighbor_am_i(this);
    1536             :               libmesh_assert_less (my_s, neigh->n_neighbors());
    1537             :               libmesh_assert_equal_to (neigh->neighbor_ptr(my_s), this);
    1538             :               neigh->set_neighbor(my_s, const_cast<RemoteElem *>(remote_elem));
    1539             : #endif
    1540             :             }
    1541             : #ifdef LIBMESH_ENABLE_AMR
    1542             :           // Even if my neighbor doesn't link back to me, it might
    1543             :           // have subactive descendants which do
    1544           0 :           else if (neigh->has_children())
    1545             :             {
    1546             :               // If my neighbor at the same level doesn't have me as a
    1547             :               // neighbor, I must be subactive
    1548           0 :               libmesh_assert(this->level() > neigh->level() ||
    1549             :                              this->subactive());
    1550             : 
    1551             :               // My neighbor must have some ancestor of mine as a
    1552             :               // neighbor
    1553           0 :               Elem * my_ancestor = this->parent();
    1554           0 :               libmesh_assert(my_ancestor);
    1555           0 :               while (!neigh->has_neighbor(my_ancestor))
    1556             :                 {
    1557           0 :                   my_ancestor = my_ancestor->parent();
    1558           0 :                   libmesh_assert(my_ancestor);
    1559             :                 }
    1560             : 
    1561             :               // My neighbor may have descendants which consider me a
    1562             :               // neighbor
    1563           0 :               std::vector<Elem *> family;
    1564           0 :               neigh->total_family_tree_by_subneighbor (family, my_ancestor, this);
    1565             : 
    1566           0 :               for (auto & n : family)
    1567             :                 {
    1568           0 :                   libmesh_assert (n);
    1569           0 :                   if (n->is_remote())
    1570           0 :                     continue;
    1571           0 :                   unsigned int my_s = n->which_neighbor_am_i(this);
    1572           0 :                   libmesh_assert_less (my_s, n->n_neighbors());
    1573           0 :                   libmesh_assert_equal_to (n->neighbor_ptr(my_s), this);
    1574             :                   // TODO: we may want to make remote_elem non-const.
    1575           0 :                   n->set_neighbor(my_s, const_cast<RemoteElem *>(remote_elem));
    1576             :                 }
    1577             :             }
    1578             : #endif
    1579             :         }
    1580             :     }
    1581             : 
    1582             : #ifdef LIBMESH_ENABLE_AMR
    1583             :   // Remotify parent's child link
    1584        3642 :   Elem * my_parent = this->parent();
    1585        3837 :   if (my_parent &&
    1586             :       // As long as it's not already remote
    1587        7490 :       my_parent != remote_elem &&
    1588             :       // And it's a real parent, not an interior parent
    1589        2016 :       this->dim() == my_parent->dim())
    1590             :     {
    1591        2016 :       unsigned int me = my_parent->which_child_am_i(this);
    1592         672 :       libmesh_assert_equal_to (my_parent->child_ptr(me), this);
    1593        2016 :       my_parent->set_child(me, const_cast<RemoteElem *>(remote_elem));
    1594             :     }
    1595             : #endif
    1596        5474 : }
    1597             : 
    1598             : 
    1599         109 : void Elem::remove_links_to_me()
    1600             : {
    1601          31 :   libmesh_assert_not_equal_to (this, remote_elem);
    1602             : 
    1603             :   // We need to have handled any children first
    1604             : #ifdef LIBMESH_ENABLE_AMR
    1605          31 :   libmesh_assert (!this->has_children());
    1606             : #endif
    1607             : 
    1608             :   // Nullify any neighbor links
    1609         545 :   for (auto neigh : this->neighbor_ptr_range())
    1610             :     {
    1611         436 :       if (neigh && neigh != remote_elem)
    1612             :         {
    1613             :           // My neighbor should never be more refined than me; my real
    1614             :           // neighbor would have been its parent in that case.
    1615          47 :           libmesh_assert_greater_equal (this->level(), neigh->level());
    1616             : 
    1617         330 :           if (this->level() == neigh->level() &&
    1618         118 :               neigh->has_neighbor(this))
    1619             :             {
    1620             : #ifdef LIBMESH_ENABLE_AMR
    1621             :               // My neighbor may have descendants which also consider me a
    1622             :               // neighbor
    1623          94 :               std::vector<Elem *> family;
    1624         165 :               neigh->total_family_tree_by_neighbor (family, this);
    1625             : 
    1626         330 :               for (auto & n : family)
    1627             :                 {
    1628          47 :                   libmesh_assert (n);
    1629         165 :                   if (n->is_remote())
    1630           0 :                     continue;
    1631         165 :                   unsigned int my_s = n->which_neighbor_am_i(this);
    1632          47 :                   libmesh_assert_less (my_s, n->n_neighbors());
    1633          47 :                   libmesh_assert_equal_to (n->neighbor_ptr(my_s), this);
    1634         165 :                   n->set_neighbor(my_s, nullptr);
    1635             :                 }
    1636             : #else
    1637             :               unsigned int my_s = neigh->which_neighbor_am_i(this);
    1638             :               libmesh_assert_less (my_s, neigh->n_neighbors());
    1639             :               libmesh_assert_equal_to (neigh->neighbor_ptr(my_s), this);
    1640             :               neigh->set_neighbor(my_s, nullptr);
    1641             : #endif
    1642             :             }
    1643             : #ifdef LIBMESH_ENABLE_AMR
    1644             :           // Even if my neighbor doesn't link back to me, it might
    1645             :           // have subactive descendants which do
    1646           0 :           else if (neigh->has_children())
    1647             :             {
    1648             :               // If my neighbor at the same level doesn't have me as a
    1649             :               // neighbor, I must be subactive
    1650           0 :               libmesh_assert(this->level() > neigh->level() ||
    1651             :                              this->subactive());
    1652             : 
    1653             :               // My neighbor must have some ancestor of mine as a
    1654             :               // neighbor
    1655           0 :               Elem * my_ancestor = this->parent();
    1656           0 :               libmesh_assert(my_ancestor);
    1657           0 :               while (!neigh->has_neighbor(my_ancestor))
    1658             :                 {
    1659           0 :                   my_ancestor = my_ancestor->parent();
    1660           0 :                   libmesh_assert(my_ancestor);
    1661             :                 }
    1662             : 
    1663             :               // My neighbor may have descendants which consider me a
    1664             :               // neighbor
    1665           0 :               std::vector<Elem *> family;
    1666           0 :               neigh->total_family_tree_by_subneighbor (family, my_ancestor, this);
    1667             : 
    1668           0 :               for (auto & n : family)
    1669             :                 {
    1670           0 :                   libmesh_assert (n);
    1671           0 :                   if (n->is_remote())
    1672           0 :                     continue;
    1673           0 :                   unsigned int my_s = n->which_neighbor_am_i(this);
    1674           0 :                   libmesh_assert_less (my_s, n->n_neighbors());
    1675           0 :                   libmesh_assert_equal_to (n->neighbor_ptr(my_s), this);
    1676           0 :                   n->set_neighbor(my_s, nullptr);
    1677             :                 }
    1678             :             }
    1679             : #endif
    1680             :         }
    1681             :     }
    1682             : 
    1683             : #ifdef LIBMESH_ENABLE_AMR
    1684             :   // We can't currently delete a child with a parent!
    1685          31 :   libmesh_assert (!this->parent());
    1686             : #endif
    1687         109 : }
    1688             : 
    1689             : 
    1690             : 
    1691       61770 : void Elem::write_connectivity (std::ostream & out_stream,
    1692             :                                const IOPackage iop) const
    1693             : {
    1694       16311 :   libmesh_assert (out_stream.good());
    1695       16311 :   libmesh_assert(_nodes);
    1696       16311 :   libmesh_assert_not_equal_to (iop, INVALID_IO_PACKAGE);
    1697             : 
    1698       61770 :   switch (iop)
    1699             :     {
    1700       16185 :     case TECPLOT:
    1701             :       {
    1702             :         // This connectivity vector will be used repeatedly instead
    1703             :         // of being reconstructed inside the loop.
    1704       32370 :         std::vector<dof_id_type> conn;
    1705      151428 :         for (auto sc : make_range(this->n_sub_elem()))
    1706             :           {
    1707       61266 :             this->connectivity(sc, TECPLOT, conn);
    1708             : 
    1709       61266 :             std::copy(conn.begin(),
    1710             :                       conn.end(),
    1711       16185 :                       std::ostream_iterator<dof_id_type>(out_stream, " "));
    1712             : 
    1713       61266 :             out_stream << '\n';
    1714             :           }
    1715       16185 :         return;
    1716             :       }
    1717             : 
    1718         126 :     case UCD:
    1719             :       {
    1720        4536 :         for (auto i : this->node_index_range())
    1721        5040 :           out_stream << this->node_id(i)+1 << "\t";
    1722             : 
    1723         504 :         out_stream << '\n';
    1724         504 :         return;
    1725             :       }
    1726             : 
    1727           0 :     default:
    1728           0 :       libmesh_error_msg("Unsupported IO package " << iop);
    1729             :     }
    1730             : }
    1731             : 
    1732             : 
    1733             : 
    1734      473269 : Real Elem::quality (const ElemQuality q) const
    1735             : {
    1736      473269 :   switch (q)
    1737             :     {
    1738             :       // Return the maximum ratio of edge lengths, or zero if that
    1739             :       // maximum would otherwise be infinity.
    1740       11225 :     case EDGE_LENGTH_RATIO:
    1741             :       {
    1742       11225 :         if (this->dim() < 2)
    1743           7 :           return 1;
    1744             : 
    1745       13998 :         std::vector<Real> edge_lengths(this->n_edges());
    1746       92063 :         for (auto e : index_range(edge_lengths))
    1747      141505 :           edge_lengths[e] = (this->point(this->local_edge_node(e,1)) -
    1748      141505 :                              this->point(this->local_edge_node(e,0))).norm();
    1749             : 
    1750        2801 :         auto [min, max] =
    1751       11197 :           std::minmax_element(edge_lengths.begin(), edge_lengths.end());
    1752             : 
    1753       11197 :         if (*min == 0.)
    1754           0 :           return 0.;
    1755             :         else
    1756       11197 :           return *max / *min;
    1757             :       }
    1758             : 
    1759       89396 :     case MIN_ANGLE:
    1760             :     case MAX_ANGLE:
    1761             :       {
    1762             :         // 1D elements don't have interior angles, so just return some
    1763             :         // dummy value in that case.
    1764       89396 :         if (this->dim() < 2)
    1765          14 :           return 0.;
    1766             : 
    1767             :         // Initialize return values
    1768       89340 :         Real min_angle = std::numeric_limits<Real>::max();
    1769       89340 :         Real max_angle = -std::numeric_limits<Real>::max();
    1770             : 
    1771     1016016 :         for (auto n : this->node_index_range())
    1772             :           {
    1773             :             // Get list of edge ids adjacent to this node.
    1774      926676 :             const auto adjacent_edge_ids = this->edges_adjacent_to_node(n);
    1775             : 
    1776             :             // Skip any nodes with fewer than 2 adjacent edges. You
    1777             :             // need at least two adjacent edges to form an interior
    1778             :             // element angle.
    1779      463436 :             auto N = adjacent_edge_ids.size();
    1780      926676 :             if (N < 2)
    1781      133098 :               continue;
    1782             : 
    1783             :             // Consider all possible pairs of edges adjacent to node n
    1784     1173204 :             for (unsigned int first = 0; first < N-1; ++first)
    1785     1951308 :               for (unsigned int second = first+1; second < N; ++second)
    1786             :                 {
    1787             :                   // Get ids of first and second edges
    1788     1465652 :                   auto first_edge = adjacent_edge_ids[first];
    1789     1172484 :                   auto second_edge = adjacent_edge_ids[second];
    1790             : 
    1791             :                   // Get node ids of first and second edge
    1792     1172484 :                   auto first_edge_node_0 = this->local_edge_node(first_edge, 0);
    1793     1172484 :                   auto first_edge_node_1 = this->local_edge_node(first_edge, 1);
    1794     1172484 :                   auto second_edge_node_0 = this->local_edge_node(second_edge, 0);
    1795     1172484 :                   auto second_edge_node_1 = this->local_edge_node(second_edge, 1);
    1796             : 
    1797             :                   // Orient both edges so that edge node 0 == n
    1798     1172484 :                   if (first_edge_node_0 != n)
    1799      201666 :                     std::swap(first_edge_node_0, first_edge_node_1);
    1800     1172484 :                   if (second_edge_node_0 != n)
    1801       96080 :                     std::swap(second_edge_node_0, second_edge_node_1);
    1802             : 
    1803      293168 :                   libmesh_assert_equal_to(first_edge_node_0, n);
    1804      293168 :                   libmesh_assert_equal_to(second_edge_node_0, n);
    1805             : 
    1806             :                   // Locally oriented edge vectors
    1807             :                   Point
    1808      586336 :                     first_ev = this->point(first_edge_node_1) - this->point(first_edge_node_0),
    1809      293168 :                     second_ev = this->point(second_edge_node_1) - this->point(second_edge_node_0);
    1810             : 
    1811             :                   // Angle between them in the range [0, pi]
    1812     1172484 :                   Real theta = std::acos(first_ev.unit() * second_ev.unit());
    1813             : 
    1814             :                   // Track min and max angles seen
    1815     1172484 :                   min_angle = std::min(theta, min_angle);
    1816     1172484 :                   max_angle = std::max(theta, max_angle);
    1817             :                 }
    1818             :           }
    1819             : 
    1820             :         // Return requested extreme value (in degrees)
    1821       89340 :         return Real(180)/libMesh::pi * ((q == MIN_ANGLE) ? min_angle : max_angle);
    1822             :       }
    1823             : 
    1824       86068 :     case MIN_DIHEDRAL_ANGLE:
    1825             :     case MAX_DIHEDRAL_ANGLE:
    1826             :       {
    1827             :         // For 1D and 2D elements, just call this function with MIN,MAX_ANGLE instead.
    1828       86068 :         if (this->dim() < 3)
    1829        2948 :           return this->quality((q == MIN_DIHEDRAL_ANGLE) ? MIN_ANGLE : MAX_ANGLE);
    1830             : 
    1831             :         // Initialize return values
    1832       83924 :         Real min_angle = std::numeric_limits<Real>::max();
    1833       83924 :         Real max_angle = -std::numeric_limits<Real>::max();
    1834             : 
    1835             :         // Algorithm for computing dihedral angles:
    1836             :         // .) Loop over the edges, use the edge_sides_map to get the
    1837             :         //    two sides adjacent to the edge.
    1838             :         // .) For each adjacent side, use the side_nodes_map to get the
    1839             :         //    list of three (or more) node ids on that side.
    1840             :         // .) Use the node ids to compute the (approximate) inward
    1841             :         //    normal for the side.  Note: this is approximate since 3D
    1842             :         //    elements with four-sided faces and quadratic tetrahedra
    1843             :         //    do not necessarily have planar faces.
    1844             :         // .) Compute the dihedral angle for the current edge, compare
    1845             :         //    it to the current min and max dihedral angles.
    1846      651488 :         for (auto e : this->edge_index_range())
    1847             :           {
    1848             :             // Get list of edge ids adjacent to this node.
    1849      709476 :             const auto adjacent_side_ids = this->sides_on_edge(e);
    1850             : 
    1851             :             // All 3D elements should have exactly two sides adjacent to each edge.
    1852      141912 :             libmesh_assert_equal_to(adjacent_side_ids.size(), 2);
    1853             : 
    1854             :             // Get lists of node ids on each side
    1855      709476 :             const auto side_0_node_ids = this->nodes_on_side(adjacent_side_ids[0]);
    1856      567564 :             const auto side_1_node_ids = this->nodes_on_side(adjacent_side_ids[1]);
    1857             : 
    1858             :             // All 3D elements have at least three nodes on each side
    1859      141912 :             libmesh_assert_greater_equal(side_0_node_ids.size(), 3);
    1860      141912 :             libmesh_assert_greater_equal(side_1_node_ids.size(), 3);
    1861             : 
    1862             :             // Construct (approximate) inward normal on each adjacent side
    1863             :             const auto side_0_normal =
    1864      709476 :               (this->point(side_0_node_ids[2]) - this->point(side_0_node_ids[0])).cross
    1865      709476 :               (this->point(side_0_node_ids[1]) - this->point(side_0_node_ids[0])).unit();
    1866             :             const auto side_1_normal =
    1867      709476 :               (this->point(side_1_node_ids[2]) - this->point(side_1_node_ids[0])).cross
    1868      709476 :               (this->point(side_1_node_ids[1]) - this->point(side_1_node_ids[0])).unit();
    1869             : 
    1870             :             // Compute dihedral angle between the planes.
    1871             :             // Using the absolute value ensures that we get the same result
    1872             :             // even if the orientation of one of the planes is flipped, i.e.
    1873             :             // it always gives us a value in the range [0, pi/2].
    1874             :             // https://en.wikipedia.org/wiki/Dihedral_angle
    1875      567564 :             Real theta = std::acos(std::abs(side_0_normal * side_1_normal));
    1876             : 
    1877             :             // Track min and max angles seen
    1878      567564 :             min_angle = std::min(theta, min_angle);
    1879      567564 :             max_angle = std::max(theta, max_angle);
    1880             :           }
    1881             : 
    1882             :         // Return requested extreme value (in degrees)
    1883       83924 :         return Real(180)/libMesh::pi * ((q == MIN_DIHEDRAL_ANGLE) ? min_angle : max_angle);
    1884             :       }
    1885             : 
    1886       77170 :     case JACOBIAN:
    1887             :     case SCALED_JACOBIAN:
    1888             :       {
    1889             :         // 1D elements don't have interior corners, so this metric
    1890             :         // does not really apply to them.
    1891       77170 :         const auto N = this->dim();
    1892       77170 :         if (N < 2)
    1893          14 :           return 1.;
    1894             : 
    1895             :         // Initialize return value
    1896       77114 :         Real min_node_area = std::numeric_limits<Real>::max();
    1897             : 
    1898      843206 :         for (auto n : this->node_index_range())
    1899             :           {
    1900             :             // Get list of edge ids adjacent to this node.
    1901      766092 :             auto adjacent_edge_ids = this->edges_adjacent_to_node(n);
    1902             : 
    1903             :             // Skip any nodes that don't have dim() adjacent edges. In 2D,
    1904             :             // you need at least two adjacent edges to compute the cross
    1905             :             // product, and in 3D, you need at least three adjacent edges
    1906             :             // to compute the scalar triple product. The only element
    1907             :             // type which has an unusual topology in this regard is the
    1908             :             // Pyramid element in 3D, where 4 edges meet at the apex node.
    1909             :             // For now, we just skip this node when computing the JACOBIAN
    1910             :             // metric for Pyramids.
    1911      957658 :             if (adjacent_edge_ids.size() != N)
    1912      109368 :               continue;
    1913             : 
    1914             :             // Construct oriented edges
    1915      328716 :             std::vector<Point> oriented_edges(N);
    1916     1304320 :             for (auto i : make_range(N))
    1917             :               {
    1918     1219556 :                 auto node_0 = this->local_edge_node(adjacent_edge_ids[i], 0);
    1919     1219556 :                 auto node_1 = this->local_edge_node(adjacent_edge_ids[i], 1);
    1920      975604 :                 if (node_0 != n)
    1921      121186 :                   std::swap(node_0, node_1);
    1922     1463508 :                 oriented_edges[i] = this->point(node_1) - this->point(node_0);
    1923             :               }
    1924             : 
    1925             :             // Compute unscaled area (2D) or volume (3D) using the
    1926             :             // cross product (2D) or scalar triple product (3D) of the
    1927             :             // oriented edges. We take the absolute value so that we
    1928             :             // don't have to worry about the sign of the area.
    1929      328716 :             Real node_area = (N == 2) ?
    1930       10544 :               cross_norm(oriented_edges[0], oriented_edges[1]) :
    1931      318172 :               std::abs(triple_product(oriented_edges[0], oriented_edges[1], oriented_edges[2]));
    1932             : 
    1933             :             // Divide by (non-zero) edge lengths if computing scaled Jacobian.
    1934             :             // If any length is zero, then set node_area to zero. This means that
    1935             :             // e.g. degenerate quadrilaterals will have a zero SCALED_JACOBIAN metric.
    1936      328716 :             if (q == SCALED_JACOBIAN)
    1937      673376 :               for (auto i : make_range(N))
    1938             :                 {
    1939      503610 :                   Real len_i = oriented_edges[i].norm();
    1940      503610 :                   node_area = (len_i == 0.) ? 0. : (node_area / len_i);
    1941             :                 }
    1942             : 
    1943             :             // Update minimum
    1944      328716 :             min_node_area = std::min(node_area, min_node_area);
    1945             :           }
    1946             : 
    1947       77114 :         return min_node_area;
    1948             :       }
    1949             : 
    1950             :       // Relative size metric: min over the corner nodes of min(tau,
    1951             :       // 1/tau), where tau is the ratio of the corner's nodal Jacobian
    1952             :       // determinant to that of the ideal (regular) element of the same
    1953             :       // volume -- ReferenceElem::ideal_target(), rescaled to this
    1954             :       // element's volume via the volume ratio. tau = 1 at every corner
    1955             :       // of an element whose Jacobian is uniform (any affine element:
    1956             :       // parallelogram, box, or regular simplex, at any scale), so the
    1957             :       // metric is 1; non-uniform (tapered/sheared) elements score below
    1958             :       // 1, and a degenerate corner drives it to 0.
    1959       36120 :     case SIZE:
    1960             :       {
    1961             :         // 1D elements don't have interior corners, so this metric does
    1962             :         // not really apply to them.
    1963       36120 :         const auto N = this->dim();
    1964       36120 :         if (N < 2)
    1965           0 :           return 1.;
    1966             : 
    1967       36120 :         const Real vol = this->volume();
    1968       36120 :         if (vol == 0.)
    1969           0 :           return 0.;
    1970             : 
    1971             :         // Ideal (regular) element of the same type; its Jacobian is
    1972             :         // uniform. We compare nodal determinants after rescaling it to
    1973             :         // this element's volume, i.e. multiply by ideal_vol / this_vol.
    1974       54184 :         const auto ideal_pair = ReferenceElem::ideal_target(this->type());
    1975        9032 :         const Elem & ideal = *ideal_pair.first;
    1976       36120 :         const Real vol_ratio = ideal.volume() / vol;
    1977             : 
    1978             :         // Nodal Jacobian determinant at node n of element el (the same
    1979             :         // construction as the JACOBIAN metric above).
    1980      153864 :         auto nodal_det = [](const Elem & el, const unsigned int n,
    1981             :                             const std::vector<unsigned int> & edge_ids,
    1982      153952 :                             const unsigned int dim)
    1983             :         {
    1984      307816 :           std::vector<Point> e(dim);
    1985     1221768 :           for (unsigned int i = 0; i != dim; ++i)
    1986             :             {
    1987     1142496 :               auto n0 = el.local_edge_node(edge_ids[i], 0);
    1988     1142496 :               auto n1 = el.local_edge_node(edge_ids[i], 1);
    1989      913952 :               if (n0 != n)
    1990      114272 :                 std::swap(n0, n1);
    1991     1371040 :               e[i] = el.point(n1) - el.point(n0);
    1992             :             }
    1993      307816 :           return (dim == 2) ? cross_norm(e[0], e[1])
    1994      608520 :                             : std::abs(triple_product(e[0], e[1], e[2]));
    1995             :         };
    1996             : 
    1997       36120 :         Real size = 1.;
    1998        9032 :         bool have_corner = false;
    1999      391852 :         for (auto n : this->node_index_range())
    2000             :           {
    2001             :             // Skip any nodes that don't have dim() adjacent edges (see
    2002             :             // the JACOBIAN metric above for the Pyramid apex caveat).
    2003      355732 :             const auto adjacent_edge_ids = this->edges_adjacent_to_node(n);
    2004      444676 :             if (adjacent_edge_ids.size() != N)
    2005       50456 :               continue;
    2006       38488 :             have_corner = true;
    2007             : 
    2008      153908 :             const Real a  = nodal_det(*this, n, adjacent_edge_ids, N);
    2009      153908 :             const Real aw = nodal_det(ideal, n, adjacent_edge_ids, N);
    2010             : 
    2011             :             // Degenerate corner: worst quality.
    2012      153908 :             if (a == 0. || aw == 0.)
    2013           0 :               return 0.;
    2014             : 
    2015      153908 :             const Real tau = (a / aw) * vol_ratio;
    2016      269328 :             size = std::min(size, std::min(tau, Real(1) / tau));
    2017             :           }
    2018             : 
    2019       36120 :         return have_corner ? size : 0.;
    2020             :       }
    2021             : 
    2022             :       // Maximum condition number of the nodal Jacobian over the corner
    2023             :       // nodes, measured against the ideal (regular) element rather than
    2024             :       // the reference element. At each corner the physical nodal
    2025             :       // Jacobian A and the ideal nodal Jacobian W (taken from the same
    2026             :       // corner of ReferenceElem::ideal_target) give the weighted
    2027             :       // Jacobian A W^{-1}, whose Frobenius condition number, via the
    2028             :       // corner metric tensors T_A = A^T A and T_W = W^T W (which also
    2029             :       // handles a lower-dimensional element embedded in 3D), is
    2030             :       //   kappa = sqrt(tr(T_A T_W^{-1}) * tr(T_W T_A^{-1})) / N.
    2031             :       // This is 1 for a corner similar to the ideal one -- so an
    2032             :       // equilateral triangle or regular tetrahedron scores 1, not just
    2033             :       // a right-angled corner -- and grows with distortion. A
    2034             :       // degenerate corner has an infinite condition number, reported as
    2035             :       // 0 (0 stands in for infinity, cf. EDGE_LENGTH_RATIO).
    2036       36106 :     case CONDITION:
    2037             :       {
    2038             :         // 1D elements don't have interior corners, so this metric does
    2039             :         // not really apply to them.
    2040       36106 :         const auto N = this->dim();
    2041       36106 :         if (N < 2)
    2042           0 :           return 1.;
    2043             : 
    2044             :         // Ideal (regular) element of the same type; W is its nodal
    2045             :         // Jacobian. Its scale is irrelevant here (the condition number
    2046             :         // is scale invariant), so the reference-volume sizing is fine.
    2047       54162 :         const auto ideal_pair = ReferenceElem::ideal_target(this->type());
    2048        9028 :         const Elem & ideal = *ideal_pair.first;
    2049             : 
    2050             :         // Corner metric tensor T = A^T A at node n of element el, padded
    2051             :         // with the identity in unused dimensions so that RealTensor's
    2052             :         // 3x3 inverse yields the correct NxN inverse.
    2053      153792 :         auto metric_tensor = [](const Elem & el, const unsigned int n,
    2054             :                                 const std::vector<unsigned int> & edge_ids,
    2055      153856 :                                 const unsigned int dim)
    2056             :         {
    2057      384576 :           std::vector<Point> e(dim);
    2058     1221152 :           for (unsigned int i = 0; i != dim; ++i)
    2059             :             {
    2060     1141920 :               auto n0 = el.local_edge_node(edge_ids[i], 0);
    2061     1141920 :               auto n1 = el.local_edge_node(edge_ids[i], 1);
    2062      913504 :               if (n0 != n)
    2063      114208 :                 std::swap(n0, n1);
    2064     1370336 :               e[i] = el.point(n1) - el.point(n0);
    2065             :             }
    2066      230720 :           RealTensor T(1, 0, 0,  0, 1, 0,  0, 0, 1);
    2067     1221152 :           for (unsigned int i = 0; i != dim; ++i)
    2068     3635136 :             for (unsigned int j = 0; j != dim; ++j)
    2069     4082656 :               T(i, j) = e[i] * e[j];
    2070      384576 :           return T;
    2071             :         };
    2072             : 
    2073             :         // kappa >= 1 for every matrix, so 1 is both the ideal value and
    2074             :         // a safe floor for the running maximum.
    2075       36106 :         Real max_cond = 1.;
    2076             : 
    2077      391754 :         for (auto n : this->node_index_range())
    2078             :           {
    2079             :             // Skip any nodes that don't have dim() adjacent edges (see
    2080             :             // the JACOBIAN metric above for the Pyramid apex caveat).
    2081      355648 :             const auto adjacent_edge_ids = this->edges_adjacent_to_node(n);
    2082      444568 :             if (adjacent_edge_ids.size() != N)
    2083       50456 :               continue;
    2084             : 
    2085      153824 :             const RealTensor Ta = metric_tensor(*this, n, adjacent_edge_ids, N);
    2086             : 
    2087             :             // Degenerate corner: infinite condition number.
    2088      153824 :             if (Ta.det() == 0.)
    2089           0 :               return 0.;
    2090             : 
    2091      153824 :             const RealTensor Tw = metric_tensor(ideal, n, adjacent_edge_ids, N);
    2092      153824 :             const RealTensor Ta_inv = Ta.inverse();
    2093      153824 :             const RealTensor Tw_inv = Tw.inverse();
    2094             : 
    2095             :             // num1 = tr(T_A T_W^{-1}), num2 = tr(T_W T_A^{-1}) over the
    2096             :             // NxN blocks (both symmetric, so summed as elementwise dot
    2097             :             // products).
    2098       38464 :             Real num1 = 0., num2 = 0.;
    2099      610576 :             for (auto i : make_range(N))
    2100     1817568 :               for (auto j : make_range(N))
    2101             :                 {
    2102     1360816 :                   num1 += Ta(i, j) * Tw_inv(i, j);
    2103     1360816 :                   num2 += Tw(i, j) * Ta_inv(i, j);
    2104             :                 }
    2105             : 
    2106      189508 :             max_cond = std::max(max_cond, std::sqrt(num1 * num2) / N);
    2107             :           }
    2108             : 
    2109       36106 :         return max_cond;
    2110             :       }
    2111             : 
    2112             :       // Return 1 if we made it here
    2113      137184 :     default:
    2114             :       {
    2115      137199 :         libmesh_do_once( libmesh_here();
    2116             : 
    2117             :                          libMesh::err << "ERROR: quality metric "
    2118             :                          << Utility::enum_to_string(q)
    2119             :                          << " not implemented on element type "
    2120             :                          << Utility::enum_to_string(this->type())
    2121             :                          << std::endl
    2122             :                          << "Returning 1."
    2123             :                          << std::endl; );
    2124             : 
    2125       34296 :         return 1.;
    2126             :       }
    2127             :     }
    2128             : }
    2129             : 
    2130             : 
    2131             : 
    2132    11532373 : bool Elem::ancestor() const
    2133             : {
    2134             : #ifdef LIBMESH_ENABLE_AMR
    2135             : 
    2136             :   // Use a fast, DistributedMesh-safe definition
    2137             :   const bool is_ancestor =
    2138     7320870 :     !this->active() && !this->subactive();
    2139             : 
    2140             :   // But check for inconsistencies if we have time
    2141             : #ifdef DEBUG
    2142     5521860 :   if (!is_ancestor && this->has_children())
    2143             :     {
    2144      557120 :       for (auto & c : this->child_ref_range())
    2145             :         {
    2146      446848 :           if (&c != remote_elem)
    2147             :             {
    2148      446800 :               libmesh_assert(!c.active());
    2149      446800 :               libmesh_assert(!c.ancestor());
    2150             :             }
    2151             :         }
    2152             :     }
    2153             : #endif // DEBUG
    2154             : 
    2155    11532373 :   return is_ancestor;
    2156             : 
    2157             : #else
    2158             :   return false;
    2159             : #endif
    2160             : }
    2161             : 
    2162             : 
    2163             : 
    2164             : #ifdef LIBMESH_ENABLE_AMR
    2165             : 
    2166       17726 : void Elem::add_child (Elem * elem)
    2167             : {
    2168       17726 :   const unsigned int nc = this->n_children();
    2169             : 
    2170       17726 :   if (!_children)
    2171             :     {
    2172        5885 :       _children = std::make_unique<Elem *[]>(nc);
    2173             : 
    2174       22175 :       for (unsigned int c = 0; c != nc; c++)
    2175        5724 :         this->set_child(c, nullptr);
    2176             :     }
    2177             : 
    2178       44245 :   for (unsigned int c = 0; c != nc; c++)
    2179             :     {
    2180       58535 :       if (this->_children[c] == nullptr || this->_children[c] == remote_elem)
    2181             :         {
    2182        5724 :           libmesh_assert_equal_to (this, elem->parent());
    2183        5724 :           this->set_child(c, elem);
    2184       17726 :           return;
    2185             :         }
    2186             :     }
    2187             : 
    2188           0 :   libmesh_error_msg("Error: Tried to add a child to an element with full children array");
    2189             : }
    2190             : 
    2191             : 
    2192             : 
    2193      975694 : void Elem::add_child (Elem * elem, unsigned int c)
    2194             : {
    2195      864606 :   if (!this->has_children())
    2196             :     {
    2197      225830 :       const unsigned int nc = this->n_children();
    2198      239496 :       _children = std::make_unique<Elem *[]>(nc);
    2199             : 
    2200     1200468 :       for (unsigned int i = 0; i != nc; i++)
    2201      864254 :         this->set_child(i, nullptr);
    2202             :     }
    2203             : 
    2204      864606 :   libmesh_assert (this->_children[c] == nullptr || this->child_ptr(c) == remote_elem);
    2205      864606 :   libmesh_assert (elem == remote_elem || this == elem->parent());
    2206             : 
    2207      864606 :   this->set_child(c, elem);
    2208      975694 : }
    2209             : 
    2210             : 
    2211             : 
    2212       78156 : void Elem::replace_child (Elem * elem, unsigned int c)
    2213             : {
    2214       24104 :   libmesh_assert(this->has_children());
    2215             : 
    2216       24104 :   libmesh_assert(this->child_ptr(c));
    2217             : 
    2218       24104 :   this->set_child(c, elem);
    2219       78156 : }
    2220             : 
    2221             : 
    2222             : 
    2223        3031 : void Elem::family_tree (std::vector<const Elem *> & family,
    2224             :                           bool reset) const
    2225             : {
    2226        3031 :   ElemInternal::family_tree(this, family, reset);
    2227        3031 : }
    2228             : 
    2229             : 
    2230             : 
    2231           0 : void Elem::family_tree (std::vector<Elem *> & family,
    2232             :                         bool reset)
    2233             : {
    2234           0 :   ElemInternal::family_tree(this, family, reset);
    2235           0 : }
    2236             : 
    2237             : 
    2238             : 
    2239        2921 : void Elem::total_family_tree (std::vector<const Elem *> & family,
    2240             :                               bool reset) const
    2241             : {
    2242        2921 :   ElemInternal::total_family_tree(this, family, reset);
    2243        2921 : }
    2244             : 
    2245             : 
    2246             : 
    2247     3380001 : void Elem::total_family_tree (std::vector<Elem *> & family,
    2248             :                               bool reset)
    2249             : {
    2250     3380001 :   ElemInternal::total_family_tree(this, family, reset);
    2251     3380001 : }
    2252             : 
    2253             : 
    2254             : 
    2255      126656 : void Elem::active_family_tree (std::vector<const Elem *> & active_family,
    2256             :                                bool reset) const
    2257             : {
    2258      126656 :   ElemInternal::active_family_tree(this, active_family, reset);
    2259      126656 : }
    2260             : 
    2261             : 
    2262             : 
    2263           0 : void Elem::active_family_tree (std::vector<Elem *> & active_family,
    2264             :                                bool reset)
    2265             : {
    2266           0 :   ElemInternal::active_family_tree(this, active_family, reset);
    2267           0 : }
    2268             : 
    2269             : 
    2270             : 
    2271           0 : void Elem::family_tree_by_side (std::vector<const Elem *> & family,
    2272             :                                 unsigned int side,
    2273             :                                 bool reset) const
    2274             : {
    2275           0 :   ElemInternal::family_tree_by_side(this, family, side, reset);
    2276           0 : }
    2277             : 
    2278             : 
    2279             : 
    2280           0 : void Elem:: family_tree_by_side (std::vector<Elem *> & family,
    2281             :                                  unsigned int side,
    2282             :                                  bool reset)
    2283             : {
    2284           0 :   ElemInternal::family_tree_by_side(this, family, side, reset);
    2285           0 : }
    2286             : 
    2287             : 
    2288             : 
    2289           0 : void Elem::total_family_tree_by_side (std::vector<const Elem *> & family,
    2290             :                                       unsigned int side,
    2291             :                                       bool reset) const
    2292             : {
    2293           0 :   ElemInternal::total_family_tree_by_side(this, family, side, reset);
    2294           0 : }
    2295             : 
    2296             : 
    2297             : 
    2298      116411 : void Elem::total_family_tree_by_side (std::vector<Elem *> & family,
    2299             :                                       unsigned int side,
    2300             :                                       bool reset)
    2301             : {
    2302      116411 :   ElemInternal::total_family_tree_by_side(this, family, side, reset);
    2303      116411 : }
    2304             : 
    2305             : 
    2306             : 
    2307      189053 : void Elem::active_family_tree_by_side (std::vector<const Elem *> & family,
    2308             :                                        unsigned int side,
    2309             :                                        bool reset) const
    2310             : {
    2311      189053 :   ElemInternal::active_family_tree_by_side(this, family, side, reset);
    2312      189053 : }
    2313             : 
    2314             : 
    2315             : 
    2316           0 : void Elem::active_family_tree_by_side (std::vector<Elem *> & family,
    2317             :                                        unsigned int side,
    2318             :                                        bool reset)
    2319             : {
    2320           0 :   ElemInternal::active_family_tree_by_side(this, family, side, reset);
    2321           0 : }
    2322             : 
    2323             : 
    2324             : 
    2325           0 : void Elem::family_tree_by_neighbor (std::vector<const Elem *> & family,
    2326             :                                     const Elem * neighbor,
    2327             :                                     bool reset) const
    2328             : {
    2329           0 :   ElemInternal::family_tree_by_neighbor(this, family, neighbor, reset);
    2330           0 : }
    2331             : 
    2332             : 
    2333             : 
    2334           0 : void Elem::family_tree_by_neighbor (std::vector<Elem *> & family,
    2335             :                                     Elem * neighbor,
    2336             :                                     bool reset)
    2337             : {
    2338           0 :   ElemInternal::family_tree_by_neighbor(this, family, neighbor, reset);
    2339           0 : }
    2340             : 
    2341             : 
    2342             : 
    2343           0 : void Elem::total_family_tree_by_neighbor (std::vector<const Elem *> & family,
    2344             :                                           const Elem * neighbor,
    2345             :                                           bool reset) const
    2346             : {
    2347           0 :   ElemInternal::total_family_tree_by_neighbor(this, family, neighbor, reset);
    2348           0 : }
    2349             : 
    2350             : 
    2351             : 
    2352       11998 : void Elem::total_family_tree_by_neighbor (std::vector<Elem *> & family,
    2353             :                                           Elem * neighbor,
    2354             :                                           bool reset)
    2355             : {
    2356       11998 :   ElemInternal::total_family_tree_by_neighbor(this, family, neighbor, reset);
    2357       11998 : }
    2358             : 
    2359             : 
    2360             : 
    2361           0 : void Elem::family_tree_by_subneighbor (std::vector<const Elem *> & family,
    2362             :                                        const Elem * neighbor,
    2363             :                                        const Elem * subneighbor,
    2364             :                                        bool reset) const
    2365             : {
    2366           0 :   ElemInternal::family_tree_by_subneighbor(this, family, neighbor, subneighbor, reset);
    2367           0 : }
    2368             : 
    2369             : 
    2370             : 
    2371           0 : void Elem::family_tree_by_subneighbor (std::vector<Elem *> & family,
    2372             :                                        Elem * neighbor,
    2373             :                                        Elem * subneighbor,
    2374             :                                        bool reset)
    2375             : {
    2376           0 :   ElemInternal::family_tree_by_subneighbor(this, family, neighbor, subneighbor, reset);
    2377           0 : }
    2378             : 
    2379             : 
    2380             : 
    2381           0 : void Elem::total_family_tree_by_subneighbor (std::vector<const Elem *> & family,
    2382             :                                              const Elem * neighbor,
    2383             :                                              const Elem * subneighbor,
    2384             :                                              bool reset) const
    2385             : {
    2386           0 :   ElemInternal::total_family_tree_by_subneighbor(this, family, neighbor, subneighbor, reset);
    2387           0 : }
    2388             : 
    2389             : 
    2390             : 
    2391           0 : void Elem::total_family_tree_by_subneighbor (std::vector<Elem *> & family,
    2392             :                                              Elem * neighbor,
    2393             :                                              Elem * subneighbor,
    2394             :                                              bool reset)
    2395             : {
    2396           0 :   ElemInternal::total_family_tree_by_subneighbor(this, family, neighbor, subneighbor, reset);
    2397           0 : }
    2398             : 
    2399             : 
    2400             : 
    2401     8105601 : void Elem::active_family_tree_by_neighbor (std::vector<const Elem *> & family,
    2402             :                                            const Elem * neighbor,
    2403             :                                            bool reset) const
    2404             : {
    2405     8105601 :   ElemInternal::active_family_tree_by_neighbor(this, family, neighbor, reset);
    2406     8105601 : }
    2407             : 
    2408             : 
    2409             : 
    2410         372 : void Elem::active_family_tree_by_neighbor (std::vector<Elem *> & family,
    2411             :                                            Elem * neighbor,
    2412             :                                            bool reset)
    2413             : {
    2414         372 :   ElemInternal::active_family_tree_by_neighbor(this, family, neighbor, reset);
    2415         372 : }
    2416             : 
    2417             : 
    2418             : 
    2419       23152 : void Elem::active_family_tree_by_topological_neighbor (std::vector<const Elem *> & family,
    2420             :                                                        const Elem * neighbor,
    2421             :                                                        const MeshBase & mesh,
    2422             :                                                        const PointLocatorBase & point_locator,
    2423             :                                                        const PeriodicBoundaries * pb,
    2424             :                                                        bool reset) const
    2425             : {
    2426       23152 :   ElemInternal::active_family_tree_by_topological_neighbor(this, family, neighbor,
    2427             :                                                            mesh, point_locator, pb,
    2428             :                                                            reset);
    2429       23152 : }
    2430             : 
    2431             : 
    2432             : 
    2433           0 : void Elem::active_family_tree_by_topological_neighbor (std::vector<Elem *> & family,
    2434             :                                                        Elem * neighbor,
    2435             :                                                        const MeshBase & mesh,
    2436             :                                                        const PointLocatorBase & point_locator,
    2437             :                                                        const PeriodicBoundaries * pb,
    2438             :                                                        bool reset)
    2439             : {
    2440           0 :   ElemInternal::active_family_tree_by_topological_neighbor(this, family, neighbor,
    2441             :                                                            mesh, point_locator, pb,
    2442             :                                                            reset);
    2443           0 : }
    2444             : 
    2445             : 
    2446    30717933 : bool Elem::is_child_on_edge(const unsigned int c,
    2447             :                             const unsigned int e) const
    2448             : {
    2449    28058004 :   libmesh_assert_less (c, this->n_children());
    2450    28058004 :   libmesh_assert_less (e, this->n_edges());
    2451             : 
    2452    57727639 :   std::unique_ptr<const Elem> my_edge = this->build_edge_ptr(e);
    2453    29669635 :   std::unique_ptr<const Elem> child_edge = this->child_ptr(c)->build_edge_ptr(e);
    2454             : 
    2455             :   // We're assuming that an overlapping child edge has the same
    2456             :   // number and orientation as its parent
    2457    55217309 :   return (child_edge->node_id(0) == my_edge->node_id(0) ||
    2458    81178717 :           child_edge->node_id(1) == my_edge->node_id(1));
    2459     1611631 : }
    2460             : 
    2461             : 
    2462             : 
    2463     5378997 : unsigned int Elem::min_p_level_by_neighbor(const Elem * neighbor_in,
    2464             :                                            unsigned int current_min) const
    2465             : {
    2466     1454087 :   libmesh_assert(!this->subactive());
    2467     1454087 :   libmesh_assert(neighbor_in->active());
    2468             : 
    2469             :   // If we're an active element this is simple
    2470     1454087 :   if (this->active())
    2471     6740172 :     return std::min(current_min, this->p_level());
    2472             : 
    2473       21066 :   libmesh_assert(has_neighbor(neighbor_in));
    2474             : 
    2475             :   // The p_level() of an ancestor element is already the minimum
    2476             :   // p_level() of its children - so if that's high enough, we don't
    2477             :   // need to examine any children.
    2478       93056 :   if (current_min <= this->p_level())
    2479       21066 :     return current_min;
    2480             : 
    2481           0 :   unsigned int min_p_level = current_min;
    2482             : 
    2483           0 :   for (auto & c : this->child_ref_range())
    2484           0 :     if (&c != remote_elem && c.has_neighbor(neighbor_in))
    2485             :       min_p_level =
    2486           0 :         c.min_p_level_by_neighbor(neighbor_in, min_p_level);
    2487             : 
    2488           0 :   return min_p_level;
    2489             : }
    2490             : 
    2491             : 
    2492      292130 : unsigned int Elem::min_new_p_level_by_neighbor(const Elem * neighbor_in,
    2493             :                                                unsigned int current_min) const
    2494             : {
    2495       95782 :   libmesh_assert(!this->subactive());
    2496       95782 :   libmesh_assert(neighbor_in->active());
    2497             : 
    2498             :   // If we're an active element this is simple
    2499       95782 :   if (this->active())
    2500             :     {
    2501      196150 :       unsigned int new_p_level = this->p_level();
    2502      260494 :       if (this->p_refinement_flag() == Elem::REFINE)
    2503           4 :         new_p_level += 1;
    2504      196150 :       if (this->p_refinement_flag() == Elem::COARSEN)
    2505             :         {
    2506           4 :           libmesh_assert_greater (new_p_level, 0);
    2507          14 :           new_p_level -= 1;
    2508             :         }
    2509      196150 :       return std::min(current_min, new_p_level);
    2510             :     }
    2511             : 
    2512       31438 :   libmesh_assert(has_neighbor(neighbor_in));
    2513             : 
    2514       95980 :   unsigned int min_p_level = current_min;
    2515             : 
    2516      488280 :   for (auto & c : this->child_ref_range())
    2517      520988 :     if (&c != remote_elem && c.has_neighbor(neighbor_in))
    2518             :       min_p_level =
    2519      196150 :         c.min_new_p_level_by_neighbor(neighbor_in, min_p_level);
    2520             : 
    2521       95980 :   return min_p_level;
    2522             : }
    2523             : 
    2524             : 
    2525             : 
    2526    29159524 : unsigned int Elem::as_parent_node (unsigned int child,
    2527             :                                    unsigned int child_node) const
    2528             : {
    2529    29159524 :   const unsigned int nc = this->n_children();
    2530     8514894 :   libmesh_assert_less(child, nc);
    2531             : 
    2532             :   // Cached return values, indexed first by embedding_matrix version,
    2533             :   // then by child number, then by child node number.
    2534             :   std::vector<std::vector<std::vector<signed char>>> &
    2535    29159524 :     cached_parent_indices = this->_get_parent_indices_cache();
    2536             : 
    2537    29159524 :   unsigned int em_vers = this->embedding_matrix_version();
    2538             : 
    2539             :   // We may be updating the cache on one thread, and while that
    2540             :   // happens we can't safely access the cache from other threads.
    2541    17029788 :   Threads::spin_mutex::scoped_lock lock(parent_indices_mutex);
    2542             : 
    2543    37674418 :   if (em_vers >= cached_parent_indices.size())
    2544         685 :     cached_parent_indices.resize(em_vers+1);
    2545             : 
    2546    46189312 :   if (child >= cached_parent_indices[em_vers].size())
    2547             :     {
    2548         689 :       const signed char nn = cast_int<signed char>(this->n_nodes());
    2549             : 
    2550         905 :       cached_parent_indices[em_vers].resize(nc);
    2551             : 
    2552        4052 :       for (unsigned int c = 0; c != nc; ++c)
    2553             :         {
    2554        3363 :           const unsigned int ncn = this->n_nodes_in_child(c);
    2555        5471 :           cached_parent_indices[em_vers][c].resize(ncn);
    2556       33624 :           for (unsigned int cn = 0; cn != ncn; ++cn)
    2557             :             {
    2558      144103 :               for (signed char n = 0; n != nn; ++n)
    2559             :                 {
    2560             :                   const Real em_val = this->embedding_matrix
    2561      144103 :                     (c, cn, n);
    2562      144103 :                   if (em_val == 1)
    2563             :                     {
    2564       15836 :                       cached_parent_indices[em_vers][c][cn] = n;
    2565        9808 :                       break;
    2566             :                     }
    2567             : 
    2568      134295 :                   if (em_val != 0)
    2569             :                     {
    2570       33061 :                       cached_parent_indices[em_vers][c][cn] =
    2571             :                         -1;
    2572       20453 :                       break;
    2573             :                     }
    2574             : 
    2575             :                   // We should never see an all-zero embedding matrix
    2576             :                   // row
    2577       34690 :                   libmesh_assert_not_equal_to (n+1, nn);
    2578             :                 }
    2579             :             }
    2580             :         }
    2581             :     }
    2582             : 
    2583             :   const signed char cache_val =
    2584    46189312 :     cached_parent_indices[em_vers][child][child_node];
    2585    29159524 :   if (cache_val == -1)
    2586     5364304 :     return libMesh::invalid_uint;
    2587             : 
    2588    10890795 :   return cached_parent_indices[em_vers][child][child_node];
    2589             : }
    2590             : 
    2591             : 
    2592             : 
    2593             : const std::vector<std::pair<unsigned char, unsigned char>> &
    2594    18467595 : Elem::parent_bracketing_nodes(unsigned int child,
    2595             :                               unsigned int child_node) const
    2596             : {
    2597             :   // Indexed first by embedding matrix type, then by child id, then by
    2598             :   // child node, then by bracketing pair
    2599             :   std::vector<std::vector<std::vector<std::vector<std::pair<unsigned char, unsigned char>>>>> &
    2600    18467595 :     cached_bracketing_nodes = this->_get_bracketing_node_cache();
    2601             : 
    2602    18467595 :   const unsigned int em_vers = this->embedding_matrix_version();
    2603             : 
    2604             :   // We may be updating the cache on one thread, and while that
    2605             :   // happens we can't safely access the cache from other threads.
    2606    11629540 :   Threads::spin_mutex::scoped_lock lock(parent_bracketing_nodes_mutex);
    2607             : 
    2608    24282365 :   if (cached_bracketing_nodes.size() <= em_vers)
    2609         629 :     cached_bracketing_nodes.resize(em_vers+1);
    2610             : 
    2611    18467595 :   const unsigned int nc = this->n_children();
    2612             : 
    2613             :   // If we haven't cached the bracketing nodes corresponding to this
    2614             :   // embedding matrix yet, let's do so now.
    2615    30097135 :   if (cached_bracketing_nodes[em_vers].size() < nc)
    2616             :     {
    2617             :       // If we're a second-order element but we're not a full-order
    2618             :       // element, then some of our bracketing nodes may not exist
    2619             :       // except on the equivalent full-order element.  Let's build an
    2620             :       // equivalent full-order element and make a copy of its cache to
    2621             :       // use.
    2622        1011 :       if (this->default_order() != FIRST &&
    2623         378 :           second_order_equivalent_type(this->type(), /*full_ordered=*/ true) != this->type())
    2624             :         {
    2625             :           // Check that we really are the non-full-order type
    2626           8 :           libmesh_assert_equal_to
    2627             :             (second_order_equivalent_type (this->type(), false),
    2628             :              this->type());
    2629             : 
    2630             :           // Build the full-order type
    2631             :           ElemType full_type =
    2632          26 :             second_order_equivalent_type(this->type(), /*full_ordered=*/ true);
    2633          34 :           std::unique_ptr<Elem> full_elem = Elem::build(full_type);
    2634             : 
    2635             :           // This won't work for elements with multiple
    2636             :           // embedding_matrix versions, but every such element is full
    2637             :           // order anyways.
    2638           8 :           libmesh_assert_equal_to(em_vers, 0);
    2639             : 
    2640             :           // Make sure its cache has been built.  We temporarily
    2641             :           // release our mutex lock so that the inner call can
    2642             :           // re-acquire it.
    2643           8 :           lock.release();
    2644          26 :           full_elem->parent_bracketing_nodes(0,0);
    2645             : 
    2646             :           // And then we need to lock again, so that if someone *else*
    2647             :           // grabbed our lock before we did we don't risk accessing
    2648             :           // cached_bracketing_nodes while they're working on it.
    2649             :           // Threading is hard.
    2650           8 :           lock.acquire(parent_bracketing_nodes_mutex);
    2651             : 
    2652             :           // Copy its cache
    2653             :           cached_bracketing_nodes =
    2654          26 :             full_elem->_get_bracketing_node_cache();
    2655             : 
    2656             :           // Now we don't need to build the cache ourselves.
    2657          42 :           return cached_bracketing_nodes[em_vers][child][child_node];
    2658          10 :         }
    2659             : 
    2660         799 :       cached_bracketing_nodes[em_vers].resize(nc);
    2661             : 
    2662         607 :       const unsigned int nn = this->n_nodes();
    2663             : 
    2664             :       // We have to examine each child
    2665        3418 :       for (unsigned int c = 0; c != nc; ++c)
    2666             :         {
    2667        2811 :           const unsigned int ncn = this->n_nodes_in_child(c);
    2668             : 
    2669        4599 :           cached_bracketing_nodes[em_vers][c].resize(ncn);
    2670             : 
    2671             :           // We have to examine each node in that child
    2672       24296 :           for (unsigned int n = 0; n != ncn; ++n)
    2673             :             {
    2674             :               // If this child node isn't a vertex or an infinite
    2675             :               // child element's mid-infinite-edge node, then we need
    2676             :               // to find bracketing nodes on the child.
    2677       21485 :               if (!this->is_vertex_on_child(c, n)
    2678             : #ifdef LIBMESH_ENABLE_INFINITE_ELEMENTS
    2679       16175 :                   && !this->is_mid_infinite_edge_node(n)
    2680             : #endif
    2681             :                   )
    2682             :                 {
    2683             :                   // Use the embedding matrix to find the child node
    2684             :                   // location in parent master element space
    2685        2572 :                   Point bracketed_pt;
    2686             : 
    2687      119276 :                   for (unsigned int pn = 0; pn != nn; ++pn)
    2688             :                     {
    2689             :                       const Real em_val =
    2690      110946 :                         this->embedding_matrix(c,n,pn);
    2691             : 
    2692       34140 :                       libmesh_assert_not_equal_to (em_val, 1);
    2693      110946 :                       if (em_val != 0.)
    2694       40290 :                         bracketed_pt.add_scaled(this->master_point(pn), em_val);
    2695             :                     }
    2696             : 
    2697             :                   // Check each pair of nodes on the child which are
    2698             :                   // also both parent nodes
    2699      119276 :                   for (unsigned int n1 = 0; n1 != ncn; ++n1)
    2700             :                     {
    2701      110946 :                       if (n1 == n)
    2702       69582 :                         continue;
    2703             : 
    2704             :                       unsigned int parent_n1 =
    2705      102616 :                         this->as_parent_node(c,n1);
    2706             : 
    2707      102616 :                       if (parent_n1 == libMesh::invalid_uint)
    2708       42492 :                         continue;
    2709             : 
    2710       41364 :                       Point p1 = this->master_point(parent_n1);
    2711             : 
    2712      435342 :                       for (unsigned int n2 = n1+1; n2 < nn; ++n2)
    2713             :                         {
    2714      403972 :                           if (n2 == n)
    2715      321862 :                             continue;
    2716             : 
    2717             :                           unsigned int parent_n2 =
    2718      372602 :                             this->as_parent_node(c,n2);
    2719             : 
    2720      372602 :                           if (parent_n2 == libMesh::invalid_uint)
    2721      201828 :                             continue;
    2722             : 
    2723       82110 :                           Point p2 = this->master_point(parent_n2);
    2724             : 
    2725       25444 :                           Point pmid = (p1 + p2)/2;
    2726             : 
    2727       25444 :                           if (pmid == bracketed_pt)
    2728             :                             {
    2729       19246 :                               cached_bracketing_nodes[em_vers][c][n].emplace_back(parent_n1, parent_n2);
    2730        9994 :                               break;
    2731             :                             }
    2732             :                           else
    2733       22360 :                             libmesh_assert(!pmid.absolute_fuzzy_equals(bracketed_pt));
    2734             :                         }
    2735             :                     }
    2736             :                 }
    2737             :               // If this child node is a parent node, we need to
    2738             :               // find bracketing nodes on the parent.
    2739             :               else
    2740             :                 {
    2741       13155 :                   unsigned int parent_node = this->as_parent_node(c,n);
    2742             : 
    2743        4202 :                   Point bracketed_pt;
    2744             : 
    2745             :                   // If we're not a parent node, use the embedding
    2746             :                   // matrix to find the child node location in parent
    2747             :                   // master element space
    2748       13155 :                   if (parent_node == libMesh::invalid_uint)
    2749             :                     {
    2750       42628 :                       for (unsigned int pn = 0; pn != nn; ++pn)
    2751             :                         {
    2752             :                           const Real em_val =
    2753       36613 :                             this->embedding_matrix(c,n,pn);
    2754             : 
    2755       11828 :                           libmesh_assert_not_equal_to (em_val, 1);
    2756       36613 :                           if (em_val != 0.)
    2757       17942 :                             bracketed_pt.add_scaled(this->master_point(pn), em_val);
    2758             :                         }
    2759             :                     }
    2760             :                   // If we're a parent node then we need no arithmetic
    2761             :                   else
    2762        7140 :                     bracketed_pt = this->master_point(parent_node);
    2763             : 
    2764      124914 :                   for (unsigned int n1 = 0; n1 != nn; ++n1)
    2765             :                     {
    2766      111759 :                       if (n1 == parent_node)
    2767        7140 :                         continue;
    2768             : 
    2769      104619 :                       Point p1 = this->master_point(n1);
    2770             : 
    2771      571295 :                       for (unsigned int n2 = n1+1; n2 < nn; ++n2)
    2772             :                         {
    2773      484573 :                           if (n2 == parent_node)
    2774       10792 :                             continue;
    2775             : 
    2776      450034 :                           Point pmid = (p1 + this->master_point(n2))/2;
    2777             : 
    2778      140116 :                           if (pmid == bracketed_pt)
    2779             :                             {
    2780       34841 :                               cached_bracketing_nodes[em_vers][c][n].emplace_back(n1, n2);
    2781        5648 :                               break;
    2782             :                             }
    2783             :                           else
    2784      134468 :                             libmesh_assert(!pmid.absolute_fuzzy_equals(bracketed_pt));
    2785             :                         }
    2786             :                     }
    2787             :                 }
    2788             :             }
    2789             :         }
    2790             :     }
    2791             : 
    2792    35911855 :   return cached_bracketing_nodes[em_vers][child][child_node];
    2793             : }
    2794             : 
    2795             : 
    2796             : const std::vector<std::pair<dof_id_type, dof_id_type>>
    2797    19944877 : Elem::bracketing_nodes(unsigned int child,
    2798             :                        unsigned int child_node) const
    2799             : {
    2800     6236850 :   std::vector<std::pair<dof_id_type, dof_id_type>> returnval;
    2801             : 
    2802             :   const std::vector<std::pair<unsigned char, unsigned char>> & pbc =
    2803    19944877 :     this->parent_bracketing_nodes(child,child_node);
    2804             : 
    2805    43086456 :   for (const auto & pb : pbc)
    2806             :     {
    2807    23141579 :       const unsigned short n_n = this->n_nodes();
    2808    23141579 :       if (pb.first < n_n && pb.second < n_n)
    2809    37407287 :         returnval.emplace_back(this->node_id(pb.first), this->node_id(pb.second));
    2810             :       else
    2811             :         {
    2812             :           // We must be on a non-full-order higher order element...
    2813       33008 :           libmesh_assert_not_equal_to(this->default_order(), FIRST);
    2814       33008 :           libmesh_assert_not_equal_to
    2815             :             (second_order_equivalent_type (this->type(), true),
    2816             :              this->type());
    2817       33008 :           libmesh_assert_equal_to
    2818             :             (second_order_equivalent_type (this->type(), false),
    2819             :              this->type());
    2820             : 
    2821             :           // And that's a shame, because this is a nasty search:
    2822             : 
    2823             :           // Build the full-order type
    2824             :           ElemType full_type =
    2825      115304 :             second_order_equivalent_type(this->type(), /*full_ordered=*/ true);
    2826      148312 :           std::unique_ptr<Elem> full_elem = Elem::build(full_type);
    2827             : 
    2828      115304 :           dof_id_type pt1 = DofObject::invalid_id;
    2829      115304 :           dof_id_type pt2 = DofObject::invalid_id;
    2830             : 
    2831             :           // Find the bracketing nodes by figuring out what
    2832             :           // already-created children will have them.
    2833             : 
    2834             :           // This only doesn't break horribly because we add children
    2835             :           // and nodes in straightforward + hierarchical orders...
    2836      630364 :           for (unsigned int c=0; c <= child; ++c)
    2837             :             {
    2838      147320 :               libmesh_assert(this->child_ptr(c));
    2839      515060 :               if (this->child_ptr(c) == remote_elem)
    2840           0 :                 continue;
    2841             : 
    2842     8945108 :               for (auto n : make_range(this->n_nodes_in_child(c)))
    2843             :                 {
    2844     8324932 :                   if (c == child && n == child_node)
    2845       33008 :                     break;
    2846             : 
    2847    10557508 :                   if (pb.first == full_elem->as_parent_node(c,n))
    2848             :                     {
    2849             :                       // We should be consistent
    2850       99160 :                       if (pt1 != DofObject::invalid_id)
    2851       66330 :                         libmesh_assert_equal_to(pt1, this->child_ptr(c)->node_id(n));
    2852             : 
    2853      445744 :                       pt1 = this->child_ptr(c)->node_id(n);
    2854             :                     }
    2855             : 
    2856    10557508 :                   if (pb.second == full_elem->as_parent_node(c,n))
    2857             :                     {
    2858             :                       // We should be consistent
    2859       72088 :                       if (pt2 != DofObject::invalid_id)
    2860       40682 :                         libmesh_assert_equal_to(pt2, this->child_ptr(c)->node_id(n));
    2861             : 
    2862      323976 :                       pt2 = this->child_ptr(c)->node_id(n);
    2863             :                     }
    2864             :                 }
    2865             :             }
    2866             : 
    2867             :           // We should *usually* find all bracketing nodes by the time
    2868             :           // we query them (again, because of the child & node add
    2869             :           // order)
    2870             :           //
    2871             :           // The exception is if we're a HEX20, in which case we will
    2872             :           // find pairs of vertex nodes and edge nodes bracketing the
    2873             :           // new central node but we *won't* find the pairs of face
    2874             :           // nodes which we would have had on a HEX27.  In that case
    2875             :           // we'll still have enough bracketing nodes for a
    2876             :           // topological lookup, but we won't be able to make the
    2877             :           // following assertions.
    2878      115304 :           if (this->type() != HEX20)
    2879             :             {
    2880       15920 :               libmesh_assert_not_equal_to (pt1, DofObject::invalid_id);
    2881       15920 :               libmesh_assert_not_equal_to (pt2, DofObject::invalid_id);
    2882             :             }
    2883             : 
    2884      115304 :           if (pt1 != DofObject::invalid_id &&
    2885      114681 :               pt2 != DofObject::invalid_id)
    2886      109697 :             returnval.emplace_back(pt1, pt2);
    2887       49288 :         }
    2888             :     }
    2889             : 
    2890    19944877 :   return returnval;
    2891             : }
    2892             : #endif // #ifdef LIBMESH_ENABLE_AMR
    2893             : 
    2894             : 
    2895             : 
    2896             : 
    2897    23092255 : bool Elem::contains_point (const Point & p, Real tol) const
    2898             : {
    2899             :   // We currently allow the user to enlarge the bounding box by
    2900             :   // providing a tol > TOLERANCE (so this routine is identical to
    2901             :   // Elem::close_to_point()), but print a warning so that the
    2902             :   // user can eventually switch his code over to calling close_to_point()
    2903             :   // instead, which is intended to be used for this purpose.
    2904    23092255 :   if (tol > TOLERANCE)
    2905             :     {
    2906           0 :       libmesh_do_once(libMesh::err
    2907             :                       << "WARNING: Resizing bounding box to match user-specified tolerance!\n"
    2908             :                       << "In the future, calls to Elem::contains_point() with tol > TOLERANCE\n"
    2909             :                       << "will be more optimized, but should not be used\n"
    2910             :                       << "to search for points 'close to' elements!\n"
    2911             :                       << "Instead, use Elem::close_to_point() for this purpose.\n"
    2912             :                       << std::endl;);
    2913           0 :       return this->point_test(p, tol, tol);
    2914             :     }
    2915             :   else
    2916    23092255 :     return this->point_test(p, TOLERANCE, tol);
    2917             : }
    2918             : 
    2919             : 
    2920             : 
    2921             : 
    2922     2838101 : bool Elem::close_to_point (const Point & p, Real tol) const
    2923             : {
    2924             :   // This test uses the user's passed-in tolerance for the
    2925             :   // bounding box test as well, thereby allowing the routine to
    2926             :   // find points which are not only "in" the element, but also
    2927             :   // "nearby" to within some tolerance.
    2928     2838101 :   return this->point_test(p, tol, tol);
    2929             : }
    2930             : 
    2931             : 
    2932             : 
    2933             : 
    2934    25930356 : bool Elem::point_test(const Point & p, Real box_tol, Real map_tol) const
    2935             : {
    2936     8017995 :   libmesh_assert_greater (box_tol, 0.);
    2937     8017995 :   libmesh_assert_greater (map_tol, 0.);
    2938             : 
    2939             :   // This is a great optimization on first order elements, but it
    2940             :   // could return false negatives on higher orders
    2941    25930356 :   if (this->default_order() == FIRST)
    2942             :     {
    2943             :       // Check to make sure the element *could* contain this point, so we
    2944             :       // can avoid an expensive inverse_map call if it doesn't.
    2945             :       bool
    2946             : #if LIBMESH_DIM > 2
    2947     3602981 :         point_above_min_z = false,
    2948     3602981 :         point_below_max_z = false,
    2949             : #endif
    2950             : #if LIBMESH_DIM > 1
    2951     3602981 :         point_above_min_y = false,
    2952     3602981 :         point_below_max_y = false,
    2953             : #endif
    2954     3602981 :         point_above_min_x = false,
    2955     3602981 :         point_below_max_x = false;
    2956             : 
    2957             :       // For relative bounding box checks in physical space
    2958    10439012 :       const Real my_hmax = this->hmax();
    2959             : 
    2960    69813633 :       for (auto & n : this->node_ref_range())
    2961             :         {
    2962    59374621 :           point_above_min_x = point_above_min_x || (n(0) - my_hmax*box_tol <= p(0));
    2963    59374621 :           point_below_max_x = point_below_max_x || (n(0) + my_hmax*box_tol >= p(0));
    2964             : #if LIBMESH_DIM > 1
    2965    59374621 :           point_above_min_y = point_above_min_y || (n(1) - my_hmax*box_tol <= p(1));
    2966    59374621 :           point_below_max_y = point_below_max_y || (n(1) + my_hmax*box_tol >= p(1));
    2967             : #endif
    2968             : #if LIBMESH_DIM > 2
    2969    59374621 :           point_above_min_z = point_above_min_z || (n(2) - my_hmax*box_tol <= p(2));
    2970    59374621 :           point_below_max_z = point_below_max_z || (n(2) + my_hmax*box_tol >= p(2));
    2971             : #endif
    2972             :         }
    2973             : 
    2974    10439012 :       if (
    2975             : #if LIBMESH_DIM > 2
    2976     3602981 :           !point_above_min_z ||
    2977     3485475 :           !point_below_max_z ||
    2978             : #endif
    2979             : #if LIBMESH_DIM > 1
    2980     9271881 :           !point_above_min_y ||
    2981     2403642 :           !point_below_max_y ||
    2982             : #endif
    2983     4707566 :           !point_above_min_x ||
    2984     1192007 :           !point_below_max_x)
    2985     2568638 :         return false;
    2986             :     }
    2987             : 
    2988             :   // To be on the safe side, we converge the inverse_map() iteration
    2989             :   // to a slightly tighter tolerance than that requested by the
    2990             :   // user...
    2991    24845129 :   const Point mapped_point = FEMap::inverse_map(this->dim(),
    2992             :                                                 this,
    2993             :                                                 p,
    2994             :                                                 0.1*map_tol, // <- this is |dx| tolerance, the Newton residual should be ~ |dx|^2
    2995             :                                                 /*secure=*/ false,
    2996    10797973 :                                                 /*extra_checks=*/ false);
    2997             : 
    2998             :   // Check that the refspace point maps back to p!  This is only necessary
    2999             :   // for 1D and 2D elements, 3D elements always live in 3D.
    3000             :   //
    3001             :   // TODO: The contains_point() function could most likely be implemented
    3002             :   // more efficiently in the element sub-classes themselves, at least for
    3003             :   // the linear element types.
    3004    19395772 :   if (this->dim() < 3)
    3005             :     {
    3006     8201254 :       Point xyz = FEMap::map(this->dim(), this, mapped_point);
    3007             : 
    3008             :       // Compute the distance between the original point and the re-mapped point.
    3009             :       // They should be in the same place.
    3010     5880859 :       Real dist = (xyz - p).norm();
    3011             : 
    3012             : 
    3013             :       // If dist is larger than some fraction of the tolerance, then return false.
    3014             :       // This can happen when e.g. a 2D element is living in 3D, and
    3015             :       // FEMap::inverse_map() maps p onto the projection of the element,
    3016             :       // effectively "tricking" on_reference_element().
    3017     8201254 :       if (dist > this->hmax() * map_tol)
    3018        2212 :         return false;
    3019             :     }
    3020             : 
    3021    19393560 :   return this->on_reference_element(mapped_point, map_tol);
    3022             : }
    3023             : 
    3024             : 
    3025             : 
    3026             : 
    3027       26264 : bool Elem::has_invertible_map(Real /*tol*/) const
    3028             : {
    3029       32830 :   QNodal qnodal {this->dim()};
    3030       39396 :   FEMap fe_map;
    3031        6566 :   auto & jac = fe_map.get_jacobian();
    3032             : 
    3033             :   // We have a separate check for is_singular_node() below, so in this
    3034             :   // case its "OK" to do nodal quadrature on pyramids.
    3035       26264 :   qnodal.allow_nodal_pyramid_quadrature = true;
    3036       26264 :   qnodal.init(*this);
    3037        6566 :   auto & qp = qnodal.get_points();
    3038        6566 :   libmesh_assert_equal_to(qp.size(), this->n_nodes());
    3039             : 
    3040       32830 :   std::vector<Point> one_point(1);
    3041       32830 :   std::vector<Real> one_weight(1,1);
    3042      363388 :   for (auto i : index_range(qp))
    3043             :     {
    3044      337124 :       if (this->is_singular_node(i))
    3045        2304 :         continue;
    3046             : 
    3047      334052 :       one_point[0] = qp[i];
    3048             : 
    3049      334052 :       fe_map.init_reference_to_physical_map(this->dim(), one_point, this);
    3050      334052 :       fe_map.compute_map(this->dim(), one_weight, this, false);
    3051             : 
    3052      334052 :       if (jac[0] <= 0)
    3053           0 :         return false;
    3054             :     }
    3055             : 
    3056       13132 :   return true;
    3057       13132 : }
    3058             : 
    3059             : 
    3060             : 
    3061           0 : void Elem::print_info (std::ostream & os) const
    3062             : {
    3063           0 :   os << this->get_info()
    3064           0 :      << std::endl;
    3065           0 : }
    3066             : 
    3067             : 
    3068             : 
    3069           0 : std::string Elem::get_info () const
    3070             : {
    3071           0 :   std::ostringstream oss;
    3072             : 
    3073             :   oss << "  Elem Information"                                      << '\n'
    3074           0 :       << "   id()=";
    3075             : 
    3076           0 :   if (this->valid_id())
    3077           0 :     oss << this->id();
    3078             :   else
    3079           0 :     oss << "invalid";
    3080             : 
    3081             : #ifdef LIBMESH_ENABLE_UNIQUE_ID
    3082           0 :   oss << ", unique_id()=";
    3083           0 :   if (this->valid_unique_id())
    3084           0 :     oss << this->unique_id();
    3085             :   else
    3086           0 :     oss << "invalid";
    3087             : #endif
    3088             : 
    3089           0 :   oss << ", subdomain_id()=" << this->subdomain_id();
    3090           0 :   oss << ", processor_id()=" << this->processor_id()               << '\n';
    3091             : 
    3092           0 :   oss << "   type()="    << Utility::enum_to_string(this->type())  << '\n'
    3093           0 :       << "   dim()="     << this->dim()                            << '\n'
    3094           0 :       << "   n_nodes()=" << this->n_nodes()                        << '\n';
    3095             : 
    3096           0 :   oss << "   mapping=" << Utility::enum_to_string(this->mapping_type()) << '\n';
    3097             : 
    3098           0 :   for (auto n : this->node_index_range())
    3099             :     {
    3100           0 :       oss << "    " << n << this->node_ref(n);
    3101           0 :       if (this->mapping_type() == RATIONAL_BERNSTEIN_MAP)
    3102             :         {
    3103           0 :           const unsigned char datum_index = this->mapping_data();
    3104           0 :           oss << "    weight=" <<
    3105           0 :             this->node_ref(n).get_extra_datum<Real>(datum_index) << '\n';
    3106             :         }
    3107             :     }
    3108             : 
    3109           0 :   oss << "   n_sides()=" << this->n_sides()                        << '\n';
    3110             : 
    3111           0 :   for (auto s : this->side_index_range())
    3112             :     {
    3113           0 :       oss << "    neighbor(" << s << ")=";
    3114           0 :       if (this->neighbor_ptr(s))
    3115           0 :         oss << this->neighbor_ptr(s)->id() << '\n';
    3116             :       else
    3117           0 :         oss << "nullptr\n";
    3118             :     }
    3119             : 
    3120           0 :   if (!this->infinite())
    3121             :     {
    3122           0 :     oss << "   hmin()=" << this->hmin()
    3123           0 :         << ", hmax()=" << this->hmax()                             << '\n'
    3124           0 :         << "   volume()=" << this->volume()                        << '\n';
    3125             :     }
    3126           0 :   oss << "   active()=" << this->active()
    3127           0 :     << ", ancestor()=" << this->ancestor()
    3128           0 :     << ", subactive()=" << this->subactive()
    3129           0 :     << ", has_children()=" << this->has_children()               << '\n'
    3130           0 :     << "   parent()=";
    3131           0 :   if (this->parent())
    3132           0 :     oss << this->parent()->id() << '\n';
    3133             :   else
    3134           0 :     oss << "nullptr\n";
    3135           0 :   oss << "   level()=" << this->level()
    3136           0 :       << ", p_level()=" << this->p_level()                         << '\n'
    3137             : #ifdef LIBMESH_ENABLE_AMR
    3138           0 :       << "   refinement_flag()=" << Utility::enum_to_string(this->refinement_flag())        << '\n'
    3139           0 :       << "   p_refinement_flag()=" << Utility::enum_to_string(this->p_refinement_flag())    << '\n'
    3140             : #endif
    3141             : #ifdef LIBMESH_ENABLE_INFINITE_ELEMENTS
    3142           0 :       << "   infinite()=" << this->infinite()    << '\n';
    3143           0 :   if (this->infinite())
    3144           0 :     oss << "   origin()=" << this->origin()    << '\n'
    3145             : #endif
    3146             :       ;
    3147             : 
    3148           0 :   oss << "   DoFs=";
    3149           0 :   for (auto s : make_range(this->n_systems()))
    3150           0 :     for (auto v : make_range(this->n_vars(s)))
    3151           0 :       for (auto c : make_range(this->n_comp(s,v)))
    3152           0 :         oss << '(' << s << '/' << v << '/' << this->dof_number(s,v,c) << ") ";
    3153             : 
    3154             : 
    3155           0 :   return oss.str();
    3156           0 : }
    3157             : 
    3158             : 
    3159             : 
    3160           0 : void Elem::nullify_neighbors ()
    3161             : {
    3162             :   // Tell any of my neighbors about my death...
    3163             :   // Looks strange, huh?
    3164           0 :   for (auto n : this->side_index_range())
    3165             :     {
    3166           0 :       Elem * current_neighbor = this->neighbor_ptr(n);
    3167           0 :       if (current_neighbor && current_neighbor != remote_elem)
    3168             :         {
    3169             :           // Note:  it is possible that I see the neighbor
    3170             :           // (which is coarser than me)
    3171             :           // but they don't see me, so avoid that case.
    3172           0 :           if (current_neighbor->level() == this->level())
    3173             :             {
    3174           0 :               const unsigned int w_n_a_i = current_neighbor->which_neighbor_am_i(this);
    3175           0 :               libmesh_assert_less (w_n_a_i, current_neighbor->n_neighbors());
    3176           0 :               current_neighbor->set_neighbor(w_n_a_i, nullptr);
    3177           0 :               this->set_neighbor(n, nullptr);
    3178             :             }
    3179             :         }
    3180             :     }
    3181           0 : }
    3182             : 
    3183             : 
    3184             : 
    3185             : 
    3186           0 : unsigned int Elem::n_second_order_adjacent_vertices (const unsigned int) const
    3187             : {
    3188             :   // for linear elements, always return 0
    3189           0 :   return 0;
    3190             : }
    3191             : 
    3192             : 
    3193             : 
    3194           0 : unsigned short int Elem::second_order_adjacent_vertex (const unsigned int,
    3195             :                                                        const unsigned int) const
    3196             : {
    3197             :   // for linear elements, always return 0
    3198           0 :   return 0;
    3199             : }
    3200             : 
    3201             : 
    3202             : 
    3203             : std::pair<unsigned short int, unsigned short int>
    3204           0 : Elem::second_order_child_vertex (const unsigned int) const
    3205             : {
    3206             :   // for linear elements, always return 0
    3207           0 :   return std::pair<unsigned short int, unsigned short int>(0,0);
    3208             : }
    3209             : 
    3210             : 
    3211             : 
    3212       85990 : ElemType Elem::first_order_equivalent_type (const ElemType et)
    3213             : {
    3214       26532 :   switch (et)
    3215             :     {
    3216          28 :     case NODEELEM:
    3217          28 :       return NODEELEM;
    3218        3118 :     case EDGE2:
    3219             :     case EDGE3:
    3220             :     case EDGE4:
    3221        3118 :       return EDGE2;
    3222         480 :     case TRI3:
    3223             :     case TRI6:
    3224             :     case TRI7:
    3225         480 :       return TRI3;
    3226           0 :     case TRISHELL3:
    3227           0 :       return TRISHELL3;
    3228       22338 :     case QUAD4:
    3229             :     case QUAD8:
    3230             :     case QUAD9:
    3231       22338 :       return QUAD4;
    3232           0 :     case QUADSHELL4:
    3233             :     case QUADSHELL8:
    3234             :     case QUADSHELL9:
    3235           0 :       return QUADSHELL4;
    3236           0 :     case TET4:
    3237             :     case TET10:
    3238             :     case TET14:
    3239           0 :       return TET4;
    3240           0 :     case HEX8:
    3241             :     case HEX27:
    3242             :     case HEX20:
    3243           0 :       return HEX8;
    3244         480 :     case PRISM6:
    3245             :     case PRISM15:
    3246             :     case PRISM18:
    3247             :     case PRISM20:
    3248             :     case PRISM21:
    3249         480 :       return PRISM6;
    3250           0 :     case PYRAMID5:
    3251             :     case PYRAMID13:
    3252             :     case PYRAMID14:
    3253             :     case PYRAMID18:
    3254           0 :       return PYRAMID5;
    3255             : 
    3256             : #ifdef LIBMESH_ENABLE_INFINITE_ELEMENTS
    3257             : 
    3258          16 :     case INFEDGE2:
    3259          16 :       return INFEDGE2;
    3260          72 :     case INFQUAD4:
    3261             :     case INFQUAD6:
    3262          72 :       return INFQUAD4;
    3263           0 :     case INFHEX8:
    3264             :     case INFHEX16:
    3265             :     case INFHEX18:
    3266           0 :       return INFHEX8;
    3267           0 :     case INFPRISM6:
    3268             :     case INFPRISM12:
    3269           0 :       return INFPRISM6;
    3270             : 
    3271             : #endif
    3272             : 
    3273           0 :     default:
    3274             :       // unknown element
    3275           0 :       return INVALID_ELEM;
    3276             :     }
    3277             : }
    3278             : 
    3279             : 
    3280             : // Most of our elements have the same topology on every side, but the
    3281             : // prisms and pyramids have both triangular and quadrilateral faces, and
    3282             : // a 2D or 3D infinite element's side 0 is its finite base while the rest
    3283             : // are infinite.  The polygons and polyhedra have one side type but no
    3284             : // fixed side count, so they can answer here too while their side index
    3285             : // goes unchecked; ask an actual Elem when you have one.
    3286       64831 : ElemType Elem::side_type (const ElemType t,
    3287             :                           const unsigned int s)
    3288             : {
    3289       51393 :   libmesh_assert_less (s, type_to_n_sides_map[t]);
    3290             : 
    3291       64831 :   switch (t)
    3292             :     {
    3293          36 :     case EDGE2:
    3294             :     case EDGE3:
    3295             :     case EDGE4:
    3296          36 :       return NODEELEM;
    3297        1710 :     case TRI3:
    3298             :     case TRISHELL3:
    3299             :     case QUAD4:
    3300             :     case QUADSHELL4:
    3301             :       // A polygon's sides are all edges and a polyhedron's are all
    3302             :       // polygons, however many a given element turns out to have
    3303             :     case C0POLYGON:
    3304        1710 :       return EDGE2;
    3305        3024 :     case TRI6:
    3306             :     case TRI7:
    3307             :     case QUAD8:
    3308             :     case QUADSHELL8:
    3309             :     case QUAD9:
    3310             :     case QUADSHELL9:
    3311        3024 :       return EDGE3;
    3312         576 :     case QUAD16:
    3313         576 :       return EDGE4;
    3314        6144 :     case TET4:
    3315        6144 :       return TRI3;
    3316       10752 :     case TET10:
    3317       10752 :       return TRI6;
    3318       12288 :     case TET14:
    3319       12288 :       return TRI7;
    3320         480 :     case HEX8:
    3321         480 :       return QUAD4;
    3322         864 :     case HEX20:
    3323         864 :       return QUAD8;
    3324         960 :     case HEX27:
    3325         960 :       return QUAD9;
    3326             :       // Sides 0 and 4 of a prism are its triangles
    3327         896 :     case PRISM6:
    3328         896 :       return (s == 0 || s == 4) ? TRI3 : QUAD4;
    3329        1472 :     case PRISM15:
    3330        1472 :       return (s == 0 || s == 4) ? TRI6 : QUAD8;
    3331        1568 :     case PRISM18:
    3332        1568 :       return (s == 0 || s == 4) ? TRI6 : QUAD9;
    3333        3264 :     case PRISM20:
    3334             :     case PRISM21:
    3335        3264 :       return (s == 0 || s == 4) ? TRI7 : QUAD9;
    3336             :       // Side 4 of a pyramid is its quadrilateral base
    3337        2496 :     case PYRAMID5:
    3338        2496 :       return (s < 4) ? TRI3 : QUAD4;
    3339        4032 :     case PYRAMID13:
    3340        4032 :       return (s < 4) ? TRI6 : QUAD8;
    3341        4128 :     case PYRAMID14:
    3342        4128 :       return (s < 4) ? TRI6 : QUAD9;
    3343        4512 :     case PYRAMID18:
    3344        4512 :       return (s < 4) ? TRI7 : QUAD9;
    3345          24 :     case C0POLYHEDRON:
    3346          24 :       return C0POLYGON;
    3347             : #ifdef LIBMESH_ENABLE_INFINITE_ELEMENTS
    3348             :       // An InfEdge2's sides are its end nodes, like any other 1D element's
    3349           0 :     case INFEDGE2:
    3350           0 :       return NODEELEM;
    3351             :       // A 2D or 3D infinite element's side 0 is the finite base it was
    3352             :       // built from; its remaining sides run out to infinity with it
    3353           9 :     case INFQUAD4:
    3354           9 :       return (s == 0) ? EDGE2 : INFEDGE2;
    3355           9 :     case INFQUAD6:
    3356           9 :       return (s == 0) ? EDGE3 : INFEDGE2;
    3357          15 :     case INFHEX8:
    3358          15 :       return (s == 0) ? QUAD4 : INFQUAD4;
    3359          15 :     case INFHEX16:
    3360          15 :       return (s == 0) ? QUAD8 : INFQUAD6;
    3361          15 :     case INFHEX18:
    3362          15 :       return (s == 0) ? QUAD9 : INFQUAD6;
    3363          12 :     case INFPRISM6:
    3364          12 :       return (s == 0) ? TRI3 : INFQUAD4;
    3365          12 :     case INFPRISM12:
    3366          12 :       return (s == 0) ? TRI6 : INFQUAD6;
    3367             : #endif
    3368           0 :     default:
    3369           0 :       libmesh_error_msg("No side type for element type " << Utility::enum_to_string(t));
    3370             :     }
    3371             : 
    3372             :   return INVALID_ELEM;
    3373             : }
    3374             : 
    3375             : 
    3376             : 
    3377             : // This reads the same constexpr side_nodes_map tables the element
    3378             : // classes use for their virtual local_side_node(), so that code which
    3379             : // knows an element type but has no Elem to call a virtual function on --
    3380             : // building a side of an element that doesn't exist yet, or a device
    3381             : // kernel that has only the type -- can still get at the reference
    3382             : // element topology.
    3383             : //
    3384             : // The polygons and polyhedra have no such table to read, and the
    3385             : // infinite elements' maps have no caller here yet, so both error out:
    3386             : // asking for a topology we can't answer is a programming error rather
    3387             : // than something to signal with a return value.
    3388             : // A finite element's edges all have the same type, unlike its sides,
    3389             : // which differ on the prisms and pyramids, so this takes no edge index.
    3390             : // The infinite elements are the exception -- the edges of their finite
    3391             : // base are not the ones running out to infinity -- and like the rest of
    3392             : // the static topology lookups they are left to their virtual overrides.
    3393       83300 : ElemType Elem::edge_type (const ElemType t)
    3394             : {
    3395       83300 :   switch (t)
    3396             :     {
    3397             :       // 1D elements have no edges
    3398           0 :     case EDGE2:
    3399             :     case EDGE3:
    3400             :     case EDGE4:
    3401           0 :       return INVALID_ELEM;
    3402             :       // A 2D element's edges are its sides
    3403        2804 :     case TRI3:
    3404             :     case TRISHELL3:
    3405             :     case TRI6:
    3406             :     case TRI7:
    3407             :     case QUAD4:
    3408             :     case QUADSHELL4:
    3409             :     case QUAD8:
    3410             :     case QUADSHELL8:
    3411             :     case QUAD9:
    3412             :     case QUADSHELL9:
    3413             :     case QUAD16:
    3414             :     case C0POLYGON:
    3415        2804 :       return side_type(t, 0);
    3416             :       // A first-order 3D element's edges hold their two vertices
    3417       21360 :     case TET4:
    3418             :     case HEX8:
    3419             :     case PRISM6:
    3420             :     case PYRAMID5:
    3421             :     case C0POLYHEDRON:
    3422       21360 :       return EDGE2;
    3423             :       // and a second-order element's add the midpoint
    3424       59136 :     case TET10:
    3425             :     case TET14:
    3426             :     case HEX20:
    3427             :     case HEX27:
    3428             :     case PRISM15:
    3429             :     case PRISM18:
    3430             :     case PRISM20:
    3431             :     case PRISM21:
    3432             :     case PYRAMID13:
    3433             :     case PYRAMID14:
    3434             :     case PYRAMID18:
    3435       59136 :       return EDGE3;
    3436           0 :     default:
    3437           0 :       libmesh_error_msg("No edge type for element type " << Utility::enum_to_string(t));
    3438             :     }
    3439             : 
    3440             :   return INVALID_ELEM;
    3441             : }
    3442             : 
    3443             : 
    3444             : 
    3445      183136 : unsigned int Elem::local_side_node (const ElemType t,
    3446             :                                     const unsigned int side,
    3447             :                                     const unsigned int side_node)
    3448             : {
    3449             :   // The nodes on a side are the nodes of the side's own element type, so
    3450             :   // we compose the two lookups here rather than tabulating side node
    3451             :   // counts a second time.  That also keeps us inside the meaningful part
    3452             :   // of a row: the prisms' and pyramids' triangular sides carry fewer
    3453             :   // nodes than their quadrilateral ones, and their rows are padded out
    3454             :   // to the longer length
    3455       45784 :   libmesh_assert_less (side, type_to_n_sides_map[t]);
    3456       45784 :   libmesh_assert_less (side_node, type_to_n_nodes_map[side_type(t, side)]);
    3457             : 
    3458      183136 :   switch (t)
    3459             :     {
    3460             :       // A 1D element's sides are its end nodes
    3461          24 :     case EDGE2:
    3462             :     case EDGE3:
    3463             :     case EDGE4:
    3464          24 :       return side;
    3465             :       // The shell elements are numbered like the elements they shadow
    3466        1536 :     case TRI3:
    3467             :     case TRISHELL3:
    3468        1536 :       return Tri3::side_nodes_map[side][side_node];
    3469        1152 :     case TRI6:
    3470        1152 :       return Tri6::side_nodes_map[side][side_node];
    3471        1152 :     case TRI7:
    3472        1152 :       return Tri7::side_nodes_map[side][side_node];
    3473        1024 :     case QUAD4:
    3474             :     case QUADSHELL4:
    3475        1024 :       return Quad4::side_nodes_map[side][side_node];
    3476        1536 :     case QUAD8:
    3477             :     case QUADSHELL8:
    3478        1536 :       return Quad8::side_nodes_map[side][side_node];
    3479        1024 :     case QUAD16:
    3480        1024 :       return Quad16::side_nodes_map[side][side_node];
    3481        1536 :     case QUAD9:
    3482             :     case QUADSHELL9:
    3483        1536 :       return Quad9::side_nodes_map[side][side_node];
    3484       18432 :     case TET4:
    3485       18432 :       return Tet4::side_nodes_map[side][side_node];
    3486       36864 :     case TET10:
    3487       36864 :       return Tet10::side_nodes_map[side][side_node];
    3488       43008 :     case TET14:
    3489       43008 :       return Tet14::side_nodes_map[side][side_node];
    3490        1536 :     case HEX8:
    3491        1536 :       return Hex8::side_nodes_map[side][side_node];
    3492        3072 :     case HEX20:
    3493        3072 :       return Hex20::side_nodes_map[side][side_node];
    3494        3456 :     case HEX27:
    3495        3456 :       return Hex27::side_nodes_map[side][side_node];
    3496        2304 :     case PRISM6:
    3497        2304 :       return Prism6::side_nodes_map[side][side_node];
    3498        4608 :     case PRISM15:
    3499        4608 :       return Prism15::side_nodes_map[side][side_node];
    3500        4992 :     case PRISM18:
    3501        4992 :       return Prism18::side_nodes_map[side][side_node];
    3502        5248 :     case PRISM20:
    3503        5248 :       return Prism20::side_nodes_map[side][side_node];
    3504        5248 :     case PRISM21:
    3505        5248 :       return Prism21::side_nodes_map[side][side_node];
    3506        6144 :     case PYRAMID5:
    3507        6144 :       return Pyramid5::side_nodes_map[side][side_node];
    3508       12288 :     case PYRAMID13:
    3509       12288 :       return Pyramid13::side_nodes_map[side][side_node];
    3510       12672 :     case PYRAMID14:
    3511       12672 :       return Pyramid14::side_nodes_map[side][side_node];
    3512       14208 :     case PYRAMID18:
    3513       14208 :       return Pyramid18::side_nodes_map[side][side_node];
    3514           0 :     default:
    3515           0 :       libmesh_error_msg("No static side node map for element type " << Utility::enum_to_string(t));
    3516             :     }
    3517             : 
    3518             :   return invalid_uint;
    3519             : }
    3520             : 
    3521             : 
    3522             : 
    3523             : // A 2D element's edges are its sides -- Face::local_edge_node() defines
    3524             : // the two to be the same thing -- so we answer those from the side map
    3525             : // and keep one copy of the 2D numbering.  1D elements have no edges to
    3526             : // ask about, and like local_side_node() above we error rather than
    3527             : // return a flag for a type we have no table for.
    3528      134272 : unsigned int Elem::local_edge_node (const ElemType t,
    3529             :                                     const unsigned int edge,
    3530             :                                     const unsigned int edge_node)
    3531             : {
    3532       33568 :   libmesh_assert_less (edge, type_to_n_edges_map[t]);
    3533       33568 :   libmesh_assert_less (edge_node, type_to_n_nodes_map[edge_type(t)]);
    3534             : 
    3535      134272 :   switch (t)
    3536             :     {
    3537        4480 :     case TRI3:
    3538             :     case TRISHELL3:
    3539             :     case TRI6:
    3540             :     case TRI7:
    3541             :     case QUAD4:
    3542             :     case QUADSHELL4:
    3543             :     case QUAD8:
    3544             :     case QUADSHELL8:
    3545             :     case QUAD9:
    3546             :     case QUADSHELL9:
    3547             :     case QUAD16:
    3548        4480 :       return local_side_node(t, edge, edge_node);
    3549       18432 :     case TET4:
    3550       18432 :       return Tet4::edge_nodes_map[edge][edge_node];
    3551       27648 :     case TET10:
    3552       27648 :       return Tet10::edge_nodes_map[edge][edge_node];
    3553       27648 :     case TET14:
    3554       27648 :       return Tet14::edge_nodes_map[edge][edge_node];
    3555        1536 :     case HEX8:
    3556        1536 :       return Hex8::edge_nodes_map[edge][edge_node];
    3557        2304 :     case HEX20:
    3558        2304 :       return Hex20::edge_nodes_map[edge][edge_node];
    3559        2304 :     case HEX27:
    3560        2304 :       return Hex27::edge_nodes_map[edge][edge_node];
    3561        2304 :     case PRISM6:
    3562        2304 :       return Prism6::edge_nodes_map[edge][edge_node];
    3563        3456 :     case PRISM15:
    3564        3456 :       return Prism15::edge_nodes_map[edge][edge_node];
    3565        3456 :     case PRISM18:
    3566        3456 :       return Prism18::edge_nodes_map[edge][edge_node];
    3567        3456 :     case PRISM20:
    3568        3456 :       return Prism20::edge_nodes_map[edge][edge_node];
    3569        3456 :     case PRISM21:
    3570        3456 :       return Prism21::edge_nodes_map[edge][edge_node];
    3571        6144 :     case PYRAMID5:
    3572        6144 :       return Pyramid5::edge_nodes_map[edge][edge_node];
    3573        9216 :     case PYRAMID13:
    3574        9216 :       return Pyramid13::edge_nodes_map[edge][edge_node];
    3575        9216 :     case PYRAMID14:
    3576        9216 :       return Pyramid14::edge_nodes_map[edge][edge_node];
    3577        9216 :     case PYRAMID18:
    3578        9216 :       return Pyramid18::edge_nodes_map[edge][edge_node];
    3579           0 :     default:
    3580           0 :       libmesh_error_msg("No static edge node map for element type " << Utility::enum_to_string(t));
    3581             :     }
    3582             : 
    3583             :   return invalid_uint;
    3584             : }
    3585             : 
    3586             : 
    3587             : 
    3588      473366 : ElemType Elem::second_order_equivalent_type (const ElemType et,
    3589             :                                              const bool full_ordered)
    3590             : {
    3591      473366 :   switch (et)
    3592             :     {
    3593           2 :     case NODEELEM:
    3594           2 :       return NODEELEM;
    3595         642 :     case EDGE2:
    3596             :     case EDGE3:
    3597             :       {
    3598             :         // full_ordered not relevant
    3599         642 :         return EDGE3;
    3600             :       }
    3601             : 
    3602           0 :     case EDGE4:
    3603             :       {
    3604             :         // full_ordered not relevant
    3605           0 :         return EDGE4;
    3606             :       }
    3607             : 
    3608        4238 :     case TRI3:
    3609             :     case TRI6:
    3610             :       {
    3611             :         // full_ordered not relevant
    3612        4238 :         return TRI6;
    3613             :       }
    3614             : 
    3615           0 :     case TRI7:
    3616           0 :       return TRI7;
    3617             : 
    3618             :       // Currently there is no TRISHELL6, so similarly to other types
    3619             :       // where this is the case, we just return the input.
    3620           0 :     case TRISHELL3:
    3621           0 :       return TRISHELL3;
    3622             : 
    3623        8578 :     case QUAD4:
    3624             :     case QUAD8:
    3625             :       {
    3626        8578 :         if (full_ordered)
    3627        2716 :           return QUAD9;
    3628             :         else
    3629         466 :           return QUAD8;
    3630             :       }
    3631             : 
    3632        2474 :     case QUADSHELL4:
    3633             :     case QUADSHELL8:
    3634             :       {
    3635        2474 :         if (full_ordered)
    3636         896 :           return QUADSHELL9;
    3637             :         else
    3638         448 :           return QUADSHELL8;
    3639             :       }
    3640             : 
    3641         260 :     case QUAD9:
    3642             :       {
    3643             :         // full_ordered not relevant
    3644         260 :         return QUAD9;
    3645             :       }
    3646             : 
    3647           0 :     case QUADSHELL9:
    3648             :       {
    3649             :         // full_ordered not relevant
    3650           0 :         return QUADSHELL9;
    3651             :       }
    3652             : 
    3653           0 :     case QUAD16:
    3654             :       {
    3655             :         // full_ordered not relevant
    3656           0 :         return QUAD16;
    3657             :       }
    3658             : 
    3659             : 
    3660      196485 :     case TET4:
    3661             :     case TET10:
    3662             :       {
    3663             :         // full_ordered not relevant
    3664      196485 :         return TET10;
    3665             :       }
    3666             : 
    3667           0 :     case TET14:
    3668           0 :       return TET14;
    3669             : 
    3670      125684 :     case HEX8:
    3671             :     case HEX20:
    3672             :       {
    3673             :         // see below how this correlates with INFHEX8
    3674      125684 :         if (full_ordered)
    3675       43662 :           return HEX27;
    3676             :         else
    3677       17090 :           return HEX20;
    3678             :       }
    3679             : 
    3680         460 :     case HEX27:
    3681             :       {
    3682             :         // full_ordered not relevant
    3683         460 :         return HEX27;
    3684             :       }
    3685             : 
    3686       81419 :     case PRISM6:
    3687             :     case PRISM15:
    3688             :       {
    3689       81419 :         if (full_ordered)
    3690       29598 :           return PRISM18;
    3691             :         else
    3692       14786 :           return PRISM15;
    3693             :       }
    3694             : 
    3695             :     // full_ordered not relevant, already fully second order
    3696          12 :     case PRISM18:
    3697          12 :       return PRISM18;
    3698           0 :     case PRISM20:
    3699           0 :       return PRISM20;
    3700           0 :     case PRISM21:
    3701           0 :       return PRISM21;
    3702           0 :     case PYRAMID18:
    3703           0 :       return PYRAMID18;
    3704             : 
    3705       51527 :     case PYRAMID5:
    3706             :     case PYRAMID13:
    3707             :       {
    3708       51527 :         if (full_ordered)
    3709        7374 :           return PYRAMID14;
    3710             :         else
    3711       14696 :           return PYRAMID13;
    3712             :       }
    3713             : 
    3714           0 :     case PYRAMID14:
    3715             :       {
    3716             :         // full_ordered not relevant
    3717           0 :         return PYRAMID14;
    3718             :       }
    3719             : 
    3720             : 
    3721             : 
    3722             : #ifdef LIBMESH_ENABLE_INFINITE_ELEMENTS
    3723             : 
    3724             :       // infinite elements
    3725           0 :     case INFEDGE2:
    3726             :       {
    3727           0 :         return INFEDGE2;
    3728             :       }
    3729             : 
    3730           5 :     case INFQUAD4:
    3731             :     case INFQUAD6:
    3732             :       {
    3733             :         // full_ordered not relevant
    3734           5 :         return INFQUAD6;
    3735             :       }
    3736             : 
    3737        1032 :     case INFHEX8:
    3738             :     case INFHEX16:
    3739             :       {
    3740             :         /*
    3741             :          * Note that this matches with \p Hex8:
    3742             :          * For full-ordered, \p InfHex18 and \p Hex27
    3743             :          * belong together, and for not full-ordered,
    3744             :          * \p InfHex16 and \p Hex20 belong together.
    3745             :          */
    3746        1032 :         if (full_ordered)
    3747         452 :           return INFHEX18;
    3748             :         else
    3749         226 :           return INFHEX16;
    3750             :       }
    3751             : 
    3752           4 :     case INFHEX18:
    3753             :       {
    3754             :         // full_ordered not relevant
    3755           4 :         return INFHEX18;
    3756             :       }
    3757             : 
    3758           5 :     case INFPRISM6:
    3759             :     case INFPRISM12:
    3760             :       {
    3761             :         // full_ordered not relevant
    3762           5 :         return INFPRISM12;
    3763             :       }
    3764             : 
    3765             : #endif
    3766             : 
    3767             : 
    3768           0 :     default:
    3769             :       {
    3770             :         // what did we miss?
    3771           0 :         libmesh_error_msg("No second order equivalent element type for et =  "
    3772             :                           << Utility::enum_to_string(et));
    3773             :       }
    3774             :     }
    3775             : }
    3776             : 
    3777             : 
    3778             : 
    3779      523753 : ElemType Elem::complete_order_equivalent_type (const ElemType et)
    3780             : {
    3781      523753 :   switch (et)
    3782             :     {
    3783           0 :     case NODEELEM:
    3784           0 :       return NODEELEM;
    3785         126 :     case EDGE2:
    3786             :     case EDGE3:
    3787         126 :       return EDGE3;
    3788             : 
    3789           0 :     case EDGE4:
    3790           0 :       return EDGE4;
    3791             : 
    3792        1365 :     case TRI3:
    3793             :     case TRI6:
    3794             :     case TRI7:
    3795        1365 :       return TRI7;
    3796             : 
    3797             :       // Currently there is no TRISHELL6, so similarly to other types
    3798             :       // where this is the case, we just return the input.
    3799           0 :     case TRISHELL3:
    3800           0 :       return TRISHELL3;
    3801             : 
    3802         100 :     case QUAD4:
    3803             :     case QUAD8:
    3804             :     case QUAD9:
    3805         100 :       return QUAD9;
    3806             : 
    3807           0 :     case QUADSHELL4:
    3808             :     case QUADSHELL8:
    3809             :     case QUADSHELL9:
    3810           0 :       return QUADSHELL9;
    3811             : 
    3812           0 :     case QUAD16:
    3813           0 :       return QUAD16;
    3814             : 
    3815      493133 :     case TET4:
    3816             :     case TET10:
    3817             :     case TET14:
    3818      493133 :       return TET14;
    3819             : 
    3820          56 :     case HEX8:
    3821             :     case HEX20:
    3822             :     case HEX27:
    3823          56 :       return HEX27;
    3824             : 
    3825             :     // We don't strictly need the 21st node for DoFs, but some code
    3826             :     // depends on it for e.g. refinement patterns
    3827        5782 :     case PRISM6:
    3828             :     case PRISM15:
    3829             :     case PRISM18:
    3830             :     case PRISM20:
    3831             :     case PRISM21:
    3832        5782 :       return PRISM21;
    3833             : 
    3834       23191 :     case PYRAMID5:
    3835             :     case PYRAMID13:
    3836             :     case PYRAMID14:
    3837             :     case PYRAMID18:
    3838       23191 :       return PYRAMID18;
    3839             : 
    3840             : 
    3841             : 
    3842             : #ifdef LIBMESH_ENABLE_INFINITE_ELEMENTS
    3843           0 :     case INFEDGE2:
    3844           0 :       return INFEDGE2;
    3845             : 
    3846           0 :     case INFQUAD4:
    3847             :     case INFQUAD6:
    3848           0 :       return INFQUAD6;
    3849             : 
    3850           0 :     case INFHEX8:
    3851             :     case INFHEX16:
    3852             :     case INFHEX18:
    3853           0 :       return INFHEX18;
    3854             : 
    3855             :     // Probably ought to implement INFPRISM13 and/or 14; until then
    3856             :     // we'll just return the not-complete-order ElemType, in
    3857             :     // accordance which what seems like bad precedent...
    3858           0 :     case INFPRISM6:
    3859             :     case INFPRISM12:
    3860           0 :       return INFPRISM12;
    3861             : #endif // LIBMESH_ENABLE_INFINITE_ELEMENTS
    3862             : 
    3863           0 :     default:
    3864             :       {
    3865             :         // what did we miss?
    3866           0 :         libmesh_error_msg("No complete order equivalent element type for et =  "
    3867             :                           << Utility::enum_to_string(et));
    3868             :       }
    3869             :     }
    3870             : }
    3871             : 
    3872             : 
    3873             : 
    3874           0 : Elem::side_iterator Elem::boundary_sides_begin()
    3875             : {
    3876           0 :   Predicates::BoundarySide<SideIter> bsp;
    3877           0 :   return side_iterator(this->_first_side(), this->_last_side(), bsp);
    3878             : }
    3879             : 
    3880             : 
    3881             : 
    3882             : 
    3883           0 : Elem::side_iterator Elem::boundary_sides_end()
    3884             : {
    3885           0 :   Predicates::BoundarySide<SideIter> bsp;
    3886           0 :   return side_iterator(this->_last_side(), this->_last_side(), bsp);
    3887             : }
    3888             : 
    3889             : 
    3890             : 
    3891             : 
    3892       54575 : Real Elem::volume () const
    3893             : {
    3894             :   // The default implementation builds a finite element of the correct
    3895             :   // order and sums up the JxW contributions.  This can be expensive,
    3896             :   // so the various element types can overload this method and compute
    3897             :   // the volume more efficiently.
    3898       54575 :   const FEFamily mapping_family = FEMap::map_fe_type(*this);
    3899       54575 :   const FEType fe_type(this->default_order(), mapping_family);
    3900             : 
    3901       54575 :   std::unique_ptr<FEBase> fe (FEBase::build(this->dim(),
    3902       72404 :                                             fe_type));
    3903             : 
    3904             :   // Use a map with a negative Jacobian tolerance, in case we're asked
    3905             :   // for the net volume of a tangled element
    3906       17829 :   fe->get_fe_map().set_jacobian_tolerance(std::numeric_limits<Real>::lowest());
    3907             : 
    3908       42264 :   const std::vector<Real> & JxW = fe->get_JxW();
    3909             : 
    3910             :   // The default quadrature rule should integrate the mass matrix,
    3911             :   // thus it should be plenty to compute the area
    3912       54575 :   QGauss qrule (this->dim(), fe_type.default_quadrature_order());
    3913             : 
    3914       54575 :   fe->attach_quadrature_rule(&qrule);
    3915             : 
    3916       54575 :   fe->reinit(this);
    3917             : 
    3918       17829 :   Real vol=0.;
    3919     2319826 :   for (auto jxw : JxW)
    3920     2265251 :     vol += jxw;
    3921             : 
    3922       72404 :   return vol;
    3923             : 
    3924       24435 : }
    3925             : 
    3926             : 
    3927             : 
    3928     9096055 : BoundingBox Elem::loose_bounding_box () const
    3929             : {
    3930     9096055 :   Point pmin = this->point(0);
    3931     9096055 :   Point pmax = pmin;
    3932             : 
    3933     9096055 :   unsigned int n_points = this->n_nodes();
    3934    46926422 :   for (unsigned int p=0; p != n_points; ++p)
    3935   151321468 :     for (unsigned d=0; d<LIBMESH_DIM; ++d)
    3936             :       {
    3937    85316802 :         const Point & pt = this->point(p);
    3938   113491101 :         if (pmin(d) > pt(d))
    3939     3301534 :           pmin(d) = pt(d);
    3940             : 
    3941   113491101 :         if (pmax(d) < pt(d))
    3942    18468532 :           pmax(d) = pt(d);
    3943             :       }
    3944             : 
    3945    13070330 :   return BoundingBox(pmin, pmax);
    3946             : }
    3947             : 
    3948             : Point
    3949        1113 : Elem::side_vertex_average_normal(const unsigned int s) const
    3950             : {
    3951        1113 :   unsigned int dim = this->dim();
    3952        1113 :   const std::unique_ptr<const Elem> face = this->build_side_ptr(s);
    3953             :   std::unique_ptr<libMesh::FEBase> fe(
    3954        1431 :       libMesh::FEBase::build(dim, libMesh::FEType(this->default_order())));
    3955         318 :   fe->add_p_level_in_reinit(false); // Yes, really use the default order
    3956         795 :   const std::vector<Point> & normals = fe->get_normals();
    3957        1113 :   std::vector<Point> ref_side_vertex_average_v = {face->reference_elem()->vertex_average()};
    3958        1113 :   fe->reinit(this, s, TOLERANCE, &ref_side_vertex_average_v);
    3959        2226 :   return normals[0];
    3960         477 : }
    3961             : 
    3962     1091642 : bool Elem::is_vertex_on_parent(unsigned int c,
    3963             :                                unsigned int n) const
    3964             : {
    3965             : #ifdef LIBMESH_ENABLE_AMR
    3966             : 
    3967     1091642 :   unsigned int my_n_vertices = this->n_vertices();
    3968     5403430 :   for (unsigned int n_parent = 0; n_parent != my_n_vertices;
    3969             :        ++n_parent)
    3970     7078917 :     if (this->node_ptr(n_parent) == this->child_ptr(c)->node_ptr(n))
    3971       54947 :       return true;
    3972      256846 :   return false;
    3973             : 
    3974             : #else
    3975             : 
    3976             :   // No AMR?
    3977             :   libmesh_ignore(c,n);
    3978             :   libmesh_error_msg("ERROR: AMR disabled, how did we get here?");
    3979             :   return true;
    3980             : 
    3981             : #endif
    3982             : }
    3983             : 
    3984             : 
    3985             : 
    3986           0 : unsigned int Elem::opposite_side(const unsigned int /*s*/) const
    3987             : {
    3988             :   // If the subclass didn't rederive this, using it is an error
    3989           0 :   libmesh_not_implemented();
    3990             : }
    3991             : 
    3992             : 
    3993             : 
    3994           0 : unsigned int Elem::opposite_node(const unsigned int /*n*/,
    3995             :                                  const unsigned int /*s*/) const
    3996             : {
    3997             :   // If the subclass didn't rederive this, using it is an error
    3998           0 :   libmesh_not_implemented();
    3999             : }
    4000             : 
    4001             : 
    4002       12675 : unsigned int Elem::center_node_on_side(const unsigned short libmesh_dbg_var(side)) const
    4003             : {
    4004        3176 :   libmesh_assert_less (side, this->n_sides());
    4005       12675 :   return invalid_uint;
    4006             : }
    4007             : 
    4008             : 
    4009       34953 : void Elem::swap2boundarysides(unsigned short s1,
    4010             :                               unsigned short s2,
    4011             :                               BoundaryInfo * boundary_info) const
    4012             : {
    4013       18988 :   std::vector<boundary_id_type> ids1, ids2;
    4014       34953 :   boundary_info->boundary_ids(this, s1, ids1);
    4015       34953 :   boundary_info->boundary_ids(this, s2, ids2);
    4016       34953 :   boundary_info->remove_side(this, s1);
    4017       34953 :   boundary_info->remove_side(this, s2);
    4018       34953 :   if (!ids1.empty())
    4019        1631 :     boundary_info->add_side(this, s2, ids1);
    4020       34953 :   if (!ids2.empty())
    4021        1074 :     boundary_info->add_side(this, s1, ids2);
    4022       34953 : }
    4023             : 
    4024             : 
    4025       70386 : void Elem::swap2boundaryedges(unsigned short e1,
    4026             :                               unsigned short e2,
    4027             :                               BoundaryInfo * boundary_info) const
    4028             : {
    4029       38036 :   std::vector<boundary_id_type> ids1, ids2;
    4030       70386 :   boundary_info->edge_boundary_ids(this, e1, ids1);
    4031       70386 :   boundary_info->edge_boundary_ids(this, e2, ids2);
    4032       70386 :   boundary_info->remove_edge(this, e1);
    4033       70386 :   boundary_info->remove_edge(this, e2);
    4034       70386 :   if (!ids1.empty())
    4035           0 :     boundary_info->add_edge(this, e2, ids1);
    4036       70386 :   if (!ids2.empty())
    4037           0 :     boundary_info->add_edge(this, e1, ids2);
    4038       70386 : }
    4039             : 
    4040             : bool
    4041     2010388 : Elem::is_internal(const unsigned int i) const
    4042             : {
    4043     2010388 :   switch (this->dim())
    4044             :   {
    4045           1 :     case 0:
    4046           1 :       return false;
    4047             : 
    4048          72 :     case 1:
    4049          72 :       return !this->is_vertex(i);
    4050             : 
    4051     1970543 :     case 2:
    4052     1970543 :       return !this->is_vertex(i) && !this->is_edge(i);
    4053             : 
    4054       39769 :     case 3:
    4055       39769 :       return !this->is_vertex(i) && !this->is_edge(i) && !this->is_face(i);
    4056             : 
    4057           0 :     default:
    4058           0 :       libmesh_error_msg("impossible element dimension " << std::to_string(this->dim()));
    4059             :       return 0;
    4060             :   }
    4061             : }
    4062             : 
    4063             : bool
    4064   275179750 : Elem::positive_edge_orientation(const unsigned int i) const
    4065             : {
    4066    70538481 :   libmesh_assert_less (i, this->n_edges());
    4067             : 
    4068   275179750 :   return this->point(this->local_edge_node(i, 0)) >
    4069   345718231 :          this->point(this->local_edge_node(i, 1));
    4070             : }
    4071             : 
    4072             : bool
    4073   104175488 : Elem::positive_face_orientation(const unsigned int i) const
    4074             : {
    4075   104175488 :   return this->face_orientation(i) % 2;
    4076             : }
    4077             : 
    4078             : 
    4079             : unsigned int
    4080   692278092 : Elem::face_orientation(const unsigned int i) const
    4081             : {
    4082   174883990 :   libmesh_assert_less (i, this->n_faces());
    4083             : 
    4084             :   // Get the number of vertices N of face i. Note that for 3d elements, i.e.
    4085             :   // elements for which this->n_faces() > 0, the number of vertices on any of
    4086             :   // its sides (or faces) is just the number of that face's sides (or edges).
    4087   692278092 :   const unsigned int N = Elem::type_to_n_sides_map[this->side_type(i)];
    4088             : 
    4089   692278092 :   const std::vector<unsigned int> nodes = this->nodes_on_side(i);
    4090             : 
    4091  1833368104 :   auto cmp = [&](const unsigned int & m, const unsigned int & n) -> bool
    4092  3863040418 :              { return this->point(m) < this->point(n); };
    4093             : 
    4094   692278092 :   const unsigned int v = std::distance(nodes.begin(),
    4095   692278092 :                          std::min_element(nodes.begin(), nodes.begin() + N, cmp));
    4096             : 
    4097             :   // v is the position of the least vertex. Compare its neighbors just
    4098             :   // before and after it, cyclically: the face is positively oriented iff
    4099             :   // the lesser neighbor precedes v, i.e. the vertices run backward from
    4100             :   // the least vertex through the face's node map.
    4101  1391814052 :   return 2 * v + cmp(nodes[(v - 1 + N) % N], nodes[(v + 1) % N]);
    4102             : }
    4103             : 
    4104             : std::vector<unsigned int>
    4105   578450080 : Elem::oriented_face_vertices(const unsigned int i) const
    4106             : {
    4107   578450080 :   const unsigned int N = Elem::type_to_n_sides_map[this->side_type(i)];
    4108   578450080 :   const unsigned int orientation = this->face_orientation(i);
    4109   578450080 :   const unsigned int least = orientation / 2;
    4110             : 
    4111             :   // A positively oriented face has the lesser neighbor before the least
    4112             :   // vertex in its side node map
    4113   578450080 :   const bool backward = orientation % 2;
    4114             : 
    4115   578450080 :   std::vector<unsigned int> vertices(N);
    4116  2605721952 :   for (const auto k : make_range(N))
    4117  2027271872 :     vertices[k] = this->local_side_node(i, (least + (backward ? N - k : k)) % N);
    4118             : 
    4119   578450080 :   return vertices;
    4120             : }
    4121             : 
    4122             : bool
    4123      143208 : Elem::relative_edge_face_order(const unsigned int e, const unsigned int s) const
    4124             : {
    4125           0 :   libmesh_assert_less (e, this->n_edges());
    4126           0 :   libmesh_assert_less (s, this->n_faces());
    4127           0 :   libmesh_assert (is_edge_on_side(e, s));
    4128             : 
    4129      143208 :   const unsigned int N = Elem::type_to_n_sides_map[this->side_type(s)];
    4130             : 
    4131             :   unsigned int v;
    4132      255312 :   for (v = 0; v < N; v++)
    4133      255312 :     if (this->local_side_node(s, v) == this->local_edge_node(e, 0))
    4134           0 :       break;
    4135             : 
    4136           0 :   libmesh_assert (this->local_side_node(s, (v + 1) % N) ==
    4137             :                   this->local_edge_node(e, 1) ||
    4138             :                   this->local_side_node(s, (v - 1 + N) % N) ==
    4139             :                   this->local_edge_node(e, 1));
    4140             : 
    4141      143208 :   return this->local_side_node(s, (v + 1) % N) ==
    4142      143208 :          this->local_edge_node(e, 1);
    4143             : }
    4144             : 
    4145             : } // namespace libMesh

Generated by: LCOV version 1.14