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