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 130026 : const_shim(const MeshBase & mesh,
63 : const Range & (MeshBase::*range_func)(),
64 : std::unique_ptr<ConstRange> & shim_range)
65 : {
66 130026 : if (!shim_range)
67 : {
68 26281 : MeshBase & evil = const_cast<MeshBase &>(mesh);
69 83903 : const Range & range = (evil.*range_func)();
70 :
71 115400 : shim_range =
72 : std::make_unique<ConstRange>
73 141681 : (range.begin(), range.end(), range.grainsize());
74 : }
75 :
76 130026 : return *shim_range;
77 : }
78 : }
79 :
80 : namespace libMesh
81 : {
82 :
83 : // ------------------------------------------------------------
84 : // MeshBase class member functions
85 53076 : MeshBase::MeshBase (const Parallel::Communicator & comm_in,
86 53076 : unsigned char d) :
87 : ParallelObject (comm_in),
88 53076 : boundary_info (new BoundaryInfo(*this)), // BoundaryInfo has protected ctor, can't use std::make_unique
89 22308 : _n_parts (1),
90 22308 : _default_mapping_type(LAGRANGE_MAP),
91 22308 : _default_mapping_data(0),
92 22308 : _preparation (),
93 22308 : _element_stored_range (),
94 22308 : _const_element_stored_range (),
95 22308 : _active_local_element_stored_range (),
96 22308 : _const_active_local_element_stored_range (),
97 22308 : _local_element_stored_range (),
98 22308 : _const_local_element_stored_range (),
99 22308 : _node_stored_range (),
100 22308 : _const_node_stored_range (),
101 22308 : _local_node_stored_range (),
102 22308 : _const_local_node_stored_range (),
103 22308 : _unpartitioned_node_stored_range (),
104 22308 : _const_unpartitioned_node_stored_range (),
105 22308 : _point_locator (),
106 22308 : _count_lower_dim_elems_in_point_locator(true),
107 22308 : _partitioner (),
108 : #ifdef LIBMESH_ENABLE_UNIQUE_ID
109 22308 : _next_unique_id(DofObject::invalid_unique_id),
110 : #endif
111 22308 : _interior_mesh(this),
112 22308 : _skip_noncritical_partitioning(false),
113 68635 : _skip_all_partitioning(libMesh::on_command_line("--skip-partitioning")),
114 22308 : _skip_renumber_nodes_and_elements(false),
115 22308 : _skip_find_neighbors(false),
116 22308 : _skip_detect_interior_parents(false),
117 22308 : _allow_remote_element_removal(true),
118 22308 : _allow_node_and_elem_unique_id_overlap(false),
119 22308 : _is_redistributing(false),
120 22308 : _spatial_dimension(d),
121 53076 : _default_ghosting(std::make_unique<GhostPointNeighbors>(*this)),
122 22308 : _n_constraint_rows(0),
123 235973 : _point_locator_close_to_point_tol(0.)
124 : {
125 37867 : _elem_dims.insert(d);
126 53076 : _ghosting_functors.push_back(_default_ghosting.get());
127 15559 : libmesh_assert_less_equal (LIBMESH_DIM, 3);
128 15559 : libmesh_assert_greater_equal (LIBMESH_DIM, d);
129 15559 : libmesh_assert (libMesh::initialized());
130 53076 : }
131 :
132 :
133 :
134 21119 : MeshBase::MeshBase (const MeshBase & other_mesh) :
135 : ParallelObject (other_mesh),
136 21119 : boundary_info (new BoundaryInfo(*this)), // BoundaryInfo has protected ctor, can't use std::make_unique
137 21119 : _n_parts (other_mesh._n_parts),
138 21119 : _default_mapping_type(other_mesh._default_mapping_type),
139 21119 : _default_mapping_data(other_mesh._default_mapping_data),
140 1047 : _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 1047 : _element_stored_range (),
144 1047 : _const_element_stored_range (),
145 1047 : _active_local_element_stored_range (),
146 1047 : _const_active_local_element_stored_range (),
147 1047 : _local_element_stored_range (),
148 1047 : _const_local_element_stored_range (),
149 1047 : _node_stored_range (),
150 1047 : _const_node_stored_range (),
151 1047 : _local_node_stored_range (),
152 1047 : _const_local_node_stored_range (),
153 1047 : _unpartitioned_node_stored_range (),
154 1047 : _const_unpartitioned_node_stored_range (),
155 1047 : _point_locator (),
156 21119 : _count_lower_dim_elems_in_point_locator(other_mesh._count_lower_dim_elems_in_point_locator),
157 1047 : _partitioner (),
158 : #ifdef LIBMESH_ENABLE_UNIQUE_ID
159 21119 : _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 21119 : _interior_mesh((other_mesh._interior_mesh == &other_mesh) ?
164 : this : other_mesh._interior_mesh),
165 21119 : _skip_noncritical_partitioning(other_mesh._skip_noncritical_partitioning),
166 21119 : _skip_all_partitioning(other_mesh._skip_all_partitioning),
167 21119 : _skip_renumber_nodes_and_elements(other_mesh._skip_renumber_nodes_and_elements),
168 21119 : _skip_find_neighbors(other_mesh._skip_find_neighbors),
169 21119 : _skip_detect_interior_parents(other_mesh._skip_detect_interior_parents),
170 21119 : _allow_remote_element_removal(other_mesh._allow_remote_element_removal),
171 21119 : _allow_node_and_elem_unique_id_overlap(other_mesh._allow_node_and_elem_unique_id_overlap),
172 1047 : _is_redistributing(false),
173 19280 : _elem_dims(other_mesh._elem_dims),
174 19280 : _elem_default_orders(other_mesh._elem_default_orders),
175 21119 : _supported_nodal_order(other_mesh._supported_nodal_order),
176 19280 : _mesh_subdomains(other_mesh._mesh_subdomains),
177 19280 : _elemset_codes_inverse_map(other_mesh._elemset_codes_inverse_map),
178 19280 : _all_elemset_ids(other_mesh._all_elemset_ids),
179 21119 : _spatial_dimension(other_mesh._spatial_dimension),
180 21119 : _default_ghosting(std::make_unique<GhostPointNeighbors>(*this)),
181 1047 : _n_constraint_rows(0),
182 86852 : _point_locator_close_to_point_tol(other_mesh._point_locator_close_to_point_tol)
183 : {
184 19280 : const GhostingFunctor * const other_default_ghosting = other_mesh._default_ghosting.get();
185 :
186 48942 : 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 27823 : if (gf == other_default_ghosting)
191 : {
192 21119 : _ghosting_functors.push_back(_default_ghosting.get());
193 21119 : continue;
194 : }
195 :
196 12400 : 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 6704 : if (clone_gf)
204 : {
205 6704 : clone_gf->set_mesh(this);
206 8964 : add_ghosting_functor(clone_gf);
207 : }
208 : else
209 : {
210 : libmesh_deprecated();
211 0 : add_ghosting_functor(*gf);
212 : }
213 : }
214 :
215 21119 : if (other_mesh._partitioner.get())
216 22816 : _partitioner = other_mesh._partitioner->clone();
217 :
218 : #ifdef LIBMESH_ENABLE_PERIODIC
219 : // Deep copy of all periodic boundaries
220 21119 : if (other_mesh._disjoint_neighbor_boundary_pairs)
221 : {
222 36 : _disjoint_neighbor_boundary_pairs = std::make_unique<PeriodicBoundaries>();
223 :
224 113 : for (const auto & [id, pb] : *other_mesh._disjoint_neighbor_boundary_pairs)
225 78 : if (pb)
226 88 : (*_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 21131 : for (const auto & [set, code] : _elemset_codes_inverse_map)
233 12 : _elemset_codes.emplace(code, &set);
234 21119 : }
235 :
236 42 : 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 42 : this->clear_stored_ranges();
250 :
251 42 : _n_parts = other_mesh.n_partitions();
252 42 : _default_mapping_type = other_mesh.default_mapping_type();
253 42 : _default_mapping_data = other_mesh.default_mapping_data();
254 42 : _preparation = other_mesh._preparation;
255 12 : _point_locator = std::move(other_mesh._point_locator);
256 42 : _count_lower_dim_elems_in_point_locator = other_mesh.get_count_lower_dim_elems_in_point_locator();
257 : #ifdef LIBMESH_ENABLE_UNIQUE_ID
258 42 : _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 42 : _interior_mesh = (other_mesh._interior_mesh == &other_mesh) ?
263 : this : other_mesh._interior_mesh;
264 42 : _skip_noncritical_partitioning = other_mesh.skip_noncritical_partitioning();
265 42 : _skip_all_partitioning = other_mesh.skip_partitioning();
266 42 : _skip_renumber_nodes_and_elements = !(other_mesh.allow_renumbering());
267 42 : _skip_find_neighbors = !(other_mesh.allow_find_neighbors());
268 42 : _skip_detect_interior_parents = !(other_mesh.allow_detect_interior_parents());
269 42 : _allow_remote_element_removal = other_mesh.allow_remote_element_removal();
270 42 : _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 42 : _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 42 : _spatial_dimension = other_mesh.spatial_dimension();
280 42 : _elem_integer_names = std::move(other_mesh._elem_integer_names);
281 42 : _elem_integer_default_values = std::move(other_mesh._elem_integer_default_values);
282 42 : _node_integer_names = std::move(other_mesh._node_integer_names);
283 42 : _node_integer_default_values = std::move(other_mesh._node_integer_default_values);
284 42 : _n_constraint_rows = other_mesh._n_constraint_rows;
285 42 : _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 42 : 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 54 : return *this;
322 : }
323 :
324 :
325 20549 : bool MeshBase::operator== (const MeshBase & other_mesh) const
326 : {
327 19516 : LOG_SCOPE("operator==()", "MeshBase");
328 :
329 20549 : bool is_equal = this->locally_equals(other_mesh);
330 20549 : this->comm().min(is_equal);
331 40065 : return is_equal;
332 : }
333 :
334 :
335 18488 : void MeshBase::assert_equal_to (const MeshBase & other_mesh,
336 : std::string_view failure_context) const
337 : {
338 : #ifndef NDEBUG
339 36976 : LOG_SCOPE("assert_equal_to()", "MeshBase");
340 :
341 18488 : std::string_view local_diff = first_difference_from(other_mesh);
342 :
343 18488 : bool diff_found = !local_diff.empty();
344 18488 : this->comm().max(diff_found);
345 :
346 18488 : 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 18488 : }
376 :
377 :
378 20549 : bool MeshBase::locally_equals (const MeshBase & other_mesh) const
379 : {
380 20549 : const std::string_view diff = first_difference_from(other_mesh);
381 40473 : return diff.empty();
382 : }
383 :
384 :
385 39037 : 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 39037 : CHECK_MEMBER(_n_parts);
402 39037 : CHECK_MEMBER(_default_mapping_type);
403 39037 : CHECK_MEMBER(_default_mapping_data);
404 39037 : CHECK_MEMBER(_preparation);
405 39037 : 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 77449 : if ((_interior_mesh == this) !=
412 39037 : (other_mesh._interior_mesh == &other_mesh))
413 0 : return "_interior_mesh";
414 :
415 39037 : CHECK_MEMBER(_skip_noncritical_partitioning);
416 39037 : CHECK_MEMBER(_skip_all_partitioning);
417 39037 : CHECK_MEMBER(_skip_renumber_nodes_and_elements);
418 39037 : CHECK_MEMBER(_skip_find_neighbors);
419 39037 : CHECK_MEMBER(_skip_detect_interior_parents);
420 39037 : CHECK_MEMBER(_allow_remote_element_removal);
421 39037 : CHECK_MEMBER(_allow_node_and_elem_unique_id_overlap);
422 39037 : CHECK_MEMBER(_spatial_dimension);
423 39037 : CHECK_MEMBER(_n_constraint_rows);
424 39037 : CHECK_MEMBER(_point_locator_close_to_point_tol);
425 39037 : CHECK_MEMBER(_block_id_to_name);
426 39037 : CHECK_MEMBER(_elem_dims);
427 39037 : CHECK_MEMBER(_elem_default_orders);
428 39037 : CHECK_MEMBER(_supported_nodal_order);
429 39043 : CHECK_MEMBER(_mesh_subdomains);
430 39016 : CHECK_MEMBER(_all_elemset_ids);
431 39016 : CHECK_MEMBER(_elem_integer_names);
432 39418 : CHECK_MEMBER(_elem_integer_default_values);
433 39016 : CHECK_MEMBER(_node_integer_names);
434 39418 : CHECK_MEMBER(_node_integer_default_values);
435 :
436 39016 : if (static_cast<bool>(_default_ghosting) != static_cast<bool>(other_mesh._default_ghosting))
437 0 : return "_default_ghosting";
438 39016 : if (static_cast<bool>(_partitioner) != static_cast<bool>(other_mesh._partitioner))
439 0 : return "_partitioner";
440 39016 : if (*boundary_info != *other_mesh.boundary_info)
441 21 : return "boundary_info";
442 :
443 : // First check whether the "existence" of the two pointers differs (one present, one absent)
444 76987 : if (static_cast<bool>(_disjoint_neighbor_boundary_pairs) !=
445 37992 : 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 39055 : if (_disjoint_neighbor_boundary_pairs &&
449 38052 : (_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 37992 : other_mesh.get_constraint_rows();
454 78634 : for (const auto & [node, row] : this->_constraint_rows)
455 : {
456 39639 : const dof_id_type node_id = node->id();
457 39639 : const Node * other_node = other_mesh.query_node_ptr(node_id);
458 39639 : if (!other_node)
459 0 : return "_constraint_rows node presence";
460 :
461 30800 : auto it = other_rows.find(other_node);
462 39639 : if (it == other_rows.end())
463 0 : return "_constraint_rows row presence";
464 :
465 30800 : const auto & other_row = it->second;
466 46055 : if (row.size() != other_row.size())
467 0 : return "_constraint_rows row size";
468 :
469 136822 : 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 97183 : if (elem_pair.first->id() !=
476 120326 : other_elem_pair.first->id() ||
477 97183 : elem_pair.second !=
478 171223 : other_elem_pair.second ||
479 97183 : coef != other_coef)
480 0 : return "_constraint_rows row entry";
481 : }
482 : }
483 :
484 39019 : 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 77383 : if (_ghosting_functors.size() !=
495 38388 : 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 38995 : return this->subclass_first_difference_from(other_mesh);
503 : }
504 :
505 :
506 90196 : MeshBase::~MeshBase()
507 : {
508 74195 : this->MeshBase::clear();
509 :
510 34839 : libmesh_exceptionless_assert (!libMesh::closed());
511 144260 : }
512 :
513 :
514 :
515 2811214 : unsigned int MeshBase::mesh_dimension() const
516 : {
517 2811214 : if (!_elem_dims.empty())
518 2811194 : 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 175 : 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 125 : 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 175 : 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 175 : if (inserted1)
553 175 : _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 175 : 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 175 : libmesh_error_msg_if(!inserted2 && it2->second != &inserted_id_set,
562 : "The elemset code " << code << " already exists with a different id_set.");
563 175 : }
564 :
565 :
566 :
567 1445 : unsigned int MeshBase::n_elemsets() const
568 : {
569 1445 : return _all_elemset_ids.size();
570 : }
571 :
572 371 : 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 371 : if (const auto it = _elemset_codes.find(elemset_code);
578 102 : it != _elemset_codes.end())
579 162 : id_set_to_fill.insert(it->second->begin(), it->second->end());
580 371 : }
581 :
582 84 : 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 84 : return (it == _elemset_codes_inverse_map.end()) ? DofObject::invalid_id : it->second;
586 : }
587 :
588 837 : std::vector<dof_id_type> MeshBase::get_elemset_codes() const
589 : {
590 240 : std::vector<dof_id_type> ret;
591 837 : ret.reserve(_elemset_codes.size());
592 914 : for (const auto & pr : _elemset_codes)
593 77 : ret.push_back(pr.first);
594 837 : return ret;
595 : }
596 :
597 28 : 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 28 : 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 28 : 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 28 : 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 20 : _elemset_codes_inverse_map.erase(inverse_it);
620 :
621 : // Erase entry from forward map
622 20 : _elemset_codes.erase(it);
623 :
624 : // Add new code with original set of ids.
625 48 : 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 28 : if (!this->has_elem_integer("elemset_code"))
629 0 : return;
630 :
631 : // Get index of elemset_code extra integer
632 28 : 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 28 : (this->element_stored_range(),
637 580 : [elemset_index, old_code, new_code](Elem * elem)
638 : {
639 : dof_id_type elemset_code =
640 490 : elem->get_extra_integer(elemset_index);
641 :
642 490 : if (elemset_code == old_code)
643 175 : elem->set_extra_integer(elemset_index, new_code);
644 490 : });
645 : }
646 :
647 28 : 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 84 : 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 20 : id_set_copy.insert(new_id);
669 : }
670 :
671 : // Store in new version of map
672 40 : 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 84 : for (const auto & [id_set, elemset_code] : _elemset_codes_inverse_map)
681 56 : _elemset_codes.emplace(elemset_code, &id_set);
682 :
683 : // Update _all_elemset_ids
684 8 : _all_elemset_ids.erase(old_id);
685 20 : _all_elemset_ids.insert(new_id);
686 : }
687 :
688 :
689 :
690 45163 : 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 45163 : _spatial_dimension = d;
696 45163 : }
697 :
698 :
699 :
700 492 : unsigned int MeshBase::add_elem_integer(std::string name,
701 : bool allocate_data,
702 : dof_id_type default_value)
703 : {
704 664 : for (auto i : index_range(_elem_integer_names))
705 136 : if (_elem_integer_names[i] == name)
706 : {
707 8 : libmesh_assert_less(i, _elem_integer_default_values.size());
708 28 : _elem_integer_default_values[i] = default_value;
709 28 : return i;
710 : }
711 :
712 136 : libmesh_assert_equal_to(_elem_integer_names.size(),
713 : _elem_integer_default_values.size());
714 464 : _elem_integer_names.push_back(std::move(name));
715 464 : _elem_integer_default_values.push_back(default_value);
716 464 : if (allocate_data)
717 388 : this->size_elem_extra_integers();
718 600 : return _elem_integer_names.size()-1;
719 : }
720 :
721 :
722 :
723 22522 : 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 19688 : libmesh_assert(!default_values || default_values->size() == names.size());
728 19688 : libmesh_assert_equal_to(_elem_integer_names.size(), _elem_integer_default_values.size());
729 :
730 39376 : std::unordered_map<std::string, std::size_t> name_indices;
731 22606 : for (auto i : index_range(_elem_integer_names))
732 84 : name_indices[_elem_integer_names[i]] = i;
733 :
734 23722 : std::vector<unsigned int> returnval(names.size());
735 :
736 19688 : bool added_an_integer = false;
737 22987 : for (auto i : index_range(names))
738 : {
739 356 : const std::string & name = names[i];
740 465 : if (const auto it = name_indices.find(name);
741 278 : it != name_indices.end())
742 : {
743 42 : returnval[i] = it->second;
744 54 : _elem_integer_default_values[it->second] =
745 42 : default_values ? (*default_values)[i] : DofObject::invalid_id;
746 : }
747 : else
748 : {
749 489 : returnval[i] = _elem_integer_names.size();
750 423 : name_indices[name] = returnval[i];
751 423 : _elem_integer_names.push_back(name);
752 : _elem_integer_default_values.push_back
753 459 : (default_values ? (*default_values)[i] : DofObject::invalid_id);
754 266 : added_an_integer = true;
755 : }
756 : }
757 :
758 22522 : if (allocate_data && added_an_integer)
759 241 : this->size_elem_extra_integers();
760 :
761 42210 : return returnval;
762 : }
763 :
764 :
765 :
766 326 : unsigned int MeshBase::get_elem_integer_index(std::string_view name) const
767 : {
768 410 : for (auto i : index_range(_elem_integer_names))
769 317 : if (_elem_integer_names[i] == name)
770 326 : return i;
771 :
772 0 : libmesh_error_msg("Unknown elem integer " << name);
773 : return libMesh::invalid_uint;
774 : }
775 :
776 :
777 :
778 2709 : bool MeshBase::has_elem_integer(std::string_view name) const
779 : {
780 2807 : for (auto & entry : _elem_integer_names)
781 239 : if (entry == name)
782 40 : return true;
783 :
784 660 : return false;
785 : }
786 :
787 :
788 :
789 3044 : unsigned int MeshBase::add_node_integer(std::string name,
790 : bool allocate_data,
791 : dof_id_type default_value)
792 : {
793 5881 : for (auto i : index_range(_node_integer_names))
794 1880 : if (_node_integer_names[i] == name)
795 : {
796 16 : libmesh_assert_less(i, _node_integer_default_values.size());
797 56 : _node_integer_default_values[i] = default_value;
798 56 : return i;
799 : }
800 :
801 942 : libmesh_assert_equal_to(_node_integer_names.size(),
802 : _node_integer_default_values.size());
803 2988 : _node_integer_names.push_back(std::move(name));
804 2988 : _node_integer_default_values.push_back(default_value);
805 2988 : if (allocate_data)
806 1000 : this->size_node_extra_integers();
807 3930 : return _node_integer_names.size()-1;
808 : }
809 :
810 :
811 :
812 22508 : 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 19684 : libmesh_assert(!default_values || default_values->size() == names.size());
817 19684 : libmesh_assert_equal_to(_node_integer_names.size(), _node_integer_default_values.size());
818 :
819 39368 : std::unordered_map<std::string, std::size_t> name_indices;
820 22564 : for (auto i : index_range(_node_integer_names))
821 56 : name_indices[_node_integer_names[i]] = i;
822 :
823 23704 : std::vector<unsigned int> returnval(names.size());
824 :
825 19684 : bool added_an_integer = false;
826 23768 : for (auto i : index_range(names))
827 : {
828 1124 : const std::string & name = names[i];
829 1260 : if (const auto it = name_indices.find(name);
830 1028 : 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 1356 : returnval[i] = _node_integer_names.size();
839 1260 : name_indices[name] = returnval[i];
840 1260 : _node_integer_names.push_back(name);
841 : _node_integer_default_values.push_back
842 1328 : (default_values ? (*default_values)[i] : DofObject::invalid_id);
843 1028 : added_an_integer = true;
844 : }
845 : }
846 :
847 22508 : if (allocate_data && added_an_integer)
848 402 : this->size_node_extra_integers();
849 :
850 42192 : return returnval;
851 : }
852 :
853 :
854 :
855 70 : unsigned int MeshBase::get_node_integer_index(std::string_view name) const
856 : {
857 196 : for (auto i : index_range(_node_integer_names))
858 176 : if (_node_integer_names[i] == name)
859 70 : return i;
860 :
861 0 : libmesh_error_msg("Unknown node integer " << name);
862 : return libMesh::invalid_uint;
863 : }
864 :
865 :
866 :
867 112 : bool MeshBase::has_node_integer(std::string_view name) const
868 : {
869 318 : for (auto & entry : _node_integer_names)
870 318 : if (entry == name)
871 32 : return true;
872 :
873 0 : return false;
874 : }
875 :
876 :
877 :
878 23902 : void MeshBase::remove_orphaned_nodes ()
879 : {
880 42131 : LOG_SCOPE("remove_orphaned_nodes()", "MeshBase");
881 :
882 : // Will hold the set of nodes that are currently connected to elements
883 20323 : 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 2090095 : for (const auto & element : this->element_ptr_range())
888 16070489 : for (auto & n : element->node_ref_range())
889 14063060 : connected_nodes.insert(&n);
890 :
891 4481995 : for (const auto & node : this->node_ptr_range())
892 3435172 : if (!connected_nodes.count(node))
893 10050 : this->delete_node(node);
894 :
895 23902 : _preparation.has_removed_orphaned_nodes = true;
896 23902 : }
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 84265 : 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 84265 : this->clear_point_locator();
942 84265 : this->clear_stored_ranges();
943 84265 : _preparation = false;
944 84265 : _preparation.has_neighbor_ptrs = _skip_find_neighbors;
945 84265 : _preparation.has_removed_remote_elements = !_allow_remote_element_removal;
946 :
947 84265 : this->complete_preparation();
948 84265 : }
949 :
950 :
951 119229 : void MeshBase::complete_preparation()
952 : {
953 124796 : LOG_SCOPE("complete_preparation()", "MeshBase");
954 :
955 47878 : parallel_object_only();
956 :
957 47878 : libmesh_assert(this->comm().verify(this->is_serial()));
958 :
959 119229 : _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 47878 : 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 47878 : 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 119229 : if (!_skip_renumber_nodes_and_elements)
994 : {
995 95075 : if (!_preparation.has_removed_orphaned_nodes ||
996 34691 : !_preparation.has_synched_id_counts)
997 60384 : this->renumber_nodes_and_elements();
998 : }
999 : else
1000 : {
1001 24154 : if (!_preparation.has_removed_orphaned_nodes)
1002 23902 : this->remove_orphaned_nodes();
1003 24154 : if (!_preparation.has_synched_id_counts)
1004 23902 : this->update_parallel_id_counts();
1005 : }
1006 :
1007 : // Let all the elements find their neighbors
1008 119229 : if (!_skip_find_neighbors && !_preparation.has_neighbor_ptrs)
1009 82960 : 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 47878 : 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 119229 : if (!_preparation.has_cached_elem_data)
1022 117661 : 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 119229 : if (!_preparation.has_synched_subdomain_name_map)
1029 84265 : 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 119229 : if (!_preparation.has_interior_parent_ptrs)
1034 : {
1035 84265 : if (!_skip_detect_interior_parents)
1036 84265 : 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 47878 : 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 119229 : if (!_preparation.has_reinit_ghosting_functors)
1059 84286 : 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 119229 : if (!skip_partitioning() && !_preparation.is_partitioned)
1065 19312 : this->partition();
1066 107556 : else if (!this->n_unpartitioned_elem() &&
1067 28566 : !this->n_unpartitioned_nodes())
1068 53778 : _preparation.is_partitioned = true;
1069 :
1070 : // If we're using DistributedMesh, we'll probably want it
1071 : // parallelized.
1072 119229 : if (this->_allow_remote_element_removal &&
1073 98846 : !_preparation.has_removed_remote_elements)
1074 63840 : this->delete_remote_elements();
1075 : else
1076 55389 : _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 119229 : if (!_preparation.has_boundary_id_sets)
1084 84307 : this->get_boundary_info().regenerate_id_sets();
1085 :
1086 119229 : if (!_skip_renumber_nodes_and_elements)
1087 95075 : 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 47878 : Preparation completed_preparation = _preparation;
1094 47878 : if (skip_partitioning())
1095 18660 : completed_preparation.is_partitioned = true;
1096 47878 : libmesh_assert(completed_preparation);
1097 : #endif
1098 :
1099 : #ifdef DEBUG
1100 : // The if() here avoids both unnecessary work *and* stack overflow
1101 47878 : if (was_partly_prepared)
1102 18464 : MeshTools::libmesh_assert_valid_is_prepared(*this);
1103 :
1104 47878 : MeshTools::libmesh_assert_valid_boundary_ids(*this);
1105 : #ifdef LIBMESH_ENABLE_UNIQUE_ID
1106 47878 : MeshTools::libmesh_assert_valid_unique_ids(*this);
1107 : #endif
1108 : #endif
1109 119229 : }
1110 :
1111 : void
1112 84286 : MeshBase::reinit_ghosting_functors()
1113 : {
1114 183736 : for (auto & gf : _ghosting_functors)
1115 : {
1116 44918 : libmesh_assert(gf);
1117 99450 : gf->mesh_reinit();
1118 : }
1119 :
1120 84286 : _preparation.has_reinit_ghosting_functors = true;
1121 84286 : }
1122 :
1123 197608 : void MeshBase::clear ()
1124 : {
1125 : // Reset the number of partitions
1126 197608 : _n_parts = 1;
1127 :
1128 : // Reset the preparation flags
1129 197608 : _preparation = false;
1130 :
1131 : // Clear boundary information
1132 197608 : if (boundary_info)
1133 197482 : boundary_info->clear();
1134 :
1135 : // Clear cached element data
1136 84210 : _elem_dims.clear();
1137 84210 : _elem_default_orders.clear();
1138 197608 : _supported_nodal_order = MAXIMUM;
1139 :
1140 84210 : _elemset_codes.clear();
1141 84210 : _elemset_codes_inverse_map.clear();
1142 :
1143 84210 : _constraint_rows.clear();
1144 197608 : _n_constraint_rows = 0;
1145 :
1146 : // Clear our point locator.
1147 197608 : this->clear_point_locator();
1148 197608 : this->clear_stored_ranges();
1149 197608 : }
1150 :
1151 :
1152 136244 : bool MeshBase::is_prepared() const
1153 : {
1154 136244 : return static_cast<bool>(_preparation);
1155 : }
1156 :
1157 :
1158 56 : void MeshBase::unset_is_prepared()
1159 : {
1160 56 : _preparation = false;
1161 56 : this->clear_point_locator();
1162 56 : this->clear_stored_ranges();
1163 56 : }
1164 :
1165 :
1166 165515 : 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 68975 : (std::remove(_ghosting_functors.begin(),
1172 : _ghosting_functors.end(),
1173 165515 : &ghosting_functor),
1174 146528 : _ghosting_functors.end());
1175 : #endif
1176 :
1177 : // We shouldn't have two copies of the same functor
1178 49988 : libmesh_assert(std::find(_ghosting_functors.begin(),
1179 : _ghosting_functors.end(),
1180 : &ghosting_functor) ==
1181 : _ghosting_functors.end());
1182 :
1183 165515 : _ghosting_functors.push_back(&ghosting_functor);
1184 165515 : }
1185 :
1186 :
1187 :
1188 158819 : void MeshBase::remove_ghosting_functor(GhostingFunctor & ghosting_functor)
1189 : {
1190 67727 : auto raw_it = std::find(_ghosting_functors.begin(),
1191 204365 : _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 158819 : if (raw_it != _ghosting_functors.end())
1199 158728 : _ghosting_functors.erase(raw_it);
1200 :
1201 : // We shouldn't have had two copies of the same functor
1202 45546 : libmesh_assert(std::find(_ghosting_functors.begin(),
1203 : _ghosting_functors.end(),
1204 : &ghosting_functor) ==
1205 : _ghosting_functors.end());
1206 :
1207 158819 : if (const auto it = _shared_functors.find(&ghosting_functor);
1208 45546 : it != _shared_functors.end())
1209 5 : _shared_functors.erase(it);
1210 158819 : }
1211 :
1212 :
1213 :
1214 3231 : 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 3337 : void operator()(const ConstElemRange & range) {
1227 695466 : for (const Elem * elem : range)
1228 692129 : my_ids.insert(elem->subdomain_id());
1229 3337 : }
1230 :
1231 36 : void join(SBDInserter & other) {
1232 36 : my_ids.merge(other.my_ids);
1233 36 : }
1234 : };
1235 :
1236 2344 : SBDInserter inserter;
1237 3231 : Threads::parallel_reduce(this->active_local_element_stored_range(), inserter);
1238 :
1239 1172 : ids.swap(inserter.my_ids);
1240 :
1241 3231 : 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 8764 : for (const auto & elem : this->active_unpartitioned_element_ptr_range())
1247 4361 : ids.insert(elem->subdomain_id());
1248 :
1249 : // Some subdomains may only live on other processors
1250 3231 : this->comm().set_union(ids);
1251 : }
1252 3231 : }
1253 :
1254 :
1255 :
1256 63458 : 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 67036 : for (auto & gf : as_range(this->ghosting_functors_begin(),
1262 178193 : this->ghosting_functors_end()))
1263 73009 : gf->redistribute();
1264 63458 : }
1265 :
1266 :
1267 :
1268 188858 : void MeshBase::update_post_partitioning()
1269 : {
1270 : // Any range over local objects is obsolete if our partitioner
1271 : // changed the definition of "local".
1272 59191 : _active_local_element_stored_range.reset();
1273 59191 : _const_active_local_element_stored_range.reset();
1274 59191 : _local_element_stored_range.reset();
1275 59191 : _const_local_element_stored_range.reset();
1276 59191 : _local_node_stored_range.reset();
1277 59191 : _const_local_node_stored_range.reset();
1278 59191 : _unpartitioned_node_stored_range.reset();
1279 59191 : _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 188858 : 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 188858 : }
1292 :
1293 :
1294 :
1295 2045 : 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 2045 : this->subdomain_ids (ids);
1303 :
1304 2645 : 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 93314 : 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 55148 : libmesh_assert (proc_id < this->n_processors() ||
1326 : proc_id == DofObject::invalid_processor_id);
1327 :
1328 131480 : return static_cast<dof_id_type>(std::distance (this->pid_nodes_begin(proc_id),
1329 224794 : this->pid_nodes_end (proc_id)));
1330 : }
1331 :
1332 :
1333 :
1334 158818 : 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 74473 : libmesh_assert (proc_id < this->n_processors() ||
1339 : proc_id == DofObject::invalid_processor_id);
1340 :
1341 243163 : return static_cast<dof_id_type>(std::distance (this->pid_elements_begin(proc_id),
1342 401981 : this->pid_elements_end (proc_id)));
1343 : }
1344 :
1345 :
1346 :
1347 6199 : 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 10136 : return static_cast<dof_id_type>(std::distance (this->active_pid_elements_begin(proc_id),
1351 16335 : 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 4210 : dof_id_type MeshBase::n_active_sub_elem () const
1369 : {
1370 1575 : dof_id_type ne=0;
1371 :
1372 2296668 : for (const auto & elem : this->active_element_ptr_range())
1373 2290883 : ne += elem->n_sub_elem();
1374 :
1375 4210 : return ne;
1376 : }
1377 :
1378 :
1379 :
1380 1438 : std::string MeshBase::get_info(const unsigned int verbosity /* = 0 */, const bool global /* = true */) const
1381 : {
1382 2286 : std::ostringstream oss;
1383 :
1384 1438 : oss << " Mesh Information:" << '\n';
1385 :
1386 1438 : if (!_elem_dims.empty())
1387 : {
1388 1438 : oss << " elem_dimensions()={";
1389 1438 : std::copy(_elem_dims.begin(),
1390 424 : --_elem_dims.end(), // --end() is valid if the set is non-empty
1391 1014 : std::ostream_iterator<unsigned int>(oss, ", "));
1392 1438 : oss << cast_int<unsigned int>(*_elem_dims.rbegin());
1393 1438 : oss << "}";
1394 1438 : if (!this->preparation().has_cached_elem_data)
1395 0 : oss << " (may be out of date)";
1396 1438 : oss << '\n';
1397 : }
1398 :
1399 1438 : if (!_elem_default_orders.empty())
1400 : {
1401 1438 : oss << " elem_default_orders()={";
1402 1438 : std::transform(_elem_default_orders.begin(),
1403 424 : --_elem_default_orders.end(),
1404 1438 : std::ostream_iterator<std::string>(oss, ", "),
1405 8 : [](Order o)
1406 20 : { return Utility::enum_to_string<Order>(o); });
1407 1438 : oss << Utility::enum_to_string<Order>(*_elem_default_orders.rbegin());
1408 1438 : oss << "}";
1409 1438 : if (!this->preparation().has_cached_elem_data)
1410 0 : oss << " (may be out of date)";
1411 1438 : oss << '\n';
1412 : }
1413 :
1414 1438 : oss << " supported_nodal_order()=" << this->_supported_nodal_order;
1415 1438 : if (!this->preparation().has_cached_elem_data)
1416 0 : oss << " (may be out of date)";
1417 1438 : oss << '\n';
1418 :
1419 1438 : oss << " spatial_dimension()=" << int(this->_spatial_dimension);
1420 1438 : if (!this->preparation().has_cached_elem_data)
1421 0 : oss << " (may be out of date)";
1422 1438 : oss << '\n';
1423 :
1424 1438 : oss << " n_nodes()=" << this->n_nodes() << '\n'
1425 1438 : << " n_local_nodes()=" << this->n_local_nodes() << '\n'
1426 1862 : << " n_elem()=" << this->n_elem() << '\n'
1427 2452 : << " n_local_elem()=" << this->n_local_elem() << '\n';
1428 : #ifdef LIBMESH_ENABLE_AMR
1429 2028 : oss << " n_active_elem()=" << this->n_active_elem() << '\n';
1430 : #endif
1431 1438 : if (global)
1432 1862 : 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 1862 : oss << " n_elemsets()=" << static_cast<std::size_t>(this->n_elemsets()) << '\n';
1436 1438 : if (!_elemset_codes.empty())
1437 0 : oss << " n_elemset_codes=" << _elemset_codes.size() << '\n';
1438 1438 : oss << " n_partitions()=" << static_cast<std::size_t>(this->n_partitions()) << '\n'
1439 2286 : << " n_processors()=" << static_cast<std::size_t>(this->n_processors()) << '\n'
1440 1862 : << " n_threads()=" << static_cast<std::size_t>(libMesh::n_threads()) << '\n'
1441 2286 : << " processor_id()=" << static_cast<std::size_t>(this->processor_id()) << '\n'
1442 1862 : << " is_prepared()=" << (this->is_prepared() ? "true" : "false") << '\n'
1443 3304 : << " is_replicated()=" << (this->is_replicated() ? "true" : "false") << '\n';
1444 :
1445 1438 : if (verbosity > 0)
1446 : {
1447 28 : if (global)
1448 : {
1449 8 : libmesh_parallel_only(this->comm());
1450 36 : if (this->processor_id() != 0)
1451 12 : 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 224 : const auto elem_type_helper = [](const std::set<int> & elem_types) {
1456 336 : std::stringstream ss;
1457 448 : for (auto it = elem_types.begin(); it != elem_types.end();)
1458 : {
1459 392 : ss << Utility::enum_to_string((ElemType)*it);
1460 224 : if (++it != elem_types.end())
1461 0 : ss << ", ";
1462 : }
1463 280 : return ss.str();
1464 112 : };
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 248680 : const auto include_object = [this, &global](const DofObject & dof_object) {
1469 355712 : return this->processor_id() == dof_object.processor_id() ||
1470 48840 : (global &&
1471 31744 : this->processor_id() == 0 &&
1472 223852 : dof_object.processor_id() == DofObject::invalid_processor_id);
1473 20 : };
1474 :
1475 8 : Real volume = 0;
1476 :
1477 : // Add bounding box information
1478 28 : const auto bbox = global ? MeshTools::create_bounding_box(*this) : MeshTools::create_local_bounding_box(*this);
1479 28 : if (!global || this->processor_id() == 0)
1480 : oss << "\n " << (global ? "" : "Local ") << "Mesh Bounding Box:\n"
1481 16 : << " Minimum: " << bbox.min() << "\n"
1482 12 : << " Maximum: " << bbox.max() << "\n"
1483 24 : << " Delta: " << (bbox.max() - bbox.min()) << "\n";
1484 :
1485 : // Obtain the global or local element types
1486 16 : std::set<int> elem_types;
1487 16432 : for (const Elem * elem : this->active_local_element_ptr_range())
1488 16396 : elem_types.insert(elem->type());
1489 28 : if (global)
1490 : {
1491 : // Pick up unpartitioned elems on rank 0
1492 36 : if (this->processor_id() == 0)
1493 28 : for (const Elem * elem : this->active_unpartitioned_element_ptr_range())
1494 8 : elem_types.insert(elem->type());
1495 :
1496 28 : this->comm().set_union(elem_types);
1497 : }
1498 :
1499 : // Add element types
1500 28 : if (!global || this->processor_id() == 0)
1501 : oss << "\n " << (global ? "" : "Local ") << "Mesh Element Type(s):\n "
1502 44 : << 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 28 : if (global)
1507 28 : this->comm().set_union(nodeset_ids);
1508 :
1509 : // Accumulate local information for each nodeset
1510 72 : 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 28420 : for (const auto & [node, id] : this->get_boundary_info().get_nodeset_map())
1517 : {
1518 28392 : if (!include_object(*node))
1519 8112 : continue;
1520 :
1521 16224 : NodesetInfo & info = nodeset_info_map[id];
1522 :
1523 16224 : ++info.num_nodes;
1524 :
1525 16224 : if (verbosity > 1)
1526 16224 : info.bbox.union_with(*node);
1527 : }
1528 :
1529 : // Add nodeset info
1530 28 : if (!global || this->processor_id() == 0)
1531 : {
1532 16 : oss << "\n " << (global ? "" : "Local ") << "Mesh Nodesets:\n";
1533 16 : 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 196 : for (const auto id : nodeset_ids)
1539 : {
1540 168 : NodesetInfo & info = nodeset_info_map[id];
1541 :
1542 : // Reduce the local information for this nodeset if required
1543 168 : if (global)
1544 : {
1545 168 : this->comm().sum(info.num_nodes);
1546 168 : if (verbosity > 1)
1547 : {
1548 216 : this->comm().min(info.bbox.min());
1549 216 : this->comm().max(info.bbox.max());
1550 : }
1551 : }
1552 :
1553 168 : const bool has_name = nodeset_name_map.count(id) && nodeset_name_map.at(id).size();
1554 264 : 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 168 : if (global ? this->processor_id() == 0 : info.num_nodes > 0)
1559 : {
1560 96 : oss << " Nodeset " << id;
1561 96 : if (has_name)
1562 144 : oss << " (" << name << ")";
1563 120 : oss << ", " << info.num_nodes << " " << (global ? "" : "local ") << "nodes\n";
1564 :
1565 96 : if (verbosity > 1)
1566 : {
1567 96 : oss << " " << (global ? "Bounding" : "Local bounding") << " box minimum: "
1568 96 : << info.bbox.min() << "\n"
1569 96 : << " " << (global ? "Bounding" : "Local bounding") << " box maximum: "
1570 96 : << info.bbox.max() << "\n"
1571 96 : << " " << (global ? "Bounding" : "Local bounding") << " box delta: "
1572 96 : << (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 28 : if (global)
1580 28 : 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 23324 : for (const auto & pair : this->get_boundary_info().get_sideset_map())
1596 : {
1597 23296 : const Elem * elem = pair.first;
1598 16640 : if (!include_object(*elem))
1599 9984 : continue;
1600 :
1601 13312 : const auto id = pair.second.second;
1602 13312 : SidesetInfo & info = sideset_info_map[id];
1603 :
1604 13312 : const auto s = pair.second.first;
1605 13312 : const Elem & side = side_builder(*elem, s);
1606 :
1607 13312 : ++info.num_sides;
1608 13312 : info.side_elem_types.insert(side.type());
1609 13312 : info.elem_types.insert(elem->type());
1610 13312 : info.elem_ids.insert(elem->id());
1611 :
1612 66560 : for (const Node & node : side.node_ref_range())
1613 39936 : if (include_object(node))
1614 52672 : info.node_ids.insert(node.id());
1615 :
1616 13312 : if (verbosity > 1)
1617 : {
1618 13312 : info.volume += side.volume();
1619 13312 : info.bbox.union_with(side.loose_bounding_box());
1620 : }
1621 : }
1622 :
1623 : // Add sideset info
1624 28 : if (!global || this->processor_id() == 0)
1625 : {
1626 16 : oss << "\n " << (global ? "" : "Local ") << "Mesh Sidesets:\n";
1627 16 : 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 196 : for (const auto id : sideset_ids)
1632 : {
1633 168 : SidesetInfo & info = sideset_info_map[id];
1634 :
1635 168 : auto num_elems = info.elem_ids.size();
1636 168 : auto num_nodes = info.node_ids.size();
1637 :
1638 : // Reduce the local information for this sideset if required
1639 168 : if (global)
1640 : {
1641 168 : this->comm().sum(info.num_sides);
1642 168 : this->comm().set_union(info.side_elem_types, 0);
1643 168 : this->comm().sum(num_elems);
1644 168 : this->comm().set_union(info.elem_types, 0);
1645 168 : this->comm().sum(num_nodes);
1646 168 : if (verbosity > 1)
1647 : {
1648 168 : this->comm().sum(info.volume);
1649 216 : this->comm().min(info.bbox.min());
1650 216 : this->comm().max(info.bbox.max());
1651 : }
1652 : }
1653 :
1654 168 : const bool has_name = sideset_name_map.count(id) && sideset_name_map.at(id).size();
1655 264 : 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 168 : if (global ? this->processor_id() == 0 : info.num_sides > 0)
1660 : {
1661 96 : oss << " Sideset " << id;
1662 96 : if (has_name)
1663 144 : oss << " (" << name << ")";
1664 168 : oss << ", " << info.num_sides << " sides (" << elem_type_helper(info.side_elem_types) << ")"
1665 288 : << ", " << num_elems << " " << (global ? "" : "local ") << "elems (" << elem_type_helper(info.elem_types) << ")"
1666 216 : << ", " << num_nodes << " " << (global ? "" : "local ") << "nodes\n";
1667 :
1668 96 : if (verbosity > 1)
1669 : {
1670 120 : oss << " " << (global ? "Side" : "Local side") << " volume: " << info.volume << "\n"
1671 96 : << " " << (global ? "Bounding" : "Local bounding") << " box minimum: "
1672 96 : << info.bbox.min() << "\n"
1673 96 : << " " << (global ? "Bounding" : "Local bounding") << " box maximum: "
1674 96 : << info.bbox.max() << "\n"
1675 96 : << " " << (global ? "Bounding" : "Local bounding") << " box delta: "
1676 96 : << (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 28 : if (global)
1684 28 : 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 28 : std::unique_ptr<const Elem> edge;
1695 :
1696 28 : 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 28 : if (!global || this->processor_id() == 0)
1716 : {
1717 16 : oss << "\n " << (global ? "" : "Local ") << "Mesh Edgesets:\n";
1718 16 : if (edgeset_ids.empty())
1719 16 : oss << " None\n";
1720 : }
1721 :
1722 8 : const auto & edgeset_name_map = this->get_boundary_info().get_edgeset_name_map();
1723 28 : 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 49200 : for (const Elem * elem : this->active_element_ptr_range())
1767 20480 : if (include_object(*elem))
1768 16396 : subdomains.insert(elem->subdomain_id());
1769 28 : if (global)
1770 28 : 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 49200 : for (const Elem * elem : this->element_ptr_range())
1786 20480 : if (include_object(*elem))
1787 : {
1788 16384 : SubdomainInfo & info = subdomain_info_map[elem->subdomain_id()];
1789 :
1790 16384 : ++info.num_elems;
1791 16384 : info.elem_types.insert(elem->type());
1792 :
1793 : #ifdef LIBMESH_ENABLE_AMR
1794 4096 : if (elem->active())
1795 16384 : ++info.num_active_elems;
1796 : #endif
1797 :
1798 147456 : for (const Node & node : elem->node_ref_range())
1799 130560 : if (include_object(node) && node.active())
1800 129536 : info.active_node_ids.insert(node.id());
1801 :
1802 16384 : if (verbosity > 1 && elem->active())
1803 : {
1804 16384 : info.volume += elem->volume();
1805 16384 : info.bbox.union_with(elem->loose_bounding_box());
1806 : }
1807 12 : }
1808 :
1809 : // Add subdomain info
1810 28 : oss << "\n " << (global ? "" : "Local ") << "Mesh Subdomains:\n";
1811 8 : const auto & subdomain_name_map = this->get_subdomain_name_map();
1812 56 : for (const auto id : subdomains)
1813 : {
1814 28 : SubdomainInfo & info = subdomain_info_map[id];
1815 :
1816 28 : auto num_active_nodes = info.active_node_ids.size();
1817 :
1818 : // Reduce the information for this subdomain if needed
1819 28 : if (global)
1820 : {
1821 28 : this->comm().sum(info.num_elems);
1822 : #ifdef LIBMESH_ENABLE_AMR
1823 28 : this->comm().sum(info.num_active_elems);
1824 : #endif
1825 28 : this->comm().sum(num_active_nodes);
1826 28 : this->comm().set_union(info.elem_types, 0);
1827 28 : if (verbosity > 1)
1828 : {
1829 36 : this->comm().min(info.bbox.min());
1830 36 : this->comm().max(info.bbox.max());
1831 28 : this->comm().sum(info.volume);
1832 : }
1833 : }
1834 28 : if (verbosity > 1)
1835 28 : volume += info.volume;
1836 :
1837 8 : const bool has_name = subdomain_name_map.count(id);
1838 36 : 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 28 : if (!global || this->processor_id() == 0)
1843 : {
1844 16 : oss << " Subdomain " << id;
1845 16 : if (has_name)
1846 0 : oss << " (" << name << ")";
1847 16 : oss << ": " << info.num_elems << " " << (global ? "" : "local ") << "elems "
1848 24 : << "(" << elem_type_helper(info.elem_types);
1849 : #ifdef LIBMESH_ENABLE_AMR
1850 16 : oss << ", " << info.num_active_elems << " active";
1851 : #endif
1852 20 : oss << "), " << num_active_nodes << " " << (global ? "" : "local ") << "active nodes\n";
1853 16 : if (verbosity > 1)
1854 : {
1855 20 : oss << " " << (global ? "Volume" : "Local volume") << ": " << info.volume << "\n";
1856 16 : oss << " " << (global ? "Bounding" : "Local bounding") << " box minimum: "
1857 16 : << info.bbox.min() << "\n"
1858 16 : << " " << (global ? "Bounding" : "Local bounding") << " box maximum: "
1859 16 : << info.bbox.max() << "\n"
1860 16 : << " " << (global ? "Bounding" : "Local bounding") << " box delta: "
1861 16 : << (info.bbox.max() - info.bbox.min()) << "\n";
1862 : }
1863 : }
1864 : }
1865 :
1866 48 : oss << " " << (global ? "Global" : "Local") << " mesh volume = " << volume << "\n";
1867 :
1868 12 : }
1869 :
1870 1862 : return oss.str();
1871 590 : }
1872 :
1873 :
1874 1438 : void MeshBase::print_info(std::ostream & os, const unsigned int verbosity /* = 0 */, const bool global /* = true */) const
1875 : {
1876 1862 : os << this->get_info(verbosity, global)
1877 424 : << std::endl;
1878 1438 : }
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 65545 : 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 65545 : if (this->n_unpartitioned_elem() > 0)
1893 : {
1894 15300 : libmesh_assert (partitioner().get());
1895 15300 : libmesh_assert (this->is_serial());
1896 51838 : 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 4039 : else if (!skip_noncritical_partitioning())
1902 : {
1903 13122 : 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 585 : MeshTools::correct_node_proc_ids(*this);
1911 :
1912 : // Make sure locally cached partition count is correct
1913 585 : this->recalculate_n_partitions();
1914 :
1915 : // Make sure any other locally cached data is correct
1916 585 : this->update_post_partitioning();
1917 : }
1918 :
1919 65545 : _preparation.is_partitioned = true;
1920 65545 : }
1921 :
1922 2595 : void MeshBase::all_second_order (const bool full_ordered)
1923 : {
1924 2595 : this->all_second_order_range(this->element_ptr_range(), full_ordered);
1925 2595 : }
1926 :
1927 2230 : void MeshBase::all_complete_order ()
1928 : {
1929 2230 : this->all_complete_order_range(this->element_ptr_range());
1930 2230 : }
1931 :
1932 585 : unsigned int MeshBase::recalculate_n_partitions()
1933 : {
1934 : // This requires an inspection on every processor
1935 170 : parallel_object_only();
1936 :
1937 585 : unsigned int max_proc_id=0;
1938 :
1939 69322 : for (const auto & elem : this->active_local_element_ptr_range())
1940 68725 : 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 585 : _n_parts = max_proc_id+1;
1944 :
1945 585 : this->comm().max(_n_parts);
1946 :
1947 585 : return _n_parts;
1948 : }
1949 :
1950 :
1951 :
1952 187753 : std::unique_ptr<PointLocatorBase> MeshBase::sub_point_locator () const
1953 : {
1954 : // If there's no master point locator, then we need one.
1955 187753 : 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 3750 : _point_locator = PointLocatorBase::build(TREE_ELEMENTS, *this);
1967 : #endif
1968 :
1969 2761 : 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 187753 : PointLocatorBase::build(TREE_ELEMENTS, *this, _point_locator.get());
1980 : #endif
1981 : }
1982 :
1983 :
1984 :
1985 6957467 : void MeshBase::clear_point_locator ()
1986 : {
1987 3120754 : _point_locator.reset(nullptr);
1988 6957467 : }
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 1792265 : bool MeshBase::get_count_lower_dim_elems_in_point_locator() const
2000 : {
2001 1792265 : return _count_lower_dim_elems_in_point_locator;
2002 : }
2003 :
2004 :
2005 : #ifdef LIBMESH_ENABLE_DEPRECATED
2006 56 : 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 56 : return _block_id_to_name[id];
2017 : }
2018 : #endif // LIBMESH_ENABLE_DEPRECATED
2019 :
2020 :
2021 5593 : 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 5593 : static const std::string empty;
2025 :
2026 5593 : 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 888 : return iter->second;
2031 : }
2032 :
2033 205378 : void MeshBase::set_subdomain_name(const subdomain_id_type id,
2034 : const std::string & name,
2035 : const bool synchronous)
2036 : {
2037 205378 : if (synchronous)
2038 6 : parallel_object_only();
2039 : else
2040 58488 : this->unset_has_synched_subdomain_name_map();
2041 205378 : _block_id_to_name[id] = name;
2042 205378 : }
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 347162 : const ElemRange & MeshBase::element_stored_range()
2059 : {
2060 347162 : if (!_element_stored_range)
2061 : {
2062 : // Assigning to our cache may not be safe within threads
2063 51289 : libmesh_assert(!Threads::in_threads);
2064 :
2065 : _element_stored_range =
2066 305086 : std::make_unique<ElemRange>(this->elements_begin(),
2067 310321 : this->elements_end());
2068 : }
2069 : else
2070 : #ifdef DEBUG
2071 62839 : 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 347162 : 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 86159 : const ElemRange & MeshBase::active_local_element_stored_range()
2099 : {
2100 86159 : if (!_active_local_element_stored_range)
2101 : {
2102 : // Assigning to our cache may not be safe within threads
2103 26289 : libmesh_assert(!Threads::in_threads);
2104 :
2105 : _active_local_element_stored_range =
2106 198765 : std::make_unique<ElemRange>(this->active_local_elements_begin(),
2107 193675 : 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 86159 : return *_active_local_element_stored_range;
2122 : }
2123 :
2124 :
2125 126610 : const ConstElemRange & MeshBase::active_local_element_stored_range() const
2126 : {
2127 47154 : return const_shim(*this, &MeshBase::active_local_element_stored_range,
2128 126610 : _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 165 : const ElemRange & MeshBase::local_element_stored_range()
2140 : {
2141 165 : 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 391 : std::make_unique<ElemRange>(this->local_elements_begin(),
2148 382 : 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 165 : 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 1708 : const ConstElemRange & MeshBase::const_local_element_stored_range() const
2174 : {
2175 576 : return const_shim(*this, &MeshBase::local_element_stored_range,
2176 1708 : _const_local_element_stored_range);
2177 : }
2178 :
2179 :
2180 450317 : const NodeRange & MeshBase::node_stored_range()
2181 : {
2182 450317 : if (!_node_stored_range)
2183 : {
2184 : // Assigning to our cache may not be safe within threads
2185 49539 : libmesh_assert(!Threads::in_threads);
2186 :
2187 : _node_stored_range =
2188 397914 : std::make_unique<NodeRange>(this->nodes_begin(),
2189 380675 : this->nodes_end());
2190 : }
2191 : else
2192 : #ifdef DEBUG
2193 83929 : 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 450317 : 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 6179 : const NodeRange & MeshBase::local_node_stored_range()
2221 : {
2222 6179 : 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 1515 : std::make_unique<NodeRange>(this->local_nodes_begin(),
2229 1444 : 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 6179 : 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 1708 : const ConstNodeRange & MeshBase::const_local_node_stored_range() const
2255 : {
2256 576 : return const_shim(*this, &MeshBase::local_node_stored_range,
2257 1708 : _const_local_node_stored_range);
2258 : }
2259 :
2260 :
2261 2584 : const NodeRange & MeshBase::unpartitioned_node_stored_range()
2262 : {
2263 2584 : 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 512 : std::make_unique<NodeRange>(this->pid_nodes_begin(DofObject::invalid_processor_id),
2270 486 : 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 2584 : 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 16981668 : void MeshBase::clear_stored_ranges()
2303 : {
2304 7942247 : _element_stored_range.reset();
2305 7942247 : _const_element_stored_range.reset();
2306 7942247 : _active_local_element_stored_range.reset();
2307 7942247 : _const_active_local_element_stored_range.reset();
2308 7942247 : _local_element_stored_range.reset();
2309 7942247 : _const_local_element_stored_range.reset();
2310 7942247 : _node_stored_range.reset();
2311 7942247 : _const_node_stored_range.reset();
2312 7942247 : _local_node_stored_range.reset();
2313 7942247 : _const_local_node_stored_range.reset();
2314 7942247 : _unpartitioned_node_stored_range.reset();
2315 7942247 : _const_unpartitioned_node_stored_range.reset();
2316 16981668 : }
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 126674 : void MeshBase::cache_elem_data()
2329 : {
2330 : // This requires an inspection on every processor
2331 50162 : 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 81486 : _elem_dims.clear();
2336 81486 : _elem_default_orders.clear();
2337 81486 : _mesh_subdomains.clear();
2338 126674 : _supported_nodal_order = MAXIMUM;
2339 :
2340 6173588 : for (const auto & elem : this->active_element_ptr_range())
2341 : {
2342 5970402 : _elem_dims.insert(cast_int<unsigned char>(elem->dim()));
2343 5970402 : _elem_default_orders.insert(elem->default_order());
2344 5970402 : _mesh_subdomains.insert(elem->subdomain_id());
2345 5970402 : _supported_nodal_order =
2346 5970402 : static_cast<Order>
2347 11940804 : (std::min(static_cast<int>(_supported_nodal_order),
2348 6046870 : static_cast<int>(elem->supported_nodal_order())));
2349 45188 : }
2350 :
2351 : // If the largest element dimension found is larger than the current
2352 : // _spatial_dimension, increase _spatial_dimension.
2353 126674 : unsigned int max_dim = this->mesh_dimension();
2354 126674 : if (max_dim > _spatial_dimension)
2355 6849 : _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 126674 : if (_spatial_dimension < LIBMESH_DIM)
2364 : {
2365 6757346 : 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 5055056 : if ((*node)(0) != 0. && _spatial_dimension < 1)
2370 0 : _spatial_dimension = 1;
2371 :
2372 5055056 : if ((*node)(1) != 0. && _spatial_dimension < 2)
2373 : {
2374 292 : _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 5055056 : if ((*node)(2) != 0.)
2385 : {
2386 : // Spatial dimension can't get any higher than this, so
2387 : // we can break out.
2388 2089 : _spatial_dimension = 3;
2389 2089 : break;
2390 : }
2391 : #endif
2392 23947 : }
2393 : }
2394 :
2395 126674 : if (!this->is_serial())
2396 : {
2397 : // Some different dimension/order/subdomain elements may only
2398 : // live on other processors
2399 900 : this->comm().set_union(_elem_dims);
2400 900 : this->comm().set_union(_elem_default_orders);
2401 900 : this->comm().min(_supported_nodal_order);
2402 900 : this->comm().set_union(_mesh_subdomains);
2403 900 : this->comm().max(_spatial_dimension);
2404 : }
2405 :
2406 126674 : _preparation.has_cached_elem_data = true;
2407 126674 : }
2408 :
2409 :
2410 91489 : void MeshBase::sync_subdomain_name_map()
2411 : {
2412 : // This requires every processor
2413 40054 : parallel_object_only();
2414 :
2415 91489 : this->comm().set_union(_block_id_to_name);
2416 :
2417 91489 : _preparation.has_synched_subdomain_name_map = true;
2418 91489 : }
2419 :
2420 :
2421 84265 : void MeshBase::detect_interior_parents()
2422 : {
2423 38048 : LOG_SCOPE("detect_interior_parents()", "MeshBase");
2424 :
2425 : // This requires an inspection on every processor
2426 37894 : 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 37894 : std::set<unsigned char> elem_dims_copy;
2433 84265 : if (_preparation.has_cached_elem_data)
2434 37894 : 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 84265 : if (elem_dims_copy.size() <= 1)
2445 : {
2446 83419 : _preparation.has_interior_parent_ptrs = true;
2447 83419 : return;
2448 : }
2449 :
2450 : // Convenient elem_dimensions iterators
2451 656 : 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 846 : std::vector<bool> skip_dimension_for_interior_parents(/*count=*/LIBMESH_DIM+1, /*value=*/false);
2461 656 : 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 846 : std::vector<bool> skip_dimensions_for_node_to_el_map(/*count=*/LIBMESH_DIM+1, /*value=*/false);
2469 846 : 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 1210 : for (auto [it, next] = std::make_tuple(dim_start, std::next(dim_start));
2477 2070 : next != dim_end; ++it, ++next)
2478 : {
2479 860 : if (*it + 1 != *next) // if sequential dimensions differ by exactly 1
2480 : {
2481 110 : skip_dimension_for_interior_parents[*it] = true;
2482 181 : skip_dimensions_for_node_to_el_map[*next] = true;
2483 : }
2484 833 : 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 846 : if (skip_all_dimensions)
2493 : {
2494 167 : _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 49660 : for (const auto & elem : this->element_ptr_range())
2507 : {
2508 : // Ignore element if it cannot be interior parent of any other elem.
2509 36344 : if (skip_dimensions_for_node_to_el_map[elem->dim()])
2510 7692 : continue;
2511 :
2512 : // Populating the node_to_elem map, same as MeshTools::build_nodes_to_elem_map
2513 138896 : for (auto n : make_range(elem->n_vertices()))
2514 : {
2515 49968 : libmesh_assert_less (elem->id(), this->max_elem_id());
2516 :
2517 137580 : node_to_elem[elem->node_id(n)].push_back(elem->id());
2518 : }
2519 231 : }
2520 :
2521 : // Automatically set interior parents
2522 49660 : 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 36344 : if (skip_dimension_for_interior_parents[element->dim()] || element->interior_parent())
2527 5785 : 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 50855 : std::vector<std::set<dof_id_type>> neighbors( element->n_vertices() );
2536 :
2537 13454 : bool found_interior_parents = true;
2538 :
2539 30880 : for (auto n : make_range(element->n_vertices()))
2540 : {
2541 37680 : auto it = node_to_elem.find(element->node_id(n));
2542 :
2543 : // Check at first that this node is not isolated.
2544 30776 : if (it == node_to_elem.end())
2545 : {
2546 2768 : found_interior_parents = false;
2547 13420 : break; // out of n-loop
2548 : }
2549 :
2550 135376 : for (const auto & vertex_neighbor_id : it->second)
2551 111053 : if (this->elem_ref(vertex_neighbor_id).dim() == element->dim()+1)
2552 867 : neighbors[n].insert(vertex_neighbor_id);
2553 :
2554 29753 : 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 173 : for (const auto & interior_parent_id : neighbors_0)
2572 : {
2573 46 : found_interior_parents = false;
2574 265 : for (auto n : make_range(1u, element->n_vertices()))
2575 : {
2576 161 : 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 146 : if (found_interior_parents)
2588 : {
2589 77 : 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 104 : if (separate_interior_mesh)
2599 0 : libmesh_not_implemented_msg
2600 : ("interior_parent() values in multiple meshes are unsupported.");
2601 : }
2602 10494 : }
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 679 : _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 140 : 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 140 : if (!_disjoint_neighbor_boundary_pairs)
2622 120 : _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 180 : PeriodicBoundary forward(translation);
2628 140 : PeriodicBoundary inverse(translation * -1.0);
2629 :
2630 140 : forward.myboundary = b1;
2631 140 : forward.pairedboundary = b2;
2632 140 : inverse.myboundary = b2;
2633 140 : inverse.pairedboundary = b1;
2634 :
2635 : // Add both directions into the container
2636 140 : db.emplace(b1, forward.clone());
2637 180 : db.emplace(b2, inverse.clone());
2638 140 : }
2639 :
2640 88182 : PeriodicBoundaries * MeshBase::get_disjoint_neighbor_boundary_pairs()
2641 : {
2642 88182 : return _disjoint_neighbor_boundary_pairs.get();
2643 : }
2644 :
2645 155827 : const PeriodicBoundaries * MeshBase::get_disjoint_neighbor_boundary_pairs() const
2646 : {
2647 155827 : return _disjoint_neighbor_boundary_pairs.get();
2648 : }
2649 :
2650 21 : 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 21 : 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 18 : 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 42 : if (it != pb_map.end())
2666 : {
2667 12 : const auto & pb = *(it->second);
2668 : // Check both directions
2669 42 : if ((pb.myboundary == key && pb.pairedboundary == pair) ||
2670 0 : (pb.pairedboundary == key && pb.myboundary == pair))
2671 30 : pb_map.erase(it);
2672 : }
2673 42 : };
2674 :
2675 21 : erase_if_match(b1, b2, pairs);
2676 21 : 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 42 : Real MeshBase::get_point_locator_close_to_point_tol() const
2699 : {
2700 42 : return _point_locator_close_to_point_tol;
2701 : }
2702 :
2703 :
2704 :
2705 671 : 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 671 : (this->element_stored_range(),
2711 1719 : [new_size, this](Elem * elem)
2712 : {
2713 1337 : elem->add_extra_integers(new_size, this->_elem_integer_default_values);
2714 382 : });
2715 671 : }
2716 :
2717 :
2718 :
2719 2562 : void MeshBase::size_node_extra_integers()
2720 : {
2721 1574 : const std::size_t new_size = _node_integer_names.size();
2722 : Threads::parallel_for_each
2723 2562 : (this->node_stored_range(),
2724 38637 : [this, new_size](Node * node)
2725 : {
2726 30051 : node->add_extra_integers(new_size, _node_integer_default_values);
2727 8586 : });
2728 2562 : }
2729 :
2730 :
2731 : std::pair<std::vector<unsigned int>, std::vector<unsigned int>>
2732 21294 : MeshBase::merge_extra_integer_names(const MeshBase & other)
2733 : {
2734 19330 : std::pair<std::vector<unsigned int>, std::vector<unsigned int>> returnval;
2735 21294 : returnval.first = this->add_elem_integers(other._elem_integer_names, true, &other._elem_integer_default_values);
2736 21294 : returnval.second = this->add_node_integers(other._node_integer_names, true, &other._node_integer_default_values);
2737 21294 : return returnval;
2738 : }
2739 :
2740 :
2741 :
2742 : void
2743 42 : 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 42 : _default_ghosting->set_mesh(this);
2752 :
2753 : // _ghosting_functors
2754 42 : _ghosting_functors = std::move(other_mesh._ghosting_functors);
2755 :
2756 84 : for (const auto gf : _ghosting_functors )
2757 : {
2758 42 : gf->set_mesh(this);
2759 : }
2760 :
2761 : // _shared_functors
2762 12 : _shared_functors = std::move(other_mesh._shared_functors);
2763 :
2764 42 : 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 42 : _n_constraint_rows = other_mesh._n_constraint_rows;
2772 :
2773 42 : if (other_mesh.partitioner())
2774 42 : _partitioner = std::move(other_mesh.partitioner());
2775 42 : }
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 38995 : bool MeshBase::nodes_and_elements_equal(const MeshBase & other_mesh) const
2790 : {
2791 5821172 : for (const auto & other_node : other_mesh.node_ptr_range())
2792 : {
2793 5718365 : const Node * node = this->query_node_ptr(other_node->id());
2794 5718365 : if (!node)
2795 0 : return false;
2796 5718365 : if (*other_node != *node)
2797 0 : return false;
2798 607 : }
2799 5821172 : for (const auto & node : this->node_ptr_range())
2800 5718365 : if (!other_mesh.query_node_ptr(node->id()))
2801 607 : return false;
2802 :
2803 3211498 : for (const auto & other_elem : other_mesh.element_ptr_range())
2804 : {
2805 3149574 : const Elem * elem = this->query_elem_ptr(other_elem->id());
2806 3149574 : if (!elem)
2807 0 : return false;
2808 3149574 : if (!other_elem->topologically_equal(*elem))
2809 6 : return false;
2810 607 : }
2811 3211462 : for (const auto & elem : this->element_ptr_range())
2812 3149553 : if (!other_mesh.query_elem_ptr(elem->id()))
2813 598 : return false;
2814 :
2815 38974 : return true;
2816 : }
2817 :
2818 :
2819 201715 : dof_id_type MeshBase::count_constraint_rows()
2820 : {
2821 201715 : dof_id_type n_local_rows=0, n_unpartitioned_rows=0;
2822 322151 : for (const auto & [node, node_constraints] : _constraint_rows)
2823 : {
2824 : // Unpartitioned nodes
2825 120436 : if (node->processor_id() == DofObject::invalid_processor_id)
2826 61098 : n_unpartitioned_rows++;
2827 72508 : else if (node->processor_id() == this->processor_id())
2828 32621 : n_local_rows++;
2829 : }
2830 :
2831 201715 : this->comm().sum(n_local_rows);
2832 :
2833 201715 : return n_unpartitioned_rows + n_local_rows;
2834 : }
2835 :
2836 :
2837 : void
2838 21119 : MeshBase::copy_constraint_rows(const MeshBase & other_mesh)
2839 : {
2840 39352 : LOG_SCOPE("copy_constraint_rows(mesh)", "MeshBase");
2841 :
2842 21119 : _n_constraint_rows = other_mesh._n_constraint_rows;
2843 :
2844 20072 : _constraint_rows.clear();
2845 :
2846 19280 : const auto & other_constraint_rows = other_mesh.get_constraint_rows();
2847 45358 : for (const auto & [other_node, other_node_constraints] : other_constraint_rows)
2848 : {
2849 24239 : const Node * const our_node = this->node_ptr(other_node->id());
2850 30800 : constraint_rows_mapped_type our_node_constraints;
2851 84402 : 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 60163 : const Elem * const our_elem = this->elem_ptr(other_elem->id());
2855 60163 : our_node_constraints.emplace_back(std::make_pair(our_elem, local_node_id), constraint_value);
2856 : }
2857 24239 : _constraint_rows[our_node] = std::move(our_node_constraints);
2858 : }
2859 21119 : }
2860 :
2861 :
2862 : template <typename T>
2863 : void
2864 60 : 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 96 : MeshSerializer serialize(*this);
2875 :
2876 : // Our current mesh should already reflect the desired assembly space
2877 60 : 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 102 : 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 1095 : for (auto i : make_range(constraint_operator.row_start(),
2911 24 : constraint_operator.row_stop()))
2912 : {
2913 640 : std::vector<numeric_index_type> indices;
2914 640 : std::vector<T> values;
2915 :
2916 1035 : constraint_operator.get_row(i, indices, values);
2917 320 : libmesh_assert_equal_to(indices.size(), values.size());
2918 :
2919 1435 : if (indices.size() == 1 &&
2920 240 : 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 278 : if (existing_unconstrained_columns.find(indices[0]) !=
2926 148 : existing_unconstrained_columns.end())
2927 : {
2928 12 : const auto j = indices[0];
2929 12 : columns[j].emplace_back(i, 1);
2930 : }
2931 : else
2932 : {
2933 195 : existing_unconstrained_nodes.insert(i);
2934 266 : existing_unconstrained_columns.emplace(indices[0],i);
2935 : }
2936 : }
2937 : else
2938 6108 : for (auto jj : index_range(indices))
2939 : {
2940 5351 : const auto j = indices[jj];
2941 7122 : const Real coef = libmesh_real(values[jj]);
2942 1771 : libmesh_assert_equal_to(coef, values[jj]);
2943 5351 : columns[j].emplace_back(i, coef);
2944 : }
2945 : }
2946 :
2947 : // Merge data from different processors' slabs of the matrix
2948 60 : this->comm().set_union(existing_unconstrained_nodes);
2949 60 : this->comm().set_union(existing_unconstrained_columns);
2950 :
2951 54 : std::vector<columns_type> all_columns;
2952 60 : this->comm().allgather(columns, all_columns);
2953 :
2954 18 : columns.clear();
2955 174 : for (auto p : index_range(all_columns))
2956 2030 : for (auto & [j, subcol] : all_columns[p])
2957 12601 : for (auto [i, v] : subcol)
2958 10685 : 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 7470 : for (const Elem * elem : this->element_ptr_range())
2971 : {
2972 4368 : subdomain_ids.insert(elem->subdomain_id());
2973 21294 : for (auto n : make_range(elem->n_nodes()))
2974 : {
2975 16926 : const Node * node = elem->node_ptr(n);
2976 16926 : if (existing_unconstrained_nodes.count(node->id()))
2977 1166 : node_to_elem_ptrs.emplace(node, std::make_pair(elem->id(), n));
2978 : }
2979 : }
2980 :
2981 60 : 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 60 : dof_id_type next_node_id = this->max_node_id();
2988 60 : 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 1609 : for (auto j : make_range(constraint_operator.n()))
2998 : {
2999 : // If we already have a good node for this then we're done
3000 603 : if (existing_unconstrained_columns.count(j))
3001 479 : 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 10507 : for (auto [i, r] : column)
3018 : {
3019 9706 : Node & constrained_node = this->node_ref(i);
3020 9706 : const Point constrained_pt = constrained_node;
3021 3232 : newpt += r*constrained_pt;
3022 9706 : total_scaling += r;
3023 9706 : ++total_entries;
3024 9706 : ++pids[constrained_node.processor_id()];
3025 : }
3026 :
3027 801 : if (precondition_constraint_operator)
3028 0 : column_sums[j] = total_scaling;
3029 :
3030 801 : 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 801 : if (total_scaling > TOLERANCE)
3039 266 : newpt /= total_scaling;
3040 : else
3041 0 : newpt /= total_entries;
3042 :
3043 801 : Node *n = this->add_point(newpt, next_node_id++);
3044 1067 : std::unique_ptr<Elem> elem = Elem::build(NODEELEM);
3045 801 : elem->set_node(0, n);
3046 801 : elem->subdomain_id() = new_sbd_id;
3047 801 : elem->set_id(next_elem_id++);
3048 :
3049 1067 : Elem * added_elem = this->add_elem(std::move(elem));
3050 801 : this->_elem_dims.insert(0);
3051 801 : this->_elem_default_orders.insert(added_elem->default_order());
3052 801 : this->_supported_nodal_order =
3053 269 : static_cast<Order>
3054 1602 : (std::min(static_cast<int>(this->_supported_nodal_order),
3055 801 : static_cast<int>(added_elem->supported_nodal_order())));
3056 535 : this->_mesh_subdomains.insert(new_sbd_id);
3057 801 : node_to_elem_ptrs.emplace(n, std::make_pair(added_elem->id(), 0));
3058 801 : 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 2112 : for (auto [pid, count] : pids)
3065 1311 : if (count >= n_pids)
3066 : {
3067 326 : n_pids = count;
3068 981 : added_elem->processor_id() = pid;
3069 981 : 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 1095 : for (auto i : make_range(constraint_operator.row_start(),
3080 24 : constraint_operator.row_stop()))
3081 : {
3082 444 : if (existing_unconstrained_nodes.count(i))
3083 266 : continue;
3084 :
3085 498 : std::vector<numeric_index_type> indices;
3086 498 : std::vector<T> values;
3087 :
3088 769 : 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 6132 : for (auto jj : index_range(indices))
3093 : {
3094 7137 : const dof_id_type node_id =
3095 : existing_unconstrained_columns[indices[jj]];
3096 :
3097 5363 : Node & constraining_node = this->node_ref(node_id);
3098 :
3099 1774 : libmesh_assert(node_to_elem_ptrs.count(&constraining_node));
3100 :
3101 5363 : auto p = node_to_elem_ptrs[&constraining_node];
3102 :
3103 5322 : 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 5363 : 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 7137 : 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 60 : this->comm().set_union(indexed_constraint_rows);
3127 :
3128 : // Add constraint rows as mesh constraint rows
3129 1576 : for (auto & [node_id, indexed_row] : indexed_constraint_rows)
3130 : {
3131 1516 : Node * constrained_node = this->node_ptr(node_id);
3132 :
3133 996 : constraint_rows_mapped_type constraint_row;
3134 :
3135 12201 : for (auto [p, coef] : indexed_row)
3136 : {
3137 10685 : const Elem * elem = this->elem_ptr(p.first);
3138 7096 : constraint_row.emplace_back
3139 14233 : (std::make_pair(std::make_pair(elem, p.second), coef));
3140 : }
3141 :
3142 1018 : this->_constraint_rows.emplace(constrained_node,
3143 498 : std::move(constraint_row));
3144 : }
3145 :
3146 60 : _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 84 : }
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 119616 : MeshBase::Preparation::Preparation() :
3216 22380 : is_partitioned(false),
3217 22380 : has_synched_id_counts(false),
3218 22380 : has_neighbor_ptrs(false),
3219 22380 : has_cached_elem_data(false),
3220 22380 : has_interior_parent_ptrs(false),
3221 22380 : has_removed_remote_elements(false),
3222 22380 : has_removed_orphaned_nodes(false),
3223 22380 : has_boundary_id_sets(false),
3224 22380 : has_reinit_ghosting_functors(false),
3225 119616 : has_synched_subdomain_name_map(false)
3226 119616 : {}
3227 :
3228 202782 : MeshBase::Preparation::operator bool() const
3229 : {
3230 401319 : return is_partitioned &&
3231 198537 : has_synched_id_counts &&
3232 198537 : has_neighbor_ptrs &&
3233 198537 : has_cached_elem_data &&
3234 198531 : has_interior_parent_ptrs &&
3235 198531 : has_removed_remote_elements &&
3236 198531 : has_removed_orphaned_nodes &&
3237 198531 : has_reinit_ghosting_functors &&
3238 541411 : has_boundary_id_sets &&
3239 339879 : has_synched_subdomain_name_map;
3240 : }
3241 :
3242 : MeshBase::Preparation &
3243 281929 : MeshBase::Preparation::operator= (bool set_all)
3244 : {
3245 281929 : is_partitioned = set_all;
3246 281929 : has_synched_id_counts = set_all;
3247 281929 : has_neighbor_ptrs = set_all;
3248 281929 : has_cached_elem_data = set_all;
3249 281929 : has_interior_parent_ptrs = set_all;
3250 281929 : has_removed_remote_elements = set_all;
3251 281929 : has_removed_orphaned_nodes = set_all;
3252 281929 : has_reinit_ghosting_functors = set_all;
3253 281929 : has_boundary_id_sets = set_all;
3254 281929 : has_synched_subdomain_name_map = set_all;
3255 :
3256 281929 : return *this;
3257 : }
3258 :
3259 : bool
3260 105577 : MeshBase::Preparation::operator== (const Preparation & other) const
3261 : {
3262 105577 : if (is_partitioned != other.is_partitioned)
3263 28516 : return false;
3264 76939 : if (has_synched_id_counts != other.has_synched_id_counts)
3265 6 : return false;
3266 76933 : if (has_neighbor_ptrs != other.has_neighbor_ptrs)
3267 388 : return false;
3268 76545 : if (has_cached_elem_data != other.has_cached_elem_data)
3269 0 : return false;
3270 76545 : if (has_interior_parent_ptrs != other.has_interior_parent_ptrs)
3271 0 : return false;
3272 76545 : if (has_removed_remote_elements != other.has_removed_remote_elements)
3273 19042 : return false;
3274 57503 : if (has_removed_orphaned_nodes != other.has_removed_orphaned_nodes)
3275 0 : return false;
3276 57503 : if (has_reinit_ghosting_functors != other.has_reinit_ghosting_functors)
3277 0 : return false;
3278 57503 : if (has_boundary_id_sets != other.has_boundary_id_sets)
3279 0 : return false;
3280 57503 : if (has_synched_subdomain_name_map != other.has_synched_subdomain_name_map)
3281 0 : return false;
3282 :
3283 56470 : return true;
3284 : }
3285 :
3286 : bool
3287 39037 : MeshBase::Preparation::operator!= (const Preparation & other) const
3288 : {
3289 39037 : return !(*this == other);
3290 : }
3291 :
3292 :
3293 : void
3294 119229 : MeshBase::Preparation::libmesh_assert_consistent (const Parallel::Communicator & libmesh_dbg_var(comm))
3295 : {
3296 47878 : libmesh_assert(comm.verify(is_partitioned));
3297 47878 : libmesh_assert(comm.verify(has_synched_id_counts));
3298 47878 : libmesh_assert(comm.verify(has_neighbor_ptrs));
3299 47878 : libmesh_assert(comm.verify(has_cached_elem_data));
3300 47878 : libmesh_assert(comm.verify(has_interior_parent_ptrs));
3301 47878 : libmesh_assert(comm.verify(has_removed_remote_elements));
3302 47878 : libmesh_assert(comm.verify(has_removed_orphaned_nodes));
3303 47878 : libmesh_assert(comm.verify(has_reinit_ghosting_functors));
3304 47878 : libmesh_assert(comm.verify(has_boundary_id_sets));
3305 119229 : }
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
|