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 : // Local includes
19 : #include "libmesh/dof_map.h"
20 :
21 : // libMesh includes
22 : #include "libmesh/boundary_info.h" // needed for dirichlet constraints
23 : #include "libmesh/dense_matrix.h"
24 : #include "libmesh/dense_vector.h"
25 : #include "libmesh/dirichlet_boundaries.h"
26 : #include "libmesh/elem.h"
27 : #include "libmesh/elem_range.h"
28 : #include "libmesh/fe_base.h"
29 : #include "libmesh/fe_interface.h"
30 : #include "libmesh/fe_type.h"
31 : #include "libmesh/function_base.h"
32 : #include "libmesh/int_range.h"
33 : #include "libmesh/libmesh_logging.h"
34 : #include "libmesh/linear_solver.h" // for spline Dirichlet projection solves
35 : #include "libmesh/mesh_base.h"
36 : #include "libmesh/null_output_iterator.h"
37 : #include "libmesh/mesh_tools.h" // for libmesh_assert_valid_boundary_ids()
38 : #include "libmesh/nonlinear_implicit_system.h"
39 : #include "libmesh/numeric_vector.h" // for enforce_constraints_exactly()
40 : #include "libmesh/parallel_algebra.h"
41 : #include "libmesh/parallel_elem.h"
42 : #include "libmesh/parallel_node.h"
43 : #include "libmesh/periodic_boundaries.h"
44 : #include "libmesh/periodic_boundary.h"
45 : #include "libmesh/periodic_boundary_base.h"
46 : #include "libmesh/point_locator_base.h"
47 : #include "libmesh/quadrature.h" // for dirichlet constraints
48 : #include "libmesh/raw_accessor.h"
49 : #include "libmesh/sparse_matrix.h" // needed to constrain adjoint rhs
50 : #include "libmesh/static_condensation_dof_map.h"
51 : #include "libmesh/system.h" // needed by enforce_constraints_exactly()
52 : #include "libmesh/tensor_tools.h"
53 : #include "libmesh/threads.h"
54 : #include "libmesh/enum_to_string.h"
55 : #include "libmesh/coupling_matrix.h"
56 :
57 : // TIMPI includes
58 : #include "timpi/parallel_implementation.h"
59 : #include "timpi/parallel_sync.h"
60 :
61 : // C++ Includes
62 : #include <set>
63 : #include <algorithm> // for std::count, std::fill
64 : #include <sstream>
65 : #include <cstdlib> // *must* precede <cmath> for proper std:abs() on PGI, Sun Studio CC
66 : #include <cmath>
67 : #include <memory>
68 : #include <numeric>
69 : #include <unordered_set>
70 :
71 : // Anonymous namespace to hold helper classes
72 : namespace {
73 :
74 : using namespace libMesh;
75 :
76 : class ComputeConstraints
77 : {
78 : public:
79 10854 : ComputeConstraints (DofConstraints & constraints,
80 : DofMap & dof_map,
81 : #ifdef LIBMESH_ENABLE_PERIODIC
82 : PeriodicBoundaries & periodic_boundaries,
83 : #endif
84 : const MeshBase & mesh,
85 380929 : const unsigned int variable_number) :
86 359221 : _constraints(constraints),
87 359221 : _dof_map(dof_map),
88 : #ifdef LIBMESH_ENABLE_PERIODIC
89 359221 : _periodic_boundaries(periodic_boundaries),
90 : #endif
91 359221 : _mesh(mesh),
92 380929 : _variable_number(variable_number)
93 10854 : {}
94 :
95 381132 : void operator()(const ConstElemRange & range) const
96 : {
97 381132 : const Variable & var_description = _dof_map.variable(_variable_number);
98 :
99 : #ifdef LIBMESH_ENABLE_PERIODIC
100 370211 : std::unique_ptr<PointLocatorBase> point_locator;
101 : const bool have_periodic_boundaries =
102 381132 : !_periodic_boundaries.empty();
103 381132 : if (have_periodic_boundaries && !range.empty())
104 3106 : point_locator = _mesh.sub_point_locator();
105 : #endif
106 :
107 8252231 : for (const auto & elem : range)
108 7871099 : if (var_description.active_on_subdomain(elem->subdomain_id()))
109 : {
110 : #ifdef LIBMESH_ENABLE_AMR
111 8555869 : FEInterface::compute_constraints (_constraints,
112 : _dof_map,
113 7856571 : _variable_number,
114 : elem);
115 : #endif
116 : #ifdef LIBMESH_ENABLE_PERIODIC
117 : // FIXME: periodic constraints won't work on a non-serial
118 : // mesh unless it's kept ghost elements from opposing
119 : // boundaries!
120 7856571 : if (have_periodic_boundaries)
121 539084 : FEInterface::compute_periodic_constraints (_constraints,
122 : _dof_map,
123 269092 : _periodic_boundaries,
124 : _mesh,
125 67498 : point_locator.get(),
126 269092 : _variable_number,
127 : elem);
128 : #endif
129 : }
130 381132 : }
131 :
132 : private:
133 : DofConstraints & _constraints;
134 : DofMap & _dof_map;
135 : #ifdef LIBMESH_ENABLE_PERIODIC
136 : PeriodicBoundaries & _periodic_boundaries;
137 : #endif
138 : const MeshBase & _mesh;
139 : const unsigned int _variable_number;
140 : };
141 :
142 :
143 :
144 : #ifdef LIBMESH_ENABLE_NODE_CONSTRAINTS
145 : class ComputeNodeConstraints
146 : {
147 : public:
148 9884 : ComputeNodeConstraints (NodeConstraints & node_constraints,
149 : #ifdef LIBMESH_ENABLE_PERIODIC
150 : PeriodicBoundaries & periodic_boundaries,
151 : #endif
152 19768 : const MeshBase & mesh) :
153 : _node_constraints(node_constraints),
154 : #ifdef LIBMESH_ENABLE_PERIODIC
155 : _periodic_boundaries(periodic_boundaries),
156 : #endif
157 19768 : _mesh(mesh)
158 9884 : {}
159 :
160 19858 : void operator()(const ConstElemRange & range) const
161 : {
162 : #ifdef LIBMESH_ENABLE_PERIODIC
163 9929 : std::unique_ptr<PointLocatorBase> point_locator;
164 19858 : bool have_periodic_boundaries = !_periodic_boundaries.empty();
165 19858 : if (have_periodic_boundaries && !range.empty())
166 272 : point_locator = _mesh.sub_point_locator();
167 : #endif
168 :
169 1059814 : for (const auto & elem : range)
170 : {
171 : #ifdef LIBMESH_ENABLE_AMR
172 1039956 : FEBase::compute_node_constraints (_node_constraints, elem);
173 : #endif
174 : #ifdef LIBMESH_ENABLE_PERIODIC
175 : // FIXME: periodic constraints won't work on a non-serial
176 : // mesh unless it's kept ghost elements from opposing
177 : // boundaries!
178 1039956 : if (have_periodic_boundaries)
179 325430 : FEBase::compute_periodic_node_constraints (_node_constraints,
180 130172 : _periodic_boundaries,
181 : _mesh,
182 65086 : point_locator.get(),
183 : elem);
184 : #endif
185 : }
186 19858 : }
187 :
188 : private:
189 : NodeConstraints & _node_constraints;
190 : #ifdef LIBMESH_ENABLE_PERIODIC
191 : PeriodicBoundaries & _periodic_boundaries;
192 : #endif
193 : const MeshBase & _mesh;
194 : };
195 : #endif // LIBMESH_ENABLE_NODE_CONSTRAINTS
196 :
197 :
198 : #ifdef LIBMESH_ENABLE_DIRICHLET
199 :
200 : /**
201 : * This functor class hierarchy adds a constraint row to a DofMap
202 : */
203 : class AddConstraint
204 : {
205 : protected:
206 : DofMap & dof_map;
207 :
208 : public:
209 21733 : AddConstraint(DofMap & dof_map_in) : dof_map(dof_map_in) {}
210 :
211 : virtual void operator()(dof_id_type dof_number,
212 : const DofConstraintRow & constraint_row,
213 : const Number constraint_rhs) const = 0;
214 : };
215 :
216 : class AddPrimalConstraint : public AddConstraint
217 : {
218 : public:
219 19548 : AddPrimalConstraint(DofMap & dof_map_in) : AddConstraint(dof_map_in) {}
220 :
221 661730 : virtual void operator()(dof_id_type dof_number,
222 : const DofConstraintRow & constraint_row,
223 : const Number constraint_rhs) const
224 : {
225 661730 : if (!dof_map.is_constrained_dof(dof_number))
226 326742 : dof_map.add_constraint_row (dof_number, constraint_row,
227 : constraint_rhs, true);
228 661730 : }
229 : };
230 :
231 : class AddAdjointConstraint : public AddConstraint
232 : {
233 : private:
234 : const unsigned int qoi_index;
235 :
236 : public:
237 62 : AddAdjointConstraint(DofMap & dof_map_in, unsigned int qoi_index_in)
238 2247 : : AddConstraint(dof_map_in), qoi_index(qoi_index_in) {}
239 :
240 14304 : virtual void operator()(dof_id_type dof_number,
241 : const DofConstraintRow & constraint_row,
242 : const Number constraint_rhs) const
243 : {
244 14304 : dof_map.add_adjoint_constraint_row
245 14304 : (qoi_index, dof_number, constraint_row, constraint_rhs,
246 : true);
247 14304 : }
248 : };
249 :
250 :
251 :
252 : /**
253 : * This class implements turning an arbitrary
254 : * boundary function into Dirichlet constraints. It
255 : * may be executed in parallel on multiple threads.
256 : */
257 2083 : class ConstrainDirichlet
258 : {
259 : private:
260 : DofMap & dof_map;
261 : const MeshBase & mesh;
262 : const Real time;
263 : const DirichletBoundaries & dirichlets;
264 :
265 : const AddConstraint & add_fn;
266 :
267 : // Whether to record each element on which this functor applies a
268 : // DirichletBoundary
269 : const bool record_elems;
270 :
271 : // The elements on which this functor applied a DirichletBoundary,
272 : // if record_elems is true
273 : std::vector<const Elem *> acted_on_elems;
274 :
275 682644 : static Number f_component (FunctionBase<Number> * f,
276 : FEMFunctionBase<Number> * f_fem,
277 : const FEMContext * c,
278 : unsigned int i,
279 : const Point & p,
280 : Real time)
281 : {
282 682644 : if (f_fem)
283 : {
284 0 : if (c)
285 0 : return f_fem->component(*c, i, p, time);
286 : else
287 0 : return std::numeric_limits<Real>::quiet_NaN();
288 : }
289 682644 : return f->component(i, p, time);
290 : }
291 :
292 30972 : static Gradient g_component (FunctionBase<Gradient> * g,
293 : FEMFunctionBase<Gradient> * g_fem,
294 : const FEMContext * c,
295 : unsigned int i,
296 : const Point & p,
297 : Real time)
298 : {
299 30972 : if (g_fem)
300 : {
301 0 : if (c)
302 0 : return g_fem->component(*c, i, p, time);
303 : else
304 0 : return std::numeric_limits<Number>::quiet_NaN();
305 : }
306 30972 : return g->component(i, p, time);
307 : }
308 :
309 :
310 :
311 : /**
312 : * Handy struct to pass around BoundaryInfo for a single Elem. Must
313 : * be created with a reference to a BoundaryInfo object and a map
314 : * from boundary_id -> set<DirichletBoundary *> objects involving
315 : * that id.
316 : */
317 : struct SingleElemBoundaryInfo
318 : {
319 21163 : SingleElemBoundaryInfo(const BoundaryInfo & bi,
320 21799 : const std::map<boundary_id_type, std::set<std::pair<unsigned int, DirichletBoundary *>>> & ordered_map_in) :
321 20527 : boundary_info(bi),
322 20527 : boundary_id_to_ordered_dirichlet_boundaries(ordered_map_in),
323 20527 : elem(nullptr),
324 20527 : n_sides(0),
325 20527 : n_edges(0),
326 21799 : n_nodes(0)
327 21163 : {}
328 :
329 : const BoundaryInfo & boundary_info;
330 : const std::map<boundary_id_type, std::set<std::pair<unsigned int, DirichletBoundary *>>> & boundary_id_to_ordered_dirichlet_boundaries;
331 : const Elem * elem;
332 :
333 : unsigned short n_sides;
334 : unsigned short n_edges;
335 : unsigned short n_nodes;
336 :
337 : // Mapping from DirichletBoundary objects which are active on this
338 : // element to sides/nodes/edges/shellfaces of this element which
339 : // they are active on.
340 : std::map<const DirichletBoundary *, std::vector<bool>> is_boundary_node_map;
341 : std::map<const DirichletBoundary *, std::vector<bool>> is_boundary_side_map;
342 : std::map<const DirichletBoundary *, std::vector<bool>> is_boundary_edge_map;
343 : std::map<const DirichletBoundary *, std::vector<bool>> is_boundary_shellface_map;
344 :
345 : std::map<const DirichletBoundary *, std::vector<bool>> is_boundary_nodeset_map;
346 :
347 : // The set of (dirichlet_id, DirichletBoundary) pairs which have at least one boundary
348 : // id related to this Elem.
349 : std::set<std::pair<unsigned int, DirichletBoundary *>> ordered_dbs;
350 :
351 : /**
352 : * Given a single Elem, fills the SingleElemBoundaryInfo struct with
353 : * required data.
354 : *
355 : * @return true if this Elem has _any_ boundary ids associated with
356 : * it, false otherwise.
357 : */
358 1174808 : bool reinit(const Elem * elem_in)
359 : {
360 1174808 : elem = elem_in;
361 :
362 1174808 : n_sides = elem->n_sides();
363 1174808 : n_edges = elem->n_edges();
364 1174808 : n_nodes = elem->n_nodes();
365 :
366 : // objects and node/side/edge/shellface ids.
367 100782 : is_boundary_node_map.clear();
368 100782 : is_boundary_side_map.clear();
369 100782 : is_boundary_edge_map.clear();
370 100782 : is_boundary_shellface_map.clear();
371 100782 : is_boundary_nodeset_map.clear();
372 :
373 : // Clear any DirichletBoundaries from the previous Elem
374 100782 : ordered_dbs.clear();
375 :
376 : // Update has_dirichlet_constraint below, and if it remains false then
377 : // we can skip this element since there are not constraints to impose.
378 100782 : bool has_dirichlet_constraint = false;
379 :
380 : // Container to catch boundary ids handed back for sides,
381 : // nodes, and edges in the loops below.
382 100782 : std::vector<boundary_id_type> ids_vec;
383 :
384 6235846 : for (unsigned char s = 0; s != n_sides; ++s)
385 : {
386 : // First see if this side has been requested
387 5061038 : boundary_info.boundary_ids (elem, s, ids_vec);
388 :
389 433177 : bool do_this_side = false;
390 5316565 : for (const auto & bc_id : ids_vec)
391 : {
392 277120 : if (auto it = boundary_id_to_ordered_dirichlet_boundaries.find(bc_id);
393 21593 : it != boundary_id_to_ordered_dirichlet_boundaries.end())
394 : {
395 13307 : do_this_side = true;
396 :
397 : // Associate every DirichletBoundary object that has this bc_id with the current Elem
398 156173 : ordered_dbs.insert(it->second.begin(), it->second.end());
399 :
400 : // Turn on the flag for the current side for each DirichletBoundary
401 321791 : for (const auto & db_pair : it->second)
402 : {
403 : // Attempt to emplace an empty vector. If there
404 : // is already an entry, the insertion will fail
405 : // and we'll get an iterator back to the
406 : // existing entry. Either way, we'll then set
407 : // index s of that vector to "true".
408 302976 : auto pr = is_boundary_side_map.emplace(db_pair.second, std::vector<bool>(n_sides, false));
409 28260 : pr.first->second[s] = true;
410 : }
411 : }
412 : }
413 :
414 5061038 : if (!do_this_side)
415 4484995 : continue;
416 :
417 13307 : has_dirichlet_constraint = true;
418 :
419 : // Then determine what nodes are on this side
420 1397628 : for (unsigned int n = 0; n != n_nodes; ++n)
421 1241455 : if (elem->is_node_on_side(n,s))
422 : {
423 : // Attempt to emplace an empty vector. If there is
424 : // already an entry, the insertion will fail and we'll
425 : // get an iterator back to the existing entry. Either
426 : // way, we'll then set index n of that vector to
427 : // "true".
428 1105933 : for (const auto & db_pair : ordered_dbs)
429 : {
430 : // Only add this as a boundary node for this db if
431 : // it is also a boundary side for this db.
432 568436 : if (auto side_it = is_boundary_side_map.find(db_pair.second);
433 568436 : side_it != is_boundary_side_map.end() && side_it->second[s])
434 : {
435 1036156 : auto pr = is_boundary_node_map.emplace(db_pair.second, std::vector<bool>(n_nodes, false));
436 95892 : pr.first->second[n] = true;
437 : }
438 : }
439 : }
440 :
441 : // Finally determine what edges are on this side
442 1297499 : for (unsigned int e = 0; e != n_edges; ++e)
443 1141326 : if (elem->is_edge_on_side(e,s))
444 : {
445 : // Attempt to emplace an empty vector. If there is
446 : // already an entry, the insertion will fail and we'll
447 : // get an iterator back to the existing entry. Either
448 : // way, we'll then set index e of that vector to
449 : // "true".
450 742451 : for (const auto & db_pair : ordered_dbs)
451 : {
452 : // Only add this as a boundary edge for this db if
453 : // it is also a boundary side for this db.
454 380548 : if (auto side_it = is_boundary_side_map.find(db_pair.second);
455 380548 : side_it != is_boundary_side_map.end() && side_it->second[s])
456 : {
457 695414 : auto pr = is_boundary_edge_map.emplace(db_pair.second, std::vector<bool>(n_edges, false));
458 64010 : pr.first->second[e] = true;
459 : }
460 : }
461 : }
462 : } // for (s = 0..n_sides)
463 :
464 : // We can also impose Dirichlet boundary conditions on nodes, so we should
465 : // also independently check whether the nodes have been requested
466 8994669 : for (unsigned int n=0; n != n_nodes; ++n)
467 : {
468 8484558 : boundary_info.boundary_ids (elem->node_ptr(n), ids_vec);
469 :
470 8390443 : for (const auto & bc_id : ids_vec)
471 : {
472 618354 : if (auto it = boundary_id_to_ordered_dirichlet_boundaries.find(bc_id);
473 47772 : it != boundary_id_to_ordered_dirichlet_boundaries.end())
474 : {
475 : // Associate every DirichletBoundary object that has this bc_id with the current Elem
476 219750 : ordered_dbs.insert(it->second.begin(), it->second.end());
477 :
478 : // Turn on the flag for the current node for each DirichletBoundary
479 459522 : for (const auto & db_pair : it->second)
480 : {
481 439116 : auto pr = is_boundary_node_map.emplace(db_pair.second, std::vector<bool>(n_nodes, false));
482 40428 : pr.first->second[n] = true;
483 :
484 439116 : auto pr2 = is_boundary_nodeset_map.emplace(db_pair.second, std::vector<bool>(n_nodes, false));
485 40428 : pr2.first->second[n] = true;
486 : }
487 :
488 18498 : has_dirichlet_constraint = true;
489 : }
490 : }
491 : } // for (n = 0..n_nodes)
492 :
493 : // We can also impose Dirichlet boundary conditions on edges, so we should
494 : // also independently check whether the edges have been requested
495 8067218 : for (unsigned short e=0; e != n_edges; ++e)
496 : {
497 6892410 : boundary_info.edge_boundary_ids (elem, e, ids_vec);
498 :
499 587195 : bool do_this_side = false;
500 6892650 : for (const auto & bc_id : ids_vec)
501 : {
502 260 : if (auto it = boundary_id_to_ordered_dirichlet_boundaries.find(bc_id);
503 20 : it != boundary_id_to_ordered_dirichlet_boundaries.end())
504 : {
505 20 : do_this_side = true;
506 :
507 : // We need to loop over all DirichletBoundary objects associated with bc_id
508 240 : ordered_dbs.insert(it->second.begin(), it->second.end());
509 :
510 : // Turn on the flag for the current edge for each DirichletBoundary
511 768 : for (const auto & db_pair : it->second)
512 : {
513 968 : auto pr = is_boundary_edge_map.emplace(db_pair.second, std::vector<bool>(n_edges, false));
514 88 : pr.first->second[e] = true;
515 : }
516 : }
517 : }
518 :
519 6892410 : if (!do_this_side)
520 6304995 : continue;
521 :
522 20 : has_dirichlet_constraint = true;
523 :
524 : // Then determine what nodes are on this edge
525 2160 : for (unsigned int n = 0; n != n_nodes; ++n)
526 1920 : if (elem->is_node_on_edge(n,e))
527 : {
528 : // Attempt to emplace an empty vector. If there is
529 : // already an entry, the insertion will fail and we'll
530 : // get an iterator back to the existing entry. Either
531 : // way, we'll then set index n of that vector to
532 : // "true".
533 1536 : for (const auto & db_pair : ordered_dbs)
534 : {
535 : // Only add this as a boundary node for this db if
536 : // it is also a boundary edge for this db.
537 1056 : if (auto edge_it = is_boundary_edge_map.find(db_pair.second);
538 1056 : edge_it != is_boundary_edge_map.end() && edge_it->second[e])
539 : {
540 1936 : auto pr = is_boundary_node_map.emplace(db_pair.second, std::vector<bool>(n_nodes, false));
541 176 : pr.first->second[n] = true;
542 : }
543 : }
544 : }
545 : }
546 :
547 : // We can also impose Dirichlet boundary conditions on shellfaces, so we should
548 : // also independently check whether the shellfaces have been requested
549 3524424 : for (unsigned short shellface=0; shellface != 2; ++shellface)
550 : {
551 2349616 : boundary_info.shellface_boundary_ids (elem, shellface, ids_vec);
552 201564 : bool do_this_shellface = false;
553 :
554 2389576 : for (const auto & bc_id : ids_vec)
555 : {
556 43290 : if (auto it = boundary_id_to_ordered_dirichlet_boundaries.find(bc_id);
557 3330 : it != boundary_id_to_ordered_dirichlet_boundaries.end())
558 : {
559 1 : has_dirichlet_constraint = true;
560 1 : do_this_shellface = true;
561 :
562 : // We need to loop over all DirichletBoundary objects associated with bc_id
563 12 : ordered_dbs.insert(it->second.begin(), it->second.end());
564 :
565 : // Turn on the flag for the current shellface for each DirichletBoundary
566 24 : for (const auto & db_pair : it->second)
567 : {
568 22 : auto pr = is_boundary_shellface_map.emplace(db_pair.second, std::vector<bool>(/*n_shellfaces=*/2, false));
569 2 : pr.first->second[shellface] = true;
570 : }
571 : }
572 : }
573 :
574 2349616 : if (do_this_shellface)
575 : {
576 : // Shellface BCs induce BCs on all the nodes of a shell Elem
577 60 : for (unsigned int n = 0; n != n_nodes; ++n)
578 96 : for (const auto & db_pair : ordered_dbs)
579 : {
580 : // Only add this as a boundary node for this db if
581 : // it is also a boundary shellface for this db.
582 48 : if (auto side_it = is_boundary_shellface_map.find(db_pair.second);
583 48 : side_it != is_boundary_shellface_map.end() && side_it->second[shellface])
584 : {
585 88 : auto pr = is_boundary_node_map.emplace(db_pair.second, std::vector<bool>(n_nodes, false));
586 8 : pr.first->second[n] = true;
587 : }
588 : }
589 : }
590 : } // for (shellface = 0..2)
591 :
592 1275590 : return has_dirichlet_constraint;
593 : } // SingleElemBoundaryInfo::reinit()
594 :
595 : }; // struct SingleElemBoundaryInfo
596 :
597 :
598 :
599 : template<typename OutputType>
600 167492 : void apply_lagrange_dirichlet_impl(const SingleElemBoundaryInfo & sebi,
601 : const Variable & variable,
602 : const DirichletBoundary & dirichlet,
603 : FEMContext & fem_context) const
604 : {
605 : // Get pointer to the Elem we are currently working on
606 167492 : const Elem * elem = sebi.elem;
607 :
608 : // Per-subdomain variables don't need to be projected on
609 : // elements where they're not active
610 167492 : if (!variable.active_on_subdomain(elem->subdomain_id()))
611 0 : return;
612 :
613 14369 : FunctionBase<Number> * f = dirichlet.f.get();
614 14369 : FEMFunctionBase<Number> * f_fem = dirichlet.f_fem.get();
615 :
616 167492 : const System * f_system = dirichlet.f_system;
617 :
618 : // We need data to project
619 14369 : libmesh_assert(f || f_fem);
620 14369 : libmesh_assert(!(f && f_fem));
621 :
622 : // Iff our data depends on a system, we should have it.
623 14369 : libmesh_assert(!(f && f_system));
624 14369 : libmesh_assert(!(f_fem && !f_system));
625 :
626 : // The new element coefficients. For Lagrange FEs, these are the
627 : // nodal values.
628 167492 : DenseVector<Number> Ue;
629 :
630 : // Get a reference to the fe_type associated with this variable
631 14369 : const FEType & fe_type = variable.type();
632 :
633 : // Dimension of the vector-valued FE (1 for scalar-valued FEs)
634 167492 : unsigned int n_vec_dim = FEInterface::n_vec_dim(mesh, fe_type);
635 :
636 : const unsigned int var_component =
637 28738 : variable.first_scalar_number();
638 :
639 : // Get this Variable's number, as determined by the System.
640 28738 : const unsigned int var = variable.number();
641 :
642 : // If our supplied functions require a FEMContext, and if we have
643 : // an initialized solution to use with that FEMContext, then
644 : // create one. We're not going to use elem_jacobian or
645 : // subjacobians here so don't allocate them.
646 153123 : std::unique_ptr<FEMContext> context;
647 167492 : if (f_fem)
648 : {
649 0 : libmesh_assert(f_system);
650 0 : if (f_system->current_local_solution->initialized())
651 : {
652 0 : context = std::make_unique<FEMContext>
653 0 : (*f_system, nullptr,
654 0 : /* allocate local_matrices = */ false);
655 0 : f_fem->init_context(*context);
656 : }
657 : }
658 :
659 167492 : if (f_system && context.get())
660 0 : context->pre_fe_reinit(*f_system, elem);
661 :
662 : // Also pre-init the fem_context() we were passed on the current Elem.
663 167492 : fem_context.pre_fe_reinit(fem_context.get_system(), elem);
664 :
665 : // Get a reference to the DOF indices for the current element
666 : const std::vector<dof_id_type> & dof_indices =
667 14369 : fem_context.get_dof_indices(var);
668 :
669 : // The number of DOFs on the element
670 : const unsigned int n_dofs =
671 28738 : cast_int<unsigned int>(dof_indices.size());
672 :
673 : // Fixed vs. free DoFs on edge/face projections
674 181861 : std::vector<char> dof_is_fixed(n_dofs, false); // bools
675 :
676 : // Zero the interpolated values
677 153123 : Ue.resize (n_dofs); Ue.zero();
678 :
679 : // For Lagrange elements, side, edge, and shellface BCs all
680 : // "induce" boundary conditions on the nodes of those entities.
681 : // In SingleElemBoundaryInfo::reinit(), we therefore set entries
682 : // in the "is_boundary_node_map" container based on side and
683 : // shellface BCs, Then, when we actually apply constraints, we
684 : // only have to check whether any Nodes are in this container, and
685 : // compute values as necessary.
686 14369 : unsigned int current_dof = 0;
687 1495513 : for (unsigned int n=0; n!= sebi.n_nodes; ++n)
688 : {
689 : // For Lagrange this can return 0 (in case of a lower-order FE
690 : // on a higher-order Elem) or 1. This function accounts for the
691 : // Elem::p_level() internally.
692 113249 : const unsigned int nc =
693 1328021 : FEInterface::n_dofs_at_node (fe_type, elem, n);
694 :
695 : // If there are no DOFs at this node, then it doesn't matter
696 : // if it's technically a boundary node or not, there's nothing
697 : // to constrain.
698 1328021 : if (!nc)
699 101893 : continue;
700 :
701 : // Check whether the current node is a boundary node
702 1281617 : auto is_boundary_node_it = sebi.is_boundary_node_map.find(&dirichlet);
703 109363 : const bool is_boundary_node =
704 1390980 : (is_boundary_node_it != sebi.is_boundary_node_map.end() &&
705 109363 : is_boundary_node_it->second[n]);
706 :
707 : // Check whether the current node is in a boundary nodeset
708 1281617 : auto is_boundary_nodeset_it = sebi.is_boundary_nodeset_map.find(&dirichlet);
709 109363 : const bool is_boundary_nodeset =
710 1349051 : (is_boundary_nodeset_it != sebi.is_boundary_nodeset_map.end() &&
711 67434 : is_boundary_nodeset_it->second[n]);
712 :
713 : // If node is neither a boundary node or from a boundary nodeset, go to the next one.
714 1281617 : if ( !(is_boundary_node || is_boundary_nodeset) )
715 : {
716 694985 : current_dof += nc;
717 694985 : continue;
718 : }
719 :
720 : // Compute function values, storing them in Ue
721 49988 : libmesh_assert_equal_to (nc, n_vec_dim);
722 1173264 : for (unsigned int c = 0; c < n_vec_dim; c++)
723 : {
724 636620 : Ue(current_dof+c) =
725 586632 : f_component(f, f_fem, context.get(), var_component+c,
726 586632 : elem->point(n), time);
727 636620 : dof_is_fixed[current_dof+c] = true;
728 : }
729 586632 : current_dof += n_vec_dim;
730 : } // end for (n=0..n_nodes)
731 :
732 : // Lock the DofConstraints since it is shared among threads.
733 : {
734 28738 : Threads::spin_mutex::scoped_lock lock(Threads::spin_mtx);
735 :
736 1449109 : for (unsigned int i = 0; i < n_dofs; i++)
737 : {
738 218726 : DofConstraintRow empty_row;
739 1390980 : if (dof_is_fixed[i] && !libmesh_isnan(Ue(i)))
740 636620 : add_fn (dof_indices[i], empty_row, Ue(i));
741 : }
742 : }
743 :
744 138754 : } // apply_lagrange_dirichlet_impl
745 :
746 :
747 :
748 : template<typename OutputType>
749 16382 : void apply_dirichlet_impl(const SingleElemBoundaryInfo & sebi,
750 : const Variable & variable,
751 : const DirichletBoundary & dirichlet,
752 : FEMContext & fem_context) const
753 : {
754 : // Get pointer to the Elem we are currently working on
755 16382 : const Elem * elem = sebi.elem;
756 :
757 : // Per-subdomain variables don't need to be projected on
758 : // elements where they're not active
759 16382 : if (!variable.active_on_subdomain(elem->subdomain_id()))
760 0 : return;
761 :
762 : typedef OutputType OutputShape;
763 : typedef typename TensorTools::IncrementRank<OutputShape>::type OutputGradient;
764 : //typedef typename TensorTools::IncrementRank<OutputGradient>::type OutputTensor;
765 : typedef typename TensorTools::MakeNumber<OutputShape>::type OutputNumber;
766 : typedef typename TensorTools::IncrementRank<OutputNumber>::type OutputNumberGradient;
767 : //typedef typename TensorTools::IncrementRank<OutputNumberGradient>::type OutputNumberTensor;
768 :
769 1367 : FunctionBase<Number> * f = dirichlet.f.get();
770 1367 : FunctionBase<Gradient> * g = dirichlet.g.get();
771 :
772 1367 : FEMFunctionBase<Number> * f_fem = dirichlet.f_fem.get();
773 1367 : FEMFunctionBase<Gradient> * g_fem = dirichlet.g_fem.get();
774 :
775 16382 : const System * f_system = dirichlet.f_system;
776 :
777 : // We need data to project
778 1367 : libmesh_assert(f || f_fem);
779 1367 : libmesh_assert(!(f && f_fem));
780 :
781 : // Iff our data depends on a system, we should have it.
782 1367 : libmesh_assert(!(f && f_system));
783 1367 : libmesh_assert(!(f_fem && !f_system));
784 :
785 : // The element matrix and RHS for projections.
786 : // Note that Ke is always real-valued, whereas
787 : // Fe may be complex valued if complex number
788 : // support is enabled
789 19116 : DenseMatrix<Real> Ke;
790 16382 : DenseVector<Number> Fe;
791 : // The new element coefficients
792 16382 : DenseVector<Number> Ue;
793 :
794 : // The dimensionality of the current mesh
795 16382 : const unsigned int dim = mesh.mesh_dimension();
796 :
797 : // Get a reference to the fe_type associated with this variable
798 1367 : const FEType & fe_type = variable.type();
799 :
800 : // Dimension of the vector-valued FE (1 for scalar-valued FEs)
801 16382 : unsigned int n_vec_dim = FEInterface::n_vec_dim(mesh, fe_type);
802 :
803 : const unsigned int var_component =
804 2734 : variable.first_scalar_number();
805 :
806 : // Get this Variable's number, as determined by the System.
807 2734 : const unsigned int var = variable.number();
808 :
809 : // The type of projections done depend on the FE's continuity.
810 16382 : FEContinuity cont = FEInterface::get_continuity(fe_type);
811 :
812 : // Make sure we have the right data available for C1 projections
813 1367 : if ((cont == C_ONE) && (fe_type.family != SUBDIVISION))
814 : {
815 : // We'll need gradient data for a C1 projection
816 459 : libmesh_assert(g || g_fem);
817 :
818 : // We currently demand that either neither nor both function
819 : // object depend on current FEM data.
820 459 : libmesh_assert(!(g && g_fem));
821 459 : libmesh_assert(!(f && g_fem));
822 459 : libmesh_assert(!(f_fem && g));
823 : }
824 :
825 : // If our supplied functions require a FEMContext, and if we have
826 : // an initialized solution to use with that FEMContext, then
827 : // create one. We're not going to use elem_jacobian or
828 : // subjacobians here so don't allocate them.
829 15015 : std::unique_ptr<FEMContext> context;
830 16382 : if (f_fem)
831 : {
832 0 : libmesh_assert(f_system);
833 0 : if (f_system->current_local_solution->initialized())
834 : {
835 0 : context = std::make_unique<FEMContext>
836 0 : (*f_system, nullptr,
837 0 : /* allocate local_matrices = */ false);
838 0 : f_fem->init_context(*context);
839 0 : if (g_fem)
840 0 : g_fem->init_context(*context);
841 : }
842 : }
843 :
844 : // There's a chicken-and-egg problem with FEMFunction-based
845 : // Dirichlet constraints: we can't evaluate the FEMFunction
846 : // until we have an initialized local solution vector, we
847 : // can't initialize local solution vectors until we have a
848 : // send list, and we can't generate a send list until we know
849 : // all our constraints
850 : //
851 : // We don't generate constraints on uninitialized systems;
852 : // currently user code will have to reinit() before any
853 : // FEMFunction-based constraints will be correct. This should
854 : // be fine, since user code would want to reinit() after
855 : // setting initial conditions anyway.
856 16382 : if (f_system && context.get())
857 0 : context->pre_fe_reinit(*f_system, elem);
858 :
859 : // Also pre-init the fem_context() we were passed on the current Elem.
860 16382 : fem_context.pre_fe_reinit(fem_context.get_system(), elem);
861 :
862 : // Get a reference to the DOF indices for the current element
863 : const std::vector<dof_id_type> & dof_indices =
864 1367 : fem_context.get_dof_indices(var);
865 :
866 : // The number of DOFs on the element
867 : const unsigned int n_dofs =
868 2734 : cast_int<unsigned int>(dof_indices.size());
869 :
870 : // Fixed vs. free DoFs on edge/face projections
871 17749 : std::vector<char> dof_is_fixed(n_dofs, false); // bools
872 17749 : std::vector<int> free_dof(n_dofs, 0);
873 :
874 : // Zero the interpolated values
875 15015 : Ue.resize (n_dofs); Ue.zero();
876 :
877 : // In general, we need a series of
878 : // projections to ensure a unique and continuous
879 : // solution. We start by interpolating boundary nodes, then
880 : // hold those fixed and project boundary edges, then hold
881 : // those fixed and project boundary faces,
882 :
883 : // Interpolate node values first.
884 : //
885 : // If we have non-vertex nodes that have a boundary nodeset, we
886 : // need to interpolate them directly, but that had better be
887 : // happening in the apply_lagrange_dirichlet_impl code path,
888 : // because you *can't* interpolate to evaluate non-nodal FE
889 : // coefficients.
890 1367 : unsigned int current_dof = 0;
891 146618 : for (unsigned int n=0; n!= sebi.n_nodes; ++n)
892 : {
893 : // FIXME: this should go through the DofMap,
894 : // not duplicate dof_indices code badly!
895 :
896 : // Get the number of DOFs at this node, accounting for
897 : // Elem::p_level() internally.
898 10864 : const unsigned int nc =
899 130236 : FEInterface::n_dofs_at_node (fe_type, elem, n);
900 :
901 : // Get a reference to the "is_boundary_node" flags for the
902 : // current DirichletBoundary object. In case the map does not
903 : // contain an entry for this DirichletBoundary object, it
904 : // means there are no boundary nodes active.
905 130236 : auto is_boundary_node_it = sebi.is_boundary_node_map.find(&dirichlet);
906 :
907 : // The current n is not a boundary node if either there is no
908 : // boundary_node_map for this DirichletBoundary object, or if
909 : // there is but the entry in the corresponding vector is
910 : // false.
911 10864 : const bool not_boundary_node =
912 141100 : (is_boundary_node_it == sebi.is_boundary_node_map.end() ||
913 21728 : !is_boundary_node_it->second[n]);
914 :
915 130236 : if (!elem->is_vertex(n) || not_boundary_node)
916 : {
917 93008 : current_dof += nc;
918 7758 : continue;
919 : }
920 37228 : if (cont == DISCONTINUOUS)
921 : {
922 0 : libmesh_assert_equal_to (nc, 0);
923 : }
924 : // Assume that C_ZERO elements have a single nodal
925 : // value shape function
926 37228 : else if ((cont == C_ZERO) || (fe_type.family == SUBDIVISION))
927 : {
928 2206 : libmesh_assert_equal_to (nc, n_vec_dim);
929 64656 : for (unsigned int c = 0; c < n_vec_dim; c++)
930 : {
931 41366 : Ue(current_dof+c) =
932 38184 : f_component(f, f_fem, context.get(), var_component+c,
933 38184 : elem->point(n), time);
934 41366 : dof_is_fixed[current_dof+c] = true;
935 : }
936 26472 : current_dof += n_vec_dim;
937 22060 : }
938 : // The hermite element vertex shape functions are weird
939 10756 : else if (fe_type.family == HERMITE)
940 : {
941 4628 : Ue(current_dof) =
942 4272 : f_component(f, f_fem, context.get(), var_component,
943 4272 : elem->point(n), time);
944 4272 : dof_is_fixed[current_dof] = true;
945 4272 : current_dof++;
946 : Gradient grad =
947 4272 : g_component(g, g_fem, context.get(), var_component,
948 4272 : elem->point(n), time);
949 : // x derivative
950 4272 : Ue(current_dof) = grad(0);
951 4272 : dof_is_fixed[current_dof] = true;
952 4272 : current_dof++;
953 4272 : if (dim > 1)
954 : {
955 : // We'll finite difference mixed derivatives
956 3744 : Point nxminus = elem->point(n),
957 3744 : nxplus = elem->point(n);
958 3744 : nxminus(0) -= TOLERANCE;
959 3744 : nxplus(0) += TOLERANCE;
960 : Gradient gxminus =
961 3744 : g_component(g, g_fem, context.get(), var_component,
962 3744 : nxminus, time);
963 : Gradient gxplus =
964 3744 : g_component(g, g_fem, context.get(), var_component,
965 3744 : nxplus, time);
966 : // y derivative
967 3744 : Ue(current_dof) = grad(1);
968 3744 : dof_is_fixed[current_dof] = true;
969 3744 : current_dof++;
970 : // xy derivative
971 4056 : Ue(current_dof) = (gxplus(1) - gxminus(1))
972 3432 : / 2. / TOLERANCE;
973 3744 : dof_is_fixed[current_dof] = true;
974 3744 : current_dof++;
975 :
976 3744 : if (dim > 2)
977 : {
978 : // z derivative
979 0 : Ue(current_dof) = grad(2);
980 0 : dof_is_fixed[current_dof] = true;
981 0 : current_dof++;
982 : // xz derivative
983 0 : Ue(current_dof) = (gxplus(2) - gxminus(2))
984 0 : / 2. / TOLERANCE;
985 0 : dof_is_fixed[current_dof] = true;
986 0 : current_dof++;
987 : // We need new points for yz
988 0 : Point nyminus = elem->point(n),
989 0 : nyplus = elem->point(n);
990 0 : nyminus(1) -= TOLERANCE;
991 0 : nyplus(1) += TOLERANCE;
992 : Gradient gyminus =
993 0 : g_component(g, g_fem, context.get(), var_component,
994 0 : nyminus, time);
995 : Gradient gyplus =
996 0 : g_component(g, g_fem, context.get(), var_component,
997 0 : nyplus, time);
998 : // xz derivative
999 0 : Ue(current_dof) = (gyplus(2) - gyminus(2))
1000 0 : / 2. / TOLERANCE;
1001 0 : dof_is_fixed[current_dof] = true;
1002 0 : current_dof++;
1003 : // Getting a 2nd order xyz is more tedious
1004 0 : Point nxmym = elem->point(n),
1005 0 : nxmyp = elem->point(n),
1006 0 : nxpym = elem->point(n),
1007 0 : nxpyp = elem->point(n);
1008 0 : nxmym(0) -= TOLERANCE;
1009 0 : nxmym(1) -= TOLERANCE;
1010 0 : nxmyp(0) -= TOLERANCE;
1011 0 : nxmyp(1) += TOLERANCE;
1012 0 : nxpym(0) += TOLERANCE;
1013 0 : nxpym(1) -= TOLERANCE;
1014 0 : nxpyp(0) += TOLERANCE;
1015 0 : nxpyp(1) += TOLERANCE;
1016 : Gradient gxmym =
1017 0 : g_component(g, g_fem, context.get(), var_component,
1018 0 : nxmym, time);
1019 : Gradient gxmyp =
1020 0 : g_component(g, g_fem, context.get(), var_component,
1021 0 : nxmyp, time);
1022 : Gradient gxpym =
1023 0 : g_component(g, g_fem, context.get(), var_component,
1024 0 : nxpym, time);
1025 : Gradient gxpyp =
1026 0 : g_component(g, g_fem, context.get(), var_component,
1027 0 : nxpyp, time);
1028 0 : Number gxzplus = (gxpyp(2) - gxmyp(2))
1029 0 : / 2. / TOLERANCE;
1030 0 : Number gxzminus = (gxpym(2) - gxmym(2))
1031 0 : / 2. / TOLERANCE;
1032 : // xyz derivative
1033 0 : Ue(current_dof) = (gxzplus - gxzminus)
1034 0 : / 2. / TOLERANCE;
1035 0 : dof_is_fixed[current_dof] = true;
1036 0 : current_dof++;
1037 : }
1038 : }
1039 : }
1040 : // Assume that other C_ONE elements have a single nodal
1041 : // value shape function and nodal gradient component
1042 : // shape functions
1043 6484 : else if (cont == C_ONE)
1044 : {
1045 544 : libmesh_assert_equal_to (nc, 1 + dim);
1046 7028 : Ue(current_dof) =
1047 6484 : f_component(f, f_fem, context.get(), var_component,
1048 6484 : elem->point(n), time);
1049 6484 : dof_is_fixed[current_dof] = true;
1050 6484 : current_dof++;
1051 : Gradient grad =
1052 6484 : g_component(g, g_fem, context.get(), var_component,
1053 6484 : elem->point(n), time);
1054 19452 : for (unsigned int i=0; i!= dim; ++i)
1055 : {
1056 12968 : Ue(current_dof) = grad(i);
1057 12968 : dof_is_fixed[current_dof] = true;
1058 12968 : current_dof++;
1059 : }
1060 : }
1061 : else
1062 0 : libmesh_error_msg("Unknown continuity cont = " << cont);
1063 : } // end for (n=0..n_nodes)
1064 :
1065 : // In 3D, project any edge values next
1066 16382 : if (dim > 2 && cont != DISCONTINUOUS)
1067 : {
1068 : // Get a pointer to the 1 dimensional (edge) FE for the current
1069 : // var which is stored in the fem_context. This will only be
1070 : // different from side_fe in 3D.
1071 98 : FEGenericBase<OutputType> * edge_fe = nullptr;
1072 98 : fem_context.get_edge_fe(var, edge_fe);
1073 :
1074 : // Set tolerance on underlying FEMap object. This will allow us to
1075 : // avoid spurious negative Jacobian errors while imposing BCs by
1076 : // simply ignoring them. This should only be required in certain
1077 : // special cases, see the DirichletBoundaries comments on this
1078 : // parameter for more information.
1079 1176 : edge_fe->get_fe_map().set_jacobian_tolerance(dirichlet.jacobian_tolerance);
1080 :
1081 : // Pre-request FE data
1082 98 : const std::vector<std::vector<OutputShape>> & phi = edge_fe->get_phi();
1083 1176 : const std::vector<Point> & xyz_values = edge_fe->get_xyz();
1084 294 : const std::vector<Real> & JxW = edge_fe->get_JxW();
1085 :
1086 : // Only pre-request gradients for C1 projections
1087 98 : const std::vector<std::vector<OutputGradient>> * dphi = nullptr;
1088 1176 : if ((cont == C_ONE) && (fe_type.family != SUBDIVISION))
1089 : {
1090 0 : const std::vector<std::vector<OutputGradient>> & ref_dphi = edge_fe->get_dphi();
1091 0 : dphi = &ref_dphi;
1092 : }
1093 :
1094 : // Vector to hold edge local DOF indices
1095 196 : std::vector<unsigned int> edge_dofs;
1096 :
1097 : // Get a reference to the "is_boundary_edge" flags for the
1098 : // current DirichletBoundary object. In case the map does not
1099 : // contain an entry for this DirichletBoundary object, it
1100 : // means there are no boundary edges active.
1101 1176 : if (const auto is_boundary_edge_it = sebi.is_boundary_edge_map.find(&dirichlet);
1102 98 : is_boundary_edge_it != sebi.is_boundary_edge_map.end())
1103 : {
1104 15288 : for (unsigned int e=0; e != sebi.n_edges; ++e)
1105 : {
1106 15288 : if (!is_boundary_edge_it->second[e])
1107 14112 : continue;
1108 :
1109 6480 : FEInterface::dofs_on_edge(elem, dim, fe_type, e,
1110 : edge_dofs);
1111 :
1112 : const unsigned int n_edge_dofs =
1113 1080 : cast_int<unsigned int>(edge_dofs.size());
1114 :
1115 : // Some edge dofs are on nodes and already
1116 : // fixed, others are free to calculate
1117 540 : unsigned int free_dofs = 0;
1118 45360 : for (unsigned int i=0; i != n_edge_dofs; ++i)
1119 45360 : if (!dof_is_fixed[edge_dofs[i]])
1120 0 : free_dof[free_dofs++] = i;
1121 :
1122 : // There may be nothing to project
1123 6480 : if (!free_dofs)
1124 5940 : continue;
1125 :
1126 0 : Ke.resize (free_dofs, free_dofs); Ke.zero();
1127 0 : Fe.resize (free_dofs); Fe.zero();
1128 : // The new edge coefficients
1129 0 : DenseVector<Number> Uedge(free_dofs);
1130 :
1131 : // Initialize FE data on the edge
1132 0 : edge_fe->edge_reinit(elem, e);
1133 0 : const unsigned int n_qp = fem_context.get_edge_qrule().n_points();
1134 :
1135 : // Loop over the quadrature points
1136 0 : for (unsigned int qp=0; qp<n_qp; qp++)
1137 : {
1138 : // solution at the quadrature point
1139 0 : OutputNumber fineval(0);
1140 0 : libMesh::RawAccessor<OutputNumber> f_accessor( fineval, dim );
1141 :
1142 0 : for (unsigned int c = 0; c < n_vec_dim; c++)
1143 0 : f_accessor(c) =
1144 0 : f_component(f, f_fem, context.get(), var_component+c,
1145 0 : xyz_values[qp], time);
1146 :
1147 : // solution grad at the quadrature point
1148 0 : OutputNumberGradient finegrad;
1149 0 : libMesh::RawAccessor<OutputNumberGradient> g_accessor( finegrad, dim );
1150 :
1151 : unsigned int g_rank;
1152 0 : switch( FEInterface::field_type( fe_type ) )
1153 : {
1154 0 : case TYPE_SCALAR:
1155 : {
1156 0 : g_rank = 1;
1157 0 : break;
1158 : }
1159 0 : case TYPE_VECTOR:
1160 : {
1161 0 : g_rank = 2;
1162 0 : break;
1163 : }
1164 0 : default:
1165 0 : libmesh_error_msg("Unknown field type!");
1166 : }
1167 :
1168 0 : if (cont == C_ONE)
1169 0 : for (unsigned int c = 0; c < n_vec_dim; c++)
1170 0 : for (unsigned int d = 0; d < g_rank; d++)
1171 0 : g_accessor(c + d*dim ) =
1172 0 : g_component(g, g_fem, context.get(), var_component,
1173 0 : xyz_values[qp], time)(c);
1174 :
1175 : // Form edge projection matrix
1176 0 : for (unsigned int sidei=0, freei=0; sidei != n_edge_dofs; ++sidei)
1177 : {
1178 0 : unsigned int i = edge_dofs[sidei];
1179 : // fixed DoFs aren't test functions
1180 0 : if (dof_is_fixed[i])
1181 0 : continue;
1182 0 : for (unsigned int sidej=0, freej=0; sidej != n_edge_dofs; ++sidej)
1183 : {
1184 0 : unsigned int j = edge_dofs[sidej];
1185 0 : if (dof_is_fixed[j])
1186 0 : Fe(freei) -= phi[i][qp] * phi[j][qp] *
1187 0 : JxW[qp] * Ue(j);
1188 : else
1189 0 : Ke(freei,freej) += phi[i][qp] *
1190 0 : phi[j][qp] * JxW[qp];
1191 0 : if (cont == C_ONE)
1192 : {
1193 0 : if (dof_is_fixed[j])
1194 0 : Fe(freei) -= ((*dphi)[i][qp].contract((*dphi)[j][qp]) ) *
1195 0 : JxW[qp] * Ue(j);
1196 : else
1197 0 : Ke(freei,freej) += ((*dphi)[i][qp].contract((*dphi)[j][qp]))
1198 0 : * JxW[qp];
1199 : }
1200 0 : if (!dof_is_fixed[j])
1201 0 : freej++;
1202 : }
1203 0 : Fe(freei) += phi[i][qp] * fineval * JxW[qp];
1204 0 : if (cont == C_ONE)
1205 0 : Fe(freei) += (finegrad.contract( (*dphi)[i][qp]) ) *
1206 : JxW[qp];
1207 0 : freei++;
1208 : }
1209 : }
1210 :
1211 0 : Ke.cholesky_solve(Fe, Uedge);
1212 :
1213 : // Transfer new edge solutions to element
1214 0 : for (unsigned int i=0; i != free_dofs; ++i)
1215 : {
1216 0 : Number & ui = Ue(edge_dofs[free_dof[i]]);
1217 0 : libmesh_assert(std::abs(ui) < TOLERANCE ||
1218 : std::abs(ui - Uedge(i)) < TOLERANCE);
1219 0 : ui = Uedge(i);
1220 0 : dof_is_fixed[edge_dofs[free_dof[i]]] = true;
1221 : }
1222 : } // end for (e = 0..n_edges)
1223 : } // end if (is_boundary_edge_it != sebi.is_boundary_edge_map.end())
1224 : } // end if (dim > 2 && cont != DISCONTINUOUS)
1225 :
1226 : // Project any side values (edges in 2D, faces in 3D)
1227 16382 : if (dim > 1 && cont != DISCONTINUOUS)
1228 : {
1229 14223 : FEGenericBase<OutputType> * side_fe = nullptr;
1230 14223 : fem_context.get_side_fe(var, side_fe);
1231 :
1232 : // Set tolerance on underlying FEMap object. This will allow us to
1233 : // avoid spurious negative Jacobian errors while imposing BCs by
1234 : // simply ignoring them. This should only be required in certain
1235 : // special cases, see the DirichletBoundaries comments on this
1236 : // parameter for more information.
1237 15518 : side_fe->get_fe_map().set_jacobian_tolerance(dirichlet.jacobian_tolerance);
1238 :
1239 : // Pre-request FE data
1240 1295 : const std::vector<std::vector<OutputShape>> & phi = side_fe->get_phi();
1241 15518 : const std::vector<Point> & xyz_values = side_fe->get_xyz();
1242 3885 : const std::vector<Real> & JxW = side_fe->get_JxW();
1243 :
1244 : // Only pre-request gradients for C1 projections
1245 1295 : const std::vector<std::vector<OutputGradient>> * dphi = nullptr;
1246 15518 : if ((cont == C_ONE) && (fe_type.family != SUBDIVISION))
1247 : {
1248 415 : const std::vector<std::vector<OutputGradient>> & ref_dphi = side_fe->get_dphi();
1249 415 : dphi = &ref_dphi;
1250 : }
1251 :
1252 : // Vector to hold side local DOF indices
1253 2590 : std::vector<unsigned int> side_dofs;
1254 :
1255 : // Get a reference to the "is_boundary_side" flags for the
1256 : // current DirichletBoundary object. In case the map does not
1257 : // contain an entry for this DirichletBoundary object, it
1258 : // means there are no boundary sides active.
1259 15518 : if (const auto is_boundary_side_it = sebi.is_boundary_side_map.find(&dirichlet);
1260 1295 : is_boundary_side_it != sebi.is_boundary_side_map.end())
1261 : {
1262 74888 : for (unsigned int s=0; s != sebi.n_sides; ++s)
1263 : {
1264 64713 : if (!is_boundary_side_it->second[s])
1265 45652 : continue;
1266 :
1267 17942 : FEInterface::dofs_on_side(elem, dim, fe_type, s,
1268 : side_dofs);
1269 :
1270 : const unsigned int n_side_dofs =
1271 2994 : cast_int<unsigned int>(side_dofs.size());
1272 :
1273 : // Some side dofs are on nodes/edges and already
1274 : // fixed, others are free to calculate
1275 1497 : unsigned int free_dofs = 0;
1276 112672 : for (unsigned int i=0; i != n_side_dofs; ++i)
1277 110544 : if (!dof_is_fixed[side_dofs[i]])
1278 17047 : free_dof[free_dofs++] = i;
1279 :
1280 : // There may be nothing to project
1281 17942 : if (!free_dofs)
1282 3542 : continue;
1283 :
1284 12903 : Ke.resize (free_dofs, free_dofs); Ke.zero();
1285 12903 : Fe.resize (free_dofs); Fe.zero();
1286 : // The new side coefficients
1287 14078 : DenseVector<Number> Uside(free_dofs);
1288 :
1289 : // Initialize FE data on the side
1290 14078 : side_fe->reinit(elem, s);
1291 1175 : const unsigned int n_qp = fem_context.get_side_qrule().n_points();
1292 :
1293 : // Loop over the quadrature points
1294 61150 : for (unsigned int qp=0; qp<n_qp; qp++)
1295 : {
1296 : // solution at the quadrature point
1297 47072 : OutputNumber fineval(0);
1298 3930 : libMesh::RawAccessor<OutputNumber> f_accessor( fineval, dim );
1299 :
1300 94144 : for (unsigned int c = 0; c < n_vec_dim; c++)
1301 47072 : f_accessor(c) =
1302 47072 : f_component(f, f_fem, context.get(), var_component+c,
1303 47072 : xyz_values[qp], time);
1304 :
1305 : // solution grad at the quadrature point
1306 3930 : OutputNumberGradient finegrad;
1307 3930 : libMesh::RawAccessor<OutputNumberGradient> g_accessor( finegrad, dim );
1308 :
1309 : unsigned int g_rank;
1310 47072 : switch( FEInterface::field_type( fe_type ) )
1311 : {
1312 3930 : case TYPE_SCALAR:
1313 : {
1314 3930 : g_rank = 1;
1315 3930 : break;
1316 : }
1317 0 : case TYPE_VECTOR:
1318 : {
1319 0 : g_rank = 2;
1320 0 : break;
1321 : }
1322 0 : default:
1323 0 : libmesh_error_msg("Unknown field type!");
1324 : }
1325 :
1326 47072 : if (cont == C_ONE)
1327 25456 : for (unsigned int c = 0; c < n_vec_dim; c++)
1328 25456 : for (unsigned int d = 0; d < g_rank; d++)
1329 13796 : g_accessor(c + d*dim ) =
1330 13796 : g_component(g, g_fem, context.get(), var_component,
1331 13796 : xyz_values[qp], time)(c);
1332 :
1333 : // Form side projection matrix
1334 247024 : for (unsigned int sidei=0, freei=0; sidei != n_side_dofs; ++sidei)
1335 : {
1336 199952 : unsigned int i = side_dofs[sidei];
1337 : // fixed DoFs aren't test functions
1338 216666 : if (dof_is_fixed[i])
1339 132924 : continue;
1340 293008 : for (unsigned int sidej=0, freej=0; sidej != n_side_dofs; ++sidej)
1341 : {
1342 238112 : unsigned int j = side_dofs[sidej];
1343 258006 : if (dof_is_fixed[j])
1344 254756 : Fe(freei) -= phi[i][qp] * phi[j][qp] *
1345 147268 : JxW[qp] * Ue(j);
1346 : else
1347 103240 : Ke(freei,freej) += phi[i][qp] *
1348 90324 : phi[j][qp] * JxW[qp];
1349 238112 : if (cont == C_ONE)
1350 : {
1351 96572 : if (dof_is_fixed[j])
1352 114816 : Fe(freei) -= ((*dphi)[i][qp].contract((*dphi)[j][qp])) *
1353 69960 : JxW[qp] * Ue(j);
1354 : else
1355 18068 : Ke(freei,freej) += ((*dphi)[i][qp].contract((*dphi)[j][qp]))
1356 14864 : * JxW[qp];
1357 : }
1358 258006 : if (!dof_is_fixed[j])
1359 77408 : freej++;
1360 : }
1361 68642 : Fe(freei) += (fineval * phi[i][qp]) * JxW[qp];
1362 54896 : if (cont == C_ONE)
1363 14864 : Fe(freei) += (finegrad.contract((*dphi)[i][qp])) *
1364 : JxW[qp];
1365 54896 : freei++;
1366 : }
1367 : }
1368 :
1369 14078 : Ke.cholesky_solve(Fe, Uside);
1370 :
1371 : // Transfer new side solutions to element
1372 29812 : for (unsigned int i=0; i != free_dofs; ++i)
1373 : {
1374 18360 : Number & ui = Ue(side_dofs[free_dof[i]]);
1375 :
1376 1313 : libmesh_assert(std::abs(ui) < TOLERANCE ||
1377 : std::abs(ui - Uside(i)) < TOLERANCE);
1378 15734 : ui = Uside(i);
1379 :
1380 17047 : dof_is_fixed[side_dofs[free_dof[i]]] = true;
1381 : }
1382 : } // end for (s = 0..n_sides)
1383 : } // end if (is_boundary_side_it != sebi.is_boundary_side_map.end())
1384 : } // end if (dim > 1 && cont != DISCONTINUOUS)
1385 :
1386 : // Project any shellface values
1387 16382 : if (dim == 2 && cont != DISCONTINUOUS)
1388 : {
1389 1197 : FEGenericBase<OutputType> * fe = nullptr;
1390 1197 : fem_context.get_element_fe(var, fe, dim);
1391 :
1392 : // Set tolerance on underlying FEMap object. This will allow us to
1393 : // avoid spurious negative Jacobian errors while imposing BCs by
1394 : // simply ignoring them. This should only be required in certain
1395 : // special cases, see the DirichletBoundaries comments on this
1396 : // parameter for more information.
1397 14342 : fe->get_fe_map().set_jacobian_tolerance(dirichlet.jacobian_tolerance);
1398 :
1399 : // Pre-request FE data
1400 1197 : const std::vector<std::vector<OutputShape>> & phi = fe->get_phi();
1401 14342 : const std::vector<Point> & xyz_values = fe->get_xyz();
1402 3591 : const std::vector<Real> & JxW = fe->get_JxW();
1403 :
1404 : // Only pre-request gradients for C1 projections
1405 1197 : const std::vector<std::vector<OutputGradient>> * dphi = nullptr;
1406 14342 : if ((cont == C_ONE) && (fe_type.family != SUBDIVISION))
1407 : {
1408 415 : const std::vector<std::vector<OutputGradient>> & ref_dphi = fe->get_dphi();
1409 415 : dphi = &ref_dphi;
1410 : }
1411 :
1412 : // Get a reference to the "is_boundary_shellface" flags for the
1413 : // current DirichletBoundary object. In case the map does not
1414 : // contain an entry for this DirichletBoundary object, it
1415 : // means there are no boundary shellfaces active.
1416 14342 : if (const auto is_boundary_shellface_it = sebi.is_boundary_shellface_map.find(&dirichlet);
1417 1197 : is_boundary_shellface_it != sebi.is_boundary_shellface_map.end())
1418 : {
1419 0 : for (unsigned int shellface=0; shellface != 2; ++shellface)
1420 : {
1421 0 : if (!is_boundary_shellface_it->second[shellface])
1422 0 : continue;
1423 :
1424 : // A shellface has the same dof indices as the element itself
1425 0 : std::vector<unsigned int> shellface_dofs(n_dofs);
1426 0 : std::iota(shellface_dofs.begin(), shellface_dofs.end(), 0);
1427 :
1428 : // Some shellface dofs are on nodes/edges and already
1429 : // fixed, others are free to calculate
1430 0 : unsigned int free_dofs = 0;
1431 0 : for (unsigned int i=0; i != n_dofs; ++i)
1432 0 : if (!dof_is_fixed[shellface_dofs[i]])
1433 0 : free_dof[free_dofs++] = i;
1434 :
1435 : // There may be nothing to project
1436 0 : if (!free_dofs)
1437 0 : continue;
1438 :
1439 0 : Ke.resize (free_dofs, free_dofs); Ke.zero();
1440 0 : Fe.resize (free_dofs); Fe.zero();
1441 : // The new shellface coefficients
1442 0 : DenseVector<Number> Ushellface(free_dofs);
1443 :
1444 : // Initialize FE data on the element
1445 0 : fe->reinit (elem);
1446 0 : const unsigned int n_qp = fem_context.get_element_qrule().n_points();
1447 :
1448 : // Loop over the quadrature points
1449 0 : for (unsigned int qp=0; qp<n_qp; qp++)
1450 : {
1451 : // solution at the quadrature point
1452 0 : OutputNumber fineval(0);
1453 0 : libMesh::RawAccessor<OutputNumber> f_accessor( fineval, dim );
1454 :
1455 0 : for (unsigned int c = 0; c < n_vec_dim; c++)
1456 0 : f_accessor(c) =
1457 0 : f_component(f, f_fem, context.get(), var_component+c,
1458 0 : xyz_values[qp], time);
1459 :
1460 : // solution grad at the quadrature point
1461 0 : OutputNumberGradient finegrad;
1462 0 : libMesh::RawAccessor<OutputNumberGradient> g_accessor( finegrad, dim );
1463 :
1464 : unsigned int g_rank;
1465 0 : switch( FEInterface::field_type( fe_type ) )
1466 : {
1467 0 : case TYPE_SCALAR:
1468 : {
1469 0 : g_rank = 1;
1470 0 : break;
1471 : }
1472 0 : case TYPE_VECTOR:
1473 : {
1474 0 : g_rank = 2;
1475 0 : break;
1476 : }
1477 0 : default:
1478 0 : libmesh_error_msg("Unknown field type!");
1479 : }
1480 :
1481 0 : if (cont == C_ONE)
1482 0 : for (unsigned int c = 0; c < n_vec_dim; c++)
1483 0 : for (unsigned int d = 0; d < g_rank; d++)
1484 0 : g_accessor(c + d*dim ) =
1485 0 : g_component(g, g_fem, context.get(), var_component,
1486 0 : xyz_values[qp], time)(c);
1487 :
1488 : // Form shellface projection matrix
1489 0 : for (unsigned int shellfacei=0, freei=0;
1490 0 : shellfacei != n_dofs; ++shellfacei)
1491 : {
1492 0 : unsigned int i = shellface_dofs[shellfacei];
1493 : // fixed DoFs aren't test functions
1494 0 : if (dof_is_fixed[i])
1495 0 : continue;
1496 0 : for (unsigned int shellfacej=0, freej=0;
1497 0 : shellfacej != n_dofs; ++shellfacej)
1498 : {
1499 0 : unsigned int j = shellface_dofs[shellfacej];
1500 0 : if (dof_is_fixed[j])
1501 0 : Fe(freei) -= phi[i][qp] * phi[j][qp] *
1502 0 : JxW[qp] * Ue(j);
1503 : else
1504 0 : Ke(freei,freej) += phi[i][qp] *
1505 0 : phi[j][qp] * JxW[qp];
1506 0 : if (cont == C_ONE)
1507 : {
1508 0 : if (dof_is_fixed[j])
1509 0 : Fe(freei) -= ((*dphi)[i][qp].contract((*dphi)[j][qp])) *
1510 0 : JxW[qp] * Ue(j);
1511 : else
1512 0 : Ke(freei,freej) += ((*dphi)[i][qp].contract((*dphi)[j][qp]))
1513 0 : * JxW[qp];
1514 : }
1515 0 : if (!dof_is_fixed[j])
1516 0 : freej++;
1517 : }
1518 0 : Fe(freei) += (fineval * phi[i][qp]) * JxW[qp];
1519 0 : if (cont == C_ONE)
1520 0 : Fe(freei) += (finegrad.contract((*dphi)[i][qp])) *
1521 : JxW[qp];
1522 0 : freei++;
1523 : }
1524 : }
1525 :
1526 0 : Ke.cholesky_solve(Fe, Ushellface);
1527 :
1528 : // Transfer new shellface solutions to element
1529 0 : for (unsigned int i=0; i != free_dofs; ++i)
1530 : {
1531 0 : Number & ui = Ue(shellface_dofs[free_dof[i]]);
1532 0 : libmesh_assert(std::abs(ui) < TOLERANCE ||
1533 : std::abs(ui - Ushellface(i)) < TOLERANCE);
1534 0 : ui = Ushellface(i);
1535 0 : dof_is_fixed[shellface_dofs[free_dof[i]]] = true;
1536 : }
1537 : } // end for (shellface = 0..2)
1538 : } // end if (is_boundary_shellface_it != sebi.is_boundary_shellface_map.end())
1539 : } // end if (dim == 2 && cont != DISCONTINUOUS)
1540 :
1541 : // Lock the DofConstraints since it is shared among threads.
1542 : {
1543 2734 : Threads::spin_mutex::scoped_lock lock(Threads::spin_mtx);
1544 :
1545 206034 : for (unsigned int i = 0; i < n_dofs; i++)
1546 : {
1547 31652 : DofConstraintRow empty_row;
1548 205478 : if (dof_is_fixed[i] && !libmesh_isnan(Ue(i)))
1549 96865 : add_fn (dof_indices[i], empty_row, Ue(i));
1550 : }
1551 : }
1552 27296 : } // apply_dirichlet_impl
1553 :
1554 : public:
1555 614 : ConstrainDirichlet (DofMap & dof_map_in,
1556 : const MeshBase & mesh_in,
1557 : const Real time_in,
1558 : const DirichletBoundaries & dirichlets_in,
1559 : const AddConstraint & add_in,
1560 21733 : const bool record_elems_in) :
1561 20505 : dof_map(dof_map_in),
1562 20505 : mesh(mesh_in),
1563 20505 : time(time_in),
1564 20505 : dirichlets(dirichlets_in),
1565 20505 : add_fn(add_in),
1566 19610 : record_elems(record_elems_in) { }
1567 :
1568 : // Splitting constructor for Threads::parallel_reduce(). Each thread
1569 : // records its elements in its own list, so no lock is necessary.
1570 44 : ConstrainDirichlet (ConstrainDirichlet & other, Threads::split) :
1571 44 : dof_map(other.dof_map),
1572 44 : mesh(other.mesh),
1573 44 : time(other.time),
1574 44 : dirichlets(other.dirichlets),
1575 44 : add_fn(other.add_fn),
1576 44 : record_elems(other.record_elems) { }
1577 :
1578 : // This class can be default copy/move constructed.
1579 : ConstrainDirichlet (ConstrainDirichlet &&) = default;
1580 : ConstrainDirichlet (const ConstrainDirichlet &) = default;
1581 :
1582 : // This class cannot be default copy/move assigned because it
1583 : // contains reference members.
1584 : ConstrainDirichlet & operator= (const ConstrainDirichlet &) = delete;
1585 : ConstrainDirichlet & operator= (ConstrainDirichlet &&) = delete;
1586 :
1587 66 : void join (ConstrainDirichlet & other)
1588 : {
1589 44 : acted_on_elems.insert(acted_on_elems.end(),
1590 : other.acted_on_elems.begin(),
1591 88 : other.acted_on_elems.end());
1592 66 : }
1593 :
1594 614 : std::vector<const Elem *> & get_acted_on_elems() { return acted_on_elems; }
1595 :
1596 21799 : void operator()(const ConstElemRange & range)
1597 : {
1598 : /**
1599 : * This method examines an arbitrary boundary solution to calculate
1600 : * corresponding Dirichlet constraints on the current mesh. The
1601 : * input function \p f gives the arbitrary solution.
1602 : */
1603 :
1604 : // Figure out which System the DirichletBoundary objects are
1605 : // defined for. We break out of the loop as soon as we encounter a
1606 : // valid System pointer, the assumption is thus that all Variables
1607 : // are defined on the same System.
1608 636 : System * system = nullptr;
1609 :
1610 : // Map from boundary_id -> set<pair<id,DirichletBoundary*>> objects which
1611 : // are active on that boundary_id. Later we will use this to determine
1612 : // which DirichletBoundary objects to loop over for each Elem.
1613 : std::map<boundary_id_type, std::set<std::pair<unsigned int, DirichletBoundary *>>>
1614 1272 : boundary_id_to_ordered_dirichlet_boundaries;
1615 :
1616 52134 : for (auto dirichlet_id : index_range(dirichlets))
1617 : {
1618 : // Pointer to the current DirichletBoundary object
1619 30335 : const auto & dirichlet = dirichlets[dirichlet_id];
1620 :
1621 : // Construct mapping from boundary_id -> (dirichlet_id, DirichletBoundary)
1622 91109 : for (const auto & b_id : dirichlet->b)
1623 60774 : boundary_id_to_ordered_dirichlet_boundaries[b_id].emplace(dirichlet_id, dirichlet.get());
1624 :
1625 70581 : for (const auto & var : dirichlet->variables)
1626 : {
1627 40246 : const Variable & variable = dof_map.variable(var);
1628 2336 : auto current_system = variable.system();
1629 :
1630 40246 : if (!system)
1631 636 : system = current_system;
1632 : else
1633 18447 : libmesh_error_msg_if(current_system != system,
1634 : "All variables should be defined on the same System");
1635 : }
1636 : }
1637 :
1638 : // If we found no System, it could be because none of the
1639 : // Variables have one defined, or because there are
1640 : // DirichletBoundary objects with no Variables defined on
1641 : // them. These situations both indicate a likely error in the
1642 : // setup of a problem, so let's throw an error in this case.
1643 21799 : libmesh_error_msg_if(!system, "Valid System not found for any Variables.");
1644 :
1645 : // Construct a FEMContext object for the System on which the
1646 : // Variables in our DirichletBoundary objects are defined. This
1647 : // will be used in the apply_dirichlet_impl() function.
1648 : // We're not going to use elem_jacobian or subjacobians here so
1649 : // don't allocate them.
1650 : auto fem_context = std::make_unique<FEMContext>
1651 22435 : (*system, nullptr, /* allocate local_matrices = */ false);
1652 :
1653 : // At the time we are using this FEMContext, the current_local_solution
1654 : // vector is not initialized, but also we don't need it, so set
1655 : // the algebraic_type flag to DOFS_ONLY.
1656 636 : fem_context->set_algebraic_type(FEMContext::DOFS_ONLY);
1657 :
1658 : // Boundary info for the current mesh
1659 21799 : const BoundaryInfo & boundary_info = mesh.get_boundary_info();
1660 :
1661 : // This object keeps track of the BoundaryInfo for a single Elem
1662 22435 : SingleElemBoundaryInfo sebi(boundary_info, boundary_id_to_ordered_dirichlet_boundaries);
1663 :
1664 : // Iterate over all the elements in the range
1665 1573598 : for (const auto & elem : range)
1666 : {
1667 : // We only calculate Dirichlet constraints on active
1668 : // elements
1669 1551799 : if (!elem->active())
1670 344808 : continue;
1671 :
1672 : // Reinitialize BoundaryInfo data structures for the current elem
1673 1174808 : bool has_dirichlet_constraint = sebi.reinit(elem);
1674 :
1675 : // If this Elem has no boundary ids, go to the next one.
1676 1174808 : if (!has_dirichlet_constraint)
1677 945301 : continue;
1678 :
1679 140719 : if (record_elems)
1680 139819 : acted_on_elems.push_back(elem);
1681 :
1682 290772 : for (const auto & db_pair : sebi.ordered_dbs)
1683 : {
1684 : // Get pointer to the DirichletBoundary object
1685 12804 : const auto & dirichlet = db_pair.second;
1686 :
1687 : // Loop over all the variables which this DirichletBoundary object is responsible for
1688 333927 : for (const auto & var : dirichlet->variables)
1689 : {
1690 183874 : const Variable & variable = dof_map.variable(var);
1691 :
1692 : // Make sure that the Variable and the DofMap agree on
1693 : // what number this variable is.
1694 15736 : libmesh_assert_equal_to(variable.number(), var);
1695 :
1696 15736 : const FEType & fe_type = variable.type();
1697 :
1698 183874 : if (fe_type.family == SCALAR)
1699 0 : continue;
1700 :
1701 183874 : switch( FEInterface::field_type( fe_type ) )
1702 : {
1703 182698 : case TYPE_SCALAR:
1704 : {
1705 : // For Lagrange FEs we don't need to do a full
1706 : // blown projection, we can just interpolate
1707 : // values directly.
1708 182698 : if (fe_type.family == LAGRANGE)
1709 167492 : this->apply_lagrange_dirichlet_impl<Real>( sebi, variable, *dirichlet, *fem_context );
1710 : else
1711 15206 : this->apply_dirichlet_impl<Real>( sebi, variable, *dirichlet, *fem_context );
1712 15638 : break;
1713 : }
1714 98 : case TYPE_VECTOR:
1715 : {
1716 1176 : this->apply_dirichlet_impl<RealGradient>( sebi, variable, *dirichlet, *fem_context );
1717 98 : break;
1718 : }
1719 0 : default:
1720 0 : libmesh_error_msg("Unknown field type!");
1721 : }
1722 : } // for (var : variables)
1723 : } // for (db_pair : ordered_dbs)
1724 : } // for (elem : range)
1725 42326 : } // operator()
1726 :
1727 : }; // class ConstrainDirichlet
1728 :
1729 :
1730 : #endif // LIBMESH_ENABLE_DIRICHLET
1731 :
1732 :
1733 : } // anonymous namespace
1734 :
1735 :
1736 :
1737 : namespace libMesh
1738 : {
1739 :
1740 : // ------------------------------------------------------------
1741 : // DofMap member functions
1742 :
1743 : #ifdef LIBMESH_ENABLE_CONSTRAINTS
1744 :
1745 :
1746 1523287 : dof_id_type DofMap::n_constrained_dofs() const
1747 : {
1748 37430 : parallel_object_only();
1749 :
1750 1523287 : dof_id_type nc_dofs = this->n_local_constrained_dofs();
1751 1523287 : this->comm().sum(nc_dofs);
1752 1523287 : return nc_dofs;
1753 : }
1754 :
1755 :
1756 1543805 : dof_id_type DofMap::n_local_constrained_dofs() const
1757 : {
1758 : const DofConstraints::const_iterator lower =
1759 38010 : _dof_constraints.lower_bound(this->first_dof()),
1760 : upper =
1761 38010 : _dof_constraints.lower_bound(this->end_dof());
1762 :
1763 1581815 : return cast_int<dof_id_type>(std::distance(lower, upper));
1764 : }
1765 :
1766 :
1767 :
1768 406328 : void DofMap::create_dof_constraints(const MeshBase & mesh, Real time)
1769 : {
1770 11612 : parallel_object_only();
1771 :
1772 21496 : LOG_SCOPE("create_dof_constraints()", "DofMap");
1773 :
1774 11612 : libmesh_assert (mesh.is_prepared());
1775 :
1776 : // The user might have set boundary conditions after the mesh was
1777 : // prepared; we should double-check that those boundary conditions
1778 : // are still consistent.
1779 : #ifdef DEBUG
1780 11612 : MeshTools::libmesh_assert_valid_boundary_ids(mesh);
1781 : #endif
1782 :
1783 : // We might get constraint equations from AMR hanging nodes in
1784 : // 2D/3D, or from spline constraint rows or boundary conditions in
1785 : // any dimension
1786 : const bool possible_local_constraints = false
1787 406328 : || !mesh.n_elem()
1788 406328 : || mesh.n_constraint_rows()
1789 : #ifdef LIBMESH_ENABLE_AMR
1790 404824 : || mesh.mesh_dimension() > 1
1791 : #endif
1792 : #ifdef LIBMESH_ENABLE_PERIODIC
1793 66296 : || !_periodic_boundaries->empty()
1794 : #endif
1795 : #ifdef LIBMESH_ENABLE_DIRICHLET
1796 482096 : || !_dirichlet_boundaries->empty()
1797 : #endif
1798 : ;
1799 :
1800 : // Even if we don't have constraints, another processor might.
1801 406328 : bool possible_global_constraints = possible_local_constraints;
1802 : #if defined(LIBMESH_ENABLE_PERIODIC) || defined(LIBMESH_ENABLE_DIRICHLET) || defined(LIBMESH_ENABLE_AMR)
1803 11612 : libmesh_assert(this->comm().verify(mesh.is_serial()));
1804 :
1805 406328 : this->comm().max(possible_global_constraints);
1806 : #endif
1807 :
1808 : // Recalculate dof constraints from scratch. (Or just clear them,
1809 : // if the user has just deleted their last dirichlet/periodic/user
1810 : // constraint)
1811 : // Note: any _stashed_dof_constraints are not cleared as it
1812 : // may be the user's intention to restore them later.
1813 : #ifdef LIBMESH_ENABLE_CONSTRAINTS
1814 11612 : _dof_constraints.clear();
1815 11612 : _primal_constraint_values.clear();
1816 11612 : _adjoint_constraint_values.clear();
1817 : #endif
1818 : #ifdef LIBMESH_ENABLE_NODE_CONSTRAINTS
1819 11612 : _node_constraints.clear();
1820 : #endif
1821 : #ifdef LIBMESH_ENABLE_DIRICHLET
1822 417940 : _dirichlet_cache = DirichletConstraintCache();
1823 : #endif
1824 :
1825 406328 : if (!possible_global_constraints)
1826 61331 : return;
1827 :
1828 : // Here we build the hanging node constraints. This is done
1829 : // by enforcing the condition u_a = u_b along hanging sides.
1830 : // u_a = u_b is collocated at the nodes of side a, which gives
1831 : // one row of the constraint matrix.
1832 :
1833 : // Processors only compute their local constraints
1834 689994 : ConstElemRange range (mesh.local_elements_begin(),
1835 1034991 : mesh.local_elements_end());
1836 :
1837 : // Global computation fails if we're using a FEMFunctionBase BC on a
1838 : // ReplicatedMesh in parallel
1839 : // ConstElemRange range (mesh.elements_begin(),
1840 : // mesh.elements_end());
1841 :
1842 : // compute_periodic_constraints requires a point_locator() from our
1843 : // Mesh, but point_locator() construction is parallel and threaded.
1844 : // Rather than nest threads within threads we'll make sure it's
1845 : // preconstructed.
1846 : #ifdef LIBMESH_ENABLE_PERIODIC
1847 669361 : bool need_point_locator = !_periodic_boundaries->empty() && !range.empty();
1848 :
1849 344997 : this->comm().max(need_point_locator);
1850 :
1851 344997 : if (need_point_locator)
1852 1314 : mesh.sub_point_locator();
1853 : #endif
1854 :
1855 : #ifdef LIBMESH_ENABLE_NODE_CONSTRAINTS
1856 29652 : Threads::parallel_for (range,
1857 19768 : ComputeNodeConstraints (_node_constraints,
1858 : #ifdef LIBMESH_ENABLE_PERIODIC
1859 9884 : *_periodic_boundaries,
1860 : #endif
1861 : mesh));
1862 : #endif // LIBMESH_ENABLE_NODE_CONSTRAINTS
1863 :
1864 :
1865 : // Look at all the variables in the system. Reset the element
1866 : // range at each iteration -- there is no need to reconstruct it.
1867 344997 : const auto n_vars = this->n_variables();
1868 725926 : for (unsigned int variable_number=0; variable_number<n_vars;
1869 370075 : ++variable_number, range.reset())
1870 391783 : Threads::parallel_for (range,
1871 391783 : ComputeConstraints (_dof_constraints,
1872 : *this,
1873 : #ifdef LIBMESH_ENABLE_PERIODIC
1874 10854 : *_periodic_boundaries,
1875 : #endif
1876 : mesh,
1877 : variable_number));
1878 :
1879 : #ifdef LIBMESH_ENABLE_DIRICHLET
1880 :
1881 344997 : if (!_dirichlet_boundaries->empty())
1882 : {
1883 : // Sanity check that the boundary ids associated with the
1884 : // DirichletBoundary objects are actually present in the
1885 : // mesh. We do this check by default, but in cases where you
1886 : // intentionally add "inconsistent but valid" DirichletBoundary
1887 : // objects in parallel, this check can deadlock since it does a
1888 : // collective communication internally. In that case it is
1889 : // possible to disable this check by setting the flag to false.
1890 18909 : if (_verify_dirichlet_bc_consistency)
1891 44796 : for (const auto & dirichlet : *_dirichlet_boundaries)
1892 25887 : this->check_dirichlet_bcid_consistency(mesh, *dirichlet);
1893 :
1894 : // The constraints that this method built so far do not depend on
1895 : // time. Keep a copy of them for reinit_dirichlet_constraint_values(),
1896 : // with the elements that the Dirichlet loops below act on.
1897 534 : _dirichlet_cache.dof_constraints = _dof_constraints;
1898 534 : _dirichlet_cache.primal_constraint_values = _primal_constraint_values;
1899 534 : _dirichlet_cache.adjoint_constraint_values = _adjoint_constraint_values;
1900 : #ifdef LIBMESH_ENABLE_NODE_CONSTRAINTS
1901 534 : _dirichlet_cache.node_constraints = _node_constraints;
1902 : #endif
1903 : _dirichlet_cache.boundary_elems =
1904 18909 : this->apply_dirichlet_boundaries(mesh, time, range, /*record_elems=*/true);
1905 18909 : _dirichlet_cache.valid = true;
1906 : }
1907 :
1908 : #endif // LIBMESH_ENABLE_DIRICHLET
1909 :
1910 : // Handle spline node constraints last, so we can try to move
1911 : // existing constraints onto the spline basis if necessary.
1912 344997 : this->process_mesh_constraint_rows(mesh);
1913 : }
1914 :
1915 :
1916 :
1917 : #ifdef LIBMESH_ENABLE_DIRICHLET
1918 : std::vector<const Elem *>
1919 19548 : DofMap::apply_dirichlet_boundaries(const MeshBase & mesh,
1920 : const Real time,
1921 : ConstElemRange & range,
1922 : const bool record_elems)
1923 : {
1924 : // Threaded loop over local elems applying all Dirichlet BCs
1925 552 : const AddPrimalConstraint add_primal(*this);
1926 552 : ConstrainDirichlet primal(*this, mesh, time, *_dirichlet_boundaries,
1927 1104 : add_primal, record_elems);
1928 19548 : Threads::parallel_reduce(range, primal);
1929 552 : std::vector<const Elem *> acted_on_elems = std::move(primal.get_acted_on_elems());
1930 :
1931 : // Threaded loop over local elems per QOI applying all adjoint
1932 : // Dirichlet BCs. Note that the ConstElemRange is reset before each
1933 : // execution of Threads::parallel_reduce().
1934 :
1935 21733 : for (auto qoi_index : index_range(_adjoint_dirichlet_boundaries))
1936 : {
1937 : const DirichletBoundaries & adb_q =
1938 124 : *(_adjoint_dirichlet_boundaries[qoi_index]);
1939 :
1940 2185 : if (!adb_q.empty())
1941 : {
1942 2185 : const AddAdjointConstraint add_adjoint(*this, qoi_index);
1943 : ConstrainDirichlet adjoint(*this, mesh, time, adb_q,
1944 62 : add_adjoint, record_elems);
1945 2185 : Threads::parallel_reduce(range.reset(), adjoint);
1946 62 : const auto & adjoint_elems = adjoint.get_acted_on_elems();
1947 2123 : acted_on_elems.insert(acted_on_elems.end(),
1948 248 : adjoint_elems.begin(), adjoint_elems.end());
1949 : }
1950 : }
1951 :
1952 : // The list joins the primal and adjoint lists. Thus an element on
1953 : // primal and adjoint boundaries appears more than once, and the list
1954 : // is not in range order. Each element applies its boundaries in the
1955 : // same order at any position in the range. The first DofMap
1956 : // constraint on a degree of freedom stays, and later constraints do
1957 : // not overwrite it. Thus only the relative order of elements
1958 : // matters, and only where two elements share a degree of freedom.
1959 : // Sorting by id gives the order in which the local element range
1960 : // visits the elements.
1961 19548 : std::sort(acted_on_elems.begin(), acted_on_elems.end(),
1962 73236 : [](const Elem * a, const Elem * b) { return a->id() < b->id(); });
1963 19548 : acted_on_elems.erase(std::unique(acted_on_elems.begin(), acted_on_elems.end()),
1964 1104 : acted_on_elems.end());
1965 20100 : return acted_on_elems;
1966 : }
1967 : #endif // LIBMESH_ENABLE_DIRICHLET
1968 :
1969 :
1970 :
1971 639 : void DofMap::reinit_dirichlet_constraint_values(const MeshBase & mesh, const Real time)
1972 : {
1973 18 : parallel_object_only();
1974 :
1975 18 : LOG_SCOPE("reinit_dirichlet_constraint_values()", "DofMap");
1976 :
1977 : #ifdef LIBMESH_ENABLE_DIRICHLET
1978 : // Each branch below is collective. Thus every processor must take
1979 : // the same branch.
1980 639 : bool reuse = _dirichlet_cache.valid;
1981 639 : this->comm().min(reuse);
1982 :
1983 639 : if (reuse)
1984 : {
1985 18 : _dof_constraints = _dirichlet_cache.dof_constraints;
1986 18 : _primal_constraint_values = _dirichlet_cache.primal_constraint_values;
1987 18 : _adjoint_constraint_values = _dirichlet_cache.adjoint_constraint_values;
1988 : #ifdef LIBMESH_ENABLE_NODE_CONSTRAINTS
1989 18 : _node_constraints = _dirichlet_cache.node_constraints;
1990 : #endif
1991 :
1992 639 : ConstElemRange range(&_dirichlet_cache.boundary_elems);
1993 639 : this->apply_dirichlet_boundaries(mesh, time, range, /*record_elems=*/false);
1994 639 : this->process_mesh_constraint_rows(mesh);
1995 18 : return;
1996 : }
1997 : #endif // LIBMESH_ENABLE_DIRICHLET
1998 :
1999 0 : this->create_dof_constraints(mesh, time);
2000 : }
2001 :
2002 :
2003 :
2004 345636 : void DofMap::process_mesh_constraint_rows(const MeshBase & mesh)
2005 : {
2006 : // If we already have simple Dirichlet constraints (with right hand
2007 : // sides but with no coupling between DoFs) on spline-constrained FE
2008 : // nodes, then we'll need a solve to compute the corresponding
2009 : // constraints on the relevant spline nodes. (If we already have
2010 : // constraints with coupling between DoFs on spline-constrained FE
2011 : // nodes, then we'll need to go sit down and cry until we figure out
2012 : // how to handle that.)
2013 :
2014 9902 : const auto & constraint_rows = mesh.get_constraint_rows();
2015 :
2016 : // This routine is too expensive to use unless we really might
2017 : // need it
2018 : #ifdef DEBUG
2019 9902 : bool constraint_rows_empty = constraint_rows.empty();
2020 9902 : this->comm().min(constraint_rows_empty);
2021 9902 : libmesh_assert_equal_to(constraint_rows_empty, !mesh.n_constraint_rows());
2022 : #endif
2023 :
2024 345636 : if (!mesh.n_constraint_rows())
2025 343919 : return;
2026 :
2027 : // We can't handle periodic boundary conditions on spline meshes
2028 : // yet.
2029 : #ifdef LIBMESH_ENABLE_PERIODIC
2030 1717 : libmesh_error_msg_if (!_periodic_boundaries->empty(),
2031 : "Periodic boundary conditions are not yet implemented for spline meshes");
2032 : #endif
2033 :
2034 : // We can handle existing Dirichlet constraints, but we'll need
2035 : // to do solves to project them down onto the spline basis.
2036 1669 : std::unique_ptr<SparsityPattern::Build> sp;
2037 1669 : std::unique_ptr<SparseMatrix<Number>> mat;
2038 :
2039 : const unsigned int n_adjoint_rhs =
2040 1717 : _adjoint_constraint_values.size();
2041 :
2042 : // [0] for primal rhs, [q+1] for adjoint qoi q
2043 : std::vector<std::unique_ptr<NumericVector<Number>>>
2044 1813 : solve_rhs(n_adjoint_rhs+1);
2045 :
2046 : // Keep track of which spline DoFs will be Dirichlet.
2047 : // We use a set here to make it easier to find what processors
2048 : // to send the DoFs to later.
2049 96 : std::set<dof_id_type> my_dirichlet_spline_dofs;
2050 :
2051 : // And keep track of which non-spline Dofs were Dirichlet
2052 96 : std::unordered_set<dof_id_type> was_previously_constrained;
2053 :
2054 96 : const unsigned int sys_num = this->sys_number();
2055 194460 : for (auto & node_row : constraint_rows)
2056 : {
2057 192743 : const Node * node = node_row.first;
2058 16612 : libmesh_assert(node == mesh.node_ptr(node->id()));
2059 :
2060 : // Each processor only computes its own (and in distributed
2061 : // cases, is only guaranteed to have the dependency data to
2062 : // compute its own) constraints here.
2063 209355 : if (node->processor_id() != mesh.processor_id())
2064 84469 : continue;
2065 :
2066 330616 : for (auto var_num : IntRange<unsigned int>(0, this->n_variables()))
2067 : {
2068 19196 : const FEFamily & fe_family = this->variable_type(var_num).family;
2069 :
2070 : // constraint_rows only applies to nodal variables
2071 230648 : if (fe_family != LAGRANGE &&
2072 18788 : fe_family != RATIONAL_BERNSTEIN)
2073 0 : continue;
2074 :
2075 38392 : DofConstraintRow dc_row;
2076 :
2077 : const dof_id_type constrained_id =
2078 230648 : node->dof_number(sys_num, var_num, 0);
2079 730527 : for (const auto & [pr, val] : node_row.second)
2080 : {
2081 499879 : const Elem * spline_elem = pr.first;
2082 41707 : libmesh_assert(spline_elem == mesh.elem_ptr(spline_elem->id()));
2083 :
2084 : const Node & spline_node =
2085 499879 : spline_elem->node_ref(pr.second);
2086 :
2087 : const dof_id_type spline_dof_id =
2088 499879 : spline_node.dof_number(sys_num, var_num, 0);
2089 499879 : dc_row[spline_dof_id] = val;
2090 : }
2091 :
2092 : // See if we already have a constraint here.
2093 230648 : if (this->is_constrained_dof(constrained_id))
2094 : {
2095 403 : was_previously_constrained.insert(constrained_id);
2096 :
2097 : // Keep track of which spline DoFs will be
2098 : // inheriting this non-spline DoF's constraints
2099 11916 : for (auto & row_entry : dc_row)
2100 7080 : my_dirichlet_spline_dofs.insert(row_entry.first);
2101 :
2102 : // If it wasn't a simple Dirichlet-type constraint
2103 : // then I don't know what to do with it. We'll make
2104 : // this an assertion only because this should only
2105 : // crop up with periodic boundary conditions, which
2106 : // we've already made sure we don't have.
2107 403 : libmesh_assert(_dof_constraints[constrained_id].empty());
2108 : }
2109 :
2110 : // Add the constraint, replacing any previous, so we can
2111 : // use the new constraint in setting up the solve below
2112 38392 : this->add_constraint_row(constrained_id, dc_row, false);
2113 : }
2114 : }
2115 :
2116 : // my_dirichlet_spline_dofs may now include DoFs whose owners
2117 : // don't know they need to become spline DoFs! We need to push
2118 : // this data to them.
2119 1717 : if (this->comm().size() > 1)
2120 : {
2121 : std::unordered_map
2122 : <processor_id_type, std::vector<dof_id_type>>
2123 96 : their_dirichlet_spline_dofs;
2124 :
2125 : // If we ever change the underlying container here then we'd
2126 : // better do some kind of sort before using it; we'll rely
2127 : // on sorting to make the processor id lookup efficient.
2128 48 : libmesh_assert(std::is_sorted(my_dirichlet_spline_dofs.begin(),
2129 : my_dirichlet_spline_dofs.end()));
2130 1672 : processor_id_type destination_pid = 0;
2131 4098 : for (auto d : my_dirichlet_spline_dofs)
2132 : {
2133 230 : libmesh_assert_less(d, this->end_dof(this->comm().size()-1));
2134 3868 : while (d >= this->end_dof(destination_pid))
2135 1201 : destination_pid++;
2136 :
2137 2656 : if (destination_pid != this->processor_id())
2138 661 : their_dirichlet_spline_dofs[destination_pid].push_back(d);
2139 : }
2140 :
2141 : auto receive_dof_functor =
2142 137 : [& my_dirichlet_spline_dofs]
2143 : (processor_id_type,
2144 141 : const std::vector<dof_id_type> & dofs)
2145 : {
2146 145 : my_dirichlet_spline_dofs.insert(dofs.begin(), dofs.end());
2147 1676 : };
2148 :
2149 : Parallel::push_parallel_vector_data
2150 1672 : (this->comm(), their_dirichlet_spline_dofs, receive_dof_functor);
2151 : }
2152 :
2153 :
2154 : // If anyone had any prior constraints in effect, then we need
2155 : // to convert them to constraints on the spline nodes.
2156 : //
2157 : // NOT simply testing prior_constraints here; maybe it turned
2158 : // out that all our constraints were on non-spline-constrained
2159 : // parts of a hybrid mesh?
2160 : bool important_prior_constraints =
2161 1717 : !was_previously_constrained.empty();
2162 1717 : this->comm().max(important_prior_constraints);
2163 :
2164 1717 : if (important_prior_constraints)
2165 : {
2166 : // Now that we have the spline constraints added, we can
2167 : // finally construct a sparsity pattern that correctly
2168 : // accounts for those constraints!
2169 1518 : mat = SparseMatrix<Number>::build(this->comm());
2170 1562 : for (auto q : IntRange<unsigned int>(0, n_adjoint_rhs+1))
2171 : {
2172 781 : solve_rhs[q] = NumericVector<Number>::build(this->comm());
2173 825 : solve_rhs[q]->init(this->n_dofs(), this->n_local_dofs(),
2174 44 : false, PARALLEL);
2175 : }
2176 :
2177 : // We need to compute our own sparsity pattern, to take into
2178 : // account the particularly non-sparse rows that can be
2179 : // created by the spline constraints we just added.
2180 781 : mat->attach_dof_map(*this);
2181 1518 : sp = this->build_sparsity(mesh);
2182 781 : mat->attach_sparsity_pattern(*sp);
2183 781 : mat->init();
2184 :
2185 94607 : for (auto & node_row : constraint_rows)
2186 : {
2187 93826 : const Node * node = node_row.first;
2188 8796 : libmesh_assert(node == mesh.node_ptr(node->id()));
2189 :
2190 372868 : for (auto var_num : IntRange<unsigned int>(0, this->n_variables()))
2191 : {
2192 26220 : const FEFamily & fe_family = this->variable_type(var_num).family;
2193 :
2194 : // constraint_rows only applies to nodal variables
2195 279042 : if (fe_family != LAGRANGE &&
2196 26136 : fe_family != RATIONAL_BERNSTEIN)
2197 0 : continue;
2198 :
2199 : const dof_id_type constrained_id =
2200 279042 : node->dof_number(sys_num, var_num, 0);
2201 :
2202 52440 : if (was_previously_constrained.count(constrained_id))
2203 : {
2204 9672 : for (auto q : IntRange<int>(0, n_adjoint_rhs+1))
2205 : {
2206 5642 : DenseMatrix<Number> K(1,1);
2207 4836 : DenseVector<Number> F(1);
2208 5239 : std::vector<dof_id_type> dof_indices(1, constrained_id);
2209 :
2210 4433 : K(0,0) = 1;
2211 :
2212 4836 : DofConstraintValueMap & vals = q ?
2213 4433 : _adjoint_constraint_values[q-1] :
2214 403 : _primal_constraint_values;
2215 :
2216 : DofConstraintValueMap::const_iterator rhsit =
2217 403 : vals.find(constrained_id);
2218 4836 : F(0) = (rhsit == vals.end()) ? 0 : rhsit->second;
2219 :
2220 : // We no longer need any rhs values here directly.
2221 4836 : if (rhsit != vals.end())
2222 4433 : vals.erase(rhsit);
2223 :
2224 : this->heterogeneously_constrain_element_matrix_and_vector
2225 4836 : (K, F, dof_indices, false, q ? (q-1) : -1);
2226 4836 : if (!q)
2227 4836 : mat->add_matrix(K, dof_indices);
2228 4836 : solve_rhs[q]->add_vector(F, dof_indices);
2229 4030 : }
2230 : }
2231 : }
2232 : }
2233 :
2234 : // Any DoFs that aren't part of any constraint, directly or
2235 : // indirectly, need a diagonal term to make the matrix
2236 : // here invertible.
2237 17777 : for (dof_id_type d : IntRange<dof_id_type>(this->first_dof(),
2238 222301 : this->end_dof()))
2239 50717 : if (!was_previously_constrained.count(d) &&
2240 16637 : !my_dirichlet_spline_dofs.count(d))
2241 196884 : mat->add(d,d,1);
2242 :
2243 : // At this point, we're finally ready to solve for Dirichlet
2244 : // constraint values on spline nodes.
2245 : std::unique_ptr<LinearSolver<Number>> linear_solver =
2246 803 : LinearSolver<Number>::build(this->comm());
2247 :
2248 : std::unique_ptr<NumericVector<Number>> projected_vals =
2249 803 : NumericVector<Number>::build(this->comm());
2250 :
2251 803 : projected_vals->init(this->n_dofs(), this->n_local_dofs(),
2252 44 : false, PARALLEL);
2253 :
2254 44 : DofConstraintRow empty_row;
2255 4202 : for (auto sd : my_dirichlet_spline_dofs)
2256 3035 : if (this->local_index(sd))
2257 230 : this->add_constraint_row(sd, empty_row);
2258 :
2259 1562 : for (auto q : IntRange<unsigned int>(0, n_adjoint_rhs+1))
2260 : {
2261 : // FIXME: we don't have an EquationSystems here, but I'd
2262 : // rather not hardcode these...
2263 22 : const double tol = double(TOLERANCE * TOLERANCE);
2264 22 : const unsigned int max_its = 5000;
2265 :
2266 781 : linear_solver->solve(*mat, *projected_vals,
2267 847 : *(solve_rhs[q]), tol, max_its);
2268 :
2269 781 : DofConstraintValueMap & vals = q ?
2270 759 : _adjoint_constraint_values[q-1] :
2271 22 : _primal_constraint_values;
2272 :
2273 4202 : for (auto sd : my_dirichlet_spline_dofs)
2274 3035 : if (this->local_index(sd))
2275 : {
2276 2760 : Number constraint_rhs = (*projected_vals)(sd);
2277 :
2278 : std::pair<DofConstraintValueMap::iterator, bool> rhs_it =
2279 230 : vals.emplace(sd, constraint_rhs);
2280 2760 : if (!rhs_it.second)
2281 2760 : rhs_it.first->second = constraint_rhs;
2282 : }
2283 : }
2284 737 : }
2285 1621 : }
2286 :
2287 :
2288 :
2289 601170 : void DofMap::add_constraint_row (const dof_id_type dof_number,
2290 : const DofConstraintRow & constraint_row,
2291 : const Number constraint_rhs,
2292 : const bool forbid_constraint_overwrite)
2293 : {
2294 : // Optionally allow the user to overwrite constraints. Defaults to false.
2295 601170 : libmesh_error_msg_if(forbid_constraint_overwrite && this->is_constrained_dof(dof_number),
2296 : "ERROR: DOF " << dof_number << " was already constrained!");
2297 :
2298 48756 : libmesh_assert_less(dof_number, this->n_dofs());
2299 :
2300 : // There is an implied "1" on the diagonal of the constraint row, and the user
2301 : // should not try to manually set _any_ value on the diagonal.
2302 48756 : libmesh_assert_msg(!constraint_row.count(dof_number),
2303 : "Error: constraint_row for dof " << dof_number <<
2304 : " should not contain an entry for dof " << dof_number);
2305 :
2306 : #ifndef NDEBUG
2307 93965 : for (const auto & pr : constraint_row)
2308 45209 : libmesh_assert_less(pr.first, this->n_dofs());
2309 : #endif
2310 :
2311 : // Store the constraint_row in the map
2312 601170 : _dof_constraints.insert_or_assign(dof_number, constraint_row);
2313 :
2314 : std::pair<DofConstraintValueMap::iterator, bool> rhs_it =
2315 48756 : _primal_constraint_values.emplace(dof_number, constraint_rhs);
2316 601170 : if (!rhs_it.second)
2317 4836 : rhs_it.first->second = constraint_rhs;
2318 601170 : }
2319 :
2320 :
2321 14304 : void DofMap::add_adjoint_constraint_row (const unsigned int qoi_index,
2322 : const dof_id_type dof_number,
2323 : const DofConstraintRow & /*constraint_row*/,
2324 : const Number constraint_rhs,
2325 : const bool forbid_constraint_overwrite)
2326 : {
2327 : // Optionally allow the user to overwrite constraints. Defaults to false.
2328 14304 : if (forbid_constraint_overwrite)
2329 : {
2330 14304 : libmesh_error_msg_if(!this->is_constrained_dof(dof_number),
2331 : "ERROR: DOF " << dof_number << " has no corresponding primal constraint!");
2332 : #ifndef NDEBUG
2333 : // No way to do this without a non-normalized tolerance?
2334 :
2335 : // // If the user passed in more than just the rhs, let's check the
2336 : // // coefficients for consistency
2337 : // if (!constraint_row.empty())
2338 : // {
2339 : // DofConstraintRow row = _dof_constraints[dof_number];
2340 : // for (const auto & [dof, val] : row)
2341 : // libmesh_assert(constraint_row.find(dof)->second == val);
2342 : // }
2343 : //
2344 : // if (_adjoint_constraint_values[qoi_index].find(dof_number) !=
2345 : // _adjoint_constraint_values[qoi_index].end())
2346 : // libmesh_assert_equal_to(_adjoint_constraint_values[qoi_index][dof_number],
2347 : // constraint_rhs);
2348 :
2349 : #endif
2350 : }
2351 :
2352 : // Creates the map of rhs values if it doesn't already exist; then
2353 : // adds the current value to that map
2354 :
2355 : // Store the rhs value in the map
2356 14304 : _adjoint_constraint_values[qoi_index].insert_or_assign(dof_number, constraint_rhs);
2357 14304 : }
2358 :
2359 :
2360 :
2361 :
2362 0 : void DofMap::print_dof_constraints(std::ostream & os,
2363 : bool print_nonlocal) const
2364 : {
2365 0 : parallel_object_only();
2366 :
2367 : std::string local_constraints =
2368 0 : this->get_local_constraints(print_nonlocal);
2369 :
2370 0 : if (this->processor_id())
2371 : {
2372 0 : this->comm().send(0, local_constraints);
2373 : }
2374 : else
2375 : {
2376 0 : os << "Processor 0:\n";
2377 0 : os << local_constraints;
2378 :
2379 0 : for (auto p : IntRange<processor_id_type>(1, this->n_processors()))
2380 : {
2381 0 : this->comm().receive(p, local_constraints);
2382 0 : os << "Processor " << p << ":\n";
2383 0 : os << local_constraints;
2384 : }
2385 : }
2386 0 : }
2387 :
2388 0 : std::string DofMap::get_local_constraints(bool print_nonlocal) const
2389 : {
2390 0 : std::ostringstream os;
2391 : #ifdef LIBMESH_ENABLE_NODE_CONSTRAINTS
2392 0 : if (print_nonlocal)
2393 0 : os << "All ";
2394 : else
2395 0 : os << "Local ";
2396 :
2397 0 : os << "Node Constraints:"
2398 0 : << std::endl;
2399 :
2400 0 : for (const auto & [node, pr] : _node_constraints)
2401 : {
2402 : // Skip non-local nodes if requested
2403 0 : if (!print_nonlocal &&
2404 0 : node->processor_id() != this->processor_id())
2405 0 : continue;
2406 :
2407 0 : const NodeConstraintRow & row = pr.first;
2408 0 : const Point & offset = pr.second;
2409 :
2410 0 : os << "Constraints for Node id " << node->id()
2411 0 : << ": \t";
2412 :
2413 0 : for (const auto & [cnode, val] : row)
2414 0 : os << " (" << cnode->id() << "," << val << ")\t";
2415 :
2416 0 : os << "rhs: " << offset;
2417 :
2418 0 : os << std::endl;
2419 : }
2420 : #endif // LIBMESH_ENABLE_NODE_CONSTRAINTS
2421 :
2422 0 : if (print_nonlocal)
2423 0 : os << "All ";
2424 : else
2425 0 : os << "Local ";
2426 :
2427 0 : os << "DoF Constraints:"
2428 0 : << std::endl;
2429 :
2430 0 : for (const auto & [i, row] : _dof_constraints)
2431 : {
2432 : // Skip non-local dofs if requested
2433 0 : if (!print_nonlocal && !this->local_index(i))
2434 0 : continue;
2435 :
2436 : DofConstraintValueMap::const_iterator rhsit =
2437 0 : _primal_constraint_values.find(i);
2438 0 : const Number rhs = (rhsit == _primal_constraint_values.end()) ?
2439 0 : 0 : rhsit->second;
2440 :
2441 0 : os << "Constraints for DoF " << i
2442 0 : << ": \t";
2443 :
2444 0 : for (const auto & item : row)
2445 0 : os << " (" << item.first << "," << item.second << ")\t";
2446 :
2447 0 : os << "rhs: " << rhs;
2448 0 : os << std::endl;
2449 : }
2450 :
2451 0 : for (unsigned int qoi_index = 0,
2452 0 : n_qois = cast_int<unsigned int>(_adjoint_dirichlet_boundaries.size());
2453 0 : qoi_index != n_qois; ++qoi_index)
2454 : {
2455 0 : os << "Adjoint " << qoi_index << " DoF rhs values:"
2456 0 : << std::endl;
2457 :
2458 0 : if (auto adjoint_map_it = _adjoint_constraint_values.find(qoi_index);
2459 0 : adjoint_map_it != _adjoint_constraint_values.end())
2460 0 : for (const auto & [i, rhs] : adjoint_map_it->second)
2461 : {
2462 : // Skip non-local dofs if requested
2463 0 : if (!print_nonlocal && !this->local_index(i))
2464 0 : continue;
2465 :
2466 0 : os << "RHS for DoF " << i
2467 0 : << ": " << rhs;
2468 :
2469 0 : os << std::endl;
2470 : }
2471 : }
2472 :
2473 0 : return os.str();
2474 0 : }
2475 :
2476 :
2477 :
2478 27028965 : void DofMap::constrain_element_matrix (DenseMatrix<Number> & matrix,
2479 : std::vector<dof_id_type> & elem_dofs,
2480 : bool asymmetric_constraint_rows) const
2481 : {
2482 1838984 : libmesh_assert_equal_to (elem_dofs.size(), matrix.m());
2483 1838984 : libmesh_assert_equal_to (elem_dofs.size(), matrix.n());
2484 :
2485 : // check for easy return
2486 27028965 : if (this->_dof_constraints.empty())
2487 11593606 : return;
2488 :
2489 : // The constrained matrix is built up as C^T K C.
2490 19080095 : DenseMatrix<Number> C;
2491 :
2492 :
2493 15435359 : this->build_constraint_matrix (C, elem_dofs);
2494 :
2495 5467104 : LOG_SCOPE("constrain_elem_matrix()", "DofMap");
2496 :
2497 : // It is possible that the matrix is not constrained at all.
2498 15454891 : if ((C.m() == matrix.m()) &&
2499 221599 : (C.n() == elem_dofs.size())) // It the matrix is constrained
2500 : {
2501 : // Compute the matrix-matrix-matrix product C^T K C
2502 221599 : matrix.left_multiply_transpose (C);
2503 221599 : matrix.right_multiply (C);
2504 :
2505 :
2506 19532 : libmesh_assert_equal_to (matrix.m(), matrix.n());
2507 19532 : libmesh_assert_equal_to (matrix.m(), elem_dofs.size());
2508 19532 : libmesh_assert_equal_to (matrix.n(), elem_dofs.size());
2509 :
2510 :
2511 1805515 : for (unsigned int i=0,
2512 39064 : n_elem_dofs = cast_int<unsigned int>(elem_dofs.size());
2513 1844579 : i != n_elem_dofs; i++)
2514 : // If the DOF is constrained
2515 1767082 : if (this->is_constrained_dof(elem_dofs[i]))
2516 : {
2517 6977008 : for (auto j : make_range(matrix.n()))
2518 6360823 : matrix(i,j) = 0.;
2519 :
2520 582961 : matrix(i,i) = 1.;
2521 :
2522 586073 : if (asymmetric_constraint_rows)
2523 : {
2524 : DofConstraints::const_iterator
2525 3684 : pos = _dof_constraints.find(elem_dofs[i]);
2526 :
2527 3684 : libmesh_assert (pos != _dof_constraints.end());
2528 :
2529 3684 : const DofConstraintRow & constraint_row = pos->second;
2530 :
2531 : // This is an overzealous assertion in the presence of
2532 : // heterogeneous constraints: we now can constrain "u_i = c"
2533 : // with no other u_j terms involved.
2534 : //
2535 : // libmesh_assert (!constraint_row.empty());
2536 :
2537 104700 : for (const auto & item : constraint_row)
2538 350886 : for (unsigned int j=0; j != n_elem_dofs; j++)
2539 319140 : if (elem_dofs[j] == item.first)
2540 53670 : matrix(i,j) = -item.second;
2541 : }
2542 : }
2543 : } // end if is constrained...
2544 11790623 : }
2545 :
2546 :
2547 :
2548 14583223 : void DofMap::constrain_element_matrix_and_vector (DenseMatrix<Number> & matrix,
2549 : DenseVector<Number> & rhs,
2550 : std::vector<dof_id_type> & elem_dofs,
2551 : bool asymmetric_constraint_rows) const
2552 : {
2553 1293055 : libmesh_assert_equal_to (elem_dofs.size(), matrix.m());
2554 1293055 : libmesh_assert_equal_to (elem_dofs.size(), matrix.n());
2555 1293055 : libmesh_assert_equal_to (elem_dofs.size(), rhs.size());
2556 :
2557 : // check for easy return
2558 14583223 : if (this->_dof_constraints.empty())
2559 7903813 : return;
2560 :
2561 : // The constrained matrix is built up as C^T K C.
2562 : // The constrained RHS is built up as C^T F
2563 8081950 : DenseMatrix<Number> C;
2564 :
2565 6679410 : this->build_constraint_matrix (C, elem_dofs);
2566 :
2567 2103810 : LOG_SCOPE("cnstrn_elem_mat_vec()", "DofMap");
2568 :
2569 : // It is possible that the matrix is not constrained at all.
2570 6800298 : if ((C.m() == matrix.m()) &&
2571 1222223 : (C.n() == elem_dofs.size())) // It the matrix is constrained
2572 : {
2573 : // Compute the matrix-matrix-matrix product C^T K C
2574 1222223 : matrix.left_multiply_transpose (C);
2575 1222223 : matrix.right_multiply (C);
2576 :
2577 :
2578 120888 : libmesh_assert_equal_to (matrix.m(), matrix.n());
2579 120888 : libmesh_assert_equal_to (matrix.m(), elem_dofs.size());
2580 120888 : libmesh_assert_equal_to (matrix.n(), elem_dofs.size());
2581 :
2582 :
2583 17600756 : for (unsigned int i=0,
2584 241776 : n_elem_dofs = cast_int<unsigned int>(elem_dofs.size());
2585 17842532 : i != n_elem_dofs; i++)
2586 18192806 : if (this->is_constrained_dof(elem_dofs[i]))
2587 : {
2588 218781934 : for (auto j : make_range(matrix.n()))
2589 201298911 : matrix(i,j) = 0.;
2590 :
2591 : // If the DOF is constrained
2592 5544636 : matrix(i,i) = 1.;
2593 :
2594 : // This will put a nonsymmetric entry in the constraint
2595 : // row to ensure that the linear system produces the
2596 : // correct value for the constrained DOF.
2597 5864193 : if (asymmetric_constraint_rows)
2598 : {
2599 : DofConstraints::const_iterator
2600 177047 : pos = _dof_constraints.find(elem_dofs[i]);
2601 :
2602 177047 : libmesh_assert (pos != _dof_constraints.end());
2603 :
2604 177047 : const DofConstraintRow & constraint_row = pos->second;
2605 :
2606 : // p refinement creates empty constraint rows
2607 : // libmesh_assert (!constraint_row.empty());
2608 :
2609 4804680 : for (const auto & item : constraint_row)
2610 49576576 : for (unsigned int j=0; j != n_elem_dofs; j++)
2611 51065424 : if (elem_dofs[j] == item.first)
2612 3323925 : matrix(i,j) = -item.second;
2613 : }
2614 : }
2615 :
2616 :
2617 : // Compute the matrix-vector product C^T F
2618 241776 : DenseVector<Number> old_rhs(rhs);
2619 :
2620 : // compute matrix/vector product
2621 1222223 : C.vector_mult_transpose(rhs, old_rhs);
2622 : } // end if is constrained...
2623 5276870 : }
2624 :
2625 :
2626 :
2627 684324 : void DofMap::heterogeneously_constrain_element_matrix_and_vector (DenseMatrix<Number> & matrix,
2628 : DenseVector<Number> & rhs,
2629 : std::vector<dof_id_type> & elem_dofs,
2630 : bool asymmetric_constraint_rows,
2631 : int qoi_index) const
2632 : {
2633 67258 : libmesh_assert_equal_to (elem_dofs.size(), matrix.m());
2634 67258 : libmesh_assert_equal_to (elem_dofs.size(), matrix.n());
2635 67258 : libmesh_assert_equal_to (elem_dofs.size(), rhs.size());
2636 :
2637 : // check for easy return
2638 684324 : if (this->_dof_constraints.empty())
2639 30110 : return;
2640 :
2641 : // The constrained matrix is built up as C^T K C.
2642 : // The constrained RHS is built up as C^T (F - K H)
2643 787476 : DenseMatrix<Number> C;
2644 654214 : DenseVector<Number> H;
2645 :
2646 654214 : this->build_constraint_matrix_and_vector (C, H, elem_dofs, qoi_index);
2647 :
2648 199893 : LOG_SCOPE("hetero_cnstrn_elem_mat_vec()", "DofMap");
2649 :
2650 : // It is possible that the matrix is not constrained at all.
2651 670511 : if ((C.m() == matrix.m()) &&
2652 172692 : (C.n() == elem_dofs.size())) // It the matrix is constrained
2653 : {
2654 : // We may have rhs values to use later
2655 16297 : const DofConstraintValueMap * rhs_values = nullptr;
2656 172692 : if (qoi_index < 0)
2657 172692 : rhs_values = &_primal_constraint_values;
2658 0 : else if (auto it = _adjoint_constraint_values.find(qoi_index);
2659 0 : it != _adjoint_constraint_values.end())
2660 0 : rhs_values = &it->second;
2661 :
2662 : // Compute matrix/vector product K H
2663 172692 : DenseVector<Number> KH;
2664 172692 : matrix.vector_mult(KH, H);
2665 :
2666 : // Compute the matrix-vector product C^T (F - KH)
2667 32594 : DenseVector<Number> F_minus_KH(rhs);
2668 156395 : F_minus_KH -= KH;
2669 172692 : C.vector_mult_transpose(rhs, F_minus_KH);
2670 :
2671 : // Compute the matrix-matrix-matrix product C^T K C
2672 172692 : matrix.left_multiply_transpose (C);
2673 172692 : matrix.right_multiply (C);
2674 :
2675 16297 : libmesh_assert_equal_to (matrix.m(), matrix.n());
2676 16297 : libmesh_assert_equal_to (matrix.m(), elem_dofs.size());
2677 16297 : libmesh_assert_equal_to (matrix.n(), elem_dofs.size());
2678 :
2679 3154852 : for (unsigned int i=0,
2680 32594 : n_elem_dofs = cast_int<unsigned int>(elem_dofs.size());
2681 3187446 : i != n_elem_dofs; i++)
2682 : {
2683 3309485 : const dof_id_type dof_id = elem_dofs[i];
2684 :
2685 2720023 : if (this->is_constrained_dof(dof_id))
2686 : {
2687 19118943 : for (auto j : make_range(matrix.n()))
2688 16938270 : matrix(i,j) = 0.;
2689 :
2690 : // If the DOF is constrained
2691 838398 : matrix(i,i) = 1.;
2692 :
2693 : // This will put a nonsymmetric entry in the constraint
2694 : // row to ensure that the linear system produces the
2695 : // correct value for the constrained DOF.
2696 903056 : if (asymmetric_constraint_rows)
2697 : {
2698 : DofConstraints::const_iterator
2699 85547 : pos = _dof_constraints.find(dof_id);
2700 :
2701 85547 : libmesh_assert (pos != _dof_constraints.end());
2702 :
2703 85547 : const DofConstraintRow & constraint_row = pos->second;
2704 :
2705 969507 : for (const auto & item : constraint_row)
2706 1679230 : for (unsigned int j=0; j != n_elem_dofs; j++)
2707 1722563 : if (elem_dofs[j] == item.first)
2708 95457 : matrix(i,j) = -item.second;
2709 :
2710 881329 : if (rhs_values)
2711 : {
2712 : const DofConstraintValueMap::const_iterator valpos =
2713 85547 : rhs_values->find(dof_id);
2714 :
2715 904266 : rhs(i) = (valpos == rhs_values->end()) ?
2716 22937 : 0 : valpos->second;
2717 : }
2718 : }
2719 : else
2720 19960 : rhs(i) = 0.;
2721 : }
2722 : }
2723 :
2724 : } // end if is constrained...
2725 520952 : }
2726 :
2727 :
2728 1760 : void DofMap::heterogeneously_constrain_element_jacobian_and_residual
2729 : (DenseMatrix<Number> & matrix,
2730 : DenseVector<Number> & rhs,
2731 : std::vector<dof_id_type> & elem_dofs,
2732 : NumericVector<Number> & solution_local) const
2733 : {
2734 160 : libmesh_assert_equal_to (elem_dofs.size(), matrix.m());
2735 160 : libmesh_assert_equal_to (elem_dofs.size(), matrix.n());
2736 160 : libmesh_assert_equal_to (elem_dofs.size(), rhs.size());
2737 :
2738 160 : libmesh_assert (solution_local.type() == SERIAL ||
2739 : solution_local.type() == GHOSTED);
2740 :
2741 : // check for easy return
2742 1760 : if (this->_dof_constraints.empty())
2743 0 : return;
2744 :
2745 : // The constrained matrix is built up as C^T K C.
2746 : // The constrained RHS is built up as C^T F
2747 : // Asymmetric residual terms are added if we do not have x = Cx+h
2748 1920 : DenseMatrix<Number> C;
2749 1600 : DenseVector<Number> H;
2750 :
2751 1760 : this->build_constraint_matrix_and_vector (C, H, elem_dofs);
2752 :
2753 320 : LOG_SCOPE("hetero_cnstrn_elem_jac_res()", "DofMap");
2754 :
2755 : // It is possible that the matrix is not constrained at all.
2756 1920 : if ((C.m() != matrix.m()) ||
2757 1760 : (C.n() != elem_dofs.size()))
2758 0 : return;
2759 :
2760 : // Compute the matrix-vector product C^T F
2761 320 : DenseVector<Number> old_rhs(rhs);
2762 1760 : C.vector_mult_transpose(rhs, old_rhs);
2763 :
2764 : // Compute the matrix-matrix-matrix product C^T K C
2765 1760 : matrix.left_multiply_transpose (C);
2766 1760 : matrix.right_multiply (C);
2767 :
2768 160 : libmesh_assert_equal_to (matrix.m(), matrix.n());
2769 160 : libmesh_assert_equal_to (matrix.m(), elem_dofs.size());
2770 160 : libmesh_assert_equal_to (matrix.n(), elem_dofs.size());
2771 :
2772 8480 : for (unsigned int i=0,
2773 320 : n_elem_dofs = cast_int<unsigned int>(elem_dofs.size());
2774 8800 : i != n_elem_dofs; i++)
2775 : {
2776 7680 : const dof_id_type dof_id = elem_dofs[i];
2777 :
2778 7040 : if (auto pos = _dof_constraints.find(dof_id);
2779 640 : pos != _dof_constraints.end())
2780 : {
2781 26400 : for (auto j : make_range(matrix.n()))
2782 21120 : matrix(i,j) = 0.;
2783 :
2784 : // If the DOF is constrained
2785 5280 : matrix(i,i) = 1.;
2786 :
2787 : // This will put a nonsymmetric entry in the constraint
2788 : // row to ensure that the linear system produces the
2789 : // correct value for the constrained DOF.
2790 480 : const DofConstraintRow & constraint_row = pos->second;
2791 :
2792 5280 : for (const auto & item : constraint_row)
2793 0 : for (unsigned int j=0; j != n_elem_dofs; j++)
2794 0 : if (elem_dofs[j] == item.first)
2795 0 : matrix(i,j) = -item.second;
2796 :
2797 : const DofConstraintValueMap::const_iterator valpos =
2798 480 : _primal_constraint_values.find(dof_id);
2799 :
2800 480 : Number & rhs_val = rhs(i);
2801 5760 : rhs_val = (valpos == _primal_constraint_values.end()) ?
2802 5280 : 0 : -valpos->second;
2803 5280 : for (const auto & [constraining_dof, coef] : constraint_row)
2804 0 : rhs_val -= coef * solution_local(constraining_dof);
2805 5280 : rhs_val += solution_local(dof_id);
2806 : }
2807 : }
2808 1440 : }
2809 :
2810 :
2811 1760 : void DofMap::heterogeneously_constrain_element_residual
2812 : (DenseVector<Number> & rhs,
2813 : std::vector<dof_id_type> & elem_dofs,
2814 : NumericVector<Number> & solution_local) const
2815 : {
2816 160 : libmesh_assert_equal_to (elem_dofs.size(), rhs.size());
2817 :
2818 160 : libmesh_assert (solution_local.type() == SERIAL ||
2819 : solution_local.type() == GHOSTED);
2820 :
2821 : // check for easy return
2822 1760 : if (this->_dof_constraints.empty())
2823 0 : return;
2824 :
2825 : // The constrained RHS is built up as C^T F
2826 : // Asymmetric residual terms are added if we do not have x = Cx+h
2827 1920 : DenseMatrix<Number> C;
2828 1600 : DenseVector<Number> H;
2829 :
2830 1760 : this->build_constraint_matrix_and_vector (C, H, elem_dofs);
2831 :
2832 320 : LOG_SCOPE("hetero_cnstrn_elem_res()", "DofMap");
2833 :
2834 : // It is possible that the element is not constrained at all.
2835 2080 : if ((C.m() != rhs.size()) ||
2836 1760 : (C.n() != elem_dofs.size()))
2837 0 : return;
2838 :
2839 : // Compute the matrix-vector product C^T F
2840 320 : DenseVector<Number> old_rhs(rhs);
2841 1760 : C.vector_mult_transpose(rhs, old_rhs);
2842 :
2843 8480 : for (unsigned int i=0,
2844 320 : n_elem_dofs = cast_int<unsigned int>(elem_dofs.size());
2845 8800 : i != n_elem_dofs; i++)
2846 : {
2847 7680 : const dof_id_type dof_id = elem_dofs[i];
2848 :
2849 7040 : if (auto pos = _dof_constraints.find(dof_id);
2850 640 : pos != _dof_constraints.end())
2851 : {
2852 : // This will put a nonsymmetric entry in the constraint
2853 : // row to ensure that the linear system produces the
2854 : // correct value for the constrained DOF.
2855 480 : const DofConstraintRow & constraint_row = pos->second;
2856 :
2857 : const DofConstraintValueMap::const_iterator valpos =
2858 480 : _primal_constraint_values.find(dof_id);
2859 :
2860 480 : Number & rhs_val = rhs(i);
2861 10080 : rhs_val = (valpos == _primal_constraint_values.end()) ?
2862 5280 : 0 : -valpos->second;
2863 5280 : for (const auto & [constraining_dof, coef] : constraint_row)
2864 0 : rhs_val -= coef * solution_local(constraining_dof);
2865 5280 : rhs_val += solution_local(dof_id);
2866 : }
2867 : }
2868 1440 : }
2869 :
2870 :
2871 0 : void DofMap::constrain_element_residual
2872 : (DenseVector<Number> & rhs,
2873 : std::vector<dof_id_type> & elem_dofs,
2874 : NumericVector<Number> & solution_local) const
2875 : {
2876 0 : libmesh_assert_equal_to (elem_dofs.size(), rhs.size());
2877 :
2878 0 : libmesh_assert (solution_local.type() == SERIAL ||
2879 : solution_local.type() == GHOSTED);
2880 :
2881 : // check for easy return
2882 0 : if (this->_dof_constraints.empty())
2883 0 : return;
2884 :
2885 : // The constrained RHS is built up as C^T F
2886 0 : DenseMatrix<Number> C;
2887 :
2888 0 : this->build_constraint_matrix (C, elem_dofs);
2889 :
2890 0 : LOG_SCOPE("cnstrn_elem_residual()", "DofMap");
2891 :
2892 : // It is possible that the matrix is not constrained at all.
2893 0 : if (C.n() != elem_dofs.size())
2894 0 : return;
2895 :
2896 : // Compute the matrix-vector product C^T F
2897 0 : DenseVector<Number> old_rhs(rhs);
2898 0 : C.vector_mult_transpose(rhs, old_rhs);
2899 :
2900 0 : for (unsigned int i=0,
2901 0 : n_elem_dofs = cast_int<unsigned int>(elem_dofs.size());
2902 0 : i != n_elem_dofs; i++)
2903 : {
2904 0 : const dof_id_type dof_id = elem_dofs[i];
2905 :
2906 0 : if (auto pos = _dof_constraints.find(dof_id);
2907 0 : pos != _dof_constraints.end())
2908 : {
2909 : // This will put a nonsymmetric entry in the constraint
2910 : // row to ensure that the linear system produces the
2911 : // correct value for the constrained DOF.
2912 0 : const DofConstraintRow & constraint_row = pos->second;
2913 :
2914 0 : Number & rhs_val = rhs(i);
2915 0 : rhs_val = 0;
2916 0 : for (const auto & [constraining_dof, coef] : constraint_row)
2917 0 : rhs_val -= coef * solution_local(constraining_dof);
2918 0 : rhs_val += solution_local(dof_id);
2919 : }
2920 : }
2921 0 : }
2922 :
2923 :
2924 1680 : void DofMap::heterogeneously_constrain_element_vector (const DenseMatrix<Number> & matrix,
2925 : DenseVector<Number> & rhs,
2926 : std::vector<dof_id_type> & elem_dofs,
2927 : bool asymmetric_constraint_rows,
2928 : int qoi_index) const
2929 : {
2930 140 : libmesh_assert_equal_to (elem_dofs.size(), matrix.m());
2931 140 : libmesh_assert_equal_to (elem_dofs.size(), matrix.n());
2932 140 : libmesh_assert_equal_to (elem_dofs.size(), rhs.size());
2933 :
2934 : // check for easy return
2935 1680 : if (this->_dof_constraints.empty())
2936 0 : return;
2937 :
2938 : // The constrained matrix is built up as C^T K C.
2939 : // The constrained RHS is built up as C^T (F - K H)
2940 1960 : DenseMatrix<Number> C;
2941 1680 : DenseVector<Number> H;
2942 :
2943 1680 : this->build_constraint_matrix_and_vector (C, H, elem_dofs, qoi_index);
2944 :
2945 420 : LOG_SCOPE("hetero_cnstrn_elem_vec()", "DofMap");
2946 :
2947 : // It is possible that the matrix is not constrained at all.
2948 1820 : if ((C.m() == matrix.m()) &&
2949 1680 : (C.n() == elem_dofs.size())) // It the matrix is constrained
2950 : {
2951 : // We may have rhs values to use later
2952 140 : const DofConstraintValueMap * rhs_values = nullptr;
2953 1680 : if (qoi_index < 0)
2954 0 : rhs_values = &_primal_constraint_values;
2955 1820 : else if (auto it = _adjoint_constraint_values.find(qoi_index);
2956 140 : it != _adjoint_constraint_values.end())
2957 1680 : rhs_values = &it->second;
2958 :
2959 : // Compute matrix/vector product K H
2960 1680 : DenseVector<Number> KH;
2961 1680 : matrix.vector_mult(KH, H);
2962 :
2963 : // Compute the matrix-vector product C^T (F - KH)
2964 280 : DenseVector<Number> F_minus_KH(rhs);
2965 1540 : F_minus_KH -= KH;
2966 1680 : C.vector_mult_transpose(rhs, F_minus_KH);
2967 :
2968 16520 : for (unsigned int i=0,
2969 280 : n_elem_dofs = cast_int<unsigned int>(elem_dofs.size());
2970 16800 : i != n_elem_dofs; i++)
2971 : {
2972 16380 : const dof_id_type dof_id = elem_dofs[i];
2973 :
2974 9420 : if (this->is_constrained_dof(dof_id))
2975 : {
2976 : // This will put a nonsymmetric entry in the constraint
2977 : // row to ensure that the linear system produces the
2978 : // correct value for the constrained DOF.
2979 5328 : if (asymmetric_constraint_rows && rhs_values)
2980 : {
2981 : const DofConstraintValueMap::const_iterator valpos =
2982 0 : rhs_values->find(dof_id);
2983 :
2984 0 : rhs(i) = (valpos == rhs_values->end()) ?
2985 0 : 0 : valpos->second;
2986 : }
2987 : else
2988 4884 : rhs(i) = 0.;
2989 : }
2990 : }
2991 :
2992 : } // end if is constrained...
2993 1400 : }
2994 :
2995 :
2996 :
2997 :
2998 0 : void DofMap::constrain_element_matrix (DenseMatrix<Number> & matrix,
2999 : std::vector<dof_id_type> & row_dofs,
3000 : std::vector<dof_id_type> & col_dofs,
3001 : bool asymmetric_constraint_rows) const
3002 : {
3003 0 : libmesh_assert_equal_to (row_dofs.size(), matrix.m());
3004 0 : libmesh_assert_equal_to (col_dofs.size(), matrix.n());
3005 :
3006 : // check for easy return
3007 0 : if (this->_dof_constraints.empty())
3008 0 : return;
3009 :
3010 : // The constrained matrix is built up as R^T K C.
3011 0 : DenseMatrix<Number> R;
3012 0 : DenseMatrix<Number> C;
3013 :
3014 : // Safeguard against the user passing us the same
3015 : // object for row_dofs and col_dofs. If that is done
3016 : // the calls to build_matrix would fail
3017 0 : std::vector<dof_id_type> orig_row_dofs(row_dofs);
3018 0 : std::vector<dof_id_type> orig_col_dofs(col_dofs);
3019 :
3020 0 : this->build_constraint_matrix (R, orig_row_dofs);
3021 0 : this->build_constraint_matrix (C, orig_col_dofs);
3022 :
3023 0 : LOG_SCOPE("constrain_elem_matrix()", "DofMap");
3024 :
3025 0 : row_dofs = orig_row_dofs;
3026 0 : col_dofs = orig_col_dofs;
3027 :
3028 0 : bool constraint_found = false;
3029 :
3030 : // K_constrained = R^T K C
3031 :
3032 0 : if ((R.m() == matrix.m()) &&
3033 0 : (R.n() == row_dofs.size()))
3034 : {
3035 0 : matrix.left_multiply_transpose (R);
3036 0 : constraint_found = true;
3037 : }
3038 :
3039 0 : if ((C.m() == matrix.n()) &&
3040 0 : (C.n() == col_dofs.size()))
3041 : {
3042 0 : matrix.right_multiply (C);
3043 0 : constraint_found = true;
3044 : }
3045 :
3046 : // It is possible that the matrix is not constrained at all.
3047 0 : if (constraint_found)
3048 : {
3049 0 : libmesh_assert_equal_to (matrix.m(), row_dofs.size());
3050 0 : libmesh_assert_equal_to (matrix.n(), col_dofs.size());
3051 :
3052 :
3053 0 : for (unsigned int i=0,
3054 0 : n_row_dofs = cast_int<unsigned int>(row_dofs.size());
3055 0 : i != n_row_dofs; i++)
3056 0 : if (this->is_constrained_dof(row_dofs[i]))
3057 : {
3058 0 : for (auto j : make_range(matrix.n()))
3059 : {
3060 0 : if (row_dofs[i] != col_dofs[j])
3061 0 : matrix(i,j) = 0.;
3062 : else // If the DOF is constrained
3063 0 : matrix(i,j) = 1.;
3064 : }
3065 :
3066 0 : if (asymmetric_constraint_rows)
3067 : {
3068 : DofConstraints::const_iterator
3069 0 : pos = _dof_constraints.find(row_dofs[i]);
3070 :
3071 0 : libmesh_assert (pos != _dof_constraints.end());
3072 :
3073 0 : const DofConstraintRow & constraint_row = pos->second;
3074 :
3075 0 : libmesh_assert (!constraint_row.empty());
3076 :
3077 0 : for (const auto & item : constraint_row)
3078 0 : for (unsigned int j=0,
3079 0 : n_col_dofs = cast_int<unsigned int>(col_dofs.size());
3080 0 : j != n_col_dofs; j++)
3081 0 : if (col_dofs[j] == item.first)
3082 0 : matrix(i,j) = -item.second;
3083 : }
3084 : }
3085 : } // end if is constrained...
3086 0 : }
3087 :
3088 :
3089 :
3090 60111120 : void DofMap::constrain_element_vector (DenseVector<Number> & rhs,
3091 : std::vector<dof_id_type> & row_dofs,
3092 : bool) const
3093 : {
3094 4367348 : libmesh_assert_equal_to (rhs.size(), row_dofs.size());
3095 :
3096 : // check for easy return
3097 60111120 : if (this->_dof_constraints.empty())
3098 21978977 : return;
3099 :
3100 : // The constrained RHS is built up as R^T F.
3101 46549607 : DenseMatrix<Number> R;
3102 :
3103 38132143 : this->build_constraint_matrix (R, row_dofs);
3104 :
3105 12730644 : LOG_SCOPE("constrain_elem_vector()", "DofMap");
3106 :
3107 : // It is possible that the vector is not constrained at all.
3108 42377476 : if ((R.m() == rhs.size()) &&
3109 1621204 : (R.n() == row_dofs.size())) // if the RHS is constrained
3110 : {
3111 : // Compute the matrix-vector product
3112 282098 : DenseVector<Number> old_rhs(rhs);
3113 1621204 : R.vector_mult_transpose(rhs, old_rhs);
3114 :
3115 141049 : libmesh_assert_equal_to (row_dofs.size(), rhs.size());
3116 :
3117 22202051 : for (unsigned int i=0,
3118 281894 : n_row_dofs = cast_int<unsigned int>(row_dofs.size());
3119 22483945 : i != n_row_dofs; i++)
3120 22686204 : if (this->is_constrained_dof(row_dofs[i]))
3121 : {
3122 : // If the DOF is constrained
3123 687331 : libmesh_assert (_dof_constraints.find(row_dofs[i]) != _dof_constraints.end());
3124 :
3125 7320953 : rhs(i) = 0;
3126 : }
3127 : } // end if the RHS is constrained.
3128 29714679 : }
3129 :
3130 :
3131 :
3132 20369 : void DofMap::constrain_element_dyad_matrix (DenseVector<Number> & v,
3133 : DenseVector<Number> & w,
3134 : std::vector<dof_id_type> & row_dofs,
3135 : bool) const
3136 : {
3137 1795 : libmesh_assert_equal_to (v.size(), row_dofs.size());
3138 1795 : libmesh_assert_equal_to (w.size(), row_dofs.size());
3139 :
3140 : // check for easy return
3141 20369 : if (this->_dof_constraints.empty())
3142 0 : return;
3143 :
3144 : // The constrained RHS is built up as R^T F.
3145 23959 : DenseMatrix<Number> R;
3146 :
3147 20369 : this->build_constraint_matrix (R, row_dofs);
3148 :
3149 5385 : LOG_SCOPE("cnstrn_elem_dyad_mat()", "DofMap");
3150 :
3151 : // It is possible that the vector is not constrained at all.
3152 22959 : if ((R.m() == v.size()) &&
3153 8489 : (R.n() == row_dofs.size())) // if the RHS is constrained
3154 : {
3155 : // Compute the matrix-vector products
3156 1590 : DenseVector<Number> old_v(v);
3157 1590 : DenseVector<Number> old_w(w);
3158 :
3159 : // compute matrix/vector product
3160 8489 : R.vector_mult_transpose(v, old_v);
3161 8489 : R.vector_mult_transpose(w, old_w);
3162 :
3163 795 : libmesh_assert_equal_to (row_dofs.size(), v.size());
3164 795 : libmesh_assert_equal_to (row_dofs.size(), w.size());
3165 :
3166 : /* Constrain only v, not w. */
3167 :
3168 50829 : for (unsigned int i=0,
3169 1590 : n_row_dofs = cast_int<unsigned int>(row_dofs.size());
3170 52419 : i != n_row_dofs; i++)
3171 48026 : if (this->is_constrained_dof(row_dofs[i]))
3172 : {
3173 : // If the DOF is constrained
3174 916 : libmesh_assert (_dof_constraints.find(row_dofs[i]) != _dof_constraints.end());
3175 :
3176 9974 : v(i) = 0;
3177 : }
3178 : } // end if the RHS is constrained.
3179 16779 : }
3180 :
3181 :
3182 :
3183 2379157 : void DofMap::constrain_nothing (std::vector<dof_id_type> & dofs) const
3184 : {
3185 : // check for easy return
3186 2379157 : if (this->_dof_constraints.empty())
3187 199777 : return;
3188 :
3189 : // All the work is done by \p build_constraint_matrix. We just need
3190 : // a dummy matrix.
3191 2593540 : DenseMatrix<Number> R;
3192 2179380 : this->build_constraint_matrix (R, dofs);
3193 1765220 : }
3194 :
3195 :
3196 :
3197 960022 : void DofMap::enforce_constraints_exactly (const System & system,
3198 : NumericVector<Number> * v,
3199 : bool homogeneous) const
3200 : {
3201 25094 : parallel_object_only();
3202 :
3203 960022 : if (!this->n_constrained_dofs())
3204 382083 : return;
3205 :
3206 42882 : LOG_SCOPE("enforce_constraints_exactly()","DofMap");
3207 :
3208 577939 : if (!v)
3209 5806 : v = system.solution.get();
3210 :
3211 577939 : if (!v->closed())
3212 0 : v->close();
3213 :
3214 14430 : NumericVector<Number> * v_local = nullptr; // will be initialized below
3215 14430 : NumericVector<Number> * v_global = nullptr; // will be initialized below
3216 563917 : std::unique_ptr<NumericVector<Number>> v_built;
3217 577939 : if (v->type() == SERIAL)
3218 : {
3219 2522 : v_built = NumericVector<Number>::build(this->comm());
3220 1261 : v_built->init(this->n_dofs(), this->n_local_dofs(), true, PARALLEL);
3221 1261 : v_built->close();
3222 :
3223 1802564 : for (dof_id_type i=v_built->first_local_index();
3224 1802564 : i<v_built->last_local_index(); i++)
3225 1801303 : v_built->set(i, (*v)(i));
3226 1261 : v_built->close();
3227 0 : v_global = v_built.get();
3228 :
3229 0 : v_local = v;
3230 0 : libmesh_assert (v_local->closed());
3231 : }
3232 576678 : else if (v->type() == PARALLEL)
3233 : {
3234 508866 : v_built = NumericVector<Number>::build(this->comm());
3235 269689 : v_built->init (v->size(), v->local_size(),
3236 : this->get_send_list(), true,
3237 15256 : GHOSTED);
3238 262061 : v->localize(*v_built, this->get_send_list());
3239 262061 : v_built->close();
3240 7628 : v_local = v_built.get();
3241 :
3242 7628 : v_global = v;
3243 : }
3244 314617 : else if (v->type() == GHOSTED)
3245 : {
3246 6802 : v_local = v;
3247 6802 : v_global = v;
3248 : }
3249 : else // unknown v->type()
3250 0 : libmesh_error_msg("ERROR: Unsupported NumericVector type == " << Utility::enum_to_string(v->type()));
3251 :
3252 : // We should never hit these asserts because we should error-out in
3253 : // else clause above. Just to be sure we don't try to use v_local
3254 : // and v_global uninitialized...
3255 14430 : libmesh_assert(v_local);
3256 14430 : libmesh_assert(v_global);
3257 14430 : libmesh_assert_equal_to (this, &(system.get_dof_map()));
3258 :
3259 10660640 : for (const auto & [constrained_dof, constraint_row] : _dof_constraints)
3260 : {
3261 10082701 : if (!this->local_index(constrained_dof))
3262 1466732 : continue;
3263 :
3264 760435 : Number exact_value = 0;
3265 8414166 : if (!homogeneous)
3266 : {
3267 6588042 : if (auto rhsit = _primal_constraint_values.find(constrained_dof);
3268 590241 : rhsit != _primal_constraint_values.end())
3269 200176 : exact_value = rhsit->second;
3270 : }
3271 16383511 : for (const auto & [dof, val] : constraint_row)
3272 7969345 : exact_value += val * (*v_local)(dof);
3273 :
3274 8414166 : v_global->set(constrained_dof, exact_value);
3275 : }
3276 :
3277 : // If the old vector was serial, we probably need to send our values
3278 : // to other processors
3279 577939 : if (v->type() == SERIAL)
3280 : {
3281 : #ifndef NDEBUG
3282 0 : v_global->close();
3283 : #endif
3284 1261 : v_global->localize (*v);
3285 : }
3286 577939 : v->close();
3287 549487 : }
3288 :
3289 281187 : void DofMap::enforce_constraints_on_residual (const NonlinearImplicitSystem & system,
3290 : NumericVector<Number> * rhs,
3291 : NumericVector<Number> const * solution,
3292 : bool homogeneous) const
3293 : {
3294 5662 : parallel_object_only();
3295 :
3296 281187 : if (!this->n_constrained_dofs())
3297 3265 : return;
3298 :
3299 277826 : if (!rhs)
3300 0 : rhs = system.rhs;
3301 277826 : if (!solution)
3302 0 : solution = system.solution.get();
3303 :
3304 5566 : NumericVector<Number> const * solution_local = nullptr; // will be initialized below
3305 272668 : std::unique_ptr<NumericVector<Number>> solution_built;
3306 277826 : if (solution->type() == SERIAL || solution->type() == GHOSTED)
3307 5566 : solution_local = solution;
3308 0 : else if (solution->type() == PARALLEL)
3309 : {
3310 0 : solution_built = NumericVector<Number>::build(this->comm());
3311 0 : solution_built->init (solution->size(), solution->local_size(),
3312 0 : this->get_send_list(), true, GHOSTED);
3313 0 : solution->localize(*solution_built, this->get_send_list());
3314 0 : solution_built->close();
3315 0 : solution_local = solution_built.get();
3316 : }
3317 : else // unknown solution->type()
3318 0 : libmesh_error_msg("ERROR: Unsupported NumericVector type == " << Utility::enum_to_string(solution->type()));
3319 :
3320 : // We should never hit these asserts because we should error-out in
3321 : // else clause above. Just to be sure we don't try to use solution_local
3322 5566 : libmesh_assert(solution_local);
3323 5566 : libmesh_assert_equal_to (this, &(system.get_dof_map()));
3324 :
3325 470932 : for (const auto & [constrained_dof, constraint_row] : _dof_constraints)
3326 : {
3327 193106 : if (!this->local_index(constrained_dof))
3328 72331 : continue;
3329 :
3330 8573 : Number exact_value = 0;
3331 198589 : for (const auto & [dof, val] : constraint_row)
3332 82936 : exact_value -= val * (*solution_local)(dof);
3333 115653 : exact_value += (*solution_local)(constrained_dof);
3334 115653 : if (!homogeneous)
3335 : {
3336 115653 : if (auto rhsit = _primal_constraint_values.find(constrained_dof);
3337 8573 : rhsit != _primal_constraint_values.end())
3338 5368 : exact_value -= rhsit->second;
3339 : }
3340 :
3341 115653 : rhs->set(constrained_dof, exact_value);
3342 : }
3343 267102 : }
3344 :
3345 158500 : void DofMap::enforce_constraints_on_jacobian (const NonlinearImplicitSystem & system,
3346 : SparseMatrix<Number> * jac) const
3347 : {
3348 3242 : parallel_object_only();
3349 :
3350 158500 : if (!this->n_constrained_dofs())
3351 76 : return;
3352 :
3353 155839 : if (!jac)
3354 0 : jac = system.matrix;
3355 :
3356 3166 : libmesh_assert_equal_to (this, &(system.get_dof_map()));
3357 :
3358 281397 : for (const auto & [constrained_dof, constraint_row] : _dof_constraints)
3359 : {
3360 125558 : if (!this->local_index(constrained_dof))
3361 42159 : continue;
3362 :
3363 127925 : for (const auto & j : constraint_row)
3364 47658 : jac->set(constrained_dof, j.first, -j.second);
3365 80267 : jac->set(constrained_dof, constrained_dof, 1);
3366 : }
3367 : }
3368 :
3369 :
3370 60624 : void DofMap::enforce_adjoint_constraints_exactly (NumericVector<Number> & v,
3371 : unsigned int q) const
3372 : {
3373 1896 : parallel_object_only();
3374 :
3375 60624 : if (!this->n_constrained_dofs())
3376 2111 : return;
3377 :
3378 5454 : LOG_SCOPE("enforce_adjoint_constraints_exactly()", "DofMap");
3379 :
3380 1818 : NumericVector<Number> * v_local = nullptr; // will be initialized below
3381 1818 : NumericVector<Number> * v_global = nullptr; // will be initialized below
3382 56695 : std::unique_ptr<NumericVector<Number>> v_built;
3383 58513 : if (v.type() == SERIAL)
3384 : {
3385 198 : v_built = NumericVector<Number>::build(this->comm());
3386 99 : v_built->init(this->n_dofs(), this->n_local_dofs(), true, PARALLEL);
3387 99 : v_built->close();
3388 :
3389 87318 : for (dof_id_type i=v_built->first_local_index();
3390 87318 : i<v_built->last_local_index(); i++)
3391 87219 : v_built->set(i, v(i));
3392 99 : v_built->close();
3393 0 : v_global = v_built.get();
3394 :
3395 0 : v_local = &v;
3396 0 : libmesh_assert (v_local->closed());
3397 : }
3398 58414 : else if (v.type() == PARALLEL)
3399 : {
3400 10920 : v_built = NumericVector<Number>::build(this->comm());
3401 6088 : v_built->init (v.size(), v.local_size(),
3402 628 : this->get_send_list(), true, GHOSTED);
3403 5774 : v.localize(*v_built, this->get_send_list());
3404 5774 : v_built->close();
3405 314 : v_local = v_built.get();
3406 :
3407 314 : v_global = &v;
3408 : }
3409 52640 : else if (v.type() == GHOSTED)
3410 : {
3411 1504 : v_local = &v;
3412 1504 : v_global = &v;
3413 : }
3414 : else // unknown v.type()
3415 0 : libmesh_error_msg("ERROR: Unknown v.type() == " << v.type());
3416 :
3417 : // We should never hit these asserts because we should error-out in
3418 : // else clause above. Just to be sure we don't try to use v_local
3419 : // and v_global uninitialized...
3420 1818 : libmesh_assert(v_local);
3421 1818 : libmesh_assert(v_global);
3422 :
3423 : // Do we have any non_homogeneous constraints?
3424 : const AdjointDofConstraintValues::const_iterator
3425 1818 : adjoint_constraint_map_it = _adjoint_constraint_values.find(q);
3426 : const DofConstraintValueMap * constraint_map =
3427 59943 : (adjoint_constraint_map_it == _adjoint_constraint_values.end()) ?
3428 1430 : nullptr : &adjoint_constraint_map_it->second;
3429 :
3430 444326 : for (const auto & [constrained_dof, constraint_row] : _dof_constraints)
3431 : {
3432 385813 : if (!this->local_index(constrained_dof))
3433 72566 : continue;
3434 :
3435 28787 : Number exact_value = 0;
3436 309230 : if (constraint_map)
3437 : {
3438 74368 : if (const auto adjoint_constraint_it = constraint_map->find(constrained_dof);
3439 7168 : adjoint_constraint_it != constraint_map->end())
3440 4476 : exact_value = adjoint_constraint_it->second;
3441 : }
3442 :
3443 523838 : for (const auto & j : constraint_row)
3444 214608 : exact_value += j.second * (*v_local)(j.first);
3445 :
3446 309230 : v_global->set(constrained_dof, exact_value);
3447 : }
3448 :
3449 : // If the old vector was serial, we probably need to send our values
3450 : // to other processors
3451 58513 : if (v.type() == SERIAL)
3452 : {
3453 : #ifndef NDEBUG
3454 0 : v_global->close();
3455 : #endif
3456 99 : v_global->localize (v);
3457 : }
3458 58513 : v.close();
3459 54877 : }
3460 :
3461 :
3462 :
3463 : std::pair<Real, Real>
3464 0 : DofMap::max_constraint_error (const System & system,
3465 : NumericVector<Number> * v) const
3466 : {
3467 0 : if (!v)
3468 0 : v = system.solution.get();
3469 0 : NumericVector<Number> & vec = *v;
3470 :
3471 : // We'll assume the vector is closed
3472 0 : libmesh_assert (vec.closed());
3473 :
3474 0 : Real max_absolute_error = 0., max_relative_error = 0.;
3475 :
3476 0 : const MeshBase & mesh = system.get_mesh();
3477 :
3478 0 : libmesh_assert_equal_to (this, &(system.get_dof_map()));
3479 :
3480 : // indices on each element
3481 0 : std::vector<dof_id_type> local_dof_indices;
3482 :
3483 0 : for (const auto & elem : mesh.active_local_element_ptr_range())
3484 : {
3485 0 : this->dof_indices(elem, local_dof_indices);
3486 0 : std::vector<dof_id_type> raw_dof_indices = local_dof_indices;
3487 :
3488 : // Constraint matrix for each element
3489 0 : DenseMatrix<Number> C;
3490 :
3491 0 : this->build_constraint_matrix (C, local_dof_indices);
3492 :
3493 : // Continue if the element is unconstrained
3494 0 : if (!C.m())
3495 0 : continue;
3496 :
3497 0 : libmesh_assert_equal_to (C.m(), raw_dof_indices.size());
3498 0 : libmesh_assert_equal_to (C.n(), local_dof_indices.size());
3499 :
3500 0 : for (auto i : make_range(C.m()))
3501 : {
3502 : // Recalculate any constrained dof owned by this processor
3503 0 : dof_id_type global_dof = raw_dof_indices[i];
3504 0 : if (this->is_constrained_dof(global_dof) &&
3505 0 : global_dof >= vec.first_local_index() &&
3506 0 : global_dof < vec.last_local_index())
3507 : {
3508 : #ifndef NDEBUG
3509 : DofConstraints::const_iterator
3510 0 : pos = _dof_constraints.find(global_dof);
3511 :
3512 0 : libmesh_assert (pos != _dof_constraints.end());
3513 : #endif
3514 :
3515 0 : Number exact_value = 0;
3516 : DofConstraintValueMap::const_iterator rhsit =
3517 0 : _primal_constraint_values.find(global_dof);
3518 0 : if (rhsit != _primal_constraint_values.end())
3519 0 : exact_value = rhsit->second;
3520 :
3521 0 : for (auto j : make_range(C.n()))
3522 : {
3523 0 : if (local_dof_indices[j] != global_dof)
3524 0 : exact_value += C(i,j) *
3525 0 : vec(local_dof_indices[j]);
3526 : }
3527 :
3528 0 : max_absolute_error = std::max(max_absolute_error,
3529 0 : std::abs(vec(global_dof) - exact_value));
3530 0 : max_relative_error = std::max(max_relative_error,
3531 0 : std::abs(vec(global_dof) - exact_value)
3532 0 : / std::abs(exact_value));
3533 : }
3534 : }
3535 0 : }
3536 :
3537 0 : return std::pair<Real, Real>(max_absolute_error, max_relative_error);
3538 : }
3539 :
3540 :
3541 :
3542 65679454 : void DofMap::build_constraint_matrix (DenseMatrix<Number> & C,
3543 : std::vector<dof_id_type> & elem_dofs,
3544 : const bool called_recursively) const
3545 : {
3546 15069454 : LOG_SCOPE_IF("build_constraint_matrix()", "DofMap", !called_recursively);
3547 :
3548 : // Create a set containing the DOFs we already depend on
3549 : typedef std::set<dof_id_type> RCSet;
3550 7342587 : RCSet dof_set;
3551 :
3552 7342587 : bool we_have_constraints = false;
3553 :
3554 : // Next insert any other dofs the current dofs might be constrained
3555 : // in terms of. Note that in this case we may not be done: Those
3556 : // may in turn depend on others. So, we need to repeat this process
3557 : // in that case until the system depends only on unconstrained
3558 : // degrees of freedom.
3559 434765930 : for (const auto & dof : elem_dofs)
3560 369086476 : if (this->is_constrained_dof(dof))
3561 : {
3562 2650472 : we_have_constraints = true;
3563 :
3564 : // If the DOF is constrained
3565 : DofConstraints::const_iterator
3566 2650472 : pos = _dof_constraints.find(dof);
3567 :
3568 2650472 : libmesh_assert (pos != _dof_constraints.end());
3569 :
3570 2650472 : const DofConstraintRow & constraint_row = pos->second;
3571 :
3572 : // Constraint rows in p refinement may be empty
3573 : //libmesh_assert (!constraint_row.empty());
3574 :
3575 63914338 : for (const auto & item : constraint_row)
3576 34899706 : dof_set.insert (item.first);
3577 : }
3578 :
3579 : // May be safe to return at this point
3580 : // (but remember to stop the perflog)
3581 65679454 : if (!we_have_constraints)
3582 6748799 : return;
3583 :
3584 80065273 : for (const auto & dof : elem_dofs)
3585 6621562 : dof_set.erase (dof);
3586 :
3587 : // If we added any DOFS then we need to do this recursively.
3588 : // It is possible that we just added a DOF that is also
3589 : // constrained!
3590 : //
3591 : // Also, we need to handle the special case of an element having DOFs
3592 : // constrained in terms of other, local DOFs
3593 6964316 : if (!dof_set.empty() || // case 1: constrained in terms of other DOFs
3594 498730 : !called_recursively) // case 2: constrained in terms of our own DOFs
3595 : {
3596 : const unsigned int old_size =
3597 593584 : cast_int<unsigned int>(elem_dofs.size());
3598 :
3599 : // Add new dependency dofs to the end of the current dof set
3600 2936103 : elem_dofs.insert(elem_dofs.end(),
3601 890478 : dof_set.begin(), dof_set.end());
3602 :
3603 : // Now we can build the constraint matrix.
3604 : // Note that resize also zeros for a DenseMatrix<Number>.
3605 3232793 : C.resize (old_size,
3606 : cast_int<unsigned int>(elem_dofs.size()));
3607 :
3608 : // Create the C constraint matrix.
3609 36880722 : for (unsigned int i=0; i != old_size; i++)
3610 36648891 : if (this->is_constrained_dof(elem_dofs[i]))
3611 : {
3612 : // If the DOF is constrained
3613 : DofConstraints::const_iterator
3614 1325236 : pos = _dof_constraints.find(elem_dofs[i]);
3615 :
3616 1325236 : libmesh_assert (pos != _dof_constraints.end());
3617 :
3618 1325236 : const DofConstraintRow & constraint_row = pos->second;
3619 :
3620 : // p refinement creates empty constraint rows
3621 : // libmesh_assert (!constraint_row.empty());
3622 :
3623 31957169 : for (const auto & item : constraint_row)
3624 887628164 : for (unsigned int j=0,
3625 3377724 : n_elem_dofs = cast_int<unsigned int>(elem_dofs.size());
3626 891005888 : j != n_elem_dofs; j++)
3627 959679560 : if (elem_dofs[j] == item.first)
3628 19138511 : C(i,j) = item.second;
3629 : }
3630 : else
3631 : {
3632 18046418 : C(i,i) = 1.;
3633 : }
3634 :
3635 : // May need to do this recursively. It is possible
3636 : // that we just replaced a constrained DOF with another
3637 : // constrained DOF.
3638 3826377 : DenseMatrix<Number> Cnew;
3639 :
3640 3232793 : this->build_constraint_matrix (Cnew, elem_dofs, true);
3641 :
3642 3232793 : if ((C.n() == Cnew.m()) &&
3643 0 : (Cnew.n() == elem_dofs.size())) // If the constraint matrix
3644 0 : C.right_multiply(Cnew); // is constrained...
3645 :
3646 296894 : libmesh_assert_equal_to (C.n(), elem_dofs.size());
3647 2639209 : }
3648 : }
3649 :
3650 :
3651 :
3652 837306 : void DofMap::build_constraint_matrix_and_vector (DenseMatrix<Number> & C,
3653 : DenseVector<Number> & H,
3654 : std::vector<dof_id_type> & elem_dofs,
3655 : int qoi_index,
3656 : const bool called_recursively) const
3657 : {
3658 201210 : LOG_SCOPE_IF("build_constraint_matrix_and_vector()", "DofMap", !called_recursively);
3659 :
3660 : // Create a set containing the DOFs we already depend on
3661 : typedef std::set<dof_id_type> RCSet;
3662 83848 : RCSet dof_set;
3663 :
3664 83848 : bool we_have_constraints = false;
3665 :
3666 : // Next insert any other dofs the current dofs might be constrained
3667 : // in terms of. Note that in this case we may not be done: Those
3668 : // may in turn depend on others. So, we need to repeat this process
3669 : // in that case until the system depends only on unconstrained
3670 : // degrees of freedom.
3671 17100989 : for (const auto & dof : elem_dofs)
3672 16263683 : if (this->is_constrained_dof(dof))
3673 : {
3674 177436 : we_have_constraints = true;
3675 :
3676 : // If the DOF is constrained
3677 : DofConstraints::const_iterator
3678 177436 : pos = _dof_constraints.find(dof);
3679 :
3680 177436 : libmesh_assert (pos != _dof_constraints.end());
3681 :
3682 177436 : const DofConstraintRow & constraint_row = pos->second;
3683 :
3684 : // Constraint rows in p refinement may be empty
3685 : //libmesh_assert (!constraint_row.empty());
3686 :
3687 2097020 : for (const auto & item : constraint_row)
3688 259132 : dof_set.insert (item.first);
3689 : }
3690 :
3691 : // May be safe to return at this point
3692 : // (but remember to stop the perflog)
3693 837306 : if (!we_have_constraints)
3694 50334 : return;
3695 :
3696 6372443 : for (const auto & dof : elem_dofs)
3697 588723 : dof_set.erase (dof);
3698 :
3699 : // If we added any DOFS then we need to do this recursively.
3700 : // It is possible that we just added a DOF that is also
3701 : // constrained!
3702 : //
3703 : // Also, we need to handle the special case of an element having DOFs
3704 : // constrained in terms of other, local DOFs
3705 386510 : if (!dof_set.empty() || // case 1: constrained in terms of other DOFs
3706 30726 : !called_recursively) // case 2: constrained in terms of our own DOFs
3707 : {
3708 16757 : const DofConstraintValueMap * rhs_values = nullptr;
3709 177892 : if (qoi_index < 0)
3710 176212 : rhs_values = &_primal_constraint_values;
3711 1820 : else if (auto it = _adjoint_constraint_values.find(qoi_index);
3712 140 : it != _adjoint_constraint_values.end())
3713 1680 : rhs_values = &it->second;
3714 :
3715 : const unsigned int old_size =
3716 33514 : cast_int<unsigned int>(elem_dofs.size());
3717 :
3718 : // Add new dependency dofs to the end of the current dof set
3719 161135 : elem_dofs.insert(elem_dofs.end(),
3720 50271 : dof_set.begin(), dof_set.end());
3721 :
3722 : // Now we can build the constraint matrix and vector.
3723 : // Note that resize also zeros for a DenseMatrix and DenseVector
3724 177892 : C.resize (old_size,
3725 : cast_int<unsigned int>(elem_dofs.size()));
3726 161135 : H.resize (old_size);
3727 :
3728 : // Create the C constraint matrix.
3729 3150597 : for (unsigned int i=0; i != old_size; i++)
3730 3264157 : if (this->is_constrained_dof(elem_dofs[i]))
3731 : {
3732 : // If the DOF is constrained
3733 : DofConstraints::const_iterator
3734 88718 : pos = _dof_constraints.find(elem_dofs[i]);
3735 :
3736 88718 : libmesh_assert (pos != _dof_constraints.end());
3737 :
3738 88718 : const DofConstraintRow & constraint_row = pos->second;
3739 :
3740 : // p refinement creates empty constraint rows
3741 : // libmesh_assert (!constraint_row.empty());
3742 :
3743 1048510 : for (const auto & item : constraint_row)
3744 1841796 : for (unsigned int j=0,
3745 21268 : n_elem_dofs = cast_int<unsigned int>(elem_dofs.size());
3746 1863064 : j != n_elem_dofs; j++)
3747 1876473 : if (elem_dofs[j] == item.first)
3748 140200 : C(i,j) = item.second;
3749 :
3750 918944 : if (rhs_values)
3751 : {
3752 918944 : if (const auto rhsit = rhs_values->find(elem_dofs[i]);
3753 88718 : rhsit != rhs_values->end())
3754 297173 : H(i) = rhsit->second;
3755 : }
3756 : }
3757 : else
3758 : {
3759 1910998 : C(i,i) = 1.;
3760 : }
3761 :
3762 : // May need to do this recursively. It is possible
3763 : // that we just replaced a constrained DOF with another
3764 : // constrained DOF.
3765 211406 : DenseMatrix<Number> Cnew;
3766 177892 : DenseVector<Number> Hnew;
3767 :
3768 177892 : this->build_constraint_matrix_and_vector (Cnew, Hnew, elem_dofs,
3769 : qoi_index, true);
3770 :
3771 177892 : if ((C.n() == Cnew.m()) && // If the constraint matrix
3772 0 : (Cnew.n() == elem_dofs.size())) // is constrained...
3773 : {
3774 : // If x = Cy + h and y = Dz + g
3775 : // Then x = (CD)z + (Cg + h)
3776 0 : C.vector_mult_add(H, 1, Hnew);
3777 :
3778 0 : C.right_multiply(Cnew);
3779 : }
3780 :
3781 16757 : libmesh_assert_equal_to (C.n(), elem_dofs.size());
3782 144378 : }
3783 : }
3784 :
3785 :
3786 407387 : void DofMap::allgather_recursive_constraints(MeshBase & mesh)
3787 : {
3788 : // This function must be run on all processors at once
3789 11642 : parallel_object_only();
3790 :
3791 : // Return immediately if there's nothing to gather
3792 419029 : if (this->n_processors() == 1)
3793 372787 : return;
3794 :
3795 : // We might get to return immediately if none of the processors
3796 : // found any constraints
3797 384477 : unsigned int has_constraints = !_dof_constraints.empty()
3798 : #ifdef LIBMESH_ENABLE_NODE_CONSTRAINTS
3799 23579 : || !_node_constraints.empty()
3800 : #endif // LIBMESH_ENABLE_NODE_CONSTRAINTS
3801 : ;
3802 384477 : this->comm().max(has_constraints);
3803 396119 : if (!has_constraints)
3804 10218 : return;
3805 :
3806 : // If we have heterogeneous adjoint constraints we need to
3807 : // communicate those too.
3808 : const unsigned int max_qoi_num =
3809 34600 : _adjoint_constraint_values.empty() ?
3810 674 : 0 : _adjoint_constraint_values.rbegin()->first+1;
3811 :
3812 : #ifdef LIBMESH_ENABLE_NODE_CONSTRAINTS
3813 : // We may need to send nodes ahead of data about them
3814 4272 : std::vector<Parallel::Request> packed_range_sends;
3815 :
3816 : // We may be receiving packed_range sends out of order with
3817 : // parallel_sync tags, so make sure they're received correctly.
3818 5696 : Parallel::MessageTag range_tag = this->comm().get_unique_tag();
3819 :
3820 : // We only need to do these sends on a distributed mesh
3821 2848 : const bool dist_mesh = !mesh.is_serial();
3822 : #endif
3823 :
3824 : // We might have calculated constraints for constrained dofs
3825 : // which have support on other processors.
3826 : // Push these out first.
3827 : {
3828 2848 : std::map<processor_id_type, std::set<dof_id_type>> pushed_ids;
3829 :
3830 : #ifdef LIBMESH_ENABLE_NODE_CONSTRAINTS
3831 2848 : std::map<processor_id_type, std::set<dof_id_type>> pushed_node_ids;
3832 : #endif
3833 :
3834 2848 : const unsigned int sys_num = this->sys_number();
3835 :
3836 : // Collect the constraints to push to each processor
3837 327134 : for (auto & elem : as_range(mesh.active_not_local_elements_begin(),
3838 12310872 : mesh.active_not_local_elements_end()))
3839 : {
3840 6217339 : const unsigned short n_nodes = elem->n_nodes();
3841 :
3842 : // Just checking dof_indices on the foreign element isn't
3843 : // enough. Consider a central hanging node between a coarse
3844 : // Q2/Q1 element and its finer neighbors on a higher-ranked
3845 : // processor. The coarse element's processor will own the node,
3846 : // and will thereby own the pressure dof on that node, despite
3847 : // the fact that that pressure dof doesn't directly exist on the
3848 : // coarse element!
3849 : //
3850 : // So, we loop through dofs manually.
3851 :
3852 : {
3853 6217339 : const unsigned int n_vars = elem->n_vars(sys_num);
3854 14209719 : for (unsigned int v=0; v != n_vars; ++v)
3855 : {
3856 7992380 : const unsigned int n_comp = elem->n_comp(sys_num,v);
3857 10557778 : for (unsigned int c=0; c != n_comp; ++c)
3858 : {
3859 : const unsigned int id =
3860 2565398 : elem->dof_number(sys_num,v,c);
3861 2498758 : if (this->is_constrained_dof(id))
3862 0 : pushed_ids[elem->processor_id()].insert(id);
3863 : }
3864 : }
3865 : }
3866 :
3867 35445792 : for (unsigned short n = 0; n != n_nodes; ++n)
3868 : {
3869 29228453 : const Node & node = elem->node_ref(n);
3870 29228453 : const unsigned int n_vars = node.n_vars(sys_num);
3871 69268211 : for (unsigned int v=0; v != n_vars; ++v)
3872 : {
3873 40039758 : const unsigned int n_comp = node.n_comp(sys_num,v);
3874 82928385 : for (unsigned int c=0; c != n_comp; ++c)
3875 : {
3876 : const unsigned int id =
3877 42888627 : node.dof_number(sys_num,v,c);
3878 40914770 : if (this->is_constrained_dof(id))
3879 260971 : pushed_ids[elem->processor_id()].insert(id);
3880 : }
3881 : }
3882 : }
3883 :
3884 : #ifdef LIBMESH_ENABLE_NODE_CONSTRAINTS
3885 3354176 : for (unsigned short n = 0; n != n_nodes; ++n)
3886 4242393 : if (this->is_constrained_node(elem->node_ptr(n)))
3887 32391 : pushed_node_ids[elem->processor_id()].insert(elem->node_id(n));
3888 : #endif
3889 31752 : }
3890 :
3891 : // Rewrite those id sets as vectors for sending and receiving,
3892 : // then find the corresponding data for each id, then push it all.
3893 : std::map<processor_id_type, std::vector<dof_id_type>>
3894 2848 : pushed_id_vecs, received_id_vecs;
3895 77163 : for (auto & p : pushed_ids)
3896 42563 : pushed_id_vecs[p.first].assign(p.second.begin(), p.second.end());
3897 :
3898 : std::map<processor_id_type, std::vector<std::vector<std::pair<dof_id_type,Real>>>>
3899 2848 : pushed_keys_vals, received_keys_vals;
3900 2848 : std::map<processor_id_type, std::vector<std::vector<Number>>> pushed_rhss, received_rhss;
3901 77163 : for (auto & p : pushed_id_vecs)
3902 : {
3903 42563 : auto & keys_vals = pushed_keys_vals[p.first];
3904 43276 : keys_vals.reserve(p.second.size());
3905 :
3906 42563 : auto & rhss = pushed_rhss[p.first];
3907 43276 : rhss.reserve(p.second.size());
3908 207154 : for (auto & pushed_id : p.second)
3909 : {
3910 164591 : const DofConstraintRow & row = _dof_constraints[pushed_id];
3911 168439 : keys_vals.emplace_back(row.begin(), row.end());
3912 :
3913 325334 : rhss.push_back(std::vector<Number>(max_qoi_num+1));
3914 3848 : std::vector<Number> & rhs = rhss.back();
3915 : DofConstraintValueMap::const_iterator rhsit =
3916 3848 : _primal_constraint_values.find(pushed_id);
3917 168439 : rhs[max_qoi_num] =
3918 167248 : (rhsit == _primal_constraint_values.end()) ?
3919 2657 : 0 : rhsit->second;
3920 166497 : for (unsigned int q = 0; q != max_qoi_num; ++q)
3921 : {
3922 : AdjointDofConstraintValues::const_iterator adjoint_map_it =
3923 124 : _adjoint_constraint_values.find(q);
3924 :
3925 1906 : if (adjoint_map_it == _adjoint_constraint_values.end())
3926 0 : continue;
3927 :
3928 : const DofConstraintValueMap & constraint_map =
3929 124 : adjoint_map_it->second;
3930 :
3931 : DofConstraintValueMap::const_iterator adj_rhsit =
3932 124 : constraint_map.find(pushed_id);
3933 :
3934 2030 : rhs[q] =
3935 2124 : (adj_rhsit == constraint_map.end()) ?
3936 94 : 0 : adj_rhsit->second;
3937 : }
3938 : }
3939 : }
3940 :
3941 : auto ids_action_functor =
3942 41137 : [& received_id_vecs]
3943 : (processor_id_type pid,
3944 41850 : const std::vector<dof_id_type> & data)
3945 : {
3946 42563 : received_id_vecs[pid] = data;
3947 35313 : };
3948 :
3949 : Parallel::push_parallel_vector_data
3950 34600 : (this->comm(), pushed_id_vecs, ids_action_functor);
3951 :
3952 : auto keys_vals_action_functor =
3953 41137 : [& received_keys_vals]
3954 : (processor_id_type pid,
3955 41850 : const std::vector<std::vector<std::pair<dof_id_type,Real>>> & data)
3956 : {
3957 42563 : received_keys_vals[pid] = data;
3958 35313 : };
3959 :
3960 : Parallel::push_parallel_vector_data
3961 34600 : (this->comm(), pushed_keys_vals, keys_vals_action_functor);
3962 :
3963 : auto rhss_action_functor =
3964 41137 : [& received_rhss]
3965 : (processor_id_type pid,
3966 41850 : const std::vector<std::vector<Number>> & data)
3967 : {
3968 42563 : received_rhss[pid] = data;
3969 35313 : };
3970 :
3971 : Parallel::push_parallel_vector_data
3972 34600 : (this->comm(), pushed_rhss, rhss_action_functor);
3973 :
3974 : // Now we have all the DofConstraint rows and rhs values received
3975 : // from others, so add the DoF constraints that we've been sent
3976 :
3977 : #ifdef LIBMESH_ENABLE_NODE_CONSTRAINTS
3978 : std::map<processor_id_type, std::vector<dof_id_type>>
3979 2848 : pushed_node_id_vecs, received_node_id_vecs;
3980 4476 : for (auto & p : pushed_node_ids)
3981 1628 : pushed_node_id_vecs[p.first].assign(p.second.begin(), p.second.end());
3982 :
3983 : std::map<processor_id_type, std::vector<std::vector<std::pair<dof_id_type,Real>>>>
3984 2848 : pushed_node_keys_vals, received_node_keys_vals;
3985 2848 : std::map<processor_id_type, std::vector<Point>> pushed_offsets, received_offsets;
3986 :
3987 4476 : for (auto & p : pushed_node_id_vecs)
3988 : {
3989 1628 : const processor_id_type pid = p.first;
3990 :
3991 : // FIXME - this could be an unordered set, given a
3992 : // hash<pointers> specialization
3993 1628 : std::set<const Node *> nodes_requested;
3994 :
3995 1628 : auto & node_keys_vals = pushed_node_keys_vals[pid];
3996 2442 : node_keys_vals.reserve(p.second.size());
3997 :
3998 1628 : auto & offsets = pushed_offsets[pid];
3999 2442 : offsets.reserve(p.second.size());
4000 :
4001 12648 : for (auto & pushed_node_id : p.second)
4002 : {
4003 11020 : const Node * node = mesh.node_ptr(pushed_node_id);
4004 11020 : NodeConstraintRow & row = _node_constraints[node].first;
4005 5510 : const std::size_t row_size = row.size();
4006 : node_keys_vals.push_back
4007 11020 : (std::vector<std::pair<dof_id_type,Real>>());
4008 : std::vector<std::pair<dof_id_type,Real>> & this_node_kv =
4009 5510 : node_keys_vals.back();
4010 11020 : this_node_kv.reserve(row_size);
4011 48141 : for (const auto & j : row)
4012 : {
4013 37121 : this_node_kv.emplace_back(j.first->id(), j.second);
4014 :
4015 : // If we're not sure whether our send
4016 : // destination already has this node, let's give
4017 : // it a copy.
4018 37121 : if (j.first->processor_id() != pid && dist_mesh)
4019 0 : nodes_requested.insert(j.first);
4020 : }
4021 :
4022 11020 : offsets.push_back(_node_constraints[node].second);
4023 :
4024 : }
4025 :
4026 : // Constraining nodes might not even exist on our
4027 : // correspondant's subset of a distributed mesh, so let's
4028 : // make them exist.
4029 1628 : if (dist_mesh)
4030 : {
4031 0 : packed_range_sends.push_back(Parallel::Request());
4032 0 : this->comm().send_packed_range
4033 0 : (pid, &mesh, nodes_requested.begin(), nodes_requested.end(),
4034 0 : packed_range_sends.back(), range_tag);
4035 : }
4036 : }
4037 :
4038 : auto node_ids_action_functor =
4039 : [& received_node_id_vecs]
4040 : (processor_id_type pid,
4041 814 : const std::vector<dof_id_type> & data)
4042 : {
4043 1628 : received_node_id_vecs[pid] = data;
4044 3662 : };
4045 :
4046 : Parallel::push_parallel_vector_data
4047 2848 : (this->comm(), pushed_node_id_vecs, node_ids_action_functor);
4048 :
4049 : auto node_keys_vals_action_functor =
4050 : [& received_node_keys_vals]
4051 : (processor_id_type pid,
4052 814 : const std::vector<std::vector<std::pair<dof_id_type,Real>>> & data)
4053 : {
4054 1628 : received_node_keys_vals[pid] = data;
4055 3662 : };
4056 :
4057 : Parallel::push_parallel_vector_data
4058 2848 : (this->comm(), pushed_node_keys_vals,
4059 : node_keys_vals_action_functor);
4060 :
4061 : auto node_offsets_action_functor =
4062 : [& received_offsets]
4063 : (processor_id_type pid,
4064 814 : const std::vector<Point> & data)
4065 : {
4066 1628 : received_offsets[pid] = data;
4067 3662 : };
4068 :
4069 : Parallel::push_parallel_vector_data
4070 2848 : (this->comm(), pushed_offsets, node_offsets_action_functor);
4071 :
4072 : #endif
4073 :
4074 : // Add all the dof constraints that I've been sent
4075 77163 : for (auto & [pid, pushed_ids_to_me] : received_id_vecs)
4076 : {
4077 713 : libmesh_assert(received_keys_vals.count(pid));
4078 713 : libmesh_assert(received_rhss.count(pid));
4079 42563 : const auto & pushed_keys_vals_to_me = received_keys_vals.at(pid);
4080 42563 : const auto & pushed_rhss_to_me = received_rhss.at(pid);
4081 :
4082 713 : libmesh_assert_equal_to (pushed_ids_to_me.size(),
4083 : pushed_keys_vals_to_me.size());
4084 713 : libmesh_assert_equal_to (pushed_ids_to_me.size(),
4085 : pushed_rhss_to_me.size());
4086 :
4087 207154 : for (auto i : index_range(pushed_ids_to_me))
4088 : {
4089 168439 : dof_id_type constrained = pushed_ids_to_me[i];
4090 :
4091 : // If we don't already have a constraint for this dof,
4092 : // add the one we were sent
4093 108846 : if (!this->is_constrained_dof(constrained))
4094 : {
4095 53759 : DofConstraintRow & row = _dof_constraints[constrained];
4096 194308 : for (auto & kv : pushed_keys_vals_to_me[i])
4097 : {
4098 1671 : libmesh_assert_less(kv.first, this->n_dofs());
4099 139618 : row[kv.first] = kv.second;
4100 : }
4101 :
4102 54690 : const Number primal_rhs = pushed_rhss_to_me[i][max_qoi_num];
4103 :
4104 931 : if (libmesh_isnan(primal_rhs))
4105 0 : libmesh_assert(pushed_keys_vals_to_me[i].empty());
4106 :
4107 53759 : if (primal_rhs != Number(0))
4108 11290 : _primal_constraint_values[constrained] = primal_rhs;
4109 : else
4110 541 : _primal_constraint_values.erase(constrained);
4111 :
4112 53759 : for (unsigned int q = 0; q != max_qoi_num; ++q)
4113 : {
4114 : AdjointDofConstraintValues::iterator adjoint_map_it =
4115 0 : _adjoint_constraint_values.find(q);
4116 :
4117 0 : const Number adj_rhs = pushed_rhss_to_me[i][q];
4118 :
4119 0 : if ((adjoint_map_it == _adjoint_constraint_values.end()) &&
4120 : adj_rhs == Number(0))
4121 0 : continue;
4122 :
4123 0 : if (adjoint_map_it == _adjoint_constraint_values.end())
4124 0 : adjoint_map_it = _adjoint_constraint_values.emplace
4125 0 : (q, DofConstraintValueMap()).first;
4126 :
4127 : DofConstraintValueMap & constraint_map =
4128 0 : adjoint_map_it->second;
4129 :
4130 0 : if (adj_rhs != Number(0))
4131 0 : constraint_map[constrained] = adj_rhs;
4132 : else
4133 0 : constraint_map.erase(constrained);
4134 : }
4135 : }
4136 : }
4137 : }
4138 :
4139 : #ifdef LIBMESH_ENABLE_NODE_CONSTRAINTS
4140 : // Add all the node constraints that I've been sent
4141 4476 : for (auto & [pid, pushed_node_ids_to_me] : received_node_id_vecs)
4142 : {
4143 : // Before we act on any new constraint rows, we may need to
4144 : // make sure we have all the nodes involved!
4145 1628 : if (dist_mesh)
4146 0 : this->comm().receive_packed_range
4147 0 : (pid, &mesh, null_output_iterator<Node>(),
4148 : (Node**)nullptr, range_tag);
4149 :
4150 814 : libmesh_assert(received_node_keys_vals.count(pid));
4151 814 : libmesh_assert(received_offsets.count(pid));
4152 1628 : const auto & pushed_node_keys_vals_to_me = received_node_keys_vals.at(pid);
4153 1628 : const auto & pushed_offsets_to_me = received_offsets.at(pid);
4154 :
4155 814 : libmesh_assert_equal_to (pushed_node_ids_to_me.size(),
4156 : pushed_node_keys_vals_to_me.size());
4157 814 : libmesh_assert_equal_to (pushed_node_ids_to_me.size(),
4158 : pushed_offsets_to_me.size());
4159 :
4160 12648 : for (auto i : index_range(pushed_node_ids_to_me))
4161 : {
4162 11020 : dof_id_type constrained_id = pushed_node_ids_to_me[i];
4163 :
4164 : // If we don't already have a constraint for this node,
4165 : // add the one we were sent
4166 11020 : const Node * constrained = mesh.node_ptr(constrained_id);
4167 9282 : if (!this->is_constrained_node(constrained))
4168 : {
4169 3476 : NodeConstraintRow & row = _node_constraints[constrained].first;
4170 18258 : for (auto & kv : pushed_node_keys_vals_to_me[i])
4171 : {
4172 13044 : const Node * key_node = mesh.node_ptr(kv.first);
4173 5661 : libmesh_assert(key_node);
4174 13044 : row[key_node] = kv.second;
4175 : }
4176 5214 : _node_constraints[constrained].second = pushed_offsets_to_me[i];
4177 : }
4178 : }
4179 : }
4180 : #endif // LIBMESH_ENABLE_NODE_CONSTRAINTS
4181 : }
4182 :
4183 : // Now start checking for any other constraints we need
4184 : // to know about, requesting them recursively.
4185 :
4186 : // Create sets containing the DOFs and nodes we already depend on
4187 : typedef std::set<dof_id_type> DoF_RCSet;
4188 2848 : DoF_RCSet unexpanded_dofs;
4189 :
4190 1052132 : for (const auto & i : _dof_constraints)
4191 1017532 : unexpanded_dofs.insert(i.first);
4192 :
4193 : // Gather all the dof constraints we need
4194 34600 : this->gather_constraints(mesh, unexpanded_dofs, false);
4195 :
4196 : // Gather all the node constraints we need
4197 : #ifdef LIBMESH_ENABLE_NODE_CONSTRAINTS
4198 : typedef std::set<const Node *> Node_RCSet;
4199 2848 : Node_RCSet unexpanded_nodes;
4200 :
4201 179096 : for (const auto & i : _node_constraints)
4202 176248 : unexpanded_nodes.insert(i.first);
4203 :
4204 : // We have to keep recursing while the unexpanded set is
4205 : // nonempty on *any* processor
4206 2848 : bool unexpanded_set_nonempty = !unexpanded_nodes.empty();
4207 2848 : this->comm().max(unexpanded_set_nonempty);
4208 :
4209 20528 : while (unexpanded_set_nonempty)
4210 : {
4211 : // Let's make sure we don't lose sync in this loop.
4212 8840 : parallel_object_only();
4213 :
4214 : // Request sets
4215 17680 : Node_RCSet node_request_set;
4216 :
4217 : // Request sets to send to each processor
4218 : std::map<processor_id_type, std::vector<dof_id_type>>
4219 17680 : requested_node_ids;
4220 :
4221 : // And the sizes of each
4222 17680 : std::map<processor_id_type, dof_id_type> node_ids_on_proc;
4223 :
4224 : // Fill (and thereby sort and uniq!) the main request sets
4225 235414 : for (const auto & i : unexpanded_nodes)
4226 : {
4227 217734 : NodeConstraintRow & row = _node_constraints[i].first;
4228 967159 : for (const auto & j : row)
4229 : {
4230 749425 : const Node * const node = j.first;
4231 344116 : libmesh_assert(node);
4232 :
4233 : // If it's non-local and we haven't already got a
4234 : // constraint for it, we might need to ask for one
4235 1230831 : if ((node->processor_id() != this->processor_id()) &&
4236 76097 : !_node_constraints.count(node))
4237 25543 : node_request_set.insert(node);
4238 : }
4239 : }
4240 :
4241 : // Clear the unexpanded constraint sets; we're about to expand
4242 : // them
4243 8840 : unexpanded_nodes.clear();
4244 :
4245 : // Count requests by processor
4246 59952 : for (const auto & node : node_request_set)
4247 : {
4248 21136 : libmesh_assert(node);
4249 21136 : libmesh_assert_less (node->processor_id(), this->n_processors());
4250 42272 : node_ids_on_proc[node->processor_id()]++;
4251 : }
4252 :
4253 30286 : for (auto pair : node_ids_on_proc)
4254 12606 : requested_node_ids[pair.first].reserve(pair.second);
4255 :
4256 : // Prepare each processor's request set
4257 59952 : for (const auto & node : node_request_set)
4258 42272 : requested_node_ids[node->processor_id()].push_back(node->id());
4259 :
4260 : typedef std::vector<std::pair<dof_id_type, Real>> row_datum;
4261 :
4262 : auto node_row_gather_functor =
4263 : [this,
4264 : & mesh,
4265 : dist_mesh,
4266 : & packed_range_sends,
4267 : & range_tag]
4268 : (processor_id_type pid,
4269 : const std::vector<dof_id_type> & ids,
4270 156518 : std::vector<row_datum> & data)
4271 : {
4272 : // FIXME - this could be an unordered set, given a
4273 : // hash<pointers> specialization
4274 12606 : std::set<const Node *> nodes_requested;
4275 :
4276 : // Fill those requests
4277 12606 : const std::size_t query_size = ids.size();
4278 :
4279 12606 : data.resize(query_size);
4280 54878 : for (std::size_t i=0; i != query_size; ++i)
4281 : {
4282 42272 : dof_id_type constrained_id = ids[i];
4283 42272 : const Node * constrained_node = mesh.node_ptr(constrained_id);
4284 21136 : if (_node_constraints.count(constrained_node))
4285 : {
4286 41486 : const NodeConstraintRow & row = _node_constraints[constrained_node].first;
4287 20743 : std::size_t row_size = row.size();
4288 62229 : data[i].reserve(row_size);
4289 177253 : for (const auto & j : row)
4290 : {
4291 135767 : const Node * node = j.first;
4292 213574 : data[i].emplace_back(node->id(), j.second);
4293 :
4294 : // If we're not sure whether our send
4295 : // destination already has this node, let's give
4296 : // it a copy.
4297 135767 : if (node->processor_id() != pid && dist_mesh)
4298 0 : nodes_requested.insert(node);
4299 :
4300 : // We can have 0 nodal constraint
4301 : // coefficients, where no Lagrange constraint
4302 : // exists but non-Lagrange basis constraints
4303 : // might.
4304 : // libmesh_assert(j.second);
4305 : }
4306 : }
4307 : else
4308 : {
4309 : // We have to distinguish "constraint with no
4310 : // constraining nodes" (e.g. due to user node
4311 : // constraint equations) from "no constraint".
4312 : // We'll use invalid_id for the latter.
4313 1179 : data[i].emplace_back(DofObject::invalid_id, Real(0));
4314 : }
4315 : }
4316 :
4317 : // Constraining nodes might not even exist on our
4318 : // correspondant's subset of a distributed mesh, so let's
4319 : // make them exist.
4320 12606 : if (dist_mesh)
4321 : {
4322 0 : packed_range_sends.push_back(Parallel::Request());
4323 0 : this->comm().send_packed_range
4324 0 : (pid, &mesh, nodes_requested.begin(), nodes_requested.end(),
4325 0 : packed_range_sends.back(), range_tag);
4326 : }
4327 12606 : };
4328 :
4329 : typedef Point node_rhs_datum;
4330 :
4331 : auto node_rhs_gather_functor =
4332 : [this,
4333 : & mesh]
4334 : (processor_id_type,
4335 : const std::vector<dof_id_type> & ids,
4336 159772 : std::vector<node_rhs_datum> & data)
4337 : {
4338 : // Fill those requests
4339 12606 : const std::size_t query_size = ids.size();
4340 :
4341 12606 : data.resize(query_size);
4342 54878 : for (std::size_t i=0; i != query_size; ++i)
4343 : {
4344 42272 : dof_id_type constrained_id = ids[i];
4345 42272 : const Node * constrained_node = mesh.node_ptr(constrained_id);
4346 21136 : if (_node_constraints.count(constrained_node))
4347 41486 : data[i] = _node_constraints[constrained_node].second;
4348 : else
4349 1179 : data[i](0) = std::numeric_limits<Real>::quiet_NaN();
4350 : }
4351 21446 : };
4352 :
4353 : auto node_row_action_functor =
4354 : [this,
4355 : & mesh,
4356 : dist_mesh,
4357 : & range_tag,
4358 : & unexpanded_nodes]
4359 : (processor_id_type pid,
4360 : const std::vector<dof_id_type> & ids,
4361 225938 : const std::vector<row_datum> & data)
4362 : {
4363 : // Before we act on any new constraint rows, we may need to
4364 : // make sure we have all the nodes involved!
4365 12606 : if (dist_mesh)
4366 0 : this->comm().receive_packed_range
4367 0 : (pid, &mesh, null_output_iterator<Node>(),
4368 : (Node**)nullptr, range_tag);
4369 :
4370 : // Add any new constraint rows we've found
4371 12606 : const std::size_t query_size = ids.size();
4372 :
4373 54878 : for (std::size_t i=0; i != query_size; ++i)
4374 : {
4375 42272 : const dof_id_type constrained_id = ids[i];
4376 :
4377 : // An empty row is an constraint with an empty row; for
4378 : // no constraint we use a "no row" placeholder
4379 63408 : if (data[i].empty())
4380 : {
4381 0 : const Node * constrained_node = mesh.node_ptr(constrained_id);
4382 0 : NodeConstraintRow & row = _node_constraints[constrained_node].first;
4383 0 : row.clear();
4384 : }
4385 42272 : else if (data[i][0].first != DofObject::invalid_id)
4386 : {
4387 41486 : const Node * constrained_node = mesh.node_ptr(constrained_id);
4388 41486 : NodeConstraintRow & row = _node_constraints[constrained_node].first;
4389 20743 : row.clear();
4390 197996 : for (auto & pair : data[i])
4391 : {
4392 : const Node * key_node =
4393 135767 : mesh.node_ptr(pair.first);
4394 57960 : libmesh_assert(key_node);
4395 135767 : row[key_node] = pair.second;
4396 : }
4397 :
4398 : // And prepare to check for more recursive constraints
4399 20743 : unexpanded_nodes.insert(constrained_node);
4400 : }
4401 : }
4402 12606 : };
4403 :
4404 : auto node_rhs_action_functor =
4405 : [this,
4406 : & mesh]
4407 : (processor_id_type,
4408 : const std::vector<dof_id_type> & ids,
4409 70104 : const std::vector<node_rhs_datum> & data)
4410 : {
4411 : // Add rhs data for any new node constraint rows we've found
4412 12606 : const std::size_t query_size = ids.size();
4413 :
4414 54878 : for (std::size_t i=0; i != query_size; ++i)
4415 : {
4416 42272 : dof_id_type constrained_id = ids[i];
4417 42272 : const Node * constrained_node = mesh.node_ptr(constrained_id);
4418 :
4419 63408 : if (!libmesh_isnan(data[i](0)))
4420 41486 : _node_constraints[constrained_node].second = data[i];
4421 : else
4422 393 : _node_constraints.erase(constrained_node);
4423 : }
4424 21446 : };
4425 :
4426 : // Now request node constraint rows from other processors
4427 8840 : row_datum * node_row_ex = nullptr;
4428 : Parallel::pull_parallel_vector_data
4429 17680 : (this->comm(), requested_node_ids, node_row_gather_functor,
4430 : node_row_action_functor, node_row_ex);
4431 :
4432 : // And request node constraint right hand sides from other procesors
4433 8840 : node_rhs_datum * node_rhs_ex = nullptr;
4434 : Parallel::pull_parallel_vector_data
4435 17680 : (this->comm(), requested_node_ids, node_rhs_gather_functor,
4436 : node_rhs_action_functor, node_rhs_ex);
4437 :
4438 :
4439 : // We have to keep recursing while the unexpanded set is
4440 : // nonempty on *any* processor
4441 17680 : unexpanded_set_nonempty = !unexpanded_nodes.empty();
4442 17680 : this->comm().max(unexpanded_set_nonempty);
4443 : }
4444 2848 : Parallel::wait(packed_range_sends);
4445 : #endif // LIBMESH_ENABLE_NODE_CONSTRAINTS
4446 : }
4447 :
4448 :
4449 :
4450 407387 : void DofMap::process_constraints (MeshBase & mesh)
4451 : {
4452 : // We've computed our local constraints, but they may depend on
4453 : // non-local constraints that we'll need to take into account.
4454 407387 : this->allgather_recursive_constraints(mesh);
4455 :
4456 407387 : if (_error_on_constraint_loop)
4457 : {
4458 : // Optionally check for constraint loops and throw an error
4459 : // if they're detected. We always do this check below in dbg/devel
4460 : // mode but here we optionally do it in opt mode as well.
4461 71 : check_for_constraint_loops();
4462 : }
4463 :
4464 : // Adjoints will be constrained where the primal is
4465 : // Therefore, we will expand the adjoint_constraint_values
4466 : // map whenever the primal_constraint_values map is expanded
4467 :
4468 : // First, figure out the total number of QoIs
4469 : const unsigned int max_qoi_num =
4470 407316 : _adjoint_constraint_values.empty() ?
4471 712 : 0 : _adjoint_constraint_values.rbegin()->first+1;
4472 :
4473 : // Create a set containing the DOFs we already depend on
4474 : typedef std::set<dof_id_type> RCSet;
4475 23280 : RCSet unexpanded_set;
4476 :
4477 1581988 : for (const auto & i : _dof_constraints)
4478 1174672 : unexpanded_set.insert(i.first);
4479 :
4480 436596 : while (!unexpanded_set.empty())
4481 238402 : for (RCSet::iterator i = unexpanded_set.begin();
4482 1263905 : i != unexpanded_set.end(); /* nothing */)
4483 : {
4484 : // If the DOF is constrained
4485 : DofConstraints::iterator
4486 105211 : pos = _dof_constraints.find(*i);
4487 :
4488 105211 : libmesh_assert (pos != _dof_constraints.end());
4489 :
4490 1234625 : DofConstraintRow & constraint_row = pos->second;
4491 :
4492 : DofConstraintValueMap::iterator rhsit =
4493 105211 : _primal_constraint_values.find(*i);
4494 1284104 : Number constraint_rhs = (rhsit == _primal_constraint_values.end()) ?
4495 91005 : 0 : rhsit->second;
4496 :
4497 : // A vector of DofConstraintValueMaps for each adjoint variable
4498 210422 : std::vector<DofConstraintValueMap::iterator> adjoint_rhs_iterators;
4499 1234625 : adjoint_rhs_iterators.resize(max_qoi_num);
4500 :
4501 : // Another to hold the adjoint constraint rhs
4502 1339836 : std::vector<Number> adjoint_constraint_rhs(max_qoi_num, 0.0);
4503 :
4504 : // Find and gather recursive constraints for each adjoint variable
4505 1255106 : for (auto & adjoint_map : _adjoint_constraint_values)
4506 : {
4507 20481 : const std::size_t q = adjoint_map.first;
4508 20481 : adjoint_rhs_iterators[q] = adjoint_map.second.find(*i);
4509 :
4510 22911 : adjoint_constraint_rhs[q] =
4511 23661 : (adjoint_rhs_iterators[q] == adjoint_map.second.end()) ?
4512 750 : 0 : adjoint_rhs_iterators[q]->second;
4513 : }
4514 :
4515 210422 : std::vector<dof_id_type> constraints_to_expand;
4516 :
4517 3183187 : for (const auto & item : constraint_row)
4518 3503502 : if (item.first != *i && this->is_constrained_dof(item.first))
4519 : {
4520 69556 : unexpanded_set.insert(item.first);
4521 69556 : constraints_to_expand.push_back(item.first);
4522 : }
4523 :
4524 1304181 : for (const auto & expandable : constraints_to_expand)
4525 : {
4526 69556 : const Real this_coef = constraint_row[expandable];
4527 :
4528 : DofConstraints::const_iterator
4529 5622 : subpos = _dof_constraints.find(expandable);
4530 :
4531 5622 : libmesh_assert (subpos != _dof_constraints.end());
4532 :
4533 5622 : const DofConstraintRow & subconstraint_row = subpos->second;
4534 :
4535 74759 : for (const auto & item : subconstraint_row)
4536 : {
4537 : // Assert that the constraint does not form a cycle.
4538 631 : libmesh_assert(item.first != expandable);
4539 5203 : constraint_row[item.first] += item.second * this_coef;
4540 : }
4541 :
4542 69556 : if (auto subrhsit = _primal_constraint_values.find(expandable);
4543 5622 : subrhsit != _primal_constraint_values.end())
4544 20133 : constraint_rhs += subrhsit->second * this_coef;
4545 :
4546 : // Find and gather recursive constraints for each adjoint variable
4547 69556 : for (const auto & adjoint_map : _adjoint_constraint_values)
4548 : {
4549 0 : if (auto adjoint_subrhsit = adjoint_map.second.find(expandable);
4550 0 : adjoint_subrhsit != adjoint_map.second.end())
4551 0 : adjoint_constraint_rhs[adjoint_map.first] += adjoint_subrhsit->second * this_coef;
4552 : }
4553 :
4554 5622 : constraint_row.erase(expandable);
4555 : }
4556 :
4557 1234625 : if (rhsit == _primal_constraint_values.end())
4558 : {
4559 587604 : if (constraint_rhs != Number(0))
4560 11631 : _primal_constraint_values[*i] = constraint_rhs;
4561 : else
4562 54919 : _primal_constraint_values.erase(*i);
4563 : }
4564 : else
4565 : {
4566 576615 : if (constraint_rhs != Number(0))
4567 146803 : rhsit->second = constraint_rhs;
4568 : else
4569 432802 : _primal_constraint_values.erase(rhsit);
4570 : }
4571 :
4572 : // Finally fill in the adjoint constraints for each adjoint variable if possible
4573 1255106 : for (auto & adjoint_map : _adjoint_constraint_values)
4574 : {
4575 20481 : const std::size_t q = adjoint_map.first;
4576 :
4577 22911 : if(adjoint_rhs_iterators[q] == adjoint_map.second.end())
4578 : {
4579 11390 : if (adjoint_constraint_rhs[q] != Number(0))
4580 0 : (adjoint_map.second)[*i] = adjoint_constraint_rhs[q];
4581 : else
4582 1680 : adjoint_map.second.erase(*i);
4583 : }
4584 : else
4585 : {
4586 9281 : if (adjoint_constraint_rhs[q] != Number(0))
4587 6810 : adjoint_rhs_iterators[q]->second = adjoint_constraint_rhs[q];
4588 : else
4589 2106 : adjoint_map.second.erase(adjoint_rhs_iterators[q]);
4590 : }
4591 : }
4592 :
4593 1234625 : if (constraints_to_expand.empty())
4594 1092743 : i = unexpanded_set.erase(i);
4595 : else
4596 3308 : ++i;
4597 : }
4598 :
4599 : // In parallel we can't guarantee that nodes/dofs which constrain
4600 : // others are on processors which are aware of that constraint, yet
4601 : // we need such awareness for sparsity pattern generation. So send
4602 : // other processors any constraints they might need to know about.
4603 407316 : this->scatter_constraints(mesh);
4604 :
4605 : // Now that we have our root constraint dependencies sorted out, add
4606 : // them to the send_list
4607 407316 : this->add_constraints_to_send_list(mesh);
4608 407316 : }
4609 :
4610 :
4611 : #ifdef LIBMESH_ENABLE_CONSTRAINTS
4612 0 : void DofMap::check_for_cyclic_constraints()
4613 : {
4614 : // Eventually make this officially libmesh_deprecated();
4615 0 : check_for_constraint_loops();
4616 0 : }
4617 :
4618 71 : void DofMap::check_for_constraint_loops()
4619 : {
4620 : // Create a set containing the DOFs we already depend on
4621 : typedef std::set<dof_id_type> RCSet;
4622 4 : RCSet unexpanded_set;
4623 :
4624 : // Use dof_constraints_copy in this method so that we don't
4625 : // mess with _dof_constraints.
4626 4 : DofConstraints dof_constraints_copy = _dof_constraints;
4627 :
4628 213 : for (const auto & i : dof_constraints_copy)
4629 142 : unexpanded_set.insert(i.first);
4630 :
4631 71 : while (!unexpanded_set.empty())
4632 73 : for (RCSet::iterator i = unexpanded_set.begin();
4633 142 : i != unexpanded_set.end(); /* nothing */)
4634 : {
4635 : // If the DOF is constrained
4636 : DofConstraints::iterator
4637 4 : pos = dof_constraints_copy.find(*i);
4638 :
4639 4 : libmesh_assert (pos != dof_constraints_copy.end());
4640 :
4641 142 : DofConstraintRow & constraint_row = pos->second;
4642 :
4643 : // Comment out "rhs" parts of this method copied from process_constraints
4644 : // DofConstraintValueMap::iterator rhsit =
4645 : // _primal_constraint_values.find(*i);
4646 : // Number constraint_rhs = (rhsit == _primal_constraint_values.end()) ?
4647 : // 0 : rhsit->second;
4648 :
4649 8 : std::vector<dof_id_type> constraints_to_expand;
4650 :
4651 284 : for (const auto & item : constraint_row)
4652 142 : if (item.first != *i && this->is_constrained_dof(item.first))
4653 : {
4654 142 : unexpanded_set.insert(item.first);
4655 142 : constraints_to_expand.push_back(item.first);
4656 : }
4657 :
4658 213 : for (const auto & expandable : constraints_to_expand)
4659 : {
4660 142 : const Real this_coef = constraint_row[expandable];
4661 :
4662 : DofConstraints::const_iterator
4663 4 : subpos = dof_constraints_copy.find(expandable);
4664 :
4665 4 : libmesh_assert (subpos != dof_constraints_copy.end());
4666 :
4667 4 : const DofConstraintRow & subconstraint_row = subpos->second;
4668 :
4669 213 : for (const auto & item : subconstraint_row)
4670 : {
4671 213 : libmesh_error_msg_if(item.first == expandable, "Constraint loop detected");
4672 :
4673 71 : constraint_row[item.first] += item.second * this_coef;
4674 : }
4675 :
4676 : // Comment out "rhs" parts of this method copied from process_constraints
4677 : // DofConstraintValueMap::const_iterator subrhsit =
4678 : // _primal_constraint_values.find(expandable);
4679 : // if (subrhsit != _primal_constraint_values.end())
4680 : // constraint_rhs += subrhsit->second * this_coef;
4681 :
4682 2 : constraint_row.erase(expandable);
4683 : }
4684 :
4685 : // Comment out "rhs" parts of this method copied from process_constraints
4686 : // if (rhsit == _primal_constraint_values.end())
4687 : // {
4688 : // if (constraint_rhs != Number(0))
4689 : // _primal_constraint_values[*i] = constraint_rhs;
4690 : // else
4691 : // _primal_constraint_values.erase(*i);
4692 : // }
4693 : // else
4694 : // {
4695 : // if (constraint_rhs != Number(0))
4696 : // rhsit->second = constraint_rhs;
4697 : // else
4698 : // _primal_constraint_values.erase(rhsit);
4699 : // }
4700 :
4701 71 : if (constraints_to_expand.empty())
4702 0 : i = unexpanded_set.erase(i);
4703 : else
4704 2 : ++i;
4705 : }
4706 0 : }
4707 : #else
4708 : void DofMap::check_for_constraint_loops() {}
4709 : void DofMap::check_for_cyclic_constraints()
4710 : {
4711 : // Do nothing
4712 : }
4713 : #endif
4714 :
4715 :
4716 407316 : void DofMap::scatter_constraints(MeshBase & mesh)
4717 : {
4718 : // At this point each processor with a constrained node knows
4719 : // the corresponding constraint row, but we also need each processor
4720 : // with a constrainer node to know the corresponding row(s).
4721 :
4722 : // This function must be run on all processors at once
4723 11640 : parallel_object_only();
4724 :
4725 : // Return immediately if there's nothing to gather
4726 418956 : if (this->n_processors() == 1)
4727 372785 : return;
4728 :
4729 : // We might get to return immediately if none of the processors
4730 : // found any constraints
4731 384410 : unsigned int has_constraints = !_dof_constraints.empty()
4732 : #ifdef LIBMESH_ENABLE_NODE_CONSTRAINTS
4733 23587 : || !_node_constraints.empty()
4734 : #endif // LIBMESH_ENABLE_NODE_CONSTRAINTS
4735 : ;
4736 384410 : this->comm().max(has_constraints);
4737 396050 : if (!has_constraints)
4738 10218 : return;
4739 :
4740 : // We may be receiving packed_range sends out of order with
4741 : // parallel_sync tags, so make sure they're received correctly.
4742 37375 : Parallel::MessageTag range_tag = this->comm().get_unique_tag();
4743 :
4744 : #ifdef LIBMESH_ENABLE_NODE_CONSTRAINTS
4745 2844 : std::map<processor_id_type, std::set<dof_id_type>> pushed_node_ids;
4746 : #endif // LIBMESH_ENABLE_NODE_CONSTRAINTS
4747 :
4748 2844 : std::map<processor_id_type, std::set<dof_id_type>> pushed_ids;
4749 :
4750 : // Collect the dof constraints I need to push to each processor
4751 1422 : dof_id_type constrained_proc_id = 0;
4752 1054196 : for (const auto & [constrained, row] : _dof_constraints)
4753 : {
4754 1216823 : while (constrained >= _end_df[constrained_proc_id])
4755 96282 : constrained_proc_id++;
4756 :
4757 1120007 : if (constrained_proc_id != this->processor_id())
4758 104146 : continue;
4759 :
4760 2307105 : for (auto & j : row)
4761 : {
4762 1394243 : const dof_id_type constraining = j.first;
4763 :
4764 1394243 : processor_id_type constraining_proc_id = 0;
4765 5270094 : while (constraining >= _end_df[constraining_proc_id])
4766 3648171 : constraining_proc_id++;
4767 :
4768 1575857 : if (constraining_proc_id != this->processor_id() &&
4769 27473 : constraining_proc_id != constrained_proc_id)
4770 227041 : pushed_ids[constraining_proc_id].insert(constrained);
4771 : }
4772 : }
4773 :
4774 : // Pack the dof constraint rows and rhs's to push
4775 :
4776 : std::map<processor_id_type,
4777 : std::vector<std::vector<std::pair<dof_id_type, Real>>>>
4778 2844 : pushed_keys_vals, pushed_keys_vals_to_me;
4779 :
4780 : std::map<processor_id_type, std::vector<std::pair<dof_id_type, Number>>>
4781 2844 : pushed_ids_rhss, pushed_ids_rhss_to_me;
4782 :
4783 : auto gather_ids =
4784 63374 : [this,
4785 : & pushed_ids,
4786 : & pushed_keys_vals,
4787 : & pushed_ids_rhss]
4788 319387 : ()
4789 : {
4790 110958 : for (const auto & [pid, pid_ids] : pushed_ids)
4791 : {
4792 729 : const std::size_t ids_size = pid_ids.size();
4793 : std::vector<std::vector<std::pair<dof_id_type, Real>>> &
4794 41896 : keys_vals = pushed_keys_vals[pid];
4795 : std::vector<std::pair<dof_id_type,Number>> &
4796 41896 : ids_rhss = pushed_ids_rhss[pid];
4797 41896 : keys_vals.resize(ids_size);
4798 41896 : ids_rhss.resize(ids_size);
4799 :
4800 : std::size_t push_i;
4801 729 : std::set<dof_id_type>::const_iterator it;
4802 84245 : for (push_i = 0, it = pid_ids.begin();
4803 313903 : it != pid_ids.end(); ++push_i, ++it)
4804 : {
4805 272007 : const dof_id_type constrained = *it;
4806 272007 : DofConstraintRow & row = _dof_constraints[constrained];
4807 272007 : keys_vals[push_i].assign(row.begin(), row.end());
4808 :
4809 : DofConstraintValueMap::const_iterator rhsit =
4810 21539 : _primal_constraint_values.find(constrained);
4811 272007 : ids_rhss[push_i].first = constrained;
4812 272007 : ids_rhss[push_i].second =
4813 293880 : (rhsit == _primal_constraint_values.end()) ?
4814 334 : 0 : rhsit->second;
4815 : }
4816 : }
4817 69062 : };
4818 :
4819 34531 : gather_ids();
4820 :
4821 : auto ids_rhss_action_functor =
4822 40438 : [& pushed_ids_rhss_to_me]
4823 : (processor_id_type pid,
4824 41167 : const std::vector<std::pair<dof_id_type, Number>> & data)
4825 : {
4826 41896 : pushed_ids_rhss_to_me[pid] = data;
4827 75698 : };
4828 :
4829 : auto keys_vals_action_functor =
4830 40438 : [& pushed_keys_vals_to_me]
4831 : (processor_id_type pid,
4832 41167 : const std::vector<std::vector<std::pair<dof_id_type, Real>>> & data)
4833 : {
4834 41896 : pushed_keys_vals_to_me[pid] = data;
4835 35260 : };
4836 :
4837 : Parallel::push_parallel_vector_data
4838 34531 : (this->comm(), pushed_ids_rhss, ids_rhss_action_functor);
4839 : Parallel::push_parallel_vector_data
4840 34531 : (this->comm(), pushed_keys_vals, keys_vals_action_functor);
4841 :
4842 : // Now work on traded dof constraint rows
4843 : auto receive_dof_constraints =
4844 63374 : [this,
4845 : & pushed_ids_rhss_to_me,
4846 : & pushed_keys_vals_to_me]
4847 283481 : ()
4848 : {
4849 110958 : for (const auto & [pid, ids_rhss] : pushed_ids_rhss_to_me)
4850 : {
4851 41896 : const auto & keys_vals = pushed_keys_vals_to_me[pid];
4852 :
4853 729 : libmesh_assert_equal_to
4854 : (ids_rhss.size(), keys_vals.size());
4855 :
4856 : // Add the dof constraints that I've been sent
4857 313903 : for (auto i : index_range(ids_rhss))
4858 : {
4859 293546 : dof_id_type constrained = ids_rhss[i].first;
4860 :
4861 : // If we don't already have a constraint for this dof,
4862 : // add the one we were sent
4863 94361 : if (!this->is_constrained_dof(constrained))
4864 : {
4865 196507 : DofConstraintRow & row = _dof_constraints[constrained];
4866 775190 : for (auto & key_val : keys_vals[i])
4867 : {
4868 49415 : libmesh_assert_less(key_val.first, this->n_dofs());
4869 558483 : row[key_val.first] = key_val.second;
4870 : }
4871 216707 : if (ids_rhss[i].second != Number(0))
4872 7861 : _primal_constraint_values[constrained] =
4873 108 : ids_rhss[i].second;
4874 : else
4875 20092 : _primal_constraint_values.erase(constrained);
4876 : }
4877 : }
4878 : }
4879 70484 : };
4880 :
4881 34531 : receive_dof_constraints();
4882 :
4883 : #ifdef LIBMESH_ENABLE_NODE_CONSTRAINTS
4884 : // Collect the node constraints to push to each processor
4885 220578 : for (auto & i : _node_constraints)
4886 : {
4887 217734 : const Node * constrained = i.first;
4888 :
4889 326601 : if (constrained->processor_id() != this->processor_id())
4890 24367 : continue;
4891 :
4892 84500 : NodeConstraintRow & row = i.second.first;
4893 757558 : for (auto & j : row)
4894 : {
4895 588558 : const Node * constraining = j.first;
4896 :
4897 918060 : if (constraining->processor_id() != this->processor_id() &&
4898 32206 : constraining->processor_id() != constrained->processor_id())
4899 32206 : pushed_node_ids[constraining->processor_id()].insert(constrained->id());
4900 : }
4901 : }
4902 :
4903 : // Pack the node constraint rows and rhss to push
4904 : std::map<processor_id_type,
4905 : std::vector<std::vector<std::pair<dof_id_type,Real>>>>
4906 2844 : pushed_node_keys_vals, pushed_node_keys_vals_to_me;
4907 : std::map<processor_id_type, std::vector<std::pair<dof_id_type, Point>>>
4908 2844 : pushed_node_ids_offsets, pushed_node_ids_offsets_to_me;
4909 2844 : std::map<processor_id_type, std::vector<const Node *>> pushed_node_vecs;
4910 :
4911 4340 : for (const auto & [pid, pid_ids]: pushed_node_ids)
4912 : {
4913 748 : const std::size_t ids_size = pid_ids.size();
4914 : std::vector<std::vector<std::pair<dof_id_type,Real>>> &
4915 1496 : keys_vals = pushed_node_keys_vals[pid];
4916 : std::vector<std::pair<dof_id_type, Point>> &
4917 1496 : ids_offsets = pushed_node_ids_offsets[pid];
4918 1496 : keys_vals.resize(ids_size);
4919 1496 : ids_offsets.resize(ids_size);
4920 1496 : std::set<Node *> nodes;
4921 :
4922 : std::size_t push_i;
4923 748 : std::set<dof_id_type>::const_iterator it;
4924 19060 : for (push_i = 0, it = pid_ids.begin();
4925 19808 : it != pid_ids.end(); ++push_i, ++it)
4926 : {
4927 18312 : Node * constrained = mesh.node_ptr(*it);
4928 :
4929 18312 : if (constrained->processor_id() != pid)
4930 9156 : nodes.insert(constrained);
4931 :
4932 18312 : NodeConstraintRow & row = _node_constraints[constrained].first;
4933 9156 : std::size_t row_size = row.size();
4934 27468 : keys_vals[push_i].reserve(row_size);
4935 89605 : for (const auto & j : row)
4936 : {
4937 71293 : Node * constraining = const_cast<Node *>(j.first);
4938 :
4939 108743 : keys_vals[push_i].emplace_back(constraining->id(), j.second);
4940 :
4941 71293 : if (constraining->processor_id() != pid)
4942 17740 : nodes.insert(constraining);
4943 : }
4944 :
4945 18312 : ids_offsets[push_i].first = *it;
4946 18312 : ids_offsets[push_i].second = _node_constraints[constrained].second;
4947 : }
4948 :
4949 1496 : if (!mesh.is_serial())
4950 : {
4951 0 : auto & pid_nodes = pushed_node_vecs[pid];
4952 0 : pid_nodes.assign(nodes.begin(), nodes.end());
4953 : }
4954 : }
4955 :
4956 : auto node_ids_offsets_action_functor =
4957 : [& pushed_node_ids_offsets_to_me]
4958 : (processor_id_type pid,
4959 748 : const std::vector<std::pair<dof_id_type, Point>> & data)
4960 : {
4961 1496 : pushed_node_ids_offsets_to_me[pid] = data;
4962 3592 : };
4963 :
4964 : auto node_keys_vals_action_functor =
4965 : [& pushed_node_keys_vals_to_me]
4966 : (processor_id_type pid,
4967 748 : const std::vector<std::vector<std::pair<dof_id_type, Real>>> & data)
4968 : {
4969 1496 : pushed_node_keys_vals_to_me[pid] = data;
4970 3592 : };
4971 :
4972 : // Trade pushed node constraint rows
4973 : Parallel::push_parallel_vector_data
4974 2844 : (this->comm(), pushed_node_ids_offsets, node_ids_offsets_action_functor);
4975 : Parallel::push_parallel_vector_data
4976 2844 : (this->comm(), pushed_node_keys_vals, node_keys_vals_action_functor);
4977 :
4978 : // Constraining nodes might not even exist on our subset of a
4979 : // distributed mesh, so let's make them exist.
4980 :
4981 : // Node unpack() now automatically adds them to the context mesh
4982 0 : auto null_node_functor = [](processor_id_type, const std::vector<const Node *> &){};
4983 :
4984 2844 : if (!mesh.is_serial())
4985 : Parallel::push_parallel_packed_range
4986 40 : (this->comm(), pushed_node_vecs, &mesh, null_node_functor);
4987 :
4988 4340 : for (const auto & [pid, ids_offsets] : pushed_node_ids_offsets_to_me)
4989 : {
4990 1496 : const auto & keys_vals = pushed_node_keys_vals_to_me[pid];
4991 :
4992 748 : libmesh_assert_equal_to
4993 : (ids_offsets.size(), keys_vals.size());
4994 :
4995 : // Add the node constraints that I've been sent
4996 19808 : for (auto i : index_range(ids_offsets))
4997 : {
4998 18312 : dof_id_type constrained_id = ids_offsets[i].first;
4999 :
5000 : // If we don't already have a constraint for this node,
5001 : // add the one we were sent
5002 18312 : const Node * constrained = mesh.node_ptr(constrained_id);
5003 12852 : if (!this->is_constrained_node(constrained))
5004 : {
5005 10920 : NodeConstraintRow & row = _node_constraints[constrained].first;
5006 61525 : for (auto & key_val : keys_vals[i])
5007 : {
5008 45145 : const Node * key_node = mesh.node_ptr(key_val.first);
5009 45145 : row[key_node] = key_val.second;
5010 : }
5011 10920 : _node_constraints[constrained].second =
5012 10920 : ids_offsets[i].second;
5013 : }
5014 : }
5015 : }
5016 : #endif // LIBMESH_ENABLE_NODE_CONSTRAINTS
5017 :
5018 : // Next we need to push constraints to processors which don't own
5019 : // the constrained dof, don't own the constraining dof, but own an
5020 : // element supporting the constraining dof.
5021 : //
5022 : // We need to be able to quickly look up constrained dof ids by what
5023 : // constrains them, so that we can handle the case where we see a
5024 : // foreign element containing one of our constraining DoF ids and we
5025 : // need to push that constraint.
5026 : //
5027 : // Getting distributed adaptive sparsity patterns right is hard.
5028 :
5029 : typedef std::map<dof_id_type, std::set<dof_id_type>> DofConstrainsMap;
5030 2844 : DofConstrainsMap dof_id_constrains;
5031 :
5032 1174797 : for (const auto & [constrained, row] : _dof_constraints)
5033 : {
5034 3053292 : for (const auto & j : row)
5035 : {
5036 1913026 : const dof_id_type constraining = j.first;
5037 :
5038 198941 : dof_id_type constraining_proc_id = 0;
5039 7436642 : while (constraining >= _end_df[constraining_proc_id])
5040 5224613 : constraining_proc_id++;
5041 :
5042 2111967 : if (constraining_proc_id == this->processor_id())
5043 1394243 : dof_id_constrains[constraining].insert(constrained);
5044 : }
5045 : }
5046 :
5047 : // Loop over all foreign elements, find any supporting our
5048 : // constrained dof indices.
5049 1422 : pushed_ids.clear();
5050 :
5051 326982 : for (const auto & elem : as_range(mesh.active_not_local_elements_begin(),
5052 12309972 : mesh.active_not_local_elements_end()))
5053 : {
5054 518684 : std::vector<dof_id_type> my_dof_indices;
5055 6217017 : this->dof_indices (elem, my_dof_indices);
5056 :
5057 50212514 : for (const auto & dof : my_dof_indices)
5058 : {
5059 43995497 : if (auto dcmi = dof_id_constrains.find(dof);
5060 1770569 : dcmi != dof_id_constrains.end())
5061 : {
5062 742156 : for (const auto & constrained : dcmi->second)
5063 : {
5064 23885 : dof_id_type the_constrained_proc_id = 0;
5065 2781860 : while (constrained >= _end_df[the_constrained_proc_id])
5066 2166420 : the_constrained_proc_id++;
5067 :
5068 581909 : const processor_id_type elemproc = elem->processor_id();
5069 581909 : if (elemproc != the_constrained_proc_id)
5070 379205 : pushed_ids[elemproc].insert(constrained);
5071 : }
5072 : }
5073 : }
5074 31687 : }
5075 :
5076 1422 : pushed_ids_rhss.clear();
5077 1422 : pushed_ids_rhss_to_me.clear();
5078 1422 : pushed_keys_vals.clear();
5079 1422 : pushed_keys_vals_to_me.clear();
5080 :
5081 34531 : gather_ids();
5082 :
5083 : // Trade pushed dof constraint rows
5084 : Parallel::push_parallel_vector_data
5085 34531 : (this->comm(), pushed_ids_rhss, ids_rhss_action_functor);
5086 : Parallel::push_parallel_vector_data
5087 34531 : (this->comm(), pushed_keys_vals, keys_vals_action_functor);
5088 :
5089 34531 : receive_dof_constraints();
5090 :
5091 : // Finally, we need to handle the case of remote dof coupling. If a
5092 : // processor's element is coupled to a ghost element, then the
5093 : // processor needs to know about all constraints which affect the
5094 : // dofs on that ghost element, so we'll have to query the ghost
5095 : // element's owner.
5096 :
5097 2844 : GhostingFunctor::map_type elements_to_couple;
5098 2844 : DofMap::CouplingMatricesSet temporary_coupling_matrices;
5099 :
5100 : this->merge_ghost_functor_outputs
5101 105015 : (elements_to_couple,
5102 : temporary_coupling_matrices,
5103 66218 : this->coupling_functors_begin(),
5104 35953 : this->coupling_functors_end(),
5105 69062 : mesh.active_local_elements_begin(),
5106 69062 : mesh.active_local_elements_end(),
5107 : this->processor_id());
5108 :
5109 : // Each ghost-coupled element's owner should get a request for its dofs
5110 2844 : std::set<dof_id_type> requested_dofs;
5111 :
5112 143656 : for (const auto & pr : elements_to_couple)
5113 : {
5114 109125 : const Elem * elem = pr.first;
5115 :
5116 : // FIXME - optimize for the non-fully-coupled case?
5117 20134 : std::vector<dof_id_type> element_dofs;
5118 109125 : this->dof_indices(elem, element_dofs);
5119 :
5120 743634 : for (auto dof : element_dofs)
5121 598813 : requested_dofs.insert(dof);
5122 : }
5123 :
5124 34531 : this->gather_constraints(mesh, requested_dofs, false);
5125 31687 : }
5126 :
5127 :
5128 69131 : void DofMap::gather_constraints (MeshBase & /*mesh*/,
5129 : std::set<dof_id_type> & unexpanded_dofs,
5130 : bool /*look_for_constrainees*/)
5131 : {
5132 : typedef std::set<dof_id_type> DoF_RCSet;
5133 :
5134 : // If we have heterogeneous adjoint constraints we need to
5135 : // communicate those too.
5136 : const unsigned int max_qoi_num =
5137 69131 : _adjoint_constraint_values.empty() ?
5138 1348 : 0 : _adjoint_constraint_values.rbegin()->first+1;
5139 :
5140 : // We have to keep recursing while the unexpanded set is
5141 : // nonempty on *any* processor
5142 69131 : bool unexpanded_set_nonempty = !unexpanded_dofs.empty();
5143 69131 : this->comm().max(unexpanded_set_nonempty);
5144 :
5145 111141 : while (unexpanded_set_nonempty)
5146 : {
5147 : // Let's make sure we don't lose sync in this loop.
5148 1670 : parallel_object_only();
5149 :
5150 : // Request sets
5151 3340 : DoF_RCSet dof_request_set;
5152 :
5153 : // Request sets to send to each processor
5154 : std::map<processor_id_type, std::vector<dof_id_type>>
5155 3340 : requested_dof_ids;
5156 :
5157 : // And the sizes of each
5158 : std::map<processor_id_type, dof_id_type>
5159 3340 : dof_ids_on_proc;
5160 :
5161 : // Fill (and thereby sort and uniq!) the main request sets
5162 1546927 : for (const auto & unexpanded_dof : unexpanded_dofs)
5163 : {
5164 : // If we were asked for a DoF and we don't already have a
5165 : // constraint for it, then we need to check for one.
5166 1504917 : if (auto pos = _dof_constraints.find(unexpanded_dof);
5167 125655 : pos == _dof_constraints.end())
5168 : {
5169 464973 : if (!this->local_index(unexpanded_dof) &&
5170 19364 : !_dof_constraints.count(unexpanded_dof) )
5171 349113 : dof_request_set.insert(unexpanded_dof);
5172 : }
5173 : // If we were asked for a DoF and we already have a
5174 : // constraint for it, then we need to check if the
5175 : // constraint is recursive.
5176 : else
5177 : {
5178 102423 : const DofConstraintRow & row = pos->second;
5179 2776121 : for (const auto & j : row)
5180 : {
5181 1716813 : const dof_id_type constraining_dof = j.first;
5182 :
5183 : // If it's non-local and we haven't already got a
5184 : // constraint for it, we might need to ask for one
5185 1583764 : if (!this->local_index(constraining_dof) &&
5186 33087 : !_dof_constraints.count(constraining_dof))
5187 401051 : dof_request_set.insert(constraining_dof);
5188 : }
5189 : }
5190 : }
5191 :
5192 : // Clear the unexpanded constraint set; we're about to expand it
5193 1670 : unexpanded_dofs.clear();
5194 :
5195 : // Count requests by processor
5196 42010 : processor_id_type proc_id = 0;
5197 574446 : for (const auto & i : dof_request_set)
5198 : {
5199 659457 : while (i >= _end_df[proc_id])
5200 98937 : proc_id++;
5201 532436 : dof_ids_on_proc[proc_id]++;
5202 : }
5203 :
5204 90627 : for (auto & pair : dof_ids_on_proc)
5205 : {
5206 48617 : requested_dof_ids[pair.first].reserve(pair.second);
5207 : }
5208 :
5209 : // Prepare each processor's request set
5210 42010 : proc_id = 0;
5211 574446 : for (const auto & i : dof_request_set)
5212 : {
5213 659457 : while (i >= _end_df[proc_id])
5214 98937 : proc_id++;
5215 532436 : requested_dof_ids[proc_id].push_back(i);
5216 : }
5217 :
5218 : typedef std::vector<std::pair<dof_id_type, Real>> row_datum;
5219 :
5220 : typedef std::vector<Number> rhss_datum;
5221 :
5222 : auto row_gather_functor =
5223 46699 : [this]
5224 : (processor_id_type,
5225 : const std::vector<dof_id_type> & ids,
5226 535267 : std::vector<row_datum> & data)
5227 : {
5228 : // Fill those requests
5229 1918 : const std::size_t query_size = ids.size();
5230 :
5231 48617 : data.resize(query_size);
5232 581053 : for (std::size_t i=0; i != query_size; ++i)
5233 : {
5234 560119 : dof_id_type constrained = ids[i];
5235 27683 : if (_dof_constraints.count(constrained))
5236 : {
5237 17057 : DofConstraintRow & row = _dof_constraints[constrained];
5238 913 : std::size_t row_size = row.size();
5239 17970 : data[i].reserve(row_size);
5240 40568 : for (const auto & j : row)
5241 : {
5242 24597 : data[i].push_back(j);
5243 :
5244 : // We should never have an invalid constraining
5245 : // dof id
5246 1086 : libmesh_assert(j.first != DofObject::invalid_id);
5247 :
5248 : // We should never have a 0 constraint
5249 : // coefficient; that's implicit via sparse
5250 : // constraint storage
5251 : //
5252 : // But we can't easily control how users add
5253 : // constraints, so we can't safely assert that
5254 : // we're being efficient here.
5255 : //
5256 : // libmesh_assert(j.second);
5257 : }
5258 : }
5259 : else
5260 : {
5261 : // We have to distinguish "constraint with no
5262 : // constraining dofs" (e.g. due to Dirichlet
5263 : // constraint equations) from "no constraint".
5264 : // We'll use invalid_id for the latter.
5265 542149 : data[i].emplace_back(DofObject::invalid_id, Real(0));
5266 : }
5267 : }
5268 88957 : };
5269 :
5270 : auto rhss_gather_functor =
5271 46699 : [this,
5272 : max_qoi_num]
5273 : (processor_id_type,
5274 : const std::vector<dof_id_type> & ids,
5275 536134 : std::vector<rhss_datum> & data)
5276 : {
5277 : // Fill those requests
5278 1918 : const std::size_t query_size = ids.size();
5279 :
5280 48617 : data.resize(query_size);
5281 581053 : for (std::size_t i=0; i != query_size; ++i)
5282 : {
5283 532436 : dof_id_type constrained = ids[i];
5284 55366 : data[i].clear();
5285 27683 : if (_dof_constraints.count(constrained))
5286 : {
5287 : DofConstraintValueMap::const_iterator rhsit =
5288 913 : _primal_constraint_values.find(constrained);
5289 913 : data[i].push_back
5290 17258 : ((rhsit == _primal_constraint_values.end()) ?
5291 201 : 0 : rhsit->second);
5292 :
5293 17057 : for (unsigned int q = 0; q != max_qoi_num; ++q)
5294 : {
5295 : AdjointDofConstraintValues::const_iterator adjoint_map_it =
5296 0 : _adjoint_constraint_values.find(q);
5297 :
5298 0 : if (adjoint_map_it == _adjoint_constraint_values.end())
5299 : {
5300 0 : data[i].push_back(0);
5301 0 : continue;
5302 : }
5303 :
5304 : const DofConstraintValueMap & constraint_map =
5305 0 : adjoint_map_it->second;
5306 :
5307 : DofConstraintValueMap::const_iterator adj_rhsit =
5308 0 : constraint_map.find(constrained);
5309 0 : data[i].push_back
5310 0 : ((adj_rhsit == constraint_map.end()) ?
5311 0 : 0 : adj_rhsit->second);
5312 : }
5313 : }
5314 : }
5315 50287 : };
5316 :
5317 : auto row_action_functor =
5318 46699 : [this,
5319 : & unexpanded_dofs]
5320 : (processor_id_type,
5321 : const std::vector<dof_id_type> & ids,
5322 33011 : const std::vector<row_datum> & data)
5323 : {
5324 : // Add any new constraint rows we've found
5325 1918 : const std::size_t query_size = ids.size();
5326 :
5327 581053 : for (std::size_t i=0; i != query_size; ++i)
5328 : {
5329 532436 : const dof_id_type constrained = ids[i];
5330 :
5331 : // An empty row is an constraint with an empty row; for
5332 : // no constraint we use a "no row" placeholder
5333 560119 : if (data[i].empty())
5334 : {
5335 2235 : DofConstraintRow & row = _dof_constraints[constrained];
5336 206 : row.clear();
5337 : }
5338 530201 : else if (data[i][0].first != DofObject::invalid_id)
5339 : {
5340 14822 : DofConstraintRow & row = _dof_constraints[constrained];
5341 707 : row.clear();
5342 39040 : for (auto & pair : data[i])
5343 : {
5344 1086 : libmesh_assert_less(pair.first, this->n_dofs());
5345 23511 : row[pair.first] = pair.second;
5346 : }
5347 :
5348 : // And prepare to check for more recursive constraints
5349 14115 : unexpanded_dofs.insert(constrained);
5350 : }
5351 : }
5352 88957 : };
5353 :
5354 : auto rhss_action_functor =
5355 46699 : [this,
5356 : max_qoi_num]
5357 : (processor_id_type,
5358 : const std::vector<dof_id_type> & ids,
5359 18155 : const std::vector<rhss_datum> & data)
5360 : {
5361 : // Add rhs data for any new constraint rows we've found
5362 1918 : const std::size_t query_size = ids.size();
5363 :
5364 581053 : for (std::size_t i=0; i != query_size; ++i)
5365 : {
5366 560119 : if (!data[i].empty())
5367 : {
5368 17057 : dof_id_type constrained = ids[i];
5369 17057 : if (data[i][0] != Number(0))
5370 815 : _primal_constraint_values[constrained] = data[i][0];
5371 : else
5372 872 : _primal_constraint_values.erase(constrained);
5373 :
5374 17057 : for (unsigned int q = 0; q != max_qoi_num; ++q)
5375 : {
5376 : AdjointDofConstraintValues::iterator adjoint_map_it =
5377 0 : _adjoint_constraint_values.find(q);
5378 :
5379 0 : if ((adjoint_map_it == _adjoint_constraint_values.end()) &&
5380 0 : data[i][q+1] == Number(0))
5381 0 : continue;
5382 :
5383 0 : if (adjoint_map_it == _adjoint_constraint_values.end())
5384 0 : adjoint_map_it = _adjoint_constraint_values.emplace
5385 0 : (q, DofConstraintValueMap()).first;
5386 :
5387 : DofConstraintValueMap & constraint_map =
5388 0 : adjoint_map_it->second;
5389 :
5390 0 : if (data[i][q+1] != Number(0))
5391 0 : constraint_map[constrained] =
5392 0 : data[i][q+1];
5393 : else
5394 0 : constraint_map.erase(constrained);
5395 : }
5396 : }
5397 : }
5398 :
5399 88957 : };
5400 :
5401 : // Now request constraint rows from other processors
5402 1670 : row_datum * row_ex = nullptr;
5403 : Parallel::pull_parallel_vector_data
5404 42010 : (this->comm(), requested_dof_ids, row_gather_functor,
5405 : row_action_functor, row_ex);
5406 :
5407 : // And request constraint right hand sides from other procesors
5408 1670 : rhss_datum * rhs_ex = nullptr;
5409 : Parallel::pull_parallel_vector_data
5410 42010 : (this->comm(), requested_dof_ids, rhss_gather_functor,
5411 : rhss_action_functor, rhs_ex);
5412 :
5413 : // We have to keep recursing while the unexpanded set is
5414 : // nonempty on *any* processor
5415 42010 : unexpanded_set_nonempty = !unexpanded_dofs.empty();
5416 42010 : this->comm().max(unexpanded_set_nonempty);
5417 : }
5418 69131 : }
5419 :
5420 407316 : void DofMap::add_constraints_to_send_list (const MeshBase & mesh)
5421 : {
5422 : // This function must be run on all processors at once
5423 11640 : parallel_object_only();
5424 :
5425 : // Return immediately if there's nothing to gather
5426 418956 : if (this->n_processors() == 1)
5427 403590 : return;
5428 :
5429 : // We might get to return immediately if none of the processors
5430 : // found any constraints
5431 396050 : unsigned int has_constraints = !_dof_constraints.empty();
5432 384410 : this->comm().max(has_constraints);
5433 396050 : if (!has_constraints)
5434 10518 : return;
5435 :
5436 1101356 : auto add_row = [this](const DofConstraintRow & constraint_row) {
5437 2330757 : for (const auto & j : constraint_row)
5438 : {
5439 1411990 : dof_id_type constraint_dependency = j.first;
5440 :
5441 : // No point in adding one of our own dofs to the send_list
5442 1257350 : if (this->local_index(constraint_dependency))
5443 1168426 : continue;
5444 :
5445 243564 : _send_list.push_back(constraint_dependency);
5446 : }
5447 228513 : };
5448 :
5449 : // We usually only need dependencies of our own constrained dofs
5450 1264889 : for (const auto & [constrained_dof, constraint_row] : _dof_constraints)
5451 1230958 : if (this->local_index(constrained_dof))
5452 912862 : add_row(constraint_row);
5453 :
5454 : // If we only need constraint DoFs constraining DoFs which are
5455 : // algebraically local, we're done.
5456 33931 : if (!this->has_static_condensation() && !_need_ghost_constraints)
5457 1014 : return;
5458 :
5459 : // If we use StaticCondensation, though, we may need constraint DoFs
5460 : // constraining DoFs which are not local (they're on someone else's
5461 : // node) but which are supported on local elements. Let's get those
5462 : // too if we have to.
5463 : //
5464 : // Kokkos-MOOSE also requires that, because it cannot use VecSetValues
5465 : // and MatSetValues which can dynamically cache remote entries. Instead,
5466 : // it allocates ghost entries in advance based on libMesh send_list and
5467 : // accumulates to those, and does the assembly at once. When ghost DOFs
5468 : // are constrained, it has to add residual/Jacobian contributions to the
5469 : // dependencies of ghost DOFs locally as well.
5470 : //
5471 : // We'll potentially be hitting the same constrained DoFs from
5472 : // multiple directions.
5473 278292 : for (auto & elem : mesh.active_local_element_ptr_range())
5474 : {
5475 29904 : std::vector<dof_id_type> di;
5476 150426 : this->dof_indices (elem, di);
5477 2184998 : for (const auto & dof_id : di)
5478 2034572 : if (!this->local_index(dof_id))
5479 129479 : if (auto pos = _dof_constraints.find(dof_id);
5480 3964 : pos != _dof_constraints.end())
5481 5905 : add_row(pos->second);
5482 3510 : }
5483 : }
5484 :
5485 :
5486 :
5487 : #endif // LIBMESH_ENABLE_CONSTRAINTS
5488 :
5489 :
5490 : #ifdef LIBMESH_ENABLE_AMR
5491 :
5492 576 : void DofMap::constrain_p_dofs (unsigned int var,
5493 : const Elem * elem,
5494 : unsigned int s,
5495 : unsigned int p)
5496 : {
5497 : // We're constraining dofs on elem which correspond to p refinement
5498 : // levels above p - this only makes sense if elem's p refinement
5499 : // level is above p.
5500 48 : libmesh_assert_greater (elem->p_level(), p);
5501 48 : libmesh_assert_less (s, elem->n_sides());
5502 :
5503 96 : const unsigned int sys_num = this->sys_number();
5504 576 : FEType fe_type = this->variable_type(var);
5505 :
5506 576 : const unsigned int n_nodes = elem->n_nodes();
5507 5760 : for (unsigned int n = 0; n != n_nodes; ++n)
5508 5184 : if (elem->is_node_on_side(n, s))
5509 : {
5510 144 : const Node & node = elem->node_ref(n);
5511 : const unsigned int low_nc =
5512 1728 : FEInterface::n_dofs_at_node (fe_type, p, elem, n);
5513 : const unsigned int high_nc =
5514 1728 : FEInterface::n_dofs_at_node (fe_type, elem, n);
5515 :
5516 : // since we may be running this method concurrently
5517 : // on multiple threads we need to acquire a lock
5518 : // before modifying the _dof_constraints object.
5519 288 : Threads::spin_mutex::scoped_lock lock(Threads::spin_mtx);
5520 :
5521 1728 : if (elem->is_vertex(n))
5522 : {
5523 : // Add "this is zero" constraint rows for high p vertex
5524 : // dofs
5525 1152 : for (unsigned int i = low_nc; i != high_nc; ++i)
5526 : {
5527 0 : _dof_constraints[node.dof_number(sys_num,var,i)].clear();
5528 0 : _primal_constraint_values.erase(node.dof_number(sys_num,var,i));
5529 : }
5530 : }
5531 : else
5532 : {
5533 576 : const unsigned int total_dofs = node.n_comp(sys_num, var);
5534 48 : libmesh_assert_greater_equal (total_dofs, high_nc);
5535 : // Add "this is zero" constraint rows for high p
5536 : // non-vertex dofs, which are numbered in reverse
5537 1152 : for (unsigned int j = low_nc; j != high_nc; ++j)
5538 : {
5539 576 : const unsigned int i = total_dofs - j - 1;
5540 576 : _dof_constraints[node.dof_number(sys_num,var,i)].clear();
5541 576 : _primal_constraint_values.erase(node.dof_number(sys_num,var,i));
5542 : }
5543 : }
5544 : }
5545 576 : }
5546 :
5547 : #endif // LIBMESH_ENABLE_AMR
5548 :
5549 :
5550 : #ifdef LIBMESH_ENABLE_DIRICHLET
5551 11527 : void DofMap::add_dirichlet_boundary (const DirichletBoundary & dirichlet_boundary)
5552 : {
5553 11527 : _dirichlet_cache.valid = false;
5554 :
5555 22402 : _dirichlet_boundaries->push_back(std::make_unique<DirichletBoundary>(dirichlet_boundary));
5556 11527 : }
5557 :
5558 :
5559 2311 : void DofMap::add_adjoint_dirichlet_boundary (const DirichletBoundary & dirichlet_boundary,
5560 : unsigned int qoi_index)
5561 : {
5562 2311 : _dirichlet_cache.valid = false;
5563 :
5564 : unsigned int old_size = cast_int<unsigned int>
5565 132 : (_adjoint_dirichlet_boundaries.size());
5566 3502 : for (unsigned int i = old_size; i <= qoi_index; ++i)
5567 2348 : _adjoint_dirichlet_boundaries.push_back(std::make_unique<DirichletBoundaries>());
5568 :
5569 : // Make copy of DirichletBoundary, owned by _adjoint_dirichlet_boundaries
5570 2311 : _adjoint_dirichlet_boundaries[qoi_index]->push_back
5571 4490 : (std::make_unique<DirichletBoundary>(dirichlet_boundary));
5572 2311 : }
5573 :
5574 :
5575 132476 : bool DofMap::has_adjoint_dirichlet_boundaries(unsigned int q) const
5576 : {
5577 136260 : if (_adjoint_dirichlet_boundaries.size() > q)
5578 130772 : return true;
5579 :
5580 48 : return false;
5581 : }
5582 :
5583 :
5584 : const DirichletBoundaries *
5585 0 : DofMap::get_adjoint_dirichlet_boundaries(unsigned int q) const
5586 : {
5587 0 : libmesh_assert_greater(_adjoint_dirichlet_boundaries.size(),q);
5588 0 : return _adjoint_dirichlet_boundaries[q].get();
5589 : }
5590 :
5591 :
5592 : DirichletBoundaries *
5593 0 : DofMap::get_adjoint_dirichlet_boundaries(unsigned int q)
5594 : {
5595 : unsigned int old_size = cast_int<unsigned int>
5596 0 : (_adjoint_dirichlet_boundaries.size());
5597 0 : for (unsigned int i = old_size; i <= q; ++i)
5598 0 : _adjoint_dirichlet_boundaries.push_back(std::make_unique<DirichletBoundaries>());
5599 :
5600 0 : return _adjoint_dirichlet_boundaries[q].get();
5601 : }
5602 :
5603 :
5604 0 : void DofMap::remove_dirichlet_boundary (const DirichletBoundary & boundary_to_remove)
5605 : {
5606 0 : _dirichlet_cache.valid = false;
5607 :
5608 : // Find a boundary condition matching the one to be removed
5609 0 : auto lam = [&boundary_to_remove](const auto & bdy)
5610 0 : {return bdy->b == boundary_to_remove.b && bdy->variables == boundary_to_remove.variables;};
5611 :
5612 0 : auto it = std::find_if(_dirichlet_boundaries->begin(), _dirichlet_boundaries->end(), lam);
5613 :
5614 : // Assert it was actually found and remove it from the vector
5615 0 : libmesh_assert (it != _dirichlet_boundaries->end());
5616 0 : _dirichlet_boundaries->erase(it);
5617 0 : }
5618 :
5619 :
5620 0 : void DofMap::remove_adjoint_dirichlet_boundary (const DirichletBoundary & boundary_to_remove,
5621 : unsigned int qoi_index)
5622 : {
5623 0 : _dirichlet_cache.valid = false;
5624 :
5625 0 : libmesh_assert_greater(_adjoint_dirichlet_boundaries.size(),
5626 : qoi_index);
5627 :
5628 0 : auto lam = [&boundary_to_remove](const auto & bdy)
5629 0 : {return bdy->b == boundary_to_remove.b && bdy->variables == boundary_to_remove.variables;};
5630 :
5631 0 : auto it = std::find_if(_adjoint_dirichlet_boundaries[qoi_index]->begin(),
5632 0 : _adjoint_dirichlet_boundaries[qoi_index]->end(),
5633 0 : lam);
5634 :
5635 : // Assert it was actually found and remove it from the vector
5636 0 : libmesh_assert (it != _adjoint_dirichlet_boundaries[qoi_index]->end());
5637 0 : _adjoint_dirichlet_boundaries[qoi_index]->erase(it);
5638 0 : }
5639 :
5640 :
5641 25887 : void DofMap::check_dirichlet_bcid_consistency (const MeshBase & mesh,
5642 : const DirichletBoundary & boundary) const
5643 : {
5644 : const std::set<boundary_id_type>& mesh_side_bcids =
5645 732 : mesh.get_boundary_info().get_boundary_ids();
5646 : const std::set<boundary_id_type>& mesh_edge_bcids =
5647 732 : mesh.get_boundary_info().get_edge_boundary_ids();
5648 : const std::set<boundary_id_type>& mesh_node_bcids =
5649 732 : mesh.get_boundary_info().get_node_boundary_ids();
5650 732 : const std::set<boundary_id_type>& dbc_bcids = boundary.b;
5651 :
5652 : // DirichletBoundary id sets should be consistent across all ranks
5653 732 : libmesh_assert(mesh.comm().verify(dbc_bcids.size()));
5654 :
5655 74929 : for (const auto & bc_id : dbc_bcids)
5656 : {
5657 : // DirichletBoundary id sets should be consistent across all ranks
5658 1388 : libmesh_assert(mesh.comm().verify(bc_id));
5659 :
5660 20778 : bool found_bcid = (mesh_side_bcids.find(bc_id) != mesh_side_bcids.end() ||
5661 68432 : mesh_edge_bcids.find(bc_id) != mesh_edge_bcids.end() ||
5662 47660 : mesh_node_bcids.find(bc_id) != mesh_node_bcids.end());
5663 :
5664 : // On a distributed mesh, boundary id sets may *not* be
5665 : // consistent across all ranks, since not all ranks see all
5666 : // boundaries
5667 49042 : mesh.comm().max(found_bcid);
5668 :
5669 49042 : libmesh_error_msg_if(!found_bcid,
5670 : "Could not find Dirichlet boundary id " << bc_id << " in mesh!");
5671 : }
5672 25887 : }
5673 :
5674 : #endif // LIBMESH_ENABLE_DIRICHLET
5675 :
5676 :
5677 : #ifdef LIBMESH_ENABLE_PERIODIC
5678 :
5679 653 : void DofMap::add_periodic_boundary (const PeriodicBoundaryBase & periodic_boundary)
5680 : {
5681 679 : auto inverse_boundary = periodic_boundary.clone(PeriodicBoundaryBase::INVERSE);
5682 653 : this->add_periodic_boundary(periodic_boundary, *inverse_boundary);
5683 653 : }
5684 :
5685 :
5686 :
5687 653 : void DofMap::add_periodic_boundary (const PeriodicBoundaryBase & boundary,
5688 : const PeriodicBoundaryBase & inverse_boundary)
5689 : {
5690 : #ifdef LIBMESH_ENABLE_DIRICHLET
5691 653 : _dirichlet_cache.valid = false;
5692 : #endif
5693 :
5694 26 : libmesh_assert_equal_to (boundary.myboundary, inverse_boundary.pairedboundary);
5695 26 : libmesh_assert_equal_to (boundary.pairedboundary, inverse_boundary.myboundary);
5696 26 : libmesh_assert(boundary.get_variables() == inverse_boundary.get_variables());
5697 :
5698 : // See if we already have a periodic boundary associated myboundary...
5699 : PeriodicBoundaryBase * existing_boundary =
5700 653 : _periodic_boundaries->boundary(boundary.myboundary);
5701 :
5702 653 : if (!existing_boundary)
5703 : {
5704 : // ...if not, clone the inputs and add them to the
5705 : // PeriodicBoundaries object.
5706 : // These will be cleaned up automatically in the
5707 : // _periodic_boundaries destructor.
5708 679 : _periodic_boundaries->emplace(boundary.myboundary, boundary.clone());
5709 679 : _periodic_boundaries->emplace(inverse_boundary.myboundary, inverse_boundary.clone());
5710 : }
5711 : else
5712 : {
5713 : // ...otherwise, merge this object's variable IDs with the
5714 : // existing boundary object's.
5715 0 : existing_boundary->merge(boundary);
5716 :
5717 : // Do the same merging process for the inverse boundary. The
5718 : // inverse had better already exist!
5719 : PeriodicBoundaryBase * existing_inverse_boundary =
5720 0 : _periodic_boundaries->boundary(boundary.pairedboundary);
5721 0 : libmesh_assert(existing_inverse_boundary);
5722 0 : existing_inverse_boundary->merge(inverse_boundary);
5723 :
5724 : // If we had to merge different *types* of boundaries then
5725 : // something likely has gone wrong.
5726 : #ifdef LIBMESH_HAVE_RTTI
5727 : // typeid needs to be given references, not just pointers, to
5728 : // return a derived class name.
5729 0 : libmesh_assert(typeid(boundary) == typeid(*existing_boundary));
5730 0 : libmesh_assert(typeid(inverse_boundary) == typeid(*existing_inverse_boundary));
5731 : #endif
5732 : }
5733 653 : }
5734 :
5735 :
5736 : #endif
5737 :
5738 :
5739 : } // namespace libMesh
|