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