LCOV - code coverage report
Current view: top level - src/systems - system_projection.C (source / functions) Hit Total Coverage
Test: libMesh/libmesh: #4667 (c50533) with base aa4ea8 Lines: 569 908 62.7 %
Date: 2026-11-17 18:48:15 Functions: 40 66 60.6 %
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             : // C++ includes
      21             : #include <vector>
      22             : #include <numeric> // std::iota
      23             : 
      24             : // Local includes
      25             : #include "libmesh/libmesh_config.h"
      26             : 
      27             : #ifdef LIBMESH_HAVE_METAPHYSICL
      28             : 
      29             : // With quad precision we need the shim function declarations to
      30             : // precede the MetaPhysicL use of them
      31             : #include "libmesh/libmesh_common.h"
      32             : #include "libmesh/compare_types.h"
      33             : 
      34             : // Template specialization declarations in here need to *precede* code
      35             : // using them.
      36             : #include "metaphysicl/dynamicsparsenumberarray_decl.h"
      37             : 
      38             : using MetaPhysicL::DynamicSparseNumberArray;
      39             : 
      40             : namespace libMesh
      41             : {
      42             : // From the perspective of libMesh gradient vectors, a DSNA is a
      43             : // scalar component
      44             : template <typename T, typename IndexType>
      45             : struct ScalarTraits<MetaPhysicL::DynamicSparseNumberArray<T,IndexType> >
      46             : {
      47             :   static const bool value = true;
      48             : };
      49             : 
      50             : // And although MetaPhysicL knows how to combine DSNA with something
      51             : // else, we need to teach libMesh too.
      52             : template <typename T, typename IndexType, typename T2>
      53             : struct CompareTypes<MetaPhysicL::DynamicSparseNumberArray<T,IndexType>, T2>
      54             : {
      55             :   typedef typename
      56             :   MetaPhysicL::DynamicSparseNumberArray
      57             :   <typename CompareTypes<T,T2>::supertype,IndexType> supertype;
      58             : };
      59             : 
      60             : template <typename T> struct TypeToSend;
      61             : 
      62             : template <typename T, typename IndexType>
      63             : struct TypeToSend<MetaPhysicL::DynamicSparseNumberArray<T,IndexType>> {
      64             :   typedef std::vector<std::pair<IndexType,T>> type;
      65             : };
      66             : 
      67             : template <typename T, typename IndexType>
      68             : const std::vector<std::pair<IndexType,T>>
      69           0 : convert_to_send(MetaPhysicL::DynamicSparseNumberArray<T,IndexType> & in)
      70             : {
      71           0 :   const std::size_t in_size = in.size();
      72           0 :   std::vector<std::pair<IndexType,T>> returnval(in_size);
      73             : 
      74           0 :   for (std::size_t i=0; i != in_size; ++i)
      75             :     {
      76           0 :       returnval[i].first = in.raw_index(i);
      77           0 :       returnval[i].second = in.raw_at(i);
      78             :     }
      79           0 :   return returnval;
      80             : }
      81             : 
      82             : template <typename SendT, typename T, typename IndexType>
      83           0 : void convert_from_receive (SendT & received,
      84             :                            MetaPhysicL::DynamicSparseNumberArray<T,IndexType> & converted)
      85             : {
      86           0 :   const std::size_t received_size = received.size();
      87           0 :   converted.resize(received_size);
      88           0 :   for (std::size_t i=0; i != received_size; ++i)
      89             :     {
      90           0 :       converted.raw_index(i) = received[i].first;
      91           0 :       converted.raw_at(i) = received[i].second;
      92             :     }
      93           0 : }
      94             : 
      95             : }
      96             : 
      97             : 
      98             : #endif
      99             : 
     100             : #include "libmesh/boundary_info.h"
     101             : #include "libmesh/dense_matrix.h"
     102             : #include "libmesh/dense_vector.h"
     103             : #include "libmesh/dof_map.h"
     104             : #include "libmesh/elem.h"
     105             : #include "libmesh/enum_to_string.h"
     106             : #include "libmesh/fe_base.h"
     107             : #include "libmesh/fe_interface.h"
     108             : #include "libmesh/generic_projector.h"
     109             : #include "libmesh/int_range.h"
     110             : #include "libmesh/libmesh_logging.h"
     111             : #include "libmesh/linear_solver.h"
     112             : #include "libmesh/mesh_base.h"
     113             : #include "libmesh/numeric_vector.h"
     114             : #include "libmesh/quadrature.h"
     115             : #include "libmesh/sparse_matrix.h"
     116             : #include "libmesh/system.h"
     117             : #include "libmesh/threads.h"
     118             : #include "libmesh/wrapped_function.h"
     119             : #include "libmesh/wrapped_functor.h"
     120             : #include "libmesh/fe_interface.h"
     121             : 
     122             : 
     123             : 
     124             : #ifdef LIBMESH_HAVE_METAPHYSICL
     125             : // Include MetaPhysicL definitions finally
     126             : #include "metaphysicl/dynamicsparsenumberarray.h"
     127             : 
     128             : // And make sure we instantiate the methods we'll need to use on them.
     129             : #include "libmesh/dense_matrix_impl.h"
     130             : 
     131             : namespace libMesh {
     132             : typedef DynamicSparseNumberArray<Real, dof_id_type> DSNAN;
     133             : 
     134             : template LIBMESH_EXPORT void
     135             : DenseMatrix<Real>::cholesky_solve(const DenseVector<DSNAN> &,
     136             :                                   DenseVector<DSNAN> &);
     137             : template LIBMESH_EXPORT void
     138             : DenseMatrix<Real>::_cholesky_back_substitute(const DenseVector<DSNAN> &,
     139             :                                              DenseVector<DSNAN> &) const;
     140             : }
     141             : #endif
     142             : 
     143             : 
     144             : 
     145             : namespace libMesh
     146             : {
     147             : 
     148             : // ------------------------------------------------------------
     149             : // Helper class definitions
     150             : 
     151             : #ifdef LIBMESH_ENABLE_AMR
     152             : 
     153             : /**
     154             :  * This class builds the send_list of old dof indices
     155             :  * whose coefficients are needed to perform a projection.
     156             :  * This may be executed in parallel on multiple threads.
     157             :  * The end result is a \p send_list vector which is
     158             :  * unsorted and may contain duplicate elements.
     159             :  * The \p unique() method can be used to sort and
     160             :  * create a unique list.
     161             :  */
     162             : 
     163       53292 : class BuildProjectionList
     164             : {
     165             : private:
     166             :   const System & system;
     167             : 
     168             : public:
     169        2006 :   BuildProjectionList (const System & system_in) :
     170       53270 :     system(system_in),
     171        4012 :     send_list()
     172        2006 :   {}
     173             : 
     174          66 :   BuildProjectionList (BuildProjectionList & other, Threads::split) :
     175          66 :     system(other.system),
     176          44 :     send_list()
     177          22 :   {}
     178             : 
     179             :   void unique();
     180             :   void operator()(const ConstElemRange & range);
     181             :   void join (const BuildProjectionList & other);
     182             :   std::vector<dof_id_type> send_list;
     183             : };
     184             : 
     185             : #endif // LIBMESH_ENABLE_AMR
     186             : 
     187             : 
     188             : /**
     189             :  * This class implements projecting an arbitrary
     190             :  * boundary function to the current mesh.  This
     191             :  * may be executed in parallel on multiple threads.
     192             :  */
     193          67 : class BoundaryProjectSolution
     194             : {
     195             : private:
     196             :   const std::set<boundary_id_type> & b;
     197             :   const std::vector<unsigned int>  & variables;
     198             :   const System                     & system;
     199             :   std::unique_ptr<FunctionBase<Number>>   f;
     200             :   std::unique_ptr<FunctionBase<Gradient>> g;
     201             :   const Parameters                 & parameters;
     202             :   NumericVector<Number>            & new_vector;
     203             : 
     204             : public:
     205          71 :   BoundaryProjectSolution (const std::set<boundary_id_type> & b_in,
     206             :                            const std::vector<unsigned int> & variables_in,
     207             :                            const System & system_in,
     208             :                            FunctionBase<Number> * f_in,
     209             :                            FunctionBase<Gradient> * g_in,
     210             :                            const Parameters & parameters_in,
     211          71 :                            NumericVector<Number> & new_v_in) :
     212          67 :     b(b_in),
     213          67 :     variables(variables_in),
     214          67 :     system(system_in),
     215          71 :     f(f_in ? f_in->clone() : std::unique_ptr<FunctionBase<Number>>()),
     216          67 :     g(g_in ? g_in->clone() : std::unique_ptr<FunctionBase<Gradient>>()),
     217          67 :     parameters(parameters_in),
     218          73 :     new_vector(new_v_in)
     219             :   {
     220           2 :     libmesh_assert(f.get());
     221          71 :     f->init();
     222          71 :     if (g.get())
     223           0 :       g->init();
     224          71 :   }
     225             : 
     226             :   BoundaryProjectSolution (const BoundaryProjectSolution & in) :
     227             :     b(in.b),
     228             :     variables(in.variables),
     229             :     system(in.system),
     230             :     f(in.f.get() ? in.f->clone() : std::unique_ptr<FunctionBase<Number>>()),
     231             :     g(in.g.get() ? in.g->clone() : std::unique_ptr<FunctionBase<Gradient>>()),
     232             :     parameters(in.parameters),
     233             :     new_vector(in.new_vector)
     234             :   {
     235             :     libmesh_assert(f.get());
     236             :     f->init();
     237             :     if (g.get())
     238             :       g->init();
     239             :   }
     240             : 
     241             :   void operator()(const ConstElemRange & range) const;
     242             : };
     243             : 
     244             : 
     245             : 
     246             : // ------------------------------------------------------------
     247             : // System implementation
     248       58017 : void System::project_vector (NumericVector<Number> & vector,
     249             :                              int is_adjoint,
     250             :                              std::optional<ConstElemRange> active_local_range,
     251             :                              std::optional<std::vector<unsigned int>> variable_numbers) const
     252             : {
     253             :   // Create a copy of the vector, which currently
     254             :   // contains the old data.
     255             :   std::unique_ptr<NumericVector<Number>>
     256       58017 :     old_vector (vector.clone());
     257             : 
     258             :   // Project the old vector to the new vector
     259      114028 :   this->project_vector (*old_vector, vector, is_adjoint, active_local_range, variable_numbers);
     260       58017 : }
     261             : 
     262             : 
     263             : /**
     264             :  * This method projects the vector
     265             :  * via L2 projections or nodal
     266             :  * interpolations on each element.
     267             :  */
     268       58017 : void System::project_vector (const NumericVector<Number> & old_v,
     269             :                              NumericVector<Number> & new_v,
     270             :                              int is_adjoint,
     271             :                              std::optional<ConstElemRange> active_local_range,
     272             :                              std::optional<std::vector<unsigned int>> variable_numbers) const
     273             : {
     274        6018 :   LOG_SCOPE ("project_vector(old,new)", "System");
     275             : 
     276             :   /**
     277             :    * This method projects a solution from an old mesh to a current, refined
     278             :    * mesh.  The input vector \p old_v gives the solution on the
     279             :    * old mesh, while the \p new_v gives the solution (to be computed)
     280             :    * on the new mesh.
     281             :    */
     282       58017 :   new_v.clear();
     283             : 
     284             : #ifdef LIBMESH_ENABLE_AMR
     285             : 
     286             :   // Resize the new vector and get a serial version.
     287        2006 :   NumericVector<Number> * new_vector_ptr = nullptr;
     288       56011 :   std::unique_ptr<NumericVector<Number>> new_vector_built;
     289             :   NumericVector<Number> * local_old_vector;
     290       56011 :   std::unique_ptr<NumericVector<Number>> local_old_vector_built;
     291        2006 :   const NumericVector<Number> * old_vector_ptr = nullptr;
     292             : 
     293             :   const ConstElemRange & range =
     294       58017 :     active_local_range ?
     295           0 :       active_local_range.value() :
     296       60023 :       this->get_mesh().active_local_element_stored_range();
     297             : 
     298             :   // If the old vector was uniprocessor, make the new
     299             :   // vector uniprocessor
     300       58017 :   if (old_v.type() == SERIAL)
     301             :     {
     302         735 :       new_v.init (this->n_dofs(), false, SERIAL);
     303           0 :       new_vector_ptr = &new_v;
     304           0 :       old_vector_ptr = &old_v;
     305             :     }
     306             : 
     307             :   // Otherwise it is a parallel, distributed vector, which
     308             :   // we need to localize.
     309       57282 :   else if (old_v.type() == PARALLEL)
     310             :     {
     311             :       // Build a send list for efficient localization
     312        1264 :       BuildProjectionList projection_list(*this);
     313       37404 :       Threads::parallel_reduce (range, projection_list);
     314             : 
     315             :       // Create a sorted, unique send_list
     316       37404 :       projection_list.unique();
     317             : 
     318       37404 :       new_v.init (this->n_dofs(), this->n_local_dofs(), false, PARALLEL);
     319       72280 :       new_vector_built = NumericVector<Number>::build(this->comm());
     320       72280 :       local_old_vector_built = NumericVector<Number>::build(this->comm());
     321        1264 :       new_vector_ptr = new_vector_built.get();
     322        1264 :       local_old_vector = local_old_vector_built.get();
     323       37404 :       new_vector_ptr->init(this->n_dofs(), this->n_local_dofs(),
     324        1264 :                            this->get_dof_map().get_send_list(), false,
     325        2528 :                            GHOSTED);
     326       37404 :       local_old_vector->init(old_v.size(), old_v.local_size(),
     327        2528 :                              projection_list.send_list, false, GHOSTED);
     328       37404 :       old_v.localize(*local_old_vector, projection_list.send_list);
     329       37404 :       local_old_vector->close();
     330        1264 :       old_vector_ptr = local_old_vector;
     331             :     }
     332       19878 :   else if (old_v.type() == GHOSTED)
     333             :     {
     334             :       // Build a send list for efficient localization
     335         742 :       BuildProjectionList projection_list(*this);
     336       19878 :       Threads::parallel_reduce (range, projection_list);
     337             : 
     338             :       // Create a sorted, unique send_list
     339       19878 :       projection_list.unique();
     340             : 
     341       19878 :       new_v.init (this->n_dofs(), this->n_local_dofs(),
     342        1484 :                   this->get_dof_map().get_send_list(), false, GHOSTED);
     343             : 
     344       38272 :       local_old_vector_built = NumericVector<Number>::build(this->comm());
     345         742 :       new_vector_ptr = &new_v;
     346         742 :       local_old_vector = local_old_vector_built.get();
     347       19878 :       local_old_vector->init(old_v.size(), old_v.local_size(),
     348        1484 :                              projection_list.send_list, false, GHOSTED);
     349       19878 :       old_v.localize(*local_old_vector, projection_list.send_list);
     350       19878 :       local_old_vector->close();
     351         742 :       old_vector_ptr = local_old_vector;
     352             :     }
     353             :   else // unknown old_v.type()
     354           0 :     libmesh_error_msg("ERROR: Unknown old_v.type() == " << old_v.type());
     355             : 
     356             :   // Note that the above will have zeroed the new_vector.
     357             :   // Just to be sure, assert that new_vector_ptr and old_vector_ptr
     358             :   // were successfully set before trying to deref them.
     359        2006 :   libmesh_assert(new_vector_ptr);
     360        2006 :   libmesh_assert(old_vector_ptr);
     361             : 
     362        2006 :   NumericVector<Number> & new_vector = *new_vector_ptr;
     363        2006 :   const NumericVector<Number> & old_vector = *old_vector_ptr;
     364             : 
     365       58017 :   const unsigned int n_variables = this->n_vars();
     366             : 
     367       58017 :   if (n_variables)
     368             :     {
     369        3796 :       std::vector<unsigned int> vars;
     370       54235 :       if (variable_numbers)
     371             :         {
     372           0 :           vars = *variable_numbers;
     373           0 :           for (auto v : vars)
     374           0 :             if (v >= n_variables)
     375           0 :               libmesh_error_msg("ERROR: variable number " << v <<
     376             :                                " out of range for system with " <<
     377             :                                n_variables << " variables.");
     378             :         }
     379             :       else
     380             :         {
     381       54235 :           vars.resize(n_variables);
     382        1898 :           std::iota(vars.begin(), vars.end(), 0);
     383             :         }
     384             : 
     385        3796 :       std::vector<unsigned int> regular_vars, vector_vars, scalar_vars;
     386      118850 :       for (auto var : vars)
     387             :       {
     388       64615 :         if (this->variable(var).type().family == SCALAR)
     389         142 :           scalar_vars.push_back(var);
     390       64473 :         else if (FEInterface::field_type(this->variable_type(var)) == TYPE_SCALAR)
     391       59290 :           regular_vars.push_back(var);
     392             :         else
     393        5183 :           vector_vars.push_back(var);
     394             :       }
     395             : 
     396        1898 :       VectorSetAction<Number> setter(new_vector);
     397             : 
     398       54235 :       if (!regular_vars.empty())
     399             :         {
     400             :           // Use a typedef to make the calling sequence for parallel_for() a bit more readable
     401             :           typedef
     402             :             GenericProjector<OldSolutionValue<Number,   &FEMContext::point_value>,
     403             :                              OldSolutionValue<Gradient, &FEMContext::point_gradient>,
     404             :                              Number, VectorSetAction<Number>> FEMProjector;
     405             : 
     406             :           OldSolutionValue<Number,   &FEMContext::point_value>
     407        3504 :             f(*this, old_vector, &regular_vars);
     408             :           OldSolutionValue<Gradient, &FEMContext::point_gradient>
     409        3504 :             g(*this, old_vector, &regular_vars);
     410             : 
     411       52556 :           FEMProjector projector(*this, f, &g, setter, regular_vars);
     412       49052 :           projector.project(range);
     413       45548 :         }
     414             : 
     415       54235 :       if (!vector_vars.empty())
     416             :         {
     417             :           typedef
     418             :             GenericProjector<OldSolutionValue<Gradient,   &FEMContext::point_value>,
     419             :                              OldSolutionValue<Tensor, &FEMContext::point_gradient>,
     420             :                              Gradient, VectorSetAction<Number>> FEMVectorProjector;
     421             : 
     422         292 :           OldSolutionValue<Gradient, &FEMContext::point_value> f_vector(*this, old_vector, &vector_vars);
     423         292 :           OldSolutionValue<Tensor, &FEMContext::point_gradient> g_vector(*this, old_vector, &vector_vars);
     424             : 
     425        5475 :           FEMVectorProjector vector_projector(*this, f_vector, &g_vector, setter, vector_vars);
     426        5183 :           vector_projector.project(range);
     427        4891 :         }
     428             : 
     429             :       // Copy the SCALAR dofs from old_vector to new_vector
     430             :       // Note: We assume that all SCALAR dofs are on the
     431             :       // processor with highest ID
     432       56133 :       if (this->processor_id() == (this->n_processors()-1))
     433             :         {
     434         949 :           const DofMap & dof_map = this->get_dof_map();
     435        9436 :           for (auto var : scalar_vars)
     436             :             {
     437             :               // We can just map SCALAR dofs directly across
     438           4 :               std::vector<dof_id_type> new_SCALAR_indices, old_SCALAR_indices;
     439          24 :               dof_map.SCALAR_dof_indices (new_SCALAR_indices, var, false);
     440          24 :               dof_map.SCALAR_dof_indices (old_SCALAR_indices, var, true);
     441          48 :               for (auto i : index_range(new_SCALAR_indices))
     442          26 :                 new_vector.set(new_SCALAR_indices[i], old_vector(old_SCALAR_indices[i]));
     443             :             }
     444             :         }
     445             :     }
     446             : 
     447       58017 :   new_vector.close();
     448             : 
     449             :   // If the old vector was serial, we probably need to send our values
     450             :   // to other processors
     451             :   //
     452             :   // FIXME: I'm not sure how to make a NumericVector do that without
     453             :   // creating a temporary parallel vector to use localize! - RHS
     454       58017 :   if (old_v.type() == SERIAL)
     455             :     {
     456         735 :       std::unique_ptr<NumericVector<Number>> dist_v = NumericVector<Number>::build(this->comm());
     457         735 :       dist_v->init(this->n_dofs(), this->n_local_dofs(), false, PARALLEL);
     458         735 :       dist_v->close();
     459             : 
     460      625585 :       for (auto i : make_range(dist_v->size()))
     461      624115 :         if (new_vector(i) != 0.0)
     462      489129 :           dist_v->set(i, new_vector(i));
     463             : 
     464         735 :       dist_v->close();
     465             : 
     466         735 :       dist_v->localize (new_v, this->get_dof_map().get_send_list());
     467         735 :       new_v.close();
     468         735 :     }
     469             :   // If the old vector was parallel, we need to update it
     470             :   // and free the localized copies
     471       57282 :   else if (old_v.type() == PARALLEL)
     472             :     {
     473             :       // We may have to set dof values that this processor doesn't
     474             :       // own in certain special cases, like LAGRANGE FIRST or
     475             :       // HERMITE THIRD elements on second-order meshes?
     476       37404 :       new_v = new_vector;
     477       37404 :       new_v.close();
     478             :     }
     479             : 
     480             : 
     481             :   // Apply constraints only if we we are asked to
     482       58017 :   if(this->project_with_constraints)
     483             :   {
     484       54183 :     if (is_adjoint == -1)
     485             :     {
     486       50998 :       this->get_dof_map().enforce_constraints_exactly(*this, &new_v);
     487             :     }
     488        3185 :     else if (is_adjoint >= 0)
     489             :     {
     490        3185 :       this->get_dof_map().enforce_adjoint_constraints_exactly(new_v,
     491             :                                                             is_adjoint);
     492             :     }
     493             :   }
     494             : #else
     495             : 
     496             :   // AMR is disabled: simply copy the vector
     497             :   new_v = old_v;
     498             : 
     499             :   libmesh_ignore(is_adjoint, active_local_range, variable_numbers);
     500             : 
     501             : #endif // #ifdef LIBMESH_ENABLE_AMR
     502       58017 : }
     503             : 
     504             : 
     505             : #ifdef LIBMESH_ENABLE_AMR
     506             : #ifdef LIBMESH_HAVE_METAPHYSICL
     507             : 
     508             : template <typename Output>
     509             : class DSNAOutput
     510             : {
     511             : public:
     512             :   typedef DynamicSparseNumberArray<Output, dof_id_type> type;
     513             : };
     514             : 
     515             : template <typename InnerOutput>
     516             : class DSNAOutput<VectorValue<InnerOutput> >
     517             : {
     518             : public:
     519             :   typedef VectorValue<DynamicSparseNumberArray<InnerOutput, dof_id_type> > type;
     520             : };
     521             : 
     522             : /**
     523             :  * The OldSolutionCoefs input functor class can be used with
     524             :  * GenericProjector to read solution transfer coefficients on a
     525             :  * just-refined-and-coarsened mesh.
     526             :  *
     527             :  * \author Roy H. Stogner
     528             :  * \date 2017
     529             :  */
     530             : 
     531             : template <typename Output,
     532             :           void (FEMContext::*point_output) (unsigned int,
     533             :                                             const Point &,
     534             :                                             Output &,
     535             :                                             const Real) const>
     536        1206 : class OldSolutionCoefs : public OldSolutionBase<Output, point_output>
     537             : {
     538             : public:
     539             :   typedef typename DSNAOutput<Output>::type DSNA;
     540             :   typedef DSNA ValuePushType;
     541             :   typedef DSNA FunctorValue;
     542             : 
     543        1260 :   OldSolutionCoefs(const libMesh::System & sys_in,
     544             :                    const std::vector<unsigned int> * vars) :
     545         648 :     OldSolutionBase<Output, point_output>(sys_in, vars)
     546             :   {
     547          36 :     this->old_context.set_algebraic_type(FEMContext::OLD_DOFS_ONLY);
     548          36 :   }
     549             : 
     550        3150 :   OldSolutionCoefs(const OldSolutionCoefs & in) :
     551        3240 :     OldSolutionBase<Output, point_output>(in.sys, in.old_context.active_vars())
     552             :   {
     553          90 :     this->old_context.set_algebraic_type(FEMContext::OLD_DOFS_ONLY);
     554          90 :   }
     555             : 
     556             :   DSNA eval_at_node (const FEMContext & c,
     557             :                      unsigned int i,
     558             :                      unsigned int elem_dim,
     559             :                      const Node & n,
     560             :                      bool extra_hanging_dofs,
     561             :                      Real /* time */ = 0.);
     562             : 
     563             :   DSNA eval_at_point(const FEMContext & c,
     564             :                      unsigned int i,
     565             :                      const Point & p,
     566             :                      Real time,
     567             :                      bool skip_context_check);
     568             : 
     569           0 :   void eval_mixed_derivatives (const FEMContext & libmesh_dbg_var(c),
     570             :                                unsigned int i,
     571             :                                unsigned int dim,
     572             :                                const Node & n,
     573             :                                std::vector<DSNA> & derivs)
     574             :   {
     575           0 :     LOG_SCOPE ("eval_mixed_derivatives", "OldSolutionCoefs");
     576             : 
     577             :     // This should only be called on vertices
     578           0 :     libmesh_assert_less(c.get_elem().get_node_index(&n),
     579             :                         c.get_elem().n_vertices());
     580             : 
     581             :     // Handle offset from non-scalar components in previous variables
     582           0 :     libmesh_assert_less(i, this->component_to_var.size());
     583           0 :     unsigned int var = this->component_to_var[i];
     584             : 
     585             :     // We have 1 mixed derivative in 2D, 4 in 3D
     586           0 :     const unsigned int n_mixed = (dim-1) * (dim-1);
     587           0 :     derivs.resize(n_mixed);
     588             : 
     589             :     // Be sure to handle cases where the variable wasn't defined on
     590             :     // this node (e.g. due to changing subdomain support)
     591           0 :     const DofObject * old_dof_object = n.get_old_dof_object();
     592           0 :     if (old_dof_object &&
     593           0 :         old_dof_object->n_vars(this->sys.number()) &&
     594           0 :         old_dof_object->n_comp(this->sys.number(), var))
     595             :       {
     596           0 :         const dof_id_type first_old_id =
     597           0 :           old_dof_object->dof_number(this->sys.number(), var, dim);
     598           0 :         std::vector<dof_id_type> old_ids(n_mixed);
     599           0 :         std::iota(old_ids.begin(), old_ids.end(), first_old_id);
     600             : 
     601           0 :         for (auto d_i : index_range(derivs))
     602             :           {
     603           0 :             derivs[d_i].resize(1);
     604           0 :             derivs[d_i].raw_at(0) = 1;
     605           0 :             derivs[d_i].raw_index(0) = old_ids[d_i];
     606             :           }
     607             :       }
     608             :     else
     609             :       {
     610           0 :         std::fill(derivs.begin(), derivs.end(), 0);
     611             :       }
     612           0 :   }
     613             : 
     614             : 
     615           0 :   void eval_old_dofs (const Elem & elem,
     616             :                       unsigned int node_num,
     617             :                       unsigned int var_num,
     618             :                       std::vector<dof_id_type> & indices,
     619             :                       std::vector<DSNA> & values)
     620             :   {
     621           0 :     LOG_SCOPE ("eval_old_dofs(node)", "OldSolutionCoefs");
     622             : 
     623             :     // We may be reusing a std::vector here, but the following
     624             :     // dof_indices call appends without first clearing.
     625           0 :     indices.clear();
     626             : 
     627           0 :     this->sys.get_dof_map().dof_indices(elem, node_num, indices, var_num);
     628             : 
     629           0 :     std::vector<dof_id_type> old_indices;
     630             : 
     631           0 :     this->sys.get_dof_map().old_dof_indices(elem, node_num, old_indices, var_num);
     632             : 
     633           0 :     libmesh_assert_equal_to (old_indices.size(), indices.size());
     634             : 
     635           0 :     values.resize(old_indices.size());
     636             : 
     637           0 :     for (auto i : index_range(values))
     638             :       {
     639           0 :         values[i].resize(1);
     640           0 :         values[i].raw_at(0) = 1;
     641           0 :         values[i].raw_index(0) = old_indices[i];
     642             :       }
     643           0 :   }
     644             : 
     645             : 
     646           0 :   void eval_old_dofs (const Elem & elem,
     647             :                       const FEType & fe_type,
     648             :                       unsigned int sys_num,
     649             :                       unsigned int var_num,
     650             :                       std::vector<dof_id_type> & indices,
     651             :                       std::vector<DSNA> & values)
     652             :   {
     653           0 :     LOG_SCOPE ("eval_old_dofs(elem)", "OldSolutionCoefs");
     654             : 
     655             :     // We're only to be asked for old dofs on elements that can copy
     656             :     // them through DO_NOTHING or through refinement.
     657           0 :     const Elem & old_elem =
     658           0 :       (elem.refinement_flag() == Elem::JUST_REFINED) ?
     659           0 :       *elem.parent() : elem;
     660             : 
     661             :     // If there are any element-based DOF numbers, get them
     662           0 :     const unsigned int nc =
     663           0 :       FEInterface::n_dofs_per_elem(fe_type, &elem);
     664             : 
     665           0 :     std::vector<dof_id_type> old_dof_indices(nc);
     666           0 :     indices.resize(nc);
     667             : 
     668             :     // We should never have fewer dofs than necessary on an
     669             :     // element unless we're getting indices on a parent element,
     670             :     // and we should never need those indices
     671           0 :     if (nc != 0)
     672             :       {
     673           0 :         const DofObject & old_dof_object = old_elem.get_old_dof_object_ref();
     674             : 
     675           0 :         const auto [vg, vig] =
     676           0 :           elem.var_to_vg_and_offset(sys_num,var_num);
     677             : 
     678           0 :         const unsigned int n_comp = elem.n_comp_group(sys_num,vg);
     679           0 :         libmesh_assert_greater(elem.n_systems(), sys_num);
     680           0 :         libmesh_assert_greater_equal(n_comp, nc);
     681             : 
     682           0 :         for (unsigned int i=0; i<nc; i++)
     683             :           {
     684           0 :             const dof_id_type d_old =
     685           0 :               old_dof_object.dof_number(sys_num, vg, vig, i, n_comp);
     686           0 :             const dof_id_type d_new =
     687           0 :               elem.dof_number(sys_num, vg, vig, i, n_comp);
     688           0 :             libmesh_assert_not_equal_to (d_old, DofObject::invalid_id);
     689           0 :             libmesh_assert_not_equal_to (d_new, DofObject::invalid_id);
     690             : 
     691           0 :             old_dof_indices[i] = d_old;
     692           0 :             indices[i] = d_new;
     693             :           }
     694             :       }
     695             : 
     696           0 :     values.resize(old_dof_indices.size());
     697             : 
     698           0 :     for (auto i : index_range(values))
     699             :       {
     700           0 :         values[i].resize(1);
     701           0 :         values[i].raw_at(0) = 1;
     702           0 :         values[i].raw_index(0) = old_dof_indices[i];
     703             :       }
     704           0 :   }
     705             : };
     706             : 
     707             : 
     708             : 
     709             : template<>
     710             : inline
     711             : DynamicSparseNumberArray<Real, dof_id_type>
     712      119439 : OldSolutionCoefs<Real, &FEMContext::point_value>::
     713             : eval_at_point(const FEMContext & c,
     714             :               unsigned int i,
     715             :               const Point & p,
     716             :               Real /* time */,
     717             :               bool skip_context_check)
     718             : {
     719       31056 :   LOG_SCOPE ("eval_at_point()", "OldSolutionCoefs");
     720             : 
     721      119439 :   if (!skip_context_check)
     722      119439 :     if (!this->check_old_context(c, p))
     723           0 :       return 0;
     724             : 
     725             :   // Get finite element object
     726       10352 :   FEGenericBase<Real> * fe = nullptr;
     727             :   this->old_context.get_element_fe<Real>
     728       10352 :     (i, fe, this->old_context.get_elem_dim());
     729             : 
     730             :   // Build a FE for calculating phi(p)
     731             :   FEGenericBase<Real> * fe_new =
     732      119439 :     this->old_context.build_new_fe(fe, p);
     733             : 
     734             :   // Get the values and global indices of the shape functions
     735       10352 :   const std::vector<std::vector<Real> > & phi = fe_new->get_phi();
     736             :   const std::vector<dof_id_type> & dof_indices =
     737       10352 :     this->old_context.get_dof_indices(i);
     738             : 
     739       20704 :   const std::size_t n_dofs = phi.size();
     740       10352 :   libmesh_assert_equal_to(n_dofs, dof_indices.size());
     741             : 
     742       20704 :   DynamicSparseNumberArray<Real, dof_id_type> returnval;
     743      109087 :   returnval.resize(n_dofs);
     744             : 
     745     1116532 :   for (auto j : index_range(phi))
     746             :     {
     747      997093 :       returnval.raw_at(j) = phi[j][0];
     748     1170881 :       returnval.raw_index(j) = dof_indices[j];
     749             :     }
     750             : 
     751       10352 :   return returnval;
     752             : }
     753             : 
     754             : 
     755             : 
     756             : template<>
     757             : inline
     758             : VectorValue<DynamicSparseNumberArray<Real, dof_id_type> >
     759           0 : OldSolutionCoefs<RealGradient, &FEMContext::point_gradient>::
     760             : eval_at_point(const FEMContext & c,
     761             :               unsigned int i,
     762             :               const Point & p,
     763             :               Real /* time */,
     764             :               bool skip_context_check)
     765             : {
     766           0 :   LOG_SCOPE ("eval_at_point()", "OldSolutionCoefs");
     767             : 
     768           0 :   if (!skip_context_check)
     769           0 :     if (!this->check_old_context(c, p))
     770           0 :       return 0;
     771             : 
     772             :   // Get finite element object
     773           0 :   FEGenericBase<Real> * fe = nullptr;
     774             :   this->old_context.get_element_fe<Real>
     775           0 :     (i, fe, this->old_context.get_elem_dim());
     776             : 
     777             :   // Build a FE for calculating phi(p)
     778             :   FEGenericBase<Real> * fe_new =
     779           0 :     this->old_context.build_new_fe(fe, p);
     780             : 
     781             :   // Get the values and global indices of the shape functions
     782           0 :   const std::vector<std::vector<RealGradient> > & dphi = fe_new->get_dphi();
     783             :   const std::vector<dof_id_type> & dof_indices =
     784           0 :     this->old_context.get_dof_indices(i);
     785             : 
     786           0 :   const std::size_t n_dofs = dphi.size();
     787           0 :   libmesh_assert_equal_to(n_dofs, dof_indices.size());
     788             : 
     789           0 :   VectorValue<DynamicSparseNumberArray<Real, dof_id_type> > returnval;
     790             : 
     791           0 :   for (unsigned int d = 0; d != LIBMESH_DIM; ++d)
     792           0 :     returnval(d).resize(n_dofs);
     793             : 
     794           0 :   for (auto j : index_range(dphi))
     795           0 :     for (int d = 0; d != LIBMESH_DIM; ++d)
     796             :       {
     797           0 :         returnval(d).raw_at(j) = dphi[j][0](d);
     798           0 :         returnval(d).raw_index(j) = dof_indices[j];
     799             :       }
     800             : 
     801           0 :   return returnval;
     802             : }
     803             : 
     804             : 
     805             : template<>
     806             : inline
     807             : DynamicSparseNumberArray<Real, dof_id_type>
     808       62759 : OldSolutionCoefs<Real, &FEMContext::point_value>::
     809             : eval_at_node(const FEMContext & c,
     810             :              unsigned int i,
     811             :              unsigned int /* elem_dim */,
     812             :              const Node & n,
     813             :              bool extra_hanging_dofs,
     814             :              Real /* time */)
     815             : {
     816       15309 :   LOG_SCOPE ("Real eval_at_node()", "OldSolutionCoefs");
     817             : 
     818             :   // Optimize for the common case, where this node was part of the
     819             :   // old solution.
     820             :   //
     821             :   // Be sure to handle cases where the variable wasn't defined on
     822             :   // this node (due to changing subdomain support) or where the
     823             :   // variable has no components on this node (due to Elem order
     824             :   // exceeding FE order) or where the old_dof_object dofs might
     825             :   // correspond to non-vertex dofs (due to extra_hanging_dofs and
     826             :   // refinement)
     827             : 
     828       10206 :   const Elem::RefinementState flag = c.get_elem().refinement_flag();
     829             : 
     830        5103 :   const DofObject * old_dof_object = n.get_old_dof_object();
     831       66707 :   if (old_dof_object &&
     832       66639 :       (!extra_hanging_dofs ||
     833       56569 :        flag == Elem::JUST_COARSENED ||
     834       61604 :        flag == Elem::DO_NOTHING) &&
     835      185967 :       old_dof_object->n_vars(sys.number()) &&
     836       61604 :       old_dof_object->n_comp(sys.number(), i))
     837             :     {
     838        7658 :       DynamicSparseNumberArray<Real, dof_id_type> returnval;
     839             :       const dof_id_type old_id =
     840       46874 :         old_dof_object->dof_number(sys.number(), i, 0);
     841       43045 :       returnval.resize(1);
     842       46874 :       returnval.raw_at(0) = 1;
     843       46874 :       returnval.raw_index(0) = old_id;
     844        3829 :       return returnval;
     845             :     }
     846             : 
     847       15885 :   return this->eval_at_point(c, i, n, 0, false);
     848             : }
     849             : 
     850             : 
     851             : 
     852             : template<>
     853             : inline
     854             : VectorValue<DynamicSparseNumberArray<Real, dof_id_type> >
     855           0 : OldSolutionCoefs<RealGradient, &FEMContext::point_gradient>::
     856             : eval_at_node(const FEMContext & c,
     857             :              unsigned int i,
     858             :              unsigned int elem_dim,
     859             :              const Node & n,
     860             :              bool extra_hanging_dofs,
     861             :              Real /* time */)
     862             : {
     863           0 :   LOG_SCOPE ("RealGradient eval_at_node()", "OldSolutionCoefs");
     864             : 
     865             :   // Optimize for the common case, where this node was part of the
     866             :   // old solution.
     867             :   //
     868             :   // Be sure to handle cases where the variable wasn't defined on
     869             :   // this node (due to changing subdomain support) or where the
     870             :   // variable has no components on this node (due to Elem order
     871             :   // exceeding FE order) or where the old_dof_object dofs might
     872             :   // correspond to non-vertex dofs (due to extra_hanging_dofs and
     873             :   // refinement)
     874             : 
     875           0 :   const Elem::RefinementState flag = c.get_elem().refinement_flag();
     876             : 
     877           0 :   const DofObject * old_dof_object = n.get_old_dof_object();
     878           0 :   if (old_dof_object &&
     879           0 :       (!extra_hanging_dofs ||
     880           0 :        flag == Elem::JUST_COARSENED ||
     881           0 :        flag == Elem::DO_NOTHING) &&
     882           0 :       old_dof_object->n_vars(sys.number()) &&
     883           0 :       old_dof_object->n_comp(sys.number(), i))
     884             :     {
     885           0 :       VectorValue<DynamicSparseNumberArray<Real, dof_id_type> > g;
     886           0 :       for (unsigned int d = 0; d != elem_dim; ++d)
     887             :         {
     888             :           const dof_id_type old_id =
     889           0 :             old_dof_object->dof_number(sys.number(), i, d+1);
     890           0 :           g(d).resize(1);
     891           0 :           g(d).raw_at(0) = 1;
     892           0 :           g(d).raw_index(0) = old_id;
     893             :         }
     894           0 :       return g;
     895             :     }
     896             : 
     897           0 :   return this->eval_at_point(c, i, n, 0, false);
     898             : }
     899             : 
     900             : 
     901             : 
     902             : /**
     903             :  * The MatrixFillAction output functor class can be used with
     904             :  * GenericProjector to write solution transfer coefficients into a
     905             :  * sparse matrix.
     906             :  *
     907             :  * \author Roy H. Stogner
     908             :  * \date 2017
     909             :  */
     910             : template <typename ValIn, typename ValOut>
     911             : class MatrixFillAction
     912             : {
     913             : public:
     914             :   typedef DynamicSparseNumberArray<ValIn, dof_id_type> InsertInput;
     915             : private:
     916             :   SparseMatrix<ValOut> & target_matrix;
     917             : 
     918             : public:
     919         630 :   MatrixFillAction(SparseMatrix<ValOut> & target_mat) :
     920         630 :     target_matrix(target_mat) {}
     921             : 
     922      166313 :   void insert(dof_id_type id,
     923             :               const DynamicSparseNumberArray<ValIn, dof_id_type> & val)
     924             :   {
     925             :     // Lock the target matrix since it is shared among threads.
     926             :     {
     927       28362 :       Threads::spin_mutex::scoped_lock lock(Threads::spin_mtx);
     928             : 
     929       14181 :       const std::size_t dnsa_size = val.size();
     930     1210280 :       for (unsigned int j = 0; j != dnsa_size; ++j)
     931             :         {
     932     1043967 :           const dof_id_type dof_j = val.raw_index(j);
     933     1043967 :           const ValIn dof_val = val.raw_at(j);
     934     1043967 :           target_matrix.set(id, dof_j, dof_val);
     935             :         }
     936             :     }
     937      166313 :   }
     938             : 
     939             : 
     940           0 :   void insert(const std::vector<dof_id_type> & dof_indices,
     941             :               const std::vector<DynamicSparseNumberArray<ValIn, dof_id_type> > & Ue)
     942             :   {
     943             :     const numeric_index_type
     944           0 :       begin_dof = target_matrix.row_start(),
     945           0 :       end_dof = target_matrix.row_stop();
     946             : 
     947           0 :     unsigned int size = Ue.size();
     948             : 
     949           0 :     libmesh_assert_equal_to(size, dof_indices.size());
     950             : 
     951             :     // Lock the target matrix since it is shared among threads.
     952             :     {
     953           0 :       Threads::spin_mutex::scoped_lock lock(Threads::spin_mtx);
     954             : 
     955           0 :       for (unsigned int i = 0; i != size; ++i)
     956             :         {
     957           0 :           const dof_id_type dof_i = dof_indices[i];
     958           0 :           if ((dof_i >= begin_dof) && (dof_i < end_dof))
     959             :             {
     960           0 :               const DynamicSparseNumberArray<ValIn,dof_id_type> & dnsa = Ue[i];
     961           0 :               const std::size_t dnsa_size = dnsa.size();
     962           0 :               for (unsigned int j = 0; j != dnsa_size; ++j)
     963             :                 {
     964           0 :                   const dof_id_type dof_j = dnsa.raw_index(j);
     965           0 :                   const ValIn dof_val = dnsa.raw_at(j);
     966           0 :                   target_matrix.set(dof_i, dof_j, dof_val);
     967             :                 }
     968             :             }
     969             :         }
     970             :     }
     971           0 :   }
     972             : };
     973             : 
     974             : 
     975             : 
     976             : /**
     977             :  * This method creates a projection matrix which corresponds to the
     978             :  * operation of project_vector between old and new solution spaces.
     979             :  */
     980         630 : void System::projection_matrix (SparseMatrix<Number> & proj_mat) const
     981             : {
     982          54 :   LOG_SCOPE ("projection_matrix()", "System");
     983             : 
     984         630 :   const unsigned int n_variables = this->n_vars();
     985             : 
     986         630 :   if (n_variables)
     987             :     {
     988         648 :       std::vector<unsigned int> vars(n_variables);
     989          18 :       std::iota(vars.begin(), vars.end(), 0);
     990             : 
     991             :       // Use a typedef to make the calling sequence for parallel_for() a bit more readable
     992             :       typedef OldSolutionCoefs<Real, &FEMContext::point_value> OldSolutionValueCoefs;
     993             :       typedef OldSolutionCoefs<RealGradient, &FEMContext::point_gradient> OldSolutionGradientCoefs;
     994             : 
     995             :       typedef
     996             :         GenericProjector<OldSolutionValueCoefs,
     997             :                          OldSolutionGradientCoefs,
     998             :                          DynamicSparseNumberArray<Real,dof_id_type>,
     999             :                          MatrixFillAction<Real, Number> > ProjMatFiller;
    1000             : 
    1001          36 :       OldSolutionValueCoefs    f(*this, &vars);
    1002          36 :       OldSolutionGradientCoefs g(*this, &vars);
    1003          18 :       MatrixFillAction<Real, Number> setter(proj_mat);
    1004             : 
    1005         666 :       ProjMatFiller mat_filler(*this, f, &g, setter, vars);
    1006         630 :       mat_filler.project(this->get_mesh().active_local_element_stored_range());
    1007             : 
    1008             :       // Set the SCALAR dof transfer entries too.
    1009             :       // Note: We assume that all SCALAR dofs are on the
    1010             :       // processor with highest ID
    1011         648 :       if (this->processor_id() == (this->n_processors()-1))
    1012             :         {
    1013           9 :           const DofMap & dof_map = this->get_dof_map();
    1014         376 :           for (auto var : make_range(this->n_vars()))
    1015         187 :             if (this->variable(var).type().family == SCALAR)
    1016             :               {
    1017             :                 // We can just map SCALAR dofs directly across
    1018           0 :                 std::vector<dof_id_type> new_SCALAR_indices, old_SCALAR_indices;
    1019           0 :                 dof_map.SCALAR_dof_indices (new_SCALAR_indices, var, false);
    1020           0 :                 dof_map.SCALAR_dof_indices (old_SCALAR_indices, var, true);
    1021             :                 const unsigned int new_n_dofs =
    1022           0 :                   cast_int<unsigned int>(new_SCALAR_indices.size());
    1023             : 
    1024           0 :                 for (unsigned int i=0; i<new_n_dofs; i++)
    1025             :                   {
    1026           0 :                     proj_mat.set( new_SCALAR_indices[i],
    1027           0 :                                   old_SCALAR_indices[i], 1);
    1028             :                   }
    1029             :               }
    1030             :         }
    1031         594 :     }
    1032         630 : }
    1033             : #endif // LIBMESH_HAVE_METAPHYSICL
    1034             : #endif // LIBMESH_ENABLE_AMR
    1035             : 
    1036             : 
    1037             : 
    1038             : /**
    1039             :  * This method projects an arbitrary function onto the solution via L2
    1040             :  * projections and nodal interpolations on each element.
    1041             :  */
    1042      327806 : void System::project_solution (ValueFunctionPointer fptr,
    1043             :                                GradientFunctionPointer gptr,
    1044             :                                const Parameters & function_parameters,
    1045             :                                std::optional<ConstElemRange> active_local_range,
    1046             :                                std::optional<std::vector<unsigned int>> variable_numbers) const
    1047             : {
    1048       18468 :   WrappedFunction<Number> f(*this, fptr, &function_parameters);
    1049        9234 :   WrappedFunction<Gradient> g(*this, gptr, &function_parameters);
    1050      646378 :   this->project_solution(&f, &g, active_local_range, variable_numbers);
    1051      327806 : }
    1052             : 
    1053             : 
    1054             : /**
    1055             :  * This method projects an arbitrary function onto the solution via L2
    1056             :  * projections and nodal interpolations on each element.
    1057             :  */
    1058      328379 : void System::project_solution (FunctionBase<Number> * f,
    1059             :                                FunctionBase<Gradient> * g,
    1060             :                                std::optional<ConstElemRange> active_local_range,
    1061             :                                std::optional<std::vector<unsigned int>> variable_numbers) const
    1062             : {
    1063      647575 :   this->project_vector(*solution, f, g, /*is_adjoint=*/-1, active_local_range, variable_numbers);
    1064             : 
    1065      337631 :   solution->localize(*current_local_solution, _dof_map->get_send_list());
    1066      328379 : }
    1067             : 
    1068             : 
    1069             : /**
    1070             :  * This method projects an arbitrary function onto the solution via L2
    1071             :  * projections and nodal interpolations on each element.
    1072             :  */
    1073         213 : void System::project_solution (FEMFunctionBase<Number> * f,
    1074             :                                FEMFunctionBase<Gradient> * g,
    1075             :                                std::optional<ConstElemRange> active_local_range,
    1076             :                                std::optional<std::vector<unsigned int>> variable_numbers) const
    1077             : {
    1078         420 :   this->project_vector(*solution, f, g, /*is_adjoint=*/-1, active_local_range, variable_numbers);
    1079             : 
    1080         219 :   solution->localize(*current_local_solution, _dof_map->get_send_list());
    1081         213 : }
    1082             : 
    1083             : 
    1084             : /**
    1085             :  * This method projects an arbitrary function via L2 projections and
    1086             :  * nodal interpolations on each element.
    1087             :  */
    1088         840 : void System::project_vector (ValueFunctionPointer fptr,
    1089             :                              GradientFunctionPointer gptr,
    1090             :                              const Parameters & function_parameters,
    1091             :                              NumericVector<Number> & new_vector,
    1092             :                              int is_adjoint,
    1093             :                              std::optional<ConstElemRange> active_local_range,
    1094             :                              std::optional<std::vector<unsigned int>> variable_numbers) const
    1095             : {
    1096          48 :   WrappedFunction<Number> f(*this, fptr, &function_parameters);
    1097          24 :   WrappedFunction<Gradient> g(*this, gptr, &function_parameters);
    1098        1656 :   this->project_vector(new_vector, &f, &g, is_adjoint, active_local_range, variable_numbers);
    1099         840 : }
    1100             : 
    1101             : /**
    1102             :  * This method projects an arbitrary function via L2 projections and
    1103             :  * nodal interpolations on each element.
    1104             :  */
    1105      329929 : void System::project_vector (NumericVector<Number> & new_vector,
    1106             :                              FunctionBase<Number> * f,
    1107             :                              FunctionBase<Gradient> * g,
    1108             :                              int is_adjoint,
    1109             :                              std::optional<ConstElemRange> active_local_range,
    1110             :                              std::optional<std::vector<unsigned int>> variable_numbers) const
    1111             : {
    1112       27888 :   LOG_SCOPE ("project_vector(FunctionBase)", "System");
    1113             : 
    1114        9296 :   libmesh_assert(f);
    1115             : 
    1116       18592 :   WrappedFunctor<Number> f_fem(*f);
    1117             : 
    1118      329929 :   if (g)
    1119             :     {
    1120        9258 :       WrappedFunctor<Gradient> g_fem(*g);
    1121             : 
    1122      648103 :       this->project_vector(new_vector, &f_fem, &g_fem, is_adjoint, active_local_range, variable_numbers);
    1123             :     }
    1124             :   else
    1125        2528 :     this->project_vector(new_vector, &f_fem, nullptr, is_adjoint, active_local_range, variable_numbers);
    1126      329929 : }
    1127             : 
    1128             : 
    1129             : /**
    1130             :  * This method projects an arbitrary function via L2 projections and
    1131             :  * nodal interpolations on each element.
    1132             :  */
    1133      330142 : void System::project_vector (NumericVector<Number> & new_vector,
    1134             :                              FEMFunctionBase<Number> * f,
    1135             :                              FEMFunctionBase<Gradient> * g,
    1136             :                              int is_adjoint,
    1137             :                              std::optional<ConstElemRange> active_local_range,
    1138             :                              std::optional<std::vector<unsigned int>> variable_numbers) const
    1139             : {
    1140       27906 :   LOG_SCOPE ("project_fem_vector()", "System");
    1141             : 
    1142        9302 :   libmesh_assert (f);
    1143             : 
    1144             :   const ConstElemRange & range =
    1145      330142 :     active_local_range ?
    1146           2 :       active_local_range.value() :
    1147      339373 :       this->get_mesh().active_local_element_stored_range();
    1148             : 
    1149        9302 :   VectorSetAction<Number> setter(new_vector);
    1150             : 
    1151      330142 :   const unsigned int n_variables = this->n_vars();
    1152             : 
    1153       18604 :   std::vector<unsigned int> vars;
    1154      330142 :   if (variable_numbers)
    1155             :     {
    1156          71 :       vars = *variable_numbers;
    1157         142 :       for (auto v : vars)
    1158          71 :         if (v >= n_variables)
    1159           0 :           libmesh_error_msg("ERROR: variable number " << v <<
    1160             :                            " out of range for system with " <<
    1161             :                            n_variables << " variables.");
    1162             :     }
    1163             :   else
    1164             :     {
    1165      330071 :       vars.resize(n_variables);
    1166        9300 :       std::iota(vars.begin(), vars.end(), 0);
    1167             :     }
    1168             : 
    1169             : 
    1170             :   // Use a typedef to make the calling sequence for parallel_for() a bit more readable
    1171             :   typedef
    1172             :     GenericProjector<FEMFunctionWrapper<Number>, FEMFunctionWrapper<Gradient>,
    1173             :                      Number, VectorSetAction<Number>> FEMProjector;
    1174             : 
    1175       18604 :   FEMFunctionWrapper<Number> fw(*f);
    1176             : 
    1177      330142 :   if (g)
    1178             :     {
    1179       18516 :       FEMFunctionWrapper<Gradient> gw(*g);
    1180             : 
    1181      347162 :       FEMProjector projector(*this, fw, &gw, setter, vars);
    1182      328646 :       projector.project(range);
    1183      310130 :     }
    1184             :   else
    1185             :     {
    1186        1584 :       FEMProjector projector(*this, fw, nullptr, setter, vars);
    1187        1496 :       projector.project(range);
    1188        1408 :     }
    1189             : 
    1190             :   // Also, load values into the SCALAR dofs
    1191             :   // Note: We assume that all SCALAR dofs are on the
    1192             :   // processor with highest ID
    1193      339444 :   if (this->processor_id() == (this->n_processors()-1))
    1194             :     {
    1195             :       // FIXME: Do we want to first check for SCALAR vars before building this? [PB]
    1196       65084 :       FEMContext context( *this );
    1197             : 
    1198        4651 :       const DofMap & dof_map = this->get_dof_map();
    1199      112248 :       for (auto var : vars)
    1200       56466 :         if (this->variable(var).type().family == SCALAR)
    1201             :           {
    1202             :             // FIXME: We reinit with an arbitrary element in case the user
    1203             :             //        doesn't override FEMFunctionBase::component. Is there
    1204             :             //        any use case we're missing? [PB]
    1205           0 :             context.pre_fe_reinit(*this, *(this->get_mesh().active_local_elements_begin()));
    1206             : 
    1207           0 :             std::vector<dof_id_type> SCALAR_indices;
    1208           0 :             dof_map.SCALAR_dof_indices (SCALAR_indices, var);
    1209             :             const unsigned int n_SCALAR_dofs =
    1210           0 :               cast_int<unsigned int>(SCALAR_indices.size());
    1211             : 
    1212           0 :             for (unsigned int i=0; i<n_SCALAR_dofs; i++)
    1213             :               {
    1214           0 :                 const dof_id_type global_index = SCALAR_indices[i];
    1215             :                 const unsigned int component_index =
    1216           0 :                   this->variable_scalar_number(var,i);
    1217             : 
    1218           0 :                 new_vector.set(global_index, f->component(context, component_index, Point(), this->time));
    1219             :               }
    1220             :           }
    1221       46480 :     }
    1222             : 
    1223      330142 :   new_vector.close();
    1224             : 
    1225             :   // Look for spline bases, in which case we need to backtrack
    1226             :   // to calculate the spline DoF values.
    1227       18604 :   std::vector<const Variable *> rational_vars;
    1228      664331 :   for (auto varnum : vars)
    1229             :     {
    1230      334189 :       const Variable & var = this->get_dof_map().variable(varnum);
    1231      334189 :       if (var.type().family == RATIONAL_BERNSTEIN)
    1232        7684 :         rational_vars.push_back(&var);
    1233             :     }
    1234             : 
    1235             :   // Okay, but are we really using any *spline* bases, or just
    1236             :   // unconstrained rational bases?
    1237      330142 :   bool using_spline_bases = false;
    1238      330142 :   if (!rational_vars.empty())
    1239             :     {
    1240             :       // Look for a spline node: a NodeElem with a rational variable
    1241             :       // on it.
    1242       12714 :       for (auto & elem : range)
    1243        5330 :         if (elem->type() == NODEELEM)
    1244         300 :           for (auto rational_var : rational_vars)
    1245         300 :             if (rational_var->active_on_subdomain(elem->subdomain_id()))
    1246             :               {
    1247         300 :                 using_spline_bases = true;
    1248         292 :                 goto checked_on_splines;
    1249             :               }
    1250             :     }
    1251             : 
    1252      329710 : checked_on_splines:
    1253             : 
    1254             :   // Not every processor may have a NodeElem, especially while
    1255             :   // we're not partitioning them efficiently yet.
    1256      330142 :   this->comm().max(using_spline_bases);
    1257             : 
    1258      330142 :   if (using_spline_bases)
    1259         300 :     this->solve_for_unconstrained_dofs(new_vector, is_adjoint);
    1260             : 
    1261             : #ifdef LIBMESH_ENABLE_CONSTRAINTS
    1262      330142 :   if (is_adjoint == -1)
    1263      330142 :     this->get_dof_map().enforce_constraints_exactly(*this, &new_vector);
    1264           0 :   else if (is_adjoint >= 0)
    1265           0 :     this->get_dof_map().enforce_adjoint_constraints_exactly(new_vector,
    1266             :                                                             is_adjoint);
    1267             : #else
    1268             :   libmesh_ignore(is_adjoint);
    1269             : #endif
    1270      330142 : }
    1271             : 
    1272             : 
    1273             : /**
    1274             :  * This method projects components of an arbitrary boundary function
    1275             :  * onto the solution via L2 projections and nodal interpolations on
    1276             :  * each element.
    1277             :  */
    1278           0 : void System::boundary_project_solution (const std::set<boundary_id_type> & b,
    1279             :                                         const std::vector<unsigned int> & variables,
    1280             :                                         ValueFunctionPointer fptr,
    1281             :                                         GradientFunctionPointer gptr,
    1282             :                                         const Parameters & function_parameters,
    1283             :                                         std::optional<ConstElemRange> active_local_range)
    1284             : 
    1285             : {
    1286           0 :   WrappedFunction<Number> f(*this, fptr, &function_parameters);
    1287           0 :   WrappedFunction<Gradient> g(*this, gptr, &function_parameters);
    1288           0 :   this->boundary_project_solution(b, variables, &f, &g, active_local_range);
    1289           0 : }
    1290             : 
    1291             : 
    1292             : /**
    1293             :  * This method projects an arbitrary boundary function onto the
    1294             :  * solution via L2 projections and nodal interpolations on each
    1295             :  * element.
    1296             :  */
    1297          71 : void System::boundary_project_solution (const std::set<boundary_id_type> & b,
    1298             :                                         const std::vector<unsigned int> & variables,
    1299             :                                         FunctionBase<Number> * f,
    1300             :                                         FunctionBase<Gradient> * g,
    1301             :                                         std::optional<ConstElemRange> active_local_range)
    1302             : {
    1303         140 :   this->boundary_project_vector(b, variables, *solution, f, g, -1 /*is_adjoint*/, active_local_range);
    1304             : 
    1305          73 :   solution->localize(*current_local_solution);
    1306          71 : }
    1307             : 
    1308             : 
    1309             : 
    1310             : 
    1311             : 
    1312             : /**
    1313             :  * This method projects an arbitrary boundary function via L2
    1314             :  * projections and nodal interpolations on each element.
    1315             :  */
    1316           0 : void System::boundary_project_vector (const std::set<boundary_id_type> & b,
    1317             :                                       const std::vector<unsigned int> & variables,
    1318             :                                       ValueFunctionPointer fptr,
    1319             :                                       GradientFunctionPointer gptr,
    1320             :                                       const Parameters & function_parameters,
    1321             :                                       NumericVector<Number> & new_vector,
    1322             :                                       int is_adjoint,
    1323             :                                       std::optional<ConstElemRange> active_local_range) const
    1324             : {
    1325           0 :   WrappedFunction<Number> f(*this, fptr, &function_parameters);
    1326           0 :   WrappedFunction<Gradient> g(*this, gptr, &function_parameters);
    1327           0 :   this->boundary_project_vector(b, variables, new_vector, &f, &g,
    1328             :                                 is_adjoint, active_local_range);
    1329           0 : }
    1330             : 
    1331             : /**
    1332             :  * This method projects an arbitrary function via L2 projections and
    1333             :  * nodal interpolations on each element.
    1334             :  */
    1335          71 : void System::boundary_project_vector (const std::set<boundary_id_type> & b,
    1336             :                                       const std::vector<unsigned int> & variables,
    1337             :                                       NumericVector<Number> & new_vector,
    1338             :                                       FunctionBase<Number> * f,
    1339             :                                       FunctionBase<Gradient> * g,
    1340             :                                       int is_adjoint,
    1341             :                                       std::optional<ConstElemRange> active_local_range) const
    1342             : {
    1343           6 :   LOG_SCOPE ("boundary_project_vector()", "System");
    1344             : 
    1345             :   const ConstElemRange & range =
    1346          71 :     active_local_range ?
    1347           0 :       active_local_range.value() :
    1348          73 :       this->get_mesh().active_local_element_stored_range();
    1349             : 
    1350             :   Threads::parallel_for
    1351          73 :     (range,
    1352         138 :      BoundaryProjectSolution(b, variables, *this, f, g,
    1353          71 :                              this->get_equation_systems().parameters,
    1354             :                              new_vector)
    1355             :      );
    1356             : 
    1357             :   // We don't do SCALAR dofs when just projecting the boundary, so
    1358             :   // we're done here.
    1359             : 
    1360          71 :   new_vector.close();
    1361             : 
    1362             : #ifdef LIBMESH_ENABLE_CONSTRAINTS
    1363          71 :   if (is_adjoint == -1)
    1364          71 :     this->get_dof_map().enforce_constraints_exactly(*this, &new_vector);
    1365           0 :   else if (is_adjoint >= 0)
    1366           0 :     this->get_dof_map().enforce_adjoint_constraints_exactly(new_vector,
    1367             :                                                             is_adjoint);
    1368             : #else
    1369             :   libmesh_ignore(is_adjoint);
    1370             : #endif
    1371          71 : }
    1372             : 
    1373             : 
    1374             : 
    1375             : #ifdef LIBMESH_ENABLE_AMR
    1376       57282 : void BuildProjectionList::unique()
    1377             : {
    1378             :   // Sort the send list.  After this duplicated
    1379             :   // elements will be adjacent in the vector
    1380       57282 :   std::sort(this->send_list.begin(),
    1381             :             this->send_list.end());
    1382             : 
    1383             :   // Now use std::unique to remove duplicate entries
    1384             :   std::vector<dof_id_type>::iterator new_end =
    1385       53270 :     std::unique (this->send_list.begin(),
    1386        4012 :                  this->send_list.end());
    1387             : 
    1388             :   // Remove the end of the send_list.  Use the "swap trick"
    1389             :   // from Effective STL
    1390       59288 :   std::vector<dof_id_type>
    1391        2006 :     (this->send_list.begin(), new_end).swap (this->send_list);
    1392       57282 : }
    1393             : 
    1394             : 
    1395             : 
    1396       57348 : void BuildProjectionList::operator()(const ConstElemRange & range)
    1397             : {
    1398             :   // The DofMap for this system
    1399       57348 :   const DofMap & dof_map = system.get_dof_map();
    1400             : 
    1401        2028 :   const dof_id_type first_old_dof = dof_map.first_old_dof();
    1402        2028 :   const dof_id_type end_old_dof   = dof_map.end_old_dof();
    1403             : 
    1404             :   // We can handle all the variables at once.
    1405             :   // The old global DOF indices
    1406        4056 :   std::vector<dof_id_type> di;
    1407             : 
    1408             :   // Iterate over the elements in the range
    1409     4246115 :   for (const auto & elem : range)
    1410             :     {
    1411             :       // If this element doesn't have an old_dof_object with dofs for the
    1412             :       // current system, then it must be newly added, so the user
    1413             :       // is responsible for setting the new dofs.
    1414             : 
    1415             :       // ... but we need a better way to test for that; the code
    1416             :       // below breaks on any FE type for which the elem stores no
    1417             :       // dofs.
    1418             :       // if (!elem->get_old_dof_object() || !elem->get_old_dof_object()->has_dofs(system.number()))
    1419             :       //  continue;
    1420             : 
    1421             :       // Examining refinement flags instead should distinguish
    1422             :       // between refinement-added and user-added elements lacking
    1423             :       // old_dof_object
    1424     4188767 :       const DofObject * old_dof_object = elem->get_old_dof_object();
    1425     2103578 :       if (!old_dof_object &&
    1426     4309317 :           elem->refinement_flag() != Elem::JUST_REFINED &&
    1427       12004 :           elem->refinement_flag() != Elem::JUST_COARSENED)
    1428      113450 :         continue;
    1429             : 
    1430      919828 :       const Elem * parent = elem->parent();
    1431             : 
    1432     4523938 :       if (elem->refinement_flag() == Elem::JUST_REFINED)
    1433             :         {
    1434      141904 :           libmesh_assert(parent);
    1435             : 
    1436             :           // We used to hack_p_level here, but that wasn't thread-safe
    1437             :           // so now we take p refinement flags into account in
    1438             :           // old_dof_indices
    1439             : 
    1440     1488524 :           dof_map.old_dof_indices (parent, di);
    1441             : 
    1442    11378092 :           for (auto & node : elem->node_ref_range())
    1443             :             {
    1444      921144 :               const DofObject * old_dofs = node.get_old_dof_object();
    1445             : 
    1446     9747664 :               if (old_dofs)
    1447             :                 {
    1448     3601310 :                   const unsigned int sysnum = system.number();
    1449     3601310 :                   const unsigned int nvg = old_dofs->n_var_groups(sysnum);
    1450             : 
    1451     7292130 :                   for (unsigned int vg=0; vg != nvg; ++vg)
    1452             :                     {
    1453             :                       const unsigned int nvig =
    1454      349560 :                         old_dofs->n_vars(sysnum, vg);
    1455     7390524 :                       for (unsigned int vig=0; vig != nvig; ++vig)
    1456             :                         {
    1457             :                           const unsigned int n_comp =
    1458      350332 :                             old_dofs->n_comp_group(sysnum, vg);
    1459     7665804 :                           for (unsigned int c=0; c != n_comp; ++c)
    1460             :                             {
    1461             :                               const dof_id_type old_id =
    1462     3591371 :                                 old_dofs->dof_number(sysnum, vg, vig,
    1463     3966100 :                                                      c, n_comp);
    1464             : 
    1465             :                               // We should either have no old id
    1466             :                               // (e.g. on a newly expanded subdomain)
    1467             :                               // or an id from the old system.
    1468      374729 :                               libmesh_assert(old_id < dof_map.n_old_dofs() ||
    1469             :                                              old_id == DofObject::invalid_id);
    1470     3966100 :                               di.push_back(old_id);
    1471             :                             }
    1472             :                         }
    1473             :                     }
    1474             :                 }
    1475             :             }
    1476             : 
    1477     1488524 :           std::sort(di.begin(), di.end());
    1478             :           std::vector<dof_id_type>::iterator new_end =
    1479     1488524 :             std::unique(di.begin(), di.end());
    1480     2835144 :           std::vector<dof_id_type>(di.begin(), new_end).swap(di);
    1481             :         }
    1482     2575500 :       else if (elem->refinement_flag() == Elem::JUST_COARSENED)
    1483             :         {
    1484       45774 :           std::vector<dof_id_type> di_child;
    1485       22887 :           di.clear();
    1486     1051066 :           for (auto & child : elem->child_ref_range())
    1487             :             {
    1488      841064 :               dof_map.old_dof_indices (&child, di_child);
    1489      841064 :               di.insert(di.end(), di_child.begin(), di_child.end());
    1490             :             }
    1491             :         }
    1492             :       else
    1493     2365498 :         dof_map.old_dof_indices (elem, di);
    1494             : 
    1495    32611339 :       for (auto di_i : di)
    1496             :         {
    1497             :           // If we've just expanded a subdomain for a
    1498             :           // subdomain-restricted variable, then we may have an
    1499             :           // old_dof_object that doesn't have an old DoF for every
    1500             :           // local index.
    1501    28547315 :           if (di_i == DofObject::invalid_id)
    1502           0 :             continue;
    1503             : 
    1504     3024444 :           libmesh_assert_less(di_i, dof_map.n_old_dofs());
    1505    28547315 :           if (di_i < first_old_dof || di_i >= end_old_dof)
    1506    12803116 :             this->send_list.push_back(di_i);
    1507             :         }
    1508             :     }  // end elem loop
    1509       57348 : }
    1510             : 
    1511             : 
    1512             : 
    1513          66 : void BuildProjectionList::join(const BuildProjectionList & other)
    1514             : {
    1515             :   // Joining simply requires I add the dof indices from the other object
    1516          44 :   this->send_list.insert(this->send_list.end(),
    1517             :                          other.send_list.begin(),
    1518          88 :                          other.send_list.end());
    1519          66 : }
    1520             : #endif // LIBMESH_ENABLE_AMR
    1521             : 
    1522             : 
    1523             : 
    1524          71 : void BoundaryProjectSolution::operator()(const ConstElemRange & range) const
    1525             : {
    1526             :   // We need data to project
    1527           2 :   libmesh_assert(f.get());
    1528             : 
    1529             :   /**
    1530             :    * This method projects an arbitrary boundary solution to the current
    1531             :    * mesh.  The input function \p f gives the arbitrary solution,
    1532             :    * while the \p new_vector (which should already be correctly sized)
    1533             :    * gives the solution (to be computed) on the current mesh.
    1534             :    */
    1535             : 
    1536             :   // The dimensionality of the current mesh
    1537          71 :   const unsigned int dim = system.get_mesh().mesh_dimension();
    1538             : 
    1539             :   // The DofMap for this system
    1540          71 :   const DofMap & dof_map = system.get_dof_map();
    1541             : 
    1542             :   // Boundary info for the current mesh
    1543             :   const BoundaryInfo & boundary_info =
    1544           4 :     system.get_mesh().get_boundary_info();
    1545             : 
    1546             :   // The element matrix and RHS for projections.
    1547             :   // Note that Ke is always real-valued, whereas
    1548             :   // Fe may be complex valued if complex number
    1549             :   // support is enabled
    1550          75 :   DenseMatrix<Real> Ke;
    1551          71 :   DenseVector<Number> Fe;
    1552             :   // The new element coefficients
    1553          71 :   DenseVector<Number> Ue;
    1554             : 
    1555             : 
    1556             :   // Loop over all the variables we've been requested to project
    1557         142 :   for (auto v : make_range(variables.size()))
    1558             :     {
    1559          71 :       const unsigned int var = variables[v];
    1560             : 
    1561          71 :       const Variable & variable = dof_map.variable(var);
    1562             : 
    1563           2 :       const FEType & fe_type = variable.type();
    1564             : 
    1565          71 :       if (fe_type.family == SCALAR)
    1566           0 :         continue;
    1567             : 
    1568             :       const unsigned int var_component =
    1569          71 :         system.variable_scalar_number(var, 0);
    1570             : 
    1571             :       // Get FE objects of the appropriate type
    1572          73 :       std::unique_ptr<FEBase> fe (FEBase::build(dim, fe_type));
    1573             : 
    1574             :       // Prepare variables for projection
    1575          73 :       std::unique_ptr<QBase> qedgerule (fe_type.default_quadrature_rule(1));
    1576          73 :       std::unique_ptr<QBase> qsiderule (fe_type.default_quadrature_rule(dim-1));
    1577             : 
    1578             :       // The values of the shape functions at the quadrature
    1579             :       // points
    1580           2 :       const std::vector<std::vector<Real>> & phi = fe->get_phi();
    1581             : 
    1582             :       // The gradients of the shape functions at the quadrature
    1583             :       // points on the child element.
    1584           2 :       const std::vector<std::vector<RealGradient>> * dphi = nullptr;
    1585             : 
    1586          71 :       const FEContinuity cont = fe->get_continuity();
    1587             : 
    1588          71 :       if (cont == C_ONE)
    1589             :         {
    1590             :           // We'll need gradient data for a C1 projection
    1591           0 :           libmesh_assert(g.get());
    1592             : 
    1593             :           const std::vector<std::vector<RealGradient>> &
    1594           0 :             ref_dphi = fe->get_dphi();
    1595           0 :           dphi = &ref_dphi;
    1596             :         }
    1597             : 
    1598             :       // The Jacobian * quadrature weight at the quadrature points
    1599             :       const std::vector<Real> & JxW =
    1600           5 :         fe->get_JxW();
    1601             : 
    1602             :       // The XYZ locations of the quadrature points
    1603             :       const std::vector<Point> & xyz_values =
    1604           5 :         fe->get_xyz();
    1605             : 
    1606             :       // The global DOF indices
    1607           4 :       std::vector<dof_id_type> dof_indices;
    1608             :       // Side/edge DOF indices
    1609           4 :       std::vector<unsigned int> side_dofs;
    1610             : 
    1611             :       // Container to catch IDs passed back from BoundaryInfo.
    1612           4 :       std::vector<boundary_id_type> bc_ids;
    1613             : 
    1614             :       // Iterate over all the elements in the range
    1615         395 :       for (const auto & elem : range)
    1616             :         {
    1617             :           // Per-subdomain variables don't need to be projected on
    1618             :           // elements where they're not active
    1619         324 :           if (!variable.active_on_subdomain(elem->subdomain_id()))
    1620           0 :             continue;
    1621             : 
    1622         324 :           const unsigned short n_nodes = elem->n_nodes();
    1623         324 :           const unsigned short n_edges = elem->n_edges();
    1624         324 :           const unsigned short n_sides = elem->n_sides();
    1625             : 
    1626             :           // Find out which nodes, edges and sides are on a requested
    1627             :           // boundary:
    1628         351 :           std::vector<bool> is_boundary_node(n_nodes, false),
    1629         351 :             is_boundary_edge(n_edges, false),
    1630         351 :             is_boundary_side(n_sides, false);
    1631             : 
    1632             :           // We also maintain a separate list of nodeset-based boundary nodes
    1633         351 :           std::vector<bool> is_boundary_nodeset(n_nodes, false);
    1634             : 
    1635        2268 :           for (unsigned char s=0; s != n_sides; ++s)
    1636             :             {
    1637             :               // First see if this side has been requested
    1638        1944 :               boundary_info.boundary_ids (elem, s, bc_ids);
    1639         162 :               bool do_this_side = false;
    1640        2484 :               for (const auto & bc_id : bc_ids)
    1641         648 :                 if (b.count(bc_id))
    1642             :                   {
    1643           9 :                     do_this_side = true;
    1644           9 :                     break;
    1645             :                   }
    1646        1944 :               if (!do_this_side)
    1647        1683 :                 continue;
    1648             : 
    1649          18 :               is_boundary_side[s] = true;
    1650             : 
    1651             :               // Then see what nodes and what edges are on it
    1652         972 :               for (unsigned int n=0; n != n_nodes; ++n)
    1653         864 :                 if (elem->is_node_on_side(n,s))
    1654          72 :                   is_boundary_node[n] = true;
    1655        1404 :               for (unsigned int e=0; e != n_edges; ++e)
    1656        1296 :                 if (elem->is_edge_on_side(e,s))
    1657          72 :                   is_boundary_edge[e] = true;
    1658             :             }
    1659             : 
    1660             :             // We can also project on nodes, so we should also independently
    1661             :             // check whether the nodes have been requested
    1662        2916 :             for (unsigned int n=0; n != n_nodes; ++n)
    1663             :               {
    1664        2808 :                 boundary_info.boundary_ids (elem->node_ptr(n), bc_ids);
    1665             : 
    1666        5196 :                 for (const auto & bc_id : bc_ids)
    1667        2604 :                   if (b.count(bc_id))
    1668             :                     {
    1669          74 :                       is_boundary_node[n] = true;
    1670          74 :                       is_boundary_nodeset[n] = true;
    1671             :                     }
    1672             :               }
    1673             : 
    1674             :             // We can also project on edges, so we should also independently
    1675             :             // check whether the edges have been requested
    1676        4212 :             for (unsigned short e=0; e != n_edges; ++e)
    1677             :               {
    1678        3888 :                 boundary_info.edge_boundary_ids (elem, e, bc_ids);
    1679             : 
    1680        3924 :                 for (const auto & bc_id : bc_ids)
    1681          36 :                   if (b.count(bc_id))
    1682           6 :                     is_boundary_edge[e] = true;
    1683             :               }
    1684             : 
    1685             :           // Update the DOF indices for this element based on
    1686             :           // the current mesh
    1687         324 :           dof_map.dof_indices (elem, dof_indices, var);
    1688             : 
    1689             :           // The number of DOFs on the element
    1690             :           const unsigned int n_dofs =
    1691          54 :             cast_int<unsigned int>(dof_indices.size());
    1692             : 
    1693             :           // Fixed vs. free DoFs on edge/face projections
    1694         351 :           std::vector<char> dof_is_fixed(n_dofs, false); // bools
    1695         351 :           std::vector<int> free_dof(n_dofs, 0);
    1696             : 
    1697             :           // Zero the interpolated values
    1698         297 :           Ue.resize (n_dofs); Ue.zero();
    1699             : 
    1700             :           // In general, we need a series of
    1701             :           // projections to ensure a unique and continuous
    1702             :           // solution.  We start by interpolating boundary nodes, then
    1703             :           // hold those fixed and project boundary edges, then hold
    1704             :           // those fixed and project boundary faces,
    1705             : 
    1706             :           // Interpolate node values first
    1707          27 :           unsigned int current_dof = 0;
    1708        2916 :           for (unsigned short n = 0; n != n_nodes; ++n)
    1709             :             {
    1710             :               // FIXME: this should go through the DofMap,
    1711             :               // not duplicate dof_indices code badly!
    1712             : 
    1713             :               // This call takes into account elem->p_level() internally.
    1714             :               const unsigned int nc =
    1715        2592 :                 FEInterface::n_dofs_at_node (fe_type, elem, n);
    1716             : 
    1717        2771 :               if ((!elem->is_vertex(n) || !is_boundary_node[n]) &&
    1718         574 :                   !is_boundary_nodeset[n])
    1719             :                 {
    1720        2148 :                   current_dof += nc;
    1721        2148 :                   continue;
    1722             :                 }
    1723         444 :               if (cont == DISCONTINUOUS)
    1724             :                 {
    1725           0 :                   libmesh_assert_equal_to (nc, 0);
    1726             :                 }
    1727             :               // Assume that C_ZERO elements have a single nodal
    1728             :               // value shape function
    1729         444 :               else if (cont == C_ZERO)
    1730             :                 {
    1731          37 :                   libmesh_assert_equal_to (nc, 1);
    1732         851 :                   Ue(current_dof) = f->component(var_component,
    1733             :                                                  elem->point(n),
    1734         444 :                                                  system.time);
    1735         444 :                   dof_is_fixed[current_dof] = true;
    1736         444 :                   current_dof++;
    1737             :                 }
    1738             :               // The hermite element vertex shape functions are weird
    1739           0 :               else if (fe_type.family == HERMITE)
    1740             :                 {
    1741           0 :                   Ue(current_dof) = f->component(var_component,
    1742             :                                                  elem->point(n),
    1743           0 :                                                  system.time);
    1744           0 :                   dof_is_fixed[current_dof] = true;
    1745           0 :                   current_dof++;
    1746           0 :                   Gradient grad = g->component(var_component,
    1747             :                                                elem->point(n),
    1748           0 :                                                system.time);
    1749             :                   // x derivative
    1750           0 :                   Ue(current_dof) = grad(0);
    1751           0 :                   dof_is_fixed[current_dof] = true;
    1752           0 :                   current_dof++;
    1753             : #if LIBMESH_DIM > 1
    1754           0 :                   if (dim > 1)
    1755             :                     {
    1756             :                       // We'll finite difference mixed derivatives
    1757           0 :                       Point nxminus = elem->point(n),
    1758           0 :                         nxplus = elem->point(n);
    1759           0 :                       nxminus(0) -= TOLERANCE;
    1760           0 :                       nxplus(0) += TOLERANCE;
    1761           0 :                       Gradient gxminus = g->component(var_component,
    1762             :                                                       nxminus,
    1763           0 :                                                       system.time);
    1764           0 :                       Gradient gxplus = g->component(var_component,
    1765             :                                                      nxplus,
    1766           0 :                                                      system.time);
    1767             :                       // y derivative
    1768           0 :                       Ue(current_dof) = grad(1);
    1769           0 :                       dof_is_fixed[current_dof] = true;
    1770           0 :                       current_dof++;
    1771             :                       // xy derivative
    1772           0 :                       Ue(current_dof) = (gxplus(1) - gxminus(1))
    1773           0 :                         / 2. / TOLERANCE;
    1774           0 :                       dof_is_fixed[current_dof] = true;
    1775           0 :                       current_dof++;
    1776             : 
    1777             : #if LIBMESH_DIM > 2
    1778           0 :                       if (dim > 2)
    1779             :                         {
    1780             :                           // z derivative
    1781           0 :                           Ue(current_dof) = grad(2);
    1782           0 :                           dof_is_fixed[current_dof] = true;
    1783           0 :                           current_dof++;
    1784             :                           // xz derivative
    1785           0 :                           Ue(current_dof) = (gxplus(2) - gxminus(2))
    1786           0 :                             / 2. / TOLERANCE;
    1787           0 :                           dof_is_fixed[current_dof] = true;
    1788           0 :                           current_dof++;
    1789             :                           // We need new points for yz
    1790           0 :                           Point nyminus = elem->point(n),
    1791           0 :                             nyplus = elem->point(n);
    1792           0 :                           nyminus(1) -= TOLERANCE;
    1793           0 :                           nyplus(1) += TOLERANCE;
    1794           0 :                           Gradient gyminus = g->component(var_component,
    1795             :                                                           nyminus,
    1796           0 :                                                           system.time);
    1797           0 :                           Gradient gyplus = g->component(var_component,
    1798             :                                                          nyplus,
    1799           0 :                                                          system.time);
    1800             :                           // xz derivative
    1801           0 :                           Ue(current_dof) = (gyplus(2) - gyminus(2))
    1802           0 :                             / 2. / TOLERANCE;
    1803           0 :                           dof_is_fixed[current_dof] = true;
    1804           0 :                           current_dof++;
    1805             :                           // Getting a 2nd order xyz is more tedious
    1806           0 :                           Point nxmym = elem->point(n),
    1807           0 :                             nxmyp = elem->point(n),
    1808           0 :                             nxpym = elem->point(n),
    1809           0 :                             nxpyp = elem->point(n);
    1810           0 :                           nxmym(0) -= TOLERANCE;
    1811           0 :                           nxmym(1) -= TOLERANCE;
    1812           0 :                           nxmyp(0) -= TOLERANCE;
    1813           0 :                           nxmyp(1) += TOLERANCE;
    1814           0 :                           nxpym(0) += TOLERANCE;
    1815           0 :                           nxpym(1) -= TOLERANCE;
    1816           0 :                           nxpyp(0) += TOLERANCE;
    1817           0 :                           nxpyp(1) += TOLERANCE;
    1818           0 :                           Gradient gxmym = g->component(var_component,
    1819             :                                                         nxmym,
    1820           0 :                                                         system.time);
    1821           0 :                           Gradient gxmyp = g->component(var_component,
    1822             :                                                         nxmyp,
    1823           0 :                                                         system.time);
    1824           0 :                           Gradient gxpym = g->component(var_component,
    1825             :                                                         nxpym,
    1826           0 :                                                         system.time);
    1827           0 :                           Gradient gxpyp = g->component(var_component,
    1828             :                                                         nxpyp,
    1829           0 :                                                         system.time);
    1830           0 :                           Number gxzplus = (gxpyp(2) - gxmyp(2))
    1831           0 :                             / 2. / TOLERANCE;
    1832           0 :                           Number gxzminus = (gxpym(2) - gxmym(2))
    1833           0 :                             / 2. / TOLERANCE;
    1834             :                           // xyz derivative
    1835           0 :                           Ue(current_dof) = (gxzplus - gxzminus)
    1836           0 :                             / 2. / TOLERANCE;
    1837           0 :                           dof_is_fixed[current_dof] = true;
    1838           0 :                           current_dof++;
    1839             :                         }
    1840             : #endif // LIBMESH_DIM > 2
    1841             :                     }
    1842             : #endif // LIBMESH_DIM > 1
    1843             :                 }
    1844             :               // Assume that other C_ONE elements have a single nodal
    1845             :               // value shape function and nodal gradient component
    1846             :               // shape functions
    1847           0 :               else if (cont == C_ONE)
    1848             :                 {
    1849           0 :                   libmesh_assert_equal_to (nc, 1 + dim);
    1850           0 :                   Ue(current_dof) = f->component(var_component,
    1851             :                                                  elem->point(n),
    1852           0 :                                                  system.time);
    1853           0 :                   dof_is_fixed[current_dof] = true;
    1854           0 :                   current_dof++;
    1855           0 :                   Gradient grad = g->component(var_component,
    1856             :                                                elem->point(n),
    1857           0 :                                                system.time);
    1858           0 :                   for (unsigned int i=0; i!= dim; ++i)
    1859             :                     {
    1860           0 :                       Ue(current_dof) = grad(i);
    1861           0 :                       dof_is_fixed[current_dof] = true;
    1862           0 :                       current_dof++;
    1863             :                     }
    1864             :                 }
    1865             :               else
    1866           0 :                 libmesh_error_msg("Unknown continuity " << cont);
    1867             :             }
    1868             : 
    1869             :           // In 3D, project any edge values next
    1870         324 :           if (dim > 2 && cont != DISCONTINUOUS)
    1871        4212 :             for (unsigned short e = 0; e != n_edges; ++e)
    1872             :               {
    1873        4212 :                 if (!is_boundary_edge[e])
    1874        3852 :                   continue;
    1875             : 
    1876         468 :                 FEInterface::dofs_on_edge(elem, dim, fe_type, e,
    1877             :                                           side_dofs);
    1878             : 
    1879             :                 const unsigned int n_side_dofs =
    1880          78 :                   cast_int<unsigned int>(side_dofs.size());
    1881             : 
    1882             :                 // Some edge dofs are on nodes and already
    1883             :                 // fixed, others are free to calculate
    1884          39 :                 unsigned int free_dofs = 0;
    1885        1404 :                 for (auto i : make_range(n_side_dofs))
    1886        1092 :                   if (!dof_is_fixed[side_dofs[i]])
    1887          78 :                     free_dof[free_dofs++] = i;
    1888             : 
    1889             :                 // There may be nothing to project
    1890         468 :                 if (!free_dofs)
    1891         396 :                   continue;
    1892             : 
    1893          33 :                 Ke.resize (free_dofs, free_dofs); Ke.zero();
    1894          33 :                 Fe.resize (free_dofs); Fe.zero();
    1895             :                 // The new edge coefficients
    1896          36 :                 DenseVector<Number> Uedge(free_dofs);
    1897             : 
    1898             :                 // Initialize FE data on the edge
    1899          39 :                 fe->attach_quadrature_rule (qedgerule.get());
    1900          36 :                 fe->edge_reinit (elem, e);
    1901           3 :                 const unsigned int n_qp = qedgerule->n_points();
    1902             : 
    1903             :                 // Loop over the quadrature points
    1904         108 :                 for (unsigned int qp=0; qp<n_qp; qp++)
    1905             :                   {
    1906             :                     // solution at the quadrature point
    1907          78 :                     Number fineval = f->component(var_component,
    1908          72 :                                                   xyz_values[qp],
    1909          72 :                                                   system.time);
    1910             :                     // solution grad at the quadrature point
    1911           6 :                     Gradient finegrad;
    1912          72 :                     if (cont == C_ONE)
    1913           0 :                       finegrad = g->component(var_component,
    1914           0 :                                               xyz_values[qp],
    1915           0 :                                               system.time);
    1916             : 
    1917             :                     // Form edge projection matrix
    1918         204 :                     for (unsigned int sidei=0, freei=0;
    1919         216 :                          sidei != n_side_dofs; ++sidei)
    1920             :                       {
    1921         144 :                         unsigned int i = side_dofs[sidei];
    1922             :                         // fixed DoFs aren't test functions
    1923         156 :                         if (dof_is_fixed[i])
    1924           0 :                           continue;
    1925         300 :                         for (unsigned int sidej=0, freej=0;
    1926         432 :                              sidej != n_side_dofs; ++sidej)
    1927             :                           {
    1928         288 :                             unsigned int j = side_dofs[sidej];
    1929         312 :                             if (dof_is_fixed[j])
    1930           0 :                               Fe(freei) -= phi[i][qp] * phi[j][qp] *
    1931           0 :                                 JxW[qp] * Ue(j);
    1932             :                             else
    1933         384 :                               Ke(freei,freej) += phi[i][qp] *
    1934         336 :                                 phi[j][qp] * JxW[qp];
    1935         288 :                             if (cont == C_ONE)
    1936             :                               {
    1937           0 :                                 if (dof_is_fixed[j])
    1938           0 :                                   Fe(freei) -= ((*dphi)[i][qp] *
    1939           0 :                                                 (*dphi)[j][qp]) *
    1940           0 :                                     JxW[qp] * Ue(j);
    1941             :                                 else
    1942           0 :                                   Ke(freei,freej) += ((*dphi)[i][qp] *
    1943           0 :                                                       (*dphi)[j][qp])
    1944           0 :                                     * JxW[qp];
    1945             :                               }
    1946         312 :                             if (!dof_is_fixed[j])
    1947         288 :                               freej++;
    1948             :                           }
    1949         168 :                         Fe(freei) += phi[i][qp] * fineval * JxW[qp];
    1950         144 :                         if (cont == C_ONE)
    1951           0 :                           Fe(freei) += (finegrad * (*dphi)[i][qp]) *
    1952           0 :                             JxW[qp];
    1953         144 :                         freei++;
    1954             :                       }
    1955             :                   }
    1956             : 
    1957          36 :                 Ke.cholesky_solve(Fe, Uedge);
    1958             : 
    1959             :                 // Transfer new edge solutions to element
    1960         108 :                 for (unsigned int i=0; i != free_dofs; ++i)
    1961             :                   {
    1962          84 :                     Number & ui = Ue(side_dofs[free_dof[i]]);
    1963           6 :                     libmesh_assert(std::abs(ui) < TOLERANCE ||
    1964             :                                    std::abs(ui - Uedge(i)) < TOLERANCE);
    1965          72 :                     ui = Uedge(i);
    1966          78 :                     dof_is_fixed[side_dofs[free_dof[i]]] = true;
    1967             :                   }
    1968             :               }
    1969             : 
    1970             :           // Project any side values (edges in 2D, faces in 3D)
    1971         324 :           if (dim > 1 && cont != DISCONTINUOUS)
    1972        2268 :             for (unsigned short s = 0; s != n_sides; ++s)
    1973             :               {
    1974        2106 :                 if (!is_boundary_side[s])
    1975        1944 :                   continue;
    1976             : 
    1977         108 :                 FEInterface::dofs_on_side(elem, dim, fe_type, s,
    1978             :                                           side_dofs);
    1979             : 
    1980             :                 // Some side dofs are on nodes/edges and already
    1981             :                 // fixed, others are free to calculate
    1982           9 :                 unsigned int free_dofs = 0;
    1983         540 :                 for (auto i : index_range(side_dofs))
    1984         468 :                   if (!dof_is_fixed[side_dofs[i]])
    1985           0 :                     free_dof[free_dofs++] = i;
    1986             : 
    1987             :                 // There may be nothing to project
    1988         108 :                 if (!free_dofs)
    1989          99 :                   continue;
    1990             : 
    1991           0 :                 Ke.resize (free_dofs, free_dofs); Ke.zero();
    1992           0 :                 Fe.resize (free_dofs); Fe.zero();
    1993             :                 // The new side coefficients
    1994           0 :                 DenseVector<Number> Uside(free_dofs);
    1995             : 
    1996             :                 // Initialize FE data on the side
    1997           0 :                 fe->attach_quadrature_rule (qsiderule.get());
    1998           0 :                 fe->reinit (elem, s);
    1999           0 :                 const unsigned int n_qp = qsiderule->n_points();
    2000             : 
    2001             :                 const unsigned int n_side_dofs =
    2002           0 :                   cast_int<unsigned int>(side_dofs.size());
    2003             : 
    2004             :                 // Loop over the quadrature points
    2005           0 :                 for (unsigned int qp=0; qp<n_qp; qp++)
    2006             :                   {
    2007             :                     // solution at the quadrature point
    2008           0 :                     Number fineval = f->component(var_component,
    2009           0 :                                                   xyz_values[qp],
    2010           0 :                                                   system.time);
    2011             :                     // solution grad at the quadrature point
    2012           0 :                     Gradient finegrad;
    2013           0 :                     if (cont == C_ONE)
    2014           0 :                       finegrad = g->component(var_component,
    2015           0 :                                               xyz_values[qp],
    2016           0 :                                               system.time);
    2017             : 
    2018             :                     // Form side projection matrix
    2019           0 :                     for (unsigned int sidei=0, freei=0;
    2020           0 :                          sidei != n_side_dofs; ++sidei)
    2021             :                       {
    2022           0 :                         unsigned int i = side_dofs[sidei];
    2023             :                         // fixed DoFs aren't test functions
    2024           0 :                         if (dof_is_fixed[i])
    2025           0 :                           continue;
    2026           0 :                         for (unsigned int sidej=0, freej=0;
    2027           0 :                              sidej != n_side_dofs; ++sidej)
    2028             :                           {
    2029           0 :                             unsigned int j = side_dofs[sidej];
    2030           0 :                             if (dof_is_fixed[j])
    2031           0 :                               Fe(freei) -= phi[i][qp] * phi[j][qp] *
    2032           0 :                                 JxW[qp] * Ue(j);
    2033             :                             else
    2034           0 :                               Ke(freei,freej) += phi[i][qp] *
    2035           0 :                                 phi[j][qp] * JxW[qp];
    2036           0 :                             if (cont == C_ONE)
    2037             :                               {
    2038           0 :                                 if (dof_is_fixed[j])
    2039           0 :                                   Fe(freei) -= ((*dphi)[i][qp] *
    2040           0 :                                                 (*dphi)[j][qp]) *
    2041           0 :                                     JxW[qp] * Ue(j);
    2042             :                                 else
    2043           0 :                                   Ke(freei,freej) += ((*dphi)[i][qp] *
    2044           0 :                                                       (*dphi)[j][qp])
    2045           0 :                                     * JxW[qp];
    2046             :                               }
    2047           0 :                             if (!dof_is_fixed[j])
    2048           0 :                               freej++;
    2049             :                           }
    2050           0 :                         Fe(freei) += (fineval * phi[i][qp]) * JxW[qp];
    2051           0 :                         if (cont == C_ONE)
    2052           0 :                           Fe(freei) += (finegrad * (*dphi)[i][qp]) *
    2053           0 :                             JxW[qp];
    2054           0 :                         freei++;
    2055             :                       }
    2056             :                   }
    2057             : 
    2058           0 :                 Ke.cholesky_solve(Fe, Uside);
    2059             : 
    2060             :                 // Transfer new side solutions to element
    2061           0 :                 for (unsigned int i=0; i != free_dofs; ++i)
    2062             :                   {
    2063           0 :                     Number & ui = Ue(side_dofs[free_dof[i]]);
    2064           0 :                     libmesh_assert(std::abs(ui) < TOLERANCE ||
    2065             :                                    std::abs(ui - Uside(i)) < TOLERANCE);
    2066           0 :                     ui = Uside(i);
    2067           0 :                     dof_is_fixed[side_dofs[free_dof[i]]] = true;
    2068             :                   }
    2069             :               }
    2070             : 
    2071             :           const dof_id_type
    2072         324 :             first = new_vector.first_local_index(),
    2073         324 :             last  = new_vector.last_local_index();
    2074             : 
    2075             :           // Lock the new_vector since it is shared among threads.
    2076             :           {
    2077          54 :             Threads::spin_mutex::scoped_lock lock(Threads::spin_mtx);
    2078             : 
    2079        2916 :             for (unsigned int i = 0; i < n_dofs; i++)
    2080        2592 :               if (dof_is_fixed[i] &&
    2081        2628 :                   (dof_indices[i] >= first) &&
    2082          36 :                   (dof_indices[i] <  last))
    2083             :                 {
    2084         434 :                   new_vector.set(dof_indices[i], Ue(i));
    2085             :                 }
    2086             :           }
    2087             :         }  // end elem loop
    2088          67 :     } // end variables loop
    2089          71 : }
    2090             : 
    2091             : 
    2092         300 : void System::solve_for_unconstrained_dofs(NumericVector<Number> & vec,
    2093             :                                           int is_adjoint) const
    2094             : {
    2095             :   // We generally don't need this function if we don't have
    2096             :   // non-assembly elements, but let's make sure it's robust to
    2097             :   // pre-INVALID_MAP definitions of those, and let's make sure
    2098             :   // *that's* robust to meshes with non-spline NodeElem too.
    2099             :   //
    2100             :   // Once everything's INVALID_MAP compliant we can remove this.
    2101         300 :   if (!this->get_mesh().n_constraint_rows())
    2102             :     {
    2103             :       libmesh_warning("Called solve_for_unconstrained_dofs on a mesh with no constraint_rows?");
    2104           0 :       return;
    2105             :     }
    2106             : 
    2107           8 :   const DofMap & dof_map = this->get_dof_map();
    2108             : 
    2109             :   std::unique_ptr<SparseMatrix<Number>> mat =
    2110         308 :     SparseMatrix<Number>::build(this->comm());
    2111             : 
    2112         292 :   std::unique_ptr<SparsityPattern::Build> sp;
    2113             : 
    2114         300 :   if (dof_map.computed_sparsity_already())
    2115           0 :     dof_map.update_sparsity_pattern(*mat);
    2116             :   else
    2117             :     {
    2118         300 :       mat->attach_dof_map(dof_map);
    2119         584 :       sp = dof_map.build_sparsity(this->get_mesh());
    2120         300 :       mat->attach_sparsity_pattern(*sp);
    2121             :     }
    2122             : 
    2123         300 :   mat->init();
    2124             : 
    2125           8 :   libmesh_assert_equal_to(vec.size(), dof_map.n_dofs());
    2126           8 :   libmesh_assert_equal_to(vec.local_size(), dof_map.n_local_dofs());
    2127             : 
    2128             :   std::unique_ptr<NumericVector<Number>> rhs =
    2129         308 :     NumericVector<Number>::build(this->comm());
    2130             : 
    2131         308 :   rhs->init(dof_map.n_dofs(), dof_map.n_local_dofs(), false,
    2132          16 :             PARALLEL);
    2133             : 
    2134             :   // Here we start with the unconstrained (and indeterminate) linear
    2135             :   // system, K*u = f, where K is the identity matrix for
    2136             :   // DoFs which have been evaluated and 0 elsewhere, and f is the
    2137             :   // current solution values for evaluated DoFs and 0 elsewhere.
    2138             :   // We then apply the usual heterogeneous constraint matrix C and
    2139             :   // offset h, where u = C*x + h,
    2140             :   // to get C^T*K*C*x = C^T*f - C^T*K*h
    2141             :   // - a constrained and no-longer-singular system that finds the
    2142             :   // closest approximation for the unconstrained degrees of freedom.
    2143             :   //
    2144             :   // Here, though "closest" is in an algebraic sense; we're
    2145             :   // effectively using a pseudoinverse that optimizes in a
    2146             :   // discretization-dependent norm.  That only seems to give ~0.1%
    2147             :   // excess error even in coarse unit test cases, but at some point it
    2148             :   // might be reasonable to weight K and f properly.
    2149             :   //
    2150             :   // We used to do this by distinguishing "constrained" from
    2151             :   // "unconstrained" DoFs rather than "evaluated" vs "unevaluated",
    2152             :   // but that was failing for any IGA meshes where we didn't bother to
    2153             :   // duplicate spline nodes that were exactly equal to assembly
    2154             :   // element vertices.
    2155             : 
    2156             :   // Keep track of which DoFs we've added.  We have a contiguous DoF
    2157             :   // range [a,b) so we'll store a vector with indices [0,b-a) for
    2158             :   // efficiency
    2159          20 :   std::size_t n_local_dofs = dof_map.n_local_dofs();
    2160         308 :   std::vector<bool> have_added(n_local_dofs, false);
    2161          16 :   std::set<dof_id_type> have_added_ghost;
    2162          20 :   std::size_t first_dof = dof_map.first_dof();
    2163             : 
    2164          16 :   std::vector<dof_id_type> di;
    2165             : 
    2166       17610 :   for (const auto & elem : this->get_mesh().active_local_element_ptr_range())
    2167             :     {
    2168             :       // INVALID_MAP for the upcoming spline node change, plus
    2169             :       // NODEELEM for meshes pre-change.  If we have non-spline-node
    2170             :       // NODEELEM we should have returned early already.
    2171       18516 :       if (elem->mapping_type() == INVALID_MAP ||
    2172        9258 :           (elem->type() == NODEELEM))
    2173        5908 :         continue;
    2174             : 
    2175        3350 :       dof_map.dof_indices(elem, di);
    2176             : 
    2177       37244 :       for (auto d : di)
    2178             :         {
    2179       33894 :           if (d < first_dof)
    2180       17003 :             continue;
    2181       31031 :           const std::size_t i = d - first_dof;
    2182       33803 :           if (i >= n_local_dofs ||
    2183        8316 :               have_added[i])
    2184       12732 :             continue;
    2185             : 
    2186       19619 :           DenseMatrix<Number> K(1,1);
    2187       16891 :           DenseVector<Number> F(1);
    2188       18255 :           std::vector<dof_id_type> dof_indices(1, d);
    2189       15527 :           K(0,0) = 1;
    2190       16891 :           F(0) = (*this->solution)(d);
    2191             :           dof_map.heterogenously_constrain_element_matrix_and_vector
    2192        1364 :             (K, F, dof_indices, false, is_adjoint);
    2193       16891 :           mat->add_matrix(K, dof_indices);
    2194       15527 :           rhs->add_vector(F, dof_indices);
    2195             : 
    2196             :           // Keep track of both constrained and constraining additions
    2197             :           // to mat, so we can fix any "leftovers" later.  Otherwise if
    2198             :           // someone puts a non-constraining DoF on a spline node we
    2199             :           // end up with a singular system.
    2200       68090 :           for (auto d2 : dof_indices)
    2201             :             {
    2202       51199 :               if (d2 < first_dof)
    2203             :                 {
    2204         939 :                   have_added_ghost.insert(d2);
    2205        1036 :                   continue;
    2206             :                 }
    2207       50163 :               const std::size_t i2 = d2 - first_dof;
    2208       50163 :               if (i2 >= n_local_dofs)
    2209             :                 {
    2210        6546 :                   have_added_ghost.insert(d2);
    2211        7926 :                   continue;
    2212             :                 }
    2213             : 
    2214        5304 :               have_added[i2] = true;
    2215             :             }
    2216       14163 :         }
    2217         284 :     }
    2218             : 
    2219          16 :   std::unordered_map<processor_id_type, std::vector<dof_id_type>> adds_to_push;
    2220         300 :   processor_id_type p=0;
    2221           8 :   dof_id_type end_p_dof = dof_map.end_dof(0);
    2222        3106 :   for (auto d : have_added_ghost)
    2223             :     {
    2224        3949 :       while (d >= end_p_dof)
    2225             :         {
    2226        1143 :           ++p;
    2227           3 :           end_p_dof = dof_map.end_dof(p);
    2228             :         }
    2229        2806 :       adds_to_push[p].push_back(d);
    2230             :     }
    2231             : 
    2232         592 :   auto act_on_adds = [&have_added, first_dof]
    2233        3338 :     (processor_id_type, const std::vector<dof_id_type> & dofs)
    2234             :     {
    2235        3406 :       for (auto d : dofs)
    2236        2806 :         have_added[d-first_dof] = true;
    2237         304 :     };
    2238             : 
    2239             :   Parallel::push_parallel_vector_data
    2240         300 :     (this->comm(), adds_to_push, act_on_adds);
    2241             : 
    2242             :   // Pin any unconstraining DoFs that lived on spline nodes
    2243       23099 :   for (auto i : index_range(have_added))
    2244             :     {
    2245       24636 :       if (have_added[i])
    2246       20962 :         continue;
    2247             : 
    2248           0 :       const std::size_t d = i + first_dof;
    2249           0 :       mat->add(d, d, 1);
    2250           0 :       rhs->add(d, (*this->solution)(d));
    2251             :     }
    2252             : 
    2253             :   std::unique_ptr<LinearSolver<Number>> linear_solver =
    2254         308 :     LinearSolver<Number>::build(this->comm());
    2255             : 
    2256         584 :   linear_solver->solve(*mat, vec, *rhs,
    2257         300 :                        double(this->get_equation_systems().parameters.get<Real>("linear solver tolerance")),
    2258          40 :                        this->get_equation_systems().parameters.get<unsigned int>("linear solver maximum iterations"));
    2259             : 
    2260         300 :   LinearConvergenceReason conv_flag = linear_solver->get_converged_reason();
    2261         300 :   libmesh_error_msg_if(conv_flag < 0, "Projection to unconstrained DoF space failed: " << Utility::enum_to_string(conv_flag) << std::endl);
    2262         568 : }
    2263             : 
    2264             : 
    2265             : } // namespace libMesh

Generated by: LCOV version 1.14