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/distributed_mesh.h"
22 :
23 : // libMesh includes
24 : #include "libmesh/boundary_info.h"
25 : #include "libmesh/elem.h"
26 : #include "libmesh/libmesh_logging.h"
27 : #include "libmesh/mesh_communication.h"
28 : #include "libmesh/mesh_tools.h"
29 : #include "libmesh/partitioner.h"
30 : #include "libmesh/string_to_enum.h"
31 :
32 : // TIMPI includes
33 : #include "timpi/parallel_implementation.h"
34 : #include "timpi/parallel_sync.h"
35 :
36 :
37 : namespace libMesh
38 : {
39 :
40 : // ------------------------------------------------------------
41 : // DistributedMesh class member functions
42 304176 : DistributedMesh::DistributedMesh (const Parallel::Communicator & comm_in,
43 304176 : unsigned char d) :
44 303680 : UnstructuredMesh (comm_in,d), _is_serial(true),
45 303680 : _is_serial_on_proc_0(true),
46 303680 : _deleted_coarse_elements(false),
47 303680 : _n_nodes(0), _n_elem(0), _max_node_id(0), _max_elem_id(0),
48 304424 : _next_free_local_node_id(this->processor_id()),
49 304176 : _next_free_local_elem_id(this->processor_id()),
50 304176 : _next_free_unpartitioned_node_id(this->n_processors()),
51 304176 : _next_free_unpartitioned_elem_id(this->n_processors())
52 : #ifdef LIBMESH_ENABLE_UNIQUE_ID
53 304176 : , _next_unpartitioned_unique_id(this->n_processors())
54 : #endif
55 : {
56 : #ifdef LIBMESH_ENABLE_UNIQUE_ID
57 304176 : _next_unique_id = this->processor_id();
58 : #endif
59 :
60 304424 : const std::string default_partitioner = "parmetis";
61 : const std::string my_partitioner =
62 : libMesh::command_line_value("--default-partitioner",
63 608352 : default_partitioner);
64 : _partitioner = Partitioner::build
65 608104 : (Utility::string_to_enum<PartitionerType>(my_partitioner));
66 304176 : }
67 :
68 270 : DistributedMesh & DistributedMesh::operator= (DistributedMesh && other_mesh)
69 : {
70 4 : LOG_SCOPE("operator=(&&)", "DistributedMesh");
71 :
72 : // Move assign as an UnstructuredMesh.
73 4 : this->UnstructuredMesh::operator=(std::move(other_mesh));
74 :
75 : // Nodes and elements belong to DistributedMesh and have to be
76 : // moved before we can move arbitrary GhostingFunctor, Partitioner,
77 : // etc. subclasses.
78 270 : this->move_nodes_and_elements(std::move(other_mesh));
79 :
80 : // But move_nodes_and_elems misses (or guesses about) some of our
81 : // subclass values, and we want more precision than a guess.
82 270 : _deleted_coarse_elements = other_mesh._deleted_coarse_elements;
83 4 : _extra_ghost_elems = std::move(other_mesh._extra_ghost_elems);
84 :
85 : // Handle remaining MeshBase moves.
86 270 : this->post_dofobject_moves(std::move(other_mesh));
87 :
88 274 : return *this;
89 : }
90 :
91 270 : MeshBase & DistributedMesh::assign(MeshBase && other_mesh)
92 : {
93 270 : *this = std::move(cast_ref<DistributedMesh&>(other_mesh));
94 :
95 270 : return *this;
96 : }
97 :
98 12346 : bool DistributedMesh::subclass_locally_equals(const MeshBase & other_mesh_base) const
99 : {
100 28 : const DistributedMesh * dist_mesh_ptr =
101 12346 : dynamic_cast<const DistributedMesh *>(&other_mesh_base);
102 12346 : if (!dist_mesh_ptr)
103 0 : return false;
104 28 : const DistributedMesh & other_mesh = *dist_mesh_ptr;
105 :
106 84 : if (_is_serial != other_mesh._is_serial ||
107 12328 : _is_serial_on_proc_0 != other_mesh._is_serial_on_proc_0 ||
108 12346 : _deleted_coarse_elements != other_mesh._deleted_coarse_elements ||
109 12328 : _n_nodes != other_mesh._n_nodes ||
110 12346 : _n_elem != other_mesh._n_elem ||
111 12346 : _max_node_id != other_mesh._max_node_id ||
112 37038 : _max_elem_id != other_mesh._max_elem_id ||
113 : // We expect these things to change in a prepare_for_use();
114 : // they're conceptually "mutable"...
115 : /*
116 : _next_free_local_node_id != other_mesh._next_free_local_node_id ||
117 : _next_free_local_elem_id != other_mesh._next_free_local_elem_id ||
118 : _next_free_unpartitioned_node_id != other_mesh._next_free_unpartitioned_node_id ||
119 : _next_free_unpartitioned_elem_id != other_mesh._next_free_unpartitioned_elem_id ||
120 : #ifdef LIBMESH_ENABLE_UNIQUE_ID
121 : _next_unpartitioned_unique_id != other_mesh._next_unpartitioned_unique_id ||
122 : #endif
123 : */
124 12346 : !this->nodes_and_elements_equal(other_mesh))
125 63 : return false;
126 :
127 12309 : if (_extra_ghost_elems.size() !=
128 26 : other_mesh._extra_ghost_elems.size())
129 0 : return false;
130 12283 : for (auto & elem : _extra_ghost_elems)
131 : {
132 0 : libmesh_assert(this->query_elem_ptr(elem->id()) == elem);
133 0 : const Elem * other_elem = other_mesh.query_elem_ptr(elem->id());
134 0 : if (!other_elem ||
135 0 : !other_mesh._extra_ghost_elems.count(const_cast<Elem *>(other_elem)))
136 0 : return false;
137 : }
138 :
139 26 : return true;
140 : }
141 :
142 346570 : DistributedMesh::~DistributedMesh ()
143 : {
144 324537 : this->DistributedMesh::clear(); // Free nodes and elements
145 346570 : }
146 :
147 :
148 : // This might be specialized later, but right now it's just here to
149 : // make sure the compiler doesn't give us a default (non-deep) copy
150 : // constructor instead.
151 17734 : DistributedMesh::DistributedMesh (const DistributedMesh & other_mesh) :
152 : DistributedMesh(static_cast<const MeshBase &>(other_mesh),
153 17734 : /*bool other_is_distributed_type=*/true)
154 : {
155 17734 : _deleted_coarse_elements = other_mesh._deleted_coarse_elements;
156 17734 : _next_free_local_node_id =
157 17734 : other_mesh._next_free_local_node_id;
158 17734 : _next_free_local_elem_id =
159 17734 : other_mesh._next_free_local_elem_id;
160 17734 : _next_free_unpartitioned_node_id =
161 17734 : other_mesh._next_free_unpartitioned_node_id;
162 17734 : _next_free_unpartitioned_elem_id =
163 17734 : other_mesh._next_free_unpartitioned_elem_id;
164 : #ifdef LIBMESH_ENABLE_UNIQUE_ID
165 17734 : _next_unique_id =
166 17734 : other_mesh._next_unique_id;
167 17734 : _next_unpartitioned_unique_id =
168 17734 : other_mesh._next_unpartitioned_unique_id;
169 : #endif
170 :
171 : // Need to copy extra_ghost_elems
172 17734 : for (auto & elem : other_mesh._extra_ghost_elems)
173 0 : _extra_ghost_elems.insert(this->elem_ptr(elem->id()));
174 17734 : }
175 :
176 :
177 :
178 20361 : DistributedMesh::DistributedMesh (const MeshBase & other_mesh, bool other_is_distributed_type) :
179 20361 : UnstructuredMesh (other_mesh), _is_serial(other_mesh.is_serial()),
180 20455 : _is_serial_on_proc_0(other_mesh.is_serial_on_zero()),
181 20173 : _deleted_coarse_elements(true), // better safe than sorry...
182 20173 : _n_nodes(0), _n_elem(0), _max_node_id(0), _max_elem_id(0), // recomputed below
183 20455 : _next_free_local_node_id(this->processor_id()),
184 20361 : _next_free_local_elem_id(this->processor_id()),
185 20361 : _next_free_unpartitioned_node_id(this->n_processors()),
186 40628 : _next_free_unpartitioned_elem_id(this->n_processors())
187 : {
188 : // Just copy, skipping preparation
189 20361 : this->copy_nodes_and_elements(other_mesh, true, 0, 0, 0, nullptr, true);
190 :
191 188 : this->allow_find_neighbors(other_mesh.allow_find_neighbors());
192 188 : this->allow_detect_interior_parents(other_mesh.allow_detect_interior_parents());
193 188 : this->allow_renumbering(other_mesh.allow_renumbering());
194 188 : this->allow_remote_element_removal(other_mesh.allow_remote_element_removal());
195 188 : this->skip_partitioning(other_mesh.skip_partitioning());
196 :
197 20361 : this->copy_constraint_rows(other_mesh);
198 :
199 94 : auto & this_boundary_info = this->get_boundary_info();
200 94 : const auto & other_boundary_info = other_mesh.get_boundary_info();
201 :
202 20361 : this_boundary_info = other_boundary_info;
203 :
204 94 : this->set_subdomain_name_map() = other_mesh.get_subdomain_name_map();
205 :
206 : // add_node()/add_elem() only increment _n_nodes/_n_elem for
207 : // nodes/elements they consider locally owned (or unpartitioned), so after
208 : // copying only the objects visible to this processor, those counts
209 : // reflect an incomplete local view rather than other_mesh's totals,
210 : // and need to be copied afresh.
211 20361 : _n_nodes = other_mesh.n_nodes();
212 20361 : _n_elem = other_mesh.n_elem();
213 20361 : _max_node_id = other_mesh.max_node_id();
214 20361 : _max_elem_id = other_mesh.max_elem_id();
215 :
216 : // If other_mesh is actually a DistributedMesh, we can just copy its raw
217 : // counters directly, mirroring other_mesh's own bookkeeping exactly. This
218 : // is done in the DistributedMesh copy constructor.
219 : // For any other MeshBase subclass (e.g. ReplicatedMesh) we have no
220 : // raw counterpart fields to copy, so derive the free-id-range
221 : // counters from _max_elem_id/_max_node_id the same way
222 : // update_parallel_id_counts() does via the set_next_ids() method. For
223 : // the unique id counters we copy other_mesh's own local next_unique_id()
224 : // and derive the next available unique ids
225 : // (partitioned and unpartitioned) via set_next_unique_ids().
226 20361 : if (!other_is_distributed_type)
227 : {
228 2627 : this->set_next_ids();
229 : #ifdef LIBMESH_ENABLE_UNIQUE_ID
230 2627 : this->set_next_unique_ids(other_mesh.next_unique_id());
231 : #endif
232 : }
233 :
234 : // Copy other_mesh's actual preparation state, including whatever it
235 : // reports for has_synched_id_counts: the counts above are a faithful copy
236 : // of other_mesh's own (possibly not-yet-synced) counts, not a freshly
237 : // verified computation, so our synced-ness should match other_mesh's.
238 20361 : this->_preparation = other_mesh.preparation();
239 20361 : }
240 :
241 270 : void DistributedMesh::move_nodes_and_elements(MeshBase && other_meshbase)
242 : {
243 4 : DistributedMesh & other_mesh = cast_ref<DistributedMesh&>(other_meshbase);
244 :
245 4 : this->_nodes = std::move(other_mesh._nodes);
246 270 : this->_n_nodes = other_mesh.n_nodes();
247 :
248 4 : this->_elements = std::move(other_mesh._elements);
249 270 : this->_n_elem = other_mesh.n_elem();
250 :
251 270 : _is_serial = other_mesh.is_serial();
252 270 : _is_serial_on_proc_0 = other_mesh.is_serial_on_zero();
253 270 : _deleted_coarse_elements = true; // Better safe than sorry
254 :
255 270 : _max_node_id = other_mesh.max_node_id();
256 270 : _max_elem_id = other_mesh.max_elem_id();
257 :
258 270 : _next_free_local_node_id = other_mesh._next_free_local_node_id;
259 270 : _next_free_local_elem_id = other_mesh._next_free_local_elem_id;
260 270 : _next_free_unpartitioned_node_id = other_mesh._next_free_unpartitioned_node_id;
261 270 : _next_free_unpartitioned_elem_id = other_mesh._next_free_unpartitioned_elem_id;
262 :
263 : #ifdef LIBMESH_ENABLE_UNIQUE_ID
264 270 : _next_unpartitioned_unique_id = other_mesh._next_unpartitioned_unique_id;
265 : #endif
266 270 : }
267 :
268 2072104 : void DistributedMesh::set_next_ids()
269 : {
270 2072104 : _next_free_unpartitioned_elem_id =
271 2073872 : ((_max_elem_id-1) / (this->n_processors() + 1) + 1) *
272 2072104 : (this->n_processors() + 1) + this->n_processors();
273 2072104 : _next_free_local_elem_id =
274 2072104 : ((_max_elem_id + this->n_processors() - 1) / (this->n_processors() + 1) + 1) *
275 2072104 : (this->n_processors() + 1) + this->processor_id();
276 :
277 2072104 : _next_free_unpartitioned_node_id =
278 2073872 : ((_max_node_id-1) / (this->n_processors() + 1) + 1) *
279 2072104 : (this->n_processors() + 1) + this->n_processors();
280 2072104 : _next_free_local_node_id =
281 2072104 : ((_max_node_id + this->n_processors() - 1) / (this->n_processors() + 1) + 1) *
282 2072104 : (this->n_processors() + 1) + this->processor_id();
283 2072104 : }
284 :
285 : #ifdef LIBMESH_ENABLE_UNIQUE_ID
286 2072104 : void DistributedMesh::set_next_unique_ids(unique_id_type parallel_max_unique_id)
287 : {
288 : // Unique ids are laid out in repeating groups of (n_processors()+1)
289 : // consecutive integers ("alignment" here means a value's position, i.e.
290 : // its residue mod (n_processors()+1), within one of these groups). In
291 : // every group, the first n_processors() slots (residues 0..n_processors()-1)
292 : // are each permanently earmarked for one processor's locally-owned
293 : // objects, and the last slot (residue n_processors()) is permanently
294 : // earmarked for unpartitioned objects.
295 : //
296 : // parallel_max_unique_id carries no guarantee of landing on any
297 : // particular residue, so rather than simply offsetting it by
298 : // processor_id()/n_processors(), we have to explicitly round up to the
299 : // start of the next full, entirely-unused group before adding this
300 : // processor's (or the unpartitioned pool's) fixed offset into that group.
301 : //
302 : // (parallel_max_unique_id - 1) / (n_processors()+1) + 1 is the standard
303 : // integer idiom for ceil(parallel_max_unique_id / (n_processors()+1)):
304 : // the number of complete groups needed to reach at least past the
305 : // current max id. Multiplying back by (n_processors()+1) gives the first
306 : // id of the next group that is guaranteed to be entirely unused; adding
307 : // n_processors(), the unpartitioned pool's fixed offset within a group,
308 : // lands on that pool's reserved slot in that group.
309 2072104 : _next_unpartitioned_unique_id =
310 2073872 : ((parallel_max_unique_id - 1) / (this->n_processors() + 1) + 1) *
311 2072104 : (this->n_processors() + 1) +
312 2072104 : this->n_processors();
313 :
314 : // Same idiom, but shifted by (n_processors()-1) before dividing so that
315 : // the rounding accounts for processor_id() possibly being less than the
316 : // residue the raw max id already occupies within its group; this can
317 : // land this processor's slot in the same group as the unpartitioned
318 : // pool's above, or push it into the following group, depending on where
319 : // the max id and this processor's offset happen to fall.
320 2072104 : _next_unique_id =
321 2072104 : ((parallel_max_unique_id + this->n_processors() - 1) / (this->n_processors() + 1) + 1) *
322 2072104 : (this->n_processors() + 1) +
323 2072104 : this->processor_id();
324 2072104 : }
325 : #endif
326 :
327 : // We use cached values for these so they can be called
328 : // from one processor without bothering the rest, but
329 : // we may need to update those caches before doing a full
330 : // renumbering
331 2069477 : void DistributedMesh::update_parallel_id_counts()
332 : {
333 : // This function must be run on all processors at once
334 1694 : parallel_object_only();
335 :
336 2069477 : _n_elem = this->parallel_n_elem();
337 2069477 : _n_nodes = this->parallel_n_nodes();
338 2069477 : _max_node_id = this->parallel_max_node_id();
339 2069477 : _max_elem_id = this->parallel_max_elem_id();
340 :
341 2069477 : this->set_next_ids();
342 :
343 : // set_next_unique_ids() needs the true parallel max unique id (not just
344 : // this processor's possibly-stale cached _next_unique_id) since the whole
345 : // point of this function is to recompute counters that may have drifted
346 : // out of sync.
347 : #ifdef LIBMESH_ENABLE_UNIQUE_ID
348 2069477 : this->set_next_unique_ids(this->parallel_max_unique_id());
349 : #endif
350 :
351 2069477 : this->_preparation.has_synched_id_counts = true;
352 2069477 : }
353 :
354 :
355 : // Or in debug mode we may want to test the uncached values without
356 : // changing the cache
357 2077202 : dof_id_type DistributedMesh::parallel_n_elem() const
358 : {
359 : // This function must be run on all processors at once
360 2542 : parallel_object_only();
361 :
362 2077202 : dof_id_type n_local = this->n_local_elem();
363 2077202 : this->comm().sum(n_local);
364 2077202 : n_local += this->n_unpartitioned_elem();
365 2077202 : return n_local;
366 : }
367 :
368 :
369 :
370 2076473 : dof_id_type DistributedMesh::parallel_max_elem_id() const
371 : {
372 : // This function must be run on all processors at once
373 8690 : parallel_object_only();
374 :
375 2076473 : dof_id_type max_local = 0;
376 :
377 : dofobject_container<Elem>::const_reverse_veclike_iterator
378 8690 : rit = _elements.rbegin();
379 :
380 : const dofobject_container<Elem>::const_reverse_veclike_iterator
381 8690 : rend = _elements.rend();
382 :
383 : // Look for the maximum element id. Search backwards through
384 : // elements so we can break out early. Beware of nullptr entries that
385 : // haven't yet been cleared from _elements.
386 10712292 : for (; rit != rend; ++rit)
387 : {
388 10144384 : const DofObject *d = *rit;
389 19001 : if (d)
390 : {
391 8475 : libmesh_assert(_elements[d->id()] == d);
392 1508565 : max_local = d->id() + 1;
393 1508565 : break;
394 : }
395 : }
396 :
397 2076473 : this->comm().max(max_local);
398 2076473 : return max_local;
399 : }
400 :
401 :
402 :
403 : #ifdef LIBMESH_ENABLE_UNIQUE_ID
404 2156873 : unique_id_type DistributedMesh::parallel_max_unique_id() const
405 : {
406 : // This function must be run on all processors at once
407 1796 : parallel_object_only();
408 :
409 4313746 : unique_id_type max_local = std::max(_next_unique_id,
410 2474782 : _next_unpartitioned_unique_id);
411 2156873 : this->comm().max(max_local);
412 2156873 : return max_local;
413 : }
414 :
415 :
416 :
417 82437 : void DistributedMesh::set_next_unique_id(unique_id_type id)
418 : {
419 148 : _next_unique_id = id;
420 82437 : _next_unpartitioned_unique_id =
421 82585 : ((_next_unique_id-1) / (this->n_processors() + 1) + 1) *
422 82437 : (this->n_processors() + 1) + this->n_processors();
423 82437 : _next_unique_id =
424 82437 : ((_next_unique_id + this->n_processors() - 1) / (this->n_processors() + 1) + 1) *
425 82437 : (this->n_processors() + 1) + this->processor_id();
426 82437 : }
427 : #endif
428 :
429 :
430 :
431 2077650 : dof_id_type DistributedMesh::parallel_n_nodes() const
432 : {
433 : // This function must be run on all processors at once
434 2542 : parallel_object_only();
435 :
436 2077650 : dof_id_type n_local = this->n_local_nodes();
437 2077650 : this->comm().sum(n_local);
438 2077650 : n_local += this->n_unpartitioned_nodes();
439 2077650 : return n_local;
440 : }
441 :
442 :
443 :
444 2074057 : dof_id_type DistributedMesh::parallel_max_node_id() const
445 : {
446 : // This function must be run on all processors at once
447 6274 : parallel_object_only();
448 :
449 2074057 : dof_id_type max_local = 0;
450 :
451 : dofobject_container<Node>::const_reverse_veclike_iterator
452 6274 : rit = _nodes.rbegin();
453 :
454 : const dofobject_container<Node>::const_reverse_veclike_iterator
455 6274 : rend = _nodes.rend();
456 :
457 : // Look for the maximum node id. Search backwards through
458 : // nodes so we can break out early. Beware of nullptr entries that
459 : // haven't yet been cleared from _nodes
460 39798444 : for (; rit != rend; ++rit)
461 : {
462 39230606 : const DofObject *d = *rit;
463 63385 : if (d)
464 : {
465 6129 : libmesh_assert(_nodes[d->id()] == d);
466 1506219 : max_local = d->id() + 1;
467 1506219 : break;
468 : }
469 : }
470 :
471 2074057 : this->comm().max(max_local);
472 2074057 : return max_local;
473 : }
474 :
475 :
476 :
477 44043212 : const Point & DistributedMesh::point (const dof_id_type i) const
478 : {
479 44043212 : return this->node_ref(i);
480 : }
481 :
482 :
483 :
484 45160245 : const Node * DistributedMesh::node_ptr (const dof_id_type i) const
485 : {
486 137681 : libmesh_assert(_nodes[i]);
487 137681 : libmesh_assert_equal_to (_nodes[i]->id(), i);
488 :
489 45160245 : return _nodes[i];
490 : }
491 :
492 :
493 :
494 :
495 657163919 : Node * DistributedMesh::node_ptr (const dof_id_type i)
496 : {
497 490764 : libmesh_assert(_nodes[i]);
498 490764 : libmesh_assert_equal_to (_nodes[i]->id(), i);
499 :
500 657163919 : return _nodes[i];
501 : }
502 :
503 :
504 :
505 :
506 3109834 : const Node * DistributedMesh::query_node_ptr (const dof_id_type i) const
507 : {
508 3109834 : if (const auto it = _nodes.find(i);
509 658111 : it != _nodes.end())
510 : {
511 2821322 : const Node * n = *it;
512 369599 : libmesh_assert (!n || n->id() == i);
513 369599 : return n;
514 : }
515 :
516 288512 : return nullptr;
517 : }
518 :
519 :
520 :
521 :
522 378349430 : Node * DistributedMesh::query_node_ptr (const dof_id_type i)
523 : {
524 378349430 : if (auto it = _nodes.find(i);
525 159506 : it != _nodes.end())
526 : {
527 294011538 : Node * n = *it;
528 111969 : libmesh_assert (!n || n->id() == i);
529 111969 : return n;
530 : }
531 :
532 47537 : return nullptr;
533 : }
534 :
535 :
536 :
537 :
538 3058368 : const Elem * DistributedMesh::elem_ptr (const dof_id_type i) const
539 : {
540 10087 : libmesh_assert(_elements[i]);
541 10087 : libmesh_assert_equal_to (_elements[i]->id(), i);
542 :
543 3058368 : return _elements[i];
544 : }
545 :
546 :
547 :
548 :
549 293017056 : Elem * DistributedMesh::elem_ptr (const dof_id_type i)
550 : {
551 167573 : libmesh_assert(_elements[i]);
552 167573 : libmesh_assert_equal_to (_elements[i]->id(), i);
553 :
554 293017056 : return _elements[i];
555 : }
556 :
557 :
558 :
559 :
560 1458021 : const Elem * DistributedMesh::query_elem_ptr (const dof_id_type i) const
561 : {
562 1458021 : if (const auto it = _elements.find(i);
563 619051 : it != _elements.end())
564 : {
565 1100442 : const Elem * e = *it;
566 270387 : libmesh_assert (!e || e->id() == i);
567 270387 : return e;
568 : }
569 :
570 348664 : return nullptr;
571 : }
572 :
573 :
574 :
575 :
576 615913676 : Elem * DistributedMesh::query_elem_ptr (const dof_id_type i)
577 : {
578 615913676 : if (auto it = _elements.find(i);
579 194370 : it != _elements.end())
580 : {
581 530167257 : Elem * e = *it;
582 190690 : libmesh_assert (!e || e->id() == i);
583 190690 : return e;
584 : }
585 :
586 3680 : return nullptr;
587 : }
588 :
589 :
590 :
591 :
592 44397175 : Elem * DistributedMesh::add_elem (Elem * e)
593 : {
594 : // Don't try to add nullptrs!
595 19053 : libmesh_assert(e);
596 :
597 : // Trying to add an existing element is a no-op
598 44397175 : if (e->valid_id() && _elements[e->id()] == e)
599 0 : return e;
600 :
601 44397175 : const processor_id_type elem_procid = e->processor_id();
602 :
603 44397175 : if (!e->valid_id())
604 : {
605 : // We should only be creating new ids past the end of the range
606 : // of existing ids
607 3568 : libmesh_assert_greater_equal(_next_free_unpartitioned_elem_id,
608 : _max_elem_id);
609 3568 : libmesh_assert_greater_equal(_next_free_local_elem_id, _max_elem_id);
610 :
611 : // Use the unpartitioned ids for unpartitioned elems, and
612 : // temporarily for ghost elems
613 3861201 : dof_id_type * next_id = &_next_free_unpartitioned_elem_id;
614 3864769 : if (elem_procid == this->processor_id())
615 1133684 : next_id = &_next_free_local_elem_id;
616 3861201 : e->set_id (*next_id);
617 : }
618 :
619 : {
620 : // Advance next_ids up high enough that each is pointing to an
621 : // unused id and any subsequent increments will still point us
622 : // to unused ids
623 88794350 : _max_elem_id = std::max(_max_elem_id,
624 44397175 : static_cast<dof_id_type>(e->id()+1));
625 :
626 44397175 : if (_next_free_unpartitioned_elem_id < _max_elem_id)
627 5196206 : _next_free_unpartitioned_elem_id =
628 5196206 : ((_max_elem_id-1) / (this->n_processors() + 1) + 1) *
629 5196206 : (this->n_processors() + 1) + this->n_processors();
630 44397175 : if (_next_free_local_elem_id < _max_elem_id)
631 3488248 : _next_free_local_elem_id =
632 3494529 : ((_max_elem_id + this->n_processors() - 1) / (this->n_processors() + 1) + 1) *
633 3488248 : (this->n_processors() + 1) + this->processor_id();
634 :
635 : #ifndef NDEBUG
636 : // We need a const dofobject_container so we don't inadvertently create
637 : // nullptr entries when testing for non-nullptr ones
638 19053 : const dofobject_container<Elem> & const_elements = _elements;
639 : #endif
640 19053 : libmesh_assert(!const_elements[_next_free_unpartitioned_elem_id]);
641 19053 : libmesh_assert(!const_elements[_next_free_local_elem_id]);
642 : }
643 :
644 : // Don't try to overwrite existing elems
645 19053 : libmesh_assert (!_elements[e->id()]);
646 :
647 44397175 : _elements[e->id()] = e;
648 :
649 : // We actually added a new element. Some of our caches might still
650 : // be valid, but we should clear the ones which definitely are not.
651 44397175 : this->clear_point_locator();
652 44397175 : this->clear_stored_ranges();
653 :
654 : // Try to make the cached elem data more accurate
655 44416228 : if (elem_procid == this->processor_id() ||
656 : elem_procid == DofObject::invalid_processor_id)
657 13246479 : _n_elem++;
658 :
659 : #ifdef LIBMESH_ENABLE_UNIQUE_ID
660 44397175 : if (!e->valid_unique_id())
661 : {
662 13841881 : if (processor_id() == e->processor_id())
663 : {
664 1133684 : e->set_unique_id(_next_unique_id);
665 1133684 : _next_unique_id += this->n_processors() + 1;
666 : }
667 : else
668 : {
669 12708197 : e->set_unique_id(_next_unpartitioned_unique_id);
670 12708197 : _next_unpartitioned_unique_id += this->n_processors() + 1;
671 : }
672 : }
673 : else
674 : {
675 30579671 : _next_unique_id = std::max(_next_unique_id, e->unique_id()+1);
676 30555294 : _next_unique_id =
677 30555294 : ((_next_unique_id + this->n_processors() - 1) / (this->n_processors() + 1) + 1) *
678 30555294 : (this->n_processors() + 1) + this->processor_id();
679 : }
680 : #endif
681 :
682 : // Unpartitioned elems should be added on every processor
683 : // And shouldn't be added in the same batch as ghost elems
684 : // But we might be just adding on processor 0 to
685 : // broadcast later
686 : // #ifdef DEBUG
687 : // if (elem_procid == DofObject::invalid_processor_id)
688 : // {
689 : // dof_id_type elem_id = e->id();
690 : // this->comm().max(elem_id);
691 : // libmesh_assert_equal_to (elem_id, e->id());
692 : // }
693 : // #endif
694 :
695 : // Make sure any new element is given space for any extra integers
696 : // we've requested
697 44397175 : e->add_extra_integers(_elem_integer_names.size(),
698 44397175 : _elem_integer_default_values);
699 :
700 : // And set mapping type and data on any new element
701 38106 : e->set_mapping_type(this->default_mapping_type());
702 38106 : e->set_mapping_data(this->default_mapping_data());
703 :
704 44397175 : return e;
705 : }
706 :
707 :
708 :
709 15081920 : Elem * DistributedMesh::add_elem (std::unique_ptr<Elem> e)
710 : {
711 : // The mesh now takes ownership of the Elem. Eventually the guts of
712 : // add_elem() will get moved to a private helper function, and
713 : // calling add_elem() directly will be deprecated.
714 15081920 : return add_elem(e.release());
715 : }
716 :
717 :
718 :
719 3527180 : Elem * DistributedMesh::insert_elem (Elem * e)
720 : {
721 3527180 : if (_elements[e->id()])
722 3527180 : this->delete_elem(_elements[e->id()]);
723 :
724 : #ifdef LIBMESH_ENABLE_UNIQUE_ID
725 3527180 : if (!e->valid_unique_id())
726 : {
727 0 : if (processor_id() == e->processor_id())
728 : {
729 0 : e->set_unique_id(_next_unique_id);
730 0 : _next_unique_id += this->n_processors() + 1;
731 : }
732 : else
733 : {
734 0 : e->set_unique_id(_next_unpartitioned_unique_id);
735 0 : _next_unpartitioned_unique_id += this->n_processors() + 1;
736 : }
737 : }
738 : else
739 : {
740 3527180 : _next_unique_id = std::max(_next_unique_id, e->unique_id()+1);
741 3527180 : _next_unique_id =
742 3530876 : ((_next_unique_id + this->n_processors() - 1) / (this->n_processors() + 1) + 1) *
743 3527180 : (this->n_processors() + 1) + this->processor_id();
744 : }
745 : #endif
746 :
747 : // Try to make the cached elem data more accurate
748 3527180 : processor_id_type elem_procid = e->processor_id();
749 3530876 : if (elem_procid == this->processor_id() ||
750 : elem_procid == DofObject::invalid_processor_id)
751 3367608 : _n_elem++;
752 :
753 3527180 : _elements[e->id()] = e;
754 :
755 : // We actually added a new element. Some of our caches might still
756 : // be valid, but we should clear the ones which definitely are not.
757 3527180 : this->clear_point_locator();
758 3527180 : this->clear_stored_ranges();
759 :
760 : // Make sure any new element is given space for any extra integers
761 : // we've requested
762 3527180 : e->add_extra_integers(_elem_integer_names.size(),
763 3527180 : _elem_integer_default_values);
764 :
765 : // And set mapping type and data on any new element
766 7392 : e->set_mapping_type(this->default_mapping_type());
767 7392 : e->set_mapping_data(this->default_mapping_data());
768 :
769 3527180 : return e;
770 : }
771 :
772 3527180 : Elem * DistributedMesh::insert_elem (std::unique_ptr<Elem> e)
773 : {
774 : // The mesh now takes ownership of the Elem. Eventually the guts of
775 : // insert_elem(Elem*) will get moved to a private helper function, and
776 : // calling insert_elem(Elem*) directly will be deprecated.
777 3527180 : return insert_elem(e.release());
778 : }
779 :
780 :
781 40408853 : void DistributedMesh::delete_elem(Elem * e)
782 : {
783 7610 : libmesh_assert (e);
784 :
785 : // Try to make the cached elem data more accurate
786 40408853 : _n_elem--;
787 :
788 : // Was this a coarse element, not just a coarsening where we still
789 : // have some ancestor structure? Was it a *local* element, that we
790 : // might have been depending on as an owner of local nodes? We'll
791 : // have to be more careful with our nodes in contract() later; no
792 : // telling if we just locally orphaned a node that should be
793 : // globally retained.
794 40418283 : if (e->processor_id() == this->processor_id() &&
795 3640 : !e->parent())
796 88153 : _deleted_coarse_elements = true;
797 :
798 : // Delete the element from the BoundaryInfo object
799 40408853 : this->get_boundary_info().remove(e);
800 :
801 : // But not yet from the container; we might invalidate
802 : // an iterator that way!
803 :
804 : //_elements.erase(e->id());
805 :
806 : // Instead, we set it to nullptr for now
807 :
808 40408853 : _elements[e->id()] = nullptr;
809 :
810 : // delete the element
811 40408853 : delete e;
812 :
813 : // Some of our caches might still be valid, but we should clear the
814 : // ones which definitely are not.
815 40408853 : this->clear_point_locator();
816 40408853 : this->clear_stored_ranges();
817 40408853 : }
818 :
819 :
820 :
821 3208092 : void DistributedMesh::renumber_elem(const dof_id_type old_id,
822 : const dof_id_type new_id)
823 : {
824 : // This could be a no-op
825 3208092 : if (old_id == new_id)
826 0 : return;
827 :
828 3208092 : Elem * el = _elements[old_id];
829 1160 : libmesh_assert (el);
830 1160 : libmesh_assert_equal_to (el->id(), old_id);
831 :
832 3208092 : el->set_id(new_id);
833 1160 : libmesh_assert (!_elements[new_id]);
834 3208092 : _elements[new_id] = el;
835 3208092 : _elements.erase(old_id);
836 :
837 : // Should we delete any caches here? Our point locator indexes by
838 : // element pointer and should be fine with an id change. Our stored
839 : // ranges are no longer sorted, which is *probably* fine, but let's
840 : // just be safe.
841 3208092 : this->clear_stored_ranges();
842 : }
843 :
844 :
845 :
846 42301929 : Node * DistributedMesh::add_point (const Point & p,
847 : const dof_id_type id,
848 : const processor_id_type proc_id)
849 : {
850 42301929 : Node * old_n = this->query_node_ptr(id);
851 :
852 42301929 : if (old_n)
853 : {
854 0 : *old_n = p;
855 0 : old_n->processor_id() = proc_id;
856 :
857 0 : return old_n;
858 : }
859 :
860 42261890 : Node * n = Node::build(p, id).release();
861 42301929 : n->processor_id() = proc_id;
862 :
863 42301929 : return DistributedMesh::add_node(n);
864 : }
865 :
866 :
867 4130 : void DistributedMesh::own_node (Node & n)
868 : {
869 : // This had better be a node in our mesh
870 0 : libmesh_assert(_nodes[n.id()] == &n);
871 :
872 4130 : _nodes[n.id()] = nullptr;
873 4130 : _n_nodes--;
874 :
875 0 : n.set_id(DofObject::invalid_id);
876 4130 : n.processor_id() = this->processor_id();
877 :
878 4130 : this->add_node(&n);
879 4130 : }
880 :
881 :
882 73134912 : Node * DistributedMesh::add_node (Node * n)
883 : {
884 : // Don't try to add nullptrs!
885 41751 : libmesh_assert(n);
886 :
887 : // Trying to add an existing node is a no-op
888 73134912 : if (n->valid_id() && _nodes[n->id()] == n)
889 0 : return n;
890 :
891 73134912 : const processor_id_type node_procid = n->processor_id();
892 :
893 73134912 : if (!n->valid_id())
894 : {
895 : // We should only be creating new ids past the end of the range
896 : // of existing ids
897 17829 : libmesh_assert_greater_equal(_next_free_unpartitioned_node_id,
898 : _max_node_id);
899 17829 : libmesh_assert_greater_equal(_next_free_local_node_id, _max_node_id);
900 :
901 : // Use the unpartitioned ids for unpartitioned nodes,
902 : // and temporarily for ghost nodes
903 17819017 : dof_id_type * next_id = &_next_free_unpartitioned_node_id;
904 17836846 : if (node_procid == this->processor_id())
905 2163672 : next_id = &_next_free_local_node_id;
906 17819017 : n->set_id (*next_id);
907 : }
908 :
909 : {
910 : // Advance next_ids up high enough that each is pointing to an
911 : // unused id and any subsequent increments will still point us
912 : // to unused ids
913 146269824 : _max_node_id = std::max(_max_node_id,
914 73134912 : static_cast<dof_id_type>(n->id()+1));
915 :
916 73134912 : if (_next_free_unpartitioned_node_id < _max_node_id)
917 20799006 : _next_free_unpartitioned_node_id =
918 20799006 : ((_max_node_id-1) / (this->n_processors() + 1) + 1) *
919 20799006 : (this->n_processors() + 1) + this->n_processors();
920 73134912 : if (_next_free_local_node_id < _max_node_id)
921 12110268 : _next_free_local_node_id =
922 12128470 : ((_max_node_id + this->n_processors() - 1) / (this->n_processors() + 1) + 1) *
923 12110268 : (this->n_processors() + 1) + this->processor_id();
924 :
925 : #ifndef NDEBUG
926 : // We need a const dofobject_container so we don't inadvertently create
927 : // nullptr entries when testing for non-nullptr ones
928 41751 : const dofobject_container<Node> & const_nodes = _nodes;
929 : #endif
930 41751 : libmesh_assert(!const_nodes[_next_free_unpartitioned_node_id]);
931 41751 : libmesh_assert(!const_nodes[_next_free_local_node_id]);
932 : }
933 :
934 : // Don't try to overwrite existing nodes
935 41751 : libmesh_assert (!_nodes[n->id()]);
936 :
937 73134912 : _nodes[n->id()] = n;
938 :
939 : // Try to make the cached node data more accurate
940 73176663 : if (node_procid == this->processor_id() ||
941 : node_procid == DofObject::invalid_processor_id)
942 39729153 : _n_nodes++;
943 :
944 : #ifdef LIBMESH_ENABLE_UNIQUE_ID
945 73134912 : if (!n->valid_unique_id())
946 : {
947 42304001 : if (processor_id() == n->processor_id())
948 : {
949 2581194 : n->set_unique_id(_next_unique_id);
950 2581194 : _next_unique_id += this->n_processors() + 1;
951 : }
952 : else
953 : {
954 39722807 : n->set_unique_id(_next_unpartitioned_unique_id);
955 39722807 : _next_unpartitioned_unique_id += this->n_processors() + 1;
956 : }
957 : }
958 : else
959 : {
960 30961496 : _next_unique_id = std::max(_next_unique_id, n->unique_id()+1);
961 30830911 : _next_unique_id =
962 30830911 : ((_next_unique_id + this->n_processors() - 1) / (this->n_processors() + 1) + 1) *
963 30830911 : (this->n_processors() + 1) + this->processor_id();
964 : }
965 : #endif
966 :
967 73134912 : n->add_extra_integers(_node_integer_names.size(),
968 73134912 : _node_integer_default_values);
969 :
970 : // Unpartitioned nodes should be added on every processor
971 : // And shouldn't be added in the same batch as ghost nodes
972 : // But we might be just adding on processor 0 to
973 : // broadcast later
974 : // #ifdef DEBUG
975 : // if (node_procid == DofObject::invalid_processor_id)
976 : // {
977 : // dof_id_type node_id = n->id();
978 : // this->comm().max(node_id);
979 : // libmesh_assert_equal_to (node_id, n->id());
980 : // }
981 : // #endif
982 :
983 73134912 : return n;
984 : }
985 :
986 30828853 : Node * DistributedMesh::add_node (std::unique_ptr<Node> n)
987 : {
988 : // The mesh now takes ownership of the Node. Eventually the guts of
989 : // add_node() will get moved to a private helper function, and
990 : // calling add_node() directly will be deprecated.
991 30828853 : return add_node(n.release());
992 : }
993 :
994 50880056 : void DistributedMesh::delete_node(Node * n)
995 : {
996 4497 : libmesh_assert(n);
997 4497 : libmesh_assert(_nodes[n->id()]);
998 :
999 : // Try to make the cached elem data more accurate
1000 50880056 : _n_nodes--;
1001 :
1002 : // Delete the node from the BoundaryInfo object
1003 50880056 : this->get_boundary_info().remove(n);
1004 8994 : _constraint_rows.erase(n);
1005 :
1006 : // But not yet from the container; we might invalidate
1007 : // an iterator that way!
1008 :
1009 : //_nodes.erase(n->id());
1010 :
1011 : // Instead, we set it to nullptr for now
1012 :
1013 50880056 : _nodes[n->id()] = nullptr;
1014 :
1015 : // delete the node
1016 50880056 : delete n;
1017 50880056 : }
1018 :
1019 :
1020 :
1021 7053540 : void DistributedMesh::renumber_node(const dof_id_type old_id,
1022 : const dof_id_type new_id)
1023 : {
1024 : // This could be a no-op
1025 7053540 : if (old_id == new_id)
1026 0 : return;
1027 :
1028 7052957 : Node * nd = _nodes[old_id];
1029 3234 : libmesh_assert (nd);
1030 3234 : libmesh_assert_equal_to (nd->id(), old_id);
1031 :
1032 : // If we have nodes shipped to this processor for NodeConstraints
1033 : // use, then those nodes will exist in _nodes, but may not be
1034 : // locatable via a TopologyMap due to the insufficiency of elements
1035 : // connecting to them. If local refinement then wants to create a
1036 : // *new* node in the same location, it will initially get a temporary
1037 : // id, and then make_node_ids_parallel_consistent() will try to move
1038 : // it to the canonical id. We need to account for this case to
1039 : // avoid false positives and memory leaks.
1040 : #ifdef LIBMESH_ENABLE_NODE_CONSTRAINTS
1041 6468 : if (_nodes[new_id])
1042 : {
1043 0 : libmesh_assert_equal_to (*(Point *)_nodes[new_id],
1044 : *(Point *)_nodes[old_id]);
1045 0 : _nodes.erase(new_id);
1046 : }
1047 : #else
1048 : // If we aren't shipping nodes for NodeConstraints, there should be
1049 : // no reason for renumbering one node onto another.
1050 : libmesh_assert (!_nodes[new_id]);
1051 : #endif
1052 7052957 : _nodes[new_id] = nd;
1053 7052957 : nd->set_id(new_id);
1054 :
1055 7052957 : _nodes.erase(old_id);
1056 : }
1057 :
1058 :
1059 :
1060 607846 : void DistributedMesh::clear ()
1061 : {
1062 : // Call parent clear function
1063 607846 : MeshBase::clear();
1064 :
1065 : // Clear our elements and nodes
1066 : // There is no need to remove them from
1067 : // the BoundaryInfo data structure since we
1068 : // already cleared it.
1069 607846 : this->DistributedMesh::clear_elems();
1070 :
1071 23555141 : for (auto & node : _nodes)
1072 42665606 : delete node;
1073 :
1074 582 : _nodes.clear();
1075 :
1076 : // We're no longer distributed if we were before
1077 607846 : _is_serial = true;
1078 607846 : _is_serial_on_proc_0 = true;
1079 :
1080 : // We deleted a ton of coarse elements, but their nodes got deleted too so
1081 : // all is copacetic.
1082 607846 : _deleted_coarse_elements = false;
1083 :
1084 : // Correct our caches
1085 607846 : _n_nodes = 0;
1086 607846 : _max_node_id = 0;
1087 608428 : _next_free_local_node_id = this->processor_id();
1088 607846 : _next_free_unpartitioned_node_id = this->n_processors();
1089 607846 : }
1090 :
1091 :
1092 :
1093 614246 : void DistributedMesh::clear_elems ()
1094 : {
1095 9501111 : for (auto & elem : _elements)
1096 8886865 : delete elem;
1097 :
1098 582 : _elements.clear();
1099 :
1100 : // Correct our caches
1101 614246 : _n_elem = 0;
1102 614246 : _max_elem_id = 0;
1103 614828 : _next_free_local_elem_id = this->processor_id();
1104 614246 : _next_free_unpartitioned_elem_id = this->n_processors();
1105 614246 : }
1106 :
1107 :
1108 :
1109 607344 : void DistributedMesh::redistribute ()
1110 : {
1111 : // If this is a truly parallel mesh, go through the redistribution/gather/delete remote steps
1112 607344 : if (!this->is_serial())
1113 : {
1114 : // Construct a MeshCommunication object to actually redistribute the nodes
1115 : // and elements according to the partitioner, and then to re-gather the neighbors.
1116 : MeshCommunication mc;
1117 72228 : mc.redistribute(*this);
1118 :
1119 72228 : this->update_parallel_id_counts();
1120 :
1121 : // We ought to still have valid neighbor links; we communicate
1122 : // them for newly-redistributed elements
1123 : // this->find_neighbors();
1124 :
1125 : // Is this necessary? If we are called from prepare_for_use(), this will be called
1126 : // anyway... but users can always call partition directly, in which case we do need
1127 : // to call delete_remote_elements()...
1128 : //
1129 : // Regardless of whether it's necessary, it isn't safe. We
1130 : // haven't communicated new node processor_ids yet, and we can't
1131 : // delete nodes until we do.
1132 : // this->delete_remote_elements();
1133 : }
1134 : else
1135 : // The base class can handle non-distributed things, like
1136 : // notifying any GhostingFunctors of changes
1137 535116 : MeshBase::redistribute();
1138 607344 : }
1139 :
1140 :
1141 :
1142 519294 : void DistributedMesh::update_post_partitioning ()
1143 : {
1144 : // this->recalculate_n_partitions();
1145 :
1146 : // Let's do the base class cache clearing first, just in case our
1147 : // later computations are ever changed to make use of a local
1148 : // elements range cache
1149 519294 : this->MeshBase::update_post_partitioning();
1150 :
1151 : // Partitioning changes our numbers of unpartitioned objects
1152 519294 : this->update_parallel_id_counts();
1153 519294 : }
1154 :
1155 :
1156 :
1157 : template <typename T>
1158 3356 : void DistributedMesh::libmesh_assert_valid_parallel_object_ids(const dofobject_container<T> & objects) const
1159 : {
1160 : // This function must be run on all processors at once
1161 3356 : parallel_object_only();
1162 :
1163 3356 : const dof_id_type pmax_node_id = this->parallel_max_node_id();
1164 3356 : const dof_id_type pmax_elem_id = this->parallel_max_elem_id();
1165 3356 : const dof_id_type pmax_id = std::max(pmax_node_id, pmax_elem_id);
1166 :
1167 894268 : for (dof_id_type i=0; i != pmax_id; ++i)
1168 : {
1169 890912 : T * obj = objects[i]; // Returns nullptr if there's no map entry
1170 :
1171 : // Local lookups by id should return the requested object
1172 890912 : libmesh_assert(!obj || obj->id() == i);
1173 :
1174 : // All processors with an object should agree on id
1175 : #ifndef NDEBUG
1176 890912 : const dof_id_type dofid = obj && obj->valid_id() ?
1177 : obj->id() : DofObject::invalid_id;
1178 890912 : libmesh_assert(this->comm().semiverify(obj ? &dofid : nullptr));
1179 : #endif
1180 :
1181 : // All processors with an object should agree on processor id
1182 890912 : const dof_id_type procid = obj && obj->valid_processor_id() ?
1183 : obj->processor_id() : DofObject::invalid_processor_id;
1184 890912 : libmesh_assert(this->comm().semiverify(obj ? &procid : nullptr));
1185 :
1186 890912 : dof_id_type min_procid = procid;
1187 890912 : this->comm().min(min_procid);
1188 :
1189 : // Either:
1190 : // 1.) I own this elem (min_procid == this->processor_id()) *and* I have a valid pointer to it (obj != nullptr)
1191 : // or
1192 : // 2.) I don't own this elem (min_procid != this->processor_id()). (In this case I may or may not have a valid pointer to it.)
1193 :
1194 : // Original assert logic
1195 : // libmesh_assert (min_procid != this->processor_id() || obj);
1196 :
1197 : // More human-understandable logic...
1198 890912 : libmesh_assert (
1199 : ((min_procid == this->processor_id()) && obj)
1200 : ||
1201 : (min_procid != this->processor_id())
1202 : );
1203 :
1204 : #if defined(LIBMESH_ENABLE_UNIQUE_ID) && !defined(NDEBUG)
1205 : // All processors with an object should agree on unique id
1206 890912 : const unique_id_type uniqueid = obj ? obj->unique_id() : 0;
1207 890912 : libmesh_assert(this->comm().semiverify(obj ? &uniqueid : nullptr));
1208 : #endif
1209 : }
1210 3356 : }
1211 :
1212 :
1213 :
1214 1678 : void DistributedMesh::libmesh_assert_valid_parallel_ids () const
1215 : {
1216 1678 : this->libmesh_assert_valid_parallel_object_ids (this->_elements);
1217 1678 : this->libmesh_assert_valid_parallel_object_ids (this->_nodes);
1218 1678 : }
1219 :
1220 :
1221 :
1222 402 : void DistributedMesh::libmesh_assert_valid_parallel_p_levels () const
1223 : {
1224 : #ifndef NDEBUG
1225 : // This function must be run on all processors at once
1226 402 : parallel_object_only();
1227 :
1228 402 : dof_id_type pmax_elem_id = this->parallel_max_elem_id();
1229 :
1230 21958 : for (dof_id_type i=0; i != pmax_elem_id; ++i)
1231 : {
1232 21556 : Elem * el = _elements[i]; // Returns nullptr if there's no map entry
1233 :
1234 21556 : unsigned int p_level = el ? (el->p_level()) : libMesh::invalid_uint;
1235 :
1236 : // All processors with an active element should agree on p level
1237 21556 : libmesh_assert(this->comm().semiverify((el && el->active()) ? &p_level : nullptr));
1238 : }
1239 : #endif
1240 402 : }
1241 :
1242 :
1243 :
1244 :
1245 1626 : void DistributedMesh::libmesh_assert_valid_parallel_flags () const
1246 : {
1247 : #if defined(LIBMESH_ENABLE_AMR) && !defined(NDEBUG)
1248 : // This function must be run on all processors at once
1249 1626 : parallel_object_only();
1250 :
1251 1626 : dof_id_type pmax_elem_id = this->parallel_max_elem_id();
1252 :
1253 144668 : for (dof_id_type i=0; i != pmax_elem_id; ++i)
1254 : {
1255 143042 : Elem * el = _elements[i]; // Returns nullptr if there's no map entry
1256 :
1257 143042 : unsigned int refinement_flag = el ?
1258 65709 : static_cast<unsigned int> (el->refinement_flag()) : libMesh::invalid_uint;
1259 143042 : unsigned int p_refinement_flag = el ?
1260 65709 : static_cast<unsigned int> (el->p_refinement_flag()) : libMesh::invalid_uint;
1261 :
1262 143042 : libmesh_assert(this->comm().semiverify(el ? &refinement_flag : nullptr));
1263 :
1264 : // p refinement flags aren't always kept correct on inactive
1265 : // ghost elements
1266 143042 : libmesh_assert(this->comm().semiverify((el && el->active()) ? &p_refinement_flag : nullptr));
1267 : }
1268 : #endif // LIBMESH_ENABLE_AMR
1269 1626 : }
1270 :
1271 :
1272 :
1273 : template <typename T>
1274 : dof_id_type
1275 1872574 : DistributedMesh::renumber_dof_objects(dofobject_container<T> & objects)
1276 : {
1277 : // This function must be run on all processors at once
1278 800 : parallel_object_only();
1279 :
1280 : typedef typename dofobject_container<T>::veclike_iterator object_iterator;
1281 :
1282 : // In parallel we may not know what objects other processors have.
1283 : // Start by figuring out how many
1284 800 : dof_id_type unpartitioned_objects = 0;
1285 :
1286 : std::unordered_map<processor_id_type, dof_id_type>
1287 1600 : ghost_objects_from_proc;
1288 :
1289 1872574 : object_iterator it = objects.begin();
1290 800 : object_iterator end = objects.end();
1291 :
1292 113903200 : while (it != end)
1293 : {
1294 112030626 : T * obj = *it;
1295 :
1296 : // Remove any nullptr container entries while we're here.
1297 112030626 : if (!obj)
1298 1450384 : it = objects.erase(it);
1299 : else
1300 : {
1301 110580242 : processor_id_type obj_procid = obj->processor_id();
1302 110580242 : if (obj_procid == DofObject::invalid_processor_id)
1303 43715475 : unpartitioned_objects++;
1304 : else
1305 66864767 : ghost_objects_from_proc[obj_procid]++;
1306 :
1307 : // Finally, increment the iterator
1308 56414 : ++it;
1309 : }
1310 : }
1311 :
1312 1874174 : std::vector<dof_id_type> objects_on_proc(this->n_processors(), 0);
1313 1873374 : auto this_it = ghost_objects_from_proc.find(this->processor_id());
1314 800 : this->comm().allgather
1315 1873065 : ((this_it == ghost_objects_from_proc.end()) ?
1316 491 : dof_id_type(0) : this_it->second, objects_on_proc);
1317 :
1318 : #ifndef NDEBUG
1319 800 : libmesh_assert(this->comm().verify(unpartitioned_objects));
1320 2400 : for (processor_id_type p=0, np=this->n_processors(); p != np; ++p)
1321 1600 : if (ghost_objects_from_proc.count(p))
1322 951 : libmesh_assert_less_equal (ghost_objects_from_proc[p], objects_on_proc[p]);
1323 : else
1324 649 : libmesh_assert_less_equal (0, objects_on_proc[p]);
1325 : #endif
1326 :
1327 : // We'll renumber objects in blocks by processor id
1328 1874174 : std::vector<dof_id_type> first_object_on_proc(this->n_processors());
1329 19872302 : for (processor_id_type i=1, np=this->n_processors(); i != np; ++i)
1330 18002928 : first_object_on_proc[i] = first_object_on_proc[i-1] +
1331 800 : objects_on_proc[i-1];
1332 1873374 : dof_id_type next_id = first_object_on_proc[this->processor_id()];
1333 1872574 : dof_id_type first_free_id =
1334 1874174 : first_object_on_proc[this->n_processors()-1] +
1335 800 : objects_on_proc[this->n_processors()-1] +
1336 : unpartitioned_objects;
1337 :
1338 : // First set new local object ids and build request sets
1339 : // for non-local object ids
1340 :
1341 : // Request sets to send to each processor
1342 : std::map<processor_id_type, std::vector<dof_id_type>>
1343 800 : requested_ids;
1344 :
1345 : // We know how many objects live on each processor, so reserve() space for
1346 : // each.
1347 800 : auto ghost_end = ghost_objects_from_proc.end();
1348 21744876 : for (auto p : make_range(this->n_processors()))
1349 19873902 : if (p != this->processor_id())
1350 : {
1351 18000528 : if (const auto p_it = ghost_objects_from_proc.find(p);
1352 800 : p_it != ghost_end)
1353 3582454 : requested_ids[p].reserve(p_it->second);
1354 : }
1355 :
1356 800 : end = objects.end();
1357 112452816 : for (it = objects.begin(); it != end; ++it)
1358 : {
1359 110580242 : T * obj = *it;
1360 110580242 : if (!obj)
1361 0 : continue;
1362 110636656 : if (obj->processor_id() == this->processor_id())
1363 20247142 : obj->set_id(next_id++);
1364 90333100 : else if (obj->processor_id() != DofObject::invalid_processor_id)
1365 46617625 : requested_ids[obj->processor_id()].push_back(obj->id());
1366 : }
1367 :
1368 : // Next set ghost object ids from other processors
1369 :
1370 1889745 : auto gather_functor =
1371 50166657 : [
1372 : #ifndef NDEBUG
1373 : this,
1374 : &first_object_on_proc,
1375 : &objects_on_proc,
1376 : #endif
1377 : &objects]
1378 : (processor_id_type, const std::vector<dof_id_type> & ids,
1379 920 : std::vector<dof_id_type> & new_ids)
1380 : {
1381 920 : std::size_t ids_size = ids.size();
1382 3582454 : new_ids.resize(ids_size);
1383 :
1384 50200079 : for (std::size_t i=0; i != ids_size; ++i)
1385 : {
1386 46617625 : T * obj = objects[ids[i]];
1387 16251 : libmesh_assert(obj);
1388 16251 : libmesh_assert_equal_to (obj->processor_id(), this->processor_id());
1389 46633876 : new_ids[i] = obj->id();
1390 :
1391 16251 : libmesh_assert_greater_equal (new_ids[i],
1392 : first_object_on_proc[this->processor_id()]);
1393 16251 : libmesh_assert_less (new_ids[i],
1394 : first_object_on_proc[this->processor_id()] +
1395 : objects_on_proc[this->processor_id()]);
1396 : }
1397 : };
1398 :
1399 1889745 : auto action_functor =
1400 50166657 : [
1401 : #ifndef NDEBUG
1402 : &first_object_on_proc,
1403 : &objects_on_proc,
1404 : #endif
1405 : &objects]
1406 : (processor_id_type libmesh_dbg_var(pid),
1407 : const std::vector<dof_id_type> & ids,
1408 920 : const std::vector<dof_id_type> & data)
1409 : {
1410 : // Copy the id changes we've now been informed of
1411 50200079 : for (auto i : index_range(ids))
1412 : {
1413 46617625 : T * obj = objects[ids[i]];
1414 16251 : libmesh_assert (obj);
1415 16251 : libmesh_assert_equal_to (obj->processor_id(), pid);
1416 16251 : libmesh_assert_greater_equal (data[i],
1417 : first_object_on_proc[pid]);
1418 16251 : libmesh_assert_less (data[i],
1419 : first_object_on_proc[pid] +
1420 : objects_on_proc[pid]);
1421 46633876 : obj->set_id(data[i]);
1422 : }
1423 : };
1424 :
1425 800 : const dof_id_type * ex = nullptr;
1426 : Parallel::pull_parallel_vector_data
1427 1872574 : (this->comm(), requested_ids, gather_functor, action_functor, ex);
1428 :
1429 : #ifdef LIBMESH_ENABLE_UNIQUE_ID
1430 1889745 : auto unique_gather_functor =
1431 50166657 : [
1432 : #ifndef NDEBUG
1433 : this,
1434 : #endif
1435 : &objects]
1436 : (processor_id_type, const std::vector<dof_id_type> & ids,
1437 920 : std::vector<unique_id_type> & data)
1438 : {
1439 920 : std::size_t ids_size = ids.size();
1440 3582454 : data.resize(ids_size);
1441 :
1442 50200079 : for (std::size_t i=0; i != ids_size; ++i)
1443 : {
1444 46617625 : T * obj = objects[ids[i]];
1445 16251 : libmesh_assert(obj);
1446 16251 : libmesh_assert_equal_to (obj->processor_id(), this->processor_id());
1447 93235250 : data[i] = obj->valid_unique_id() ? obj->unique_id() : DofObject::invalid_unique_id;
1448 : }
1449 : };
1450 :
1451 1889745 : auto unique_action_functor =
1452 50166657 : [&objects]
1453 : (processor_id_type libmesh_dbg_var(pid),
1454 : const std::vector<dof_id_type> & ids,
1455 920 : const std::vector<unique_id_type> & data)
1456 : {
1457 50200079 : for (auto i : index_range(ids))
1458 : {
1459 46617625 : T * obj = objects[ids[i]];
1460 16251 : libmesh_assert (obj);
1461 16251 : libmesh_assert_equal_to (obj->processor_id(), pid);
1462 46617625 : if (!obj->valid_unique_id() && data[i] != DofObject::invalid_unique_id)
1463 0 : obj->set_unique_id(data[i]);
1464 : }
1465 : };
1466 :
1467 800 : const unique_id_type * unique_ex = nullptr;
1468 : Parallel::pull_parallel_vector_data
1469 1872574 : (this->comm(), requested_ids, unique_gather_functor,
1470 : unique_action_functor, unique_ex);
1471 : #endif
1472 :
1473 : // Next set unpartitioned object ids
1474 800 : next_id = 0;
1475 21744876 : for (auto i : make_range(this->n_processors()))
1476 19873902 : next_id += objects_on_proc[i];
1477 112452816 : for (it = objects.begin(); it != end; ++it)
1478 : {
1479 110580242 : T * obj = *it;
1480 110580242 : if (!obj)
1481 0 : continue;
1482 110580242 : if (obj->processor_id() == DofObject::invalid_processor_id)
1483 43715475 : obj->set_id(next_id++);
1484 : }
1485 :
1486 : // Finally shuffle around objects so that container indices
1487 : // match ids
1488 1872574 : it = objects.begin();
1489 800 : end = objects.end();
1490 119049574 : while (it != end)
1491 : {
1492 117177000 : T * obj = *it;
1493 117177000 : if (obj) // don't try shuffling already-nullptr entries
1494 : {
1495 117177000 : T * next = objects[obj->id()];
1496 : // If we have to move this object
1497 117177000 : if (next != obj)
1498 : {
1499 : // nullptr out its original position for now
1500 : // (our shuffling may put another object there shortly)
1501 40782501 : *it = nullptr;
1502 :
1503 : // There may already be another object with this id that
1504 : // needs to be moved itself
1505 52935281 : while (next)
1506 : {
1507 : // We shouldn't be trying to give two objects the
1508 : // same id
1509 9329 : libmesh_assert_not_equal_to (next->id(), obj->id());
1510 12152780 : objects[obj->id()] = obj;
1511 9329 : obj = next;
1512 12152780 : next = objects[obj->id()];
1513 : }
1514 40782501 : objects[obj->id()] = obj;
1515 : }
1516 : }
1517 :
1518 : // Remove any container entries that were left as nullptr.
1519 115296 : if (!obj)
1520 0 : it = objects.erase(it);
1521 : else
1522 57648 : ++it;
1523 : }
1524 :
1525 1873374 : return first_free_id;
1526 : }
1527 :
1528 :
1529 937957 : void DistributedMesh::renumber_nodes_and_elements ()
1530 : {
1531 402 : parallel_object_only();
1532 :
1533 : #ifdef DEBUG
1534 : // Make sure our ids and flags are consistent
1535 402 : this->libmesh_assert_valid_parallel_ids();
1536 402 : this->libmesh_assert_valid_parallel_flags();
1537 402 : this->libmesh_assert_valid_parallel_p_levels();
1538 : #endif
1539 :
1540 402 : LOG_SCOPE("renumber_nodes_and_elements()", "DistributedMesh");
1541 :
1542 : // Nodes not connected to any elements, and nullptr node entries
1543 : // in our container, should be deleted. But wait! If we've deleted coarse
1544 : // local elements on some processor, other processors might have ghosted
1545 : // nodes from it that are now no longer connected to any elements on it, but
1546 : // that are connected to their own semilocal elements. We'll have to
1547 : // communicate to ascertain if that's the case.
1548 937957 : this->comm().max(_deleted_coarse_elements);
1549 :
1550 : // What used nodes do we see on our proc?
1551 402 : std::set<dof_id_type> used_nodes;
1552 :
1553 : // What used node info should we send from our proc? Could we take ownership
1554 : // of each node if we needed to?
1555 : std::map<processor_id_type, std::map<dof_id_type, bool>>
1556 402 : used_nodes_on_proc;
1557 :
1558 : // flag the nodes we need
1559 58751092 : for (auto & elem : this->element_ptr_range())
1560 306407997 : for (const Node & node : elem->node_ref_range())
1561 : {
1562 277940793 : const dof_id_type n = node.id();
1563 277796423 : used_nodes.insert(n);
1564 277940793 : if (_deleted_coarse_elements)
1565 : {
1566 3706021 : const processor_id_type p = node.processor_id();
1567 3706305 : if (p != this->processor_id())
1568 : {
1569 2706008 : auto & used_nodes_on_p = used_nodes_on_proc[p];
1570 2706150 : if (elem->processor_id() == this->processor_id())
1571 245676 : used_nodes_on_p[n] = true;
1572 : else
1573 105 : if (!used_nodes_on_p.count(n))
1574 705390 : used_nodes_on_p[n] = false;
1575 : }
1576 : }
1577 937153 : }
1578 :
1579 937957 : if (_deleted_coarse_elements)
1580 : {
1581 : // "unsigned char" == "bool, but MPI::BOOL is iffy to use"
1582 : typedef unsigned char boolish;
1583 : std::map<processor_id_type, std::vector<std::pair<dof_id_type, boolish>>>
1584 28 : used_nodes_on_proc_vecs;
1585 81661 : for (auto & [pid, nodemap] : used_nodes_on_proc)
1586 63999 : used_nodes_on_proc_vecs[pid].assign(nodemap.begin(), nodemap.end());
1587 :
1588 28 : std::map<dof_id_type,processor_id_type> repartitioned_node_pids;
1589 : std::map<processor_id_type, std::set<dof_id_type>>
1590 28 : repartitioned_node_sets_to_push;
1591 :
1592 : auto ids_action_functor =
1593 63973 : [&used_nodes, &repartitioned_node_pids,
1594 : &repartitioned_node_sets_to_push]
1595 : (processor_id_type pid,
1596 112 : const std::vector<std::pair<dof_id_type, boolish>> & ids_and_bools)
1597 : {
1598 779532 : for (auto [n, sender_could_become_owner] : ids_and_bools)
1599 : {
1600 : // If we don't see a use for our own node, but someone
1601 : // else does, better figure out who should own it next.
1602 97 : if (!used_nodes.count(n))
1603 : {
1604 2 : if (auto it = repartitioned_node_pids.find(n);
1605 2 : sender_could_become_owner)
1606 : {
1607 1 : if (it != repartitioned_node_pids.end() &&
1608 1 : pid < it->second)
1609 1 : it->second = pid;
1610 : else
1611 0 : repartitioned_node_pids[n] = pid;
1612 : }
1613 : else
1614 1 : if (it == repartitioned_node_pids.end())
1615 1 : repartitioned_node_pids[n] =
1616 : DofObject::invalid_processor_id;
1617 :
1618 2 : repartitioned_node_sets_to_push[pid].insert(n);
1619 : }
1620 : }
1621 17674 : };
1622 :
1623 : // We need two pushes instead of a pull here because we need to
1624 : // know *all* the queries for a particular node before we can
1625 : // respond to *any* of them.
1626 : Parallel::push_parallel_vector_data
1627 17662 : (this->comm(), used_nodes_on_proc_vecs, ids_action_functor);
1628 :
1629 : // Repartition (what used to be) our own nodes first
1630 17663 : for (auto & [n, p] : repartitioned_node_pids)
1631 : {
1632 1 : Node & node = this->node_ref(n);
1633 0 : libmesh_assert_equal_to(node.processor_id(), this->processor_id());
1634 0 : libmesh_assert_not_equal_to_msg(p, DofObject::invalid_processor_id, "Node " << n << " is lost?");
1635 1 : node.processor_id() = p;
1636 : }
1637 :
1638 : // Then push to repartition others' ghosted copies.
1639 :
1640 : std::map<processor_id_type, std::vector<std::pair<dof_id_type,processor_id_type>>>
1641 28 : repartitioned_node_vecs;
1642 :
1643 17664 : for (auto & [p, nodeset] : repartitioned_node_sets_to_push)
1644 : {
1645 2 : auto & rn_vec = repartitioned_node_vecs[p];
1646 4 : for (auto n : nodeset)
1647 2 : rn_vec.emplace_back(n, repartitioned_node_pids[n]);
1648 : }
1649 :
1650 : auto repartition_node_functor =
1651 2 : [this]
1652 : (processor_id_type libmesh_dbg_var(pid),
1653 2 : const std::vector<std::pair<dof_id_type, processor_id_type>> & ids_and_pids)
1654 : {
1655 4 : for (auto [n, p] : ids_and_pids)
1656 : {
1657 0 : libmesh_assert_not_equal_to(p, DofObject::invalid_processor_id);
1658 2 : Node & node = this->node_ref(n);
1659 0 : libmesh_assert_equal_to(node.processor_id(), pid);
1660 2 : node.processor_id() = p;
1661 : }
1662 17634 : };
1663 :
1664 : Parallel::push_parallel_vector_data
1665 17662 : (this->comm(), repartitioned_node_vecs, repartition_node_functor);
1666 : }
1667 :
1668 937957 : _deleted_coarse_elements = false;
1669 :
1670 : // Nodes not connected to any local elements, and nullptr node entries
1671 : // in our container, are deleted
1672 : {
1673 937957 : node_iterator_imp it = _nodes.begin();
1674 402 : node_iterator_imp end = _nodes.end();
1675 :
1676 90116862 : while (it != end)
1677 : {
1678 89178905 : Node * nd = *it;
1679 89178905 : if (!nd)
1680 3535641 : it = _nodes.erase(it);
1681 85643264 : else if (!used_nodes.count(nd->id()))
1682 : {
1683 : // remove any boundary information associated with
1684 : // this node
1685 2496575 : this->get_boundary_info().remove (nd);
1686 2828 : _constraint_rows.erase(nd);
1687 :
1688 : // delete the node
1689 2496575 : delete nd;
1690 :
1691 2496575 : it = _nodes.erase(it);
1692 : }
1693 : else
1694 45269 : ++it;
1695 : }
1696 : }
1697 :
1698 937957 : this->_preparation.has_removed_orphaned_nodes = true;
1699 :
1700 937957 : if (_skip_renumber_nodes_and_elements)
1701 : {
1702 1670 : this->update_parallel_id_counts();
1703 2 : return;
1704 : }
1705 :
1706 : // Finally renumber all the elements
1707 936287 : _n_elem = this->renumber_dof_objects (this->_elements);
1708 :
1709 : // and all the remaining nodes
1710 936287 : _n_nodes = this->renumber_dof_objects (this->_nodes);
1711 :
1712 : // And figure out what IDs we should use when adding new nodes and
1713 : // new elements
1714 936287 : this->update_parallel_id_counts();
1715 :
1716 : // Make sure our caches are up to date and our
1717 : // DofObjects are well packed
1718 : #ifdef DEBUG
1719 400 : libmesh_assert_equal_to (this->n_nodes(), this->parallel_n_nodes());
1720 400 : libmesh_assert_equal_to (this->n_elem(), this->parallel_n_elem());
1721 400 : const dof_id_type pmax_node_id = this->parallel_max_node_id();
1722 400 : const dof_id_type pmax_elem_id = this->parallel_max_elem_id();
1723 400 : libmesh_assert_equal_to (this->max_node_id(), pmax_node_id);
1724 400 : libmesh_assert_equal_to (this->max_elem_id(), pmax_elem_id);
1725 400 : libmesh_assert_equal_to (this->n_nodes(), this->max_node_id());
1726 400 : libmesh_assert_equal_to (this->n_elem(), this->max_elem_id());
1727 :
1728 : // Make sure our ids and flags are consistent
1729 400 : this->libmesh_assert_valid_parallel_ids();
1730 400 : this->libmesh_assert_valid_parallel_flags();
1731 :
1732 : // And make sure we've made our numbering monotonic
1733 400 : MeshTools::libmesh_assert_valid_elem_ids(*this);
1734 : #endif
1735 : }
1736 :
1737 :
1738 :
1739 17410 : void DistributedMesh::fix_broken_node_and_element_numbering ()
1740 : {
1741 : // We can't use range-for here because we need access to the special
1742 : // iterators' methods, not just to their dereferenced values.
1743 :
1744 : // Nodes first
1745 0 : for (auto pr = this->_nodes.begin(),
1746 5444770 : end = this->_nodes.end(); pr != end; ++pr)
1747 : {
1748 5427360 : Node * n = *pr;
1749 5427360 : if (n != nullptr)
1750 : {
1751 0 : const dof_id_type id = pr.index();
1752 5204550 : n->set_id() = id;
1753 0 : libmesh_assert_equal_to(this->node_ptr(id), n);
1754 : }
1755 : }
1756 :
1757 : // Elements next
1758 0 : for (auto pr = this->_elements.begin(),
1759 5671231 : end = this->_elements.end(); pr != end; ++pr)
1760 : {
1761 5653821 : Elem * e = *pr;
1762 5653821 : if (e != nullptr)
1763 : {
1764 0 : const dof_id_type id = pr.index();
1765 5590229 : e->set_id() = id;
1766 0 : libmesh_assert_equal_to(this->elem_ptr(id), e);
1767 : }
1768 : }
1769 17410 : }
1770 :
1771 :
1772 :
1773 686227 : dof_id_type DistributedMesh::n_active_elem () const
1774 : {
1775 554 : parallel_object_only();
1776 :
1777 : // Get local active elements first
1778 : dof_id_type active_elements =
1779 1371900 : static_cast<dof_id_type>(std::distance (this->active_local_elements_begin(),
1780 2057573 : this->active_local_elements_end()));
1781 686227 : this->comm().sum(active_elements);
1782 :
1783 : // Then add unpartitioned active elements, which should exist on
1784 : // every processor
1785 686227 : active_elements +=
1786 686227 : static_cast<dof_id_type>(std::distance
1787 1371900 : (this->active_pid_elements_begin(DofObject::invalid_processor_id),
1788 686781 : this->active_pid_elements_end(DofObject::invalid_processor_id)));
1789 686227 : return active_elements;
1790 : }
1791 :
1792 :
1793 :
1794 434529 : void DistributedMesh::delete_remote_elements()
1795 : {
1796 : #ifdef DEBUG
1797 : // Make sure our neighbor links are all fine
1798 388 : MeshTools::libmesh_assert_valid_neighbors(*this);
1799 :
1800 : // And our child/parent links, and our flags
1801 388 : MeshTools::libmesh_assert_valid_refinement_tree(*this);
1802 :
1803 : // Make sure our ids and flags are consistent
1804 388 : this->libmesh_assert_valid_parallel_ids();
1805 388 : this->libmesh_assert_valid_parallel_flags();
1806 :
1807 388 : libmesh_assert_equal_to (this->n_nodes(), this->parallel_n_nodes());
1808 388 : libmesh_assert_equal_to (this->n_elem(), this->parallel_n_elem());
1809 388 : const dof_id_type pmax_node_id = this->parallel_max_node_id();
1810 388 : const dof_id_type pmax_elem_id = this->parallel_max_elem_id();
1811 388 : libmesh_assert_equal_to (this->max_node_id(), pmax_node_id);
1812 388 : libmesh_assert_equal_to (this->max_elem_id(), pmax_elem_id);
1813 : #endif
1814 :
1815 434529 : _is_serial = false;
1816 434529 : _is_serial_on_proc_0 = false;
1817 :
1818 434529 : MeshCommunication().delete_remote_elements(*this, _extra_ghost_elems);
1819 :
1820 388 : libmesh_assert_equal_to (this->max_elem_id(), this->parallel_max_elem_id());
1821 :
1822 : // Now make sure the containers actually shrink - strip
1823 : // any newly-created nullptr voids out of the element array
1824 434529 : dofobject_container<Elem>::veclike_iterator e_it = _elements.begin();
1825 388 : const dofobject_container<Elem>::veclike_iterator e_end = _elements.end();
1826 53925946 : while (e_it != e_end)
1827 53491417 : if (!*e_it)
1828 39614758 : e_it = _elements.erase(e_it);
1829 : else
1830 17389 : ++e_it;
1831 :
1832 434529 : dofobject_container<Node>::veclike_iterator n_it = _nodes.begin();
1833 388 : const dofobject_container<Node>::veclike_iterator n_end = _nodes.end();
1834 102268482 : while (n_it != n_end)
1835 101833953 : if (!*n_it)
1836 67389264 : n_it = _nodes.erase(n_it);
1837 : else
1838 48597 : ++n_it;
1839 :
1840 : // We may have deleted no-longer-connected nodes or coarsened-away
1841 : // elements; let's update our caches.
1842 434529 : this->update_parallel_id_counts();
1843 :
1844 : // We may have deleted nodes or elements that were the only local
1845 : // representatives of some particular boundary id(s); let's update
1846 : // those caches.
1847 434529 : this->get_boundary_info().regenerate_id_sets();
1848 :
1849 : #ifdef DEBUG
1850 : // We might not have well-packed objects if the user didn't allow us
1851 : // to renumber
1852 : // libmesh_assert_equal_to (this->n_nodes(), this->max_node_id());
1853 : // libmesh_assert_equal_to (this->n_elem(), this->max_elem_id());
1854 :
1855 : // Make sure our neighbor links are all fine
1856 388 : MeshTools::libmesh_assert_valid_neighbors(*this);
1857 :
1858 : // And our child/parent links, and our flags
1859 388 : MeshTools::libmesh_assert_valid_refinement_tree(*this);
1860 :
1861 : // Make sure our ids and flags are consistent
1862 388 : this->libmesh_assert_valid_parallel_ids();
1863 388 : this->libmesh_assert_valid_parallel_flags();
1864 : #endif
1865 :
1866 434529 : this->_preparation.has_removed_remote_elements = true;
1867 434529 : }
1868 :
1869 :
1870 0 : void DistributedMesh::add_extra_ghost_elem(Elem * e)
1871 : {
1872 : // First add the elem like normal
1873 0 : add_elem(e);
1874 :
1875 : // Now add it to the set that won't be deleted when we call
1876 : // delete_remote_elements()
1877 0 : _extra_ghost_elems.insert(e);
1878 0 : }
1879 :
1880 : void
1881 0 : DistributedMesh::clear_extra_ghost_elems(const std::set<Elem *> & extra_ghost_elems)
1882 : {
1883 0 : std::set<Elem *> tmp;
1884 0 : std::set_difference(_extra_ghost_elems.begin(), _extra_ghost_elems.end(),
1885 : extra_ghost_elems.begin(), extra_ghost_elems.end(),
1886 0 : std::inserter(tmp, tmp.begin()));
1887 0 : _extra_ghost_elems = tmp;
1888 0 : }
1889 :
1890 99896 : void DistributedMesh::allgather()
1891 : {
1892 99896 : if (_is_serial)
1893 0 : return;
1894 99894 : MeshCommunication().allgather(*this);
1895 99894 : _is_serial = true;
1896 99894 : _is_serial_on_proc_0 = true;
1897 :
1898 : // Make sure our caches are up to date and our
1899 : // DofObjects are well packed
1900 : #ifdef DEBUG
1901 48 : libmesh_assert_equal_to (this->n_nodes(), this->parallel_n_nodes());
1902 48 : libmesh_assert_equal_to (this->n_elem(), this->parallel_n_elem());
1903 48 : const dof_id_type pmax_node_id = this->parallel_max_node_id();
1904 48 : const dof_id_type pmax_elem_id = this->parallel_max_elem_id();
1905 48 : libmesh_assert_equal_to (this->max_node_id(), pmax_node_id);
1906 48 : libmesh_assert_equal_to (this->max_elem_id(), pmax_elem_id);
1907 :
1908 : // If we've disabled renumbering we can't be sure we're contiguous
1909 : // libmesh_assert_equal_to (this->n_nodes(), this->max_node_id());
1910 : // libmesh_assert_equal_to (this->n_elem(), this->max_elem_id());
1911 :
1912 : // Make sure our neighbor links are all fine
1913 48 : MeshTools::libmesh_assert_valid_neighbors(*this);
1914 :
1915 : // Make sure our ids and flags are consistent
1916 48 : this->libmesh_assert_valid_parallel_ids();
1917 48 : this->libmesh_assert_valid_parallel_flags();
1918 : #endif
1919 : }
1920 :
1921 18352 : void DistributedMesh::gather_to_zero()
1922 : {
1923 18352 : if (_is_serial_on_proc_0)
1924 4 : return;
1925 :
1926 14114 : _is_serial_on_proc_0 = true;
1927 14114 : MeshCommunication().gather(0, *this);
1928 : }
1929 :
1930 :
1931 : } // namespace libMesh
|