Line data Source code
1 : // The libMesh Finite Element Library.
2 : // Copyright (C) 2002-2026 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3 :
4 : // This library is free software; you can redistribute it and/or
5 : // modify it under the terms of the GNU Lesser General Public
6 : // License as published by the Free Software Foundation; either
7 : // version 2.1 of the License, or (at your option) any later version.
8 :
9 : // This library is distributed in the hope that it will be useful,
10 : // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 : // Lesser General Public License for more details.
13 :
14 : // You should have received a copy of the GNU Lesser General Public
15 : // License along with this library; if not, write to the Free Software
16 : // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 :
18 :
19 :
20 : // Local includes
21 : #include "libmesh/elem.h"
22 : #include "libmesh/elem_range.h"
23 : #include "libmesh/libmesh_logging.h"
24 : #include "libmesh/mesh_base.h"
25 : #include "libmesh/mesh_communication.h"
26 : #include "libmesh/mesh_serializer.h"
27 : #include "libmesh/mesh_tools.h"
28 : #include "libmesh/node_range.h"
29 : #include "libmesh/parallel.h"
30 : #include "libmesh/parallel_algebra.h"
31 : #include "libmesh/parallel_ghost_sync.h"
32 : #include "libmesh/sphere.h"
33 : #include "libmesh/threads.h"
34 : #include "libmesh/enum_to_string.h"
35 : #include "libmesh/enum_elem_type.h"
36 : #include "libmesh/int_range.h"
37 : #include "libmesh/utility.h"
38 : #include "libmesh/boundary_info.h"
39 :
40 : #ifndef NDEBUG
41 : # include "libmesh/remote_elem.h"
42 : #endif
43 :
44 : // C++ includes
45 : #include <limits>
46 : #include <numeric> // for std::accumulate
47 : #include <set>
48 : #include <unordered_map>
49 : #include <unordered_set>
50 :
51 :
52 :
53 : // ------------------------------------------------------------
54 : // anonymous namespace for helper classes and subroutines
55 : namespace {
56 :
57 : using namespace libMesh;
58 :
59 : /**
60 : * SumElemWeight(Range) sums the number of nodes per element
61 : * for each element in the provided range. The join() method
62 : * defines how to combine the reduction operation from two
63 : * distinct instances of this class which may be executed on
64 : * separate threads.
65 : */
66 : class SumElemWeight
67 : {
68 : public:
69 0 : SumElemWeight () :
70 0 : _weight(0)
71 0 : {}
72 :
73 0 : SumElemWeight (SumElemWeight &, Threads::split) :
74 0 : _weight(0)
75 0 : {}
76 :
77 0 : void operator()(const ConstElemRange & range)
78 : {
79 0 : for (const auto & elem : range)
80 0 : _weight += elem->n_nodes();
81 0 : }
82 :
83 0 : dof_id_type weight() const
84 0 : { return _weight; }
85 :
86 : // If we don't have threads we never need a join, and icpc yells a
87 : // warning if it sees an anonymous function that's never used
88 : #if LIBMESH_USING_THREADS
89 0 : void join (const SumElemWeight & other)
90 0 : { _weight += other.weight(); }
91 : #endif
92 :
93 : private:
94 : dof_id_type _weight;
95 : };
96 :
97 :
98 : /**
99 : * FindBBox(Range) computes the bounding box for the objects
100 : * in the specified range. This class may be split and subranges
101 : * can be executed on separate threads. The join() method
102 : * defines how the results from two separate threads are combined.
103 : */
104 : class FindBBox
105 : {
106 : public:
107 61893 : FindBBox () : _bbox()
108 31078 : {}
109 :
110 750 : FindBBox (FindBBox & other, Threads::split) :
111 750 : _bbox(other._bbox)
112 254 : {}
113 :
114 4210 : void operator()(const ConstNodeRange & range)
115 : {
116 1411556 : for (const auto & node : range)
117 : {
118 461782 : libmesh_assert(node);
119 1407346 : _bbox.union_with(*node);
120 : }
121 4210 : }
122 :
123 90686 : void operator()(const ConstElemRange & range)
124 : {
125 2544794 : for (const auto & elem : range)
126 : {
127 878575 : libmesh_assert(elem);
128 2454108 : _bbox.union_with(elem->loose_bounding_box());
129 : }
130 90686 : }
131 :
132 15835 : Point & min() { return _bbox.min(); }
133 :
134 15835 : Point & max() { return _bbox.max(); }
135 :
136 : // If we don't have threads we never need a join, and icpc yells a
137 : // warning if it sees an anonymous function that's never used
138 : #if LIBMESH_USING_THREADS
139 254 : void join (const FindBBox & other)
140 : {
141 750 : _bbox.union_with(other._bbox);
142 500 : }
143 : #endif
144 :
145 46321 : libMesh::BoundingBox & bbox ()
146 : {
147 46321 : return _bbox;
148 : }
149 :
150 : private:
151 : BoundingBox _bbox;
152 : };
153 :
154 : #ifdef DEBUG
155 3056732 : void assert_semiverify_dofobj(const Parallel::Communicator & communicator,
156 : const DofObject * d,
157 : unsigned int sysnum = libMesh::invalid_uint)
158 : {
159 3056732 : if (d)
160 : {
161 3010817 : const unsigned int n_sys = d->n_systems();
162 :
163 6021634 : std::vector<unsigned int> n_vars (n_sys, 0);
164 6718752 : for (unsigned int s = 0; s != n_sys; ++s)
165 3707935 : if (sysnum == s ||
166 : sysnum == libMesh::invalid_uint)
167 3010817 : n_vars[s] = d->n_vars(s);
168 :
169 : const unsigned int tot_n_vars =
170 3010817 : std::accumulate(n_vars.begin(), n_vars.end(), 0);
171 :
172 6021634 : std::vector<unsigned int> n_comp (tot_n_vars, 0);
173 6021634 : std::vector<dof_id_type> first_dof (tot_n_vars, 0);
174 :
175 6718752 : for (unsigned int s = 0, i=0; s != n_sys; ++s)
176 : {
177 3707935 : if (sysnum != s &&
178 : sysnum != libMesh::invalid_uint)
179 697118 : continue;
180 :
181 9981172 : for (unsigned int v = 0; v != n_vars[s]; ++v, ++i)
182 : {
183 6970355 : n_comp[i] = d->n_comp(s,v);
184 6970355 : first_dof[i] = n_comp[i] ? d->dof_number(s,v,0) : DofObject::invalid_id;
185 : }
186 : }
187 :
188 3010817 : libmesh_assert(communicator.semiverify(&n_sys));
189 3010817 : libmesh_assert(communicator.semiverify(&n_vars));
190 3010817 : libmesh_assert(communicator.semiverify(&n_comp));
191 3010817 : libmesh_assert(communicator.semiverify(&first_dof));
192 : }
193 : else // ranks w/o a DofObject d must still perform matching calls to the semiverify() collective
194 : {
195 45915 : const unsigned int * p_ui = nullptr;
196 45915 : const std::vector<unsigned int> * p_vui = nullptr;
197 45915 : const std::vector<dof_id_type> * p_vdid = nullptr;
198 :
199 45915 : libmesh_assert(communicator.semiverify(p_ui));
200 45915 : libmesh_assert(communicator.semiverify(p_vui));
201 45915 : libmesh_assert(communicator.semiverify(p_vui));
202 45915 : libmesh_assert(communicator.semiverify(p_vdid));
203 : }
204 3056732 : }
205 :
206 :
207 :
208 : #ifdef LIBMESH_ENABLE_UNIQUE_ID
209 18115890 : void assert_dofobj_unique_id(const Parallel::Communicator & comm,
210 : const DofObject * d,
211 : const std::unordered_set<unique_id_type> & unique_ids)
212 : {
213 : // Duplicating some semiverify code here so we can reuse
214 : // tempmin,tempmax afterward
215 :
216 : unique_id_type tempmin, tempmax;
217 18115890 : if (d)
218 : {
219 17524005 : tempmin = tempmax = d->unique_id();
220 : }
221 : else
222 : {
223 591885 : TIMPI::Attributes<unique_id_type>::set_highest(tempmin);
224 591885 : TIMPI::Attributes<unique_id_type>::set_lowest(tempmax);
225 : }
226 18115890 : comm.min(tempmin);
227 18115890 : comm.max(tempmax);
228 35639895 : bool invalid = d && ((d->unique_id() != tempmin) ||
229 17524005 : (d->unique_id() != tempmax));
230 18115890 : comm.max(invalid);
231 :
232 : // First verify that everything is in sync
233 18115890 : libmesh_assert(!invalid);
234 :
235 : // Then verify that any remote id doesn't duplicate a local one.
236 18115890 : if (!d && tempmin == tempmax)
237 81829 : libmesh_assert(!unique_ids.count(tempmin));
238 18115890 : }
239 : #endif // LIBMESH_ENABLE_UNIQUE_ID
240 : #endif // DEBUG
241 :
242 29559 : void find_nodal_neighbors_helper(const dof_id_type global_id,
243 : const std::vector<const Elem *> & node_to_elem_vec,
244 : std::vector<const Node *> & neighbors)
245 : {
246 : // We'll construct a std::set<const Node *> for more efficient
247 : // searching while finding the nodal neighbors, and return it to the
248 : // user in a std::vector.
249 16232 : std::set<const Node *> neighbor_set;
250 :
251 : // Look through the elements that contain this node
252 : // find the local node id... then find the side that
253 : // node lives on in the element
254 : // next, look for the _other_ node on that side
255 : // That other node is a "nodal_neighbor"... save it
256 178553 : for (const auto & elem : node_to_elem_vec)
257 : {
258 : // We only care about active elements...
259 148994 : if (elem->active())
260 : {
261 : // Which local node number is global_id?
262 107640 : unsigned local_node_number = elem->local_node(global_id);
263 :
264 : // Make sure it was found
265 41354 : libmesh_assert_not_equal_to(local_node_number, libMesh::invalid_uint);
266 :
267 148994 : const unsigned short n_edges = elem->n_edges();
268 :
269 : // If this element has no edges, the edge-based algorithm below doesn't make sense.
270 148994 : if (!n_edges)
271 : {
272 158 : switch (elem->type())
273 : {
274 64 : case EDGE2:
275 : {
276 18 : switch (local_node_number)
277 : {
278 32 : case 0:
279 : // The other node is a nodal neighbor
280 41 : neighbor_set.insert(elem->node_ptr(1));
281 32 : break;
282 :
283 32 : case 1:
284 : // The other node is a nodal neighbor
285 41 : neighbor_set.insert(elem->node_ptr(0));
286 32 : break;
287 :
288 0 : default:
289 0 : libmesh_error_msg("Invalid local node number: " << local_node_number << " found." << std::endl);
290 : }
291 18 : break;
292 : }
293 :
294 66 : case EDGE3:
295 : {
296 18 : switch (local_node_number)
297 : {
298 : // The outside nodes have node 2 as a neighbor
299 52 : case 0:
300 : case 1:
301 66 : neighbor_set.insert(elem->node_ptr(2));
302 52 : break;
303 :
304 : // The middle node has the outer nodes as neighbors
305 14 : case 2:
306 18 : neighbor_set.insert(elem->node_ptr(0));
307 18 : neighbor_set.insert(elem->node_ptr(1));
308 14 : break;
309 :
310 0 : default:
311 0 : libmesh_error_msg("Invalid local node number: " << local_node_number << " found." << std::endl);
312 : }
313 18 : break;
314 : }
315 :
316 28 : case EDGE4:
317 : {
318 8 : switch (local_node_number)
319 : {
320 7 : case 0:
321 : // The left-middle node is a nodal neighbor
322 9 : neighbor_set.insert(elem->node_ptr(2));
323 7 : break;
324 :
325 7 : case 1:
326 : // The right-middle node is a nodal neighbor
327 9 : neighbor_set.insert(elem->node_ptr(3));
328 7 : break;
329 :
330 : // The left-middle node
331 7 : case 2:
332 9 : neighbor_set.insert(elem->node_ptr(0));
333 9 : neighbor_set.insert(elem->node_ptr(3));
334 7 : break;
335 :
336 : // The right-middle node
337 7 : case 3:
338 9 : neighbor_set.insert(elem->node_ptr(1));
339 9 : neighbor_set.insert(elem->node_ptr(2));
340 7 : break;
341 :
342 0 : default:
343 0 : libmesh_error_msg("Invalid local node number: " << local_node_number << " found." << std::endl);
344 : }
345 8 : break;
346 : }
347 :
348 0 : default:
349 0 : libmesh_error_msg("Unrecognized ElemType: " << Utility::enum_to_string(elem->type()) << std::endl);
350 : }
351 : }
352 :
353 148994 : const auto elem_order = Elem::type_to_default_order_map[elem->type()];
354 :
355 : // Index of the current edge
356 41354 : unsigned current_edge = 0;
357 :
358 148994 : const unsigned short n_nodes = elem->n_nodes();
359 :
360 604820 : while (current_edge < n_edges)
361 : {
362 : // Find the edge the node is on
363 126386 : bool found_edge = false;
364 1125096 : for (; current_edge<n_edges; ++current_edge)
365 1030368 : if (elem->is_node_on_edge(local_node_number, current_edge))
366 : {
367 100178 : found_edge = true;
368 100178 : break;
369 : }
370 :
371 : // Did we find one?
372 455826 : if (found_edge)
373 : {
374 361098 : const Node * node_to_save = nullptr;
375 :
376 : // Find another node in this element on this edge
377 2800670 : for (unsigned other_node_this_edge = 0; other_node_this_edge != n_nodes; other_node_this_edge++)
378 : {
379 4449028 : const bool both_vertices = elem->is_vertex(local_node_number) &&
380 2009456 : elem->is_vertex(other_node_this_edge);
381 3025312 : if ( elem->is_node_on_edge(other_node_this_edge, current_edge) && // On the current edge
382 2488128 : elem->node_id(other_node_this_edge) != global_id && // But not the original node
383 : // vertex nodes on the same edge of higher order elements are not nodal neighbors
384 373376 : (elem_order == 1 || !both_vertices))
385 : {
386 : // We've found a nodal neighbor! Save a pointer to it..
387 394360 : node_to_save = elem->node_ptr(other_node_this_edge);
388 :
389 : // Make sure we found something
390 109454 : libmesh_assert(node_to_save != nullptr);
391 :
392 284906 : neighbor_set.insert(node_to_save);
393 : }
394 : }
395 : }
396 :
397 : // Keep looking for edges, node may be on more than one edge
398 455826 : current_edge++;
399 : }
400 : } // if (elem->active())
401 : } // for
402 :
403 : // Assign the entries from the set to the vector. Note: this
404 : // replaces any existing contents in neighbors and modifies its size
405 : // accordingly.
406 21443 : neighbors.assign(neighbor_set.begin(), neighbor_set.end());
407 29559 : }
408 :
409 :
410 :
411 18660 : std::unique_ptr<MeshBase> reprepared_mesh_clone (const MeshBase & mesh)
412 : {
413 18660 : const MeshBase::Preparation prep = mesh.preparation();
414 :
415 : // If the mesh thinks it's prepared in some way, *re*-preparing in
416 : // that way shouldn't change a clone of it, as long as we disallow
417 : // repartitioning or renumbering or remote element removal.
418 18660 : std::unique_ptr<MeshBase> mesh_clone = mesh.clone();
419 :
420 18588 : const bool old_allow_renumbering = mesh_clone->allow_renumbering();
421 : const bool old_allow_remote_element_removal =
422 18588 : mesh_clone->allow_remote_element_removal();
423 18588 : const bool old_skip_partitioning = mesh_clone->skip_partitioning();
424 18538 : mesh_clone->allow_renumbering(false);
425 18538 : mesh_clone->allow_remote_element_removal(false);
426 18538 : mesh_clone->skip_partitioning(true);
427 :
428 : // If the mesh thinks it's already completely prepared, test that
429 18660 : if (prep)
430 18660 : mesh_clone->prepare_for_use();
431 : // If the mesh thinks it's somewhat prepared, test each way it
432 : // thinks so.
433 : else
434 : {
435 0 : if (prep.has_synched_id_counts)
436 0 : mesh_clone->update_parallel_id_counts();
437 :
438 0 : if (prep.has_neighbor_ptrs)
439 0 : mesh_clone->find_neighbors();
440 :
441 0 : if (prep.has_cached_elem_data)
442 0 : mesh_clone->cache_elem_data();
443 :
444 0 : if (mesh.allow_detect_interior_parents() &&
445 0 : prep.has_interior_parent_ptrs)
446 0 : mesh_clone->detect_interior_parents();
447 :
448 0 : if (old_allow_remote_element_removal &&
449 0 : prep.has_removed_remote_elements)
450 0 : mesh_clone->delete_remote_elements();
451 :
452 0 : if (prep.has_removed_orphaned_nodes)
453 0 : mesh_clone->remove_orphaned_nodes();
454 :
455 0 : if (prep.has_boundary_id_sets)
456 0 : mesh_clone->get_boundary_info().regenerate_id_sets();
457 :
458 : // I don't know how we'll tell if this changes anything, but
459 : // we'll do it for completeness
460 0 : if (prep.has_reinit_ghosting_functors)
461 0 : mesh_clone->reinit_ghosting_functors();
462 : }
463 :
464 : // Restore original flag values
465 18538 : mesh_clone->allow_renumbering(old_allow_renumbering);
466 18538 : mesh_clone->allow_remote_element_removal(old_allow_remote_element_removal);
467 18538 : mesh_clone->skip_partitioning(old_skip_partitioning);
468 :
469 37198 : return mesh_clone;
470 0 : }
471 :
472 :
473 : }
474 :
475 :
476 : namespace libMesh
477 : {
478 :
479 : // ------------------------------------------------------------
480 : // MeshTools functions
481 :
482 : namespace MeshTools
483 : {
484 :
485 0 : dof_id_type total_weight(const MeshBase & mesh)
486 : {
487 0 : if (!mesh.is_serial())
488 : {
489 0 : libmesh_parallel_only(mesh.comm());
490 0 : dof_id_type weight = MeshTools::weight (mesh, mesh.processor_id());
491 0 : mesh.comm().sum(weight);
492 : dof_id_type unpartitioned_weight =
493 0 : MeshTools::weight (mesh, DofObject::invalid_processor_id);
494 0 : return weight + unpartitioned_weight;
495 : }
496 :
497 0 : SumElemWeight sew;
498 :
499 0 : Threads::parallel_reduce (ConstElemRange (mesh.elements_begin(),
500 0 : mesh.elements_end()),
501 : sew);
502 0 : return sew.weight();
503 :
504 : }
505 :
506 :
507 :
508 0 : dof_id_type weight(const MeshBase & mesh, const processor_id_type pid)
509 : {
510 0 : SumElemWeight sew;
511 :
512 0 : Threads::parallel_reduce (ConstElemRange (mesh.pid_elements_begin(pid),
513 0 : mesh.pid_elements_end(pid)),
514 : sew);
515 0 : return sew.weight();
516 : }
517 :
518 :
519 :
520 1615 : void build_nodes_to_elem_map (const MeshBase & mesh,
521 : std::vector<std::vector<dof_id_type>> & nodes_to_elem_map)
522 : {
523 : // A vector indexed over all nodes is too inefficient to use for a
524 : // distributed mesh. Use the unordered_map API instead.
525 1615 : if (!mesh.is_serial())
526 : libmesh_deprecated();
527 :
528 1615 : nodes_to_elem_map.resize (mesh.max_node_id());
529 :
530 29311 : for (const auto & elem : mesh.element_ptr_range())
531 84628 : for (auto & node : elem->node_ref_range())
532 : {
533 26388 : libmesh_assert_less (node.id(), nodes_to_elem_map.size());
534 26388 : libmesh_assert_less (elem->id(), mesh.n_elem());
535 :
536 73560 : nodes_to_elem_map[node.id()].push_back(elem->id());
537 543 : }
538 1615 : }
539 :
540 :
541 :
542 0 : void build_nodes_to_elem_map (const MeshBase & mesh,
543 : std::vector<std::vector<const Elem *>> & nodes_to_elem_map)
544 : {
545 : // A vector indexed over all nodes is too inefficient to use for a
546 : // distributed mesh. Use the unordered_map API instead.
547 0 : if (!mesh.is_serial())
548 : libmesh_deprecated();
549 :
550 0 : nodes_to_elem_map.resize (mesh.max_node_id());
551 :
552 0 : for (const auto & elem : mesh.element_ptr_range())
553 0 : for (auto & node : elem->node_ref_range())
554 : {
555 0 : libmesh_assert_less (node.id(), nodes_to_elem_map.size());
556 :
557 0 : nodes_to_elem_map[node.id()].push_back(elem);
558 0 : }
559 0 : }
560 :
561 :
562 :
563 41341 : void build_nodes_to_elem_map (const MeshBase & mesh,
564 : std::unordered_map<dof_id_type, std::vector<dof_id_type>> & nodes_to_elem_map)
565 : {
566 11990 : nodes_to_elem_map.clear();
567 :
568 7205136 : for (const auto & elem : mesh.element_ptr_range())
569 33402515 : for (auto & node : elem->node_ref_range())
570 26581484 : nodes_to_elem_map[node.id()].push_back(elem->id());
571 41341 : }
572 :
573 :
574 :
575 589 : void build_nodes_to_elem_map (const MeshBase & mesh,
576 : std::unordered_map<dof_id_type, std::vector<const Elem *>> & nodes_to_elem_map)
577 : {
578 168 : nodes_to_elem_map.clear();
579 :
580 85402 : for (const auto & elem : mesh.element_ptr_range())
581 404727 : for (auto & node : elem->node_ref_range())
582 329052 : nodes_to_elem_map[node.id()].push_back(elem);
583 589 : }
584 :
585 :
586 :
587 : std::unordered_set<dof_id_type>
588 629 : find_boundary_nodes(const MeshBase & mesh)
589 : {
590 182 : std::unordered_set<dof_id_type> boundary_nodes;
591 :
592 : // Loop over elements, find those on boundary, and
593 : // mark them as true in on_boundary.
594 130056 : for (const auto & elem : mesh.active_element_ptr_range())
595 524016 : for (auto s : elem->side_index_range())
596 524530 : if (elem->neighbor_ptr(s) == nullptr) // on the boundary
597 : {
598 29260 : auto nodes_on_side = elem->nodes_on_side(s);
599 :
600 123286 : for (auto & local_id : nodes_on_side)
601 130214 : boundary_nodes.insert(elem->node_ptr(local_id)->id());
602 265 : }
603 :
604 629 : return boundary_nodes;
605 : }
606 :
607 : std::unordered_set<dof_id_type>
608 321 : find_block_boundary_nodes(const MeshBase & mesh)
609 : {
610 94 : std::unordered_set<dof_id_type> block_boundary_nodes;
611 :
612 : // Loop over elements, find those on boundary, and
613 : // mark them as true in on_boundary.
614 98538 : for (const auto & elem : mesh.active_element_ptr_range())
615 401625 : for (auto s : elem->side_index_range())
616 403030 : if (elem->neighbor_ptr(s) && (elem->neighbor_ptr(s)->subdomain_id() != elem->subdomain_id()))
617 : {
618 0 : auto nodes_on_side = elem->nodes_on_side(s);
619 :
620 0 : for (auto & local_id : nodes_on_side)
621 0 : block_boundary_nodes.insert(elem->node_ptr(local_id)->id());
622 133 : }
623 :
624 321 : return block_boundary_nodes;
625 : }
626 :
627 :
628 :
629 : libMesh::BoundingBox
630 45274 : create_bounding_box (const MeshBase & mesh)
631 : {
632 : // This function must be run on all processors at once
633 15243 : libmesh_parallel_only(mesh.comm());
634 :
635 15243 : FindBBox find_bbox;
636 :
637 : // Start with any unpartitioned elements we know about locally
638 90548 : Threads::parallel_reduce (ConstElemRange (mesh.pid_elements_begin(DofObject::invalid_processor_id),
639 60517 : mesh.pid_elements_end(DofObject::invalid_processor_id)),
640 : find_bbox);
641 :
642 : // And combine with our local elements
643 45274 : find_bbox.bbox().union_with(create_local_bounding_box(mesh));
644 :
645 : // Compare the bounding boxes across processors
646 45274 : mesh.comm().min(find_bbox.min());
647 45274 : mesh.comm().max(find_bbox.max());
648 :
649 45274 : return find_bbox.bbox();
650 : }
651 :
652 :
653 :
654 : libMesh::BoundingBox
655 1799 : create_nodal_bounding_box (const MeshBase & mesh)
656 : {
657 : // This function must be run on all processors at once
658 592 : libmesh_parallel_only(mesh.comm());
659 :
660 592 : FindBBox find_bbox;
661 :
662 : // Start with any unpartitioned nodes we know about locally
663 3598 : Threads::parallel_reduce (ConstNodeRange (mesh.pid_nodes_begin(DofObject::invalid_processor_id),
664 3598 : mesh.pid_nodes_end(DofObject::invalid_processor_id)),
665 : find_bbox);
666 :
667 : // Add our local nodes
668 3598 : Threads::parallel_reduce (ConstNodeRange (mesh.local_nodes_begin(),
669 3006 : mesh.local_nodes_end()),
670 : find_bbox);
671 :
672 : // Compare the bounding boxes across processors
673 1799 : mesh.comm().min(find_bbox.min());
674 1799 : mesh.comm().max(find_bbox.max());
675 :
676 1799 : return find_bbox.bbox();
677 : }
678 :
679 :
680 :
681 : Sphere
682 0 : bounding_sphere(const MeshBase & mesh)
683 : {
684 0 : libMesh::BoundingBox bbox = create_bounding_box(mesh);
685 :
686 0 : const Real diag = (bbox.second - bbox.first).norm();
687 0 : const Point cent = (bbox.second + bbox.first)/2;
688 :
689 0 : return Sphere (cent, .5*diag);
690 : }
691 :
692 :
693 :
694 : libMesh::BoundingBox
695 45274 : create_local_bounding_box (const MeshBase & mesh)
696 : {
697 15243 : FindBBox find_bbox;
698 :
699 45274 : Threads::parallel_reduce (mesh.active_local_element_stored_range(),
700 : find_bbox);
701 :
702 45274 : return find_bbox.bbox();
703 : }
704 :
705 :
706 :
707 : libMesh::BoundingBox
708 0 : create_processor_bounding_box (const MeshBase & mesh,
709 : const processor_id_type pid)
710 : {
711 : // This can only be run in parallel, with consistent arguments.
712 0 : libmesh_parallel_only(mesh.comm());
713 0 : libmesh_assert(mesh.comm().verify(pid));
714 :
715 0 : libmesh_assert_less (pid, mesh.n_processors());
716 :
717 0 : FindBBox find_bbox;
718 :
719 0 : Threads::parallel_reduce (ConstElemRange (mesh.pid_elements_begin(pid),
720 0 : mesh.pid_elements_end(pid)),
721 : find_bbox);
722 :
723 : // Compare the bounding boxes across processors
724 0 : mesh.comm().min(find_bbox.min());
725 0 : mesh.comm().max(find_bbox.max());
726 :
727 0 : return find_bbox.bbox();
728 : }
729 :
730 :
731 :
732 : Sphere
733 0 : processor_bounding_sphere (const MeshBase & mesh,
734 : const processor_id_type pid)
735 : {
736 : libMesh::BoundingBox bbox =
737 0 : create_processor_bounding_box(mesh, pid);
738 :
739 0 : const Real diag = (bbox.second - bbox.first).norm();
740 0 : const Point cent = (bbox.second + bbox.first)/2;
741 :
742 0 : return Sphere (cent, .5*diag);
743 : }
744 :
745 :
746 :
747 : libMesh::BoundingBox
748 0 : create_subdomain_bounding_box (const MeshBase & mesh,
749 : const subdomain_id_type sid)
750 : {
751 : // This can only be run in parallel, with consistent arguments.
752 0 : libmesh_parallel_only(mesh.comm());
753 0 : libmesh_assert(mesh.comm().verify(sid));
754 :
755 0 : FindBBox find_bbox;
756 :
757 : Threads::parallel_reduce
758 0 : (ConstElemRange (mesh.active_local_subdomain_elements_begin(sid),
759 0 : mesh.active_local_subdomain_elements_end(sid)),
760 : find_bbox);
761 :
762 : // Compare the bounding boxes across processors
763 0 : mesh.comm().min(find_bbox.min());
764 0 : mesh.comm().max(find_bbox.max());
765 :
766 0 : return find_bbox.bbox();
767 : }
768 :
769 :
770 :
771 : Sphere
772 0 : subdomain_bounding_sphere (const MeshBase & mesh,
773 : const subdomain_id_type sid)
774 : {
775 : libMesh::BoundingBox bbox =
776 0 : create_subdomain_bounding_box(mesh, sid);
777 :
778 0 : const Real diag = (bbox.second - bbox.first).norm();
779 0 : const Point cent = (bbox.second + bbox.first)/2;
780 :
781 0 : return Sphere (cent, .5*diag);
782 : }
783 :
784 :
785 :
786 0 : void elem_types (const MeshBase & mesh,
787 : std::vector<ElemType> & et)
788 : {
789 : // Loop over the the elements. If the current element type isn't in
790 : // the vector, insert it.
791 0 : for (const auto & elem : mesh.element_ptr_range())
792 0 : if (!std::count(et.begin(), et.end(), elem->type()))
793 0 : et.push_back(elem->type());
794 0 : }
795 :
796 :
797 :
798 0 : dof_id_type n_elem_of_type (const MeshBase & mesh,
799 : const ElemType type)
800 : {
801 0 : return static_cast<dof_id_type>(std::distance(mesh.type_elements_begin(type),
802 0 : mesh.type_elements_end (type)));
803 : }
804 :
805 :
806 :
807 0 : dof_id_type n_active_elem_of_type (const MeshBase & mesh,
808 : const ElemType type)
809 : {
810 0 : return static_cast<dof_id_type>(std::distance(mesh.active_type_elements_begin(type),
811 0 : mesh.active_type_elements_end (type)));
812 : }
813 :
814 0 : dof_id_type n_non_subactive_elem_of_type_at_level(const MeshBase & mesh,
815 : const ElemType type,
816 : const unsigned int level)
817 : {
818 0 : dof_id_type cnt = 0;
819 :
820 : // iterate over the elements of the specified type
821 0 : for (const auto & elem : as_range(mesh.type_elements_begin(type),
822 0 : mesh.type_elements_end(type)))
823 0 : if ((elem->level() == level) && !elem->subactive())
824 0 : cnt++;
825 :
826 0 : return cnt;
827 : }
828 :
829 :
830 543 : unsigned int n_active_local_levels(const MeshBase & mesh)
831 : {
832 : struct LevelCounter {
833 : unsigned int nl;
834 :
835 543 : LevelCounter () : nl(0) {}
836 :
837 0 : LevelCounter (LevelCounter &, Threads::split) :
838 0 : nl(0) {}
839 :
840 543 : void operator()(const ConstElemRange & range) {
841 23314 : for (const Elem * elem : range)
842 24146 : nl = std::max(elem->level() + 1, nl);
843 543 : }
844 :
845 0 : void join(const LevelCounter & other) {
846 0 : nl = std::max(nl, other.nl);
847 0 : }
848 : };
849 :
850 156 : LevelCounter counter;
851 :
852 543 : Threads::parallel_reduce(mesh.active_local_element_stored_range(), counter);
853 :
854 543 : return counter.nl;
855 : }
856 :
857 :
858 :
859 543 : unsigned int n_active_levels(const MeshBase & mesh)
860 : {
861 156 : libmesh_parallel_only(mesh.comm());
862 :
863 543 : unsigned int nl = n_active_local_levels(mesh);
864 :
865 930 : for (const auto & elem : as_range(mesh.unpartitioned_elements_begin(),
866 1860 : mesh.unpartitioned_elements_end()))
867 0 : if (elem->active())
868 231 : nl = std::max(elem->level() + 1, nl);
869 :
870 543 : mesh.comm().max(nl);
871 543 : return nl;
872 : }
873 :
874 :
875 :
876 164106 : unsigned int n_local_levels(const MeshBase & mesh)
877 : {
878 164106 : unsigned int nl = 0;
879 :
880 2681268 : for (const auto & elem : as_range(mesh.local_elements_begin(),
881 6929068 : mesh.local_elements_end()))
882 6668992 : nl = std::max(elem->level() + 1, nl);
883 :
884 164106 : return nl;
885 : }
886 :
887 :
888 :
889 164106 : unsigned int n_levels(const MeshBase & mesh)
890 : {
891 62863 : libmesh_parallel_only(mesh.comm());
892 :
893 164106 : unsigned int nl = n_local_levels(mesh);
894 :
895 689295 : for (const auto & elem : as_range(mesh.unpartitioned_elements_begin(),
896 1993950 : mesh.unpartitioned_elements_end()))
897 1520470 : nl = std::max(elem->level() + 1, nl);
898 :
899 164106 : mesh.comm().max(nl);
900 :
901 : // n_levels() is only valid and should only be called in cases where
902 : // the mesh is validly distributed (or serialized). Let's run an
903 : // expensive test in debug mode to make sure this is such a case.
904 : #ifdef DEBUG
905 62863 : const unsigned int paranoid_nl = paranoid_n_levels(mesh);
906 62863 : libmesh_assert_equal_to(nl, paranoid_nl);
907 : #endif
908 164106 : return nl;
909 : }
910 :
911 :
912 :
913 64223 : unsigned int paranoid_n_levels(const MeshBase & mesh)
914 : {
915 63311 : libmesh_parallel_only(mesh.comm());
916 :
917 64223 : unsigned int nl = 0;
918 5421650 : for (const auto & elem : mesh.element_ptr_range())
919 5356979 : nl = std::max(elem->level() + 1, nl);
920 :
921 64223 : mesh.comm().max(nl);
922 64223 : return nl;
923 : }
924 :
925 :
926 :
927 63 : dof_id_type n_connected_components(const MeshBase & mesh,
928 : Real constraint_tol)
929 : {
930 54 : LOG_SCOPE("n_connected_components()", "MeshTools");
931 :
932 : // Yes, I'm being lazy. This is for mesh analysis before a
933 : // simulation, not anything going in any loops.
934 63 : if (!mesh.is_serial_on_zero())
935 0 : libmesh_not_implemented();
936 :
937 63 : dof_id_type n_components = 0;
938 :
939 81 : if (mesh.processor_id())
940 : {
941 18 : mesh.comm().broadcast(n_components);
942 27 : return n_components;
943 : }
944 :
945 : // All nodes in a set here are connected (at least indirectly) to
946 : // all other nodes in the same set, but have not yet been discovered
947 : // to be connected to nodes in other sets.
948 : //
949 : // Using an unordered_set of ids rather than a set of pointers seems
950 : // to be roughly 150% faster?
951 : // typedef const Node * node_entry_type
952 : typedef dof_id_type node_entry_type;
953 9 : std::vector<std::unordered_set<node_entry_type>> components;
954 :
955 : // With a typical mesh with few components and somewhat-contiguous
956 : // ordering, vector performance should be fine. With a mesh with
957 : // many components or completely scrambled ordering, performance
958 : // can be a disaster.
959 864 : auto find_component = [&components](node_entry_type n) {
960 1692 : for (auto & c: components)
961 1044 : if (c.find(n) != c.end())
962 54 : return &c;
963 :
964 162 : return (std::unordered_set<node_entry_type> *)(nullptr);
965 36 : };
966 :
967 : auto add_to_component =
968 402 : [&find_component]
969 852 : (std::unordered_set<node_entry_type> & component, node_entry_type n) {
970 : // We may already be in the desired component
971 804 : if (component.find(n) != component.end())
972 51 : return;
973 :
974 600 : auto current_component = find_component(n);
975 :
976 : // Didn't we *just* check this?
977 150 : libmesh_assert (&component != current_component);
978 :
979 : // If we're unknown, we should be in the desired component
980 600 : if (!current_component)
981 147 : component.insert(n);
982 :
983 : // If we think we're in another component, it should actually be
984 : // part of the desired component
985 : else
986 : {
987 : // Merge the component likely to be smaller into the one
988 : // likely to be larger - this is orders of magnitude faster
989 : // than the other way around!
990 3 : current_component->merge(component);
991 3 : current_component->swap(component);
992 3 : libmesh_assert(current_component->empty());
993 : }
994 36 : };
995 :
996 9 : const auto & constraint_rows = mesh.get_constraint_rows();
997 :
998 459 : for (const auto & elem : mesh.element_ptr_range())
999 : {
1000 : // const node_entry_type first_node = elem->node_ptr(0);
1001 264 : const node_entry_type first_node = elem->node_id(0);
1002 :
1003 264 : auto component = find_component(first_node);
1004 :
1005 : // If we didn't find one, make a new one, reusing an existing
1006 : // slot if possible or growing our vector if necessary
1007 264 : if (!component)
1008 84 : for (auto & c: components)
1009 24 : if (c.empty())
1010 0 : component = &c;
1011 :
1012 162 : if (!component)
1013 60 : component = &components.emplace_back();
1014 :
1015 1122 : for (const Node & node : elem->node_ref_range())
1016 : {
1017 : // const node_entry_type n = &node;
1018 396 : const node_entry_type n = node.id();
1019 792 : add_to_component(*component, n);
1020 :
1021 792 : auto it = constraint_rows.find(&node);
1022 792 : if (it == constraint_rows.end())
1023 195 : continue;
1024 :
1025 24 : for (const auto & [pr, val] : it->second)
1026 : {
1027 : // Ignore too-trivial constraint coefficients if
1028 : // we get a non-default-0 constraint_tol
1029 15 : if (std::abs(val) < constraint_tol)
1030 0 : continue;
1031 :
1032 12 : const Elem * spline_elem = pr.first;
1033 3 : libmesh_assert(spline_elem == mesh.elem_ptr(spline_elem->id()));
1034 :
1035 : const Node * spline_node =
1036 12 : spline_elem->node_ptr(pr.second);
1037 :
1038 : // add_to_component(*component, spline_node);
1039 12 : add_to_component(*component, spline_node->id());
1040 : }
1041 : }
1042 18 : }
1043 :
1044 96 : for (auto & component : components)
1045 60 : if (!component.empty())
1046 48 : ++n_components;
1047 :
1048 : // We calculated this on proc 0; now let everyone else know too
1049 36 : mesh.comm().broadcast(n_components);
1050 :
1051 36 : return n_components;
1052 18 : }
1053 :
1054 :
1055 :
1056 0 : void get_not_subactive_node_ids(const MeshBase & mesh,
1057 : std::set<dof_id_type> & not_subactive_node_ids)
1058 : {
1059 0 : for (const auto & elem : mesh.element_ptr_range())
1060 0 : if (!elem->subactive())
1061 0 : for (auto & n : elem->node_ref_range())
1062 0 : not_subactive_node_ids.insert(n.id());
1063 0 : }
1064 :
1065 :
1066 :
1067 53033 : dof_id_type n_elem (const MeshBase::const_element_iterator & begin,
1068 : const MeshBase::const_element_iterator & end)
1069 : {
1070 78324 : return cast_int<dof_id_type>(std::distance(begin, end));
1071 : }
1072 :
1073 :
1074 :
1075 0 : dof_id_type n_nodes (const MeshBase::const_node_iterator & begin,
1076 : const MeshBase::const_node_iterator & end)
1077 : {
1078 0 : return cast_int<dof_id_type>(std::distance(begin, end));
1079 : }
1080 :
1081 :
1082 :
1083 532 : Real volume (const MeshBase & mesh,
1084 : unsigned int dim)
1085 : {
1086 148 : libmesh_parallel_only(mesh.comm());
1087 :
1088 532 : if (dim == libMesh::invalid_uint)
1089 532 : dim = mesh.mesh_dimension();
1090 :
1091 532 : Real vol = 0;
1092 :
1093 : // first my local elements
1094 2771 : for (const auto & elem : as_range(mesh.local_elements_begin(),
1095 15015 : mesh.local_elements_end()))
1096 7519 : if (elem->dim() == dim)
1097 7755 : vol += elem->volume();
1098 :
1099 : // then count any unpartitioned objects, once
1100 680 : if (mesh.processor_id() == 0)
1101 546 : for (const auto & elem : as_range(mesh.unpartitioned_elements_begin(),
1102 1092 : mesh.unpartitioned_elements_end()))
1103 0 : if (elem->dim() == dim)
1104 162 : vol += elem->volume();
1105 :
1106 532 : mesh.comm().sum(vol);
1107 532 : return vol;
1108 : }
1109 :
1110 :
1111 :
1112 543 : unsigned int n_p_levels (const MeshBase & mesh)
1113 : {
1114 156 : libmesh_parallel_only(mesh.comm());
1115 :
1116 543 : unsigned int max_p_level = 0;
1117 :
1118 : // first my local elements
1119 7624 : for (const auto & elem : as_range(mesh.local_elements_begin(),
1120 27194 : mesh.local_elements_end()))
1121 32259 : max_p_level = std::max(elem->p_level(), max_p_level);
1122 :
1123 : // then any unpartitioned objects
1124 930 : for (const auto & elem : as_range(mesh.unpartitioned_elements_begin(),
1125 1860 : mesh.unpartitioned_elements_end()))
1126 231 : max_p_level = std::max(elem->p_level(), max_p_level);
1127 :
1128 543 : mesh.comm().max(max_p_level);
1129 543 : return max_p_level + 1;
1130 : }
1131 :
1132 :
1133 :
1134 0 : void find_nodal_neighbors(const MeshBase &,
1135 : const Node & node,
1136 : const std::vector<std::vector<const Elem *>> & nodes_to_elem_map,
1137 : std::vector<const Node *> & neighbors)
1138 : {
1139 0 : find_nodal_neighbors_helper(node.id(), nodes_to_elem_map[node.id()],
1140 : neighbors);
1141 0 : }
1142 :
1143 :
1144 :
1145 29559 : void find_nodal_neighbors(const MeshBase &,
1146 : const Node & node,
1147 : const std::unordered_map<dof_id_type, std::vector<const Elem *>> & nodes_to_elem_map,
1148 : std::vector<const Node *> & neighbors)
1149 : {
1150 : const std::vector<const Elem *> node_to_elem_vec =
1151 37675 : libmesh_map_find(nodes_to_elem_map, node.id());
1152 29559 : find_nodal_neighbors_helper(node.id(), node_to_elem_vec, neighbors);
1153 29559 : }
1154 :
1155 12164 : void find_nodal_or_face_neighbors(
1156 : const MeshBase & mesh,
1157 : const Node & node,
1158 : const std::unordered_map<dof_id_type, std::vector<const Elem *>> & nodes_to_elem_map,
1159 : std::vector<const Node *> & neighbors)
1160 : {
1161 : // Find all the nodal neighbors... that is the nodes directly connected
1162 : // to this node through one edge.
1163 12164 : find_nodal_neighbors(mesh, node, nodes_to_elem_map, neighbors);
1164 :
1165 : // If no neighbors are found, use all nodes on the containing side as
1166 : // neighbors.
1167 12164 : if (!neighbors.size())
1168 : {
1169 : // Grab the element containing node
1170 1328 : const auto * elem = libmesh_map_find(nodes_to_elem_map, node.id()).front();
1171 : // Find the element side containing node
1172 3754 : for (const auto &side : elem->side_index_range())
1173 : {
1174 3754 : const auto &nodes_on_side = elem->nodes_on_side(side);
1175 : const auto it =
1176 9325 : std::find_if(nodes_on_side.begin(), nodes_on_side.end(), [&](auto local_node_id) {
1177 13707 : return elem->node_id(local_node_id) == node.id();
1178 1946 : });
1179 :
1180 4727 : if (it != nodes_on_side.end())
1181 : {
1182 11816 : for (const auto &local_node_id : nodes_on_side)
1183 : // No need to add node itself as a neighbor
1184 13215 : if (const auto *node_ptr = elem->node_ptr(local_node_id);
1185 2727 : *node_ptr != node)
1186 9160 : neighbors.push_back(node_ptr);
1187 347 : break;
1188 : }
1189 : }
1190 : }
1191 3146 : libmesh_assert(neighbors.size());
1192 12164 : }
1193 :
1194 :
1195 :
1196 0 : void find_hanging_nodes_and_parents(const MeshBase & mesh,
1197 : std::map<dof_id_type, std::vector<dof_id_type>> & hanging_nodes)
1198 : {
1199 : // Loop through all the elements
1200 0 : for (auto & elem : mesh.active_local_element_ptr_range())
1201 0 : if (elem->type() == QUAD4)
1202 0 : for (auto s : elem->side_index_range())
1203 : {
1204 : // Loop over the sides looking for sides that have hanging nodes
1205 : // This code is inspired by compute_proj_constraints()
1206 0 : const Elem * neigh = elem->neighbor_ptr(s);
1207 :
1208 : // If not a boundary side
1209 0 : if (neigh != nullptr)
1210 : {
1211 : // Is there a coarser element next to this one?
1212 0 : if (neigh->level() < elem->level())
1213 : {
1214 0 : const Elem * ancestor = elem;
1215 0 : while (neigh->level() < ancestor->level())
1216 0 : ancestor = ancestor->parent();
1217 0 : unsigned int s_neigh = neigh->which_neighbor_am_i(ancestor);
1218 0 : libmesh_assert_less (s_neigh, neigh->n_neighbors());
1219 :
1220 : // Couple of helper uints...
1221 0 : unsigned int local_node1=0;
1222 0 : unsigned int local_node2=0;
1223 :
1224 0 : bool found_in_neighbor = false;
1225 :
1226 : // Find the two vertices that make up this side
1227 0 : while (!elem->is_node_on_side(local_node1++,s)) { }
1228 0 : local_node1--;
1229 :
1230 : // Start looking for the second one with the next node
1231 0 : local_node2=local_node1+1;
1232 :
1233 : // Find the other one
1234 0 : while (!elem->is_node_on_side(local_node2++,s)) { }
1235 0 : local_node2--;
1236 :
1237 : //Pull out their global ids:
1238 0 : dof_id_type node1 = elem->node_id(local_node1);
1239 0 : dof_id_type node2 = elem->node_id(local_node2);
1240 :
1241 : // Now find which node is present in the neighbor
1242 : // FIXME This assumes a level one rule!
1243 : // The _other_ one is the hanging node
1244 :
1245 : // First look for the first one
1246 : // FIXME could be streamlined a bit
1247 0 : for (unsigned int n=0;n<neigh->n_sides();n++)
1248 0 : if (neigh->node_id(n) == node1)
1249 0 : found_in_neighbor=true;
1250 :
1251 0 : dof_id_type hanging_node=0;
1252 :
1253 0 : if (!found_in_neighbor)
1254 0 : hanging_node=node1;
1255 : else // If it wasn't node1 then it must be node2!
1256 0 : hanging_node=node2;
1257 :
1258 : // Reset for reuse
1259 0 : local_node1=0;
1260 :
1261 : // Find the first node that makes up the side in the neighbor (these should be the parent nodes)
1262 0 : while (!neigh->is_node_on_side(local_node1++,s_neigh)) { }
1263 0 : local_node1--;
1264 :
1265 0 : local_node2=local_node1+1;
1266 :
1267 : // Find the second node...
1268 0 : while (!neigh->is_node_on_side(local_node2++,s_neigh)) { }
1269 0 : local_node2--;
1270 :
1271 : // Save them if we haven't already found the parents for this one
1272 0 : if (hanging_nodes[hanging_node].size()<2)
1273 : {
1274 0 : hanging_nodes[hanging_node].push_back(neigh->node_id(local_node1));
1275 0 : hanging_nodes[hanging_node].push_back(neigh->node_id(local_node2));
1276 : }
1277 : }
1278 : }
1279 0 : }
1280 0 : }
1281 :
1282 :
1283 :
1284 12 : void clear_spline_nodes(MeshBase & mesh)
1285 : {
1286 6 : std::vector<Elem *> nodeelem_to_delete;
1287 :
1288 2385 : for (auto & elem : mesh.element_ptr_range())
1289 1921 : if (elem->type() == NODEELEM &&
1290 1380 : elem->mapping_type() == RATIONAL_BERNSTEIN_MAP)
1291 1386 : nodeelem_to_delete.push_back(elem);
1292 :
1293 3 : auto & constraint_rows = mesh.set_constraint_rows();
1294 :
1295 : // All our constraint_rows ought to be for spline constraints we're
1296 : // about to get rid of.
1297 : #ifndef NDEBUG
1298 762 : for (auto & node_row : constraint_rows)
1299 2064 : for (auto pr : node_row.second)
1300 : {
1301 1305 : const Elem * elem = pr.first.first;
1302 1305 : libmesh_assert(elem->type() == NODEELEM);
1303 1305 : libmesh_assert(elem->mapping_type() == RATIONAL_BERNSTEIN_MAP);
1304 : }
1305 : #endif
1306 :
1307 3 : constraint_rows.clear();
1308 :
1309 1392 : for (Elem * elem : nodeelem_to_delete)
1310 : {
1311 690 : Node * node = elem->node_ptr(0);
1312 1380 : mesh.delete_elem(elem);
1313 1380 : mesh.delete_node(node);
1314 : }
1315 12 : }
1316 :
1317 :
1318 :
1319 172 : bool valid_is_prepared (const MeshBase & mesh)
1320 : {
1321 150 : LOG_SCOPE("valid_is_prepared()", "MeshTools");
1322 :
1323 172 : const MeshBase::Preparation prep = mesh.preparation();
1324 :
1325 : // If the mesh doesn't think *anything* has been prepared, we have
1326 : // nothing to check.
1327 172 : if (prep == MeshBase::Preparation())
1328 0 : return true;
1329 :
1330 : // If the mesh thinks it's partitioned, check. These are counts,
1331 : // not caches, so it's a real check.
1332 172 : if (prep.is_partitioned)
1333 344 : if (mesh.n_unpartitioned_elem() ||
1334 50 : mesh.n_unpartitioned_nodes())
1335 0 : return false;
1336 :
1337 : // If the mesh thinks it's prepared in some way, *re*-preparing in
1338 : // that way shouldn't change a clone of it, as long as we disallow
1339 : // repartitioning or renumbering or remote element removal.
1340 222 : std::unique_ptr<MeshBase> mesh_clone = reprepared_mesh_clone(mesh);
1341 :
1342 : // Check whether the original and clone compare equal
1343 172 : return (mesh == *mesh_clone);
1344 72 : }
1345 :
1346 :
1347 :
1348 : #ifndef NDEBUG
1349 :
1350 :
1351 18490 : void libmesh_assert_valid_is_prepared (const MeshBase & mesh)
1352 : {
1353 18490 : LOG_SCOPE("libmesh_assert_valid_is_prepared()", "MeshTools");
1354 :
1355 18490 : const MeshBase::Preparation prep = mesh.preparation();
1356 :
1357 : // If the mesh doesn't think *anything* has been prepared, we have
1358 : // nothing to check.
1359 18490 : if (prep == MeshBase::Preparation())
1360 2 : return;
1361 :
1362 : // If the mesh thinks it's partitioned, check. These are counts,
1363 : // not caches, so it's a real check.
1364 18488 : if (prep.is_partitioned)
1365 18488 : libmesh_assert_msg
1366 : (!mesh.n_unpartitioned_elem() && !mesh.n_unpartitioned_nodes(),
1367 : "Mesh preparation().is_partitioned does not reflect mesh data.");
1368 :
1369 : // If the mesh thinks it's prepared in some way, *re*-preparing in
1370 : // that way shouldn't change a clone of it, as long as we disallow
1371 : // repartitioning or renumbering or remote element removal.
1372 18488 : std::unique_ptr<MeshBase> mesh_clone = reprepared_mesh_clone(mesh);
1373 :
1374 : mesh.assert_equal_to
1375 18488 : (*mesh_clone,
1376 : "Mesh data does not match mesh preparation().\n"
1377 18488 : "Efficiently-prepared mesh != exhaustively-prepared clone.");
1378 : }
1379 :
1380 :
1381 :
1382 :
1383 :
1384 138 : void libmesh_assert_equal_n_systems (const MeshBase & mesh)
1385 : {
1386 276 : LOG_SCOPE("libmesh_assert_equal_n_systems()", "MeshTools");
1387 :
1388 138 : unsigned int n_sys = libMesh::invalid_uint;
1389 :
1390 12694 : for (const auto & elem : mesh.element_ptr_range())
1391 : {
1392 12556 : if (n_sys == libMesh::invalid_uint)
1393 138 : n_sys = elem->n_systems();
1394 : else
1395 12418 : libmesh_assert_equal_to (elem->n_systems(), n_sys);
1396 : }
1397 :
1398 31375 : for (const auto & node : mesh.node_ptr_range())
1399 : {
1400 31237 : if (n_sys == libMesh::invalid_uint)
1401 0 : n_sys = node->n_systems();
1402 : else
1403 31237 : libmesh_assert_equal_to (node->n_systems(), n_sys);
1404 : }
1405 138 : }
1406 :
1407 :
1408 :
1409 : #ifdef LIBMESH_ENABLE_AMR
1410 0 : void libmesh_assert_old_dof_objects (const MeshBase & mesh)
1411 : {
1412 0 : LOG_SCOPE("libmesh_assert_old_dof_objects()", "MeshTools");
1413 :
1414 0 : for (const auto & elem : mesh.element_ptr_range())
1415 : {
1416 0 : if (elem->refinement_flag() == Elem::JUST_REFINED ||
1417 0 : elem->refinement_flag() == Elem::INACTIVE)
1418 0 : continue;
1419 :
1420 0 : if (elem->has_dofs())
1421 0 : libmesh_assert(elem->get_old_dof_object());
1422 :
1423 0 : for (auto & node : elem->node_ref_range())
1424 0 : if (node.has_dofs())
1425 0 : libmesh_assert(node.get_old_dof_object());
1426 : }
1427 0 : }
1428 : #else
1429 : void libmesh_assert_old_dof_objects (const MeshBase &) {}
1430 : #endif // LIBMESH_ENABLE_AMR
1431 :
1432 :
1433 :
1434 0 : void libmesh_assert_valid_node_pointers(const MeshBase & mesh)
1435 : {
1436 0 : LOG_SCOPE("libmesh_assert_valid_node_pointers()", "MeshTools");
1437 :
1438 : // Here we specifically do not want "auto &" because we need to
1439 : // reseat the (temporary) pointer variable in the loop below,
1440 : // without modifying the original.
1441 0 : for (const Elem * elem : mesh.element_ptr_range())
1442 : {
1443 0 : libmesh_assert (elem);
1444 0 : while (elem)
1445 : {
1446 0 : elem->libmesh_assert_valid_node_pointers();
1447 0 : for (auto n : elem->neighbor_ptr_range())
1448 0 : if (n && n != remote_elem)
1449 0 : n->libmesh_assert_valid_node_pointers();
1450 :
1451 0 : libmesh_assert_not_equal_to (elem->parent(), remote_elem);
1452 0 : elem = elem->parent();
1453 : }
1454 : }
1455 0 : }
1456 :
1457 :
1458 :
1459 12521 : void libmesh_assert_valid_remote_elems(const MeshBase & mesh)
1460 : {
1461 25042 : LOG_SCOPE("libmesh_assert_valid_remote_elems()", "MeshTools");
1462 :
1463 695200 : for (const auto & elem : as_range(mesh.local_elements_begin(),
1464 1390400 : mesh.local_elements_end()))
1465 : {
1466 682679 : libmesh_assert (elem);
1467 :
1468 : // We currently don't allow active_local_elements to have
1469 : // remote_elem neighbors
1470 682679 : if (elem->active())
1471 2977857 : for (auto n : elem->neighbor_ptr_range())
1472 2396191 : libmesh_assert_not_equal_to (n, remote_elem);
1473 :
1474 : #ifdef LIBMESH_ENABLE_AMR
1475 682679 : const Elem * parent = elem->parent();
1476 682679 : if (parent)
1477 377374 : libmesh_assert_not_equal_to (parent, remote_elem);
1478 :
1479 : // We can only be strict about active elements' subactive
1480 : // children
1481 682679 : if (elem->active() && elem->has_children())
1482 30186 : for (auto & child : elem->child_ref_range())
1483 24168 : libmesh_assert_not_equal_to (&child, remote_elem);
1484 : #endif
1485 : }
1486 12521 : }
1487 :
1488 :
1489 :
1490 428 : void libmesh_assert_valid_elem_ids(const MeshBase & mesh)
1491 : {
1492 856 : LOG_SCOPE("libmesh_assert_valid_elem_ids()", "MeshTools");
1493 :
1494 428 : processor_id_type lastprocid = 0;
1495 428 : dof_id_type lastelemid = 0;
1496 :
1497 14001 : for (const auto & elem : mesh.active_element_ptr_range())
1498 : {
1499 13573 : libmesh_assert (elem);
1500 13573 : processor_id_type elemprocid = elem->processor_id();
1501 13573 : dof_id_type elemid = elem->id();
1502 :
1503 13573 : libmesh_assert_greater_equal (elemid, lastelemid);
1504 13573 : libmesh_assert_greater_equal (elemprocid, lastprocid);
1505 :
1506 13573 : lastelemid = elemid;
1507 13573 : lastprocid = elemprocid;
1508 : }
1509 428 : }
1510 :
1511 :
1512 :
1513 19330 : void libmesh_assert_valid_amr_elem_ids(const MeshBase & mesh)
1514 : {
1515 38660 : LOG_SCOPE("libmesh_assert_valid_amr_elem_ids()", "MeshTools");
1516 :
1517 1721454 : for (const auto & elem : mesh.element_ptr_range())
1518 : {
1519 1702124 : libmesh_assert (elem);
1520 :
1521 1702124 : const Elem * parent = elem->parent();
1522 :
1523 1702124 : if (parent)
1524 : {
1525 838642 : libmesh_assert_greater_equal (elem->id(), parent->id());
1526 838642 : libmesh_assert_greater_equal (elem->processor_id(), parent->processor_id());
1527 : }
1528 : }
1529 19330 : }
1530 :
1531 :
1532 :
1533 38114 : void libmesh_assert_valid_amr_interior_parents(const MeshBase & mesh)
1534 : {
1535 76228 : LOG_SCOPE("libmesh_assert_valid_amr_interior_parents()", "MeshTools");
1536 :
1537 2916885 : for (const auto & elem : mesh.element_ptr_range())
1538 : {
1539 2878771 : libmesh_assert (elem);
1540 :
1541 : // We can skip to the next element if we're full-dimension
1542 : // and therefore don't have any interior parents
1543 2878771 : if (elem->dim() >= LIBMESH_DIM)
1544 1290888 : continue;
1545 :
1546 1587883 : const Elem * ip = elem->interior_parent();
1547 :
1548 1587883 : const Elem * parent = elem->parent();
1549 :
1550 1587883 : if (ip && (ip != remote_elem) && parent)
1551 : {
1552 1952 : libmesh_assert_equal_to (ip->top_parent(),
1553 : elem->top_parent()->interior_parent());
1554 :
1555 1952 : if (ip->level() == elem->level())
1556 1952 : libmesh_assert_equal_to (ip->parent(),
1557 : parent->interior_parent());
1558 : else
1559 : {
1560 0 : libmesh_assert_less (ip->level(), elem->level());
1561 0 : libmesh_assert_equal_to (ip, parent->interior_parent());
1562 : }
1563 : }
1564 : }
1565 38114 : }
1566 :
1567 :
1568 :
1569 0 : void libmesh_assert_contiguous_dof_ids(const MeshBase & mesh, unsigned int sysnum)
1570 : {
1571 0 : LOG_SCOPE("libmesh_assert_contiguous_dof_ids()", "MeshTools");
1572 :
1573 0 : if (mesh.n_processors() == 1)
1574 0 : return;
1575 :
1576 0 : libmesh_parallel_only(mesh.comm());
1577 :
1578 0 : dof_id_type min_dof_id = std::numeric_limits<dof_id_type>::max(),
1579 0 : max_dof_id = std::numeric_limits<dof_id_type>::min();
1580 :
1581 : // Figure out what our local dof id range is
1582 0 : for (const auto * node : mesh.local_node_ptr_range())
1583 : {
1584 0 : for (auto v : make_range(node->n_vars(sysnum)))
1585 0 : for (auto c : make_range(node->n_comp(sysnum, v)))
1586 : {
1587 0 : dof_id_type id = node->dof_number(sysnum, v, c);
1588 0 : min_dof_id = std::min (min_dof_id, id);
1589 0 : max_dof_id = std::max (max_dof_id, id);
1590 : }
1591 : }
1592 :
1593 : // Make sure no other processors' ids are inside it
1594 0 : for (const auto * node : mesh.node_ptr_range())
1595 : {
1596 0 : if (node->processor_id() == mesh.processor_id())
1597 0 : continue;
1598 0 : for (auto v : make_range(node->n_vars(sysnum)))
1599 0 : for (auto c : make_range(node->n_comp(sysnum, v)))
1600 : {
1601 0 : dof_id_type id = node->dof_number(sysnum, v, c);
1602 0 : libmesh_assert (id < min_dof_id ||
1603 : id > max_dof_id);
1604 : }
1605 : }
1606 : }
1607 :
1608 :
1609 :
1610 : template <>
1611 25490 : void libmesh_assert_topology_consistent_procids<Elem>(const MeshBase & mesh)
1612 : {
1613 50980 : LOG_SCOPE("libmesh_assert_topology_consistent_procids()", "MeshTools");
1614 :
1615 : // This parameter is not used when !LIBMESH_ENABLE_AMR
1616 25490 : libmesh_ignore(mesh);
1617 :
1618 : // If we're adaptively refining, check processor ids for consistency
1619 : // between parents and children.
1620 : #ifdef LIBMESH_ENABLE_AMR
1621 :
1622 : // Ancestor elements we won't worry about, but subactive and active
1623 : // elements ought to have parents with consistent processor ids
1624 2838486 : for (const auto & elem : mesh.element_ptr_range())
1625 : {
1626 2812996 : libmesh_assert(elem);
1627 :
1628 2812996 : if (!elem->active() && !elem->subactive())
1629 306836 : continue;
1630 :
1631 2506160 : const Elem * parent = elem->parent();
1632 :
1633 2506160 : if (parent)
1634 : {
1635 1225780 : libmesh_assert(parent->has_children());
1636 1225780 : processor_id_type parent_procid = parent->processor_id();
1637 1225780 : bool matching_child_id = false;
1638 : // If we've got a remote_elem then we don't know whether
1639 : // it's responsible for the parent's processor id; all
1640 : // we can do is assume it is and let its processor fail
1641 : // an assert if there's something wrong.
1642 7556572 : for (auto & child : parent->child_ref_range())
1643 12658824 : if (&child == remote_elem ||
1644 6328032 : child.processor_id() == parent_procid)
1645 6158732 : matching_child_id = true;
1646 1225780 : libmesh_assert(matching_child_id);
1647 : }
1648 : }
1649 : #endif
1650 25490 : }
1651 :
1652 :
1653 :
1654 : template <>
1655 40011 : void libmesh_assert_topology_consistent_procids<Node>(const MeshBase & mesh)
1656 : {
1657 40011 : LOG_SCOPE("libmesh_assert_topology_consistent_procids()", "MeshTools");
1658 :
1659 40011 : if (mesh.n_processors() == 1)
1660 1 : return;
1661 :
1662 40010 : libmesh_parallel_only(mesh.comm());
1663 :
1664 : // We want this test to be valid even when called after nodes have
1665 : // been added asynchronously but before they're renumbered.
1666 : //
1667 : // Plus, some code (looking at you, stitch_meshes) modifies
1668 : // DofObject ids without keeping max_elem_id()/max_node_id()
1669 : // consistent, but that's done in a safe way for performance
1670 : // reasons, so we'll play along and just figure out new max ids
1671 : // ourselves.
1672 40010 : dof_id_type parallel_max_node_id = 0;
1673 7952929 : for (const auto & node : mesh.node_ptr_range())
1674 7912919 : parallel_max_node_id = std::max<dof_id_type>(parallel_max_node_id,
1675 7912919 : node->id()+1);
1676 40010 : mesh.comm().max(parallel_max_node_id);
1677 :
1678 :
1679 80020 : std::vector<bool> node_touched_by_me(parallel_max_node_id, false);
1680 :
1681 2241535 : for (const auto & elem : as_range(mesh.local_elements_begin(),
1682 4483070 : mesh.local_elements_end()))
1683 : {
1684 2201525 : libmesh_assert (elem);
1685 :
1686 16979596 : for (auto & node : elem->node_ref_range())
1687 : {
1688 14778071 : dof_id_type nodeid = node.id();
1689 14778071 : node_touched_by_me[nodeid] = true;
1690 : }
1691 : }
1692 80020 : std::vector<bool> node_touched_by_anyone(node_touched_by_me);
1693 40010 : mesh.comm().max(node_touched_by_anyone);
1694 :
1695 3983996 : for (const auto & node : mesh.local_node_ptr_range())
1696 : {
1697 3943986 : libmesh_assert(node);
1698 3943986 : dof_id_type nodeid = node->id();
1699 3943986 : libmesh_assert(!node_touched_by_anyone[nodeid] ||
1700 : node_touched_by_me[nodeid]);
1701 : }
1702 : }
1703 :
1704 :
1705 :
1706 0 : void libmesh_assert_canonical_node_procids (const MeshBase & mesh)
1707 : {
1708 0 : for (const auto & elem : mesh.active_element_ptr_range())
1709 0 : for (auto & node : elem->node_ref_range())
1710 0 : libmesh_assert_equal_to
1711 : (node.processor_id(),
1712 : node.choose_processor_id(node.processor_id(),
1713 : elem->processor_id()));
1714 0 : }
1715 :
1716 :
1717 :
1718 : #ifdef LIBMESH_ENABLE_AMR
1719 1344 : void libmesh_assert_valid_refinement_tree(const MeshBase & mesh)
1720 : {
1721 2688 : LOG_SCOPE("libmesh_assert_valid_refinement_tree()", "MeshTools");
1722 :
1723 68254 : for (const auto & elem : mesh.element_ptr_range())
1724 : {
1725 66910 : libmesh_assert(elem);
1726 66910 : if (elem->has_children())
1727 27268 : for (auto & child : elem->child_ref_range())
1728 23216 : if (&child != remote_elem)
1729 20800 : libmesh_assert_equal_to (child.parent(), elem);
1730 66910 : if (elem->active())
1731 : {
1732 62858 : libmesh_assert(!elem->ancestor());
1733 62858 : libmesh_assert(!elem->subactive());
1734 : }
1735 4052 : else if (elem->ancestor())
1736 : {
1737 4052 : libmesh_assert(!elem->subactive());
1738 : }
1739 : else
1740 0 : libmesh_assert(elem->subactive());
1741 :
1742 66910 : if (elem->p_refinement_flag() == Elem::JUST_REFINED)
1743 0 : libmesh_assert_greater(elem->p_level(), 0);
1744 : }
1745 1344 : }
1746 : #else
1747 : void libmesh_assert_valid_refinement_tree(const MeshBase &)
1748 : {
1749 : }
1750 : #endif // LIBMESH_ENABLE_AMR
1751 :
1752 : #endif // !NDEBUG
1753 :
1754 :
1755 :
1756 : #ifdef DEBUG
1757 :
1758 0 : void libmesh_assert_no_links_to_elem(const MeshBase & mesh,
1759 : const Elem * bad_elem)
1760 : {
1761 0 : for (const auto & elem : mesh.element_ptr_range())
1762 : {
1763 0 : libmesh_assert (elem);
1764 0 : libmesh_assert_not_equal_to (elem->parent(), bad_elem);
1765 0 : for (auto n : elem->neighbor_ptr_range())
1766 0 : libmesh_assert_not_equal_to (n, bad_elem);
1767 :
1768 : #ifdef LIBMESH_ENABLE_AMR
1769 0 : if (elem->has_children())
1770 0 : for (auto & child : elem->child_ref_range())
1771 0 : libmesh_assert_not_equal_to (&child, bad_elem);
1772 : #endif
1773 : }
1774 0 : }
1775 :
1776 :
1777 570 : void libmesh_assert_equal_points (const MeshBase & mesh)
1778 : {
1779 1140 : LOG_SCOPE("libmesh_assert_equal_points()", "MeshTools");
1780 :
1781 570 : dof_id_type pmax_node_id = mesh.max_node_id();
1782 570 : mesh.comm().max(pmax_node_id);
1783 :
1784 922414 : for (dof_id_type i=0; i != pmax_node_id; ++i)
1785 : {
1786 921844 : const Point * p = mesh.query_node_ptr(i);
1787 :
1788 921844 : libmesh_assert(mesh.comm().semiverify(p));
1789 : }
1790 570 : }
1791 :
1792 :
1793 570 : void libmesh_assert_equal_connectivity (const MeshBase & mesh)
1794 : {
1795 1140 : LOG_SCOPE("libmesh_assert_equal_connectivity()", "MeshTools");
1796 :
1797 570 : dof_id_type pmax_elem_id = mesh.max_elem_id();
1798 570 : mesh.comm().max(pmax_elem_id);
1799 :
1800 1144326 : for (dof_id_type i=0; i != pmax_elem_id; ++i)
1801 : {
1802 1143756 : const Elem * e = mesh.query_elem_ptr(i);
1803 :
1804 2287512 : std::vector<dof_id_type> nodes;
1805 1143756 : if (e)
1806 5792488 : for (auto n : e->node_index_range())
1807 4648848 : nodes.push_back(e->node_id(n));
1808 :
1809 1143756 : libmesh_assert(mesh.comm().semiverify(e ? &nodes : nullptr));
1810 : }
1811 570 : }
1812 :
1813 :
1814 0 : void libmesh_assert_connected_nodes (const MeshBase & mesh)
1815 : {
1816 0 : LOG_SCOPE("libmesh_assert_connected_nodes()", "MeshTools");
1817 :
1818 0 : std::set<const Node *> used_nodes;
1819 :
1820 0 : for (const auto & elem : mesh.element_ptr_range())
1821 : {
1822 0 : libmesh_assert (elem);
1823 :
1824 0 : for (auto & n : elem->node_ref_range())
1825 0 : used_nodes.insert(&n);
1826 : }
1827 :
1828 0 : for (const auto & node : mesh.node_ptr_range())
1829 : {
1830 0 : libmesh_assert(node);
1831 0 : libmesh_assert(used_nodes.count(node));
1832 : }
1833 0 : }
1834 :
1835 :
1836 :
1837 48418 : void libmesh_assert_valid_constraint_rows (const MeshBase & mesh)
1838 : {
1839 48418 : libmesh_parallel_only(mesh.comm());
1840 :
1841 48418 : const auto & constraint_rows = mesh.get_constraint_rows();
1842 :
1843 48418 : bool have_constraint_rows = !constraint_rows.empty();
1844 48418 : mesh.comm().max(have_constraint_rows);
1845 48418 : if (!have_constraint_rows)
1846 48344 : return;
1847 :
1848 24938 : for (auto & row : constraint_rows)
1849 : {
1850 24864 : const Node * node = row.first;
1851 24864 : libmesh_assert(node == mesh.node_ptr(node->id()));
1852 :
1853 84326 : for (auto & pr : row.second)
1854 : {
1855 59462 : const Elem * spline_elem = pr.first.first;
1856 59462 : libmesh_assert(spline_elem == mesh.elem_ptr(spline_elem->id()));
1857 : }
1858 : }
1859 :
1860 74 : dof_id_type pmax_node_id = mesh.max_node_id();
1861 74 : mesh.comm().max(pmax_node_id);
1862 :
1863 33224 : for (dof_id_type i=0; i != pmax_node_id; ++i)
1864 : {
1865 33150 : const Node * node = mesh.query_node_ptr(i);
1866 :
1867 33150 : bool have_constraint = constraint_rows.count(node);
1868 :
1869 33150 : const std::size_t my_n_constraints = have_constraint ?
1870 24864 : libmesh_map_find(constraint_rows, node).size() : std::size_t(-1);
1871 33150 : const std::size_t * n_constraints = node ?
1872 33150 : &my_n_constraints : nullptr;
1873 :
1874 33150 : libmesh_assert(mesh.comm().semiverify(n_constraints));
1875 : }
1876 : }
1877 :
1878 :
1879 :
1880 108216 : void libmesh_assert_valid_boundary_ids(const MeshBase & mesh)
1881 : {
1882 108216 : LOG_SCOPE("libmesh_assert_valid_boundary_ids()", "MeshTools");
1883 :
1884 108216 : if (mesh.n_processors() == 1)
1885 3324 : return;
1886 :
1887 104892 : libmesh_parallel_only(mesh.comm());
1888 :
1889 104892 : const BoundaryInfo & boundary_info = mesh.get_boundary_info();
1890 :
1891 104892 : dof_id_type pmax_elem_id = mesh.max_elem_id();
1892 104892 : mesh.comm().max(pmax_elem_id);
1893 :
1894 7102110 : for (dof_id_type i=0; i != pmax_elem_id; ++i)
1895 : {
1896 6997218 : const Elem * elem = mesh.query_elem_ptr(i);
1897 6997218 : const unsigned int my_n_nodes = elem ? elem->n_nodes() : 0;
1898 6997218 : const unsigned int my_n_edges = elem ? elem->n_edges() : 0;
1899 6997218 : const unsigned int my_n_sides = elem ? elem->n_sides() : 0;
1900 : unsigned int
1901 6997218 : n_nodes = my_n_nodes,
1902 6997218 : n_edges = my_n_edges,
1903 6997218 : n_sides = my_n_sides;
1904 :
1905 6997218 : mesh.comm().max(n_nodes);
1906 6997218 : mesh.comm().max(n_edges);
1907 6997218 : mesh.comm().max(n_sides);
1908 :
1909 6997218 : if (elem)
1910 : {
1911 6856657 : libmesh_assert_equal_to(my_n_nodes, n_nodes);
1912 6856657 : libmesh_assert_equal_to(my_n_edges, n_edges);
1913 6856657 : libmesh_assert_equal_to(my_n_sides, n_sides);
1914 : }
1915 :
1916 : // Let's test all IDs on the element with one communication
1917 : // rather than n_nodes + n_edges + n_sides communications, to
1918 : // cut down on latency in dbg modes.
1919 13994436 : std::vector<boundary_id_type> all_bcids;
1920 :
1921 57380632 : for (unsigned int n=0; n != n_nodes; ++n)
1922 : {
1923 100766828 : std::vector<boundary_id_type> bcids;
1924 50383414 : if (elem)
1925 : {
1926 50112472 : boundary_info.boundary_ids(elem->node_ptr(n), bcids);
1927 :
1928 : // Ordering of boundary ids shouldn't matter
1929 50112472 : std::sort(bcids.begin(), bcids.end());
1930 : }
1931 : // libmesh_assert(mesh.comm().semiverify (elem ? &bcids : nullptr));
1932 :
1933 50383414 : all_bcids.insert(all_bcids.end(), bcids.begin(),
1934 100766828 : bcids.end());
1935 : // Separator
1936 50383414 : all_bcids.push_back(BoundaryInfo::invalid_id);
1937 : }
1938 :
1939 43855752 : for (unsigned short e=0; e != n_edges; ++e)
1940 : {
1941 73717068 : std::vector<boundary_id_type> bcids;
1942 :
1943 36858534 : if (elem)
1944 : {
1945 36655122 : boundary_info.edge_boundary_ids(elem, e, bcids);
1946 :
1947 : // Ordering of boundary ids shouldn't matter
1948 36655122 : std::sort(bcids.begin(), bcids.end());
1949 : }
1950 :
1951 : // libmesh_assert(mesh.comm().semiverify (elem ? &bcids : nullptr));
1952 :
1953 36858534 : all_bcids.insert(all_bcids.end(), bcids.begin(),
1954 73717068 : bcids.end());
1955 : // Separator
1956 36858534 : all_bcids.push_back(BoundaryInfo::invalid_id);
1957 :
1958 36858534 : if (elem)
1959 : {
1960 36655122 : boundary_info.raw_edge_boundary_ids(elem, e, bcids);
1961 :
1962 : // Ordering of boundary ids shouldn't matter
1963 36655122 : std::sort(bcids.begin(), bcids.end());
1964 :
1965 36655122 : all_bcids.insert(all_bcids.end(), bcids.begin(),
1966 73310244 : bcids.end());
1967 : // Separator
1968 36655122 : all_bcids.push_back(BoundaryInfo::invalid_id);
1969 : }
1970 :
1971 : // libmesh_assert(mesh.comm().semiverify (elem ? &bcids : nullptr));
1972 : }
1973 :
1974 34733876 : for (unsigned short s=0; s != n_sides; ++s)
1975 : {
1976 55473316 : std::vector<boundary_id_type> bcids;
1977 :
1978 27736658 : if (elem)
1979 : {
1980 27600710 : boundary_info.boundary_ids(elem, s, bcids);
1981 :
1982 : // Ordering of boundary ids shouldn't matter
1983 27600710 : std::sort(bcids.begin(), bcids.end());
1984 :
1985 27600710 : all_bcids.insert(all_bcids.end(), bcids.begin(),
1986 55201420 : bcids.end());
1987 : // Separator
1988 27600710 : all_bcids.push_back(BoundaryInfo::invalid_id);
1989 : }
1990 :
1991 : // libmesh_assert(mesh.comm().semiverify (elem ? &bcids : nullptr));
1992 :
1993 27736658 : if (elem)
1994 : {
1995 27600710 : boundary_info.raw_boundary_ids(elem, s, bcids);
1996 :
1997 : // Ordering of boundary ids shouldn't matter
1998 27600710 : std::sort(bcids.begin(), bcids.end());
1999 :
2000 27600710 : all_bcids.insert(all_bcids.end(), bcids.begin(),
2001 55201420 : bcids.end());
2002 : // Separator
2003 27600710 : all_bcids.push_back(BoundaryInfo::invalid_id);
2004 : }
2005 :
2006 : // libmesh_assert(mesh.comm().semiverify (elem ? &bcids : nullptr));
2007 : }
2008 :
2009 20991654 : for (unsigned short sf=0; sf != 2; ++sf)
2010 : {
2011 27988872 : std::vector<boundary_id_type> bcids;
2012 :
2013 13994436 : if (elem)
2014 : {
2015 13713314 : boundary_info.shellface_boundary_ids(elem, sf, bcids);
2016 :
2017 : // Ordering of boundary ids shouldn't matter
2018 13713314 : std::sort(bcids.begin(), bcids.end());
2019 :
2020 13713314 : all_bcids.insert(all_bcids.end(), bcids.begin(),
2021 27426628 : bcids.end());
2022 : // Separator
2023 13713314 : all_bcids.push_back(BoundaryInfo::invalid_id);
2024 : }
2025 :
2026 : // libmesh_assert(mesh.comm().semiverify (elem ? &bcids : nullptr));
2027 :
2028 13994436 : if (elem)
2029 : {
2030 13713314 : boundary_info.raw_shellface_boundary_ids(elem, sf, bcids);
2031 :
2032 : // Ordering of boundary ids shouldn't matter
2033 13713314 : std::sort(bcids.begin(), bcids.end());
2034 :
2035 13713314 : all_bcids.insert(all_bcids.end(), bcids.begin(),
2036 27426628 : bcids.end());
2037 : // Separator
2038 13713314 : all_bcids.push_back(BoundaryInfo::invalid_id);
2039 : }
2040 :
2041 : // libmesh_assert(mesh.comm().semiverify (elem ? &bcids : nullptr));
2042 : }
2043 :
2044 6997218 : libmesh_assert(mesh.comm().semiverify
2045 : (elem ? &all_bcids : nullptr));
2046 : }
2047 : }
2048 :
2049 :
2050 12386 : void libmesh_assert_valid_dof_ids(const MeshBase & mesh, unsigned int sysnum)
2051 : {
2052 12386 : LOG_SCOPE("libmesh_assert_valid_dof_ids()", "MeshTools");
2053 :
2054 12386 : if (mesh.n_processors() == 1)
2055 0 : return;
2056 :
2057 12386 : libmesh_parallel_only(mesh.comm());
2058 :
2059 12386 : dof_id_type pmax_elem_id = mesh.max_elem_id();
2060 12386 : mesh.comm().max(pmax_elem_id);
2061 :
2062 1066152 : for (dof_id_type i=0; i != pmax_elem_id; ++i)
2063 1053766 : assert_semiverify_dofobj(mesh.comm(),
2064 1053766 : mesh.query_elem_ptr(i),
2065 : sysnum);
2066 :
2067 12386 : dof_id_type pmax_node_id = mesh.max_node_id();
2068 12386 : mesh.comm().max(pmax_node_id);
2069 :
2070 2015352 : for (dof_id_type i=0; i != pmax_node_id; ++i)
2071 2002966 : assert_semiverify_dofobj(mesh.comm(),
2072 2002966 : mesh.query_node_ptr(i),
2073 : sysnum);
2074 : }
2075 :
2076 :
2077 : #ifdef LIBMESH_ENABLE_UNIQUE_ID
2078 95760 : void libmesh_assert_valid_unique_ids(const MeshBase & mesh)
2079 : {
2080 191520 : LOG_SCOPE("libmesh_assert_valid_unique_ids()", "MeshTools");
2081 :
2082 95760 : libmesh_parallel_only(mesh.comm());
2083 :
2084 : // Storage for semi-local DofObject ids.
2085 191520 : std::unordered_set<unique_id_type> semilocal_unique_ids;
2086 :
2087 95760 : auto gather_elem_ids = [&]()
2088 : {
2089 4987137 : for (auto const & elem : mesh.active_element_ptr_range())
2090 : {
2091 4891377 : auto [it, inserted] = semilocal_unique_ids.insert(elem->unique_id());
2092 4891377 : libmesh_assert(inserted);
2093 4891377 : libmesh_ignore(it);
2094 : }
2095 95760 : };
2096 :
2097 95760 : auto gather_node_ids = [&]()
2098 : {
2099 11774026 : for (auto const & node : mesh.node_ptr_range())
2100 : {
2101 11678266 : auto [it, inserted] = semilocal_unique_ids.insert(node->unique_id());
2102 11678266 : libmesh_assert(inserted);
2103 11678266 : libmesh_ignore(it);
2104 : }
2105 95760 : };
2106 :
2107 95760 : auto verify_elems = [&]()
2108 : {
2109 95760 : dof_id_type pmax_elem_id = mesh.max_elem_id();
2110 95760 : mesh.comm().max(pmax_elem_id);
2111 :
2112 6079640 : for (auto i : make_range(pmax_elem_id))
2113 : {
2114 5983880 : const Elem * elem = mesh.query_elem_ptr(i);
2115 5983880 : assert_dofobj_unique_id(mesh.comm(), elem, semilocal_unique_ids);
2116 : }
2117 95760 : };
2118 :
2119 95760 : auto verify_nodes = [&]()
2120 : {
2121 95760 : dof_id_type pmax_node_id = mesh.max_node_id();
2122 95760 : mesh.comm().max(pmax_node_id);
2123 :
2124 12227770 : for (auto i : make_range(pmax_node_id))
2125 : {
2126 12132010 : const Node * node = mesh.query_node_ptr(i);
2127 12132010 : assert_dofobj_unique_id(mesh.comm(), node, semilocal_unique_ids);
2128 : }
2129 95760 : };
2130 :
2131 95760 : if (!mesh.allow_node_and_elem_unique_id_overlap())
2132 : {
2133 : // First collect all the unique_ids we can see and make sure there's
2134 : // no duplicates
2135 95760 : gather_elem_ids();
2136 95760 : gather_node_ids();
2137 :
2138 : // Then make sure elements/nodes are all in sync and remote
2139 : // elements don't duplicate semilocal
2140 95760 : verify_elems();
2141 95760 : verify_nodes();
2142 : }
2143 : else
2144 : {
2145 : // If the mesh allows Node and Elem unique_ids to overlap, then we only
2146 : // check for validity and uniqueness of an Elem (resp. Node) unique id
2147 : // within the set of Elem (resp. Node) unique_ids.
2148 0 : gather_elem_ids();
2149 0 : verify_elems();
2150 :
2151 : // Clear id list before checking Nodes
2152 0 : semilocal_unique_ids.clear();
2153 :
2154 : // Finally, check Nodes
2155 0 : gather_node_ids();
2156 0 : verify_nodes();
2157 : }
2158 95760 : }
2159 : #endif
2160 :
2161 0 : void libmesh_assert_consistent_distributed(const MeshBase & mesh)
2162 : {
2163 0 : libmesh_parallel_only(mesh.comm());
2164 :
2165 0 : dof_id_type parallel_max_elem_id = mesh.max_elem_id();
2166 0 : mesh.comm().max(parallel_max_elem_id);
2167 :
2168 0 : for (dof_id_type i=0; i != parallel_max_elem_id; ++i)
2169 : {
2170 0 : const Elem * elem = mesh.query_elem_ptr(i);
2171 : processor_id_type pid =
2172 0 : elem ? elem->processor_id() : DofObject::invalid_processor_id;
2173 0 : mesh.comm().min(pid);
2174 0 : libmesh_assert(elem || pid != mesh.processor_id());
2175 : }
2176 :
2177 0 : dof_id_type parallel_max_node_id = mesh.max_node_id();
2178 0 : mesh.comm().max(parallel_max_node_id);
2179 :
2180 0 : for (dof_id_type i=0; i != parallel_max_node_id; ++i)
2181 : {
2182 0 : const Node * node = mesh.query_node_ptr(i);
2183 : processor_id_type pid =
2184 0 : node ? node->processor_id() : DofObject::invalid_processor_id;
2185 0 : mesh.comm().min(pid);
2186 0 : libmesh_assert(node || pid != mesh.processor_id());
2187 : }
2188 0 : }
2189 :
2190 :
2191 0 : void libmesh_assert_consistent_distributed_nodes(const MeshBase & mesh)
2192 : {
2193 0 : libmesh_parallel_only(mesh.comm());
2194 0 : auto locator = mesh.sub_point_locator();
2195 :
2196 0 : dof_id_type parallel_max_elem_id = mesh.max_elem_id();
2197 0 : mesh.comm().max(parallel_max_elem_id);
2198 :
2199 0 : for (dof_id_type i=0; i != parallel_max_elem_id; ++i)
2200 : {
2201 0 : const Elem * elem = mesh.query_elem_ptr(i);
2202 :
2203 0 : const unsigned int my_n_nodes = elem ? elem->n_nodes() : 0;
2204 0 : unsigned int n_nodes = my_n_nodes;
2205 0 : mesh.comm().max(n_nodes);
2206 :
2207 0 : if (n_nodes)
2208 0 : libmesh_assert(mesh.comm().semiverify(elem ? &my_n_nodes : nullptr));
2209 :
2210 0 : for (unsigned int n=0; n != n_nodes; ++n)
2211 : {
2212 0 : const Node * node = elem ? elem->node_ptr(n) : nullptr;
2213 : processor_id_type pid =
2214 0 : node ? node->processor_id() : DofObject::invalid_processor_id;
2215 0 : mesh.comm().min(pid);
2216 0 : libmesh_assert(node || pid != mesh.processor_id());
2217 : }
2218 : }
2219 0 : }
2220 :
2221 :
2222 :
2223 : template <>
2224 25490 : void libmesh_assert_parallel_consistent_procids<Elem>(const MeshBase & mesh)
2225 : {
2226 25490 : LOG_SCOPE("libmesh_assert_parallel_consistent_procids()", "MeshTools");
2227 :
2228 25490 : if (mesh.n_processors() == 1)
2229 2 : return;
2230 :
2231 25488 : libmesh_parallel_only(mesh.comm());
2232 :
2233 : // Some code (looking at you, stitch_meshes) modifies DofObject ids
2234 : // without keeping max_elem_id()/max_node_id() consistent, but
2235 : // that's done in a safe way for performance reasons, so we'll play
2236 : // along and just figure out new max ids ourselves.
2237 25488 : dof_id_type parallel_max_elem_id = 0;
2238 2838476 : for (const auto & elem : mesh.element_ptr_range())
2239 2812988 : parallel_max_elem_id = std::max<dof_id_type>(parallel_max_elem_id,
2240 2812988 : elem->id()+1);
2241 25488 : mesh.comm().max(parallel_max_elem_id);
2242 :
2243 : // Check processor ids for consistency between processors
2244 :
2245 2905534 : for (dof_id_type i=0; i != parallel_max_elem_id; ++i)
2246 : {
2247 2880046 : const Elem * elem = mesh.query_elem_ptr(i);
2248 :
2249 : processor_id_type min_id =
2250 2812988 : elem ? elem->processor_id() :
2251 2880046 : std::numeric_limits<processor_id_type>::max();
2252 2880046 : mesh.comm().min(min_id);
2253 :
2254 : processor_id_type max_id =
2255 2812988 : elem ? elem->processor_id() :
2256 2880046 : std::numeric_limits<processor_id_type>::min();
2257 2880046 : mesh.comm().max(max_id);
2258 :
2259 2880046 : if (elem)
2260 : {
2261 2812988 : libmesh_assert_equal_to (min_id, elem->processor_id());
2262 2812988 : libmesh_assert_equal_to (max_id, elem->processor_id());
2263 : }
2264 :
2265 2880046 : if (min_id == mesh.processor_id())
2266 1367303 : libmesh_assert(elem);
2267 : }
2268 : }
2269 :
2270 :
2271 :
2272 76 : void libmesh_assert_parallel_consistent_new_node_procids(const MeshBase & mesh)
2273 : {
2274 76 : LOG_SCOPE("libmesh_assert_parallel_consistent_new_node_procids()", "MeshTools");
2275 :
2276 76 : if (mesh.n_processors() == 1)
2277 0 : return;
2278 :
2279 76 : libmesh_parallel_only(mesh.comm());
2280 :
2281 : // We want this test to hit every node when called even after nodes
2282 : // have been added asynchronously but before everything has been
2283 : // renumbered.
2284 76 : dof_id_type parallel_max_elem_id = mesh.max_elem_id();
2285 76 : mesh.comm().max(parallel_max_elem_id);
2286 :
2287 152 : std::vector<bool> elem_touched_by_anyone(parallel_max_elem_id, false);
2288 :
2289 42408 : for (dof_id_type i=0; i != parallel_max_elem_id; ++i)
2290 : {
2291 42332 : const Elem * elem = mesh.query_elem_ptr(i);
2292 :
2293 42332 : const unsigned int my_n_nodes = elem ? elem->n_nodes() : 0;
2294 42332 : unsigned int n_nodes = my_n_nodes;
2295 42332 : mesh.comm().max(n_nodes);
2296 :
2297 42332 : if (n_nodes)
2298 15840 : libmesh_assert(mesh.comm().semiverify(elem ? &my_n_nodes : nullptr));
2299 :
2300 172564 : for (unsigned int n=0; n != n_nodes; ++n)
2301 : {
2302 130232 : const Node * node = elem ? elem->node_ptr(n) : nullptr;
2303 130232 : const processor_id_type pid = node ? node->processor_id() : 0;
2304 130232 : libmesh_assert(mesh.comm().semiverify (node ? &pid : nullptr));
2305 : }
2306 : }
2307 : }
2308 :
2309 : template <>
2310 59587 : void libmesh_assert_parallel_consistent_procids<Node>(const MeshBase & mesh)
2311 : {
2312 59587 : LOG_SCOPE("libmesh_assert_parallel_consistent_procids()", "MeshTools");
2313 :
2314 59587 : if (mesh.n_processors() == 1)
2315 717 : return;
2316 :
2317 58870 : libmesh_parallel_only(mesh.comm());
2318 :
2319 : // We want this test to be valid even when called even after nodes
2320 : // have been added asynchronously but before they're renumbered
2321 : //
2322 : // Plus, some code (looking at you, stitch_meshes) modifies
2323 : // DofObject ids without keeping max_elem_id()/max_node_id()
2324 : // consistent, but that's done in a safe way for performance
2325 : // reasons, so we'll play along and just figure out new max ids
2326 : // ourselves.
2327 58870 : dof_id_type parallel_max_node_id = 0;
2328 11106156 : for (const auto & node : mesh.node_ptr_range())
2329 11047286 : parallel_max_node_id = std::max<dof_id_type>(parallel_max_node_id,
2330 11047286 : node->id()+1);
2331 58870 : mesh.comm().max(parallel_max_node_id);
2332 :
2333 117740 : std::vector<bool> node_touched_by_anyone(parallel_max_node_id, false);
2334 :
2335 3156416 : for (const auto & elem : as_range(mesh.local_elements_begin(),
2336 6312832 : mesh.local_elements_end()))
2337 : {
2338 3097546 : libmesh_assert (elem);
2339 :
2340 24123049 : for (auto & node : elem->node_ref_range())
2341 : {
2342 21025503 : dof_id_type nodeid = node.id();
2343 21025503 : node_touched_by_anyone[nodeid] = true;
2344 : }
2345 : }
2346 58870 : mesh.comm().max(node_touched_by_anyone);
2347 :
2348 : // Check processor ids for consistency between processors
2349 : // on any node an element touches
2350 11623728 : for (dof_id_type i=0; i != parallel_max_node_id; ++i)
2351 : {
2352 11564858 : if (!node_touched_by_anyone[i])
2353 511596 : continue;
2354 :
2355 11053262 : const Node * node = mesh.query_node_ptr(i);
2356 11053262 : const processor_id_type pid = node ? node->processor_id() : 0;
2357 :
2358 11053262 : libmesh_assert(mesh.comm().semiverify (node ? &pid : nullptr));
2359 : }
2360 : }
2361 :
2362 :
2363 :
2364 : #ifdef LIBMESH_ENABLE_AMR
2365 0 : void libmesh_assert_valid_refinement_flags(const MeshBase & mesh)
2366 : {
2367 0 : LOG_SCOPE("libmesh_assert_valid_refinement_flags()", "MeshTools");
2368 :
2369 0 : libmesh_parallel_only(mesh.comm());
2370 0 : if (mesh.n_processors() == 1)
2371 0 : return;
2372 :
2373 0 : dof_id_type pmax_elem_id = mesh.max_elem_id();
2374 0 : mesh.comm().max(pmax_elem_id);
2375 :
2376 0 : std::vector<unsigned char> my_elem_h_state(pmax_elem_id, 255);
2377 0 : std::vector<unsigned char> my_elem_p_state(pmax_elem_id, 255);
2378 :
2379 0 : for (const auto & elem : mesh.element_ptr_range())
2380 : {
2381 0 : libmesh_assert (elem);
2382 0 : dof_id_type elemid = elem->id();
2383 :
2384 0 : my_elem_h_state[elemid] =
2385 0 : static_cast<unsigned char>(elem->refinement_flag());
2386 :
2387 0 : my_elem_p_state[elemid] =
2388 0 : static_cast<unsigned char>(elem->p_refinement_flag());
2389 : }
2390 0 : std::vector<unsigned char> min_elem_h_state(my_elem_h_state);
2391 0 : mesh.comm().min(min_elem_h_state);
2392 :
2393 0 : std::vector<unsigned char> min_elem_p_state(my_elem_p_state);
2394 0 : mesh.comm().min(min_elem_p_state);
2395 :
2396 0 : for (dof_id_type i=0; i!= pmax_elem_id; ++i)
2397 : {
2398 0 : libmesh_assert(my_elem_h_state[i] == 255 ||
2399 : my_elem_h_state[i] == min_elem_h_state[i]);
2400 0 : libmesh_assert(my_elem_p_state[i] == 255 ||
2401 : my_elem_p_state[i] == min_elem_p_state[i]);
2402 : }
2403 : }
2404 : #else
2405 : void libmesh_assert_valid_refinement_flags(const MeshBase &)
2406 : {
2407 : }
2408 : #endif // LIBMESH_ENABLE_AMR
2409 :
2410 :
2411 :
2412 41442 : void libmesh_assert_valid_neighbors(const MeshBase & mesh,
2413 : bool assert_valid_remote_elems)
2414 : {
2415 41442 : LOG_SCOPE("libmesh_assert_valid_neighbors()", "MeshTools");
2416 :
2417 4324672 : for (const auto & elem : mesh.element_ptr_range())
2418 : {
2419 4283230 : libmesh_assert (elem);
2420 4283230 : elem->libmesh_assert_valid_neighbors();
2421 : }
2422 :
2423 41442 : if (mesh.n_processors() == 1)
2424 1892 : return;
2425 :
2426 39550 : libmesh_parallel_only(mesh.comm());
2427 :
2428 39550 : dof_id_type pmax_elem_id = mesh.max_elem_id();
2429 39550 : mesh.comm().max(pmax_elem_id);
2430 :
2431 4438472 : for (dof_id_type i=0; i != pmax_elem_id; ++i)
2432 : {
2433 4398922 : const Elem * elem = mesh.query_elem_ptr(i);
2434 :
2435 4398922 : const unsigned int my_n_neigh = elem ? elem->n_neighbors() : 0;
2436 4398922 : unsigned int n_neigh = my_n_neigh;
2437 4398922 : mesh.comm().max(n_neigh);
2438 4398922 : if (elem)
2439 4264842 : libmesh_assert_equal_to (my_n_neigh, n_neigh);
2440 :
2441 21425946 : for (unsigned int n = 0; n != n_neigh; ++n)
2442 : {
2443 17027024 : dof_id_type my_neighbor = DofObject::invalid_id;
2444 17027024 : dof_id_type * p_my_neighbor = nullptr;
2445 :
2446 : // If we have a non-remote_elem neighbor link, then we can
2447 : // verify it.
2448 17027024 : if (elem && elem->neighbor_ptr(n) != remote_elem)
2449 : {
2450 16903849 : p_my_neighbor = &my_neighbor;
2451 16903849 : if (elem->neighbor_ptr(n))
2452 15823322 : my_neighbor = elem->neighbor_ptr(n)->id();
2453 :
2454 : // But wait - if we haven't set remote_elem links yet then
2455 : // some nullptr links on ghost elements might be
2456 : // future-remote_elem links, so we can't verify those.
2457 33814642 : if (!assert_valid_remote_elems &&
2458 16905499 : !elem->neighbor_ptr(n) &&
2459 1650 : elem->processor_id() != mesh.processor_id())
2460 773 : p_my_neighbor = nullptr;
2461 : }
2462 17027024 : libmesh_assert(mesh.comm().semiverify(p_my_neighbor));
2463 : }
2464 : }
2465 : }
2466 : #endif // DEBUG
2467 :
2468 :
2469 :
2470 : // Functors for correct_node_proc_ids
2471 : namespace {
2472 :
2473 : typedef std::unordered_map<dof_id_type, processor_id_type> proc_id_map_type;
2474 :
2475 : struct SyncNodeSet
2476 : {
2477 : typedef unsigned char datum; // bool but without bit twiddling issues
2478 :
2479 182 : SyncNodeSet(std::unordered_set<const Node *> & _set,
2480 623 : MeshBase & _mesh) :
2481 623 : node_set(_set), mesh(_mesh) {}
2482 :
2483 : std::unordered_set<const Node *> & node_set;
2484 :
2485 : MeshBase & mesh;
2486 :
2487 : // ------------------------------------------------------------
2488 334 : void gather_data (const std::vector<dof_id_type> & ids,
2489 : std::vector<datum> & data)
2490 : {
2491 : // Find whether each requested node belongs in the set
2492 334 : data.resize(ids.size());
2493 :
2494 223405 : for (auto i : index_range(ids))
2495 : {
2496 223071 : const dof_id_type id = ids[i];
2497 :
2498 : // We'd better have every node we're asked for
2499 223071 : Node * node = mesh.node_ptr(id);
2500 :
2501 : // Return if the node is in the set.
2502 291667 : data[i] = node_set.count(node);
2503 : }
2504 334 : }
2505 :
2506 : // ------------------------------------------------------------
2507 334 : bool act_on_data (const std::vector<dof_id_type> & ids,
2508 : const std::vector<datum> in_set)
2509 : {
2510 167 : bool data_changed = false;
2511 :
2512 : // Add nodes we've been informed of to our own set
2513 223405 : for (auto i : index_range(ids))
2514 : {
2515 223071 : if (in_set[i])
2516 : {
2517 137195 : Node * node = mesh.node_ptr(ids[i]);
2518 137195 : if (!node_set.count(node))
2519 : {
2520 256 : node_set.insert(node);
2521 256 : data_changed = true;
2522 : }
2523 : }
2524 : }
2525 :
2526 334 : return data_changed;
2527 : }
2528 : };
2529 :
2530 :
2531 259 : struct NodesNotInSet
2532 : {
2533 182 : NodesNotInSet(const std::unordered_set<const Node *> _set)
2534 441 : : node_set(_set) {}
2535 :
2536 479564 : bool operator() (const Node * node) const
2537 : {
2538 959128 : if (node_set.count(node))
2539 287060 : return false;
2540 192504 : return true;
2541 : }
2542 :
2543 : const std::unordered_set<const Node *> node_set;
2544 : };
2545 :
2546 :
2547 : struct SyncProcIdsFromMap
2548 : {
2549 : typedef processor_id_type datum;
2550 :
2551 218 : SyncProcIdsFromMap(const proc_id_map_type & _map,
2552 747 : MeshBase & _mesh) :
2553 747 : new_proc_ids(_map), mesh(_mesh) {}
2554 :
2555 : const proc_id_map_type & new_proc_ids;
2556 :
2557 : MeshBase & mesh;
2558 :
2559 : // ------------------------------------------------------------
2560 164 : void gather_data (const std::vector<dof_id_type> & ids,
2561 : std::vector<datum> & data)
2562 : {
2563 : // Find the new processor id of each requested node
2564 164 : data.resize(ids.size());
2565 :
2566 94188 : for (auto i : index_range(ids))
2567 : {
2568 94024 : const dof_id_type id = ids[i];
2569 :
2570 : // Return the node's new processor id if it has one, or its
2571 : // old processor id if not.
2572 94024 : if (const auto it = new_proc_ids.find(id);
2573 47012 : it != new_proc_ids.end())
2574 8168 : data[i] = it->second;
2575 : else
2576 : {
2577 : // We'd better find every node we're asked for
2578 85856 : const Node & node = mesh.node_ref(id);
2579 85856 : data[i] = node.processor_id();
2580 : }
2581 : }
2582 164 : }
2583 :
2584 : // ------------------------------------------------------------
2585 164 : void act_on_data (const std::vector<dof_id_type> & ids,
2586 : const std::vector<datum> proc_ids)
2587 : {
2588 : // Set the node processor ids we've now been informed of
2589 94188 : for (auto i : index_range(ids))
2590 : {
2591 94024 : Node & node = mesh.node_ref(ids[i]);
2592 94024 : node.processor_id() = proc_ids[i];
2593 : }
2594 164 : }
2595 : };
2596 : }
2597 :
2598 :
2599 :
2600 747 : void correct_node_proc_ids (MeshBase & mesh)
2601 : {
2602 654 : LOG_SCOPE("correct_node_proc_ids()","MeshTools");
2603 :
2604 : // This function must be run on all processors at once
2605 218 : libmesh_parallel_only(mesh.comm());
2606 :
2607 : // We require all processors to agree on nodal processor ids before
2608 : // going into this algorithm.
2609 : #ifdef DEBUG
2610 218 : libmesh_assert_parallel_consistent_procids<Node>(mesh);
2611 : #endif
2612 :
2613 : // If we have any unpartitioned elements at this
2614 : // stage there is a problem
2615 218 : libmesh_assert (n_elem(mesh.unpartitioned_elements_begin(),
2616 : mesh.unpartitioned_elements_end()) == 0);
2617 :
2618 : // Fix nodes' processor ids. Coarsening may have left us with nodes
2619 : // which are no longer touched by any elements of the same processor
2620 : // id, and for DofMap to work we need to fix that.
2621 :
2622 : // This is harder now that libMesh no longer requires a distributed
2623 : // mesh to ghost all nodal neighbors: it is possible for two active
2624 : // elements on two different processors to share the same node in
2625 : // such a way that neither processor knows the others' element
2626 : // exists!
2627 :
2628 : // While we're at it, if this mesh is configured to allow
2629 : // repartitioning, we'll repartition *all* the nodes' processor ids
2630 : // using the canonical Node heuristic, to try and improve DoF load
2631 : // balancing. But if the mesh is disallowing repartitioning, we
2632 : // won't touch processor_id on any node where it's valid, regardless
2633 : // of whether or not it's canonical.
2634 218 : bool repartition_all_nodes = !mesh.skip_noncritical_partitioning();
2635 436 : std::unordered_set<const Node *> valid_nodes;
2636 :
2637 : // If we aren't allowed to repartition, then we're going to leave
2638 : // every node we can at its current processor_id, and *only*
2639 : // repartition the nodes whose current processor id is incompatible
2640 : // with DoFMap (because it doesn't touch an active element, e.g. due
2641 : // to coarsening)
2642 747 : if (!repartition_all_nodes)
2643 : {
2644 173668 : for (const auto & elem : mesh.active_element_ptr_range())
2645 1223889 : for (const auto & node : elem->node_ref_range())
2646 1067015 : if (elem->processor_id() == node.processor_id())
2647 1053348 : valid_nodes.insert(&node);
2648 :
2649 182 : SyncNodeSet syncv(valid_nodes, mesh);
2650 :
2651 : Parallel::sync_dofobject_data_by_id
2652 1064 : (mesh.comm(), mesh.nodes_begin(), mesh.nodes_end(), syncv);
2653 : }
2654 :
2655 : // We build up a set of compatible processor ids for each node
2656 436 : proc_id_map_type new_proc_ids;
2657 :
2658 192368 : for (auto & elem : mesh.active_element_ptr_range())
2659 : {
2660 134596 : processor_id_type pid = elem->processor_id();
2661 :
2662 1345443 : for (auto & node : elem->node_ref_range())
2663 : {
2664 1210847 : const dof_id_type id = node.id();
2665 1210847 : if (auto it = new_proc_ids.find(id);
2666 350660 : it == new_proc_ids.end())
2667 155764 : new_proc_ids.emplace(id, pid);
2668 : else
2669 674739 : it->second = node.choose_processor_id(it->second, pid);
2670 : }
2671 311 : }
2672 :
2673 : // Sort the new pids to push to each processor
2674 : std::map<processor_id_type, std::vector<std::pair<dof_id_type, processor_id_type>>>
2675 436 : ids_to_push;
2676 :
2677 1219820 : for (const auto & node : mesh.node_ptr_range())
2678 861524 : if (const auto it = std::as_const(new_proc_ids).find(node->id());
2679 861524 : it != new_proc_ids.end() && node->processor_id() != DofObject::invalid_processor_id)
2680 536419 : ids_to_push[node->processor_id()].emplace_back(node->id(), /*pid=*/it->second);
2681 :
2682 : auto action_functor =
2683 503 : [& mesh, & new_proc_ids]
2684 : (processor_id_type,
2685 1003810 : const std::vector<std::pair<dof_id_type, processor_id_type>> & data)
2686 : {
2687 537431 : for (const auto & [id, pid] : data)
2688 : {
2689 536108 : if (const auto it = new_proc_ids.find(id);
2690 155764 : it == new_proc_ids.end())
2691 0 : new_proc_ids.emplace(id, pid);
2692 : else
2693 : {
2694 536108 : const Node & node = mesh.node_ref(id);
2695 536108 : it->second = node.choose_processor_id(it->second, pid);
2696 : }
2697 : }
2698 1349 : };
2699 :
2700 : Parallel::push_parallel_vector_data
2701 747 : (mesh.comm(), ids_to_push, action_functor);
2702 :
2703 : // Now new_proc_ids is correct for every node we used to own. Let's
2704 : // ask every other processor about the nodes they used to own. But
2705 : // first we'll need to keep track of which nodes we used to own,
2706 : // lest we get them confused with nodes we newly own.
2707 436 : std::unordered_set<Node *> ex_local_nodes;
2708 757382 : for (auto & node : mesh.local_node_ptr_range())
2709 514694 : if (const auto it = new_proc_ids.find(node->id());
2710 514694 : it != new_proc_ids.end() && it->second != mesh.processor_id())
2711 360 : ex_local_nodes.insert(node);
2712 :
2713 218 : SyncProcIdsFromMap sync(new_proc_ids, mesh);
2714 747 : if (repartition_all_nodes)
2715 : Parallel::sync_dofobject_data_by_id
2716 212 : (mesh.comm(), mesh.nodes_begin(), mesh.nodes_end(), sync);
2717 : else
2718 : {
2719 182 : NodesNotInSet nnis(valid_nodes);
2720 :
2721 : Parallel::sync_dofobject_data_by_id
2722 1064 : (mesh.comm(), mesh.nodes_begin(), mesh.nodes_end(), nnis, sync);
2723 : }
2724 :
2725 : // And finally let's update the nodes we used to own.
2726 894 : for (const auto & node : ex_local_nodes)
2727 : {
2728 137 : if (valid_nodes.count(node))
2729 117 : continue;
2730 :
2731 30 : const dof_id_type id = node->id();
2732 10 : const proc_id_map_type::iterator it = new_proc_ids.find(id);
2733 10 : libmesh_assert(it != new_proc_ids.end());
2734 30 : node->processor_id() = it->second;
2735 : }
2736 :
2737 : // We should still have consistent nodal processor ids coming out of
2738 : // this algorithm, but if we're allowed to repartition the mesh then
2739 : // they should be canonically correct too.
2740 : #ifdef DEBUG
2741 218 : libmesh_assert_valid_procids<Node>(mesh);
2742 : //if (repartition_all_nodes)
2743 : // libmesh_assert_canonical_node_procids(mesh);
2744 : #endif
2745 747 : }
2746 :
2747 :
2748 :
2749 1708 : void Private::globally_renumber_nodes_and_elements (MeshBase & mesh)
2750 : {
2751 1708 : MeshCommunication().assign_global_indices(mesh);
2752 1708 : }
2753 :
2754 : } // namespace MeshTools
2755 :
2756 : } // namespace libMesh
|