LCOV - code coverage report
Current view: top level - include/base - dof_map.h (source / functions) Hit Total Coverage
Test: libMesh/libmesh: #4542 (771e6d) with base faa874 Lines: 220 235 93.6 %
Date: 2026-09-10 20:05:06 Functions: 67 76 88.2 %
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             : #ifndef LIBMESH_DOF_MAP_H
      21             : #define LIBMESH_DOF_MAP_H
      22             : 
      23             : // Local Includes
      24             : #include "libmesh/libmesh_common.h"
      25             : #include "libmesh/reference_counted_object.h"
      26             : #include "libmesh/libmesh.h" // libMesh::invalid_uint
      27             : #include "libmesh/variable.h"
      28             : #include "libmesh/threads.h"
      29             : #include "libmesh/threads_allocators.h"
      30             : #include "libmesh/elem_range.h"
      31             : #include "libmesh/ghosting_functor.h"
      32             : #include "libmesh/sparsity_pattern.h"
      33             : #include "libmesh/parallel_object.h"
      34             : #include "libmesh/point.h"
      35             : #include "libmesh/utility.h"
      36             : #include "libmesh/elem.h"
      37             : #include "libmesh/fe_interface.h"
      38             : #include "libmesh/libmesh_logging.h"
      39             : #include "libmesh/enum_elem_type.h"
      40             : #include "libmesh/mesh_subdivision_support.h"
      41             : #include "libmesh/dof_map_base.h"
      42             : 
      43             : // TIMPI includes
      44             : #include "timpi/parallel_implementation.h"
      45             : #include "timpi/parallel_sync.h"
      46             : 
      47             : // C++ Includes
      48             : #include <algorithm>
      49             : #include <cstddef>
      50             : #include <iterator>
      51             : #include <map>
      52             : #include <string>
      53             : #include <vector>
      54             : #include <memory>
      55             : 
      56             : namespace libMesh
      57             : {
      58             : 
      59             : // Forward Declarations
      60             : class CouplingMatrix;
      61             : class DefaultCoupling;
      62             : class DirichletBoundary;
      63             : class DirichletBoundaries;
      64             : class DofMap;
      65             : class DofObject;
      66             : class FEType;
      67             : class MeshBase;
      68             : class PeriodicBoundaryBase;
      69             : class PeriodicBoundaries;
      70             : class System;
      71             : class NonlinearImplicitSystem;
      72             : class StaticCondensationDofMap;
      73             : template <typename T> class DenseVectorBase;
      74             : template <typename T> class DenseVector;
      75             : template <typename T> class DenseMatrix;
      76             : template <typename T> class SparseMatrix;
      77             : template <typename T> class NumericVector;
      78             : enum Order : int;
      79             : 
      80             : 
      81             : 
      82             : // ------------------------------------------------------------
      83             : // Do we need constraints for anything?
      84             : 
      85             : #if defined(LIBMESH_ENABLE_AMR) ||              \
      86             :   defined(LIBMESH_ENABLE_PERIODIC) ||           \
      87             :   defined(LIBMESH_ENABLE_DIRICHLET)
      88             : #  define LIBMESH_ENABLE_CONSTRAINTS 1
      89             : #endif
      90             : 
      91             : // ------------------------------------------------------------
      92             : // AMR constraint matrix types
      93             : 
      94             : #ifdef LIBMESH_ENABLE_CONSTRAINTS
      95             : /**
      96             :  * A row of the Dof constraint matrix.
      97             :  */
      98             : typedef std::map<dof_id_type, Real,
      99             :                  std::less<dof_id_type>,
     100             :                  Threads::scalable_allocator<std::pair<const dof_id_type, Real>>> DofConstraintRow;
     101             : 
     102             : /**
     103             :  * The constraint matrix storage format.
     104             :  * We're using a class instead of a typedef to allow forward
     105             :  * declarations and future flexibility.  Don't delete this from
     106             :  * a pointer-to-std-map; the destructor isn't virtual!
     107             :  */
     108      255166 : class DofConstraints : public std::map<dof_id_type,
     109             :                                        DofConstraintRow,
     110             :                                        std::less<dof_id_type>,
     111             :                                        Threads::scalable_allocator<std::pair<const dof_id_type, DofConstraintRow>>>
     112             : {
     113             : };
     114             : 
     115             : /**
     116             :  * Storage for DofConstraint right hand sides for a particular
     117             :  * problem.  Each dof id with a non-zero constraint offset
     118             :  * stores it in such a structure.
     119             :  */
     120      255030 : class DofConstraintValueMap :
     121             :     public std::map<dof_id_type, Number,
     122             :                     std::less<dof_id_type>,
     123             :                     Threads::scalable_allocator<std::pair<const dof_id_type, Number>>>
     124             : {
     125             : };
     126             : 
     127             : /**
     128             :  * Storage for DofConstraint right hand sides for all adjoint
     129             :  * problems.
     130             :  */
     131      255030 : class AdjointDofConstraintValues :
     132             :     public std::map<unsigned int, DofConstraintValueMap,
     133             :                     std::less<unsigned int>,
     134             :                     Threads::scalable_allocator
     135             :                     <std::pair<const unsigned int, DofConstraintValueMap>>>
     136             : {
     137             : };
     138             : 
     139             : #ifdef LIBMESH_ENABLE_NODE_CONSTRAINTS
     140             : /**
     141             :  * A row of the Node constraint mapping.  Currently this just
     142             :  * stores the topology of the constrained Nodes, but for forward
     143             :  * compatibility we also include coefficients, so we could add
     144             :  * Lagrange-positioned-node constraints later.
     145             :  */
     146             : typedef std::map<const Node *, Real,
     147             :                  std::less<const Node *>,
     148             :                  Threads::scalable_allocator<std::pair<const Node * const, Real>>> NodeConstraintRow;
     149             : 
     150             : /**
     151             :  * The Node constraint storage format.
     152             :  * We're using a class instead of a typedef to allow forward
     153             :  * declarations and future flexibility.  Don't delete this from
     154             :  * a pointer-to-std-map; the destructor isn't virtual!
     155             :  */
     156        7448 : class NodeConstraints : public std::map<const Node *,
     157             :                                         std::pair<NodeConstraintRow,Point>,
     158             :                                         std::less<const Node *>,
     159             :                                         Threads::scalable_allocator<std::pair<const Node * const, std::pair<NodeConstraintRow,Point>>>>
     160             : {
     161             : };
     162             : #endif // LIBMESH_ENABLE_NODE_CONSTRAINTS
     163             : 
     164             : #endif // LIBMESH_ENABLE_CONSTRAINTS
     165             : 
     166             : 
     167             : 
     168             : /**
     169             :  * This class handles the numbering of degrees of freedom on a mesh.
     170             :  * For systems of equations the class supports a fixed number of variables.
     171             :  * The degrees of freedom are numbered such that sequential, contiguous blocks
     172             :  * belong to distinct processors.  This is so that the resulting data
     173             :  * structures will work well with parallel linear algebra packages.
     174             :  *
     175             :  * \author Benjamin S. Kirk
     176             :  * \date 2002-2007
     177             :  * \brief Manages the degrees of freedom (DOFs) in a simulation.
     178             :  */
     179             : class DofMap : public DofMapBase,
     180             :                public ReferenceCountedObject<DofMap>
     181             : {
     182             : public:
     183             : 
     184             :   /**
     185             :    * Constructor.  Requires the number of the system for which we
     186             :    * will be numbering degrees of freedom & the parent object
     187             :    * we are contained in, which defines our communication space.
     188             :    */
     189             :   explicit
     190             :   DofMap(const unsigned int sys_number,
     191             :          MeshBase & mesh);
     192             : 
     193             :   /**
     194             :    * Destructor.
     195             :    */
     196             :   ~DofMap();
     197             : 
     198             :   /**
     199             :    * Backwards compatibility for prior AugmentSparsityPattern users.
     200             :    */
     201             :   class AugmentSparsityPattern : public SparsityPattern::AugmentSparsityPattern
     202             :   {};
     203             : 
     204             :   /**
     205             :    * Abstract base class to be used to add user-defined parallel
     206             :    * degree of freedom couplings.
     207             :    */
     208             :   class AugmentSendList
     209             :   {
     210             :   public:
     211             :     virtual ~AugmentSendList () = default;
     212             : 
     213             :     /**
     214             :      * User-defined function to augment the send list.
     215             :      */
     216             :     virtual void augment_send_list (std::vector<dof_id_type> & send_list) = 0;
     217             :   };
     218             : 
     219             :   /**
     220             :    * Additional matrices may be attached to this \p DofMap.
     221             :    * They are initialized to the same sparsity structure as
     222             :    * the major matrix.
     223             :    */
     224             :   void attach_matrix (SparseMatrix<Number> & matrix);
     225             : 
     226             :   /**
     227             :    * Additional matrices may be be temporarily initialized by this \p
     228             :    * DofMap.
     229             :    * They are initialized to the same sparsity structure as
     230             :    * the major matrix.
     231             :    */
     232             :   void update_sparsity_pattern(SparseMatrix<Number> & matrix) const;
     233             : 
     234             :   /**
     235             :    * Matrices should not be attached more than once.  We can test for
     236             :    * an already-attached matrix if necessary using \p is_attached
     237             :    */
     238             :   bool is_attached (SparseMatrix<Number> & matrix);
     239             : 
     240             :   /**
     241             :    * Distribute dofs on the current mesh.  Also builds the send list for
     242             :    * processor \p proc_id, which defaults to 0 for ease of use in serial
     243             :    * applications.
     244             :    * \returns The total number of DOFs for the System, summed across all procs.
     245             :    */
     246             :   std::size_t distribute_dofs (MeshBase &);
     247             : 
     248             :   /**
     249             :    * Computes the sparsity pattern for the matrices corresponding to
     250             :    * \p proc_id and sends that data to Linear Algebra packages for
     251             :    * preallocation of sparse matrices.
     252             :    */
     253             :   void compute_sparsity (const MeshBase &);
     254             : 
     255             :   /**
     256             :    * Returns true iff a sparsity pattern has already been computed.
     257             :    */
     258             :   bool computed_sparsity_already () const;
     259             : 
     260             :   /**
     261             :    * Sets the current policy for constructing sparsity patterns: if
     262             :    * \p use_constraints is true (for robustness), we explicitly
     263             :    * account for sparsity entries created by constraint matrix pre-
     264             :    * and post- application.  If \p use_constraints is false (for
     265             :    * speed), we calculate only the sparsity pattern of an
     266             :    * unconstrained matrix.  This is false by default, because in
     267             :    * nearly all applications our constraints do not increase the
     268             :    * number of non-zeros required in a sparse matrix.
     269             :    */
     270             :   void set_constrained_sparsity_construction(bool use_constraints);
     271             : 
     272             :   /**
     273             :    * Sets _need_full_sparsity_pattern to true regardless of the
     274             :    * requirements by matrices
     275             :    */
     276             :   void full_sparsity_pattern_needed();
     277             : 
     278             :   /**
     279             :    * Sets _need_ghost_constraints to true regardless of the requirements
     280             :    * by static condensation
     281             :    */
     282             :   void ghost_constraints_needed();
     283             : 
     284             :   /**
     285             :    * Returns true iff the current policy when constructing sparsity
     286             :    * patterns is to explicitly account for sparsity entries created by
     287             :    * constraint matrix pre- and post- application.
     288             :    */
     289             :   bool constrained_sparsity_construction();
     290             : 
     291             :   /**
     292             :    * Clears the sparsity pattern
     293             :    */
     294             :   void clear_sparsity();
     295             : 
     296             :   /**
     297             :    * Remove any default ghosting functor(s).  User-added ghosting
     298             :    * functors will be unaffected.
     299             :    *
     300             :    * Unless user-added equivalent ghosting functors exist, removing
     301             :    * the default coupling functor is only safe for explicit solves,
     302             :    * and removing the default algebraic ghosting functor is only safe
     303             :    * for codes where no evaluations on neighbor cells (e.g. no jump
     304             :    * error estimators) are done.
     305             :    *
     306             :    * Defaults can be restored manually via add_default_ghosting(), or
     307             :    * automatically if clear() returns the DofMap to a default state.
     308             :    */
     309             :   void remove_default_ghosting();
     310             : 
     311             :   /**
     312             :    * Add the default functor(s) for coupling and algebraic ghosting.
     313             :    * User-added ghosting functors will be unaffected.
     314             :    */
     315             :   void add_default_ghosting();
     316             : 
     317             :   /**
     318             :    * Iterator type for coupling and algebraic ghosting functor ranges.
     319             :    * This has changed in the past and may change again; code should
     320             :    * use auto or the type here.
     321             :    */
     322             :   typedef std::vector<GhostingFunctor *>::const_iterator GhostingFunctorIterator;
     323             : 
     324             :   /**
     325             :    * Adds a functor which can specify coupling requirements for
     326             :    * creation of sparse matrices.
     327             :    * Degree of freedom pairs which match the elements and variables
     328             :    * returned by these functors will be added to the sparsity pattern,
     329             :    * and the degrees of freedom which live on other processors will be
     330             :    * added to the send_list for use on ghosted vectors, and the
     331             :    * elements which live on other processors will be ghosted on a
     332             :    * distributed mesh.
     333             :    *
     334             :    * GhostingFunctor memory must be managed by the code which calls
     335             :    * this function; the GhostingFunctor lifetime is expected to extend
     336             :    * until either the functor is removed or the DofMap is destructed.
     337             :    *
     338             :    * When \p to_mesh is true, the \p coupling_functor is also added to
     339             :    * our associated mesh, to ensure that coupled elements do not get
     340             :    * lost during mesh distribution.  (if coupled elements were
     341             :    * *already* lost there's no getting them back after the fact,
     342             :    * sorry)
     343             :    *
     344             :    * If \p to_mesh is false, no change to mesh ghosting is made;
     345             :    * the Mesh must already have ghosting functor(s) specifying a
     346             :    * superset of \p coupling_functor or this is a horrible bug.
     347             :    */
     348             :   void add_coupling_functor(GhostingFunctor & coupling_functor,
     349             :                             bool to_mesh = true);
     350             : 
     351             :   /**
     352             :    * Adds a functor which can specify coupling requirements for
     353             :    * creation of sparse matrices.
     354             :    *
     355             :    * GhostingFunctor memory when using this method is managed by the
     356             :    * shared_ptr mechanism.
     357             :    */
     358             :   void add_coupling_functor(std::shared_ptr<GhostingFunctor> coupling_functor,
     359             :                             bool to_mesh = true)
     360             :   { _shared_functors[coupling_functor.get()] = coupling_functor;
     361             :     this->add_coupling_functor(*coupling_functor, to_mesh); }
     362             : 
     363             :   /**
     364             :    * Removes a functor which was previously added to the set of
     365             :    * coupling functors, from both this DofMap and from the underlying
     366             :    * mesh.
     367             :    */
     368             :   void remove_coupling_functor(GhostingFunctor & coupling_functor);
     369             : 
     370             :   /**
     371             :    * Beginning of range of coupling functors
     372             :    */
     373      317469 :   GhostingFunctorIterator coupling_functors_begin() const
     374      647460 :   { return _coupling_functors.begin(); }
     375             : 
     376             :   /**
     377             :    * End of range of coupling functors
     378             :    */
     379      317469 :   GhostingFunctorIterator coupling_functors_end() const
     380      647460 :   { return _coupling_functors.end(); }
     381             : 
     382             :   /**
     383             :    * Default coupling functor
     384             :    */
     385           0 :   DefaultCoupling & default_coupling() { return *_default_coupling; }
     386             : 
     387             :   /**
     388             :    * Adds a functor which can specify algebraic ghosting requirements
     389             :    * for use with distributed vectors.  Degrees of freedom on other
     390             :    * processors which match the elements and variables returned by
     391             :    * these functors will be added to the send_list, and the elements
     392             :    * on other processors will be ghosted on a distributed mesh, so
     393             :    * that the elements can always be found and the solutions on them
     394             :    * will always be evaluable.
     395             :    *
     396             :    * GhostingFunctor memory must be managed by the code which calls
     397             :    * this function; the GhostingFunctor lifetime is expected to extend
     398             :    * until either the functor is removed or the DofMap is destructed.
     399             :    *
     400             :    * When \p to_mesh is true, the \p coupling_functor is also added to
     401             :    * our associated mesh, to ensure that evaluable elements do not get
     402             :    * lost during mesh distribution.  (if evaluable elements were
     403             :    * *already* lost there's no getting them back after the fact,
     404             :    * sorry)
     405             :    *
     406             :    * If \p to_mesh is false, no change to mesh ghosting is made;
     407             :    * the Mesh must already have ghosting functor(s) specifying a
     408             :    * superset of \p evaluable_functor or this is a horrible bug.
     409             :    */
     410             :   void add_algebraic_ghosting_functor(GhostingFunctor & evaluable_functor,
     411             :                                       bool to_mesh = true);
     412             : 
     413             :   /**
     414             :    * Adds a functor which can specify algebraic ghosting requirements
     415             :    * for use with distributed vectors.
     416             :    *
     417             :    * GhostingFunctor memory when using this method is managed by the
     418             :    * shared_ptr mechanism.
     419             :    */
     420             :   void add_algebraic_ghosting_functor(std::shared_ptr<GhostingFunctor> evaluable_functor,
     421             :                                       bool to_mesh = true)
     422             :   { _shared_functors[evaluable_functor.get()] = evaluable_functor;
     423             :     this->add_algebraic_ghosting_functor(*evaluable_functor, to_mesh); }
     424             : 
     425             :   /**
     426             :    * Removes a functor which was previously added to the set of
     427             :    * algebraic ghosting functors, from both this DofMap and from the
     428             :    * underlying mesh.
     429             :    */
     430             :   void remove_algebraic_ghosting_functor(GhostingFunctor & evaluable_functor);
     431             : 
     432             :   /**
     433             :    * Beginning of range of algebraic ghosting functors
     434             :    */
     435        1754 :   GhostingFunctorIterator algebraic_ghosting_functors_begin() const
     436       10240 :   { return _algebraic_ghosting_functors.begin(); }
     437             : 
     438             :   /**
     439             :    * End of range of algebraic ghosting functors
     440             :    */
     441        1754 :   GhostingFunctorIterator algebraic_ghosting_functors_end() const
     442       10240 :   { return _algebraic_ghosting_functors.end(); }
     443             : 
     444             :   /**
     445             :    * Default algebraic ghosting functor
     446             :    */
     447           0 :   DefaultCoupling & default_algebraic_ghosting() { return *_default_evaluating; }
     448             : 
     449             :   /**
     450             :    * Attach an object to use to populate the
     451             :    * sparsity pattern with extra entries.
     452             :    *
     453             :    * Care must be taken that when adding entries they are sorted into the Rows
     454             :    *
     455             :    * Further, you _must_ modify n_nz and n_oz properly!
     456             :    *
     457             :    * This is an advanced function... use at your own peril!
     458             :    */
     459             :   void attach_extra_sparsity_object (SparsityPattern::AugmentSparsityPattern & asp)
     460             :   {
     461             :     _augment_sparsity_pattern = &asp;
     462             :   }
     463             : 
     464             :   /**
     465             :    * Attach a function pointer to use as a callback to populate the
     466             :    * sparsity pattern with extra entries.
     467             :    *
     468             :    * Care must be taken that when adding entries they are sorted into the Rows
     469             :    *
     470             :    * Further, you _must_ modify n_nz and n_oz properly!
     471             :    *
     472             :    * This is an advanced function... use at your own peril!
     473             :    */
     474             :   void attach_extra_sparsity_function(void (*func)(SparsityPattern::Graph & sparsity,
     475             :                                                    std::vector<dof_id_type> & n_nz,
     476             :                                                    std::vector<dof_id_type> & n_oz,
     477             :                                                    void *),
     478             :                                       void * context = nullptr)
     479             :   { _extra_sparsity_function = func; _extra_sparsity_context = context; }
     480             : 
     481             :   /**
     482             :    * Attach an object to populate the send_list with extra entries.
     483             :    * This should only add to the send list, but no checking is done
     484             :    * to enforce this behavior.
     485             :    *
     486             :    * This is an advanced function... use at your own peril!
     487             :    */
     488             :   void attach_extra_send_list_object (DofMap::AugmentSendList & asl)
     489             :   {
     490             :     _augment_send_list = &asl;
     491             :   }
     492             : 
     493             :   /**
     494             :    * Attach a function pointer to use as a callback to populate the
     495             :    * send_list with extra entries.
     496             :    */
     497             :   void attach_extra_send_list_function(void (*func)(std::vector<dof_id_type> &, void *),
     498             :                                        void * context = nullptr)
     499             :   { _extra_send_list_function = func; _extra_send_list_context = context; }
     500             : 
     501             :   /**
     502             :    * Takes the \p _send_list vector (which may have duplicate entries)
     503             :    * and sorts it.  The duplicate entries are then removed, resulting in
     504             :    * a sorted \p _send_list with unique entries.  Also calls any user-provided
     505             :    * methods for adding to the send list.
     506             :    */
     507             :   void prepare_send_list ();
     508             : 
     509             :   /**
     510             :    * Clears the \p _send_list vector. This should be done in order to completely
     511             :    * rebuild the send_list from scratch rather than merely adding to the existing
     512             :    * send_list.
     513             :    */
     514       15962 :   void clear_send_list ()
     515             :   {
     516       15962 :     _send_list.clear();
     517       15962 :   }
     518             : 
     519             :   /**
     520             :    * Clears the \p _send_list vector and then rebuilds it. This may be needed
     521             :    * in special situations, for example when an algebraic coupling functor cannot
     522             :    * be added to the \p DofMap until after it is completely setup. Then this method
     523             :    * can be used to rebuild the send_list once the algebraic coupling functor is
     524             :    * added. Note that while this will recommunicate constraints with the updated
     525             :    * send_list, this does assume no new constraints have been added since the previous
     526             :    * reinit_constraints call.
     527             :    */
     528             :   void reinit_send_list (MeshBase & mesh);
     529             : 
     530             : 
     531             :   /**
     532             :    * \returns A constant reference to the \p _send_list for this processor.
     533             :    *
     534             :    * The \p _send_list contains the global indices of all the
     535             :    * variables in the global solution vector that influence the
     536             :    * current processor.  This information can be used for gathers at
     537             :    * each solution step to retrieve solution values needed for
     538             :    * computation.
     539             :    */
     540     2757842 :   const std::vector<dof_id_type> & get_send_list() const { return _send_list; }
     541             : 
     542             :   /**
     543             :    * \returns A constant reference to the \p _n_nz list for this processor.
     544             :    *
     545             :    * The vector contains the bandwidth of the on-processor coupling for each
     546             :    * row of the global matrix that the current processor owns.  This
     547             :    * information can be used to preallocate space for a parallel sparse matrix.
     548             :    */
     549             :   const std::vector<dof_id_type> & get_n_nz() const
     550             :   {
     551             :     libmesh_assert(_sp);
     552             :     return _sp->get_n_nz();
     553             :   }
     554             : 
     555             :   /**
     556             :    * \returns A constant reference to the \p _n_oz list for this processor.
     557             :    *
     558             :    * The vector contains the bandwidth of the off-processor coupling for each
     559             :    * row of the global matrix that the current processor owns.  This
     560             :    * information can be used to preallocate space for a parallel sparse matrix.
     561             :    */
     562             :   const std::vector<dof_id_type> & get_n_oz() const
     563             :   {
     564             :     libmesh_assert(_sp);
     565             :     return _sp->get_n_oz();
     566             :   }
     567             : 
     568             : 
     569             :   /**
     570             :    * \returns A constant pointer to the sparsity pattern stored here,
     571             :    * once that has been computed.  Returns null if no sparsity pattern
     572             :    * has yet been computed.
     573             :    *
     574             :    * If need_full_sparsity_pattern is false, the "sparsity pattern"
     575             :    * may only own n_nz and n_oz lists.
     576             :    */
     577         668 :   const SparsityPattern::Build * get_sparsity_pattern() const
     578             :   {
     579         668 :     return _sp.get();
     580             :   }
     581             : 
     582             :   /**
     583             :    * \returns The number of variables in the system
     584             :    */
     585             :   unsigned int n_vars() const;
     586             : 
     587             :   /**
     588             :    * \returns The name of variable \p i.
     589             :    */
     590             :   const std::string & variable_name(const unsigned int i) const;
     591             : 
     592             :   /**
     593             :    * \returns The total number of scalar components in the system's
     594             :    * variables.  This will equal \p n_vars() in the case of all
     595             :    * scalar-valued variables. If vector variables are involved, we
     596             :    * will need to leverage the \p mesh
     597             :    */
     598             :   unsigned int n_components(const MeshBase & mesh) const;
     599             : 
     600             :   /**
     601             :    * \returns \p true when \p VariableGroup structures should be
     602             :    * automatically identified, \p false otherwise.
     603             :    */
     604             :   bool identify_variable_groups () const;
     605             : 
     606             :   /**
     607             :    * Toggle automatic \p VariableGroup identification.
     608             :    */
     609             :   void identify_variable_groups (const bool);
     610             : 
     611             :   /**
     612             :    * \returns An index, starting from 0 for the first component of the
     613             :    * first variable, and incrementing for each component of each
     614             :    * (potentially vector-valued) variable in the system in order.
     615             :    * For systems with only scalar-valued variables, this will be the
     616             :    * same as \p var_num
     617             :    *
     618             :    * Irony: currently our only non-scalar-valued variable type is
     619             :    * SCALAR.
     620             :    */
     621             :   unsigned int variable_scalar_number (unsigned int var_num,
     622             :                                        unsigned int component) const;
     623             : 
     624             :   /**
     625             :    * \returns The finite element type for variable number \p i.
     626             :    */
     627             :   const FEType & variable_type (const unsigned int i) const;
     628             : 
     629             :   /**
     630             :    * \returns The finite element type for variable \p var.
     631             :    */
     632             :   const FEType & variable_type (std::string_view var) const;
     633             : 
     634             :   /**
     635             :    * \returns The variable number associated with
     636             :    * the user-specified variable named \p var.
     637             :    */
     638             :   unsigned int variable_number (std::string_view var) const;
     639             : 
     640             :   /**
     641             :    * \returns \p true if a variable named \p var exists in this System
     642             :    */
     643             :   bool has_variable(std::string_view var) const;
     644             : 
     645             :   /**
     646             :    * Fills \p all_variable_numbers with all the variable numbers for the
     647             :    * variables that have been added to this system.
     648             :    */
     649             :   void get_all_variable_numbers(std::vector<unsigned int> & all_variable_numbers) const;
     650             : 
     651             :   /**
     652             :    * Adds the variable \p var to the list of variables
     653             :    * for this system. If \p active_subdomains is either \p nullptr
     654             :    * (the default) or points to an empty set, then it will be assumed that
     655             :    * \p var has no subdomain restrictions
     656             :    *
     657             :    * \returns The index number for the new variable.
     658             :    */
     659             :   unsigned int add_variable (System & sys,
     660             :                              std::string_view var,
     661             :                              const FEType & type,
     662             :                              const std::set<subdomain_id_type> * const active_subdomains = nullptr);
     663             : 
     664             :   /**
     665             :    * Adds the variables \p vars to the list of variables
     666             :    * for this system. If \p active_subdomains is either \p nullptr
     667             :    * (the default) or points to an empty set, then it will be assumed that
     668             :    * the \p vars have no subdomain restrictions
     669             :    *
     670             :    * \returns The index number for the last of the new variables.
     671             :    */
     672             :   unsigned int add_variables (System & sys,
     673             :                               const std::vector<std::string> & vars,
     674             :                               const FEType & type,
     675             :                               const std::set<subdomain_id_type> * const active_subdomains = nullptr);
     676             : 
     677             :   /**
     678             :    * Adds variables \p vars to the list of variables
     679             :    * for this system. If \p active_subdomains is either \p nullptr
     680             :    * (the default) or points to an empty set, then it will be assumed that
     681             :    * the \p vars have no subdomain restrictions. This API will end up
     682             :    * calling \p this->add_variables(). However, we will additionally store data
     683             :    * that can be leveraged by the \p DofMap to build degrees of freedom
     684             :    * containers corresponding to all the variables in this variable array
     685             :    *
     686             :    * An 'array variable' is simply a sequence
     687             :    * of contiguous variable numbers defined by pair where the first member of the pair
     688             :    * is the first number in the variable sequence and the second member of the pair is
     689             :    * the number of the last variable in the sequence plus one. Array variables may be
     690             :    * used in tandem with variable grouping by downstream code to build optimized physics
     691             :    * kernels since each variable in the array will have the same shape functions.
     692             :    *
     693             :    * \returns The index number for the last of the new variables.
     694             :    */
     695             :   unsigned int add_variable_array (System & sys,
     696             :                                    const std::vector<std::string> & vars,
     697             :                                    const FEType & type,
     698             :                                    const std::set<subdomain_id_type> * const active_subdomains = nullptr);
     699             : 
     700             :   /**
     701             :    * Specify whether or not we perform an extra (opt-mode enabled) check
     702             :    * for constraint loops. If a constraint loop is present then
     703             :    * the system constraints are not valid, so if \p error_on_constraint_loop
     704             :    * is true we will throw an error in this case.
     705             :    *
     706             :    * \note We previously referred to these types of constraints as
     707             :    * "cyclic" but that has now been deprecated, and these will now
     708             :    * instead be referred to as "constraint loops" in libMesh.
     709             :    */
     710             :   void set_error_on_cyclic_constraint(bool error_on_cyclic_constraint);
     711             :   void set_error_on_constraint_loop(bool error_on_constraint_loop);
     712             : 
     713             :   /**
     714             :    * \returns The \p VariableGroup description object for group \p g.
     715             :    */
     716             :   const VariableGroup & variable_group (const unsigned int c) const;
     717             : 
     718             :   const Variable & variable (const unsigned int c) const override;
     719             : 
     720             :   /**
     721             :    * \returns The approximation order for variable \p c.
     722             :    */
     723             :   Order variable_order (const unsigned int c) const;
     724             : 
     725             :   /**
     726             :    * \returns The approximation order for \p VariableGroup \p vg.
     727             :    */
     728             :   Order variable_group_order (const unsigned int vg) const;
     729             : 
     730             :   /**
     731             :    * \returns The finite element type for \p VariableGroup \p vg.
     732             :    */
     733             :   const FEType & variable_group_type (const unsigned int vg) const;
     734             : 
     735             :   /**
     736             :    * \returns The number of variables in the global solution vector. Defaults
     737             :    * to 1, should be 1 for a scalar equation, 3 for 2D incompressible Navier
     738             :    * Stokes (u,v,p), etc...
     739             :    */
     740    12152606 :   unsigned int n_variable_groups() const
     741    24096315 :   { return cast_int<unsigned int>(_variable_groups.size()); }
     742             : 
     743     1448038 :   unsigned int n_variables() const override
     744     1501566 :   { return cast_int<unsigned int>(_variables.size()); }
     745             : 
     746             :   /**
     747             :    * \returns The variable group number that the provided variable number belongs to
     748             :    */
     749             :   unsigned int var_group_from_var_number(unsigned int var_num) const;
     750             : 
     751             :   /**
     752             :    * \returns \p true if the variables are capable of being stored in a blocked
     753             :    * form.  Presently, this means that there can only be one variable group,
     754             :    * and that the group has more than one variable.
     755             :    */
     756       45104 :   bool has_blocked_representation() const
     757             :   {
     758       46548 :     return ((this->n_variable_groups() == 1) && (this->n_variables() > 1));
     759             :   }
     760             : 
     761             :   /**
     762             :    * \returns The block size, if the variables are amenable to block storage.
     763             :    * Otherwise 1.
     764             :    * This routine was originally designed to enable a blocked storage, but
     765             :    * it turns out this information is still super useful for solvers even when
     766             :    * we do not use the blocked storage (e.g., MATMPIBAIJ in PETSc). For example (in PCHMG),
     767             :    * for a system of PDEs, to construct an efficient multilevel preconditioner, we coarsen
     768             :    * the matrix of one single PDE instead of the entire huge matrix. In order to
     769             :    * accomplish this, we need to know many PDEs we have. Another use case,
     770             :    * the fieldsplit preconditioner can be constructed in place with this info without
     771             :    * involving any user efforts.
     772             :    */
     773       46548 :   unsigned int block_size() const
     774             :   {
     775       45238 :     return (this->has_blocked_representation() ? this->n_variables() : 1);
     776             :   }
     777             : 
     778             :   using DofMapBase::n_dofs;
     779             :   /**
     780             :    * \returns The total number of degrees of freedom for a particular
     781             :    * variable \p vn.
     782             :    */
     783             :   dof_id_type n_dofs(const unsigned int vn) const
     784             :   {
     785             :     dof_id_type n = this->n_local_dofs(vn);
     786             :     this->comm().sum(n);
     787             :     return n;
     788             :   }
     789             : 
     790             :   /**
     791             :    * \returns The number of SCALAR dofs.
     792             :    */
     793      287059 :   dof_id_type n_SCALAR_dofs() const { return _n_SCALAR_dofs; }
     794             : 
     795             :   using DofMapBase::n_local_dofs;
     796             :   /**
     797             :    * \returns The number of degrees of freedom on this processor for a
     798             :    * particular variable \p vn.  This is an O(N) operation on serialized or
     799             :    * O(N/Nproc) operation on distributed meshes.
     800             :    */
     801           0 :   dof_id_type n_local_dofs(const unsigned int vn) const
     802             :   {
     803             :     dof_id_type n;
     804           0 :     this->local_variable_indices(n, _mesh, vn);
     805           0 :     return n;
     806             :   }
     807             : 
     808             :   /**
     809             :    * \returns The number of degrees of freedom on each partition for a
     810             :    * particular variable \p vn.
     811             :    */
     812         804 :   std::vector<dof_id_type> n_dofs_per_processor(const unsigned int vn) const
     813             :   {
     814         804 :     std::vector<dof_id_type> n_local_dofs(this->n_processors(), 0);
     815         804 :     this->comm().allgather(this->n_local_dofs(vn), n_local_dofs);
     816         804 :     return n_local_dofs;
     817             :   }
     818             : 
     819             :   /**
     820             :    * \returns The processor id that owns the dof index \p dof
     821             :    */
     822      261363 :   processor_id_type dof_owner(const dof_id_type dof) const
     823             :   { std::vector<dof_id_type>::const_iterator ub =
     824      261363 :       std::upper_bound(_end_df.begin(), _end_df.end(), dof);
     825        7518 :     libmesh_assert (ub != _end_df.end());
     826      261363 :     return cast_int<processor_id_type>(ub - _end_df.begin());
     827             :   }
     828             : 
     829             :   void dof_indices (const Elem * const elem,
     830             :                     std::vector<dof_id_type> & di) const;
     831             : 
     832             :   /**
     833             :    * Fills the vector \p di with the global degree of freedom indices
     834             :    * for the element.  For one variable, and potentially for a
     835             :    * non-default element p refinement level
     836             :    */
     837             :   void dof_indices (const Elem * const elem,
     838             :                     std::vector<dof_id_type> & di,
     839             :                     const unsigned int vn,
     840             :                     int p_level = -12345) const override;
     841             : 
     842             :   /**
     843             :    * Fills the vector \p di with the global degree of freedom indices
     844             :    * for the element. This will aggregate all the degrees of the freedom
     845             :    * from the variable array that \p vn is a member of, and potentially for a
     846             :    * non-default element p refinement level
     847             :    */
     848             :   void array_dof_indices(const Elem * const elem,
     849             :                          std::vector<dof_id_type> & di,
     850             :                          const unsigned int vn,
     851             :                          int p_level = -12345) const;
     852             : 
     853             :   void array_dof_indices(const Node * const node,
     854             :                          std::vector<dof_id_type> & di,
     855             :                          const unsigned int vn) const;
     856             : 
     857             :   template <typename DofIndicesFunctor>
     858             :   void array_dof_indices(const DofIndicesFunctor & functor,
     859             :                          std::vector<dof_id_type> & di,
     860             :                          const unsigned int vn) const;
     861             : 
     862             :   /**
     863             :    * Retrieves degree of freedom indices for a given \p elem and then performs actions for these
     864             :    * indices defined by the user-provided functors \p scalar_dofs_functor and \p field_dofs_functor.
     865             :    * This API is useful when a user wants to do more than simply fill a degree of freedom container
     866             :    * @param elem The element to get degrees of freedom for
     867             :    * @param di A container for degrees of freedom. It is up to the provided functors how this gets
     868             :    *           filled
     869             :    * @param vn The variable number to retrieve degrees of freedom for
     870             :    * @param scalar_dofs_functor The functor that acts on scalar degrees of freedom. This functor has
     871             :    *                            the interface:
     872             :    *                            void scalar_dofs_functor(const Elem & elem,
     873             :    *                                                    std::vector<dof_id_type> & di,
     874             :    *                                                    const std::vector<dof_id_type> & scalar_dof_indices)
     875             :    *                            where \p di is the degree of freedom container described above and
     876             :    *                            \p scalar_dof_indices are the scalar dof indices available to
     877             :    *                            \p elem
     878             :    * @param field_dofs_functor The functor that acts on "field" (e.g. non-scalar, non-global)
     879             :    *                           degrees of freedom. This functor has
     880             :    *                           the interface:
     881             :    *                           void field_dofs_functor(const Elem & elem,
     882             :    *                                                   const unsigned int node_num,
     883             :    *                                                   const unsigned int var_num,
     884             :    *                                                   std::vector<dof_id_type> & di,
     885             :    *                                                   const dof_id_type field_dof)
     886             :    *                           where \p field_dof represents a field degree of freedom to act on and
     887             :    *                           is associated with \p node_num and \p var_num. If the degree of
     888             :    *                           freedom is elemental than \p node_num will be \p invalid_uint. \p di
     889             :    *                           is again the degree of freedom container provided above
     890             :    */
     891             :   template <typename ScalarDofsFunctor, typename FieldDofsFunctor>
     892             :   void dof_indices(const Elem * const elem,
     893             :                    std::vector<dof_id_type> & di,
     894             :                    const unsigned int vn,
     895             :                    ScalarDofsFunctor scalar_dofs_functor,
     896             :                    FieldDofsFunctor field_dofs_functor,
     897             :                    int p_level = -12345) const;
     898             : 
     899             :   /**
     900             :    * Fills the vector \p di with the global degree of freedom indices
     901             :    * for the \p node.
     902             :    */
     903             :   void dof_indices (const Node * const node,
     904             :                     std::vector<dof_id_type> & di) const;
     905             : 
     906             :   /**
     907             :    * Fills the vector \p di with the global degree of freedom indices
     908             :    * for the \p node, for one variable \p vn.
     909             :    */
     910             :   void dof_indices (const Node * const node,
     911             :                     std::vector<dof_id_type> & di,
     912             :                     const unsigned int vn) const override;
     913             : 
     914             :   /**
     915             :    * Appends to the vector \p di the global degree of freedom indices
     916             :    * for \p elem.node_ref(n), for one variable \p vn.  On hanging
     917             :    * nodes with both vertex and non-vertex DoFs, only those indices
     918             :    * which are directly supported on \p elem are included.
     919             :    */
     920             :   void dof_indices (const Elem & elem,
     921             :                     unsigned int n,
     922             :                     std::vector<dof_id_type> & di,
     923             :                     const unsigned int vn) const;
     924             : 
     925             : #ifdef LIBMESH_ENABLE_AMR
     926             : 
     927             :   /**
     928             :    * Appends to the vector \p di the old global degree of freedom
     929             :    * indices for \p elem.node_ref(n), for one variable \p vn.  On
     930             :    * hanging nodes with both vertex and non-vertex DoFs, only those
     931             :    * indices which are directly supported on \p elem are included.
     932             :    */
     933             :   void old_dof_indices (const Elem & elem,
     934             :                         unsigned int n,
     935             :                         std::vector<dof_id_type> & di,
     936             :                         const unsigned int vn) const;
     937             : 
     938             : #endif // LIBMESH_ENABLE_AMR
     939             : 
     940             :   /**
     941             :    * Fills the vector \p di with the global degree of freedom indices
     942             :    * corresponding to the SCALAR variable vn. If old_dofs=true,
     943             :    * the old SCALAR dof indices are returned.
     944             :    *
     945             :    * \note We do not need to pass in an element since SCALARs are
     946             :    * global variables.
     947             :    */
     948             :   void SCALAR_dof_indices (std::vector<dof_id_type> & di,
     949             :                            const unsigned int vn,
     950             :                            const bool old_dofs=false) const;
     951             : 
     952             :   /**
     953             :    * \returns \p true if degree of freedom index \p dof_index
     954             :    * is either a local index or in the \p send_list.
     955             :    *
     956             :    * \note This is an O(logN) operation for a send_list of size N; we
     957             :    * don't cache enough information for O(1) right now.
     958             :    */
     959             :   bool semilocal_index (dof_id_type dof_index) const;
     960             : 
     961             :   /**
     962             :    * \returns \p true if all degree of freedom indices in \p
     963             :    * dof_indices are either local indices or in the \p send_list.
     964             :    *
     965             :    * \note This is an O(logN) operation for a send_list of size N; we
     966             :    * don't cache enough information for O(1) right now.
     967             :    */
     968             :   bool all_semilocal_indices (const std::vector<dof_id_type> & dof_indices) const;
     969             : 
     970             :   /**
     971             :    * \returns \p true if degree of freedom index \p dof_index
     972             :    * is a local index.
     973             :    */
     974    78236570 :   bool local_index (dof_id_type dof_index) const
     975    85169124 :   { return (dof_index >= this->first_dof()) && (dof_index < this->end_dof()); }
     976             : 
     977             :   /**
     978             :    * \returns \p true iff our solutions can be locally evaluated on
     979             :    * \p obj (which should be an Elem or a Node) for variable number \p
     980             :    * var_num (for all variables, if \p var_num is invalid_uint)
     981             :    */
     982             :   template <typename DofObjectSubclass>
     983             :   bool is_evaluable(const DofObjectSubclass & obj,
     984             :                     unsigned int var_num = libMesh::invalid_uint) const;
     985             : 
     986             :   /**
     987             :    * Allow the implicit_neighbor_dofs flag to be set programmatically.
     988             :    * This overrides the --implicit_neighbor_dofs commandline option.
     989             :    * We can use this to set the implicit neighbor dofs option differently
     990             :    * for different systems, whereas the commandline option is the same
     991             :    * for all systems.
     992             :    */
     993             :   void set_implicit_neighbor_dofs(bool implicit_neighbor_dofs);
     994             : 
     995             :   /**
     996             :    * Set the _verify_dirichlet_bc_consistency flag.
     997             :    */
     998             :   void set_verify_dirichlet_bc_consistency(bool val);
     999             : 
    1000             :   /**
    1001             :    * Tells other library functions whether or not this problem
    1002             :    * includes coupling between dofs in neighboring cells, as can
    1003             :    * currently be specified on the command line or inferred from
    1004             :    * the use of all discontinuous variables.
    1005             :    */
    1006             :   bool use_coupled_neighbor_dofs(const MeshBase & mesh) const;
    1007             : 
    1008             :   /**
    1009             :    * Builds the local element vector \p Ue from the global vector \p Ug,
    1010             :    * accounting for any constrained degrees of freedom.  For an element
    1011             :    * without constrained degrees of freedom this is the trivial mapping
    1012             :    * \f$ Ue[i] = Ug[dof_indices[i]] \f$
    1013             :    *
    1014             :    * \note The user must ensure that the element vector \p Ue is
    1015             :    * properly sized when calling this method.  This is because there
    1016             :    * is no \p resize() method in the \p DenseVectorBase<> class.
    1017             :    */
    1018             :   void extract_local_vector (const NumericVector<Number> & Ug,
    1019             :                              const std::vector<dof_id_type> & dof_indices,
    1020             :                              DenseVectorBase<Number> & Ue) const;
    1021             : 
    1022             :   /**
    1023             :    * If T == dof_id_type, counts, if T == std::vector<dof_id_type>, fills an
    1024             :    * array of, those dof indices which belong to the given variable number and
    1025             :    * live on the current processor.
    1026             :    */
    1027             :   template <typename T, std::enable_if_t<std::is_same_v<T, dof_id_type> ||
    1028             :                                          std::is_same_v<T, std::vector<dof_id_type>>, int> = 0>
    1029             :   void local_variable_indices(T & idx,
    1030             :                               const MeshBase & mesh,
    1031             :                               unsigned int var_num) const;
    1032             : 
    1033             :   /**
    1034             :    * If T == dof_id_type, counts, if T == std::vector<dof_id_type>, fills an
    1035             :    * array of, those dof indices which belong to the given variable number and
    1036             :    * live on the current processor.
    1037             :    */
    1038             :   template <typename T,
    1039             :             std::enable_if_t<std::is_same_v<T, dof_id_type> ||
    1040             :                                  std::is_same_v<T, std::vector<dof_id_type>>,
    1041             :                              int> = 0>
    1042             :   void local_variable_indices(T & idx, unsigned int var_num) const
    1043             :   { this->local_variable_indices(idx, this->_mesh, var_num); }
    1044             : 
    1045             : #ifdef LIBMESH_ENABLE_CONSTRAINTS
    1046             : 
    1047             :   //--------------------------------------------------------------------
    1048             :   // Constraint-specific methods
    1049             :   /**
    1050             :    * \returns The total number of constrained degrees of freedom
    1051             :    * in the problem.
    1052             :    */
    1053             :   dof_id_type n_constrained_dofs() const;
    1054             : 
    1055             :   /**
    1056             :    * \returns The number of constrained degrees of freedom
    1057             :    * on this processor.
    1058             :    */
    1059             :   dof_id_type n_local_constrained_dofs() const;
    1060             : 
    1061             : #ifdef LIBMESH_ENABLE_NODE_CONSTRAINTS
    1062             :   /**
    1063             :    * \returns The total number of constrained Nodes
    1064             :    * in the mesh.
    1065             :    */
    1066             :   dof_id_type n_constrained_nodes() const
    1067             :   { return cast_int<dof_id_type>(_node_constraints.size()); }
    1068             : #endif // LIBMESH_ENABLE_NODE_CONSTRAINTS
    1069             : 
    1070             :   /**
    1071             :    * Rebuilds the raw degree of freedom and DofObject constraints,
    1072             :    * based on attached DirichletBoundary objects and on non-conforming
    1073             :    * interface in adapted meshes.
    1074             :    *
    1075             :    * A time is specified for use in building time-dependent Dirichlet
    1076             :    * constraints.
    1077             :    */
    1078             :   void create_dof_constraints (const MeshBase &, Real time=0);
    1079             : 
    1080             :   /**
    1081             :    * Gathers constraint equation dependencies from other processors
    1082             :    */
    1083             :   void allgather_recursive_constraints (MeshBase &);
    1084             : 
    1085             :   /**
    1086             :    * Sends constraint equations to constraining processors
    1087             :    */
    1088             :   void scatter_constraints (MeshBase &);
    1089             : 
    1090             :   /**
    1091             :    * Helper function for querying about constraint equations on other
    1092             :    * processors.  If any id in \p requested_dof_ids is constrained on
    1093             :    * another processor, its constraint will be added on this processor
    1094             :    * as well.  If \p look_for_constrainees is true, then constraints
    1095             :    * will also be returned if the id appears as a constraining value
    1096             :    * not just if it appears as a constrained value.
    1097             :    *
    1098             :    * This function operates recursively: if the constraint for a
    1099             :    * constrained dof is newly added locally, then any other dofs which
    1100             :    * constrain it are queried to see if they are in turn constrained,
    1101             :    * and so on.
    1102             :    */
    1103             :   void gather_constraints (MeshBase & mesh,
    1104             :                            std::set<dof_id_type> & unexpanded_dofs,
    1105             :                            bool look_for_constrainees);
    1106             : 
    1107             :   /**
    1108             :    * Postprocesses any constrained degrees of freedom
    1109             :    * to be constrained only in terms of unconstrained dofs, then adds
    1110             :    * unconstrained dofs to the send_list and prepares that for use.
    1111             :    * This should be run after both system (create_dof_constraints) and
    1112             :    * user constraints have all been added.
    1113             :    */
    1114             :   void process_constraints (MeshBase &);
    1115             : 
    1116             :   /**
    1117             :    * Throw an error if we detect any constraint loops, i.e.
    1118             :    * A -> B -> C -> A
    1119             :    * that is, "dof A is constrained in terms of dof B which is
    1120             :    * constrained in terms of dof C which is constrained in terms of
    1121             :    * dof A", since these are not supported by libMesh and give
    1122             :    * erroneous results if they are present.
    1123             :    *
    1124             :    * \note The original "cyclic constraint" terminology was
    1125             :    * unfortunate since the word cyclic is used by some software to
    1126             :    * indicate an actual type of rotational/angular constraint and not
    1127             :    * (as here) a cyclic graph. The former nomenclature will eventually
    1128             :    * be deprecated in favor of "constraint loop".
    1129             :    */
    1130             :   void check_for_cyclic_constraints();
    1131             :   void check_for_constraint_loops();
    1132             : 
    1133             :   /**
    1134             :    * Adds a copy of the user-defined row to the constraint matrix, using
    1135             :    * an inhomogeneous right-hand-side for the constraint equation.
    1136             :    */
    1137             :   void add_constraint_row (const dof_id_type dof_number,
    1138             :                            const DofConstraintRow & constraint_row,
    1139             :                            const Number constraint_rhs,
    1140             :                            const bool forbid_constraint_overwrite);
    1141             : 
    1142             :   /**
    1143             :    * Adds a copy of the user-defined row to the constraint matrix,
    1144             :    * using an inhomogeneous right-hand-side for the adjoint constraint
    1145             :    * equation.
    1146             :    *
    1147             :    * \p forbid_constraint_overwrite here only tests for overwriting
    1148             :    * the rhs.  This method should only be used when an equivalent
    1149             :    * constraint (with a potentially different rhs) already exists for
    1150             :    * the primal problem.
    1151             :    */
    1152             :   void add_adjoint_constraint_row (const unsigned int qoi_index,
    1153             :                                    const dof_id_type dof_number,
    1154             :                                    const DofConstraintRow & constraint_row,
    1155             :                                    const Number constraint_rhs,
    1156             :                                    const bool forbid_constraint_overwrite);
    1157             : 
    1158             :   /**
    1159             :    * Adds a copy of the user-defined row to the constraint matrix, using
    1160             :    * a homogeneous right-hand-side for the constraint equation.
    1161             :    * By default, produces an error if the DOF was already constrained.
    1162             :    */
    1163       17640 :   void add_constraint_row (const dof_id_type dof_number,
    1164             :                            const DofConstraintRow & constraint_row,
    1165             :                            const bool forbid_constraint_overwrite = true)
    1166      231006 :   { add_constraint_row(dof_number, constraint_row, 0., forbid_constraint_overwrite); }
    1167             : 
    1168             :   /**
    1169             :    * \returns An iterator pointing to the first DoF constraint row.
    1170             :    */
    1171             :   DofConstraints::const_iterator constraint_rows_begin() const
    1172             :   { return _dof_constraints.begin(); }
    1173             : 
    1174             :   /**
    1175             :    * \returns An iterator pointing just past the last DoF constraint row.
    1176             :    */
    1177             :   DofConstraints::const_iterator constraint_rows_end() const
    1178             :   { return _dof_constraints.end(); }
    1179             : 
    1180             :   /**
    1181             :    * Provide a const accessor to the DofConstraints map. This allows the user
    1182             :    * to quickly search the data structure rather than just iterating over it.
    1183             :    */
    1184      221857 :   const DofConstraints & get_dof_constraints() const { return _dof_constraints; }
    1185             : 
    1186             :   void stash_dof_constraints()
    1187             :   {
    1188             :     libmesh_assert(_stashed_dof_constraints.empty());
    1189             :     _dof_constraints.swap(_stashed_dof_constraints);
    1190             :   }
    1191             : 
    1192             :   void unstash_dof_constraints()
    1193             :   {
    1194             :     libmesh_assert(_dof_constraints.empty());
    1195             :     _dof_constraints.swap(_stashed_dof_constraints);
    1196             :   }
    1197             : 
    1198             :   /**
    1199             :    * Similar to the stash/unstash_dof_constraints() API, but swaps
    1200             :    * _dof_constraints and _stashed_dof_constraints without asserting
    1201             :    * that the source or destination is empty first.
    1202             :    *
    1203             :    * \note There is an implicit assumption that swapping between sets
    1204             :    * of Constraints does not change the sparsity pattern or expand the
    1205             :    * send_list, since the only thing changed is the DofConstraints
    1206             :    * themselves.  This is intended to work for swapping between
    1207             :    * DofConstraints A and B, where A is used to define the send_list,
    1208             :    * and B is a subset of A.
    1209             :    */
    1210             :   void swap_dof_constraints()
    1211             :   {
    1212             :     _dof_constraints.swap(_stashed_dof_constraints);
    1213             :   }
    1214             : 
    1215             : #ifdef LIBMESH_ENABLE_NODE_CONSTRAINTS
    1216             :   /**
    1217             :    * \returns An iterator pointing to the first Node constraint row.
    1218             :    */
    1219             :   NodeConstraints::const_iterator node_constraint_rows_begin() const
    1220             :   { return _node_constraints.begin(); }
    1221             : 
    1222             :   /**
    1223             :    * \returns An iterator pointing just past the last Node constraint row.
    1224             :    */
    1225             :   NodeConstraints::const_iterator node_constraint_rows_end() const
    1226             :   { return _node_constraints.end(); }
    1227             : #endif // LIBMESH_ENABLE_NODE_CONSTRAINTS
    1228             : 
    1229             :   /**
    1230             :    * \returns \p true if the degree of freedom dof is constrained,
    1231             :    * \p false otherwise.
    1232             :    */
    1233             :   bool is_constrained_dof (const dof_id_type dof) const;
    1234             : 
    1235             :   /**
    1236             :    * \returns \p true if the system has any heterogeneous constraints for
    1237             :    * adjoint solution \p qoi_num, \p false otherwise.
    1238             :    */
    1239             :   bool has_heterogeneous_adjoint_constraints (const unsigned int qoi_num) const;
    1240             : 
    1241             :   /**
    1242             :    * Backwards compatibility with misspelling.
    1243             :    */
    1244        3072 :   bool has_heterogenous_adjoint_constraints (const unsigned int qoi_num) const
    1245             :   {
    1246       79722 :     return this->has_heterogeneous_adjoint_constraints (qoi_num);
    1247             :   }
    1248             : 
    1249             :   /**
    1250             :    * \returns The heterogeneous constraint value if the degree of
    1251             :    * freedom \p dof has a heterogeneous constraint for adjoint solution
    1252             :    * \p qoi_num, zero otherwise.
    1253             :    */
    1254             :   Number has_heterogeneous_adjoint_constraint (const unsigned int qoi_num,
    1255             :                                                const dof_id_type dof) const;
    1256             : 
    1257             :   /**
    1258             :    * Backwards compatibility with misspelling.
    1259             :    */
    1260     1093446 :   Number has_heterogenous_adjoint_constraint (const unsigned int qoi_num,
    1261             :                                               const dof_id_type dof) const
    1262             :   {
    1263     1414426 :     return this->has_heterogeneous_adjoint_constraint (qoi_num, dof);
    1264             :   }
    1265             : 
    1266             :   /**
    1267             :    * \returns A reference to the set of right-hand-side values in
    1268             :    * primal constraint equations
    1269             :    */
    1270             :   DofConstraintValueMap & get_primal_constraint_values();
    1271             : 
    1272             :   /**
    1273             :    * \returns \p true if the Node is constrained,
    1274             :    * false otherwise.
    1275             :    */
    1276             :   bool is_constrained_node (const Node * node) const;
    1277             : 
    1278             :   /**
    1279             :    * Prints (from processor 0) all DoF and Node constraints.  If \p
    1280             :    * print_nonlocal is true, then each constraint is printed once for
    1281             :    * each processor that knows about it, which may be useful for \p
    1282             :    * DistributedMesh debugging.
    1283             :    */
    1284             :   void print_dof_constraints(std::ostream & os=libMesh::out,
    1285             :                              bool print_nonlocal=false) const;
    1286             : 
    1287             :   /**
    1288             :    * Gets a string reporting all DoF and Node constraints local to
    1289             :    * this processor.  If \p print_nonlocal is true, then nonlocal
    1290             :    * constraints which are locally known are included.
    1291             :    */
    1292             :   std::string get_local_constraints(bool print_nonlocal=false) const;
    1293             : 
    1294             : 
    1295             :   /**
    1296             :    * Tests the constrained degrees of freedom on the numeric vector \p v, which
    1297             :    * represents a solution defined on the mesh, returning a pair whose first
    1298             :    * entry is the maximum absolute error on a constrained DoF and whose second
    1299             :    * entry is the maximum relative error.  Useful for debugging purposes.
    1300             :    *
    1301             :    * If \p v == nullptr, the system solution vector is tested.
    1302             :    */
    1303             :   std::pair<Real, Real> max_constraint_error(const System & system,
    1304             :                                              NumericVector<Number> * v = nullptr) const;
    1305             : 
    1306             : #endif // LIBMESH_ENABLE_CONSTRAINTS
    1307             : 
    1308             :   //--------------------------------------------------------------------
    1309             :   // Constraint-specific methods
    1310             :   // Some of these methods are enabled (but inlined away to nothing)
    1311             :   // when constraints are disabled at configure-time.  This is to
    1312             :   // increase API compatibility of user code with different library
    1313             :   // builds.
    1314             : 
    1315             :   /**
    1316             :    * Constrains the element matrix.  This method requires the
    1317             :    * element matrix to be square, in which case the elem_dofs
    1318             :    * correspond to the global DOF indices of both the rows and
    1319             :    * columns of the element matrix.  For this case the rows
    1320             :    * and columns of the matrix necessarily correspond to variables
    1321             :    * of the same approximation order.
    1322             :    *
    1323             :    * If \p asymmetric_constraint_rows is set to true (as it is by
    1324             :    * default), constraint row equations will be reinforced in a way
    1325             :    * which breaks matrix symmetry but makes inexact linear solver
    1326             :    * solutions more likely to satisfy hanging node constraints.
    1327             :    */
    1328             :   void constrain_element_matrix (DenseMatrix<Number> & matrix,
    1329             :                                  std::vector<dof_id_type> & elem_dofs,
    1330             :                                  bool asymmetric_constraint_rows = true) const;
    1331             : 
    1332             :   /**
    1333             :    * Constrains the element matrix.  This method allows the
    1334             :    * element matrix to be non-square, in which case the row_dofs
    1335             :    * and col_dofs may be of different size and correspond to
    1336             :    * variables approximated in different spaces.
    1337             :    */
    1338             :   void constrain_element_matrix (DenseMatrix<Number> & matrix,
    1339             :                                  std::vector<dof_id_type> & row_dofs,
    1340             :                                  std::vector<dof_id_type> & col_dofs,
    1341             :                                  bool asymmetric_constraint_rows = true) const;
    1342             : 
    1343             :   /**
    1344             :    * Constrains the element vector.
    1345             :    */
    1346             :   void constrain_element_vector (DenseVector<Number> & rhs,
    1347             :                                  std::vector<dof_id_type> & dofs,
    1348             :                                  bool asymmetric_constraint_rows = true) const;
    1349             : 
    1350             :   /**
    1351             :    * Constrains the element matrix and vector.  This method requires
    1352             :    * the element matrix to be square, in which case the elem_dofs
    1353             :    * correspond to the global DOF indices of both the rows and
    1354             :    * columns of the element matrix.  For this case the rows
    1355             :    * and columns of the matrix necessarily correspond to variables
    1356             :    * of the same approximation order.
    1357             :    */
    1358             :   void constrain_element_matrix_and_vector (DenseMatrix<Number> & matrix,
    1359             :                                             DenseVector<Number> & rhs,
    1360             :                                             std::vector<dof_id_type> & elem_dofs,
    1361             :                                             bool asymmetric_constraint_rows = true) const;
    1362             : 
    1363             :   /**
    1364             :    * Constrains the element matrix and vector.  This method requires
    1365             :    * the element matrix to be square, in which case the elem_dofs
    1366             :    * correspond to the global DOF indices of both the rows and
    1367             :    * columns of the element matrix.  For this case the rows
    1368             :    * and columns of the matrix necessarily correspond to variables
    1369             :    * of the same approximation order.
    1370             :    *
    1371             :    * The heterogeneous version of this method creates linear systems in
    1372             :    * which heterogeneously constrained degrees of freedom will solve to
    1373             :    * their correct offset values, as would be appropriate for finding
    1374             :    * a solution to a linear problem in a single algebraic solve.  The
    1375             :    * non-heterogeneous version of this method creates linear systems in
    1376             :    * which even heterogeneously constrained degrees of freedom are
    1377             :    * solved without offset values taken into account, as would be
    1378             :    * appropriate for finding linearized updates to a solution in which
    1379             :    * heterogeneous constraints are already satisfied.
    1380             :    *
    1381             :    * By default, the constraints for the primal solution of this
    1382             :    * system are used.  If a non-negative \p qoi_index is passed in,
    1383             :    * then the constraints for the corresponding adjoint solution are
    1384             :    * used instead.
    1385             :    */
    1386             :   void heterogeneously_constrain_element_matrix_and_vector (DenseMatrix<Number> & matrix,
    1387             :                                                             DenseVector<Number> & rhs,
    1388             :                                                             std::vector<dof_id_type> & elem_dofs,
    1389             :                                                             bool asymmetric_constraint_rows = true,
    1390             :                                                             int qoi_index = -1) const;
    1391             : 
    1392             :   /*
    1393             :    * Backwards compatibility with misspelling.
    1394             :    */
    1395           0 :   void heterogenously_constrain_element_matrix_and_vector (DenseMatrix<Number> & matrix,
    1396             :                                                            DenseVector<Number> & rhs,
    1397             :                                                            std::vector<dof_id_type> & elem_dofs,
    1398             :                                                            bool asymmetric_constraint_rows = true,
    1399             :                                                            int qoi_index = -1) const
    1400             :   {
    1401             :     return this->heterogeneously_constrain_element_matrix_and_vector
    1402       15527 :       (matrix, rhs, elem_dofs, asymmetric_constraint_rows, qoi_index);
    1403             :   }
    1404             : 
    1405             :   /**
    1406             :    * Constrains the element vector.  This method requires
    1407             :    * the element matrix to be square and not-yet-constrained, in which
    1408             :    * case the elem_dofs correspond to the global DOF indices of both
    1409             :    * the rows and columns of the element matrix.
    1410             :    *
    1411             :    * The heterogeneous version of this method creates linear systems in
    1412             :    * which heterogeneously constrained degrees of freedom will solve to
    1413             :    * their correct offset values, as would be appropriate for finding
    1414             :    * a solution to a linear problem in a single algebraic solve.  The
    1415             :    * non-heterogeneous version of this method creates linear systems in
    1416             :    * which even heterogeneously constrained degrees of freedom are
    1417             :    * solved without offset values taken into account, as would be
    1418             :    * appropriate for finding linearized updates to a solution in which
    1419             :    * heterogeneous constraints are already satisfied.
    1420             :    *
    1421             :    * Note the sign difference from the nonlinear heterogeneous constraint
    1422             :    * method: Solving u:=K\f has the opposite sign convention from
    1423             :    * u:=u_in-J\r, and we apply heterogeneous constraints accordingly.
    1424             :    *
    1425             :    * By default, the constraints for the primal solution of this
    1426             :    * system are used.  If a non-negative \p qoi_index is passed in,
    1427             :    * then the constraints for the corresponding adjoint solution are
    1428             :    * used instead.
    1429             :    */
    1430             :   void heterogeneously_constrain_element_vector (const DenseMatrix<Number> & matrix,
    1431             :                                                  DenseVector<Number> & rhs,
    1432             :                                                  std::vector<dof_id_type> & elem_dofs,
    1433             :                                                  bool asymmetric_constraint_rows = true,
    1434             :                                                  int qoi_index = -1) const;
    1435             : 
    1436             :   /*
    1437             :    * Backwards compatibility with misspelling.
    1438             :    */
    1439         140 :   void heterogenously_constrain_element_vector (const DenseMatrix<Number> & matrix,
    1440             :                                                 DenseVector<Number> & rhs,
    1441             :                                                 std::vector<dof_id_type> & elem_dofs,
    1442             :                                                 bool asymmetric_constraint_rows = true,
    1443             :                                                 int qoi_index = -1) const
    1444             :   {
    1445             :     return this->heterogeneously_constrain_element_vector
    1446        1680 :       (matrix, rhs, elem_dofs, asymmetric_constraint_rows, qoi_index);
    1447             :   }
    1448             : 
    1449             :   /**
    1450             :    * Constrains the element Jacobian and residual.  The element
    1451             :    * Jacobian is square, and the elem_dofs should correspond to the
    1452             :    * global DOF indices of both the rows and columns of the element
    1453             :    * matrix.
    1454             :    *
    1455             :    * The residual-constraining version of this method creates linear
    1456             :    * systems in which heterogeneously constrained degrees of freedom
    1457             :    * create non-zero residual terms when not at their correct offset
    1458             :    * values, as would be appropriate for finding a solution to a
    1459             :    * nonlinear problem in a quasi-Newton solve.
    1460             :    *
    1461             :    * Note the sign difference from the linear heterogeneous constraint
    1462             :    * method: Solving u:=u_in-J\r has the opposite sign convention from
    1463             :    * u:=K\f, and we apply heterogeneous constraints accordingly.
    1464             :    *
    1465             :    * The \p solution vector passed in should be a serialized or
    1466             :    * ghosted primal solution
    1467             :    */
    1468             :   void heterogeneously_constrain_element_jacobian_and_residual (DenseMatrix<Number> & matrix,
    1469             :                                                                 DenseVector<Number> & rhs,
    1470             :                                                                 std::vector<dof_id_type> & elem_dofs,
    1471             :                                                                 NumericVector<Number> & solution_local) const;
    1472             : 
    1473             :   /**
    1474             :    * Constrains the element residual.  The element Jacobian is square,
    1475             :    * and the elem_dofs should correspond to the global DOF indices of
    1476             :    * both the rows and columns of the element matrix.
    1477             :    *
    1478             :    * The residual-constraining version of this method creates linear
    1479             :    * systems in which heterogeneously constrained degrees of freedom
    1480             :    * create non-zero residual terms when not at their correct offset
    1481             :    * values, as would be appropriate for finding a solution to a
    1482             :    * nonlinear problem in a quasi-Newton solve.
    1483             :    *
    1484             :    * The \p solution vector passed in should be a serialized or
    1485             :    * ghosted primal solution
    1486             :    */
    1487             :   void heterogeneously_constrain_element_residual (DenseVector<Number> & rhs,
    1488             :                                                    std::vector<dof_id_type> & elem_dofs,
    1489             :                                                    NumericVector<Number> & solution_local) const;
    1490             : 
    1491             : 
    1492             :   /**
    1493             :    * Constrains the element residual.  The element Jacobian is square,
    1494             :    * and the elem_dofs should correspond to the global DOF indices of
    1495             :    * both the rows and columns of the element matrix, and the dof
    1496             :    * constraint should not include any heterogeneous terms.
    1497             :    *
    1498             :    * The residual-constraining version of this method creates linear
    1499             :    * systems in which heterogeneously constrained degrees of freedom
    1500             :    * create non-zero residual terms when not at their correct offset
    1501             :    * values, as would be appropriate for finding a solution to a
    1502             :    * nonlinear problem in a quasi-Newton solve.
    1503             :    *
    1504             :    * The \p solution vector passed in should be a serialized or
    1505             :    * ghosted primal solution
    1506             :    */
    1507             :   void constrain_element_residual (DenseVector<Number> & rhs,
    1508             :                                    std::vector<dof_id_type> & elem_dofs,
    1509             :                                    NumericVector<Number> & solution_local) const;
    1510             : 
    1511             :   /**
    1512             :    * Constrains a dyadic element matrix B = v w'.  This method
    1513             :    * requires the element matrix to be square, in which case the
    1514             :    * elem_dofs correspond to the global DOF indices of both the rows
    1515             :    * and columns of the element matrix.  For this case the rows and
    1516             :    * columns of the matrix necessarily correspond to variables of the
    1517             :    * same approximation order.
    1518             :    */
    1519             :   void constrain_element_dyad_matrix (DenseVector<Number> & v,
    1520             :                                       DenseVector<Number> & w,
    1521             :                                       std::vector<dof_id_type> & row_dofs,
    1522             :                                       bool asymmetric_constraint_rows = true) const;
    1523             : 
    1524             :   /**
    1525             :    * Does not actually constrain anything, but modifies \p dofs in the
    1526             :    * same way as any of the constrain functions would do, i.e. adds
    1527             :    * those dofs in terms of which any of the existing dofs is
    1528             :    * constrained.
    1529             :    */
    1530             :   void constrain_nothing (std::vector<dof_id_type> & dofs) const;
    1531             : 
    1532             :   /**
    1533             :    * Constrains the numeric vector \p v, which represents a solution defined on
    1534             :    * the mesh.  This may need to be used after a linear solve, if your linear
    1535             :    * solver's solutions do not satisfy your DoF constraints to a tight enough
    1536             :    * tolerance.
    1537             :    *
    1538             :    * If \p v == nullptr, the system solution vector is constrained
    1539             :    *
    1540             :    * If \p homogeneous == true, heterogeneous constraints are enforced
    1541             :    * as if they were homogeneous.  This might be appropriate for e.g. a
    1542             :    * vector representing a difference between two
    1543             :    * heterogeneously-constrained solutions.
    1544             :    */
    1545             :   void enforce_constraints_exactly (const System & system,
    1546             :                                     NumericVector<Number> * v = nullptr,
    1547             :                                     bool homogeneous = false) const;
    1548             : 
    1549             :   /**
    1550             :    * Heterogeneously constrains the numeric vector \p v, which
    1551             :    * represents an adjoint solution defined on the mesh for quantity
    1552             :    * fo interest \p q.  For homogeneous constraints, use \p
    1553             :    * enforce_constraints_exactly instead
    1554             :    */
    1555             :   void enforce_adjoint_constraints_exactly (NumericVector<Number> & v,
    1556             :                                             unsigned int q) const;
    1557             : 
    1558             :   void enforce_constraints_on_residual (const NonlinearImplicitSystem & system,
    1559             :                                         NumericVector<Number> * rhs,
    1560             :                                         NumericVector<Number> const * solution,
    1561             :                                         bool homogeneous = true) const;
    1562             : 
    1563             :   void enforce_constraints_on_jacobian (const NonlinearImplicitSystem & system,
    1564             :                                         SparseMatrix<Number> * jac) const;
    1565             : 
    1566             : #ifdef LIBMESH_ENABLE_PERIODIC
    1567             : 
    1568             :   //--------------------------------------------------------------------
    1569             :   // PeriodicBoundary-specific methods
    1570             : 
    1571             :   /**
    1572             :    * Adds a copy of the specified periodic boundary to the system.
    1573             :    */
    1574             :   void add_periodic_boundary (const PeriodicBoundaryBase & periodic_boundary);
    1575             : 
    1576             :   /**
    1577             :    * Add a periodic boundary pair
    1578             :    *
    1579             :    * \param boundary - primary boundary
    1580             :    * \param inverse_boundary - inverse boundary
    1581             :    */
    1582             :   void add_periodic_boundary (const PeriodicBoundaryBase & boundary, const PeriodicBoundaryBase & inverse_boundary);
    1583             : 
    1584             :   /**
    1585             :    * \returns \p true if the boundary given by \p boundaryid is periodic,
    1586             :    * false otherwise
    1587             :    */
    1588             :   bool is_periodic_boundary (const boundary_id_type boundaryid) const;
    1589             : 
    1590             :   PeriodicBoundaries * get_periodic_boundaries()
    1591             :   {
    1592             :     return _periodic_boundaries.get();
    1593             :   }
    1594             : 
    1595             :   const PeriodicBoundaries * get_periodic_boundaries() const
    1596             :   {
    1597             :     return _periodic_boundaries.get();
    1598             :   }
    1599             : 
    1600             : #endif // LIBMESH_ENABLE_PERIODIC
    1601             : 
    1602             : 
    1603             : #ifdef LIBMESH_ENABLE_DIRICHLET
    1604             : 
    1605             :   //--------------------------------------------------------------------
    1606             :   // DirichletBoundary-specific methods
    1607             : 
    1608             :   /**
    1609             :    * Adds a copy of the specified Dirichlet boundary to the system.
    1610             :    *
    1611             :    * The constraints implied by DirichletBoundary objects are imposed
    1612             :    * in the same order in which DirichletBoundary objects are added to
    1613             :    * the DofMap. When multiple DirichletBoundary objects would impose
    1614             :    * competing constraints on a given DOF, the *first*
    1615             :    * DirichletBoundary to constrain the DOF "wins". This distinction
    1616             :    * is important when e.g. two surfaces (sidesets) intersect. The
    1617             :    * nodes on the intersection will be constrained according to
    1618             :    * whichever sideset's DirichletBoundary object was added to the
    1619             :    * DofMap first.
    1620             :    */
    1621             :   void add_dirichlet_boundary (const DirichletBoundary & dirichlet_boundary);
    1622             : 
    1623             :   /**
    1624             :    * Adds a copy of the specified Dirichlet boundary to the system,
    1625             :    * corresponding to the adjoint problem defined by Quantity of
    1626             :    * Interest \p q.
    1627             :    */
    1628             :   void add_adjoint_dirichlet_boundary (const DirichletBoundary & dirichlet_boundary,
    1629             :                                        unsigned int q);
    1630             : 
    1631             :   /**
    1632             :    * Removes the specified Dirichlet boundary from the system.
    1633             :    */
    1634             :   void remove_dirichlet_boundary (const DirichletBoundary & dirichlet_boundary);
    1635             : 
    1636             :   /**
    1637             :    * Removes from the system the specified Dirichlet boundary for the
    1638             :    * adjoint equation defined by Quantity of interest index q
    1639             :    */
    1640             :   void remove_adjoint_dirichlet_boundary (const DirichletBoundary & dirichlet_boundary,
    1641             :                                           unsigned int q);
    1642             : 
    1643             :   const DirichletBoundaries * get_dirichlet_boundaries() const
    1644             :   {
    1645             :     return _dirichlet_boundaries.get();
    1646             :   }
    1647             : 
    1648          24 :   DirichletBoundaries * get_dirichlet_boundaries()
    1649             :   {
    1650          24 :     return _dirichlet_boundaries.get();
    1651             :   }
    1652             : 
    1653             :   bool has_adjoint_dirichlet_boundaries(unsigned int q) const;
    1654             : 
    1655             :   const DirichletBoundaries *
    1656             :   get_adjoint_dirichlet_boundaries(unsigned int q) const;
    1657             : 
    1658             :   DirichletBoundaries *
    1659             :   get_adjoint_dirichlet_boundaries(unsigned int q);
    1660             : 
    1661             :   /**
    1662             :    * Check that all the ids in dirichlet_bcids are actually present in the mesh.
    1663             :    * If not, this will throw an error.
    1664             :    */
    1665             :   void check_dirichlet_bcid_consistency (const MeshBase & mesh,
    1666             :                                          const DirichletBoundary & boundary) const;
    1667             : #endif // LIBMESH_ENABLE_DIRICHLET
    1668             : 
    1669             : 
    1670             : #ifdef LIBMESH_ENABLE_AMR
    1671             : 
    1672             :   //--------------------------------------------------------------------
    1673             :   // AMR-specific methods
    1674             : 
    1675             :   /**
    1676             :    * After a mesh is refined and repartitioned it is possible that the
    1677             :    * \p _send_list will need to be augmented.  This is the case when an
    1678             :    * element is refined and its children end up on different processors
    1679             :    * than the parent.  These children will need values from the parent
    1680             :    * when projecting the solution onto the refined mesh, hence the parent's
    1681             :    * DOF indices need to be included in the \p _send_list.
    1682             :    */
    1683             :   // void augment_send_list_for_projection(const MeshBase &);
    1684             : 
    1685             : #ifdef LIBMESH_ENABLE_AMR
    1686             : 
    1687             :   /**
    1688             :    * Fills the vector di with the global degree of freedom indices
    1689             :    * for the element using the \p DofMap::old_dof_object.
    1690             :    * If no variable number is specified then all
    1691             :    * variables are returned.
    1692             :    */
    1693             :   void old_dof_indices (const Elem * const elem,
    1694             :                         std::vector<dof_id_type> & di,
    1695             :                         const unsigned int vn = libMesh::invalid_uint) const;
    1696             : 
    1697             : #endif // LIBMESH_ENABLE_AMR
    1698             : 
    1699             :   /**
    1700             :    * Constrains degrees of freedom on side \p s of element \p elem which
    1701             :    * correspond to variable number \p var and to p refinement levels
    1702             :    * above \p p.
    1703             :    */
    1704             :   void constrain_p_dofs (unsigned int var,
    1705             :                          const Elem * elem,
    1706             :                          unsigned int s,
    1707             :                          unsigned int p);
    1708             : 
    1709             : #endif // LIBMESH_ENABLE_AMR
    1710             : 
    1711             :   /**
    1712             :    * Reinitialize the underlying data structures conformal to the current mesh.
    1713             :    */
    1714             :   void reinit
    1715             :     (MeshBase & mesh,
    1716             :      const std::map<const Node *, std::set<subdomain_id_type>> &
    1717             :        constraining_subdomains);
    1718             : 
    1719             :   /**
    1720             :    * Free all new memory associated with the object, but restore its
    1721             :    * original state, with the mesh pointer and any default ghosting.
    1722             :    */
    1723             :   virtual void clear () override;
    1724             : 
    1725             :   /**
    1726             :    * Prints summary info about the sparsity bandwidth and constraints.
    1727             :    */
    1728             :   void print_info(std::ostream & os=libMesh::out) const;
    1729             : 
    1730             :   /**
    1731             :    * Gets summary info about the sparsity bandwidth and constraints.
    1732             :    */
    1733             :   std::string get_info() const;
    1734             : 
    1735             :   /**
    1736             :    * Degree of freedom coupling.  If left empty each DOF
    1737             :    * couples to all others.  Can be used to reduce memory
    1738             :    * requirements for sparse matrices.  DOF 0 might only
    1739             :    * couple to itself, in which case \p dof_coupling(0,0)
    1740             :    * should be 1 and \p dof_coupling(0,j) = 0 for j not equal
    1741             :    * to 0.
    1742             :    *
    1743             :    * This variable is named as though it were class private,
    1744             :    * but it is in the public interface.  Also there are no
    1745             :    * public methods for accessing it...  This typically means
    1746             :    * you should only use it if you know what you are doing.
    1747             :    */
    1748             :   CouplingMatrix * _dof_coupling;
    1749             : 
    1750             :   /**
    1751             :    * \returns The number of the system we are responsible for.
    1752             :    */
    1753             :   unsigned int sys_number() const;
    1754             : 
    1755             :   /**
    1756             :    * Builds a sparsity pattern for matrices using the current
    1757             :    * degree-of-freedom numbering and coupling.
    1758             :    *
    1759             :    * By default, ignores constraint equations, for build speed; this
    1760             :    * is valid for the combination of !need_full_sparsity_pattern and
    1761             :    * constraints which only come from periodic boundary conditions and
    1762             :    * adaptive mesh refinement, where matrix constraint adds some
    1763             :    * matrix entries but removes equally many (or more) other entries.
    1764             :    *
    1765             :    * Can be told to calculate sparsity for the constrained matrix,
    1766             :    * which may be necessary in the case of spline control node
    1767             :    * constraints or sufficiently many user constraints.
    1768             :    */
    1769             :   std::unique_ptr<SparsityPattern::Build> build_sparsity(const MeshBase & mesh,
    1770             :                                                          bool calculate_constrained = false,
    1771             :                                                          bool use_condensed_system = false) const;
    1772             : 
    1773             :   /**
    1774             :    * Set whether the given variable group should be p-refined on a
    1775             :    * p-refined Elem.  This changes the FEType of the variable group to
    1776             :    * enable or disable p-refinement.
    1777             :    */
    1778             :   void should_p_refine(unsigned int g, bool p_refine);
    1779             : 
    1780             :   /**
    1781             :    * Whether the given variable group should be p-refined
    1782             :    */
    1783             :   bool should_p_refine(unsigned int g) const;
    1784             : 
    1785             :   /**
    1786             :    * Whether the given variable should be p-refined
    1787             :    */
    1788             :   bool should_p_refine_var(unsigned int var) const;
    1789             : 
    1790             :   // Prevent bad user implicit conversions
    1791             :   void should_p_refine(FEFamily, bool) = delete;
    1792             :   void should_p_refine(Order, bool) = delete;
    1793             :   bool should_p_refine(FEFamily) const = delete;
    1794             :   bool should_p_refine(Order) const = delete;
    1795             : 
    1796             :   /**
    1797             :    * Add a static condensation class
    1798             :    */
    1799             :   void create_static_condensation(MeshBase & mesh, System & system);
    1800             : 
    1801             :   /**
    1802             :    * Checks whether we have static condensation
    1803             :    */
    1804       88545 :   bool has_static_condensation() const { return _sc.get(); }
    1805             : 
    1806             :   /**
    1807             :    * @returns the static condensation class. This should have been already added with a call to \p
    1808             :    * add_static_condensation()
    1809             :    */
    1810             :   StaticCondensationDofMap & get_static_condensation();
    1811             : 
    1812             :   /**
    1813             :    * @returns the static condensation class. This should have been already added with a call to \p
    1814             :    * add_static_condensation()
    1815             :    */
    1816             :   const StaticCondensationDofMap & get_static_condensation() const;
    1817             : 
    1818             :   /**
    1819             :    * Calls reinit on the static condensation map if it exists
    1820             :    */
    1821             :   void reinit_static_condensation();
    1822             : 
    1823             : private:
    1824             : 
    1825             :   /**
    1826             :    * Retrieve the array variable bounds for a given variable \p vi. This variable may
    1827             :    * lie anywhere within an array variable range. An 'array variable' is simply a sequence
    1828             :    * of contiguous variable numbers defined by pair where the first member of the pair
    1829             :    * is the first number in the variable sequence and the second member of the pair is
    1830             :    * the number of the last variable in the sequence plus one. Array variables may be
    1831             :    * used in tandem with variable grouping by downstream code to build optimized physics
    1832             :    * kernels since each variable in the array will have the same shape functions.
    1833             :    *
    1834             :    * We note that we store array variables as a container of the above described pairs. Within
    1835             :    * this API we will do a binary search such that the complexity is O(log(N)) where N is the
    1836             :    * number of array variables present in \p this
    1837             :    */
    1838             :   const std::pair<unsigned int, unsigned int> &
    1839             :   get_variable_array(unsigned int vi) const;
    1840             : 
    1841             :   /**
    1842             :    * Helper function that gets the dof indices on the current element
    1843             :    * for a non-SCALAR type variable, where the variable is identified
    1844             :    * by its variable group number \p vg and its offset \p vig from the
    1845             :    * first variable in that group.
    1846             :    *
    1847             :    * In DEBUG mode, the tot_size parameter will add up the total
    1848             :    * number of dof indices that should have been added to di, and v
    1849             :    * will be the variable number corresponding to vg and vig.
    1850             :    */
    1851             :   void _dof_indices (const Elem & elem,
    1852             :                      int p_level,
    1853             :                      std::vector<dof_id_type> & di,
    1854             :                      const unsigned int vg,
    1855             :                      const unsigned int vig,
    1856             :                      const Node * const * nodes,
    1857             :                      unsigned int       n_nodes,
    1858             :                      const unsigned int v
    1859             : #ifdef DEBUG
    1860             :                      ,
    1861             :                      std::size_t & tot_size
    1862             : #endif
    1863             :                     ) const;
    1864             : 
    1865             :   /**
    1866             :    * As above except a \p field_dofs_functor must be provided. This method is useful when the caller
    1867             :    * wants to do more than simply fill a degree of freedom container
    1868             :    * @param field_dofs_functor This functor has the interface:
    1869             :    *                           void field_dofs_functor(const Elem & elem,
    1870             :    *                                                   const unsigned int node_num,
    1871             :    *                                                   const unsigned int var_num,
    1872             :    *                                                   std::vector<dof_id_type> & di,
    1873             :    *                                                   const dof_id_type field_dof)
    1874             :    *                           where \p field_dof represents a field degree of freedom to act on and
    1875             :    *                           is associated with \p node_num and \p var_num. If the degree of
    1876             :    *                           freedom is elemental than \p node_num will be \p invalid_uint. \p di
    1877             :    *                           is the degree of freedom container provided to the \p _dof_indices
    1878             :    *                           method
    1879             :    */
    1880             :   template <typename FieldDofsFunctor>
    1881             :   void _dof_indices (const Elem & elem,
    1882             :                      int p_level,
    1883             :                      std::vector<dof_id_type> & di,
    1884             :                      const unsigned int vg,
    1885             :                      const unsigned int vig,
    1886             :                      const Node * const * nodes,
    1887             :                      unsigned int       n_nodes,
    1888             :                      const unsigned int v,
    1889             : #ifdef DEBUG
    1890             :                      std::size_t & tot_size,
    1891             : #endif
    1892             :                      FieldDofsFunctor field_dofs_functor) const;
    1893             : 
    1894             :   /**
    1895             :    * Helper function that implements the element-nodal versions of
    1896             :    * dof_indices and old_dof_indices
    1897             :    */
    1898             :   void _node_dof_indices (const Elem & elem,
    1899             :                           unsigned int n,
    1900             :                           const DofObject & obj,
    1901             :                           std::vector<dof_id_type> & di,
    1902             :                           const unsigned int vn) const;
    1903             : 
    1904             :   /**
    1905             :    * Invalidates all active DofObject dofs for this system
    1906             :    */
    1907             :   void invalidate_dofs(MeshBase & mesh) const;
    1908             : 
    1909             :   /**
    1910             :    * \returns The Node pointer with index \p i from the \p mesh.
    1911             :    */
    1912             :   DofObject * node_ptr(MeshBase & mesh, dof_id_type i) const;
    1913             : 
    1914             :   /**
    1915             :    * \returns The Elem pointer with index \p i from the \p mesh.
    1916             :    */
    1917             :   DofObject * elem_ptr(MeshBase & mesh, dof_id_type i) const;
    1918             : 
    1919             :   /**
    1920             :    * A member function type like \p node_ptr() or \p elem_ptr().
    1921             :    */
    1922             :   typedef DofObject * (DofMap::*dofobject_accessor)
    1923             :     (MeshBase & mesh, dof_id_type i) const;
    1924             : 
    1925             :   /**
    1926             :    * Helper function for distributing dofs in parallel
    1927             :    */
    1928             :   template<typename iterator_type>
    1929             :   void set_nonlocal_dof_objects(iterator_type objects_begin,
    1930             :                                 iterator_type objects_end,
    1931             :                                 MeshBase & mesh,
    1932             :                                 dofobject_accessor objects);
    1933             : 
    1934             :   /**
    1935             :    * We may have mesh constraint rows with dependent nodes in one
    1936             :    * subdomain but dependency nodes in another subdomain, and we may
    1937             :    * have variables whose subdomain restriction includes the dependent
    1938             :    * subdomain but not the dependency.  In those cases we need to
    1939             :    * place degrees of freedom on dependency nodes anyway.
    1940             :    *
    1941             :    * The set value for node n will include all subdomain ids of
    1942             :    * elements with nodes in subdomains constrained by n.
    1943             :    *
    1944             :    * We use a map<set> rather than a multimap here because we expect
    1945             :    * to be inserting the same subdomain multiple times and we don't
    1946             :    * need duplicate values.
    1947             :    */
    1948             :   std::map<const Node *, std::set<subdomain_id_type>>
    1949             :   calculate_constraining_subdomains();
    1950             : 
    1951             :   /**
    1952             :    * Distributes the global degrees of freedom, for dofs on
    1953             :    * this processor.  In this format the local
    1954             :    * degrees of freedom are in a contiguous block for each
    1955             :    * variable in the system.
    1956             :    * Starts at index next_free_dof, and increments it to
    1957             :    * the post-final index.
    1958             :    *
    1959             :    * Uses the provided constraining_subdomains map from
    1960             :    * calculate_constraining_subdomains() to ensure allocation of all
    1961             :    * DoFs on constraining nodes.
    1962             :    */
    1963             :   void distribute_local_dofs_var_major
    1964             :     (dof_id_type & next_free_dof,
    1965             :      MeshBase & mesh,
    1966             :      const std::map<const Node *, std::set<subdomain_id_type>> &
    1967             :        constraining_subdomains);
    1968             : 
    1969             :   /**
    1970             :    * Distributes the global degrees of freedom for dofs on this
    1971             :    * processor.  In this format all the degrees of freedom at a
    1972             :    * node/element are in contiguous blocks.  Starts at index \p
    1973             :    * next_free_dof, and increments it to the post-final index.  If \p
    1974             :    * build_send_list is \p true, builds the send list.  If \p false,
    1975             :    * clears and reserves the send list.
    1976             :    *
    1977             :    * Uses the provided constraining_subdomains map from
    1978             :    * calculate_constraining_subdomains() to ensure allocation of all
    1979             :    * DoFs on constraining nodes.
    1980             :    *
    1981             :    * \note The degrees of freedom for a given variable are not in
    1982             :    * contiguous blocks, as in the case of \p distribute_local_dofs_var_major.
    1983             :    */
    1984             :   void distribute_local_dofs_node_major
    1985             :     (dof_id_type & next_free_dof,
    1986             :      MeshBase & mesh,
    1987             :      const std::map<const Node *, std::set<subdomain_id_type>> &
    1988             :        constraining_subdomains);
    1989             : 
    1990             :   /*
    1991             :    * Helper method for the above two to count + distriubte SCALAR dofs
    1992             :    */
    1993             :   void distribute_scalar_dofs (dof_id_type & next_free_dof);
    1994             : 
    1995             : #ifdef DEBUG
    1996             :   /*
    1997             :    * Internal assertions for distribute_local_dofs_*
    1998             :    */
    1999             :   void assert_no_nodes_missed(MeshBase & mesh);
    2000             : #endif
    2001             : 
    2002             :   /*
    2003             :    * A utility method for obtaining a set of elements to ghost along
    2004             :    * with merged coupling matrices.
    2005             :    */
    2006             :   typedef std::set<std::unique_ptr<CouplingMatrix>, Utility::CompareUnderlying> CouplingMatricesSet;
    2007             :   static void
    2008             :   merge_ghost_functor_outputs (GhostingFunctor::map_type & elements_to_ghost,
    2009             :                                CouplingMatricesSet & temporary_coupling_matrices,
    2010             :                                const GhostingFunctorIterator & gf_begin,
    2011             :                                const GhostingFunctorIterator & gf_end,
    2012             :                                const MeshBase::const_element_iterator & elems_begin,
    2013             :                                const MeshBase::const_element_iterator & elems_end,
    2014             :                                processor_id_type p);
    2015             : 
    2016             :   /**
    2017             :    * Adds entries to the \p _send_list vector corresponding to DoFs
    2018             :    * on elements neighboring the current processor.
    2019             :    */
    2020             :   void add_neighbors_to_send_list(MeshBase & mesh);
    2021             : 
    2022             : #ifdef LIBMESH_ENABLE_CONSTRAINTS
    2023             : 
    2024             :   /**
    2025             :    * Build the constraint matrix C associated with the element
    2026             :    * degree of freedom indices elem_dofs. The optional parameter
    2027             :    * \p called_recursively should be left at the default value
    2028             :    * \p false.  This is used to handle the special case of
    2029             :    * an element's degrees of freedom being constrained in terms
    2030             :    * of other, local degrees of freedom.  The usual case is
    2031             :    * for an elements DOFs to be constrained by some other,
    2032             :    * external DOFs.
    2033             :    */
    2034             :   void build_constraint_matrix (DenseMatrix<Number> & C,
    2035             :                                 std::vector<dof_id_type> & elem_dofs,
    2036             :                                 const bool called_recursively=false) const;
    2037             : 
    2038             :   /**
    2039             :    * Build the constraint matrix C and the forcing vector H
    2040             :    * associated with the element degree of freedom indices elem_dofs.
    2041             :    * The optional parameter \p called_recursively should be left at
    2042             :    * the default value \p false.  This is used to handle the special
    2043             :    * case of an element's degrees of freedom being constrained in
    2044             :    * terms of other, local degrees of freedom.  The usual case is for
    2045             :    * an elements DOFs to be constrained by some other, external DOFs
    2046             :    * and/or Dirichlet conditions.
    2047             :    *
    2048             :    * The forcing vector will depend on which solution's heterogeneous
    2049             :    * constraints are being applied.  For the default \p qoi_index this
    2050             :    * will be the primal solution; for \p qoi_index >= 0 the
    2051             :    * corresponding adjoint solution's constraints will be used.
    2052             :    */
    2053             :   void build_constraint_matrix_and_vector (DenseMatrix<Number> & C,
    2054             :                                            DenseVector<Number> & H,
    2055             :                                            std::vector<dof_id_type> & elem_dofs,
    2056             :                                            int qoi_index = -1,
    2057             :                                            const bool called_recursively=false) const;
    2058             : 
    2059             :   /**
    2060             :    * Finds all the DOFS associated with the element DOFs elem_dofs.
    2061             :    * This will account for off-element couplings via hanging nodes.
    2062             :    */
    2063             :   void find_connected_dofs (std::vector<dof_id_type> & elem_dofs) const;
    2064             : 
    2065             :   /**
    2066             :    * Finds all the DofObjects associated with the set in \p objs.
    2067             :    * This will account for off-element couplings via hanging nodes.
    2068             :    */
    2069             :   void find_connected_dof_objects (std::vector<const DofObject *> & objs) const;
    2070             : 
    2071             :   /**
    2072             :    * Adds entries to the \p _send_list vector corresponding to DoFs
    2073             :    * which are dependencies for constraint equations on the current
    2074             :    * processor.
    2075             :    */
    2076             :   void add_constraints_to_send_list(const MeshBase & mesh);
    2077             : 
    2078             :   /**
    2079             :    * Adds any spline constraints from the Mesh to our DoF constraints.
    2080             :    * If any Dirichlet constraints exist on spline-constrained nodes,
    2081             :    * l2-projects those constraints onto the spline basis.
    2082             :    */
    2083             :   void process_mesh_constraint_rows(const MeshBase & mesh);
    2084             : 
    2085             : #endif // LIBMESH_ENABLE_CONSTRAINTS
    2086             : 
    2087             :   /**
    2088             :    * This flag indicates whether or not we do an opt-mode check for
    2089             :    * the presence of constraint loops, i.e. cases where the constraint
    2090             :    * graph is cyclic.
    2091             :    */
    2092             :   bool _error_on_constraint_loop;
    2093             : 
    2094             :   /**
    2095             :    * This flag indicates whether or not we explicitly take constraint
    2096             :    * equations into account when computing a sparsity pattern.
    2097             :    */
    2098             :   bool _constrained_sparsity_construction;
    2099             : 
    2100             :   /**
    2101             :    * The variables in this system/degree of freedom map
    2102             :    */
    2103             :   std::vector<Variable> _variables;
    2104             : 
    2105             :   /**
    2106             :    * The variable groups in this system/degree of freedom map
    2107             :    */
    2108             :   std::vector<VariableGroup> _variable_groups;
    2109             : 
    2110             :   /**
    2111             :    * The variable group number for each variable.
    2112             :    */
    2113             :   std::vector<unsigned int> _variable_group_numbers;
    2114             : 
    2115             :   /**
    2116             :    * A map from variable number to variable group number
    2117             :    */
    2118             :   std::unordered_map<unsigned int, unsigned int> _var_to_vg;
    2119             : 
    2120             :   /**
    2121             :    * The variable numbers corresponding to user-specified
    2122             :    * names, useful for name-based lookups.
    2123             :    */
    2124             :   std::map<std::string, unsigned int, std::less<>> _variable_numbers;
    2125             : 
    2126             :   /**
    2127             :    * Array variable information storage. For a given array "variable", the first member of the pair
    2128             :    * denotes the first variable number present in the array variable and the second member of the
    2129             :    * pair denotes the last variable number present in the array variables plus one
    2130             :    */
    2131             :   std::vector<std::pair<unsigned int, unsigned int>> _array_variables;
    2132             : 
    2133             :   /**
    2134             :    * \p true when \p VariableGroup structures should be automatically
    2135             :    * identified, \p false otherwise.  Defaults to \p true.
    2136             :    */
    2137             :   bool _identify_variable_groups = true;
    2138             : 
    2139             :   /**
    2140             :    * The number of the system we manage DOFs for.
    2141             :    */
    2142             :   const unsigned int _sys_number;
    2143             : 
    2144             :   /**
    2145             :    * The mesh that system uses.
    2146             :    */
    2147             :   MeshBase & _mesh;
    2148             : 
    2149             :   /**
    2150             :    * Additional matrices handled by this object.  These pointers do \e
    2151             :    * not handle the memory, instead, \p System, who
    2152             :    * told \p DofMap about them, owns them.
    2153             :    */
    2154             :   std::vector<SparseMatrix<Number> * > _matrices;
    2155             : 
    2156             :   /**
    2157             :    * First DOF index for SCALAR variable v, or garbage for non-SCALAR
    2158             :    * variable v
    2159             :    */
    2160             :   std::vector<dof_id_type> _first_scalar_df;
    2161             : 
    2162             :   /**
    2163             :    * A list containing all the global DOF indices that affect the
    2164             :    * solution on my processor.
    2165             :    */
    2166             :   std::vector<dof_id_type> _send_list;
    2167             : 
    2168             :   /**
    2169             :    * Function object to call to add extra entries to the sparsity pattern
    2170             :    */
    2171             :   SparsityPattern::AugmentSparsityPattern * _augment_sparsity_pattern;
    2172             : 
    2173             :   /**
    2174             :    * A function pointer to a function to call to add extra entries to the sparsity pattern
    2175             :    */
    2176             :   void (*_extra_sparsity_function)(SparsityPattern::Graph &,
    2177             :                                    std::vector<dof_id_type> & n_nz,
    2178             :                                    std::vector<dof_id_type> & n_oz,
    2179             :                                    void *);
    2180             :   /**
    2181             :    * A pointer associated with the extra sparsity that can optionally be passed in
    2182             :    */
    2183             :   void * _extra_sparsity_context;
    2184             : 
    2185             :   /**
    2186             :    * Function object to call to add extra entries to the send list
    2187             :    */
    2188             :   AugmentSendList * _augment_send_list;
    2189             : 
    2190             :   /**
    2191             :    * A function pointer to a function to call to add extra entries to the send list
    2192             :    */
    2193             :   void (*_extra_send_list_function)(std::vector<dof_id_type> &, void *);
    2194             : 
    2195             :   /**
    2196             :    * A pointer associated with the extra send list that can optionally be passed in
    2197             :    */
    2198             :   void * _extra_send_list_context;
    2199             : 
    2200             :   /**
    2201             :    * The default coupling GhostingFunctor, used to implement standard
    2202             :    * libMesh sparsity pattern construction.
    2203             :    *
    2204             :    * We use a std::unique_ptr here to reduce header dependencies.
    2205             :    */
    2206             :   std::unique_ptr<DefaultCoupling> _default_coupling;
    2207             : 
    2208             :   /**
    2209             :    * The default algebraic GhostingFunctor, used to implement standard
    2210             :    * libMesh send_list construction.
    2211             :    *
    2212             :    * We use a std::unique_ptr here to reduce header dependencies.
    2213             :    */
    2214             :   std::unique_ptr<DefaultCoupling> _default_evaluating;
    2215             : 
    2216             :   /**
    2217             :    * The list of all GhostingFunctor objects to be used when
    2218             :    * distributing ghosted vectors.
    2219             :    *
    2220             :    * The library should automatically refer these functors to the
    2221             :    * MeshBase, too, so any algebraically ghosted dofs will live on
    2222             :    * geometrically ghosted elements.
    2223             :    *
    2224             :    * Keep these in a vector so any parallel computation is done in the
    2225             :    * same order on all processors.
    2226             :    */
    2227             :   std::vector<GhostingFunctor *> _algebraic_ghosting_functors;
    2228             : 
    2229             :   /**
    2230             :    * The list of all GhostingFunctor objects to be used when
    2231             :    * coupling degrees of freedom in matrix sparsity patterns.
    2232             :    *
    2233             :    * These objects will *also* be used as algebraic ghosting functors,
    2234             :    * but not vice-versa.
    2235             :    *
    2236             :    * The library should automatically refer these functors to the
    2237             :    * MeshBase, too, so any dofs coupled to local dofs will live on
    2238             :    * geometrically ghosted elements.
    2239             :    */
    2240             :   std::vector<GhostingFunctor *> _coupling_functors;
    2241             : 
    2242             :   /**
    2243             :    * Hang on to references to any GhostingFunctor objects we were
    2244             :    * passed in shared_ptr form
    2245             :    */
    2246             :   std::map<GhostingFunctor *, std::shared_ptr<GhostingFunctor> > _shared_functors;
    2247             : 
    2248             :   /**
    2249             :    * Default false; set to true if any attached matrix requires a full
    2250             :    * sparsity pattern.
    2251             :    */
    2252             :   bool _need_full_sparsity_pattern;
    2253             : 
    2254             :   /**
    2255             :    * Default false; set to true if the dependencies of constrained ghost
    2256             :    * DOFs supported by local elements should also be ghosted
    2257             :    */
    2258             :   bool _need_ghost_constraints;
    2259             : 
    2260             :   /**
    2261             :    * The sparsity pattern of the global matrix.  If
    2262             :    * need_full_sparsity_pattern is true, we save the entire sparse
    2263             :    * graph here.  Otherwise we save just the n_nz and n_oz vectors.
    2264             :    */
    2265             :   std::unique_ptr<SparsityPattern::Build> _sp;
    2266             : 
    2267             :   /**
    2268             :    * The total number of SCALAR dofs associated to
    2269             :    * all SCALAR variables.
    2270             :    */
    2271             :   dof_id_type _n_SCALAR_dofs;
    2272             : 
    2273             : #ifdef LIBMESH_ENABLE_AMR
    2274             : 
    2275             :   /**
    2276             :    * First old DOF index for SCALAR variable v, or garbage for
    2277             :    * non-SCALAR variable v
    2278             :    */
    2279             :   std::vector<dof_id_type> _first_old_scalar_df;
    2280             : #endif
    2281             : 
    2282             : #ifdef LIBMESH_ENABLE_CONSTRAINTS
    2283             :   /**
    2284             :    * Data structure containing DOF constraints.  The ith
    2285             :    * entry is the constraint matrix row for DOF i.
    2286             :    */
    2287             :   DofConstraints _dof_constraints, _stashed_dof_constraints;
    2288             : 
    2289             :   DofConstraintValueMap      _primal_constraint_values;
    2290             : 
    2291             :   AdjointDofConstraintValues _adjoint_constraint_values;
    2292             : #endif
    2293             : 
    2294             : #ifdef LIBMESH_ENABLE_NODE_CONSTRAINTS
    2295             :   /**
    2296             :    * Data structure containing DofObject constraints.
    2297             :    */
    2298             :   NodeConstraints _node_constraints;
    2299             : #endif // LIBMESH_ENABLE_NODE_CONSTRAINTS
    2300             : 
    2301             : 
    2302             : #ifdef LIBMESH_ENABLE_PERIODIC
    2303             :   /**
    2304             :    * Data structure containing periodic boundaries.  The ith
    2305             :    * entry is the constraint matrix row for boundaryid i.
    2306             :    */
    2307             :   std::unique_ptr<PeriodicBoundaries> _periodic_boundaries;
    2308             : #endif
    2309             : 
    2310             : #ifdef LIBMESH_ENABLE_DIRICHLET
    2311             :   /**
    2312             :    * Data structure containing Dirichlet functions.  The ith
    2313             :    * entry is the constraint matrix row for boundaryid i.
    2314             :    */
    2315             :   std::unique_ptr<DirichletBoundaries> _dirichlet_boundaries;
    2316             : 
    2317             :   /**
    2318             :    * Data structure containing Dirichlet functions.  The ith
    2319             :    * entry is the constraint matrix row for boundaryid i.
    2320             :    */
    2321             :   std::vector<std::unique_ptr<DirichletBoundaries>> _adjoint_dirichlet_boundaries;
    2322             : #endif
    2323             : 
    2324             :   friend class SparsityPattern::Build;
    2325             : 
    2326             :   /**
    2327             :    * Bools to indicate if we override the --implicit_neighbor_dofs
    2328             :    * commandline options.
    2329             :    */
    2330             :   bool _implicit_neighbor_dofs_initialized;
    2331             :   bool _implicit_neighbor_dofs;
    2332             : 
    2333             :   /**
    2334             :    * Flag which determines whether we should do some additional
    2335             :    * checking of the consistency of the DirichletBoundary objects
    2336             :    * added by the user. Defaults to true, but can be disabled in cases
    2337             :    * where you only want to add DirichletBoundary objects "locally"
    2338             :    * and can guarantee that no repartitioning will be done, since
    2339             :    * repartitioning could cause processors to own new boundary sides
    2340             :    * for which they no longer have the proper DirichletBoundary
    2341             :    * objects stored.
    2342             :    */
    2343             :   bool _verify_dirichlet_bc_consistency;
    2344             : 
    2345             :   /// Static condensation class
    2346             :   std::unique_ptr<StaticCondensationDofMap> _sc;
    2347             : };
    2348             : 
    2349             : 
    2350             : // ------------------------------------------------------------
    2351             : // Dof Map inline member functions
    2352             : inline
    2353    44134145 : unsigned int DofMap::sys_number() const
    2354             : {
    2355   307892029 :   return _sys_number;
    2356             : }
    2357             : 
    2358             : 
    2359             : 
    2360             : inline
    2361    48900875 : const VariableGroup & DofMap::variable_group (const unsigned int g) const
    2362             : {
    2363    48900875 :   libmesh_assert_less (g, _variable_groups.size());
    2364             : 
    2365   316736530 :   return _variable_groups[g];
    2366             : }
    2367             : 
    2368             : 
    2369             : 
    2370             : inline
    2371    67295953 : const Variable & DofMap::variable (const unsigned int c) const
    2372             : {
    2373     6356984 :   libmesh_assert_less (c, _variables.size());
    2374             : 
    2375    72441690 :   return _variables[c];
    2376             : }
    2377             : 
    2378             : 
    2379             : 
    2380             : inline
    2381             : Order DofMap::variable_order (const unsigned int c) const
    2382             : {
    2383             :   libmesh_assert_less (c, _variables.size());
    2384             : 
    2385             :   return _variables[c].type().order;
    2386             : }
    2387             : 
    2388             : 
    2389             : 
    2390             : inline
    2391             : Order DofMap::variable_group_order (const unsigned int vg) const
    2392             : {
    2393             :   libmesh_assert_less (vg, _variable_groups.size());
    2394             : 
    2395             :   return _variable_groups[vg].type().order;
    2396             : }
    2397             : 
    2398             : 
    2399             : 
    2400             : inline
    2401     2035331 : const FEType & DofMap::variable_type (const unsigned int c) const
    2402             : {
    2403     2035331 :   libmesh_assert_less (c, _variables.size());
    2404             : 
    2405    15327009 :   return _variables[c].type();
    2406             : }
    2407             : 
    2408             : 
    2409             : 
    2410             : inline
    2411             : const FEType & DofMap::variable_group_type (const unsigned int vg) const
    2412             : {
    2413             :   libmesh_assert_less (vg, _variable_groups.size());
    2414             : 
    2415             :   return _variable_groups[vg].type();
    2416             : }
    2417             : 
    2418             : 
    2419             : #ifdef LIBMESH_ENABLE_CONSTRAINTS
    2420             : 
    2421             : 
    2422             : inline
    2423     1422047 : bool DofMap::is_constrained_node (const Node *
    2424             : #ifdef LIBMESH_ENABLE_NODE_CONSTRAINTS
    2425             :                                   node
    2426             : #endif
    2427             :                                   ) const
    2428             : {
    2429             : #ifdef LIBMESH_ENABLE_NODE_CONSTRAINTS
    2430     1422047 :   if (_node_constraints.count(node))
    2431       17527 :     return true;
    2432             : #endif
    2433             : 
    2434     1404520 :   return false;
    2435             : }
    2436             : 
    2437             : 
    2438             : inline
    2439    52879649 : bool DofMap::is_constrained_dof (const dof_id_type dof) const
    2440             : {
    2441    52879649 :   if (_dof_constraints.count(dof))
    2442     5769474 :     return true;
    2443             : 
    2444    47110175 :   return false;
    2445             : }
    2446             : 
    2447             : 
    2448             : inline
    2449       79722 : bool DofMap::has_heterogeneous_adjoint_constraints (const unsigned int qoi_num) const
    2450             : {
    2451             :   AdjointDofConstraintValues::const_iterator it =
    2452        3072 :     _adjoint_constraint_values.find(qoi_num);
    2453       82794 :   if (it == _adjoint_constraint_values.end())
    2454        1618 :     return false;
    2455       24662 :   if (it->second.empty())
    2456       23040 :     return false;
    2457             : 
    2458          14 :   return true;
    2459             : }
    2460             : 
    2461             : 
    2462             : inline
    2463     1414426 : Number DofMap::has_heterogeneous_adjoint_constraint (const unsigned int qoi_num,
    2464             :                                                     const dof_id_type dof) const
    2465             : {
    2466             :   AdjointDofConstraintValues::const_iterator it =
    2467     1093446 :     _adjoint_constraint_values.find(qoi_num);
    2468     1414426 :   if (it != _adjoint_constraint_values.end())
    2469             :     {
    2470             :       DofConstraintValueMap::const_iterator rhsit =
    2471      104428 :         it->second.find(dof);
    2472      425408 :       if (rhsit == it->second.end())
    2473      154302 :         return 0;
    2474             :       else
    2475        1820 :         return rhsit->second;
    2476             :     }
    2477             : 
    2478      989018 :   return 0;
    2479             : }
    2480             : 
    2481             : 
    2482             : 
    2483             : inline
    2484             : DofConstraintValueMap & DofMap::get_primal_constraint_values()
    2485             : {
    2486             :   return _primal_constraint_values;
    2487             : }
    2488             : 
    2489             : 
    2490             : 
    2491             : #else
    2492             : 
    2493             : //--------------------------------------------------------------------
    2494             : // Constraint-specific methods get inlined into nothing if
    2495             : // constraints are disabled, so there's no reason for users not to
    2496             : // use them.
    2497             : 
    2498             : inline void DofMap::constrain_element_matrix (DenseMatrix<Number> &,
    2499             :                                               std::vector<dof_id_type> &,
    2500             :                                               bool) const {}
    2501             : 
    2502             : inline void DofMap::constrain_element_matrix (DenseMatrix<Number> &,
    2503             :                                               std::vector<dof_id_type> &,
    2504             :                                               std::vector<dof_id_type> &,
    2505             :                                               bool) const {}
    2506             : 
    2507             : inline void DofMap::constrain_element_vector (DenseVector<Number> &,
    2508             :                                               std::vector<dof_id_type> &,
    2509             :                                               bool) const {}
    2510             : 
    2511             : inline void DofMap::constrain_element_matrix_and_vector (DenseMatrix<Number> &,
    2512             :                                                          DenseVector<Number> &,
    2513             :                                                          std::vector<dof_id_type> &,
    2514             :                                                          bool) const {}
    2515             : 
    2516             : inline void DofMap::heterogeneously_constrain_element_matrix_and_vector
    2517             :   (DenseMatrix<Number> &, DenseVector<Number> &,
    2518             :    std::vector<dof_id_type> &, bool, int) const {}
    2519             : 
    2520             : inline void DofMap::heterogeneously_constrain_element_vector
    2521             :   (const DenseMatrix<Number> &, DenseVector<Number> &,
    2522             :    std::vector<dof_id_type> &, bool, int) const {}
    2523             : 
    2524             : inline void DofMap::constrain_element_dyad_matrix (DenseVector<Number> &,
    2525             :                                                    DenseVector<Number> &,
    2526             :                                                    std::vector<dof_id_type> &,
    2527             :                                                    bool) const {}
    2528             : 
    2529             : inline void DofMap::constrain_nothing (std::vector<dof_id_type> &) const {}
    2530             : 
    2531             : inline void DofMap::enforce_constraints_exactly (const System &,
    2532             :                                                  NumericVector<Number> *,
    2533             :                                                  bool) const {}
    2534             : 
    2535             : inline void DofMap::enforce_adjoint_constraints_exactly (NumericVector<Number> &,
    2536             :                                                          unsigned int) const {}
    2537             : 
    2538             : 
    2539             : inline void DofMap::enforce_constraints_on_residual
    2540             :   (const NonlinearImplicitSystem &,
    2541             :    NumericVector<Number> *,
    2542             :    NumericVector<Number> const *,
    2543             :    bool) const {}
    2544             : 
    2545             : inline void DofMap::enforce_constraints_on_jacobian
    2546             :   (const NonlinearImplicitSystem &,
    2547             :    SparseMatrix<Number> *) const {}
    2548             : 
    2549             : #endif // LIBMESH_ENABLE_CONSTRAINTS
    2550             : 
    2551             : 
    2552             : 
    2553             : inline
    2554             : void DofMap::set_constrained_sparsity_construction(bool use_constraints)
    2555             : {
    2556             :   // This got only partly finished...
    2557             :   if (use_constraints)
    2558             :     libmesh_not_implemented();
    2559             : 
    2560             : #ifdef LIBMESH_ENABLE_CONSTRAINTS
    2561             :   _constrained_sparsity_construction = use_constraints;
    2562             : #endif
    2563             :   libmesh_ignore(use_constraints);
    2564             : }
    2565             : 
    2566             : inline
    2567             : void DofMap::full_sparsity_pattern_needed()
    2568             : {
    2569             :   _need_full_sparsity_pattern = true;
    2570             : }
    2571             : 
    2572             : inline
    2573             : void DofMap::ghost_constraints_needed()
    2574             : {
    2575             :   _need_ghost_constraints = true;
    2576             : }
    2577             : 
    2578             : inline
    2579             : bool DofMap::constrained_sparsity_construction()
    2580             : {
    2581             : #ifdef LIBMESH_ENABLE_CONSTRAINTS
    2582             :   return _constrained_sparsity_construction;
    2583             : #else
    2584             :   return true;
    2585             : #endif
    2586             : }
    2587             : 
    2588             : inline
    2589             : void DofMap::should_p_refine(const unsigned int g, const bool p_refine)
    2590             : {
    2591             : #ifdef LIBMESH_ENABLE_AMR
    2592             :   VariableGroup & var = _variable_groups[g];
    2593             :   var.type().p_refinement = p_refine;
    2594             : 
    2595             :   for (auto v : make_range(var.first_scalar_number(0),
    2596             :                            var.first_scalar_number(0) +
    2597             :                            var.n_variables()))
    2598             :     this->_variables[v].type().p_refinement = p_refine;
    2599             : 
    2600             : 
    2601             : #else
    2602             :   libmesh_ignore(g, p_refine);
    2603             : #endif
    2604             : }
    2605             : 
    2606             : inline
    2607             : bool DofMap::should_p_refine(const unsigned int g) const
    2608             : {
    2609             : #ifdef LIBMESH_ENABLE_AMR
    2610             :   const VariableGroup & var = this->variable_group(g);
    2611             :   return var.type().p_refinement;
    2612             : #else
    2613             :   libmesh_ignore(g);
    2614             :   return false;
    2615             : #endif
    2616             : }
    2617             : 
    2618             : inline
    2619             : unsigned int DofMap::var_group_from_var_number(const unsigned int var_num) const
    2620             : {
    2621             :   libmesh_assert(var_num < n_variables());
    2622             :   return libmesh_map_find(_var_to_vg, var_num);
    2623             : }
    2624             : 
    2625             : inline
    2626             : bool DofMap::should_p_refine_var(const unsigned int var) const
    2627             : {
    2628             : #ifdef LIBMESH_ENABLE_AMR
    2629             :   const auto vg = this->var_group_from_var_number(var);
    2630             :   return this->should_p_refine(vg);
    2631             : #else
    2632             :   libmesh_ignore(var);
    2633             :   return false;
    2634             : #endif
    2635             : }
    2636             : 
    2637             : template <typename FieldDofsFunctor>
    2638   273293682 : void DofMap::_dof_indices (const Elem & elem,
    2639             :                            int p_level,
    2640             :                            std::vector<dof_id_type> & di,
    2641             :                            const unsigned int vg,
    2642             :                            const unsigned int vig,
    2643             :                            const Node * const * nodes,
    2644             :                            unsigned int       n_nodes,
    2645             :                            const unsigned int v,
    2646             : #ifdef DEBUG
    2647             :                            std::size_t & tot_size,
    2648             : #endif
    2649             :                            FieldDofsFunctor field_dofs_functor) const
    2650             : {
    2651    23996472 :   const VariableGroup & var = this->variable_group(vg);
    2652             : 
    2653   273293682 :   if (var.active_on_subdomain(elem.subdomain_id()))
    2654             :     {
    2655   273155933 :       const ElemType type        = elem.type();
    2656    24049191 :       const unsigned int sys_num = this->sys_number();
    2657             : #ifdef LIBMESH_ENABLE_INFINITE_ELEMENTS
    2658    31517662 :       const bool is_inf          = elem.infinite();
    2659             : #endif
    2660             : 
    2661             :       const bool extra_hanging_dofs =
    2662   273155933 :         FEInterface::extra_hanging_dofs(var.type());
    2663             : 
    2664   273155933 :       FEType fe_type = var.type();
    2665             : 
    2666   273155933 :       const bool add_p_level = fe_type.p_refinement;
    2667             : 
    2668             : #ifdef DEBUG
    2669             :       // The number of dofs per element is non-static for subdivision FE
    2670    23986099 :       if (var.type().family == SUBDIVISION)
    2671        3936 :         tot_size += n_nodes;
    2672             :       else
    2673             :         // FIXME: Is the passed-in p_level just elem.p_level()? If so,
    2674             :         // this seems redundant.
    2675    23982163 :         tot_size += FEInterface::n_dofs(fe_type, add_p_level*p_level, &elem);
    2676             : #endif
    2677             : 
    2678             :       // The total Order is not required when getting the function
    2679             :       // pointer, it is only needed when the function is called (see
    2680             :       // below).
    2681             :       const FEInterface::n_dofs_at_node_ptr ndan =
    2682   273155933 :         FEInterface::n_dofs_at_node_function(fe_type, &elem);
    2683             : 
    2684             :       // Get the node-based DOF numbers
    2685  1632560514 :       for (unsigned int n=0; n != n_nodes; n++)
    2686             :         {
    2687  1359404581 :           const Node & node = *nodes[n];
    2688             : 
    2689             :           // Cache the intermediate lookups that are common to every
    2690             :           // component
    2691             : #ifdef DEBUG
    2692             :           const std::pair<unsigned int, unsigned int>
    2693   121190808 :             vg_and_offset = node.var_to_vg_and_offset(sys_num,v);
    2694   121190808 :           libmesh_assert_equal_to (vg, vg_and_offset.first);
    2695   121190808 :           libmesh_assert_equal_to (vig, vg_and_offset.second);
    2696             : #endif
    2697  1359404581 :           const unsigned int n_comp = node.n_comp_group(sys_num,vg);
    2698             : 
    2699             :           // There is a potential problem with h refinement.  Imagine a
    2700             :           // quad9 that has a linear FE on it.  Then, on the hanging side,
    2701             :           // it can falsely identify a DOF at the mid-edge node. This is why
    2702             :           // we go through FEInterface instead of node.n_comp() directly.
    2703  1359404581 :           const unsigned int nc =
    2704             : #ifdef LIBMESH_ENABLE_INFINITE_ELEMENTS
    2705    58167604 :             is_inf ?
    2706       82322 :             FEInterface::n_dofs_at_node(fe_type, add_p_level*p_level, &elem, n) :
    2707             : #endif
    2708  1359801368 :             ndan (type, fe_type.order + add_p_level*p_level, n);
    2709             : 
    2710             :           // If this is a non-vertex on a hanging node with extra
    2711             :           // degrees of freedom, we use the non-vertex dofs (which
    2712             :           // come in reverse order starting from the end, to
    2713             :           // simplify p refinement)
    2714  1359404581 :           if (extra_hanging_dofs && !elem.is_vertex(n))
    2715             :             {
    2716    45656782 :               const int dof_offset = n_comp - nc;
    2717             : 
    2718             :               // We should never have fewer dofs than necessary on a
    2719             :               // node unless we're getting indices on a parent element,
    2720             :               // and we should never need the indices on such a node
    2721    45656782 :               if (dof_offset < 0)
    2722             :                 {
    2723           0 :                   libmesh_assert(!elem.active());
    2724           0 :                   di.resize(di.size() + nc, DofObject::invalid_id);
    2725             :                 }
    2726             :               else
    2727   113749537 :                 for (int i=int(n_comp)-1; i>=dof_offset; i--)
    2728             :                   {
    2729     5277671 :                     const dof_id_type d =
    2730    68092755 :                       node.dof_number(sys_num, vg, vig, i, n_comp);
    2731     5277671 :                     libmesh_assert_not_equal_to (d, DofObject::invalid_id);
    2732    68092755 :                     field_dofs_functor(elem, n, v, di, d);
    2733             :                   }
    2734             :             }
    2735             :           // If this is a vertex or an element without extra hanging
    2736             :           // dofs, our dofs come in forward order coming from the
    2737             :           // beginning
    2738             :           else
    2739             :             {
    2740             :               // We have a good component index only if it's being
    2741             :               // used on this FE type (nc) *and* it's available on
    2742             :               // this DofObject (n_comp).
    2743  1313747799 :               const unsigned int good_nc = std::min(n_comp, nc);
    2744  2544954108 :               for (unsigned int i=0; i!=good_nc; ++i)
    2745             :                 {
    2746   108561053 :                   const dof_id_type d =
    2747  1230982806 :                     node.dof_number(sys_num, vg, vig, i, n_comp);
    2748   108561053 :                   libmesh_assert_not_equal_to (d, DofObject::invalid_id);
    2749   108561053 :                   libmesh_assert_less (d, this->n_dofs());
    2750  1231206309 :                   field_dofs_functor(elem, n, v, di, d);
    2751             :                 }
    2752             : 
    2753             :               // With fewer good component indices than we need, e.g.
    2754             :               // due to subdomain expansion, the remaining expected
    2755             :               // indices are marked invalid.
    2756  1313747799 :               if (n_comp < nc)
    2757           0 :                 for (unsigned int i=n_comp; i!=nc; ++i)
    2758           0 :                   di.push_back(DofObject::invalid_id);
    2759             :             }
    2760             :         }
    2761             : 
    2762             :       // If there are any element-based DOF numbers, get them
    2763   273155933 :       const unsigned int nc = FEInterface::n_dofs_per_elem(fe_type, add_p_level*p_level, &elem);
    2764             : 
    2765             :       // We should never have fewer dofs than necessary on an
    2766             :       // element unless we're getting indices on a parent element
    2767             :       // (and we should never need those indices) or off-domain for a
    2768             :       // subdomain-restricted variable (where invalid_id is the
    2769             :       // correct thing to return)
    2770   273155933 :       if (nc != 0)
    2771             :         {
    2772     1720214 :           const unsigned int n_comp = elem.n_comp_group(sys_num,vg);
    2773    19817686 :           if (elem.n_systems() > sys_num && nc <= n_comp)
    2774             :             {
    2775   108990777 :               for (unsigned int i=0; i<nc; i++)
    2776             :                 {
    2777     7791149 :                   const dof_id_type d =
    2778    89009138 :                     elem.dof_number(sys_num, vg, vig, i, n_comp);
    2779     7791149 :                   libmesh_assert_not_equal_to (d, DofObject::invalid_id);
    2780             : 
    2781    89173091 :                   field_dofs_functor(elem, invalid_uint, v, di, d);
    2782             :                 }
    2783             :             }
    2784             :           else
    2785             :             {
    2786           0 :               libmesh_assert(!elem.active() || fe_type.family == LAGRANGE || fe_type.family == SUBDIVISION);
    2787           0 :               di.resize(di.size() + nc, DofObject::invalid_id);
    2788             :             }
    2789             :         }
    2790             :     }
    2791   273293682 : }
    2792             : 
    2793             : 
    2794             : 
    2795             : template <typename ScalarDofsFunctor, typename FieldDofsFunctor>
    2796   115266307 : void DofMap::dof_indices (const Elem * const elem,
    2797             :                           std::vector<dof_id_type> & di,
    2798             :                           const unsigned int vn,
    2799             :                           ScalarDofsFunctor scalar_dofs_functor,
    2800             :                           FieldDofsFunctor field_dofs_functor,
    2801             :                           int p_level) const
    2802             : {
    2803             :   // We now allow elem==nullptr to request just SCALAR dofs
    2804             :   // libmesh_assert(elem);
    2805             : 
    2806             :   // dof_indices() is a relatively light-weight function that is
    2807             :   // called millions of times in normal codes. Therefore, it is not a
    2808             :   // good candidate for logging, since the cost of the logging code
    2809             :   // itself is roughly on par with the time required to call
    2810             :   // dof_indices().
    2811             :   // LOG_SCOPE("dof_indices()", "DofMap");
    2812             : 
    2813             :   // Clear the DOF indices vector
    2814    11025233 :   di.clear();
    2815             : 
    2816             :   // Use the default p refinement level?
    2817   115266307 :   if (p_level == -12345)
    2818   113719003 :     p_level = elem ? elem->p_level() : 0;
    2819             : 
    2820   115329431 :   const unsigned int vg = this->_variable_group_numbers[vn];
    2821    11025233 :   const VariableGroup & var = this->variable_group(vg);
    2822   115266307 :   const unsigned int vig = vn - var.number();
    2823             : 
    2824             : #ifdef DEBUG
    2825             :   // Check that sizes match in DEBUG mode
    2826    11025233 :   std::size_t tot_size = 0;
    2827             : #endif
    2828             : 
    2829   115266307 :   if (elem && elem->type() == TRI3SUBDIVISION)
    2830             :     {
    2831             :       // Subdivision surface FE require the 1-ring around elem
    2832        2712 :       const Tri3Subdivision * sd_elem = static_cast<const Tri3Subdivision *>(elem);
    2833             : 
    2834             :       // Ghost subdivision elements have no real dofs
    2835       29832 :       if (!sd_elem->is_ghost())
    2836             :         {
    2837             :           // Determine the nodes contributing to element elem
    2838        4608 :           std::vector<const Node *> elem_nodes;
    2839       25344 :           MeshTools::Subdivision::find_one_ring(sd_elem, elem_nodes);
    2840             : 
    2841       25344 :           _dof_indices(*elem, p_level, di, vg, vig, elem_nodes.data(),
    2842             :                        cast_int<unsigned int>(elem_nodes.size()), vn,
    2843             : #ifdef DEBUG
    2844             :                        tot_size,
    2845             : #endif
    2846             :                        field_dofs_functor);
    2847             :         }
    2848             : 
    2849       29832 :       return;
    2850             :     }
    2851             : 
    2852             :   // Get the dof numbers
    2853   115334430 :   if (var.type().family == SCALAR &&
    2854     1027221 :       (!elem ||
    2855     1027253 :        var.active_on_subdomain(elem->subdomain_id())))
    2856             :     {
    2857             : #ifdef DEBUG
    2858       97955 :       tot_size += var.type().order;
    2859             : #endif
    2860      195910 :       std::vector<dof_id_type> di_new;
    2861     1027253 :       this->SCALAR_dof_indices(di_new,vn);
    2862     1027253 :       scalar_dofs_functor(*elem, di, di_new);
    2863             :     }
    2864   114209222 :   else if (elem)
    2865   114209222 :     _dof_indices(*elem, p_level, di, vg, vig, elem->get_nodes(),
    2866   114209222 :                  elem->n_nodes(), vn,
    2867             : #ifdef DEBUG
    2868             :                  tot_size,
    2869             : #endif
    2870             :                  field_dofs_functor);
    2871             : 
    2872             : #ifdef DEBUG
    2873    11022521 :   libmesh_assert_equal_to (tot_size, di.size());
    2874             : #endif
    2875             : }
    2876             : 
    2877             : inline
    2878          18 : StaticCondensationDofMap & DofMap::get_static_condensation()
    2879             : {
    2880          18 :   libmesh_assert(_sc);
    2881          18 :   return *_sc;
    2882             : }
    2883             : 
    2884             : inline
    2885          28 : const StaticCondensationDofMap & DofMap::get_static_condensation() const
    2886             : {
    2887          28 :   libmesh_assert(_sc);
    2888          28 :   return *_sc;
    2889             : }
    2890             : 
    2891             : inline const std::pair<unsigned int, unsigned int> &
    2892         104 : DofMap::get_variable_array(const unsigned int vi) const
    2893             : {
    2894          88 :   auto it = std::upper_bound(
    2895             :       _array_variables.begin(),
    2896             :       _array_variables.end(),
    2897             :       vi,
    2898         136 :       [](unsigned int value, const std::pair<unsigned int, unsigned int> & b) { return value < b.first; });
    2899             : 
    2900          16 :   libmesh_assert_msg(it != _array_variables.begin(),
    2901             :                      "Passed in " << std::to_string(vi) << " is not in any of our array variables");
    2902          16 :   --it;
    2903          16 :   libmesh_assert_msg(vi < it->second,
    2904             :                      "Passed in " << std::to_string(vi) << " is not in any of our array variables");
    2905         120 :   return *it;
    2906             : }
    2907             : 
    2908             : template <typename DofIndicesFunctor>
    2909         120 : void DofMap::array_dof_indices(const DofIndicesFunctor & functor,
    2910             :                                std::vector<dof_id_type> & di,
    2911             :                                const unsigned int vn) const
    2912             : {
    2913         136 :   const auto [begin, end] = this->get_variable_array(vn);
    2914         104 :   functor(di, begin);
    2915             : 
    2916         120 :   const unsigned int count = end - begin;
    2917             :   // We make count, which could be >> ntest, the inner index in hopes of vectorization
    2918         120 :   if (count > 1)
    2919             :     {
    2920          40 :       const dof_id_type component_size = di.size();
    2921         120 :       di.resize(count * component_size);
    2922             : 
    2923        1512 :       const auto pack_container = [&di,
    2924             :                                    component_size](const unsigned int j,
    2925             :                                                    const std::vector<dof_id_type> & j_dof_indices,
    2926          32 :                                                    const unsigned int stride) {
    2927          32 :         if (&j_dof_indices != &di)
    2928           4 :           libmesh_assert(j_dof_indices.size() == component_size);
    2929        1568 :         for (const auto i : make_range(component_size))
    2930        1536 :           di[j * component_size + i] = j_dof_indices[i] + stride * j;
    2931             :       };
    2932         104 :       pack_container(0, di, 0);
    2933             : 
    2934         120 :       const auto & fe_type = _variable_groups[libmesh_map_find(_var_to_vg, vn)].type();
    2935         136 :       if (const bool lagrange = fe_type.family == LAGRANGE;
    2936         120 :           lagrange || (FEInterface::get_continuity(fe_type) == DISCONTINUOUS))
    2937             :         {
    2938          90 :           const auto stride = lagrange ? 1 : component_size;
    2939         180 :           for (const auto j : make_range((unsigned int)1, count))
    2940          78 :             pack_container(j, di, stride);
    2941             :         }
    2942             :       else
    2943             :         {
    2944          30 :           static thread_local std::vector<dof_id_type> work_dof_indices;
    2945           4 :           unsigned int j = 1;
    2946          60 :           for (const auto i : make_range(begin + 1, end))
    2947             :             {
    2948          26 :               functor(work_dof_indices, i);
    2949          30 :               pack_container(j++, work_dof_indices, 0);
    2950             :             }
    2951             :         }
    2952             :     }
    2953         120 : }
    2954             : 
    2955             : inline
    2956    10428798 : unsigned int DofMap::n_vars() const
    2957             : {
    2958    20839165 :   return cast_int<unsigned int>(_variables.size());
    2959             : }
    2960             : 
    2961             : inline
    2962     2556107 : const std::string & DofMap::variable_name (const unsigned int i) const
    2963             : {
    2964     2556107 :   libmesh_assert_less (i, _variables.size());
    2965             : 
    2966    31172273 :   return _variables[i].name();
    2967             : }
    2968             : 
    2969             : inline
    2970        1352 : bool DofMap::identify_variable_groups () const
    2971             : {
    2972       46750 :   return _identify_variable_groups;
    2973             : }
    2974             : 
    2975             : inline
    2976           0 : void DofMap::identify_variable_groups (const bool ivg)
    2977             : {
    2978           0 :   _identify_variable_groups = ivg;
    2979           0 : }
    2980             : 
    2981             : inline
    2982      266582 : unsigned int DofMap::n_components(const MeshBase & mesh) const
    2983             : {
    2984      274366 :   if (_variables.empty())
    2985        7414 :     return 0;
    2986             : 
    2987         370 :   const Variable & last = _variables.back();
    2988       13084 :   return last.first_scalar_number() + last.n_components(mesh);
    2989             : }
    2990             : 
    2991             : inline
    2992             : unsigned int
    2993      530221 : DofMap::variable_scalar_number (unsigned int var_num,
    2994             :                                 unsigned int component) const
    2995             : {
    2996     7195635 :   return _variables[var_num].first_scalar_number() + component;
    2997             : }
    2998             : 
    2999             : inline
    3000       34524 : const FEType & DofMap::variable_type (std::string_view var) const
    3001             : {
    3002       35430 :   return _variables[this->variable_number(var)].type();
    3003             : }
    3004             : 
    3005         894 : inline bool DofMap::has_variable(std::string_view var) const
    3006             : {
    3007         894 :   return _variable_numbers.count(var);
    3008             : }
    3009             : 
    3010      385573 : inline unsigned int DofMap::variable_number(std::string_view var) const
    3011             : {
    3012     9023923 :   auto var_num = libmesh_map_find(_variable_numbers, var);
    3013      385573 :   libmesh_assert_equal_to(_variables[var_num].name(), var);
    3014      385573 :   return var_num;
    3015             : }
    3016             : 
    3017             : } // namespace libMesh
    3018             : 
    3019             : #endif // LIBMESH_DOF_MAP_H

Generated by: LCOV version 1.14