LCOV - code coverage report
Current view: top level - src/systems - system.C (source / functions) Hit Total Coverage
Test: libMesh/libmesh: #4667 (c50533) with base aa4ea8 Lines: 744 1152 64.6 %
Date: 2026-11-17 18:48:15 Functions: 107 166 64.5 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : // The libMesh Finite Element Library.
       2             : // Copyright (C) 2002-2026 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
       3             : 
       4             : // This library is free software; you can redistribute it and/or
       5             : // modify it under the terms of the GNU Lesser General Public
       6             : // License as published by the Free Software Foundation; either
       7             : // version 2.1 of the License, or (at your option) any later version.
       8             : 
       9             : // This library is distributed in the hope that it will be useful,
      10             : // but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      12             : // Lesser General Public License for more details.
      13             : 
      14             : // You should have received a copy of the GNU Lesser General Public
      15             : // License along with this library; if not, write to the Free Software
      16             : // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
      17             : 
      18             : 
      19             : 
      20             : // Local includes
      21             : #include "libmesh/dof_map.h"
      22             : #include "libmesh/equation_systems.h"
      23             : #include "libmesh/int_range.h"
      24             : #include "libmesh/libmesh_logging.h"
      25             : #include "libmesh/mesh_base.h"
      26             : #include "libmesh/mesh_tools.h"
      27             : #include "libmesh/numeric_vector.h"
      28             : #include "libmesh/parameter_vector.h"
      29             : #include "libmesh/point.h"              // For point_value
      30             : #include "libmesh/point_locator_base.h" // For point_value
      31             : #include "libmesh/qoi_set.h"
      32             : #include "libmesh/enum_to_string.h"
      33             : #include "libmesh/sparse_matrix.h"
      34             : #include "libmesh/system.h"
      35             : #include "libmesh/system_norm.h"
      36             : #include "libmesh/utility.h"
      37             : #include "libmesh/elem.h"
      38             : #include "libmesh/fe_type.h"
      39             : #include "libmesh/fe_interface.h"
      40             : #include "libmesh/fe_compute_data.h"
      41             : #include "libmesh/static_condensation.h"
      42             : #include "libmesh/static_condensation_dof_map.h"
      43             : #include "libmesh/node_range.h"
      44             : 
      45             : // includes for calculate_norm, point_*
      46             : #include "libmesh/fe_base.h"
      47             : #include "libmesh/fe_interface.h"
      48             : #include "libmesh/parallel.h"
      49             : #include "libmesh/parallel_algebra.h"
      50             : #include "libmesh/quadrature.h"
      51             : #include "libmesh/tensor_value.h"
      52             : #include "libmesh/vector_value.h"
      53             : #include "libmesh/tensor_tools.h"
      54             : #include "libmesh/enum_norm_type.h"
      55             : #include "libmesh/enum_fe_family.h"
      56             : 
      57             : // C++ includes
      58             : #include <sstream>   // for std::ostringstream
      59             : 
      60             : namespace libMesh
      61             : {
      62             : 
      63             : 
      64             : // ------------------------------------------------------------
      65             : // System implementation
      66      373870 : System::System (EquationSystems & es,
      67             :                 const std::string & name_in,
      68      373870 :                 const unsigned int number_in) :
      69             : 
      70             :   ParallelObject                    (es),
      71      352698 :   assemble_before_solve             (true),
      72      352698 :   use_fixed_solution                (false),
      73      352698 :   extra_quadrature_order            (0),
      74      352698 :   solution                          (NumericVector<Number>::build(this->comm())),
      75      352698 :   current_local_solution            (NumericVector<Number>::build(this->comm())),
      76      352698 :   time                              (0.),
      77      352698 :   _init_system_function             (nullptr),
      78      352698 :   _init_system_object               (nullptr),
      79      352698 :   _assemble_system_function         (nullptr),
      80      352698 :   _assemble_system_object           (nullptr),
      81      352698 :   _constrain_system_function        (nullptr),
      82      352698 :   _constrain_system_object          (nullptr),
      83      352698 :   _qoi_evaluate_function            (nullptr),
      84      352698 :   _qoi_evaluate_object              (nullptr),
      85      352698 :   _qoi_evaluate_derivative_function (nullptr),
      86      352698 :   _qoi_evaluate_derivative_object   (nullptr),
      87      352698 :   _dof_map                          (std::make_unique<DofMap>(number_in, es.get_mesh())),
      88      352698 :   _equation_systems                 (es),
      89      384456 :   _mesh                             (es.get_mesh()),
      90      352698 :   _sys_name                         (name_in),
      91      352698 :   _sys_number                       (number_in),
      92      352698 :   _active                           (true),
      93      352698 :   _matrices_initialized             (false),
      94      352698 :   _solution_projection              (true),
      95      352698 :   _basic_system_only                (false),
      96      352698 :   _is_initialized                   (false),
      97      352698 :   _additional_data_written          (false),
      98      352698 :   adjoint_already_solved            (false),
      99      352698 :   _hide_output                      (false),
     100      352698 :   project_with_constraints          (true),
     101      352698 :   _prefer_hash_table_matrix_assembly(false),
     102      352698 :   _require_sparsity_pattern         (false),
     103      737154 :   _prefix_with_name                 (false)
     104             : {
     105      737154 :   if (libMesh::on_command_line("--solver-system-names"))
     106           0 :     this->prefix_with_name(true);
     107     1089852 :   if (libMesh::on_command_line("--" + name_in + "-static-condensation"))
     108         420 :     this->create_static_condensation();
     109      373870 : }
     110             : 
     111             : 
     112             : 
     113     1064782 : System::~System ()
     114             : {
     115       10586 :   libmesh_exceptionless_assert (!libMesh::closed());
     116     1064782 : }
     117             : 
     118             : 
     119             : 
     120     1688409 : dof_id_type System::n_dofs() const
     121             : {
     122     1688409 :   return _dof_map->n_dofs();
     123             : }
     124             : 
     125             : 
     126             : 
     127       51241 : dof_id_type System::n_constrained_dofs() const
     128             : {
     129             : #ifdef LIBMESH_ENABLE_CONSTRAINTS
     130             : 
     131       51241 :   return _dof_map->n_constrained_dofs();
     132             : 
     133             : #else
     134             : 
     135             :   return 0;
     136             : 
     137             : #endif
     138             : }
     139             : 
     140             : 
     141             : 
     142       20386 : dof_id_type System::n_local_constrained_dofs() const
     143             : {
     144             : #ifdef LIBMESH_ENABLE_CONSTRAINTS
     145             : 
     146       20386 :   return _dof_map->n_local_constrained_dofs();
     147             : 
     148             : #else
     149             : 
     150             :   return 0;
     151             : 
     152             : #endif
     153             : }
     154             : 
     155             : 
     156             : 
     157     1669375 : dof_id_type System::n_local_dofs() const
     158             : {
     159     1669375 :   return _dof_map->n_local_dofs();
     160             : }
     161             : 
     162             : 
     163             : 
     164  1075980472 : Number System::current_solution (const dof_id_type global_dof_number) const
     165             : {
     166             :   // Check the sizes
     167    96018496 :   libmesh_assert_less (global_dof_number, _dof_map->n_dofs());
     168    96018496 :   libmesh_assert_less (global_dof_number, current_local_solution->size());
     169             : 
     170  1075980472 :   return (*current_local_solution)(global_dof_number);
     171             : }
     172             : 
     173             : 
     174             : 
     175         919 : void System::clear ()
     176             : {
     177         919 :   _dof_map->clear ();
     178         919 :   solution->clear ();
     179         919 :   current_local_solution->clear ();
     180             : 
     181             :   // clear any user-added vectors
     182          28 :   _vectors.clear();
     183          28 :   _vector_projections.clear();
     184          28 :   _vector_is_adjoint.clear();
     185         919 :   _is_initialized = false;
     186             : 
     187             :   // clear any user-added matrices
     188          28 :   _matrices.clear();
     189         919 :   _matrices_initialized = false;
     190             : 
     191             :   // But our "basic"/"null" state may still have a StaticCondensation
     192        2673 :   if (libMesh::on_command_line("--" + _sys_name + "-static-condensation"))
     193          70 :     this->create_static_condensation();
     194         919 : }
     195             : 
     196             : 
     197             : 
     198         633 : void System::init ()
     199             : {
     200             :   // Calling init() twice on the same system currently works evil
     201             :   // magic, whether done directly or via EquationSystems::read()
     202          18 :   libmesh_assert(!this->is_initialized());
     203             : 
     204         633 :   this->reinit_mesh();
     205         633 : }
     206             : 
     207             : 
     208             : 
     209      374149 : void System::init_data ()
     210             : {
     211       10584 :   parallel_object_only();
     212             : 
     213       21168 :   MeshBase & mesh = this->get_mesh();
     214             : 
     215             :   // Hopefully the user or the EquationSystems prepared the mesh, but
     216             :   // if not then we'd better do it now.
     217      374149 :   if (!mesh.is_prepared())
     218           0 :     mesh.complete_preparation();
     219             : 
     220             :   // Distribute the degrees of freedom on the mesh
     221      374149 :   auto total_dofs = _dof_map->distribute_dofs (mesh);
     222             : 
     223             :   // Throw an error if the total number of DOFs is not capable of
     224             :   // being indexed by our solution vector.
     225      374126 :   auto max_allowed_id = solution->max_allowed_id();
     226      374126 :   libmesh_error_msg_if(total_dofs > max_allowed_id,
     227             :                        "Cannot allocate a NumericVector with " << total_dofs << " degrees of freedom. "
     228             :                        "The vector can only index up to " << max_allowed_id << " entries.");
     229             : 
     230             :   // Recreate any user or internal constraints
     231      374126 :   this->reinit_constraints();
     232             : 
     233             :   // Even if there weren't any constraint changes,
     234             :   // reinit_constraints() did prepare_send_list() for us.
     235             : 
     236             :   // Now finally after dof distribution and construction of any
     237             :   // possible constraints, we may init any static condensation
     238             :   // data
     239      374055 :   _dof_map->reinit_static_condensation();
     240             : 
     241             :   // Resize the solution conformal to the current mesh
     242      374055 :   solution->init (this->n_dofs(), this->n_local_dofs(), false, PARALLEL);
     243             : 
     244             :   // Resize the current_local_solution for the current mesh
     245             : #ifdef LIBMESH_ENABLE_GHOSTED
     246      384635 :   current_local_solution->init (this->n_dofs(), this->n_local_dofs(),
     247             :                                 _dof_map->get_send_list(), /*fast=*/false,
     248       21160 :                                 GHOSTED);
     249             : #else
     250             :   current_local_solution->init (this->n_dofs(), false, SERIAL);
     251             : #endif
     252             : 
     253             :   // from now on, adding additional vectors or variables can't be done
     254             :   // without immediately initializing them
     255      374055 :   _is_initialized = true;
     256             : 
     257             :   // initialize & zero other vectors, if necessary
     258      419897 :   for (auto & [vec_name, vec] : _vectors)
     259             :     {
     260        1364 :       libmesh_ignore(vec_name); // spurious warning from old gcc
     261       45842 :       const ParallelType type = vec->type();
     262             : 
     263       45842 :       if (type == GHOSTED)
     264             :         {
     265             : #ifdef LIBMESH_ENABLE_GHOSTED
     266        6184 :           vec->init (this->n_dofs(), this->n_local_dofs(),
     267             :                            _dof_map->get_send_list(), /*fast=*/false,
     268         348 :                            GHOSTED);
     269             : #else
     270             :           libmesh_error_msg("Cannot initialize ghosted vectors when they are not enabled.");
     271             : #endif
     272             :         }
     273       39832 :       else if (type == SERIAL)
     274             :         {
     275           0 :           vec->init (this->n_dofs(), false, type);
     276             :         }
     277             :       else
     278             :         {
     279        1190 :           libmesh_assert_equal_to(type, PARALLEL);
     280       39832 :           vec->init (this->n_dofs(), this->n_local_dofs(), false, type);
     281             :         }
     282             :     }
     283             : 
     284             :   // Add matrices
     285      374055 :   this->add_matrices();
     286             : 
     287             :   // Clear any existing matrices
     288      395836 :   for (auto & pr : _matrices)
     289       21781 :     pr.second->clear();
     290             : 
     291             :   // Initialize the matrices for the system
     292      374055 :   if (!_basic_system_only)
     293      374055 :    this->init_matrices();
     294      374055 : }
     295             : 
     296      374149 : void System::reinit_mesh ()
     297             : {
     298       10584 :   parallel_object_only();
     299             : 
     300             :   // First initialize any required data:
     301             :   // either only the basic System data
     302      374149 :   if (_basic_system_only)
     303           0 :     System::init_data();
     304             :   // or all the derived class' data too
     305             :   else
     306      374149 :     this->init_data();
     307             : 
     308             :   // If no variables have been added to this system
     309             :   // don't do anything
     310      374055 :   if (!this->n_vars())
     311          34 :     return;
     312             : 
     313             :   // Then call the user-provided initialization function
     314      372862 :   this->user_initialization();
     315             : 
     316             : }
     317             : 
     318      374055 : void System::init_matrices ()
     319             : {
     320       10580 :   parallel_object_only();
     321             : 
     322             :   // No matrices to init
     323      374055 :   if (_matrices.empty())
     324             :     {
     325             :       // any future matrices to be added will need their own
     326             :       // initialization
     327      352983 :       _matrices_initialized = true;
     328             : 
     329      352983 :       return;
     330             :     }
     331             : 
     332             :   // Check for quick return in case the first matrix
     333             :   // (and by extension all the matrices) has already
     334             :   // been initialized
     335       21072 :   if (_matrices.begin()->second->initialized())
     336             :     {
     337           0 :       libmesh_assert(_matrices_initialized);
     338           0 :       return;
     339             :     }
     340             : 
     341       21072 :   _matrices_initialized = true;
     342             : 
     343             :   // Tell the matrices about the dof map, and vice versa
     344       42853 :   for (auto & pr : _matrices)
     345             :     {
     346         606 :       SparseMatrix<Number> & m = *(pr.second);
     347         606 :       libmesh_assert (!m.initialized());
     348             : 
     349             :       // We want to allow repeated init() on systems, but we don't
     350             :       // want to attach the same matrix to the DofMap twice
     351       21781 :       if (!this->get_dof_map().is_attached(m))
     352       21781 :         this->get_dof_map().attach_matrix(m);
     353             : 
     354             :       // If the user has already explicitly requested that this matrix use a hash table, then we
     355             :       // always honor that
     356             :       const bool use_hash =
     357       22387 :           pr.second->use_hash_table() ||
     358       21715 :           (this->_prefer_hash_table_matrix_assembly && pr.second->supports_hash_table());
     359       21781 :       pr.second->use_hash_table(use_hash);
     360             :       // Make this call after we've determined whether the matrix is using a hash table
     361       21781 :       if (pr.second->require_sparsity_pattern())
     362       21159 :         this->_require_sparsity_pattern = true;
     363             :     }
     364             : 
     365             :   // Compute the sparsity pattern for the current
     366             :   // mesh and DOF distribution.  This also updates
     367             :   // additional matrices, \p DofMap now knows them
     368       21072 :   if (this->_require_sparsity_pattern)
     369       20516 :     this->get_dof_map().compute_sparsity(this->get_mesh());
     370             : 
     371             :   // Initialize matrices and set to zero
     372       42853 :   for (auto & [name, mat] : _matrices)
     373             :     {
     374       21781 :       mat->init(_matrix_types[name]);
     375       21781 :       mat->zero();
     376             :     }
     377             : }
     378             : 
     379             : 
     380             : 
     381       31354 : void System::restrict_vectors ()
     382             : {
     383        1006 :   parallel_object_only();
     384             : 
     385             : #ifdef LIBMESH_ENABLE_AMR
     386             :   // Restrict the _vectors on the coarsened cells
     387       91539 :   for (auto & [vec_name, vec] : _vectors)
     388             :     {
     389        2140 :       NumericVector<Number> * v = vec.get();
     390             : 
     391       60185 :       if (_vector_projections[vec_name])
     392             :         {
     393       54006 :           this->project_vector (*v, this->vector_is_adjoint(vec_name));
     394             :         }
     395             :       else
     396             :         {
     397       32670 :           const ParallelType type = vec->type();
     398             : 
     399       32670 :           if (type == GHOSTED)
     400             :             {
     401             : #ifdef LIBMESH_ENABLE_GHOSTED
     402           0 :               vec->init (this->n_dofs(), this->n_local_dofs(),
     403             :                                _dof_map->get_send_list(), /*fast=*/false,
     404           0 :                                GHOSTED);
     405             : #else
     406             :               libmesh_error_msg("Cannot initialize ghosted vectors when they are not enabled.");
     407             : #endif
     408             :             }
     409             :           else
     410       32670 :             vec->init (this->n_dofs(), this->n_local_dofs(), false, type);
     411             :         }
     412             :     }
     413             : 
     414        1006 :   const std::vector<dof_id_type> & send_list = _dof_map->get_send_list ();
     415             : 
     416             :   // Restrict the solution on the coarsened cells
     417       31354 :   if (_solution_projection)
     418       60022 :     this->project_vector (*solution);
     419             :   // Or at least make sure the solution vector is the correct size
     420             :   else
     421         852 :     solution->init (this->n_dofs(), this->n_local_dofs(), true, PARALLEL);
     422             : 
     423             : #ifdef LIBMESH_ENABLE_GHOSTED
     424       32360 :   current_local_solution->init(this->n_dofs(),
     425             :                                this->n_local_dofs(), send_list,
     426        2012 :                                false, GHOSTED);
     427             : #else
     428             :   current_local_solution->init(this->n_dofs());
     429             : #endif
     430             : 
     431       31354 :   if (_solution_projection)
     432       31484 :     solution->localize (*current_local_solution, send_list);
     433             : 
     434             : #endif // LIBMESH_ENABLE_AMR
     435       31354 : }
     436             : 
     437             : 
     438             : 
     439       31283 : void System::prolong_vectors ()
     440             : {
     441             : #ifdef LIBMESH_ENABLE_AMR
     442             :   // Currently project_vector handles both restriction and prolongation
     443       31283 :   this->restrict_vectors();
     444             : #endif
     445       31283 : }
     446             : 
     447             : 
     448             : 
     449       30141 : void System::reinit ()
     450             : {
     451         970 :   parallel_object_only();
     452             : 
     453             :   // project_vector handles vector initialization now
     454         970 :   libmesh_assert_equal_to (solution->size(), current_local_solution->size());
     455             : 
     456             :   // Make sure our static condensation dof map is up-to-date before we init any
     457             :   // static condensation matrices
     458       30141 :   this->get_dof_map().reinit_static_condensation();
     459             : 
     460       30141 :   if (!_matrices.empty() && !_basic_system_only)
     461             :     {
     462             :       // Clear the matrices
     463       47024 :       for (auto & pr : _matrices)
     464             :         {
     465       23617 :           pr.second->clear();
     466       23617 :           pr.second->attach_dof_map(this->get_dof_map());
     467             :         }
     468             : 
     469       23407 :       if (this->_require_sparsity_pattern)
     470             :         {
     471             :           // Clear the sparsity pattern
     472       19767 :           this->get_dof_map().clear_sparsity();
     473             : 
     474             :           // Compute the sparsity pattern for the current
     475             :           // mesh and DOF distribution.  This also updates
     476             :           // additional matrices, \p DofMap now knows them
     477       19767 :           this->get_dof_map().compute_sparsity (this->get_mesh());
     478             :         }
     479             : 
     480             :       // Initialize matrices and set to zero
     481       47024 :       for (auto & pr : _matrices)
     482             :         {
     483       23617 :           pr.second->init();
     484       23617 :           pr.second->zero();
     485             :         }
     486             :     }
     487       30141 : }
     488             : 
     489             : 
     490      406328 : void System::reinit_constraints()
     491             : {
     492       11612 :   parallel_object_only();
     493             : 
     494             : #ifdef LIBMESH_ENABLE_CONSTRAINTS
     495      406328 :   get_dof_map().create_dof_constraints(_mesh, this->time);
     496      406328 :   user_constrain();
     497      406328 :   get_dof_map().process_constraints(_mesh);
     498      800904 :   if (libMesh::on_command_line ("--print-constraints"))
     499           0 :     get_dof_map().print_dof_constraints(libMesh::out);
     500             : #endif
     501      406257 :   get_dof_map().prepare_send_list();
     502      406257 : }
     503             : 
     504             : 
     505             : 
     506         639 : void System::reinit_dirichlet_constraint_values()
     507             : {
     508          18 :   parallel_object_only();
     509             : 
     510             : #ifdef LIBMESH_ENABLE_CONSTRAINTS
     511         639 :   get_dof_map().reinit_dirichlet_constraint_values(_mesh, this->time);
     512         639 :   user_constrain();
     513         639 :   get_dof_map().process_constraints(_mesh);
     514        1260 :   if (libMesh::on_command_line ("--print-constraints"))
     515           0 :     get_dof_map().print_dof_constraints(libMesh::out);
     516             : #endif
     517         639 :   get_dof_map().prepare_send_list();
     518         639 : }
     519             : 
     520             : 
     521     1471326 : void System::update ()
     522             : {
     523       37778 :   parallel_object_only();
     524             : 
     525       37778 :   libmesh_assert(solution->closed());
     526             : 
     527       37778 :   const std::vector<dof_id_type> & send_list = _dof_map->get_send_list ();
     528             : 
     529             :   // Check sizes
     530       37778 :   libmesh_assert_equal_to (current_local_solution->size(), solution->size());
     531             :   // More processors than elements => empty send_list
     532             :   //  libmesh_assert (!send_list.empty());
     533       37778 :   libmesh_assert_less_equal (send_list.size(), solution->size());
     534             : 
     535             :   // Create current_local_solution from solution.  This will
     536             :   // put a local copy of solution into current_local_solution.
     537             :   // Only the necessary values (specified by the send_list)
     538             :   // are copied to minimize communication
     539     1508288 :   solution->localize (*current_local_solution, send_list);
     540     1471326 : }
     541             : 
     542             : 
     543             : 
     544       30076 : void System::re_update ()
     545             : {
     546         970 :   parallel_object_only();
     547             : 
     548             :   // If this system is empty... don't do anything!
     549       30076 :   if (!this->n_vars())
     550         108 :     return;
     551             : 
     552         862 :   const std::vector<dof_id_type> & send_list = this->get_dof_map().get_send_list ();
     553             : 
     554             :   // Check sizes
     555         862 :   libmesh_assert_equal_to (current_local_solution->size(), solution->size());
     556             :   // Not true with ghosted vectors
     557             :   // libmesh_assert_equal_to (current_local_solution->local_size(), solution->size());
     558             :   // libmesh_assert (!send_list.empty());
     559         862 :   libmesh_assert_less_equal (send_list.size(), solution->size());
     560             : 
     561             :   // Create current_local_solution from solution.  This will
     562             :   // put a local copy of solution into current_local_solution.
     563       27156 :   solution->localize (*current_local_solution, send_list);
     564             : }
     565             : 
     566             : 
     567             : 
     568           0 : void System::restrict_solve_to (const SystemSubset * subset,
     569             :                                 const SubsetSolveMode /*subset_solve_mode*/)
     570             : {
     571           0 :   if (subset != nullptr)
     572           0 :     libmesh_not_implemented();
     573           0 : }
     574             : 
     575             : 
     576             : 
     577       46544 : void System::assemble ()
     578             : {
     579             :   // Log how long the user's assembly code takes
     580        4650 :   LOG_SCOPE("assemble()", "System");
     581             : 
     582        1550 :   libmesh_assert(this->get_mesh().is_prepared());
     583             : #if defined(DEBUG) && !defined(LIBMESH_ENABLE_DEPRECATED)
     584             :   MeshTools::libmesh_assert_valid_is_prepared(this->get_mesh());
     585             : #endif
     586             : 
     587             :   // Call the user-specified assembly function
     588       46544 :   this->user_assembly();
     589       46544 : }
     590             : 
     591             : 
     592             : 
     593           0 : void System::assemble_qoi (const QoISet & qoi_indices)
     594             : {
     595             :   // Log how long the user's assembly code takes
     596           0 :   LOG_SCOPE("assemble_qoi()", "System");
     597             : 
     598           0 :   libmesh_assert(this->get_mesh().is_prepared());
     599             : #if defined(DEBUG) && !defined(LIBMESH_ENABLE_DEPRECATED)
     600             :   MeshTools::libmesh_assert_valid_is_prepared(this->get_mesh());
     601             : #endif
     602             : 
     603             :   // Call the user-specified quantity of interest function
     604           0 :   this->user_QOI(qoi_indices);
     605           0 : }
     606             : 
     607             : 
     608             : 
     609           0 : void System::assemble_qoi_derivative(const QoISet & qoi_indices,
     610             :                                      bool include_liftfunc,
     611             :                                      bool apply_constraints)
     612             : {
     613             :   // Log how long the user's assembly code takes
     614           0 :   LOG_SCOPE("assemble_qoi_derivative()", "System");
     615             : 
     616           0 :   libmesh_assert(this->get_mesh().is_prepared());
     617             : #if defined(DEBUG) && !defined(LIBMESH_ENABLE_DEPRECATED)
     618             :   MeshTools::libmesh_assert_valid_is_prepared(this->get_mesh());
     619             : #endif
     620             : 
     621             :   // Call the user-specified quantity of interest function
     622           0 :   this->user_QOI_derivative(qoi_indices, include_liftfunc,
     623           0 :                             apply_constraints);
     624           0 : }
     625             : 
     626             : 
     627             : 
     628           0 : void System::qoi_parameter_sensitivity (const QoISet & qoi_indices,
     629             :                                         const ParameterVector & parameters_vec,
     630             :                                         SensitivityData & sensitivities)
     631             : {
     632             :   // Forward sensitivities are more efficient for Nq > Np
     633           0 :   if (qoi_indices.size(*this) > parameters_vec.size())
     634           0 :     forward_qoi_parameter_sensitivity(qoi_indices, parameters_vec, sensitivities);
     635             :   // Adjoint sensitivities are more efficient for Np > Nq,
     636             :   // and an adjoint may be more reusable than a forward
     637             :   // solution sensitivity in the Np == Nq case.
     638             :   else
     639           0 :     adjoint_qoi_parameter_sensitivity(qoi_indices, parameters_vec, sensitivities);
     640           0 : }
     641             : 
     642             : 
     643             : 
     644           0 : bool System::compare (const System & other_system,
     645             :                       const Real threshold,
     646             :                       const bool verbose) const
     647             : {
     648             :   // we do not care for matrices, but for vectors
     649           0 :   libmesh_assert (_is_initialized);
     650           0 :   libmesh_assert (other_system._is_initialized);
     651             : 
     652           0 :   if (verbose)
     653             :     {
     654           0 :       libMesh::out << "  Systems \"" << _sys_name << "\"" << std::endl;
     655           0 :       libMesh::out << "   comparing matrices not supported." << std::endl;
     656           0 :       libMesh::out << "   comparing names...";
     657             :     }
     658             : 
     659             :   // compare the name: 0 means identical
     660           0 :   const int name_result = _sys_name.compare(other_system.name());
     661           0 :   if (verbose)
     662             :     {
     663           0 :       if (name_result == 0)
     664           0 :         libMesh::out << " identical." << std::endl;
     665             :       else
     666           0 :         libMesh::out << "  names not identical." << std::endl;
     667           0 :       libMesh::out << "   comparing solution vector...";
     668             :     }
     669             : 
     670             : 
     671             :   // compare the solution: -1 means identical
     672           0 :   const int solu_result = solution->compare (*other_system.solution.get(),
     673           0 :                                              threshold);
     674             : 
     675           0 :   if (verbose)
     676             :     {
     677           0 :       if (solu_result == -1)
     678           0 :         libMesh::out << " identical up to threshold." << std::endl;
     679             :       else
     680           0 :         libMesh::out << "  first difference occurred at index = "
     681           0 :                      << solu_result << "." << std::endl;
     682             :     }
     683             : 
     684             : 
     685             :   // safety check, whether we handle at least the same number
     686             :   // of vectors
     687           0 :   std::vector<int> ov_result;
     688             : 
     689           0 :   if (this->n_vectors() != other_system.n_vectors())
     690             :     {
     691           0 :       if (verbose)
     692             :         {
     693           0 :           libMesh::out << "   Fatal difference. This system handles "
     694           0 :                        << this->n_vectors() << " add'l vectors," << std::endl
     695           0 :                        << "   while the other system handles "
     696           0 :                        << other_system.n_vectors()
     697           0 :                        << " add'l vectors." << std::endl
     698           0 :                        << "   Aborting comparison." << std::endl;
     699             :         }
     700           0 :       return false;
     701             :     }
     702           0 :   else if (this->n_vectors() == 0)
     703             :     {
     704             :       // there are no additional vectors...
     705           0 :       ov_result.clear ();
     706             :     }
     707             :   else
     708             :     {
     709             :       // compare other vectors
     710           0 :       for (auto & [vec_name, vec] : _vectors)
     711             :         {
     712           0 :           if (verbose)
     713           0 :             libMesh::out << "   comparing vector \""
     714           0 :                          << vec_name << "\" ...";
     715             : 
     716             :           // assume they have the same name
     717             :           const NumericVector<Number> & other_system_vector =
     718           0 :             other_system.get_vector(vec_name);
     719             : 
     720           0 :           ov_result.push_back(vec->compare(other_system_vector, threshold));
     721             : 
     722           0 :           if (verbose)
     723             :             {
     724           0 :               if (ov_result[ov_result.size()-1] == -1)
     725           0 :                 libMesh::out << " identical up to threshold." << std::endl;
     726             :               else
     727           0 :                 libMesh::out << " first difference occurred at" << std::endl
     728           0 :                              << "   index = " << ov_result[ov_result.size()-1] << "." << std::endl;
     729             :             }
     730             :         }
     731             :     } // finished comparing additional vectors
     732             : 
     733             : 
     734             :   bool overall_result;
     735             : 
     736             :   // sum up the results
     737           0 :   if ((name_result==0) && (solu_result==-1))
     738             :     {
     739           0 :       if (ov_result.size()==0)
     740           0 :         overall_result = true;
     741             :       else
     742             :         {
     743             :           bool ov_identical;
     744           0 :           unsigned int n    = 0;
     745           0 :           do
     746             :             {
     747           0 :               ov_identical = (ov_result[n]==-1);
     748           0 :               n++;
     749             :             }
     750           0 :           while (ov_identical && n<ov_result.size());
     751           0 :           overall_result = ov_identical;
     752           0 :         }
     753             :     }
     754             :   else
     755           0 :     overall_result = false;
     756             : 
     757           0 :   if (verbose)
     758             :     {
     759           0 :       libMesh::out << "   finished comparisons, ";
     760           0 :       if (overall_result)
     761           0 :         libMesh::out << "found no differences." << std::endl << std::endl;
     762             :       else
     763           0 :         libMesh::out << "found differences." << std::endl << std::endl;
     764             :     }
     765             : 
     766           0 :   return overall_result;
     767             : }
     768             : 
     769             : 
     770             : 
     771          71 : void System::update_global_solution (std::vector<Number> & global_soln) const
     772             : {
     773           2 :   parallel_object_only();
     774             : 
     775          71 :   global_soln.resize (solution->size());
     776             : 
     777          71 :   solution->localize (global_soln);
     778          71 : }
     779             : 
     780             : 
     781             : 
     782        5578 : void System::update_global_solution (std::vector<Number> & global_soln,
     783             :                                      const processor_id_type dest_proc) const
     784             : {
     785         232 :   parallel_object_only();
     786             : 
     787        5578 :   global_soln.resize        (solution->size());
     788             : 
     789        5578 :   solution->localize_to_one (global_soln, dest_proc);
     790        5578 : }
     791             : 
     792             : 
     793             : 
     794      212062 : NumericVector<Number> & System::add_vector (std::string_view vec_name,
     795             :                                             const bool projections,
     796             :                                             const ParallelType type)
     797             : {
     798        6300 :   parallel_object_only();
     799             : 
     800        6300 :   libmesh_assert(this->comm().verify(std::string(vec_name)));
     801        6300 :   libmesh_assert(this->comm().verify(int(type)));
     802        6300 :   libmesh_assert(this->comm().verify(projections));
     803             : 
     804             :   // Return the vector if it is already there.
     805      212062 :   if (auto it = this->_vectors.find(vec_name);
     806        6300 :       it != this->_vectors.end())
     807             :     {
     808             :       // If the projection setting has *upgraded*, change it.
     809      119165 :       if (projections) // only do expensive lookup if needed
     810       46734 :         libmesh_map_find(_vector_projections, vec_name) = projections;
     811             : 
     812        3550 :       NumericVector<Number> & vec = *it->second;
     813             : 
     814             :       // If we're in serial, our vectors are effectively SERIAL, so
     815             :       // we'll ignore any type setting.  If we're in parallel, we
     816             :       // might have a type change to deal with.
     817             : 
     818      122715 :       if (this->n_processors() > 1)
     819             :         {
     820             :           // If the type setting has changed in a way we can't
     821             :           // perceive as an upgrade or a downgrade, scream.
     822        3550 :           libmesh_assert_equal_to(type == SERIAL,
     823             :                                   vec.type() == SERIAL);
     824             : 
     825             :           // If the type setting has *upgraded*, change it.
     826      117339 :           if (type == GHOSTED && vec.type() == PARALLEL)
     827             :             {
     828             :               // A *really* late upgrade is expensive, but better not
     829             :               // to risk zeroing data.
     830         138 :               if (vec.initialized())
     831             :                 {
     832          69 :                   if (!vec.closed())
     833           0 :                     vec.close();
     834             : 
     835             :                   // Ideally we'd move parallel coefficients and then
     836             :                   // add ghosted coefficients, but copy and swap is
     837             :                   // simpler.  If anyone actually ever uses this case
     838             :                   // for real we can look into optimizing it.
     839          71 :                   auto new_vec = NumericVector<Number>::build(this->comm());
     840         134 :                   new_vec->set_options_prefix(_options_prefix);
     841             : #ifdef LIBMESH_ENABLE_GHOSTED
     842          71 :                   new_vec->init (this->n_dofs(), this->n_local_dofs(),
     843             :                                  _dof_map->get_send_list(), /*fast=*/false,
     844           4 :                                  GHOSTED);
     845             : #else
     846             :                   libmesh_error_msg("Cannot initialize ghosted vectors when they are not enabled.");
     847             : #endif
     848             : 
     849          69 :                   *new_vec = vec;
     850          71 :                   vec.swap(*new_vec);
     851          65 :                 }
     852             :               else
     853             :                 // The PARALLEL vec is not yet initialized, so we can
     854             :                 // just "upgrade" it to GHOSTED.
     855          69 :                 vec.set_type(type);
     856             :             }
     857             :         }
     858             : 
     859             :       // Any upgrades are done; we're happy here.
     860        3550 :       return vec;
     861             :     }
     862             : 
     863             :   // Otherwise, build the vector. The following emplace() is
     864             :   // guaranteed to succeed because, if we made it here, we don't
     865             :   // already have a vector named "vec_name". We pass the user's
     866             :   // requested ParallelType directly to NumericVector::build() so
     867             :   // that, even if the vector is not initialized now, it will get the
     868             :   // right type when it is initialized later.
     869             :   auto pr =
     870             :     _vectors.emplace(vec_name,
     871      185794 :                      NumericVector<Number>::build(this->comm(),
     872             :                                                   libMesh::default_solver_package(),
     873        5500 :                                                   type));
     874        2750 :   auto buf = pr.first->second.get();
     875      180294 :   buf->set_options_prefix(_options_prefix);
     876        2750 :   _vector_projections.emplace(vec_name, projections);
     877             : 
     878             :   // Vectors are primal by default
     879       92897 :   _vector_is_adjoint.emplace(vec_name, -1);
     880             : 
     881             :   // Initialize it if necessary
     882       92897 :   if (_is_initialized)
     883             :     {
     884       45702 :       if (type == GHOSTED)
     885             :         {
     886             : #ifdef LIBMESH_ENABLE_GHOSTED
     887        4200 :           buf->init (this->n_dofs(), this->n_local_dofs(),
     888             :                      _dof_map->get_send_list(), /*fast=*/false,
     889         240 :                      GHOSTED);
     890             : #else
     891             :           libmesh_error_msg("Cannot initialize ghosted vectors when they are not enabled.");
     892             : #endif
     893             :         }
     894             :       else
     895       41502 :         buf->init (this->n_dofs(), this->n_local_dofs(), false, type);
     896             :     }
     897             : 
     898        2750 :   return *buf;
     899             : }
     900             : 
     901       39413 : void System::remove_vector (std::string_view vec_name)
     902             : {
     903        1126 :   parallel_object_only();  // Not strictly needed, but the only safe way to keep in sync
     904             : 
     905       39413 :   if (const auto pos = _vectors.find(vec_name);
     906        1126 :       pos != _vectors.end())
     907             :     {
     908       38287 :       _vectors.erase(pos);
     909        1126 :       auto proj_it = _vector_projections.find(vec_name);
     910        1126 :       libmesh_assert(proj_it != _vector_projections.end());
     911       38287 :       _vector_projections.erase(proj_it);
     912             : 
     913        1126 :       auto adj_it = _vector_is_adjoint.find(vec_name);
     914        1126 :       libmesh_assert(adj_it != _vector_is_adjoint.end());
     915       38287 :       _vector_is_adjoint.erase(adj_it);
     916             :     }
     917       39413 : }
     918             : 
     919           0 : const NumericVector<Number> * System::request_vector (std::string_view vec_name) const
     920             : {
     921           0 :   if (const auto pos = _vectors.find(vec_name);
     922           0 :       pos != _vectors.end())
     923           0 :     return pos->second.get();
     924             : 
     925             :   // Otherwise, vec_name was not found
     926           0 :   return nullptr;
     927             : }
     928             : 
     929             : 
     930             : 
     931           0 : NumericVector<Number> * System::request_vector (std::string_view vec_name)
     932             : {
     933           0 :   if (auto pos = _vectors.find(vec_name);
     934           0 :       pos != _vectors.end())
     935           0 :     return pos->second.get();
     936             : 
     937             :   // Otherwise, vec_name was not found
     938           0 :   return nullptr;
     939             : }
     940             : 
     941             : 
     942             : 
     943           0 : const NumericVector<Number> * System::request_vector (const unsigned int vec_num) const
     944             : {
     945             :   // If we don't have that many vectors, return nullptr
     946           0 :   if (vec_num >= _vectors.size())
     947           0 :     return nullptr;
     948             : 
     949             :   // Otherwise return a pointer to the vec_num'th vector
     950           0 :   auto it = vectors_begin();
     951           0 :   std::advance(it, vec_num);
     952           0 :   return it->second.get();
     953             : }
     954             : 
     955             : 
     956             : 
     957           0 : NumericVector<Number> * System::request_vector (const unsigned int vec_num)
     958             : {
     959             :   // If we don't have that many vectors, return nullptr
     960           0 :   if (vec_num >= _vectors.size())
     961           0 :     return nullptr;
     962             : 
     963             :   // Otherwise return a pointer to the vec_num'th vector
     964           0 :   auto it = vectors_begin();
     965           0 :   std::advance(it, vec_num);
     966           0 :   return it->second.get();
     967             : }
     968             : 
     969             : 
     970             : 
     971        5792 : const NumericVector<Number> & System::get_vector (std::string_view vec_name) const
     972             : {
     973        5792 :   return *(libmesh_map_find(_vectors, vec_name));
     974             : }
     975             : 
     976             : 
     977             : 
     978     6371820 : NumericVector<Number> & System::get_vector (std::string_view vec_name)
     979             : {
     980     6371820 :   return *(libmesh_map_find(_vectors, vec_name));
     981             : }
     982             : 
     983             : 
     984             : 
     985           0 : const NumericVector<Number> & System::get_vector (const unsigned int vec_num) const
     986             : {
     987             :   // If we don't have that many vectors, throw an error
     988           0 :   libmesh_assert_less(vec_num, _vectors.size());
     989             : 
     990             :   // Otherwise return a reference to the vec_num'th vector
     991           0 :   auto it = vectors_begin();
     992           0 :   std::advance(it, vec_num);
     993           0 :   return *(it->second);
     994             : }
     995             : 
     996             : 
     997             : 
     998           0 : NumericVector<Number> & System::get_vector (const unsigned int vec_num)
     999             : {
    1000             :   // If we don't have that many vectors, throw an error
    1001           0 :   libmesh_assert_less(vec_num, _vectors.size());
    1002             : 
    1003             :   // Otherwise return a reference to the vec_num'th vector
    1004           0 :   auto it = vectors_begin();
    1005           0 :   std::advance(it, vec_num);
    1006           0 :   return *(it->second);
    1007             : }
    1008             : 
    1009             : 
    1010             : 
    1011           0 : const std::string & System::vector_name (const unsigned int vec_num) const
    1012             : {
    1013             :   // If we don't have that many vectors, throw an error
    1014           0 :   libmesh_assert_less(vec_num, _vectors.size());
    1015             : 
    1016             :   // Otherwise return a reference to the vec_num'th vector name
    1017           0 :   auto it = vectors_begin();
    1018           0 :   std::advance(it, vec_num);
    1019           0 :   return it->first;
    1020             : }
    1021             : 
    1022       66238 : const std::string & System::vector_name (const NumericVector<Number> & vec_reference) const
    1023             : {
    1024             :   // Linear search for a vector whose pointer matches vec_reference
    1025       62454 :   auto it = std::find_if(vectors_begin(), vectors_end(),
    1026       31852 :                          [&vec_reference](const decltype(_vectors)::value_type & pr)
    1027       17818 :                          { return &vec_reference == pr.second.get(); });
    1028             : 
    1029             :   // Before returning, make sure we didn't loop till the end and not find any match
    1030        1892 :   libmesh_assert (it != vectors_end());
    1031             : 
    1032             :   // Return the string associated with the current vector
    1033       66238 :   return it->first;
    1034             : }
    1035             : 
    1036             : 
    1037             : 
    1038       21291 : SparseMatrix<Number> & System::add_matrix (std::string_view mat_name,
    1039             :                                            const ParallelType type,
    1040             :                                            const MatrixBuildType mat_build_type)
    1041             : {
    1042         592 :   parallel_object_only();
    1043             : 
    1044         592 :   libmesh_assert(this->comm().verify(std::string(mat_name)));
    1045         592 :   libmesh_assert(this->comm().verify(int(type)));
    1046         592 :   libmesh_assert(this->comm().verify(int(mat_build_type)));
    1047             : 
    1048             :   // Return the matrix if it is already there.
    1049       21291 :   if (auto it = this->_matrices.find(mat_name);
    1050         592 :       it != this->_matrices.end())
    1051           0 :     return *it->second;
    1052             : 
    1053             :   // Otherwise build the matrix to return.
    1054       20699 :   std::unique_ptr<SparseMatrix<Number>> matrix;
    1055       21291 :   if (this->has_static_condensation())
    1056             :     {
    1057           0 :       if (mat_build_type == MatrixBuildType::DIAGONAL)
    1058           0 :         libmesh_error_msg(
    1059             :             "We do not currently support static condensation of the diagonal matrix type");
    1060           0 :       matrix = std::make_unique<StaticCondensation>(this->get_mesh(),
    1061             :                                                     *this,
    1062             :                                                     this->get_dof_map(),
    1063           0 :                                                     this->get_dof_map().get_static_condensation());
    1064             :     }
    1065             :   else
    1066       41990 :     matrix = SparseMatrix<Number>::build(this->comm(), libMesh::default_solver_package());
    1067         592 :   auto & mat = *matrix;
    1068       41398 :   mat.set_options_prefix(_options_prefix);
    1069             : 
    1070         592 :   _matrices.emplace(mat_name, std::move(matrix));
    1071             : 
    1072         592 :   _matrix_types.emplace(mat_name, type);
    1073             : 
    1074             :   // Initialize it first if we've already initialized the others.
    1075       21291 :   this->late_matrix_init(mat, type);
    1076             : 
    1077         592 :   return mat;
    1078       20107 : }
    1079             : 
    1080             : 
    1081             : 
    1082         630 : SparseMatrix<Number> & System::add_matrix (std::string_view mat_name,
    1083             :                                            std::unique_ptr<SparseMatrix<Number>> matrix,
    1084             :                                            const ParallelType type)
    1085             : {
    1086          18 :   parallel_object_only();
    1087             : 
    1088         612 :   const std::string namestr{mat_name};
    1089             : 
    1090          18 :   libmesh_assert(this->comm().verify(namestr));
    1091          18 :   libmesh_assert(this->comm().verify(int(type)));
    1092             : 
    1093          18 :   SparseMatrix<Number> & mat = *matrix;
    1094         630 :   mat.set_options_prefix(_options_prefix);
    1095             : 
    1096         630 :   _matrices[namestr] = std::move(matrix);
    1097         630 :   _matrix_types[namestr] = type;
    1098             : 
    1099             :   // Initialize it first if we've already initialized the others.
    1100         630 :   this->late_matrix_init(mat, type);
    1101             : 
    1102         648 :   return mat;
    1103             : }
    1104             : 
    1105       21921 : void System::late_matrix_init(SparseMatrix<Number> & mat,
    1106             :                               ParallelType type)
    1107             : {
    1108       21921 :   if (_matrices_initialized)
    1109             :     {
    1110           0 :       this->get_dof_map().attach_matrix(mat);
    1111           0 :       mat.init(type);
    1112             :     }
    1113       21921 : }
    1114             : 
    1115             : 
    1116             : 
    1117          70 : void System::set_options_prefix(std::string options_prefix)
    1118             : {
    1119          70 :   _options_prefix = std::move(options_prefix);
    1120             : 
    1121         136 :   solution->set_options_prefix(_options_prefix);
    1122         136 :   current_local_solution->set_options_prefix(_options_prefix);
    1123         210 :   for (auto & pr : _vectors)
    1124         272 :     pr.second->set_options_prefix(_options_prefix);
    1125          70 :   for (auto & pr : _matrices)
    1126           0 :     pr.second->set_options_prefix(_options_prefix);
    1127          70 : }
    1128             : 
    1129             : 
    1130             : 
    1131             : 
    1132           0 : void System::remove_matrix (std::string_view mat_name)
    1133             : {
    1134           0 :   parallel_object_only();  // Not strictly needed, but the only safe way to keep in sync
    1135             : 
    1136           0 :   if (const auto pos = _matrices.find(mat_name);
    1137           0 :       pos != _matrices.end())
    1138           0 :     _matrices.erase(pos); // erase()'d entries are destroyed
    1139           0 : }
    1140             : 
    1141             : 
    1142             : 
    1143          14 : const SparseMatrix<Number> * System::request_matrix (std::string_view mat_name) const
    1144             : {
    1145          14 :   if (const auto pos = _matrices.find(mat_name);
    1146          14 :       pos != _matrices.end())
    1147          14 :     return pos->second.get();
    1148             : 
    1149             :   // Otherwise, mat_name does not exist
    1150           0 :   return nullptr;
    1151             : }
    1152             : 
    1153             : 
    1154             : 
    1155      258866 : SparseMatrix<Number> * System::request_matrix (std::string_view mat_name)
    1156             : {
    1157      258866 :   if (auto pos = _matrices.find(mat_name);
    1158        7554 :       pos != _matrices.end())
    1159           2 :     return pos->second.get();
    1160             : 
    1161             :   // Otherwise, mat_name does not exist
    1162        7552 :   return nullptr;
    1163             : }
    1164             : 
    1165             : 
    1166             : 
    1167           0 : const SparseMatrix<Number> & System::get_matrix (std::string_view mat_name) const
    1168             : {
    1169           0 :   return *libmesh_map_find(_matrices, mat_name);
    1170             : }
    1171             : 
    1172             : 
    1173             : 
    1174      667105 : SparseMatrix<Number> & System::get_matrix (std::string_view mat_name)
    1175             : {
    1176      667105 :   return *libmesh_map_find(_matrices, mat_name);
    1177             : }
    1178             : 
    1179             : 
    1180             : 
    1181       69038 : void System::set_vector_preservation (const std::string & vec_name,
    1182             :                                       bool preserve)
    1183             : {
    1184        1972 :   parallel_object_only();  // Not strictly needed, but the only safe way to keep in sync
    1185             : 
    1186       69038 :   _vector_projections[vec_name] = preserve;
    1187       69038 : }
    1188             : 
    1189             : 
    1190             : 
    1191      159257 : bool System::vector_preservation (std::string_view vec_name) const
    1192             : {
    1193      159257 :   if (auto it = _vector_projections.find(vec_name);
    1194        4550 :       it != _vector_projections.end())
    1195      159257 :     return it->second;
    1196             : 
    1197             :   // vec_name was not in the map, return false
    1198           0 :   return false;
    1199             : }
    1200             : 
    1201             : 
    1202             : 
    1203       24746 : void System::set_vector_as_adjoint (const std::string & vec_name,
    1204             :                                     int qoi_num)
    1205             : {
    1206         798 :   parallel_object_only();  // Not strictly needed, but the only safe way to keep in sync
    1207             : 
    1208             :   // We reserve -1 for vectors which get primal constraints, -2 for
    1209             :   // vectors which get no constraints
    1210         798 :   libmesh_assert_greater_equal(qoi_num, -2);
    1211       24746 :   _vector_is_adjoint[vec_name] = qoi_num;
    1212       24746 : }
    1213             : 
    1214             : 
    1215             : 
    1216       27515 : int System::vector_is_adjoint (std::string_view vec_name) const
    1217             : {
    1218        1024 :   const auto it = _vector_is_adjoint.find(vec_name);
    1219        1024 :   libmesh_assert(it != _vector_is_adjoint.end());
    1220       27515 :   return it->second;
    1221             : }
    1222             : 
    1223             : 
    1224             : 
    1225         142 : NumericVector<Number> & System::add_sensitivity_solution (unsigned int i)
    1226             : {
    1227         142 :   std::ostringstream sensitivity_name;
    1228         138 :   sensitivity_name << "sensitivity_solution" << i;
    1229             : 
    1230         284 :   return this->add_vector(sensitivity_name.str());
    1231         134 : }
    1232             : 
    1233             : 
    1234             : 
    1235         284 : NumericVector<Number> & System::get_sensitivity_solution (unsigned int i)
    1236             : {
    1237         284 :   std::ostringstream sensitivity_name;
    1238         276 :   sensitivity_name << "sensitivity_solution" << i;
    1239             : 
    1240         568 :   return this->get_vector(sensitivity_name.str());
    1241         268 : }
    1242             : 
    1243             : 
    1244             : 
    1245           0 : const NumericVector<Number> & System::get_sensitivity_solution (unsigned int i) const
    1246             : {
    1247           0 :   std::ostringstream sensitivity_name;
    1248           0 :   sensitivity_name << "sensitivity_solution" << i;
    1249             : 
    1250           0 :   return this->get_vector(sensitivity_name.str());
    1251           0 : }
    1252             : 
    1253             : 
    1254             : 
    1255           0 : NumericVector<Number> & System::add_weighted_sensitivity_solution ()
    1256             : {
    1257           0 :   return this->add_vector("weighted_sensitivity_solution");
    1258             : }
    1259             : 
    1260             : 
    1261             : 
    1262           0 : NumericVector<Number> & System::get_weighted_sensitivity_solution ()
    1263             : {
    1264           0 :   return this->get_vector("weighted_sensitivity_solution");
    1265             : }
    1266             : 
    1267             : 
    1268             : 
    1269           0 : const NumericVector<Number> & System::get_weighted_sensitivity_solution () const
    1270             : {
    1271           0 :   return this->get_vector("weighted_sensitivity_solution");
    1272             : }
    1273             : 
    1274             : 
    1275             : 
    1276       24746 : NumericVector<Number> & System::add_adjoint_solution (unsigned int i)
    1277             : {
    1278       24746 :   std::ostringstream adjoint_name;
    1279       23948 :   adjoint_name << "adjoint_solution" << i;
    1280             : 
    1281       24746 :   NumericVector<Number> & returnval = this->add_vector(adjoint_name.str());
    1282       25544 :   this->set_vector_as_adjoint(adjoint_name.str(), i);
    1283       25544 :   return returnval;
    1284       23150 : }
    1285             : 
    1286             : 
    1287             : 
    1288     2874725 : NumericVector<Number> & System::get_adjoint_solution (unsigned int i)
    1289             : {
    1290     2874725 :   std::ostringstream adjoint_name;
    1291     2655887 :   adjoint_name << "adjoint_solution" << i;
    1292             : 
    1293     5749450 :   return this->get_vector(adjoint_name.str());
    1294     2436861 : }
    1295             : 
    1296             : 
    1297             : 
    1298        5792 : const NumericVector<Number> & System::get_adjoint_solution (unsigned int i) const
    1299             : {
    1300        5792 :   std::ostringstream adjoint_name;
    1301        5480 :   adjoint_name << "adjoint_solution" << i;
    1302             : 
    1303       11584 :   return this->get_vector(adjoint_name.str());
    1304        5168 : }
    1305             : 
    1306             : 
    1307             : 
    1308           0 : NumericVector<Number> & System::add_weighted_sensitivity_adjoint_solution (unsigned int i)
    1309             : {
    1310           0 :   std::ostringstream adjoint_name;
    1311           0 :   adjoint_name << "weighted_sensitivity_adjoint_solution" << i;
    1312             : 
    1313           0 :   NumericVector<Number> & returnval = this->add_vector(adjoint_name.str());
    1314           0 :   this->set_vector_as_adjoint(adjoint_name.str(), i);
    1315           0 :   return returnval;
    1316           0 : }
    1317             : 
    1318             : 
    1319             : 
    1320           0 : NumericVector<Number> & System::get_weighted_sensitivity_adjoint_solution (unsigned int i)
    1321             : {
    1322           0 :   std::ostringstream adjoint_name;
    1323           0 :   adjoint_name << "weighted_sensitivity_adjoint_solution" << i;
    1324             : 
    1325           0 :   return this->get_vector(adjoint_name.str());
    1326           0 : }
    1327             : 
    1328             : 
    1329             : 
    1330           0 : const NumericVector<Number> & System::get_weighted_sensitivity_adjoint_solution (unsigned int i) const
    1331             : {
    1332           0 :   std::ostringstream adjoint_name;
    1333           0 :   adjoint_name << "weighted_sensitivity_adjoint_solution" << i;
    1334             : 
    1335           0 :   return this->get_vector(adjoint_name.str());
    1336           0 : }
    1337             : 
    1338             : 
    1339             : 
    1340       24817 : NumericVector<Number> & System::add_adjoint_rhs (unsigned int i)
    1341             : {
    1342       24817 :   std::ostringstream adjoint_rhs_name;
    1343       24017 :   adjoint_rhs_name << "adjoint_rhs" << i;
    1344             : 
    1345       49634 :   return this->add_vector(adjoint_rhs_name.str(), false);
    1346       23217 : }
    1347             : 
    1348             : 
    1349             : 
    1350     2562841 : NumericVector<Number> & System::get_adjoint_rhs (unsigned int i)
    1351             : {
    1352     2562841 :   std::ostringstream adjoint_rhs_name;
    1353     2332791 :   adjoint_rhs_name << "adjoint_rhs" << i;
    1354             : 
    1355     5125682 :   return this->get_vector(adjoint_rhs_name.str());
    1356     2102741 : }
    1357             : 
    1358             : 
    1359             : 
    1360           0 : const NumericVector<Number> & System::get_adjoint_rhs (unsigned int i) const
    1361             : {
    1362           0 :   std::ostringstream adjoint_rhs_name;
    1363           0 :   adjoint_rhs_name << "adjoint_rhs" << i;
    1364             : 
    1365           0 :   return this->get_vector(adjoint_rhs_name.str());
    1366           0 : }
    1367             : 
    1368             : 
    1369             : 
    1370        7430 : NumericVector<Number> & System::add_sensitivity_rhs (unsigned int i)
    1371             : {
    1372        7430 :   std::ostringstream sensitivity_rhs_name;
    1373        7218 :   sensitivity_rhs_name << "sensitivity_rhs" << i;
    1374             : 
    1375       14860 :   return this->add_vector(sensitivity_rhs_name.str(), false);
    1376        7006 : }
    1377             : 
    1378             : 
    1379             : 
    1380        7430 : NumericVector<Number> & System::get_sensitivity_rhs (unsigned int i)
    1381             : {
    1382        7430 :   std::ostringstream sensitivity_rhs_name;
    1383        7218 :   sensitivity_rhs_name << "sensitivity_rhs" << i;
    1384             : 
    1385       14860 :   return this->get_vector(sensitivity_rhs_name.str());
    1386        7006 : }
    1387             : 
    1388             : 
    1389             : 
    1390           0 : const NumericVector<Number> & System::get_sensitivity_rhs (unsigned int i) const
    1391             : {
    1392           0 :   std::ostringstream sensitivity_rhs_name;
    1393           0 :   sensitivity_rhs_name << "sensitivity_rhs" << i;
    1394             : 
    1395           0 :   return this->get_vector(sensitivity_rhs_name.str());
    1396           0 : }
    1397             : 
    1398             : 
    1399             : 
    1400      407452 : unsigned int System::add_variable (std::string_view var,
    1401             :                                    const FEType & type,
    1402             :                                    const std::set<subdomain_id_type> * const active_subdomains)
    1403             : {
    1404      407452 :   return this->get_dof_map().add_variable(*this, var, type, active_subdomains);
    1405             : }
    1406             : 
    1407             : 
    1408             : 
    1409      396403 : unsigned int System::add_variable (std::string_view var,
    1410             :                                    const Order order,
    1411             :                                    const FEFamily family,
    1412             :                                    const std::set<subdomain_id_type> * const active_subdomains,
    1413             :                                    const bool p_refinement)
    1414             : {
    1415      396403 :   return this->add_variable(var,
    1416      407601 :                             FEType(order, family).set_p_refinement(p_refinement),
    1417      407601 :                             active_subdomains);
    1418             : }
    1419             : 
    1420             : 
    1421             : 
    1422          71 : unsigned int System::add_variables (const std::vector<std::string> & vars,
    1423             :                                     const FEType & type,
    1424             :                                     const std::set<subdomain_id_type> * const active_subdomains)
    1425             : {
    1426          71 :   return this->get_dof_map().add_variables(*this, vars, type, active_subdomains);
    1427             : }
    1428             : 
    1429             : 
    1430             : 
    1431          71 : unsigned int System::add_variables (const std::vector<std::string> & vars,
    1432             :                                     const Order order,
    1433             :                                     const FEFamily family,
    1434             :                                     const std::set<subdomain_id_type> * const active_subdomains,
    1435             :                                     const bool p_refinement)
    1436             : {
    1437          71 :   return this->add_variables(vars,
    1438          73 :                              FEType(order, family).set_p_refinement(p_refinement),
    1439          73 :                              active_subdomains);
    1440             : }
    1441             : 
    1442         568 : unsigned int System::add_variable_array (const std::vector<std::string> & vars,
    1443             :                                          const FEType & type,
    1444             :                                          const std::set<subdomain_id_type> * const active_subdomains)
    1445             : {
    1446         568 :   return this->get_dof_map().add_variable_array(*this, vars, type, active_subdomains);
    1447             : }
    1448             : 
    1449         898 : bool System::has_variable (std::string_view var) const
    1450             : {
    1451         898 :   return this->get_dof_map().has_variable(var);
    1452             : }
    1453             : 
    1454     8440322 : unsigned int System::variable_number (std::string_view var) const
    1455             : {
    1456     8440322 :   return this->get_dof_map().variable_number(var);
    1457             : }
    1458             : 
    1459         493 : void System::get_all_variable_numbers(std::vector<unsigned int> & all_variable_numbers) const
    1460             : {
    1461         493 :   this->get_dof_map().get_all_variable_numbers(all_variable_numbers);
    1462         493 : }
    1463             : 
    1464             : 
    1465         142 : void System::local_dof_indices(const unsigned int var,
    1466             :                                std::set<dof_id_type> & var_indices) const
    1467             : {
    1468             :   // Make sure the set is clear
    1469           4 :   var_indices.clear();
    1470             : 
    1471           8 :   std::vector<dof_id_type> dof_indices;
    1472             : 
    1473             :   const dof_id_type
    1474           4 :     first_local = this->get_dof_map().first_dof(),
    1475           4 :     end_local   = this->get_dof_map().end_dof();
    1476             : 
    1477             :   // Begin the loop over the elements
    1478         984 :   for (const auto & elem : this->get_mesh().active_local_element_ptr_range())
    1479             :     {
    1480         384 :       this->get_dof_map().dof_indices (elem, dof_indices, var);
    1481             : 
    1482        1152 :       for (dof_id_type dof : dof_indices)
    1483             :         //If the dof is owned by the local processor
    1484         768 :         if (first_local <= dof && dof < end_local)
    1485         503 :           var_indices.insert(dof);
    1486         134 :     }
    1487             : 
    1488             :   // we may have missed assigning DOFs to nodes that we own
    1489             :   // but to which we have no connected elements matching our
    1490             :   // variable restriction criterion.  this will happen, for example,
    1491             :   // if variable V is restricted to subdomain S.  We may not own
    1492             :   // any elements which live in S, but we may own nodes which are
    1493             :   // *connected* to elements which do.
    1494        1380 :   for (const auto & node : this->get_mesh().local_node_ptr_range())
    1495             :     {
    1496          50 :       libmesh_assert(node);
    1497         600 :       this->get_dof_map().dof_indices (node, dof_indices, var);
    1498         960 :       for (auto dof : dof_indices)
    1499         360 :         if (first_local <= dof && dof < end_local)
    1500         330 :           var_indices.insert(dof);
    1501         134 :     }
    1502         142 : }
    1503             : 
    1504             : 
    1505             : 
    1506           0 : void System::zero_variable (NumericVector<Number> & v,
    1507             :                             unsigned int var_num) const
    1508             : {
    1509             :   /* Make sure the call makes sense.  */
    1510           0 :   libmesh_assert_less (var_num, this->n_vars());
    1511             : 
    1512             :   /* Get a reference to the mesh.  */
    1513           0 :   const MeshBase & mesh = this->get_mesh();
    1514             : 
    1515             :   /* Check which system we are.  */
    1516           0 :   const unsigned int sys_num = this->number();
    1517             : 
    1518             :   // Loop over nodes.
    1519             :   Threads::parallel_for_each
    1520           0 :     (mesh.node_stored_range(),
    1521           0 :      [sys_num, var_num, &v](const Node * node)
    1522             :      {
    1523           0 :        unsigned int n_comp = node->n_comp(sys_num,var_num);
    1524           0 :        for (unsigned int i=0; i<n_comp; i++)
    1525             :          {
    1526           0 :            const dof_id_type index = node->dof_number(sys_num,var_num,i);
    1527           0 :            v.set(index,0.0);
    1528             :          }
    1529           0 :      });
    1530             : 
    1531             :   // Loop over elements.
    1532             :   Threads::parallel_for_each
    1533           0 :     (mesh.active_local_element_stored_range(),
    1534           0 :      [sys_num, var_num, &v](const Elem * elem)
    1535             :      {
    1536           0 :        unsigned int n_comp = elem->n_comp(sys_num,var_num);
    1537           0 :        for (unsigned int i=0; i<n_comp; i++)
    1538             :          {
    1539           0 :            const dof_id_type index = elem->dof_number(sys_num,var_num,i);
    1540           0 :            v.set(index,0.0);
    1541             :          }
    1542           0 :      });
    1543           0 : }
    1544             : 
    1545             : 
    1546             : 
    1547           0 : Real System::discrete_var_norm(const NumericVector<Number> & v,
    1548             :                                unsigned int var,
    1549             :                                FEMNormType norm_type) const
    1550             : {
    1551           0 :   std::set<dof_id_type> var_indices;
    1552           0 :   local_dof_indices(var, var_indices);
    1553             : 
    1554           0 :   if (norm_type == DISCRETE_L1)
    1555           0 :     return v.subset_l1_norm(var_indices);
    1556           0 :   if (norm_type == DISCRETE_L2)
    1557           0 :     return v.subset_l2_norm(var_indices);
    1558           0 :   if (norm_type == DISCRETE_L_INF)
    1559           0 :     return v.subset_linfty_norm(var_indices);
    1560             :   else
    1561           0 :     libmesh_error_msg("Invalid norm_type = " << Utility::enum_to_string(norm_type));
    1562             : }
    1563             : 
    1564             : 
    1565             : 
    1566      106884 : Real System::calculate_norm(const NumericVector<Number> & v,
    1567             :                             unsigned int var,
    1568             :                             FEMNormType norm_type,
    1569             :                             std::set<unsigned int> * skip_dimensions) const
    1570             : {
    1571             :   //short circuit to save time
    1572      106884 :   if (norm_type == DISCRETE_L1 ||
    1573      106884 :       norm_type == DISCRETE_L2 ||
    1574             :       norm_type == DISCRETE_L_INF)
    1575           0 :     return discrete_var_norm(v,var,norm_type);
    1576             : 
    1577             :   // Not a discrete norm
    1578      110118 :   std::vector<FEMNormType> norms(this->n_vars(), L2);
    1579      106884 :   std::vector<Real> weights(this->n_vars(), 0.0);
    1580      106884 :   norms[var] = norm_type;
    1581      106884 :   weights[var] = 1.0;
    1582      207488 :   Real val = this->calculate_norm(v, SystemNorm(norms, weights), skip_dimensions);
    1583        3234 :   return val;
    1584             : }
    1585             : 
    1586             : 
    1587             : 
    1588      133121 : Real System::calculate_norm(const NumericVector<Number> & v,
    1589             :                             const SystemNorm & norm,
    1590             :                             std::set<unsigned int> * skip_dimensions) const
    1591             : {
    1592             :   // This function must be run on all processors at once
    1593        4172 :   parallel_object_only();
    1594             : 
    1595       12328 :   LOG_SCOPE ("calculate_norm()", "System");
    1596             : 
    1597             :   // Zero the norm before summation
    1598      133121 :   Real v_norm = 0.;
    1599             : 
    1600      133121 :   if (norm.is_discrete())
    1601             :     {
    1602             :       //Check to see if all weights are 1.0 and all types are equal
    1603       11340 :       FEMNormType norm_type0 = norm.type(0);
    1604       11340 :       unsigned int check_var = 0, check_end = this->n_vars();
    1605       22680 :       for (; check_var != check_end; ++check_var)
    1606       11340 :         if ((norm.weight(check_var) != 1.0) || (norm.type(check_var) != norm_type0))
    1607           0 :           break;
    1608             : 
    1609             :       //All weights were 1.0 so just do the full vector discrete norm
    1610       11340 :       if (check_var == this->n_vars())
    1611             :         {
    1612       11340 :           if (norm_type0 == DISCRETE_L1)
    1613           0 :             return v.l1_norm();
    1614       11340 :           if (norm_type0 == DISCRETE_L2)
    1615       11340 :             return v.l2_norm();
    1616           0 :           if (norm_type0 == DISCRETE_L_INF)
    1617           0 :             return v.linfty_norm();
    1618             :           else
    1619           0 :             libmesh_error_msg("Invalid norm_type0 = " << Utility::enum_to_string(norm_type0));
    1620             :         }
    1621             : 
    1622           0 :       for (auto var : make_range(this->n_vars()))
    1623             :         {
    1624             :           // Skip any variables we don't need to integrate
    1625           0 :           if (norm.weight(var) == 0.0)
    1626           0 :             continue;
    1627             : 
    1628           0 :           v_norm += norm.weight(var) * discrete_var_norm(v, var, norm.type(var));
    1629             :         }
    1630             : 
    1631           0 :       return v_norm;
    1632             :     }
    1633             : 
    1634             :   // Localize the potentially parallel vector
    1635      121781 :   std::unique_ptr<NumericVector<Number>> local_v = NumericVector<Number>::build(this->comm());
    1636      125629 :   local_v->init(v.size(), v.local_size(), _dof_map->get_send_list(),
    1637        7508 :                 true, GHOSTED);
    1638      125441 :   v.localize (*local_v, _dof_map->get_send_list());
    1639             : 
    1640             :   // I'm not sure how best to mix Hilbert norms on some variables (for
    1641             :   // which we'll want to square then sum then square root) with norms
    1642             :   // like L_inf (for which we'll just want to take an absolute value
    1643             :   // and then sum).
    1644        3848 :   bool using_hilbert_norm = true,
    1645        3848 :     using_nonhilbert_norm = true;
    1646             : 
    1647             :   // Loop over all variables
    1648      243562 :   for (auto var : make_range(this->n_vars()))
    1649             :     {
    1650             :       // Skip any variables we don't need to integrate
    1651      121781 :       Real norm_weight_sq = norm.weight_sq(var);
    1652      121781 :       if (norm_weight_sq == 0.0)
    1653           0 :         continue;
    1654      121781 :       Real norm_weight = norm.weight(var);
    1655             : 
    1656             :       // Check for unimplemented norms (rather than just returning 0).
    1657      121781 :       FEMNormType norm_type = norm.type(var);
    1658      121781 :       if ((norm_type==H1) ||
    1659      117977 :           (norm_type==H2) ||
    1660      117957 :           (norm_type==L2) ||
    1661      117949 :           (norm_type==H1_SEMINORM) ||
    1662             :           (norm_type==H2_SEMINORM))
    1663             :         {
    1664      121181 :           if (!using_hilbert_norm)
    1665           0 :             libmesh_not_implemented();
    1666        3832 :           using_nonhilbert_norm = false;
    1667             :         }
    1668         616 :       else if ((norm_type==L1) ||
    1669         592 :                (norm_type==L_INF) ||
    1670         584 :                (norm_type==W1_INF_SEMINORM) ||
    1671             :                (norm_type==W2_INF_SEMINORM))
    1672             :         {
    1673         600 :           if (!using_nonhilbert_norm)
    1674           0 :             libmesh_not_implemented();
    1675          16 :           using_hilbert_norm = false;
    1676             :         }
    1677             :       else
    1678           0 :         libmesh_not_implemented();
    1679             : 
    1680        3848 :       const FEType & fe_type = this->get_dof_map().variable_type(var);
    1681             : 
    1682             :       // Allow space for dims 0-3, and for both scalar and vector
    1683             :       // elements, even if we don't use them all
    1684      129289 :       std::vector<std::unique_ptr<FEBase>> fe_ptrs(4);
    1685      129289 :       std::vector<std::unique_ptr<FEVectorBase>> vec_fe_ptrs(4);
    1686      129289 :       std::vector<std::unique_ptr<QBase>> q_rules(4);
    1687             : 
    1688      121781 :       const std::set<unsigned char> & elem_dims = _mesh.elem_dimensions();
    1689             : 
    1690             :       // Prepare finite elements for each dimension present in the mesh
    1691      244972 :       for (const auto & dim : elem_dims)
    1692             :         {
    1693      123267 :           if (skip_dimensions && skip_dimensions->find(dim) != skip_dimensions->end())
    1694        1372 :             continue;
    1695             : 
    1696             :           // Construct quadrature and finite element objects
    1697      121781 :           q_rules[dim] = fe_type.default_quadrature_rule (dim);
    1698             : 
    1699      121781 :           const FEFieldType field_type = FEInterface::field_type(fe_type);
    1700      121781 :           if (field_type == TYPE_SCALAR)
    1701             :             {
    1702      121355 :               fe_ptrs[dim] = FEBase::build(dim, fe_type);
    1703      128651 :               fe_ptrs[dim]->attach_quadrature_rule (q_rules[dim].get());
    1704             :             }
    1705             :           else
    1706             :             {
    1707         426 :               vec_fe_ptrs[dim] = FEVectorBase::build(dim, fe_type);
    1708         450 :               vec_fe_ptrs[dim]->attach_quadrature_rule (q_rules[dim].get());
    1709          12 :               libmesh_assert_equal_to(field_type, TYPE_VECTOR);
    1710             :             }
    1711             : 
    1712             :         }
    1713             : 
    1714        7696 :       std::vector<dof_id_type> dof_indices;
    1715             : 
    1716             :       // Begin the loop over the elements
    1717    31858292 :       for (const auto & elem : this->get_mesh().active_local_element_ptr_range())
    1718             :         {
    1719    17465285 :           const unsigned int dim = elem->dim();
    1720             : 
    1721             :           // One way for implementing this would be to exchange the fe with the FEInterface- class.
    1722             :           // However, it needs to be discussed whether integral-norms make sense for infinite elements.
    1723             :           // or in which sense they could make sense.
    1724     3519179 :           if (elem->infinite() )
    1725           0 :             libmesh_not_implemented();
    1726             : 
    1727    17469083 :           if (skip_dimensions && skip_dimensions->find(dim) != skip_dimensions->end())
    1728       25062 :             continue;
    1729             : 
    1730    19065365 :           QBase * qrule = q_rules[dim].get();
    1731     1682806 :           libmesh_assert(qrule);
    1732             : 
    1733    17440223 :           this->get_dof_map().dof_indices (elem, dof_indices, var);
    1734             : 
    1735    14132275 :           auto element_calculation = [&dof_indices, &elem,
    1736             :                norm_type, norm_weight, norm_weight_sq, &qrule,
    1737   944653564 :                &local_v, &v_norm](auto & fe) {
    1738             :           typedef typename std::remove_reference<decltype(fe)>::type::OutputShape OutputShape;
    1739             :           typedef typename TensorTools::MakeNumber<OutputShape>::type OutputNumberShape;
    1740             :           typedef typename std::remove_reference<decltype(fe)>::type::OutputGradient OutputGradient;
    1741             :           typedef typename TensorTools::MakeNumber<OutputGradient>::type OutputNumberGradient;
    1742             : 
    1743    17440223 :           const std::vector<Real> &                     JxW = fe.get_JxW();
    1744     1682806 :           const std::vector<std::vector<OutputShape>> * phi = nullptr;
    1745    17440223 :           if (norm_type == H1 ||
    1746    15759626 :               norm_type == H2 ||
    1747    15758245 :               norm_type == L2 ||
    1748    15758245 :               norm_type == L1 ||
    1749             :               norm_type == L_INF)
    1750     1682254 :             phi = &(fe.get_phi());
    1751             : 
    1752     1682806 :           const std::vector<std::vector<OutputGradient>> * dphi = nullptr;
    1753    19065365 :           if (norm_type == H1 ||
    1754    15759626 :               norm_type == H2 ||
    1755    15759350 :               norm_type == H1_SEMINORM ||
    1756             :               norm_type == W1_INF_SEMINORM)
    1757     1681149 :             dphi = &(fe.get_dphi());
    1758             : 
    1759             : #ifdef LIBMESH_ENABLE_SECOND_DERIVATIVES
    1760             :           typedef typename std::remove_reference<decltype(fe)>::type::OutputTensor OutputTensor;
    1761             : 
    1762     1682806 :           const std::vector<std::vector<OutputTensor>> *  d2phi = nullptr;
    1763    19065365 :           if (norm_type == H2 ||
    1764    17440223 :               norm_type == H2_SEMINORM ||
    1765             :               norm_type == W2_INF_SEMINORM)
    1766           0 :             d2phi = &(fe.get_d2phi());
    1767             : #endif
    1768             : 
    1769    17440223 :           fe.reinit (elem);
    1770             : 
    1771    17440223 :           const unsigned int n_qp = qrule->n_points();
    1772             : 
    1773             :           const unsigned int n_sf = cast_int<unsigned int>
    1774     3307948 :             (dof_indices.size());
    1775             : 
    1776             :           // Begin the loop over the Quadrature points.
    1777    90826991 :           for (unsigned int qp=0; qp<n_qp; qp++)
    1778             :             {
    1779    73386768 :               if (norm_type == L1)
    1780             :                 {
    1781           0 :                   OutputNumberShape u_h = 0.;
    1782           0 :                   for (unsigned int i=0; i != n_sf; ++i)
    1783           0 :                     u_h += (*phi)[i][qp] * (*local_v)(dof_indices[i]);
    1784           0 :                   v_norm += norm_weight *
    1785           0 :                     JxW[qp] * TensorTools::norm(u_h);
    1786             :                 }
    1787             : 
    1788    73386768 :               if (norm_type == L_INF)
    1789             :                 {
    1790        2772 :                   OutputNumberShape u_h = 0.;
    1791      440028 :                   for (unsigned int i=0; i != n_sf; ++i)
    1792      537030 :                     u_h += (*phi)[i][qp] * (*local_v)(dof_indices[i]);
    1793       34529 :                   v_norm = std::max(v_norm, norm_weight * TensorTools::norm(u_h));
    1794             :                 }
    1795             : 
    1796    73386768 :               if (norm_type == H1 ||
    1797    66354024 :                   norm_type == H2 ||
    1798             :                   norm_type == L2)
    1799             :                 {
    1800     7049587 :                   OutputNumberShape u_h = 0.;
    1801   415478076 :                   for (unsigned int i=0; i != n_sf; ++i)
    1802   467512996 :                     u_h += (*phi)[i][qp] * (*local_v)(dof_indices[i]);
    1803    80104017 :                   v_norm += norm_weight_sq *
    1804    73285086 :                     JxW[qp] * TensorTools::norm_sq(u_h);
    1805             :                 }
    1806             : 
    1807    80214015 :               if (norm_type == H1 ||
    1808    66354024 :                   norm_type == H2 ||
    1809    66328865 :                   norm_type == H1_SEMINORM)
    1810             :                 {
    1811     7035516 :                   OutputNumberGradient grad_u_h;
    1812   409310880 :                   for (unsigned int i=0; i != n_sf; ++i)
    1813   367399682 :                     grad_u_h.add_scaled((*dphi)[i][qp], (*local_v)(dof_indices[i]));
    1814    79921094 :                   v_norm += norm_weight_sq *
    1815    73116234 :                     JxW[qp] * grad_u_h.norm_sq();
    1816             :                 }
    1817             : 
    1818    73386768 :               if (norm_type == W1_INF_SEMINORM)
    1819             :                 {
    1820        2772 :                   OutputNumberGradient grad_u_h;
    1821      440028 :                   for (unsigned int i=0; i != n_sf; ++i)
    1822      438858 :                     grad_u_h.add_scaled((*dphi)[i][qp], (*local_v)(dof_indices[i]));
    1823       36441 :                   v_norm = std::max(v_norm, norm_weight * grad_u_h.norm());
    1824             :                 }
    1825             : 
    1826             : #ifdef LIBMESH_ENABLE_SECOND_DERIVATIVES
    1827             :               typedef typename TensorTools::MakeNumber<OutputTensor>::type OutputNumberTensor;
    1828             : 
    1829    80214015 :               if (norm_type == H2 ||
    1830    66328865 :                   norm_type == H2_SEMINORM)
    1831             :                 {
    1832           0 :                   OutputNumberTensor hess_u_h;
    1833           0 :                   for (unsigned int i=0; i != n_sf; ++i)
    1834           0 :                     hess_u_h.add_scaled((*d2phi)[i][qp], (*local_v)(dof_indices[i]));
    1835           0 :                   v_norm += norm_weight_sq *
    1836           0 :                     JxW[qp] * hess_u_h.norm_sq();
    1837             :                 }
    1838             : 
    1839    73386768 :               if (norm_type == W2_INF_SEMINORM)
    1840             :                 {
    1841           0 :                   OutputNumberTensor hess_u_h;
    1842           0 :                   for (unsigned int i=0; i != n_sf; ++i)
    1843           0 :                     hess_u_h.add_scaled((*d2phi)[i][qp], (*local_v)(dof_indices[i]));
    1844           0 :                   v_norm = std::max(v_norm, norm_weight * hess_u_h.norm());
    1845             :                 }
    1846             : #endif
    1847             :             }
    1848    34880446 :           };
    1849             : 
    1850     3307948 :           FEBase * scalar_fe = fe_ptrs[dim].get();
    1851     3307948 :           FEVectorBase * vec_fe = vec_fe_ptrs[dim].get();
    1852             : 
    1853    17440223 :           if (scalar_fe)
    1854             :             {
    1855     1681701 :               libmesh_assert(!vec_fe);
    1856    17426963 :               element_calculation(*scalar_fe);
    1857             :             }
    1858             : 
    1859    17440223 :           if (vec_fe)
    1860             :             {
    1861        1105 :               libmesh_assert(!scalar_fe);
    1862       13260 :               element_calculation(*vec_fe);
    1863             :             }
    1864      114273 :         }
    1865      114273 :     }
    1866             : 
    1867      121781 :   if (using_hilbert_norm)
    1868             :     {
    1869      121181 :       this->comm().sum(v_norm);
    1870      121181 :       v_norm = std::sqrt(v_norm);
    1871             :     }
    1872             :   else
    1873             :     {
    1874         600 :       this->comm().max(v_norm);
    1875             :     }
    1876             : 
    1877      121781 :   return v_norm;
    1878      114273 : }
    1879             : 
    1880             : 
    1881             : 
    1882       18100 : std::string System::get_info() const
    1883             : {
    1884       19104 :   std::ostringstream oss;
    1885             : 
    1886             : 
    1887         502 :   const std::string & sys_name = this->name();
    1888             : 
    1889       18100 :   oss << "   System #"  << this->number() << ", \"" << sys_name << "\"\n"
    1890       18100 :       << "    Type \""  << this->system_type() << "\"\n"
    1891       52292 :       << "    Variables=";
    1892             : 
    1893       58658 :   for (auto vg : make_range(this->n_variable_groups()))
    1894             :     {
    1895       23462 :       const VariableGroup & vg_description (this->variable_group(vg));
    1896             : 
    1897       23462 :       if (vg_description.n_variables() > 1) oss << "{ ";
    1898       58027 :       for (auto vn : make_range(vg_description.n_variables()))
    1899       67194 :         oss << "\"" << vg_description.name(vn) << "\" ";
    1900       23462 :       if (vg_description.n_variables() > 1) oss << "} ";
    1901             :     }
    1902             : 
    1903       18100 :   oss << '\n';
    1904             : 
    1905       18100 :   oss << "    Finite Element Types=";
    1906             : #ifndef LIBMESH_ENABLE_INFINITE_ELEMENTS
    1907       55731 :   for (auto vg : make_range(this->n_variable_groups()))
    1908             :     oss << "\""
    1909       43842 :         << Utility::enum_to_string<FEFamily>(this->get_dof_map().variable_group(vg).type().family)
    1910       43842 :         << "\" ";
    1911             : #else
    1912        2927 :   for (auto vg : make_range(this->n_variable_groups()))
    1913             :     {
    1914             :       oss << "\""
    1915        3082 :           << Utility::enum_to_string<FEFamily>(this->get_dof_map().variable_group(vg).type().family)
    1916             :           << "\", \""
    1917        3082 :           << Utility::enum_to_string<FEFamily>(this->get_dof_map().variable_group(vg).type().radial_family)
    1918        3315 :           << "\" ";
    1919             :     }
    1920             : 
    1921        1195 :   oss << '\n' << "    Infinite Element Mapping=";
    1922        2927 :   for (auto vg : make_range(this->n_variable_groups()))
    1923             :     oss << "\""
    1924        3082 :         << Utility::enum_to_string<InfMapType>(this->get_dof_map().variable_group(vg).type().inf_map)
    1925        2428 :         << "\" ";
    1926             : #endif
    1927             : 
    1928       18100 :   oss << '\n';
    1929             : 
    1930       18100 :   oss << "    Approximation Orders=";
    1931       58658 :   for (auto vg : make_range(this->n_variable_groups()))
    1932             :     {
    1933             : #ifndef LIBMESH_ENABLE_INFINITE_ELEMENTS
    1934             :       oss << "\""
    1935       43842 :           << Utility::enum_to_string<Order>(this->get_dof_map().variable_group(vg).type().order)
    1936       43842 :           << "\" ";
    1937             : #else
    1938             :       oss << "\""
    1939        3082 :           << Utility::enum_to_string<Order>(this->get_dof_map().variable_group(vg).type().order)
    1940             :           << "\", \""
    1941        3082 :           << Utility::enum_to_string<Order>(this->get_dof_map().variable_group(vg).type().radial_order)
    1942        3315 :           << "\" ";
    1943             : #endif
    1944             :     }
    1945             : 
    1946       18100 :   oss << '\n';
    1947             : 
    1948       18100 :   if (this->is_initialized())
    1949             :     {
    1950       35058 :       oss << "    n_dofs()="             << this->n_dofs()             << '\n';
    1951       18029 :       dof_id_type local_dofs = this->n_local_dofs();
    1952       18029 :       oss << "    n_local_dofs()="       << local_dofs                 << '\n';
    1953       18029 :       this->comm().max(local_dofs);
    1954       18029 :       oss << "    max(n_local_dofs())="  << local_dofs                 << '\n';
    1955             : #ifdef LIBMESH_ENABLE_CONSTRAINTS
    1956       18029 :       if (this->n_constrained_dofs())
    1957             :         {
    1958       20096 :           oss << "    n_constrained_dofs()=" << this->n_constrained_dofs() << '\n';
    1959       19806 :           oss << "    n_local_constrained_dofs()=" << this->n_local_constrained_dofs() << '\n';
    1960       10193 :           dof_id_type local_unconstrained_dofs = this->n_local_dofs() - this->n_local_constrained_dofs();
    1961       10193 :           this->comm().max(local_unconstrained_dofs);
    1962       10483 :           oss << "    max(local unconstrained dofs)=" << local_unconstrained_dofs << '\n';
    1963             :         }
    1964             : #endif
    1965       18029 :       if (this->has_static_condensation())
    1966         952 :         oss << "    n uncondensed dofs="
    1967        1008 :             << this->get_dof_map().get_static_condensation().n_dofs() << '\n';
    1968             :     }
    1969             :   else
    1970          71 :     oss << "    (still uninitialized)\n";
    1971             : 
    1972       35196 :   oss << "    " << "n_vectors()="  << this->n_vectors()  << '\n';
    1973       35196 :   oss << "    " << "n_matrices()="  << this->n_matrices()  << '\n';
    1974             :   //   oss << "    " << "n_additional_matrices()=" << this->n_additional_matrices() << '\n';
    1975             : 
    1976       35698 :   oss << this->get_dof_map().get_info();
    1977             : 
    1978       18602 :   return oss.str();
    1979       17096 : }
    1980             : 
    1981             : 
    1982             : 
    1983         430 : void System::attach_init_function (void fptr(EquationSystems & es,
    1984             :                                              const std::string & name))
    1985             : {
    1986          14 :   libmesh_assert(fptr);
    1987             : 
    1988         430 :   if (_init_system_object != nullptr)
    1989             :     {
    1990             :       libmesh_warning("WARNING:  Cannot specify both initialization function and object!");
    1991             : 
    1992           0 :       _init_system_object = nullptr;
    1993             :     }
    1994             : 
    1995         430 :   _init_system_function = fptr;
    1996         430 : }
    1997             : 
    1998             : 
    1999             : 
    2000         280 : void System::attach_init_object (System::Initialization & init_in)
    2001             : {
    2002         280 :   if (_init_system_function != nullptr)
    2003             :     {
    2004             :       libmesh_warning("WARNING:  Cannot specify both initialization object and function!");
    2005             : 
    2006           0 :       _init_system_function = nullptr;
    2007             :     }
    2008             : 
    2009         280 :   _init_system_object = &init_in;
    2010         280 : }
    2011             : 
    2012             : 
    2013             : 
    2014       10199 : void System::attach_assemble_function (void fptr(EquationSystems & es,
    2015             :                                                  const std::string & name))
    2016             : {
    2017         284 :   libmesh_assert(fptr);
    2018             : 
    2019       10199 :   if (_assemble_system_object != nullptr)
    2020             :     {
    2021             :       libmesh_warning("WARNING:  Cannot specify both assembly function and object!");
    2022             : 
    2023           0 :       _assemble_system_object = nullptr;
    2024             :     }
    2025             : 
    2026       10199 :   _assemble_system_function = fptr;
    2027       10199 : }
    2028             : 
    2029             : 
    2030             : 
    2031         142 : void System::attach_assemble_object (System::Assembly & assemble_in)
    2032             : {
    2033         142 :   if (_assemble_system_function != nullptr)
    2034             :     {
    2035             :       libmesh_warning("WARNING:  Cannot specify both assembly object and function!");
    2036             : 
    2037           0 :       _assemble_system_function = nullptr;
    2038             :     }
    2039             : 
    2040         142 :   _assemble_system_object = &assemble_in;
    2041         142 : }
    2042             : 
    2043             : 
    2044             : 
    2045           0 : void System::attach_constraint_function(void fptr(EquationSystems & es,
    2046             :                                                   const std::string & name))
    2047             : {
    2048           0 :   libmesh_assert(fptr);
    2049             : 
    2050           0 :   if (_constrain_system_object != nullptr)
    2051             :     {
    2052             :       libmesh_warning("WARNING:  Cannot specify both constraint function and object!");
    2053             : 
    2054           0 :       _constrain_system_object = nullptr;
    2055             :     }
    2056             : 
    2057           0 :   _constrain_system_function = fptr;
    2058           0 : }
    2059             : 
    2060             : 
    2061             : 
    2062        2765 : void System::attach_constraint_object (System::Constraint & constrain)
    2063             : {
    2064        2765 :   if (_constrain_system_function != nullptr)
    2065             :     {
    2066             :       libmesh_warning("WARNING:  Cannot specify both constraint object and function!");
    2067             : 
    2068           0 :       _constrain_system_function = nullptr;
    2069             :     }
    2070             : 
    2071        2765 :   _constrain_system_object = &constrain;
    2072        2765 : }
    2073             : 
    2074           0 : bool System::has_constraint_object () const
    2075             : {
    2076           0 :   return _constrain_system_object != nullptr;
    2077             : }
    2078             : 
    2079           0 : System::Constraint& System::get_constraint_object ()
    2080             : {
    2081           0 :   libmesh_assert_msg(_constrain_system_object,"No constraint object available.");
    2082           0 :   return *_constrain_system_object;
    2083             : }
    2084             : 
    2085             : 
    2086             : 
    2087           0 : void System::attach_QOI_function(void fptr(EquationSystems &,
    2088             :                                            const std::string &,
    2089             :                                            const QoISet &))
    2090             : {
    2091           0 :   libmesh_assert(fptr);
    2092             : 
    2093           0 :   if (_qoi_evaluate_object != nullptr)
    2094             :     {
    2095             :       libmesh_warning("WARNING:  Cannot specify both QOI function and object!");
    2096             : 
    2097           0 :       _qoi_evaluate_object = nullptr;
    2098             :     }
    2099             : 
    2100           0 :   _qoi_evaluate_function = fptr;
    2101           0 : }
    2102             : 
    2103             : 
    2104             : 
    2105           0 : void System::attach_QOI_object (QOI & qoi_in)
    2106             : {
    2107           0 :   if (_qoi_evaluate_function != nullptr)
    2108             :     {
    2109             :       libmesh_warning("WARNING:  Cannot specify both QOI object and function!");
    2110             : 
    2111           0 :       _qoi_evaluate_function = nullptr;
    2112             :     }
    2113             : 
    2114           0 :   _qoi_evaluate_object = &qoi_in;
    2115           0 : }
    2116             : 
    2117             : 
    2118             : 
    2119           0 : void System::attach_QOI_derivative(void fptr(EquationSystems &, const std::string &,
    2120             :                                              const QoISet &, bool, bool))
    2121             : {
    2122           0 :   libmesh_assert(fptr);
    2123             : 
    2124           0 :   if (_qoi_evaluate_derivative_object != nullptr)
    2125             :     {
    2126             :       libmesh_warning("WARNING:  Cannot specify both QOI derivative function and object!");
    2127             : 
    2128           0 :       _qoi_evaluate_derivative_object = nullptr;
    2129             :     }
    2130             : 
    2131           0 :   _qoi_evaluate_derivative_function = fptr;
    2132           0 : }
    2133             : 
    2134             : 
    2135             : 
    2136           0 : void System::attach_QOI_derivative_object (QOIDerivative & qoi_derivative)
    2137             : {
    2138           0 :   if (_qoi_evaluate_derivative_function != nullptr)
    2139             :     {
    2140             :       libmesh_warning("WARNING:  Cannot specify both QOI derivative object and function!");
    2141             : 
    2142           0 :       _qoi_evaluate_derivative_function = nullptr;
    2143             :     }
    2144             : 
    2145           0 :   _qoi_evaluate_derivative_object = &qoi_derivative;
    2146           0 : }
    2147             : 
    2148             : 
    2149             : 
    2150      372933 : void System::user_initialization ()
    2151             : {
    2152             :   // Call the user-provided initialization function,
    2153             :   // if it was provided
    2154      372933 :   if (_init_system_function != nullptr)
    2155         501 :     this->_init_system_function (_equation_systems, this->name());
    2156             : 
    2157             :   // ...or the user-provided initialization object.
    2158      372432 :   else if (_init_system_object != nullptr)
    2159         280 :     this->_init_system_object->initialize();
    2160      372933 : }
    2161             : 
    2162             : 
    2163             : 
    2164       46544 : void System::user_assembly ()
    2165             : {
    2166             :   // Call the user-provided assembly function,
    2167             :   // if it was provided
    2168       46544 :   if (_assemble_system_function != nullptr)
    2169       46402 :     this->_assemble_system_function (_equation_systems, this->name());
    2170             : 
    2171             :   // ...or the user-provided assembly object.
    2172         142 :   else if (_assemble_system_object != nullptr)
    2173         142 :     this->_assemble_system_object->assemble();
    2174       46544 : }
    2175             : 
    2176             : 
    2177             : 
    2178      406967 : void System::user_constrain ()
    2179             : {
    2180             :   // Call the user-provided constraint function,
    2181             :   // if it was provided
    2182      406967 :   if (_constrain_system_function!= nullptr)
    2183           0 :     this->_constrain_system_function(_equation_systems, this->name());
    2184             : 
    2185             :   // ...or the user-provided constraint object.
    2186      406967 :   else if (_constrain_system_object != nullptr)
    2187        2765 :     this->_constrain_system_object->constrain();
    2188      406967 : }
    2189             : 
    2190             : 
    2191             : 
    2192           0 : void System::user_QOI (const QoISet & qoi_indices)
    2193             : {
    2194             :   // Call the user-provided quantity of interest function,
    2195             :   // if it was provided
    2196           0 :   if (_qoi_evaluate_function != nullptr)
    2197           0 :     this->_qoi_evaluate_function(_equation_systems, this->name(), qoi_indices);
    2198             : 
    2199             :   // ...or the user-provided QOI function object.
    2200           0 :   else if (_qoi_evaluate_object != nullptr)
    2201           0 :     this->_qoi_evaluate_object->qoi(qoi_indices);
    2202           0 : }
    2203             : 
    2204             : 
    2205             : 
    2206           0 : void System::user_QOI_derivative(const QoISet & qoi_indices,
    2207             :                                  bool include_liftfunc,
    2208             :                                  bool apply_constraints)
    2209             : {
    2210             :   // Call the user-provided quantity of interest derivative,
    2211             :   // if it was provided
    2212           0 :   if (_qoi_evaluate_derivative_function != nullptr)
    2213           0 :     this->_qoi_evaluate_derivative_function
    2214           0 :       (_equation_systems, this->name(), qoi_indices, include_liftfunc,
    2215             :        apply_constraints);
    2216             : 
    2217             :   // ...or the user-provided QOI derivative function object.
    2218           0 :   else if (_qoi_evaluate_derivative_object != nullptr)
    2219           0 :     this->_qoi_evaluate_derivative_object->qoi_derivative
    2220           0 :       (qoi_indices, include_liftfunc, apply_constraints);
    2221           0 : }
    2222             : 
    2223             : 
    2224        2272 : void System::init_qois(unsigned int n_qois)
    2225             : {
    2226        2272 :   _qoi.resize(n_qois);
    2227        2272 :   _qoi_error_estimates.resize(n_qois);
    2228        2272 : }
    2229             : 
    2230             : 
    2231      117555 : void System::set_qoi(unsigned int qoi_index, Number qoi_value)
    2232             : {
    2233        3358 :   libmesh_assert(qoi_index < _qoi.size());
    2234             : 
    2235      120913 :   _qoi[qoi_index] = qoi_value;
    2236      117555 : }
    2237             : 
    2238             : 
    2239       51380 : Number System::get_qoi_value(unsigned int qoi_index) const
    2240             : {
    2241        1468 :   libmesh_assert(qoi_index < _qoi.size());
    2242       52848 :   return _qoi[qoi_index];
    2243             : }
    2244             : 
    2245             : 
    2246       54015 : std::vector<Number> System::get_qoi_values() const
    2247             : {
    2248       54015 :   return this->_qoi;
    2249             : }
    2250             : 
    2251             : 
    2252       39155 : void System::set_qoi(std::vector<Number> new_qoi)
    2253             : {
    2254        1118 :   libmesh_assert_equal_to(this->_qoi.size(), new_qoi.size());
    2255       39155 :   this->_qoi = std::move(new_qoi);
    2256       39155 : }
    2257             : 
    2258             : 
    2259       22400 : void System::set_qoi_error_estimate(unsigned int qoi_index, Number qoi_error_estimate)
    2260             : {
    2261         640 :   libmesh_assert(qoi_index < _qoi_error_estimates.size());
    2262             : 
    2263       23040 :   _qoi_error_estimates[qoi_index] = qoi_error_estimate;
    2264       22400 : }
    2265             : 
    2266       22400 : Number System::get_qoi_error_estimate_value(unsigned int qoi_index) const
    2267             : {
    2268         640 :   libmesh_assert(qoi_index < _qoi_error_estimates.size());
    2269       23040 :   return _qoi_error_estimates[qoi_index];
    2270             : }
    2271             : 
    2272             : 
    2273             : 
    2274      401455 : Number System::point_value(unsigned int var,
    2275             :                            const Point & p,
    2276             :                            const bool insist_on_success,
    2277             :                            const NumericVector<Number> *sol) const
    2278             : {
    2279             :   // This function must be called on every processor; there's no
    2280             :   // telling where in the partition p falls.
    2281       11274 :   parallel_object_only();
    2282             : 
    2283             :   // And every processor had better agree about which point we're
    2284             :   // looking for
    2285             : #ifndef NDEBUG
    2286       11274 :   libmesh_assert(this->comm().verify(p(0)));
    2287             : #if LIBMESH_DIM > 1
    2288       11274 :   libmesh_assert(this->comm().verify(p(1)));
    2289             : #endif
    2290             : #if LIBMESH_DIM > 2
    2291       11274 :   libmesh_assert(this->comm().verify(p(2)));
    2292             : #endif
    2293             : #endif // NDEBUG
    2294             : 
    2295             :   // Get a reference to the mesh object associated with the system object that calls this function
    2296       22548 :   const MeshBase & mesh = this->get_mesh();
    2297             : 
    2298             :   // Use an existing PointLocator or create a new one
    2299      401455 :   std::unique_ptr<PointLocatorBase> locator_ptr = mesh.sub_point_locator();
    2300       11274 :   PointLocatorBase & locator = *locator_ptr;
    2301             : 
    2302      401455 :   if (!insist_on_success || !mesh.is_serial())
    2303      342514 :     locator.enable_out_of_mesh_mode();
    2304             : 
    2305             :   // Get a pointer to an element that contains p and allows us to
    2306             :   // evaluate var
    2307             :   const std::set<subdomain_id_type> & raw_subdomains =
    2308      401455 :     this->variable(var).active_subdomains();
    2309             :   const std::set<subdomain_id_type> * implicit_subdomains =
    2310      401455 :     raw_subdomains.empty() ? nullptr : &raw_subdomains;
    2311      401455 :   const Elem * e = locator(p, implicit_subdomains);
    2312             : 
    2313      401455 :   Number u = 0;
    2314             : 
    2315      401455 :   if (e && this->get_dof_map().is_evaluable(*e, var))
    2316      149706 :     u = point_value(var, p, *e, sol);
    2317             : 
    2318             :   // If I have an element containing p, then let's let everyone know
    2319             :   processor_id_type lowest_owner =
    2320      401455 :     (e && (e->processor_id() == this->processor_id())) ?
    2321      395818 :     this->processor_id() : this->n_processors();
    2322      401455 :   this->comm().min(lowest_owner);
    2323             : 
    2324             :   // Everybody should get their value from a processor that was able
    2325             :   // to compute it.
    2326             :   // If nobody admits owning the point, we have a problem.
    2327      412729 :   if (lowest_owner != this->n_processors())
    2328      390181 :     this->comm().broadcast(u, lowest_owner);
    2329             :   else
    2330           0 :     libmesh_assert(!insist_on_success);
    2331             : 
    2332      424003 :   return u;
    2333      378907 : }
    2334             : 
    2335      168087 : Number System::point_value(unsigned int var,
    2336             :                            const Point & p,
    2337             :                            const Elem & e,
    2338             :                            const NumericVector<Number> *sol) const
    2339             : {
    2340             :   // Ensuring that the given point is really in the element is an
    2341             :   // expensive assert, but as long as debugging is turned on we might
    2342             :   // as well try to catch a particularly nasty potential error
    2343        9293 :   libmesh_assert (e.contains_point(p));
    2344             : 
    2345      168087 :   if (!sol)
    2346        6631 :     sol = this->current_local_solution.get();
    2347             : 
    2348             :   // Get the dof map to get the proper indices for our computation
    2349        9293 :   const DofMap & dof_map = this->get_dof_map();
    2350             : 
    2351             :   // Make sure we can evaluate on this element.
    2352        9293 :   libmesh_assert (dof_map.is_evaluable(e, var));
    2353             : 
    2354             :   // Need dof_indices for phi[i][j]
    2355       18586 :   std::vector<dof_id_type> dof_indices;
    2356             : 
    2357             :   // Fill in the dof_indices for our element
    2358      168087 :   dof_map.dof_indices (&e, dof_indices, var);
    2359             : 
    2360             :   // Get the no of dofs associated with this point
    2361             :   const unsigned int num_dofs = cast_int<unsigned int>
    2362       18586 :     (dof_indices.size());
    2363             : 
    2364      168087 :   FEType fe_type = dof_map.variable_type(var);
    2365             : 
    2366             :   // Map the physical co-ordinates to the master co-ordinates
    2367      168087 :   Point coor = FEMap::inverse_map(e.dim(), &e, p);
    2368             : 
    2369             :   // get the shape function value via the FEInterface to also handle the case
    2370             :   // of infinite elements correctly, the shape function is not fe->phi().
    2371      177380 :   FEComputeData fe_data(this->get_equation_systems(), coor);
    2372      168087 :   FEInterface::compute_data(e.dim(), fe_type, &e, fe_data);
    2373             : 
    2374             :   // Get ready to accumulate a value
    2375        9293 :   Number u = 0;
    2376             : 
    2377     3587425 :   for (unsigned int l=0; l<num_dofs; l++)
    2378             :     {
    2379     3735618 :       u += fe_data.shape[l] * (*sol)(dof_indices[l]);
    2380             :     }
    2381             : 
    2382      177380 :   return u;
    2383      149501 : }
    2384             : 
    2385             : 
    2386             : 
    2387       18381 : Number System::point_value(unsigned int var, const Point & p, const Elem * e) const
    2388             : {
    2389         337 :   libmesh_assert(e);
    2390       18381 :   return this->point_value(var, p, *e);
    2391             : }
    2392             : 
    2393             : 
    2394             : 
    2395      123722 : Number System::point_value(unsigned int var, const Point & p, const NumericVector<Number> * sol) const
    2396             : {
    2397      123722 :   return this->point_value(var, p, true, sol);
    2398             : }
    2399             : 
    2400             : 
    2401             : 
    2402             : 
    2403           0 : Gradient System::point_gradient(unsigned int var,
    2404             :                                 const Point & p,
    2405             :                                 const bool insist_on_success,
    2406             :                                 const NumericVector<Number> *sol) const
    2407             : {
    2408             :   // This function must be called on every processor; there's no
    2409             :   // telling where in the partition p falls.
    2410           0 :   parallel_object_only();
    2411             : 
    2412             :   // And every processor had better agree about which point we're
    2413             :   // looking for
    2414             : #ifndef NDEBUG
    2415           0 :   libmesh_assert(this->comm().verify(p(0)));
    2416             : #if LIBMESH_DIM > 1
    2417           0 :   libmesh_assert(this->comm().verify(p(1)));
    2418             : #endif
    2419             : #if LIBMESH_DIM > 2
    2420           0 :   libmesh_assert(this->comm().verify(p(2)));
    2421             : #endif
    2422             : #endif // NDEBUG
    2423             : 
    2424             :   // Get a reference to the mesh object associated with the system object that calls this function
    2425           0 :   const MeshBase & mesh = this->get_mesh();
    2426             : 
    2427             :   // Use an existing PointLocator or create a new one
    2428           0 :   std::unique_ptr<PointLocatorBase> locator_ptr = mesh.sub_point_locator();
    2429           0 :   PointLocatorBase & locator = *locator_ptr;
    2430             : 
    2431           0 :   if (!insist_on_success || !mesh.is_serial())
    2432           0 :     locator.enable_out_of_mesh_mode();
    2433             : 
    2434             :   // Get a pointer to an element that contains p and allows us to
    2435             :   // evaluate var
    2436             :   const std::set<subdomain_id_type> & raw_subdomains =
    2437           0 :     this->variable(var).active_subdomains();
    2438             :   const std::set<subdomain_id_type> * implicit_subdomains =
    2439           0 :     raw_subdomains.empty() ? nullptr : &raw_subdomains;
    2440           0 :   const Elem * e = locator(p, implicit_subdomains);
    2441             : 
    2442           0 :   Gradient grad_u;
    2443             : 
    2444           0 :   if (e && this->get_dof_map().is_evaluable(*e, var))
    2445           0 :     grad_u = point_gradient(var, p, *e, sol);
    2446             : 
    2447             :   // If I have an element containing p, then let's let everyone know
    2448             :   processor_id_type lowest_owner =
    2449           0 :     (e && (e->processor_id() == this->processor_id())) ?
    2450           0 :     this->processor_id() : this->n_processors();
    2451           0 :   this->comm().min(lowest_owner);
    2452             : 
    2453             :   // Everybody should get their value from a processor that was able
    2454             :   // to compute it.
    2455             :   // If nobody admits owning the point, we may have a problem.
    2456           0 :   if (lowest_owner != this->n_processors())
    2457           0 :     this->comm().broadcast(grad_u, lowest_owner);
    2458             :   else
    2459           0 :     libmesh_assert(!insist_on_success);
    2460             : 
    2461           0 :   return grad_u;
    2462           0 : }
    2463             : 
    2464             : 
    2465           0 : Gradient System::point_gradient(unsigned int var,
    2466             :                                 const Point & p,
    2467             :                                 const Elem & e,
    2468             :                                 const NumericVector<Number> *sol) const
    2469             : {
    2470             :   // Ensuring that the given point is really in the element is an
    2471             :   // expensive assert, but as long as debugging is turned on we might
    2472             :   // as well try to catch a particularly nasty potential error
    2473           0 :   libmesh_assert (e.contains_point(p));
    2474             : 
    2475           0 :   if (!sol)
    2476           0 :     sol = this->current_local_solution.get();
    2477             : 
    2478             :   // Get the dof map to get the proper indices for our computation
    2479           0 :   const DofMap & dof_map = this->get_dof_map();
    2480             : 
    2481             :   // write the element dimension into a separate variable.
    2482           0 :   const unsigned int dim = e.dim();
    2483             : 
    2484             :   // Make sure we can evaluate on this element.
    2485           0 :   libmesh_assert (dof_map.is_evaluable(e, var));
    2486             : 
    2487             :   // Need dof_indices for phi[i][j]
    2488           0 :   std::vector<dof_id_type> dof_indices;
    2489             : 
    2490             :   // Fill in the dof_indices for our element
    2491           0 :   dof_map.dof_indices (&e, dof_indices, var);
    2492             : 
    2493             :   // Get the no of dofs associated with this point
    2494             :   const unsigned int num_dofs = cast_int<unsigned int>
    2495           0 :     (dof_indices.size());
    2496             : 
    2497           0 :   FEType fe_type = dof_map.variable_type(var);
    2498             : 
    2499             :   // Map the physical co-ordinates to the master co-ordinates
    2500           0 :   Point coor = FEMap::inverse_map(dim, &e, p);
    2501             : 
    2502             :   // get the shape function value via the FEInterface to also handle the case
    2503             :   // of infinite elements correctly, the shape function is not fe->phi().
    2504           0 :   FEComputeData fe_data(this->get_equation_systems(), coor);
    2505           0 :   fe_data.enable_derivative();
    2506           0 :   FEInterface::compute_data(dim, fe_type, &e, fe_data);
    2507             : 
    2508             :   // Get ready to accumulate a gradient
    2509           0 :   Gradient grad_u;
    2510             : 
    2511           0 :   for (unsigned int l=0; l<num_dofs; l++)
    2512             :     {
    2513             :       // Chartesian coordinates have always LIBMESH_DIM entries,
    2514             :       // local coordinates have as many coordinates as the element has.
    2515           0 :       for (std::size_t v=0; v<dim; v++)
    2516           0 :         for (std::size_t xyz=0; xyz<LIBMESH_DIM; xyz++)
    2517             :           {
    2518             :             // FIXME: this needs better syntax: It is matrix-vector multiplication.
    2519           0 :             grad_u(xyz) += fe_data.local_transform[v][xyz]
    2520           0 :               * fe_data.dshape[l](v)
    2521           0 :               * (*sol)(dof_indices[l]);
    2522             :           }
    2523             :     }
    2524             : 
    2525           0 :   return grad_u;
    2526           0 : }
    2527             : 
    2528             : 
    2529             : 
    2530           0 : Gradient System::point_gradient(unsigned int var, const Point & p, const Elem * e) const
    2531             : {
    2532           0 :   libmesh_assert(e);
    2533           0 :   return this->point_gradient(var, p, *e);
    2534             : }
    2535             : 
    2536             : 
    2537             : 
    2538           0 : Gradient System::point_gradient(unsigned int var, const Point & p, const NumericVector<Number> * sol) const
    2539             : {
    2540           0 :   return this->point_gradient(var, p, true, sol);
    2541             : }
    2542             : 
    2543             : 
    2544             : 
    2545             : // We can only accumulate a hessian with --enable-second
    2546             : #ifdef LIBMESH_ENABLE_SECOND_DERIVATIVES
    2547           0 : Tensor System::point_hessian(unsigned int var,
    2548             :                              const Point & p,
    2549             :                              const bool insist_on_success,
    2550             :                              const NumericVector<Number> *sol) const
    2551             : {
    2552             :   // This function must be called on every processor; there's no
    2553             :   // telling where in the partition p falls.
    2554           0 :   parallel_object_only();
    2555             : 
    2556             :   // And every processor had better agree about which point we're
    2557             :   // looking for
    2558             : #ifndef NDEBUG
    2559           0 :   libmesh_assert(this->comm().verify(p(0)));
    2560             : #if LIBMESH_DIM > 1
    2561           0 :   libmesh_assert(this->comm().verify(p(1)));
    2562             : #endif
    2563             : #if LIBMESH_DIM > 2
    2564           0 :   libmesh_assert(this->comm().verify(p(2)));
    2565             : #endif
    2566             : #endif // NDEBUG
    2567             : 
    2568             :   // Get a reference to the mesh object associated with the system object that calls this function
    2569           0 :   const MeshBase & mesh = this->get_mesh();
    2570             : 
    2571             :   // Use an existing PointLocator or create a new one
    2572           0 :   std::unique_ptr<PointLocatorBase> locator_ptr = mesh.sub_point_locator();
    2573           0 :   PointLocatorBase & locator = *locator_ptr;
    2574             : 
    2575           0 :   if (!insist_on_success || !mesh.is_serial())
    2576           0 :     locator.enable_out_of_mesh_mode();
    2577             : 
    2578             :   // Get a pointer to an element that contains p and allows us to
    2579             :   // evaluate var
    2580             :   const std::set<subdomain_id_type> & raw_subdomains =
    2581           0 :     this->variable(var).active_subdomains();
    2582             :   const std::set<subdomain_id_type> * implicit_subdomains =
    2583           0 :     raw_subdomains.empty() ? nullptr : &raw_subdomains;
    2584           0 :   const Elem * e = locator(p, implicit_subdomains);
    2585             : 
    2586           0 :   Tensor hess_u;
    2587             : 
    2588           0 :   if (e && this->get_dof_map().is_evaluable(*e, var))
    2589           0 :     hess_u = point_hessian(var, p, *e, sol);
    2590             : 
    2591             :   // If I have an element containing p, then let's let everyone know
    2592             :   processor_id_type lowest_owner =
    2593           0 :     (e && (e->processor_id() == this->processor_id())) ?
    2594           0 :     this->processor_id() : this->n_processors();
    2595           0 :   this->comm().min(lowest_owner);
    2596             : 
    2597             :   // Everybody should get their value from a processor that was able
    2598             :   // to compute it.
    2599             :   // If nobody admits owning the point, we may have a problem.
    2600           0 :   if (lowest_owner != this->n_processors())
    2601           0 :     this->comm().broadcast(hess_u, lowest_owner);
    2602             :   else
    2603           0 :     libmesh_assert(!insist_on_success);
    2604             : 
    2605           0 :   return hess_u;
    2606           0 : }
    2607             : 
    2608           0 : Tensor System::point_hessian(unsigned int var,
    2609             :                              const Point & p,
    2610             :                              const Elem & e,
    2611             :                              const NumericVector<Number> *sol) const
    2612             : {
    2613             :   // Ensuring that the given point is really in the element is an
    2614             :   // expensive assert, but as long as debugging is turned on we might
    2615             :   // as well try to catch a particularly nasty potential error
    2616           0 :   libmesh_assert (e.contains_point(p));
    2617             : 
    2618           0 :   if (!sol)
    2619           0 :     sol = this->current_local_solution.get();
    2620             : 
    2621           0 :   if (e.infinite())
    2622           0 :     libmesh_not_implemented();
    2623             : 
    2624             :   // Get the dof map to get the proper indices for our computation
    2625           0 :   const DofMap & dof_map = this->get_dof_map();
    2626             : 
    2627             :   // Make sure we can evaluate on this element.
    2628           0 :   libmesh_assert (dof_map.is_evaluable(e, var));
    2629             : 
    2630             :   // Need dof_indices for phi[i][j]
    2631           0 :   std::vector<dof_id_type> dof_indices;
    2632             : 
    2633             :   // Fill in the dof_indices for our element
    2634           0 :   dof_map.dof_indices (&e, dof_indices, var);
    2635             : 
    2636             :   // Get the no of dofs associated with this point
    2637             :   const unsigned int num_dofs = cast_int<unsigned int>
    2638           0 :     (dof_indices.size());
    2639             : 
    2640           0 :   FEType fe_type = dof_map.variable_type(var);
    2641             : 
    2642             :   // Build a FE again so we can calculate u(p)
    2643           0 :   std::unique_ptr<FEBase> fe (FEBase::build(e.dim(), fe_type));
    2644             : 
    2645             :   // Map the physical co-ordinates to the master co-ordinates
    2646             :   // Build a vector of point co-ordinates to send to reinit
    2647           0 :   std::vector<Point> coor(1, FEMap::inverse_map(e.dim(), &e, p));
    2648             : 
    2649             :   // Get the values of the shape function derivatives
    2650           0 :   const std::vector<std::vector<RealTensor>> &  d2phi = fe->get_d2phi();
    2651             : 
    2652             :   // Reinitialize the element and compute the shape function values at coor
    2653           0 :   fe->reinit (&e, &coor);
    2654             : 
    2655             :   // Get ready to accumulate a hessian
    2656           0 :   Tensor hess_u;
    2657             : 
    2658           0 :   for (unsigned int l=0; l<num_dofs; l++)
    2659             :     {
    2660           0 :       hess_u.add_scaled (d2phi[l][0], (*sol)(dof_indices[l]));
    2661             :     }
    2662             : 
    2663           0 :   return hess_u;
    2664           0 : }
    2665             : 
    2666             : 
    2667             : 
    2668           0 : Tensor System::point_hessian(unsigned int var, const Point & p, const Elem * e) const
    2669             : {
    2670           0 :   libmesh_assert(e);
    2671           0 :   return this->point_hessian(var, p, *e);
    2672             : }
    2673             : 
    2674             : 
    2675             : 
    2676           0 : Tensor System::point_hessian(unsigned int var, const Point & p, const NumericVector<Number> * sol) const
    2677             : {
    2678           0 :   return this->point_hessian(var, p, true, sol);
    2679             : }
    2680             : 
    2681             : #else
    2682             : 
    2683             : Tensor System::point_hessian(unsigned int, const Point &, const bool,
    2684             :                              const NumericVector<Number> *) const
    2685             : {
    2686             :   libmesh_error_msg("We can only accumulate a hessian with --enable-second");
    2687             : 
    2688             :   // Avoid compiler warnings
    2689             :   return Tensor();
    2690             : }
    2691             : 
    2692             : Tensor System::point_hessian(unsigned int, const Point &, const Elem &,
    2693             :                              const NumericVector<Number> *) const
    2694             : {
    2695             :   libmesh_error_msg("We can only accumulate a hessian with --enable-second");
    2696             : 
    2697             :   // Avoid compiler warnings
    2698             :   return Tensor();
    2699             : }
    2700             : 
    2701             : Tensor System::point_hessian(unsigned int, const Point &, const Elem *) const
    2702             : {
    2703             :   libmesh_error_msg("We can only accumulate a hessian with --enable-second");
    2704             : 
    2705             :   // Avoid compiler warnings
    2706             :   return Tensor();
    2707             : }
    2708             : 
    2709             : Tensor System::point_hessian(unsigned int, const Point &, const NumericVector<Number> *) const
    2710             : {
    2711             :   libmesh_error_msg("We can only accumulate a hessian with --enable-second");
    2712             : 
    2713             :   // Avoid compiler warnings
    2714             :   return Tensor();
    2715             : }
    2716             : 
    2717             : #endif // LIBMESH_ENABLE_SECOND_DERIVATIVES
    2718             : 
    2719         560 : void System::create_static_condensation()
    2720             : {
    2721         560 :   this->get_dof_map().create_static_condensation(this->get_mesh(), *this);
    2722         560 : }
    2723             : 
    2724       89840 : bool System::has_static_condensation() const
    2725             : {
    2726       89840 :   return this->get_dof_map().has_static_condensation();
    2727             : }
    2728             : 
    2729   128319524 : unsigned int System::n_vars() const
    2730             : {
    2731   128319524 :   return this->get_dof_map().n_vars();
    2732             : }
    2733             : 
    2734    33211378 : const std::string & System::variable_name (const unsigned int i) const
    2735             : {
    2736    33211378 :   return this->get_dof_map().variable_name(i);
    2737             : }
    2738             : 
    2739           0 : bool System::identify_variable_groups () const
    2740             : {
    2741           0 :   return this->get_dof_map().identify_variable_groups();
    2742             : }
    2743             : 
    2744           0 : void System::identify_variable_groups (const bool ivg)
    2745             : {
    2746           0 :   this->get_dof_map().identify_variable_groups(ivg);
    2747           0 : }
    2748             : 
    2749           0 : unsigned int System::n_components() const
    2750             : {
    2751           0 :   return this->get_dof_map().n_components(this->get_mesh());
    2752             : }
    2753             : 
    2754      277828 : unsigned int System::n_variable_groups() const
    2755             : {
    2756      277828 :   return this->get_dof_map().n_variable_groups();
    2757             : }
    2758             : 
    2759    54840748 : const Variable & System::variable (const unsigned int i) const
    2760             : {
    2761    54840748 :   return this->get_dof_map().variable(i);
    2762             : }
    2763             : 
    2764      289325 : const VariableGroup & System::variable_group (const unsigned int vg) const
    2765             : {
    2766      289325 :   return this->get_dof_map().variable_group(vg);
    2767             : }
    2768             : 
    2769             : unsigned int
    2770     6862184 : System::variable_scalar_number (unsigned int var_num,
    2771             :                                 unsigned int component) const
    2772             : {
    2773     6862184 :   return this->get_dof_map().variable_scalar_number(var_num, component);
    2774             : }
    2775             : 
    2776    54674965 : const FEType & System::variable_type (const unsigned int i) const
    2777             : {
    2778    54674965 :   return this->get_dof_map().variable_type(i);
    2779             : }
    2780             : 
    2781       34666 : const FEType & System::variable_type (std::string_view var) const
    2782             : {
    2783       34666 :   return this->get_dof_map().variable_type(var);
    2784             : }
    2785             : 
    2786             : } // namespace libMesh

Generated by: LCOV version 1.14