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/boundary_info.h"
22 : #include "libmesh/ghosting_functor.h"
23 : #include "libmesh/ghost_point_neighbors.h"
24 : #include "libmesh/unstructured_mesh.h"
25 : #include "libmesh/libmesh_logging.h"
26 : #include "libmesh/elem.h"
27 : #include "libmesh/elem_range.h"
28 : #include "libmesh/face_polygon.h"
29 : #include "libmesh/mesh_tools.h" // For n_levels
30 : #include "libmesh/parallel.h"
31 : #include "libmesh/remote_elem.h"
32 : #include "libmesh/namebased_io.h"
33 : #include "libmesh/partitioner.h"
34 : #include "libmesh/enum_order.h"
35 : #include "libmesh/mesh_communication.h"
36 : #include "libmesh/enum_to_string.h"
37 : #include "libmesh/mesh_serializer.h"
38 : #include "libmesh/utility.h"
39 :
40 : #ifdef LIBMESH_HAVE_NANOFLANN
41 : #include "libmesh/nanoflann.hpp"
42 : #endif
43 :
44 : // C++ includes
45 : #include <algorithm> // std::all_of
46 : #include <atomic>
47 : #include <fstream>
48 : #include <iomanip>
49 : #include <map>
50 : #include <sstream>
51 : #include <unordered_map>
52 :
53 : // for disjoint neighbors
54 : #include "libmesh/periodic_boundaries.h"
55 : #include "libmesh/periodic_boundary.h"
56 :
57 : namespace {
58 :
59 : using namespace libMesh;
60 :
61 : // Helper functions for all_second_order, all_complete_order
62 :
63 : std::map<std::vector<dof_id_type>, Node *>::iterator
64 3776790 : map_hi_order_node(unsigned int hon,
65 : const Elem & hi_elem,
66 : std::map<std::vector<dof_id_type>, Node *> & adj_vertices_to_ho_nodes)
67 : {
68 : /*
69 : * form a vector that will hold the node id's of
70 : * the vertices that are adjacent to the nth
71 : * higher-order node.
72 : */
73 : const unsigned int n_adjacent_vertices =
74 3776790 : hi_elem.n_second_order_adjacent_vertices(hon);
75 :
76 3776790 : std::vector<dof_id_type> adjacent_vertices_ids(n_adjacent_vertices);
77 :
78 12736119 : for (unsigned int v=0; v<n_adjacent_vertices; v++)
79 11444263 : adjacent_vertices_ids[v] =
80 8959329 : hi_elem.node_id( hi_elem.second_order_adjacent_vertex(hon,v) );
81 :
82 : /*
83 : * \p adjacent_vertices_ids is now in order of the current
84 : * side. sort it, so that comparisons with the
85 : * \p adjacent_vertices_ids created through other elements'
86 : * sides can match
87 : */
88 3776790 : std::sort(adjacent_vertices_ids.begin(),
89 : adjacent_vertices_ids.end());
90 :
91 : // Does this set of vertices already have a mid-node added? If not
92 : // we'll want to add it.
93 5871526 : return adj_vertices_to_ho_nodes.try_emplace(adjacent_vertices_ids, nullptr).first;
94 : }
95 :
96 : // With unique ids enabled, \p previous_max_unique_id is an upper bound on the
97 : // unique_id values in the mesh before the order increase. New node unique_id
98 : // values start above it. \p max_new_node_unique_id is updated to the largest
99 : // unique_id given to a new node.
100 401245 : void transfer_elem(Elem & lo_elem,
101 : std::unique_ptr<Elem> hi_elem,
102 : #ifdef LIBMESH_ENABLE_UNIQUE_ID
103 : unique_id_type previous_max_unique_id,
104 : unique_id_type max_new_nodes_per_elem,
105 : unique_id_type & max_new_node_unique_id,
106 : #endif
107 : UnstructuredMesh & mesh,
108 : std::map<std::vector<dof_id_type>, Node *> & adj_vertices_to_ho_nodes,
109 : std::unordered_map<Elem *, std::vector<Elem *>> & exterior_children_of)
110 : {
111 111458 : libmesh_assert_equal_to (lo_elem.n_vertices(), hi_elem->n_vertices());
112 :
113 222916 : const processor_id_type my_pid = mesh.processor_id();
114 401245 : const processor_id_type lo_pid = lo_elem.processor_id();
115 :
116 : /*
117 : * Now handle the additional higher-order nodes. This
118 : * is simply handled through a map that remembers
119 : * the already-added nodes. This map maps the global
120 : * ids of the vertices (that uniquely define this
121 : * higher-order node) to the new node.
122 : * Notation: hon = high-order node
123 : */
124 401245 : const unsigned int hon_begin = lo_elem.n_nodes();
125 401245 : const unsigned int hon_end = hi_elem->n_nodes();
126 :
127 111458 : libmesh_assert_less (hon_begin, hon_end);
128 : #ifdef LIBMESH_ENABLE_UNIQUE_ID
129 111458 : libmesh_assert_less_equal
130 : (hon_end-hon_begin, max_new_nodes_per_elem);
131 : #endif
132 :
133 4129868 : for (unsigned int hon=hon_begin; hon<hon_end; hon++)
134 : {
135 3728623 : auto pos = map_hi_order_node(hon, *hi_elem, adj_vertices_to_ho_nodes);
136 :
137 : // no, not added yet
138 3728623 : if (!pos->second)
139 : {
140 376112 : const auto & adjacent_vertices_ids = pos->first;
141 :
142 : /*
143 : * for this set of vertices, there is no
144 : * second_order node yet. Add it.
145 : *
146 : * compute the location of the new node as
147 : * the average over the adjacent vertices.
148 : */
149 376112 : Point new_location = 0;
150 4865624 : for (dof_id_type vertex_id : adjacent_vertices_ids)
151 3511082 : new_location += mesh.point(vertex_id);
152 :
153 1730654 : new_location /= static_cast<Real>(adjacent_vertices_ids.size());
154 :
155 : /* Add the new point to the mesh.
156 : *
157 : * If we are on a serialized mesh, then we're doing this
158 : * all in sync, and the node processor_id will be
159 : * consistent between processors.
160 : *
161 : * If we are on a distributed mesh, we can fix
162 : * inconsistent processor ids later, but only if every
163 : * processor gives new nodes a *locally* consistent
164 : * processor id, so we'll give the new node the
165 : * processor id of an adjacent element for now and then
166 : * we'll update that later if appropriate.
167 : */
168 : Node * hi_node = mesh.add_point
169 1354542 : (new_location, DofObject::invalid_id, lo_pid);
170 :
171 : /* Come up with a unique unique_id for a potentially new
172 : * node. On a distributed mesh we don't yet know what
173 : * processor_id will definitely own it, so we can't let
174 : * the pid determine the unique_id. But we're not
175 : * adding unpartitioned nodes in sync, so we can't let
176 : * the mesh autodetermine a unique_id for a new
177 : * unpartitioned node either. So we have to pick unique
178 : * unique_id values manually.
179 : *
180 : * We don't have to pick the *same* unique_id value as
181 : * will be picked on other processors, though; we'll
182 : * sync up each node later. We just need to make sure
183 : * we don't duplicate any unique_id that might be chosen
184 : * by the same process elsewhere.
185 : */
186 : #ifdef LIBMESH_ENABLE_UNIQUE_ID
187 1354542 : unique_id_type new_unique_id = previous_max_unique_id +
188 1730654 : max_new_nodes_per_elem * lo_elem.id() +
189 1354542 : hon - hon_begin;
190 :
191 376112 : hi_node->set_unique_id(new_unique_id);
192 1354542 : max_new_node_unique_id = std::max(max_new_node_unique_id, new_unique_id);
193 : #endif
194 :
195 : /*
196 : * insert the new node with its defining vertex
197 : * set into the map, and relocate pos to this
198 : * new entry, so that the hi_elem can use
199 : * \p pos for inserting the node
200 : */
201 1354542 : pos->second = hi_node;
202 :
203 1354542 : hi_elem->set_node(hon, hi_node);
204 : }
205 : // yes, already added.
206 : else
207 : {
208 657494 : Node * hi_node = pos->second;
209 657494 : libmesh_assert(hi_node);
210 657494 : libmesh_assert_equal_to(mesh.node_ptr(hi_node->id()), hi_node);
211 :
212 2374081 : hi_elem->set_node(hon, hi_node);
213 :
214 : // We need to ensure that the processor who should own a
215 : // node *knows* they own the node. And because
216 : // Node::choose_processor_id() may depend on Node id,
217 : // which may not yet be authoritative, we still have to
218 : // use a dumb-but-id-independent partitioning heuristic.
219 : processor_id_type chosen_pid =
220 2374081 : std::min (hi_node->processor_id(), lo_pid);
221 :
222 : // Plus, if we just discovered that we own this node,
223 : // then on a distributed mesh we need to make sure to
224 : // give it a valid id, not just a placeholder id!
225 2374081 : if (!mesh.is_replicated() &&
226 2374081 : hi_node->processor_id() != my_pid &&
227 : chosen_pid == my_pid)
228 0 : mesh.own_node(*hi_node);
229 :
230 2374081 : hi_node->processor_id() = chosen_pid;
231 : }
232 : }
233 :
234 : /*
235 : * find_neighbors relies on remote_elem neighbor links being
236 : * properly maintained. Our own code here relies on ordinary
237 : * neighbor links being properly maintained, so let's just keep
238 : * everything up to date.
239 : */
240 2071811 : for (auto s : lo_elem.side_index_range())
241 : {
242 1670566 : Elem * neigh = lo_elem.neighbor_ptr(s);
243 1670566 : if (!neigh)
244 1132687 : continue;
245 :
246 103885 : if (neigh != remote_elem)
247 : {
248 : // We don't support AMR even outside our own range yet.
249 30842 : libmesh_assert_equal_to (neigh->level(), 0);
250 :
251 103405 : const unsigned int ns = neigh->which_neighbor_am_i(&lo_elem);
252 30842 : libmesh_assert_not_equal_to(ns, libMesh::invalid_uint);
253 :
254 61684 : neigh->set_neighbor(ns, hi_elem.get());
255 : }
256 :
257 62004 : hi_elem->set_neighbor(s, neigh);
258 : }
259 :
260 : /**
261 : * If the old element has an interior_parent(), transfer it to the
262 : * new element ... and if the interior_parent itself might be
263 : * getting upgraded, make sure we later consider the new element to
264 : * be its exterior child, not the old element.
265 : */
266 401245 : Elem * interior_p = lo_elem.interior_parent();
267 401245 : if (interior_p)
268 0 : hi_elem->set_interior_parent(interior_p);
269 :
270 401245 : if (auto parent_exterior_it = exterior_children_of.find(interior_p);
271 111458 : parent_exterior_it != exterior_children_of.end())
272 : {
273 0 : auto & exteriors = parent_exterior_it->second;
274 0 : for (std::size_t i : index_range(exteriors))
275 0 : if (exteriors[i] == &lo_elem)
276 : {
277 0 : exteriors[i] = hi_elem.get();
278 0 : break;
279 : }
280 : }
281 :
282 : /**
283 : * If we had interior_parent() links to the old element, transfer
284 : * them to the new element.
285 : */
286 512703 : if (auto exterior_it = exterior_children_of.find(&lo_elem);
287 111458 : exterior_it != exterior_children_of.end())
288 : {
289 401245 : for (Elem * exterior_elem : exterior_it->second)
290 : {
291 0 : libmesh_assert(exterior_elem->interior_parent() == &lo_elem);
292 0 : exterior_elem->set_interior_parent(hi_elem.get());
293 : }
294 : }
295 :
296 : /**
297 : * If the old element had any boundary conditions they
298 : * should be transferred to the second-order element. The old
299 : * boundary conditions will be removed from the BoundaryInfo
300 : * data structure by insert_elem.
301 : *
302 : * Also, prepare_for_use() will reconstruct most of our neighbor
303 : * links, but if we have any remote_elem links in a distributed
304 : * mesh, they need to be preserved. We do that in the same loop
305 : * here.
306 : */
307 111458 : mesh.get_boundary_info().copy_boundary_ids
308 401245 : (mesh.get_boundary_info(), &lo_elem, hi_elem.get());
309 :
310 : /*
311 : * The new second-order element is ready.
312 : * Inserting it into the mesh will replace and delete
313 : * the first-order element.
314 : */
315 222916 : hi_elem->set_id(lo_elem.id());
316 : #ifdef LIBMESH_ENABLE_UNIQUE_ID
317 222916 : hi_elem->set_unique_id(lo_elem.unique_id());
318 : #endif
319 :
320 401245 : const unsigned int nei = lo_elem.n_extra_integers();
321 401245 : hi_elem->add_extra_integers(nei);
322 401340 : for (unsigned int i=0; i != nei; ++i)
323 95 : hi_elem->set_extra_integer(i, lo_elem.get_extra_integer(i));
324 :
325 289787 : hi_elem->inherit_data_from(lo_elem);
326 :
327 512703 : mesh.insert_elem(std::move(hi_elem));
328 401245 : }
329 :
330 :
331 : template <typename ElemTypeConverter>
332 : void
333 5637 : all_increased_order_range (UnstructuredMesh & mesh,
334 : const SimpleRange<MeshBase::element_iterator> & range,
335 : const unsigned int max_new_nodes_per_elem,
336 : const ElemTypeConverter & elem_type_converter)
337 : {
338 : // This function must be run on all processors at once
339 1616 : timpi_parallel_only(mesh.comm());
340 :
341 : /*
342 : * The maximum number of new higher-order nodes we might be adding,
343 : * for use when picking unique unique_id values later. This variable
344 : * is not used unless unique ids are enabled, so libmesh_ignore() it
345 : * to avoid warnings in that case.
346 : */
347 1616 : libmesh_ignore(max_new_nodes_per_elem);
348 :
349 : /*
350 : * The mesh should at least be consistent enough for us to add new
351 : * nodes consistently.
352 : */
353 5637 : mesh.update_parallel_id_counts();
354 :
355 : /*
356 : * If the mesh is empty then we have nothing to do
357 : */
358 5637 : if (!mesh.n_elem())
359 400 : return;
360 :
361 : // If every element in the range _on every proc_ is already of the
362 : // requested higher order then we have nothing to do. However, if
363 : // any proc has some lower-order elements in the range, then _all_
364 : // processors need to continue this function because it is
365 : // parallel_only().
366 : //
367 : // Note: std::all_of() returns true for an empty range, which can
368 : // happen for example in the DistributedMesh case when there are
369 : // more processors than elements. In the case of an empty range we
370 : // therefore set already_second_order to true on that proc.
371 14032 : auto is_higher_order = [&elem_type_converter](const Elem * elem) {
372 6058 : ElemType old_type = elem->type();
373 2698 : ElemType new_type = elem_type_converter(old_type);
374 6058 : return old_type == new_type;
375 : };
376 :
377 5637 : bool already_higher_order =
378 9658 : std::all_of(range.begin(), range.end(), is_higher_order);
379 :
380 : // Check with other processors and possibly return early
381 5637 : mesh.comm().min(already_higher_order);
382 5637 : if (already_higher_order)
383 116 : return;
384 :
385 : /*
386 : * this map helps in identifying higher order
387 : * nodes. Namely, a higher-order node:
388 : * - edge node
389 : * - face node
390 : * - bubble node
391 : * is uniquely defined through a set of adjacent
392 : * vertices. This set of adjacent vertices is
393 : * used to identify already added higher-order
394 : * nodes. We are safe to use node id's since we
395 : * make sure that these are correctly numbered.
396 : *
397 : * We lazily use an ordered map here to avoid having to implement a
398 : * good hash for vector<dof_id_type>
399 : */
400 3000 : std::map<std::vector<dof_id_type>, Node *> adj_vertices_to_ho_nodes;
401 :
402 : /*
403 : * This map helps us reset any interior_parent() values from the
404 : * lower order element to its higher order replacement. Unlike with
405 : * neighbor pointers, we don't have backlinks here, so we have to
406 : * iterate over the mesh to track forward links.
407 : */
408 3000 : std::unordered_map<Elem *, std::vector<Elem *>> exterior_children_of;
409 :
410 : /*
411 : * max_new_nodes_per_elem is the maximum number of new higher order
412 : * nodes we might be adding, for use when picking unique unique_id
413 : * values later. This variable is not used unless unique ids are
414 : * enabled.
415 : */
416 : #ifdef LIBMESH_ENABLE_UNIQUE_ID
417 5237 : unique_id_type previous_max_unique_id = mesh.parallel_max_unique_id();
418 : #endif
419 :
420 : /**
421 : * On distributed meshes we currently only support unpartitioned
422 : * meshes (where we'll add every node in sync) or
423 : * completely-partitioned meshes (where we'll sync nodes later);
424 : * let's keep track to make sure we're not in any in-between state.
425 : */
426 1500 : dof_id_type n_unpartitioned_elem = 0;
427 :
428 : #ifdef LIBMESH_ENABLE_UNIQUE_ID
429 : // The largest unique_id that this processor assigns to a new node
430 5237 : unique_id_type max_new_node_unique_id = previous_max_unique_id;
431 : #endif
432 :
433 : /**
434 : * Loop over the elements in the given range. If any are
435 : * already at higher than first-order, track their higher-order
436 : * nodes in case we need them for neighboring elements later.
437 : *
438 : * In this way we can use this method to "fix up" a mesh which has
439 : * otherwise inconsistent neighbor pairs of lower and higher order
440 : * geometric elements.
441 : *
442 : * If any elements are not at the desired order yet, we need to
443 : * check their neighbors and even their edge neighbors for higher
444 : * order; we may need to share elements with a neighbor not in the
445 : * range.
446 : */
447 705431 : auto track_if_necessary = [&adj_vertices_to_ho_nodes,
448 : &exterior_children_of,
449 226780 : &elem_type_converter](Elem * elem) {
450 408007 : if (elem && elem != remote_elem)
451 : {
452 408007 : if (elem->default_order() != FIRST)
453 51569 : for (unsigned int hon : make_range(elem->n_vertices(), elem->n_nodes()))
454 : {
455 48167 : auto pos = map_hi_order_node(hon, *elem, adj_vertices_to_ho_nodes);
456 61929 : pos->second = elem->node_ptr(hon);
457 : }
458 :
459 408007 : const ElemType old_type = elem->type();
460 155470 : const ElemType new_type = elem_type_converter(old_type);
461 408007 : if (old_type != new_type)
462 404605 : exterior_children_of.emplace(elem, std::vector<Elem *>());
463 : }
464 : };
465 :
466 : // If we're in the common case then just track everything; otherwise
467 : // find point neighbors to track
468 16987 : if (range.begin() == mesh.elements_begin() &&
469 16996 : range.end() == mesh.elements_end())
470 : {
471 583175 : for (auto & elem : range)
472 400403 : track_if_necessary(elem);
473 : }
474 : else
475 : {
476 240 : GhostingFunctor::map_type point_neighbor_elements;
477 :
478 240 : GhostPointNeighbors point_neighbor_finder(mesh);
479 1032 : point_neighbor_finder(range.begin(), range.end(),
480 : mesh.n_processors(),
481 : point_neighbor_elements);
482 :
483 8028 : for (auto & [elem, coupling_map] : point_neighbor_elements)
484 : {
485 2168 : libmesh_ignore(coupling_map);
486 7604 : track_if_necessary(const_cast<Elem *>(elem));
487 : }
488 : }
489 :
490 : /**
491 : * Loop over all mesh elements to look for interior_parent links we
492 : * need to upgrade later.
493 : */
494 611035 : for (auto & elem : mesh.element_ptr_range())
495 530928 : if (auto exterior_map_it = exterior_children_of.find(elem->interior_parent());
496 115508 : exterior_map_it != exterior_children_of.end())
497 0 : exterior_map_it->second.push_back(elem);
498 :
499 : /**
500 : * Loop over the low-ordered elements in the _elements vector.
501 : * First make sure they _are_ indeed low-order, and then replace
502 : * them with an equivalent second-order element. Don't
503 : * forget to delete the low-order element, or else it will leak!
504 : */
505 584921 : for (auto & lo_elem : range)
506 : {
507 : // Now we can skip the elements in the range that are already
508 : // higher-order.
509 401322 : const ElemType old_type = lo_elem->type();
510 153098 : const ElemType new_type = elem_type_converter(old_type);
511 :
512 401322 : if (old_type == new_type)
513 77 : continue;
514 :
515 : // this does _not_ work for refined elements
516 111458 : libmesh_assert_equal_to (lo_elem->level(), 0);
517 :
518 401245 : if (lo_elem->processor_id() == DofObject::invalid_processor_id)
519 380263 : ++n_unpartitioned_elem;
520 :
521 : /*
522 : * Build the higher-order equivalent; add to
523 : * the new_elements list.
524 : */
525 401245 : auto ho_elem = Elem::build (new_type);
526 :
527 111458 : libmesh_assert_equal_to (lo_elem->n_vertices(), ho_elem->n_vertices());
528 :
529 : /*
530 : * By definition the initial nodes of the lower and higher order
531 : * element are identically numbered. Transfer these.
532 : */
533 2103213 : for (unsigned int v=0, lnn=lo_elem->n_nodes(); v < lnn; v++)
534 2176360 : ho_elem->set_node(v, lo_elem->node_ptr(v));
535 :
536 624161 : transfer_elem(*lo_elem, std::move(ho_elem),
537 : #ifdef LIBMESH_ENABLE_UNIQUE_ID
538 : previous_max_unique_id, max_new_nodes_per_elem,
539 : max_new_node_unique_id,
540 : #endif
541 : mesh, adj_vertices_to_ho_nodes,
542 : exterior_children_of);
543 : } // end for (auto & lo_elem : range)
544 :
545 : // we can clear the map at this point.
546 1500 : adj_vertices_to_ho_nodes.clear();
547 :
548 : #ifdef LIBMESH_ENABLE_UNIQUE_ID
549 : // make_nodes_parallel_consistent() can give a node the unique_id that
550 : // another processor assigned, so the next unique_id must exceed the
551 : // largest one assigned on any processor.
552 5237 : mesh.comm().max(max_new_node_unique_id);
553 5237 : mesh.set_next_unique_id(max_new_node_unique_id + 1);
554 : #endif
555 :
556 : // On a DistributedMesh our ghost node processor ids may be bad,
557 : // the ids of nodes touching remote elements may be inconsistent,
558 : // unique_ids of newly added non-local nodes remain unset, and our
559 : // partitioning of new nodes may not be well balanced.
560 : //
561 : // make_nodes_parallel_consistent() will fix all this.
562 5237 : if (!mesh.is_replicated())
563 : {
564 98 : dof_id_type max_unpartitioned_elem = n_unpartitioned_elem;
565 98 : mesh.comm().max(max_unpartitioned_elem);
566 98 : if (max_unpartitioned_elem)
567 : {
568 : // We'd better be effectively serialized here. In theory we
569 : // could support more complicated cases but for now we
570 : // only support "completely partitioned" and/or "serialized"
571 77 : if (mesh.is_serial())
572 22 : libmesh_assert(mesh.comm().verify(n_unpartitioned_elem));
573 : else
574 0 : libmesh_not_implemented();
575 : }
576 : else
577 : {
578 21 : MeshCommunication().make_nodes_parallel_consistent (mesh);
579 : }
580 : }
581 :
582 : // renumber nodes, repartition nodes, etc. We may no longer need a
583 : // find_neighbors() here since we're keeping neighbor links intact
584 : // ourselves, *except* that if we're not already prepared we may
585 : // have user code that was expecting this call to prepare neighbors.
586 3000 : const bool old_find_neighbors = mesh.allow_find_neighbors();
587 5237 : if (mesh.is_prepared())
588 300 : mesh.allow_find_neighbors(false);
589 5237 : mesh.prepare_for_use();
590 1500 : mesh.allow_find_neighbors(old_find_neighbors);
591 : }
592 :
593 :
594 : } // anonymous namespace
595 :
596 :
597 : namespace libMesh
598 : {
599 :
600 : // This class adapts a vector of Nodes (represented by a pair of a Point and a dof_id_type)
601 : // for use in a nanoflann KD-Tree
602 :
603 0 : class VectorOfNodesAdaptor
604 : {
605 : private:
606 : const std::vector<std::pair<Point, dof_id_type>> _nodes;
607 :
608 : public:
609 0 : VectorOfNodesAdaptor(const std::vector<std::pair<Point, dof_id_type>> & nodes) :
610 0 : _nodes(nodes)
611 0 : {}
612 :
613 : /**
614 : * Must return the number of data points
615 : */
616 0 : inline size_t kdtree_get_point_count() const { return _nodes.size(); }
617 :
618 : /**
619 : * \returns The dim'th component of the idx'th point in the class:
620 : * Since this is inlined and the "dim" argument is typically an immediate value, the
621 : * "if's" are actually solved at compile time.
622 : */
623 0 : inline Real kdtree_get_pt(const size_t idx, int dim) const
624 : {
625 0 : libmesh_assert_less (idx, _nodes.size());
626 0 : libmesh_assert_less (dim, 3);
627 :
628 0 : const Point & p(_nodes[idx].first);
629 :
630 0 : if (dim==0) return p(0);
631 0 : if (dim==1) return p(1);
632 0 : return p(2);
633 : }
634 :
635 : /*
636 : * Optional bounding-box computation
637 : */
638 : template <class BBOX>
639 0 : bool kdtree_get_bbox(BBOX & /* bb */) const { return false; }
640 : };
641 :
642 :
643 : // ------------------------------------------------------------
644 : // UnstructuredMesh class member functions
645 53076 : UnstructuredMesh::UnstructuredMesh (const Parallel::Communicator & comm_in,
646 53076 : unsigned char d) :
647 53076 : MeshBase (comm_in,d)
648 : {
649 15559 : libmesh_assert (libMesh::initialized());
650 53076 : }
651 :
652 :
653 :
654 21119 : UnstructuredMesh::UnstructuredMesh (const MeshBase & other_mesh) :
655 21119 : MeshBase (other_mesh)
656 : {
657 19280 : libmesh_assert (libMesh::initialized());
658 21119 : }
659 :
660 :
661 :
662 21294 : void UnstructuredMesh::copy_nodes_and_elements(const MeshBase & other_mesh,
663 : const bool skip_find_neighbors,
664 : dof_id_type element_id_offset,
665 : dof_id_type node_id_offset,
666 : unique_id_type
667 : #ifdef LIBMESH_ENABLE_UNIQUE_ID
668 : unique_id_offset
669 : #endif
670 : ,
671 : std::unordered_map<subdomain_id_type, subdomain_id_type> *
672 : id_remapping,
673 : const bool skip_preparation)
674 : {
675 39502 : LOG_SCOPE("copy_nodes_and_elements()", "UnstructuredMesh");
676 :
677 : // If we're asked to skip all preparation, we should be skipping
678 : // find_neighbors specifically.
679 19330 : libmesh_assert(!skip_preparation || skip_find_neighbors);
680 :
681 : std::pair<std::vector<unsigned int>, std::vector<unsigned int>>
682 41466 : extra_int_maps = this->merge_extra_integer_names(other_mesh);
683 :
684 21294 : const unsigned int n_old_node_ints = extra_int_maps.second.size(),
685 21294 : n_new_node_ints = _node_integer_names.size(),
686 21294 : n_old_elem_ints = extra_int_maps.first.size(),
687 21294 : n_new_elem_ints = _elem_integer_names.size();
688 :
689 : // If we are partitioned into fewer parts than the incoming mesh has
690 : // processors to handle, then we need to "wrap" the other Mesh's
691 : // processor ids to fit within our range. This can happen, for
692 : // example, while stitching meshes with small numbers of elements in
693 : // parallel...
694 20172 : bool wrap_proc_ids = (this->n_processors() <
695 20172 : other_mesh.n_partitions());
696 :
697 : // We're assuming the other mesh has proper element number ordering,
698 : // so that we add parents before their children, and that the other
699 : // mesh is consistently partitioned. We're not assuming that node
700 : // proc ids are topologically consistent, so we don't just
701 : // libmesh_assert_valid_procids.
702 : #ifdef DEBUG
703 19330 : MeshTools::libmesh_assert_valid_amr_elem_ids(other_mesh);
704 19330 : MeshTools::libmesh_assert_parallel_consistent_procids<Node>(other_mesh);
705 : #endif
706 :
707 : //Copy in Nodes
708 : {
709 : //Preallocate Memory if necessary
710 21294 : this->reserve_nodes(other_mesh.n_nodes());
711 :
712 3523650 : for (const auto & oldn : other_mesh.node_ptr_range())
713 : {
714 : processor_id_type added_pid = cast_int<processor_id_type>
715 3283459 : (wrap_proc_ids ? oldn->processor_id() % this->n_processors() : oldn->processor_id());
716 :
717 : // Add new nodes in old node Point locations
718 : Node * newn =
719 11943499 : this->add_point(*oldn,
720 3283459 : oldn->id() + node_id_offset,
721 3066526 : added_pid);
722 :
723 3283459 : newn->add_extra_integers(n_new_node_ints);
724 3368433 : for (unsigned int i = 0; i != n_old_node_ints; ++i)
725 96922 : newn->set_extra_integer(extra_int_maps.second[i],
726 84974 : oldn->get_extra_integer(i));
727 :
728 : #ifdef LIBMESH_ENABLE_UNIQUE_ID
729 3283459 : newn->set_unique_id(oldn->unique_id() + unique_id_offset);
730 : #endif
731 1122 : }
732 : }
733 :
734 : //Copy in Elements
735 : {
736 : //Preallocate Memory if necessary
737 21294 : this->reserve_elem(other_mesh.n_elem());
738 :
739 : // Declare a map linking old and new elements, needed to copy the neighbor lists
740 : typedef std::unordered_map<const Elem *, Elem *> map_type;
741 38660 : map_type old_elems_to_new_elems, ip_map;
742 :
743 : // Loop over the elements
744 2641584 : for (const auto & old : other_mesh.element_ptr_range())
745 : {
746 : // Build a new element
747 2603745 : Elem * newparent = old->parent() ?
748 893462 : this->elem_ptr(old->parent()->id() + element_id_offset) :
749 1702124 : nullptr;
750 4010189 : auto el = old->disconnected_clone();
751 1997804 : el->set_parent(newparent);
752 :
753 2308065 : subdomain_id_type sbd_id = old->subdomain_id();
754 2308065 : if (id_remapping)
755 : {
756 538 : auto remapping_it = id_remapping->find(sbd_id);
757 1883 : if (remapping_it != id_remapping->end())
758 56 : sbd_id = remapping_it->second;
759 : }
760 2308065 : el->subdomain_id() = sbd_id;
761 :
762 : // Hold off on trying to set the interior parent because we may actually
763 : // add lower dimensional elements before their interior parents
764 2308065 : if (old->interior_parent())
765 2320 : ip_map[old] = el.get();
766 :
767 : #ifdef LIBMESH_ENABLE_AMR
768 2308065 : if (old->has_children())
769 1097563 : for (unsigned int c = 0, nc = old->n_children(); c != nc; ++c)
770 918782 : if (old->child_ptr(c) == remote_elem)
771 896 : el->add_child(const_cast<RemoteElem *>(remote_elem), c);
772 :
773 : //Create the parent's child pointers if necessary
774 2308065 : if (newparent)
775 : {
776 917866 : unsigned int oldc = old->parent()->which_child_am_i(old);
777 893462 : newparent->add_child(el.get(), oldc);
778 : }
779 :
780 : // Copy the refinement flags
781 2308065 : el->set_refinement_flag(old->refinement_flag());
782 :
783 : // Use hack_p_level since we may not have sibling elements
784 : // added yet
785 1997804 : el->hack_p_level(old->p_level());
786 :
787 1997804 : el->set_p_refinement_flag(old->p_refinement_flag());
788 : #endif // #ifdef LIBMESH_ENABLE_AMR
789 :
790 : //Assign all the nodes
791 16782498 : for (auto i : el->node_index_range())
792 26131309 : el->set_node(i,
793 14474433 : this->node_ptr(old->node_id(i) + node_id_offset));
794 :
795 : // And start it off with the same processor id (mod _n_parts).
796 2308065 : el->processor_id() = cast_int<processor_id_type>
797 2308065 : (wrap_proc_ids ? old->processor_id() % this->n_processors() : old->processor_id());
798 :
799 : // Give it the same element and unique ids
800 2308065 : el->set_id(old->id() + element_id_offset);
801 :
802 2308065 : el->add_extra_integers(n_new_elem_ints);
803 2315014 : for (unsigned int i = 0; i != n_old_elem_ints; ++i)
804 13169 : el->set_extra_integer(extra_int_maps.first[i],
805 6949 : old->get_extra_integer(i));
806 :
807 : #ifdef LIBMESH_ENABLE_UNIQUE_ID
808 2308065 : el->set_unique_id(old->unique_id() + unique_id_offset);
809 : #endif
810 :
811 : //Hold onto it
812 2308065 : if (!skip_find_neighbors)
813 : {
814 24960 : for (auto s : old->side_index_range())
815 25600 : if (old->neighbor_ptr(s) == remote_elem)
816 256 : el->set_neighbor(s, const_cast<RemoteElem *>(remote_elem));
817 7808 : this->add_elem(std::move(el));
818 : }
819 : else
820 : {
821 2597345 : Elem * new_el = this->add_elem(std::move(el));
822 2303073 : old_elems_to_new_elems[old] = new_el;
823 : }
824 311383 : }
825 :
826 : // If the other_mesh had some interior parents, we may need to
827 : // copy those pointers (if they're to elements in a third mesh),
828 : // or create new equivalent pointers (if they're to elements we
829 : // just copied), or scream and die (if the other mesh had interior
830 : // parents from a third mesh but we already have interior parents
831 : // that aren't to that same third mesh.
832 21294 : if (!ip_map.empty())
833 : {
834 142 : std::atomic<bool> existing_interior_parents{false};
835 :
836 : Threads::parallel_for
837 142 : (this->element_stored_range(),
838 142 : [&existing_interior_parents](const ElemRange & range)
839 : {
840 7832 : for (Elem * elem : range)
841 7690 : if (elem->interior_parent())
842 : {
843 0 : existing_interior_parents = true;
844 0 : break;
845 : }
846 142 : });
847 :
848 : MeshBase * other_interior_mesh =
849 142 : const_cast<MeshBase *>(&other_mesh.interior_mesh());
850 :
851 : // If we don't already have interior parents, then we can just
852 : // use whatever interior_mesh we need for the incoming
853 : // elements.
854 142 : if (!existing_interior_parents)
855 : {
856 142 : if (other_interior_mesh == &other_mesh)
857 62 : this->set_interior_mesh(*this);
858 : else
859 80 : this->set_interior_mesh(*other_interior_mesh);
860 : }
861 :
862 142 : if (other_interior_mesh == &other_mesh &&
863 62 : _interior_mesh == this)
864 694 : for (auto & elem_pair : ip_map)
865 632 : elem_pair.second->set_interior_parent(
866 694 : this->elem_ptr(elem_pair.first->interior_parent()->id() + element_id_offset));
867 80 : else if (other_interior_mesh == _interior_mesh)
868 1768 : for (auto & elem_pair : ip_map)
869 : {
870 1688 : Elem * ip = const_cast<Elem *>(elem_pair.first->interior_parent());
871 1688 : libmesh_assert(ip == remote_elem ||
872 : ip == other_interior_mesh->elem_ptr(ip->id()));
873 1688 : elem_pair.second->set_interior_parent(ip);
874 : }
875 : else
876 0 : libmesh_error_msg("Cannot copy boundary elements between meshes with different interior meshes");
877 : }
878 :
879 : // Loop (again) over the elements to fill in the neighbors
880 21294 : if (skip_find_neighbors)
881 : {
882 21266 : old_elems_to_new_elems[remote_elem] = const_cast<RemoteElem*>(remote_elem);
883 :
884 2634368 : for (const auto & old_elem : other_mesh.element_ptr_range())
885 : {
886 2303073 : Elem * new_elem = old_elems_to_new_elems[old_elem];
887 11830291 : for (auto s : old_elem->side_index_range())
888 : {
889 10401418 : const Elem * old_neighbor = old_elem->neighbor_ptr(s);
890 9232946 : Elem * new_neighbor = old_elems_to_new_elems[old_neighbor];
891 8013496 : new_elem->set_neighbor(s, new_neighbor);
892 : }
893 1110 : }
894 : }
895 : }
896 :
897 : #ifdef LIBMESH_ENABLE_UNIQUE_ID
898 : // We set the unique ids of nodes after adding them to the mesh such that our value of
899 : // _next_unique_id may be wrong. So we amend that here
900 21294 : this->set_next_unique_id(other_mesh.parallel_max_unique_id() + unique_id_offset + 1);
901 : #endif
902 :
903 : // Finally, partially prepare the new Mesh for use, if that isn't
904 : // being skipped.
905 : // Even the default behavior here is for backwards compatibility,
906 : // and we don't want to prepare everything.
907 :
908 21294 : if (!skip_preparation)
909 : {
910 : // Keep the same numbering and partitioning and distribution
911 : // status for now, but save our original policies to restore
912 : // later.
913 100 : const bool allowed_renumbering = this->allow_renumbering();
914 100 : const bool allowed_find_neighbors = this->allow_find_neighbors();
915 100 : const bool allowed_elem_removal = this->allow_remote_element_removal();
916 100 : const bool allowed_detect_detect_interior_parents = this->allow_detect_interior_parents();
917 50 : this->allow_renumbering(false);
918 50 : this->allow_remote_element_removal(false);
919 50 : this->allow_find_neighbors(!skip_find_neighbors);
920 100 : this->allow_detect_interior_parents(other_mesh.allow_detect_interior_parents());
921 :
922 : // We should generally be able to skip *all* partitioning here
923 : // because we're only adding one already-consistent mesh to
924 : // another.
925 100 : const bool skipped_partitioning = this->skip_partitioning();
926 50 : this->skip_partitioning(true);
927 :
928 50 : const Preparation old_preparation = this->preparation();
929 175 : this->prepare_for_use();
930 :
931 : //But in the long term, don't change our policies.
932 50 : this->allow_find_neighbors(allowed_find_neighbors);
933 50 : this->allow_renumbering(allowed_renumbering);
934 50 : this->allow_remote_element_removal(allowed_elem_removal);
935 50 : this->skip_partitioning(skipped_partitioning);
936 50 : this->allow_detect_interior_parents(allowed_detect_detect_interior_parents);
937 :
938 : // That prepare_for_use() call marked us as prepared, but we
939 : // specifically avoided some important preparation, so we might not
940 : // actually be prepared now.
941 175 : if (skip_find_neighbors)
942 42 : this->unset_has_neighbor_ptrs();
943 :
944 50 : const Preparation other_preparation = other_mesh.preparation();
945 175 : if (!old_preparation.is_partitioned ||
946 175 : !other_preparation.is_partitioned)
947 0 : this->unset_is_partitioned();
948 175 : if (!old_preparation.has_removed_orphaned_nodes ||
949 175 : !other_preparation.has_removed_orphaned_nodes)
950 0 : this->unset_has_removed_orphaned_nodes();
951 175 : if (!old_preparation.has_removed_remote_elements ||
952 175 : !other_preparation.has_removed_remote_elements)
953 0 : this->unset_has_removed_remote_elements();
954 : }
955 :
956 : // In general we've just invalidated just about everything, and we'd
957 : // like to unset_is_prepared(), but specific use cases might know a
958 : // priori that they're still partitioned well, or that they've
959 : // copied in a disjoint mesh component and don't need new neighbor
960 : // pointers, or that they're not adding anything that would change
961 : // cached subdomain/element/boundary sets, etc., so we'll rely on
962 : // users of the "advanced" skip_preparation option to also set what
963 : // preparation they still need.
964 :
965 : // else
966 : // this->unset_is_prepared();
967 21294 : }
968 :
969 :
970 :
971 74195 : UnstructuredMesh::~UnstructuredMesh ()
972 : {
973 : // this->clear (); // Nothing to clear at this level
974 :
975 34839 : libmesh_exceptionless_assert (!libMesh::closed());
976 74195 : }
977 :
978 :
979 :
980 : namespace {
981 : /**
982 : * \returns \p true if element sides \p a and \p b are the same elem,
983 : * for the purpose of linking them as neighbors in find_neighbors().
984 : *
985 : * This is normally just Elem::operator==, which compares (sorted) node
986 : * ids. But operator== first requires the two sides to have the same
987 : * element type, so it never matches a Polyhedron's polygonal side
988 : * against the TRI3/QUAD4 side of an adjacent standard element (tet,
989 : * hex, ...) even when they are geometrically the same face. At such a
990 : * mixed interface -- exactly one side is a polygon -- we fall back to
991 : * comparing the vertex node ids, which is what low_order_key() already
992 : * keys on. Purely standard/standard and polygon/polygon pairs are left
993 : * entirely to operator==.
994 : */
995 11407824 : bool sides_are_the_same_elem(const Elem & a, const Elem & b)
996 : {
997 11407824 : if (a == b)
998 5255656 : return true;
999 :
1000 : // The check above would have been sufficient if they were both
1001 : // polygons or both not. If just one is a polygon, we need to check the nodes
1002 18 : if (!a.runtime_topology() && !b.runtime_topology())
1003 0 : return false;
1004 :
1005 18 : const unsigned int nv = a.n_vertices();
1006 18 : if (nv != b.n_vertices())
1007 0 : return false;
1008 :
1009 34 : std::vector<dof_id_type> a_ids(nv), b_ids(nv);
1010 72 : for (unsigned int v = 0; v != nv; ++v)
1011 : {
1012 66 : a_ids[v] = a.node_id(v);
1013 78 : b_ids[v] = b.node_id(v);
1014 : }
1015 18 : std::sort(a_ids.begin(), a_ids.end());
1016 18 : std::sort(b_ids.begin(), b_ids.end());
1017 18 : return a_ids == b_ids;
1018 : }
1019 : }
1020 :
1021 :
1022 :
1023 84986 : void UnstructuredMesh::find_neighbors (const bool reset_remote_elements,
1024 : const bool reset_current_list,
1025 : const bool assert_valid,
1026 : const bool check_non_remote)
1027 : {
1028 : // We might actually want to run this on an empty mesh
1029 : // (e.g. the boundary mesh for a nonexistent bcid!)
1030 : // libmesh_assert_not_equal_to (this->n_nodes(), 0);
1031 : // libmesh_assert_not_equal_to (this->n_elem(), 0);
1032 :
1033 : // This function must be run on all processors at once
1034 38252 : parallel_object_only();
1035 :
1036 57666 : LOG_SCOPE("find_neighbors()", "Mesh");
1037 :
1038 : //TODO:[BSK] This should be removed later?!
1039 84986 : if (reset_current_list)
1040 : Threads::parallel_for_each
1041 84536 : (this->element_stored_range(),
1042 6298209 : [reset_remote_elements](Elem * e)
1043 : {
1044 31443267 : for (auto s : e->side_index_range())
1045 31080930 : if (e->neighbor_ptr(s) != remote_elem || reset_remote_elements)
1046 11583958 : e->set_neighbor(s, nullptr);
1047 6263923 : });
1048 :
1049 : // Find neighboring elements by first finding elements
1050 : // with identical side keys and then check to see if they
1051 : // are neighbors
1052 : {
1053 : // data structures -- Use the hash_multimap if available
1054 : typedef dof_id_type key_type;
1055 : typedef std::pair<Elem *, unsigned char> val_type;
1056 : typedef std::unordered_multimap<key_type, val_type> map_type;
1057 :
1058 : // A map from side keys to corresponding elements & side numbers
1059 76504 : map_type side_to_elem_map;
1060 :
1061 : // Pull objects out of the loop to reduce heap operations
1062 84986 : std::unique_ptr<Elem> my_side, their_side;
1063 :
1064 8363458 : for (const auto & element : this->element_ptr_range())
1065 : {
1066 33153501 : for (auto ms : element->side_index_range())
1067 : {
1068 36765712 : next_side:
1069 : // If we haven't yet found a neighbor on this side, try.
1070 : // Even if we think our neighbor is remote, that
1071 : // information may be out of date.
1072 : //
1073 : // If we're only checking remote neighbors, after a
1074 : // redistribution, then we'll skip the non-remote ones
1075 50714856 : if ((element->neighbor_ptr(ms) == nullptr && check_non_remote) ||
1076 11580528 : element->neighbor_ptr(ms) == remote_elem)
1077 : {
1078 : // Get the key for the side of this element. Use the
1079 : // low_order_key so we can find neighbors in
1080 : // mixed-order meshes if necessary.
1081 25207418 : const dof_id_type key = element->low_order_key(ms);
1082 :
1083 : // Look for elements that have an identical side key
1084 11603460 : auto bounds = side_to_elem_map.equal_range(key);
1085 :
1086 : // May be multiple keys, check all the possible
1087 : // elements which _might_ be neighbors.
1088 25207418 : if (bounds.first != bounds.second)
1089 : {
1090 : // Get the side for this element
1091 11400118 : element->side_ptr(my_side, ms);
1092 :
1093 : // Look at all the entries with an equivalent key
1094 11413342 : while (bounds.first != bounds.second)
1095 : {
1096 : // Get the potential element
1097 11407824 : Elem * neighbor = bounds.first->second.first;
1098 :
1099 : // Get the side for the neighboring element
1100 11407824 : const unsigned int ns = bounds.first->second.second;
1101 11407824 : neighbor->side_ptr(their_side, ns);
1102 : //libmesh_assert(my_side.get());
1103 : //libmesh_assert(their_side.get());
1104 :
1105 : // If found a match with my side
1106 : //
1107 : // In 1D, since parents and children have an
1108 : // equal side (i.e. a node) we need to check
1109 : // for matching level() to avoid setting our
1110 : // neighbor pointer to any of our neighbor's
1111 : // descendants.
1112 22815648 : if (sides_are_the_same_elem(*my_side, *their_side) &&
1113 11407824 : (element->level() == neighbor->level()))
1114 : {
1115 : // So share a side. Is this a mixed pair
1116 : // of subactive and active/ancestor
1117 : // elements?
1118 : // If not, then we're neighbors.
1119 : // If so, then the subactive's neighbor is
1120 :
1121 14066950 : if (element->subactive() ==
1122 11394600 : neighbor->subactive())
1123 : {
1124 : // an element is only subactive if it has
1125 : // been coarsened but not deleted
1126 7900486 : element->set_neighbor (ms,neighbor);
1127 11365152 : neighbor->set_neighbor(ns,element);
1128 : }
1129 29448 : else if (element->subactive())
1130 : {
1131 7608 : element->set_neighbor(ms,neighbor);
1132 : }
1133 19036 : else if (neighbor->subactive())
1134 : {
1135 14052 : neighbor->set_neighbor(ns,element);
1136 : }
1137 5249796 : side_to_elem_map.erase (bounds.first);
1138 :
1139 : // get out of this nested crap
1140 11394600 : goto next_side;
1141 : }
1142 :
1143 5868 : ++bounds.first;
1144 : }
1145 : }
1146 :
1147 : // didn't find a match...
1148 : // Build the map entry for this element
1149 : side_to_elem_map.emplace
1150 13812818 : (key, std::make_pair(element, cast_int<unsigned char>(ms)));
1151 : }
1152 : }
1153 27320 : }
1154 27320 : }
1155 :
1156 : #ifdef LIBMESH_ENABLE_PERIODIC
1157 : // Get the disjoint neighbor boundary pairs object (from periodic BCs)
1158 84986 : auto * db = this->get_disjoint_neighbor_boundary_pairs();
1159 :
1160 84986 : if (db)
1161 : {
1162 : // Obtain a point locator
1163 182 : std::unique_ptr<PointLocatorBase> point_locator = this->sub_point_locator();
1164 :
1165 604 : for (const auto & element : this->element_ptr_range())
1166 : {
1167 1598 : for (auto ms : element->side_index_range())
1168 : {
1169 : // Skip if this side already has a valid neighbor (including remote neighbors)
1170 1600 : if (element->neighbor_ptr(ms) != nullptr &&
1171 234 : element->neighbor_ptr(ms) != remote_elem)
1172 182 : continue;
1173 :
1174 3438 : for (const auto & [id, boundary_ptr] : *db)
1175 : {
1176 2448 : if (!this->get_boundary_info().has_boundary_id(element, ms, id))
1177 2286 : continue;
1178 :
1179 : unsigned int neigh_side;
1180 : const Elem * neigh =
1181 198 : db->neighbor(id, *point_locator, element, ms, &neigh_side);
1182 :
1183 162 : if (neigh && neigh != remote_elem && neigh != element)
1184 : {
1185 162 : auto neigh_changeable = this->elem_ptr(neigh->id());
1186 162 : element->set_neighbor(ms, neigh_changeable);
1187 162 : neigh_changeable->set_neighbor(neigh_side, element);
1188 : }
1189 : }
1190 : }
1191 42 : }
1192 42 : }
1193 : #endif // LIBMESH_ENABLE_PERIODIC
1194 :
1195 : #ifdef LIBMESH_ENABLE_AMR
1196 :
1197 : /**
1198 : * Here we look at all of the child elements which
1199 : * don't already have valid neighbors.
1200 : *
1201 : * If a child element has a nullptr neighbor it is
1202 : * either because it is on the boundary or because
1203 : * its neighbor is at a different level. In the
1204 : * latter case we must get the neighbor from the
1205 : * parent.
1206 : *
1207 : * If a child element has a remote_elem neighbor
1208 : * on a boundary it shares with its parent, that
1209 : * info may have become out-dated through coarsening
1210 : * of the neighbor's parent. In this case, if the
1211 : * parent's neighbor is active then the child should
1212 : * share it.
1213 : *
1214 : * Furthermore, that neighbor better be active,
1215 : * otherwise we missed a child somewhere.
1216 : *
1217 : *
1218 : * We also need to look through children ordered by increasing
1219 : * refinement level in order to add new interior_parent() links in
1220 : * boundary elements which have just been generated by refinement,
1221 : * and fix links in boundary elements whose previous
1222 : * interior_parent() has just been coarsened away.
1223 : */
1224 84986 : const unsigned int n_levels = MeshTools::n_levels(*this);
1225 104141 : for (unsigned int level = 1; level < n_levels; ++level)
1226 : {
1227 1675034 : for (auto & current_elem : as_range(level_elements_begin(level),
1228 6149184 : level_elements_end(level)))
1229 : {
1230 1645768 : libmesh_assert(current_elem);
1231 3461091 : Elem * parent = current_elem->parent();
1232 1645768 : libmesh_assert(parent);
1233 3461091 : const unsigned int my_child_num = parent->which_child_am_i(current_elem);
1234 :
1235 17826773 : for (auto s : current_elem->side_index_range())
1236 : {
1237 22789550 : if (current_elem->neighbor_ptr(s) == nullptr ||
1238 12570752 : (current_elem->neighbor_ptr(s) == remote_elem &&
1239 9098 : parent->is_child_on_side(my_child_num, s)))
1240 : {
1241 700448 : Elem * neigh = parent->neighbor_ptr(s);
1242 :
1243 : // If neigh was refined and had non-subactive children
1244 : // made remote earlier, then our current elem should
1245 : // actually have one of those remote children as a
1246 : // neighbor
1247 1418546 : if (neigh &&
1248 553766 : (neigh->ancestor() ||
1249 : // If neigh has subactive children which should have
1250 : // matched as neighbors of the current element but
1251 : // did not, then those likewise must be remote
1252 : // children.
1253 444868 : (current_elem->subactive() && neigh->has_children() &&
1254 0 : (neigh->level()+1) == current_elem->level())))
1255 : {
1256 : #ifdef DEBUG
1257 : // Let's make sure that "had children made remote"
1258 : // situation is actually the case
1259 0 : libmesh_assert(neigh->has_children());
1260 0 : bool neigh_has_remote_children = false;
1261 0 : for (auto & child : neigh->child_ref_range())
1262 0 : if (&child == remote_elem)
1263 0 : neigh_has_remote_children = true;
1264 0 : libmesh_assert(neigh_has_remote_children);
1265 :
1266 : // And let's double-check that we don't have
1267 : // a remote_elem neighboring an active local element
1268 0 : if (current_elem->active())
1269 0 : libmesh_assert_not_equal_to (current_elem->processor_id(),
1270 : this->processor_id());
1271 : #endif // DEBUG
1272 0 : neigh = const_cast<RemoteElem *>(remote_elem);
1273 : }
1274 : // If neigh and current_elem are more than one level
1275 : // apart, figuring out whether we have a remote
1276 : // neighbor here becomes much harder.
1277 989494 : else if (neigh && (current_elem->subactive() &&
1278 15816 : neigh->has_children()))
1279 : {
1280 : // Find the deepest descendant of neigh which
1281 : // we could consider for a neighbor. If we run
1282 : // out of neigh children, then that's our
1283 : // neighbor. If we find a potential neighbor
1284 : // with remote_children and we don't find any
1285 : // potential neighbors among its non-remote
1286 : // children, then our neighbor must be remote.
1287 0 : while (neigh != remote_elem &&
1288 0 : neigh->has_children())
1289 : {
1290 0 : bool found_neigh = false;
1291 0 : for (unsigned int c = 0, nc = neigh->n_children();
1292 0 : !found_neigh && c != nc; ++c)
1293 : {
1294 0 : Elem * child = neigh->child_ptr(c);
1295 0 : if (child == remote_elem)
1296 0 : continue;
1297 0 : for (auto ncn : child->neighbor_ptr_range())
1298 : {
1299 0 : if (ncn != remote_elem &&
1300 0 : ncn->is_ancestor_of(current_elem))
1301 : {
1302 0 : neigh = ncn;
1303 0 : found_neigh = true;
1304 0 : break;
1305 : }
1306 : }
1307 : }
1308 0 : if (!found_neigh)
1309 0 : neigh = const_cast<RemoteElem *>(remote_elem);
1310 : }
1311 : }
1312 973678 : current_elem->set_neighbor(s, neigh);
1313 : #ifdef DEBUG
1314 465336 : if (neigh != nullptr && neigh != remote_elem)
1315 : // We ignore subactive elements here because
1316 : // we don't care about neighbors of subactive element.
1317 214712 : if ((!neigh->active()) && (!current_elem->subactive()))
1318 : {
1319 0 : libMesh::err << "On processor " << this->processor_id()
1320 0 : << std::endl;
1321 0 : libMesh::err << "Bad element ID = " << current_elem->id()
1322 0 : << ", Side " << s << ", Bad neighbor ID = " << neigh->id() << std::endl;
1323 0 : libMesh::err << "Bad element proc_ID = " << current_elem->processor_id()
1324 0 : << ", Bad neighbor proc_ID = " << neigh->processor_id() << std::endl;
1325 0 : libMesh::err << "Bad element size = " << current_elem->hmin()
1326 0 : << ", Bad neighbor size = " << neigh->hmin() << std::endl;
1327 0 : libMesh::err << "Bad element center = " << current_elem->vertex_average()
1328 0 : << ", Bad neighbor center = " << neigh->vertex_average() << std::endl;
1329 0 : libMesh::err << "ERROR: "
1330 0 : << (current_elem->active()?"Active":"Ancestor")
1331 0 : << " Element at level "
1332 0 : << current_elem->level() << std::endl;
1333 0 : libMesh::err << "with "
1334 0 : << (parent->active()?"active":
1335 0 : (parent->subactive()?"subactive":"ancestor"))
1336 0 : << " parent share "
1337 0 : << (neigh->subactive()?"subactive":"ancestor")
1338 0 : << " neighbor at level " << neigh->level()
1339 0 : << std::endl;
1340 0 : NameBasedIO(*this).write ("bad_mesh.gmv");
1341 0 : libmesh_error_msg("Problematic mesh written to bad_mesh.gmv.");
1342 : }
1343 : #endif // DEBUG
1344 : }
1345 : }
1346 :
1347 : // We can skip to the next element if we're full-dimension
1348 : // and therefore don't have any interior parents
1349 3461091 : if (current_elem->dim() >= LIBMESH_DIM)
1350 1884484 : continue;
1351 :
1352 : // We have no interior parents unless we can find one later
1353 2610375 : current_elem->set_interior_parent(nullptr);
1354 :
1355 2610375 : Elem * pip = parent->interior_parent();
1356 :
1357 2610375 : if (!pip)
1358 1980425 : continue;
1359 :
1360 : // If there's no interior_parent children, whether due to a
1361 : // remote element or a non-conformity, then there's no
1362 : // children to search.
1363 4444 : if (pip == remote_elem || pip->active())
1364 : {
1365 0 : current_elem->set_interior_parent(pip);
1366 0 : continue;
1367 : }
1368 :
1369 : // For node comparisons we'll need a sensible tolerance
1370 4444 : Real node_tolerance = current_elem->hmin() * TOLERANCE;
1371 :
1372 : // Otherwise our interior_parent should be a child of our
1373 : // parent's interior_parent.
1374 13354 : for (auto & child : pip->child_ref_range())
1375 : {
1376 : // If we have a remote_elem, that might be our
1377 : // interior_parent. We'll set it provisionally now and
1378 : // keep trying to find something better.
1379 13354 : if (&child == remote_elem)
1380 : {
1381 : current_elem->set_interior_parent
1382 0 : (const_cast<RemoteElem *>(remote_elem));
1383 0 : continue;
1384 : }
1385 :
1386 5860 : bool child_contains_our_nodes = true;
1387 30634 : for (auto & n : current_elem->node_ref_range())
1388 : {
1389 10176 : bool child_contains_this_node = false;
1390 147434 : for (auto & cn : child.node_ref_range())
1391 138524 : if (cn.absolute_fuzzy_equals
1392 107324 : (n, node_tolerance))
1393 : {
1394 6268 : child_contains_this_node = true;
1395 6268 : break;
1396 : }
1397 23182 : if (!child_contains_this_node)
1398 : {
1399 3908 : child_contains_our_nodes = false;
1400 3908 : break;
1401 : }
1402 : }
1403 13354 : if (child_contains_our_nodes)
1404 : {
1405 4444 : current_elem->set_interior_parent(&child);
1406 1952 : break;
1407 : }
1408 : }
1409 :
1410 : // We should have found *some* interior_parent at this
1411 : // point, whether semilocal or remote.
1412 1952 : libmesh_assert(current_elem->interior_parent());
1413 5523 : }
1414 : }
1415 : #endif // AMR
1416 :
1417 : #ifdef DEBUG
1418 38252 : if (assert_valid)
1419 : {
1420 38114 : MeshTools::libmesh_assert_valid_neighbors(*this,
1421 38114 : !reset_remote_elements);
1422 38114 : MeshTools::libmesh_assert_valid_amr_interior_parents(*this);
1423 : }
1424 : #else
1425 : libmesh_ignore(assert_valid);
1426 : #endif
1427 :
1428 84986 : this->_preparation.has_neighbor_ptrs = true;
1429 84986 : }
1430 :
1431 :
1432 :
1433 592 : void UnstructuredMesh::read (const std::string & name,
1434 : void *,
1435 : bool skip_renumber_nodes_and_elements,
1436 : bool skip_find_neighbors,
1437 : bool skip_detect_interior_parents)
1438 : {
1439 : // Set the skip_renumber_nodes_and_elements flag on all processors
1440 : // if necessary.
1441 : // This ensures that renumber_nodes_and_elements is *not* called
1442 : // during prepare_for_use() for certain types of mesh files.
1443 : // This is required in cases where there is an associated solution
1444 : // file which expects a certain ordering of the nodes.
1445 592 : if (Utility::ends_with(name, ".gmv"))
1446 0 : this->allow_renumbering(false);
1447 :
1448 592 : NameBasedIO(*this).read(name);
1449 :
1450 592 : if (skip_renumber_nodes_and_elements)
1451 : {
1452 : // Use MeshBase::allow_renumbering() yourself instead.
1453 : libmesh_deprecated();
1454 0 : this->allow_renumbering(false);
1455 : }
1456 :
1457 : // Done reading the mesh. Now prepare it for use.
1458 356 : const bool old_allow_find_neighbors = this->allow_find_neighbors();
1459 356 : const bool old_allow_detect_interior_parents = this->allow_detect_interior_parents();
1460 :
1461 178 : this->allow_find_neighbors(!skip_find_neighbors);
1462 178 : this->allow_detect_interior_parents(!skip_detect_interior_parents);
1463 :
1464 592 : this->prepare_for_use();
1465 :
1466 178 : this->allow_find_neighbors(old_allow_find_neighbors);
1467 178 : this->allow_detect_interior_parents(old_allow_detect_interior_parents);
1468 592 : }
1469 :
1470 :
1471 :
1472 1274 : void UnstructuredMesh::write (const std::string & name) const
1473 : {
1474 1196 : LOG_SCOPE("write()", "Mesh");
1475 :
1476 1872 : NameBasedIO(*this).write(name);
1477 1274 : }
1478 :
1479 :
1480 :
1481 0 : void UnstructuredMesh::write (const std::string & name,
1482 : const std::vector<Number> & v,
1483 : const std::vector<std::string> & vn) const
1484 : {
1485 0 : LOG_SCOPE("write()", "Mesh");
1486 :
1487 0 : NameBasedIO(*this).write_nodal_data(name, v, vn);
1488 0 : }
1489 :
1490 :
1491 :
1492 :
1493 :
1494 0 : void UnstructuredMesh::create_pid_mesh(UnstructuredMesh & pid_mesh,
1495 : const processor_id_type pid) const
1496 : {
1497 :
1498 : // Issue a warning if the number the number of processors
1499 : // currently available is less that that requested for
1500 : // partitioning. This is not necessarily an error since
1501 : // you may run on one processor and still partition the
1502 : // mesh into several partitions.
1503 : #ifdef DEBUG
1504 0 : if (this->n_processors() < pid)
1505 : {
1506 0 : libMesh::out << "WARNING: You are creating a "
1507 0 : << "mesh for a processor id (="
1508 0 : << pid
1509 0 : << ") greater than "
1510 0 : << "the number of processors available for "
1511 0 : << "the calculation. (="
1512 0 : << this->n_processors()
1513 0 : << ")."
1514 0 : << std::endl;
1515 : }
1516 : #endif
1517 :
1518 0 : this->create_submesh (pid_mesh,
1519 0 : this->active_pid_elements_begin(pid),
1520 0 : this->active_pid_elements_end(pid));
1521 0 : }
1522 :
1523 :
1524 :
1525 :
1526 :
1527 :
1528 :
1529 0 : void UnstructuredMesh::create_submesh (UnstructuredMesh & new_mesh,
1530 : const const_element_iterator & it,
1531 : const const_element_iterator & it_end) const
1532 : {
1533 : // Just in case the subdomain_mesh already has some information
1534 : // in it, get rid of it.
1535 0 : new_mesh.clear();
1536 :
1537 : // If we're not serial, our submesh isn't either.
1538 : // There are no remote elements to delete on an empty mesh, but
1539 : // calling the method to do so marks the mesh as parallel.
1540 0 : if (!this->is_serial())
1541 0 : new_mesh.delete_remote_elements();
1542 :
1543 : // Fail if (*this == new_mesh), we cannot create a submesh inside ourself!
1544 : // This may happen if the user accidentally passes the original mesh into
1545 : // this function! We will check this by making sure we did not just
1546 : // clear ourself.
1547 0 : libmesh_assert_not_equal_to (this->n_nodes(), 0);
1548 0 : libmesh_assert_not_equal_to (this->n_elem(), 0);
1549 :
1550 : // Container to catch boundary IDs handed back by BoundaryInfo
1551 0 : std::vector<boundary_id_type> bc_ids;
1552 :
1553 : // Put any extra integers on the new mesh too
1554 0 : new_mesh.merge_extra_integer_names(*this);
1555 0 : const unsigned int n_node_ints = _node_integer_names.size();
1556 :
1557 0 : for (const auto & old_elem : as_range(it, it_end))
1558 : {
1559 : // Add an equivalent element type to the new_mesh.
1560 : // disconnected_clone() copies ids, extra element integers, etc.
1561 0 : auto uelem = old_elem->disconnected_clone();
1562 0 : Elem * new_elem = new_mesh.add_elem(std::move(uelem));
1563 0 : libmesh_assert(new_elem);
1564 :
1565 : // Loop over the nodes on this element.
1566 0 : for (auto n : old_elem->node_index_range())
1567 : {
1568 0 : const dof_id_type this_node_id = old_elem->node_id(n);
1569 :
1570 : // Add this node to the new mesh if it's not there already
1571 0 : if (!new_mesh.query_node_ptr(this_node_id))
1572 : {
1573 : Node * newn =
1574 0 : new_mesh.add_point (old_elem->point(n),
1575 : this_node_id,
1576 0 : old_elem->node_ptr(n)->processor_id());
1577 :
1578 0 : newn->add_extra_integers(n_node_ints);
1579 0 : for (unsigned int i = 0; i != n_node_ints; ++i)
1580 0 : newn->set_extra_integer(i, old_elem->node_ptr(n)->get_extra_integer(i));
1581 :
1582 : #ifdef LIBMESH_ENABLE_UNIQUE_ID
1583 0 : newn->set_unique_id(old_elem->node_ptr(n)->unique_id());
1584 : #endif
1585 : }
1586 :
1587 : // Define this element's connectivity on the new mesh
1588 0 : new_elem->set_node(n, new_mesh.node_ptr(this_node_id));
1589 : }
1590 :
1591 : // Maybe add boundary conditions for this element
1592 0 : for (auto s : old_elem->side_index_range())
1593 : {
1594 0 : this->get_boundary_info().boundary_ids(old_elem, s, bc_ids);
1595 0 : new_mesh.get_boundary_info().add_side (new_elem, s, bc_ids);
1596 : }
1597 0 : } // end loop over elements
1598 :
1599 : // Prepare the new_mesh for use
1600 0 : new_mesh.prepare_for_use();
1601 0 : }
1602 :
1603 :
1604 :
1605 : #ifdef LIBMESH_ENABLE_AMR
1606 2799 : bool UnstructuredMesh::contract ()
1607 : {
1608 1724 : LOG_SCOPE ("contract()", "Mesh");
1609 :
1610 : // Flag indicating if this call actually changes the mesh
1611 862 : bool mesh_changed = false;
1612 :
1613 : #ifdef DEBUG
1614 545256 : for (const auto & elem : this->element_ptr_range())
1615 544394 : libmesh_assert(elem->active() || elem->subactive() || elem->ancestor());
1616 : #endif
1617 :
1618 : // Loop over the elements.
1619 2387880 : for (auto & elem : this->element_ptr_range())
1620 : {
1621 : // Delete all the subactive ones
1622 1735966 : if (elem->subactive())
1623 : {
1624 : // No level-0 element should be subactive.
1625 : // Note that we CAN'T test elem->level(), as that
1626 : // touches elem->parent()->dim(), and elem->parent()
1627 : // might have already been deleted!
1628 71824 : libmesh_assert(elem->parent());
1629 :
1630 : // Delete the element
1631 : // This just sets a pointer to nullptr, and doesn't
1632 : // invalidate any iterators
1633 236212 : this->delete_elem(elem);
1634 :
1635 : // the mesh has certainly changed
1636 71824 : mesh_changed = true;
1637 : }
1638 : else
1639 : {
1640 : // Compress all the active ones
1641 472570 : if (elem->active())
1642 1140592 : elem->contract();
1643 : else
1644 113338 : libmesh_assert (elem->ancestor());
1645 : }
1646 1075 : }
1647 :
1648 : // Strip any newly-created nullptr voids out of the element array
1649 2799 : this->renumber_nodes_and_elements();
1650 :
1651 : // FIXME: Need to understand why deleting subactive children
1652 : // invalidates the point locator. For now we will clear it explicitly
1653 2799 : this->clear_point_locator();
1654 :
1655 : // Allow our GhostingFunctor objects to reinit if necessary.
1656 4769 : for (auto & gf : as_range(this->ghosting_functors_begin(),
1657 15653 : this->ghosting_functors_end()))
1658 : {
1659 2832 : libmesh_assert(gf);
1660 9160 : gf->mesh_reinit();
1661 : }
1662 :
1663 3661 : return mesh_changed;
1664 : }
1665 : #endif // #ifdef LIBMESH_ENABLE_AMR
1666 :
1667 :
1668 :
1669 1289 : void UnstructuredMesh::all_first_order ()
1670 : {
1671 1188 : LOG_SCOPE("all_first_order()", "Mesh");
1672 :
1673 : /**
1674 : * Prepare to identify (and then delete) a bunch of no-longer-used nodes.
1675 : */
1676 1685 : std::vector<bool> node_touched_by_me(this->max_node_id(), false);
1677 :
1678 : // Loop over the high-ordered elements.
1679 : // First make sure they _are_ indeed high-order, and then replace
1680 : // them with an equivalent first-order element.
1681 117934 : for (auto & so_elem : element_ptr_range())
1682 : {
1683 25880 : libmesh_assert(so_elem);
1684 :
1685 : /*
1686 : * build the first-order equivalent, add to
1687 : * the new_elements list.
1688 : */
1689 : auto lo_elem = Elem::build
1690 : (Elem::first_order_equivalent_type
1691 109636 : (so_elem->type()), so_elem->parent());
1692 :
1693 83756 : const unsigned short n_sides = so_elem->n_sides();
1694 :
1695 399110 : for (unsigned short s=0; s != n_sides; ++s)
1696 413254 : if (so_elem->neighbor_ptr(s) == remote_elem)
1697 0 : lo_elem->set_neighbor(s, const_cast<RemoteElem *>(remote_elem));
1698 :
1699 : #ifdef LIBMESH_ENABLE_AMR
1700 : /*
1701 : * Reset the parent links of any child elements
1702 : */
1703 83756 : if (so_elem->has_children())
1704 99935 : for (unsigned int c = 0, nc = so_elem->n_children(); c != nc; ++c)
1705 : {
1706 78156 : Elem * child = so_elem->child_ptr(c);
1707 78156 : if (child != remote_elem)
1708 48208 : child->set_parent(lo_elem.get());
1709 78156 : lo_elem->add_child(child, c);
1710 : }
1711 :
1712 : /*
1713 : * Reset the child link of any parent element
1714 : */
1715 109636 : if (so_elem->parent())
1716 : {
1717 : unsigned int c =
1718 78156 : so_elem->parent()->which_child_am_i(so_elem);
1719 102260 : lo_elem->parent()->replace_child(lo_elem.get(), c);
1720 : }
1721 :
1722 : /*
1723 : * Copy as much data to the new element as makes sense
1724 : */
1725 109636 : lo_elem->set_p_level(so_elem->p_level());
1726 83756 : lo_elem->set_refinement_flag(so_elem->refinement_flag());
1727 51760 : lo_elem->set_p_refinement_flag(so_elem->p_refinement_flag());
1728 : #endif
1729 :
1730 25880 : libmesh_assert_equal_to (lo_elem->n_vertices(), so_elem->n_vertices());
1731 :
1732 : /*
1733 : * By definition the vertices of the linear and
1734 : * second order element are identically numbered.
1735 : * transfer these.
1736 : */
1737 400790 : for (unsigned int v=0, snv=so_elem->n_vertices(); v < snv; v++)
1738 : {
1739 415414 : lo_elem->set_node(v, so_elem->node_ptr(v));
1740 295140 : node_touched_by_me[lo_elem->node_id(v)] = true;
1741 : }
1742 :
1743 : /*
1744 : * find_neighbors relies on remote_elem neighbor links being
1745 : * properly maintained.
1746 : */
1747 399110 : for (unsigned short s=0; s != n_sides; s++)
1748 : {
1749 413254 : if (so_elem->neighbor_ptr(s) == remote_elem)
1750 0 : lo_elem->set_neighbor(s, const_cast<RemoteElem*>(remote_elem));
1751 : }
1752 :
1753 : /**
1754 : * If the second order element had any boundary conditions they
1755 : * should be transferred to the first-order element. The old
1756 : * boundary conditions will be removed from the BoundaryInfo
1757 : * data structure by insert_elem.
1758 : */
1759 25880 : this->get_boundary_info().copy_boundary_ids
1760 83756 : (this->get_boundary_info(), so_elem, lo_elem.get());
1761 :
1762 : /*
1763 : * The new first-order element is ready.
1764 : * Inserting it into the mesh will replace and delete
1765 : * the second-order element.
1766 : */
1767 83756 : lo_elem->set_id(so_elem->id());
1768 : #ifdef LIBMESH_ENABLE_UNIQUE_ID
1769 51760 : lo_elem->set_unique_id(so_elem->unique_id());
1770 : #endif
1771 :
1772 83756 : const unsigned int nei = so_elem->n_extra_integers();
1773 83756 : lo_elem->add_extra_integers(nei);
1774 84459 : for (unsigned int i=0; i != nei; ++i)
1775 703 : lo_elem->set_extra_integer(i, so_elem->get_extra_integer(i));
1776 :
1777 83756 : lo_elem->inherit_data_from(*so_elem);
1778 :
1779 135516 : this->insert_elem(std::move(lo_elem));
1780 32493 : }
1781 :
1782 : // Deleting nodes does not invalidate iterators, so this is safe.
1783 388442 : for (const auto & node : this->node_ptr_range())
1784 368166 : if (!node_touched_by_me[node->id()])
1785 198083 : this->delete_node(node);
1786 :
1787 : // If crazy people applied boundary info to non-vertices and then
1788 : // deleted those non-vertices, we should make sure their boundary id
1789 : // caches are correct.
1790 1289 : this->get_boundary_info().regenerate_id_sets();
1791 :
1792 : // On hanging nodes that used to also be second order nodes, we
1793 : // might now have an invalid nodal processor_id()
1794 1289 : Partitioner::set_node_processor_ids(*this);
1795 :
1796 : // delete or renumber nodes if desired
1797 1289 : this->prepare_for_use();
1798 1289 : }
1799 :
1800 :
1801 :
1802 : void
1803 2882 : UnstructuredMesh::all_second_order_range (const SimpleRange<element_iterator> & range,
1804 : const bool full_ordered)
1805 : {
1806 1660 : LOG_SCOPE("all_second_order_range()", "Mesh");
1807 :
1808 : /*
1809 : * The maximum number of new second order nodes we might be adding,
1810 : * for use when picking unique unique_id values later. This variable
1811 : * is not used unless unique ids are enabled.
1812 : */
1813 : unsigned int max_new_nodes_per_elem;
1814 :
1815 : /*
1816 : * For speed-up of the \p add_point() method, we
1817 : * can reserve memory. Guess the number of additional
1818 : * nodes based on the element spatial dimensions and the
1819 : * total number of nodes in the mesh as an upper bound.
1820 : */
1821 2882 : switch (this->mesh_dimension())
1822 : {
1823 91 : case 1:
1824 : /*
1825 : * in 1D, there can only be order-increase from Edge2
1826 : * to Edge3. Something like 1/2 of n_nodes() have
1827 : * to be added
1828 : */
1829 26 : max_new_nodes_per_elem = 3 - 2;
1830 182 : this->reserve_nodes(static_cast<unsigned int>
1831 91 : (1.5*static_cast<double>(this->n_nodes())));
1832 65 : break;
1833 :
1834 449 : case 2:
1835 : /*
1836 : * in 2D, either refine from Tri3 to Tri6 (double the nodes)
1837 : * or from Quad4 to Quad8 (again, double) or Quad9 (2.25 that much)
1838 : */
1839 134 : max_new_nodes_per_elem = 9 - 4;
1840 898 : this->reserve_nodes(static_cast<unsigned int>
1841 449 : (2*static_cast<double>(this->n_nodes())));
1842 315 : break;
1843 :
1844 :
1845 2342 : case 3:
1846 : /*
1847 : * in 3D, either refine from Tet4 to Tet10 (factor = 2.5) up to
1848 : * Hex8 to Hex27 (something > 3). Since in 3D there _are_ already
1849 : * quite some nodes, and since we do not want to overburden the memory by
1850 : * a too conservative guess, use the lower bound
1851 : */
1852 670 : max_new_nodes_per_elem = 27 - 8;
1853 4684 : this->reserve_nodes(static_cast<unsigned int>
1854 2342 : (2.5*static_cast<double>(this->n_nodes())));
1855 1672 : break;
1856 :
1857 0 : default:
1858 : // Hm?
1859 0 : libmesh_error_msg("Unknown mesh dimension " << this->mesh_dimension());
1860 : }
1861 :
1862 : // All the real work is done in the helper function
1863 2882 : all_increased_order_range(*this, range, max_new_nodes_per_elem,
1864 85874 : [full_ordered](ElemType t) {
1865 291634 : return Elem::second_order_equivalent_type(t, full_ordered);
1866 : });
1867 2882 : }
1868 :
1869 :
1870 :
1871 2755 : void UnstructuredMesh::all_complete_order_range(const SimpleRange<element_iterator> & range)
1872 : {
1873 1572 : LOG_SCOPE("all_complete_order()", "Mesh");
1874 :
1875 : /*
1876 : * The maximum number of new higher-order nodes we might be adding,
1877 : * for use when picking unique unique_id values later. This variable
1878 : * is not used unless unique ids are enabled.
1879 : */
1880 : unsigned int max_new_nodes_per_elem;
1881 :
1882 : /*
1883 : * for speed-up of the \p add_point() method, we
1884 : * can reserve memory. Guess the number of additional
1885 : * nodes based on the element spatial dimensions and the
1886 : * total number of nodes in the mesh as an upper bound.
1887 : */
1888 2755 : switch (this->mesh_dimension())
1889 : {
1890 0 : case 1:
1891 : /*
1892 : * in 1D, there can only be order-increase from Edge2
1893 : * to Edge3. Something like 1/2 of n_nodes() have
1894 : * to be added
1895 : */
1896 0 : max_new_nodes_per_elem = 3 - 2;
1897 0 : this->reserve_nodes(static_cast<unsigned int>
1898 0 : (1.5*static_cast<double>(this->n_nodes())));
1899 0 : break;
1900 :
1901 175 : case 2:
1902 : /*
1903 : * in 2D, we typically refine from Tri3 or Tri6 to Tri7 (2.3333
1904 : * or 1.1667 times the nodes) but might refine from Quad4 to
1905 : * Quad9 (2.25 times the nodes)
1906 : */
1907 50 : max_new_nodes_per_elem = 9 - 4;
1908 350 : this->reserve_nodes(static_cast<unsigned int>
1909 175 : (2*static_cast<double>(this->n_nodes())));
1910 125 : break;
1911 :
1912 :
1913 2580 : case 3:
1914 : /*
1915 : * in 3D, we typically refine from Tet10 to Tet14 (factor = 1.4)
1916 : * but may go Hex8 to Hex27 or Tet4 to Tet14 (something > 3).
1917 : * Since in 3D there _are_ already quite some nodes, and since
1918 : * we do not want to overburden the memory by a too-conservative
1919 : * guess, use a moderate bound
1920 : */
1921 736 : max_new_nodes_per_elem = 27 - 8;
1922 5160 : this->reserve_nodes(static_cast<unsigned int>
1923 2580 : (2.5*static_cast<double>(this->n_nodes())));
1924 1844 : break;
1925 :
1926 0 : default:
1927 : // Hm?
1928 0 : libmesh_error_msg("Unknown mesh dimension " << this->mesh_dimension());
1929 : }
1930 :
1931 : // All the real work is done in the helper function
1932 2755 : all_increased_order_range(*this, range, max_new_nodes_per_elem,
1933 143145 : [](ElemType t) {
1934 523753 : return Elem::complete_order_equivalent_type(t);
1935 : });
1936 2755 : }
1937 :
1938 :
1939 : std::size_t
1940 161 : UnstructuredMesh::stitch_meshes (const MeshBase & other_mesh,
1941 : boundary_id_type this_mesh_boundary_id,
1942 : boundary_id_type other_mesh_boundary_id,
1943 : Real tol,
1944 : bool clear_stitched_boundary_ids,
1945 : bool verbose,
1946 : bool use_binary_search,
1947 : bool enforce_all_nodes_match_on_boundaries,
1948 : bool merge_boundary_nodes_all_or_nothing,
1949 : bool remap_subdomain_ids,
1950 : bool prepare_after_stitching)
1951 : {
1952 138 : LOG_SCOPE("stitch_meshes()", "UnstructuredMesh");
1953 161 : return stitching_helper(&other_mesh,
1954 : this_mesh_boundary_id,
1955 : other_mesh_boundary_id,
1956 : tol,
1957 : clear_stitched_boundary_ids,
1958 : verbose,
1959 : use_binary_search,
1960 : enforce_all_nodes_match_on_boundaries,
1961 : true,
1962 : merge_boundary_nodes_all_or_nothing,
1963 : remap_subdomain_ids,
1964 189 : prepare_after_stitching);
1965 : }
1966 :
1967 :
1968 : std::size_t
1969 21 : UnstructuredMesh::stitch_surfaces (boundary_id_type boundary_id_1,
1970 : boundary_id_type boundary_id_2,
1971 : Real tol,
1972 : bool clear_stitched_boundary_ids,
1973 : bool verbose,
1974 : bool use_binary_search,
1975 : bool enforce_all_nodes_match_on_boundaries,
1976 : bool merge_boundary_nodes_all_or_nothing,
1977 : bool prepare_after_stitching)
1978 :
1979 : {
1980 21 : return stitching_helper(nullptr,
1981 : boundary_id_1,
1982 : boundary_id_2,
1983 : tol,
1984 : clear_stitched_boundary_ids,
1985 : verbose,
1986 : use_binary_search,
1987 : enforce_all_nodes_match_on_boundaries,
1988 : /* skip_find_neighbors = */ true,
1989 : merge_boundary_nodes_all_or_nothing,
1990 : /* remap_subdomain_ids = */ false,
1991 21 : prepare_after_stitching);
1992 : }
1993 :
1994 :
1995 : std::size_t
1996 182 : UnstructuredMesh::stitching_helper (const MeshBase * other_mesh,
1997 : boundary_id_type this_mesh_boundary_id,
1998 : boundary_id_type other_mesh_boundary_id,
1999 : Real tol,
2000 : bool clear_stitched_boundary_ids,
2001 : bool verbose,
2002 : bool use_binary_search,
2003 : bool enforce_all_nodes_match_on_boundaries,
2004 : bool skip_find_neighbors,
2005 : bool merge_boundary_nodes_all_or_nothing,
2006 : bool remap_subdomain_ids,
2007 : bool prepare_after_stitching)
2008 : {
2009 : #ifdef DEBUG
2010 : // We rely on neighbor links here
2011 52 : MeshTools::libmesh_assert_valid_neighbors(*this);
2012 : #endif
2013 :
2014 52 : bool is_valid_disjoint_pair_to_stitch = false;
2015 :
2016 : #ifdef LIBMESH_ENABLE_PERIODIC
2017 182 : auto * this_db = this->get_disjoint_neighbor_boundary_pairs();
2018 182 : auto * other_db = (other_mesh ? other_mesh->get_disjoint_neighbor_boundary_pairs() : nullptr);
2019 : const bool have_disc_bdys =
2020 182 : (this_db && !this_db->empty()) || (other_db && !other_db->empty());
2021 :
2022 52 : if (have_disc_bdys)
2023 : {
2024 10 : const boundary_id_type a = this_mesh_boundary_id;
2025 10 : const boundary_id_type b = other_mesh_boundary_id;
2026 :
2027 40 : auto get_pb = [](const PeriodicBoundaries * db, boundary_id_type id)
2028 : {
2029 91 : return db ? db->boundary(id) : nullptr;
2030 : };
2031 :
2032 : // this mesh
2033 35 : const auto * pb_this_a = get_pb(this_db, a);
2034 35 : const auto * pb_this_b = get_pb(this_db, b);
2035 10 : const bool in_this =
2036 35 : (pb_this_a && pb_this_a->pairedboundary == b) ||
2037 0 : (pb_this_b && pb_this_b->pairedboundary == a);
2038 :
2039 : // other mesh
2040 25 : const auto * pb_other_b = get_pb(other_db, b);
2041 10 : const auto * pb_other_a = get_pb(other_db, a);
2042 10 : const bool in_other =
2043 35 : (pb_other_b && pb_other_b->pairedboundary == a) ||
2044 0 : (pb_other_a && pb_other_a->pairedboundary == b);
2045 :
2046 : // Conflict conditions:
2047 : // Case 1: On "this" mesh, a or b exist but are not paired,
2048 : // while the other mesh pairs them.
2049 35 : if (!in_this && (pb_this_a || pb_this_b) && in_other)
2050 18 : libmesh_error_msg("Disjoint neighbor boundary pairing mismatch: on 'this' mesh, "
2051 : "boundary (" << a << " or " << b
2052 : << ") exists but is not paired; on 'other' mesh the pair is present.");
2053 :
2054 : // Case 2: On "other" mesh, a or b exist but are not paired,
2055 : // while this mesh pairs them.
2056 28 : if (!in_other && (pb_other_a || pb_other_b) && in_this)
2057 0 : libmesh_error_msg("Disjoint neighbor boundary pairing mismatch: on 'other' mesh, "
2058 : "boundary (" << a << " or " << b
2059 : << ") exists but is not paired; on 'this' mesh the pair is present.");
2060 :
2061 : // Legal conditions: either side has a correct pairing
2062 28 : if (in_this || in_other)
2063 6 : is_valid_disjoint_pair_to_stitch = true;
2064 : }
2065 : #endif // LIBMESH_ENABLE_PERIODIC
2066 :
2067 : // We can't even afford any unset neighbor links here.
2068 175 : if (!this->is_prepared())
2069 7 : this->find_neighbors();
2070 :
2071 : // FIXME: make distributed mesh support efficient.
2072 : // Yes, we currently suck.
2073 275 : MeshSerializer serialize(*this);
2074 :
2075 : // *Badly*.
2076 125 : std::unique_ptr<MeshSerializer> serialize_other;
2077 175 : if (other_mesh)
2078 : serialize_other = std::make_unique<MeshSerializer>
2079 220 : (*const_cast<MeshBase *>(other_mesh));
2080 :
2081 102 : std::map<dof_id_type, dof_id_type> node_to_node_map, other_to_this_node_map; // The second is the inverse map of the first
2082 100 : std::map<dof_id_type, std::vector<dof_id_type>> node_to_elems_map;
2083 :
2084 : typedef dof_id_type key_type;
2085 : typedef std::pair<const Elem *, unsigned char> val_type;
2086 : typedef std::pair<key_type, val_type> key_val_pair;
2087 : typedef std::unordered_multimap<key_type, val_type> map_type;
2088 : // Mapping between all side keys in this mesh and elements+side numbers relevant to the boundary in this mesh as well.
2089 100 : map_type side_to_elem_map;
2090 :
2091 : // If there is only one mesh (i.e. other_mesh == nullptr), then loop over this mesh twice
2092 175 : if (!other_mesh)
2093 : {
2094 6 : other_mesh = this;
2095 : }
2096 :
2097 175 : if ((this_mesh_boundary_id != BoundaryInfo::invalid_id) &&
2098 50 : (other_mesh_boundary_id != BoundaryInfo::invalid_id))
2099 : {
2100 150 : LOG_SCOPE("stitch_meshes node merging", "UnstructuredMesh");
2101 :
2102 : // While finding nodes on the boundary, also find the minimum edge length
2103 : // of all faces on both boundaries. This will later be used in relative
2104 : // distance checks when stitching nodes.
2105 175 : Real h_min = std::numeric_limits<Real>::max();
2106 50 : bool h_min_updated = false;
2107 :
2108 : // Loop below fills in these sets for the two meshes.
2109 100 : std::set<dof_id_type> this_boundary_node_ids, other_boundary_node_ids;
2110 :
2111 : // Pull objects out of the loop to reduce heap operations
2112 175 : std::unique_ptr<const Elem> side;
2113 :
2114 : {
2115 : // Make temporary fixed-size arrays for loop
2116 175 : boundary_id_type id_array[2] = {this_mesh_boundary_id, other_mesh_boundary_id};
2117 175 : std::set<dof_id_type> * set_array[2] = {&this_boundary_node_ids, &other_boundary_node_ids};
2118 175 : const MeshBase * mesh_array[2] = {this, other_mesh};
2119 :
2120 525 : for (unsigned i=0; i<2; ++i)
2121 : {
2122 : // First we deal with node boundary IDs. We only enter
2123 : // this loop if we have at least one nodeset. Note that we
2124 : // do not attempt to make an h_min determination here.
2125 : // The h_min determination is done while looping over the
2126 : // Elems and checking their sides and edges for boundary
2127 : // information, below.
2128 450 : if (mesh_array[i]->get_boundary_info().n_nodeset_conds() > 0)
2129 : {
2130 : // build_node_list() returns a vector of (node-id, bc-id) tuples
2131 32046 : for (const auto & t : mesh_array[i]->get_boundary_info().build_node_list())
2132 : {
2133 31668 : boundary_id_type node_bc_id = std::get<1>(t);
2134 31668 : if (node_bc_id == id_array[i])
2135 : {
2136 6055 : dof_id_type this_node_id = std::get<0>(t);
2137 6055 : set_array[i]->insert( this_node_id );
2138 : }
2139 : }
2140 : }
2141 :
2142 : // Container to catch boundary IDs passed back from BoundaryInfo.
2143 200 : std::vector<boundary_id_type> bc_ids;
2144 :
2145 : // Pointers to boundary NodeElems encountered while looping over the entire Mesh
2146 : // and checking side and edge boundary ids. The Nodes associated with NodeElems
2147 : // may be in a boundary nodeset, but not connected to any other Elems. In this
2148 : // case, we also consider the "minimum node separation distance" amongst all
2149 : // NodeElems when determining the relevant h_min value for this mesh.
2150 200 : std::vector<const Elem *> boundary_node_elems;
2151 :
2152 6390 : for (auto & el : mesh_array[i]->element_ptr_range())
2153 : {
2154 : // Now check whether elem has a face on the specified boundary
2155 27401 : for (auto side_id : el->side_index_range())
2156 : {
2157 : bool should_stitch_this_side =
2158 28570 : (el->neighbor_ptr(side_id) == nullptr) ||
2159 140 : (is_valid_disjoint_pair_to_stitch &&
2160 180 : mesh_array[i]->get_boundary_info().has_boundary_id(el, side_id, id_array[i]));
2161 :
2162 6340 : if (should_stitch_this_side)
2163 : {
2164 : // Get *all* boundary IDs on this side, not just the first one!
2165 12294 : mesh_array[i]->get_boundary_info().boundary_ids (el, side_id, bc_ids);
2166 :
2167 12294 : if (std::find(bc_ids.begin(), bc_ids.end(), id_array[i]) != bc_ids.end())
2168 : {
2169 1624 : el->build_side_ptr(side, side_id);
2170 11095 : for (auto & n : side->node_ref_range())
2171 9471 : set_array[i]->insert(n.id());
2172 :
2173 1624 : h_min = std::min(h_min, side->hmin());
2174 464 : h_min_updated = true;
2175 :
2176 : // This side is on the boundary, add its information to side_to_elem
2177 1624 : if (skip_find_neighbors && (i==0))
2178 : {
2179 812 : key_type key = el->low_order_key(side_id);
2180 232 : val_type val;
2181 812 : val.first = el;
2182 232 : val.second = cast_int<unsigned char>(side_id);
2183 :
2184 812 : key_val_pair kvp;
2185 812 : kvp.first = key;
2186 232 : kvp.second = val;
2187 232 : side_to_elem_map.insert (kvp);
2188 : }
2189 : }
2190 :
2191 : // Also, check the edges on this side. We don't have to worry about
2192 : // updating neighbor info in this case since elements don't store
2193 : // neighbor info on edges.
2194 121886 : for (auto edge_id : el->edge_index_range())
2195 : {
2196 109592 : if (el->is_edge_on_side(edge_id, side_id))
2197 : {
2198 : // Get *all* boundary IDs on this edge, not just the first one!
2199 36330 : mesh_array[i]->get_boundary_info().edge_boundary_ids (el, edge_id, bc_ids);
2200 :
2201 36330 : if (std::find(bc_ids.begin(), bc_ids.end(), id_array[i]) != bc_ids.end())
2202 : {
2203 0 : std::unique_ptr<const Elem> edge (el->build_edge_ptr(edge_id));
2204 0 : for (auto & n : edge->node_ref_range())
2205 0 : set_array[i]->insert( n.id() );
2206 :
2207 0 : h_min = std::min(h_min, edge->hmin());
2208 0 : h_min_updated = true;
2209 0 : }
2210 : }
2211 : } // end for (edge_id)
2212 : } // end if (should_stitch_this_side)
2213 : } // end for (side_id)
2214 :
2215 : // Alternatively, is this a boundary NodeElem? If so,
2216 : // add it to a list of NodeElems that will later be
2217 : // used to set h_min based on the minimum node
2218 : // separation distance between all pairs of boundary
2219 : // NodeElems.
2220 4053 : if (el->type() == NODEELEM)
2221 : {
2222 396 : mesh_array[i]->get_boundary_info().boundary_ids(el->node_ptr(0), bc_ids);
2223 324 : if (std::find(bc_ids.begin(), bc_ids.end(), id_array[i]) != bc_ids.end())
2224 : {
2225 252 : boundary_node_elems.push_back(el);
2226 :
2227 : // Debugging:
2228 : // libMesh::out << "Elem " << el->id() << " is a NodeElem on boundary " << id_array[i] << std::endl;
2229 : }
2230 : } // end if (el->type() == NODEELEM)
2231 150 : } // end for (el)
2232 :
2233 : // Compute the minimum node separation distance amongst
2234 : // all boundary NodeElem pairs.
2235 : {
2236 200 : const auto N = boundary_node_elems.size();
2237 602 : for (auto node_elem_i : make_range(N))
2238 4662 : for (auto node_elem_j : make_range(node_elem_i+1, N))
2239 : {
2240 : Real node_sep =
2241 6930 : (boundary_node_elems[node_elem_i]->point(0) - boundary_node_elems[node_elem_j]->point(0)).norm();
2242 :
2243 : // We only want to consider non-coincident
2244 : // boundary NodeElem pairs when determining the
2245 : // minimum node separation distance.
2246 4410 : if (node_sep > 0.)
2247 : {
2248 4410 : h_min = std::min(h_min, node_sep);
2249 1260 : h_min_updated = true;
2250 : }
2251 : } // end for (node_elem_j)
2252 : } // end minimum NodeElem separation scope
2253 : } // end for (i)
2254 : } // end scope
2255 :
2256 175 : if (verbose)
2257 : {
2258 14 : libMesh::out << "In UnstructuredMesh::stitch_meshes:\n"
2259 14 : << "This mesh has " << this_boundary_node_ids.size()
2260 14 : << " nodes on boundary `"
2261 49 : << this->get_boundary_info().get_sideset_name(this_mesh_boundary_id)
2262 14 : << "' (" << this_mesh_boundary_id << ").\n"
2263 14 : << "Other mesh has " << other_boundary_node_ids.size()
2264 14 : << " nodes on boundary `"
2265 49 : << other_mesh->get_boundary_info().get_sideset_name(other_mesh_boundary_id)
2266 14 : << "' (" << other_mesh_boundary_id << ").\n";
2267 :
2268 49 : if (h_min_updated)
2269 : {
2270 28 : libMesh::out << "Minimum edge length on both surfaces is " << h_min << ".\n";
2271 : }
2272 : else
2273 : {
2274 0 : libMesh::out << "No minimum edge length determined on specified surfaces." << std::endl;
2275 : }
2276 : }
2277 :
2278 : // At this point, if h_min==0 it means that there were at least two coincident
2279 : // nodes on the surfaces being stitched, and we don't currently support that case.
2280 : // (It might be possible to support, but getting it exactly right would be tricky
2281 : // and probably not worth the extra complications to the "normal" case.)
2282 175 : libmesh_error_msg_if(h_min < std::numeric_limits<Real>::epsilon(),
2283 : "Coincident nodes detected on source and/or target "
2284 : "surface, stitching meshes is not possible.");
2285 :
2286 : // We require nanoflann for the "binary search" (really kd-tree)
2287 : // option to work. If it's not available, turn that option off,
2288 : // warn the user, and fall back on the N^2 search algorithm.
2289 : if (use_binary_search)
2290 : {
2291 : #ifndef LIBMESH_HAVE_NANOFLANN
2292 : use_binary_search = false;
2293 : libmesh_warning("The use_binary_search option in the "
2294 : "UnstructuredMesh stitching algorithms requires nanoflann "
2295 : "support. Falling back on N^2 search algorithm.");
2296 : #endif
2297 : }
2298 :
2299 175 : if (!this_boundary_node_ids.empty())
2300 : {
2301 175 : if (use_binary_search)
2302 : {
2303 : #ifdef LIBMESH_HAVE_NANOFLANN
2304 : typedef nanoflann::KDTreeSingleIndexAdaptor<nanoflann::L2_Simple_Adaptor<Real, VectorOfNodesAdaptor>,
2305 : VectorOfNodesAdaptor, 3, std::size_t> kd_tree_t;
2306 :
2307 : // Create the dataset needed to build the kd tree with nanoflann
2308 0 : std::vector<std::pair<Point, dof_id_type>> this_mesh_nodes(this_boundary_node_ids.size());
2309 :
2310 0 : for (auto [it, ctr] = std::make_tuple(this_boundary_node_ids.begin(), 0u);
2311 0 : it != this_boundary_node_ids.end(); ++it, ++ctr)
2312 : {
2313 0 : this_mesh_nodes[ctr].first = this->point(*it);
2314 0 : this_mesh_nodes[ctr].second = *it;
2315 : }
2316 :
2317 0 : VectorOfNodesAdaptor vec_nodes_adaptor(this_mesh_nodes);
2318 :
2319 0 : kd_tree_t this_kd_tree(3, vec_nodes_adaptor, 10);
2320 0 : this_kd_tree.buildIndex();
2321 :
2322 : // Storage for nearest neighbor in the loop below
2323 : std::size_t ret_index;
2324 : Real ret_dist_sqr;
2325 :
2326 : // Loop over other mesh. For each node, find its nearest neighbor in this mesh, and fill in the maps.
2327 0 : for (const auto & node_id : other_boundary_node_ids)
2328 : {
2329 0 : const auto & p = other_mesh->point(node_id);
2330 0 : const Real query_pt[] = {p(0), p(1), p(2)};
2331 0 : this_kd_tree.knnSearch(&query_pt[0], 1, &ret_index, &ret_dist_sqr);
2332 :
2333 : // TODO: here we should use the user's specified tolerance
2334 : // and the previously determined value of h_min in the
2335 : // distance comparison, not just TOLERANCE^2.
2336 0 : if (ret_dist_sqr < TOLERANCE*TOLERANCE)
2337 : {
2338 0 : node_to_node_map[this_mesh_nodes[ret_index].second] = node_id;
2339 0 : other_to_this_node_map[node_id] = this_mesh_nodes[ret_index].second;
2340 : }
2341 : }
2342 :
2343 : // If the two maps don't have the same size, it means one
2344 : // node in this mesh is the nearest neighbor of several
2345 : // nodes in other mesh. Since the stitching is ambiguous in
2346 : // this case, we throw an error.
2347 0 : libmesh_error_msg_if(node_to_node_map.size() != other_to_this_node_map.size(),
2348 : "Error: Found multiple matching nodes in stitch_meshes");
2349 : #endif
2350 : }
2351 : else // !use_binary_search
2352 : {
2353 : // In the unlikely event that two meshes composed entirely of
2354 : // NodeElems are being stitched together, we will not have
2355 : // selected a valid h_min value yet, and the distance
2356 : // comparison below will be true for essentially any two
2357 : // nodes. In this case we simply fall back on an absolute
2358 : // distance check.
2359 175 : if (!h_min_updated)
2360 : {
2361 : libmesh_warning("No valid h_min value was found, falling back on "
2362 : "absolute distance check in the N^2 search algorithm.");
2363 0 : h_min = 1.;
2364 : }
2365 :
2366 : // Otherwise, use a simple N^2 search to find the closest matching points. This can be helpful
2367 : // in the case that we have tolerance issues which cause mismatch between the two surfaces
2368 : // that are being stitched.
2369 3248 : for (const auto & this_node_id : this_boundary_node_ids)
2370 : {
2371 3073 : Node & this_node = this->node_ref(this_node_id);
2372 :
2373 878 : bool found_matching_nodes = false;
2374 :
2375 91546 : for (const auto & other_node_id : other_boundary_node_ids)
2376 : {
2377 88473 : const Node & other_node = other_mesh->node_ref(other_node_id);
2378 :
2379 63195 : Real node_distance = (this_node - other_node).norm();
2380 :
2381 88473 : if (node_distance < tol*h_min)
2382 : {
2383 : // Make sure we didn't already find a matching node!
2384 3073 : libmesh_error_msg_if(found_matching_nodes,
2385 : "Error: Found multiple matching nodes in stitch_meshes");
2386 :
2387 3073 : node_to_node_map[this_node_id] = other_node_id;
2388 3073 : other_to_this_node_map[other_node_id] = this_node_id;
2389 :
2390 878 : found_matching_nodes = true;
2391 : }
2392 : }
2393 : }
2394 : }
2395 : }
2396 :
2397 : // Build up the node_to_elems_map, using only one loop over other_mesh
2398 3150 : for (auto & el : other_mesh->element_ptr_range())
2399 : {
2400 : // For each node on the element, find the corresponding node
2401 : // on "this" Mesh, 'this_node_id', if it exists, and push
2402 : // the current element ID back onto node_to_elems_map[this_node_id].
2403 : // For that we will use the reverse mapping we created at
2404 : // the same time as the forward mapping.
2405 29585 : for (auto & n : el->node_ref_range())
2406 34740 : if (const auto it = other_to_this_node_map.find(/*other_node_id=*/n.id());
2407 7720 : it != other_to_this_node_map.end())
2408 4977 : node_to_elems_map[/*this_node_id=*/it->second].push_back( el->id() );
2409 75 : }
2410 :
2411 175 : if (verbose)
2412 : {
2413 14 : libMesh::out << "In UnstructuredMesh::stitch_meshes:\n"
2414 28 : << "Found " << node_to_node_map.size()
2415 14 : << " matching nodes.\n"
2416 14 : << std::endl;
2417 : }
2418 :
2419 175 : if (enforce_all_nodes_match_on_boundaries)
2420 : {
2421 2 : std::size_t n_matching_nodes = node_to_node_map.size();
2422 2 : std::size_t this_mesh_n_nodes = this_boundary_node_ids.size();
2423 2 : std::size_t other_mesh_n_nodes = other_boundary_node_ids.size();
2424 7 : libmesh_error_msg_if((n_matching_nodes != this_mesh_n_nodes) || (n_matching_nodes != other_mesh_n_nodes),
2425 : "Error: We expected the number of nodes to match.");
2426 : }
2427 :
2428 175 : if (merge_boundary_nodes_all_or_nothing)
2429 : {
2430 2 : std::size_t n_matching_nodes = node_to_node_map.size();
2431 2 : std::size_t this_mesh_n_nodes = this_boundary_node_ids.size();
2432 2 : std::size_t other_mesh_n_nodes = other_boundary_node_ids.size();
2433 7 : if ((n_matching_nodes != this_mesh_n_nodes) || (n_matching_nodes != other_mesh_n_nodes))
2434 : {
2435 0 : if (verbose)
2436 : {
2437 : libMesh::out << "Skipping node merging in "
2438 : "UnstructuredMesh::stitch_meshes because not "
2439 0 : "all boundary nodes were matched."
2440 0 : << std::endl;
2441 : }
2442 0 : node_to_node_map.clear();
2443 0 : other_to_this_node_map.clear();
2444 0 : node_to_elems_map.clear();
2445 : }
2446 100 : }
2447 150 : }
2448 : else
2449 : {
2450 0 : if (verbose)
2451 : {
2452 0 : libMesh::out << "Skip node merging in UnstructuredMesh::stitch_meshes:" << std::endl;
2453 : }
2454 : }
2455 :
2456 175 : dof_id_type node_delta = this->max_node_id();
2457 175 : dof_id_type elem_delta = this->max_elem_id();
2458 :
2459 : unique_id_type unique_delta =
2460 : #ifdef LIBMESH_ENABLE_UNIQUE_ID
2461 175 : this->parallel_max_unique_id();
2462 : #else
2463 : 0;
2464 : #endif
2465 :
2466 : // If other_mesh != nullptr, then we have to do a bunch of work
2467 : // in order to copy it to this mesh
2468 175 : if (this!=other_mesh)
2469 : {
2470 132 : LOG_SCOPE("stitch_meshes copying", "UnstructuredMesh");
2471 :
2472 : #ifdef LIBMESH_ENABLE_PERIODIC
2473 : // Copy disjoint neighbor boundary pairs (PeriodicBoundary objects)
2474 : // from `other_mesh` to `this` mesh
2475 154 : if (other_db && !other_db->empty())
2476 : {
2477 21 : for (const auto & [bdy_id, pb_ptr] : *other_db)
2478 : {
2479 4 : const auto & pb = *pb_ptr;
2480 14 : const boundary_id_type a = pb.myboundary;
2481 14 : const boundary_id_type b = pb.pairedboundary;
2482 :
2483 14 : if (this_db)
2484 : {
2485 : // Skip if identical pair already exists
2486 14 : if (const auto * existing_pb = this_db->boundary(a))
2487 7 : if ((existing_pb->myboundary == a && existing_pb->pairedboundary == b) ||
2488 0 : (existing_pb->myboundary == b && existing_pb->pairedboundary == a))
2489 5 : continue;
2490 :
2491 : // If both boundary ids exist on this mesh but aren't paired here, refuse to create a new pair
2492 2 : const auto & bdy_ids = this->get_boundary_info().get_boundary_ids();
2493 4 : const bool a_exists = bdy_ids.count(a);
2494 4 : const bool b_exists = bdy_ids.count(b);
2495 : // If a and b already exist on `this`, we should be screaming and dying
2496 : // unless they already have PeriodicBoundary objects connecting them too
2497 7 : if (a_exists && b_exists && !this_db->boundary(a))
2498 0 : libmesh_error_msg("Conflict: boundaries " << a << " and " << b
2499 : << " already exist on this mesh but are not paired.");
2500 : }
2501 :
2502 12 : this->add_disjoint_neighbor_boundary_pairs(a, b, pb.get_corresponding_pos(Point(0.0,0.0,0.0)));
2503 : }
2504 : }
2505 : #endif // LIBMESH_ENABLE_PERIODIC
2506 :
2507 :
2508 : // Increment the node_to_node_map and node_to_elems_map
2509 : // to account for id offsets
2510 3185 : for (auto & pr : node_to_node_map)
2511 3031 : pr.second += node_delta;
2512 :
2513 3185 : for (auto & pr : node_to_elems_map)
2514 7966 : for (auto & entry : pr.second)
2515 4935 : entry += elem_delta;
2516 :
2517 : // We run into problems when the libMesh subdomain standard (the
2518 : // id defines the subdomain; the name was an afterthought) and
2519 : // the MOOSE standard (the name defines the subdomain; the id
2520 : // might be autogenerated) clash.
2521 : //
2522 : // Subdomain ids with the same name in both meshes are surely
2523 : // meant to represent the same subdomain. We can just merge
2524 : // them.
2525 : //
2526 : // Subdomain ids which don't have a name in either mesh are
2527 : // almost surely meant to represent the same subdomain. We'll
2528 : // just merge them.
2529 : //
2530 : // Subdomain ids with different names in different meshes, or
2531 : // names with different ids in different meshes, are trickier.
2532 : // For backwards compatibility we default to the old "just copy
2533 : // all the subdomain ids over" behavior, but if requested we'll
2534 : // remap any ids that appear to be clear conflicts, and we'll
2535 : // scream and die if we see any ids that are ambiguous due to
2536 : // being named in one mesh but not the other.
2537 88 : std::unordered_map<subdomain_id_type, subdomain_id_type> id_remapping;
2538 154 : if (remap_subdomain_ids)
2539 : {
2540 4 : const auto & this_map = this->get_subdomain_name_map();
2541 4 : const auto & other_map = other_mesh->get_subdomain_name_map();
2542 8 : std::unordered_map<std::string, subdomain_id_type> other_map_reversed;
2543 28 : for (auto & [sid, sname] : other_map)
2544 4 : other_map_reversed.emplace(sname, sid);
2545 :
2546 8 : std::unordered_map<std::string, subdomain_id_type> this_map_reversed;
2547 21 : for (auto & [sid, sname] : this_map)
2548 2 : this_map_reversed.emplace(sname, sid);
2549 :
2550 : // We don't require either mesh to be prepared, but that
2551 : // means we need to check for subdomains manually.
2552 28 : auto get_subdomains = [](const MeshBase & mesh) {
2553 8 : std::set<subdomain_id_type> all_subdomains;
2554 368 : for (auto & el : mesh.element_ptr_range())
2555 236 : all_subdomains.insert(el->subdomain_id());
2556 28 : return all_subdomains;
2557 : };
2558 :
2559 18 : const auto this_subdomains = get_subdomains(*this);
2560 18 : const auto other_subdomains = get_subdomains(*other_mesh);
2561 :
2562 21 : for (auto & [sid, sname] : this_map)
2563 : {
2564 : // The same name with the same id means we're fine. The
2565 : // same name with another id means we remap their id to
2566 : // ours
2567 2 : if (const auto other_reverse_it = other_map_reversed.find(sname);
2568 7 : other_reverse_it != other_map_reversed.end() && other_reverse_it->second != sid)
2569 7 : id_remapping[other_reverse_it->second] = sid;
2570 :
2571 : // The same id with a different name, we'll get to
2572 : // later. The same id without any name means we don't
2573 : // know what the user wants.
2574 2 : if (other_subdomains.count(sid) && !other_map.count(sid))
2575 0 : libmesh_error_msg("Can't safely stitch with a mesh sharing subdomain id "
2576 : << sid << " but not subdomain name " << sname);
2577 : }
2578 :
2579 14 : subdomain_id_type next_free_id = 0;
2580 : // We might try to stitch empty meshes ...
2581 14 : if (!this_subdomains.empty())
2582 14 : next_free_id = *this_subdomains.rbegin() + 1;
2583 14 : if (!other_subdomains.empty())
2584 14 : next_free_id =
2585 14 : std::max(next_free_id,
2586 : cast_int<subdomain_id_type>
2587 23 : (*other_subdomains.rbegin() + 1));
2588 :
2589 21 : for (auto & [sid, sname] : other_map)
2590 : {
2591 : // At this point we've figured out any remapping
2592 : // necessary for an sname that we share. And we don't
2593 : // need to remap any sid we don't share.
2594 8 : if (!this_map_reversed.count(sname))
2595 : {
2596 : // But if we don't have this sname and we do have this
2597 : // sid then we can't just merge into that.
2598 2 : if (this_subdomains.count(sid))
2599 : {
2600 : // If we have this sid with no name, we don't
2601 : // know what the user wants.
2602 2 : if (!this_map.count(sid))
2603 19 : libmesh_error_msg("Can't safely stitch with a mesh sharing subdomain id "
2604 : << sid << " but under subdomain name " << sname);
2605 :
2606 : // We have this sid under a different name, so
2607 : // we just need to give the other elements a new
2608 : // id.
2609 :
2610 : // Users might have done crazy things with id
2611 : // choice so let's make sure they didn't get too
2612 : // crazy.
2613 0 : libmesh_error_msg_if ((!this_subdomains.empty() &&
2614 : next_free_id < *this_subdomains.rbegin()) ||
2615 : (!other_subdomains.empty() &&
2616 : next_free_id < *other_subdomains.rbegin()),
2617 : "Subdomain id overflow");
2618 :
2619 0 : id_remapping[sid] = next_free_id++;
2620 0 : this->set_subdomain_name(next_free_id, sname);
2621 : }
2622 : // If we don't have this subdomain id, well, we're
2623 : // about to, so we should have its name too.
2624 : else
2625 0 : this->set_subdomain_name(sid, sname);
2626 : }
2627 : }
2628 : }
2629 :
2630 : // Copy mesh data. If we skip the call to find_neighbors(), the lists
2631 : // of neighbors will be copied verbatim from the other mesh
2632 147 : this->copy_nodes_and_elements(*other_mesh, skip_find_neighbors,
2633 : elem_delta, node_delta,
2634 84 : unique_delta, &id_remapping);
2635 :
2636 : // Copy BoundaryInfo from other_mesh too. We do this via the
2637 : // list APIs rather than element-by-element for speed.
2638 42 : BoundaryInfo & boundary = this->get_boundary_info();
2639 42 : const BoundaryInfo & other_boundary = other_mesh->get_boundary_info();
2640 :
2641 15701 : for (const auto & t : other_boundary.build_node_list())
2642 15512 : boundary.add_node(std::get<0>(t) + node_delta,
2643 15512 : std::get<1>(t));
2644 :
2645 4263 : for (const auto & t : other_boundary.build_side_list())
2646 5238 : boundary.add_side(std::get<0>(t) + elem_delta,
2647 4074 : std::get<1>(t),
2648 4074 : std::get<2>(t));
2649 :
2650 189 : for (const auto & t : other_boundary.build_edge_list())
2651 0 : boundary.add_edge(std::get<0>(t) + elem_delta,
2652 0 : std::get<1>(t),
2653 0 : std::get<2>(t));
2654 :
2655 189 : for (const auto & t : other_boundary.build_shellface_list())
2656 0 : boundary.add_shellface(std::get<0>(t) + elem_delta,
2657 0 : std::get<1>(t),
2658 0 : std::get<2>(t));
2659 :
2660 42 : const auto & other_ns_id_to_name = other_boundary.get_nodeset_name_map();
2661 42 : auto & ns_id_to_name = boundary.set_nodeset_name_map();
2662 147 : ns_id_to_name.insert(other_ns_id_to_name.begin(), other_ns_id_to_name.end());
2663 :
2664 42 : const auto & other_ss_id_to_name = other_boundary.get_sideset_name_map();
2665 42 : auto & ss_id_to_name = boundary.set_sideset_name_map();
2666 147 : ss_id_to_name.insert(other_ss_id_to_name.begin(), other_ss_id_to_name.end());
2667 :
2668 42 : const auto & other_es_id_to_name = other_boundary.get_edgeset_name_map();
2669 42 : auto & es_id_to_name = boundary.set_edgeset_name_map();
2670 147 : es_id_to_name.insert(other_es_id_to_name.begin(), other_es_id_to_name.end());
2671 :
2672 : // Merge other_mesh's elemset information with ours. Throw an
2673 : // error if this and other_mesh have overlapping elemset codes
2674 : // that refer to different elemset ids.
2675 189 : std::vector<dof_id_type> this_elemset_codes = this->get_elemset_codes();
2676 84 : MeshBase::elemset_type this_id_set_to_fill, other_id_set_to_fill;
2677 217 : for (const auto & elemset_code : other_mesh->get_elemset_codes())
2678 : {
2679 : // Get the elemset ids for this elemset_code on other_mesh
2680 28 : other_mesh->get_elemsets(elemset_code, other_id_set_to_fill);
2681 :
2682 : // Check that this elemset code does not already exist
2683 : // in this mesh, or if it does, that it has the same elemset
2684 : // ids associated with it.
2685 : //
2686 : // Note: get_elemset_codes() is guaranteed to return a
2687 : // sorted vector, so we can binary search in it.
2688 12 : auto it = Utility::binary_find(this_elemset_codes.begin(),
2689 : this_elemset_codes.end(),
2690 16 : elemset_code);
2691 :
2692 28 : if (it != this_elemset_codes.end())
2693 : {
2694 : // This mesh has the same elemset code. Does it refer to
2695 : // the same elemset ids?
2696 0 : this->get_elemsets(elemset_code, this_id_set_to_fill);
2697 :
2698 : // Throw an error if they don't match, otherwise we
2699 : // don't need to do anything
2700 0 : libmesh_error_msg_if(other_id_set_to_fill != this_id_set_to_fill,
2701 : "Attempted to stitch together meshes with conflicting elemset codes.");
2702 : }
2703 : else
2704 : {
2705 : // Add other_mesh's elemset code to this mesh
2706 48 : this->add_elemset_code(elemset_code, other_id_set_to_fill);
2707 : }
2708 : }
2709 :
2710 : } // end if (other_mesh)
2711 :
2712 : // Finally, we need to "merge" the overlapping nodes
2713 : // We do this by iterating over node_to_elems_map and updating
2714 : // the elements so that they "point" to the nodes that came
2715 : // from this mesh, rather than from other_mesh.
2716 : // Then we iterate over node_to_node_map and delete the
2717 : // duplicate nodes that came from other_mesh.
2718 :
2719 : {
2720 144 : LOG_SCOPE("stitch_meshes node updates", "UnstructuredMesh");
2721 :
2722 : // Container to catch boundary IDs passed back from BoundaryInfo.
2723 96 : std::vector<boundary_id_type> bc_ids;
2724 :
2725 3178 : for (const auto & [target_node_id, elem_vec] : node_to_elems_map)
2726 : {
2727 3010 : dof_id_type other_node_id = node_to_node_map[target_node_id];
2728 3010 : Node & target_node = this->node_ref(target_node_id);
2729 :
2730 1720 : std::size_t n_elems = elem_vec.size();
2731 7875 : for (std::size_t i=0; i<n_elems; i++)
2732 : {
2733 4865 : dof_id_type elem_id = elem_vec[i];
2734 4865 : Elem * el = this->elem_ptr(elem_id);
2735 :
2736 : // find the local node index that we want to update
2737 3475 : unsigned int local_node_index = el->local_node(other_node_id);
2738 1390 : libmesh_assert_not_equal_to(local_node_index, libMesh::invalid_uint);
2739 :
2740 : // We also need to copy over the nodeset info here,
2741 : // because the node will get deleted below
2742 6255 : this->get_boundary_info().boundary_ids(el->node_ptr(local_node_index), bc_ids);
2743 4865 : el->set_node(local_node_index, &target_node);
2744 4865 : this->get_boundary_info().add_node(&target_node, bc_ids);
2745 : }
2746 : }
2747 : }
2748 :
2749 : {
2750 144 : LOG_SCOPE("stitch_meshes node deletion", "UnstructuredMesh");
2751 3178 : for (const auto & [other_node_id, this_node_id] : node_to_node_map)
2752 : {
2753 : // In the case that this==other_mesh, the two nodes might be the same (e.g. if
2754 : // we're stitching a "sliver"), hence we need to skip node deletion in that case.
2755 3010 : if ((this == other_mesh) && (this_node_id == other_node_id))
2756 0 : continue;
2757 :
2758 3010 : this->delete_node( this->node_ptr(this_node_id) );
2759 : }
2760 : }
2761 :
2762 : // If find_neighbors() wasn't called in prepare_for_use(), we need to
2763 : // manually loop once more over all elements adjacent to the stitched boundary
2764 : // and fix their lists of neighbors.
2765 : // This is done according to the following steps:
2766 : // 1. Loop over all copied elements adjacent to the boundary using node_to_elems_map (trying to avoid duplicates)
2767 : // 2. Look at all their sides with a nullptr neighbor and update them using side_to_elem_map if necessary
2768 : // 3. Update the corresponding side in side_to_elem_map as well
2769 168 : if (skip_find_neighbors)
2770 : {
2771 144 : LOG_SCOPE("stitch_meshes neighbor fixes", "UnstructuredMesh");
2772 :
2773 : // Pull objects out of the loop to reduce heap operations
2774 168 : std::unique_ptr<const Elem> my_side, their_side;
2775 :
2776 96 : std::set<dof_id_type> fixed_elems;
2777 3178 : for (const auto & pr : node_to_elems_map)
2778 : {
2779 1720 : std::size_t n_elems = pr.second.size();
2780 7875 : for (std::size_t i=0; i<n_elems; i++)
2781 : {
2782 6255 : dof_id_type elem_id = pr.second[i];
2783 1390 : if (!fixed_elems.count(elem_id))
2784 : {
2785 1036 : Elem * el = this->elem_ptr(elem_id);
2786 740 : fixed_elems.insert(elem_id);
2787 5642 : for (auto s : el->side_index_range())
2788 : {
2789 4606 : bool has_real_neighbor = (el->neighbor_ptr(s) != nullptr);
2790 4690 : bool has_disdjoint_neighbor = is_valid_disjoint_pair_to_stitch &&
2791 84 : (this->get_boundary_info().has_boundary_id(el, s, this_mesh_boundary_id)
2792 84 : || this->get_boundary_info().has_boundary_id(el, s, other_mesh_boundary_id));
2793 :
2794 4606 : if (!has_real_neighbor || has_disdjoint_neighbor)
2795 : {
2796 2072 : key_type key = el->low_order_key(s);
2797 592 : auto bounds = side_to_elem_map.equal_range(key);
2798 :
2799 2072 : if (bounds.first != bounds.second)
2800 : {
2801 : // Get the side for this element
2802 784 : el->side_ptr(my_side, s);
2803 :
2804 : // Look at all the entries with an equivalent key
2805 784 : while (bounds.first != bounds.second)
2806 : {
2807 : // Get the potential element
2808 784 : Elem * neighbor = const_cast<Elem *>(bounds.first->second.first);
2809 :
2810 : // Get the side for the neighboring element
2811 784 : const unsigned int ns = bounds.first->second.second;
2812 784 : neighbor->side_ptr(their_side, ns);
2813 : //libmesh_assert(my_side.get());
2814 : //libmesh_assert(their_side.get());
2815 :
2816 : // If found a match with my side
2817 : //
2818 : // We need special tests here for 1D:
2819 : // since parents and children have an equal
2820 : // side (i.e. a node), we need to check
2821 : // ns != ms, and we also check level() to
2822 : // avoid setting our neighbor pointer to
2823 : // any of our neighbor's descendants
2824 1344 : if ((*my_side == *their_side) &&
2825 1568 : (el->level() == neighbor->level()) &&
2826 784 : ((el->dim() != 1) || (ns != s)))
2827 : {
2828 : // So share a side. Is this a mixed pair
2829 : // of subactive and active/ancestor
2830 : // elements?
2831 : // If not, then we're neighbors.
2832 : // If so, then the subactive's neighbor is
2833 :
2834 1008 : if (el->subactive() ==
2835 784 : neighbor->subactive())
2836 : {
2837 : // an element is only subactive if it has
2838 : // been coarsened but not deleted
2839 448 : el->set_neighbor (s,neighbor);
2840 448 : neighbor->set_neighbor(ns,el);
2841 : }
2842 0 : else if (el->subactive())
2843 : {
2844 0 : el->set_neighbor(s,neighbor);
2845 : }
2846 0 : else if (neighbor->subactive())
2847 : {
2848 0 : neighbor->set_neighbor(ns,el);
2849 : }
2850 : // It's OK to invalidate the
2851 : // bounds.first iterator here,
2852 : // as we are immediately going
2853 : // to break out of this while
2854 : // loop. bounds.first will
2855 : // therefore not be used for
2856 : // anything else.
2857 224 : side_to_elem_map.erase (bounds.first);
2858 224 : break;
2859 : }
2860 :
2861 0 : ++bounds.first;
2862 : }
2863 : }
2864 : }
2865 : }
2866 : }
2867 : }
2868 : }
2869 72 : }
2870 :
2871 : #ifdef LIBMESH_ENABLE_PERIODIC
2872 : // Remove only the disjoint pair that was actually stitched.
2873 : // Safe because `is_valid_disjoint_pair_to_stitch` is true
2874 : // only if this exact (a,b) pair exists in the registry.
2875 : // Other disjoint pairs remain untouched.
2876 168 : if (is_valid_disjoint_pair_to_stitch)
2877 21 : this->remove_disjoint_boundary_pair(this_mesh_boundary_id, other_mesh_boundary_id);
2878 : #endif
2879 :
2880 168 : if (prepare_after_stitching)
2881 : {
2882 : // We set our new neighbor pointers already
2883 92 : const bool old_allow_find_neighbors = this->allow_find_neighbors();
2884 46 : this->allow_find_neighbors(!skip_find_neighbors);
2885 :
2886 : // We haven't newly remoted any elements
2887 92 : const bool old_allow_remote_element_removal = this->allow_remote_element_removal();
2888 46 : this->allow_remote_element_removal(false);
2889 :
2890 161 : this->prepare_for_use();
2891 :
2892 46 : this->allow_find_neighbors(old_allow_find_neighbors);
2893 46 : this->allow_remote_element_removal(old_allow_remote_element_removal);
2894 : }
2895 :
2896 : // After the stitching, we may want to clear boundary IDs from element
2897 : // faces that are now internal to the mesh
2898 168 : if (clear_stitched_boundary_ids)
2899 : {
2900 138 : LOG_SCOPE("stitch_meshes clear bcids", "UnstructuredMesh");
2901 :
2902 161 : this->get_boundary_info().clear_stitched_boundary_side_ids(
2903 : this_mesh_boundary_id, other_mesh_boundary_id, /*clear_nodeset_data=*/true);
2904 : }
2905 :
2906 : // Return the number of nodes which were merged.
2907 216 : return node_to_node_map.size();
2908 78 : }
2909 :
2910 :
2911 : } // namespace libMesh
|