LCOV - code coverage report
Current view: top level - src/base - dof_map.C (source / functions) Hit Total Coverage
Test: libMesh/libmesh: #4417 (f0ff5e) with base 9bbd75 Lines: 1105 1298 85.1 %
Date: 2026-11-16 16:30:47 Functions: 108 120 90.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : // The libMesh Finite Element Library.
       2             : // Copyright (C) 2002-2026 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
       3             : 
       4             : // This library is free software; you can redistribute it and/or
       5             : // modify it under the terms of the GNU Lesser General Public
       6             : // License as published by the Free Software Foundation; either
       7             : // version 2.1 of the License, or (at your option) any later version.
       8             : 
       9             : // This library is distributed in the hope that it will be useful,
      10             : // but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      12             : // Lesser General Public  License for more details.
      13             : 
      14             : // You should have received a copy of the GNU Lesser General Public
      15             : // License along with this library; if not, write to the Free Software
      16             : // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
      17             : 
      18             : 
      19             : 
      20             : // Local includes
      21             : #include "libmesh/dof_map.h"
      22             : 
      23             : // libMesh includes
      24             : #include "libmesh/coupling_matrix.h"
      25             : #include "libmesh/default_coupling.h"
      26             : #include "libmesh/dense_matrix.h"
      27             : #include "libmesh/dense_vector_base.h"
      28             : #include "libmesh/dirichlet_boundaries.h"
      29             : #include "libmesh/enum_to_string.h"
      30             : #include "libmesh/fe_type.h"
      31             : #include "libmesh/fe_base.h" // FEBase::build() for continuity test
      32             : #include "libmesh/ghosting_functor.h"
      33             : #include "libmesh/int_range.h"
      34             : #include "libmesh/mesh_base.h"
      35             : #include "libmesh/mesh_tools.h"
      36             : #include "libmesh/node_range.h"
      37             : #include "libmesh/numeric_vector.h"
      38             : #include "libmesh/periodic_boundary_base.h"
      39             : #include "libmesh/periodic_boundaries.h"
      40             : #include "libmesh/sparse_matrix.h"
      41             : #include "libmesh/sparsity_pattern.h"
      42             : #include "libmesh/threads.h"
      43             : #include "libmesh/static_condensation_dof_map.h"
      44             : #include "libmesh/system.h"
      45             : #include "libmesh/parallel_fe_type.h"
      46             : 
      47             : // TIMPI includes
      48             : #include "timpi/parallel_implementation.h"
      49             : #include "timpi/parallel_sync.h"
      50             : 
      51             : // C++ Includes
      52             : #include <algorithm> // for std::fill, std::equal_range, std::max, std::lower_bound, etc.
      53             : #include <memory>
      54             : #include <set>
      55             : #include <sstream>
      56             : #include <unordered_map>
      57             : 
      58             : namespace libMesh
      59             : {
      60             : 
      61             : // ------------------------------------------------------------
      62             : // DofMap member functions
      63             : std::unique_ptr<SparsityPattern::Build>
      64       45704 : DofMap::build_sparsity (const MeshBase & mesh,
      65             :                         const bool calculate_constrained,
      66             :                         const bool use_condensed_system) const
      67             : {
      68        1410 :   libmesh_assert (mesh.is_prepared());
      69             : 
      70        4230 :   LOG_SCOPE("build_sparsity()", "DofMap");
      71             : 
      72             :   // Compute the sparsity structure of the global matrix.  This can be
      73             :   // fed into a PetscMatrixBase to allocate exactly the number of nonzeros
      74             :   // necessary to store the matrix.  This algorithm should be linear
      75             :   // in the (# of elements)*(# nodes per element)
      76             : 
      77             :   // We can be more efficient in the threaded sparsity pattern assembly
      78             :   // if we don't need the exact pattern.  For some sparse matrix formats
      79             :   // a good upper bound will suffice.
      80             : 
      81             :   // See if we need to include sparsity pattern entries for coupling
      82             :   // between neighbor dofs
      83       45704 :   bool implicit_neighbor_dofs = this->use_coupled_neighbor_dofs(mesh);
      84             : 
      85       44294 :   const StaticCondensationDofMap * sc = nullptr;
      86       45704 :   if (use_condensed_system)
      87             :     {
      88         118 :       libmesh_assert(this->has_static_condensation());
      89        4012 :       sc = _sc.get();
      90             :     }
      91             : 
      92             :   // We can compute the sparsity pattern in parallel on multiple
      93             :   // threads.  The goal is for each thread to compute the full sparsity
      94             :   // pattern for a subset of elements.  These sparsity patterns can
      95             :   // be efficiently merged in the SparsityPattern::Build::join()
      96             :   // method, especially if there is not too much overlap between them.
      97             :   // Even better, if the full sparsity pattern is not needed then
      98             :   // the number of nonzeros per row can be estimated from the
      99             :   // sparsity patterns created on each thread.
     100             :   auto sp = std::make_unique<SparsityPattern::Build>
     101             :     (*this,
     102       44294 :      this->_dof_coupling,
     103       45704 :      this->_coupling_functors,
     104             :      implicit_neighbor_dofs,
     105       44294 :      _need_full_sparsity_pattern,
     106             :      calculate_constrained,
     107       45704 :      sc);
     108             : 
     109       45704 :   Threads::parallel_reduce (mesh.active_local_element_stored_range(), *sp);
     110             : 
     111       45704 :   sp->parallel_sync();
     112             : 
     113        1410 :   libmesh_assert_equal_to (sp->get_sparsity_pattern().size(), this->n_local_dofs());
     114             : 
     115             :   // Check to see if we have any extra stuff to add to the sparsity_pattern
     116       45704 :   if (_extra_sparsity_function)
     117             :     {
     118           0 :       if (_augment_sparsity_pattern)
     119             :         {
     120           0 :           libmesh_here();
     121           0 :           libMesh::out << "WARNING:  You have specified both an extra sparsity function and object.\n"
     122           0 :                        << "          Are you sure this is what you meant to do??"
     123           0 :                        << std::endl;
     124             :         }
     125             : 
     126           0 :       sp->apply_extra_sparsity_function(_extra_sparsity_function,
     127           0 :                                         _extra_sparsity_context);
     128             :     }
     129             : 
     130       45704 :   if (_augment_sparsity_pattern)
     131           0 :     sp->apply_extra_sparsity_object(*_augment_sparsity_pattern);
     132             : 
     133       47114 :   return sp;
     134           0 : }
     135             : 
     136             : 
     137             : 
     138      373870 : DofMap::DofMap(const unsigned int number,
     139      373870 :                MeshBase & mesh) :
     140             :   DofMapBase (mesh.comm()),
     141      352698 :   _dof_coupling(nullptr),
     142      352698 :   _error_on_constraint_loop(false),
     143      352698 :   _constrained_sparsity_construction(false),
     144      352698 :   _variables(),
     145      352698 :   _variable_groups(),
     146             :   _variable_group_numbers(),
     147      352698 :   _sys_number(number),
     148      352698 :   _mesh(mesh),
     149             :   _matrices(),
     150             :   _first_scalar_df(),
     151             :   _send_list(),
     152      352698 :   _augment_sparsity_pattern(nullptr),
     153      352698 :   _extra_sparsity_function(nullptr),
     154      352698 :   _extra_sparsity_context(nullptr),
     155      352698 :   _augment_send_list(nullptr),
     156      352698 :   _extra_send_list_function(nullptr),
     157      352698 :   _extra_send_list_context(nullptr),
     158      352698 :   _default_coupling(std::make_unique<DefaultCoupling>()),
     159      352698 :   _default_evaluating(std::make_unique<DefaultCoupling>()),
     160      352698 :   _need_full_sparsity_pattern(false),
     161      352698 :   _need_ghost_constraints(false),
     162      352698 :   _n_SCALAR_dofs(0)
     163             : #ifdef LIBMESH_ENABLE_AMR
     164             :   , _first_old_scalar_df()
     165             : #endif
     166             : #ifdef LIBMESH_ENABLE_CONSTRAINTS
     167             :   , _dof_constraints()
     168             :   , _stashed_dof_constraints()
     169             :   , _primal_constraint_values()
     170             :   , _adjoint_constraint_values()
     171             : #endif
     172             : #ifdef LIBMESH_ENABLE_NODE_CONSTRAINTS
     173             :   , _node_constraints()
     174             : #endif
     175             : #ifdef LIBMESH_ENABLE_PERIODIC
     176      352698 :   , _periodic_boundaries(std::make_unique<PeriodicBoundaries>())
     177             : #endif
     178             : #ifdef LIBMESH_ENABLE_DIRICHLET
     179      352698 :   , _dirichlet_boundaries(std::make_unique<DirichletBoundaries>())
     180      352698 :   , _adjoint_dirichlet_boundaries()
     181             : #endif
     182      352698 :   , _implicit_neighbor_dofs_initialized(false),
     183      352698 :   _implicit_neighbor_dofs(false),
     184      352698 :   _verify_dirichlet_bc_consistency(true),
     185     1495480 :   _sc(nullptr)
     186             : {
     187       10586 :   _matrices.clear();
     188             : 
     189      373870 :   _default_coupling->set_mesh(&_mesh);
     190      373870 :   _default_evaluating->set_mesh(&_mesh);
     191       10586 :   _default_evaluating->set_n_levels(1);
     192             : 
     193             : #ifdef LIBMESH_ENABLE_PERIODIC
     194      384456 :   _default_coupling->set_periodic_boundaries(_periodic_boundaries.get());
     195      384456 :   _default_evaluating->set_periodic_boundaries(_periodic_boundaries.get());
     196             : 
     197      374912 :   for (auto & gf : as_range(_mesh.ghosting_functors_begin(),
     198      802563 :                             _mesh.ghosting_functors_end()))
     199             :   {
     200             :     // FIXME - we need to handle multiple systems properly here, not
     201             :     // just "assume the first system has every PBC".
     202      406479 :     if (!gf->get_periodic_boundaries())
     203      382898 :       gf->set_periodic_boundaries(_periodic_boundaries.get());
     204             :   }
     205             : #endif
     206             : 
     207      373870 :   this->add_coupling_functor(*_default_coupling);
     208      373870 :   this->add_algebraic_ghosting_functor(*_default_evaluating);
     209      373870 : }
     210             : 
     211             : 
     212             : 
     213             : // Destructor
     214      779498 : DofMap::~DofMap()
     215             : {
     216      373870 :   this->clear();
     217             : 
     218             :   // clear() resets all but the default DofMap-based functors.  We
     219             :   // need to remove those from the mesh too before we die.
     220      384456 :   _mesh.remove_ghosting_functor(*_default_coupling);
     221      384456 :   _mesh.remove_ghosting_functor(*_default_evaluating);
     222     2158532 : }
     223             : 
     224             : 
     225             : #ifdef LIBMESH_ENABLE_PERIODIC
     226             : 
     227           0 : bool DofMap::is_periodic_boundary (const boundary_id_type boundaryid) const
     228             : {
     229           0 :   if (_periodic_boundaries->count(boundaryid) != 0)
     230           0 :     return true;
     231             : 
     232           0 :   return false;
     233             : }
     234             : 
     235             : #endif
     236             : 
     237           0 : void DofMap::set_error_on_cyclic_constraint(bool error_on_cyclic_constraint)
     238             : {
     239             :   // This function will eventually be officially libmesh_deprecated();
     240             :   // Call DofMap::set_error_on_constraint_loop() instead.
     241           0 :   set_error_on_constraint_loop(error_on_cyclic_constraint);
     242           0 : }
     243             : 
     244          71 : void DofMap::set_error_on_constraint_loop(bool error_on_constraint_loop)
     245             : {
     246          71 :   _error_on_constraint_loop = error_on_constraint_loop;
     247          71 : }
     248             : 
     249             : 
     250       23625 : void DofMap::attach_matrix (SparseMatrix<Number> & matrix)
     251             : {
     252         658 :   parallel_object_only();
     253             : 
     254             :   // We shouldn't be trying to re-attach the same matrices repeatedly
     255         658 :   libmesh_assert (std::find(_matrices.begin(), _matrices.end(),
     256             :                             &matrix) == _matrices.end());
     257             : 
     258       23625 :   _matrices.push_back(&matrix);
     259             : 
     260       23625 :   this->update_sparsity_pattern(matrix);
     261             : 
     262       23625 :   if (matrix.need_full_sparsity_pattern())
     263           0 :     _need_full_sparsity_pattern = true;
     264       23625 : }
     265             : 
     266             : 
     267             : 
     268       23925 : bool DofMap::computed_sparsity_already() const
     269             : {
     270       23977 :   bool computed_sparsity_already = _sp &&
     271          56 :     (!_sp->get_n_nz().empty() ||
     272       23925 :      !_sp->get_n_oz().empty());
     273       23925 :   this->comm().max(computed_sparsity_already);
     274       23925 :   return computed_sparsity_already;
     275             : }
     276             : 
     277             : 
     278             : 
     279       23625 : void DofMap::update_sparsity_pattern(SparseMatrix<Number> & matrix) const
     280             : {
     281       23625 :   matrix.attach_dof_map (*this);
     282             : 
     283             :   // If we've already computed sparsity, then it's too late
     284             :   // to wait for "compute_sparsity" to help with sparse matrix
     285             :   // initialization, and we need to handle this matrix individually
     286       23625 :   if (this->computed_sparsity_already())
     287             :     {
     288          52 :       libmesh_assert(_sp.get());
     289             : 
     290        1844 :       if (matrix.need_full_sparsity_pattern())
     291             :         {
     292             :           // We'd better have already computed the full sparsity
     293             :           // pattern if we need it here
     294           0 :           libmesh_assert(_need_full_sparsity_pattern);
     295             : 
     296           0 :           matrix.update_sparsity_pattern (_sp->get_sparsity_pattern());
     297             :         }
     298             : 
     299        1844 :       matrix.attach_sparsity_pattern(*_sp);
     300             :     }
     301       23625 : }
     302             : 
     303             : 
     304             : 
     305       21787 : bool DofMap::is_attached (SparseMatrix<Number> & matrix)
     306             : {
     307       21787 :   return (std::find(_matrices.begin(), _matrices.end(),
     308       22393 :                     &matrix) != _matrices.end());
     309             : }
     310             : 
     311             : 
     312             : 
     313    68314778 : DofObject * DofMap::node_ptr(MeshBase & mesh, dof_id_type i) const
     314             : {
     315    68314778 :   return mesh.node_ptr(i);
     316             : }
     317             : 
     318             : 
     319             : 
     320    38553560 : DofObject * DofMap::elem_ptr(MeshBase & mesh, dof_id_type i) const
     321             : {
     322    38553560 :   return mesh.elem_ptr(i);
     323             : }
     324             : 
     325             : 
     326             : 
     327             : template <typename iterator_type>
     328      790306 : void DofMap::set_nonlocal_dof_objects(iterator_type objects_begin,
     329             :                                       iterator_type objects_end,
     330             :                                       MeshBase & mesh,
     331             :                                       dofobject_accessor objects)
     332             : {
     333             :   // This function must be run on all processors at once
     334       23228 :   parallel_object_only();
     335             : 
     336             :   // First, iterate over local objects to find out how many
     337             :   // are on each processor
     338       46456 :   std::unordered_map<processor_id_type, dof_id_type> ghost_objects_from_proc;
     339             : 
     340       46456 :   iterator_type it  = objects_begin;
     341             : 
     342   101673034 :   for (; it != objects_end; ++it)
     343             :     {
     344    53434169 :       DofObject * obj = *it;
     345             : 
     346     2992805 :       if (obj)
     347             :         {
     348    53434169 :           processor_id_type obj_procid = obj->processor_id();
     349             :           // We'd better be completely partitioned by now
     350     2992805 :           libmesh_assert_not_equal_to (obj_procid, DofObject::invalid_processor_id);
     351    53434169 :           ghost_objects_from_proc[obj_procid]++;
     352             :         }
     353             :     }
     354             : 
     355             :   // Request sets to send to each processor
     356             :   std::map<processor_id_type, std::vector<dof_id_type>>
     357       46456 :     requested_ids;
     358             : 
     359             :   // We know how many of our objects live on each processor, so
     360             :   // reserve() space for requests from each.
     361     2476463 :   for (auto [p, size] : ghost_objects_from_proc)
     362             :     {
     363     1722879 :       if (p != this->processor_id())
     364     1369553 :         requested_ids[p].reserve(size);
     365             :     }
     366             : 
     367   101673034 :   for (it = objects_begin; it != objects_end; ++it)
     368             :     {
     369    53434169 :       DofObject * obj = *it;
     370    53434169 :       if (obj->processor_id() != DofObject::invalid_processor_id)
     371    53434169 :         requested_ids[obj->processor_id()].push_back(obj->id());
     372             :     }
     373             : #ifdef DEBUG
     374       69684 :   for (auto p : make_range(this->n_processors()))
     375             :     {
     376       46456 :       if (ghost_objects_from_proc.count(p))
     377       36722 :         libmesh_assert_equal_to (requested_ids[p].size(), ghost_objects_from_proc[p]);
     378             :       else
     379        9734 :         libmesh_assert(!requested_ids.count(p));
     380             :     }
     381             : #endif
     382             : 
     383             :   typedef std::vector<dof_id_type> datum;
     384             : 
     385     9878887 :   auto gather_functor =
     386     1612713 :     [this, &mesh, &objects]
     387             :     (processor_id_type,
     388             :      const std::vector<dof_id_type> & ids,
     389       73444 :      std::vector<datum> & data)
     390             :     {
     391             :       // Fill those requests
     392             :       const unsigned int
     393       73444 :         sys_num      = this->sys_number(),
     394       36722 :         n_var_groups = this->n_variable_groups();
     395             : 
     396       73444 :       const std::size_t query_size = ids.size();
     397             : 
     398     1686157 :       data.resize(query_size);
     399    55120326 :       for (auto & d : data)
     400    53434169 :         d.resize(2 * n_var_groups);
     401             : 
     402    55120326 :       for (std::size_t i=0; i != query_size; ++i)
     403             :         {
     404    53434169 :           DofObject * requested = (this->*objects)(mesh, ids[i]);
     405     2992805 :           libmesh_assert(requested);
     406     2992805 :           libmesh_assert_equal_to (requested->processor_id(), this->processor_id());
     407     2992805 :           libmesh_assert_equal_to (requested->n_var_groups(sys_num), n_var_groups);
     408   109758817 :           for (unsigned int vg=0; vg != n_var_groups; ++vg)
     409             :             {
     410             :               unsigned int n_comp_g =
     411     3529701 :                 requested->n_comp_group(sys_num, vg);
     412    59854349 :               data[i][vg] = n_comp_g;
     413    56324648 :               dof_id_type my_first_dof = n_comp_g ?
     414     1409551 :                 requested->vg_dof_base(sys_num, vg) : 0;
     415     3529701 :               libmesh_assert_not_equal_to (my_first_dof, DofObject::invalid_id);
     416    59854349 :               data[i][n_var_groups+vg] = my_first_dof;
     417             :             }
     418             :         }
     419             :     };
     420             : 
     421     9878887 :   auto action_functor =
     422     1612713 :     [this, &mesh, &objects]
     423             :     (processor_id_type libmesh_dbg_var(pid),
     424             :      const std::vector<dof_id_type> & ids,
     425       73444 :      const std::vector<datum> & data)
     426             :     {
     427             :       const unsigned int
     428       73444 :         sys_num      = this->sys_number(),
     429       36722 :         n_var_groups = this->n_variable_groups();
     430             : 
     431             :       // Copy the id changes we've now been informed of
     432    55120326 :       for (auto i : index_range(ids))
     433             :         {
     434    53434169 :           DofObject * requested = (this->*objects)(mesh, ids[i]);
     435     2992805 :           libmesh_assert(requested);
     436     2992805 :           libmesh_assert_equal_to (requested->processor_id(), pid);
     437   109758817 :           for (unsigned int vg=0; vg != n_var_groups; ++vg)
     438             :             {
     439             :               unsigned int n_comp_g =
     440    59854349 :                 cast_int<unsigned int>(data[i][vg]);
     441    56324648 :               requested->set_n_comp_group(sys_num, vg, n_comp_g);
     442    56324648 :               if (n_comp_g)
     443             :                 {
     444    25768899 :                   dof_id_type my_first_dof = data[i][n_var_groups+vg];
     445     1409551 :                   libmesh_assert_not_equal_to (my_first_dof, DofObject::invalid_id);
     446             :                   requested->set_vg_dof_base
     447     1409551 :                     (sys_num, vg, my_first_dof);
     448             :                 }
     449             :             }
     450             :         }
     451             :     };
     452             : 
     453       23228 :   datum * ex = nullptr;
     454             :   Parallel::pull_parallel_vector_data
     455      790306 :     (this->comm(), requested_ids, gather_functor, action_functor, ex);
     456             : 
     457             : #ifdef DEBUG
     458             :   // Double check for invalid dofs
     459     3016033 :   for (it = objects_begin; it != objects_end; ++it)
     460             :     {
     461     2992805 :       DofObject * obj = *it;
     462     2992805 :       libmesh_assert (obj);
     463     2992805 :       unsigned int num_variables = obj->n_vars(this->sys_number());
     464     9945148 :       for (unsigned int v=0; v != num_variables; ++v)
     465             :         {
     466             :           unsigned int n_comp =
     467     6952343 :             obj->n_comp(this->sys_number(), v);
     468     6952343 :           dof_id_type my_first_dof = n_comp ?
     469     2753517 :             obj->dof_number(this->sys_number(), v, 0) : 0;
     470     6952343 :           libmesh_assert_not_equal_to (my_first_dof, DofObject::invalid_id);
     471             :         }
     472             :     }
     473             : #endif
     474      790306 : }
     475             : 
     476             : 
     477             : 
     478      406413 : void DofMap::reinit
     479             :   (MeshBase & mesh,
     480             :    const std::map<const Node *, std::set<subdomain_id_type>> &
     481             :      constraining_subdomains)
     482             : {
     483       11616 :   libmesh_assert (mesh.is_prepared());
     484             : 
     485       34848 :   LOG_SCOPE("reinit()", "DofMap");
     486             : 
     487             :   // This is the common case and we want to optimize for it
     488             :   const bool constraining_subdomains_empty =
     489       11616 :     constraining_subdomains.empty();
     490             : 
     491             :   // We ought to reconfigure our default coupling functor.
     492             :   //
     493             :   // The user might have removed it from our coupling functors set,
     494             :   // but if so, who cares, this reconfiguration is cheap.
     495             : 
     496             :   // Avoid calling set_dof_coupling() with an empty/non-nullptr
     497             :   // _dof_coupling matrix which may happen when there are actually no
     498             :   // variables on the system.
     499      406413 :   if (this->_dof_coupling && this->_dof_coupling->empty() && !this->n_variables())
     500           0 :     this->_dof_coupling = nullptr;
     501      406413 :   _default_coupling->set_dof_coupling(this->_dof_coupling);
     502             : 
     503             :   // By default we may want 0 or 1 levels of coupling
     504             :   unsigned int standard_n_levels =
     505      406413 :     this->use_coupled_neighbor_dofs(mesh);
     506             :   _default_coupling->set_n_levels
     507      600382 :     (std::max(_default_coupling->n_levels(), standard_n_levels));
     508             : 
     509             :   // But we *don't* want to restrict to a CouplingMatrix unless the
     510             :   // user does so manually; the original libMesh behavior was to put
     511             :   // ghost indices on the send_list regardless of variable.
     512             :   //_default_evaluating->set_dof_coupling(this->_dof_coupling);
     513             : 
     514             :   const unsigned int
     515       23232 :     sys_num      = this->sys_number(),
     516       11616 :     n_var_groups = this->n_variable_groups();
     517             : 
     518             :   // The DofObjects need to know how many variable groups we have, and
     519             :   // how many variables there are in each group.
     520      418029 :   std::vector<unsigned int> n_vars_per_group; /**/ n_vars_per_group.reserve (n_var_groups);
     521             : 
     522      824112 :   for (unsigned int vg=0; vg<n_var_groups; vg++)
     523      417699 :     n_vars_per_group.push_back (this->variable_group(vg).n_variables());
     524             : 
     525             : #ifdef LIBMESH_ENABLE_AMR
     526             : 
     527             :   //------------------------------------------------------------
     528             :   // Clear the old_dof_objects for all the nodes
     529             :   // and elements so that we can overwrite them
     530             :   Threads::parallel_for_each
     531      406413 :     (mesh.node_stored_range(),
     532     1945169 :      [](Node * node)
     533             :      {
     534    35277333 :        node->clear_old_dof_object();
     535     1945169 :        libmesh_assert (!node->get_old_dof_object());
     536     1945169 :      });
     537             : 
     538             :   Threads::parallel_for_each
     539      406413 :     (mesh.element_stored_range(),
     540     1047718 :      [](Elem * elem)
     541             :      {
     542    19691785 :        elem->clear_old_dof_object();
     543     1047718 :        libmesh_assert (!elem->get_old_dof_object());
     544     1047718 :      });
     545             : 
     546             :   //------------------------------------------------------------
     547             :   // Set the old_dof_objects for the elements that
     548             :   // weren't just created, if these old dof objects
     549             :   // had variables
     550    39024760 :   for (auto & elem : mesh.element_ptr_range())
     551             :     {
     552             :       // Skip the elements that were just refined
     553    20159493 :       if (elem->refinement_flag() == Elem::JUST_REFINED)
     554     1637350 :         continue;
     555             : 
     556   132092082 :       for (Node & node : elem->node_ref_range())
     557   113715051 :         if (node.get_old_dof_object() == nullptr)
     558    81623978 :           if (node.has_dofs(sys_num))
     559    11092612 :             node.set_old_dof_object();
     560             : 
     561      902606 :       libmesh_assert (!elem->get_old_dof_object());
     562             : 
     563    18377031 :       if (elem->has_dofs(sys_num))
     564     9063041 :         elem->set_old_dof_object();
     565      383181 :     }
     566             : 
     567             : #endif // #ifdef LIBMESH_ENABLE_AMR
     568             : 
     569             : 
     570             :   //------------------------------------------------------------
     571             :   // Then set the number of variables for each \p DofObject
     572             :   // equal to n_variables() for this system.  This will
     573             :   // handle new \p DofObjects that may have just been created
     574             : 
     575             :   // All the nodes
     576             :   Threads::parallel_for_each
     577      418029 :     (mesh.node_stored_range(),
     578     4668316 :      [sys_num, n_vars_per_group](Node * node)
     579             :      {
     580    35935039 :        node->set_n_vars_per_group(sys_num, n_vars_per_group);
     581     1945169 :      });
     582             : 
     583             :   // All the elements
     584             :   Threads::parallel_for_each
     585      406415 :     (mesh.element_stored_range(),
     586     2490252 :      [sys_num, n_vars_per_group](Elem * elem)
     587             :      {
     588    20159493 :        elem->set_n_vars_per_group(sys_num, n_vars_per_group);
     589     1047718 :      });
     590             : 
     591             :   // Zero _n_SCALAR_dofs, it will be updated below.
     592      406413 :   this->_n_SCALAR_dofs = 0;
     593             : 
     594             :   //------------------------------------------------------------
     595             :   // Next allocate space for the DOF indices
     596      824089 :   for (unsigned int vg=0; vg<n_var_groups; vg++)
     597             :     {
     598       11934 :       const VariableGroup & vg_description = this->variable_group(vg);
     599             : 
     600       11934 :       const unsigned int n_var_in_group = vg_description.n_variables();
     601       11934 :       const FEType & base_fe_type        = vg_description.type();
     602             : 
     603      417699 :       const bool add_p_level = base_fe_type.p_refinement;
     604             : 
     605             :       // Don't need to loop over elements for a SCALAR variable
     606             :       // Just increment _n_SCALAR_dofs
     607      417699 :       if (base_fe_type.family == SCALAR)
     608             :         {
     609        1485 :           this->_n_SCALAR_dofs += base_fe_type.order.get_order()*n_var_in_group;
     610        1485 :           continue;
     611             :         }
     612             : 
     613             :       // This should be constant even on p-refined elements
     614             :       const bool extra_hanging_dofs =
     615      416214 :         FEInterface::extra_hanging_dofs(base_fe_type);
     616             : 
     617             :       // For all the active elements, count vertex degrees of freedom.
     618    29888704 :       for (auto & elem : mesh.active_element_ptr_range())
     619             :         {
     620      881720 :           libmesh_assert(elem);
     621             : 
     622             :           // Only number dofs connected to active elements on this
     623             :           // processor and only for variables which are active on on
     624             :           // this element's subdomain or which are active on the
     625             :           // subdomain of a node constrained by this node.
     626             :           const bool active_on_elem =
     627    15415822 :             vg_description.active_on_subdomain(elem->subdomain_id());
     628             : 
     629             :           // If there's no way we're active on this element then we're
     630             :           // done
     631    15415822 :           if (!active_on_elem && constraining_subdomains_empty)
     632       36504 :             continue;
     633             : 
     634    15379318 :           FEType fe_type = base_fe_type;
     635             : 
     636    15379318 :           const ElemType type = elem->type();
     637             : 
     638    15379442 :           libmesh_error_msg_if(base_fe_type.order.get_order() >
     639             :                                int(FEInterface::max_order(base_fe_type,type)),
     640             :                                "ERROR: Finite element "
     641             :                                << Utility::enum_to_string(base_fe_type.family)
     642             :                                << " on geometric element "
     643             :                                << Utility::enum_to_string(type)
     644             :                                << "\nonly supports FEInterface::max_order = "
     645             :                                << FEInterface::max_order(base_fe_type,type)
     646             :                                << ", not fe_type.order = "
     647             :                                << base_fe_type.order);
     648             : 
     649             : #ifdef LIBMESH_ENABLE_AMR
     650             :           // Make sure we haven't done more p refinement than we can
     651             :           // handle
     652    15379295 :           if (base_fe_type.order + add_p_level*elem->p_level() >
     653             :               FEInterface::max_order(base_fe_type, type))
     654             :             {
     655             : #  ifdef DEBUG
     656           0 :               libMesh::err << "WARNING: Finite element "
     657           0 :                            << Utility::enum_to_string(base_fe_type.family)
     658           0 :                            << " on geometric element "
     659           0 :                            << Utility::enum_to_string(type) << std::endl
     660           0 :                            << "could not be p refined past FEInterface::max_order = "
     661           0 :                            << FEInterface::max_order(base_fe_type,type)
     662           0 :                            << std::endl;
     663             : #  endif
     664           0 :               elem->set_p_level(int(FEInterface::max_order(base_fe_type,type))
     665           0 :                                 - int(base_fe_type.order));
     666             :             }
     667             : #endif
     668             : 
     669             :           // Allocate the vertex DOFs
     670   123926616 :           for (auto n : elem->node_index_range())
     671             :             {
     672   107667643 :               Node & node = elem->node_ref(n);
     673             : 
     674             :               // If we're active on the element then we're active on
     675             :               // its nodes.  If we're not then we might *still* be
     676             :               // active on particular constraining nodes.
     677     6608082 :               bool active_on_node = active_on_elem;
     678   107667643 :               if (!active_on_node)
     679           0 :                 if (auto it = constraining_subdomains.find(&node);
     680           0 :                     it != constraining_subdomains.end())
     681           0 :                   for (auto s : it->second)
     682           0 :                     if (vg_description.active_on_subdomain(s))
     683             :                       {
     684           0 :                         active_on_node = true;
     685           0 :                         break;
     686             :                       }
     687             : 
     688    13216164 :               if (!active_on_node)
     689           0 :                 continue;
     690             : 
     691   107667643 :               if (elem->is_vertex(n))
     692             :                 {
     693             :                   const unsigned int old_node_dofs =
     694    62423507 :                     node.n_comp_group(sys_num, vg);
     695             : 
     696             :                   const unsigned int vertex_dofs =
     697    62423526 :                     std::max(FEInterface::n_dofs_at_node(fe_type, elem, n, add_p_level),
     698    62423507 :                              old_node_dofs);
     699             : 
     700             :                   // Some discontinuous FEs have no vertex dofs
     701    62423507 :                   if (vertex_dofs > old_node_dofs)
     702             :                     {
     703     9918923 :                       node.set_n_comp_group(sys_num, vg,
     704             :                                             vertex_dofs);
     705             : 
     706             :                       // Abusing dof_number to set a "this is a
     707             :                       // vertex" flag
     708     9111375 :                       node.set_vg_dof_base(sys_num, vg,
     709             :                                            vertex_dofs);
     710             : 
     711             :                       // libMesh::out << "sys_num,vg,old_node_dofs,vertex_dofs="
     712             :                       //       << sys_num << ","
     713             :                       //       << vg << ","
     714             :                       //       << old_node_dofs << ","
     715             :                       //       << vertex_dofs << '\n',
     716             :                       // node.debug_buffer();
     717             : 
     718             :                       // libmesh_assert_equal_to (vertex_dofs, node.n_comp(sys_num, vg));
     719             :                       // libmesh_assert_equal_to (vertex_dofs, node.vg_dof_base(sys_num, vg));
     720             :                     }
     721             :                 }
     722             :             }
     723      392430 :         } // done counting vertex dofs
     724             : 
     725             :       // count edge & face dofs next
     726    29888654 :       for (auto & elem : mesh.active_element_ptr_range())
     727             :         {
     728      881718 :           libmesh_assert(elem);
     729             : 
     730             :           // Only number dofs connected to active elements on this
     731             :           // processor and only for variables which are active on on
     732             :           // this element's subdomain or which are active on the
     733             :           // subdomain of a node constrained by this node.
     734             :           const bool active_on_elem =
     735    15415799 :             vg_description.active_on_subdomain(elem->subdomain_id());
     736             : 
     737             :           // If there's no way we're active on this element then we're
     738             :           // done
     739    15415799 :           if (!active_on_elem && constraining_subdomains_empty)
     740       34464 :             continue;
     741             : 
     742             :           // Allocate the edge and face DOFs
     743   123046938 :           for (auto n : elem->node_index_range())
     744             :             {
     745   107667643 :               Node & node = elem->node_ref(n);
     746             : 
     747             :               // If we're active on the element then we're active on
     748             :               // its nodes.  If we're not then we might *still* be
     749             :               // active on particular constraining nodes.
     750     6608082 :               bool active_on_node = active_on_elem;
     751   107667643 :               if (!active_on_node)
     752           0 :                 if (auto it = constraining_subdomains.find(&node);
     753           0 :                     it != constraining_subdomains.end())
     754           0 :                   for (auto s : it->second)
     755           0 :                     if (vg_description.active_on_subdomain(s))
     756             :                       {
     757           0 :                         active_on_node = true;
     758           0 :                         break;
     759             :                       }
     760             : 
     761    13216164 :               if (!active_on_node)
     762           0 :                 continue;
     763             : 
     764             :               const unsigned int old_node_dofs =
     765     6608082 :                 node.n_comp_group(sys_num, vg);
     766             : 
     767   104661319 :               const unsigned int vertex_dofs = old_node_dofs?
     768     6608082 :                 cast_int<unsigned int>(node.vg_dof_base (sys_num,vg)):0;
     769             : 
     770             :               const unsigned int new_node_dofs =
     771   107667643 :                 FEInterface::n_dofs_at_node(base_fe_type, elem, n, add_p_level);
     772             : 
     773             :               // We've already allocated vertex DOFs
     774   107667643 :               if (elem->is_vertex(n))
     775             :                 {
     776     3767100 :                   libmesh_assert_greater_equal (old_node_dofs, vertex_dofs);
     777             :                   // //if (vertex_dofs < new_node_dofs)
     778             :                   //   libMesh::out << "sys_num,vg,old_node_dofs,vertex_dofs,new_node_dofs="
     779             :                   //                << sys_num << ","
     780             :                   //                << vg << ","
     781             :                   //                << old_node_dofs << ","
     782             :                   //                << vertex_dofs << ","
     783             :                   //                << new_node_dofs << '\n',
     784             :                   //     node.debug_buffer();
     785             : 
     786     3767100 :                   libmesh_assert_greater_equal (vertex_dofs,   new_node_dofs);
     787             :                 }
     788             :               // We need to allocate the rest
     789             :               else
     790             :                 {
     791             :                   // If this has no dofs yet, it needs no vertex
     792             :                   // dofs, so we just give it edge or face dofs
     793    45244136 :                   if (!old_node_dofs)
     794             :                     {
     795    37194740 :                       node.set_n_comp_group(sys_num, vg,
     796             :                                             new_node_dofs);
     797             :                       // Abusing dof_number to set a "this has no
     798             :                       // vertex dofs" flag
     799    37194740 :                       if (new_node_dofs)
     800      588258 :                         node.set_vg_dof_base(sys_num, vg, 0);
     801             :                     }
     802             : 
     803             :                   // If this has dofs, but has no vertex dofs,
     804             :                   // it may still need more edge or face dofs if
     805             :                   // we're p-refined.
     806     8049396 :                   else if (vertex_dofs == 0)
     807             :                     {
     808     7684401 :                       if (new_node_dofs > old_node_dofs)
     809             :                         {
     810         224 :                           node.set_n_comp_group(sys_num, vg,
     811             :                                                 new_node_dofs);
     812             : 
     813          18 :                           node.set_vg_dof_base(sys_num, vg,
     814             :                                                vertex_dofs);
     815             :                         }
     816             :                     }
     817             :                   // If this is another element's vertex,
     818             :                   // add more (non-overlapping) edge/face dofs if
     819             :                   // necessary
     820      364995 :                   else if (extra_hanging_dofs)
     821             :                     {
     822      318894 :                       if (new_node_dofs > old_node_dofs - vertex_dofs)
     823             :                         {
     824      307509 :                           node.set_n_comp_group(sys_num, vg,
     825             :                                                 vertex_dofs + new_node_dofs);
     826             : 
     827      297124 :                           node.set_vg_dof_base(sys_num, vg,
     828             :                                                vertex_dofs);
     829             :                         }
     830             :                     }
     831             :                   // If this is another element's vertex, add any
     832             :                   // (overlapping) edge/face dofs if necessary
     833             :                   else
     834             :                     {
     835        5064 :                       libmesh_assert_greater_equal (old_node_dofs, vertex_dofs);
     836       46101 :                       if (new_node_dofs > old_node_dofs)
     837             :                         {
     838           0 :                           node.set_n_comp_group(sys_num, vg,
     839             :                                                 new_node_dofs);
     840             : 
     841           0 :                           node.set_vg_dof_base (sys_num, vg,
     842             :                                                 vertex_dofs);
     843             :                         }
     844             :                     }
     845             :                 }
     846             :             }
     847             :           // Allocate the element DOFs
     848             :           const unsigned int dofs_per_elem =
     849    15379295 :             FEInterface::n_dofs_per_elem(base_fe_type, elem, add_p_level);
     850             : 
     851    15379295 :           elem->set_n_comp_group(sys_num, vg, dofs_per_elem);
     852             : 
     853      392411 :         }
     854             :     } // end loop over variable groups
     855             : 
     856             :   // Calling DofMap::reinit() by itself makes little sense,
     857             :   // so we won't bother with nonlocal DofObjects.
     858             :   // Those will be fixed by distribute_dofs
     859             : 
     860             :   //------------------------------------------------------------
     861             :   // Finally, clear all the current DOF indices
     862             :   // (distribute_dofs expects them cleared!)
     863      406390 :   this->invalidate_dofs(mesh);
     864     1172752 : }
     865             : 
     866             : 
     867             : 
     868      812780 : void DofMap::invalidate_dofs(MeshBase & mesh) const
     869             : {
     870       46456 :   const unsigned int sys_num = this->sys_number();
     871             : 
     872             :   // All the nodes
     873             :   Threads::parallel_for_each
     874      812780 :     (mesh.node_stored_range(),
     875    71869186 :      [sys_num](Node * node)
     876             :      {
     877    71869186 :        node->invalidate_dofs(sys_num);
     878     3890238 :      });
     879             : 
     880             :   // All the active elements.
     881             :   Threads::parallel_for_each
     882      812780 :     (mesh.element_stored_range(),
     883    39847862 :      [sys_num](Elem * elem)
     884             :      {
     885     2095372 :        if (elem->active())
     886    30466968 :          elem->invalidate_dofs(sys_num);
     887    38223082 :      });
     888      812780 : }
     889             : 
     890             : 
     891             : 
     892      374789 : void DofMap::clear()
     893             : {
     894      374789 :   DofMapBase::clear();
     895             : 
     896             :   // we don't want to clear
     897             :   // the coupling matrix!
     898             :   // It should not change...
     899             :   //_dof_coupling->clear();
     900             :   //
     901             :   // But it would be inconsistent to leave our coupling settings
     902             :   // through a clear()...
     903      374789 :   _dof_coupling = nullptr;
     904             : 
     905             :   // Reset ghosting functor statuses
     906             :   {
     907      749717 :     for (const auto & gf : _coupling_functors)
     908             :       {
     909       10618 :         libmesh_assert(gf);
     910      374928 :         _mesh.remove_ghosting_functor(*gf);
     911             :       }
     912       10614 :     this->_coupling_functors.clear();
     913             : 
     914             :     // Go back to default coupling
     915             : 
     916      374789 :     _default_coupling->set_dof_coupling(this->_dof_coupling);
     917      374789 :     _default_coupling->set_n_levels(this->use_coupled_neighbor_dofs(this->_mesh));
     918             : 
     919      374789 :     this->add_coupling_functor(*_default_coupling);
     920             :   }
     921             : 
     922             : 
     923             :   {
     924      750001 :     for (const auto & gf : _algebraic_ghosting_functors)
     925             :       {
     926       10626 :         libmesh_assert(gf);
     927      375212 :         _mesh.remove_ghosting_functor(*gf);
     928             :       }
     929       10614 :     this->_algebraic_ghosting_functors.clear();
     930             : 
     931             :     // Go back to default send_list generation
     932             : 
     933             :     // _default_evaluating->set_dof_coupling(this->_dof_coupling);
     934       10614 :     _default_evaluating->set_n_levels(1);
     935      374789 :     this->add_algebraic_ghosting_functor(*_default_evaluating);
     936             :   }
     937             : 
     938       10614 :   this->_shared_functors.clear();
     939             : 
     940      364175 :   _variables.clear();
     941      364175 :   _variable_groups.clear();
     942       10614 :   _var_to_vg.clear();
     943       10614 :   _variable_group_numbers.clear();
     944       10614 :   _array_variables.clear();
     945       10614 :   _first_scalar_df.clear();
     946       10614 :   this->clear_send_list();
     947      374789 :   this->clear_sparsity();
     948      374789 :   _need_full_sparsity_pattern = false;
     949      374789 :   _need_ghost_constraints = false;
     950             : 
     951             : #ifdef LIBMESH_ENABLE_AMR
     952             : 
     953       10614 :   _dof_constraints.clear();
     954       10614 :   _stashed_dof_constraints.clear();
     955       10614 :   _primal_constraint_values.clear();
     956       10614 :   _adjoint_constraint_values.clear();
     957             : #ifdef LIBMESH_ENABLE_DIRICHLET
     958      385403 :   _dirichlet_cache = DirichletConstraintCache();
     959             : #endif
     960      374789 :   _n_old_dfs = 0;
     961       10614 :   _first_old_df.clear();
     962       10614 :   _end_old_df.clear();
     963       10614 :   _first_old_scalar_df.clear();
     964             : 
     965             : #endif
     966             : 
     967       10614 :   _matrices.clear();
     968      374789 :   if (_sc)
     969         560 :     _sc->clear();
     970      374789 : }
     971             : 
     972             : 
     973             : 
     974      406413 : std::size_t DofMap::distribute_dofs (MeshBase & mesh)
     975             : {
     976             :   // This function must be run on all processors at once
     977       11616 :   parallel_object_only();
     978             : 
     979             :   // Log how long it takes to distribute the degrees of freedom
     980       34848 :   LOG_SCOPE("distribute_dofs()", "DofMap");
     981             : 
     982       11616 :   libmesh_assert (mesh.is_prepared());
     983             : 
     984       23232 :   const processor_id_type proc_id = this->processor_id();
     985             : #ifndef NDEBUG
     986       11616 :   const processor_id_type n_proc  = this->n_processors();
     987             : #endif
     988             : 
     989             :   //  libmesh_assert_greater (this->n_variables(), 0);
     990       11616 :   libmesh_assert_less (proc_id, n_proc);
     991             : 
     992             :   // Data structure to ensure we can correctly combine
     993             :   // subdomain-restricted variables with constraining nodes from
     994             :   // different subdomains
     995             :   const std::map<const Node *, std::set<subdomain_id_type>>
     996             :     constraining_subdomains =
     997      406415 :     this->calculate_constraining_subdomains();
     998             : 
     999             : #ifdef LIBMESH_ENABLE_DIRICHLET
    1000             :   // New degree of freedom indices invalidate every cached constraint
    1001      418029 :   _dirichlet_cache = DirichletConstraintCache();
    1002             : #endif
    1003             : 
    1004             :   // re-init in case the mesh has changed
    1005      406413 :   this->reinit(mesh,
    1006             :                constraining_subdomains);
    1007             : 
    1008             :   // By default distribute variables in a
    1009             :   // var-major fashion, but allow run-time
    1010             :   // specification
    1011      406390 :   bool node_major_dofs = libMesh::on_command_line ("--node-major-dofs");
    1012             : 
    1013             :   // The DOF counter, will be incremented as we encounter
    1014             :   // new degrees of freedom
    1015      406390 :   dof_id_type next_free_dof = 0;
    1016             : 
    1017             :   // Clear the send list before we rebuild it
    1018       11614 :   this->clear_send_list();
    1019             : 
    1020             :   // Set temporary DOF indices on this processor
    1021      406390 :   if (node_major_dofs)
    1022             :     this->distribute_local_dofs_node_major
    1023         840 :       (next_free_dof, mesh, constraining_subdomains);
    1024             :   else
    1025             :     this->distribute_local_dofs_var_major
    1026      405550 :       (next_free_dof, mesh, constraining_subdomains);
    1027             : 
    1028             :   // Get DOF counts on all processors
    1029      406390 :   const auto n_dofs = this->compute_dof_info(next_free_dof);
    1030             : 
    1031             :   // Clear all the current DOF indices
    1032             :   // (distribute_dofs expects them cleared!)
    1033      406390 :   this->invalidate_dofs(mesh);
    1034             : 
    1035      406390 :   next_free_dof = _first_df[proc_id];
    1036             : 
    1037             :   // Set permanent DOF indices on this processor
    1038      406390 :   if (node_major_dofs)
    1039             :     this->distribute_local_dofs_node_major
    1040         840 :       (next_free_dof, mesh, constraining_subdomains);
    1041             :   else
    1042             :     this->distribute_local_dofs_var_major
    1043      405550 :       (next_free_dof, mesh, constraining_subdomains);
    1044             : 
    1045       11614 :   libmesh_assert_equal_to (next_free_dof, _end_df[proc_id]);
    1046             : 
    1047             :   //------------------------------------------------------------
    1048             :   // At this point, all n_comp and dof_number values on local
    1049             :   // DofObjects should be correct, but a DistributedMesh might have
    1050             :   // incorrect values on non-local DofObjects.  Let's request the
    1051             :   // correct values from each other processor.
    1052             : 
    1053      418004 :   if (this->n_processors() > 1)
    1054             :     {
    1055      778692 :       this->set_nonlocal_dof_objects(mesh.nodes_begin(),
    1056      406767 :                                      mesh.nodes_end(),
    1057             :                                      mesh, &DofMap::node_ptr);
    1058             : 
    1059      778692 :       this->set_nonlocal_dof_objects(mesh.elements_begin(),
    1060      790327 :                                      mesh.elements_end(),
    1061             :                                      mesh, &DofMap::elem_ptr);
    1062             :     }
    1063             : 
    1064             : #ifdef DEBUG
    1065             :   {
    1066             :     const unsigned int
    1067       11614 :       sys_num = this->sys_number();
    1068             : 
    1069             :     // Processors should all agree on DoF ids for the newly numbered
    1070             :     // system.
    1071       11614 :     MeshTools::libmesh_assert_valid_dof_ids(mesh, sys_num);
    1072             : 
    1073             :     // DoF processor ids should match DofObject processor ids
    1074     1956733 :     for (auto & node : mesh.node_ptr_range())
    1075             :       {
    1076     1945119 :         DofObject const * const dofobj = node;
    1077     1945119 :         const processor_id_type obj_proc_id = dofobj->processor_id();
    1078             : 
    1079     6683462 :         for (auto v : make_range(dofobj->n_vars(sys_num)))
    1080     7609104 :           for (auto c : make_range(dofobj->n_comp(sys_num,v)))
    1081             :             {
    1082     2870761 :               const dof_id_type dofid = dofobj->dof_number(sys_num,v,c);
    1083     2870761 :               libmesh_assert_greater_equal (dofid, this->first_dof(obj_proc_id));
    1084     2870761 :               libmesh_assert_less (dofid, this->end_dof(obj_proc_id));
    1085             :             }
    1086             :       }
    1087             : 
    1088     1059300 :     for (auto & elem : mesh.element_ptr_range())
    1089             :       {
    1090     1047686 :         DofObject const * const dofobj = elem;
    1091     1047686 :         const processor_id_type obj_proc_id = dofobj->processor_id();
    1092             : 
    1093     3261686 :         for (auto v : make_range(dofobj->n_vars(sys_num)))
    1094     3577218 :           for (auto c : make_range(dofobj->n_comp(sys_num,v)))
    1095             :             {
    1096     1363218 :               const dof_id_type dofid = dofobj->dof_number(sys_num,v,c);
    1097     1363218 :               libmesh_assert_greater_equal (dofid, this->first_dof(obj_proc_id));
    1098     1363218 :               libmesh_assert_less (dofid, this->end_dof(obj_proc_id));
    1099             :             }
    1100             :       }
    1101             :   }
    1102             : #endif
    1103             : 
    1104             :   // start finding SCALAR degrees of freedom
    1105             : #ifdef LIBMESH_ENABLE_AMR
    1106      406390 :   _first_old_scalar_df = _first_scalar_df;
    1107             : #endif
    1108       11614 :   _first_scalar_df.clear();
    1109      406390 :   _first_scalar_df.resize(this->n_variables(), DofObject::invalid_id);
    1110      406390 :   dof_id_type current_SCALAR_dof_index = n_dofs - n_SCALAR_dofs();
    1111             : 
    1112             :   // Calculate and cache the initial DoF indices for SCALAR variables.
    1113             :   // This is an O(N_vars) calculation so we want to do it once per
    1114             :   // renumbering rather than once per SCALAR_dof_indices() call
    1115             : 
    1116     8344477 :   for (auto v : make_range(this->n_variables()))
    1117     7543311 :     if (this->variable(v).type().family == SCALAR)
    1118             :       {
    1119        1485 :         _first_scalar_df[v] = current_SCALAR_dof_index;
    1120        1485 :         current_SCALAR_dof_index += this->variable(v).type().order.get_order();
    1121             :       }
    1122             : 
    1123             :   // Allow our GhostingFunctor objects to reinit if necessary
    1124      814697 :   for (const auto & gf : _algebraic_ghosting_functors)
    1125             :     {
    1126       11668 :       libmesh_assert(gf);
    1127      408307 :       gf->dofmap_reinit();
    1128             :     }
    1129             : 
    1130      812780 :   for (const auto & gf : _coupling_functors)
    1131             :     {
    1132       11614 :       libmesh_assert(gf);
    1133      406390 :       gf->dofmap_reinit();
    1134             :     }
    1135             : 
    1136             :   // Note that in the add_neighbors_to_send_list nodes on processor
    1137             :   // boundaries that are shared by multiple elements are added for
    1138             :   // each element.
    1139      406390 :   this->add_neighbors_to_send_list(mesh);
    1140             : 
    1141             :   // Here we used to clean up that data structure; now System and
    1142             :   // EquationSystems call that for us, after we've added constraint
    1143             :   // dependencies to the send_list too.
    1144             :   // this->sort_send_list ();
    1145             : 
    1146      418004 :   return n_dofs;
    1147             : }
    1148             : 
    1149             : 
    1150             : template <typename T, std::enable_if_t<std::is_same_v<T, dof_id_type> ||
    1151             :                                        std::is_same_v<T, std::vector<dof_id_type>>, int>>
    1152        2348 : void DofMap::local_variable_indices(T & idx,
    1153             :                                     const MeshBase & mesh,
    1154             :                                     unsigned int var_num) const
    1155             : {
    1156             :   // Only used if T == dof_id_type to keep track of the greatest dof we've seen
    1157          44 :   dof_id_type greatest = 0;
    1158             : 
    1159             :   if constexpr (std::is_same_v<T, dof_id_type>)
    1160         946 :     idx = 0;
    1161             :   else if constexpr (std::is_same_v<T, std::vector<dof_id_type>>)
    1162          40 :     idx.clear();
    1163             : 
    1164             :   // Count dofs in the *exact* order that distribute_dofs numbered
    1165             :   // them, so that we can assume ascending indices and use push_back
    1166             :   // instead of find+insert.
    1167             : 
    1168          88 :   const unsigned int sys_num = this->sys_number();
    1169             : 
    1170             :   // If this isn't a SCALAR variable, we need to find all its field
    1171             :   // dofs on the mesh
    1172        2348 :   if (this->variable_type(var_num).family != SCALAR)
    1173             :     {
    1174        2348 :       const Variable & var(this->variable(var_num));
    1175             : 
    1176      262330 :       for (auto & elem : mesh.active_local_element_ptr_range())
    1177             :         {
    1178      132285 :           if (!var.active_on_subdomain(elem->subdomain_id()))
    1179         176 :             continue;
    1180             : 
    1181             :           // Only count dofs connected to active
    1182             :           // elements on this processor.
    1183      132093 :           const unsigned int n_nodes = elem->n_nodes();
    1184             : 
    1185             :           // First get any new nodal DOFS
    1186     1483365 :           for (unsigned int n=0; n<n_nodes; n++)
    1187             :             {
    1188     1351272 :               const Node & node = elem->node_ref(n);
    1189             : 
    1190     1392184 :               if (node.processor_id() != this->processor_id())
    1191      103469 :                 continue;
    1192             : 
    1193     1247007 :               const unsigned int n_comp = node.n_comp(sys_num, var_num);
    1194     1933831 :               for(unsigned int i=0; i<n_comp; i++)
    1195             :                 {
    1196      686824 :                   const dof_id_type index = node.dof_number(sys_num,var_num,i);
    1197       32636 :                   libmesh_assert (this->local_index(index));
    1198             : 
    1199             :                   if constexpr (std::is_same_v<T, dof_id_type>)
    1200             :                     {
    1201      344228 :                       if (idx == 0 || index > greatest)
    1202      143178 :                         { idx++; greatest = index; }
    1203             :                     }
    1204             :                   else if constexpr (std::is_same_v<T, std::vector<dof_id_type>>)
    1205             :                     {
    1206      342596 :                       if (idx.empty() || index > idx.back())
    1207      159448 :                         idx.push_back(index);
    1208             :                     }
    1209             :                 }
    1210             :             }
    1211             : 
    1212             :           // Next get any new element DOFS
    1213      132093 :           const unsigned int n_comp = elem->n_comp(sys_num, var_num);
    1214      132093 :           for (unsigned int i=0; i<n_comp; i++)
    1215             :             {
    1216           0 :               const dof_id_type index = elem->dof_number(sys_num,var_num,i);
    1217             : 
    1218             :               if constexpr (std::is_same_v<T, dof_id_type>)
    1219             :                 {
    1220           0 :                   if (idx == 0 || index > greatest)
    1221           0 :                     { idx++; greatest = index; }
    1222             :                 }
    1223             :               else if constexpr (std::is_same_v<T, std::vector<dof_id_type>>)
    1224             :                 {
    1225           0 :                   if (idx.empty() || index > idx.back())
    1226           0 :                     idx.push_back(index);
    1227             :                 }
    1228             :             }
    1229             :         } // done looping over elements
    1230             : 
    1231             : 
    1232             :       // we may have missed assigning DOFs to nodes that we own
    1233             :       // but to which we have no connected elements matching our
    1234             :       // variable restriction criterion.  this will happen, for example,
    1235             :       // if variable V is restricted to subdomain S.  We may not own
    1236             :       // any elements which live in S, but we may own nodes which are
    1237             :       // *connected* to elements which do.  in this scenario these nodes
    1238             :       // will presently have unnumbered DOFs. we need to take care of
    1239             :       // them here since we own them and no other processor will touch them.
    1240     1471850 :       for (const auto & node : mesh.local_node_ptr_range())
    1241             :         {
    1242       43286 :           libmesh_assert(node);
    1243             : 
    1244      775755 :           const unsigned int n_comp = node->n_comp(sys_num, var_num);
    1245     1078419 :           for (unsigned int i=0; i<n_comp; i++)
    1246             :             {
    1247      302664 :               const dof_id_type index = node->dof_number(sys_num,var_num,i);
    1248             : 
    1249             :               if constexpr (std::is_same_v<T, dof_id_type>)
    1250             :                 {
    1251      143178 :                   if (idx == 0 || index > greatest)
    1252           0 :                     { idx++; greatest = index; }
    1253             :                 }
    1254             :               else if constexpr (std::is_same_v<T, std::vector<dof_id_type>>)
    1255             :                 {
    1256      159486 :                   if (idx.empty() || index > idx.back())
    1257          38 :                     idx.push_back(index);
    1258             :                 }
    1259             :             }
    1260             :         }
    1261             :     }
    1262             :   // Otherwise, count up the SCALAR dofs, if we're on the processor
    1263             :   // that holds this SCALAR variable
    1264           0 :   else if (this->processor_id() == (this->n_processors()-1))
    1265             :     {
    1266           0 :       std::vector<dof_id_type> di_scalar;
    1267           0 :       this->SCALAR_dof_indices(di_scalar,var_num);
    1268             : 
    1269             :       if constexpr (std::is_same_v<T, dof_id_type>)
    1270           0 :         idx += std::distance(di_scalar.begin(), di_scalar.end());
    1271             :       else if constexpr (std::is_same_v<T, std::vector<dof_id_type>>)
    1272           0 :         idx.insert(idx.end(), di_scalar.begin(), di_scalar.end());
    1273             :     }
    1274        2348 : }
    1275             : 
    1276             : template void DofMap::local_variable_indices(dof_id_type &,
    1277             :                                              const MeshBase &,
    1278             :                                              unsigned int) const;
    1279             : 
    1280             : template void DofMap::local_variable_indices(std::vector<dof_id_type> &,
    1281             :                                              const MeshBase &,
    1282             :                                              unsigned int) const;
    1283             : 
    1284             : 
    1285             : std::map<const Node *, std::set<subdomain_id_type>>
    1286      406413 : DofMap::calculate_constraining_subdomains()
    1287             : {
    1288       11616 :   std::map<const Node *, std::set<subdomain_id_type>> constraining_subdomains;
    1289      406413 :   const auto & constraint_rows = _mesh.get_constraint_rows();
    1290             : 
    1291             :   // We can't just loop over constraint rows here because we need
    1292             :   // element subdomain ids for the constrained nodes, but we don't
    1293             :   // want an extra loop if there are no constraint rows.
    1294      406413 :   if (!constraint_rows.empty())
    1295      262290 :     for (auto & elem : _mesh.active_element_ptr_range())
    1296             :       {
    1297      140405 :         const subdomain_id_type sbdid = elem->subdomain_id();
    1298             : 
    1299      682001 :         for (const Node & node : elem->node_ref_range())
    1300             :           {
    1301      541596 :             if (auto it = constraint_rows.find(&node);
    1302       44376 :                 it != constraint_rows.end())
    1303             :               {
    1304     2780932 :                 for (const auto & [pr, val] : it->second)
    1305             :                   {
    1306             :                     const Node * spline_node =
    1307     2318253 :                       pr.first->node_ptr(pr.second);
    1308             : 
    1309     2318253 :                     constraining_subdomains[spline_node].insert(sbdid);
    1310             :                   }
    1311             :               }
    1312             :           }
    1313         982 :       }
    1314             : 
    1315      406413 :   return constraining_subdomains;
    1316             : }
    1317             : 
    1318             : 
    1319        1680 : void DofMap::distribute_local_dofs_node_major
    1320             :   (dof_id_type & next_free_dof,
    1321             :    MeshBase & mesh,
    1322             :    const std::map<const Node *, std::set<subdomain_id_type>> &
    1323             :      constraining_subdomains)
    1324             : {
    1325          96 :   const unsigned int sys_num       = this->sys_number();
    1326          48 :   const unsigned int n_var_groups  = this->n_variable_groups();
    1327             : 
    1328             :   // This is the common case and we want to optimize for it
    1329             :   const bool constraining_subdomains_empty =
    1330          48 :     constraining_subdomains.empty();
    1331             : 
    1332             :   // Our numbering here must be kept consistent with the numbering
    1333             :   // scheme assumed by DofMap::local_variable_indices!
    1334             : 
    1335             :   //-------------------------------------------------------------------------
    1336             :   // First count and assign temporary numbers to local dofs
    1337      128592 :   for (auto & elem : mesh.active_local_element_ptr_range())
    1338             :     {
    1339             :       // Only number dofs connected to active
    1340             :       // elements on this processor.
    1341       68904 :       const unsigned int n_nodes = elem->n_nodes();
    1342             : 
    1343       68904 :       const subdomain_id_type sbdid = elem->subdomain_id();
    1344             : 
    1345             :       // First number the nodal DOFS
    1346      689040 :       for (unsigned int n=0; n<n_nodes; n++)
    1347             :         {
    1348      112752 :           Node & node = elem->node_ref(n);
    1349             : 
    1350     1860408 :           for (unsigned vg=0; vg<n_var_groups; vg++)
    1351             :             {
    1352      112752 :               const VariableGroup & vg_description(this->variable_group(vg));
    1353             : 
    1354     1240272 :               if (vg_description.type().family == SCALAR)
    1355           0 :                 continue;
    1356             : 
    1357             :               bool active_on_node =
    1358     1127520 :                 vg_description.active_on_subdomain(sbdid);
    1359             : 
    1360             :               // Are we at least active indirectly here?
    1361     1240272 :               if (!active_on_node && !constraining_subdomains_empty)
    1362           0 :                 if (auto it = constraining_subdomains.find(&node);
    1363           0 :                     it != constraining_subdomains.end())
    1364           0 :                   for (auto s : it->second)
    1365           0 :                     if (vg_description.active_on_subdomain(s))
    1366             :                       {
    1367           0 :                         active_on_node = true;
    1368           0 :                         break;
    1369             :                       }
    1370             : 
    1371     1240272 :               if (active_on_node)
    1372             :                 {
    1373             :                   // assign dof numbers (all at once) if this is
    1374             :                   // our node and if they aren't already there
    1375      927072 :                   if ((node.n_comp_group(sys_num,vg) > 0) &&
    1376     1319890 :                       (node.processor_id() == this->processor_id()) &&
    1377       79618 :                       (node.vg_dof_base(sys_num,vg) ==
    1378             :                        DofObject::invalid_id))
    1379             :                     {
    1380      368016 :                       node.set_vg_dof_base(sys_num, vg,
    1381             :                                            next_free_dof);
    1382      368016 :                       next_free_dof += (vg_description.n_variables()*
    1383       33456 :                                         node.n_comp_group(sys_num,vg));
    1384             :                       //node.debug_buffer();
    1385             :                     }
    1386             :                 }
    1387             :             }
    1388             :         }
    1389             : 
    1390             :       // Now number the element DOFS
    1391      206712 :       for (unsigned vg=0; vg<n_var_groups; vg++)
    1392             :         {
    1393       12528 :           const VariableGroup & vg_description(this->variable_group(vg));
    1394             : 
    1395      150336 :           if ((vg_description.type().family != SCALAR) &&
    1396      137808 :               (vg_description.active_on_subdomain(elem->subdomain_id())))
    1397      137808 :             if (elem->n_comp_group(sys_num,vg) > 0)
    1398             :               {
    1399           0 :                 libmesh_assert_equal_to (elem->vg_dof_base(sys_num,vg),
    1400             :                                          DofObject::invalid_id);
    1401             : 
    1402           0 :                 elem->set_vg_dof_base(sys_num,
    1403             :                                       vg,
    1404             :                                       next_free_dof);
    1405             : 
    1406           0 :                 next_free_dof += (vg_description.n_variables()*
    1407           0 :                                   elem->n_comp_group(sys_num,vg));
    1408             :               }
    1409             :         }
    1410        1584 :     } // done looping over elements
    1411             : 
    1412             : 
    1413             :   // we may have missed assigning DOFs to nodes that we own
    1414             :   // but to which we have no connected elements matching our
    1415             :   // variable restriction criterion.  this will happen, for example,
    1416             :   // if variable V is restricted to subdomain S.  We may not own
    1417             :   // any elements which live in S, but we may own nodes which are
    1418             :   // *connected* to elements which do.  in this scenario these nodes
    1419             :   // will presently have unnumbered DOFs. we need to take care of
    1420             :   // them here since we own them and no other processor will touch them.
    1421      995472 :   for (auto & node : mesh.local_node_ptr_range())
    1422     1637064 :     for (unsigned vg=0; vg<n_var_groups; vg++)
    1423             :       {
    1424       99216 :         const VariableGroup & vg_description(this->variable_group(vg));
    1425             : 
    1426     1190592 :         if (node->n_comp_group(sys_num,vg))
    1427      368016 :           if (node->vg_dof_base(sys_num,vg) == DofObject::invalid_id)
    1428             :             {
    1429           0 :               node->set_vg_dof_base (sys_num,
    1430             :                                      vg,
    1431             :                                      next_free_dof);
    1432             : 
    1433           0 :               next_free_dof += (vg_description.n_variables()*
    1434           0 :                                 node->n_comp(sys_num,vg));
    1435             :             }
    1436        1584 :       }
    1437             : 
    1438        1680 :   this->distribute_scalar_dofs(next_free_dof);
    1439             : 
    1440             : #ifdef DEBUG
    1441          48 :   this->assert_no_nodes_missed(mesh);
    1442             : #endif // DEBUG
    1443        1680 : }
    1444             : 
    1445             : 
    1446             : 
    1447      811100 : void DofMap::distribute_local_dofs_var_major
    1448             :   (dof_id_type & next_free_dof,
    1449             :    MeshBase & mesh,
    1450             :    const std::map<const Node *, std::set<subdomain_id_type>> &
    1451             :      constraining_subdomains)
    1452             : {
    1453       46360 :   const unsigned int sys_num      = this->sys_number();
    1454       23180 :   const unsigned int n_var_groups = this->n_variable_groups();
    1455             : 
    1456             :   // This is the common case and we want to optimize for it
    1457             :   const bool constraining_subdomains_empty =
    1458       23180 :     constraining_subdomains.empty();
    1459             : 
    1460             :   // Our numbering here must be kept consistent with the numbering
    1461             :   // scheme assumed by DofMap::local_variable_indices!
    1462             : 
    1463             :   //-------------------------------------------------------------------------
    1464             :   // First count and assign temporary numbers to local dofs
    1465     1643092 :   for (unsigned vg=0; vg<n_var_groups; vg++)
    1466             :     {
    1467       23768 :       const VariableGroup & vg_description(this->variable_group(vg));
    1468             : 
    1469       23768 :       const unsigned int n_vars_in_group = vg_description.n_variables();
    1470             : 
    1471             :       // Skip the SCALAR dofs
    1472      831992 :       if (vg_description.type().family == SCALAR)
    1473        2886 :         continue;
    1474             : 
    1475    19769628 :       for (auto & elem : mesh.active_local_element_ptr_range())
    1476             :         {
    1477             :           // Only number dofs connected to active elements on this
    1478             :           // processor and only for variables which are active on on
    1479             :           // this element's subdomain or which are active on the
    1480             :           // subdomain of a node constrained by this node.
    1481             :           const bool active_on_elem =
    1482     9939542 :             vg_description.active_on_subdomain(elem->subdomain_id());
    1483             : 
    1484             :           // If there's no way we're active on this element then we're
    1485             :           // done
    1486     9939542 :           if (!active_on_elem && constraining_subdomains_empty)
    1487       22308 :             continue;
    1488             : 
    1489     9915194 :           const unsigned int n_nodes = elem->n_nodes();
    1490             : 
    1491             :           // First number the nodal DOFS
    1492    89669288 :           for (unsigned int n=0; n<n_nodes; n++)
    1493             :             {
    1494    79754094 :               Node & node = elem->node_ref(n);
    1495             : 
    1496     6519642 :               bool active_on_node = active_on_elem;
    1497    79754094 :               if (!active_on_node)
    1498           0 :                 if (auto it = constraining_subdomains.find(&node);
    1499           0 :                     it != constraining_subdomains.end())
    1500           0 :                   for (auto s : it->second)
    1501           0 :                     if (vg_description.active_on_subdomain(s))
    1502             :                       {
    1503           0 :                         active_on_node = true;
    1504           0 :                         break;
    1505             :                       }
    1506             : 
    1507    13039284 :               if (!active_on_node)
    1508           0 :                 continue;
    1509             : 
    1510             :               // assign dof numbers (all at once) if this is
    1511             :               // our node and if they aren't already there
    1512    43105146 :               if ((node.n_comp_group(sys_num,vg) > 0) &&
    1513    83176042 :                   (node.processor_id() == this->processor_id()) &&
    1514     3421948 :                   (node.vg_dof_base(sys_num,vg) ==
    1515             :                    DofObject::invalid_id))
    1516             :                 {
    1517    13227930 :                   node.set_vg_dof_base(sys_num, vg, next_free_dof);
    1518             : 
    1519    13227930 :                   next_free_dof += (n_vars_in_group*
    1520     1145952 :                                     node.n_comp_group(sys_num,vg));
    1521             :                 }
    1522             :             }
    1523             : 
    1524             :           // Now number the element DOFS
    1525    10785062 :           if (elem->n_comp_group(sys_num,vg) > 0)
    1526             :             {
    1527      235910 :               libmesh_assert_equal_to (elem->vg_dof_base(sys_num,vg),
    1528             :                                        DofObject::invalid_id);
    1529             : 
    1530     2737344 :               elem->set_vg_dof_base(sys_num,
    1531             :                                     vg,
    1532             :                                     next_free_dof);
    1533             : 
    1534     2737344 :               next_free_dof += (n_vars_in_group*
    1535      235910 :                                 elem->n_comp_group(sys_num,vg));
    1536             :             }
    1537      781654 :         } // end loop on elements
    1538             : 
    1539             :       // we may have missed assigning DOFs to nodes that we own
    1540             :       // but to which we have no connected elements matching our
    1541             :       // variable restriction criterion.  this will happen, for example,
    1542             :       // if variable V is restricted to subdomain S.  We may not own
    1543             :       // any elements which live in S, but we may own nodes which are
    1544             :       // *connected* to elements which do.  in this scenario these nodes
    1545             :       // will presently have unnumbered DOFs. we need to take care of
    1546             :       // them here since we own them and no other processor will touch them.
    1547    50893932 :       for (auto & node : mesh.local_node_ptr_range())
    1548    29017710 :         if (node->n_comp_group(sys_num,vg))
    1549    13236546 :           if (node->vg_dof_base(sys_num,vg) == DofObject::invalid_id)
    1550             :             {
    1551        8616 :               node->set_vg_dof_base (sys_num,
    1552             :                                      vg,
    1553             :                                      next_free_dof);
    1554             : 
    1555        8616 :               next_free_dof += (n_vars_in_group*
    1556         682 :                                 node->n_comp_group(sys_num,vg));
    1557      781654 :             }
    1558             :     } // end loop on variable groups
    1559             : 
    1560      811100 :   this->distribute_scalar_dofs(next_free_dof);
    1561             : 
    1562             : #ifdef DEBUG
    1563       23180 :   this->assert_no_nodes_missed(mesh);
    1564             : #endif
    1565      811100 : }
    1566             : 
    1567             : 
    1568             : 
    1569      812780 : void DofMap::distribute_scalar_dofs(dof_id_type & next_free_dof)
    1570             : {
    1571      812780 :   this->_n_SCALAR_dofs = 0;
    1572     1648132 :   for (auto vg : make_range(this->n_variable_groups()))
    1573             :     {
    1574       23864 :       const VariableGroup & vg_description(this->variable_group(vg));
    1575             : 
    1576      835352 :       if (vg_description.type().family == SCALAR)
    1577             :         {
    1578        2970 :           this->_n_SCALAR_dofs += (vg_description.n_variables()*
    1579        2970 :                                    vg_description.type().order.get_order());
    1580        2970 :           continue;
    1581             :         }
    1582             :     }
    1583             : 
    1584             :   // Only increment next_free_dof if we're on the processor
    1585             :   // that holds this SCALAR variable
    1586      836008 :   if (this->processor_id() == (this->n_processors()-1))
    1587      137298 :     next_free_dof += _n_SCALAR_dofs;
    1588      812780 : }
    1589             : 
    1590             : 
    1591             : 
    1592             : #ifdef DEBUG
    1593       23228 : void DofMap::assert_no_nodes_missed(MeshBase & mesh)
    1594             : {
    1595       23228 :   MeshTools::libmesh_assert_valid_procids<Node>(mesh);
    1596             : 
    1597     1978962 :   for (auto & node : mesh.local_node_ptr_range())
    1598             :     {
    1599     1955734 :       unsigned int n_var_g = node->n_var_groups(this->sys_number());
    1600     4372578 :       for (unsigned int vg=0; vg != n_var_g; ++vg)
    1601             :         {
    1602             :           unsigned int n_comp_g =
    1603     2416844 :             node->n_comp_group(this->sys_number(), vg);
    1604     2416844 :           dof_id_type my_first_dof = n_comp_g ?
    1605     1180090 :             node->vg_dof_base(this->sys_number(), vg) : 0;
    1606     2416844 :           libmesh_assert_not_equal_to (my_first_dof, DofObject::invalid_id);
    1607             :         }
    1608             :     }
    1609       23228 : }
    1610             : #endif // DEBUG
    1611             : 
    1612             : 
    1613             : void
    1614     4353382 : DofMap::
    1615             : merge_ghost_functor_outputs(GhostingFunctor::map_type & elements_to_ghost,
    1616             :                             CouplingMatricesSet & temporary_coupling_matrices,
    1617             :                             const GhostingFunctorIterator & gf_begin,
    1618             :                             const GhostingFunctorIterator & gf_end,
    1619             :                             const MeshBase::const_element_iterator & elems_begin,
    1620             :                             const MeshBase::const_element_iterator & elems_end,
    1621             :                             processor_id_type p)
    1622             : {
    1623     8810672 :   for (const auto & gf : as_range(gf_begin, gf_end))
    1624             :     {
    1625      708310 :       GhostingFunctor::map_type more_elements_to_ghost;
    1626             : 
    1627      354155 :       libmesh_assert(gf);
    1628     4457290 :       (*gf)(elems_begin, elems_end, p, more_elements_to_ghost);
    1629             : 
    1630             :       // A GhostingFunctor should only return active elements, but
    1631             :       // I forgot to *document* that, so let's go as easy as we
    1632             :       // can on functors that return inactive elements.
    1633             : #if defined(LIBMESH_ENABLE_DEPRECATED) && defined(LIBMESH_ENABLE_AMR)
    1634      708310 :       std::vector<std::pair<const Elem*, const CouplingMatrix*>> children_to_couple;
    1635     4818252 :       for (auto it = more_elements_to_ghost.begin();
    1636    12769292 :            it != more_elements_to_ghost.end();)
    1637             :         {
    1638     8312002 :           const Elem * elem = it->first;
    1639      715117 :           if (!elem->active())
    1640             :             {
    1641             :               libmesh_deprecated();
    1642           0 :               std::vector<const Elem*> children_to_ghost;
    1643           0 :               elem->active_family_tree(children_to_ghost,
    1644             :                                        /*reset=*/ false);
    1645           0 :               for (const Elem * child : children_to_ghost)
    1646           0 :                 if (child->processor_id() != p)
    1647           0 :                   children_to_couple.emplace_back(child, it->second);
    1648             : 
    1649           0 :               it = more_elements_to_ghost.erase(it);
    1650             :             }
    1651             :           else
    1652      715117 :             ++it;
    1653             :         }
    1654      354155 :       more_elements_to_ghost.insert(children_to_couple.begin(),
    1655             :                                     children_to_couple.end());
    1656             : #endif
    1657             : 
    1658    12769292 :       for (const auto & [elem, elem_cm] : more_elements_to_ghost)
    1659             :         {
    1660             :           // At this point we should only have active elements, even
    1661             :           // if we had to fix up gf output to get here.
    1662      715117 :           libmesh_assert(elem->active());
    1663             : 
    1664     8312002 :           if (const auto existing_it = elements_to_ghost.find(elem);
    1665      715117 :               existing_it == elements_to_ghost.end())
    1666     7112965 :             elements_to_ghost.emplace(elem, elem_cm);
    1667             :           else
    1668             :             {
    1669      509811 :               if (existing_it->second)
    1670             :                 {
    1671           0 :                   if (elem_cm)
    1672             :                     {
    1673             :                       // If this isn't already a temporary
    1674             :                       // then we need to make one so we'll
    1675             :                       // have a non-const matrix to merge
    1676           0 :                       if (temporary_coupling_matrices.empty() ||
    1677           0 :                           !temporary_coupling_matrices.count(existing_it->second))
    1678             :                         {
    1679             :                           // Make copy. This just calls the
    1680             :                           // compiler-generated copy constructor
    1681             :                           // because the CouplingMatrix class does not
    1682             :                           // define a custom copy constructor.
    1683           0 :                           auto result_pr = temporary_coupling_matrices.insert(std::make_unique<CouplingMatrix>(*existing_it->second));
    1684           0 :                           existing_it->second = result_pr.first->get();
    1685             :                         }
    1686             : 
    1687             :                       // Merge elem_cm into existing CouplingMatrix
    1688           0 :                       const_cast<CouplingMatrix &>(*existing_it->second) &= *elem_cm;
    1689             :                     }
    1690             :                   else // elem_cm == nullptr
    1691             :                     {
    1692             :                       // Any existing_it matrix merged with a full
    1693             :                       // matrix (symbolized as nullptr) gives another
    1694             :                       // full matrix (symbolizable as nullptr).
    1695             : 
    1696             :                       // So if existing_it->second is a temporary then
    1697             :                       // we don't need it anymore; we might as well
    1698             :                       // remove it to keep the set of temporaries
    1699             :                       // small.
    1700           0 :                       if (const auto temp_it = temporary_coupling_matrices.find(existing_it->second);
    1701           0 :                           temp_it != temporary_coupling_matrices.end())
    1702           0 :                         temporary_coupling_matrices.erase(temp_it);
    1703             : 
    1704           0 :                       existing_it->second = nullptr;
    1705             :                     }
    1706             :                 }
    1707             :               // else we have a nullptr already, then we have a full
    1708             :               // coupling matrix, already, and merging with anything
    1709             :               // else won't change that, so we're done.
    1710             :             }
    1711             :         }
    1712             :     }
    1713     4353382 : }
    1714             : 
    1715             : 
    1716             : 
    1717      406810 : void DofMap::add_neighbors_to_send_list(MeshBase & mesh)
    1718             : {
    1719       23252 :   LOG_SCOPE("add_neighbors_to_send_list()", "DofMap");
    1720             : 
    1721             :   // Return immediately if there's no ghost data
    1722      418436 :   if (this->n_processors() == 1)
    1723       11243 :     return;
    1724             : 
    1725      395567 :   const unsigned int n_var  = this->n_variables();
    1726             : 
    1727             :   MeshBase::const_element_iterator       local_elem_it
    1728      407193 :     = mesh.active_local_elements_begin();
    1729             :   const MeshBase::const_element_iterator local_elem_end
    1730      791134 :     = mesh.active_local_elements_end();
    1731             : 
    1732       23252 :   GhostingFunctor::map_type elements_to_send;
    1733       23252 :   DofMap::CouplingMatricesSet temporary_coupling_matrices;
    1734             : 
    1735             :   // We need to add dofs to the send list if they've been directly
    1736             :   // requested by an algebraic ghosting functor or they've been
    1737             :   // indirectly requested by a coupling functor.
    1738      407193 :   this->merge_ghost_functor_outputs(elements_to_send,
    1739             :                                     temporary_coupling_matrices,
    1740      767882 :                                     this->algebraic_ghosting_functors_begin(),
    1741      395567 :                                     this->algebraic_ghosting_functors_end(),
    1742             :                                     local_elem_it, local_elem_end, mesh.processor_id());
    1743             : 
    1744      407193 :   this->merge_ghost_functor_outputs(elements_to_send,
    1745             :                                     temporary_coupling_matrices,
    1746      767882 :                                     this->coupling_functors_begin(),
    1747      395567 :                                     this->coupling_functors_end(),
    1748             :                                     local_elem_it, local_elem_end, mesh.processor_id());
    1749             : 
    1750             :   // Making a list of non-zero coupling matrix columns is an
    1751             :   // O(N_var^2) operation.  We cache it so we only have to do it once
    1752             :   // per CouplingMatrix and not once per element.
    1753             :   std::map<const CouplingMatrix *, std::vector<unsigned int>>
    1754       23252 :     column_variable_lists;
    1755             : 
    1756     1693617 :   for (const auto & [partner, ghost_coupling] : elements_to_send)
    1757             :     {
    1758             :       // We asked ghosting functors not to give us local elements
    1759       44863 :       libmesh_assert_not_equal_to
    1760             :         (partner->processor_id(), this->processor_id());
    1761             : 
    1762             :       // Loop over any present coupling matrix column variables if we
    1763             :       // have a coupling matrix, or just add all variables to
    1764             :       // send_list if not.
    1765     1298050 :       if (ghost_coupling)
    1766             :         {
    1767           6 :           libmesh_assert_equal_to (ghost_coupling->size(), n_var);
    1768             : 
    1769             :           // Try to find a cached list of column variables.
    1770             :           std::map<const CouplingMatrix *, std::vector<unsigned int>>::const_iterator
    1771           6 :             column_variable_list = column_variable_lists.find(ghost_coupling);
    1772             : 
    1773             :           // If we didn't find it, then we need to create it.
    1774          74 :           if (column_variable_list == column_variable_lists.end())
    1775             :             {
    1776             :               auto inserted_variable_list_pair =
    1777          54 :                 column_variable_lists.emplace(ghost_coupling, std::vector<unsigned int>());
    1778           4 :               column_variable_list = inserted_variable_list_pair.first;
    1779             : 
    1780             :               std::vector<unsigned int> & new_variable_list =
    1781          54 :                 inserted_variable_list_pair.first->second;
    1782             : 
    1783          58 :               std::vector<unsigned char> has_variable(n_var, false);
    1784             : 
    1785         270 :               for (unsigned int vi = 0; vi != n_var; ++vi)
    1786             :                 {
    1787         216 :                   ConstCouplingRow ccr(vi, *ghost_coupling);
    1788             : 
    1789         486 :                   for (const auto & vj : ccr)
    1790         290 :                     has_variable[vj] = true;
    1791             :                 }
    1792         270 :               for (unsigned int vj = 0; vj != n_var; ++vj)
    1793             :                 {
    1794         232 :                   if (has_variable[vj])
    1795         216 :                     new_variable_list.push_back(vj);
    1796             :                 }
    1797             :             }
    1798             : 
    1799             :           const std::vector<unsigned int> & variable_list =
    1800           6 :             column_variable_list->second;
    1801             : 
    1802         370 :           for (const auto & vj : variable_list)
    1803             :             {
    1804          48 :               std::vector<dof_id_type> di;
    1805         296 :               this->dof_indices (partner, di, vj);
    1806             : 
    1807             :               // Insert the remote DOF indices into the send list
    1808         888 :               for (auto d : di)
    1809         640 :                 if (d != DofObject::invalid_id &&
    1810         544 :                     !this->local_index(d))
    1811             :                   {
    1812          48 :                     libmesh_assert_less(d, this->n_dofs());
    1813         592 :                     _send_list.push_back(d);
    1814             :                   }
    1815             :             }
    1816             :         }
    1817             :       else
    1818             :         {
    1819       89714 :           std::vector<dof_id_type> di;
    1820     1297976 :           this->dof_indices (partner, di);
    1821             : 
    1822             :           // Insert the remote DOF indices into the send list
    1823    26949802 :           for (const auto & dof : di)
    1824    26560604 :             if (dof != DofObject::invalid_id &&
    1825    24743048 :                 !this->local_index(dof))
    1826             :               {
    1827      737214 :                 libmesh_assert_less(dof, this->n_dofs());
    1828    21076429 :                 _send_list.push_back(dof);
    1829             :               }
    1830             :         }
    1831             : 
    1832             :     }
    1833             : 
    1834             :   // We're now done with any merged coupling matrices we had to create.
    1835       11626 :   temporary_coupling_matrices.clear();
    1836             : 
    1837             :   //-------------------------------------------------------------------------
    1838             :   // Our coupling functors added dofs from neighboring elements to the
    1839             :   // send list, but we may still need to add non-local dofs from local
    1840             :   // elements.
    1841             :   //-------------------------------------------------------------------------
    1842             : 
    1843             :   // Loop over the active local elements, adding all active elements
    1844             :   // that neighbor an active local element to the send list.
    1845     7400301 :   for ( ; local_elem_it != local_elem_end; ++local_elem_it)
    1846             :     {
    1847     3909333 :       const Elem * elem = *local_elem_it;
    1848             : 
    1849      813932 :       std::vector<dof_id_type> di;
    1850     3909333 :       this->dof_indices (elem, di);
    1851             : 
    1852             :       // Insert the remote DOF indices into the send list
    1853    44981618 :       for (const auto & dof : di)
    1854    45251832 :         if (dof != DofObject::invalid_id &&
    1855    36892738 :             !this->local_index(dof))
    1856             :           {
    1857      189127 :             libmesh_assert_less(dof, this->n_dofs());
    1858     5172778 :             _send_list.push_back(dof);
    1859             :           }
    1860             :     }
    1861             : }
    1862             : 
    1863             : 
    1864             : 
    1865      407876 : void DofMap::prepare_send_list ()
    1866             : {
    1867       23312 :   LOG_SCOPE("prepare_send_list()", "DofMap");
    1868             : 
    1869             :   // Return immediately if there's no ghost data
    1870      419532 :   if (this->n_processors() == 1)
    1871       11274 :     return;
    1872             : 
    1873             :   // Check to see if we have any extra stuff to add to the send_list
    1874      396602 :   if (_extra_send_list_function)
    1875             :     {
    1876           0 :       if (_augment_send_list)
    1877             :         {
    1878           0 :           libmesh_here();
    1879           0 :           libMesh::out << "WARNING:  You have specified both an extra send list function and object.\n"
    1880           0 :                        << "          Are you sure this is what you meant to do??"
    1881           0 :                        << std::endl;
    1882             :         }
    1883             : 
    1884           0 :       _extra_send_list_function(_send_list, _extra_send_list_context);
    1885             :     }
    1886             : 
    1887      396602 :   if (_augment_send_list)
    1888           0 :     _augment_send_list->augment_send_list (_send_list);
    1889             : 
    1890             :   // First sort the send list.  After this
    1891             :   // duplicated elements will be adjacent in the
    1892             :   // vector
    1893      396602 :   std::sort(_send_list.begin(), _send_list.end());
    1894             : 
    1895             :   // Now use std::unique to remove duplicate entries
    1896             :   std::vector<dof_id_type>::iterator new_end =
    1897      396602 :     std::unique (_send_list.begin(), _send_list.end());
    1898             : 
    1899             :   // Remove the end of the send_list.  Use the "swap trick"
    1900             :   // from Effective STL
    1901      781548 :   std::vector<dof_id_type> (_send_list.begin(), new_end).swap (_send_list);
    1902             : 
    1903             :   // Make sure the send list has nothing invalid in it.
    1904       11656 :   libmesh_assert(_send_list.empty() || _send_list.back() < this->n_dofs());
    1905             : }
    1906             : 
    1907         420 : void DofMap::reinit_send_list (MeshBase & mesh)
    1908             : {
    1909          12 :   this->clear_send_list();
    1910         420 :   this->add_neighbors_to_send_list(mesh);
    1911             : 
    1912             : #ifdef LIBMESH_ENABLE_CONSTRAINTS
    1913             :   // This is assuming that we only need to recommunicate
    1914             :   // the constraints and no new ones have been added since
    1915             :   // a previous call to reinit_constraints.
    1916         420 :   this->process_constraints(mesh);
    1917             : #endif
    1918         420 :   this->prepare_send_list();
    1919         420 : }
    1920             : 
    1921         142 : void DofMap::set_implicit_neighbor_dofs(bool implicit_neighbor_dofs)
    1922             : {
    1923         142 :   _implicit_neighbor_dofs_initialized = true;
    1924         142 :   _implicit_neighbor_dofs = implicit_neighbor_dofs;
    1925         142 : }
    1926             : 
    1927           0 : void DofMap::set_verify_dirichlet_bc_consistency(bool val)
    1928             : {
    1929           0 :   _verify_dirichlet_bc_consistency = val;
    1930           0 : }
    1931             : 
    1932             : 
    1933      826906 : bool DofMap::use_coupled_neighbor_dofs(const MeshBase & /*mesh*/) const
    1934             : {
    1935             :   // If we were asked on the command line, then we need to
    1936             :   // include sensitivities between neighbor degrees of freedom
    1937             :   bool implicit_neighbor_dofs =
    1938      826906 :     libMesh::on_command_line ("--implicit-neighbor-dofs");
    1939             : 
    1940             :   // If the user specifies --implicit-neighbor-dofs 0, then
    1941             :   // presumably he knows what he is doing and we won't try to
    1942             :   // automatically turn it on even when all the variables are
    1943             :   // discontinuous.
    1944      826906 :   if (implicit_neighbor_dofs)
    1945             :     {
    1946             :       // No flag provided defaults to 'true'
    1947           0 :       int flag = 1;
    1948           0 :       flag = libMesh::command_line_next ("--implicit-neighbor-dofs", flag);
    1949             : 
    1950           0 :       if (!flag)
    1951             :         {
    1952             :           // The user said --implicit-neighbor-dofs 0, so he knows
    1953             :           // what he is doing and really doesn't want it.
    1954           0 :           return false;
    1955             :         }
    1956             :     }
    1957             : 
    1958             :   // Possibly override the commandline option, if set_implicit_neighbor_dofs
    1959             :   // has been called.
    1960      826906 :   if (_implicit_neighbor_dofs_initialized)
    1961             :     {
    1962         426 :       implicit_neighbor_dofs = _implicit_neighbor_dofs;
    1963             : 
    1964             :       // Again, if the user explicitly says implicit_neighbor_dofs = false,
    1965             :       // then we return here.
    1966         426 :       if (!implicit_neighbor_dofs)
    1967           0 :         return false;
    1968             :     }
    1969             : 
    1970             :   // Look at all the variables in this system.  If every one is
    1971             :   // discontinuous then the user must be doing DG/FVM, so be nice
    1972             :   // and force implicit_neighbor_dofs=true.
    1973             :   {
    1974       23640 :     bool all_discontinuous_dofs = true;
    1975             : 
    1976             :     // We may call this method even without ever having initialized our data
    1977    15947098 :       for (auto var : index_range(this->_variables))
    1978    15120192 :         if (FEInterface::get_continuity(this->variable_type(var)) !=  DISCONTINUOUS)
    1979      413710 :           all_discontinuous_dofs = false;
    1980             : 
    1981      826906 :     if (all_discontinuous_dofs)
    1982       11618 :       implicit_neighbor_dofs = true;
    1983             :   }
    1984             : 
    1985       23640 :   return implicit_neighbor_dofs;
    1986             : }
    1987             : 
    1988             : 
    1989             : 
    1990       40493 : void DofMap::compute_sparsity(const MeshBase & mesh)
    1991             : {
    1992       79724 :   _sp = this->build_sparsity(mesh, this->_constrained_sparsity_construction);
    1993             : 
    1994             :   // It is possible that some \p SparseMatrix implementations want to
    1995             :   // see the sparsity pattern before we throw it away.  If so, we
    1996             :   // share a view of its arrays, and we pass it in to the matrices.
    1997       81905 :   for (const auto & mat : _matrices)
    1998             :     {
    1999       42700 :       mat->attach_sparsity_pattern (*_sp);
    2000       41412 :       if (_need_full_sparsity_pattern)
    2001           0 :         mat->update_sparsity_pattern (_sp->get_sparsity_pattern());
    2002             :     }
    2003             :   // If we don't need the full sparsity pattern anymore, free the
    2004             :   // parts of it we don't need.
    2005       40493 :   if (!_need_full_sparsity_pattern)
    2006       40493 :     _sp->clear_full_sparsity();
    2007       40493 : }
    2008             : 
    2009             : 
    2010             : 
    2011      394766 : void DofMap::clear_sparsity()
    2012             : {
    2013       11304 :   _sp.reset();
    2014      394766 : }
    2015             : 
    2016             : 
    2017             : 
    2018         355 : void DofMap::remove_default_ghosting()
    2019             : {
    2020         355 :   this->remove_coupling_functor(this->default_coupling());
    2021         355 :   this->remove_algebraic_ghosting_functor(this->default_algebraic_ghosting());
    2022         355 : }
    2023             : 
    2024             : 
    2025             : 
    2026          71 : void DofMap::add_default_ghosting()
    2027             : {
    2028          71 :   this->add_coupling_functor(this->default_coupling());
    2029          71 :   this->add_algebraic_ghosting_functor(this->default_algebraic_ghosting());
    2030          71 : }
    2031             : 
    2032             : 
    2033             : 
    2034             : void
    2035      749437 : DofMap::add_coupling_functor(GhostingFunctor & coupling_functor,
    2036             :                              bool to_mesh)
    2037             : {
    2038             :   // We used to implicitly support duplicate inserts to std::set
    2039             : #ifdef LIBMESH_ENABLE_DEPRECATED
    2040             :   _coupling_functors.erase
    2041      706993 :     (std::remove(_coupling_functors.begin(),
    2042             :                  _coupling_functors.end(),
    2043      749437 :                  &coupling_functor),
    2044       63666 :      _coupling_functors.end());
    2045             : #endif
    2046             : 
    2047             :   // We shouldn't have two copies of the same functor
    2048       21222 :   libmesh_assert(std::find(_coupling_functors.begin(),
    2049             :                            _coupling_functors.end(),
    2050             :                            &coupling_functor) ==
    2051             :                  _coupling_functors.end());
    2052             : 
    2053      749437 :   _coupling_functors.push_back(&coupling_functor);
    2054      749437 :   coupling_functor.set_mesh(&_mesh);
    2055      749437 :   if (to_mesh)
    2056      749366 :     _mesh.add_ghosting_functor(coupling_functor);
    2057      749437 : }
    2058             : 
    2059             : 
    2060             : 
    2061             : void
    2062         639 : DofMap::remove_coupling_functor(GhostingFunctor & coupling_functor)
    2063             : {
    2064         603 :   auto raw_it = std::find(_coupling_functors.begin(),
    2065         657 :                           _coupling_functors.end(), &coupling_functor);
    2066             : 
    2067             : #ifndef LIBMESH_ENABLE_DEPRECATED
    2068             :   // We shouldn't be trying to remove a functor that isn't there
    2069             :   libmesh_assert(raw_it != _coupling_functors.end());
    2070             : #else
    2071             :   // Our old API supported trying to remove a functor that isn't there
    2072         639 :   if (raw_it != _coupling_functors.end())
    2073             : #endif
    2074         639 :     _coupling_functors.erase(raw_it);
    2075             : 
    2076             :   // We shouldn't have had two copies of the same functor
    2077          18 :   libmesh_assert(std::find(_coupling_functors.begin(),
    2078             :                            _coupling_functors.end(),
    2079             :                            &coupling_functor) ==
    2080             :                  _coupling_functors.end());
    2081             : 
    2082         639 :   _mesh.remove_ghosting_functor(coupling_functor);
    2083             : 
    2084         639 :   if (const auto it = _shared_functors.find(&coupling_functor);
    2085          18 :       it != _shared_functors.end())
    2086           0 :     _shared_functors.erase(it);
    2087         639 : }
    2088             : 
    2089             : 
    2090             : 
    2091             : void
    2092      749721 : DofMap::add_algebraic_ghosting_functor(GhostingFunctor & evaluable_functor,
    2093             :                                        bool to_mesh)
    2094             : {
    2095             :   // We used to implicitly support duplicate inserts to std::set
    2096             : #ifdef LIBMESH_ENABLE_DEPRECATED
    2097             :   _algebraic_ghosting_functors.erase
    2098      707261 :     (std::remove(_algebraic_ghosting_functors.begin(),
    2099             :                  _algebraic_ghosting_functors.end(),
    2100      749721 :                  &evaluable_functor),
    2101       63690 :      _algebraic_ghosting_functors.end());
    2102             : #endif
    2103             : 
    2104             :   // We shouldn't have two copies of the same functor
    2105       21230 :   libmesh_assert(std::find(_algebraic_ghosting_functors.begin(),
    2106             :                            _algebraic_ghosting_functors.end(),
    2107             :                            &evaluable_functor) ==
    2108             :                  _algebraic_ghosting_functors.end());
    2109             : 
    2110      749721 :   _algebraic_ghosting_functors.push_back(&evaluable_functor);
    2111      749721 :   evaluable_functor.set_mesh(&_mesh);
    2112      749721 :   if (to_mesh)
    2113      749721 :     _mesh.add_ghosting_functor(evaluable_functor);
    2114      749721 : }
    2115             : 
    2116             : 
    2117             : 
    2118             : void
    2119         639 : DofMap::remove_algebraic_ghosting_functor(GhostingFunctor & evaluable_functor)
    2120             : {
    2121         603 :   auto raw_it = std::find(_algebraic_ghosting_functors.begin(),
    2122             :                           _algebraic_ghosting_functors.end(),
    2123         657 :                           &evaluable_functor);
    2124             : 
    2125             : #ifndef LIBMESH_ENABLE_DEPRECATED
    2126             :   // We shouldn't be trying to remove a functor that isn't there
    2127             :   libmesh_assert(raw_it != _algebraic_ghosting_functors.end());
    2128             : #else
    2129             :   // Our old API supported trying to remove a functor that isn't there
    2130         639 :   if (raw_it != _algebraic_ghosting_functors.end())
    2131             : #endif
    2132         639 :     _algebraic_ghosting_functors.erase(raw_it);
    2133             : 
    2134             :   // We shouldn't have had two copies of the same functor
    2135          18 :   libmesh_assert(std::find(_algebraic_ghosting_functors.begin(),
    2136             :                            _algebraic_ghosting_functors.end(),
    2137             :                            &evaluable_functor) ==
    2138             :                  _algebraic_ghosting_functors.end());
    2139             : 
    2140         639 :   _mesh.remove_ghosting_functor(evaluable_functor);
    2141             : 
    2142         639 :   if (const auto it = _shared_functors.find(&evaluable_functor);
    2143          18 :       it != _shared_functors.end())
    2144           0 :     _shared_functors.erase(it);
    2145         639 : }
    2146             : 
    2147             : 
    2148             : 
    2149           0 : void DofMap::extract_local_vector (const NumericVector<Number> & Ug,
    2150             :                                    const std::vector<dof_id_type> & dof_indices_in,
    2151             :                                    DenseVectorBase<Number> & Ue) const
    2152             : {
    2153           0 :   const unsigned int n_original_dofs = dof_indices_in.size();
    2154             : 
    2155             : #ifdef LIBMESH_ENABLE_AMR
    2156             : 
    2157             :   // Trivial mapping
    2158           0 :   libmesh_assert_equal_to (dof_indices_in.size(), Ue.size());
    2159           0 :   bool has_constrained_dofs = false;
    2160             : 
    2161           0 :   for (unsigned int il=0; il != n_original_dofs; ++il)
    2162             :     {
    2163           0 :       const dof_id_type ig = dof_indices_in[il];
    2164             : 
    2165           0 :       if (this->is_constrained_dof (ig)) has_constrained_dofs = true;
    2166             : 
    2167           0 :       libmesh_assert_less (ig, Ug.size());
    2168             : 
    2169           0 :       Ue.el(il) = Ug(ig);
    2170             :     }
    2171             : 
    2172             :   // If the element has any constrained DOFs then we need
    2173             :   // to account for them in the mapping.  This will handle
    2174             :   // the case that the input vector is not constrained.
    2175           0 :   if (has_constrained_dofs)
    2176             :     {
    2177             :       // Copy the input DOF indices.
    2178           0 :       std::vector<dof_id_type> constrained_dof_indices(dof_indices_in);
    2179             : 
    2180           0 :       DenseMatrix<Number> C;
    2181           0 :       DenseVector<Number> H;
    2182             : 
    2183           0 :       this->build_constraint_matrix_and_vector (C, H, constrained_dof_indices);
    2184             : 
    2185           0 :       libmesh_assert_equal_to (dof_indices_in.size(), C.m());
    2186           0 :       libmesh_assert_equal_to (constrained_dof_indices.size(), C.n());
    2187             : 
    2188             :       // zero-out Ue
    2189           0 :       Ue.zero();
    2190             : 
    2191             :       // compute Ue = C Ug, with proper mapping.
    2192           0 :       for (unsigned int i=0; i != n_original_dofs; i++)
    2193             :         {
    2194           0 :           Ue.el(i) = H(i);
    2195             : 
    2196             :           const unsigned int n_constrained =
    2197           0 :             cast_int<unsigned int>(constrained_dof_indices.size());
    2198           0 :           for (unsigned int j=0; j<n_constrained; j++)
    2199             :             {
    2200           0 :               const dof_id_type jg = constrained_dof_indices[j];
    2201             : 
    2202             :               //          If Ug is a serial or ghosted vector, then this assert is
    2203             :               //          overzealous.  If Ug is a parallel vector, then this assert
    2204             :               //          is redundant.
    2205             :               //    libmesh_assert ((jg >= Ug.first_local_index()) &&
    2206             :               //    (jg <  Ug.last_local_index()));
    2207             : 
    2208           0 :               Ue.el(i) += C(i,j)*Ug(jg);
    2209             :             }
    2210             :         }
    2211           0 :     }
    2212             : 
    2213             : #else
    2214             : 
    2215             :   // Trivial mapping
    2216             : 
    2217             :   libmesh_assert_equal_to (n_original_dofs, Ue.size());
    2218             : 
    2219             :   for (unsigned int il=0; il<n_original_dofs; il++)
    2220             :     {
    2221             :       const dof_id_type ig = dof_indices_in[il];
    2222             : 
    2223             :       libmesh_assert ((ig >= Ug.first_local_index()) && (ig <  Ug.last_local_index()));
    2224             : 
    2225             :       Ue.el(il) = Ug(ig);
    2226             :     }
    2227             : 
    2228             : #endif
    2229           0 : }
    2230             : 
    2231   144011719 : void DofMap::dof_indices (const Elem * const elem,
    2232             :                           std::vector<dof_id_type> & di) const
    2233             : {
    2234             :   // We now allow elem==nullptr to request just SCALAR dofs
    2235             :   // libmesh_assert(elem);
    2236             : 
    2237             :   // If we are asking for current indices on an element, it ought to
    2238             :   // be an active element (or a temporary side, which also thinks it's
    2239             :   // active)
    2240    10930897 :   libmesh_assert(!elem || elem->active());
    2241             : 
    2242             :   // dof_indices() is a relatively light-weight function that is
    2243             :   // called millions of times in normal codes. Therefore, it is not a
    2244             :   // good candidate for logging, since the cost of the logging code
    2245             :   // itself is roughly on par with the time required to call
    2246             :   // dof_indices().
    2247             :   // LOG_SCOPE("dof_indices()", "DofMap");
    2248             : 
    2249             :   // Clear the DOF indices vector
    2250    10930897 :   di.clear();
    2251             : 
    2252    10930897 :   const unsigned int n_var_groups  = this->n_variable_groups();
    2253             : 
    2254             : #ifdef DEBUG
    2255             :   // Check that sizes match in DEBUG mode
    2256    10930897 :   std::size_t tot_size = 0;
    2257             : #endif
    2258             : 
    2259   144011719 :   if (elem && elem->type() == TRI3SUBDIVISION)
    2260             :     {
    2261             :       // Subdivision surface FE require the 1-ring around elem
    2262         616 :       const Tri3Subdivision * sd_elem = static_cast<const Tri3Subdivision *>(elem);
    2263             : 
    2264             :       // Ghost subdivision elements have no real dofs
    2265        7238 :       if (!sd_elem->is_ghost())
    2266             :         {
    2267             :           // Determine the nodes contributing to element elem
    2268        1088 :           std::vector<const Node *> elem_nodes;
    2269        6457 :           MeshTools::Subdivision::find_one_ring(sd_elem, elem_nodes);
    2270             : 
    2271             :           // Get the dof numbers
    2272       12914 :           for (unsigned int vg=0; vg<n_var_groups; vg++)
    2273             :             {
    2274         544 :               const VariableGroup & var = this->variable_group(vg);
    2275         544 :               const unsigned int vars_in_group = var.n_variables();
    2276             : 
    2277        6457 :               if (var.type().family == SCALAR &&
    2278           0 :                   var.active_on_subdomain(elem->subdomain_id()))
    2279             :                 {
    2280           0 :                   for (unsigned int vig=0; vig != vars_in_group; ++vig)
    2281             :                     {
    2282             : #ifdef DEBUG
    2283           0 :                       tot_size += var.type().order;
    2284             : #endif
    2285           0 :                       std::vector<dof_id_type> di_new;
    2286           0 :                       this->SCALAR_dof_indices(di_new,var.number(vig));
    2287           0 :                       di.insert( di.end(), di_new.begin(), di_new.end());
    2288             :                     }
    2289             :                 }
    2290             :               else
    2291       25828 :                 for (unsigned int vig=0; vig != vars_in_group; ++vig)
    2292             :                   {
    2293       24267 :                     _dof_indices(*elem, elem->p_level(), di, vg, vig,
    2294        1632 :                                  elem_nodes.data(),
    2295             :                                  cast_int<unsigned int>(elem_nodes.size()),
    2296             :                                  var.number(vig)
    2297             : #ifdef DEBUG
    2298             :                                  , tot_size
    2299             : #endif
    2300             :                                  );
    2301             :                   }
    2302             :             }
    2303             :         }
    2304             : 
    2305        7238 :       return;
    2306             :     }
    2307             : 
    2308             :   // Get the dof numbers for each variable
    2309   144004481 :   const unsigned int n_nodes = elem ? elem->n_nodes() : 0;
    2310   294829497 :   for (unsigned int vg=0; vg<n_var_groups; vg++)
    2311             :     {
    2312    11487267 :       const VariableGroup & var = this->variable_group(vg);
    2313    11487267 :       const unsigned int vars_in_group = var.n_variables();
    2314             : 
    2315   150904402 :       if (var.type().family == SCALAR &&
    2316      819811 :           (!elem ||
    2317      899197 :            var.active_on_subdomain(elem->subdomain_id())))
    2318             :         {
    2319     1798394 :           for (unsigned int vig=0; vig != vars_in_group; ++vig)
    2320             :             {
    2321             : #ifdef DEBUG
    2322       79386 :               tot_size += var.type().order;
    2323             : #endif
    2324       79386 :               std::vector<dof_id_type> di_new;
    2325      978583 :               this->SCALAR_dof_indices(di_new,var.number(vig));
    2326      899197 :               di.insert( di.end(), di_new.begin(), di_new.end());
    2327             :             }
    2328             :         }
    2329   149925819 :       else if (elem)
    2330   321927003 :         for (unsigned int vig=0; vig != vars_in_group; ++vig)
    2331             :           {
    2332   185118394 :             _dof_indices(*elem, elem->p_level(), di, vg, vig,
    2333             :                          elem->get_nodes(), n_nodes, var.number(vig)
    2334             : #ifdef DEBUG
    2335             :                          , tot_size
    2336             : #endif
    2337             :                      );
    2338             :           }
    2339             :     }
    2340             : 
    2341             : #ifdef DEBUG
    2342    10930281 :   libmesh_assert_equal_to (tot_size, di.size());
    2343             : #endif
    2344             : }
    2345             : 
    2346             : 
    2347   124974925 : void DofMap::dof_indices (const Elem * const elem,
    2348             :                           std::vector<dof_id_type> & di,
    2349             :                           const unsigned int vn,
    2350             :                           int p_level) const
    2351             : {
    2352   124974925 :   dof_indices(
    2353             :       elem,
    2354             :       di,
    2355             :       vn,
    2356      928978 :       [](const Elem &,
    2357             :          std::vector<dof_id_type> & dof_indices,
    2358      195846 :          const std::vector<dof_id_type> & scalar_dof_indices) {
    2359     1222747 :         dof_indices.insert(dof_indices.end(), scalar_dof_indices.begin(), scalar_dof_indices.end());
    2360     1124824 :       },
    2361             :       [](const Elem &,
    2362             :          unsigned int,
    2363             :          unsigned int,
    2364             :          std::vector<dof_id_type> & dof_indices,
    2365   616039303 :          const dof_id_type dof) { dof_indices.push_back(dof); },
    2366             :       p_level);
    2367   124974925 : }
    2368             : 
    2369         120 : void DofMap::array_dof_indices(const Elem * const elem,
    2370             :                                std::vector<dof_id_type> & di,
    2371             :                                const unsigned int vn,
    2372             :                                int p_level) const
    2373             : {
    2374         110 :   auto dof_indices_functor = [elem, p_level, this](std::vector<dof_id_type> & functor_di,
    2375         190 :                                                    const unsigned int functor_vn) {
    2376         150 :     this->dof_indices(elem, functor_di, functor_vn, p_level);
    2377         250 :   };
    2378         120 :   this->array_dof_indices(dof_indices_functor, di, vn);
    2379         120 : }
    2380             : 
    2381           0 : void DofMap::array_dof_indices(const Node * const node,
    2382             :                                std::vector<dof_id_type> & di,
    2383             :                                const unsigned int vn) const
    2384             : {
    2385             :   auto dof_indices_functor = [node, this](std::vector<dof_id_type> & functor_di,
    2386           0 :                                           const unsigned int functor_vn) {
    2387           0 :     this->dof_indices(node, functor_di, functor_vn);
    2388           0 :   };
    2389           0 :   this->array_dof_indices(dof_indices_functor, di, vn);
    2390           0 : }
    2391             : 
    2392      167508 : void DofMap::dof_indices (const Node * const node,
    2393             :                           std::vector<dof_id_type> & di) const
    2394             : {
    2395             :   // We allow node==nullptr to request just SCALAR dofs
    2396             :   // libmesh_assert(elem);
    2397             : 
    2398             :   // dof_indices() is a relatively light-weight function that is
    2399             :   // called millions of times in normal codes. Therefore, it is not a
    2400             :   // good candidate for logging, since the cost of the logging code
    2401             :   // itself is roughly on par with the time required to call
    2402             :   // dof_indices().
    2403             :   // LOG_SCOPE("dof_indices(Node)", "DofMap");
    2404             : 
    2405             :   // Clear the DOF indices vector
    2406       15228 :   di.clear();
    2407             : 
    2408       15228 :   const unsigned int n_var_groups  = this->n_variable_groups();
    2409       30456 :   const unsigned int sys_num = this->sys_number();
    2410             : 
    2411             :   // Get the dof numbers
    2412      502524 :   for (unsigned int vg=0; vg<n_var_groups; vg++)
    2413             :     {
    2414       30456 :       const VariableGroup & var = this->variable_group(vg);
    2415       30456 :       const unsigned int vars_in_group = var.n_variables();
    2416             : 
    2417      335016 :       if (var.type().family == SCALAR)
    2418             :         {
    2419           0 :           for (unsigned int vig=0; vig != vars_in_group; ++vig)
    2420             :             {
    2421           0 :               std::vector<dof_id_type> di_new;
    2422           0 :               this->SCALAR_dof_indices(di_new,var.number(vig));
    2423           0 :               di.insert( di.end(), di_new.begin(), di_new.end());
    2424             :             }
    2425             :         }
    2426             :       else
    2427             :         {
    2428      335016 :           const int n_comp = node->n_comp_group(sys_num,vg);
    2429      837540 :           for (unsigned int vig=0; vig != vars_in_group; ++vig)
    2430             :             {
    2431      911988 :               for (int i=0; i != n_comp; ++i)
    2432             :                 {
    2433             :                   const dof_id_type d =
    2434      409464 :                     node->dof_number(sys_num, vg, vig, i, n_comp);
    2435       37224 :                   libmesh_assert_not_equal_to
    2436             :                     (d, DofObject::invalid_id);
    2437      409464 :                   di.push_back(d);
    2438             :                 }
    2439             :             }
    2440             :         }
    2441             :     }
    2442      167508 : }
    2443             : 
    2444             : 
    2445         600 : void DofMap::dof_indices (const Node * const node,
    2446             :                           std::vector<dof_id_type> & di,
    2447             :                           const unsigned int vn) const
    2448             : {
    2449         600 :   if (vn == libMesh::invalid_uint)
    2450             :     {
    2451           0 :       this->dof_indices(node, di);
    2452           0 :       return;
    2453             :     }
    2454             : 
    2455             :   // We allow node==nullptr to request just SCALAR dofs
    2456             :   // libmesh_assert(elem);
    2457             : 
    2458             :   // dof_indices() is a relatively light-weight function that is
    2459             :   // called millions of times in normal codes. Therefore, it is not a
    2460             :   // good candidate for logging, since the cost of the logging code
    2461             :   // itself is roughly on par with the time required to call
    2462             :   // dof_indices().
    2463             :   // LOG_SCOPE("dof_indices(Node)", "DofMap");
    2464             : 
    2465             :   // Clear the DOF indices vector
    2466          50 :   di.clear();
    2467             : 
    2468         100 :   const unsigned int sys_num = this->sys_number();
    2469             : 
    2470             :   // Get the dof numbers
    2471         650 :   const unsigned int vg = this->_variable_group_numbers[vn];
    2472          50 :   const VariableGroup & var = this->variable_group(vg);
    2473             : 
    2474         600 :   if (var.type().family == SCALAR)
    2475             :     {
    2476           0 :       std::vector<dof_id_type> di_new;
    2477           0 :       this->SCALAR_dof_indices(di_new,vn);
    2478           0 :       di.insert( di.end(), di_new.begin(), di_new.end());
    2479             :     }
    2480             :   else
    2481             :     {
    2482         600 :       const unsigned int vig = vn - var.number();
    2483         600 :       const int n_comp = node->n_comp_group(sys_num,vg);
    2484         960 :       for (int i=0; i != n_comp; ++i)
    2485             :         {
    2486             :           const dof_id_type d =
    2487         360 :             node->dof_number(sys_num, vg, vig, i, n_comp);
    2488          30 :           libmesh_assert_not_equal_to
    2489             :             (d, DofObject::invalid_id);
    2490         360 :           di.push_back(d);
    2491             :         }
    2492             :     }
    2493             : }
    2494             : 
    2495             : 
    2496     5442787 : void DofMap::dof_indices (const Elem & elem,
    2497             :                           unsigned int n,
    2498             :                           std::vector<dof_id_type> & di,
    2499             :                           const unsigned int vn) const
    2500             : {
    2501     5442787 :   this->_node_dof_indices(elem, n, elem.node_ref(n), di, vn);
    2502     5442787 : }
    2503             : 
    2504             : 
    2505             : 
    2506             : #ifdef LIBMESH_ENABLE_AMR
    2507             : 
    2508     4408777 : void DofMap::old_dof_indices (const Elem & elem,
    2509             :                               unsigned int n,
    2510             :                               std::vector<dof_id_type> & di,
    2511             :                               const unsigned int vn) const
    2512             : {
    2513      451492 :   const DofObject & old_obj = elem.node_ref(n).get_old_dof_object_ref();
    2514     4408777 :   this->_node_dof_indices(elem, n, old_obj, di, vn);
    2515     4408777 : }
    2516             : 
    2517             : #endif // LIBMESH_ENABLE_AMR
    2518             : 
    2519             : 
    2520             : 
    2521     9851564 : void DofMap::_node_dof_indices (const Elem & elem,
    2522             :                                 unsigned int n,
    2523             :                                 const DofObject & obj,
    2524             :                                 std::vector<dof_id_type> & di,
    2525             :                                 const unsigned int vn) const
    2526             : {
    2527             :   // Half of this is a cut and paste of _dof_indices code below, but
    2528             :   // duplication actually seems cleaner than creating a helper
    2529             :   // function with a million arguments and hoping the compiler inlines
    2530             :   // it properly into one of our most highly trafficked functions.
    2531             : 
    2532             :   // dof_indices() is a relatively light-weight function; the cost of
    2533             :   // the logging code itself is roughly on par with the time required
    2534             :   // to call dof_indices().
    2535             :   // LOG_SCOPE("_node_dof_indices()", "DofMap");
    2536             : 
    2537     1886690 :   const unsigned int sys_num = this->sys_number();
    2538      943345 :   const auto [vg, vig] =
    2539     8908219 :     obj.var_to_vg_and_offset(sys_num,vn);
    2540     8908219 :   const unsigned int n_comp = obj.n_comp_group(sys_num,vg);
    2541             : 
    2542      943345 :   const VariableGroup & var = this->variable_group(vg);
    2543     9851564 :   FEType fe_type = var.type();
    2544             :   const bool extra_hanging_dofs =
    2545     9851564 :     FEInterface::extra_hanging_dofs(fe_type);
    2546             : 
    2547     9851564 :   const bool add_p_level = fe_type.p_refinement;
    2548             : 
    2549             :   // There is a potential problem with h refinement.  Imagine a
    2550             :   // quad9 that has a linear FE on it.  Then, on the hanging side,
    2551             :   // it can falsely identify a DOF at the mid-edge node. This is why
    2552             :   // we go through FEInterface instead of obj->n_comp() directly.
    2553             :   const unsigned int nc =
    2554     9851564 :     FEInterface::n_dofs_at_node(fe_type, &elem, n, add_p_level);
    2555             : 
    2556             :   // If this is a non-vertex on a hanging node with extra
    2557             :   // degrees of freedom, we use the non-vertex dofs (which
    2558             :   // come in reverse order starting from the end, to
    2559             :   // simplify p refinement)
    2560     9851564 :   if (extra_hanging_dofs && nc && !elem.is_vertex(n))
    2561             :     {
    2562     2119604 :       const int dof_offset = n_comp - nc;
    2563             : 
    2564             :       // We should never have fewer dofs than necessary on a
    2565             :       // node unless we're getting indices on a parent element,
    2566             :       // and we should never need the indices on such a node
    2567     2119604 :       if (dof_offset < 0)
    2568             :         {
    2569           0 :           libmesh_assert(!elem.active());
    2570           0 :           di.resize(di.size() + nc, DofObject::invalid_id);
    2571             :         }
    2572             :       else
    2573     6291726 :         for (unsigned int i = dof_offset; i != n_comp; ++i)
    2574             :           {
    2575             :             const dof_id_type d =
    2576     4172122 :               obj.dof_number(sys_num, vg, vig, i, n_comp);
    2577      343571 :             libmesh_assert_not_equal_to (d, DofObject::invalid_id);
    2578     4172122 :             di.push_back(d);
    2579             :           }
    2580             :     }
    2581             :   // If this is a vertex or an element without extra hanging
    2582             :   // dofs, our dofs come in forward order coming from the
    2583             :   // beginning.  But we still might not have all those dofs, in cases
    2584             :   // where a subdomain-restricted variable just had its subdomain
    2585             :   // expanded.
    2586             :   else
    2587             :     {
    2588             :       const unsigned int good_nc =
    2589     7750971 :         std::min(static_cast<unsigned int>(n_comp), nc);
    2590    15384764 :       for (unsigned int i=0; i != good_nc; ++i)
    2591             :         {
    2592             :           const dof_id_type d =
    2593     7652804 :             obj.dof_number(sys_num, vg, vig, i, n_comp);
    2594      747747 :           libmesh_assert_not_equal_to (d, DofObject::invalid_id);
    2595     7652804 :           di.push_back(d);
    2596             :         }
    2597     7731960 :       for (unsigned int i=good_nc; i != nc; ++i)
    2598           0 :         di.push_back(DofObject::invalid_id);
    2599             :     }
    2600     9851564 : }
    2601             : 
    2602             : void
    2603   172237849 : DofMap::_dof_indices(const Elem & elem,
    2604             :                      int p_level,
    2605             :                      std::vector<dof_id_type> & di,
    2606             :                      const unsigned int vg,
    2607             :                      const unsigned int vig,
    2608             :                      const Node * const * nodes,
    2609             :                      unsigned int n_nodes,
    2610             :                      const unsigned int v
    2611             : #ifdef DEBUG
    2612             :                      ,
    2613             :                      std::size_t & tot_size
    2614             : #endif
    2615             : ) const
    2616             : {
    2617   172237849 :   _dof_indices(elem,
    2618             :                p_level,
    2619             :                di,
    2620             :                vg,
    2621             :                vig,
    2622             :                nodes,
    2623             :                n_nodes,
    2624             :                v,
    2625             : #ifdef DEBUG
    2626             :                tot_size,
    2627             : #endif
    2628             :                [](const Elem &,
    2629             :                   unsigned int,
    2630             :                   unsigned int,
    2631             :                   std::vector<dof_id_type> & functor_di,
    2632   790107515 :                   const dof_id_type dof) { functor_di.push_back(dof); });
    2633   172237849 : }
    2634             : 
    2635     2025571 : void DofMap::SCALAR_dof_indices (std::vector<dof_id_type> & di,
    2636             :                                  const unsigned int vn,
    2637             : #ifdef LIBMESH_ENABLE_AMR
    2638             :                                  const bool old_dofs
    2639             : #else
    2640             :                                  const bool
    2641             : #endif
    2642             :                                  ) const
    2643             : {
    2644             :   // dof_indices() is a relatively light-weight function; the cost of
    2645             :   // the logging code itself is roughly on par with the time required
    2646             :   // to call dof_indices().
    2647             :   // LOG_SCOPE("SCALAR_dof_indices()", "DofMap");
    2648             : 
    2649      177429 :   libmesh_assert(this->variable(vn).type().family == SCALAR);
    2650             : 
    2651             : #ifdef LIBMESH_ENABLE_AMR
    2652             :   // If we're asking for old dofs then we'd better have some
    2653      177429 :   if (old_dofs)
    2654          66 :     libmesh_assert_greater_equal(n_old_dofs(), n_SCALAR_dofs());
    2655             : 
    2656     2203000 :   dof_id_type my_idx = old_dofs ?
    2657     2025571 :     this->_first_old_scalar_df[vn] : this->_first_scalar_df[vn];
    2658             : #else
    2659             :   dof_id_type my_idx = this->_first_scalar_df[vn];
    2660             : #endif
    2661             : 
    2662      177429 :   libmesh_assert_not_equal_to(my_idx, DofObject::invalid_id);
    2663             : 
    2664             :   // The number of SCALAR dofs comes from the variable order
    2665     2025571 :   const int n_dofs_vn = this->variable(vn).type().order.get_order();
    2666             : 
    2667     2025571 :   di.resize(n_dofs_vn);
    2668     4051142 :   for (int i = 0; i != n_dofs_vn; ++i)
    2669     2203000 :     di[i] = my_idx++;
    2670     2025571 : }
    2671             : 
    2672             : 
    2673             : 
    2674     2327715 : bool DofMap::semilocal_index (dof_id_type dof_index) const
    2675             : {
    2676             :   // If it's not in the local indices
    2677     2327715 :   if (!this->local_index(dof_index))
    2678             :     {
    2679             :       // and if it's not in the ghost indices, then we're not
    2680             :       // semilocal
    2681     2124675 :       if (!std::binary_search(_send_list.begin(), _send_list.end(), dof_index))
    2682        4772 :         return false;
    2683             :     }
    2684             : 
    2685      168178 :   return true;
    2686             : }
    2687             : 
    2688             : 
    2689             : 
    2690      175000 : bool DofMap::all_semilocal_indices (const std::vector<dof_id_type> & dof_indices_in) const
    2691             : {
    2692             :   // We're all semilocal unless we find a counterexample
    2693     2427515 :   for (const auto & di : dof_indices_in)
    2694     2327715 :     if (!this->semilocal_index(di))
    2695        2386 :       return false;
    2696             : 
    2697        7870 :   return true;
    2698             : }
    2699             : 
    2700             : 
    2701             : 
    2702             : template <typename DofObjectSubclass>
    2703      256085 : bool DofMap::is_evaluable(const DofObjectSubclass & obj,
    2704             :                           unsigned int var_num) const
    2705             : {
    2706             :   // Everything is evaluable on a local object
    2707      268346 :   if (obj.processor_id() == this->processor_id())
    2708       12452 :     return true;
    2709             : 
    2710       20464 :   std::vector<dof_id_type> di;
    2711             : 
    2712      174148 :   if (var_num == libMesh::invalid_uint)
    2713       16710 :     this->dof_indices(&obj, di);
    2714             :   else
    2715      157438 :     this->dof_indices(&obj, di, var_num);
    2716             : 
    2717      174148 :   return this->all_semilocal_indices(di);
    2718             : }
    2719             : 
    2720             : 
    2721             : 
    2722             : #ifdef LIBMESH_ENABLE_AMR
    2723             : 
    2724    11628974 : void DofMap::old_dof_indices (const Elem * const elem,
    2725             :                               std::vector<dof_id_type> & di,
    2726             :                               const unsigned int vn) const
    2727             : {
    2728             :   // dof_indices() is a relatively light-weight function; the cost of
    2729             :   // the logging code itself is roughly on par with the time required
    2730             :   // to call dof_indices().
    2731             :   // LOG_SCOPE("old_dof_indices()", "DofMap");
    2732             : 
    2733     1120950 :   libmesh_assert(elem);
    2734             : 
    2735    11628974 :   const ElemType type              = elem->type();
    2736     2243412 :   const unsigned int sys_num       = this->sys_number();
    2737     1120950 :   const unsigned int n_var_groups  = this->n_variable_groups();
    2738             : #ifdef LIBMESH_ENABLE_INFINITE_ELEMENTS
    2739     2643833 :   const bool is_inf                = elem->infinite();
    2740             : #endif
    2741             : 
    2742             :   // If we have dof indices stored on the elem, and there's no chance
    2743             :   // that we only have those indices because we were just p refined,
    2744             :   // then we should have old dof indices too.
    2745     1120950 :   libmesh_assert(!elem->has_dofs(sys_num) ||
    2746             :                  elem->p_refinement_flag() == Elem::JUST_REFINED ||
    2747             :                  elem->get_old_dof_object());
    2748             : 
    2749             :   // Clear the DOF indices vector.
    2750     1120950 :   di.clear();
    2751             : 
    2752             :   // Determine the nodes contributing to element elem
    2753     2241900 :   std::vector<const Node *> elem_nodes;
    2754             :   const Node * const * nodes_ptr;
    2755             :   unsigned int n_nodes;
    2756    11628974 :   if (elem->type() == TRI3SUBDIVISION)
    2757             :     {
    2758             :       // Subdivision surface FE require the 1-ring around elem
    2759           0 :       const Tri3Subdivision * sd_elem = static_cast<const Tri3Subdivision *>(elem);
    2760           0 :       MeshTools::Subdivision::find_one_ring(sd_elem, elem_nodes);
    2761           0 :       nodes_ptr = elem_nodes.data();
    2762           0 :       n_nodes = cast_int<unsigned int>(elem_nodes.size());
    2763             :     }
    2764             :   else
    2765             :     {
    2766             :       // All other FE use only the nodes of elem itself
    2767     2243412 :       nodes_ptr = elem->get_nodes();
    2768    11628974 :       n_nodes = elem->n_nodes();
    2769             :     }
    2770             : 
    2771             :   // Get the dof numbers
    2772    23836143 :   for (unsigned int vg=0; vg<n_var_groups; vg++)
    2773             :     {
    2774     1168826 :       const VariableGroup & var = this->variable_group(vg);
    2775     1168826 :       const unsigned int vars_in_group = var.n_variables();
    2776             : 
    2777    24734256 :       for (unsigned int vig=0; vig<vars_in_group; vig++)
    2778             :         {
    2779     2391482 :           const unsigned int v = var.number(vig);
    2780    12527087 :           if ((vn == v) || (vn == libMesh::invalid_uint))
    2781             :             {
    2782    11846817 :               if (var.type().family == SCALAR &&
    2783         672 :                   (!elem ||
    2784         736 :                    var.active_on_subdomain(elem->subdomain_id())))
    2785             :                 {
    2786             :                   // We asked for this variable, so add it to the vector.
    2787          64 :                   std::vector<dof_id_type> di_new;
    2788         736 :                   this->SCALAR_dof_indices(di_new,v,true);
    2789         736 :                   di.insert( di.end(), di_new.begin(), di_new.end());
    2790             :                 }
    2791             :               else
    2792    11846017 :                 if (var.active_on_subdomain(elem->subdomain_id()))
    2793             :                   { // Do this for all the variables if one was not specified
    2794             :                     // or just for the specified variable
    2795             : 
    2796    11800483 :                     FEType fe_type = var.type();
    2797    11800483 :                     const bool add_p_level = fe_type.p_refinement;
    2798             : 
    2799             :                     // Increase the polynomial order on p refined elements,
    2800             :                     // but make sure you get the right polynomial order for
    2801             :                     // the OLD degrees of freedom
    2802     1133716 :                     int p_adjustment = 0;
    2803    12935783 :                     if (elem->p_refinement_flag() == Elem::JUST_REFINED)
    2804             :                       {
    2805        2660 :                         libmesh_assert_greater (elem->p_level(), 0);
    2806        2660 :                         p_adjustment = -1;
    2807             :                       }
    2808    11769180 :                     else if (elem->p_refinement_flag() == Elem::JUST_COARSENED)
    2809             :                       {
    2810          53 :                         p_adjustment = 1;
    2811             :                       }
    2812    11800483 :                     p_adjustment *= add_p_level;
    2813             : 
    2814             :                     // Compute the net amount of "extra" order, including Elem::p_level()
    2815    11800483 :                     int extra_order = int(add_p_level*elem->p_level()) + p_adjustment;
    2816             : 
    2817             :                     const bool extra_hanging_dofs =
    2818    11800483 :                       FEInterface::extra_hanging_dofs(fe_type);
    2819             : 
    2820             :                     const FEInterface::n_dofs_at_node_ptr ndan =
    2821    11800483 :                       FEInterface::n_dofs_at_node_function(fe_type, elem);
    2822             : 
    2823             :                     // Get the node-based DOF numbers
    2824    96774735 :                     for (unsigned int n=0; n<n_nodes; n++)
    2825             :                       {
    2826    84974252 :                         const Node * node = nodes_ptr[n];
    2827     7761522 :                         const DofObject & old_dof_obj = node->get_old_dof_object_ref();
    2828             : 
    2829             :                         // There is a potential problem with h refinement.  Imagine a
    2830             :                         // quad9 that has a linear FE on it.  Then, on the hanging side,
    2831             :                         // it can falsely identify a DOF at the mid-edge node. This is why
    2832             :                         // we call FEInterface instead of node->n_comp() directly.
    2833             :                         const unsigned int nc =
    2834             : #ifdef LIBMESH_ENABLE_INFINITE_ELEMENTS
    2835    19290632 :                           is_inf ?
    2836       29600 :                           FEInterface::n_dofs_at_node(var.type(), extra_order, elem, n) :
    2837             : #endif
    2838    92730124 :                           ndan (type, var.type().order + extra_order, n);
    2839             : 
    2840    84974252 :                         const int n_comp = old_dof_obj.n_comp_group(sys_num,vg);
    2841             : 
    2842             :                         // If this is a non-vertex on a hanging node with extra
    2843             :                         // degrees of freedom, we use the non-vertex dofs (which
    2844             :                         // come in reverse order starting from the end, to
    2845             :                         // simplify p refinement)
    2846    84974252 :                         if (extra_hanging_dofs && !elem->is_vertex(n))
    2847             :                           {
    2848     8158973 :                             const int dof_offset = n_comp - nc;
    2849             : 
    2850             :                             // We should never have fewer dofs than necessary on a
    2851             :                             // node unless we're getting indices on a parent element
    2852             :                             // or a just-coarsened element
    2853     8158973 :                             if (dof_offset < 0)
    2854             :                               {
    2855           0 :                                 libmesh_assert(!elem->active() || elem->refinement_flag() ==
    2856             :                                                Elem::JUST_COARSENED);
    2857           0 :                                 di.resize(di.size() + nc, DofObject::invalid_id);
    2858             :                               }
    2859             :                             else
    2860    22105809 :                               for (int i=n_comp-1; i>=dof_offset; i--)
    2861             :                                 {
    2862             :                                   const dof_id_type d =
    2863    13946836 :                                     old_dof_obj.dof_number(sys_num, vg, vig, i, n_comp);
    2864             : 
    2865             :                                   // On a newly-expanded subdomain, we
    2866             :                                   // may have some DoFs that didn't
    2867             :                                   // exist in the old system, in which
    2868             :                                   // case we can't assert this:
    2869             :                                   // libmesh_assert_not_equal_to (d, DofObject::invalid_id);
    2870             : 
    2871    13946836 :                                   di.push_back(d);
    2872             :                                 }
    2873             :                           }
    2874             :                         // If this is a vertex or an element without extra hanging
    2875             :                         // dofs, our dofs come in forward order coming from the
    2876             :                         // beginning.  But we still might not have all
    2877             :                         // those dofs on the old_dof_obj, in cases
    2878             :                         // where a subdomain-restricted variable just
    2879             :                         // had its subdomain expanded.
    2880             :                         else
    2881             :                           {
    2882             :                             const unsigned int old_nc =
    2883    76886153 :                               std::min(static_cast<unsigned int>(n_comp), nc);
    2884   153692241 :                             for (unsigned int i=0; i != old_nc; ++i)
    2885             :                               {
    2886             :                                 const dof_id_type d =
    2887    76876962 :                                   old_dof_obj.dof_number(sys_num, vg, vig, i, n_comp);
    2888             : 
    2889     7061589 :                                 libmesh_assert_not_equal_to (d, DofObject::invalid_id);
    2890             : 
    2891    76876962 :                                 di.push_back(d);
    2892             :                               }
    2893    76815279 :                             for (unsigned int i=old_nc; i != nc; ++i)
    2894           0 :                               di.push_back(DofObject::invalid_id);
    2895             :                           }
    2896             :                       }
    2897             : 
    2898             :                     // If there are any element-based DOF numbers, get them
    2899             :                     const unsigned int nc =
    2900    11800483 :                       FEInterface::n_dofs_per_elem(fe_type, extra_order, elem);
    2901             : 
    2902    11800483 :                     if (nc != 0)
    2903             :                       {
    2904      160923 :                         const DofObject & old_dof_obj = elem->get_old_dof_object_ref();
    2905             : 
    2906             :                         const unsigned int n_comp =
    2907      160923 :                           old_dof_obj.n_comp_group(sys_num,vg);
    2908             : 
    2909     1835581 :                         if (old_dof_obj.n_systems() > sys_num &&
    2910             :                             nc <= n_comp)
    2911             :                           {
    2912             : 
    2913     6966919 :                             for (unsigned int i=0; i<nc; i++)
    2914             :                               {
    2915             :                                 const dof_id_type d =
    2916     5131338 :                                   old_dof_obj.dof_number(sys_num, vg, vig, i, n_comp);
    2917             : 
    2918     5131338 :                                 di.push_back(d);
    2919             :                               }
    2920             :                           }
    2921             :                         else
    2922             :                           {
    2923             :                             // We should never have fewer dofs than
    2924             :                             // necessary on an element unless we're
    2925             :                             // getting indices on a parent element, a
    2926             :                             // just-coarsened element ... or a
    2927             :                             // subdomain-restricted variable with a
    2928             :                             // just-expanded subdomain
    2929             :                             // libmesh_assert(!elem->active() || fe_type.family == LAGRANGE ||
    2930             :                             //                 elem->refinement_flag() == Elem::JUST_COARSENED);
    2931           0 :                             di.resize(di.size() + nc, DofObject::invalid_id);
    2932             :                           }
    2933             :                       }
    2934             :                   }
    2935             :             }
    2936             :         } // end loop over variables within group
    2937             :     } // end loop over variable groups
    2938    11628974 : }
    2939             : 
    2940             : #endif // LIBMESH_ENABLE_AMR
    2941             : 
    2942             : 
    2943             : #ifdef LIBMESH_ENABLE_CONSTRAINTS
    2944             : 
    2945     8883597 : void DofMap::find_connected_dofs (std::vector<dof_id_type> & elem_dofs) const
    2946             : {
    2947             :   typedef std::set<dof_id_type> RCSet;
    2948             : 
    2949             :   // First insert the DOFS we already depend on into the set.
    2950     9755725 :   RCSet dof_set (elem_dofs.begin(), elem_dofs.end());
    2951             : 
    2952      872128 :   bool done = true;
    2953             : 
    2954             :   // Next insert any dofs those might be constrained in terms
    2955             :   // of.  Note that in this case we may not be done:  Those may
    2956             :   // in turn depend on others.  So, we need to repeat this process
    2957             :   // in that case until the system depends only on unconstrained
    2958             :   // degrees of freedom.
    2959    59169545 :   for (const auto & dof : elem_dofs)
    2960    50285948 :     if (this->is_constrained_dof(dof))
    2961             :       {
    2962             :         // If the DOF is constrained
    2963             :         DofConstraints::const_iterator
    2964      631761 :           pos = _dof_constraints.find(dof);
    2965             : 
    2966      631761 :         libmesh_assert (pos != _dof_constraints.end());
    2967             : 
    2968      631761 :         const DofConstraintRow & constraint_row = pos->second;
    2969             : 
    2970             :         // adaptive p refinement currently gives us lots of empty constraint
    2971             :         // rows - we should optimize those DoFs away in the future.  [RHS]
    2972             :         //libmesh_assert (!constraint_row.empty());
    2973             : 
    2974             :         // Add the DOFs this dof is constrained in terms of.
    2975             :         // note that these dofs might also be constrained, so
    2976             :         // we will need to call this function recursively.
    2977    19961140 :         for (const auto & pr : constraint_row)
    2978    12158578 :           if (!dof_set.count (pr.first))
    2979             :             {
    2980     3294201 :               dof_set.insert (pr.first);
    2981      321645 :               done = false;
    2982             :             }
    2983             :       }
    2984             : 
    2985             : 
    2986             :   // If not done then we need to do more work
    2987             :   // (obviously :-) )!
    2988     8883597 :   if (!done)
    2989             :     {
    2990             :       // Fill the vector with the contents of the set
    2991      125654 :       elem_dofs.clear();
    2992     1037193 :       elem_dofs.insert (elem_dofs.end(),
    2993      376962 :                         dof_set.begin(), dof_set.end());
    2994             : 
    2995             : 
    2996             :       // May need to do this recursively.  It is possible
    2997             :       // that we just replaced a constrained DOF with another
    2998             :       // constrained DOF.
    2999     1162847 :       this->find_connected_dofs (elem_dofs);
    3000             : 
    3001             :     } // end if (!done)
    3002     8883597 : }
    3003             : 
    3004             : #endif // LIBMESH_ENABLE_CONSTRAINTS
    3005             : 
    3006             : 
    3007             : 
    3008           0 : void DofMap::print_info(std::ostream & os) const
    3009             : {
    3010           0 :   os << this->get_info();
    3011           0 : }
    3012             : 
    3013             : 
    3014             : 
    3015       18100 : std::string DofMap::get_info() const
    3016             : {
    3017       19104 :   std::ostringstream os;
    3018             : 
    3019             :   // If we didn't calculate the exact sparsity pattern, the threaded
    3020             :   // sparsity pattern assembly may have just given us an upper bound
    3021             :   // on sparsity.
    3022         502 :   const char * may_equal = " <= ";
    3023             : 
    3024             :   // If we calculated the exact sparsity pattern, then we can report
    3025             :   // exact bandwidth figures:
    3026       36414 :   for (const auto & mat : _matrices)
    3027       18314 :     if (mat->need_full_sparsity_pattern())
    3028           0 :       may_equal = " = ";
    3029             : 
    3030       18100 :   dof_id_type max_n_nz = 0, max_n_oz = 0;
    3031       18100 :   long double avg_n_nz = 0, avg_n_oz = 0;
    3032             : 
    3033       18100 :   if (_sp)
    3034             :     {
    3035     4742824 :       for (const auto & val : _sp->get_n_nz())
    3036             :         {
    3037     4726833 :           max_n_nz = std::max(max_n_nz, val);
    3038     4726833 :           avg_n_nz += val;
    3039             :         }
    3040             : 
    3041       15991 :       std::size_t n_nz_size = _sp->get_n_nz().size();
    3042             : 
    3043       15991 :       this->comm().max(max_n_nz);
    3044       15991 :       this->comm().sum(avg_n_nz);
    3045       15991 :       this->comm().sum(n_nz_size);
    3046             : 
    3047       15991 :       avg_n_nz /= std::max(n_nz_size,std::size_t(1));
    3048             : 
    3049     4742824 :       for (const auto & val : _sp->get_n_oz())
    3050             :         {
    3051     4726833 :           max_n_oz = std::max(max_n_oz, val);
    3052     4726833 :           avg_n_oz += val;
    3053             :         }
    3054             : 
    3055       15991 :       std::size_t n_oz_size = _sp->get_n_oz().size();
    3056             : 
    3057       15991 :       this->comm().max(max_n_oz);
    3058       15991 :       this->comm().sum(avg_n_oz);
    3059       15991 :       this->comm().sum(n_oz_size);
    3060             : 
    3061       15991 :       avg_n_oz /= std::max(n_oz_size,std::size_t(1));
    3062             :     }
    3063             : 
    3064             :   os << "    DofMap Sparsity\n      Average  On-Processor Bandwidth"
    3065       18602 :      << may_equal << avg_n_nz << '\n';
    3066             : 
    3067             :   os << "      Average Off-Processor Bandwidth"
    3068       18602 :      << may_equal << avg_n_oz << '\n';
    3069             : 
    3070             :   os << "      Maximum  On-Processor Bandwidth"
    3071       18602 :      << may_equal << max_n_nz << '\n';
    3072             : 
    3073             :   os << "      Maximum Off-Processor Bandwidth"
    3074       18100 :      << may_equal << max_n_oz << std::endl;
    3075             : 
    3076             : #ifdef LIBMESH_ENABLE_CONSTRAINTS
    3077             : 
    3078       18100 :   std::size_t n_constraints = 0, max_constraint_length = 0,
    3079       18100 :     n_rhss = 0;
    3080       18100 :   long double avg_constraint_length = 0.;
    3081             : 
    3082      657686 :   for (const auto & [constrained_dof, row] : _dof_constraints)
    3083             :     {
    3084             :       // Only count local constraints, then sum later
    3085      639586 :       if (!this->local_index(constrained_dof))
    3086      138334 :         continue;
    3087             : 
    3088      501252 :       std::size_t rowsize = row.size();
    3089             : 
    3090      501252 :       max_constraint_length = std::max(max_constraint_length,
    3091       42776 :                                        rowsize);
    3092      501252 :       avg_constraint_length += rowsize;
    3093      501252 :       n_constraints++;
    3094             : 
    3095       42776 :       if (_primal_constraint_values.count(constrained_dof))
    3096       44535 :         n_rhss++;
    3097             :     }
    3098             : 
    3099       18100 :   this->comm().sum(n_constraints);
    3100       18100 :   this->comm().sum(n_rhss);
    3101       18100 :   this->comm().sum(avg_constraint_length);
    3102       18100 :   this->comm().max(max_constraint_length);
    3103             : 
    3104       17598 :   os << "    DofMap Constraints\n      Number of DoF Constraints = "
    3105       18100 :      << n_constraints;
    3106       18100 :   if (n_rhss)
    3107             :     os << '\n'
    3108        4147 :        << "      Number of Heterogenous Constraints= " << n_rhss;
    3109       18100 :   if (n_constraints)
    3110             :     {
    3111       10193 :       avg_constraint_length /= n_constraints;
    3112             : 
    3113             :       os << '\n'
    3114       10483 :          << "      Average DoF Constraint Length= " << avg_constraint_length;
    3115             :     }
    3116             : 
    3117             : #ifdef LIBMESH_ENABLE_NODE_CONSTRAINTS
    3118        1004 :   std::size_t n_node_constraints = 0, max_node_constraint_length = 0,
    3119        1004 :     n_node_rhss = 0;
    3120        1004 :   long double avg_node_constraint_length = 0.;
    3121             : 
    3122       36228 :   for (const auto & [node, pr] : _node_constraints)
    3123             :     {
    3124             :       // Only count local constraints, then sum later
    3125       52836 :       if (node->processor_id() != this->processor_id())
    3126        6652 :         continue;
    3127             : 
    3128       14286 :       const NodeConstraintRow & row = pr.first;
    3129       28572 :       std::size_t rowsize = row.size();
    3130             : 
    3131       28572 :       max_node_constraint_length = std::max(max_node_constraint_length,
    3132       14286 :                                             rowsize);
    3133       28572 :       avg_node_constraint_length += rowsize;
    3134       28572 :       n_node_constraints++;
    3135             : 
    3136       14286 :       if (pr.second != Point(0))
    3137           0 :         n_node_rhss++;
    3138             :     }
    3139             : 
    3140        1004 :   this->comm().sum(n_node_constraints);
    3141        1004 :   this->comm().sum(n_node_rhss);
    3142        1004 :   this->comm().sum(avg_node_constraint_length);
    3143        1004 :   this->comm().max(max_node_constraint_length);
    3144             : 
    3145        1004 :   os << "\n      Number of Node Constraints = " << n_node_constraints;
    3146        1004 :   if (n_node_rhss)
    3147             :     os << '\n'
    3148           0 :        << "      Number of Heterogenous Node Constraints= " << n_node_rhss;
    3149        1004 :   if (n_node_constraints)
    3150             :     {
    3151         200 :       avg_node_constraint_length /= n_node_constraints;
    3152         100 :       os << "\n      Maximum Node Constraint Length= " << max_node_constraint_length
    3153             :          << '\n'
    3154         400 :          << "      Average Node Constraint Length= " << avg_node_constraint_length;
    3155             :     }
    3156             : #endif // LIBMESH_ENABLE_NODE_CONSTRAINTS
    3157             : 
    3158         502 :   os << std::endl;
    3159             : 
    3160             : #endif // LIBMESH_ENABLE_CONSTRAINTS
    3161             : 
    3162       18602 :   return os.str();
    3163       17096 : }
    3164             : 
    3165         560 : void DofMap::create_static_condensation(MeshBase & mesh, System & sys)
    3166             : {
    3167        1088 :   _sc = std::make_unique<StaticCondensationDofMap>(mesh, sys, *this);
    3168         560 : }
    3169             : 
    3170      404196 : void DofMap::reinit_static_condensation()
    3171             : {
    3172      404196 :   if (_sc)
    3173        4130 :     _sc->reinit();
    3174      404196 : }
    3175             : 
    3176      407452 : unsigned int DofMap::add_variable(System & sys,
    3177             :                                   std::string_view var,
    3178             :                                   const FEType & type,
    3179             :                                   const std::set<subdomain_id_type> * const active_subdomains)
    3180             : {
    3181       11516 :   parallel_object_only(); // Not strictly needed, but the only safe way to keep in sync
    3182             : 
    3183       11516 :   libmesh_assert(this->comm().verify(std::string(var)));
    3184       11516 :   libmesh_assert(this->comm().verify(type));
    3185       11516 :   libmesh_assert(this->comm().verify((active_subdomains == nullptr)));
    3186             : 
    3187       11516 :   if (active_subdomains)
    3188         148 :     libmesh_assert(this->comm().verify(active_subdomains->size()));
    3189             : 
    3190             :   // Make sure the variable isn't there already
    3191             :   // or if it is, that it's the type we want
    3192      490321 :   for (auto v : make_range(this->n_vars()))
    3193       83265 :     if (this->variable_name(v) == var)
    3194             :       {
    3195           0 :         if (this->variable_type(v) == type)
    3196             :           {
    3197             :             // Check whether the existing variable's active subdomains also matches
    3198             :             // the incoming variable's active subdomains. If they don't match, then
    3199             :             // either it is an error by the user or the user is trying to change the
    3200             :             // subdomain restriction after the variable has already been added, which
    3201             :             // is not supported.
    3202         396 :             const Variable & existing_var = this->variable(v);
    3203             : 
    3204             :             // Check whether active_subdomains is not provided/empty and the existing_var is
    3205             :             // implicitly_active()
    3206         396 :             bool check1 = (!active_subdomains || active_subdomains->empty()) &&
    3207           0 :                           existing_var.implicitly_active();
    3208             : 
    3209             :             // Check if the provided active_subdomains is equal to the existing_var's
    3210             :             // active_subdomains
    3211             :             bool check2 =
    3212         396 :                 (active_subdomains && (*active_subdomains == existing_var.active_subdomains()));
    3213             : 
    3214             :             // If either of these checks passed, then we already have this variable
    3215         396 :             if (check1 || check2)
    3216           0 :               return _variables[v].number();
    3217             :           }
    3218             : 
    3219           0 :         libmesh_error_msg("ERROR: incompatible variable "
    3220             :                           << var << " has already been added for this system!");
    3221             :       }
    3222             : 
    3223       11516 :   libmesh_assert(!sys.is_initialized());
    3224             : 
    3225      407056 :   if (this->n_variable_groups())
    3226             :     {
    3227             :       // Optimize for VariableGroups here - if the user is adding multiple
    3228             :       // variables of the same FEType and subdomain restriction, catch
    3229             :       // that here and add them as members of the same VariableGroup.
    3230             :       //
    3231             :       // start by setting this flag to whatever the user has requested
    3232             :       // and then consider the conditions which should negate it.
    3233        1976 :       bool should_be_in_vg = this->identify_variable_groups();
    3234             : 
    3235         988 :       VariableGroup & vg = _variable_groups.back();
    3236             : 
    3237             :       // get a pointer to their subdomain restriction, if any.
    3238             :       const std::set<subdomain_id_type> * const their_active_subdomains(
    3239       35231 :           vg.implicitly_active() ? nullptr : &vg.active_subdomains());
    3240             : 
    3241             :       // Different types?
    3242        2371 :       if (vg.type() != type)
    3243         338 :         should_be_in_vg = false;
    3244             : 
    3245             :       // they are restricted, we aren't?
    3246       35335 :       if (their_active_subdomains &&
    3247        3674 :           (!active_subdomains || (active_subdomains && active_subdomains->empty())))
    3248           0 :         should_be_in_vg = false;
    3249             : 
    3250             :       // they aren't restricted, we are?
    3251       35231 :       if (!their_active_subdomains && (active_subdomains && !active_subdomains->empty()))
    3252           2 :         should_be_in_vg = false;
    3253             : 
    3254       35231 :       if (their_active_subdomains && active_subdomains)
    3255             :         // restricted to different sets?
    3256        3674 :         if (*their_active_subdomains != *active_subdomains)
    3257          52 :           should_be_in_vg = false;
    3258             : 
    3259             :       // OK, after all that, append the variable to the vg if none of the conditions
    3260             :       // were violated
    3261       33443 :       if (should_be_in_vg)
    3262             :         {
    3263       22076 :           const unsigned int vn = this->n_vars();
    3264             : 
    3265       21460 :           std::string varstr(var);
    3266             : 
    3267       22076 :           _variable_numbers[varstr] = vn;
    3268       22076 :           vg.append(std::move(varstr));
    3269       44152 :           _variables.push_back(vg(vg.n_variables() - 1));
    3270       22076 :           const unsigned int vgn = _variable_groups.size() - 1;
    3271       22076 :           _variable_group_numbers.push_back(vgn);
    3272         616 :           _var_to_vg.emplace(vn, vgn);
    3273             : 
    3274        1232 :           return vn;
    3275             :         }
    3276             :     }
    3277             : 
    3278             :   // otherwise, fall back to adding a single variable group
    3279      384980 :   return this->add_variables(
    3280     1133140 :       sys, std::vector<std::string>(1, std::string(var)), type, active_subdomains);
    3281             : }
    3282             : 
    3283      385619 : unsigned int DofMap::add_variables(System & sys,
    3284             :                                    const std::vector<std::string> & vars,
    3285             :                                    const FEType & type,
    3286             :                                    const std::set<subdomain_id_type> * const active_subdomains)
    3287             : {
    3288       10918 :   parallel_object_only(); // Not strictly needed, but the only safe way to keep in sync
    3289             : 
    3290       10918 :   libmesh_assert(!sys.is_initialized());
    3291             : 
    3292       10918 :   libmesh_assert(this->comm().verify(vars.size()));
    3293       10918 :   libmesh_assert(this->comm().verify(type));
    3294       10918 :   libmesh_assert(this->comm().verify((active_subdomains == nullptr)));
    3295             : 
    3296       10918 :   if (active_subdomains)
    3297          96 :     libmesh_assert(this->comm().verify(active_subdomains->size()));
    3298             : 
    3299             :   // Make sure the variable isn't there already
    3300             :   // or if it is, that it's the type we want
    3301     7871735 :   for (auto ovar : vars)
    3302             :     {
    3303      210932 :       libmesh_assert(this->comm().verify(ovar));
    3304             : 
    3305     7511369 :       for (auto v : make_range(this->n_vars()))
    3306       24537 :         if (this->variable_name(v) == ovar)
    3307             :           {
    3308           0 :             if (this->variable_type(v) == type)
    3309           0 :               return _variables[v].number();
    3310             : 
    3311           0 :             libmesh_error_msg("ERROR: incompatible variable "
    3312             :                               << ovar << " has already been added for this system!");
    3313             :           }
    3314             :     }
    3315             : 
    3316      385619 :   if (this->n_variable_groups())
    3317             :     {
    3318             :       // Optimize for VariableGroups here - if the user is adding multiple
    3319             :       // variables of the same FEType and subdomain restriction, catch
    3320             :       // that here and add them as members of the same VariableGroup.
    3321             :       //
    3322             :       // start by setting this flag to whatever the user has requested
    3323             :       // and then consider the conditions which should negate it.
    3324         744 :       bool should_be_in_vg = this->identify_variable_groups();
    3325             : 
    3326         372 :       VariableGroup & vg = _variable_groups.back();
    3327             : 
    3328             :       // get a pointer to their subdomain restriction, if any.
    3329             :       const std::set<subdomain_id_type> * const their_active_subdomains(
    3330       13155 :           vg.implicitly_active() ? nullptr : &vg.active_subdomains());
    3331             : 
    3332             :       // Different types?
    3333         879 :       if (vg.type() != type)
    3334         338 :         should_be_in_vg = false;
    3335             : 
    3336             :       // they are restricted, we aren't?
    3337       13207 :       if (their_active_subdomains &&
    3338        1840 :           (!active_subdomains || (active_subdomains && active_subdomains->empty())))
    3339           0 :         should_be_in_vg = false;
    3340             : 
    3341             :       // they aren't restricted, we are?
    3342       13155 :       if (!their_active_subdomains && (active_subdomains && !active_subdomains->empty()))
    3343           2 :         should_be_in_vg = false;
    3344             : 
    3345       13155 :       if (their_active_subdomains && active_subdomains)
    3346             :         // restricted to different sets?
    3347        1840 :         if (*their_active_subdomains != *active_subdomains)
    3348          52 :           should_be_in_vg = false;
    3349             : 
    3350             :       // If after all that none of the conditions were violated,
    3351             :       // append the variables to the vg and we're done
    3352       11367 :       if (should_be_in_vg)
    3353             :         {
    3354           0 :           unsigned int vn = this->n_vars();
    3355           0 :           const unsigned int vgn = _variable_groups.size() - 1;
    3356             : 
    3357           0 :           for (auto ovar : vars)
    3358             :             {
    3359           0 :               vn = this->n_vars();
    3360             : 
    3361           0 :               vg.append(ovar);
    3362             : 
    3363           0 :               _variables.push_back(vg(vg.n_variables() - 1));
    3364           0 :               _variable_numbers[ovar] = vn;
    3365           0 :               _variable_group_numbers.push_back(vgn);
    3366           0 :               _var_to_vg.emplace(vn, vgn);
    3367             :             }
    3368           0 :           return vn;
    3369             :         }
    3370             :     }
    3371             : 
    3372       10918 :   const unsigned int curr_n_vars = this->n_vars();
    3373             : 
    3374      385619 :   const unsigned int next_first_component = this->n_components(sys.get_mesh());
    3375             : 
    3376             :   // We weren't able to add to an existing variable group, so
    3377             :   // add a new variable group to the list
    3378      385619 :   _variable_groups.push_back(
    3379             :       (active_subdomains == nullptr)
    3380      771238 :           ? VariableGroup(&sys, vars, curr_n_vars, next_first_component, type)
    3381             :           : VariableGroup(&sys, vars, curr_n_vars, next_first_component, type, *active_subdomains));
    3382             : 
    3383       10918 :   const VariableGroup & vg(_variable_groups.back());
    3384      385619 :   const unsigned int vgn = _variable_groups.size() - 1;
    3385             : 
    3386             :   // Add each component of the group individually
    3387     7871735 :   for (auto v : make_range(vars.size()))
    3388             :     {
    3389     7486116 :       const unsigned int vn = curr_n_vars + v;
    3390    14761300 :       _variables.push_back(vg(v));
    3391     7697048 :       _variable_numbers[vars[v]] = vn;
    3392     7486116 :       _variable_group_numbers.push_back(vgn);
    3393      210932 :       _var_to_vg.emplace(vn, vgn);
    3394             :     }
    3395             : 
    3396       10918 :   libmesh_assert_equal_to((curr_n_vars + vars.size()), this->n_vars());
    3397             : 
    3398             :   // BSK - Defer this now to System::init_data() so we can detect
    3399             :   // VariableGroups 12/28/2012
    3400             :   // // Add the variable group to the _dof_map
    3401             :   // _dof_map->add_variable_group (vg);
    3402             : 
    3403             :   // Return the number of the new variable
    3404      407455 :   return cast_int<unsigned int>(curr_n_vars + vars.size() - 1);
    3405             : }
    3406             : 
    3407         568 : unsigned int DofMap::add_variable_array (System & sys,
    3408             :                                          const std::vector<std::string> & vars,
    3409             :                                          const FEType & type,
    3410             :                                          const std::set<subdomain_id_type> * const active_subdomains)
    3411             : {
    3412          32 :   const unsigned int count = cast_int<unsigned int>(vars.size());
    3413         568 :   const unsigned int last_var = this->add_variables(sys, vars, type, active_subdomains);
    3414         568 :   const unsigned int first_var = last_var + 1 - count;
    3415         568 :   _array_variables.push_back({first_var, first_var + count});
    3416         568 :   return last_var;
    3417             : }
    3418             : 
    3419         493 : void DofMap::get_all_variable_numbers(std::vector<unsigned int> & all_variable_numbers) const
    3420             : {
    3421         493 :   all_variable_numbers.resize(n_vars());
    3422             : 
    3423          14 :   unsigned int count = 0;
    3424        1266 :   for (auto vn : _variable_numbers)
    3425         795 :     all_variable_numbers[count++] = vn.second;
    3426         493 : }
    3427             : 
    3428             : template LIBMESH_EXPORT bool DofMap::is_evaluable<Elem>(const Elem &, unsigned int) const;
    3429             : template LIBMESH_EXPORT bool DofMap::is_evaluable<Node>(const Node &, unsigned int) const;
    3430             : 
    3431             : } // namespace libMesh

Generated by: LCOV version 1.14