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 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 : // library configuration
21 : #include "libmesh/libmesh_config.h"
22 :
23 : // Local includes
24 : #include "libmesh/boundary_info.h"
25 : #include "libmesh/libmesh_logging.h"
26 : #include "libmesh/elem.h"
27 : #include "libmesh/ghost_point_neighbors.h"
28 : #include "libmesh/mesh_base.h"
29 : #include "libmesh/mesh_communication.h"
30 : #include "libmesh/mesh_serializer.h"
31 : #include "libmesh/mesh_tools.h"
32 : #include "libmesh/parallel.h"
33 : #include "libmesh/parallel_algebra.h"
34 : #include "libmesh/parallel_fe_type.h"
35 : #include "libmesh/partitioner.h"
36 : #include "libmesh/point_locator_base.h"
37 : #include "libmesh/sparse_matrix.h"
38 : #include "libmesh/threads.h"
39 : #include "libmesh/enum_elem_type.h"
40 : #include "libmesh/enum_point_locator_type.h"
41 : #include "libmesh/enum_to_string.h"
42 : #include "libmesh/point_locator_nanoflann.h"
43 : #include "libmesh/elem_side_builder.h"
44 : #include "libmesh/elem_range.h"
45 : #include "libmesh/node_range.h"
46 :
47 : // C++ includes
48 : #include <algorithm> // for std::min
49 : #include <map> // for std::multimap
50 : #include <memory>
51 : #include <sstream> // for std::ostringstream
52 : #include <unordered_map>
53 :
54 : #include "libmesh/periodic_boundaries.h"
55 : #include "libmesh/periodic_boundary.h"
56 :
57 : namespace {
58 : using namespace libMesh;
59 :
60 : template <typename ConstRange, typename Range>
61 : const ConstRange &
62 7640957 : const_shim(const MeshBase & mesh,
63 : const Range & (MeshBase::*range_func)(),
64 : std::unique_ptr<ConstRange> & shim_range)
65 : {
66 7640957 : if (!shim_range)
67 : {
68 81000 : MeshBase & evil = const_cast<MeshBase &>(mesh);
69 5697901 : const Range & range = (evil.*range_func)();
70 :
71 11235512 : shim_range =
72 : std::make_unique<ConstRange>
73 11316512 : (range.begin(), range.end(), range.grainsize());
74 : }
75 :
76 7640957 : return *shim_range;
77 : }
78 :
79 :
80 144 : struct SBDInserter {
81 : std::set<subdomain_id_type> my_ids;
82 :
83 65125 : SBDInserter () {}
84 146 : SBDInserter (SBDInserter &, Threads::split) {}
85 :
86 4862357 : void operator()(const ConstElemRange & range) {
87 32902151 : for (const Elem * elem : range)
88 28039794 : my_ids.insert(elem->subdomain_id());
89 4862357 : }
90 :
91 146 : void join(SBDInserter & other) {
92 146 : my_ids.merge(other.my_ids);
93 146 : }
94 : };
95 :
96 : }
97 :
98 : namespace libMesh
99 : {
100 :
101 : // ------------------------------------------------------------
102 : // MeshBase class member functions
103 498439 : MeshBase::MeshBase (const Parallel::Communicator & comm_in,
104 498439 : unsigned char d) :
105 : ParallelObject (comm_in),
106 498439 : boundary_info (new BoundaryInfo(*this)), // BoundaryInfo has protected ctor, can't use std::make_unique
107 469265 : _n_parts (1),
108 469265 : _default_mapping_type(LAGRANGE_MAP),
109 469265 : _default_mapping_data(0),
110 469265 : _preparation (),
111 469265 : _element_stored_range (),
112 469265 : _const_element_stored_range (),
113 469265 : _active_local_element_stored_range (),
114 469265 : _const_active_local_element_stored_range (),
115 469265 : _local_element_stored_range (),
116 469265 : _const_local_element_stored_range (),
117 469265 : _node_stored_range (),
118 469265 : _const_node_stored_range (),
119 469265 : _local_node_stored_range (),
120 469265 : _const_local_node_stored_range (),
121 469265 : _unpartitioned_node_stored_range (),
122 469265 : _const_unpartitioned_node_stored_range (),
123 469265 : _point_locator (),
124 469265 : _count_lower_dim_elems_in_point_locator(true),
125 469265 : _partitioner (),
126 : #ifdef LIBMESH_ENABLE_UNIQUE_ID
127 469265 : _next_unique_id(DofObject::invalid_unique_id),
128 : #endif
129 469265 : _interior_mesh(this),
130 469265 : _skip_noncritical_partitioning(false),
131 513194 : _skip_all_partitioning(libMesh::on_command_line("--skip-partitioning")),
132 469265 : _skip_renumber_nodes_and_elements(false),
133 469265 : _skip_find_neighbors(false),
134 469265 : _skip_detect_interior_parents(false),
135 469265 : _allow_remote_element_removal(true),
136 469265 : _allow_node_and_elem_unique_id_overlap(false),
137 469265 : _is_redistributing(false),
138 469265 : _spatial_dimension(d),
139 498439 : _default_ghosting(std::make_unique<GhostPointNeighbors>(*this)),
140 469265 : _n_constraint_rows(0),
141 1568084 : _point_locator_close_to_point_tol(0.)
142 : {
143 484020 : _elem_dims.insert(d);
144 498439 : _ghosting_functors.push_back(_default_ghosting.get());
145 14755 : libmesh_assert_less_equal (LIBMESH_DIM, 3);
146 14755 : libmesh_assert_greater_equal (LIBMESH_DIM, d);
147 14755 : libmesh_assert (libMesh::initialized());
148 498439 : }
149 :
150 :
151 :
152 42155 : MeshBase::MeshBase (const MeshBase & other_mesh) :
153 : ParallelObject (other_mesh),
154 42155 : boundary_info (new BoundaryInfo(*this)), // BoundaryInfo has protected ctor, can't use std::make_unique
155 42155 : _n_parts (other_mesh._n_parts),
156 42155 : _default_mapping_type(other_mesh._default_mapping_type),
157 42155 : _default_mapping_data(other_mesh._default_mapping_data),
158 22935 : _preparation (other_mesh._preparation),
159 : // Clear cached ranges; we can't copy them and it's most efficient
160 : // to regenerate them on demand vs in advance
161 22935 : _element_stored_range (),
162 22935 : _const_element_stored_range (),
163 22935 : _active_local_element_stored_range (),
164 22935 : _const_active_local_element_stored_range (),
165 22935 : _local_element_stored_range (),
166 22935 : _const_local_element_stored_range (),
167 22935 : _node_stored_range (),
168 22935 : _const_node_stored_range (),
169 22935 : _local_node_stored_range (),
170 22935 : _const_local_node_stored_range (),
171 22935 : _unpartitioned_node_stored_range (),
172 22935 : _const_unpartitioned_node_stored_range (),
173 22935 : _point_locator (),
174 42155 : _count_lower_dim_elems_in_point_locator(other_mesh._count_lower_dim_elems_in_point_locator),
175 22935 : _partitioner (),
176 : #ifdef LIBMESH_ENABLE_UNIQUE_ID
177 42155 : _next_unique_id(other_mesh._next_unique_id),
178 : #endif
179 : // If the other mesh interior_parent pointers just go back to
180 : // itself, so should we
181 42155 : _interior_mesh((other_mesh._interior_mesh == &other_mesh) ?
182 : this : other_mesh._interior_mesh),
183 42155 : _skip_noncritical_partitioning(other_mesh._skip_noncritical_partitioning),
184 42155 : _skip_all_partitioning(other_mesh._skip_all_partitioning),
185 42155 : _skip_renumber_nodes_and_elements(other_mesh._skip_renumber_nodes_and_elements),
186 42155 : _skip_find_neighbors(other_mesh._skip_find_neighbors),
187 42155 : _skip_detect_interior_parents(other_mesh._skip_detect_interior_parents),
188 42155 : _allow_remote_element_removal(other_mesh._allow_remote_element_removal),
189 42155 : _allow_node_and_elem_unique_id_overlap(other_mesh._allow_node_and_elem_unique_id_overlap),
190 22935 : _is_redistributing(false),
191 18428 : _elem_dims(other_mesh._elem_dims),
192 18428 : _elem_default_orders(other_mesh._elem_default_orders),
193 42155 : _supported_nodal_order(other_mesh._supported_nodal_order),
194 18428 : _mesh_subdomains(other_mesh._mesh_subdomains),
195 18428 : _mesh_local_subdomains(other_mesh._mesh_local_subdomains),
196 18428 : _elemset_codes_inverse_map(other_mesh._elemset_codes_inverse_map),
197 18428 : _all_elemset_ids(other_mesh._all_elemset_ids),
198 42155 : _spatial_dimension(other_mesh._spatial_dimension),
199 42155 : _default_ghosting(std::make_unique<GhostPointNeighbors>(*this)),
200 22935 : _n_constraint_rows(0),
201 170996 : _point_locator_close_to_point_tol(other_mesh._point_locator_close_to_point_tol)
202 : {
203 18428 : const GhostingFunctor * const other_default_ghosting = other_mesh._default_ghosting.get();
204 :
205 118150 : for (GhostingFunctor * const gf : other_mesh._ghosting_functors)
206 : {
207 : // If the other mesh is using default ghosting, then we will use our own
208 : // default ghosting
209 75995 : if (gf == other_default_ghosting)
210 : {
211 42155 : _ghosting_functors.push_back(_default_ghosting.get());
212 42155 : continue;
213 : }
214 :
215 66672 : std::shared_ptr<GhostingFunctor> clone_gf = gf->clone();
216 : // Some subclasses of GhostingFunctor might not override the
217 : // clone function yet. If this is the case, GhostingFunctor will
218 : // return nullptr by default. The clone function should be overridden
219 : // in all derived classes. This following code ("else") is written
220 : // for API upgrade. That will allow users gradually to update their code.
221 : // Once the API upgrade is done, we will come back and delete "else."
222 33840 : if (clone_gf)
223 : {
224 33840 : clone_gf->set_mesh(this);
225 63236 : add_ghosting_functor(clone_gf);
226 : }
227 : else
228 : {
229 : libmesh_deprecated();
230 0 : add_ghosting_functor(*gf);
231 : }
232 : }
233 :
234 42155 : if (other_mesh._partitioner.get())
235 65740 : _partitioner = other_mesh._partitioner->clone();
236 :
237 : #ifdef LIBMESH_ENABLE_PERIODIC
238 : // Deep copy of all periodic boundaries
239 42155 : if (other_mesh._disjoint_neighbor_boundary_pairs)
240 : {
241 164 : _disjoint_neighbor_boundary_pairs = std::make_unique<PeriodicBoundaries>();
242 :
243 305 : for (const auto & [id, pb] : *other_mesh._disjoint_neighbor_boundary_pairs)
244 206 : if (pb)
245 344 : (*_disjoint_neighbor_boundary_pairs)[id] = pb->clone();
246 : }
247 : #endif
248 :
249 : // _elemset_codes stores pointers to entries in _elemset_codes_inverse_map,
250 : // so it is not possible to simply copy it directly from other_mesh
251 42167 : for (const auto & [set, code] : _elemset_codes_inverse_map)
252 12 : _elemset_codes.emplace(code, &set);
253 42155 : }
254 :
255 426 : MeshBase& MeshBase::operator= (MeshBase && other_mesh)
256 : {
257 24 : LOG_SCOPE("operator=(&&)", "MeshBase");
258 :
259 : // Move assign as a ParallelObject.
260 12 : this->ParallelObject::operator=(other_mesh);
261 :
262 : // We can safely clear stored ranges, rebuilding them on-demand
263 : // whenever they're requested later.
264 : //
265 : // We *might* be able to safely move stored ranges from other_mesh,
266 : // but I'm less confident about that and I don't want to try it
267 : // without more test coverage.
268 426 : this->clear_stored_ranges();
269 :
270 426 : _n_parts = other_mesh.n_partitions();
271 426 : _default_mapping_type = other_mesh.default_mapping_type();
272 426 : _default_mapping_data = other_mesh.default_mapping_data();
273 426 : _preparation = other_mesh._preparation;
274 12 : _point_locator = std::move(other_mesh._point_locator);
275 426 : _count_lower_dim_elems_in_point_locator = other_mesh.get_count_lower_dim_elems_in_point_locator();
276 : #ifdef LIBMESH_ENABLE_UNIQUE_ID
277 426 : _next_unique_id = other_mesh.next_unique_id();
278 : #endif
279 : // If the other mesh interior_parent pointers just go back to
280 : // itself, so should we
281 426 : _interior_mesh = (other_mesh._interior_mesh == &other_mesh) ?
282 : this : other_mesh._interior_mesh;
283 426 : _skip_noncritical_partitioning = other_mesh.skip_noncritical_partitioning();
284 426 : _skip_all_partitioning = other_mesh.skip_partitioning();
285 426 : _skip_renumber_nodes_and_elements = !(other_mesh.allow_renumbering());
286 426 : _skip_find_neighbors = !(other_mesh.allow_find_neighbors());
287 426 : _skip_detect_interior_parents = !(other_mesh.allow_detect_interior_parents());
288 426 : _allow_remote_element_removal = other_mesh.allow_remote_element_removal();
289 426 : _allow_node_and_elem_unique_id_overlap = other_mesh.allow_node_and_elem_unique_id_overlap();
290 12 : _block_id_to_name = std::move(other_mesh._block_id_to_name);
291 12 : _elem_dims = std::move(other_mesh.elem_dimensions());
292 12 : _elem_default_orders = std::move(other_mesh.elem_default_orders());
293 426 : _supported_nodal_order = other_mesh.supported_nodal_order();
294 12 : _mesh_subdomains = other_mesh._mesh_subdomains;
295 12 : _mesh_local_subdomains = other_mesh._mesh_local_subdomains;
296 12 : _elemset_codes = std::move(other_mesh._elemset_codes);
297 12 : _elemset_codes_inverse_map = std::move(other_mesh._elemset_codes_inverse_map);
298 12 : _all_elemset_ids = std::move(other_mesh._all_elemset_ids);
299 426 : _spatial_dimension = other_mesh.spatial_dimension();
300 426 : _elem_integer_names = std::move(other_mesh._elem_integer_names);
301 426 : _elem_integer_default_values = std::move(other_mesh._elem_integer_default_values);
302 426 : _node_integer_names = std::move(other_mesh._node_integer_names);
303 426 : _node_integer_default_values = std::move(other_mesh._node_integer_default_values);
304 426 : _n_constraint_rows = other_mesh._n_constraint_rows;
305 426 : _point_locator_close_to_point_tol = other_mesh.get_point_locator_close_to_point_tol();
306 :
307 : #ifdef LIBMESH_ENABLE_PERIODIC
308 : // Deep copy of all periodic boundaries:
309 : // We must clone each PeriodicBoundaryBase in the source map,
310 : // since unique_ptr cannot be copied and we need independent instances
311 426 : if (other_mesh._disjoint_neighbor_boundary_pairs)
312 : {
313 0 : _disjoint_neighbor_boundary_pairs = std::make_unique<PeriodicBoundaries>();
314 :
315 0 : for (const auto & [id, pb] : *other_mesh._disjoint_neighbor_boundary_pairs)
316 0 : if (pb)
317 0 : (*_disjoint_neighbor_boundary_pairs)[id] = pb->clone();
318 : }
319 : #endif
320 :
321 : // This relies on our subclasses *not* invalidating pointers when we
322 : // do their portion of the move assignment later!
323 12 : boundary_info = std::move(other_mesh.boundary_info);
324 12 : boundary_info->set_mesh(*this);
325 :
326 : #ifdef DEBUG
327 : // Make sure that move assignment worked for pointers
328 12 : for (const auto & [set, code] : _elemset_codes_inverse_map)
329 : {
330 0 : auto it = _elemset_codes.find(code);
331 0 : libmesh_assert_msg(it != _elemset_codes.end(),
332 : "Elemset code " << code << " not found in _elmset_codes container.");
333 0 : libmesh_assert_equal_to(it->second, &set);
334 : }
335 : #endif
336 :
337 : // We're *not* really done at this point, but we have the problem
338 : // that some of our data movement might be expecting subclasses data
339 : // movement to happen first. We'll let subclasses handle that by
340 : // calling our post_dofobject_moves()
341 438 : return *this;
342 : }
343 :
344 :
345 32753 : bool MeshBase::operator== (const MeshBase & other_mesh) const
346 : {
347 18664 : LOG_SCOPE("operator==()", "MeshBase");
348 :
349 32753 : bool is_equal = this->locally_equals(other_mesh);
350 32753 : this->comm().min(is_equal);
351 51417 : return is_equal;
352 : }
353 :
354 :
355 17636 : void MeshBase::assert_equal_to (const MeshBase & other_mesh,
356 : std::string_view failure_context) const
357 : {
358 : #ifndef NDEBUG
359 35272 : LOG_SCOPE("assert_equal_to()", "MeshBase");
360 :
361 17636 : std::string_view local_diff = first_difference_from(other_mesh);
362 :
363 17636 : bool diff_found = !local_diff.empty();
364 17636 : this->comm().max(diff_found);
365 :
366 17636 : if (diff_found)
367 : {
368 : // Construct a user-friendly message to throw on pid 0
369 0 : std::set<std::string> unique_diffs;
370 0 : if (!local_diff.empty())
371 0 : unique_diffs.insert(std::string(local_diff));
372 0 : this->comm().set_union(unique_diffs);
373 :
374 0 : if (!this->processor_id())
375 : {
376 0 : std::string error_msg {failure_context};
377 0 : error_msg += "\nMeshes failed asserted equality in at least these aspects:\n";
378 0 : for (auto & diff : unique_diffs)
379 : {
380 0 : error_msg += diff;
381 0 : error_msg += '\n';
382 : }
383 0 : libmesh_assert_msg(!diff_found, error_msg);
384 : }
385 :
386 : // We're not going to throw on other processors because we don't
387 : // want to accidentally preempt pid 0's error message. We're
388 : // not even going to exit on other processors because for all we
389 : // know user code is going to catch that error and sync up with
390 : // us later.
391 : }
392 : #else
393 : libmesh_ignore(other_mesh, failure_context);
394 : #endif // NDEBUG
395 17636 : }
396 :
397 :
398 32753 : bool MeshBase::locally_equals (const MeshBase & other_mesh) const
399 : {
400 32753 : const std::string_view diff = first_difference_from(other_mesh);
401 51825 : return diff.empty();
402 : }
403 :
404 :
405 50389 : std::string_view MeshBase::first_difference_from(const MeshBase & other_mesh) const
406 : {
407 : // Check whether (almost) everything in the base is equal
408 : //
409 : // We don't check _next_unique_id here, because it's expected to
410 : // change in a DistributedMesh prepare_for_use(); it's conceptually
411 : // "mutable".
412 : //
413 : // We use separate tests here and return strings for each test,
414 : // to make it easy to see the failing condition a
415 : // MeshTools::libmesh_valid_is_prepared(mesh) is failing.
416 :
417 : #define CHECK_MEMBER(member_name) \
418 : if (member_name != other_mesh.member_name) \
419 : return #member_name;
420 :
421 50389 : CHECK_MEMBER(_n_parts);
422 50389 : CHECK_MEMBER(_default_mapping_type);
423 50389 : CHECK_MEMBER(_default_mapping_data);
424 50389 : CHECK_MEMBER(_preparation);
425 50389 : CHECK_MEMBER(_count_lower_dim_elems_in_point_locator);
426 :
427 : // We should either both have our own interior parents or both not;
428 : // but if we both don't then we can't really assert anything else
429 : // because pointing at the same interior mesh is fair but so is
430 : // pointing at two different copies of "the same" interior mesh.
431 87097 : if ((_interior_mesh == this) !=
432 50389 : (other_mesh._interior_mesh == &other_mesh))
433 0 : return "_interior_mesh";
434 :
435 50389 : CHECK_MEMBER(_skip_noncritical_partitioning);
436 50389 : CHECK_MEMBER(_skip_all_partitioning);
437 50389 : CHECK_MEMBER(_skip_renumber_nodes_and_elements);
438 50389 : CHECK_MEMBER(_skip_find_neighbors);
439 50389 : CHECK_MEMBER(_skip_detect_interior_parents);
440 50389 : CHECK_MEMBER(_allow_remote_element_removal);
441 50389 : CHECK_MEMBER(_allow_node_and_elem_unique_id_overlap);
442 50389 : CHECK_MEMBER(_spatial_dimension);
443 50389 : CHECK_MEMBER(_n_constraint_rows);
444 50389 : CHECK_MEMBER(_point_locator_close_to_point_tol);
445 50389 : CHECK_MEMBER(_block_id_to_name);
446 50389 : CHECK_MEMBER(_elem_dims);
447 50389 : CHECK_MEMBER(_elem_default_orders);
448 50389 : CHECK_MEMBER(_supported_nodal_order);
449 50395 : CHECK_MEMBER(_mesh_subdomains);
450 50179 : CHECK_MEMBER(_mesh_local_subdomains);
451 50128 : CHECK_MEMBER(_all_elemset_ids);
452 50128 : CHECK_MEMBER(_elem_integer_names);
453 50527 : CHECK_MEMBER(_elem_integer_default_values);
454 50128 : CHECK_MEMBER(_node_integer_names);
455 50527 : CHECK_MEMBER(_node_integer_default_values);
456 :
457 50128 : if (static_cast<bool>(_default_ghosting) != static_cast<bool>(other_mesh._default_ghosting))
458 0 : return "_default_ghosting";
459 50128 : if (static_cast<bool>(_partitioner) != static_cast<bool>(other_mesh._partitioner))
460 0 : return "_partitioner";
461 50128 : if (*boundary_info != *other_mesh.boundary_info)
462 165 : return "boundary_info";
463 :
464 : // First check whether the "existence" of the two pointers differs (one present, one absent)
465 86251 : if (static_cast<bool>(_disjoint_neighbor_boundary_pairs) !=
466 36288 : static_cast<bool>(other_mesh._disjoint_neighbor_boundary_pairs))
467 0 : return "_disjoint_neighbor_boundary_pairs existence";
468 : // If both exist, compare the contents (Weak Test: just compare sizes like `_ghosting_functors`)
469 50023 : if (_disjoint_neighbor_boundary_pairs &&
470 36348 : (_disjoint_neighbor_boundary_pairs->size() != other_mesh._disjoint_neighbor_boundary_pairs->size()))
471 0 : return "_disjoint_neighbor_boundary_pairs size";
472 :
473 : const constraint_rows_type & other_rows =
474 36288 : other_mesh.get_constraint_rows();
475 125983 : for (const auto & [node, row] : this->_constraint_rows)
476 : {
477 76020 : const dof_id_type node_id = node->id();
478 76020 : const Node * other_node = other_mesh.query_node_ptr(node_id);
479 76020 : if (!other_node)
480 0 : return "_constraint_rows node presence";
481 :
482 30800 : auto it = other_rows.find(other_node);
483 76020 : if (it == other_rows.end())
484 0 : return "_constraint_rows row presence";
485 :
486 30800 : const auto & other_row = it->second;
487 82436 : if (row.size() != other_row.size())
488 0 : return "_constraint_rows row size";
489 :
490 312221 : for (auto i : index_range(row))
491 : {
492 74040 : const auto & [elem_pair, coef] = row[i];
493 74040 : const auto & [other_elem_pair, other_coef] = other_row[i];
494 74040 : libmesh_assert(elem_pair.first);
495 74040 : libmesh_assert(other_elem_pair.first);
496 236201 : if (elem_pair.first->id() !=
497 398362 : other_elem_pair.first->id() ||
498 236201 : elem_pair.second !=
499 310241 : other_elem_pair.second ||
500 236201 : coef != other_coef)
501 0 : return "_constraint_rows row entry";
502 : }
503 : }
504 :
505 49987 : for (const auto & [elemset_code, elemset_ptr] : this->_elemset_codes)
506 24 : if (const auto it = other_mesh._elemset_codes.find(elemset_code);
507 24 : it == other_mesh._elemset_codes.end() || *elemset_ptr != *it->second)
508 0 : return "_elemset_codes";
509 :
510 : // FIXME: we have no good way to compare ghosting functors, since
511 : // they're in a vector of pointers, and we have no way *at all*
512 : // to compare ghosting functors, since they don't have operator==
513 : // defined and we encourage users to subclass them. We can check if
514 : // we have the same number, is all.
515 86647 : if (_ghosting_functors.size() !=
516 36684 : other_mesh._ghosting_functors.size())
517 0 : return "_ghosting_functors size";
518 :
519 : // Same deal for partitioners. We tested that we both have one or
520 : // both don't, but are they equivalent? Let's guess "yes".
521 :
522 : // Now let the subclasses decide whether everything else is equal
523 49963 : return this->subclass_first_difference_from(other_mesh);
524 : }
525 :
526 :
527 555805 : MeshBase::~MeshBase()
528 : {
529 540594 : this->MeshBase::clear();
530 :
531 33183 : libmesh_exceptionless_assert (!libMesh::closed());
532 2017194 : }
533 :
534 :
535 :
536 11991406 : unsigned int MeshBase::mesh_dimension() const
537 : {
538 11991406 : if (!_elem_dims.empty())
539 11752135 : return cast_int<unsigned int>(*_elem_dims.rbegin());
540 14 : return 0;
541 : }
542 :
543 :
544 :
545 0 : void MeshBase::set_elem_dimensions(std::set<unsigned char> elem_dims)
546 : {
547 : #ifdef DEBUG
548 : // In debug mode, we call cache_elem_data() and then make sure
549 : // the result actually agrees with what the user specified.
550 0 : parallel_object_only();
551 :
552 0 : this->cache_elem_data();
553 0 : libmesh_assert_msg(_elem_dims == elem_dims, \
554 : "Specified element dimensions does not match true element dimensions!");
555 : #endif
556 :
557 0 : _elem_dims = std::move(elem_dims);
558 0 : }
559 :
560 :
561 :
562 1775 : void MeshBase::add_elemset_code(dof_id_type code, MeshBase::elemset_type id_set)
563 : {
564 : // Populate inverse map, stealing id_set's resources
565 1725 : auto [it1, inserted1] = _elemset_codes_inverse_map.emplace(std::move(id_set), code);
566 :
567 : // Reference to the newly inserted (or previously existing) id_set
568 1775 : const auto & inserted_id_set = it1->first;
569 :
570 : // Keep track of all elemset ids ever added for O(1) n_elemsets()
571 : // performance. Only need to do this if we didn't know about this
572 : // id_set before...
573 1775 : if (inserted1)
574 1775 : _all_elemset_ids.insert(inserted_id_set.begin(), inserted_id_set.end());
575 :
576 : // Take the address of the newly emplaced set to use in
577 : // _elemset_codes, avoid duplicating std::set storage
578 1775 : auto [it2, inserted2] = _elemset_codes.emplace(code, &inserted_id_set);
579 :
580 : // Throw an error if this code already exists with a pointer to a
581 : // different set of ids.
582 1775 : libmesh_error_msg_if(!inserted2 && it2->second != &inserted_id_set,
583 : "The elemset code " << code << " already exists with a different id_set.");
584 1775 : }
585 :
586 :
587 :
588 15152 : unsigned int MeshBase::n_elemsets() const
589 : {
590 15152 : return _all_elemset_ids.size();
591 : }
592 :
593 2963 : void MeshBase::get_elemsets(dof_id_type elemset_code, MeshBase::elemset_type & id_set_to_fill) const
594 : {
595 : // If we don't recognize this elemset_code, hand back an empty set
596 102 : id_set_to_fill.clear();
597 :
598 2963 : if (const auto it = _elemset_codes.find(elemset_code);
599 102 : it != _elemset_codes.end())
600 1386 : id_set_to_fill.insert(it->second->begin(), it->second->end());
601 2963 : }
602 :
603 852 : dof_id_type MeshBase::get_elemset_code(const MeshBase::elemset_type & id_set) const
604 : {
605 24 : auto it = _elemset_codes_inverse_map.find(id_set);
606 852 : return (it == _elemset_codes_inverse_map.end()) ? DofObject::invalid_id : it->second;
607 : }
608 :
609 8453 : std::vector<dof_id_type> MeshBase::get_elemset_codes() const
610 : {
611 240 : std::vector<dof_id_type> ret;
612 8453 : ret.reserve(_elemset_codes.size());
613 9234 : for (const auto & pr : _elemset_codes)
614 781 : ret.push_back(pr.first);
615 8453 : return ret;
616 : }
617 :
618 284 : void MeshBase::change_elemset_code(dof_id_type old_code, dof_id_type new_code)
619 : {
620 : // Look up elemset ids for old_code
621 8 : auto it = _elemset_codes.find(old_code);
622 :
623 : // If we don't have the old_code, then do nothing. Alternatively, we
624 : // could throw an error since trying to change an elemset code you
625 : // don't have could indicate there's a problem...
626 284 : if (it == _elemset_codes.end())
627 0 : return;
628 :
629 : // Make copy of the set of elemset ids. We are not changing these,
630 : // only updating the elemset code it corresponds to.
631 284 : elemset_type id_set_copy = *(it->second);
632 :
633 : // Look up the corresponding entry in the inverse map. Note: we want
634 : // the iterator because we are going to remove it.
635 8 : auto inverse_it = _elemset_codes_inverse_map.find(id_set_copy);
636 284 : libmesh_error_msg_if(inverse_it == _elemset_codes_inverse_map.end(),
637 : "Expected _elemset_codes_inverse_map entry for elemset code " << old_code);
638 :
639 : // Erase entry from inverse map
640 276 : _elemset_codes_inverse_map.erase(inverse_it);
641 :
642 : // Erase entry from forward map
643 276 : _elemset_codes.erase(it);
644 :
645 : // Add new code with original set of ids.
646 560 : this->add_elemset_code(new_code, id_set_copy);
647 :
648 : // We can't update any actual elemset codes if there is no extra integer defined for it.
649 284 : if (!this->has_elem_integer("elemset_code"))
650 0 : return;
651 :
652 : // Get index of elemset_code extra integer
653 284 : unsigned int elemset_index = this->get_elem_integer_index("elemset_code");
654 :
655 : // Loop over all elems and update code
656 : Threads::parallel_for_each
657 284 : (this->element_stored_range(),
658 1092 : [elemset_index, old_code, new_code](Elem * elem)
659 : {
660 : dof_id_type elemset_code =
661 4970 : elem->get_extra_integer(elemset_index);
662 :
663 4970 : if (elemset_code == old_code)
664 2415 : elem->set_extra_integer(elemset_index, new_code);
665 4970 : });
666 : }
667 :
668 284 : void MeshBase::change_elemset_id(elemset_id_type old_id, elemset_id_type new_id)
669 : {
670 : // Early return if we don't have old_id
671 8 : if (!_all_elemset_ids.count(old_id))
672 0 : return;
673 :
674 : // Throw an error if the new_id is already used
675 8 : libmesh_error_msg_if(_all_elemset_ids.count(new_id),
676 : "Cannot change elemset id " << old_id <<
677 : " to " << new_id << ", " << new_id << " already exists.");
678 :
679 : // We will build up a new version of the inverse map so we can iterate over
680 : // the current one without invalidating anything.
681 16 : std::map<MeshBase::elemset_type, dof_id_type> new_elemset_codes_inverse_map;
682 852 : for (const auto & [id_set, elemset_code] : _elemset_codes_inverse_map)
683 : {
684 32 : auto id_set_copy = id_set;
685 16 : if (id_set_copy.count(old_id))
686 : {
687 : // Remove old_id, insert new_id
688 8 : id_set_copy.erase(old_id);
689 276 : id_set_copy.insert(new_id);
690 : }
691 :
692 : // Store in new version of map
693 552 : new_elemset_codes_inverse_map.emplace(id_set_copy, elemset_code);
694 : }
695 :
696 : // Swap existing map with newly-built one
697 8 : _elemset_codes_inverse_map.swap(new_elemset_codes_inverse_map);
698 :
699 : // Reconstruct _elemset_codes map
700 8 : _elemset_codes.clear();
701 852 : for (const auto & [id_set, elemset_code] : _elemset_codes_inverse_map)
702 568 : _elemset_codes.emplace(elemset_code, &id_set);
703 :
704 : // Update _all_elemset_ids
705 8 : _all_elemset_ids.erase(old_id);
706 276 : _all_elemset_ids.insert(new_id);
707 : }
708 :
709 :
710 :
711 431729 : void MeshBase::set_spatial_dimension(unsigned char d)
712 : {
713 : // The user can set the _spatial_dimension however they wish,
714 : // libMesh will only *increase* the spatial dimension, however,
715 : // never decrease it.
716 431729 : _spatial_dimension = d;
717 431729 : }
718 :
719 :
720 :
721 4716 : unsigned int MeshBase::add_elem_integer(std::string name,
722 : bool allocate_data,
723 : dof_id_type default_value)
724 : {
725 5912 : for (auto i : index_range(_elem_integer_names))
726 1416 : if (_elem_integer_names[i] == name)
727 : {
728 8 : libmesh_assert_less(i, _elem_integer_default_values.size());
729 284 : _elem_integer_default_values[i] = default_value;
730 284 : return i;
731 : }
732 :
733 136 : libmesh_assert_equal_to(_elem_integer_names.size(),
734 : _elem_integer_default_values.size());
735 4432 : _elem_integer_names.push_back(std::move(name));
736 4432 : _elem_integer_default_values.push_back(default_value);
737 4432 : if (allocate_data)
738 3844 : this->size_elem_extra_integers();
739 4568 : return _elem_integer_names.size()-1;
740 : }
741 :
742 :
743 :
744 56102 : std::vector<unsigned int> MeshBase::add_elem_integers(const std::vector<std::string> & names,
745 : bool allocate_data,
746 : const std::vector<dof_id_type> * default_values)
747 : {
748 18836 : libmesh_assert(!default_values || default_values->size() == names.size());
749 18836 : libmesh_assert_equal_to(_elem_integer_names.size(), _elem_integer_default_values.size());
750 :
751 37672 : std::unordered_map<std::string, std::size_t> name_indices;
752 56954 : for (auto i : index_range(_elem_integer_names))
753 852 : name_indices[_elem_integer_names[i]] = i;
754 :
755 57302 : std::vector<unsigned int> returnval(names.size());
756 :
757 18836 : bool added_an_integer = false;
758 58807 : for (auto i : index_range(names))
759 : {
760 356 : const std::string & name = names[i];
761 2705 : if (const auto it = name_indices.find(name);
762 278 : it != name_indices.end())
763 : {
764 426 : returnval[i] = it->second;
765 438 : _elem_integer_default_values[it->second] =
766 426 : default_values ? (*default_values)[i] : DofObject::invalid_id;
767 : }
768 : else
769 : {
770 2345 : returnval[i] = _elem_integer_names.size();
771 2279 : name_indices[name] = returnval[i];
772 2279 : _elem_integer_names.push_back(name);
773 : _elem_integer_default_values.push_back
774 3083 : (default_values ? (*default_values)[i] : DofObject::invalid_id);
775 266 : added_an_integer = true;
776 : }
777 : }
778 :
779 56102 : if (allocate_data && added_an_integer)
780 1201 : this->size_elem_extra_integers();
781 :
782 74938 : return returnval;
783 : }
784 :
785 :
786 :
787 3278 : unsigned int MeshBase::get_elem_integer_index(std::string_view name) const
788 : {
789 4130 : for (auto i : index_range(_elem_integer_names))
790 4037 : if (_elem_integer_names[i] == name)
791 3278 : return i;
792 :
793 0 : libmesh_error_msg("Unknown elem integer " << name);
794 : return libMesh::invalid_uint;
795 : }
796 :
797 :
798 :
799 8969 : bool MeshBase::has_elem_integer(std::string_view name) const
800 : {
801 9963 : for (auto & entry : _elem_integer_names)
802 2367 : if (entry == name)
803 40 : return true;
804 :
805 660 : return false;
806 : }
807 :
808 :
809 :
810 21620 : unsigned int MeshBase::add_node_integer(std::string name,
811 : bool allocate_data,
812 : dof_id_type default_value)
813 : {
814 34625 : for (auto i : index_range(_node_integer_names))
815 12608 : if (_node_integer_names[i] == name)
816 : {
817 16 : libmesh_assert_less(i, _node_integer_default_values.size());
818 568 : _node_integer_default_values[i] = default_value;
819 568 : return i;
820 : }
821 :
822 894 : libmesh_assert_equal_to(_node_integer_names.size(),
823 : _node_integer_default_values.size());
824 21052 : _node_integer_names.push_back(std::move(name));
825 21052 : _node_integer_default_values.push_back(default_value);
826 21052 : if (allocate_data)
827 9392 : this->size_node_extra_integers();
828 21946 : return _node_integer_names.size()-1;
829 : }
830 :
831 :
832 :
833 55960 : std::vector<unsigned int> MeshBase::add_node_integers(const std::vector<std::string> & names,
834 : bool allocate_data,
835 : const std::vector<dof_id_type> * default_values)
836 : {
837 18832 : libmesh_assert(!default_values || default_values->size() == names.size());
838 18832 : libmesh_assert_equal_to(_node_integer_names.size(), _node_integer_default_values.size());
839 :
840 37664 : std::unordered_map<std::string, std::size_t> name_indices;
841 56528 : for (auto i : index_range(_node_integer_names))
842 568 : name_indices[_node_integer_names[i]] = i;
843 :
844 57156 : std::vector<unsigned int> returnval(names.size());
845 :
846 18832 : bool added_an_integer = false;
847 59476 : for (auto i : index_range(names))
848 : {
849 1076 : const std::string & name = names[i];
850 3516 : if (const auto it = name_indices.find(name);
851 980 : it != name_indices.end())
852 : {
853 0 : returnval[i] = it->second;
854 0 : _node_integer_default_values[it->second] =
855 0 : default_values ? (*default_values)[i] : DofObject::invalid_id;
856 : }
857 : else
858 : {
859 3612 : returnval[i] = _node_integer_names.size();
860 3516 : name_indices[name] = returnval[i];
861 3516 : _node_integer_names.push_back(name);
862 : _node_integer_default_values.push_back
863 4608 : (default_values ? (*default_values)[i] : DofObject::invalid_id);
864 980 : added_an_integer = true;
865 : }
866 : }
867 :
868 55960 : if (allocate_data && added_an_integer)
869 1282 : this->size_node_extra_integers();
870 :
871 74792 : return returnval;
872 : }
873 :
874 :
875 :
876 710 : unsigned int MeshBase::get_node_integer_index(std::string_view name) const
877 : {
878 1988 : for (auto i : index_range(_node_integer_names))
879 1968 : if (_node_integer_names[i] == name)
880 710 : return i;
881 :
882 0 : libmesh_error_msg("Unknown node integer " << name);
883 : return libMesh::invalid_uint;
884 : }
885 :
886 :
887 :
888 1136 : bool MeshBase::has_node_integer(std::string_view name) const
889 : {
890 3134 : for (auto & entry : _node_integer_names)
891 3134 : if (entry == name)
892 32 : return true;
893 :
894 0 : return false;
895 : }
896 :
897 :
898 :
899 66970 : void MeshBase::remove_orphaned_nodes ()
900 : {
901 40357 : LOG_SCOPE("remove_orphaned_nodes()", "MeshBase");
902 :
903 : // Will hold the set of nodes that are currently connected to elements
904 19443 : std::unordered_set<Node *> connected_nodes;
905 :
906 : // Loop over the elements. Find which nodes are connected to at
907 : // least one of them.
908 5468893 : for (const auto & element : this->element_ptr_range())
909 31029228 : for (auto & n : element->node_ref_range())
910 27422327 : connected_nodes.insert(&n);
911 :
912 14035269 : for (const auto & node : this->node_ptr_range())
913 3414618 : if (!connected_nodes.count(node))
914 129080 : this->delete_node(node);
915 :
916 66970 : _preparation.has_removed_orphaned_nodes = true;
917 66970 : }
918 :
919 :
920 :
921 : #ifdef LIBMESH_ENABLE_DEPRECATED
922 0 : void MeshBase::prepare_for_use (const bool skip_renumber_nodes_and_elements, const bool skip_find_neighbors)
923 : {
924 : libmesh_deprecated();
925 :
926 : // We only respect the users wish if they tell us to skip renumbering. If they tell us not to
927 : // skip renumbering but someone previously called allow_renumbering(false), then the latter takes
928 : // precedence
929 0 : if (skip_renumber_nodes_and_elements)
930 0 : this->allow_renumbering(false);
931 :
932 : // We always accept the user's value for skip_find_neighbors, in contrast to skip_renumber
933 0 : const bool old_allow_find_neighbors = this->allow_find_neighbors();
934 0 : this->allow_find_neighbors(!skip_find_neighbors);
935 :
936 0 : this->prepare_for_use();
937 :
938 0 : this->allow_find_neighbors(old_allow_find_neighbors);
939 0 : }
940 :
941 0 : void MeshBase::prepare_for_use (const bool skip_renumber_nodes_and_elements)
942 : {
943 : libmesh_deprecated();
944 :
945 : // We only respect the users wish if they tell us to skip renumbering. If they tell us not to
946 : // skip renumbering but someone previously called allow_renumbering(false), then the latter takes
947 : // precedence
948 0 : if (skip_renumber_nodes_and_elements)
949 0 : this->allow_renumbering(false);
950 :
951 0 : this->prepare_for_use();
952 0 : }
953 : #endif // LIBMESH_ENABLE_DEPRECATED
954 :
955 :
956 :
957 :
958 634900 : void MeshBase::prepare_for_use ()
959 : {
960 : // Mark everything as unprepared, except for those things we've been
961 : // told we don't need to prepare, for backwards compatibility
962 634900 : this->clear_point_locator();
963 634900 : this->clear_stored_ranges();
964 634900 : _preparation = false;
965 634900 : _preparation.has_neighbor_ptrs = _skip_find_neighbors;
966 634900 : _preparation.has_removed_remote_elements = !_allow_remote_element_removal;
967 :
968 634900 : this->complete_preparation();
969 634900 : }
970 :
971 :
972 962586 : void MeshBase::complete_preparation()
973 : {
974 118234 : LOG_SCOPE("complete_preparation()", "MeshBase");
975 :
976 45402 : parallel_object_only();
977 :
978 45402 : libmesh_assert(this->comm().verify(this->is_serial()));
979 :
980 962586 : _preparation.libmesh_assert_consistent(this->comm());
981 :
982 : #ifdef DEBUG
983 : // If we don't go into this method with valid constraint rows, we're
984 : // only going to be able to make that worse.
985 45402 : MeshTools::libmesh_assert_valid_constraint_rows(*this);
986 :
987 : // If this mesh thinks it's already partially prepared, then in
988 : // optimized builds we'll trust it, but in debug builds we'll check.
989 45402 : const bool was_partly_prepared = (_preparation == Preparation());
990 : #endif
991 :
992 : // A distributed mesh may have processors with no elements (or
993 : // processors with no elements of higher dimension, if we ever
994 : // support mixed-dimension meshes), but we want consistent
995 : // mesh_dimension anyways.
996 : //
997 : // cache_elem_data() should get the elem_dimensions() and
998 : // mesh_dimension() correct later, and we don't need it earlier.
999 :
1000 :
1001 : // Renumber the nodes and elements so that they in contiguous
1002 : // blocks. By default, _skip_renumber_nodes_and_elements is false.
1003 : //
1004 : // Instances where you if prepare_for_use() should not renumber the nodes
1005 : // and elements include reading in e.g. an xda/r or gmv file. In
1006 : // this case, the ordering of the nodes may depend on an accompanying
1007 : // solution, and the node ordering cannot be changed.
1008 :
1009 :
1010 : // Mesh modification operations might not leave us with consistent
1011 : // id counts, or might leave us with orphaned nodes we're no longer
1012 : // using, but our partitioner might need that consistency and/or
1013 : // might be confused by orphaned nodes.
1014 962586 : if (!_skip_renumber_nodes_and_elements)
1015 : {
1016 893060 : if (!_preparation.has_removed_orphaned_nodes ||
1017 324917 : !_preparation.has_synched_id_counts)
1018 568143 : this->renumber_nodes_and_elements();
1019 : }
1020 : else
1021 : {
1022 69526 : if (!_preparation.has_removed_orphaned_nodes)
1023 66970 : this->remove_orphaned_nodes();
1024 69526 : if (!_preparation.has_synched_id_counts)
1025 66970 : this->update_parallel_id_counts();
1026 : }
1027 :
1028 : // Let all the elements find their neighbors
1029 962586 : if (!_skip_find_neighbors && !_preparation.has_neighbor_ptrs)
1030 622007 : this->find_neighbors();
1031 :
1032 : // The user may have set boundary conditions. We require that the
1033 : // boundary conditions were set consistently. Because we examine
1034 : // neighbors when evaluating non-raw boundary condition IDs, this
1035 : // assert is only valid when our neighbor links are in place.
1036 : #ifdef DEBUG
1037 45402 : MeshTools::libmesh_assert_valid_boundary_ids(*this);
1038 : #endif
1039 :
1040 : // Search the mesh for all the dimensions of the elements
1041 : // and cache them.
1042 962586 : if (!_preparation.has_cached_elem_data)
1043 947605 : this->cache_elem_data();
1044 :
1045 : // libMesh expects every processor to know about every subdomain
1046 : // name, but distributed mesh generators may only have set names for
1047 : // the part of the mesh they know about, so make sure the map is
1048 : // consistent everywhere.
1049 962586 : if (!_preparation.has_synched_subdomain_name_map)
1050 634900 : this->sync_subdomain_name_map();
1051 :
1052 : // Search the mesh for elements that have a neighboring element
1053 : // of dim+1 and set that element as the interior parent
1054 962586 : if (!_preparation.has_interior_parent_ptrs)
1055 : {
1056 634900 : if (!_skip_detect_interior_parents)
1057 634900 : this->detect_interior_parents();
1058 : else
1059 : {
1060 : // We must set the flag that says "interior parent pointers have been set up"
1061 : // even though we skip detect_interior_parents().
1062 0 : _preparation.has_interior_parent_ptrs = true;
1063 : }
1064 : }
1065 :
1066 : // Fix up node unique ids in case mesh generation code didn't take
1067 : // exceptional care to do so.
1068 : // MeshCommunication().make_node_unique_ids_parallel_consistent(*this);
1069 :
1070 : // We're going to still require that mesh generation code gets
1071 : // element unique ids consistent.
1072 : #if defined(DEBUG) && defined(LIBMESH_ENABLE_UNIQUE_ID)
1073 45402 : MeshTools::libmesh_assert_valid_unique_ids(*this);
1074 : #endif
1075 :
1076 : // Allow our GhostingFunctor objects to reinit if necessary.
1077 : // Do this before partitioning and redistributing, and before
1078 : // deleting remote elements.
1079 962586 : if (!_preparation.has_reinit_ghosting_functors)
1080 635113 : this->reinit_ghosting_functors();
1081 :
1082 : // Partition the mesh unless *all* partitioning is to be skipped.
1083 : // If only noncritical partitioning is to be skipped, the
1084 : // partition() call will still check for orphaned nodes.
1085 962586 : if (!skip_partitioning() && !_preparation.is_partitioned)
1086 18460 : this->partition();
1087 697312 : else if (!this->n_unpartitioned_elem() &&
1088 26942 : !this->n_unpartitioned_nodes())
1089 348656 : _preparation.is_partitioned = true;
1090 :
1091 : // If we're using DistributedMesh, we'll probably want it
1092 : // parallelized.
1093 962586 : if (this->_allow_remote_element_removal &&
1094 930255 : !_preparation.has_removed_remote_elements)
1095 550906 : this->delete_remote_elements();
1096 : else
1097 411680 : _preparation.has_removed_remote_elements = true;
1098 :
1099 : // Much of our boundary info may have been for now-remote parts of the mesh,
1100 : // in which case we don't want to keep local copies of data meant to be
1101 : // local. On the other hand we may have deleted, or the user may have added in
1102 : // a distributed fashion, boundary data that is meant to be global. So we
1103 : // handle both of those scenarios here
1104 962586 : if (!_preparation.has_boundary_id_sets)
1105 635326 : this->get_boundary_info().regenerate_id_sets();
1106 :
1107 962586 : if (!_skip_renumber_nodes_and_elements)
1108 893060 : this->renumber_nodes_and_elements();
1109 :
1110 : // The mesh is now prepared for use, with the possible exception of
1111 : // partitioning that was supposed to be skipped, and it should know
1112 : // it.
1113 : #ifndef NDEBUG
1114 45402 : Preparation completed_preparation = _preparation;
1115 45402 : if (skip_partitioning())
1116 17808 : completed_preparation.is_partitioned = true;
1117 45402 : libmesh_assert(completed_preparation);
1118 : #endif
1119 :
1120 : #ifdef DEBUG
1121 : // The if() here avoids both unnecessary work *and* stack overflow
1122 45402 : if (was_partly_prepared)
1123 17612 : MeshTools::libmesh_assert_valid_is_prepared(*this);
1124 :
1125 45402 : MeshTools::libmesh_assert_valid_boundary_ids(*this);
1126 : #ifdef LIBMESH_ENABLE_UNIQUE_ID
1127 45402 : MeshTools::libmesh_assert_valid_unique_ids(*this);
1128 : #endif
1129 : #endif
1130 962586 : }
1131 :
1132 : void
1133 635113 : MeshBase::reinit_ghosting_functors()
1134 : {
1135 1385271 : for (auto & gf : _ghosting_functors)
1136 : {
1137 43214 : libmesh_assert(gf);
1138 750158 : gf->mesh_reinit();
1139 : }
1140 :
1141 635113 : _preparation.has_reinit_ghosting_functors = true;
1142 635113 : }
1143 :
1144 1536990 : void MeshBase::clear ()
1145 : {
1146 : // Reset the number of partitions
1147 1536990 : _n_parts = 1;
1148 :
1149 : // Reset the preparation flags
1150 1536990 : _preparation = false;
1151 :
1152 : // Clear boundary information
1153 1536990 : if (boundary_info)
1154 1535712 : boundary_info->clear();
1155 :
1156 : // Clear cached element data
1157 80144 : _elem_dims.clear();
1158 80144 : _elem_default_orders.clear();
1159 1536990 : _supported_nodal_order = MAXIMUM;
1160 :
1161 80144 : _elemset_codes.clear();
1162 80144 : _elemset_codes_inverse_map.clear();
1163 :
1164 80144 : _constraint_rows.clear();
1165 1536990 : _n_constraint_rows = 0;
1166 :
1167 : // Clear our point locator.
1168 1536990 : this->clear_point_locator();
1169 1536990 : this->clear_stored_ranges();
1170 1536990 : }
1171 :
1172 :
1173 864458 : bool MeshBase::is_prepared() const
1174 : {
1175 864458 : return static_cast<bool>(_preparation);
1176 : }
1177 :
1178 :
1179 568 : void MeshBase::unset_is_prepared()
1180 : {
1181 568 : _preparation = false;
1182 568 : this->clear_point_locator();
1183 568 : this->clear_stored_ranges();
1184 568 : }
1185 :
1186 :
1187 1533138 : void MeshBase::add_ghosting_functor(GhostingFunctor & ghosting_functor)
1188 : {
1189 : // We used to implicitly support duplicate inserts to std::set
1190 : #ifdef LIBMESH_ENABLE_DEPRECATED
1191 : _ghosting_functors.erase
1192 1442774 : (std::remove(_ghosting_functors.begin(),
1193 : _ghosting_functors.end(),
1194 1533138 : &ghosting_functor),
1195 137264 : _ghosting_functors.end());
1196 : #endif
1197 :
1198 : // We shouldn't have two copies of the same functor
1199 46900 : libmesh_assert(std::find(_ghosting_functors.begin(),
1200 : _ghosting_functors.end(),
1201 : &ghosting_functor) ==
1202 : _ghosting_functors.end());
1203 :
1204 1533138 : _ghosting_functors.push_back(&ghosting_functor);
1205 1533138 : }
1206 :
1207 :
1208 :
1209 1499371 : void MeshBase::remove_ghosting_functor(GhostingFunctor & ghosting_functor)
1210 : {
1211 1414455 : auto raw_it = std::find(_ghosting_functors.begin(),
1212 1541829 : _ghosting_functors.end(), &ghosting_functor);
1213 :
1214 : // The DofMap has a "to_mesh" parameter that tells it to avoid
1215 : // registering a new functor with the mesh, but it doesn't keep
1216 : // track of which functors weren't added, so we'll support "remove a
1217 : // functor that isn't there" just like we did with set::erase
1218 : // before.
1219 1499371 : if (raw_it != _ghosting_functors.end())
1220 1498448 : _ghosting_functors.erase(raw_it);
1221 :
1222 : // We shouldn't have had two copies of the same functor
1223 42458 : libmesh_assert(std::find(_ghosting_functors.begin(),
1224 : _ghosting_functors.end(),
1225 : &ghosting_functor) ==
1226 : _ghosting_functors.end());
1227 :
1228 1499371 : if (const auto it = _shared_functors.find(&ghosting_functor);
1229 42458 : it != _shared_functors.end())
1230 69 : _shared_functors.erase(it);
1231 1499371 : }
1232 :
1233 :
1234 :
1235 2210 : void MeshBase::subdomain_ids (std::set<subdomain_id_type> & ids, const bool global /* = true */) const
1236 : {
1237 572 : libmesh_assert(this->preparation().has_cached_elem_data);
1238 :
1239 2210 : if (global)
1240 2210 : ids = this->get_mesh_subdomains();
1241 : else
1242 0 : ids = this->get_mesh_local_subdomains();
1243 2210 : }
1244 :
1245 :
1246 :
1247 883443 : void MeshBase::redistribute()
1248 : {
1249 : // We now have all elements and nodes redistributed; our ghosting
1250 : // functors should be ready to redistribute and/or recompute any
1251 : // cached data they use too.
1252 887007 : for (auto & gf : as_range(this->ghosting_functors_begin(),
1253 1946851 : this->ghosting_functors_end()))
1254 1023386 : gf->redistribute();
1255 883443 : }
1256 :
1257 :
1258 :
1259 4861177 : void MeshBase::update_post_partitioning()
1260 : {
1261 : // Any range over local objects is obsolete if our partitioner
1262 : // changed the definition of "local".
1263 64377 : _active_local_element_stored_range.reset();
1264 64377 : _const_active_local_element_stored_range.reset();
1265 64377 : _local_element_stored_range.reset();
1266 64377 : _const_local_element_stored_range.reset();
1267 64377 : _local_node_stored_range.reset();
1268 64377 : _const_local_node_stored_range.reset();
1269 64377 : _unpartitioned_node_stored_range.reset();
1270 64377 : _const_unpartitioned_node_stored_range.reset();
1271 :
1272 : // A range over all elements or nodes might still be fine here, but
1273 : // not if there's a possibility that we've remoted or gathered
1274 : // previously-remote objects.
1275 4861177 : if (!this->is_replicated())
1276 : {
1277 2600 : _element_stored_range.reset();
1278 2600 : _const_element_stored_range.reset();
1279 2600 : _node_stored_range.reset();
1280 2600 : _const_node_stored_range.reset();
1281 : }
1282 :
1283 128754 : SBDInserter inserter;
1284 4861177 : Threads::parallel_reduce(this->const_active_local_element_stored_range(), inserter);
1285 :
1286 64377 : _mesh_local_subdomains.swap(inserter.my_ids);
1287 4861177 : }
1288 :
1289 :
1290 :
1291 6175 : subdomain_id_type MeshBase::n_subdomains() const
1292 : {
1293 6175 : return cast_int<subdomain_id_type>(this->get_mesh_subdomains().size());
1294 : }
1295 :
1296 :
1297 :
1298 0 : subdomain_id_type MeshBase::n_local_subdomains() const
1299 : {
1300 0 : std::set<subdomain_id_type> ids;
1301 :
1302 0 : this->subdomain_ids (ids, /* global = */ false);
1303 :
1304 0 : return cast_int<subdomain_id_type>(ids.size());
1305 : }
1306 :
1307 :
1308 :
1309 8385 : const std::set<subdomain_id_type> & MeshBase::get_mesh_subdomains() const
1310 : {
1311 748 : libmesh_assert(_preparation.has_cached_elem_data);
1312 :
1313 : #ifdef DEBUG
1314 : // Hit local dbg test; global cache test would need parallel_only()
1315 748 : this->get_mesh_local_subdomains();
1316 : #endif
1317 :
1318 8385 : return _mesh_subdomains;
1319 : }
1320 :
1321 :
1322 :
1323 748 : const std::set<subdomain_id_type> & MeshBase::get_mesh_local_subdomains() const
1324 : {
1325 748 : libmesh_assert(_preparation.has_cached_elem_data);
1326 :
1327 : #ifdef DEBUG
1328 : // This is O(N) instead of O(1), but that's what dbg is for, and
1329 : // it's not worse than this call was before the cache was added
1330 748 : SBDInserter inserter;
1331 : Threads::parallel_reduce
1332 748 : (this->const_active_local_element_stored_range(), inserter);
1333 :
1334 748 : libmesh_assert(inserter.my_ids == _mesh_local_subdomains);
1335 : #endif
1336 :
1337 1496 : return _mesh_local_subdomains;
1338 : }
1339 :
1340 :
1341 :
1342 8977534 : dof_id_type MeshBase::n_nodes_on_proc (const processor_id_type proc_id) const
1343 : {
1344 : // We're either counting a processor's nodes or unpartitioned
1345 : // nodes
1346 52700 : libmesh_assert (proc_id < this->n_processors() ||
1347 : proc_id == DofObject::invalid_processor_id);
1348 :
1349 17902368 : return static_cast<dof_id_type>(std::distance (this->pid_nodes_begin(proc_id),
1350 26879902 : this->pid_nodes_end (proc_id)));
1351 : }
1352 :
1353 :
1354 :
1355 9591017 : dof_id_type MeshBase::n_elem_on_proc (const processor_id_type proc_id) const
1356 : {
1357 : // We're either counting a processor's elements or unpartitioned
1358 : // elements
1359 71173 : libmesh_assert (proc_id < this->n_processors() ||
1360 : proc_id == DofObject::invalid_processor_id);
1361 :
1362 19110861 : return static_cast<dof_id_type>(std::distance (this->pid_elements_begin(proc_id),
1363 28701878 : this->pid_elements_end (proc_id)));
1364 : }
1365 :
1366 :
1367 :
1368 1678865 : dof_id_type MeshBase::n_active_elem_on_proc (const processor_id_type proc_id) const
1369 : {
1370 2262 : libmesh_assert_less (proc_id, this->n_processors());
1371 3355468 : return static_cast<dof_id_type>(std::distance (this->active_pid_elements_begin(proc_id),
1372 5034333 : this->active_pid_elements_end (proc_id)));
1373 : }
1374 :
1375 :
1376 :
1377 0 : dof_id_type MeshBase::n_sub_elem () const
1378 : {
1379 0 : dof_id_type ne=0;
1380 :
1381 0 : for (const auto & elem : this->element_ptr_range())
1382 0 : ne += elem->n_sub_elem();
1383 :
1384 0 : return ne;
1385 : }
1386 :
1387 :
1388 :
1389 32266 : dof_id_type MeshBase::n_active_sub_elem () const
1390 : {
1391 1575 : dof_id_type ne=0;
1392 :
1393 25301373 : for (const auto & elem : this->active_element_ptr_range())
1394 25267532 : ne += elem->n_sub_elem();
1395 :
1396 32266 : return ne;
1397 : }
1398 :
1399 :
1400 :
1401 15137 : std::string MeshBase::get_info(const unsigned int verbosity /* = 0 */, const bool global /* = true */) const
1402 : {
1403 15985 : std::ostringstream oss;
1404 :
1405 15137 : oss << " Mesh Information:" << '\n';
1406 :
1407 15137 : if (!_elem_dims.empty())
1408 : {
1409 15137 : oss << " elem_dimensions()={";
1410 15137 : std::copy(_elem_dims.begin(),
1411 424 : --_elem_dims.end(), // --end() is valid if the set is non-empty
1412 14713 : std::ostream_iterator<unsigned int>(oss, ", "));
1413 15137 : oss << cast_int<unsigned int>(*_elem_dims.rbegin());
1414 15137 : oss << "}";
1415 15137 : if (!this->preparation().has_cached_elem_data)
1416 0 : oss << " (may be out of date)";
1417 15137 : oss << '\n';
1418 : }
1419 :
1420 15137 : if (!_elem_default_orders.empty())
1421 : {
1422 15137 : oss << " elem_default_orders()={";
1423 15137 : std::transform(_elem_default_orders.begin(),
1424 424 : --_elem_default_orders.end(),
1425 15137 : std::ostream_iterator<std::string>(oss, ", "),
1426 8 : [](Order o)
1427 276 : { return Utility::enum_to_string<Order>(o); });
1428 15137 : oss << Utility::enum_to_string<Order>(*_elem_default_orders.rbegin());
1429 15137 : oss << "}";
1430 15137 : if (!this->preparation().has_cached_elem_data)
1431 0 : oss << " (may be out of date)";
1432 15137 : oss << '\n';
1433 : }
1434 :
1435 15137 : oss << " supported_nodal_order()=" << this->_supported_nodal_order;
1436 15137 : if (!this->preparation().has_cached_elem_data)
1437 0 : oss << " (may be out of date)";
1438 15137 : oss << '\n';
1439 :
1440 15137 : oss << " spatial_dimension()=" << int(this->_spatial_dimension);
1441 15137 : if (!this->preparation().has_cached_elem_data)
1442 0 : oss << " (may be out of date)";
1443 15137 : oss << '\n';
1444 :
1445 15137 : oss << " n_nodes()=" << this->n_nodes() << '\n'
1446 15137 : << " n_local_nodes()=" << this->n_local_nodes() << '\n'
1447 15561 : << " n_elem()=" << this->n_elem() << '\n'
1448 29850 : << " n_local_elem()=" << this->n_local_elem() << '\n';
1449 : #ifdef LIBMESH_ENABLE_AMR
1450 29850 : oss << " n_active_elem()=" << this->n_active_elem() << '\n';
1451 : #endif
1452 15137 : if (!this->preparation().has_cached_elem_data)
1453 : {
1454 0 : if (global)
1455 0 : oss << " n_subdomains()=" << static_cast<std::size_t>(this->n_subdomains()) << '\n';
1456 : else
1457 0 : oss << " n_local_subdomains()= " << static_cast<std::size_t>(this->n_local_subdomains()) << '\n';
1458 : }
1459 15561 : oss << " n_elemsets()=" << static_cast<std::size_t>(this->n_elemsets()) << '\n';
1460 15137 : if (!_elemset_codes.empty())
1461 0 : oss << " n_elemset_codes=" << _elemset_codes.size() << '\n';
1462 15137 : oss << " n_partitions()=" << static_cast<std::size_t>(this->n_partitions()) << '\n'
1463 15985 : << " n_processors()=" << static_cast<std::size_t>(this->n_processors()) << '\n'
1464 15561 : << " n_threads()=" << static_cast<std::size_t>(libMesh::n_threads()) << '\n'
1465 15985 : << " processor_id()=" << static_cast<std::size_t>(this->processor_id()) << '\n'
1466 15561 : << " is_prepared()=" << (this->is_prepared() ? "true" : "false") << '\n'
1467 43505 : << " is_replicated()=" << (this->is_replicated() ? "true" : "false") << '\n';
1468 :
1469 15137 : if (verbosity > 0)
1470 : {
1471 284 : if (global)
1472 : {
1473 8 : libmesh_parallel_only(this->comm());
1474 292 : if (this->processor_id() != 0)
1475 236 : oss << "\n Detailed global get_info() (verbosity > 0) is reduced and output to only rank 0.";
1476 : }
1477 :
1478 : // Helper for printing element types
1479 672 : const auto elem_type_helper = [](const std::set<int> & elem_types) {
1480 784 : std::stringstream ss;
1481 1344 : for (auto it = elem_types.begin(); it != elem_types.end();)
1482 : {
1483 1288 : ss << Utility::enum_to_string((ElemType)*it);
1484 672 : if (++it != elem_types.end())
1485 0 : ss << ", ";
1486 : }
1487 728 : return ss.str();
1488 560 : };
1489 :
1490 : // Helper for whether or not the given DofObject is to be included. If we're doing
1491 : // a global reduction, we also count unpartitioned objects on rank 0.
1492 871156 : const auto include_object = [this, &global](const DofObject & dof_object) {
1493 978188 : return this->processor_id() == dof_object.processor_id() ||
1494 250756 : (global &&
1495 42324 : this->processor_id() == 0 &&
1496 846328 : dof_object.processor_id() == DofObject::invalid_processor_id);
1497 276 : };
1498 :
1499 8 : Real volume = 0;
1500 :
1501 : // Add bounding box information
1502 284 : const auto bbox = global ? MeshTools::create_bounding_box(*this) : MeshTools::create_local_bounding_box(*this);
1503 284 : if (!global || this->processor_id() == 0)
1504 : oss << "\n " << (global ? "" : "Local ") << "Mesh Bounding Box:\n"
1505 48 : << " Minimum: " << bbox.min() << "\n"
1506 44 : << " Maximum: " << bbox.max() << "\n"
1507 88 : << " Delta: " << (bbox.max() - bbox.min()) << "\n";
1508 :
1509 : // Obtain the global or local element types
1510 16 : std::set<int> elem_types;
1511 49712 : for (const Elem * elem : this->active_local_element_ptr_range())
1512 49420 : elem_types.insert(elem->type());
1513 284 : if (global)
1514 : {
1515 : // Pick up unpartitioned elems on rank 0
1516 292 : if (this->processor_id() == 0)
1517 92 : for (const Elem * elem : this->active_unpartitioned_element_ptr_range())
1518 40 : elem_types.insert(elem->type());
1519 :
1520 284 : this->comm().set_union(elem_types);
1521 : }
1522 :
1523 : // Add element types
1524 284 : if (!global || this->processor_id() == 0)
1525 : oss << "\n " << (global ? "" : "Local ") << "Mesh Element Type(s):\n "
1526 140 : << elem_type_helper(elem_types) << "\n";
1527 :
1528 : // Reduce the nodeset ids
1529 16 : auto nodeset_ids = this->get_boundary_info().get_node_boundary_ids();
1530 284 : if (global)
1531 284 : this->comm().set_union(nodeset_ids);
1532 :
1533 : // Accumulate local information for each nodeset
1534 1608 : struct NodesetInfo
1535 : {
1536 : std::size_t num_nodes = 0;
1537 : BoundingBox bbox;
1538 : };
1539 16 : std::map<boundary_id_type, NodesetInfo> nodeset_info_map;
1540 91376 : for (const auto & [node, id] : this->get_boundary_info().get_nodeset_map())
1541 : {
1542 91092 : if (!include_object(*node))
1543 38364 : continue;
1544 :
1545 48672 : NodesetInfo & info = nodeset_info_map[id];
1546 :
1547 48672 : ++info.num_nodes;
1548 :
1549 48672 : if (verbosity > 1)
1550 48672 : info.bbox.union_with(*node);
1551 : }
1552 :
1553 : // Add nodeset info
1554 284 : if (!global || this->processor_id() == 0)
1555 : {
1556 48 : oss << "\n " << (global ? "" : "Local ") << "Mesh Nodesets:\n";
1557 48 : if (nodeset_ids.empty())
1558 0 : oss << " None\n";
1559 : }
1560 :
1561 8 : const auto & nodeset_name_map = this->get_boundary_info().get_nodeset_name_map();
1562 1988 : for (const auto id : nodeset_ids)
1563 : {
1564 1704 : NodesetInfo & info = nodeset_info_map[id];
1565 :
1566 : // Reduce the local information for this nodeset if required
1567 1704 : if (global)
1568 : {
1569 1704 : this->comm().sum(info.num_nodes);
1570 1704 : if (verbosity > 1)
1571 : {
1572 1752 : this->comm().min(info.bbox.min());
1573 1752 : this->comm().max(info.bbox.max());
1574 : }
1575 : }
1576 :
1577 1704 : const bool has_name = nodeset_name_map.count(id) && nodeset_name_map.at(id).size();
1578 1800 : const std::string name = has_name ? nodeset_name_map.at(id) : "";
1579 96 : if (global)
1580 48 : libmesh_assert(this->comm().verify(name));
1581 :
1582 1704 : if (global ? this->processor_id() == 0 : info.num_nodes > 0)
1583 : {
1584 288 : oss << " Nodeset " << id;
1585 288 : if (has_name)
1586 528 : oss << " (" << name << ")";
1587 312 : oss << ", " << info.num_nodes << " " << (global ? "" : "local ") << "nodes\n";
1588 :
1589 288 : if (verbosity > 1)
1590 : {
1591 288 : oss << " " << (global ? "Bounding" : "Local bounding") << " box minimum: "
1592 288 : << info.bbox.min() << "\n"
1593 288 : << " " << (global ? "Bounding" : "Local bounding") << " box maximum: "
1594 288 : << info.bbox.max() << "\n"
1595 288 : << " " << (global ? "Bounding" : "Local bounding") << " box delta: "
1596 288 : << (info.bbox.max() - info.bbox.min()) << "\n";
1597 : }
1598 : }
1599 : }
1600 :
1601 : // Reduce the sideset ids
1602 16 : auto sideset_ids = this->get_boundary_info().get_side_boundary_ids();
1603 284 : if (global)
1604 284 : this->comm().set_union(sideset_ids);
1605 :
1606 : // Accumulate local information for each sideset
1607 : struct SidesetInfo
1608 : {
1609 : std::size_t num_sides = 0;
1610 : Real volume = 0;
1611 : std::set<int> side_elem_types;
1612 : std::set<int> elem_types;
1613 : std::set<dof_id_type> elem_ids;
1614 : std::set<dof_id_type> node_ids;
1615 : BoundingBox bbox;
1616 : };
1617 16 : ElemSideBuilder side_builder;
1618 16 : std::map<boundary_id_type, SidesetInfo> sideset_info_map;
1619 70396 : for (const auto & pair : this->get_boundary_info().get_sideset_map())
1620 : {
1621 70112 : const Elem * elem = pair.first;
1622 63456 : if (!include_object(*elem))
1623 30176 : continue;
1624 :
1625 39936 : const auto id = pair.second.second;
1626 39936 : SidesetInfo & info = sideset_info_map[id];
1627 :
1628 39936 : const auto s = pair.second.first;
1629 39936 : const Elem & side = side_builder(*elem, s);
1630 :
1631 39936 : ++info.num_sides;
1632 39936 : info.side_elem_types.insert(side.type());
1633 39936 : info.elem_types.insert(elem->type());
1634 39936 : info.elem_ids.insert(elem->id());
1635 :
1636 199680 : for (const Node & node : side.node_ref_range())
1637 146432 : if (include_object(node))
1638 147552 : info.node_ids.insert(node.id());
1639 :
1640 39936 : if (verbosity > 1)
1641 : {
1642 39936 : info.volume += side.volume();
1643 39936 : info.bbox.union_with(side.loose_bounding_box());
1644 : }
1645 : }
1646 :
1647 : // Add sideset info
1648 284 : if (!global || this->processor_id() == 0)
1649 : {
1650 48 : oss << "\n " << (global ? "" : "Local ") << "Mesh Sidesets:\n";
1651 48 : if (sideset_ids.empty())
1652 0 : oss << " None\n";
1653 : }
1654 8 : const auto & sideset_name_map = this->get_boundary_info().get_sideset_name_map();
1655 1988 : for (const auto id : sideset_ids)
1656 : {
1657 1704 : SidesetInfo & info = sideset_info_map[id];
1658 :
1659 1704 : auto num_elems = info.elem_ids.size();
1660 1704 : auto num_nodes = info.node_ids.size();
1661 :
1662 : // Reduce the local information for this sideset if required
1663 1704 : if (global)
1664 : {
1665 1704 : this->comm().sum(info.num_sides);
1666 1704 : this->comm().set_union(info.side_elem_types, 0);
1667 1704 : this->comm().sum(num_elems);
1668 1704 : this->comm().set_union(info.elem_types, 0);
1669 1704 : this->comm().sum(num_nodes);
1670 1704 : if (verbosity > 1)
1671 : {
1672 1704 : this->comm().sum(info.volume);
1673 1752 : this->comm().min(info.bbox.min());
1674 1752 : this->comm().max(info.bbox.max());
1675 : }
1676 : }
1677 :
1678 1704 : const bool has_name = sideset_name_map.count(id) && sideset_name_map.at(id).size();
1679 1800 : const std::string name = has_name ? sideset_name_map.at(id) : "";
1680 96 : if (global)
1681 48 : libmesh_assert(this->comm().verify(name));
1682 :
1683 1704 : if (global ? this->processor_id() == 0 : info.num_sides > 0)
1684 : {
1685 288 : oss << " Sideset " << id;
1686 288 : if (has_name)
1687 528 : oss << " (" << name << ")";
1688 552 : oss << ", " << info.num_sides << " sides (" << elem_type_helper(info.side_elem_types) << ")"
1689 1056 : << ", " << num_elems << " " << (global ? "" : "local ") << "elems (" << elem_type_helper(info.elem_types) << ")"
1690 600 : << ", " << num_nodes << " " << (global ? "" : "local ") << "nodes\n";
1691 :
1692 288 : if (verbosity > 1)
1693 : {
1694 312 : oss << " " << (global ? "Side" : "Local side") << " volume: " << info.volume << "\n"
1695 288 : << " " << (global ? "Bounding" : "Local bounding") << " box minimum: "
1696 288 : << info.bbox.min() << "\n"
1697 288 : << " " << (global ? "Bounding" : "Local bounding") << " box maximum: "
1698 288 : << info.bbox.max() << "\n"
1699 288 : << " " << (global ? "Bounding" : "Local bounding") << " box delta: "
1700 288 : << (info.bbox.max() - info.bbox.min()) << "\n";
1701 : }
1702 : }
1703 : }
1704 :
1705 : // Reduce the edgeset ids
1706 16 : auto edgeset_ids = this->get_boundary_info().get_edge_boundary_ids();
1707 284 : if (global)
1708 284 : this->comm().set_union(edgeset_ids);
1709 :
1710 : // Accumulate local information for each edgeset
1711 0 : struct EdgesetInfo
1712 : {
1713 : std::size_t num_edges = 0;
1714 : std::set<int> edge_elem_types;
1715 : BoundingBox bbox;
1716 : };
1717 16 : std::map<boundary_id_type, EdgesetInfo> edgeset_info_map;
1718 284 : std::unique_ptr<const Elem> edge;
1719 :
1720 284 : for (const auto & pair : this->get_boundary_info().get_edgeset_map())
1721 : {
1722 0 : const Elem * elem = pair.first;
1723 0 : if (!include_object(*elem))
1724 0 : continue;
1725 :
1726 0 : const auto id = pair.second.second;
1727 0 : EdgesetInfo & info = edgeset_info_map[id];
1728 :
1729 0 : elem->build_edge_ptr(edge, pair.second.first);
1730 :
1731 0 : ++info.num_edges;
1732 0 : info.edge_elem_types.insert(edge->type());
1733 :
1734 0 : if (verbosity > 1)
1735 0 : info.bbox.union_with(edge->loose_bounding_box());
1736 : }
1737 :
1738 : // Add edgeset info
1739 284 : if (!global || this->processor_id() == 0)
1740 : {
1741 48 : oss << "\n " << (global ? "" : "Local ") << "Mesh Edgesets:\n";
1742 48 : if (edgeset_ids.empty())
1743 48 : oss << " None\n";
1744 : }
1745 :
1746 8 : const auto & edgeset_name_map = this->get_boundary_info().get_edgeset_name_map();
1747 284 : for (const auto id : edgeset_ids)
1748 : {
1749 0 : EdgesetInfo & info = edgeset_info_map[id];
1750 :
1751 : // Reduce the local information for this edgeset if required
1752 0 : if (global)
1753 : {
1754 0 : this->comm().sum(info.num_edges);
1755 0 : this->comm().set_union(info.edge_elem_types, 0);
1756 0 : if (verbosity > 1)
1757 : {
1758 0 : this->comm().min(info.bbox.min());
1759 0 : this->comm().min(info.bbox.max());
1760 : }
1761 : }
1762 :
1763 0 : const bool has_name = edgeset_name_map.count(id) && edgeset_name_map.at(id).size();
1764 0 : const std::string name = has_name ? edgeset_name_map.at(id) : "";
1765 0 : if (global)
1766 0 : libmesh_assert(this->comm().verify(name));
1767 :
1768 0 : if (global ? this->processor_id() == 0 : info.num_edges > 0)
1769 : {
1770 0 : oss << " Edgeset " << id;
1771 0 : if (has_name)
1772 0 : oss << " (" << name << ")";
1773 0 : oss << ", " << info.num_edges << " " << (global ? "" : "local ") << "edges ("
1774 0 : << elem_type_helper(info.edge_elem_types) << ")\n";
1775 :
1776 0 : if (verbosity > 1)
1777 : {
1778 0 : oss << " " << (global ? "Bounding" : "Local bounding") << " box minimum: "
1779 0 : << info.bbox.min() << "\n"
1780 0 : << " " << (global ? "Bounding" : "Local bounding") << " box maximum: "
1781 0 : << info.bbox.max() << "\n"
1782 0 : << " " << (global ? "Bounding" : "Local bounding") << " box delta: "
1783 0 : << (info.bbox.max() - info.bbox.min()) << "\n";
1784 : }
1785 : }
1786 : }
1787 :
1788 : // Reduce the block IDs and block names
1789 16 : std::set<subdomain_id_type> subdomains;
1790 194032 : for (const Elem * elem : this->active_element_ptr_range())
1791 92640 : if (include_object(*elem))
1792 49420 : subdomains.insert(elem->subdomain_id());
1793 284 : if (global)
1794 284 : this->comm().set_union(subdomains);
1795 :
1796 : // Accumulate local information for each subdomain
1797 0 : struct SubdomainInfo
1798 : {
1799 : std::size_t num_elems = 0;
1800 : Real volume = 0;
1801 : std::set<int> elem_types;
1802 : std::set<dof_id_type> active_node_ids;
1803 : #ifdef LIBMESH_ENABLE_AMR
1804 : std::size_t num_active_elems = 0;
1805 : #endif
1806 : BoundingBox bbox;
1807 : };
1808 16 : std::map<subdomain_id_type, SubdomainInfo> subdomain_info_map;
1809 194032 : for (const Elem * elem : this->element_ptr_range())
1810 92640 : if (include_object(*elem))
1811 : {
1812 49152 : SubdomainInfo & info = subdomain_info_map[elem->subdomain_id()];
1813 :
1814 49152 : ++info.num_elems;
1815 49152 : info.elem_types.insert(elem->type());
1816 :
1817 : #ifdef LIBMESH_ENABLE_AMR
1818 4096 : if (elem->active())
1819 49152 : ++info.num_active_elems;
1820 : #endif
1821 :
1822 442368 : for (const Node & node : elem->node_ref_range())
1823 392704 : if (include_object(node) && node.active())
1824 330608 : info.active_node_ids.insert(node.id());
1825 :
1826 49152 : if (verbosity > 1 && elem->active())
1827 : {
1828 49152 : info.volume += elem->volume();
1829 49152 : info.bbox.union_with(elem->loose_bounding_box());
1830 : }
1831 268 : }
1832 :
1833 : // Add subdomain info
1834 284 : oss << "\n " << (global ? "" : "Local ") << "Mesh Subdomains:\n";
1835 8 : const auto & subdomain_name_map = this->get_subdomain_name_map();
1836 568 : for (const auto id : subdomains)
1837 : {
1838 284 : SubdomainInfo & info = subdomain_info_map[id];
1839 :
1840 284 : auto num_active_nodes = info.active_node_ids.size();
1841 :
1842 : // Reduce the information for this subdomain if needed
1843 284 : if (global)
1844 : {
1845 284 : this->comm().sum(info.num_elems);
1846 : #ifdef LIBMESH_ENABLE_AMR
1847 284 : this->comm().sum(info.num_active_elems);
1848 : #endif
1849 284 : this->comm().sum(num_active_nodes);
1850 284 : this->comm().set_union(info.elem_types, 0);
1851 284 : if (verbosity > 1)
1852 : {
1853 292 : this->comm().min(info.bbox.min());
1854 292 : this->comm().max(info.bbox.max());
1855 284 : this->comm().sum(info.volume);
1856 : }
1857 : }
1858 284 : if (verbosity > 1)
1859 284 : volume += info.volume;
1860 :
1861 8 : const bool has_name = subdomain_name_map.count(id);
1862 292 : const std::string name = has_name ? subdomain_name_map.at(id) : "";
1863 16 : if (global)
1864 8 : libmesh_assert(this->comm().verify(name));
1865 :
1866 284 : if (!global || this->processor_id() == 0)
1867 : {
1868 48 : oss << " Subdomain " << id;
1869 48 : if (has_name)
1870 0 : oss << " (" << name << ")";
1871 48 : oss << ": " << info.num_elems << " " << (global ? "" : "local ") << "elems "
1872 56 : << "(" << elem_type_helper(info.elem_types);
1873 : #ifdef LIBMESH_ENABLE_AMR
1874 48 : oss << ", " << info.num_active_elems << " active";
1875 : #endif
1876 52 : oss << "), " << num_active_nodes << " " << (global ? "" : "local ") << "active nodes\n";
1877 48 : if (verbosity > 1)
1878 : {
1879 52 : oss << " " << (global ? "Volume" : "Local volume") << ": " << info.volume << "\n";
1880 48 : oss << " " << (global ? "Bounding" : "Local bounding") << " box minimum: "
1881 48 : << info.bbox.min() << "\n"
1882 48 : << " " << (global ? "Bounding" : "Local bounding") << " box maximum: "
1883 48 : << info.bbox.max() << "\n"
1884 48 : << " " << (global ? "Bounding" : "Local bounding") << " box delta: "
1885 48 : << (info.bbox.max() - info.bbox.min()) << "\n";
1886 : }
1887 : }
1888 : }
1889 :
1890 560 : oss << " " << (global ? "Global" : "Local") << " mesh volume = " << volume << "\n";
1891 :
1892 268 : }
1893 :
1894 15561 : return oss.str();
1895 14289 : }
1896 :
1897 :
1898 15137 : void MeshBase::print_info(std::ostream & os, const unsigned int verbosity /* = 0 */, const bool global /* = true */) const
1899 : {
1900 15561 : os << this->get_info(verbosity, global)
1901 424 : << std::endl;
1902 15137 : }
1903 :
1904 :
1905 0 : std::ostream & operator << (std::ostream & os, const MeshBase & m)
1906 : {
1907 0 : m.print_info(os);
1908 0 : return os;
1909 : }
1910 :
1911 :
1912 614863 : void MeshBase::partition (const unsigned int n_parts)
1913 : {
1914 : // If we get here and we have unpartitioned elements, we need that
1915 : // fixed.
1916 614863 : if (this->n_unpartitioned_elem() > 0)
1917 : {
1918 14500 : libmesh_assert (partitioner().get());
1919 14500 : libmesh_assert (this->is_serial());
1920 481239 : partitioner()->partition (*this, n_parts);
1921 : }
1922 : // A nullptr partitioner or a skip_partitioning(true) call or a
1923 : // skip_noncritical_partitioning(true) call means don't repartition;
1924 : // skip_noncritical_partitioning() checks all these.
1925 3987 : else if (!skip_noncritical_partitioning())
1926 : {
1927 126895 : partitioner()->partition (*this, n_parts);
1928 : }
1929 : else
1930 : {
1931 : // Adaptive coarsening may have "orphaned" nodes on processors
1932 : // whose elements no longer share them. We need to check for
1933 : // and possibly fix that.
1934 6729 : MeshTools::correct_node_proc_ids(*this);
1935 :
1936 : // Make sure locally cached partition count is correct
1937 6729 : this->recalculate_n_partitions();
1938 :
1939 : // Make sure any other locally cached data is correct
1940 6729 : this->update_post_partitioning();
1941 : }
1942 :
1943 614863 : _preparation.is_partitioned = true;
1944 614863 : }
1945 :
1946 25039 : void MeshBase::all_second_order (const bool full_ordered)
1947 : {
1948 25039 : this->all_second_order_range(this->element_ptr_range(), full_ordered);
1949 25039 : }
1950 :
1951 21716 : void MeshBase::all_complete_order ()
1952 : {
1953 21716 : this->all_complete_order_range(this->element_ptr_range());
1954 21716 : }
1955 :
1956 6729 : unsigned int MeshBase::recalculate_n_partitions()
1957 : {
1958 : // This requires an inspection on every processor
1959 170 : parallel_object_only();
1960 :
1961 6729 : unsigned int max_proc_id=0;
1962 :
1963 225762 : for (const auto & elem : this->active_local_element_ptr_range())
1964 223045 : max_proc_id = std::max(max_proc_id, static_cast<unsigned int>(elem->processor_id()));
1965 :
1966 : // The number of partitions is one more than the max processor ID.
1967 6729 : _n_parts = max_proc_id+1;
1968 :
1969 6729 : this->comm().max(_n_parts);
1970 :
1971 6729 : return _n_parts;
1972 : }
1973 :
1974 :
1975 :
1976 629315 : std::unique_ptr<PointLocatorBase> MeshBase::sub_point_locator () const
1977 : {
1978 : // If there's no master point locator, then we need one.
1979 629315 : if (_point_locator.get() == nullptr)
1980 : {
1981 : // PointLocator construction may not be safe within threads
1982 964 : libmesh_assert(!Threads::in_threads);
1983 :
1984 : // And it may require parallel communication
1985 964 : parallel_object_only();
1986 :
1987 : #ifdef LIBMESH_ENABLE_NANOFLANN_POINTLOCATOR
1988 : _point_locator = PointLocatorBase::build(NANOFLANN, *this);
1989 : #else
1990 48496 : _point_locator = PointLocatorBase::build(TREE_ELEMENTS, *this);
1991 : #endif
1992 :
1993 25134 : if (_point_locator_close_to_point_tol > 0.)
1994 0 : _point_locator->set_close_to_point_tol(_point_locator_close_to_point_tol);
1995 : }
1996 :
1997 : // Otherwise there was a master point locator, and we can grab a
1998 : // sub-locator easily.
1999 : return
2000 : #ifdef LIBMESH_ENABLE_NANOFLANN_POINTLOCATOR
2001 : PointLocatorBase::build(NANOFLANN, *this, _point_locator.get());
2002 : #else
2003 629315 : PointLocatorBase::build(TREE_ELEMENTS, *this, _point_locator.get());
2004 : #endif
2005 : }
2006 :
2007 :
2008 :
2009 113583861 : void MeshBase::clear_point_locator ()
2010 : {
2011 3102648 : _point_locator.reset(nullptr);
2012 113583861 : }
2013 :
2014 :
2015 :
2016 0 : void MeshBase::set_count_lower_dim_elems_in_point_locator(bool count_lower_dim_elems)
2017 : {
2018 0 : _count_lower_dim_elems_in_point_locator = count_lower_dim_elems;
2019 0 : }
2020 :
2021 :
2022 :
2023 6905049 : bool MeshBase::get_count_lower_dim_elems_in_point_locator() const
2024 : {
2025 6905049 : return _count_lower_dim_elems_in_point_locator;
2026 : }
2027 :
2028 :
2029 : #ifdef LIBMESH_ENABLE_DEPRECATED
2030 568 : std::string & MeshBase::subdomain_name(subdomain_id_type id)
2031 : {
2032 : // Use set_subdomain_name() instead
2033 : libmesh_deprecated();
2034 :
2035 : // We'd like to do this so our preparation isn't invalidated by a
2036 : // write, but for all we know the user is just reading from a
2037 : // non-const mesh!
2038 : // this->unset_has_synched_subdomain_name_map();
2039 :
2040 568 : return _block_id_to_name[id];
2041 : }
2042 : #endif // LIBMESH_ENABLE_DEPRECATED
2043 :
2044 :
2045 44675 : const std::string & MeshBase::subdomain_name(subdomain_id_type id) const
2046 : {
2047 : // An empty string to return when no matching subdomain name is found
2048 44675 : static const std::string empty;
2049 :
2050 44675 : if (const auto iter = _block_id_to_name.find(id);
2051 1651 : iter == _block_id_to_name.end())
2052 1429 : return empty;
2053 : else
2054 2856 : return iter->second;
2055 : }
2056 :
2057 1379974 : void MeshBase::set_subdomain_name(const subdomain_id_type id,
2058 : const std::string & name,
2059 : const bool synchronous)
2060 : {
2061 1379974 : if (synchronous)
2062 6 : parallel_object_only();
2063 : else
2064 58488 : this->unset_has_synched_subdomain_name_map();
2065 1379974 : _block_id_to_name[id] = name;
2066 1379974 : }
2067 :
2068 :
2069 0 : subdomain_id_type MeshBase::get_id_by_name(std::string_view name) const
2070 : {
2071 : // Linear search over the map values.
2072 0 : for (const auto & [sbd_id, sbd_name] : _block_id_to_name)
2073 0 : if (sbd_name == name)
2074 0 : return sbd_id;
2075 :
2076 : // If we made it here without returning, we don't have a subdomain
2077 : // with the requested name, so return Elem::invalid_subdomain_id.
2078 0 : return Elem::invalid_subdomain_id;
2079 : }
2080 :
2081 :
2082 3222273 : const ElemRange & MeshBase::element_stored_range()
2083 : {
2084 3222273 : if (!_element_stored_range)
2085 : {
2086 : // Assigning to our cache may not be safe within threads
2087 48815 : libmesh_assert(!Threads::in_threads);
2088 :
2089 : _element_stored_range =
2090 3351420 : std::make_unique<ElemRange>(this->elements_begin(),
2091 2336015 : this->elements_end());
2092 : }
2093 : else
2094 : #ifdef DEBUG
2095 58977 : libmesh_assert(*_element_stored_range ==
2096 : ElemRange(this->elements_begin(),
2097 : this->elements_end()));
2098 : #else
2099 : libmesh_assert_equal_to
2100 : (cast_int<std::ptrdiff_t>(_element_stored_range->size()),
2101 : std::distance(this->elements_begin(), this->elements_end()));
2102 : #endif
2103 :
2104 3222273 : return *_element_stored_range;
2105 : }
2106 :
2107 :
2108 0 : const ConstElemRange & MeshBase::element_stored_range() const
2109 : {
2110 0 : return const_shim(*this, &MeshBase::element_stored_range,
2111 0 : _const_element_stored_range);
2112 : }
2113 :
2114 :
2115 0 : const ConstElemRange & MeshBase::const_element_stored_range() const
2116 : {
2117 0 : return const_shim(*this, &MeshBase::element_stored_range,
2118 0 : _const_element_stored_range);
2119 : }
2120 :
2121 :
2122 5692849 : const ElemRange & MeshBase::active_local_element_stored_range()
2123 : {
2124 5692849 : if (!_active_local_element_stored_range)
2125 : {
2126 : // Assigning to our cache may not be safe within threads
2127 81006 : libmesh_assert(!Threads::in_threads);
2128 :
2129 : _active_local_element_stored_range =
2130 16901761 : std::make_unique<ElemRange>(this->active_local_elements_begin(),
2131 11455644 : this->active_local_elements_end());
2132 : }
2133 : else
2134 : #ifdef DEBUG
2135 2172 : libmesh_assert(*_active_local_element_stored_range ==
2136 : ElemRange(this->active_local_elements_begin(),
2137 : this->active_local_elements_end()));
2138 : #else
2139 : libmesh_assert_equal_to
2140 : (cast_int<std::ptrdiff_t>(_active_local_element_stored_range->size()),
2141 : std::distance(this->active_local_elements_begin(),
2142 : this->active_local_elements_end()));
2143 : #endif
2144 :
2145 5692849 : return *_active_local_element_stored_range;
2146 : }
2147 :
2148 :
2149 2739772 : const ConstElemRange & MeshBase::active_local_element_stored_range() const
2150 : {
2151 2665136 : return const_shim(*this, &MeshBase::active_local_element_stored_range,
2152 2739772 : _const_active_local_element_stored_range);
2153 : }
2154 :
2155 :
2156 4861925 : const ConstElemRange & MeshBase::const_active_local_element_stored_range() const
2157 : {
2158 4733431 : return const_shim(*this, &MeshBase::active_local_element_stored_range,
2159 4861925 : _const_active_local_element_stored_range);
2160 : }
2161 :
2162 :
2163 5419 : const ElemRange & MeshBase::local_element_stored_range()
2164 : {
2165 5419 : if (!_local_element_stored_range)
2166 : {
2167 : // Assigning to our cache may not be safe within threads
2168 52 : libmesh_assert(!Threads::in_threads);
2169 :
2170 : _local_element_stored_range =
2171 16153 : std::make_unique<ElemRange>(this->local_elements_begin(),
2172 10890 : this->local_elements_end());
2173 : }
2174 : else
2175 : #ifdef DEBUG
2176 0 : libmesh_assert(*_local_element_stored_range ==
2177 : ElemRange(this->local_elements_begin(),
2178 : this->local_elements_end()));
2179 : #else
2180 : libmesh_assert_equal_to
2181 : (cast_int<std::ptrdiff_t>(_local_element_stored_range->size()),
2182 : std::distance(this->local_elements_begin(),
2183 : this->local_elements_end()));
2184 : #endif
2185 :
2186 5419 : return *_local_element_stored_range;
2187 : }
2188 :
2189 :
2190 0 : const ConstElemRange & MeshBase::local_element_stored_range() const
2191 : {
2192 0 : return const_shim(*this, &MeshBase::local_element_stored_range,
2193 0 : _const_local_element_stored_range);
2194 : }
2195 :
2196 :
2197 19630 : const ConstElemRange & MeshBase::const_local_element_stored_range() const
2198 : {
2199 18498 : return const_shim(*this, &MeshBase::local_element_stored_range,
2200 19630 : _const_local_element_stored_range);
2201 : }
2202 :
2203 :
2204 4863621 : const NodeRange & MeshBase::node_stored_range()
2205 : {
2206 4863621 : if (!_node_stored_range)
2207 : {
2208 : // Assigning to our cache may not be safe within threads
2209 46773 : libmesh_assert(!Threads::in_threads);
2210 :
2211 : _node_stored_range =
2212 5547971 : std::make_unique<NodeRange>(this->nodes_begin(),
2213 3807603 : this->nodes_end());
2214 : }
2215 : else
2216 : #ifdef DEBUG
2217 78941 : libmesh_assert(*_node_stored_range ==
2218 : NodeRange(this->nodes_begin(),
2219 : this->nodes_end()));
2220 : #else
2221 : libmesh_assert_equal_to
2222 : (cast_int<std::ptrdiff_t>(_node_stored_range->size()),
2223 : std::distance(this->nodes_begin(), this->nodes_end()));
2224 : #endif
2225 :
2226 4863621 : return *_node_stored_range;
2227 : }
2228 :
2229 :
2230 0 : const ConstNodeRange & MeshBase::node_stored_range() const
2231 : {
2232 0 : return const_shim(*this, &MeshBase::node_stored_range,
2233 0 : _const_node_stored_range);
2234 : }
2235 :
2236 :
2237 0 : const ConstNodeRange & MeshBase::const_node_stored_range() const
2238 : {
2239 0 : return const_shim(*this, &MeshBase::node_stored_range,
2240 0 : _const_node_stored_range);
2241 : }
2242 :
2243 :
2244 66301 : const NodeRange & MeshBase::local_node_stored_range()
2245 : {
2246 66301 : if (!_local_node_stored_range)
2247 : {
2248 : // Assigning to our cache may not be safe within threads
2249 186 : libmesh_assert(!Threads::in_threads);
2250 :
2251 : _local_node_stored_range =
2252 29757 : std::make_unique<NodeRange>(this->local_nodes_begin(),
2253 20272 : this->local_nodes_end());
2254 : }
2255 : else
2256 : #ifdef DEBUG
2257 1590 : libmesh_assert(*_local_node_stored_range ==
2258 : NodeRange(this->local_nodes_begin(),
2259 : this->local_nodes_end()));
2260 : #else
2261 : libmesh_assert_equal_to
2262 : (cast_int<std::ptrdiff_t>(_local_node_stored_range->size()),
2263 : std::distance(this->local_nodes_begin(),
2264 : this->local_nodes_end()));
2265 : #endif
2266 :
2267 66301 : return *_local_node_stored_range;
2268 : }
2269 :
2270 :
2271 0 : const ConstNodeRange & MeshBase::local_node_stored_range() const
2272 : {
2273 0 : return const_shim(*this, &MeshBase::local_node_stored_range,
2274 0 : _const_local_node_stored_range);
2275 : }
2276 :
2277 :
2278 19630 : const ConstNodeRange & MeshBase::const_local_node_stored_range() const
2279 : {
2280 18498 : return const_shim(*this, &MeshBase::local_node_stored_range,
2281 19630 : _const_local_node_stored_range);
2282 : }
2283 :
2284 :
2285 25880 : const NodeRange & MeshBase::unpartitioned_node_stored_range()
2286 : {
2287 25880 : if (!_unpartitioned_node_stored_range)
2288 : {
2289 : // Assigning to our cache may not be safe within threads
2290 62 : libmesh_assert(!Threads::in_threads);
2291 :
2292 : _unpartitioned_node_stored_range =
2293 6080 : std::make_unique<NodeRange>(this->pid_nodes_begin(DofObject::invalid_processor_id),
2294 4198 : this->pid_nodes_end(DofObject::invalid_processor_id));
2295 : }
2296 : else
2297 : #ifdef DEBUG
2298 682 : libmesh_assert(*_unpartitioned_node_stored_range ==
2299 : NodeRange(this->pid_nodes_begin(DofObject::invalid_processor_id),
2300 : this->pid_nodes_end(DofObject::invalid_processor_id)));
2301 : #else
2302 : libmesh_assert_equal_to
2303 : (cast_int<std::ptrdiff_t>(_unpartitioned_node_stored_range->size()),
2304 : std::distance(this->pid_nodes_begin(DofObject::invalid_processor_id),
2305 : this->pid_nodes_end(DofObject::invalid_processor_id)));
2306 : #endif
2307 :
2308 25880 : return *_unpartitioned_node_stored_range;
2309 : }
2310 :
2311 :
2312 0 : const ConstNodeRange & MeshBase::unpartitioned_node_stored_range() const
2313 : {
2314 0 : return const_shim(*this, &MeshBase::unpartitioned_node_stored_range,
2315 0 : _const_unpartitioned_node_stored_range);
2316 : }
2317 :
2318 :
2319 0 : const ConstNodeRange & MeshBase::const_unpartitioned_node_stored_range() const
2320 : {
2321 0 : return const_shim(*this, &MeshBase::unpartitioned_node_stored_range,
2322 0 : _const_unpartitioned_node_stored_range);
2323 : }
2324 :
2325 :
2326 261755183 : void MeshBase::clear_stored_ranges()
2327 : {
2328 7885365 : _element_stored_range.reset();
2329 7885365 : _const_element_stored_range.reset();
2330 7885365 : _active_local_element_stored_range.reset();
2331 7885365 : _const_active_local_element_stored_range.reset();
2332 7885365 : _local_element_stored_range.reset();
2333 7885365 : _const_local_element_stored_range.reset();
2334 7885365 : _node_stored_range.reset();
2335 7885365 : _const_node_stored_range.reset();
2336 7885365 : _local_node_stored_range.reset();
2337 7885365 : _const_local_node_stored_range.reset();
2338 7885365 : _unpartitioned_node_stored_range.reset();
2339 7885365 : _const_unpartitioned_node_stored_range.reset();
2340 261755183 : }
2341 :
2342 :
2343 : #ifdef LIBMESH_ENABLE_DEPRECATED
2344 0 : void MeshBase::cache_elem_dims()
2345 : {
2346 : libmesh_deprecated();
2347 :
2348 0 : this->cache_elem_data();
2349 0 : }
2350 : #endif // LIBMESH_ENABLE_DEPRECATED
2351 :
2352 1057490 : void MeshBase::cache_elem_data()
2353 : {
2354 : // This requires an inspection on every processor
2355 48922 : parallel_object_only();
2356 :
2357 : // Need to clear containers first in case all elements of a
2358 : // particular dimension/order/subdomain have been deleted.
2359 79872 : _elem_dims.clear();
2360 79872 : _elem_default_orders.clear();
2361 79872 : _mesh_subdomains.clear();
2362 48922 : _mesh_local_subdomains.clear();
2363 1057490 : _supported_nodal_order = MAXIMUM;
2364 :
2365 40965668 : for (const auto & elem : this->active_element_ptr_range())
2366 : {
2367 38899610 : _elem_dims.insert(cast_int<unsigned char>(elem->dim()));
2368 38899610 : _elem_default_orders.insert(elem->default_order());
2369 38899610 : _mesh_subdomains.insert(elem->subdomain_id());
2370 40354338 : if (elem->processor_id() == this->processor_id())
2371 5466092 : _mesh_local_subdomains.insert(elem->subdomain_id());
2372 38899610 : _supported_nodal_order =
2373 38899610 : static_cast<Order>
2374 77799220 : (std::min(static_cast<int>(_supported_nodal_order),
2375 39668667 : static_cast<int>(elem->supported_nodal_order())));
2376 977618 : }
2377 :
2378 : // If the largest element dimension found is larger than the current
2379 : // _spatial_dimension, increase _spatial_dimension.
2380 1057490 : unsigned int max_dim = this->mesh_dimension();
2381 1057490 : if (max_dim > _spatial_dimension)
2382 55850 : _spatial_dimension = cast_int<unsigned char>(max_dim);
2383 :
2384 : // _spatial_dimension may need to increase from 1->2 or 2->3 if the
2385 : // mesh is full of 1D elements but they are not x-aligned, or the
2386 : // mesh is full of 2D elements but they are not in the x-y plane.
2387 : // If the mesh is x-aligned or x-y planar, we will end up checking
2388 : // every node's coordinates and not breaking out of the loop
2389 : // early...
2390 1057490 : if (_spatial_dimension < LIBMESH_DIM)
2391 : {
2392 53758636 : for (const auto & node : this->node_ptr_range())
2393 : {
2394 : // Note: the exact floating point comparison is intentional,
2395 : // we don't want to get tripped up by tolerances.
2396 28067913 : if ((*node)(0) != 0. && _spatial_dimension < 1)
2397 0 : _spatial_dimension = 1;
2398 :
2399 28067913 : if ((*node)(1) != 0. && _spatial_dimension < 2)
2400 : {
2401 2643 : _spatial_dimension = 2;
2402 : #if LIBMESH_DIM == 2
2403 : // If libmesh is compiled in 2D mode, this is the
2404 : // largest spatial dimension possible so we can break
2405 : // out.
2406 : break;
2407 : #endif
2408 : }
2409 :
2410 : #if LIBMESH_DIM > 2
2411 28067913 : if ((*node)(2) != 0.)
2412 : {
2413 : // Spatial dimension can't get any higher than this, so
2414 : // we can break out.
2415 14899 : _spatial_dimension = 3;
2416 14899 : break;
2417 : }
2418 : #endif
2419 524365 : }
2420 : }
2421 :
2422 1057490 : if (!this->is_serial())
2423 : {
2424 : // Some different dimension/order/subdomain elements may only
2425 : // live on other processors
2426 434560 : this->comm().set_union(_elem_dims);
2427 434560 : this->comm().set_union(_elem_default_orders);
2428 434560 : this->comm().min(_supported_nodal_order);
2429 434560 : this->comm().set_union(_mesh_subdomains);
2430 434560 : this->comm().max(_spatial_dimension);
2431 : }
2432 :
2433 1057490 : _preparation.has_cached_elem_data = true;
2434 1057490 : }
2435 :
2436 :
2437 700492 : void MeshBase::sync_subdomain_name_map()
2438 : {
2439 : // This requires every processor
2440 38350 : parallel_object_only();
2441 :
2442 700492 : this->comm().set_union(_block_id_to_name);
2443 :
2444 700492 : _preparation.has_synched_subdomain_name_map = true;
2445 700492 : }
2446 :
2447 :
2448 634900 : void MeshBase::detect_interior_parents()
2449 : {
2450 36344 : LOG_SCOPE("detect_interior_parents()", "MeshBase");
2451 :
2452 : // This requires an inspection on every processor
2453 36190 : parallel_object_only();
2454 :
2455 : // This requires up-to-date mesh dimensions, but if we don't have
2456 : // them cached then we can't update them without changing the mesh
2457 : // in unexpected ways that interfere with our tests of
2458 : // partially-prepared meshes in MeshTools::*valid_is_prepared
2459 36190 : std::set<unsigned char> elem_dims_copy;
2460 634900 : if (_preparation.has_cached_elem_data)
2461 36190 : elem_dims_copy = this->elem_dimensions();
2462 : else
2463 : {
2464 0 : for (const auto & elem : this->active_element_ptr_range())
2465 0 : elem_dims_copy.insert(cast_int<unsigned char>(elem->dim()));
2466 0 : if (!this->is_serial())
2467 0 : this->comm().set_union(elem_dims_copy);
2468 : }
2469 :
2470 : // Early return if the mesh is empty or has elements of a single spatial dimension.
2471 634900 : if (elem_dims_copy.size() <= 1)
2472 : {
2473 627974 : _preparation.has_interior_parent_ptrs = true;
2474 627974 : return;
2475 : }
2476 :
2477 : // Convenient elem_dimensions iterators
2478 6736 : const auto dim_start = elem_dims_copy.begin();
2479 360 : const auto dim_end = elem_dims_copy.end();
2480 :
2481 : // In this function we find only +1 dimensional interior parents,
2482 : // (so, for a given element el, the interior parent p must satisfy p.dim() == el.dim() + 1).
2483 : // Therefore, we can avoid checking the existence of interior parents
2484 : // for all those elements el such there there is no p with p.dim() == el.dim() + 1.
2485 : // We store whether to skip any given dimension in the construction of interior parents
2486 : // inside the vector in dimensions_to_skip_for_interior_parents.
2487 6926 : std::vector<bool> skip_dimension_for_interior_parents(/*count=*/LIBMESH_DIM+1, /*value=*/false);
2488 6736 : skip_dimension_for_interior_parents.back() = true;
2489 :
2490 : // Moreover, in the following, we will build a node-to-elem map.
2491 : // It is among the elems of this map that we will look for interior parents.
2492 : // Therefore, we can skip all elems p such that there is no el with el.dim() == p.dim() - 1.
2493 : // We store whether to skip any given dimension in the construction of the node-to-elem map
2494 : // in the vector skip_dimensions_for_node_to_el_map.
2495 6926 : std::vector<bool> skip_dimensions_for_node_to_el_map(/*count=*/LIBMESH_DIM+1, /*value=*/false);
2496 6926 : skip_dimensions_for_node_to_el_map[*dim_start] = true;
2497 :
2498 : // We also create a flag to know if all dimensions should be skipped,
2499 : // and if we should therefore return early.
2500 360 : bool skip_all_dimensions = true;
2501 :
2502 : // Fill dimensions_to_skip_for_interior_parents and dimensions_to_skip_for_node_to_el_map.
2503 7290 : for (auto [it, next] = std::make_tuple(dim_start, std::next(dim_start));
2504 14358 : next != dim_end; ++it, ++next)
2505 : {
2506 7068 : if (*it + 1 != *next) // if sequential dimensions differ by exactly 1
2507 : {
2508 110 : skip_dimension_for_interior_parents[*it] = true;
2509 1461 : skip_dimensions_for_node_to_el_map[*next] = true;
2510 : }
2511 5761 : else if (!skip_dimension_for_interior_parents[*it])
2512 294 : skip_all_dimensions = false;
2513 : }
2514 :
2515 : // There is nothing to do if all dimensions should be
2516 : // skipped. Before returning, we must also set the flag that says
2517 : // "interior parent pointers have been set up" even though we
2518 : // determined there was no work to be done.
2519 6926 : if (skip_all_dimensions)
2520 : {
2521 1319 : _preparation.has_interior_parent_ptrs = true;
2522 66 : return;
2523 : }
2524 :
2525 : // Do we have interior parent pointers going to a different mesh?
2526 : // If so then we'll still check to make sure that's the only place
2527 : // they go, so we can libmesh_not_implemented() if not.
2528 448 : const bool separate_interior_mesh = (&(this->interior_mesh()) != this);
2529 :
2530 : // This map will be used to set interior parents
2531 294 : std::unordered_map<dof_id_type, std::vector<dof_id_type>> node_to_elem;
2532 :
2533 312030 : for (const auto & elem : this->element_ptr_range())
2534 : {
2535 : // Ignore element if it cannot be interior parent of any other elem.
2536 162601 : if (skip_dimensions_for_node_to_el_map[elem->dim()])
2537 66943 : continue;
2538 :
2539 : // Populating the node_to_elem map, same as MeshTools::build_nodes_to_elem_map
2540 527464 : for (auto n : make_range(elem->n_vertices()))
2541 : {
2542 49968 : libmesh_assert_less (elem->id(), this->max_elem_id());
2543 :
2544 459142 : node_to_elem[elem->node_id(n)].push_back(elem->id());
2545 : }
2546 5159 : }
2547 :
2548 : // Automatically set interior parents
2549 312030 : for (const auto & element : this->element_ptr_range())
2550 : {
2551 : // Ignore elements with dimensions to skip
2552 : // or elements that already have an interior parent.
2553 162601 : if (skip_dimension_for_interior_parents[element->dim()] || element->interior_parent())
2554 33922 : continue;
2555 :
2556 : // Start by generating sets of dim+1 dimensional elements that
2557 : // touch each vertex of the current element. If we encounter a
2558 : // vertex not connected to _any_ dim+1 dimensional elements,
2559 : // then we can exit the loop without checking the remaining
2560 : // vertices since an interior parent (if it exists) will be
2561 : // connected to all vertices of the current element.
2562 148975 : std::vector<std::set<dof_id_type>> neighbors( element->n_vertices() );
2563 :
2564 13454 : bool found_interior_parents = true;
2565 :
2566 131880 : for (auto n : make_range(element->n_vertices()))
2567 : {
2568 137784 : auto it = node_to_elem.find(element->node_id(n));
2569 :
2570 : // Check at first that this node is not isolated.
2571 130880 : if (it == node_to_elem.end())
2572 : {
2573 2768 : found_interior_parents = false;
2574 13420 : break; // out of n-loop
2575 : }
2576 :
2577 387454 : for (const auto & vertex_neighbor_id : it->second)
2578 310013 : if (this->elem_ref(vertex_neighbor_id).dim() == element->dim()+1)
2579 8739 : neighbors[n].insert(vertex_neighbor_id);
2580 :
2581 82871 : if (neighbors[n].empty())
2582 : {
2583 : // We have found an empty set for one vertex, no reason
2584 : // to continue.
2585 10652 : found_interior_parents = false;
2586 10652 : break; // out of n-loop
2587 : }
2588 : }
2589 :
2590 : // If we have generated a non-empty set of elements for each
2591 : // vertex, we will now look for a vertex_neighbor_id that
2592 : // appears in _all_ of those sets. If found, this is our interior
2593 : // parent id. If multiple such common ids are found, we will
2594 : // take the lowest such id to be the interior parent id.
2595 13454 : if (found_interior_parents)
2596 : {
2597 62 : std::set<dof_id_type> & neighbors_0 = neighbors[0];
2598 1645 : for (const auto & interior_parent_id : neighbors_0)
2599 : {
2600 46 : found_interior_parents = false;
2601 2633 : for (auto n : make_range(1u, element->n_vertices()))
2602 : {
2603 1633 : if (neighbors[n].count(interior_parent_id))
2604 : {
2605 34 : found_interior_parents = true;
2606 : }
2607 : else
2608 : {
2609 12 : found_interior_parents = false;
2610 12 : break;
2611 : }
2612 : }
2613 :
2614 1426 : if (found_interior_parents)
2615 : {
2616 781 : element->set_interior_parent(this->elem_ptr(interior_parent_id));
2617 22 : break;
2618 : }
2619 : }
2620 :
2621 : // Do we have a mixed dimensional mesh that contains some of
2622 : // its own interior parents, but we already expect to have
2623 : // interior parents on a different mesh? That's going to
2624 : // take some work to support if anyone needs it.
2625 1000 : if (separate_interior_mesh)
2626 0 : libmesh_not_implemented_msg
2627 : ("interior_parent() values in multiple meshes are unsupported.");
2628 : }
2629 113542 : }
2630 :
2631 : // This flag doesn't necessarily mean any Elems actually have
2632 : // interior parent pointers, just that we did all the work to
2633 : // determine whether or not they do.
2634 5607 : _preparation.has_interior_parent_ptrs = true;
2635 : }
2636 :
2637 :
2638 :
2639 : #ifdef LIBMESH_ENABLE_PERIODIC
2640 : /**
2641 : * Register a pair of boundaries as disjoint neighbor boundary pairs.
2642 : */
2643 1420 : void MeshBase::add_disjoint_neighbor_boundary_pairs(const boundary_id_type b1,
2644 : const boundary_id_type b2,
2645 : const RealVectorValue & translation)
2646 : {
2647 : // Lazily allocate the container the first time it’s needed
2648 1420 : if (!_disjoint_neighbor_boundary_pairs)
2649 2040 : _disjoint_neighbor_boundary_pairs = std::make_unique<PeriodicBoundaries>();
2650 :
2651 40 : PeriodicBoundaries & db = *_disjoint_neighbor_boundary_pairs;
2652 :
2653 : // Create forward and inverse boundary mappings
2654 1460 : PeriodicBoundary forward(translation);
2655 1420 : PeriodicBoundary inverse(translation * -1.0);
2656 :
2657 1420 : forward.myboundary = b1;
2658 1420 : forward.pairedboundary = b2;
2659 1420 : inverse.myboundary = b2;
2660 1420 : inverse.pairedboundary = b1;
2661 :
2662 : // Add both directions into the container
2663 1420 : db.emplace(b1, forward.clone());
2664 1460 : db.emplace(b2, inverse.clone());
2665 1420 : }
2666 :
2667 855425 : PeriodicBoundaries * MeshBase::get_disjoint_neighbor_boundary_pairs()
2668 : {
2669 855425 : return _disjoint_neighbor_boundary_pairs.get();
2670 : }
2671 :
2672 3683032 : const PeriodicBoundaries * MeshBase::get_disjoint_neighbor_boundary_pairs() const
2673 : {
2674 3683032 : return _disjoint_neighbor_boundary_pairs.get();
2675 : }
2676 :
2677 213 : void MeshBase::remove_disjoint_boundary_pair(const boundary_id_type b1,
2678 : const boundary_id_type b2)
2679 : {
2680 : // Nothing to remove if not allocated or empty
2681 213 : if (!_disjoint_neighbor_boundary_pairs || _disjoint_neighbor_boundary_pairs->empty())
2682 0 : return;
2683 :
2684 6 : auto & pairs = *_disjoint_neighbor_boundary_pairs;
2685 :
2686 : // Helper to check and erase both directions
2687 402 : auto erase_if_match = [](boundary_id_type key,
2688 : boundary_id_type pair,
2689 24 : PeriodicBoundaries & pb_map)
2690 : {
2691 12 : auto it = pb_map.find(key);
2692 426 : if (it != pb_map.end())
2693 : {
2694 12 : const auto & pb = *(it->second);
2695 : // Check both directions
2696 426 : if ((pb.myboundary == key && pb.pairedboundary == pair) ||
2697 0 : (pb.pairedboundary == key && pb.myboundary == pair))
2698 414 : pb_map.erase(it);
2699 : }
2700 426 : };
2701 :
2702 213 : erase_if_match(b1, b2, pairs);
2703 213 : erase_if_match(b2, b1, pairs);
2704 : }
2705 :
2706 :
2707 : #endif
2708 :
2709 :
2710 :
2711 0 : void MeshBase::set_point_locator_close_to_point_tol(Real val)
2712 : {
2713 0 : _point_locator_close_to_point_tol = val;
2714 0 : if (_point_locator)
2715 : {
2716 0 : if (val > 0.)
2717 0 : _point_locator->set_close_to_point_tol(val);
2718 : else
2719 0 : _point_locator->unset_close_to_point_tol();
2720 : }
2721 0 : }
2722 :
2723 :
2724 :
2725 426 : Real MeshBase::get_point_locator_close_to_point_tol() const
2726 : {
2727 426 : return _point_locator_close_to_point_tol;
2728 : }
2729 :
2730 :
2731 :
2732 5471 : void MeshBase::size_elem_extra_integers()
2733 : {
2734 444 : const std::size_t new_size = _elem_integer_names.size();
2735 :
2736 : Threads::parallel_for_each
2737 5471 : (this->element_stored_range(),
2738 13687 : [new_size, this](Elem * elem)
2739 : {
2740 13305 : elem->add_extra_integers(new_size, this->_elem_integer_default_values);
2741 382 : });
2742 5471 : }
2743 :
2744 :
2745 :
2746 21418 : void MeshBase::size_node_extra_integers()
2747 : {
2748 1494 : const std::size_t new_size = _node_integer_names.size();
2749 : Threads::parallel_for_each
2750 21418 : (this->node_stored_range(),
2751 209324 : [this, new_size](Node * node)
2752 : {
2753 200738 : node->add_extra_integers(new_size, _node_integer_default_values);
2754 8586 : });
2755 21418 : }
2756 :
2757 :
2758 : std::pair<std::vector<unsigned int>, std::vector<unsigned int>>
2759 43930 : MeshBase::merge_extra_integer_names(const MeshBase & other)
2760 : {
2761 18478 : std::pair<std::vector<unsigned int>, std::vector<unsigned int>> returnval;
2762 43930 : returnval.first = this->add_elem_integers(other._elem_integer_names, true, &other._elem_integer_default_values);
2763 43930 : returnval.second = this->add_node_integers(other._node_integer_names, true, &other._node_integer_default_values);
2764 43930 : return returnval;
2765 : }
2766 :
2767 :
2768 :
2769 : void
2770 426 : MeshBase::post_dofobject_moves(MeshBase && other_mesh)
2771 : {
2772 : // Now that all the DofObject moving is done, we can move the GhostingFunctor objects
2773 : // which include the _default_ghosting,_ghosting_functors and _shared_functors. We also need
2774 : // to set the mesh object associated with these functors to the assignee mesh.
2775 :
2776 : // _default_ghosting
2777 12 : _default_ghosting = std::move(other_mesh._default_ghosting);
2778 426 : _default_ghosting->set_mesh(this);
2779 :
2780 : // _ghosting_functors
2781 426 : _ghosting_functors = std::move(other_mesh._ghosting_functors);
2782 :
2783 852 : for (const auto gf : _ghosting_functors )
2784 : {
2785 426 : gf->set_mesh(this);
2786 : }
2787 :
2788 : // _shared_functors
2789 12 : _shared_functors = std::move(other_mesh._shared_functors);
2790 :
2791 426 : for (const auto & sf : _shared_functors )
2792 : {
2793 0 : (sf.second)->set_mesh(this);
2794 : }
2795 :
2796 : // _constraint_rows
2797 12 : _constraint_rows = std::move(other_mesh._constraint_rows);
2798 426 : _n_constraint_rows = other_mesh._n_constraint_rows;
2799 :
2800 426 : if (other_mesh.partitioner())
2801 426 : _partitioner = std::move(other_mesh.partitioner());
2802 426 : }
2803 :
2804 :
2805 : void
2806 0 : MeshBase::copy_cached_data(const MeshBase & other_mesh)
2807 : {
2808 0 : this->_spatial_dimension = other_mesh._spatial_dimension;
2809 0 : this->_elem_dims = other_mesh._elem_dims;
2810 0 : this->_elem_default_orders = other_mesh._elem_default_orders;
2811 0 : this->_supported_nodal_order = other_mesh._supported_nodal_order;
2812 0 : this->_mesh_subdomains = other_mesh._mesh_subdomains;
2813 0 : this->_mesh_local_subdomains = other_mesh._mesh_local_subdomains;
2814 0 : }
2815 :
2816 :
2817 49963 : bool MeshBase::nodes_and_elements_equal(const MeshBase & other_mesh) const
2818 : {
2819 8227296 : for (const auto & other_node : other_mesh.node_ptr_range())
2820 : {
2821 6889389 : const Node * node = this->query_node_ptr(other_node->id());
2822 6889389 : if (!node)
2823 0 : return false;
2824 6889389 : if (*other_node != *node)
2825 0 : return false;
2826 13279 : }
2827 8227296 : for (const auto & node : this->node_ptr_range())
2828 6889389 : if (!other_mesh.query_node_ptr(node->id()))
2829 13279 : return false;
2830 :
2831 4054856 : for (const auto & other_elem : other_mesh.element_ptr_range())
2832 : {
2833 3555479 : const Elem * elem = this->query_elem_ptr(other_elem->id());
2834 3555479 : if (!elem)
2835 0 : return false;
2836 3555479 : if (!other_elem->topologically_equal(*elem))
2837 6 : return false;
2838 13279 : }
2839 4054580 : for (const auto & elem : this->element_ptr_range())
2840 3555338 : if (!other_mesh.query_elem_ptr(elem->id()))
2841 13150 : return false;
2842 :
2843 49822 : return true;
2844 : }
2845 :
2846 :
2847 4569301 : dof_id_type MeshBase::count_constraint_rows()
2848 : {
2849 4569301 : dof_id_type n_local_rows=0, n_unpartitioned_rows=0;
2850 6509046 : for (const auto & [node, node_constraints] : _constraint_rows)
2851 : {
2852 : // Unpartitioned nodes
2853 1939745 : if (node->processor_id() == DofObject::invalid_processor_id)
2854 1215490 : n_unpartitioned_rows++;
2855 737425 : else if (node->processor_id() == this->processor_id())
2856 200700 : n_local_rows++;
2857 : }
2858 :
2859 4569301 : this->comm().sum(n_local_rows);
2860 :
2861 4569301 : return n_unpartitioned_rows + n_local_rows;
2862 : }
2863 :
2864 :
2865 : void
2866 42155 : MeshBase::copy_constraint_rows(const MeshBase & other_mesh)
2867 : {
2868 37648 : LOG_SCOPE("copy_constraint_rows(mesh)", "MeshBase");
2869 :
2870 42155 : _n_constraint_rows = other_mesh._n_constraint_rows;
2871 :
2872 19220 : _constraint_rows.clear();
2873 :
2874 18428 : const auto & other_constraint_rows = other_mesh.get_constraint_rows();
2875 102775 : for (const auto & [other_node, other_node_constraints] : other_constraint_rows)
2876 : {
2877 60620 : const Node * const our_node = this->node_ptr(other_node->id());
2878 30800 : constraint_rows_mapped_type our_node_constraints;
2879 259801 : for (const auto & [other_inner_key_pair, constraint_value] : other_node_constraints)
2880 : {
2881 37020 : const auto & [other_elem, local_node_id] = other_inner_key_pair;
2882 199181 : const Elem * const our_elem = this->elem_ptr(other_elem->id());
2883 199181 : our_node_constraints.emplace_back(std::make_pair(our_elem, local_node_id), constraint_value);
2884 : }
2885 60620 : _constraint_rows[our_node] = std::move(our_node_constraints);
2886 : }
2887 42155 : }
2888 :
2889 :
2890 : template <typename T>
2891 : void
2892 636 : MeshBase::copy_constraint_rows(const SparseMatrix<T> & constraint_operator,
2893 : bool precondition_constraint_operator)
2894 : {
2895 54 : LOG_SCOPE("copy_constraint_rows(mat)", "MeshBase");
2896 :
2897 18 : this->_constraint_rows.clear();
2898 :
2899 : // We're not going to support doing this distributed yet; it'd be
2900 : // pointless unless we temporarily had a linear partitioning to
2901 : // better match the constraint operator.
2902 672 : MeshSerializer serialize(*this);
2903 :
2904 : // Our current mesh should already reflect the desired assembly space
2905 636 : libmesh_error_msg_if(this->n_nodes() != constraint_operator.m(),
2906 : "Constraint operator matrix with " <<
2907 : constraint_operator.m() <<
2908 : "rows does not match this mesh with " <<
2909 : this->n_nodes() << " nodes");
2910 :
2911 : // First, find what new unconstrained DoFs we need to add. We can't
2912 : // iterate over columns in a SparseMatrix, so we'll iterate over
2913 : // rows and keep track of columns.
2914 :
2915 : // If we have nodes that will work unconstrained, keep track of
2916 : // their node ids and corresponding column indices.
2917 : // existing_unconstrained_nodes[column_id] = node_id
2918 36 : std::map<dof_id_type, dof_id_type> existing_unconstrained_columns;
2919 36 : std::set<dof_id_type> existing_unconstrained_nodes;
2920 :
2921 : // In case we need new nodes, keep track of their columns.
2922 : // columns[j][k] will be the kth row index and value of column j
2923 : typedef
2924 : std::unordered_map<dof_id_type,
2925 : std::vector<std::pair<dof_id_type, Real>>>
2926 : columns_type;
2927 1254 : columns_type columns(constraint_operator.n());
2928 :
2929 : // If we need to precondition the constraint operator (e.g. it's an
2930 : // unpreconditioned extraction operator for a Flex IGA matrix),
2931 : // we'll want to keep track of the sum of each column, because we'll
2932 : // be dividing each column by that sum (Jacobi preconditioning on
2933 : // the right, which then leads to symmetric preconditioning on a
2934 : // physics Jacobian).
2935 36 : std::unordered_map<dof_id_type, Real> column_sums;
2936 :
2937 : // Work in parallel, though we'll have to sync shortly
2938 4231 : for (auto i : make_range(constraint_operator.row_start(),
2939 600 : constraint_operator.row_stop()))
2940 : {
2941 640 : std::vector<numeric_index_type> indices;
2942 640 : std::vector<T> values;
2943 :
2944 3595 : constraint_operator.get_row(i, indices, values);
2945 320 : libmesh_assert_equal_to(indices.size(), values.size());
2946 :
2947 3995 : if (indices.size() == 1 &&
2948 880 : values[0] == T(1))
2949 : {
2950 : // If we have multiple simple Ui=Uj constraints, let the
2951 : // first one be our "unconstrained" node and let the others
2952 : // be constrained to it.
2953 870 : if (existing_unconstrained_columns.find(indices[0]) !=
2954 148 : existing_unconstrained_columns.end())
2955 : {
2956 36 : const auto j = indices[0];
2957 36 : columns[j].emplace_back(i, 1);
2958 : }
2959 : else
2960 : {
2961 763 : existing_unconstrained_nodes.insert(i);
2962 834 : existing_unconstrained_columns.emplace(indices[0],i);
2963 : }
2964 : }
2965 : else
2966 22244 : for (auto jj : index_range(indices))
2967 : {
2968 19519 : const auto j = indices[jj];
2969 21290 : const Real coef = libmesh_real(values[jj]);
2970 1771 : libmesh_assert_equal_to(coef, values[jj]);
2971 19519 : columns[j].emplace_back(i, coef);
2972 : }
2973 : }
2974 :
2975 : // Merge data from different processors' slabs of the matrix
2976 636 : this->comm().set_union(existing_unconstrained_nodes);
2977 636 : this->comm().set_union(existing_unconstrained_columns);
2978 :
2979 54 : std::vector<columns_type> all_columns;
2980 636 : this->comm().allgather(columns, all_columns);
2981 :
2982 18 : columns.clear();
2983 6870 : for (auto p : index_range(all_columns))
2984 66720 : for (auto & [j, subcol] : all_columns[p])
2985 184707 : for (auto [i, v] : subcol)
2986 124221 : columns[j].emplace_back(i,v);
2987 :
2988 : // Keep track of elements on which unconstrained nodes exist, and
2989 : // their local node indices.
2990 : // node_to_elem_ptrs[node] = [elem_id, local_node_num]
2991 36 : std::unordered_map<const Node *, std::pair<dof_id_type, unsigned int>> node_to_elem_ptrs;
2992 :
2993 : // Find elements attached to any existing nodes that will stay
2994 : // unconstrained. We'll also build a subdomain set here so we don't
2995 : // have to assert that the mesh is already prepared before we pick a
2996 : // new subdomain for any NodeElems we need to add.
2997 36 : std::set<subdomain_id_type> subdomain_ids;
2998 147438 : for (const Elem * elem : this->element_ptr_range())
2999 : {
3000 50448 : subdomain_ids.insert(elem->subdomain_id());
3001 246702 : for (auto n : make_range(elem->n_nodes()))
3002 : {
3003 196254 : const Node * node = elem->node_ptr(n);
3004 196254 : if (existing_unconstrained_nodes.count(node->id()))
3005 12814 : node_to_elem_ptrs.emplace(node, std::make_pair(elem->id(), n));
3006 : }
3007 : }
3008 :
3009 636 : const subdomain_id_type new_sbd_id = *subdomain_ids.rbegin() + 1;
3010 :
3011 : // If we're on a DistributedMesh, even when we're serialized the
3012 : // mesh may try to add new objects using the sparse id rule designed
3013 : // for distributed additions. Manually choosing object ids will
3014 : // avoid creating non-contiguous ids unnecessarily.
3015 636 : dof_id_type next_node_id = this->max_node_id();
3016 636 : dof_id_type next_elem_id = this->max_elem_id();
3017 :
3018 : // Make sure we don't screw up contiguous numbering of an input
3019 : #ifndef NDEBUG
3020 18 : const bool mesh_was_contiguous =
3021 36 : this->max_elem_id() == this->n_elem() &&
3022 18 : this->max_node_id() == this->n_nodes();
3023 : #endif
3024 :
3025 23753 : for (auto j : make_range(constraint_operator.n()))
3026 : {
3027 : // If we already have a good node for this then we're done
3028 5147 : if (existing_unconstrained_columns.count(j))
3029 5023 : continue;
3030 :
3031 : // Get a half-decent spot to place a new NodeElem for
3032 : // unconstrained DoF(s) here. Getting a *fully*-decent spot
3033 : // would require finding a Moore-Penrose pseudoinverse, and I'm
3034 : // not going to do that, but scaling a transpose will at least
3035 : // get us a little uniqueness to make visualization reasonable.
3036 266 : Point newpt;
3037 266 : Real total_scaling = 0;
3038 266 : unsigned int total_entries = 0;
3039 :
3040 : // We'll get a decent initial pid choice here too, if only to
3041 : // aid in later repartitioning.
3042 532 : std::map<processor_id_type, int> pids;
3043 :
3044 266 : auto & column = columns[j];
3045 122443 : for (auto [i, r] : column)
3046 : {
3047 113130 : Node & constrained_node = this->node_ref(i);
3048 113130 : const Point constrained_pt = constrained_node;
3049 3232 : newpt += r*constrained_pt;
3050 113130 : total_scaling += r;
3051 113130 : ++total_entries;
3052 113130 : ++pids[constrained_node.processor_id()];
3053 : }
3054 :
3055 9313 : if (precondition_constraint_operator)
3056 0 : column_sums[j] = total_scaling;
3057 :
3058 9313 : libmesh_error_msg_if
3059 : (!total_entries,
3060 : "Empty column " << j <<
3061 : " found in constraint operator matrix");
3062 :
3063 : // If we have *cancellation* here then we can end up dividing by
3064 : // zero; try just evenly scaling across all constrained node
3065 : // points instead.
3066 9313 : if (total_scaling > TOLERANCE)
3067 266 : newpt /= total_scaling;
3068 : else
3069 0 : newpt /= total_entries;
3070 :
3071 9313 : Node *n = this->add_point(newpt, next_node_id++);
3072 9579 : std::unique_ptr<Elem> elem = Elem::build(NODEELEM);
3073 9313 : elem->set_node(0, n);
3074 9313 : elem->subdomain_id() = new_sbd_id;
3075 9313 : elem->set_id(next_elem_id++);
3076 :
3077 9579 : Elem * added_elem = this->add_elem(std::move(elem));
3078 9313 : this->_elem_dims.insert(0);
3079 9313 : this->_elem_default_orders.insert(added_elem->default_order());
3080 9313 : this->_supported_nodal_order =
3081 8781 : static_cast<Order>
3082 18626 : (std::min(static_cast<int>(this->_supported_nodal_order),
3083 9313 : static_cast<int>(added_elem->supported_nodal_order())));
3084 9047 : this->_mesh_subdomains.insert(new_sbd_id);
3085 9313 : node_to_elem_ptrs.emplace(n, std::make_pair(added_elem->id(), 0));
3086 9313 : existing_unconstrained_columns.emplace(j,n->id());
3087 :
3088 : // Repartition the new objects *after* adding them, so a
3089 : // DistributedMesh doesn't get confused and think you're not
3090 : // adding them on all processors at once.
3091 266 : int n_pids = 0;
3092 266 : processor_id_type best_pid = DofObject::invalid_processor_id;
3093 44054 : for (auto [pid, count] : pids)
3094 34741 : if (count >= n_pids)
3095 : {
3096 326 : n_pids = count;
3097 326 : best_pid = pid;
3098 : }
3099 266 : libmesh_assert_not_equal_to(best_pid, DofObject::invalid_processor_id);
3100 9313 : added_elem->processor_id() = best_pid;
3101 9313 : n->processor_id() = best_pid;
3102 9579 : if (this->processor_id() == best_pid)
3103 1333 : this->_mesh_local_subdomains.insert(new_sbd_id);
3104 : }
3105 :
3106 : // Calculate constraint rows in an indexed form that's easy for us
3107 : // to allgather
3108 : std::unordered_map<dof_id_type,
3109 : std::vector<std::pair<std::pair<dof_id_type, unsigned int>,Real>>>
3110 36 : indexed_constraint_rows;
3111 :
3112 4231 : for (auto i : make_range(constraint_operator.row_start(),
3113 600 : constraint_operator.row_stop()))
3114 : {
3115 1012 : if (existing_unconstrained_nodes.count(i))
3116 834 : continue;
3117 :
3118 498 : std::vector<numeric_index_type> indices;
3119 498 : std::vector<T> values;
3120 :
3121 2761 : constraint_operator.get_row(i, indices, values);
3122 :
3123 498 : std::vector<std::pair<std::pair<dof_id_type, unsigned int>, Real>> constraint_row;
3124 :
3125 22316 : for (auto jj : index_range(indices))
3126 : {
3127 21329 : const dof_id_type node_id =
3128 : existing_unconstrained_columns[indices[jj]];
3129 :
3130 19555 : Node & constraining_node = this->node_ref(node_id);
3131 :
3132 1774 : libmesh_assert(node_to_elem_ptrs.count(&constraining_node));
3133 :
3134 19555 : auto p = node_to_elem_ptrs[&constraining_node];
3135 :
3136 19514 : Real coef = libmesh_real(values[jj]);
3137 1774 : libmesh_assert_equal_to(coef, values[jj]);
3138 :
3139 : // If we're preconditioning and we created a nodeelem then
3140 : // we can scale the meaning of that nodeelem's value to give
3141 : // us a better-conditioned matrix after the constraints are
3142 : // applied.
3143 19555 : if (precondition_constraint_operator)
3144 0 : if (auto sum_it = column_sums.find(indices[jj]);
3145 0 : sum_it != column_sums.end())
3146 : {
3147 0 : const Real scaling = sum_it->second;
3148 :
3149 0 : if (scaling > TOLERANCE)
3150 0 : coef /= scaling;
3151 : }
3152 :
3153 21329 : constraint_row.emplace_back(std::make_pair(p, coef));
3154 : }
3155 :
3156 249 : indexed_constraint_rows.emplace(i, std::move(constraint_row));
3157 : }
3158 :
3159 636 : this->comm().set_union(indexed_constraint_rows);
3160 :
3161 : // Add constraint rows as mesh constraint rows
3162 18088 : for (auto & [node_id, indexed_row] : indexed_constraint_rows)
3163 : {
3164 17452 : Node * constrained_node = this->node_ptr(node_id);
3165 :
3166 996 : constraint_rows_mapped_type constraint_row;
3167 :
3168 141673 : for (auto [p, coef] : indexed_row)
3169 : {
3170 124221 : const Elem * elem = this->elem_ptr(p.first);
3171 7096 : constraint_row.emplace_back
3172 127769 : (std::make_pair(std::make_pair(elem, p.second), coef));
3173 : }
3174 :
3175 16954 : this->_constraint_rows.emplace(constrained_node,
3176 498 : std::move(constraint_row));
3177 : }
3178 :
3179 636 : _n_constraint_rows = this->count_constraint_rows();
3180 :
3181 : // Make sure we didn't screw up contiguous numbering of an input
3182 18 : libmesh_assert(!mesh_was_contiguous ||
3183 : (this->max_elem_id() == this->n_elem() &&
3184 : this->max_node_id() == this->n_nodes()));
3185 1236 : }
3186 :
3187 :
3188 0 : void MeshBase::print_constraint_rows(std::ostream & os,
3189 : bool print_nonlocal) const
3190 : {
3191 0 : parallel_object_only();
3192 :
3193 : std::string local_constraints =
3194 0 : this->get_local_constraints(print_nonlocal);
3195 :
3196 0 : if (this->processor_id())
3197 : {
3198 0 : this->comm().send(0, local_constraints);
3199 : }
3200 : else
3201 : {
3202 0 : os << "Processor 0:\n";
3203 0 : os << local_constraints;
3204 :
3205 0 : for (auto p : IntRange<processor_id_type>(1, this->n_processors()))
3206 : {
3207 0 : this->comm().receive(p, local_constraints);
3208 0 : os << "Processor " << p << ":\n";
3209 0 : os << local_constraints;
3210 : }
3211 : }
3212 0 : }
3213 :
3214 :
3215 :
3216 0 : std::string MeshBase::get_local_constraints(bool print_nonlocal) const
3217 : {
3218 0 : std::ostringstream os;
3219 :
3220 0 : if (print_nonlocal)
3221 0 : os << "All ";
3222 : else
3223 0 : os << "Local ";
3224 :
3225 0 : os << "Mesh Constraint Rows:"
3226 0 : << std::endl;
3227 :
3228 0 : for (const auto & [node, row] : _constraint_rows)
3229 : {
3230 0 : const bool local = (node->processor_id() == this->processor_id());
3231 :
3232 : // Skip non-local dofs if requested
3233 0 : if (!print_nonlocal && !local)
3234 0 : continue;
3235 :
3236 0 : os << "Constraints for " << (local ? "Local" : "Ghost") << " Node " << node->id()
3237 0 : << ": \t";
3238 :
3239 0 : for (const auto & [elem_and_node, coef] : row)
3240 0 : os << " ((" << elem_and_node.first->id() << ',' << elem_and_node.second << "), " << coef << ")\t";
3241 :
3242 0 : os << std::endl;
3243 : }
3244 :
3245 0 : return os.str();
3246 0 : }
3247 :
3248 563251 : MeshBase::Preparation::Preparation() :
3249 470937 : is_partitioned(false),
3250 470937 : has_synched_id_counts(false),
3251 470937 : has_neighbor_ptrs(false),
3252 470937 : has_cached_elem_data(false),
3253 470937 : has_interior_parent_ptrs(false),
3254 470937 : has_removed_remote_elements(false),
3255 470937 : has_removed_orphaned_nodes(false),
3256 470937 : has_boundary_id_sets(false),
3257 470937 : has_reinit_ghosting_functors(false),
3258 563251 : has_synched_subdomain_name_map(false)
3259 563251 : {}
3260 :
3261 929268 : MeshBase::Preparation::operator bool() const
3262 : {
3263 1817641 : return is_partitioned &&
3264 888373 : has_synched_id_counts &&
3265 888373 : has_neighbor_ptrs &&
3266 888373 : has_cached_elem_data &&
3267 888367 : has_interior_parent_ptrs &&
3268 888367 : has_removed_remote_elements &&
3269 888367 : has_removed_orphaned_nodes &&
3270 888367 : has_reinit_ghosting_functors &&
3271 1950545 : has_boundary_id_sets &&
3272 1022475 : has_synched_subdomain_name_map;
3273 : }
3274 :
3275 : MeshBase::Preparation &
3276 2172458 : MeshBase::Preparation::operator= (bool set_all)
3277 : {
3278 2172458 : is_partitioned = set_all;
3279 2172458 : has_synched_id_counts = set_all;
3280 2172458 : has_neighbor_ptrs = set_all;
3281 2172458 : has_cached_elem_data = set_all;
3282 2172458 : has_interior_parent_ptrs = set_all;
3283 2172458 : has_removed_remote_elements = set_all;
3284 2172458 : has_removed_orphaned_nodes = set_all;
3285 2172458 : has_reinit_ghosting_functors = set_all;
3286 2172458 : has_boundary_id_sets = set_all;
3287 2172458 : has_synched_subdomain_name_map = set_all;
3288 :
3289 2172458 : return *this;
3290 : }
3291 :
3292 : bool
3293 115201 : MeshBase::Preparation::operator== (const Preparation & other) const
3294 : {
3295 115201 : if (is_partitioned != other.is_partitioned)
3296 26892 : return false;
3297 86587 : if (has_synched_id_counts != other.has_synched_id_counts)
3298 6 : return false;
3299 86581 : if (has_neighbor_ptrs != other.has_neighbor_ptrs)
3300 388 : return false;
3301 86193 : if (has_cached_elem_data != other.has_cached_elem_data)
3302 0 : return false;
3303 86193 : if (has_interior_parent_ptrs != other.has_interior_parent_ptrs)
3304 0 : return false;
3305 86193 : if (has_removed_remote_elements != other.has_removed_remote_elements)
3306 18190 : return false;
3307 68003 : if (has_removed_orphaned_nodes != other.has_removed_orphaned_nodes)
3308 0 : return false;
3309 68003 : if (has_reinit_ghosting_functors != other.has_reinit_ghosting_functors)
3310 0 : return false;
3311 68003 : if (has_boundary_id_sets != other.has_boundary_id_sets)
3312 0 : return false;
3313 68003 : if (has_synched_subdomain_name_map != other.has_synched_subdomain_name_map)
3314 0 : return false;
3315 :
3316 53914 : return true;
3317 : }
3318 :
3319 : bool
3320 50389 : MeshBase::Preparation::operator!= (const Preparation & other) const
3321 : {
3322 50389 : return !(*this == other);
3323 : }
3324 :
3325 :
3326 : void
3327 962586 : MeshBase::Preparation::libmesh_assert_consistent (const Parallel::Communicator & libmesh_dbg_var(comm))
3328 : {
3329 45402 : libmesh_assert(comm.verify(is_partitioned));
3330 45402 : libmesh_assert(comm.verify(has_synched_id_counts));
3331 45402 : libmesh_assert(comm.verify(has_neighbor_ptrs));
3332 45402 : libmesh_assert(comm.verify(has_cached_elem_data));
3333 45402 : libmesh_assert(comm.verify(has_interior_parent_ptrs));
3334 45402 : libmesh_assert(comm.verify(has_removed_remote_elements));
3335 45402 : libmesh_assert(comm.verify(has_removed_orphaned_nodes));
3336 45402 : libmesh_assert(comm.verify(has_reinit_ghosting_functors));
3337 45402 : libmesh_assert(comm.verify(has_boundary_id_sets));
3338 962586 : }
3339 :
3340 :
3341 : // Explicit instantiations for our template function
3342 : template LIBMESH_EXPORT void
3343 : MeshBase::copy_constraint_rows(const SparseMatrix<Real> & constraint_operator,
3344 : bool precondition_constraint_operator);
3345 :
3346 : #ifdef LIBMESH_USE_COMPLEX_NUMBERS
3347 : template LIBMESH_EXPORT void
3348 : MeshBase::copy_constraint_rows(const SparseMatrix<Complex> & constraint_operator,
3349 : bool precondition_constraint_operator);
3350 : #endif
3351 :
3352 :
3353 : } // namespace libMesh
|