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