LCOV - code coverage report
Current view: top level - src/mesh - mesh_base.C (source / functions) Hit Total Coverage
Test: libMesh/libmesh: #4476 (4beb67) with base a68cc6 Lines: 1162 1375 84.5 %
Date: 2026-06-03 20:22:46 Functions: 96 110 87.3 %
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 and/or
       5             : // modify it under the terms of the GNU Lesser General Public
       6             : // License as published by the Free Software Foundation; either
       7             : // version 2.1 of the License, or (at your option) any later version.
       8             : 
       9             : // This library is distributed in the hope that it will be useful,
      10             : // but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      12             : // Lesser General Public License for more details.
      13             : 
      14             : // You should have received a copy of the GNU Lesser General Public
      15             : // License along with this library; if not, write to the Free Software
      16             : // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
      17             : 
      18             : 
      19             : 
      20             : // library configuration
      21             : #include "libmesh/libmesh_config.h"
      22             : 
      23             : // Local includes
      24             : #include "libmesh/boundary_info.h"
      25             : #include "libmesh/libmesh_logging.h"
      26             : #include "libmesh/elem.h"
      27             : #include "libmesh/ghost_point_neighbors.h"
      28             : #include "libmesh/mesh_base.h"
      29             : #include "libmesh/mesh_communication.h"
      30             : #include "libmesh/mesh_serializer.h"
      31             : #include "libmesh/mesh_tools.h"
      32             : #include "libmesh/parallel.h"
      33             : #include "libmesh/parallel_algebra.h"
      34             : #include "libmesh/parallel_fe_type.h"
      35             : #include "libmesh/partitioner.h"
      36             : #include "libmesh/point_locator_base.h"
      37             : #include "libmesh/sparse_matrix.h"
      38             : #include "libmesh/threads.h"
      39             : #include "libmesh/enum_elem_type.h"
      40             : #include "libmesh/enum_point_locator_type.h"
      41             : #include "libmesh/enum_to_string.h"
      42             : #include "libmesh/point_locator_nanoflann.h"
      43             : #include "libmesh/elem_side_builder.h"
      44             : #include "libmesh/elem_range.h"
      45             : #include "libmesh/node_range.h"
      46             : 
      47             : // C++ includes
      48             : #include <algorithm> // for std::min
      49             : #include <map>       // for std::multimap
      50             : #include <memory>
      51             : #include <sstream>   // for std::ostringstream
      52             : #include <unordered_map>
      53             : 
      54             : #include "libmesh/periodic_boundaries.h"
      55             : #include "libmesh/periodic_boundary.h"
      56             : 
      57             : namespace libMesh
      58             : {
      59             : 
      60             : 
      61             : 
      62             : // ------------------------------------------------------------
      63             : // MeshBase class member functions
      64      331691 : MeshBase::MeshBase (const Parallel::Communicator & comm_in,
      65      331691 :                     unsigned char d) :
      66             :   ParallelObject (comm_in),
      67      331691 :   boundary_info  (new BoundaryInfo(*this)), // BoundaryInfo has protected ctor, can't use std::make_unique
      68      312431 :   _n_parts       (1),
      69      312431 :   _default_mapping_type(LAGRANGE_MAP),
      70      312431 :   _default_mapping_data(0),
      71      312431 :   _preparation (),
      72      312431 :   _element_stored_range (),
      73      312431 :   _const_active_local_element_stored_range (),
      74      312431 :   _point_locator (),
      75      312431 :   _count_lower_dim_elems_in_point_locator(true),
      76      312431 :   _partitioner   (),
      77             : #ifdef LIBMESH_ENABLE_UNIQUE_ID
      78      312431 :   _next_unique_id(DofObject::invalid_unique_id),
      79             : #endif
      80      312431 :   _interior_mesh(this),
      81      312431 :   _skip_noncritical_partitioning(false),
      82      341321 :   _skip_all_partitioning(libMesh::on_command_line("--skip-partitioning")),
      83      312431 :   _skip_renumber_nodes_and_elements(false),
      84      312431 :   _skip_find_neighbors(false),
      85      312431 :   _skip_detect_interior_parents(false),
      86      312431 :   _allow_remote_element_removal(true),
      87      312431 :   _allow_node_and_elem_unique_id_overlap(false),
      88      312431 :   _spatial_dimension(d),
      89      331691 :   _default_ghosting(std::make_unique<GhostPointNeighbors>(*this)),
      90     1043223 :   _point_locator_close_to_point_tol(0.)
      91             : {
      92      322061 :   _elem_dims.insert(d);
      93      331691 :   _ghosting_functors.push_back(_default_ghosting.get());
      94        9630 :   libmesh_assert_less_equal (LIBMESH_DIM, 3);
      95        9630 :   libmesh_assert_greater_equal (LIBMESH_DIM, d);
      96        9630 :   libmesh_assert (libMesh::initialized());
      97      331691 : }
      98             : 
      99             : 
     100             : 
     101       24392 : MeshBase::MeshBase (const MeshBase & other_mesh) :
     102             :   ParallelObject (other_mesh),
     103       24392 :   boundary_info  (new BoundaryInfo(*this)), // BoundaryInfo has protected ctor, can't use std::make_unique
     104       24392 :   _n_parts       (other_mesh._n_parts),
     105       24392 :   _default_mapping_type(other_mesh._default_mapping_type),
     106       24392 :   _default_mapping_data(other_mesh._default_mapping_data),
     107       22800 :   _preparation (other_mesh._preparation),
     108       22800 :   _element_stored_range (),
     109       22800 :   _const_active_local_element_stored_range (),
     110       22800 :   _point_locator (),
     111       24392 :   _count_lower_dim_elems_in_point_locator(other_mesh._count_lower_dim_elems_in_point_locator),
     112       22800 :   _partitioner   (),
     113             : #ifdef LIBMESH_ENABLE_UNIQUE_ID
     114       24392 :   _next_unique_id(other_mesh._next_unique_id),
     115             : #endif
     116             :   // If the other mesh interior_parent pointers just go back to
     117             :   // itself, so should we
     118       24392 :   _interior_mesh((other_mesh._interior_mesh == &other_mesh) ?
     119             :                  this : other_mesh._interior_mesh),
     120       24392 :   _skip_noncritical_partitioning(other_mesh._skip_noncritical_partitioning),
     121       24392 :   _skip_all_partitioning(other_mesh._skip_all_partitioning),
     122       24392 :   _skip_renumber_nodes_and_elements(other_mesh._skip_renumber_nodes_and_elements),
     123       24392 :   _skip_find_neighbors(other_mesh._skip_find_neighbors),
     124       24392 :   _skip_detect_interior_parents(other_mesh._skip_detect_interior_parents),
     125       24392 :   _allow_remote_element_removal(other_mesh._allow_remote_element_removal),
     126       24392 :   _allow_node_and_elem_unique_id_overlap(other_mesh._allow_node_and_elem_unique_id_overlap),
     127         804 :   _elem_dims(other_mesh._elem_dims),
     128         804 :   _elem_default_orders(other_mesh._elem_default_orders),
     129       24392 :   _supported_nodal_order(other_mesh._supported_nodal_order),
     130         804 :   _mesh_subdomains(other_mesh._mesh_subdomains),
     131         804 :   _elemset_codes_inverse_map(other_mesh._elemset_codes_inverse_map),
     132         804 :   _all_elemset_ids(other_mesh._all_elemset_ids),
     133       24392 :   _spatial_dimension(other_mesh._spatial_dimension),
     134       24392 :   _default_ghosting(std::make_unique<GhostPointNeighbors>(*this)),
     135       99932 :   _point_locator_close_to_point_tol(other_mesh._point_locator_close_to_point_tol)
     136             : {
     137         804 :   const GhostingFunctor * const other_default_ghosting = other_mesh._default_ghosting.get();
     138             : 
     139       79468 :   for (GhostingFunctor * const gf : other_mesh._ghosting_functors)
     140             :     {
     141             :       // If the other mesh is using default ghosting, then we will use our own
     142             :       // default ghosting
     143       55076 :       if (gf == other_default_ghosting)
     144             :         {
     145       24392 :           _ghosting_functors.push_back(_default_ghosting.get());
     146       24392 :           continue;
     147             :         }
     148             : 
     149       60352 :       std::shared_ptr<GhostingFunctor> clone_gf = gf->clone();
     150             :       // Some subclasses of GhostingFunctor might not override the
     151             :       // clone function yet. If this is the case, GhostingFunctor will
     152             :       // return nullptr by default. The clone function should be overridden
     153             :       // in all derived classes. This following code ("else") is written
     154             :       // for API upgrade. That will allow users gradually to update their code.
     155             :       // Once the API upgrade is done, we will come back and delete "else."
     156       30684 :       if (clone_gf)
     157             :         {
     158       30684 :           clone_gf->set_mesh(this);
     159       60352 :           add_ghosting_functor(clone_gf);
     160             :         }
     161             :       else
     162             :         {
     163             :           libmesh_deprecated();
     164           0 :           add_ghosting_functor(*gf);
     165             :         }
     166             :     }
     167             : 
     168       24392 :   if (other_mesh._partitioner.get())
     169       47980 :     _partitioner = other_mesh._partitioner->clone();
     170             : 
     171             : #ifdef LIBMESH_ENABLE_PERIODIC
     172             :   // Deep copy of all periodic boundaries
     173       24392 :   if (other_mesh._disjoint_neighbor_boundary_pairs)
     174             :     {
     175         136 :       _disjoint_neighbor_boundary_pairs = std::make_unique<PeriodicBoundaries>();
     176             : 
     177         213 :       for (const auto & [id, pb] : *other_mesh._disjoint_neighbor_boundary_pairs)
     178         142 :         if (pb)
     179         280 :           (*_disjoint_neighbor_boundary_pairs)[id] = pb->clone();
     180             :     }
     181             : #endif
     182             : 
     183             :   // _elemset_codes stores pointers to entries in _elemset_codes_inverse_map,
     184             :   // so it is not possible to simply copy it directly from other_mesh
     185       24392 :   for (const auto & [set, code] : _elemset_codes_inverse_map)
     186           0 :     _elemset_codes.emplace(code, &set);
     187       24392 : }
     188             : 
     189         426 : MeshBase& MeshBase::operator= (MeshBase && other_mesh)
     190             : {
     191          12 :   LOG_SCOPE("operator=(&&)", "MeshBase");
     192             : 
     193             :   // Move assign as a ParallelObject.
     194          12 :   this->ParallelObject::operator=(other_mesh);
     195             : 
     196         426 :   _n_parts = other_mesh.n_partitions();
     197         426 :   _default_mapping_type = other_mesh.default_mapping_type();
     198         426 :   _default_mapping_data = other_mesh.default_mapping_data();
     199         426 :   _preparation = other_mesh._preparation;
     200          12 :   _element_stored_range = std::move(other_mesh._element_stored_range);
     201          12 :   _const_active_local_element_stored_range = std::move(other_mesh._const_active_local_element_stored_range);
     202          12 :   _point_locator = std::move(other_mesh._point_locator);
     203         426 :   _count_lower_dim_elems_in_point_locator = other_mesh.get_count_lower_dim_elems_in_point_locator();
     204             : #ifdef LIBMESH_ENABLE_UNIQUE_ID
     205         426 :   _next_unique_id = other_mesh.next_unique_id();
     206             : #endif
     207             :   // If the other mesh interior_parent pointers just go back to
     208             :   // itself, so should we
     209         426 :   _interior_mesh = (other_mesh._interior_mesh == &other_mesh) ?
     210             :                    this : other_mesh._interior_mesh;
     211         426 :   _skip_noncritical_partitioning = other_mesh.skip_noncritical_partitioning();
     212         426 :   _skip_all_partitioning = other_mesh.skip_partitioning();
     213         426 :   _skip_renumber_nodes_and_elements = !(other_mesh.allow_renumbering());
     214         426 :   _skip_find_neighbors = !(other_mesh.allow_find_neighbors());
     215         426 :   _skip_detect_interior_parents = !(other_mesh.allow_detect_interior_parents());
     216         426 :   _allow_remote_element_removal = other_mesh.allow_remote_element_removal();
     217         426 :   _allow_node_and_elem_unique_id_overlap = other_mesh.allow_node_and_elem_unique_id_overlap();
     218          12 :   _block_id_to_name = std::move(other_mesh._block_id_to_name);
     219          12 :   _elem_dims = std::move(other_mesh.elem_dimensions());
     220          12 :   _elem_default_orders = std::move(other_mesh.elem_default_orders());
     221         426 :   _supported_nodal_order = other_mesh.supported_nodal_order();
     222          12 :   _mesh_subdomains = other_mesh._mesh_subdomains;
     223          12 :   _elemset_codes = std::move(other_mesh._elemset_codes);
     224          12 :   _elemset_codes_inverse_map = std::move(other_mesh._elemset_codes_inverse_map);
     225          12 :   _all_elemset_ids = std::move(other_mesh._all_elemset_ids);
     226         426 :   _spatial_dimension = other_mesh.spatial_dimension();
     227         426 :   _elem_integer_names = std::move(other_mesh._elem_integer_names);
     228         426 :   _elem_integer_default_values = std::move(other_mesh._elem_integer_default_values);
     229         426 :   _node_integer_names = std::move(other_mesh._node_integer_names);
     230         426 :   _node_integer_default_values = std::move(other_mesh._node_integer_default_values);
     231         426 :   _point_locator_close_to_point_tol = other_mesh.get_point_locator_close_to_point_tol();
     232             : 
     233             : #ifdef LIBMESH_ENABLE_PERIODIC
     234             :   // Deep copy of all periodic boundaries:
     235             :   // We must clone each PeriodicBoundaryBase in the source map,
     236             :   // since unique_ptr cannot be copied and we need independent instances
     237         426 :   if (other_mesh._disjoint_neighbor_boundary_pairs)
     238             :     {
     239           0 :       _disjoint_neighbor_boundary_pairs = std::make_unique<PeriodicBoundaries>();
     240             : 
     241           0 :       for (const auto & [id, pb] : *other_mesh._disjoint_neighbor_boundary_pairs)
     242           0 :         if (pb)
     243           0 :           (*_disjoint_neighbor_boundary_pairs)[id] = pb->clone();
     244             :     }
     245             : #endif
     246             : 
     247             :   // This relies on our subclasses *not* invalidating pointers when we
     248             :   // do their portion of the move assignment later!
     249          12 :   boundary_info = std::move(other_mesh.boundary_info);
     250          12 :   boundary_info->set_mesh(*this);
     251             : 
     252             : #ifdef DEBUG
     253             :   // Make sure that move assignment worked for pointers
     254          12 :   for (const auto & [set, code] : _elemset_codes_inverse_map)
     255             :     {
     256           0 :       auto it = _elemset_codes.find(code);
     257           0 :       libmesh_assert_msg(it != _elemset_codes.end(),
     258             :                          "Elemset code " << code << " not found in _elmset_codes container.");
     259           0 :       libmesh_assert_equal_to(it->second, &set);
     260             :     }
     261             : #endif
     262             : 
     263             :   // We're *not* really done at this point, but we have the problem
     264             :   // that some of our data movement might be expecting subclasses data
     265             :   // movement to happen first.  We'll let subclasses handle that by
     266             :   // calling our post_dofobject_moves()
     267         438 :   return *this;
     268             : }
     269             : 
     270             : 
     271       15151 : bool MeshBase::operator== (const MeshBase & other_mesh) const
     272             : {
     273        1062 :   LOG_SCOPE("operator==()", "MeshBase");
     274             : 
     275       15151 :   bool is_equal = this->locally_equals(other_mesh);
     276       15151 :   this->comm().min(is_equal);
     277       16213 :   return is_equal;
     278             : }
     279             : 
     280             : 
     281       15151 : bool MeshBase::locally_equals (const MeshBase & other_mesh) const
     282             : {
     283             :   // Check whether (almost) everything in the base is equal
     284             :   //
     285             :   // We don't check _next_unique_id here, because it's expected to
     286             :   // change in a DistributedMesh prepare_for_use(); it's conceptually
     287             :   // "mutable".
     288             :   //
     289             :   // We use separate if statements instead of logical operators here,
     290             :   // to make it easy to see the failing condition when using a
     291             :   // debugger to figure out why a MeshTools::valid_is_prepared(mesh)
     292             :   // is failing.
     293       15151 :   if (_n_parts != other_mesh._n_parts)
     294           0 :     return false;
     295       15151 :   if (_default_mapping_type != other_mesh._default_mapping_type)
     296           0 :     return false;
     297       15151 :   if (_default_mapping_data != other_mesh._default_mapping_data)
     298           0 :     return false;
     299       15151 :   if (_preparation != other_mesh._preparation)
     300           0 :     return false;
     301       16621 :   if (_count_lower_dim_elems_in_point_locator !=
     302       15151 :         other_mesh._count_lower_dim_elems_in_point_locator)
     303           0 :     return false;
     304             : 
     305             :   // We should either both have our own interior parents or both not;
     306             :   // but if we both don't then we can't really assert anything else
     307             :   // because pointing at the same interior mesh is fair but so is
     308             :   // pointing at two different copies of "the same" interior mesh.
     309       16621 :   if ((_interior_mesh == this) !=
     310       15151 :       (other_mesh._interior_mesh == &other_mesh))
     311           0 :     return false;
     312             : 
     313       15151 :   if (_skip_noncritical_partitioning != other_mesh._skip_noncritical_partitioning)
     314           0 :     return false;
     315       15151 :   if (_skip_all_partitioning != other_mesh._skip_all_partitioning)
     316           0 :     return false;
     317       15151 :   if (_skip_renumber_nodes_and_elements != other_mesh._skip_renumber_nodes_and_elements)
     318           0 :     return false;
     319       15151 :   if (_skip_find_neighbors != other_mesh._skip_find_neighbors)
     320           0 :     return false;
     321       15151 :   if (_skip_detect_interior_parents != other_mesh._skip_detect_interior_parents)
     322           0 :     return false;
     323       15151 :   if (_allow_remote_element_removal != other_mesh._allow_remote_element_removal)
     324           0 :     return false;
     325       15151 :   if (_allow_node_and_elem_unique_id_overlap != other_mesh._allow_node_and_elem_unique_id_overlap)
     326           0 :     return false;
     327       15151 :   if (_spatial_dimension != other_mesh._spatial_dimension)
     328           0 :     return false;
     329       15151 :   if (_point_locator_close_to_point_tol != other_mesh._point_locator_close_to_point_tol)
     330           0 :     return false;
     331       15151 :   if (_block_id_to_name != other_mesh._block_id_to_name)
     332           0 :     return false;
     333       15151 :   if (_elem_dims != other_mesh._elem_dims)
     334           0 :     return false;
     335       15151 :   if (_elem_default_orders != other_mesh._elem_default_orders)
     336           0 :     return false;
     337       15151 :   if (_supported_nodal_order != other_mesh._supported_nodal_order)
     338           0 :     return false;
     339       15151 :   if (_mesh_subdomains != other_mesh._mesh_subdomains)
     340           6 :     return false;
     341       14938 :   if (_all_elemset_ids != other_mesh._all_elemset_ids)
     342           0 :     return false;
     343       14938 :   if (_elem_integer_names != other_mesh._elem_integer_names)
     344           0 :     return false;
     345       15340 :   if (_elem_integer_default_values != other_mesh._elem_integer_default_values)
     346           0 :     return false;
     347       14938 :   if (_node_integer_names != other_mesh._node_integer_names)
     348           0 :     return false;
     349       15340 :   if (_node_integer_default_values != other_mesh._node_integer_default_values)
     350           0 :     return false;
     351       14938 :   if (static_cast<bool>(_default_ghosting) != static_cast<bool>(other_mesh._default_ghosting))
     352           0 :     return false;
     353       14938 :   if (static_cast<bool>(_partitioner) != static_cast<bool>(other_mesh._partitioner))
     354           0 :     return false;
     355       14938 :   if (*boundary_info != *other_mesh.boundary_info)
     356           6 :     return false;
     357             : 
     358             :   // First check whether the "existence" of the two pointers differs (one present, one absent)
     359       15775 :   if (static_cast<bool>(_disjoint_neighbor_boundary_pairs) !=
     360        1050 :       static_cast<bool>(other_mesh._disjoint_neighbor_boundary_pairs))
     361           0 :     return false;
     362             :   // If both exist, compare the contents (Weak Test: just compare sizes like `_ghosting_functors`)
     363       14729 :   if (_disjoint_neighbor_boundary_pairs &&
     364        1054 :       (_disjoint_neighbor_boundary_pairs->size() != other_mesh._disjoint_neighbor_boundary_pairs->size()))
     365           0 :     return false;
     366             : 
     367             :   const constraint_rows_type & other_rows =
     368        1050 :     other_mesh.get_constraint_rows();
     369       66361 :   for (const auto & [node, row] : this->_constraint_rows)
     370             :     {
     371       51636 :       const dof_id_type node_id = node->id();
     372       51636 :       const Node * other_node = other_mesh.query_node_ptr(node_id);
     373       51636 :       if (!other_node)
     374           0 :         return false;
     375             : 
     376        6416 :       auto it = other_rows.find(other_node);
     377       51636 :       if (it == other_rows.end())
     378           0 :         return false;
     379             : 
     380        6416 :       const auto & other_row = it->second;
     381       58052 :       if (row.size() != other_row.size())
     382           0 :         return false;
     383             : 
     384      231893 :       for (auto i : index_range(row))
     385             :         {
     386       18096 :           const auto & [elem_pair, coef] = row[i];
     387       18096 :           const auto & [other_elem_pair, other_coef] = other_row[i];
     388       18096 :           libmesh_assert(elem_pair.first);
     389       18096 :           libmesh_assert(other_elem_pair.first);
     390      180257 :           if (elem_pair.first->id() !=
     391      342418 :               other_elem_pair.first->id() ||
     392      180257 :               elem_pair.second !=
     393      198353 :               other_elem_pair.second ||
     394      180257 :               coef != other_coef)
     395           0 :             return false;
     396             :         }
     397             :     }
     398             : 
     399       14725 :   for (const auto & [elemset_code, elemset_ptr] : this->_elemset_codes)
     400           0 :     if (const auto it = other_mesh._elemset_codes.find(elemset_code);
     401           0 :         it == other_mesh._elemset_codes.end() || *elemset_ptr != *it->second)
     402           0 :       return false;
     403             : 
     404             :   // FIXME: we have no good way to compare ghosting functors, since
     405             :   // they're in a vector of pointers, and we have no way *at all*
     406             :   // to compare ghosting functors, since they don't have operator==
     407             :   // defined and we encourage users to subclass them.  We can check if
     408             :   // we have the same number, is all.
     409       16171 :   if (_ghosting_functors.size() !=
     410        1446 :       other_mesh._ghosting_functors.size())
     411           0 :     return false;
     412             : 
     413             :   // Same deal for partitioners.  We tested that we both have one or
     414             :   // both don't, but are they equivalent?  Let's guess "yes".
     415             : 
     416             :   // Now let the subclasses decide whether everything else is equal
     417       14725 :   return this->subclass_locally_equals(other_mesh);
     418             : }
     419             : 
     420             : 
     421      366501 : MeshBase::~MeshBase()
     422             : {
     423      356083 :   this->MeshBase::clear();
     424             : 
     425       10434 :   libmesh_exceptionless_assert (!libMesh::closed());
     426     1361776 : }
     427             : 
     428             : 
     429             : 
     430    11925313 : unsigned int MeshBase::mesh_dimension() const
     431             : {
     432    11925313 :   if (!_elem_dims.empty())
     433    11925313 :     return cast_int<unsigned int>(*_elem_dims.rbegin());
     434           0 :   return 0;
     435             : }
     436             : 
     437             : 
     438             : 
     439           0 : void MeshBase::set_elem_dimensions(std::set<unsigned char> elem_dims)
     440             : {
     441             : #ifdef DEBUG
     442             :   // In debug mode, we call cache_elem_data() and then make sure
     443             :   // the result actually agrees with what the user specified.
     444           0 :   parallel_object_only();
     445             : 
     446           0 :   this->cache_elem_data();
     447           0 :   libmesh_assert_msg(_elem_dims == elem_dims, \
     448             :                      "Specified element dimensions does not match true element dimensions!");
     449             : #endif
     450             : 
     451           0 :   _elem_dims = std::move(elem_dims);
     452           0 : }
     453             : 
     454             : 
     455             : 
     456        1775 : void MeshBase::add_elemset_code(dof_id_type code, MeshBase::elemset_type id_set)
     457             : {
     458             :   // Populate inverse map, stealing id_set's resources
     459        1725 :   auto [it1, inserted1] = _elemset_codes_inverse_map.emplace(std::move(id_set), code);
     460             : 
     461             :   // Reference to the newly inserted (or previously existing) id_set
     462        1775 :   const auto & inserted_id_set = it1->first;
     463             : 
     464             :   // Keep track of all elemset ids ever added for O(1) n_elemsets()
     465             :   // performance. Only need to do this if we didn't know about this
     466             :   // id_set before...
     467        1775 :   if (inserted1)
     468        1775 :     _all_elemset_ids.insert(inserted_id_set.begin(), inserted_id_set.end());
     469             : 
     470             :   // Take the address of the newly emplaced set to use in
     471             :   // _elemset_codes, avoid duplicating std::set storage
     472        1775 :   auto [it2, inserted2] = _elemset_codes.emplace(code, &inserted_id_set);
     473             : 
     474             :   // Throw an error if this code already exists with a pointer to a
     475             :   // different set of ids.
     476        1775 :   libmesh_error_msg_if(!inserted2 && it2->second != &inserted_id_set,
     477             :                        "The elemset code " << code << " already exists with a different id_set.");
     478        1775 : }
     479             : 
     480             : 
     481             : 
     482       15010 : unsigned int MeshBase::n_elemsets() const
     483             : {
     484       15010 :   return _all_elemset_ids.size();
     485             : }
     486             : 
     487        2963 : void MeshBase::get_elemsets(dof_id_type elemset_code, MeshBase::elemset_type & id_set_to_fill) const
     488             : {
     489             :   // If we don't recognize this elemset_code, hand back an empty set
     490         102 :   id_set_to_fill.clear();
     491             : 
     492        2963 :   if (const auto it = _elemset_codes.find(elemset_code);
     493         102 :       it != _elemset_codes.end())
     494        1386 :     id_set_to_fill.insert(it->second->begin(), it->second->end());
     495        2963 : }
     496             : 
     497         852 : dof_id_type MeshBase::get_elemset_code(const MeshBase::elemset_type & id_set) const
     498             : {
     499          24 :   auto it = _elemset_codes_inverse_map.find(id_set);
     500         852 :   return (it == _elemset_codes_inverse_map.end()) ? DofObject::invalid_id : it->second;
     501             : }
     502             : 
     503        8453 : std::vector<dof_id_type> MeshBase::get_elemset_codes() const
     504             : {
     505         240 :   std::vector<dof_id_type> ret;
     506        8453 :   ret.reserve(_elemset_codes.size());
     507        9234 :   for (const auto & pr : _elemset_codes)
     508         781 :     ret.push_back(pr.first);
     509        8453 :   return ret;
     510             : }
     511             : 
     512         284 : void MeshBase::change_elemset_code(dof_id_type old_code, dof_id_type new_code)
     513             : {
     514             :   // Look up elemset ids for old_code
     515           8 :   auto it = _elemset_codes.find(old_code);
     516             : 
     517             :   // If we don't have the old_code, then do nothing. Alternatively, we
     518             :   // could throw an error since trying to change an elemset code you
     519             :   // don't have could indicate there's a problem...
     520         284 :   if (it == _elemset_codes.end())
     521           0 :     return;
     522             : 
     523             :   // Make copy of the set of elemset ids. We are not changing these,
     524             :   // only updating the elemset code it corresponds to.
     525         284 :   elemset_type id_set_copy = *(it->second);
     526             : 
     527             :   // Look up the corresponding entry in the inverse map. Note: we want
     528             :   // the iterator because we are going to remove it.
     529           8 :   auto inverse_it = _elemset_codes_inverse_map.find(id_set_copy);
     530         284 :   libmesh_error_msg_if(inverse_it == _elemset_codes_inverse_map.end(),
     531             :                        "Expected _elemset_codes_inverse_map entry for elemset code " << old_code);
     532             : 
     533             :   // Erase entry from inverse map
     534         276 :   _elemset_codes_inverse_map.erase(inverse_it);
     535             : 
     536             :   // Erase entry from forward map
     537         276 :   _elemset_codes.erase(it);
     538             : 
     539             :   // Add new code with original set of ids.
     540         560 :   this->add_elemset_code(new_code, id_set_copy);
     541             : 
     542             :   // We can't update any actual elemset codes if there is no extra integer defined for it.
     543         284 :   if (!this->has_elem_integer("elemset_code"))
     544           0 :     return;
     545             : 
     546             :   // Get index of elemset_code extra integer
     547         284 :   unsigned int elemset_index = this->get_elem_integer_index("elemset_code");
     548             : 
     549             :   // Loop over all elems and update code
     550             :   Threads::parallel_for
     551         284 :     (this->element_stored_range(),
     552        1240 :      [elemset_index, old_code, new_code](const ElemRange & range)
     553             :      {
     554        5254 :        for (Elem * elem : range)
     555             :          {
     556             :            dof_id_type elemset_code =
     557        4970 :              elem->get_extra_integer(elemset_index);
     558             : 
     559        4970 :            if (elemset_code == old_code)
     560        2415 :              elem->set_extra_integer(elemset_index, new_code);
     561             :          }
     562         284 :      });
     563             : }
     564             : 
     565         284 : void MeshBase::change_elemset_id(elemset_id_type old_id, elemset_id_type new_id)
     566             : {
     567             :   // Early return if we don't have old_id
     568           8 :   if (!_all_elemset_ids.count(old_id))
     569           0 :     return;
     570             : 
     571             :   // Throw an error if the new_id is already used
     572           8 :   libmesh_error_msg_if(_all_elemset_ids.count(new_id),
     573             :                        "Cannot change elemset id " << old_id <<
     574             :                        " to " << new_id << ", " << new_id << " already exists.");
     575             : 
     576             :   // We will build up a new version of the inverse map so we can iterate over
     577             :   // the current one without invalidating anything.
     578          16 :   std::map<MeshBase::elemset_type, dof_id_type> new_elemset_codes_inverse_map;
     579         852 :   for (const auto & [id_set, elemset_code] : _elemset_codes_inverse_map)
     580             :     {
     581          32 :       auto id_set_copy = id_set;
     582          16 :       if (id_set_copy.count(old_id))
     583             :         {
     584             :           // Remove old_id, insert new_id
     585           8 :           id_set_copy.erase(old_id);
     586         276 :           id_set_copy.insert(new_id);
     587             :         }
     588             : 
     589             :       // Store in new version of map
     590         552 :       new_elemset_codes_inverse_map.emplace(id_set_copy, elemset_code);
     591             :     }
     592             : 
     593             :   // Swap existing map with newly-built one
     594           8 :   _elemset_codes_inverse_map.swap(new_elemset_codes_inverse_map);
     595             : 
     596             :   // Reconstruct _elemset_codes map
     597           8 :   _elemset_codes.clear();
     598         852 :   for (const auto & [id_set, elemset_code] : _elemset_codes_inverse_map)
     599         568 :     _elemset_codes.emplace(elemset_code, &id_set);
     600             : 
     601             :   // Update _all_elemset_ids
     602           8 :   _all_elemset_ids.erase(old_id);
     603         276 :   _all_elemset_ids.insert(new_id);
     604             : }
     605             : 
     606      262591 : unsigned int MeshBase::spatial_dimension () const
     607             : {
     608      271295 :   return cast_int<unsigned int>(_spatial_dimension);
     609             : }
     610             : 
     611             : 
     612             : 
     613      369601 : void MeshBase::set_spatial_dimension(unsigned char d)
     614             : {
     615             :   // The user can set the _spatial_dimension however they wish,
     616             :   // libMesh will only *increase* the spatial dimension, however,
     617             :   // never decrease it.
     618      369601 :   _spatial_dimension = d;
     619      369601 : }
     620             : 
     621             : 
     622             : 
     623        4290 : unsigned int MeshBase::add_elem_integer(std::string name,
     624             :                                         bool allocate_data,
     625             :                                         dof_id_type default_value)
     626             : {
     627        5486 :   for (auto i : index_range(_elem_integer_names))
     628        1416 :     if (_elem_integer_names[i] == name)
     629             :       {
     630           8 :         libmesh_assert_less(i, _elem_integer_default_values.size());
     631         284 :         _elem_integer_default_values[i] = default_value;
     632         284 :         return i;
     633             :       }
     634             : 
     635         124 :   libmesh_assert_equal_to(_elem_integer_names.size(),
     636             :                           _elem_integer_default_values.size());
     637        4006 :   _elem_integer_names.push_back(std::move(name));
     638        4006 :   _elem_integer_default_values.push_back(default_value);
     639        4006 :   if (allocate_data)
     640        3418 :     this->size_elem_extra_integers();
     641        4130 :   return _elem_integer_names.size()-1;
     642             : }
     643             : 
     644             : 
     645             : 
     646       38197 : std::vector<unsigned int> MeshBase::add_elem_integers(const std::vector<std::string> & names,
     647             :                                                       bool allocate_data,
     648             :                                                       const std::vector<dof_id_type> * default_values)
     649             : {
     650        1208 :   libmesh_assert(!default_values || default_values->size() == names.size());
     651        1208 :   libmesh_assert_equal_to(_elem_integer_names.size(), _elem_integer_default_values.size());
     652             : 
     653        2416 :   std::unordered_map<std::string, std::size_t> name_indices;
     654       39049 :   for (auto i : index_range(_elem_integer_names))
     655         852 :     name_indices[_elem_integer_names[i]] = i;
     656             : 
     657       39389 :   std::vector<unsigned int> returnval(names.size());
     658             : 
     659        1208 :   bool added_an_integer = false;
     660       40702 :   for (auto i : index_range(names))
     661             :     {
     662         156 :       const std::string & name = names[i];
     663        2505 :       if (const auto it = name_indices.find(name);
     664          78 :           it != name_indices.end())
     665             :         {
     666         426 :           returnval[i] = it->second;
     667         438 :           _elem_integer_default_values[it->second] =
     668         426 :             default_values ? (*default_values)[i] : DofObject::invalid_id;
     669             :         }
     670             :       else
     671             :         {
     672        2145 :           returnval[i] = _elem_integer_names.size();
     673        2079 :           name_indices[name] = returnval[i];
     674        2079 :           _elem_integer_names.push_back(name);
     675             :           _elem_integer_default_values.push_back
     676        2883 :             (default_values ? (*default_values)[i] : DofObject::invalid_id);
     677          66 :           added_an_integer = true;
     678             :         }
     679             :     }
     680             : 
     681       38197 :   if (allocate_data && added_an_integer)
     682        1065 :     this->size_elem_extra_integers();
     683             : 
     684       39405 :   return returnval;
     685             : }
     686             : 
     687             : 
     688             : 
     689        3278 : unsigned int MeshBase::get_elem_integer_index(std::string_view name) const
     690             : {
     691        4130 :   for (auto i : index_range(_elem_integer_names))
     692        4037 :     if (_elem_integer_names[i] == name)
     693        3278 :       return i;
     694             : 
     695           0 :   libmesh_error_msg("Unknown elem integer " << name);
     696             :   return libMesh::invalid_uint;
     697             : }
     698             : 
     699             : 
     700             : 
     701        8539 : bool MeshBase::has_elem_integer(std::string_view name) const
     702             : {
     703        9533 :   for (auto & entry : _elem_integer_names)
     704        2367 :     if (entry == name)
     705          40 :       return true;
     706             : 
     707         632 :   return false;
     708             : }
     709             : 
     710             : 
     711             : 
     712       16460 : unsigned int MeshBase::add_node_integer(std::string name,
     713             :                                         bool allocate_data,
     714             :                                         dof_id_type default_value)
     715             : {
     716       26843 :   for (auto i : index_range(_node_integer_names))
     717       10190 :     if (_node_integer_names[i] == name)
     718             :       {
     719          16 :         libmesh_assert_less(i, _node_integer_default_values.size());
     720         568 :         _node_integer_default_values[i] = default_value;
     721         568 :         return i;
     722             :       }
     723             : 
     724         678 :   libmesh_assert_equal_to(_node_integer_names.size(),
     725             :                           _node_integer_default_values.size());
     726       15892 :   _node_integer_names.push_back(std::move(name));
     727       15892 :   _node_integer_default_values.push_back(default_value);
     728       15892 :   if (allocate_data)
     729        7120 :     this->size_node_extra_integers();
     730       16570 :   return _node_integer_names.size()-1;
     731             : }
     732             : 
     733             : 
     734             : 
     735       38055 : std::vector<unsigned int> MeshBase::add_node_integers(const std::vector<std::string> & names,
     736             :                                                       bool allocate_data,
     737             :                                                       const std::vector<dof_id_type> * default_values)
     738             : {
     739        1204 :   libmesh_assert(!default_values || default_values->size() == names.size());
     740        1204 :   libmesh_assert_equal_to(_node_integer_names.size(), _node_integer_default_values.size());
     741             : 
     742        2408 :   std::unordered_map<std::string, std::size_t> name_indices;
     743       38623 :   for (auto i : index_range(_node_integer_names))
     744         568 :     name_indices[_node_integer_names[i]] = i;
     745             : 
     746       39243 :   std::vector<unsigned int> returnval(names.size());
     747             : 
     748        1204 :   bool added_an_integer = false;
     749       40687 :   for (auto i : index_range(names))
     750             :     {
     751         192 :       const std::string & name = names[i];
     752        2632 :       if (const auto it = name_indices.find(name);
     753          96 :           it != name_indices.end())
     754             :         {
     755           0 :           returnval[i] = it->second;
     756           0 :           _node_integer_default_values[it->second] =
     757           0 :             default_values ? (*default_values)[i] : DofObject::invalid_id;
     758             :         }
     759             :       else
     760             :         {
     761        2728 :           returnval[i] = _node_integer_names.size();
     762        2632 :           name_indices[name] = returnval[i];
     763        2632 :           _node_integer_names.push_back(name);
     764             :           _node_integer_default_values.push_back
     765        3724 :             (default_values ? (*default_values)[i] : DofObject::invalid_id);
     766          96 :           added_an_integer = true;
     767             :         }
     768             :     }
     769             : 
     770       38055 :   if (allocate_data && added_an_integer)
     771        1010 :     this->size_node_extra_integers();
     772             : 
     773       39259 :   return returnval;
     774             : }
     775             : 
     776             : 
     777             : 
     778         710 : unsigned int MeshBase::get_node_integer_index(std::string_view name) const
     779             : {
     780        1988 :   for (auto i : index_range(_node_integer_names))
     781        1968 :     if (_node_integer_names[i] == name)
     782         710 :       return i;
     783             : 
     784           0 :   libmesh_error_msg("Unknown node integer " << name);
     785             :   return libMesh::invalid_uint;
     786             : }
     787             : 
     788             : 
     789             : 
     790        1136 : bool MeshBase::has_node_integer(std::string_view name) const
     791             : {
     792        3134 :   for (auto & entry : _node_integer_names)
     793        3134 :     if (entry == name)
     794          32 :       return true;
     795             : 
     796           0 :   return false;
     797             : }
     798             : 
     799             : 
     800             : 
     801       42305 : void MeshBase::remove_orphaned_nodes ()
     802             : {
     803        2584 :   LOG_SCOPE("remove_orphaned_nodes()", "MeshBase");
     804             : 
     805             :   // Will hold the set of nodes that are currently connected to elements
     806        1292 :   std::unordered_set<Node *> connected_nodes;
     807             : 
     808             :   // Loop over the elements.  Find which nodes are connected to at
     809             :   // least one of them.
     810     3889342 :   for (const auto & element : this->element_ptr_range())
     811    19081100 :     for (auto & n : element->node_ref_range())
     812    16944527 :       connected_nodes.insert(&n);
     813             : 
     814    11204814 :   for (const auto & node : this->node_ptr_range())
     815      723942 :     if (!connected_nodes.count(node))
     816      122761 :       this->delete_node(node);
     817             : 
     818       42305 :   _preparation.has_removed_orphaned_nodes = true;
     819       42305 : }
     820             : 
     821             : 
     822             : 
     823             : #ifdef LIBMESH_ENABLE_DEPRECATED
     824           0 : void MeshBase::prepare_for_use (const bool skip_renumber_nodes_and_elements, const bool skip_find_neighbors)
     825             : {
     826             :   libmesh_deprecated();
     827             : 
     828             :   // We only respect the users wish if they tell us to skip renumbering. If they tell us not to
     829             :   // skip renumbering but someone previously called allow_renumbering(false), then the latter takes
     830             :   // precedence
     831           0 :   if (skip_renumber_nodes_and_elements)
     832           0 :     this->allow_renumbering(false);
     833             : 
     834             :   // We always accept the user's value for skip_find_neighbors, in contrast to skip_renumber
     835           0 :   const bool old_allow_find_neighbors = this->allow_find_neighbors();
     836           0 :   this->allow_find_neighbors(!skip_find_neighbors);
     837             : 
     838           0 :   this->prepare_for_use();
     839             : 
     840           0 :   this->allow_find_neighbors(old_allow_find_neighbors);
     841           0 : }
     842             : 
     843           0 : void MeshBase::prepare_for_use (const bool skip_renumber_nodes_and_elements)
     844             : {
     845             :   libmesh_deprecated();
     846             : 
     847             :   // We only respect the users wish if they tell us to skip renumbering. If they tell us not to
     848             :   // skip renumbering but someone previously called allow_renumbering(false), then the latter takes
     849             :   // precedence
     850           0 :   if (skip_renumber_nodes_and_elements)
     851           0 :     this->allow_renumbering(false);
     852             : 
     853           0 :   this->prepare_for_use();
     854           0 : }
     855             : #endif // LIBMESH_ENABLE_DEPRECATED
     856             : 
     857             : 
     858             : 
     859             : 
     860      432118 : void MeshBase::prepare_for_use ()
     861             : {
     862             :   // Mark everything as unprepared, except for those things we've been
     863             :   // told we don't need to prepare, for backwards compatibility
     864      432118 :   this->clear_point_locator();
     865      432118 :   this->clear_stored_ranges();
     866      432118 :   _preparation = false;
     867      432118 :   _preparation.has_neighbor_ptrs = _skip_find_neighbors;
     868      432118 :   _preparation.has_removed_remote_elements = !_allow_remote_element_removal;
     869             : 
     870      432118 :   this->complete_preparation();
     871      432118 : }
     872             : 
     873             : 
     874      435171 : void MeshBase::complete_preparation()
     875             : {
     876       26076 :   LOG_SCOPE("complete_preparation()", "MeshBase");
     877             : 
     878       13038 :   parallel_object_only();
     879             : 
     880       13038 :   libmesh_assert(this->comm().verify(this->is_serial()));
     881             : 
     882             :   // If we don't go into this method with valid constraint rows, we're
     883             :   // only going to be able to make that worse.
     884             : #ifdef DEBUG
     885       13038 :   MeshTools::libmesh_assert_valid_constraint_rows(*this);
     886             : #endif
     887             : 
     888             :   // A distributed mesh may have processors with no elements (or
     889             :   // processors with no elements of higher dimension, if we ever
     890             :   // support mixed-dimension meshes), but we want consistent
     891             :   // mesh_dimension anyways.
     892             :   //
     893             :   // cache_elem_data() should get the elem_dimensions() and
     894             :   // mesh_dimension() correct later, and we don't need it earlier.
     895             : 
     896             : 
     897             :   // Renumber the nodes and elements so that they in contiguous
     898             :   // blocks.  By default, _skip_renumber_nodes_and_elements is false.
     899             :   //
     900             :   // Instances where you if prepare_for_use() should not renumber the nodes
     901             :   // and elements include reading in e.g. an xda/r or gmv file. In
     902             :   // this case, the ordering of the nodes may depend on an accompanying
     903             :   // solution, and the node ordering cannot be changed.
     904             : 
     905             : 
     906             :   // Mesh modification operations might not leave us with consistent
     907             :   // id counts, or might leave us with orphaned nodes we're no longer
     908             :   // using, but our partitioner might need that consistency and/or
     909             :   // might be confused by orphaned nodes.
     910      435171 :   if (!_skip_renumber_nodes_and_elements)
     911             :     {
     912      390452 :       if (!_preparation.has_removed_orphaned_nodes ||
     913         426 :           !_preparation.has_synched_id_counts)
     914      390026 :         this->renumber_nodes_and_elements();
     915             :     }
     916             :   else
     917             :     {
     918       44719 :       if (!_preparation.has_removed_orphaned_nodes)
     919       42305 :         this->remove_orphaned_nodes();
     920       44719 :       if (!_preparation.has_synched_id_counts)
     921       42305 :         this->update_parallel_id_counts();
     922             :     }
     923             : 
     924             :   // Let all the elements find their neighbors
     925      435171 :   if (!_skip_find_neighbors && !_preparation.has_neighbor_ptrs)
     926      421008 :     this->find_neighbors();
     927             : 
     928             :   // The user may have set boundary conditions.  We require that the
     929             :   // boundary conditions were set consistently.  Because we examine
     930             :   // neighbors when evaluating non-raw boundary condition IDs, this
     931             :   // assert is only valid when our neighbor links are in place.
     932             : #ifdef DEBUG
     933       13038 :   MeshTools::libmesh_assert_valid_boundary_ids(*this);
     934             : #endif
     935             : 
     936             :   // Search the mesh for all the dimensions of the elements
     937             :   // and cache them.
     938      435171 :   if (!_preparation.has_cached_elem_data)
     939      432331 :     this->cache_elem_data();
     940             : 
     941             :   // Search the mesh for elements that have a neighboring element
     942             :   // of dim+1 and set that element as the interior parent
     943      435171 :   if (!_preparation.has_interior_parent_ptrs)
     944             :     {
     945      432118 :       if (!_skip_detect_interior_parents)
     946      432118 :         this->detect_interior_parents();
     947             :       else
     948             :         {
     949             :           // We must set the flag that says "interior parent pointers have been set up"
     950             :           // even though we skip detect_interior_parents().
     951           0 :           _preparation.has_interior_parent_ptrs = true;
     952             :         }
     953             :     }
     954             : 
     955             :   // Fix up node unique ids in case mesh generation code didn't take
     956             :   // exceptional care to do so.
     957             :   //  MeshCommunication().make_node_unique_ids_parallel_consistent(*this);
     958             : 
     959             :   // We're going to still require that mesh generation code gets
     960             :   // element unique ids consistent.
     961             : #if defined(DEBUG) && defined(LIBMESH_ENABLE_UNIQUE_ID)
     962       13038 :   MeshTools::libmesh_assert_valid_unique_ids(*this);
     963             : #endif
     964             : 
     965             :   // Allow our GhostingFunctor objects to reinit if necessary.
     966             :   // Do this before partitioning and redistributing, and before
     967             :   // deleting remote elements.
     968      435171 :   if (!_preparation.has_reinit_ghosting_functors)
     969      432118 :     this->reinit_ghosting_functors();
     970             : 
     971             :   // Partition the mesh unless *all* partitioning is to be skipped.
     972             :   // If only noncritical partitioning is to be skipped, the
     973             :   // partition() call will still check for orphaned nodes.
     974      435171 :   if (!skip_partitioning() && !_preparation.is_partitioned)
     975       12842 :     this->partition();
     976       12806 :   else if (!this->n_unpartitioned_elem() &&
     977         196 :            !this->n_unpartitioned_nodes())
     978        6403 :     _preparation.is_partitioned = true;
     979             : 
     980             :   // If we're using DistributedMesh, we'll probably want it
     981             :   // parallelized.
     982      435171 :   if (this->_allow_remote_element_removal &&
     983      423087 :       !_preparation.has_removed_remote_elements)
     984      378680 :     this->delete_remote_elements();
     985             :   else
     986       56491 :     _preparation.has_removed_remote_elements = true;
     987             : 
     988             :   // Much of our boundary info may have been for now-remote parts of the mesh,
     989             :   // in which case we don't want to keep local copies of data meant to be
     990             :   // local. On the other hand we may have deleted, or the user may have added in
     991             :   // a distributed fashion, boundary data that is meant to be global. So we
     992             :   // handle both of those scenarios here
     993      435171 :   if (!_preparation.has_boundary_id_sets)
     994       89039 :     this->get_boundary_info().regenerate_id_sets();
     995             : 
     996      435171 :   if (!_skip_renumber_nodes_and_elements)
     997      390452 :     this->renumber_nodes_and_elements();
     998             : 
     999             :   // The mesh is now prepared for use, with the possible exception of
    1000             :   // partitioning that was supposed to be skipped, and it should know
    1001             :   // it.
    1002             : #ifndef NDEBUG
    1003       13038 :   Preparation completed_preparation = _preparation;
    1004       13038 :   if (skip_partitioning())
    1005         184 :     completed_preparation.is_partitioned = true;
    1006       13038 :   libmesh_assert(completed_preparation);
    1007             : #endif
    1008             : 
    1009             : #ifdef DEBUG
    1010       13038 :   MeshTools::libmesh_assert_valid_boundary_ids(*this);
    1011             : #ifdef LIBMESH_ENABLE_UNIQUE_ID
    1012       13038 :   MeshTools::libmesh_assert_valid_unique_ids(*this);
    1013             : #endif
    1014             : #endif
    1015      435171 : }
    1016             : 
    1017             : void
    1018      432118 : MeshBase::reinit_ghosting_functors()
    1019             : {
    1020      974386 :   for (auto & gf : _ghosting_functors)
    1021             :     {
    1022       16496 :       libmesh_assert(gf);
    1023      542268 :       gf->mesh_reinit();
    1024             :     }
    1025             : 
    1026      432118 :   _preparation.has_reinit_ghosting_functors = true;
    1027      432118 : }
    1028             : 
    1029     1014008 : void MeshBase::clear ()
    1030             : {
    1031             :   // Reset the number of partitions
    1032     1014008 :   _n_parts = 1;
    1033             : 
    1034             :   // Reset the preparation flags
    1035     1014008 :   _preparation = false;
    1036             : 
    1037             :   // Clear boundary information
    1038     1014008 :   if (boundary_info)
    1039     1012730 :     boundary_info->clear();
    1040             : 
    1041             :   // Clear cached element data
    1042       30309 :   _elem_dims.clear();
    1043       30309 :   _elem_default_orders.clear();
    1044     1014008 :   _supported_nodal_order = MAXIMUM;
    1045             : 
    1046       30309 :   _elemset_codes.clear();
    1047       30309 :   _elemset_codes_inverse_map.clear();
    1048             : 
    1049       30309 :   _constraint_rows.clear();
    1050             : 
    1051             :   // Clear our point locator.
    1052     1014008 :   this->clear_point_locator();
    1053     1014008 :   this->clear_stored_ranges();
    1054     1014008 : }
    1055             : 
    1056             : 
    1057       88988 : bool MeshBase::is_prepared() const
    1058             : {
    1059       88988 :   return static_cast<bool>(_preparation);
    1060             : }
    1061             : 
    1062             : 
    1063        2059 : void MeshBase::unset_is_prepared()
    1064             : {
    1065        2059 :   _preparation = false;
    1066        2059 :   this->clear_point_locator();
    1067        2059 :   this->clear_stored_ranges();
    1068        2059 : }
    1069             : 
    1070             : 
    1071     1004841 : void MeshBase::add_ghosting_functor(GhostingFunctor & ghosting_functor)
    1072             : {
    1073             :   // We used to implicitly support duplicate inserts to std::set
    1074             : #ifdef LIBMESH_ENABLE_DEPRECATED
    1075             :   _ghosting_functors.erase
    1076      947485 :     (std::remove(_ghosting_functors.begin(),
    1077             :                  _ghosting_functors.end(),
    1078     1004841 :                  &ghosting_functor),
    1079       86034 :      _ghosting_functors.end());
    1080             : #endif
    1081             : 
    1082             :   // We shouldn't have two copies of the same functor
    1083       28678 :   libmesh_assert(std::find(_ghosting_functors.begin(),
    1084             :                            _ghosting_functors.end(),
    1085             :                            &ghosting_functor) ==
    1086             :                  _ghosting_functors.end());
    1087             : 
    1088     1004841 :   _ghosting_functors.push_back(&ghosting_functor);
    1089     1004841 : }
    1090             : 
    1091             : 
    1092             : 
    1093      974299 : void MeshBase::remove_ghosting_functor(GhostingFunctor & ghosting_functor)
    1094             : {
    1095      918967 :   auto raw_it = std::find(_ghosting_functors.begin(),
    1096     1001965 :                           _ghosting_functors.end(), &ghosting_functor);
    1097             : 
    1098             :   // The DofMap has a "to_mesh" parameter that tells it to avoid
    1099             :   // registering a new functor with the mesh, but it doesn't keep
    1100             :   // track of which functors weren't added, so we'll support "remove a
    1101             :   // functor that isn't there" just like we did with set::erase
    1102             :   // before.
    1103      974299 :   if (raw_it != _ghosting_functors.end())
    1104      973376 :     _ghosting_functors.erase(raw_it);
    1105             : 
    1106             :   // We shouldn't have had two copies of the same functor
    1107       27666 :   libmesh_assert(std::find(_ghosting_functors.begin(),
    1108             :                            _ghosting_functors.end(),
    1109             :                            &ghosting_functor) ==
    1110             :                  _ghosting_functors.end());
    1111             : 
    1112      974299 :   if (const auto it = _shared_functors.find(&ghosting_functor);
    1113       27666 :       it != _shared_functors.end())
    1114          69 :     _shared_functors.erase(it);
    1115      974299 : }
    1116             : 
    1117             : 
    1118             : 
    1119       27995 : void MeshBase::subdomain_ids (std::set<subdomain_id_type> & ids, const bool global /* = true */) const
    1120             : {
    1121             :   // This requires an inspection on every processor
    1122        1298 :   if (global)
    1123        1298 :     parallel_object_only();
    1124             : 
    1125          34 :   struct SBDInserter {
    1126             :     std::set<subdomain_id_type> my_ids;
    1127             : 
    1128        1298 :     SBDInserter () {}
    1129          36 :     SBDInserter (SBDInserter &, Threads::split) {}
    1130             : 
    1131       28101 :     void operator()(const ConstElemRange & range) {
    1132     2218422 :       for (const Elem * elem : range)
    1133     2190321 :         my_ids.insert(elem->subdomain_id());
    1134       28101 :     }
    1135             : 
    1136          36 :     void join(SBDInserter & other) {
    1137          36 :       my_ids.merge(other.my_ids);
    1138          36 :     }
    1139             :   };
    1140             : 
    1141        2596 :   SBDInserter inserter;
    1142       27995 :   Threads::parallel_reduce(this->active_local_element_stored_range(), inserter);
    1143             : 
    1144        1298 :   ids.swap(inserter.my_ids);
    1145             : 
    1146       27995 :   if (global)
    1147             :     {
    1148             :       // Only include the unpartitioned elements if the user requests the global IDs.
    1149             :       // In the case of the local subdomain IDs, it doesn't make sense to include the
    1150             :       // unpartitioned elements because said elements do not have a sense of locality.
    1151       58166 :       for (const auto & elem : this->active_unpartitioned_element_ptr_range())
    1152       28873 :         ids.insert(elem->subdomain_id());
    1153             : 
    1154             :       // Some subdomains may only live on other processors
    1155       27995 :       this->comm().set_union(ids);
    1156             :     }
    1157       27995 : }
    1158             : 
    1159             : 
    1160             : 
    1161      628915 : void MeshBase::redistribute()
    1162             : {
    1163             :   // We now have all elements and nodes redistributed; our ghosting
    1164             :   // functors should be ready to redistribute and/or recompute any
    1165             :   // cached data they use too.
    1166      632099 :   for (auto & gf : as_range(this->ghosting_functors_begin(),
    1167     1424219 :                             this->ghosting_functors_end()))
    1168      766552 :     gf->redistribute();
    1169      628915 : }
    1170             : 
    1171             : 
    1172             : 
    1173      530503 : void MeshBase::update_post_partitioning()
    1174             : {
    1175             :   // A range over all elements might still be fine here, but any range
    1176             :   // over local elements is obsolete if our partitioner changed the
    1177             :   // definition of "local".
    1178        9694 :   _const_active_local_element_stored_range.reset(nullptr);
    1179      530503 : }
    1180             : 
    1181             : 
    1182             : 
    1183       21170 : subdomain_id_type MeshBase::n_subdomains() const
    1184             : {
    1185             :   // This requires an inspection on every processor
    1186         596 :   parallel_object_only();
    1187             : 
    1188        1192 :   std::set<subdomain_id_type> ids;
    1189             : 
    1190       21170 :   this->subdomain_ids (ids);
    1191             : 
    1192       21766 :   return cast_int<subdomain_id_type>(ids.size());
    1193             : }
    1194             : 
    1195             : 
    1196             : 
    1197           0 : subdomain_id_type MeshBase::n_local_subdomains() const
    1198             : {
    1199           0 :   std::set<subdomain_id_type> ids;
    1200             : 
    1201           0 :   this->subdomain_ids (ids, /* global = */ false);
    1202             : 
    1203           0 :   return cast_int<subdomain_id_type>(ids.size());
    1204             : }
    1205             : 
    1206             : 
    1207             : 
    1208             : 
    1209     3427021 : dof_id_type MeshBase::n_nodes_on_proc (const processor_id_type proc_id) const
    1210             : {
    1211             :   // We're either counting a processor's nodes or unpartitioned
    1212             :   // nodes
    1213        5752 :   libmesh_assert (proc_id < this->n_processors() ||
    1214             :                   proc_id == DofObject::invalid_processor_id);
    1215             : 
    1216     6848290 :   return static_cast<dof_id_type>(std::distance (this->pid_nodes_begin(proc_id),
    1217    10275311 :                                                  this->pid_nodes_end  (proc_id)));
    1218             : }
    1219             : 
    1220             : 
    1221             : 
    1222     3855261 : dof_id_type MeshBase::n_elem_on_proc (const processor_id_type proc_id) const
    1223             : {
    1224             :   // We're either counting a processor's elements or unpartitioned
    1225             :   // elements
    1226       18604 :   libmesh_assert (proc_id < this->n_processors() ||
    1227             :                   proc_id == DofObject::invalid_processor_id);
    1228             : 
    1229     7691918 :   return static_cast<dof_id_type>(std::distance (this->pid_elements_begin(proc_id),
    1230    11547179 :                                                  this->pid_elements_end  (proc_id)));
    1231             : }
    1232             : 
    1233             : 
    1234             : 
    1235     1284522 : dof_id_type MeshBase::n_active_elem_on_proc (const processor_id_type proc_id) const
    1236             : {
    1237        2164 :   libmesh_assert_less (proc_id, this->n_processors());
    1238     2566880 :   return static_cast<dof_id_type>(std::distance (this->active_pid_elements_begin(proc_id),
    1239     3851402 :                                                  this->active_pid_elements_end  (proc_id)));
    1240             : }
    1241             : 
    1242             : 
    1243             : 
    1244           0 : dof_id_type MeshBase::n_sub_elem () const
    1245             : {
    1246           0 :   dof_id_type ne=0;
    1247             : 
    1248           0 :   for (const auto & elem : this->element_ptr_range())
    1249           0 :     ne += elem->n_sub_elem();
    1250             : 
    1251           0 :   return ne;
    1252             : }
    1253             : 
    1254             : 
    1255             : 
    1256       32266 : dof_id_type MeshBase::n_active_sub_elem () const
    1257             : {
    1258        1575 :   dof_id_type ne=0;
    1259             : 
    1260    25301373 :   for (const auto & elem : this->active_element_ptr_range())
    1261    25267532 :     ne += elem->n_sub_elem();
    1262             : 
    1263       32266 :   return ne;
    1264             : }
    1265             : 
    1266             : 
    1267             : 
    1268       14995 : std::string MeshBase::get_info(const unsigned int verbosity /* = 0 */, const bool global /* = true */) const
    1269             : {
    1270       15835 :   std::ostringstream oss;
    1271             : 
    1272       14995 :   oss << " Mesh Information:" << '\n';
    1273             : 
    1274       14995 :   if (!_elem_dims.empty())
    1275             :     {
    1276       14995 :       oss << "  elem_dimensions()={";
    1277       14995 :       std::copy(_elem_dims.begin(),
    1278         420 :                 --_elem_dims.end(), // --end() is valid if the set is non-empty
    1279       14575 :                 std::ostream_iterator<unsigned int>(oss, ", "));
    1280       14995 :       oss << cast_int<unsigned int>(*_elem_dims.rbegin());
    1281       14995 :       oss << "}\n";
    1282             :     }
    1283             : 
    1284       14995 :   if (!_elem_default_orders.empty())
    1285             :     {
    1286       14995 :       oss << "  elem_default_orders()={";
    1287       14995 :       std::transform(_elem_default_orders.begin(),
    1288         420 :                      --_elem_default_orders.end(),
    1289       14995 :                      std::ostream_iterator<std::string>(oss, ", "),
    1290           8 :                      [](Order o)
    1291         276 :                        { return Utility::enum_to_string<Order>(o); });
    1292       14995 :       oss << Utility::enum_to_string<Order>(*_elem_default_orders.rbegin());
    1293       14995 :       oss << "}\n";
    1294             :     }
    1295             : 
    1296       14995 :   oss << "  supported_nodal_order()=" << this->supported_nodal_order()                        << '\n'
    1297       15835 :       << "  spatial_dimension()="     << this->spatial_dimension()                            << '\n'
    1298       15415 :       << "  n_nodes()="               << this->n_nodes()                                      << '\n'
    1299       14995 :       << "    n_local_nodes()="       << this->n_local_nodes()                                << '\n'
    1300       15415 :       << "  n_elem()="                << this->n_elem()                                       << '\n'
    1301       29570 :       << "    n_local_elem()="        << this->n_local_elem()                                 << '\n';
    1302             : #ifdef LIBMESH_ENABLE_AMR
    1303       29150 :   oss << "    n_active_elem()="       << this->n_active_elem()                                << '\n';
    1304             : #endif
    1305       14995 :   if (global)
    1306       15415 :     oss << "  n_subdomains()="        << static_cast<std::size_t>(this->n_subdomains())       << '\n';
    1307             :   else
    1308           0 :     oss << "  n_local_subdomains()= " << static_cast<std::size_t>(this->n_local_subdomains()) << '\n';
    1309       15415 :   oss << "  n_elemsets()="            << static_cast<std::size_t>(this->n_elemsets())         << '\n';
    1310       14995 :   if (!_elemset_codes.empty())
    1311           0 :     oss << "    n_elemset_codes="     << _elemset_codes.size()                                << '\n';
    1312       14995 :   oss << "  n_partitions()="          << static_cast<std::size_t>(this->n_partitions())       << '\n'
    1313       15835 :       << "  n_processors()="          << static_cast<std::size_t>(this->n_processors())       << '\n'
    1314       15415 :       << "  n_threads()="             << static_cast<std::size_t>(libMesh::n_threads())       << '\n'
    1315       15835 :       << "  processor_id()="          << static_cast<std::size_t>(this->processor_id())       << '\n'
    1316       15415 :       << "  is_prepared()="           << (this->is_prepared() ? "true" : "false")             << '\n'
    1317       43089 :       << "  is_replicated()="         << (this->is_replicated() ? "true" : "false")           << '\n';
    1318             : 
    1319       14995 :   if (verbosity > 0)
    1320             :     {
    1321         284 :       if (global)
    1322             :         {
    1323           8 :           libmesh_parallel_only(this->comm());
    1324         292 :           if (this->processor_id() != 0)
    1325         236 :             oss << "\n Detailed global get_info() (verbosity > 0) is reduced and output to only rank 0.";
    1326             :         }
    1327             : 
    1328             :       // Helper for printing element types
    1329         672 :       const auto elem_type_helper = [](const std::set<int> & elem_types) {
    1330         784 :         std::stringstream ss;
    1331        1344 :         for (auto it = elem_types.begin(); it != elem_types.end();)
    1332             :           {
    1333        1288 :             ss << Utility::enum_to_string((ElemType)*it);
    1334         672 :             if (++it != elem_types.end())
    1335           0 :               ss << ", ";
    1336             :           }
    1337         728 :         return ss.str();
    1338         560 :       };
    1339             : 
    1340             :       // Helper for whether or not the given DofObject is to be included. If we're doing
    1341             :       // a global reduction, we also count unpartitioned objects on rank 0.
    1342      871156 :       const auto include_object = [this, &global](const DofObject & dof_object) {
    1343      978188 :         return this->processor_id() == dof_object.processor_id() ||
    1344      250756 :                (global &&
    1345       42324 :                 this->processor_id() == 0 &&
    1346      846328 :                 dof_object.processor_id() == DofObject::invalid_processor_id);
    1347         276 :       };
    1348             : 
    1349           8 :       Real volume = 0;
    1350             : 
    1351             :       // Add bounding box information
    1352         284 :       const auto bbox = global ? MeshTools::create_bounding_box(*this) : MeshTools::create_local_bounding_box(*this);
    1353         284 :       if (!global || this->processor_id() == 0)
    1354             :         oss << "\n " << (global ? "" : "Local ") << "Mesh Bounding Box:\n"
    1355          48 :             << "  Minimum: " << bbox.min()                << "\n"
    1356          44 :             << "  Maximum: " << bbox.max()                << "\n"
    1357          88 :             << "  Delta:   " << (bbox.max() - bbox.min()) << "\n";
    1358             : 
    1359             :       // Obtain the global or local element types
    1360          16 :       std::set<int> elem_types;
    1361       49712 :       for (const Elem * elem : this->active_local_element_ptr_range())
    1362       49420 :         elem_types.insert(elem->type());
    1363         284 :       if (global)
    1364             :         {
    1365             :           // Pick up unpartitioned elems on rank 0
    1366         292 :           if (this->processor_id() == 0)
    1367          92 :             for (const Elem * elem : this->active_unpartitioned_element_ptr_range())
    1368          40 :               elem_types.insert(elem->type());
    1369             : 
    1370         284 :           this->comm().set_union(elem_types);
    1371             :         }
    1372             : 
    1373             :       // Add element types
    1374         284 :       if (!global || this->processor_id() == 0)
    1375             :         oss << "\n " << (global ? "" : "Local ") << "Mesh Element Type(s):\n  "
    1376         140 :             << elem_type_helper(elem_types) << "\n";
    1377             : 
    1378             :       // Reduce the nodeset ids
    1379          16 :       auto nodeset_ids = this->get_boundary_info().get_node_boundary_ids();
    1380         284 :       if (global)
    1381         284 :         this->comm().set_union(nodeset_ids);
    1382             : 
    1383             :       // Accumulate local information for each nodeset
    1384        1608 :       struct NodesetInfo
    1385             :       {
    1386             :         std::size_t num_nodes = 0;
    1387             :         BoundingBox bbox;
    1388             :       };
    1389          16 :       std::map<boundary_id_type, NodesetInfo> nodeset_info_map;
    1390       91376 :       for (const auto & [node, id] : this->get_boundary_info().get_nodeset_map())
    1391             :         {
    1392       91092 :           if (!include_object(*node))
    1393       38364 :             continue;
    1394             : 
    1395       48672 :           NodesetInfo & info = nodeset_info_map[id];
    1396             : 
    1397       48672 :           ++info.num_nodes;
    1398             : 
    1399       48672 :           if (verbosity > 1)
    1400       48672 :             info.bbox.union_with(*node);
    1401             :         }
    1402             : 
    1403             :       // Add nodeset info
    1404         284 :       if (!global || this->processor_id() == 0)
    1405             :         {
    1406          48 :           oss << "\n " << (global ? "" : "Local ") << "Mesh Nodesets:\n";
    1407          48 :           if (nodeset_ids.empty())
    1408           0 :             oss << "  None\n";
    1409             :         }
    1410             : 
    1411           8 :       const auto & nodeset_name_map = this->get_boundary_info().get_nodeset_name_map();
    1412        1988 :       for (const auto id : nodeset_ids)
    1413             :         {
    1414        1704 :           NodesetInfo & info = nodeset_info_map[id];
    1415             : 
    1416             :           // Reduce the local information for this nodeset if required
    1417        1704 :           if (global)
    1418             :             {
    1419        1704 :               this->comm().sum(info.num_nodes);
    1420        1704 :               if (verbosity > 1)
    1421             :                 {
    1422        1752 :                   this->comm().min(info.bbox.min());
    1423        1752 :                   this->comm().max(info.bbox.max());
    1424             :                 }
    1425             :             }
    1426             : 
    1427        1704 :           const bool has_name = nodeset_name_map.count(id) && nodeset_name_map.at(id).size();
    1428        1800 :           const std::string name = has_name ? nodeset_name_map.at(id) : "";
    1429          96 :           if (global)
    1430          48 :             libmesh_assert(this->comm().verify(name));
    1431             : 
    1432        1704 :           if (global ? this->processor_id() == 0 : info.num_nodes > 0)
    1433             :             {
    1434         288 :               oss << "  Nodeset " << id;
    1435         288 :               if (has_name)
    1436         528 :                 oss << " (" << name << ")";
    1437         312 :               oss << ", " << info.num_nodes << " " << (global ? "" : "local ") << "nodes\n";
    1438             : 
    1439         288 :               if (verbosity > 1)
    1440             :               {
    1441         288 :                 oss << "   " << (global ? "Bounding" : "Local bounding") << " box minimum: "
    1442         288 :                     << info.bbox.min() << "\n"
    1443         288 :                     << "   " << (global ? "Bounding" : "Local bounding") << " box maximum: "
    1444         288 :                     << info.bbox.max() << "\n"
    1445         288 :                     << "   " << (global ? "Bounding" : "Local bounding") << " box delta: "
    1446         288 :                     << (info.bbox.max() - info.bbox.min()) << "\n";
    1447             :               }
    1448             :             }
    1449             :         }
    1450             : 
    1451             :       // Reduce the sideset ids
    1452          16 :       auto sideset_ids = this->get_boundary_info().get_side_boundary_ids();
    1453         284 :       if (global)
    1454         284 :         this->comm().set_union(sideset_ids);
    1455             : 
    1456             :       // Accumulate local information for each sideset
    1457             :       struct SidesetInfo
    1458             :       {
    1459             :         std::size_t num_sides = 0;
    1460             :         Real volume = 0;
    1461             :         std::set<int> side_elem_types;
    1462             :         std::set<int> elem_types;
    1463             :         std::set<dof_id_type> elem_ids;
    1464             :         std::set<dof_id_type> node_ids;
    1465             :         BoundingBox bbox;
    1466             :       };
    1467          16 :       ElemSideBuilder side_builder;
    1468          16 :       std::map<boundary_id_type, SidesetInfo> sideset_info_map;
    1469       70396 :       for (const auto & pair : this->get_boundary_info().get_sideset_map())
    1470             :         {
    1471       70112 :           const Elem * elem = pair.first;
    1472       63456 :           if (!include_object(*elem))
    1473       30176 :             continue;
    1474             : 
    1475       39936 :           const auto id = pair.second.second;
    1476       39936 :           SidesetInfo & info = sideset_info_map[id];
    1477             : 
    1478       39936 :           const auto s = pair.second.first;
    1479       39936 :           const Elem & side = side_builder(*elem, s);
    1480             : 
    1481       39936 :           ++info.num_sides;
    1482       39936 :           info.side_elem_types.insert(side.type());
    1483       39936 :           info.elem_types.insert(elem->type());
    1484       39936 :           info.elem_ids.insert(elem->id());
    1485             : 
    1486      199680 :           for (const Node & node : side.node_ref_range())
    1487      146432 :             if (include_object(node))
    1488      147552 :               info.node_ids.insert(node.id());
    1489             : 
    1490       39936 :           if (verbosity > 1)
    1491             :           {
    1492       39936 :             info.volume += side.volume();
    1493       39936 :             info.bbox.union_with(side.loose_bounding_box());
    1494             :           }
    1495             :         }
    1496             : 
    1497             :       // Add sideset info
    1498         284 :       if (!global || this->processor_id() == 0)
    1499             :         {
    1500          48 :           oss << "\n " << (global ? "" : "Local ") << "Mesh Sidesets:\n";
    1501          48 :           if (sideset_ids.empty())
    1502           0 :             oss << "  None\n";
    1503             :         }
    1504           8 :       const auto & sideset_name_map = this->get_boundary_info().get_sideset_name_map();
    1505        1988 :       for (const auto id : sideset_ids)
    1506             :         {
    1507        1704 :           SidesetInfo & info = sideset_info_map[id];
    1508             : 
    1509        1704 :           auto num_elems = info.elem_ids.size();
    1510        1704 :           auto num_nodes = info.node_ids.size();
    1511             : 
    1512             :           // Reduce the local information for this sideset if required
    1513        1704 :           if (global)
    1514             :             {
    1515        1704 :               this->comm().sum(info.num_sides);
    1516        1704 :               this->comm().set_union(info.side_elem_types, 0);
    1517        1704 :               this->comm().sum(num_elems);
    1518        1704 :               this->comm().set_union(info.elem_types, 0);
    1519        1704 :               this->comm().sum(num_nodes);
    1520        1704 :               if (verbosity > 1)
    1521             :               {
    1522        1704 :                 this->comm().sum(info.volume);
    1523        1752 :                 this->comm().min(info.bbox.min());
    1524        1752 :                 this->comm().max(info.bbox.max());
    1525             :               }
    1526             :             }
    1527             : 
    1528        1704 :           const bool has_name = sideset_name_map.count(id) && sideset_name_map.at(id).size();
    1529        1800 :           const std::string name = has_name ? sideset_name_map.at(id) : "";
    1530          96 :           if (global)
    1531          48 :             libmesh_assert(this->comm().verify(name));
    1532             : 
    1533        1704 :           if (global ? this->processor_id() == 0 : info.num_sides > 0)
    1534             :             {
    1535         288 :               oss << "  Sideset " << id;
    1536         288 :               if (has_name)
    1537         528 :                 oss << " (" << name << ")";
    1538         552 :               oss << ", " << info.num_sides << " sides (" << elem_type_helper(info.side_elem_types) << ")"
    1539        1056 :                   << ", " << num_elems << " " << (global ? "" : "local ") << "elems (" << elem_type_helper(info.elem_types) << ")"
    1540         600 :                   << ", " << num_nodes << " " << (global ? "" : "local ") << "nodes\n";
    1541             : 
    1542         288 :               if (verbosity > 1)
    1543             :               {
    1544         312 :                 oss << "   " << (global ? "Side" : "Local side") << " volume: " << info.volume << "\n"
    1545         288 :                     << "   " << (global ? "Bounding" : "Local bounding") << " box minimum: "
    1546         288 :                     << info.bbox.min() << "\n"
    1547         288 :                     << "   " << (global ? "Bounding" : "Local bounding") << " box maximum: "
    1548         288 :                     << info.bbox.max() << "\n"
    1549         288 :                     << "   " << (global ? "Bounding" : "Local bounding") << " box delta: "
    1550         288 :                     << (info.bbox.max() - info.bbox.min()) << "\n";
    1551             :               }
    1552             :             }
    1553             :         }
    1554             : 
    1555             :       // Reduce the edgeset ids
    1556          16 :       auto edgeset_ids = this->get_boundary_info().get_edge_boundary_ids();
    1557         284 :       if (global)
    1558         284 :         this->comm().set_union(edgeset_ids);
    1559             : 
    1560             :       // Accumulate local information for each edgeset
    1561           0 :       struct EdgesetInfo
    1562             :       {
    1563             :         std::size_t num_edges = 0;
    1564             :         std::set<int> edge_elem_types;
    1565             :         BoundingBox bbox;
    1566             :       };
    1567          16 :       std::map<boundary_id_type, EdgesetInfo> edgeset_info_map;
    1568         284 :       std::unique_ptr<const Elem> edge;
    1569             : 
    1570         284 :       for (const auto & pair : this->get_boundary_info().get_edgeset_map())
    1571             :         {
    1572           0 :           const Elem * elem = pair.first;
    1573           0 :           if (!include_object(*elem))
    1574           0 :             continue;
    1575             : 
    1576           0 :           const auto id = pair.second.second;
    1577           0 :           EdgesetInfo & info = edgeset_info_map[id];
    1578             : 
    1579           0 :           elem->build_edge_ptr(edge, pair.second.first);
    1580             : 
    1581           0 :           ++info.num_edges;
    1582           0 :           info.edge_elem_types.insert(edge->type());
    1583             : 
    1584           0 :           if (verbosity > 1)
    1585           0 :             info.bbox.union_with(edge->loose_bounding_box());
    1586             :         }
    1587             : 
    1588             :       // Add edgeset info
    1589         284 :       if (!global || this->processor_id() == 0)
    1590             :         {
    1591          48 :           oss << "\n " << (global ? "" : "Local ") << "Mesh Edgesets:\n";
    1592          48 :           if (edgeset_ids.empty())
    1593          48 :             oss << "  None\n";
    1594             :         }
    1595             : 
    1596           8 :       const auto & edgeset_name_map = this->get_boundary_info().get_edgeset_name_map();
    1597         284 :       for (const auto id : edgeset_ids)
    1598             :         {
    1599           0 :           EdgesetInfo & info = edgeset_info_map[id];
    1600             : 
    1601             :           // Reduce the local information for this edgeset if required
    1602           0 :           if (global)
    1603             :             {
    1604           0 :               this->comm().sum(info.num_edges);
    1605           0 :               this->comm().set_union(info.edge_elem_types, 0);
    1606           0 :               if (verbosity > 1)
    1607             :                 {
    1608           0 :                   this->comm().min(info.bbox.min());
    1609           0 :                   this->comm().min(info.bbox.max());
    1610             :                 }
    1611             :             }
    1612             : 
    1613           0 :           const bool has_name = edgeset_name_map.count(id) && edgeset_name_map.at(id).size();
    1614           0 :           const std::string name = has_name ? edgeset_name_map.at(id) : "";
    1615           0 :           if (global)
    1616           0 :             libmesh_assert(this->comm().verify(name));
    1617             : 
    1618           0 :           if (global ? this->processor_id() == 0 : info.num_edges > 0)
    1619             :             {
    1620           0 :               oss << "  Edgeset " << id;
    1621           0 :               if (has_name)
    1622           0 :                 oss << " (" << name << ")";
    1623           0 :               oss << ", " << info.num_edges << " " << (global ? "" : "local ") << "edges ("
    1624           0 :                   << elem_type_helper(info.edge_elem_types) << ")\n";
    1625             : 
    1626           0 :               if (verbosity > 1)
    1627             :               {
    1628           0 :                 oss << "   " << (global ? "Bounding" : "Local bounding") << " box minimum: "
    1629           0 :                     << info.bbox.min() << "\n"
    1630           0 :                     << "   " << (global ? "Bounding" : "Local bounding") << " box maximum: "
    1631           0 :                     << info.bbox.max() << "\n"
    1632           0 :                     << "   " << (global ? "Bounding" : "Local bounding") << " box delta: "
    1633           0 :                     << (info.bbox.max() - info.bbox.min()) << "\n";
    1634             :               }
    1635             :             }
    1636             :         }
    1637             : 
    1638             :       // Reduce the block IDs and block names
    1639          16 :       std::set<subdomain_id_type> subdomains;
    1640      194032 :       for (const Elem * elem : this->active_element_ptr_range())
    1641       92640 :         if (include_object(*elem))
    1642       49420 :           subdomains.insert(elem->subdomain_id());
    1643         284 :       if (global)
    1644         284 :         this->comm().set_union(subdomains);
    1645             : 
    1646             :       // Accumulate local information for each subdomain
    1647           0 :       struct SubdomainInfo
    1648             :       {
    1649             :         std::size_t num_elems = 0;
    1650             :         Real volume = 0;
    1651             :         std::set<int> elem_types;
    1652             :         std::set<dof_id_type> active_node_ids;
    1653             : #ifdef LIBMESH_ENABLE_AMR
    1654             :         std::size_t num_active_elems = 0;
    1655             : #endif
    1656             :         BoundingBox bbox;
    1657             :       };
    1658          16 :       std::map<subdomain_id_type, SubdomainInfo> subdomain_info_map;
    1659      194032 :       for (const Elem * elem : this->element_ptr_range())
    1660       92640 :         if (include_object(*elem))
    1661             :           {
    1662       49152 :             SubdomainInfo & info = subdomain_info_map[elem->subdomain_id()];
    1663             : 
    1664       49152 :             ++info.num_elems;
    1665       49152 :             info.elem_types.insert(elem->type());
    1666             : 
    1667             : #ifdef LIBMESH_ENABLE_AMR
    1668        4096 :             if (elem->active())
    1669       49152 :               ++info.num_active_elems;
    1670             : #endif
    1671             : 
    1672      442368 :             for (const Node & node : elem->node_ref_range())
    1673      392704 :               if (include_object(node) && node.active())
    1674      330608 :                 info.active_node_ids.insert(node.id());
    1675             : 
    1676       49152 :             if (verbosity > 1 && elem->active())
    1677             :               {
    1678       49152 :                 info.volume += elem->volume();
    1679       49152 :                 info.bbox.union_with(elem->loose_bounding_box());
    1680             :               }
    1681         268 :           }
    1682             : 
    1683             :       // Add subdomain info
    1684         284 :       oss << "\n " << (global ? "" : "Local ") << "Mesh Subdomains:\n";
    1685           8 :       const auto & subdomain_name_map = this->get_subdomain_name_map();
    1686         568 :       for (const auto id : subdomains)
    1687             :       {
    1688         284 :         SubdomainInfo & info = subdomain_info_map[id];
    1689             : 
    1690         284 :         auto num_active_nodes = info.active_node_ids.size();
    1691             : 
    1692             :         // Reduce the information for this subdomain if needed
    1693         284 :         if (global)
    1694             :           {
    1695         284 :             this->comm().sum(info.num_elems);
    1696             : #ifdef LIBMESH_ENABLE_AMR
    1697         284 :             this->comm().sum(info.num_active_elems);
    1698             : #endif
    1699         284 :             this->comm().sum(num_active_nodes);
    1700         284 :             this->comm().set_union(info.elem_types, 0);
    1701         284 :             if (verbosity > 1)
    1702             :             {
    1703         292 :               this->comm().min(info.bbox.min());
    1704         292 :               this->comm().max(info.bbox.max());
    1705         284 :               this->comm().sum(info.volume);
    1706             :             }
    1707             :           }
    1708         284 :         if (verbosity > 1)
    1709         284 :           volume += info.volume;
    1710             : 
    1711           8 :         const bool has_name = subdomain_name_map.count(id);
    1712         292 :         const std::string name = has_name ? subdomain_name_map.at(id) : "";
    1713          16 :         if (global)
    1714           8 :           libmesh_assert(this->comm().verify(name));
    1715             : 
    1716         284 :         if (!global || this->processor_id() == 0)
    1717             :           {
    1718          48 :             oss << "  Subdomain " << id;
    1719          48 :             if (has_name)
    1720           0 :               oss << " (" << name << ")";
    1721          48 :             oss << ": " << info.num_elems << " " << (global ? "" : "local ") << "elems "
    1722          56 :                 << "(" << elem_type_helper(info.elem_types);
    1723             : #ifdef LIBMESH_ENABLE_AMR
    1724          48 :             oss << ", " << info.num_active_elems << " active";
    1725             : #endif
    1726          52 :             oss << "), " << num_active_nodes << " " << (global ? "" : "local ") << "active nodes\n";
    1727          48 :             if (verbosity > 1)
    1728             :             {
    1729          52 :               oss << "   " << (global ? "Volume" : "Local volume") << ": " << info.volume << "\n";
    1730          48 :               oss << "   " << (global ? "Bounding" : "Local bounding") << " box minimum: "
    1731          48 :                   << info.bbox.min() << "\n"
    1732          48 :                   << "   " << (global ? "Bounding" : "Local bounding") << " box maximum: "
    1733          48 :                   << info.bbox.max() << "\n"
    1734          48 :                   << "   " << (global ? "Bounding" : "Local bounding") << " box delta: "
    1735          48 :                   << (info.bbox.max() - info.bbox.min()) << "\n";
    1736             :             }
    1737             :           }
    1738             :       }
    1739             : 
    1740         560 :       oss << "  " << (global ? "Global" : "Local") << " mesh volume = " << volume << "\n";
    1741             : 
    1742         268 :     }
    1743             : 
    1744       15415 :   return oss.str();
    1745       14155 : }
    1746             : 
    1747             : 
    1748       14995 : void MeshBase::print_info(std::ostream & os, const unsigned int verbosity /* = 0 */, const bool global /* = true */) const
    1749             : {
    1750       15415 :   os << this->get_info(verbosity, global)
    1751         420 :      << std::endl;
    1752       14995 : }
    1753             : 
    1754             : 
    1755           0 : std::ostream & operator << (std::ostream & os, const MeshBase & m)
    1756             : {
    1757           0 :   m.print_info(os);
    1758           0 :   return os;
    1759             : }
    1760             : 
    1761             : 
    1762      429620 : void MeshBase::partition (const unsigned int n_parts)
    1763             : {
    1764             :   // If we get here and we have unpartitioned elements, we need that
    1765             :   // fixed.
    1766      429620 :   if (this->n_unpartitioned_elem() > 0)
    1767             :     {
    1768        9392 :       libmesh_assert (partitioner().get());
    1769        9392 :       libmesh_assert (this->is_serial());
    1770      314977 :       partitioner()->partition (*this, n_parts);
    1771             :     }
    1772             :   // A nullptr partitioner or a skip_partitioning(true) call or a
    1773             :   // skip_noncritical_partitioning(true) call means don't repartition;
    1774             :   // skip_noncritical_partitioning() checks all these.
    1775        3474 :   else if (!skip_noncritical_partitioning())
    1776             :     {
    1777      108553 :       partitioner()->partition (*this, n_parts);
    1778             :     }
    1779             :   else
    1780             :     {
    1781             :       // Adaptive coarsening may have "orphaned" nodes on processors
    1782             :       // whose elements no longer share them.  We need to check for
    1783             :       // and possibly fix that.
    1784        6090 :       MeshTools::correct_node_proc_ids(*this);
    1785             : 
    1786             :       // Make sure locally cached partition count is correct
    1787        6090 :       this->recalculate_n_partitions();
    1788             : 
    1789             :       // Make sure any other locally cached data is correct
    1790        6090 :       this->update_post_partitioning();
    1791             :     }
    1792             : 
    1793      429620 :   _preparation.is_partitioned = true;
    1794      429620 : }
    1795             : 
    1796       18223 : void MeshBase::all_second_order (const bool full_ordered)
    1797             : {
    1798       18223 :   this->all_second_order_range(this->element_ptr_range(), full_ordered);
    1799       18223 : }
    1800             : 
    1801       14971 : void MeshBase::all_complete_order ()
    1802             : {
    1803       14971 :   this->all_complete_order_range(this->element_ptr_range());
    1804       14971 : }
    1805             : 
    1806        6090 : unsigned int MeshBase::recalculate_n_partitions()
    1807             : {
    1808             :   // This requires an inspection on every processor
    1809         152 :   parallel_object_only();
    1810             : 
    1811        6090 :   unsigned int max_proc_id=0;
    1812             : 
    1813      218454 :   for (const auto & elem : this->active_local_element_ptr_range())
    1814      215989 :     max_proc_id = std::max(max_proc_id, static_cast<unsigned int>(elem->processor_id()));
    1815             : 
    1816             :   // The number of partitions is one more than the max processor ID.
    1817        6090 :   _n_parts = max_proc_id+1;
    1818             : 
    1819        6090 :   this->comm().max(_n_parts);
    1820             : 
    1821        6090 :   return _n_parts;
    1822             : }
    1823             : 
    1824             : 
    1825             : 
    1826      555104 : std::unique_ptr<PointLocatorBase> MeshBase::sub_point_locator () const
    1827             : {
    1828             :   // If there's no master point locator, then we need one.
    1829      555104 :   if (_point_locator.get() == nullptr)
    1830             :     {
    1831             :       // PointLocator construction may not be safe within threads
    1832         760 :       libmesh_assert(!Threads::in_threads);
    1833             : 
    1834             :       // And it may require parallel communication
    1835         760 :       parallel_object_only();
    1836             : 
    1837             : #ifdef LIBMESH_ENABLE_NANOFLANN_POINTLOCATOR
    1838             :       _point_locator = PointLocatorBase::build(NANOFLANN, *this);
    1839             : #else
    1840       43522 :       _point_locator = PointLocatorBase::build(TREE_ELEMENTS, *this);
    1841             : #endif
    1842             : 
    1843       22521 :       if (_point_locator_close_to_point_tol > 0.)
    1844           0 :         _point_locator->set_close_to_point_tol(_point_locator_close_to_point_tol);
    1845             :     }
    1846             : 
    1847             :   // Otherwise there was a master point locator, and we can grab a
    1848             :   // sub-locator easily.
    1849             :   return
    1850             : #ifdef LIBMESH_ENABLE_NANOFLANN_POINTLOCATOR
    1851             :     PointLocatorBase::build(NANOFLANN, *this, _point_locator.get());
    1852             : #else
    1853      555104 :     PointLocatorBase::build(TREE_ELEMENTS, *this, _point_locator.get());
    1854             : #endif
    1855             : }
    1856             : 
    1857             : 
    1858             : 
    1859   108382066 : void MeshBase::clear_point_locator ()
    1860             : {
    1861     1552623 :   _point_locator.reset(nullptr);
    1862   108382066 : }
    1863             : 
    1864             : 
    1865             : 
    1866           0 : void MeshBase::set_count_lower_dim_elems_in_point_locator(bool count_lower_dim_elems)
    1867             : {
    1868           0 :   _count_lower_dim_elems_in_point_locator = count_lower_dim_elems;
    1869           0 : }
    1870             : 
    1871             : 
    1872             : 
    1873     6658441 : bool MeshBase::get_count_lower_dim_elems_in_point_locator() const
    1874             : {
    1875     6658441 :   return _count_lower_dim_elems_in_point_locator;
    1876             : }
    1877             : 
    1878             : 
    1879             : 
    1880     1063271 : std::string & MeshBase::subdomain_name(subdomain_id_type id)
    1881             : {
    1882     1063271 :   return _block_id_to_name[id];
    1883             : }
    1884             : 
    1885       44105 : const std::string & MeshBase::subdomain_name(subdomain_id_type id) const
    1886             : {
    1887             :   // An empty string to return when no matching subdomain name is found
    1888       44105 :   static const std::string empty;
    1889             : 
    1890       44105 :   if (const auto iter = _block_id_to_name.find(id);
    1891        1623 :       iter == _block_id_to_name.end())
    1892        1417 :     return empty;
    1893             :   else
    1894        2476 :     return iter->second;
    1895             : }
    1896             : 
    1897             : 
    1898             : 
    1899             : 
    1900           0 : subdomain_id_type MeshBase::get_id_by_name(std::string_view name) const
    1901             : {
    1902             :   // Linear search over the map values.
    1903           0 :   for (const auto & [sbd_id, sbd_name] : _block_id_to_name)
    1904           0 :     if (sbd_name == name)
    1905           0 :       return sbd_id;
    1906             : 
    1907             :   // If we made it here without returning, we don't have a subdomain
    1908             :   // with the requested name, so return Elem::invalid_subdomain_id.
    1909           0 :   return Elem::invalid_subdomain_id;
    1910             : }
    1911             : 
    1912             : 
    1913     1666752 : const ElemRange & MeshBase::element_stored_range()
    1914             : {
    1915     1666752 :   if (!_element_stored_range)
    1916             :   {
    1917             :     // Range construction may not be safe within threads
    1918       13908 :     libmesh_assert(!Threads::in_threads);
    1919             : 
    1920             :     _element_stored_range =
    1921     2028793 :       std::make_unique<ElemRange>(this->elements_begin(),
    1922     1384970 :                                   this->elements_end());
    1923             :   }
    1924             : 
    1925     1666752 :   return *_element_stored_range;
    1926             : }
    1927             : 
    1928      567201 : const ConstElemRange & MeshBase::active_local_element_stored_range() const
    1929             : {
    1930      567201 :   if (!_const_active_local_element_stored_range)
    1931             :   {
    1932             :     // Range construction may not be safe within threads
    1933        1754 :     libmesh_assert(!Threads::in_threads);
    1934             : 
    1935             :     _const_active_local_element_stored_range =
    1936      898262 :       std::make_unique<ConstElemRange>(this->active_local_elements_begin(),
    1937      602934 :                                   this->active_local_elements_end());
    1938             :   }
    1939             : 
    1940      567201 :   return *_const_active_local_element_stored_range;
    1941             : }
    1942             : 
    1943   110606460 : void MeshBase::clear_stored_ranges()
    1944             : {
    1945     1549103 :   _element_stored_range.reset(nullptr);
    1946     1549103 :   _const_active_local_element_stored_range.reset(nullptr);
    1947   110606460 : }
    1948             : 
    1949             : 
    1950             : #ifdef LIBMESH_ENABLE_DEPRECATED
    1951           0 : void MeshBase::cache_elem_dims()
    1952             : {
    1953             :   libmesh_deprecated();
    1954             : 
    1955           0 :   this->cache_elem_data();
    1956           0 : }
    1957             : #endif // LIBMESH_ENABLE_DEPRECATED
    1958             : 
    1959      497512 : void MeshBase::cache_elem_data()
    1960             : {
    1961             :   // This requires an inspection on every processor
    1962       15032 :   parallel_object_only();
    1963             : 
    1964             :   // Need to clear containers first in case all elements of a
    1965             :   // particular dimension/order/subdomain have been deleted.
    1966       30048 :   _elem_dims.clear();
    1967       30048 :   _elem_default_orders.clear();
    1968       30048 :   _mesh_subdomains.clear();
    1969      497512 :   _supported_nodal_order = MAXIMUM;
    1970             : 
    1971    34392519 :   for (const auto & elem : this->active_element_ptr_range())
    1972             :   {
    1973    33412527 :     _elem_dims.insert(cast_int<unsigned char>(elem->dim()));
    1974    33412527 :     _elem_default_orders.insert(elem->default_order());
    1975    33412527 :     _mesh_subdomains.insert(elem->subdomain_id());
    1976    33412527 :     _supported_nodal_order =
    1977    33412527 :       static_cast<Order>
    1978    66825054 :         (std::min(static_cast<int>(_supported_nodal_order),
    1979    33866008 :                   static_cast<int>(elem->supported_nodal_order())));
    1980      467464 :   }
    1981             : 
    1982      497512 :   if (!this->is_serial())
    1983             :   {
    1984             :     // Some different dimension/order/subdomain elements may only live
    1985             :     // on other processors
    1986      121114 :     this->comm().set_union(_elem_dims);
    1987      121114 :     this->comm().set_union(_elem_default_orders);
    1988      121114 :     this->comm().min(_supported_nodal_order);
    1989      121114 :     this->comm().set_union(_mesh_subdomains);
    1990             :   }
    1991             : 
    1992             :   // If the largest element dimension found is larger than the current
    1993             :   // _spatial_dimension, increase _spatial_dimension.
    1994      497512 :   unsigned int max_dim = this->mesh_dimension();
    1995      497512 :   if (max_dim > _spatial_dimension)
    1996       42439 :     _spatial_dimension = cast_int<unsigned char>(max_dim);
    1997             : 
    1998             :   // _spatial_dimension may need to increase from 1->2 or 2->3 if the
    1999             :   // mesh is full of 1D elements but they are not x-aligned, or the
    2000             :   // mesh is full of 2D elements but they are not in the x-y plane.
    2001             :   // If the mesh is x-aligned or x-y planar, we will end up checking
    2002             :   // every node's coordinates and not breaking out of the loop
    2003             :   // early...
    2004      497512 :   if (_spatial_dimension < LIBMESH_DIM)
    2005             :     {
    2006    49250688 :       for (const auto & node : this->node_ptr_range())
    2007             :         {
    2008             :           // Note: the exact floating point comparison is intentional,
    2009             :           // we don't want to get tripped up by tolerances.
    2010    25476188 :           if ((*node)(0) != 0. && _spatial_dimension < 1)
    2011           0 :             _spatial_dimension = 1;
    2012             : 
    2013    25476188 :           if ((*node)(1) != 0. && _spatial_dimension < 2)
    2014             :             {
    2015        2501 :               _spatial_dimension = 2;
    2016             : #if LIBMESH_DIM == 2
    2017             :               // If libmesh is compiled in 2D mode, this is the
    2018             :               // largest spatial dimension possible so we can break
    2019             :               // out.
    2020             :               break;
    2021             : #endif
    2022             :             }
    2023             : 
    2024             : #if LIBMESH_DIM > 2
    2025    25476188 :           if ((*node)(2) != 0.)
    2026             :             {
    2027             :               // Spatial dimension can't get any higher than this, so
    2028             :               // we can break out.
    2029        7801 :               _spatial_dimension = 3;
    2030        7801 :               break;
    2031             :             }
    2032             : #endif
    2033      254307 :         }
    2034             :     }
    2035             : 
    2036      497512 :   _preparation.has_cached_elem_data = true;
    2037      497512 : }
    2038             : 
    2039             : 
    2040       49940 : void MeshBase::sync_subdomain_name_map()
    2041             : {
    2042             :   // This requires every processor
    2043        1628 :   parallel_object_only();
    2044             : 
    2045       49940 :   this->comm().set_union(_block_id_to_name);
    2046       49940 : }
    2047             : 
    2048             : 
    2049      432118 : void MeshBase::detect_interior_parents()
    2050             : {
    2051       12952 :   LOG_SCOPE("detect_interior_parents()", "MeshBase");
    2052             : 
    2053             :   // This requires an inspection on every processor
    2054       12952 :   parallel_object_only();
    2055             : 
    2056             :   // This requires up-to-date mesh dimensions in cache
    2057       12952 :   libmesh_assert(_preparation.has_cached_elem_data);
    2058             : 
    2059             :   // Early return if the mesh is empty or has elements of a single spatial dimension.
    2060      432118 :   if (this->elem_dimensions().size() <= 1)
    2061             :     {
    2062      425433 :       _preparation.has_interior_parent_ptrs = true;
    2063      426650 :       return;
    2064             :     }
    2065             : 
    2066             :   // Convenient elem_dimensions iterators
    2067        6497 :   const auto dim_start = this->elem_dimensions().begin();
    2068         188 :   const auto dim_end = this->elem_dimensions().end();
    2069             : 
    2070             :   // In this function we find only +1 dimensional interior parents,
    2071             :   // (so, for a given element el, the interior parent p must satisfy p.dim() == el.dim() + 1).
    2072             :   // Therefore, we can avoid checking the existence of interior parents
    2073             :   // for all those elements el such there there is no p with p.dim() == el.dim() + 1.
    2074             :   // We store whether to skip any given dimension in the construction of interior parents
    2075             :   // inside the vector in dimensions_to_skip_for_interior_parents.
    2076        6685 :   std::vector<bool> skip_dimension_for_interior_parents(/*count=*/LIBMESH_DIM+1, /*value=*/false);
    2077        6497 :   skip_dimension_for_interior_parents.back() = true;
    2078             : 
    2079             :   // Moreover, in the following, we will build a node-to-elem map.
    2080             :   // It is among the elems of this map that we will look for interior parents.
    2081             :   // Therefore, we can skip all elems p such that there is no el with el.dim() == p.dim() - 1.
    2082             :   // We store whether to skip any given dimension in the construction of the node-to-elem map
    2083             :   // in the vector skip_dimensions_for_node_to_el_map.
    2084        6685 :   std::vector<bool> skip_dimensions_for_node_to_el_map(/*count=*/LIBMESH_DIM+1, /*value=*/false);
    2085        6685 :   skip_dimensions_for_node_to_el_map[*dim_start] = true;
    2086             : 
    2087             :   // We also create a flag to know if all dimensions should be skipped,
    2088             :   // and if we should therefore return early.
    2089         188 :   bool skip_all_dimensions = true;
    2090             : 
    2091             :   // Fill dimensions_to_skip_for_interior_parents and dimensions_to_skip_for_node_to_el_map.
    2092        6877 :   for (auto [it, next] = std::make_tuple(dim_start, std::next(dim_start));
    2093       13704 :        next != dim_end; ++it, ++next)
    2094             :     {
    2095        6827 :       if (*it + 1 != *next) // if sequential dimensions differ by exactly 1
    2096             :         {
    2097          80 :           skip_dimension_for_interior_parents[*it] = true;
    2098        1431 :           skip_dimensions_for_node_to_el_map[*next] = true;
    2099             :         }
    2100        5548 :       else if (!skip_dimension_for_interior_parents[*it])
    2101         152 :         skip_all_dimensions = false;
    2102             :     }
    2103             : 
    2104             :   // There is nothing to do if all dimensions should be
    2105             :   // skipped. Before returning, we must also set the flag that says
    2106             :   // "interior parent pointers have been set up" even though we
    2107             :   // determined there was no work to be done.
    2108        6685 :   if (skip_all_dimensions)
    2109             :     {
    2110        1289 :       _preparation.has_interior_parent_ptrs = true;
    2111          36 :       return;
    2112             :     }
    2113             : 
    2114             :   // Do we have interior parent pointers going to a different mesh?
    2115             :   // If so then we'll still check to make sure that's the only place
    2116             :   // they go, so we can libmesh_not_implemented() if not.
    2117         304 :   const bool separate_interior_mesh = (&(this->interior_mesh()) != this);
    2118             : 
    2119             :   // This map will be used to set interior parents
    2120         152 :   std::unordered_map<dof_id_type, std::vector<dof_id_type>> node_to_elem;
    2121             : 
    2122      303450 :   for (const auto & elem : this->element_ptr_range())
    2123             :     {
    2124             :       // Ignore element if it cannot be interior parent of any other elem.
    2125      154551 :       if (skip_dimensions_for_node_to_el_map[elem->dim()])
    2126       64974 :         continue;
    2127             : 
    2128             :       // Populating the node_to_elem map, same as MeshTools::build_nodes_to_elem_map
    2129      495939 :       for (auto n : make_range(elem->n_vertices()))
    2130             :         {
    2131       24992 :           libmesh_assert_less (elem->id(), this->max_elem_id());
    2132             : 
    2133      433656 :           node_to_elem[elem->node_id(n)].push_back(elem->id());
    2134             :         }
    2135        5092 :     }
    2136             : 
    2137             :   // Automatically set interior parents
    2138      303450 :   for (const auto & element : this->element_ptr_range())
    2139             :     {
    2140             :       // Ignore elements with dimensions to skip
    2141             :       // or elements that already have an interior parent.
    2142      154551 :       if (skip_dimension_for_interior_parents[element->dim()] || element->interior_parent())
    2143       32778 :         continue;
    2144             : 
    2145             :       // Start by generating sets of dim+1 dimensional elements that
    2146             :       // touch each vertex of the current element.  If we encounter a
    2147             :       // vertex not connected to _any_ dim+1 dimensional elements,
    2148             :       // then we can exit the loop without checking the remaining
    2149             :       // vertices since an interior parent (if it exists) will be
    2150             :       // connected to all vertices of the current element.
    2151      135413 :       std::vector<std::set<dof_id_type>> neighbors( element->n_vertices() );
    2152             : 
    2153        6820 :       bool found_interior_parents = true;
    2154             : 
    2155      124968 :       for (auto n : make_range(element->n_vertices()))
    2156             :         {
    2157      130856 :           auto it = node_to_elem.find(element->node_id(n));
    2158             : 
    2159             :           // Check at first that this node is not isolated.
    2160      123974 :           if (it == node_to_elem.end())
    2161             :             {
    2162        1472 :               found_interior_parents = false;
    2163        6792 :               break; // out of n-loop
    2164             :             }
    2165             : 
    2166      356987 :           for (const auto & vertex_neighbor_id : it->second)
    2167      285119 :             if (this->elem_ref(vertex_neighbor_id).dim() == element->dim()+1)
    2168        8733 :               neighbors[n].insert(vertex_neighbor_id);
    2169             : 
    2170       77278 :           if (neighbors[n].empty())
    2171             :             {
    2172             :               // We have found an empty set for one vertex, no reason
    2173             :               // to continue.
    2174        5320 :               found_interior_parents = false;
    2175        5320 :               break; // out of n-loop
    2176             :             }
    2177             :         }
    2178             : 
    2179             :       // If we have generated a non-empty set of elements for each
    2180             :       // vertex, we will now look for a vertex_neighbor_id that
    2181             :       // appears in _all_ of those sets.  If found, this is our interior
    2182             :       // parent id.  If multiple such common ids are found, we will
    2183             :       // take the lowest such id to be the interior parent id.
    2184        6820 :       if (found_interior_parents)
    2185             :         {
    2186          56 :           std::set<dof_id_type> & neighbors_0 = neighbors[0];
    2187        1633 :           for (const auto & interior_parent_id : neighbors_0)
    2188             :             {
    2189          40 :               found_interior_parents = false;
    2190        2627 :               for (auto n : make_range(1u, element->n_vertices()))
    2191             :                 {
    2192        1633 :                   if (neighbors[n].count(interior_parent_id))
    2193             :                     {
    2194          34 :                       found_interior_parents = true;
    2195             :                     }
    2196             :                   else
    2197             :                     {
    2198          12 :                       found_interior_parents = false;
    2199          12 :                       break;
    2200             :                     }
    2201             :                 }
    2202             : 
    2203        1420 :               if (found_interior_parents)
    2204             :                 {
    2205         781 :                   element->set_interior_parent(this->elem_ptr(interior_parent_id));
    2206          22 :                   break;
    2207             :                 }
    2208             :             }
    2209             : 
    2210             :           // Do we have a mixed dimensional mesh that contains some of
    2211             :           // its own interior parents, but we already expect to have
    2212             :           // interior parents on a different mesh?  That's going to
    2213             :           // take some work to support if anyone needs it.
    2214         994 :           if (separate_interior_mesh)
    2215           0 :             libmesh_not_implemented_msg
    2216             :               ("interior_parent() values in multiple meshes are unsupported.");
    2217             :         }
    2218      113225 :     }
    2219             : 
    2220             :   // This flag doesn't necessarily mean any Elems actually have
    2221             :   // interior parent pointers, just that we did all the work to
    2222             :   // determine whether or not they do.
    2223        5396 :   _preparation.has_interior_parent_ptrs = true;
    2224             : }
    2225             : 
    2226             : 
    2227             : 
    2228             : #ifdef LIBMESH_ENABLE_PERIODIC
    2229             :   /**
    2230             :    * Register a pair of boundaries as disjoint neighbor boundary pairs.
    2231             :    */
    2232         994 :   void MeshBase::add_disjoint_neighbor_boundary_pairs(const boundary_id_type b1,
    2233             :                                              const boundary_id_type b2,
    2234             :                                              const RealVectorValue & translation)
    2235             :     {
    2236             :       // Lazily allocate the container the first time it’s needed
    2237         994 :       if (!_disjoint_neighbor_boundary_pairs)
    2238        1496 :         _disjoint_neighbor_boundary_pairs = std::make_unique<PeriodicBoundaries>();
    2239             : 
    2240          28 :       PeriodicBoundaries & db = *_disjoint_neighbor_boundary_pairs;
    2241             : 
    2242             :       // Create forward and inverse boundary mappings
    2243        1022 :       PeriodicBoundary forward(translation);
    2244         994 :       PeriodicBoundary inverse(translation * -1.0);
    2245             : 
    2246         994 :       forward.myboundary       = b1;
    2247         994 :       forward.pairedboundary   = b2;
    2248         994 :       inverse.myboundary       = b2;
    2249         994 :       inverse.pairedboundary   = b1;
    2250             : 
    2251             :       // Add both directions into the container
    2252         994 :       db.emplace(b1, forward.clone());
    2253        1022 :       db.emplace(b2, inverse.clone());
    2254         994 :     }
    2255             : 
    2256      561802 :   PeriodicBoundaries * MeshBase::get_disjoint_neighbor_boundary_pairs()
    2257             :     {
    2258      561802 :       return _disjoint_neighbor_boundary_pairs.get();
    2259             :     }
    2260             : 
    2261     2651037 :   const PeriodicBoundaries * MeshBase::get_disjoint_neighbor_boundary_pairs() const
    2262             :     {
    2263     2651037 :       return _disjoint_neighbor_boundary_pairs.get();
    2264             :     }
    2265             : 
    2266         213 :     void MeshBase::remove_disjoint_boundary_pair(const boundary_id_type b1,
    2267             :                                                    const boundary_id_type b2)
    2268             :     {
    2269             :       // Nothing to remove if not allocated or empty
    2270         213 :       if (!_disjoint_neighbor_boundary_pairs || _disjoint_neighbor_boundary_pairs->empty())
    2271           0 :         return;
    2272             : 
    2273           6 :       auto & pairs = *_disjoint_neighbor_boundary_pairs;
    2274             : 
    2275             :       // Helper to check and erase both directions
    2276         402 :       auto erase_if_match = [](boundary_id_type key,
    2277             :                               boundary_id_type pair,
    2278          24 :                               PeriodicBoundaries & pb_map)
    2279             :         {
    2280          12 :           auto it = pb_map.find(key);
    2281         426 :           if (it != pb_map.end())
    2282             :             {
    2283          12 :               const auto & pb = *(it->second);
    2284             :               // Check both directions
    2285         426 :               if ((pb.myboundary == key && pb.pairedboundary == pair) ||
    2286           0 :                   (pb.pairedboundary == key && pb.myboundary == pair))
    2287         414 :                 pb_map.erase(it);
    2288             :             }
    2289         426 :         };
    2290             : 
    2291         213 :       erase_if_match(b1, b2, pairs);
    2292         213 :       erase_if_match(b2, b1, pairs);
    2293             :     }
    2294             : 
    2295             : 
    2296             : #endif
    2297             : 
    2298             : 
    2299             : 
    2300           0 : void MeshBase::set_point_locator_close_to_point_tol(Real val)
    2301             : {
    2302           0 :   _point_locator_close_to_point_tol = val;
    2303           0 :   if (_point_locator)
    2304             :     {
    2305           0 :       if (val > 0.)
    2306           0 :         _point_locator->set_close_to_point_tol(val);
    2307             :       else
    2308           0 :         _point_locator->unset_close_to_point_tol();
    2309             :     }
    2310           0 : }
    2311             : 
    2312             : 
    2313             : 
    2314         426 : Real MeshBase::get_point_locator_close_to_point_tol() const
    2315             : {
    2316         426 :   return _point_locator_close_to_point_tol;
    2317             : }
    2318             : 
    2319             : 
    2320             : 
    2321        4909 : void MeshBase::size_elem_extra_integers()
    2322             : {
    2323         284 :   const std::size_t new_size = _elem_integer_names.size();
    2324             : 
    2325             :   Threads::parallel_for
    2326        4909 :     (this->element_stored_range(),
    2327       10156 :      [new_size, this](const ElemRange & range)
    2328             :      {
    2329       18214 :        for (Elem * elem : range)
    2330       13305 :          elem->add_extra_integers(new_size, this->_elem_integer_default_values);
    2331        4767 :      });
    2332        4909 : }
    2333             : 
    2334             : 
    2335             : 
    2336       16176 : void MeshBase::size_node_extra_integers()
    2337             : {
    2338         942 :   const std::size_t new_size = _node_integer_names.size();
    2339      356623 :   for (auto node : this->node_ptr_range())
    2340      184151 :     node->add_extra_integers(new_size, _node_integer_default_values);
    2341       16176 : }
    2342             : 
    2343             : 
    2344             : std::pair<std::vector<unsigned int>, std::vector<unsigned int>>
    2345       26167 : MeshBase::merge_extra_integer_names(const MeshBase & other)
    2346             : {
    2347         854 :   std::pair<std::vector<unsigned int>, std::vector<unsigned int>> returnval;
    2348       26167 :   returnval.first = this->add_elem_integers(other._elem_integer_names, true, &other._elem_integer_default_values);
    2349       26167 :   returnval.second = this->add_node_integers(other._node_integer_names, true, &other._node_integer_default_values);
    2350       26167 :   return returnval;
    2351             : }
    2352             : 
    2353             : 
    2354             : 
    2355             : void
    2356         426 : MeshBase::post_dofobject_moves(MeshBase && other_mesh)
    2357             : {
    2358             :   // Now that all the DofObject moving is done, we can move the GhostingFunctor objects
    2359             :   // which include the _default_ghosting,_ghosting_functors and _shared_functors. We also need
    2360             :   // to set the mesh object associated with these functors to the assignee mesh.
    2361             : 
    2362             :    // _default_ghosting
    2363          12 :   _default_ghosting = std::move(other_mesh._default_ghosting);
    2364         426 :   _default_ghosting->set_mesh(this);
    2365             : 
    2366             :   // _ghosting_functors
    2367         426 :   _ghosting_functors = std::move(other_mesh._ghosting_functors);
    2368             : 
    2369         852 :   for (const auto gf : _ghosting_functors )
    2370             :   {
    2371         426 :     gf->set_mesh(this);
    2372             :   }
    2373             : 
    2374             :   // _shared_functors
    2375          12 :   _shared_functors = std::move(other_mesh._shared_functors);
    2376             : 
    2377         426 :   for (const auto & sf : _shared_functors )
    2378             :   {
    2379           0 :     (sf.second)->set_mesh(this);
    2380             :   }
    2381             : 
    2382             :   // _constraint_rows
    2383          12 :   _constraint_rows = std::move(other_mesh._constraint_rows);
    2384             : 
    2385         426 :   if (other_mesh.partitioner())
    2386         426 :     _partitioner = std::move(other_mesh.partitioner());
    2387         426 : }
    2388             : 
    2389             : 
    2390             : void
    2391           0 : MeshBase::copy_cached_data(const MeshBase & other_mesh)
    2392             : {
    2393           0 :   this->_spatial_dimension = other_mesh._spatial_dimension;
    2394           0 :   this->_elem_dims = other_mesh._elem_dims;
    2395           0 :   this->_elem_default_orders = other_mesh._elem_default_orders;
    2396           0 :   this->_supported_nodal_order = other_mesh._supported_nodal_order;
    2397           0 :   this->_mesh_subdomains = other_mesh._mesh_subdomains;
    2398           0 : }
    2399             : 
    2400             : 
    2401       14725 : bool MeshBase::nodes_and_elements_equal(const MeshBase & other_mesh) const
    2402             : {
    2403     2826850 :   for (const auto & other_node : other_mesh.node_ptr_range())
    2404             :     {
    2405     1524181 :       const Node * node = this->query_node_ptr(other_node->id());
    2406     1524181 :       if (!node)
    2407           0 :         return false;
    2408     1524181 :       if (*other_node != *node)
    2409           0 :         return false;
    2410       13279 :     }
    2411     2826850 :   for (const auto & node : this->node_ptr_range())
    2412     1524181 :     if (!other_mesh.query_node_ptr(node->id()))
    2413       13279 :       return false;
    2414             : 
    2415     1253356 :   for (const auto & other_elem : other_mesh.element_ptr_range())
    2416             :     {
    2417      789217 :       const Elem * elem = this->query_elem_ptr(other_elem->id());
    2418      789217 :       if (!elem)
    2419           0 :         return false;
    2420      789217 :       if (!other_elem->topologically_equal(*elem))
    2421           6 :         return false;
    2422       13279 :     }
    2423     1253080 :   for (const auto & elem : this->element_ptr_range())
    2424      789076 :     if (!other_mesh.query_elem_ptr(elem->id()))
    2425       13150 :       return false;
    2426             : 
    2427       14584 :   return true;
    2428             : }
    2429             : 
    2430             : 
    2431         764 : dof_id_type MeshBase::n_constraint_rows() const
    2432             : {
    2433         764 :   dof_id_type n_local_rows=0, n_unpartitioned_rows=0;
    2434         764 :   for (const auto & [node, node_constraints] : _constraint_rows)
    2435             :     {
    2436             :       // Unpartitioned nodes
    2437           0 :       if (node->processor_id() == DofObject::invalid_processor_id)
    2438           0 :         n_unpartitioned_rows++;
    2439           0 :       else if (node->processor_id() == this->processor_id())
    2440           0 :         n_local_rows++;
    2441             :     }
    2442             : 
    2443         764 :   this->comm().sum(n_local_rows);
    2444             : 
    2445         764 :   return n_unpartitioned_rows + n_local_rows;
    2446             : }
    2447             : 
    2448             : 
    2449             : void
    2450       24392 : MeshBase::copy_constraint_rows(const MeshBase & other_mesh)
    2451             : {
    2452        1608 :   LOG_SCOPE("copy_constraint_rows(mesh)", "MeshBase");
    2453             : 
    2454        1592 :   _constraint_rows.clear();
    2455             : 
    2456         804 :   const auto & other_constraint_rows = other_mesh.get_constraint_rows();
    2457       72820 :   for (const auto & [other_node, other_node_constraints] : other_constraint_rows)
    2458             :   {
    2459       48428 :     const Node * const our_node = this->node_ptr(other_node->id());
    2460        6416 :     constraint_rows_mapped_type our_node_constraints;
    2461      219637 :     for (const auto & [other_inner_key_pair, constraint_value] : other_node_constraints)
    2462             :     {
    2463        9048 :       const auto & [other_elem, local_node_id] = other_inner_key_pair;
    2464      171209 :       const Elem * const our_elem = this->elem_ptr(other_elem->id());
    2465      171209 :       our_node_constraints.emplace_back(std::make_pair(our_elem, local_node_id), constraint_value);
    2466             :     }
    2467       48428 :     _constraint_rows[our_node] = std::move(our_node_constraints);
    2468             :   }
    2469       24392 : }
    2470             : 
    2471             : 
    2472             : template <typename T>
    2473             : void
    2474         565 : MeshBase::copy_constraint_rows(const SparseMatrix<T> & constraint_operator,
    2475             :                                bool precondition_constraint_operator)
    2476             : {
    2477          32 :   LOG_SCOPE("copy_constraint_rows(mat)", "MeshBase");
    2478             : 
    2479          16 :   this->_constraint_rows.clear();
    2480             : 
    2481             :   // We're not going to support doing this distributed yet; it'd be
    2482             :   // pointless unless we temporarily had a linear partitioning to
    2483             :   // better match the constraint operator.
    2484         597 :   MeshSerializer serialize(*this);
    2485             : 
    2486             :   // Our current mesh should already reflect the desired assembly space
    2487         565 :   libmesh_error_msg_if(this->n_nodes() != constraint_operator.m(),
    2488             :                        "Constraint operator matrix with " <<
    2489             :                        constraint_operator.m() <<
    2490             :                        "rows does not match this mesh with " <<
    2491             :                        this->n_nodes() << " nodes");
    2492             : 
    2493             :   // First, find what new unconstrained DoFs we need to add.  We can't
    2494             :   // iterate over columns in a SparseMatrix, so we'll iterate over
    2495             :   // rows and keep track of columns.
    2496             : 
    2497             :   // If we have nodes that will work unconstrained, keep track of
    2498             :   // their node ids and corresponding column indices.
    2499             :   // existing_unconstrained_nodes[column_id] = node_id
    2500          32 :   std::map<dof_id_type, dof_id_type> existing_unconstrained_columns;
    2501          32 :   std::set<dof_id_type> existing_unconstrained_nodes;
    2502             : 
    2503             :   // In case we need new nodes, keep track of their columns.
    2504             :   // columns[j][k] will be the kth row index and value of column j
    2505             :   typedef
    2506             :     std::unordered_map<dof_id_type,
    2507             :                        std::vector<std::pair<dof_id_type, Real>>>
    2508             :     columns_type;
    2509        1114 :   columns_type columns(constraint_operator.n());
    2510             : 
    2511             :   // If we need to precondition the constraint operator (e.g.  it's an
    2512             :   // unpreconditioned extraction operator for a Flex IGA matrix),
    2513             :   // we'll want to keep track of the sum of each column, because we'll
    2514             :   // be dividing each column by that sum (Jacobi preconditioning on
    2515             :   // the right, which then leads to symmetric preconditioning on a
    2516             :   // physics Jacobian).
    2517          32 :   std::unordered_map<dof_id_type, Real> column_sums;
    2518             : 
    2519             :   // Work in parallel, though we'll have to sync shortly
    2520        4028 :   for (auto i : make_range(constraint_operator.row_start(),
    2521         533 :                            constraint_operator.row_stop()))
    2522             :     {
    2523         618 :       std::vector<numeric_index_type> indices;
    2524         618 :       std::vector<T> values;
    2525             : 
    2526        3463 :       constraint_operator.get_row(i, indices, values);
    2527         309 :       libmesh_assert_equal_to(indices.size(), values.size());
    2528             : 
    2529        3847 :       if (indices.size() == 1 &&
    2530         825 :           values[0] == T(1))
    2531             :         {
    2532             :           // If we have multiple simple Ui=Uj constraints, let the
    2533             :           // first one be our "unconstrained" node and let the others
    2534             :           // be constrained to it.
    2535         810 :           if (existing_unconstrained_columns.find(indices[0]) !=
    2536         138 :               existing_unconstrained_columns.end())
    2537             :             {
    2538          36 :               const auto j = indices[0];
    2539          36 :               columns[j].emplace_back(i, 1);
    2540             :             }
    2541             :           else
    2542             :             {
    2543         708 :               existing_unconstrained_nodes.insert(i);
    2544         774 :               existing_unconstrained_columns.emplace(indices[0],i);
    2545             :             }
    2546             :         }
    2547             :       else
    2548       22028 :         for (auto jj : index_range(indices))
    2549             :           {
    2550       19375 :             const auto j = indices[jj];
    2551       21134 :             const Real coef = libmesh_real(values[jj]);
    2552        1759 :             libmesh_assert_equal_to(coef, values[jj]);
    2553       19375 :             columns[j].emplace_back(i, coef);
    2554             :           }
    2555             :     }
    2556             : 
    2557             :   // Merge data from different processors' slabs of the matrix
    2558         565 :   this->comm().set_union(existing_unconstrained_nodes);
    2559         565 :   this->comm().set_union(existing_unconstrained_columns);
    2560             : 
    2561          48 :   std::vector<columns_type> all_columns;
    2562         565 :   this->comm().allgather(columns, all_columns);
    2563             : 
    2564          16 :   columns.clear();
    2565        6106 :   for (auto p : index_range(all_columns))
    2566       65601 :     for (auto & [j, subcol] : all_columns[p])
    2567      183429 :       for (auto [i, v] : subcol)
    2568      123369 :         columns[j].emplace_back(i,v);
    2569             : 
    2570             :   // Keep track of elements on which unconstrained nodes exist, and
    2571             :   // their local node indices.
    2572             :   // node_to_elem_ptrs[node] = [elem_id, local_node_num]
    2573          32 :   std::unordered_map<const Node *, std::pair<dof_id_type, unsigned int>> node_to_elem_ptrs;
    2574             : 
    2575             :   // Find elements attached to any existing nodes that will stay
    2576             :   // unconstrained.  We'll also build a subdomain set here so we don't
    2577             :   // have to assert that the mesh is already prepared before we pick a
    2578             :   // new subdomain for any NodeElems we need to add.
    2579          32 :   std::set<subdomain_id_type> subdomain_ids;
    2580      145181 :   for (const Elem * elem : this->element_ptr_range())
    2581             :     {
    2582       49738 :       subdomain_ids.insert(elem->subdomain_id());
    2583      244572 :       for (auto n : make_range(elem->n_nodes()))
    2584             :         {
    2585      194834 :           const Node * node = elem->node_ptr(n);
    2586      194834 :           if (existing_unconstrained_nodes.count(node->id()))
    2587       12175 :             node_to_elem_ptrs.emplace(node, std::make_pair(elem->id(), n));
    2588             :         }
    2589             :     }
    2590             : 
    2591         565 :   const subdomain_id_type new_sbd_id = *subdomain_ids.rbegin() + 1;
    2592             : 
    2593       23189 :   for (auto j : make_range(constraint_operator.n()))
    2594             :     {
    2595             :       // If we already have a good node for this then we're done
    2596        4800 :       if (existing_unconstrained_columns.count(j))
    2597        4668 :         continue;
    2598             : 
    2599             :       // Get a half-decent spot to place a new NodeElem for
    2600             :       // unconstrained DoF(s) here.  Getting a *fully*-decent spot
    2601             :       // would require finding a Moore-Penrose pseudoinverse, and I'm
    2602             :       // not going to do that, but scaling a transpose will at least
    2603             :       // get us a little uniqueness to make visualization reasonable.
    2604         264 :       Point newpt;
    2605         264 :       Real total_scaling = 0;
    2606         264 :       unsigned int total_entries = 0;
    2607             : 
    2608             :       // We'll get a decent initial pid choice here too, if only to
    2609             :       // aid in later repartitioning.
    2610         528 :       std::map<processor_id_type, int> pids;
    2611             : 
    2612         264 :       auto & column = columns[j];
    2613      122230 :       for (auto [i, r] : column)
    2614             :         {
    2615      112988 :           Node & constrained_node = this->node_ref(i);
    2616      112988 :           const Point constrained_pt = constrained_node;
    2617        3228 :           newpt += r*constrained_pt;
    2618      112988 :           total_scaling += r;
    2619      112988 :           ++total_entries;
    2620      112988 :           ++pids[constrained_node.processor_id()];
    2621             :         }
    2622             : 
    2623        9242 :       if (precondition_constraint_operator)
    2624           0 :         column_sums[j] = total_scaling;
    2625             : 
    2626        9242 :       libmesh_error_msg_if
    2627             :         (!total_entries,
    2628             :          "Empty column " << j <<
    2629             :          " found in constraint operator matrix");
    2630             : 
    2631             :       // If we have *cancellation* here then we can end up dividing by
    2632             :       // zero; try just evenly scaling across all constrained node
    2633             :       // points instead.
    2634        9242 :       if (total_scaling > TOLERANCE)
    2635         264 :         newpt /= total_scaling;
    2636             :       else
    2637           0 :         newpt /= total_entries;
    2638             : 
    2639        9242 :       Node *n = this->add_point(newpt);
    2640        9506 :       std::unique_ptr<Elem> elem = Elem::build(NODEELEM);
    2641        9242 :       elem->set_node(0, n);
    2642        9242 :       elem->subdomain_id() = new_sbd_id;
    2643             : 
    2644        9506 :       Elem * added_elem = this->add_elem(std::move(elem));
    2645        9242 :       this->_elem_dims.insert(0);
    2646        9242 :       this->_elem_default_orders.insert(added_elem->default_order());
    2647        9242 :       this->_supported_nodal_order =
    2648        8714 :         static_cast<Order>
    2649       18484 :           (std::min(static_cast<int>(this->_supported_nodal_order),
    2650        9242 :                     static_cast<int>(added_elem->supported_nodal_order())));
    2651        8978 :       this->_mesh_subdomains.insert(new_sbd_id);
    2652        9242 :       node_to_elem_ptrs.emplace(n, std::make_pair(added_elem->id(), 0));
    2653        9242 :       existing_unconstrained_columns.emplace(j,n->id());
    2654             : 
    2655             :       // Repartition the new objects *after* adding them, so a
    2656             :       // DistributedMesh doesn't get confused and think you're not
    2657             :       // adding them on all processors at once.
    2658         264 :       int n_pids = 0;
    2659       43912 :       for (auto [pid, count] : pids)
    2660       34670 :         if (count >= n_pids)
    2661             :           {
    2662         324 :             n_pids = count;
    2663       15469 :             added_elem->processor_id() = pid;
    2664       15469 :             n->processor_id() = pid;
    2665             :           }
    2666             :     }
    2667             : 
    2668             :   // Calculate constraint rows in an indexed form that's easy for us
    2669             :   // to allgather
    2670             :   std::unordered_map<dof_id_type,
    2671             :     std::vector<std::pair<std::pair<dof_id_type, unsigned int>,Real>>>
    2672          32 :     indexed_constraint_rows;
    2673             : 
    2674        4028 :   for (auto i : make_range(constraint_operator.row_start(),
    2675         533 :                            constraint_operator.row_stop()))
    2676             :     {
    2677         951 :       if (existing_unconstrained_nodes.count(i))
    2678         774 :         continue;
    2679             : 
    2680         486 :       std::vector<numeric_index_type> indices;
    2681         486 :       std::vector<T> values;
    2682             : 
    2683        2689 :       constraint_operator.get_row(i, indices, values);
    2684             : 
    2685         486 :       std::vector<std::pair<std::pair<dof_id_type, unsigned int>, Real>> constraint_row;
    2686             : 
    2687       22100 :       for (auto jj : index_range(indices))
    2688             :         {
    2689       21173 :           const dof_id_type node_id =
    2690             :             existing_unconstrained_columns[indices[jj]];
    2691             : 
    2692       19411 :           Node & constraining_node = this->node_ref(node_id);
    2693             : 
    2694        1762 :           libmesh_assert(node_to_elem_ptrs.count(&constraining_node));
    2695             : 
    2696       19411 :           auto p = node_to_elem_ptrs[&constraining_node];
    2697             : 
    2698       19382 :           Real coef = libmesh_real(values[jj]);
    2699        1762 :           libmesh_assert_equal_to(coef, values[jj]);
    2700             : 
    2701             :           // If we're preconditioning and we created a nodeelem then
    2702             :           // we can scale the meaning of that nodeelem's value to give
    2703             :           // us a better-conditioned matrix after the constraints are
    2704             :           // applied.
    2705       19411 :           if (precondition_constraint_operator)
    2706           0 :             if (auto sum_it = column_sums.find(indices[jj]);
    2707           0 :                 sum_it != column_sums.end())
    2708             :               {
    2709           0 :                 const Real scaling = sum_it->second;
    2710             : 
    2711           0 :                 if (scaling > TOLERANCE)
    2712           0 :                   coef /= scaling;
    2713             :               }
    2714             : 
    2715       21173 :           constraint_row.emplace_back(std::make_pair(p, coef));
    2716             :         }
    2717             : 
    2718         243 :       indexed_constraint_rows.emplace(i, std::move(constraint_row));
    2719             :     }
    2720             : 
    2721         565 :   this->comm().set_union(indexed_constraint_rows);
    2722             : 
    2723             :   // Add constraint rows as mesh constraint rows
    2724       17591 :   for (auto & [node_id, indexed_row] : indexed_constraint_rows)
    2725             :     {
    2726       17026 :       Node * constrained_node = this->node_ptr(node_id);
    2727             : 
    2728         972 :       constraint_rows_mapped_type constraint_row;
    2729             : 
    2730      140395 :       for (auto [p, coef] : indexed_row)
    2731             :         {
    2732      123369 :           const Elem * elem = this->elem_ptr(p.first);
    2733        7048 :           constraint_row.emplace_back
    2734      126893 :             (std::make_pair(std::make_pair(elem, p.second), coef));
    2735             :         }
    2736             : 
    2737       16540 :       this->_constraint_rows.emplace(constrained_node,
    2738         486 :                                      std::move(constraint_row));
    2739             :     }
    2740        1098 : }
    2741             : 
    2742             : 
    2743           0 : void MeshBase::print_constraint_rows(std::ostream & os,
    2744             :                                      bool print_nonlocal) const
    2745             : {
    2746           0 :   parallel_object_only();
    2747             : 
    2748             :   std::string local_constraints =
    2749           0 :     this->get_local_constraints(print_nonlocal);
    2750             : 
    2751           0 :   if (this->processor_id())
    2752             :     {
    2753           0 :       this->comm().send(0, local_constraints);
    2754             :     }
    2755             :   else
    2756             :     {
    2757           0 :       os << "Processor 0:\n";
    2758           0 :       os << local_constraints;
    2759             : 
    2760           0 :       for (auto p : IntRange<processor_id_type>(1, this->n_processors()))
    2761             :         {
    2762           0 :           this->comm().receive(p, local_constraints);
    2763           0 :           os << "Processor " << p << ":\n";
    2764           0 :           os << local_constraints;
    2765             :         }
    2766             :     }
    2767           0 : }
    2768             : 
    2769             : 
    2770             : 
    2771           0 : std::string MeshBase::get_local_constraints(bool print_nonlocal) const
    2772             : {
    2773           0 :   std::ostringstream os;
    2774             : 
    2775           0 :   if (print_nonlocal)
    2776           0 :     os << "All ";
    2777             :   else
    2778           0 :     os << "Local ";
    2779             : 
    2780           0 :   os << "Mesh Constraint Rows:"
    2781           0 :      << std::endl;
    2782             : 
    2783           0 :   for (const auto & [node, row] : _constraint_rows)
    2784             :     {
    2785           0 :       const bool local = (node->processor_id() == this->processor_id());
    2786             : 
    2787             :       // Skip non-local dofs if requested
    2788           0 :       if (!print_nonlocal && !local)
    2789           0 :         continue;
    2790             : 
    2791           0 :       os << "Constraints for " << (local ? "Local" : "Ghost") << " Node " << node->id()
    2792           0 :          << ": \t";
    2793             : 
    2794           0 :       for (const auto & [elem_and_node, coef] : row)
    2795           0 :         os << " ((" << elem_and_node.first->id() << ',' << elem_and_node.second << "), " << coef << ")\t";
    2796             : 
    2797           0 :       os << std::endl;
    2798             :     }
    2799             : 
    2800           0 :   return os.str();
    2801           0 : }
    2802             : 
    2803      331691 : MeshBase::Preparation::Preparation() :
    2804      312431 :   is_partitioned(false),
    2805      312431 :   has_synched_id_counts(false),
    2806      312431 :   has_neighbor_ptrs(false),
    2807      312431 :   has_cached_elem_data(false),
    2808      312431 :   has_interior_parent_ptrs(false),
    2809      312431 :   has_removed_remote_elements(false),
    2810      312431 :   has_removed_orphaned_nodes(false),
    2811      312431 :   has_boundary_id_sets(false),
    2812      331691 :   has_reinit_ghosting_functors(false)
    2813      331691 : {}
    2814             : 
    2815      102026 : MeshBase::Preparation::operator bool() const
    2816             : {
    2817      174692 :   return is_partitioned &&
    2818       72666 :          has_synched_id_counts &&
    2819       72666 :          has_neighbor_ptrs &&
    2820       72666 :          has_cached_elem_data &&
    2821       72666 :          has_interior_parent_ptrs &&
    2822       72666 :          has_removed_remote_elements &&
    2823       72666 :          has_removed_orphaned_nodes &&
    2824      215828 :          has_reinit_ghosting_functors &&
    2825      114668 :          has_boundary_id_sets;
    2826             : }
    2827             : 
    2828             : MeshBase::Preparation &
    2829     1448185 : MeshBase::Preparation::operator= (bool set_all)
    2830             : {
    2831     1448185 :   is_partitioned = set_all;
    2832     1448185 :   has_synched_id_counts = set_all;
    2833     1448185 :   has_neighbor_ptrs = set_all;
    2834     1448185 :   has_cached_elem_data = set_all;
    2835     1448185 :   has_interior_parent_ptrs = set_all;
    2836     1448185 :   has_removed_remote_elements = set_all;
    2837     1448185 :   has_removed_orphaned_nodes = set_all;
    2838     1448185 :   has_reinit_ghosting_functors = set_all;
    2839     1448185 :   has_boundary_id_sets = set_all;
    2840             : 
    2841     1448185 :   return *this;
    2842             : }
    2843             : 
    2844             : bool
    2845       15151 : MeshBase::Preparation::operator== (const Preparation & other) const
    2846             : {
    2847       15151 :   if (is_partitioned != other.is_partitioned)
    2848           0 :     return false;
    2849       15151 :   if (has_synched_id_counts != other.has_synched_id_counts)
    2850           0 :     return false;
    2851       15151 :   if (has_neighbor_ptrs != other.has_neighbor_ptrs)
    2852           0 :     return false;
    2853       15151 :   if (has_cached_elem_data != other.has_cached_elem_data)
    2854           0 :     return false;
    2855       15151 :   if (has_interior_parent_ptrs != other.has_interior_parent_ptrs)
    2856           0 :     return false;
    2857       15151 :   if (has_removed_remote_elements != other.has_removed_remote_elements)
    2858           0 :     return false;
    2859       15151 :   if (has_removed_orphaned_nodes != other.has_removed_orphaned_nodes)
    2860           0 :     return false;
    2861       15151 :   if (has_reinit_ghosting_functors != other.has_reinit_ghosting_functors)
    2862           0 :     return false;
    2863       15151 :   if (has_boundary_id_sets != other.has_boundary_id_sets)
    2864           0 :     return false;
    2865             : 
    2866        1062 :   return true;
    2867             : }
    2868             : 
    2869             : bool
    2870       15151 : MeshBase::Preparation::operator!= (const Preparation & other) const
    2871             : {
    2872       15151 :   return !(*this == other);
    2873             : }
    2874             : 
    2875             : 
    2876             : // Explicit instantiations for our template function
    2877             : template LIBMESH_EXPORT void
    2878             : MeshBase::copy_constraint_rows(const SparseMatrix<Real> & constraint_operator,
    2879             :                                bool precondition_constraint_operator);
    2880             : 
    2881             : #ifdef LIBMESH_USE_COMPLEX_NUMBERS
    2882             : template LIBMESH_EXPORT void
    2883             : MeshBase::copy_constraint_rows(const SparseMatrix<Complex> & constraint_operator,
    2884             :                                bool precondition_constraint_operator);
    2885             : #endif
    2886             : 
    2887             : 
    2888             : } // namespace libMesh

Generated by: LCOV version 1.14