LCOV - code coverage report
Current view: top level - src/mesh - mesh_base.C (source / functions) Hit Total Coverage
Test: libMesh/libmesh: #4485 (d48e80) with base 862243 Lines: 1170 1384 84.5 %
Date: 2026-06-17 01:59:26 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      332543 : MeshBase::MeshBase (const Parallel::Communicator & comm_in,
      65      332543 :                     unsigned char d) :
      66             :   ParallelObject (comm_in),
      67      332543 :   boundary_info  (new BoundaryInfo(*this)), // BoundaryInfo has protected ctor, can't use std::make_unique
      68      313235 :   _n_parts       (1),
      69      313235 :   _default_mapping_type(LAGRANGE_MAP),
      70      313235 :   _default_mapping_data(0),
      71      313235 :   _preparation (),
      72      313235 :   _element_stored_range (),
      73      313235 :   _const_active_local_element_stored_range (),
      74      313235 :   _point_locator (),
      75      313235 :   _count_lower_dim_elems_in_point_locator(true),
      76      313235 :   _partitioner   (),
      77             : #ifdef LIBMESH_ENABLE_UNIQUE_ID
      78      313235 :   _next_unique_id(DofObject::invalid_unique_id),
      79             : #endif
      80      313235 :   _interior_mesh(this),
      81      313235 :   _skip_noncritical_partitioning(false),
      82      342197 :   _skip_all_partitioning(libMesh::on_command_line("--skip-partitioning")),
      83      313235 :   _skip_renumber_nodes_and_elements(false),
      84      313235 :   _skip_find_neighbors(false),
      85      313235 :   _skip_detect_interior_parents(false),
      86      313235 :   _allow_remote_element_removal(true),
      87      313235 :   _allow_node_and_elem_unique_id_overlap(false),
      88      313235 :   _spatial_dimension(d),
      89      332543 :   _default_ghosting(std::make_unique<GhostPointNeighbors>(*this)),
      90     1045899 :   _point_locator_close_to_point_tol(0.)
      91             : {
      92      322889 :   _elem_dims.insert(d);
      93      332543 :   _ghosting_functors.push_back(_default_ghosting.get());
      94        9654 :   libmesh_assert_less_equal (LIBMESH_DIM, 3);
      95        9654 :   libmesh_assert_greater_equal (LIBMESH_DIM, d);
      96        9654 :   libmesh_assert (libMesh::initialized());
      97      332543 : }
      98             : 
      99             : 
     100             : 
     101       24405 : MeshBase::MeshBase (const MeshBase & other_mesh) :
     102             :   ParallelObject (other_mesh),
     103       24405 :   boundary_info  (new BoundaryInfo(*this)), // BoundaryInfo has protected ctor, can't use std::make_unique
     104       24405 :   _n_parts       (other_mesh._n_parts),
     105       24405 :   _default_mapping_type(other_mesh._default_mapping_type),
     106       24405 :   _default_mapping_data(other_mesh._default_mapping_data),
     107       22813 :   _preparation (other_mesh._preparation),
     108       22813 :   _element_stored_range (),
     109       22813 :   _const_active_local_element_stored_range (),
     110       22813 :   _point_locator (),
     111       24405 :   _count_lower_dim_elems_in_point_locator(other_mesh._count_lower_dim_elems_in_point_locator),
     112       22813 :   _partitioner   (),
     113             : #ifdef LIBMESH_ENABLE_UNIQUE_ID
     114       24405 :   _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       24405 :   _interior_mesh((other_mesh._interior_mesh == &other_mesh) ?
     119             :                  this : other_mesh._interior_mesh),
     120       24405 :   _skip_noncritical_partitioning(other_mesh._skip_noncritical_partitioning),
     121       24405 :   _skip_all_partitioning(other_mesh._skip_all_partitioning),
     122       24405 :   _skip_renumber_nodes_and_elements(other_mesh._skip_renumber_nodes_and_elements),
     123       24405 :   _skip_find_neighbors(other_mesh._skip_find_neighbors),
     124       24405 :   _skip_detect_interior_parents(other_mesh._skip_detect_interior_parents),
     125       24405 :   _allow_remote_element_removal(other_mesh._allow_remote_element_removal),
     126       24405 :   _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       24405 :   _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       24405 :   _spatial_dimension(other_mesh._spatial_dimension),
     134       24405 :   _default_ghosting(std::make_unique<GhostPointNeighbors>(*this)),
     135       99984 :   _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       79546 :   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       55141 :       if (gf == other_default_ghosting)
     144             :         {
     145       24405 :           _ghosting_functors.push_back(_default_ghosting.get());
     146       24405 :           continue;
     147             :         }
     148             : 
     149       60456 :       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       30736 :       if (clone_gf)
     157             :         {
     158       30736 :           clone_gf->set_mesh(this);
     159       60456 :           add_ghosting_functor(clone_gf);
     160             :         }
     161             :       else
     162             :         {
     163             :           libmesh_deprecated();
     164           0 :           add_ghosting_functor(*gf);
     165             :         }
     166             :     }
     167             : 
     168       24405 :   if (other_mesh._partitioner.get())
     169       48006 :     _partitioner = other_mesh._partitioner->clone();
     170             : 
     171             : #ifdef LIBMESH_ENABLE_PERIODIC
     172             :   // Deep copy of all periodic boundaries
     173       24405 :   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       24405 :   for (const auto & [set, code] : _elemset_codes_inverse_map)
     186           0 :     _elemset_codes.emplace(code, &set);
     187       24405 : }
     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      367390 : MeshBase::~MeshBase()
     422             : {
     423      356948 :   this->MeshBase::clear();
     424             : 
     425       10458 :   libmesh_exceptionless_assert (!libMesh::closed());
     426     1365092 : }
     427             : 
     428             : 
     429             : 
     430    10921244 : unsigned int MeshBase::mesh_dimension() const
     431             : {
     432    10921244 :   if (!_elem_dims.empty())
     433    10921244 :     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        4716 : unsigned int MeshBase::add_elem_integer(std::string name,
     624             :                                         bool allocate_data,
     625             :                                         dof_id_type default_value)
     626             : {
     627        5912 :   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         136 :   libmesh_assert_equal_to(_elem_integer_names.size(),
     636             :                           _elem_integer_default_values.size());
     637        4432 :   _elem_integer_names.push_back(std::move(name));
     638        4432 :   _elem_integer_default_values.push_back(default_value);
     639        4432 :   if (allocate_data)
     640        3844 :     this->size_elem_extra_integers();
     641        4568 :   return _elem_integer_names.size()-1;
     642             : }
     643             : 
     644             : 
     645             : 
     646       38210 : 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       39062 :   for (auto i : index_range(_elem_integer_names))
     655         852 :     name_indices[_elem_integer_names[i]] = i;
     656             : 
     657       39402 :   std::vector<unsigned int> returnval(names.size());
     658             : 
     659        1208 :   bool added_an_integer = false;
     660       40715 :   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       38210 :   if (allocate_data && added_an_integer)
     682        1065 :     this->size_elem_extra_integers();
     683             : 
     684       39418 :   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       16916 : unsigned int MeshBase::add_node_integer(std::string name,
     713             :                                         bool allocate_data,
     714             :                                         dof_id_type default_value)
     715             : {
     716       27329 :   for (auto i : index_range(_node_integer_names))
     717       10208 :     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         702 :   libmesh_assert_equal_to(_node_integer_names.size(),
     725             :                           _node_integer_default_values.size());
     726       16348 :   _node_integer_names.push_back(std::move(name));
     727       16348 :   _node_integer_default_values.push_back(default_value);
     728       16348 :   if (allocate_data)
     729        7120 :     this->size_node_extra_integers();
     730       17050 :   return _node_integer_names.size()-1;
     731             : }
     732             : 
     733             : 
     734             : 
     735       38068 : 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       38636 :   for (auto i : index_range(_node_integer_names))
     744         568 :     name_indices[_node_integer_names[i]] = i;
     745             : 
     746       39256 :   std::vector<unsigned int> returnval(names.size());
     747             : 
     748        1204 :   bool added_an_integer = false;
     749       40700 :   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       38068 :   if (allocate_data && added_an_integer)
     771        1010 :     this->size_node_extra_integers();
     772             : 
     773       39272 :   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      435229 : 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      435229 :   this->clear_point_locator();
     865      435229 :   this->clear_stored_ranges();
     866      435229 :   _preparation = false;
     867      435229 :   _preparation.has_neighbor_ptrs = _skip_find_neighbors;
     868      435229 :   _preparation.has_removed_remote_elements = !_allow_remote_element_removal;
     869             : 
     870      435229 :   this->complete_preparation();
     871      435229 : }
     872             : 
     873             : 
     874      438282 : void MeshBase::complete_preparation()
     875             : {
     876       26208 :   LOG_SCOPE("complete_preparation()", "MeshBase");
     877             : 
     878       13104 :   parallel_object_only();
     879             : 
     880       13104 :   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       13104 :   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      438282 :   if (!_skip_renumber_nodes_and_elements)
     911             :     {
     912      393563 :       if (!_preparation.has_removed_orphaned_nodes ||
     913         426 :           !_preparation.has_synched_id_counts)
     914      393137 :         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      438282 :   if (!_skip_find_neighbors && !_preparation.has_neighbor_ptrs)
     926      423693 :     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       13104 :   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      438282 :   if (!_preparation.has_cached_elem_data)
     939      435442 :     this->cache_elem_data();
     940             : 
     941             :   // libMesh expects every processor to know about every subdomain
     942             :   // name, but distributed mesh generators may only have set names for
     943             :   // the part of the mesh they know about, so make sure the map is
     944             :   // consistent everywhere.
     945      438282 :   if (!_preparation.has_synched_subdomain_name_map)
     946      435229 :     this->sync_subdomain_name_map();
     947             : 
     948             :   // Search the mesh for elements that have a neighboring element
     949             :   // of dim+1 and set that element as the interior parent
     950      438282 :   if (!_preparation.has_interior_parent_ptrs)
     951             :     {
     952      435229 :       if (!_skip_detect_interior_parents)
     953      435229 :         this->detect_interior_parents();
     954             :       else
     955             :         {
     956             :           // We must set the flag that says "interior parent pointers have been set up"
     957             :           // even though we skip detect_interior_parents().
     958           0 :           _preparation.has_interior_parent_ptrs = true;
     959             :         }
     960             :     }
     961             : 
     962             :   // Fix up node unique ids in case mesh generation code didn't take
     963             :   // exceptional care to do so.
     964             :   //  MeshCommunication().make_node_unique_ids_parallel_consistent(*this);
     965             : 
     966             :   // We're going to still require that mesh generation code gets
     967             :   // element unique ids consistent.
     968             : #if defined(DEBUG) && defined(LIBMESH_ENABLE_UNIQUE_ID)
     969       13104 :   MeshTools::libmesh_assert_valid_unique_ids(*this);
     970             : #endif
     971             : 
     972             :   // Allow our GhostingFunctor objects to reinit if necessary.
     973             :   // Do this before partitioning and redistributing, and before
     974             :   // deleting remote elements.
     975      438282 :   if (!_preparation.has_reinit_ghosting_functors)
     976      435229 :     this->reinit_ghosting_functors();
     977             : 
     978             :   // Partition the mesh unless *all* partitioning is to be skipped.
     979             :   // If only noncritical partitioning is to be skipped, the
     980             :   // partition() call will still check for orphaned nodes.
     981      438282 :   if (!skip_partitioning() && !_preparation.is_partitioned)
     982       12908 :     this->partition();
     983       12806 :   else if (!this->n_unpartitioned_elem() &&
     984         196 :            !this->n_unpartitioned_nodes())
     985        6403 :     _preparation.is_partitioned = true;
     986             : 
     987             :   // If we're using DistributedMesh, we'll probably want it
     988             :   // parallelized.
     989      438282 :   if (this->_allow_remote_element_removal &&
     990      426198 :       !_preparation.has_removed_remote_elements)
     991      381078 :     this->delete_remote_elements();
     992             :   else
     993       57204 :     _preparation.has_removed_remote_elements = true;
     994             : 
     995             :   // Much of our boundary info may have been for now-remote parts of the mesh,
     996             :   // in which case we don't want to keep local copies of data meant to be
     997             :   // local. On the other hand we may have deleted, or the user may have added in
     998             :   // a distributed fashion, boundary data that is meant to be global. So we
     999             :   // handle both of those scenarios here
    1000      438282 :   if (!_preparation.has_boundary_id_sets)
    1001       89270 :     this->get_boundary_info().regenerate_id_sets();
    1002             : 
    1003      438282 :   if (!_skip_renumber_nodes_and_elements)
    1004      393563 :     this->renumber_nodes_and_elements();
    1005             : 
    1006             :   // The mesh is now prepared for use, with the possible exception of
    1007             :   // partitioning that was supposed to be skipped, and it should know
    1008             :   // it.
    1009             : #ifndef NDEBUG
    1010       13104 :   Preparation completed_preparation = _preparation;
    1011       13104 :   if (skip_partitioning())
    1012         184 :     completed_preparation.is_partitioned = true;
    1013       13104 :   libmesh_assert(completed_preparation);
    1014             : #endif
    1015             : 
    1016             : #ifdef DEBUG
    1017       13104 :   MeshTools::libmesh_assert_valid_boundary_ids(*this);
    1018             : #ifdef LIBMESH_ENABLE_UNIQUE_ID
    1019       13104 :   MeshTools::libmesh_assert_valid_unique_ids(*this);
    1020             : #endif
    1021             : #endif
    1022      438282 : }
    1023             : 
    1024             : void
    1025      435229 : MeshBase::reinit_ghosting_functors()
    1026             : {
    1027      980608 :   for (auto & gf : _ghosting_functors)
    1028             :     {
    1029       16562 :       libmesh_assert(gf);
    1030      545379 :       gf->mesh_reinit();
    1031             :     }
    1032             : 
    1033      435229 :   _preparation.has_reinit_ghosting_functors = true;
    1034      435229 : }
    1035             : 
    1036     1016590 : void MeshBase::clear ()
    1037             : {
    1038             :   // Reset the number of partitions
    1039     1016590 :   _n_parts = 1;
    1040             : 
    1041             :   // Reset the preparation flags
    1042     1016590 :   _preparation = false;
    1043             : 
    1044             :   // Clear boundary information
    1045     1016590 :   if (boundary_info)
    1046     1015312 :     boundary_info->clear();
    1047             : 
    1048             :   // Clear cached element data
    1049       30381 :   _elem_dims.clear();
    1050       30381 :   _elem_default_orders.clear();
    1051     1016590 :   _supported_nodal_order = MAXIMUM;
    1052             : 
    1053       30381 :   _elemset_codes.clear();
    1054       30381 :   _elemset_codes_inverse_map.clear();
    1055             : 
    1056       30381 :   _constraint_rows.clear();
    1057             : 
    1058             :   // Clear our point locator.
    1059     1016590 :   this->clear_point_locator();
    1060     1016590 :   this->clear_stored_ranges();
    1061     1016590 : }
    1062             : 
    1063             : 
    1064       89630 : bool MeshBase::is_prepared() const
    1065             : {
    1066       89630 :   return static_cast<bool>(_preparation);
    1067             : }
    1068             : 
    1069             : 
    1070        2059 : void MeshBase::unset_is_prepared()
    1071             : {
    1072        2059 :   _preparation = false;
    1073        2059 :   this->clear_point_locator();
    1074        2059 :   this->clear_stored_ranges();
    1075        2059 : }
    1076             : 
    1077             : 
    1078     1004893 : void MeshBase::add_ghosting_functor(GhostingFunctor & ghosting_functor)
    1079             : {
    1080             :   // We used to implicitly support duplicate inserts to std::set
    1081             : #ifdef LIBMESH_ENABLE_DEPRECATED
    1082             :   _ghosting_functors.erase
    1083      947537 :     (std::remove(_ghosting_functors.begin(),
    1084             :                  _ghosting_functors.end(),
    1085     1004893 :                  &ghosting_functor),
    1086       86034 :      _ghosting_functors.end());
    1087             : #endif
    1088             : 
    1089             :   // We shouldn't have two copies of the same functor
    1090       28678 :   libmesh_assert(std::find(_ghosting_functors.begin(),
    1091             :                            _ghosting_functors.end(),
    1092             :                            &ghosting_functor) ==
    1093             :                  _ghosting_functors.end());
    1094             : 
    1095     1004893 :   _ghosting_functors.push_back(&ghosting_functor);
    1096     1004893 : }
    1097             : 
    1098             : 
    1099             : 
    1100      974299 : void MeshBase::remove_ghosting_functor(GhostingFunctor & ghosting_functor)
    1101             : {
    1102      918967 :   auto raw_it = std::find(_ghosting_functors.begin(),
    1103     1001965 :                           _ghosting_functors.end(), &ghosting_functor);
    1104             : 
    1105             :   // The DofMap has a "to_mesh" parameter that tells it to avoid
    1106             :   // registering a new functor with the mesh, but it doesn't keep
    1107             :   // track of which functors weren't added, so we'll support "remove a
    1108             :   // functor that isn't there" just like we did with set::erase
    1109             :   // before.
    1110      974299 :   if (raw_it != _ghosting_functors.end())
    1111      973376 :     _ghosting_functors.erase(raw_it);
    1112             : 
    1113             :   // We shouldn't have had two copies of the same functor
    1114       27666 :   libmesh_assert(std::find(_ghosting_functors.begin(),
    1115             :                            _ghosting_functors.end(),
    1116             :                            &ghosting_functor) ==
    1117             :                  _ghosting_functors.end());
    1118             : 
    1119      974299 :   if (const auto it = _shared_functors.find(&ghosting_functor);
    1120       27666 :       it != _shared_functors.end())
    1121          69 :     _shared_functors.erase(it);
    1122      974299 : }
    1123             : 
    1124             : 
    1125             : 
    1126       27995 : void MeshBase::subdomain_ids (std::set<subdomain_id_type> & ids, const bool global /* = true */) const
    1127             : {
    1128             :   // This requires an inspection on every processor
    1129        1298 :   if (global)
    1130        1298 :     parallel_object_only();
    1131             : 
    1132          34 :   struct SBDInserter {
    1133             :     std::set<subdomain_id_type> my_ids;
    1134             : 
    1135        1298 :     SBDInserter () {}
    1136          36 :     SBDInserter (SBDInserter &, Threads::split) {}
    1137             : 
    1138       28101 :     void operator()(const ConstElemRange & range) {
    1139     2218422 :       for (const Elem * elem : range)
    1140     2190321 :         my_ids.insert(elem->subdomain_id());
    1141       28101 :     }
    1142             : 
    1143          36 :     void join(SBDInserter & other) {
    1144          36 :       my_ids.merge(other.my_ids);
    1145          36 :     }
    1146             :   };
    1147             : 
    1148        2596 :   SBDInserter inserter;
    1149       27995 :   Threads::parallel_reduce(this->active_local_element_stored_range(), inserter);
    1150             : 
    1151        1298 :   ids.swap(inserter.my_ids);
    1152             : 
    1153       27995 :   if (global)
    1154             :     {
    1155             :       // Only include the unpartitioned elements if the user requests the global IDs.
    1156             :       // In the case of the local subdomain IDs, it doesn't make sense to include the
    1157             :       // unpartitioned elements because said elements do not have a sense of locality.
    1158       58166 :       for (const auto & elem : this->active_unpartitioned_element_ptr_range())
    1159       28873 :         ids.insert(elem->subdomain_id());
    1160             : 
    1161             :       // Some subdomains may only live on other processors
    1162       27995 :       this->comm().set_union(ids);
    1163             :     }
    1164       27995 : }
    1165             : 
    1166             : 
    1167             : 
    1168      632430 : void MeshBase::redistribute()
    1169             : {
    1170             :   // We now have all elements and nodes redistributed; our ghosting
    1171             :   // functors should be ready to redistribute and/or recompute any
    1172             :   // cached data they use too.
    1173      635614 :   for (auto & gf : as_range(this->ghosting_functors_begin(),
    1174     1431331 :                             this->ghosting_functors_end()))
    1175      770053 :     gf->redistribute();
    1176      632430 : }
    1177             : 
    1178             : 
    1179             : 
    1180      534645 : void MeshBase::update_post_partitioning()
    1181             : {
    1182             :   // A range over all elements might still be fine here, but any range
    1183             :   // over local elements is obsolete if our partitioner changed the
    1184             :   // definition of "local".
    1185        9760 :   _const_active_local_element_stored_range.reset(nullptr);
    1186      534645 : }
    1187             : 
    1188             : 
    1189             : 
    1190       21170 : subdomain_id_type MeshBase::n_subdomains() const
    1191             : {
    1192             :   // This requires an inspection on every processor
    1193         596 :   parallel_object_only();
    1194             : 
    1195        1192 :   std::set<subdomain_id_type> ids;
    1196             : 
    1197       21170 :   this->subdomain_ids (ids);
    1198             : 
    1199       21766 :   return cast_int<subdomain_id_type>(ids.size());
    1200             : }
    1201             : 
    1202             : 
    1203             : 
    1204           0 : subdomain_id_type MeshBase::n_local_subdomains() const
    1205             : {
    1206           0 :   std::set<subdomain_id_type> ids;
    1207             : 
    1208           0 :   this->subdomain_ids (ids, /* global = */ false);
    1209             : 
    1210           0 :   return cast_int<subdomain_id_type>(ids.size());
    1211             : }
    1212             : 
    1213             : 
    1214             : 
    1215             : 
    1216     3457487 : dof_id_type MeshBase::n_nodes_on_proc (const processor_id_type proc_id) const
    1217             : {
    1218             :   // We're either counting a processor's nodes or unpartitioned
    1219             :   // nodes
    1220        5752 :   libmesh_assert (proc_id < this->n_processors() ||
    1221             :                   proc_id == DofObject::invalid_processor_id);
    1222             : 
    1223     6909222 :   return static_cast<dof_id_type>(std::distance (this->pid_nodes_begin(proc_id),
    1224    10366709 :                                                  this->pid_nodes_end  (proc_id)));
    1225             : }
    1226             : 
    1227             : 
    1228             : 
    1229     3888838 : dof_id_type MeshBase::n_elem_on_proc (const processor_id_type proc_id) const
    1230             : {
    1231             :   // We're either counting a processor's elements or unpartitioned
    1232             :   // elements
    1233       18670 :   libmesh_assert (proc_id < this->n_processors() ||
    1234             :                   proc_id == DofObject::invalid_processor_id);
    1235             : 
    1236     7759006 :   return static_cast<dof_id_type>(std::distance (this->pid_elements_begin(proc_id),
    1237    11647844 :                                                  this->pid_elements_end  (proc_id)));
    1238             : }
    1239             : 
    1240             : 
    1241             : 
    1242     1295862 : dof_id_type MeshBase::n_active_elem_on_proc (const processor_id_type proc_id) const
    1243             : {
    1244        2164 :   libmesh_assert_less (proc_id, this->n_processors());
    1245     2589560 :   return static_cast<dof_id_type>(std::distance (this->active_pid_elements_begin(proc_id),
    1246     3885422 :                                                  this->active_pid_elements_end  (proc_id)));
    1247             : }
    1248             : 
    1249             : 
    1250             : 
    1251           0 : dof_id_type MeshBase::n_sub_elem () const
    1252             : {
    1253           0 :   dof_id_type ne=0;
    1254             : 
    1255           0 :   for (const auto & elem : this->element_ptr_range())
    1256           0 :     ne += elem->n_sub_elem();
    1257             : 
    1258           0 :   return ne;
    1259             : }
    1260             : 
    1261             : 
    1262             : 
    1263       32266 : dof_id_type MeshBase::n_active_sub_elem () const
    1264             : {
    1265        1575 :   dof_id_type ne=0;
    1266             : 
    1267    25301373 :   for (const auto & elem : this->active_element_ptr_range())
    1268    25267532 :     ne += elem->n_sub_elem();
    1269             : 
    1270       32266 :   return ne;
    1271             : }
    1272             : 
    1273             : 
    1274             : 
    1275       14995 : std::string MeshBase::get_info(const unsigned int verbosity /* = 0 */, const bool global /* = true */) const
    1276             : {
    1277       15835 :   std::ostringstream oss;
    1278             : 
    1279       14995 :   oss << " Mesh Information:" << '\n';
    1280             : 
    1281       14995 :   if (!_elem_dims.empty())
    1282             :     {
    1283       14995 :       oss << "  elem_dimensions()={";
    1284       14995 :       std::copy(_elem_dims.begin(),
    1285         420 :                 --_elem_dims.end(), // --end() is valid if the set is non-empty
    1286       14575 :                 std::ostream_iterator<unsigned int>(oss, ", "));
    1287       14995 :       oss << cast_int<unsigned int>(*_elem_dims.rbegin());
    1288       14995 :       oss << "}\n";
    1289             :     }
    1290             : 
    1291       14995 :   if (!_elem_default_orders.empty())
    1292             :     {
    1293       14995 :       oss << "  elem_default_orders()={";
    1294       14995 :       std::transform(_elem_default_orders.begin(),
    1295         420 :                      --_elem_default_orders.end(),
    1296       14995 :                      std::ostream_iterator<std::string>(oss, ", "),
    1297           8 :                      [](Order o)
    1298         276 :                        { return Utility::enum_to_string<Order>(o); });
    1299       14995 :       oss << Utility::enum_to_string<Order>(*_elem_default_orders.rbegin());
    1300       14995 :       oss << "}\n";
    1301             :     }
    1302             : 
    1303       14995 :   oss << "  supported_nodal_order()=" << this->supported_nodal_order()                        << '\n'
    1304       15835 :       << "  spatial_dimension()="     << this->spatial_dimension()                            << '\n'
    1305       15415 :       << "  n_nodes()="               << this->n_nodes()                                      << '\n'
    1306       14995 :       << "    n_local_nodes()="       << this->n_local_nodes()                                << '\n'
    1307       15415 :       << "  n_elem()="                << this->n_elem()                                       << '\n'
    1308       29570 :       << "    n_local_elem()="        << this->n_local_elem()                                 << '\n';
    1309             : #ifdef LIBMESH_ENABLE_AMR
    1310       29150 :   oss << "    n_active_elem()="       << this->n_active_elem()                                << '\n';
    1311             : #endif
    1312       14995 :   if (global)
    1313       15415 :     oss << "  n_subdomains()="        << static_cast<std::size_t>(this->n_subdomains())       << '\n';
    1314             :   else
    1315           0 :     oss << "  n_local_subdomains()= " << static_cast<std::size_t>(this->n_local_subdomains()) << '\n';
    1316       15415 :   oss << "  n_elemsets()="            << static_cast<std::size_t>(this->n_elemsets())         << '\n';
    1317       14995 :   if (!_elemset_codes.empty())
    1318           0 :     oss << "    n_elemset_codes="     << _elemset_codes.size()                                << '\n';
    1319       14995 :   oss << "  n_partitions()="          << static_cast<std::size_t>(this->n_partitions())       << '\n'
    1320       15835 :       << "  n_processors()="          << static_cast<std::size_t>(this->n_processors())       << '\n'
    1321       15415 :       << "  n_threads()="             << static_cast<std::size_t>(libMesh::n_threads())       << '\n'
    1322       15835 :       << "  processor_id()="          << static_cast<std::size_t>(this->processor_id())       << '\n'
    1323       15415 :       << "  is_prepared()="           << (this->is_prepared() ? "true" : "false")             << '\n'
    1324       43089 :       << "  is_replicated()="         << (this->is_replicated() ? "true" : "false")           << '\n';
    1325             : 
    1326       14995 :   if (verbosity > 0)
    1327             :     {
    1328         284 :       if (global)
    1329             :         {
    1330           8 :           libmesh_parallel_only(this->comm());
    1331         292 :           if (this->processor_id() != 0)
    1332         236 :             oss << "\n Detailed global get_info() (verbosity > 0) is reduced and output to only rank 0.";
    1333             :         }
    1334             : 
    1335             :       // Helper for printing element types
    1336         672 :       const auto elem_type_helper = [](const std::set<int> & elem_types) {
    1337         784 :         std::stringstream ss;
    1338        1344 :         for (auto it = elem_types.begin(); it != elem_types.end();)
    1339             :           {
    1340        1288 :             ss << Utility::enum_to_string((ElemType)*it);
    1341         672 :             if (++it != elem_types.end())
    1342           0 :               ss << ", ";
    1343             :           }
    1344         728 :         return ss.str();
    1345         560 :       };
    1346             : 
    1347             :       // Helper for whether or not the given DofObject is to be included. If we're doing
    1348             :       // a global reduction, we also count unpartitioned objects on rank 0.
    1349      871156 :       const auto include_object = [this, &global](const DofObject & dof_object) {
    1350      978188 :         return this->processor_id() == dof_object.processor_id() ||
    1351      250756 :                (global &&
    1352       42324 :                 this->processor_id() == 0 &&
    1353      846328 :                 dof_object.processor_id() == DofObject::invalid_processor_id);
    1354         276 :       };
    1355             : 
    1356           8 :       Real volume = 0;
    1357             : 
    1358             :       // Add bounding box information
    1359         284 :       const auto bbox = global ? MeshTools::create_bounding_box(*this) : MeshTools::create_local_bounding_box(*this);
    1360         284 :       if (!global || this->processor_id() == 0)
    1361             :         oss << "\n " << (global ? "" : "Local ") << "Mesh Bounding Box:\n"
    1362          48 :             << "  Minimum: " << bbox.min()                << "\n"
    1363          44 :             << "  Maximum: " << bbox.max()                << "\n"
    1364          88 :             << "  Delta:   " << (bbox.max() - bbox.min()) << "\n";
    1365             : 
    1366             :       // Obtain the global or local element types
    1367          16 :       std::set<int> elem_types;
    1368       49712 :       for (const Elem * elem : this->active_local_element_ptr_range())
    1369       49420 :         elem_types.insert(elem->type());
    1370         284 :       if (global)
    1371             :         {
    1372             :           // Pick up unpartitioned elems on rank 0
    1373         292 :           if (this->processor_id() == 0)
    1374          92 :             for (const Elem * elem : this->active_unpartitioned_element_ptr_range())
    1375          40 :               elem_types.insert(elem->type());
    1376             : 
    1377         284 :           this->comm().set_union(elem_types);
    1378             :         }
    1379             : 
    1380             :       // Add element types
    1381         284 :       if (!global || this->processor_id() == 0)
    1382             :         oss << "\n " << (global ? "" : "Local ") << "Mesh Element Type(s):\n  "
    1383         140 :             << elem_type_helper(elem_types) << "\n";
    1384             : 
    1385             :       // Reduce the nodeset ids
    1386          16 :       auto nodeset_ids = this->get_boundary_info().get_node_boundary_ids();
    1387         284 :       if (global)
    1388         284 :         this->comm().set_union(nodeset_ids);
    1389             : 
    1390             :       // Accumulate local information for each nodeset
    1391        1608 :       struct NodesetInfo
    1392             :       {
    1393             :         std::size_t num_nodes = 0;
    1394             :         BoundingBox bbox;
    1395             :       };
    1396          16 :       std::map<boundary_id_type, NodesetInfo> nodeset_info_map;
    1397       91376 :       for (const auto & [node, id] : this->get_boundary_info().get_nodeset_map())
    1398             :         {
    1399       91092 :           if (!include_object(*node))
    1400       38364 :             continue;
    1401             : 
    1402       48672 :           NodesetInfo & info = nodeset_info_map[id];
    1403             : 
    1404       48672 :           ++info.num_nodes;
    1405             : 
    1406       48672 :           if (verbosity > 1)
    1407       48672 :             info.bbox.union_with(*node);
    1408             :         }
    1409             : 
    1410             :       // Add nodeset info
    1411         284 :       if (!global || this->processor_id() == 0)
    1412             :         {
    1413          48 :           oss << "\n " << (global ? "" : "Local ") << "Mesh Nodesets:\n";
    1414          48 :           if (nodeset_ids.empty())
    1415           0 :             oss << "  None\n";
    1416             :         }
    1417             : 
    1418           8 :       const auto & nodeset_name_map = this->get_boundary_info().get_nodeset_name_map();
    1419        1988 :       for (const auto id : nodeset_ids)
    1420             :         {
    1421        1704 :           NodesetInfo & info = nodeset_info_map[id];
    1422             : 
    1423             :           // Reduce the local information for this nodeset if required
    1424        1704 :           if (global)
    1425             :             {
    1426        1704 :               this->comm().sum(info.num_nodes);
    1427        1704 :               if (verbosity > 1)
    1428             :                 {
    1429        1752 :                   this->comm().min(info.bbox.min());
    1430        1752 :                   this->comm().max(info.bbox.max());
    1431             :                 }
    1432             :             }
    1433             : 
    1434        1704 :           const bool has_name = nodeset_name_map.count(id) && nodeset_name_map.at(id).size();
    1435        1800 :           const std::string name = has_name ? nodeset_name_map.at(id) : "";
    1436          96 :           if (global)
    1437          48 :             libmesh_assert(this->comm().verify(name));
    1438             : 
    1439        1704 :           if (global ? this->processor_id() == 0 : info.num_nodes > 0)
    1440             :             {
    1441         288 :               oss << "  Nodeset " << id;
    1442         288 :               if (has_name)
    1443         528 :                 oss << " (" << name << ")";
    1444         312 :               oss << ", " << info.num_nodes << " " << (global ? "" : "local ") << "nodes\n";
    1445             : 
    1446         288 :               if (verbosity > 1)
    1447             :               {
    1448         288 :                 oss << "   " << (global ? "Bounding" : "Local bounding") << " box minimum: "
    1449         288 :                     << info.bbox.min() << "\n"
    1450         288 :                     << "   " << (global ? "Bounding" : "Local bounding") << " box maximum: "
    1451         288 :                     << info.bbox.max() << "\n"
    1452         288 :                     << "   " << (global ? "Bounding" : "Local bounding") << " box delta: "
    1453         288 :                     << (info.bbox.max() - info.bbox.min()) << "\n";
    1454             :               }
    1455             :             }
    1456             :         }
    1457             : 
    1458             :       // Reduce the sideset ids
    1459          16 :       auto sideset_ids = this->get_boundary_info().get_side_boundary_ids();
    1460         284 :       if (global)
    1461         284 :         this->comm().set_union(sideset_ids);
    1462             : 
    1463             :       // Accumulate local information for each sideset
    1464             :       struct SidesetInfo
    1465             :       {
    1466             :         std::size_t num_sides = 0;
    1467             :         Real volume = 0;
    1468             :         std::set<int> side_elem_types;
    1469             :         std::set<int> elem_types;
    1470             :         std::set<dof_id_type> elem_ids;
    1471             :         std::set<dof_id_type> node_ids;
    1472             :         BoundingBox bbox;
    1473             :       };
    1474          16 :       ElemSideBuilder side_builder;
    1475          16 :       std::map<boundary_id_type, SidesetInfo> sideset_info_map;
    1476       70396 :       for (const auto & pair : this->get_boundary_info().get_sideset_map())
    1477             :         {
    1478       70112 :           const Elem * elem = pair.first;
    1479       63456 :           if (!include_object(*elem))
    1480       30176 :             continue;
    1481             : 
    1482       39936 :           const auto id = pair.second.second;
    1483       39936 :           SidesetInfo & info = sideset_info_map[id];
    1484             : 
    1485       39936 :           const auto s = pair.second.first;
    1486       39936 :           const Elem & side = side_builder(*elem, s);
    1487             : 
    1488       39936 :           ++info.num_sides;
    1489       39936 :           info.side_elem_types.insert(side.type());
    1490       39936 :           info.elem_types.insert(elem->type());
    1491       39936 :           info.elem_ids.insert(elem->id());
    1492             : 
    1493      199680 :           for (const Node & node : side.node_ref_range())
    1494      146432 :             if (include_object(node))
    1495      147552 :               info.node_ids.insert(node.id());
    1496             : 
    1497       39936 :           if (verbosity > 1)
    1498             :           {
    1499       39936 :             info.volume += side.volume();
    1500       39936 :             info.bbox.union_with(side.loose_bounding_box());
    1501             :           }
    1502             :         }
    1503             : 
    1504             :       // Add sideset info
    1505         284 :       if (!global || this->processor_id() == 0)
    1506             :         {
    1507          48 :           oss << "\n " << (global ? "" : "Local ") << "Mesh Sidesets:\n";
    1508          48 :           if (sideset_ids.empty())
    1509           0 :             oss << "  None\n";
    1510             :         }
    1511           8 :       const auto & sideset_name_map = this->get_boundary_info().get_sideset_name_map();
    1512        1988 :       for (const auto id : sideset_ids)
    1513             :         {
    1514        1704 :           SidesetInfo & info = sideset_info_map[id];
    1515             : 
    1516        1704 :           auto num_elems = info.elem_ids.size();
    1517        1704 :           auto num_nodes = info.node_ids.size();
    1518             : 
    1519             :           // Reduce the local information for this sideset if required
    1520        1704 :           if (global)
    1521             :             {
    1522        1704 :               this->comm().sum(info.num_sides);
    1523        1704 :               this->comm().set_union(info.side_elem_types, 0);
    1524        1704 :               this->comm().sum(num_elems);
    1525        1704 :               this->comm().set_union(info.elem_types, 0);
    1526        1704 :               this->comm().sum(num_nodes);
    1527        1704 :               if (verbosity > 1)
    1528             :               {
    1529        1704 :                 this->comm().sum(info.volume);
    1530        1752 :                 this->comm().min(info.bbox.min());
    1531        1752 :                 this->comm().max(info.bbox.max());
    1532             :               }
    1533             :             }
    1534             : 
    1535        1704 :           const bool has_name = sideset_name_map.count(id) && sideset_name_map.at(id).size();
    1536        1800 :           const std::string name = has_name ? sideset_name_map.at(id) : "";
    1537          96 :           if (global)
    1538          48 :             libmesh_assert(this->comm().verify(name));
    1539             : 
    1540        1704 :           if (global ? this->processor_id() == 0 : info.num_sides > 0)
    1541             :             {
    1542         288 :               oss << "  Sideset " << id;
    1543         288 :               if (has_name)
    1544         528 :                 oss << " (" << name << ")";
    1545         552 :               oss << ", " << info.num_sides << " sides (" << elem_type_helper(info.side_elem_types) << ")"
    1546        1056 :                   << ", " << num_elems << " " << (global ? "" : "local ") << "elems (" << elem_type_helper(info.elem_types) << ")"
    1547         600 :                   << ", " << num_nodes << " " << (global ? "" : "local ") << "nodes\n";
    1548             : 
    1549         288 :               if (verbosity > 1)
    1550             :               {
    1551         312 :                 oss << "   " << (global ? "Side" : "Local side") << " volume: " << info.volume << "\n"
    1552         288 :                     << "   " << (global ? "Bounding" : "Local bounding") << " box minimum: "
    1553         288 :                     << info.bbox.min() << "\n"
    1554         288 :                     << "   " << (global ? "Bounding" : "Local bounding") << " box maximum: "
    1555         288 :                     << info.bbox.max() << "\n"
    1556         288 :                     << "   " << (global ? "Bounding" : "Local bounding") << " box delta: "
    1557         288 :                     << (info.bbox.max() - info.bbox.min()) << "\n";
    1558             :               }
    1559             :             }
    1560             :         }
    1561             : 
    1562             :       // Reduce the edgeset ids
    1563          16 :       auto edgeset_ids = this->get_boundary_info().get_edge_boundary_ids();
    1564         284 :       if (global)
    1565         284 :         this->comm().set_union(edgeset_ids);
    1566             : 
    1567             :       // Accumulate local information for each edgeset
    1568           0 :       struct EdgesetInfo
    1569             :       {
    1570             :         std::size_t num_edges = 0;
    1571             :         std::set<int> edge_elem_types;
    1572             :         BoundingBox bbox;
    1573             :       };
    1574          16 :       std::map<boundary_id_type, EdgesetInfo> edgeset_info_map;
    1575         284 :       std::unique_ptr<const Elem> edge;
    1576             : 
    1577         284 :       for (const auto & pair : this->get_boundary_info().get_edgeset_map())
    1578             :         {
    1579           0 :           const Elem * elem = pair.first;
    1580           0 :           if (!include_object(*elem))
    1581           0 :             continue;
    1582             : 
    1583           0 :           const auto id = pair.second.second;
    1584           0 :           EdgesetInfo & info = edgeset_info_map[id];
    1585             : 
    1586           0 :           elem->build_edge_ptr(edge, pair.second.first);
    1587             : 
    1588           0 :           ++info.num_edges;
    1589           0 :           info.edge_elem_types.insert(edge->type());
    1590             : 
    1591           0 :           if (verbosity > 1)
    1592           0 :             info.bbox.union_with(edge->loose_bounding_box());
    1593             :         }
    1594             : 
    1595             :       // Add edgeset info
    1596         284 :       if (!global || this->processor_id() == 0)
    1597             :         {
    1598          48 :           oss << "\n " << (global ? "" : "Local ") << "Mesh Edgesets:\n";
    1599          48 :           if (edgeset_ids.empty())
    1600          48 :             oss << "  None\n";
    1601             :         }
    1602             : 
    1603           8 :       const auto & edgeset_name_map = this->get_boundary_info().get_edgeset_name_map();
    1604         284 :       for (const auto id : edgeset_ids)
    1605             :         {
    1606           0 :           EdgesetInfo & info = edgeset_info_map[id];
    1607             : 
    1608             :           // Reduce the local information for this edgeset if required
    1609           0 :           if (global)
    1610             :             {
    1611           0 :               this->comm().sum(info.num_edges);
    1612           0 :               this->comm().set_union(info.edge_elem_types, 0);
    1613           0 :               if (verbosity > 1)
    1614             :                 {
    1615           0 :                   this->comm().min(info.bbox.min());
    1616           0 :                   this->comm().min(info.bbox.max());
    1617             :                 }
    1618             :             }
    1619             : 
    1620           0 :           const bool has_name = edgeset_name_map.count(id) && edgeset_name_map.at(id).size();
    1621           0 :           const std::string name = has_name ? edgeset_name_map.at(id) : "";
    1622           0 :           if (global)
    1623           0 :             libmesh_assert(this->comm().verify(name));
    1624             : 
    1625           0 :           if (global ? this->processor_id() == 0 : info.num_edges > 0)
    1626             :             {
    1627           0 :               oss << "  Edgeset " << id;
    1628           0 :               if (has_name)
    1629           0 :                 oss << " (" << name << ")";
    1630           0 :               oss << ", " << info.num_edges << " " << (global ? "" : "local ") << "edges ("
    1631           0 :                   << elem_type_helper(info.edge_elem_types) << ")\n";
    1632             : 
    1633           0 :               if (verbosity > 1)
    1634             :               {
    1635           0 :                 oss << "   " << (global ? "Bounding" : "Local bounding") << " box minimum: "
    1636           0 :                     << info.bbox.min() << "\n"
    1637           0 :                     << "   " << (global ? "Bounding" : "Local bounding") << " box maximum: "
    1638           0 :                     << info.bbox.max() << "\n"
    1639           0 :                     << "   " << (global ? "Bounding" : "Local bounding") << " box delta: "
    1640           0 :                     << (info.bbox.max() - info.bbox.min()) << "\n";
    1641             :               }
    1642             :             }
    1643             :         }
    1644             : 
    1645             :       // Reduce the block IDs and block names
    1646          16 :       std::set<subdomain_id_type> subdomains;
    1647      194032 :       for (const Elem * elem : this->active_element_ptr_range())
    1648       92640 :         if (include_object(*elem))
    1649       49420 :           subdomains.insert(elem->subdomain_id());
    1650         284 :       if (global)
    1651         284 :         this->comm().set_union(subdomains);
    1652             : 
    1653             :       // Accumulate local information for each subdomain
    1654           0 :       struct SubdomainInfo
    1655             :       {
    1656             :         std::size_t num_elems = 0;
    1657             :         Real volume = 0;
    1658             :         std::set<int> elem_types;
    1659             :         std::set<dof_id_type> active_node_ids;
    1660             : #ifdef LIBMESH_ENABLE_AMR
    1661             :         std::size_t num_active_elems = 0;
    1662             : #endif
    1663             :         BoundingBox bbox;
    1664             :       };
    1665          16 :       std::map<subdomain_id_type, SubdomainInfo> subdomain_info_map;
    1666      194032 :       for (const Elem * elem : this->element_ptr_range())
    1667       92640 :         if (include_object(*elem))
    1668             :           {
    1669       49152 :             SubdomainInfo & info = subdomain_info_map[elem->subdomain_id()];
    1670             : 
    1671       49152 :             ++info.num_elems;
    1672       49152 :             info.elem_types.insert(elem->type());
    1673             : 
    1674             : #ifdef LIBMESH_ENABLE_AMR
    1675        4096 :             if (elem->active())
    1676       49152 :               ++info.num_active_elems;
    1677             : #endif
    1678             : 
    1679      442368 :             for (const Node & node : elem->node_ref_range())
    1680      392704 :               if (include_object(node) && node.active())
    1681      330608 :                 info.active_node_ids.insert(node.id());
    1682             : 
    1683       49152 :             if (verbosity > 1 && elem->active())
    1684             :               {
    1685       49152 :                 info.volume += elem->volume();
    1686       49152 :                 info.bbox.union_with(elem->loose_bounding_box());
    1687             :               }
    1688         268 :           }
    1689             : 
    1690             :       // Add subdomain info
    1691         284 :       oss << "\n " << (global ? "" : "Local ") << "Mesh Subdomains:\n";
    1692           8 :       const auto & subdomain_name_map = this->get_subdomain_name_map();
    1693         568 :       for (const auto id : subdomains)
    1694             :       {
    1695         284 :         SubdomainInfo & info = subdomain_info_map[id];
    1696             : 
    1697         284 :         auto num_active_nodes = info.active_node_ids.size();
    1698             : 
    1699             :         // Reduce the information for this subdomain if needed
    1700         284 :         if (global)
    1701             :           {
    1702         284 :             this->comm().sum(info.num_elems);
    1703             : #ifdef LIBMESH_ENABLE_AMR
    1704         284 :             this->comm().sum(info.num_active_elems);
    1705             : #endif
    1706         284 :             this->comm().sum(num_active_nodes);
    1707         284 :             this->comm().set_union(info.elem_types, 0);
    1708         284 :             if (verbosity > 1)
    1709             :             {
    1710         292 :               this->comm().min(info.bbox.min());
    1711         292 :               this->comm().max(info.bbox.max());
    1712         284 :               this->comm().sum(info.volume);
    1713             :             }
    1714             :           }
    1715         284 :         if (verbosity > 1)
    1716         284 :           volume += info.volume;
    1717             : 
    1718           8 :         const bool has_name = subdomain_name_map.count(id);
    1719         292 :         const std::string name = has_name ? subdomain_name_map.at(id) : "";
    1720          16 :         if (global)
    1721           8 :           libmesh_assert(this->comm().verify(name));
    1722             : 
    1723         284 :         if (!global || this->processor_id() == 0)
    1724             :           {
    1725          48 :             oss << "  Subdomain " << id;
    1726          48 :             if (has_name)
    1727           0 :               oss << " (" << name << ")";
    1728          48 :             oss << ": " << info.num_elems << " " << (global ? "" : "local ") << "elems "
    1729          56 :                 << "(" << elem_type_helper(info.elem_types);
    1730             : #ifdef LIBMESH_ENABLE_AMR
    1731          48 :             oss << ", " << info.num_active_elems << " active";
    1732             : #endif
    1733          52 :             oss << "), " << num_active_nodes << " " << (global ? "" : "local ") << "active nodes\n";
    1734          48 :             if (verbosity > 1)
    1735             :             {
    1736          52 :               oss << "   " << (global ? "Volume" : "Local volume") << ": " << info.volume << "\n";
    1737          48 :               oss << "   " << (global ? "Bounding" : "Local bounding") << " box minimum: "
    1738          48 :                   << info.bbox.min() << "\n"
    1739          48 :                   << "   " << (global ? "Bounding" : "Local bounding") << " box maximum: "
    1740          48 :                   << info.bbox.max() << "\n"
    1741          48 :                   << "   " << (global ? "Bounding" : "Local bounding") << " box delta: "
    1742          48 :                   << (info.bbox.max() - info.bbox.min()) << "\n";
    1743             :             }
    1744             :           }
    1745             :       }
    1746             : 
    1747         560 :       oss << "  " << (global ? "Global" : "Local") << " mesh volume = " << volume << "\n";
    1748             : 
    1749         268 :     }
    1750             : 
    1751       15415 :   return oss.str();
    1752       14155 : }
    1753             : 
    1754             : 
    1755       14995 : void MeshBase::print_info(std::ostream & os, const unsigned int verbosity /* = 0 */, const bool global /* = true */) const
    1756             : {
    1757       15415 :   os << this->get_info(verbosity, global)
    1758         420 :      << std::endl;
    1759       14995 : }
    1760             : 
    1761             : 
    1762           0 : std::ostream & operator << (std::ostream & os, const MeshBase & m)
    1763             : {
    1764           0 :   m.print_info(os);
    1765           0 :   return os;
    1766             : }
    1767             : 
    1768             : 
    1769      432731 : void MeshBase::partition (const unsigned int n_parts)
    1770             : {
    1771             :   // If we get here and we have unpartitioned elements, we need that
    1772             :   // fixed.
    1773      432731 :   if (this->n_unpartitioned_elem() > 0)
    1774             :     {
    1775        9404 :       libmesh_assert (partitioner().get());
    1776        9404 :       libmesh_assert (this->is_serial());
    1777      315403 :       partitioner()->partition (*this, n_parts);
    1778             :     }
    1779             :   // A nullptr partitioner or a skip_partitioning(true) call or a
    1780             :   // skip_noncritical_partitioning(true) call means don't repartition;
    1781             :   // skip_noncritical_partitioning() checks all these.
    1782        3528 :   else if (!skip_noncritical_partitioning())
    1783             :     {
    1784      110599 :       partitioner()->partition (*this, n_parts);
    1785             :     }
    1786             :   else
    1787             :     {
    1788             :       // Adaptive coarsening may have "orphaned" nodes on processors
    1789             :       // whose elements no longer share them.  We need to check for
    1790             :       // and possibly fix that.
    1791        6729 :       MeshTools::correct_node_proc_ids(*this);
    1792             : 
    1793             :       // Make sure locally cached partition count is correct
    1794        6729 :       this->recalculate_n_partitions();
    1795             : 
    1796             :       // Make sure any other locally cached data is correct
    1797        6729 :       this->update_post_partitioning();
    1798             :     }
    1799             : 
    1800      432731 :   _preparation.is_partitioned = true;
    1801      432731 : }
    1802             : 
    1803       18649 : void MeshBase::all_second_order (const bool full_ordered)
    1804             : {
    1805       18649 :   this->all_second_order_range(this->element_ptr_range(), full_ordered);
    1806       18649 : }
    1807             : 
    1808       14971 : void MeshBase::all_complete_order ()
    1809             : {
    1810       14971 :   this->all_complete_order_range(this->element_ptr_range());
    1811       14971 : }
    1812             : 
    1813        6729 : unsigned int MeshBase::recalculate_n_partitions()
    1814             : {
    1815             :   // This requires an inspection on every processor
    1816         170 :   parallel_object_only();
    1817             : 
    1818        6729 :   unsigned int max_proc_id=0;
    1819             : 
    1820      225762 :   for (const auto & elem : this->active_local_element_ptr_range())
    1821      223045 :     max_proc_id = std::max(max_proc_id, static_cast<unsigned int>(elem->processor_id()));
    1822             : 
    1823             :   // The number of partitions is one more than the max processor ID.
    1824        6729 :   _n_parts = max_proc_id+1;
    1825             : 
    1826        6729 :   this->comm().max(_n_parts);
    1827             : 
    1828        6729 :   return _n_parts;
    1829             : }
    1830             : 
    1831             : 
    1832             : 
    1833      555104 : std::unique_ptr<PointLocatorBase> MeshBase::sub_point_locator () const
    1834             : {
    1835             :   // If there's no master point locator, then we need one.
    1836      555104 :   if (_point_locator.get() == nullptr)
    1837             :     {
    1838             :       // PointLocator construction may not be safe within threads
    1839         760 :       libmesh_assert(!Threads::in_threads);
    1840             : 
    1841             :       // And it may require parallel communication
    1842         760 :       parallel_object_only();
    1843             : 
    1844             : #ifdef LIBMESH_ENABLE_NANOFLANN_POINTLOCATOR
    1845             :       _point_locator = PointLocatorBase::build(NANOFLANN, *this);
    1846             : #else
    1847       43522 :       _point_locator = PointLocatorBase::build(TREE_ELEMENTS, *this);
    1848             : #endif
    1849             : 
    1850       22521 :       if (_point_locator_close_to_point_tol > 0.)
    1851           0 :         _point_locator->set_close_to_point_tol(_point_locator_close_to_point_tol);
    1852             :     }
    1853             : 
    1854             :   // Otherwise there was a master point locator, and we can grab a
    1855             :   // sub-locator easily.
    1856             :   return
    1857             : #ifdef LIBMESH_ENABLE_NANOFLANN_POINTLOCATOR
    1858             :     PointLocatorBase::build(NANOFLANN, *this, _point_locator.get());
    1859             : #else
    1860      555104 :     PointLocatorBase::build(TREE_ELEMENTS, *this, _point_locator.get());
    1861             : #endif
    1862             : }
    1863             : 
    1864             : 
    1865             : 
    1866   108760430 : void MeshBase::clear_point_locator ()
    1867             : {
    1868     1564897 :   _point_locator.reset(nullptr);
    1869   108760430 : }
    1870             : 
    1871             : 
    1872             : 
    1873           0 : void MeshBase::set_count_lower_dim_elems_in_point_locator(bool count_lower_dim_elems)
    1874             : {
    1875           0 :   _count_lower_dim_elems_in_point_locator = count_lower_dim_elems;
    1876           0 : }
    1877             : 
    1878             : 
    1879             : 
    1880     6658441 : bool MeshBase::get_count_lower_dim_elems_in_point_locator() const
    1881             : {
    1882     6658441 :   return _count_lower_dim_elems_in_point_locator;
    1883             : }
    1884             : 
    1885             : 
    1886             : 
    1887     1063271 : std::string & MeshBase::subdomain_name(subdomain_id_type id)
    1888             : {
    1889       45266 :   this->unset_has_synched_subdomain_name_map();
    1890     1063271 :   return _block_id_to_name[id];
    1891             : }
    1892             : 
    1893       44105 : const std::string & MeshBase::subdomain_name(subdomain_id_type id) const
    1894             : {
    1895             :   // An empty string to return when no matching subdomain name is found
    1896       44105 :   static const std::string empty;
    1897             : 
    1898       44105 :   if (const auto iter = _block_id_to_name.find(id);
    1899        1623 :       iter == _block_id_to_name.end())
    1900        1417 :     return empty;
    1901             :   else
    1902        2476 :     return iter->second;
    1903             : }
    1904             : 
    1905             : 
    1906             : 
    1907             : 
    1908           0 : subdomain_id_type MeshBase::get_id_by_name(std::string_view name) const
    1909             : {
    1910             :   // Linear search over the map values.
    1911           0 :   for (const auto & [sbd_id, sbd_name] : _block_id_to_name)
    1912           0 :     if (sbd_name == name)
    1913           0 :       return sbd_id;
    1914             : 
    1915             :   // If we made it here without returning, we don't have a subdomain
    1916             :   // with the requested name, so return Elem::invalid_subdomain_id.
    1917           0 :   return Elem::invalid_subdomain_id;
    1918             : }
    1919             : 
    1920             : 
    1921     1670491 : const ElemRange & MeshBase::element_stored_range()
    1922             : {
    1923     1670491 :   if (!_element_stored_range)
    1924             :   {
    1925             :     // Range construction may not be safe within threads
    1926       13974 :     libmesh_assert(!Threads::in_threads);
    1927             : 
    1928             :     _element_stored_range =
    1929     2039551 :       std::make_unique<ElemRange>(this->elements_begin(),
    1930     1392296 :                                   this->elements_end());
    1931             :   }
    1932             : 
    1933     1670491 :   return *_element_stored_range;
    1934             : }
    1935             : 
    1936      569469 : const ConstElemRange & MeshBase::active_local_element_stored_range() const
    1937             : {
    1938      569469 :   if (!_const_active_local_element_stored_range)
    1939             :   {
    1940             :     // Range construction may not be safe within threads
    1941        1754 :     libmesh_assert(!Threads::in_threads);
    1942             : 
    1943             :     _const_active_local_element_stored_range =
    1944      905066 :       std::make_unique<ConstElemRange>(this->active_local_elements_begin(),
    1945      607470 :                                   this->active_local_elements_end());
    1946             :   }
    1947             : 
    1948      569469 :   return *_const_active_local_element_stored_range;
    1949             : }
    1950             : 
    1951   111010944 : void MeshBase::clear_stored_ranges()
    1952             : {
    1953     1561281 :   _element_stored_range.reset(nullptr);
    1954     1561281 :   _const_active_local_element_stored_range.reset(nullptr);
    1955   111010944 : }
    1956             : 
    1957             : 
    1958             : #ifdef LIBMESH_ENABLE_DEPRECATED
    1959           0 : void MeshBase::cache_elem_dims()
    1960             : {
    1961             :   libmesh_deprecated();
    1962             : 
    1963           0 :   this->cache_elem_data();
    1964           0 : }
    1965             : #endif // LIBMESH_ENABLE_DEPRECATED
    1966             : 
    1967      500623 : void MeshBase::cache_elem_data()
    1968             : {
    1969             :   // This requires an inspection on every processor
    1970       15098 :   parallel_object_only();
    1971             : 
    1972             :   // Need to clear containers first in case all elements of a
    1973             :   // particular dimension/order/subdomain have been deleted.
    1974       30180 :   _elem_dims.clear();
    1975       30180 :   _elem_default_orders.clear();
    1976       30180 :   _mesh_subdomains.clear();
    1977      500623 :   _supported_nodal_order = MAXIMUM;
    1978             : 
    1979    34590567 :   for (const auto & elem : this->active_element_ptr_range())
    1980             :   {
    1981    33604419 :     _elem_dims.insert(cast_int<unsigned char>(elem->dim()));
    1982    33604419 :     _elem_default_orders.insert(elem->default_order());
    1983    33604419 :     _mesh_subdomains.insert(elem->subdomain_id());
    1984    33604419 :     _supported_nodal_order =
    1985    33604419 :       static_cast<Order>
    1986    67208838 :         (std::min(static_cast<int>(_supported_nodal_order),
    1987    34060665 :                   static_cast<int>(elem->supported_nodal_order())));
    1988      470443 :   }
    1989             : 
    1990      500623 :   if (!this->is_serial())
    1991             :   {
    1992             :     // Some different dimension/order/subdomain elements may only live
    1993             :     // on other processors
    1994      123610 :     this->comm().set_union(_elem_dims);
    1995      123610 :     this->comm().set_union(_elem_default_orders);
    1996      123610 :     this->comm().min(_supported_nodal_order);
    1997      123610 :     this->comm().set_union(_mesh_subdomains);
    1998             :   }
    1999             : 
    2000             :   // If the largest element dimension found is larger than the current
    2001             :   // _spatial_dimension, increase _spatial_dimension.
    2002      500623 :   unsigned int max_dim = this->mesh_dimension();
    2003      500623 :   if (max_dim > _spatial_dimension)
    2004       43291 :     _spatial_dimension = cast_int<unsigned char>(max_dim);
    2005             : 
    2006             :   // _spatial_dimension may need to increase from 1->2 or 2->3 if the
    2007             :   // mesh is full of 1D elements but they are not x-aligned, or the
    2008             :   // mesh is full of 2D elements but they are not in the x-y plane.
    2009             :   // If the mesh is x-aligned or x-y planar, we will end up checking
    2010             :   // every node's coordinates and not breaking out of the loop
    2011             :   // early...
    2012      500623 :   if (_spatial_dimension < LIBMESH_DIM)
    2013             :     {
    2014    49252236 :       for (const auto & node : this->node_ptr_range())
    2015             :         {
    2016             :           // Note: the exact floating point comparison is intentional,
    2017             :           // we don't want to get tripped up by tolerances.
    2018    25476902 :           if ((*node)(0) != 0. && _spatial_dimension < 1)
    2019           0 :             _spatial_dimension = 1;
    2020             : 
    2021    25476902 :           if ((*node)(1) != 0. && _spatial_dimension < 2)
    2022             :             {
    2023        2501 :               _spatial_dimension = 2;
    2024             : #if LIBMESH_DIM == 2
    2025             :               // If libmesh is compiled in 2D mode, this is the
    2026             :               // largest spatial dimension possible so we can break
    2027             :               // out.
    2028             :               break;
    2029             : #endif
    2030             :             }
    2031             : 
    2032             : #if LIBMESH_DIM > 2
    2033    25476902 :           if ((*node)(2) != 0.)
    2034             :             {
    2035             :               // Spatial dimension can't get any higher than this, so
    2036             :               // we can break out.
    2037        8187 :               _spatial_dimension = 3;
    2038        8187 :               break;
    2039             :             }
    2040             : #endif
    2041      254729 :         }
    2042             :     }
    2043             : 
    2044      500623 :   _preparation.has_cached_elem_data = true;
    2045      500623 : }
    2046             : 
    2047             : 
    2048      485169 : void MeshBase::sync_subdomain_name_map()
    2049             : {
    2050             :   // This requires every processor
    2051       14646 :   parallel_object_only();
    2052             : 
    2053      485169 :   this->comm().set_union(_block_id_to_name);
    2054             : 
    2055      485169 :   _preparation.has_synched_subdomain_name_map = true;
    2056      485169 : }
    2057             : 
    2058             : 
    2059      435229 : void MeshBase::detect_interior_parents()
    2060             : {
    2061       13018 :   LOG_SCOPE("detect_interior_parents()", "MeshBase");
    2062             : 
    2063             :   // This requires an inspection on every processor
    2064       13018 :   parallel_object_only();
    2065             : 
    2066             :   // This requires up-to-date mesh dimensions in cache
    2067       13018 :   libmesh_assert(_preparation.has_cached_elem_data);
    2068             : 
    2069             :   // Early return if the mesh is empty or has elements of a single spatial dimension.
    2070      435229 :   if (this->elem_dimensions().size() <= 1)
    2071             :     {
    2072      428544 :       _preparation.has_interior_parent_ptrs = true;
    2073      429761 :       return;
    2074             :     }
    2075             : 
    2076             :   // Convenient elem_dimensions iterators
    2077        6497 :   const auto dim_start = this->elem_dimensions().begin();
    2078         188 :   const auto dim_end = this->elem_dimensions().end();
    2079             : 
    2080             :   // In this function we find only +1 dimensional interior parents,
    2081             :   // (so, for a given element el, the interior parent p must satisfy p.dim() == el.dim() + 1).
    2082             :   // Therefore, we can avoid checking the existence of interior parents
    2083             :   // for all those elements el such there there is no p with p.dim() == el.dim() + 1.
    2084             :   // We store whether to skip any given dimension in the construction of interior parents
    2085             :   // inside the vector in dimensions_to_skip_for_interior_parents.
    2086        6685 :   std::vector<bool> skip_dimension_for_interior_parents(/*count=*/LIBMESH_DIM+1, /*value=*/false);
    2087        6497 :   skip_dimension_for_interior_parents.back() = true;
    2088             : 
    2089             :   // Moreover, in the following, we will build a node-to-elem map.
    2090             :   // It is among the elems of this map that we will look for interior parents.
    2091             :   // Therefore, we can skip all elems p such that there is no el with el.dim() == p.dim() - 1.
    2092             :   // We store whether to skip any given dimension in the construction of the node-to-elem map
    2093             :   // in the vector skip_dimensions_for_node_to_el_map.
    2094        6685 :   std::vector<bool> skip_dimensions_for_node_to_el_map(/*count=*/LIBMESH_DIM+1, /*value=*/false);
    2095        6685 :   skip_dimensions_for_node_to_el_map[*dim_start] = true;
    2096             : 
    2097             :   // We also create a flag to know if all dimensions should be skipped,
    2098             :   // and if we should therefore return early.
    2099         188 :   bool skip_all_dimensions = true;
    2100             : 
    2101             :   // Fill dimensions_to_skip_for_interior_parents and dimensions_to_skip_for_node_to_el_map.
    2102        6877 :   for (auto [it, next] = std::make_tuple(dim_start, std::next(dim_start));
    2103       13704 :        next != dim_end; ++it, ++next)
    2104             :     {
    2105        6827 :       if (*it + 1 != *next) // if sequential dimensions differ by exactly 1
    2106             :         {
    2107          80 :           skip_dimension_for_interior_parents[*it] = true;
    2108        1431 :           skip_dimensions_for_node_to_el_map[*next] = true;
    2109             :         }
    2110        5548 :       else if (!skip_dimension_for_interior_parents[*it])
    2111         152 :         skip_all_dimensions = false;
    2112             :     }
    2113             : 
    2114             :   // There is nothing to do if all dimensions should be
    2115             :   // skipped. Before returning, we must also set the flag that says
    2116             :   // "interior parent pointers have been set up" even though we
    2117             :   // determined there was no work to be done.
    2118        6685 :   if (skip_all_dimensions)
    2119             :     {
    2120        1289 :       _preparation.has_interior_parent_ptrs = true;
    2121          36 :       return;
    2122             :     }
    2123             : 
    2124             :   // Do we have interior parent pointers going to a different mesh?
    2125             :   // If so then we'll still check to make sure that's the only place
    2126             :   // they go, so we can libmesh_not_implemented() if not.
    2127         304 :   const bool separate_interior_mesh = (&(this->interior_mesh()) != this);
    2128             : 
    2129             :   // This map will be used to set interior parents
    2130         152 :   std::unordered_map<dof_id_type, std::vector<dof_id_type>> node_to_elem;
    2131             : 
    2132      303450 :   for (const auto & elem : this->element_ptr_range())
    2133             :     {
    2134             :       // Ignore element if it cannot be interior parent of any other elem.
    2135      154551 :       if (skip_dimensions_for_node_to_el_map[elem->dim()])
    2136       64974 :         continue;
    2137             : 
    2138             :       // Populating the node_to_elem map, same as MeshTools::build_nodes_to_elem_map
    2139      495939 :       for (auto n : make_range(elem->n_vertices()))
    2140             :         {
    2141       24992 :           libmesh_assert_less (elem->id(), this->max_elem_id());
    2142             : 
    2143      433656 :           node_to_elem[elem->node_id(n)].push_back(elem->id());
    2144             :         }
    2145        5092 :     }
    2146             : 
    2147             :   // Automatically set interior parents
    2148      303450 :   for (const auto & element : this->element_ptr_range())
    2149             :     {
    2150             :       // Ignore elements with dimensions to skip
    2151             :       // or elements that already have an interior parent.
    2152      154551 :       if (skip_dimension_for_interior_parents[element->dim()] || element->interior_parent())
    2153       32778 :         continue;
    2154             : 
    2155             :       // Start by generating sets of dim+1 dimensional elements that
    2156             :       // touch each vertex of the current element.  If we encounter a
    2157             :       // vertex not connected to _any_ dim+1 dimensional elements,
    2158             :       // then we can exit the loop without checking the remaining
    2159             :       // vertices since an interior parent (if it exists) will be
    2160             :       // connected to all vertices of the current element.
    2161      135413 :       std::vector<std::set<dof_id_type>> neighbors( element->n_vertices() );
    2162             : 
    2163        6820 :       bool found_interior_parents = true;
    2164             : 
    2165      124968 :       for (auto n : make_range(element->n_vertices()))
    2166             :         {
    2167      130856 :           auto it = node_to_elem.find(element->node_id(n));
    2168             : 
    2169             :           // Check at first that this node is not isolated.
    2170      123974 :           if (it == node_to_elem.end())
    2171             :             {
    2172        1472 :               found_interior_parents = false;
    2173        6792 :               break; // out of n-loop
    2174             :             }
    2175             : 
    2176      356987 :           for (const auto & vertex_neighbor_id : it->second)
    2177      285119 :             if (this->elem_ref(vertex_neighbor_id).dim() == element->dim()+1)
    2178        8733 :               neighbors[n].insert(vertex_neighbor_id);
    2179             : 
    2180       77278 :           if (neighbors[n].empty())
    2181             :             {
    2182             :               // We have found an empty set for one vertex, no reason
    2183             :               // to continue.
    2184        5320 :               found_interior_parents = false;
    2185        5320 :               break; // out of n-loop
    2186             :             }
    2187             :         }
    2188             : 
    2189             :       // If we have generated a non-empty set of elements for each
    2190             :       // vertex, we will now look for a vertex_neighbor_id that
    2191             :       // appears in _all_ of those sets.  If found, this is our interior
    2192             :       // parent id.  If multiple such common ids are found, we will
    2193             :       // take the lowest such id to be the interior parent id.
    2194        6820 :       if (found_interior_parents)
    2195             :         {
    2196          56 :           std::set<dof_id_type> & neighbors_0 = neighbors[0];
    2197        1633 :           for (const auto & interior_parent_id : neighbors_0)
    2198             :             {
    2199          40 :               found_interior_parents = false;
    2200        2627 :               for (auto n : make_range(1u, element->n_vertices()))
    2201             :                 {
    2202        1633 :                   if (neighbors[n].count(interior_parent_id))
    2203             :                     {
    2204          34 :                       found_interior_parents = true;
    2205             :                     }
    2206             :                   else
    2207             :                     {
    2208          12 :                       found_interior_parents = false;
    2209          12 :                       break;
    2210             :                     }
    2211             :                 }
    2212             : 
    2213        1420 :               if (found_interior_parents)
    2214             :                 {
    2215         781 :                   element->set_interior_parent(this->elem_ptr(interior_parent_id));
    2216          22 :                   break;
    2217             :                 }
    2218             :             }
    2219             : 
    2220             :           // Do we have a mixed dimensional mesh that contains some of
    2221             :           // its own interior parents, but we already expect to have
    2222             :           // interior parents on a different mesh?  That's going to
    2223             :           // take some work to support if anyone needs it.
    2224         994 :           if (separate_interior_mesh)
    2225           0 :             libmesh_not_implemented_msg
    2226             :               ("interior_parent() values in multiple meshes are unsupported.");
    2227             :         }
    2228      113225 :     }
    2229             : 
    2230             :   // This flag doesn't necessarily mean any Elems actually have
    2231             :   // interior parent pointers, just that we did all the work to
    2232             :   // determine whether or not they do.
    2233        5396 :   _preparation.has_interior_parent_ptrs = true;
    2234             : }
    2235             : 
    2236             : 
    2237             : 
    2238             : #ifdef LIBMESH_ENABLE_PERIODIC
    2239             :   /**
    2240             :    * Register a pair of boundaries as disjoint neighbor boundary pairs.
    2241             :    */
    2242         994 :   void MeshBase::add_disjoint_neighbor_boundary_pairs(const boundary_id_type b1,
    2243             :                                              const boundary_id_type b2,
    2244             :                                              const RealVectorValue & translation)
    2245             :     {
    2246             :       // Lazily allocate the container the first time it’s needed
    2247         994 :       if (!_disjoint_neighbor_boundary_pairs)
    2248        1496 :         _disjoint_neighbor_boundary_pairs = std::make_unique<PeriodicBoundaries>();
    2249             : 
    2250          28 :       PeriodicBoundaries & db = *_disjoint_neighbor_boundary_pairs;
    2251             : 
    2252             :       // Create forward and inverse boundary mappings
    2253        1022 :       PeriodicBoundary forward(translation);
    2254         994 :       PeriodicBoundary inverse(translation * -1.0);
    2255             : 
    2256         994 :       forward.myboundary       = b1;
    2257         994 :       forward.pairedboundary   = b2;
    2258         994 :       inverse.myboundary       = b2;
    2259         994 :       inverse.pairedboundary   = b1;
    2260             : 
    2261             :       // Add both directions into the container
    2262         994 :       db.emplace(b1, forward.clone());
    2263        1022 :       db.emplace(b2, inverse.clone());
    2264         994 :     }
    2265             : 
    2266      565115 :   PeriodicBoundaries * MeshBase::get_disjoint_neighbor_boundary_pairs()
    2267             :     {
    2268      565115 :       return _disjoint_neighbor_boundary_pairs.get();
    2269             :     }
    2270             : 
    2271     2682151 :   const PeriodicBoundaries * MeshBase::get_disjoint_neighbor_boundary_pairs() const
    2272             :     {
    2273     2682151 :       return _disjoint_neighbor_boundary_pairs.get();
    2274             :     }
    2275             : 
    2276         213 :     void MeshBase::remove_disjoint_boundary_pair(const boundary_id_type b1,
    2277             :                                                    const boundary_id_type b2)
    2278             :     {
    2279             :       // Nothing to remove if not allocated or empty
    2280         213 :       if (!_disjoint_neighbor_boundary_pairs || _disjoint_neighbor_boundary_pairs->empty())
    2281           0 :         return;
    2282             : 
    2283           6 :       auto & pairs = *_disjoint_neighbor_boundary_pairs;
    2284             : 
    2285             :       // Helper to check and erase both directions
    2286         402 :       auto erase_if_match = [](boundary_id_type key,
    2287             :                               boundary_id_type pair,
    2288          24 :                               PeriodicBoundaries & pb_map)
    2289             :         {
    2290          12 :           auto it = pb_map.find(key);
    2291         426 :           if (it != pb_map.end())
    2292             :             {
    2293          12 :               const auto & pb = *(it->second);
    2294             :               // Check both directions
    2295         426 :               if ((pb.myboundary == key && pb.pairedboundary == pair) ||
    2296           0 :                   (pb.pairedboundary == key && pb.myboundary == pair))
    2297         414 :                 pb_map.erase(it);
    2298             :             }
    2299         426 :         };
    2300             : 
    2301         213 :       erase_if_match(b1, b2, pairs);
    2302         213 :       erase_if_match(b2, b1, pairs);
    2303             :     }
    2304             : 
    2305             : 
    2306             : #endif
    2307             : 
    2308             : 
    2309             : 
    2310           0 : void MeshBase::set_point_locator_close_to_point_tol(Real val)
    2311             : {
    2312           0 :   _point_locator_close_to_point_tol = val;
    2313           0 :   if (_point_locator)
    2314             :     {
    2315           0 :       if (val > 0.)
    2316           0 :         _point_locator->set_close_to_point_tol(val);
    2317             :       else
    2318           0 :         _point_locator->unset_close_to_point_tol();
    2319             :     }
    2320           0 : }
    2321             : 
    2322             : 
    2323             : 
    2324         426 : Real MeshBase::get_point_locator_close_to_point_tol() const
    2325             : {
    2326         426 :   return _point_locator_close_to_point_tol;
    2327             : }
    2328             : 
    2329             : 
    2330             : 
    2331        5335 : void MeshBase::size_elem_extra_integers()
    2332             : {
    2333         308 :   const std::size_t new_size = _elem_integer_names.size();
    2334             : 
    2335             :   Threads::parallel_for
    2336        5335 :     (this->element_stored_range(),
    2337       10972 :      [new_size, this](const ElemRange & range)
    2338             :      {
    2339       18640 :        for (Elem * elem : range)
    2340       13305 :          elem->add_extra_integers(new_size, this->_elem_integer_default_values);
    2341        5181 :      });
    2342        5335 : }
    2343             : 
    2344             : 
    2345             : 
    2346       16602 : void MeshBase::size_node_extra_integers()
    2347             : {
    2348         966 :   const std::size_t new_size = _node_integer_names.size();
    2349      417025 :   for (auto node : this->node_ptr_range())
    2350      216374 :     node->add_extra_integers(new_size, _node_integer_default_values);
    2351       16602 : }
    2352             : 
    2353             : 
    2354             : std::pair<std::vector<unsigned int>, std::vector<unsigned int>>
    2355       26180 : MeshBase::merge_extra_integer_names(const MeshBase & other)
    2356             : {
    2357         854 :   std::pair<std::vector<unsigned int>, std::vector<unsigned int>> returnval;
    2358       26180 :   returnval.first = this->add_elem_integers(other._elem_integer_names, true, &other._elem_integer_default_values);
    2359       26180 :   returnval.second = this->add_node_integers(other._node_integer_names, true, &other._node_integer_default_values);
    2360       26180 :   return returnval;
    2361             : }
    2362             : 
    2363             : 
    2364             : 
    2365             : void
    2366         426 : MeshBase::post_dofobject_moves(MeshBase && other_mesh)
    2367             : {
    2368             :   // Now that all the DofObject moving is done, we can move the GhostingFunctor objects
    2369             :   // which include the _default_ghosting,_ghosting_functors and _shared_functors. We also need
    2370             :   // to set the mesh object associated with these functors to the assignee mesh.
    2371             : 
    2372             :    // _default_ghosting
    2373          12 :   _default_ghosting = std::move(other_mesh._default_ghosting);
    2374         426 :   _default_ghosting->set_mesh(this);
    2375             : 
    2376             :   // _ghosting_functors
    2377         426 :   _ghosting_functors = std::move(other_mesh._ghosting_functors);
    2378             : 
    2379         852 :   for (const auto gf : _ghosting_functors )
    2380             :   {
    2381         426 :     gf->set_mesh(this);
    2382             :   }
    2383             : 
    2384             :   // _shared_functors
    2385          12 :   _shared_functors = std::move(other_mesh._shared_functors);
    2386             : 
    2387         426 :   for (const auto & sf : _shared_functors )
    2388             :   {
    2389           0 :     (sf.second)->set_mesh(this);
    2390             :   }
    2391             : 
    2392             :   // _constraint_rows
    2393          12 :   _constraint_rows = std::move(other_mesh._constraint_rows);
    2394             : 
    2395         426 :   if (other_mesh.partitioner())
    2396         426 :     _partitioner = std::move(other_mesh.partitioner());
    2397         426 : }
    2398             : 
    2399             : 
    2400             : void
    2401           0 : MeshBase::copy_cached_data(const MeshBase & other_mesh)
    2402             : {
    2403           0 :   this->_spatial_dimension = other_mesh._spatial_dimension;
    2404           0 :   this->_elem_dims = other_mesh._elem_dims;
    2405           0 :   this->_elem_default_orders = other_mesh._elem_default_orders;
    2406           0 :   this->_supported_nodal_order = other_mesh._supported_nodal_order;
    2407           0 :   this->_mesh_subdomains = other_mesh._mesh_subdomains;
    2408           0 : }
    2409             : 
    2410             : 
    2411       14725 : bool MeshBase::nodes_and_elements_equal(const MeshBase & other_mesh) const
    2412             : {
    2413     2826850 :   for (const auto & other_node : other_mesh.node_ptr_range())
    2414             :     {
    2415     1524181 :       const Node * node = this->query_node_ptr(other_node->id());
    2416     1524181 :       if (!node)
    2417           0 :         return false;
    2418     1524181 :       if (*other_node != *node)
    2419           0 :         return false;
    2420       13279 :     }
    2421     2826850 :   for (const auto & node : this->node_ptr_range())
    2422     1524181 :     if (!other_mesh.query_node_ptr(node->id()))
    2423       13279 :       return false;
    2424             : 
    2425     1253356 :   for (const auto & other_elem : other_mesh.element_ptr_range())
    2426             :     {
    2427      789217 :       const Elem * elem = this->query_elem_ptr(other_elem->id());
    2428      789217 :       if (!elem)
    2429           0 :         return false;
    2430      789217 :       if (!other_elem->topologically_equal(*elem))
    2431           6 :         return false;
    2432       13279 :     }
    2433     1253080 :   for (const auto & elem : this->element_ptr_range())
    2434      789076 :     if (!other_mesh.query_elem_ptr(elem->id()))
    2435       13150 :       return false;
    2436             : 
    2437       14584 :   return true;
    2438             : }
    2439             : 
    2440             : 
    2441         764 : dof_id_type MeshBase::n_constraint_rows() const
    2442             : {
    2443         764 :   dof_id_type n_local_rows=0, n_unpartitioned_rows=0;
    2444         764 :   for (const auto & [node, node_constraints] : _constraint_rows)
    2445             :     {
    2446             :       // Unpartitioned nodes
    2447           0 :       if (node->processor_id() == DofObject::invalid_processor_id)
    2448           0 :         n_unpartitioned_rows++;
    2449           0 :       else if (node->processor_id() == this->processor_id())
    2450           0 :         n_local_rows++;
    2451             :     }
    2452             : 
    2453         764 :   this->comm().sum(n_local_rows);
    2454             : 
    2455         764 :   return n_unpartitioned_rows + n_local_rows;
    2456             : }
    2457             : 
    2458             : 
    2459             : void
    2460       24405 : MeshBase::copy_constraint_rows(const MeshBase & other_mesh)
    2461             : {
    2462        1608 :   LOG_SCOPE("copy_constraint_rows(mesh)", "MeshBase");
    2463             : 
    2464        1592 :   _constraint_rows.clear();
    2465             : 
    2466         804 :   const auto & other_constraint_rows = other_mesh.get_constraint_rows();
    2467       72833 :   for (const auto & [other_node, other_node_constraints] : other_constraint_rows)
    2468             :   {
    2469       48428 :     const Node * const our_node = this->node_ptr(other_node->id());
    2470        6416 :     constraint_rows_mapped_type our_node_constraints;
    2471      219637 :     for (const auto & [other_inner_key_pair, constraint_value] : other_node_constraints)
    2472             :     {
    2473        9048 :       const auto & [other_elem, local_node_id] = other_inner_key_pair;
    2474      171209 :       const Elem * const our_elem = this->elem_ptr(other_elem->id());
    2475      171209 :       our_node_constraints.emplace_back(std::make_pair(our_elem, local_node_id), constraint_value);
    2476             :     }
    2477       48428 :     _constraint_rows[our_node] = std::move(our_node_constraints);
    2478             :   }
    2479       24405 : }
    2480             : 
    2481             : 
    2482             : template <typename T>
    2483             : void
    2484         565 : MeshBase::copy_constraint_rows(const SparseMatrix<T> & constraint_operator,
    2485             :                                bool precondition_constraint_operator)
    2486             : {
    2487          32 :   LOG_SCOPE("copy_constraint_rows(mat)", "MeshBase");
    2488             : 
    2489          16 :   this->_constraint_rows.clear();
    2490             : 
    2491             :   // We're not going to support doing this distributed yet; it'd be
    2492             :   // pointless unless we temporarily had a linear partitioning to
    2493             :   // better match the constraint operator.
    2494         597 :   MeshSerializer serialize(*this);
    2495             : 
    2496             :   // Our current mesh should already reflect the desired assembly space
    2497         565 :   libmesh_error_msg_if(this->n_nodes() != constraint_operator.m(),
    2498             :                        "Constraint operator matrix with " <<
    2499             :                        constraint_operator.m() <<
    2500             :                        "rows does not match this mesh with " <<
    2501             :                        this->n_nodes() << " nodes");
    2502             : 
    2503             :   // First, find what new unconstrained DoFs we need to add.  We can't
    2504             :   // iterate over columns in a SparseMatrix, so we'll iterate over
    2505             :   // rows and keep track of columns.
    2506             : 
    2507             :   // If we have nodes that will work unconstrained, keep track of
    2508             :   // their node ids and corresponding column indices.
    2509             :   // existing_unconstrained_nodes[column_id] = node_id
    2510          32 :   std::map<dof_id_type, dof_id_type> existing_unconstrained_columns;
    2511          32 :   std::set<dof_id_type> existing_unconstrained_nodes;
    2512             : 
    2513             :   // In case we need new nodes, keep track of their columns.
    2514             :   // columns[j][k] will be the kth row index and value of column j
    2515             :   typedef
    2516             :     std::unordered_map<dof_id_type,
    2517             :                        std::vector<std::pair<dof_id_type, Real>>>
    2518             :     columns_type;
    2519        1114 :   columns_type columns(constraint_operator.n());
    2520             : 
    2521             :   // If we need to precondition the constraint operator (e.g.  it's an
    2522             :   // unpreconditioned extraction operator for a Flex IGA matrix),
    2523             :   // we'll want to keep track of the sum of each column, because we'll
    2524             :   // be dividing each column by that sum (Jacobi preconditioning on
    2525             :   // the right, which then leads to symmetric preconditioning on a
    2526             :   // physics Jacobian).
    2527          32 :   std::unordered_map<dof_id_type, Real> column_sums;
    2528             : 
    2529             :   // Work in parallel, though we'll have to sync shortly
    2530        4028 :   for (auto i : make_range(constraint_operator.row_start(),
    2531         533 :                            constraint_operator.row_stop()))
    2532             :     {
    2533         618 :       std::vector<numeric_index_type> indices;
    2534         618 :       std::vector<T> values;
    2535             : 
    2536        3463 :       constraint_operator.get_row(i, indices, values);
    2537         309 :       libmesh_assert_equal_to(indices.size(), values.size());
    2538             : 
    2539        3847 :       if (indices.size() == 1 &&
    2540         825 :           values[0] == T(1))
    2541             :         {
    2542             :           // If we have multiple simple Ui=Uj constraints, let the
    2543             :           // first one be our "unconstrained" node and let the others
    2544             :           // be constrained to it.
    2545         810 :           if (existing_unconstrained_columns.find(indices[0]) !=
    2546         138 :               existing_unconstrained_columns.end())
    2547             :             {
    2548          36 :               const auto j = indices[0];
    2549          36 :               columns[j].emplace_back(i, 1);
    2550             :             }
    2551             :           else
    2552             :             {
    2553         708 :               existing_unconstrained_nodes.insert(i);
    2554         774 :               existing_unconstrained_columns.emplace(indices[0],i);
    2555             :             }
    2556             :         }
    2557             :       else
    2558       22028 :         for (auto jj : index_range(indices))
    2559             :           {
    2560       19375 :             const auto j = indices[jj];
    2561       21134 :             const Real coef = libmesh_real(values[jj]);
    2562        1759 :             libmesh_assert_equal_to(coef, values[jj]);
    2563       19375 :             columns[j].emplace_back(i, coef);
    2564             :           }
    2565             :     }
    2566             : 
    2567             :   // Merge data from different processors' slabs of the matrix
    2568         565 :   this->comm().set_union(existing_unconstrained_nodes);
    2569         565 :   this->comm().set_union(existing_unconstrained_columns);
    2570             : 
    2571          48 :   std::vector<columns_type> all_columns;
    2572         565 :   this->comm().allgather(columns, all_columns);
    2573             : 
    2574          16 :   columns.clear();
    2575        6106 :   for (auto p : index_range(all_columns))
    2576       65601 :     for (auto & [j, subcol] : all_columns[p])
    2577      183429 :       for (auto [i, v] : subcol)
    2578      123369 :         columns[j].emplace_back(i,v);
    2579             : 
    2580             :   // Keep track of elements on which unconstrained nodes exist, and
    2581             :   // their local node indices.
    2582             :   // node_to_elem_ptrs[node] = [elem_id, local_node_num]
    2583          32 :   std::unordered_map<const Node *, std::pair<dof_id_type, unsigned int>> node_to_elem_ptrs;
    2584             : 
    2585             :   // Find elements attached to any existing nodes that will stay
    2586             :   // unconstrained.  We'll also build a subdomain set here so we don't
    2587             :   // have to assert that the mesh is already prepared before we pick a
    2588             :   // new subdomain for any NodeElems we need to add.
    2589          32 :   std::set<subdomain_id_type> subdomain_ids;
    2590      192079 :   for (const Elem * elem : this->element_ptr_range())
    2591             :     {
    2592       49738 :       subdomain_ids.insert(elem->subdomain_id());
    2593      244572 :       for (auto n : make_range(elem->n_nodes()))
    2594             :         {
    2595      194834 :           const Node * node = elem->node_ptr(n);
    2596      194834 :           if (existing_unconstrained_nodes.count(node->id()))
    2597       12175 :             node_to_elem_ptrs.emplace(node, std::make_pair(elem->id(), n));
    2598             :         }
    2599             :     }
    2600             : 
    2601         565 :   const subdomain_id_type new_sbd_id = *subdomain_ids.rbegin() + 1;
    2602             : 
    2603       23189 :   for (auto j : make_range(constraint_operator.n()))
    2604             :     {
    2605             :       // If we already have a good node for this then we're done
    2606        4800 :       if (existing_unconstrained_columns.count(j))
    2607        4668 :         continue;
    2608             : 
    2609             :       // Get a half-decent spot to place a new NodeElem for
    2610             :       // unconstrained DoF(s) here.  Getting a *fully*-decent spot
    2611             :       // would require finding a Moore-Penrose pseudoinverse, and I'm
    2612             :       // not going to do that, but scaling a transpose will at least
    2613             :       // get us a little uniqueness to make visualization reasonable.
    2614         264 :       Point newpt;
    2615         264 :       Real total_scaling = 0;
    2616         264 :       unsigned int total_entries = 0;
    2617             : 
    2618             :       // We'll get a decent initial pid choice here too, if only to
    2619             :       // aid in later repartitioning.
    2620         528 :       std::map<processor_id_type, int> pids;
    2621             : 
    2622         264 :       auto & column = columns[j];
    2623      122230 :       for (auto [i, r] : column)
    2624             :         {
    2625      112988 :           Node & constrained_node = this->node_ref(i);
    2626      112988 :           const Point constrained_pt = constrained_node;
    2627        3228 :           newpt += r*constrained_pt;
    2628      112988 :           total_scaling += r;
    2629      112988 :           ++total_entries;
    2630      112988 :           ++pids[constrained_node.processor_id()];
    2631             :         }
    2632             : 
    2633        9242 :       if (precondition_constraint_operator)
    2634           0 :         column_sums[j] = total_scaling;
    2635             : 
    2636        9242 :       libmesh_error_msg_if
    2637             :         (!total_entries,
    2638             :          "Empty column " << j <<
    2639             :          " found in constraint operator matrix");
    2640             : 
    2641             :       // If we have *cancellation* here then we can end up dividing by
    2642             :       // zero; try just evenly scaling across all constrained node
    2643             :       // points instead.
    2644        9242 :       if (total_scaling > TOLERANCE)
    2645         264 :         newpt /= total_scaling;
    2646             :       else
    2647           0 :         newpt /= total_entries;
    2648             : 
    2649        9242 :       Node *n = this->add_point(newpt);
    2650        9506 :       std::unique_ptr<Elem> elem = Elem::build(NODEELEM);
    2651        9242 :       elem->set_node(0, n);
    2652        9242 :       elem->subdomain_id() = new_sbd_id;
    2653             : 
    2654        9506 :       Elem * added_elem = this->add_elem(std::move(elem));
    2655        9242 :       this->_elem_dims.insert(0);
    2656        9242 :       this->_elem_default_orders.insert(added_elem->default_order());
    2657        9242 :       this->_supported_nodal_order =
    2658        8714 :         static_cast<Order>
    2659       18484 :           (std::min(static_cast<int>(this->_supported_nodal_order),
    2660        9242 :                     static_cast<int>(added_elem->supported_nodal_order())));
    2661        8978 :       this->_mesh_subdomains.insert(new_sbd_id);
    2662        9242 :       node_to_elem_ptrs.emplace(n, std::make_pair(added_elem->id(), 0));
    2663        9242 :       existing_unconstrained_columns.emplace(j,n->id());
    2664             : 
    2665             :       // Repartition the new objects *after* adding them, so a
    2666             :       // DistributedMesh doesn't get confused and think you're not
    2667             :       // adding them on all processors at once.
    2668         264 :       int n_pids = 0;
    2669       43912 :       for (auto [pid, count] : pids)
    2670       34670 :         if (count >= n_pids)
    2671             :           {
    2672         324 :             n_pids = count;
    2673       15469 :             added_elem->processor_id() = pid;
    2674       15469 :             n->processor_id() = pid;
    2675             :           }
    2676             :     }
    2677             : 
    2678             :   // Calculate constraint rows in an indexed form that's easy for us
    2679             :   // to allgather
    2680             :   std::unordered_map<dof_id_type,
    2681             :     std::vector<std::pair<std::pair<dof_id_type, unsigned int>,Real>>>
    2682          32 :     indexed_constraint_rows;
    2683             : 
    2684        4028 :   for (auto i : make_range(constraint_operator.row_start(),
    2685         533 :                            constraint_operator.row_stop()))
    2686             :     {
    2687         951 :       if (existing_unconstrained_nodes.count(i))
    2688         774 :         continue;
    2689             : 
    2690         486 :       std::vector<numeric_index_type> indices;
    2691         486 :       std::vector<T> values;
    2692             : 
    2693        2689 :       constraint_operator.get_row(i, indices, values);
    2694             : 
    2695         486 :       std::vector<std::pair<std::pair<dof_id_type, unsigned int>, Real>> constraint_row;
    2696             : 
    2697       22100 :       for (auto jj : index_range(indices))
    2698             :         {
    2699       21173 :           const dof_id_type node_id =
    2700             :             existing_unconstrained_columns[indices[jj]];
    2701             : 
    2702       19411 :           Node & constraining_node = this->node_ref(node_id);
    2703             : 
    2704        1762 :           libmesh_assert(node_to_elem_ptrs.count(&constraining_node));
    2705             : 
    2706       19411 :           auto p = node_to_elem_ptrs[&constraining_node];
    2707             : 
    2708       19382 :           Real coef = libmesh_real(values[jj]);
    2709        1762 :           libmesh_assert_equal_to(coef, values[jj]);
    2710             : 
    2711             :           // If we're preconditioning and we created a nodeelem then
    2712             :           // we can scale the meaning of that nodeelem's value to give
    2713             :           // us a better-conditioned matrix after the constraints are
    2714             :           // applied.
    2715       19411 :           if (precondition_constraint_operator)
    2716           0 :             if (auto sum_it = column_sums.find(indices[jj]);
    2717           0 :                 sum_it != column_sums.end())
    2718             :               {
    2719           0 :                 const Real scaling = sum_it->second;
    2720             : 
    2721           0 :                 if (scaling > TOLERANCE)
    2722           0 :                   coef /= scaling;
    2723             :               }
    2724             : 
    2725       21173 :           constraint_row.emplace_back(std::make_pair(p, coef));
    2726             :         }
    2727             : 
    2728         243 :       indexed_constraint_rows.emplace(i, std::move(constraint_row));
    2729             :     }
    2730             : 
    2731         565 :   this->comm().set_union(indexed_constraint_rows);
    2732             : 
    2733             :   // Add constraint rows as mesh constraint rows
    2734       17591 :   for (auto & [node_id, indexed_row] : indexed_constraint_rows)
    2735             :     {
    2736       17026 :       Node * constrained_node = this->node_ptr(node_id);
    2737             : 
    2738         972 :       constraint_rows_mapped_type constraint_row;
    2739             : 
    2740      140395 :       for (auto [p, coef] : indexed_row)
    2741             :         {
    2742      123369 :           const Elem * elem = this->elem_ptr(p.first);
    2743        7048 :           constraint_row.emplace_back
    2744      126893 :             (std::make_pair(std::make_pair(elem, p.second), coef));
    2745             :         }
    2746             : 
    2747       16540 :       this->_constraint_rows.emplace(constrained_node,
    2748         486 :                                      std::move(constraint_row));
    2749             :     }
    2750        1098 : }
    2751             : 
    2752             : 
    2753           0 : void MeshBase::print_constraint_rows(std::ostream & os,
    2754             :                                      bool print_nonlocal) const
    2755             : {
    2756           0 :   parallel_object_only();
    2757             : 
    2758             :   std::string local_constraints =
    2759           0 :     this->get_local_constraints(print_nonlocal);
    2760             : 
    2761           0 :   if (this->processor_id())
    2762             :     {
    2763           0 :       this->comm().send(0, local_constraints);
    2764             :     }
    2765             :   else
    2766             :     {
    2767           0 :       os << "Processor 0:\n";
    2768           0 :       os << local_constraints;
    2769             : 
    2770           0 :       for (auto p : IntRange<processor_id_type>(1, this->n_processors()))
    2771             :         {
    2772           0 :           this->comm().receive(p, local_constraints);
    2773           0 :           os << "Processor " << p << ":\n";
    2774           0 :           os << local_constraints;
    2775             :         }
    2776             :     }
    2777           0 : }
    2778             : 
    2779             : 
    2780             : 
    2781           0 : std::string MeshBase::get_local_constraints(bool print_nonlocal) const
    2782             : {
    2783           0 :   std::ostringstream os;
    2784             : 
    2785           0 :   if (print_nonlocal)
    2786           0 :     os << "All ";
    2787             :   else
    2788           0 :     os << "Local ";
    2789             : 
    2790           0 :   os << "Mesh Constraint Rows:"
    2791           0 :      << std::endl;
    2792             : 
    2793           0 :   for (const auto & [node, row] : _constraint_rows)
    2794             :     {
    2795           0 :       const bool local = (node->processor_id() == this->processor_id());
    2796             : 
    2797             :       // Skip non-local dofs if requested
    2798           0 :       if (!print_nonlocal && !local)
    2799           0 :         continue;
    2800             : 
    2801           0 :       os << "Constraints for " << (local ? "Local" : "Ghost") << " Node " << node->id()
    2802           0 :          << ": \t";
    2803             : 
    2804           0 :       for (const auto & [elem_and_node, coef] : row)
    2805           0 :         os << " ((" << elem_and_node.first->id() << ',' << elem_and_node.second << "), " << coef << ")\t";
    2806             : 
    2807           0 :       os << std::endl;
    2808             :     }
    2809             : 
    2810           0 :   return os.str();
    2811           0 : }
    2812             : 
    2813      332543 : MeshBase::Preparation::Preparation() :
    2814      313235 :   is_partitioned(false),
    2815      313235 :   has_synched_id_counts(false),
    2816      313235 :   has_neighbor_ptrs(false),
    2817      313235 :   has_cached_elem_data(false),
    2818      313235 :   has_interior_parent_ptrs(false),
    2819      313235 :   has_removed_remote_elements(false),
    2820      313235 :   has_removed_orphaned_nodes(false),
    2821      313235 :   has_boundary_id_sets(false),
    2822      313235 :   has_reinit_ghosting_functors(false),
    2823      332543 :   has_synched_subdomain_name_map(false)
    2824      332543 : {}
    2825             : 
    2826      102734 : MeshBase::Preparation::operator bool() const
    2827             : {
    2828      176096 :   return is_partitioned &&
    2829       73362 :          has_synched_id_counts &&
    2830       73362 :          has_neighbor_ptrs &&
    2831       73362 :          has_cached_elem_data &&
    2832       73362 :          has_interior_parent_ptrs &&
    2833       73362 :          has_removed_remote_elements &&
    2834       73362 :          has_removed_orphaned_nodes &&
    2835       73362 :          has_reinit_ghosting_functors &&
    2836      217322 :          has_boundary_id_sets &&
    2837      115466 :          has_synched_subdomain_name_map;
    2838             : }
    2839             : 
    2840             : MeshBase::Preparation &
    2841     1453878 : MeshBase::Preparation::operator= (bool set_all)
    2842             : {
    2843     1453878 :   is_partitioned = set_all;
    2844     1453878 :   has_synched_id_counts = set_all;
    2845     1453878 :   has_neighbor_ptrs = set_all;
    2846     1453878 :   has_cached_elem_data = set_all;
    2847     1453878 :   has_interior_parent_ptrs = set_all;
    2848     1453878 :   has_removed_remote_elements = set_all;
    2849     1453878 :   has_removed_orphaned_nodes = set_all;
    2850     1453878 :   has_reinit_ghosting_functors = set_all;
    2851     1453878 :   has_boundary_id_sets = set_all;
    2852     1453878 :   has_synched_subdomain_name_map = set_all;
    2853             : 
    2854     1453878 :   return *this;
    2855             : }
    2856             : 
    2857             : bool
    2858       15151 : MeshBase::Preparation::operator== (const Preparation & other) const
    2859             : {
    2860       15151 :   if (is_partitioned != other.is_partitioned)
    2861           0 :     return false;
    2862       15151 :   if (has_synched_id_counts != other.has_synched_id_counts)
    2863           0 :     return false;
    2864       15151 :   if (has_neighbor_ptrs != other.has_neighbor_ptrs)
    2865           0 :     return false;
    2866       15151 :   if (has_cached_elem_data != other.has_cached_elem_data)
    2867           0 :     return false;
    2868       15151 :   if (has_interior_parent_ptrs != other.has_interior_parent_ptrs)
    2869           0 :     return false;
    2870       15151 :   if (has_removed_remote_elements != other.has_removed_remote_elements)
    2871           0 :     return false;
    2872       15151 :   if (has_removed_orphaned_nodes != other.has_removed_orphaned_nodes)
    2873           0 :     return false;
    2874       15151 :   if (has_reinit_ghosting_functors != other.has_reinit_ghosting_functors)
    2875           0 :     return false;
    2876       15151 :   if (has_boundary_id_sets != other.has_boundary_id_sets)
    2877           0 :     return false;
    2878       15151 :   if (has_synched_subdomain_name_map != other.has_synched_subdomain_name_map)
    2879           0 :     return false;
    2880             : 
    2881        1062 :   return true;
    2882             : }
    2883             : 
    2884             : bool
    2885       15151 : MeshBase::Preparation::operator!= (const Preparation & other) const
    2886             : {
    2887       15151 :   return !(*this == other);
    2888             : }
    2889             : 
    2890             : 
    2891             : // Explicit instantiations for our template function
    2892             : template LIBMESH_EXPORT void
    2893             : MeshBase::copy_constraint_rows(const SparseMatrix<Real> & constraint_operator,
    2894             :                                bool precondition_constraint_operator);
    2895             : 
    2896             : #ifdef LIBMESH_USE_COMPLEX_NUMBERS
    2897             : template LIBMESH_EXPORT void
    2898             : MeshBase::copy_constraint_rows(const SparseMatrix<Complex> & constraint_operator,
    2899             :                                bool precondition_constraint_operator);
    2900             : #endif
    2901             : 
    2902             : 
    2903             : } // namespace libMesh

Generated by: LCOV version 1.14