libMesh
Loading...
Searching...
No Matches
mesh_base.C
Go to the documentation of this file.
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
57namespace libMesh
58{
59
60
61
62// ------------------------------------------------------------
63// MeshBase class member functions
65 unsigned char d) :
66 ParallelObject (comm_in),
67 boundary_info (new BoundaryInfo(*this)), // BoundaryInfo has protected ctor, can't use std::make_unique
68 _n_parts (1),
69 _default_mapping_type(LAGRANGE_MAP),
70 _default_mapping_data(0),
71 _preparation (),
72 _element_stored_range (),
73 _const_active_local_element_stored_range (),
74 _point_locator (),
75 _count_lower_dim_elems_in_point_locator(true),
76 _partitioner (),
77#ifdef LIBMESH_ENABLE_UNIQUE_ID
78 _next_unique_id(DofObject::invalid_unique_id),
79#endif
80 _interior_mesh(this),
81 _skip_noncritical_partitioning(false),
82 _skip_all_partitioning(libMesh::on_command_line("--skip-partitioning")),
83 _skip_renumber_nodes_and_elements(false),
84 _skip_find_neighbors(false),
85 _skip_detect_interior_parents(false),
86 _allow_remote_element_removal(true),
87 _allow_node_and_elem_unique_id_overlap(false),
88 _spatial_dimension(d),
89 _default_ghosting(std::make_unique<GhostPointNeighbors>(*this)),
90 _point_locator_close_to_point_tol(0.)
91{
92 _elem_dims.insert(d);
94 libmesh_assert_less_equal (LIBMESH_DIM, 3);
95 libmesh_assert_greater_equal (LIBMESH_DIM, d);
97}
98
99
100
101MeshBase::MeshBase (const MeshBase & other_mesh) :
102 ParallelObject (other_mesh),
103 boundary_info (new BoundaryInfo(*this)), // BoundaryInfo has protected ctor, can't use std::make_unique
104 _n_parts (other_mesh._n_parts),
105 _default_mapping_type(other_mesh._default_mapping_type),
106 _default_mapping_data(other_mesh._default_mapping_data),
107 _preparation (other_mesh._preparation),
108 _element_stored_range (),
109 _const_active_local_element_stored_range (),
110 _point_locator (),
111 _count_lower_dim_elems_in_point_locator(other_mesh._count_lower_dim_elems_in_point_locator),
112 _partitioner (),
113#ifdef LIBMESH_ENABLE_UNIQUE_ID
114 _next_unique_id(other_mesh._next_unique_id),
115#endif
116 // If the other mesh interior_parent pointers just go back to
117 // itself, so should we
118 _interior_mesh((other_mesh._interior_mesh == &other_mesh) ?
119 this : other_mesh._interior_mesh),
120 _skip_noncritical_partitioning(other_mesh._skip_noncritical_partitioning),
121 _skip_all_partitioning(other_mesh._skip_all_partitioning),
122 _skip_renumber_nodes_and_elements(other_mesh._skip_renumber_nodes_and_elements),
123 _skip_find_neighbors(other_mesh._skip_find_neighbors),
124 _skip_detect_interior_parents(other_mesh._skip_detect_interior_parents),
125 _allow_remote_element_removal(other_mesh._allow_remote_element_removal),
126 _allow_node_and_elem_unique_id_overlap(other_mesh._allow_node_and_elem_unique_id_overlap),
127 _elem_dims(other_mesh._elem_dims),
128 _elem_default_orders(other_mesh._elem_default_orders),
129 _supported_nodal_order(other_mesh._supported_nodal_order),
130 _mesh_subdomains(other_mesh._mesh_subdomains),
131 _elemset_codes_inverse_map(other_mesh._elemset_codes_inverse_map),
132 _all_elemset_ids(other_mesh._all_elemset_ids),
133 _spatial_dimension(other_mesh._spatial_dimension),
134 _default_ghosting(std::make_unique<GhostPointNeighbors>(*this)),
135 _point_locator_close_to_point_tol(other_mesh._point_locator_close_to_point_tol)
136{
137 const GhostingFunctor * const other_default_ghosting = other_mesh._default_ghosting.get();
138
139 for (GhostingFunctor * const gf : other_mesh._ghosting_functors)
140 {
141 // If the other mesh is using default ghosting, then we will use our own
142 // default ghosting
143 if (gf == other_default_ghosting)
144 {
145 _ghosting_functors.push_back(_default_ghosting.get());
146 continue;
147 }
148
149 std::shared_ptr<GhostingFunctor> clone_gf = gf->clone();
150 // Some subclasses of GhostingFunctor might not override the
151 // clone function yet. If this is the case, GhostingFunctor will
152 // return nullptr by default. The clone function should be overridden
153 // in all derived classes. This following code ("else") is written
154 // for API upgrade. That will allow users gradually to update their code.
155 // Once the API upgrade is done, we will come back and delete "else."
156 if (clone_gf)
157 {
158 clone_gf->set_mesh(this);
159 add_ghosting_functor(clone_gf);
160 }
161 else
162 {
163 libmesh_deprecated();
165 }
166 }
167
168 if (other_mesh._partitioner.get())
169 _partitioner = other_mesh._partitioner->clone();
170
171#ifdef LIBMESH_ENABLE_PERIODIC
172 // Deep copy of all periodic boundaries
174 {
175 _disjoint_neighbor_boundary_pairs = std::make_unique<PeriodicBoundaries>();
176
177 for (const auto & [id, pb] : *other_mesh._disjoint_neighbor_boundary_pairs)
178 if (pb)
179 (*_disjoint_neighbor_boundary_pairs)[id] = pb->clone();
180 }
181#endif
182
183 // _elemset_codes stores pointers to entries in _elemset_codes_inverse_map,
184 // so it is not possible to simply copy it directly from other_mesh
185 for (const auto & [set, code] : _elemset_codes_inverse_map)
186 _elemset_codes.emplace(code, &set);
187}
188
190{
191 LOG_SCOPE("operator=(&&)", "MeshBase");
192
193 // Move assign as a ParallelObject.
194 this->ParallelObject::operator=(other_mesh);
195
196 _n_parts = other_mesh.n_partitions();
197 _default_mapping_type = other_mesh.default_mapping_type();
198 _default_mapping_data = other_mesh.default_mapping_data();
199 _preparation = other_mesh._preparation;
200 _element_stored_range = std::move(other_mesh._element_stored_range);
201 _const_active_local_element_stored_range = std::move(other_mesh._const_active_local_element_stored_range);
202 _point_locator = std::move(other_mesh._point_locator);
203 _count_lower_dim_elems_in_point_locator = other_mesh.get_count_lower_dim_elems_in_point_locator();
204#ifdef LIBMESH_ENABLE_UNIQUE_ID
205 _next_unique_id = other_mesh.next_unique_id();
206#endif
207 // If the other mesh interior_parent pointers just go back to
208 // itself, so should we
209 _interior_mesh = (other_mesh._interior_mesh == &other_mesh) ?
210 this : other_mesh._interior_mesh;
211 _skip_noncritical_partitioning = other_mesh.skip_noncritical_partitioning();
212 _skip_all_partitioning = other_mesh.skip_partitioning();
213 _skip_renumber_nodes_and_elements = !(other_mesh.allow_renumbering());
214 _skip_find_neighbors = !(other_mesh.allow_find_neighbors());
215 _skip_detect_interior_parents = !(other_mesh.allow_detect_interior_parents());
216 _allow_remote_element_removal = other_mesh.allow_remote_element_removal();
217 _allow_node_and_elem_unique_id_overlap = other_mesh.allow_node_and_elem_unique_id_overlap();
218 _block_id_to_name = std::move(other_mesh._block_id_to_name);
219 _elem_dims = std::move(other_mesh.elem_dimensions());
220 _elem_default_orders = std::move(other_mesh.elem_default_orders());
221 _supported_nodal_order = other_mesh.supported_nodal_order();
222 _mesh_subdomains = other_mesh._mesh_subdomains;
223 _elemset_codes = std::move(other_mesh._elemset_codes);
224 _elemset_codes_inverse_map = std::move(other_mesh._elemset_codes_inverse_map);
225 _all_elemset_ids = std::move(other_mesh._all_elemset_ids);
226 _spatial_dimension = other_mesh.spatial_dimension();
227 _elem_integer_names = std::move(other_mesh._elem_integer_names);
228 _elem_integer_default_values = std::move(other_mesh._elem_integer_default_values);
229 _node_integer_names = std::move(other_mesh._node_integer_names);
230 _node_integer_default_values = std::move(other_mesh._node_integer_default_values);
231 _point_locator_close_to_point_tol = other_mesh.get_point_locator_close_to_point_tol();
232
233#ifdef LIBMESH_ENABLE_PERIODIC
234 // Deep copy of all periodic boundaries:
235 // We must clone each PeriodicBoundaryBase in the source map,
236 // since unique_ptr cannot be copied and we need independent instances
237 if (other_mesh._disjoint_neighbor_boundary_pairs)
238 {
239 _disjoint_neighbor_boundary_pairs = std::make_unique<PeriodicBoundaries>();
240
241 for (const auto & [id, pb] : *other_mesh._disjoint_neighbor_boundary_pairs)
242 if (pb)
243 (*_disjoint_neighbor_boundary_pairs)[id] = pb->clone();
244 }
245#endif
246
247 // This relies on our subclasses *not* invalidating pointers when we
248 // do their portion of the move assignment later!
249 boundary_info = std::move(other_mesh.boundary_info);
250 boundary_info->set_mesh(*this);
251
252#ifdef DEBUG
253 // Make sure that move assignment worked for pointers
254 for (const auto & [set, code] : _elemset_codes_inverse_map)
255 {
256 auto it = _elemset_codes.find(code);
257 libmesh_assert_msg(it != _elemset_codes.end(),
258 "Elemset code " << code << " not found in _elmset_codes container.");
259 libmesh_assert_equal_to(it->second, &set);
260 }
261#endif
262
263 // We're *not* really done at this point, but we have the problem
264 // that some of our data movement might be expecting subclasses data
265 // movement to happen first. We'll let subclasses handle that by
266 // calling our post_dofobject_moves()
267 return *this;
268}
269
270
271bool MeshBase::operator== (const MeshBase & other_mesh) const
272{
273 LOG_SCOPE("operator==()", "MeshBase");
274
275 bool is_equal = this->locally_equals(other_mesh);
276 this->comm().min(is_equal);
277 return is_equal;
278}
279
280
281bool MeshBase::locally_equals (const MeshBase & other_mesh) const
282{
283 // Check whether (almost) everything in the base is equal
284 //
285 // We don't check _next_unique_id here, because it's expected to
286 // change in a DistributedMesh prepare_for_use(); it's conceptually
287 // "mutable".
288 //
289 // We use separate if statements instead of logical operators here,
290 // to make it easy to see the failing condition when using a
291 // debugger to figure out why a MeshTools::valid_is_prepared(mesh)
292 // is failing.
293 if (_n_parts != other_mesh._n_parts)
294 return false;
296 return false;
298 return false;
299 if (_preparation != other_mesh._preparation)
300 return false;
303 return false;
304
305 // We should either both have our own interior parents or both not;
306 // but if we both don't then we can't really assert anything else
307 // because pointing at the same interior mesh is fair but so is
308 // pointing at two different copies of "the same" interior mesh.
309 if ((_interior_mesh == this) !=
310 (other_mesh._interior_mesh == &other_mesh))
311 return false;
312
314 return false;
316 return false;
318 return false;
320 return false;
322 return false;
324 return false;
326 return false;
327 if (_spatial_dimension != other_mesh._spatial_dimension)
328 return false;
330 return false;
331 if (_block_id_to_name != other_mesh._block_id_to_name)
332 return false;
333 if (_elem_dims != other_mesh._elem_dims)
334 return false;
336 return false;
338 return false;
339 if (_mesh_subdomains != other_mesh._mesh_subdomains)
340 return false;
341 if (_all_elemset_ids != other_mesh._all_elemset_ids)
342 return false;
344 return false;
346 return false;
348 return false;
350 return false;
351 if (static_cast<bool>(_default_ghosting) != static_cast<bool>(other_mesh._default_ghosting))
352 return false;
353 if (static_cast<bool>(_partitioner) != static_cast<bool>(other_mesh._partitioner))
354 return false;
355 if (*boundary_info != *other_mesh.boundary_info)
356 return false;
357
358 // First check whether the "existence" of the two pointers differs (one present, one absent)
359 if (static_cast<bool>(_disjoint_neighbor_boundary_pairs) !=
360 static_cast<bool>(other_mesh._disjoint_neighbor_boundary_pairs))
361 return false;
362 // If both exist, compare the contents (Weak Test: just compare sizes like `_ghosting_functors`)
365 return false;
366
367 const constraint_rows_type & other_rows =
368 other_mesh.get_constraint_rows();
369 for (const auto & [node, row] : this->_constraint_rows)
370 {
371 const dof_id_type node_id = node->id();
372 const Node * other_node = other_mesh.query_node_ptr(node_id);
373 if (!other_node)
374 return false;
375
376 auto it = other_rows.find(other_node);
377 if (it == other_rows.end())
378 return false;
379
380 const auto & other_row = it->second;
381 if (row.size() != other_row.size())
382 return false;
383
384 for (auto i : index_range(row))
385 {
386 const auto & [elem_pair, coef] = row[i];
387 const auto & [other_elem_pair, other_coef] = other_row[i];
388 libmesh_assert(elem_pair.first);
389 libmesh_assert(other_elem_pair.first);
390 if (elem_pair.first->id() !=
391 other_elem_pair.first->id() ||
392 elem_pair.second !=
393 other_elem_pair.second ||
394 coef != other_coef)
395 return false;
396 }
397 }
398
399 for (const auto & [elemset_code, elemset_ptr] : this->_elemset_codes)
400 if (const auto it = other_mesh._elemset_codes.find(elemset_code);
401 it == other_mesh._elemset_codes.end() || *elemset_ptr != *it->second)
402 return false;
403
404 // FIXME: we have no good way to compare ghosting functors, since
405 // they're in a vector of pointers, and we have no way *at all*
406 // to compare ghosting functors, since they don't have operator==
407 // defined and we encourage users to subclass them. We can check if
408 // we have the same number, is all.
409 if (_ghosting_functors.size() !=
410 other_mesh._ghosting_functors.size())
411 return false;
412
413 // Same deal for partitioners. We tested that we both have one or
414 // both don't, but are they equivalent? Let's guess "yes".
415
416 // Now let the subclasses decide whether everything else is equal
417 return this->subclass_locally_equals(other_mesh);
418}
419
420
422{
423 this->MeshBase::clear();
424
425 libmesh_exceptionless_assert (!libMesh::closed());
426}
427
428
429
430unsigned int MeshBase::mesh_dimension() const
431{
432 if (!_elem_dims.empty())
433 return cast_int<unsigned int>(*_elem_dims.rbegin());
434 return 0;
435}
436
437
438
439void MeshBase::set_elem_dimensions(std::set<unsigned char> elem_dims)
440{
441#ifdef DEBUG
442 // In debug mode, we call cache_elem_data() and then make sure
443 // the result actually agrees with what the user specified.
444 parallel_object_only();
445
446 this->cache_elem_data();
447 libmesh_assert_msg(_elem_dims == elem_dims, \
448 "Specified element dimensions does not match true element dimensions!");
449#endif
450
451 _elem_dims = std::move(elem_dims);
452}
453
454
455
457{
458 // Populate inverse map, stealing id_set's resources
459 auto [it1, inserted1] = _elemset_codes_inverse_map.emplace(std::move(id_set), code);
460
461 // Reference to the newly inserted (or previously existing) id_set
462 const auto & inserted_id_set = it1->first;
463
464 // Keep track of all elemset ids ever added for O(1) n_elemsets()
465 // performance. Only need to do this if we didn't know about this
466 // id_set before...
467 if (inserted1)
468 _all_elemset_ids.insert(inserted_id_set.begin(), inserted_id_set.end());
469
470 // Take the address of the newly emplaced set to use in
471 // _elemset_codes, avoid duplicating std::set storage
472 auto [it2, inserted2] = _elemset_codes.emplace(code, &inserted_id_set);
473
474 // Throw an error if this code already exists with a pointer to a
475 // different set of ids.
476 libmesh_error_msg_if(!inserted2 && it2->second != &inserted_id_set,
477 "The elemset code " << code << " already exists with a different id_set.");
478}
479
480
481
482unsigned int MeshBase::n_elemsets() const
483{
484 return _all_elemset_ids.size();
485}
486
487void MeshBase::get_elemsets(dof_id_type elemset_code, MeshBase::elemset_type & id_set_to_fill) const
488{
489 // If we don't recognize this elemset_code, hand back an empty set
490 id_set_to_fill.clear();
491
492 if (const auto it = _elemset_codes.find(elemset_code);
493 it != _elemset_codes.end())
494 id_set_to_fill.insert(it->second->begin(), it->second->end());
495}
496
498{
499 auto it = _elemset_codes_inverse_map.find(id_set);
500 return (it == _elemset_codes_inverse_map.end()) ? DofObject::invalid_id : it->second;
501}
502
503std::vector<dof_id_type> MeshBase::get_elemset_codes() const
504{
505 std::vector<dof_id_type> ret;
506 ret.reserve(_elemset_codes.size());
507 for (const auto & pr : _elemset_codes)
508 ret.push_back(pr.first);
509 return ret;
510}
511
513{
514 // Look up elemset ids for old_code
515 auto it = _elemset_codes.find(old_code);
516
517 // If we don't have the old_code, then do nothing. Alternatively, we
518 // could throw an error since trying to change an elemset code you
519 // don't have could indicate there's a problem...
520 if (it == _elemset_codes.end())
521 return;
522
523 // Make copy of the set of elemset ids. We are not changing these,
524 // only updating the elemset code it corresponds to.
525 elemset_type id_set_copy = *(it->second);
526
527 // Look up the corresponding entry in the inverse map. Note: we want
528 // the iterator because we are going to remove it.
529 auto inverse_it = _elemset_codes_inverse_map.find(id_set_copy);
530 libmesh_error_msg_if(inverse_it == _elemset_codes_inverse_map.end(),
531 "Expected _elemset_codes_inverse_map entry for elemset code " << old_code);
532
533 // Erase entry from inverse map
534 _elemset_codes_inverse_map.erase(inverse_it);
535
536 // Erase entry from forward map
537 _elemset_codes.erase(it);
538
539 // Add new code with original set of ids.
540 this->add_elemset_code(new_code, id_set_copy);
541
542 // We can't update any actual elemset codes if there is no extra integer defined for it.
543 if (!this->has_elem_integer("elemset_code"))
544 return;
545
546 // Get index of elemset_code extra integer
547 unsigned int elemset_index = this->get_elem_integer_index("elemset_code");
548
549 // Loop over all elems and update code
551 (this->element_stored_range(),
552 [elemset_index, old_code, new_code](const ElemRange & range)
553 {
554 for (Elem * elem : range)
555 {
556 dof_id_type elemset_code =
557 elem->get_extra_integer(elemset_index);
558
559 if (elemset_code == old_code)
560 elem->set_extra_integer(elemset_index, new_code);
561 }
562 });
563}
564
566{
567 // Early return if we don't have old_id
568 if (!_all_elemset_ids.count(old_id))
569 return;
570
571 // Throw an error if the new_id is already used
572 libmesh_error_msg_if(_all_elemset_ids.count(new_id),
573 "Cannot change elemset id " << old_id <<
574 " to " << new_id << ", " << new_id << " already exists.");
575
576 // We will build up a new version of the inverse map so we can iterate over
577 // the current one without invalidating anything.
578 std::map<MeshBase::elemset_type, dof_id_type> new_elemset_codes_inverse_map;
579 for (const auto & [id_set, elemset_code] : _elemset_codes_inverse_map)
580 {
581 auto id_set_copy = id_set;
582 if (id_set_copy.count(old_id))
583 {
584 // Remove old_id, insert new_id
585 id_set_copy.erase(old_id);
586 id_set_copy.insert(new_id);
587 }
588
589 // Store in new version of map
590 new_elemset_codes_inverse_map.emplace(id_set_copy, elemset_code);
591 }
592
593 // Swap existing map with newly-built one
594 _elemset_codes_inverse_map.swap(new_elemset_codes_inverse_map);
595
596 // Reconstruct _elemset_codes map
597 _elemset_codes.clear();
598 for (const auto & [id_set, elemset_code] : _elemset_codes_inverse_map)
599 _elemset_codes.emplace(elemset_code, &id_set);
600
601 // Update _all_elemset_ids
602 _all_elemset_ids.erase(old_id);
603 _all_elemset_ids.insert(new_id);
604}
605
606unsigned int MeshBase::spatial_dimension () const
607{
608 return cast_int<unsigned int>(_spatial_dimension);
609}
610
611
612
614{
615 // The user can set the _spatial_dimension however they wish,
616 // libMesh will only *increase* the spatial dimension, however,
617 // never decrease it.
619}
620
621
622
623unsigned int MeshBase::add_elem_integer(std::string name,
624 bool allocate_data,
625 dof_id_type default_value)
626{
627 for (auto i : index_range(_elem_integer_names))
628 if (_elem_integer_names[i] == name)
629 {
630 libmesh_assert_less(i, _elem_integer_default_values.size());
631 _elem_integer_default_values[i] = default_value;
632 return i;
633 }
634
635 libmesh_assert_equal_to(_elem_integer_names.size(),
637 _elem_integer_names.push_back(std::move(name));
638 _elem_integer_default_values.push_back(default_value);
639 if (allocate_data)
641 return _elem_integer_names.size()-1;
642}
643
644
645
646std::vector<unsigned int> MeshBase::add_elem_integers(const std::vector<std::string> & names,
647 bool allocate_data,
648 const std::vector<dof_id_type> * default_values)
649{
650 libmesh_assert(!default_values || default_values->size() == names.size());
651 libmesh_assert_equal_to(_elem_integer_names.size(), _elem_integer_default_values.size());
652
653 std::unordered_map<std::string, std::size_t> name_indices;
654 for (auto i : index_range(_elem_integer_names))
655 name_indices[_elem_integer_names[i]] = i;
656
657 std::vector<unsigned int> returnval(names.size());
658
659 bool added_an_integer = false;
660 for (auto i : index_range(names))
661 {
662 const std::string & name = names[i];
663 if (const auto it = name_indices.find(name);
664 it != name_indices.end())
665 {
666 returnval[i] = it->second;
667 _elem_integer_default_values[it->second] =
668 default_values ? (*default_values)[i] : DofObject::invalid_id;
669 }
670 else
671 {
672 returnval[i] = _elem_integer_names.size();
673 name_indices[name] = returnval[i];
674 _elem_integer_names.push_back(name);
676 (default_values ? (*default_values)[i] : DofObject::invalid_id);
677 added_an_integer = true;
678 }
679 }
680
681 if (allocate_data && added_an_integer)
683
684 return returnval;
685}
686
687
688
689unsigned int MeshBase::get_elem_integer_index(std::string_view name) const
690{
691 for (auto i : index_range(_elem_integer_names))
692 if (_elem_integer_names[i] == name)
693 return i;
694
695 libmesh_error_msg("Unknown elem integer " << name);
697}
698
699
700
701bool MeshBase::has_elem_integer(std::string_view name) const
702{
703 for (auto & entry : _elem_integer_names)
704 if (entry == name)
705 return true;
706
707 return false;
708}
709
710
711
712unsigned int MeshBase::add_node_integer(std::string name,
713 bool allocate_data,
714 dof_id_type default_value)
715{
716 for (auto i : index_range(_node_integer_names))
717 if (_node_integer_names[i] == name)
718 {
719 libmesh_assert_less(i, _node_integer_default_values.size());
720 _node_integer_default_values[i] = default_value;
721 return i;
722 }
723
724 libmesh_assert_equal_to(_node_integer_names.size(),
726 _node_integer_names.push_back(std::move(name));
727 _node_integer_default_values.push_back(default_value);
728 if (allocate_data)
730 return _node_integer_names.size()-1;
731}
732
733
734
735std::vector<unsigned int> MeshBase::add_node_integers(const std::vector<std::string> & names,
736 bool allocate_data,
737 const std::vector<dof_id_type> * default_values)
738{
739 libmesh_assert(!default_values || default_values->size() == names.size());
740 libmesh_assert_equal_to(_node_integer_names.size(), _node_integer_default_values.size());
741
742 std::unordered_map<std::string, std::size_t> name_indices;
743 for (auto i : index_range(_node_integer_names))
744 name_indices[_node_integer_names[i]] = i;
745
746 std::vector<unsigned int> returnval(names.size());
747
748 bool added_an_integer = false;
749 for (auto i : index_range(names))
750 {
751 const std::string & name = names[i];
752 if (const auto it = name_indices.find(name);
753 it != name_indices.end())
754 {
755 returnval[i] = it->second;
756 _node_integer_default_values[it->second] =
757 default_values ? (*default_values)[i] : DofObject::invalid_id;
758 }
759 else
760 {
761 returnval[i] = _node_integer_names.size();
762 name_indices[name] = returnval[i];
763 _node_integer_names.push_back(name);
765 (default_values ? (*default_values)[i] : DofObject::invalid_id);
766 added_an_integer = true;
767 }
768 }
769
770 if (allocate_data && added_an_integer)
772
773 return returnval;
774}
775
776
777
778unsigned int MeshBase::get_node_integer_index(std::string_view name) const
779{
780 for (auto i : index_range(_node_integer_names))
781 if (_node_integer_names[i] == name)
782 return i;
783
784 libmesh_error_msg("Unknown node integer " << name);
786}
787
788
789
790bool MeshBase::has_node_integer(std::string_view name) const
791{
792 for (auto & entry : _node_integer_names)
793 if (entry == name)
794 return true;
795
796 return false;
797}
798
799
800
802{
803 LOG_SCOPE("remove_orphaned_nodes()", "MeshBase");
804
805 // Will hold the set of nodes that are currently connected to elements
806 std::unordered_set<Node *> connected_nodes;
807
808 // Loop over the elements. Find which nodes are connected to at
809 // least one of them.
810 for (const auto & element : this->element_ptr_range())
811 for (auto & n : element->node_ref_range())
812 connected_nodes.insert(&n);
813
814 for (const auto & node : this->node_ptr_range())
815 if (!connected_nodes.count(node))
816 this->delete_node(node);
817
819}
820
821
822
823#ifdef LIBMESH_ENABLE_DEPRECATED
824void MeshBase::prepare_for_use (const bool skip_renumber_nodes_and_elements, const bool skip_find_neighbors)
825{
826 libmesh_deprecated();
827
828 // We only respect the users wish if they tell us to skip renumbering. If they tell us not to
829 // skip renumbering but someone previously called allow_renumbering(false), then the latter takes
830 // precedence
831 if (skip_renumber_nodes_and_elements)
832 this->allow_renumbering(false);
833
834 // We always accept the user's value for skip_find_neighbors, in contrast to skip_renumber
835 const bool old_allow_find_neighbors = this->allow_find_neighbors();
836 this->allow_find_neighbors(!skip_find_neighbors);
837
838 this->prepare_for_use();
839
840 this->allow_find_neighbors(old_allow_find_neighbors);
841}
842
843void MeshBase::prepare_for_use (const bool skip_renumber_nodes_and_elements)
844{
845 libmesh_deprecated();
846
847 // We only respect the users wish if they tell us to skip renumbering. If they tell us not to
848 // skip renumbering but someone previously called allow_renumbering(false), then the latter takes
849 // precedence
850 if (skip_renumber_nodes_and_elements)
851 this->allow_renumbering(false);
852
853 this->prepare_for_use();
854}
855#endif // LIBMESH_ENABLE_DEPRECATED
856
857
858
859
861{
862 // Mark everything as unprepared, except for those things we've been
863 // told we don't need to prepare, for backwards compatibility
864 this->clear_point_locator();
865 this->clear_stored_ranges();
866 _preparation = false;
869
870 this->complete_preparation();
871}
872
873
875{
876 LOG_SCOPE("complete_preparation()", "MeshBase");
877
878 parallel_object_only();
879
880 libmesh_assert(this->comm().verify(this->is_serial()));
881
882 // If we don't go into this method with valid constraint rows, we're
883 // only going to be able to make that worse.
884#ifdef DEBUG
886#endif
887
888 // A distributed mesh may have processors with no elements (or
889 // processors with no elements of higher dimension, if we ever
890 // support mixed-dimension meshes), but we want consistent
891 // mesh_dimension anyways.
892 //
893 // cache_elem_data() should get the elem_dimensions() and
894 // mesh_dimension() correct later, and we don't need it earlier.
895
896
897 // Renumber the nodes and elements so that they in contiguous
898 // blocks. By default, _skip_renumber_nodes_and_elements is false.
899 //
900 // Instances where you if prepare_for_use() should not renumber the nodes
901 // and elements include reading in e.g. an xda/r or gmv file. In
902 // this case, the ordering of the nodes may depend on an accompanying
903 // solution, and the node ordering cannot be changed.
904
905
906 // Mesh modification operations might not leave us with consistent
907 // id counts, or might leave us with orphaned nodes we're no longer
908 // using, but our partitioner might need that consistency and/or
909 // might be confused by orphaned nodes.
911 {
915 }
916 else
917 {
919 this->remove_orphaned_nodes();
922 }
923
924 // Let all the elements find their neighbors
926 this->find_neighbors();
927
928 // The user may have set boundary conditions. We require that the
929 // boundary conditions were set consistently. Because we examine
930 // neighbors when evaluating non-raw boundary condition IDs, this
931 // assert is only valid when our neighbor links are in place.
932#ifdef DEBUG
934#endif
935
936 // Search the mesh for all the dimensions of the elements
937 // and cache them.
939 this->cache_elem_data();
940
941 // libMesh expects every processor to know about every subdomain
942 // name, but distributed mesh generators may only have set names for
943 // the part of the mesh they know about, so make sure the map is
944 // consistent everywhere.
947
948 // Search the mesh for elements that have a neighboring element
949 // of dim+1 and set that element as the interior parent
951 {
954 else
955 {
956 // We must set the flag that says "interior parent pointers have been set up"
957 // even though we skip detect_interior_parents().
959 }
960 }
961
962 // Fix up node unique ids in case mesh generation code didn't take
963 // exceptional care to do so.
964 // MeshCommunication().make_node_unique_ids_parallel_consistent(*this);
965
966 // We're going to still require that mesh generation code gets
967 // element unique ids consistent.
968#if defined(DEBUG) && defined(LIBMESH_ENABLE_UNIQUE_ID)
970#endif
971
972 // Allow our GhostingFunctor objects to reinit if necessary.
973 // Do this before partitioning and redistributing, and before
974 // deleting remote elements.
977
978 // Partition the mesh unless *all* partitioning is to be skipped.
979 // If only noncritical partitioning is to be skipped, the
980 // partition() call will still check for orphaned nodes.
982 this->partition();
983 else if (!this->n_unpartitioned_elem() &&
984 !this->n_unpartitioned_nodes())
986
987 // If we're using DistributedMesh, we'll probably want it
988 // parallelized.
992 else
994
995 // Much of our boundary info may have been for now-remote parts of the mesh,
996 // in which case we don't want to keep local copies of data meant to be
997 // local. On the other hand we may have deleted, or the user may have added in
998 // a distributed fashion, boundary data that is meant to be global. So we
999 // handle both of those scenarios here
1002
1005
1006 // The mesh is now prepared for use, with the possible exception of
1007 // partitioning that was supposed to be skipped, and it should know
1008 // it.
1009#ifndef NDEBUG
1010 Preparation completed_preparation = _preparation;
1011 if (skip_partitioning())
1012 completed_preparation.is_partitioned = true;
1013 libmesh_assert(completed_preparation);
1014#endif
1015
1016#ifdef DEBUG
1018#ifdef LIBMESH_ENABLE_UNIQUE_ID
1020#endif
1021#endif
1022}
1023
1024void
1026{
1027 for (auto & gf : _ghosting_functors)
1028 {
1029 libmesh_assert(gf);
1030 gf->mesh_reinit();
1031 }
1032
1034}
1035
1037{
1038 // Reset the number of partitions
1039 _n_parts = 1;
1040
1041 // Reset the preparation flags
1042 _preparation = false;
1043
1044 // Clear boundary information
1045 if (boundary_info)
1046 boundary_info->clear();
1047
1048 // Clear cached element data
1049 _elem_dims.clear();
1050 _elem_default_orders.clear();
1052
1053 _elemset_codes.clear();
1055
1056 _constraint_rows.clear();
1057
1058 // Clear our point locator.
1059 this->clear_point_locator();
1060 this->clear_stored_ranges();
1061}
1062
1063
1065{
1066 return static_cast<bool>(_preparation);
1067}
1068
1069
1071{
1072 _preparation = false;
1073 this->clear_point_locator();
1074 this->clear_stored_ranges();
1075}
1076
1077
1079{
1080 // We used to implicitly support duplicate inserts to std::set
1081#ifdef LIBMESH_ENABLE_DEPRECATED
1082 _ghosting_functors.erase
1083 (std::remove(_ghosting_functors.begin(),
1084 _ghosting_functors.end(),
1085 &ghosting_functor),
1086 _ghosting_functors.end());
1087#endif
1088
1089 // We shouldn't have two copies of the same functor
1090 libmesh_assert(std::find(_ghosting_functors.begin(),
1091 _ghosting_functors.end(),
1092 &ghosting_functor) ==
1093 _ghosting_functors.end());
1094
1095 _ghosting_functors.push_back(&ghosting_functor);
1096}
1097
1098
1099
1101{
1102 auto raw_it = std::find(_ghosting_functors.begin(),
1103 _ghosting_functors.end(), &ghosting_functor);
1104
1105 // The DofMap has a "to_mesh" parameter that tells it to avoid
1106 // registering a new functor with the mesh, but it doesn't keep
1107 // track of which functors weren't added, so we'll support "remove a
1108 // functor that isn't there" just like we did with set::erase
1109 // before.
1110 if (raw_it != _ghosting_functors.end())
1111 _ghosting_functors.erase(raw_it);
1112
1113 // We shouldn't have had two copies of the same functor
1114 libmesh_assert(std::find(_ghosting_functors.begin(),
1115 _ghosting_functors.end(),
1116 &ghosting_functor) ==
1117 _ghosting_functors.end());
1118
1119 if (const auto it = _shared_functors.find(&ghosting_functor);
1120 it != _shared_functors.end())
1121 _shared_functors.erase(it);
1122}
1123
1124
1125
1126void MeshBase::subdomain_ids (std::set<subdomain_id_type> & ids, const bool global /* = true */) const
1127{
1128 // This requires an inspection on every processor
1129 if (global)
1130 parallel_object_only();
1131
1132 struct SBDInserter {
1133 std::set<subdomain_id_type> my_ids;
1134
1135 SBDInserter () {}
1136 SBDInserter (SBDInserter &, Threads::split) {}
1137
1138 void operator()(const ConstElemRange & range) {
1139 for (const Elem * elem : range)
1140 my_ids.insert(elem->subdomain_id());
1141 }
1142
1143 void join(SBDInserter & other) {
1144 my_ids.merge(other.my_ids);
1145 }
1146 };
1147
1148 SBDInserter inserter;
1150
1151 ids.swap(inserter.my_ids);
1152
1153 if (global)
1154 {
1155 // Only include the unpartitioned elements if the user requests the global IDs.
1156 // In the case of the local subdomain IDs, it doesn't make sense to include the
1157 // unpartitioned elements because said elements do not have a sense of locality.
1158 for (const auto & elem : this->active_unpartitioned_element_ptr_range())
1159 ids.insert(elem->subdomain_id());
1160
1161 // Some subdomains may only live on other processors
1162 this->comm().set_union(ids);
1163 }
1164}
1165
1166
1167
1169{
1170 // We now have all elements and nodes redistributed; our ghosting
1171 // functors should be ready to redistribute and/or recompute any
1172 // cached data they use too.
1173 for (auto & gf : as_range(this->ghosting_functors_begin(),
1174 this->ghosting_functors_end()))
1175 gf->redistribute();
1176}
1177
1178
1179
1181{
1182 // A range over all elements might still be fine here, but any range
1183 // over local elements is obsolete if our partitioner changed the
1184 // definition of "local".
1186}
1187
1188
1189
1191{
1192 // This requires an inspection on every processor
1193 parallel_object_only();
1194
1195 std::set<subdomain_id_type> ids;
1196
1197 this->subdomain_ids (ids);
1198
1199 return cast_int<subdomain_id_type>(ids.size());
1200}
1201
1202
1203
1205{
1206 std::set<subdomain_id_type> ids;
1207
1208 this->subdomain_ids (ids, /* global = */ false);
1209
1210 return cast_int<subdomain_id_type>(ids.size());
1211}
1212
1213
1214
1215
1217{
1218 // We're either counting a processor's nodes or unpartitioned
1219 // nodes
1220 libmesh_assert (proc_id < this->n_processors() ||
1222
1223 return static_cast<dof_id_type>(std::distance (this->pid_nodes_begin(proc_id),
1224 this->pid_nodes_end (proc_id)));
1225}
1226
1227
1228
1230{
1231 // We're either counting a processor's elements or unpartitioned
1232 // elements
1233 libmesh_assert (proc_id < this->n_processors() ||
1235
1236 return static_cast<dof_id_type>(std::distance (this->pid_elements_begin(proc_id),
1237 this->pid_elements_end (proc_id)));
1238}
1239
1240
1241
1243{
1244 libmesh_assert_less (proc_id, this->n_processors());
1245 return static_cast<dof_id_type>(std::distance (this->active_pid_elements_begin(proc_id),
1246 this->active_pid_elements_end (proc_id)));
1247}
1248
1249
1250
1252{
1253 dof_id_type ne=0;
1254
1255 for (const auto & elem : this->element_ptr_range())
1256 ne += elem->n_sub_elem();
1257
1258 return ne;
1259}
1260
1261
1262
1264{
1265 dof_id_type ne=0;
1266
1267 for (const auto & elem : this->active_element_ptr_range())
1268 ne += elem->n_sub_elem();
1269
1270 return ne;
1271}
1272
1273
1274
1275std::string MeshBase::get_info(const unsigned int verbosity /* = 0 */, const bool global /* = true */) const
1276{
1277 std::ostringstream oss;
1278
1279 oss << " Mesh Information:" << '\n';
1280
1281 if (!_elem_dims.empty())
1282 {
1283 oss << " elem_dimensions()={";
1284 std::copy(_elem_dims.begin(),
1285 --_elem_dims.end(), // --end() is valid if the set is non-empty
1286 std::ostream_iterator<unsigned int>(oss, ", "));
1287 oss << cast_int<unsigned int>(*_elem_dims.rbegin());
1288 oss << "}\n";
1289 }
1290
1291 if (!_elem_default_orders.empty())
1292 {
1293 oss << " elem_default_orders()={";
1294 std::transform(_elem_default_orders.begin(),
1295 --_elem_default_orders.end(),
1296 std::ostream_iterator<std::string>(oss, ", "),
1297 [](Order o)
1298 { return Utility::enum_to_string<Order>(o); });
1299 oss << Utility::enum_to_string<Order>(*_elem_default_orders.rbegin());
1300 oss << "}\n";
1301 }
1302
1303 oss << " supported_nodal_order()=" << this->supported_nodal_order() << '\n'
1304 << " spatial_dimension()=" << this->spatial_dimension() << '\n'
1305 << " n_nodes()=" << this->n_nodes() << '\n'
1306 << " n_local_nodes()=" << this->n_local_nodes() << '\n'
1307 << " n_elem()=" << this->n_elem() << '\n'
1308 << " n_local_elem()=" << this->n_local_elem() << '\n';
1309#ifdef LIBMESH_ENABLE_AMR
1310 oss << " n_active_elem()=" << this->n_active_elem() << '\n';
1311#endif
1312 if (global)
1313 oss << " n_subdomains()=" << static_cast<std::size_t>(this->n_subdomains()) << '\n';
1314 else
1315 oss << " n_local_subdomains()= " << static_cast<std::size_t>(this->n_local_subdomains()) << '\n';
1316 oss << " n_elemsets()=" << static_cast<std::size_t>(this->n_elemsets()) << '\n';
1317 if (!_elemset_codes.empty())
1318 oss << " n_elemset_codes=" << _elemset_codes.size() << '\n';
1319 oss << " n_partitions()=" << static_cast<std::size_t>(this->n_partitions()) << '\n'
1320 << " n_processors()=" << static_cast<std::size_t>(this->n_processors()) << '\n'
1321 << " n_threads()=" << static_cast<std::size_t>(libMesh::n_threads()) << '\n'
1322 << " processor_id()=" << static_cast<std::size_t>(this->processor_id()) << '\n'
1323 << " is_prepared()=" << (this->is_prepared() ? "true" : "false") << '\n'
1324 << " is_replicated()=" << (this->is_replicated() ? "true" : "false") << '\n';
1325
1326 if (verbosity > 0)
1327 {
1328 if (global)
1329 {
1330 libmesh_parallel_only(this->comm());
1331 if (this->processor_id() != 0)
1332 oss << "\n Detailed global get_info() (verbosity > 0) is reduced and output to only rank 0.";
1333 }
1334
1335 // Helper for printing element types
1336 const auto elem_type_helper = [](const std::set<int> & elem_types) {
1337 std::stringstream ss;
1338 for (auto it = elem_types.begin(); it != elem_types.end();)
1339 {
1341 if (++it != elem_types.end())
1342 ss << ", ";
1343 }
1344 return ss.str();
1345 };
1346
1347 // Helper for whether or not the given DofObject is to be included. If we're doing
1348 // a global reduction, we also count unpartitioned objects on rank 0.
1349 const auto include_object = [this, &global](const DofObject & dof_object) {
1350 return this->processor_id() == dof_object.processor_id() ||
1351 (global &&
1352 this->processor_id() == 0 &&
1353 dof_object.processor_id() == DofObject::invalid_processor_id);
1354 };
1355
1356 Real volume = 0;
1357
1358 // Add bounding box information
1359 const auto bbox = global ? MeshTools::create_bounding_box(*this) : MeshTools::create_local_bounding_box(*this);
1360 if (!global || this->processor_id() == 0)
1361 oss << "\n " << (global ? "" : "Local ") << "Mesh Bounding Box:\n"
1362 << " Minimum: " << bbox.min() << "\n"
1363 << " Maximum: " << bbox.max() << "\n"
1364 << " Delta: " << (bbox.max() - bbox.min()) << "\n";
1365
1366 // Obtain the global or local element types
1367 std::set<int> elem_types;
1368 for (const Elem * elem : this->active_local_element_ptr_range())
1369 elem_types.insert(elem->type());
1370 if (global)
1371 {
1372 // Pick up unpartitioned elems on rank 0
1373 if (this->processor_id() == 0)
1374 for (const Elem * elem : this->active_unpartitioned_element_ptr_range())
1375 elem_types.insert(elem->type());
1376
1377 this->comm().set_union(elem_types);
1378 }
1379
1380 // Add element types
1381 if (!global || this->processor_id() == 0)
1382 oss << "\n " << (global ? "" : "Local ") << "Mesh Element Type(s):\n "
1383 << elem_type_helper(elem_types) << "\n";
1384
1385 // Reduce the nodeset ids
1386 auto nodeset_ids = this->get_boundary_info().get_node_boundary_ids();
1387 if (global)
1388 this->comm().set_union(nodeset_ids);
1389
1390 // Accumulate local information for each nodeset
1391 struct NodesetInfo
1392 {
1393 std::size_t num_nodes = 0;
1394 BoundingBox bbox;
1395 };
1396 std::map<boundary_id_type, NodesetInfo> nodeset_info_map;
1397 for (const auto & [node, id] : this->get_boundary_info().get_nodeset_map())
1398 {
1399 if (!include_object(*node))
1400 continue;
1401
1402 NodesetInfo & info = nodeset_info_map[id];
1403
1404 ++info.num_nodes;
1405
1406 if (verbosity > 1)
1407 info.bbox.union_with(*node);
1408 }
1409
1410 // Add nodeset info
1411 if (!global || this->processor_id() == 0)
1412 {
1413 oss << "\n " << (global ? "" : "Local ") << "Mesh Nodesets:\n";
1414 if (nodeset_ids.empty())
1415 oss << " None\n";
1416 }
1417
1418 const auto & nodeset_name_map = this->get_boundary_info().get_nodeset_name_map();
1419 for (const auto id : nodeset_ids)
1420 {
1421 NodesetInfo & info = nodeset_info_map[id];
1422
1423 // Reduce the local information for this nodeset if required
1424 if (global)
1425 {
1426 this->comm().sum(info.num_nodes);
1427 if (verbosity > 1)
1428 {
1429 this->comm().min(info.bbox.min());
1430 this->comm().max(info.bbox.max());
1431 }
1432 }
1433
1434 const bool has_name = nodeset_name_map.count(id) && nodeset_name_map.at(id).size();
1435 const std::string name = has_name ? nodeset_name_map.at(id) : "";
1436 if (global)
1437 libmesh_assert(this->comm().verify(name));
1438
1439 if (global ? this->processor_id() == 0 : info.num_nodes > 0)
1440 {
1441 oss << " Nodeset " << id;
1442 if (has_name)
1443 oss << " (" << name << ")";
1444 oss << ", " << info.num_nodes << " " << (global ? "" : "local ") << "nodes\n";
1445
1446 if (verbosity > 1)
1447 {
1448 oss << " " << (global ? "Bounding" : "Local bounding") << " box minimum: "
1449 << info.bbox.min() << "\n"
1450 << " " << (global ? "Bounding" : "Local bounding") << " box maximum: "
1451 << info.bbox.max() << "\n"
1452 << " " << (global ? "Bounding" : "Local bounding") << " box delta: "
1453 << (info.bbox.max() - info.bbox.min()) << "\n";
1454 }
1455 }
1456 }
1457
1458 // Reduce the sideset ids
1459 auto sideset_ids = this->get_boundary_info().get_side_boundary_ids();
1460 if (global)
1461 this->comm().set_union(sideset_ids);
1462
1463 // Accumulate local information for each sideset
1464 struct SidesetInfo
1465 {
1466 std::size_t num_sides = 0;
1467 Real volume = 0;
1468 std::set<int> side_elem_types;
1469 std::set<int> elem_types;
1470 std::set<dof_id_type> elem_ids;
1471 std::set<dof_id_type> node_ids;
1472 BoundingBox bbox;
1473 };
1474 ElemSideBuilder side_builder;
1475 std::map<boundary_id_type, SidesetInfo> sideset_info_map;
1476 for (const auto & pair : this->get_boundary_info().get_sideset_map())
1477 {
1478 const Elem * elem = pair.first;
1479 if (!include_object(*elem))
1480 continue;
1481
1482 const auto id = pair.second.second;
1483 SidesetInfo & info = sideset_info_map[id];
1484
1485 const auto s = pair.second.first;
1486 const Elem & side = side_builder(*elem, s);
1487
1488 ++info.num_sides;
1489 info.side_elem_types.insert(side.type());
1490 info.elem_types.insert(elem->type());
1491 info.elem_ids.insert(elem->id());
1492
1493 for (const Node & node : side.node_ref_range())
1494 if (include_object(node))
1495 info.node_ids.insert(node.id());
1496
1497 if (verbosity > 1)
1498 {
1499 info.volume += side.volume();
1500 info.bbox.union_with(side.loose_bounding_box());
1501 }
1502 }
1503
1504 // Add sideset info
1505 if (!global || this->processor_id() == 0)
1506 {
1507 oss << "\n " << (global ? "" : "Local ") << "Mesh Sidesets:\n";
1508 if (sideset_ids.empty())
1509 oss << " None\n";
1510 }
1511 const auto & sideset_name_map = this->get_boundary_info().get_sideset_name_map();
1512 for (const auto id : sideset_ids)
1513 {
1514 SidesetInfo & info = sideset_info_map[id];
1515
1516 auto num_elems = info.elem_ids.size();
1517 auto num_nodes = info.node_ids.size();
1518
1519 // Reduce the local information for this sideset if required
1520 if (global)
1521 {
1522 this->comm().sum(info.num_sides);
1523 this->comm().set_union(info.side_elem_types, 0);
1524 this->comm().sum(num_elems);
1525 this->comm().set_union(info.elem_types, 0);
1526 this->comm().sum(num_nodes);
1527 if (verbosity > 1)
1528 {
1529 this->comm().sum(info.volume);
1530 this->comm().min(info.bbox.min());
1531 this->comm().max(info.bbox.max());
1532 }
1533 }
1534
1535 const bool has_name = sideset_name_map.count(id) && sideset_name_map.at(id).size();
1536 const std::string name = has_name ? sideset_name_map.at(id) : "";
1537 if (global)
1538 libmesh_assert(this->comm().verify(name));
1539
1540 if (global ? this->processor_id() == 0 : info.num_sides > 0)
1541 {
1542 oss << " Sideset " << id;
1543 if (has_name)
1544 oss << " (" << name << ")";
1545 oss << ", " << info.num_sides << " sides (" << elem_type_helper(info.side_elem_types) << ")"
1546 << ", " << num_elems << " " << (global ? "" : "local ") << "elems (" << elem_type_helper(info.elem_types) << ")"
1547 << ", " << num_nodes << " " << (global ? "" : "local ") << "nodes\n";
1548
1549 if (verbosity > 1)
1550 {
1551 oss << " " << (global ? "Side" : "Local side") << " volume: " << info.volume << "\n"
1552 << " " << (global ? "Bounding" : "Local bounding") << " box minimum: "
1553 << info.bbox.min() << "\n"
1554 << " " << (global ? "Bounding" : "Local bounding") << " box maximum: "
1555 << info.bbox.max() << "\n"
1556 << " " << (global ? "Bounding" : "Local bounding") << " box delta: "
1557 << (info.bbox.max() - info.bbox.min()) << "\n";
1558 }
1559 }
1560 }
1561
1562 // Reduce the edgeset ids
1563 auto edgeset_ids = this->get_boundary_info().get_edge_boundary_ids();
1564 if (global)
1565 this->comm().set_union(edgeset_ids);
1566
1567 // Accumulate local information for each edgeset
1568 struct EdgesetInfo
1569 {
1570 std::size_t num_edges = 0;
1571 std::set<int> edge_elem_types;
1572 BoundingBox bbox;
1573 };
1574 std::map<boundary_id_type, EdgesetInfo> edgeset_info_map;
1575 std::unique_ptr<const Elem> edge;
1576
1577 for (const auto & pair : this->get_boundary_info().get_edgeset_map())
1578 {
1579 const Elem * elem = pair.first;
1580 if (!include_object(*elem))
1581 continue;
1582
1583 const auto id = pair.second.second;
1584 EdgesetInfo & info = edgeset_info_map[id];
1585
1586 elem->build_edge_ptr(edge, pair.second.first);
1587
1588 ++info.num_edges;
1589 info.edge_elem_types.insert(edge->type());
1590
1591 if (verbosity > 1)
1592 info.bbox.union_with(edge->loose_bounding_box());
1593 }
1594
1595 // Add edgeset info
1596 if (!global || this->processor_id() == 0)
1597 {
1598 oss << "\n " << (global ? "" : "Local ") << "Mesh Edgesets:\n";
1599 if (edgeset_ids.empty())
1600 oss << " None\n";
1601 }
1602
1603 const auto & edgeset_name_map = this->get_boundary_info().get_edgeset_name_map();
1604 for (const auto id : edgeset_ids)
1605 {
1606 EdgesetInfo & info = edgeset_info_map[id];
1607
1608 // Reduce the local information for this edgeset if required
1609 if (global)
1610 {
1611 this->comm().sum(info.num_edges);
1612 this->comm().set_union(info.edge_elem_types, 0);
1613 if (verbosity > 1)
1614 {
1615 this->comm().min(info.bbox.min());
1616 this->comm().min(info.bbox.max());
1617 }
1618 }
1619
1620 const bool has_name = edgeset_name_map.count(id) && edgeset_name_map.at(id).size();
1621 const std::string name = has_name ? edgeset_name_map.at(id) : "";
1622 if (global)
1623 libmesh_assert(this->comm().verify(name));
1624
1625 if (global ? this->processor_id() == 0 : info.num_edges > 0)
1626 {
1627 oss << " Edgeset " << id;
1628 if (has_name)
1629 oss << " (" << name << ")";
1630 oss << ", " << info.num_edges << " " << (global ? "" : "local ") << "edges ("
1631 << elem_type_helper(info.edge_elem_types) << ")\n";
1632
1633 if (verbosity > 1)
1634 {
1635 oss << " " << (global ? "Bounding" : "Local bounding") << " box minimum: "
1636 << info.bbox.min() << "\n"
1637 << " " << (global ? "Bounding" : "Local bounding") << " box maximum: "
1638 << info.bbox.max() << "\n"
1639 << " " << (global ? "Bounding" : "Local bounding") << " box delta: "
1640 << (info.bbox.max() - info.bbox.min()) << "\n";
1641 }
1642 }
1643 }
1644
1645 // Reduce the block IDs and block names
1646 std::set<subdomain_id_type> subdomains;
1647 for (const Elem * elem : this->active_element_ptr_range())
1648 if (include_object(*elem))
1649 subdomains.insert(elem->subdomain_id());
1650 if (global)
1651 this->comm().set_union(subdomains);
1652
1653 // Accumulate local information for each subdomain
1654 struct SubdomainInfo
1655 {
1656 std::size_t num_elems = 0;
1657 Real volume = 0;
1658 std::set<int> elem_types;
1659 std::set<dof_id_type> active_node_ids;
1660#ifdef LIBMESH_ENABLE_AMR
1661 std::size_t num_active_elems = 0;
1662#endif
1663 BoundingBox bbox;
1664 };
1665 std::map<subdomain_id_type, SubdomainInfo> subdomain_info_map;
1666 for (const Elem * elem : this->element_ptr_range())
1667 if (include_object(*elem))
1668 {
1669 SubdomainInfo & info = subdomain_info_map[elem->subdomain_id()];
1670
1671 ++info.num_elems;
1672 info.elem_types.insert(elem->type());
1673
1674#ifdef LIBMESH_ENABLE_AMR
1675 if (elem->active())
1676 ++info.num_active_elems;
1677#endif
1678
1679 for (const Node & node : elem->node_ref_range())
1680 if (include_object(node) && node.active())
1681 info.active_node_ids.insert(node.id());
1682
1683 if (verbosity > 1 && elem->active())
1684 {
1685 info.volume += elem->volume();
1686 info.bbox.union_with(elem->loose_bounding_box());
1687 }
1688 }
1689
1690 // Add subdomain info
1691 oss << "\n " << (global ? "" : "Local ") << "Mesh Subdomains:\n";
1692 const auto & subdomain_name_map = this->get_subdomain_name_map();
1693 for (const auto id : subdomains)
1694 {
1695 SubdomainInfo & info = subdomain_info_map[id];
1696
1697 auto num_active_nodes = info.active_node_ids.size();
1698
1699 // Reduce the information for this subdomain if needed
1700 if (global)
1701 {
1702 this->comm().sum(info.num_elems);
1703#ifdef LIBMESH_ENABLE_AMR
1704 this->comm().sum(info.num_active_elems);
1705#endif
1706 this->comm().sum(num_active_nodes);
1707 this->comm().set_union(info.elem_types, 0);
1708 if (verbosity > 1)
1709 {
1710 this->comm().min(info.bbox.min());
1711 this->comm().max(info.bbox.max());
1712 this->comm().sum(info.volume);
1713 }
1714 }
1715 if (verbosity > 1)
1716 volume += info.volume;
1717
1718 const bool has_name = subdomain_name_map.count(id);
1719 const std::string name = has_name ? subdomain_name_map.at(id) : "";
1720 if (global)
1721 libmesh_assert(this->comm().verify(name));
1722
1723 if (!global || this->processor_id() == 0)
1724 {
1725 oss << " Subdomain " << id;
1726 if (has_name)
1727 oss << " (" << name << ")";
1728 oss << ": " << info.num_elems << " " << (global ? "" : "local ") << "elems "
1729 << "(" << elem_type_helper(info.elem_types);
1730#ifdef LIBMESH_ENABLE_AMR
1731 oss << ", " << info.num_active_elems << " active";
1732#endif
1733 oss << "), " << num_active_nodes << " " << (global ? "" : "local ") << "active nodes\n";
1734 if (verbosity > 1)
1735 {
1736 oss << " " << (global ? "Volume" : "Local volume") << ": " << info.volume << "\n";
1737 oss << " " << (global ? "Bounding" : "Local bounding") << " box minimum: "
1738 << info.bbox.min() << "\n"
1739 << " " << (global ? "Bounding" : "Local bounding") << " box maximum: "
1740 << info.bbox.max() << "\n"
1741 << " " << (global ? "Bounding" : "Local bounding") << " box delta: "
1742 << (info.bbox.max() - info.bbox.min()) << "\n";
1743 }
1744 }
1745 }
1746
1747 oss << " " << (global ? "Global" : "Local") << " mesh volume = " << volume << "\n";
1748
1749 }
1750
1751 return oss.str();
1752}
1753
1754
1755void MeshBase::print_info(std::ostream & os, const unsigned int verbosity /* = 0 */, const bool global /* = true */) const
1756{
1757 os << this->get_info(verbosity, global)
1758 << std::endl;
1759}
1760
1761
1762std::ostream & operator << (std::ostream & os, const MeshBase & m)
1763{
1764 m.print_info(os);
1765 return os;
1766}
1767
1768
1769void MeshBase::partition (const unsigned int n_parts)
1770{
1771 // If we get here and we have unpartitioned elements, we need that
1772 // fixed.
1773 if (this->n_unpartitioned_elem() > 0)
1774 {
1775 libmesh_assert (partitioner().get());
1776 libmesh_assert (this->is_serial());
1777 partitioner()->partition (*this, n_parts);
1778 }
1779 // A nullptr partitioner or a skip_partitioning(true) call or a
1780 // skip_noncritical_partitioning(true) call means don't repartition;
1781 // skip_noncritical_partitioning() checks all these.
1783 {
1784 partitioner()->partition (*this, n_parts);
1785 }
1786 else
1787 {
1788 // Adaptive coarsening may have "orphaned" nodes on processors
1789 // whose elements no longer share them. We need to check for
1790 // and possibly fix that.
1792
1793 // Make sure locally cached partition count is correct
1795
1796 // Make sure any other locally cached data is correct
1798 }
1799
1801}
1802
1803void MeshBase::all_second_order (const bool full_ordered)
1804{
1805 this->all_second_order_range(this->element_ptr_range(), full_ordered);
1806}
1807
1809{
1810 this->all_complete_order_range(this->element_ptr_range());
1811}
1812
1814{
1815 // This requires an inspection on every processor
1816 parallel_object_only();
1817
1818 unsigned int max_proc_id=0;
1819
1820 for (const auto & elem : this->active_local_element_ptr_range())
1821 max_proc_id = std::max(max_proc_id, static_cast<unsigned int>(elem->processor_id()));
1822
1823 // The number of partitions is one more than the max processor ID.
1824 _n_parts = max_proc_id+1;
1825
1826 this->comm().max(_n_parts);
1827
1828 return _n_parts;
1829}
1830
1831
1832
1833std::unique_ptr<PointLocatorBase> MeshBase::sub_point_locator () const
1834{
1835 // If there's no master point locator, then we need one.
1836 if (_point_locator.get() == nullptr)
1837 {
1838 // PointLocator construction may not be safe within threads
1840
1841 // And it may require parallel communication
1842 parallel_object_only();
1843
1844#ifdef LIBMESH_ENABLE_NANOFLANN_POINTLOCATOR
1846#else
1848#endif
1849
1851 _point_locator->set_close_to_point_tol(_point_locator_close_to_point_tol);
1852 }
1853
1854 // Otherwise there was a master point locator, and we can grab a
1855 // sub-locator easily.
1856 return
1857#ifdef LIBMESH_ENABLE_NANOFLANN_POINTLOCATOR
1859#else
1861#endif
1862}
1863
1864
1865
1867{
1868 _point_locator.reset(nullptr);
1869}
1870
1871
1872
1874{
1875 _count_lower_dim_elems_in_point_locator = count_lower_dim_elems;
1876}
1877
1878
1879
1884
1885
1886#ifdef LIBMESH_ENABLE_DEPRECATED
1888{
1889 // Use set_subdomain_name() instead
1890 libmesh_deprecated();
1891
1892 // We'd like to do this so our preparation isn't invalidated by a
1893 // write, but for all we know the user is just reading from a
1894 // non-const mesh!
1895 // this->unset_has_synched_subdomain_name_map();
1896
1897 return _block_id_to_name[id];
1898}
1899#endif // LIBMESH_ENABLE_DEPRECATED
1900
1901
1902const std::string & MeshBase::subdomain_name(subdomain_id_type id) const
1903{
1904 // An empty string to return when no matching subdomain name is found
1905 static const std::string empty;
1906
1907 if (const auto iter = _block_id_to_name.find(id);
1908 iter == _block_id_to_name.end())
1909 return empty;
1910 else
1911 return iter->second;
1912}
1913
1915 const std::string & name,
1916 const bool synchronous)
1917{
1918 if (synchronous)
1919 parallel_object_only();
1920 else
1922 _block_id_to_name[id] = name;
1923}
1924
1925
1927{
1928 // Linear search over the map values.
1929 for (const auto & [sbd_id, sbd_name] : _block_id_to_name)
1930 if (sbd_name == name)
1931 return sbd_id;
1932
1933 // If we made it here without returning, we don't have a subdomain
1934 // with the requested name, so return Elem::invalid_subdomain_id.
1936}
1937
1938
1940{
1942 {
1943 // Range construction may not be safe within threads
1945
1947 std::make_unique<ElemRange>(this->elements_begin(),
1948 this->elements_end());
1949 }
1950
1951 return *_element_stored_range;
1952}
1953
1955{
1957 {
1958 // Range construction may not be safe within threads
1960
1962 std::make_unique<ConstElemRange>(this->active_local_elements_begin(),
1963 this->active_local_elements_end());
1964 }
1965
1967}
1968
1970{
1971 _element_stored_range.reset(nullptr);
1973}
1974
1975
1976#ifdef LIBMESH_ENABLE_DEPRECATED
1978{
1979 libmesh_deprecated();
1980
1981 this->cache_elem_data();
1982}
1983#endif // LIBMESH_ENABLE_DEPRECATED
1984
1986{
1987 // This requires an inspection on every processor
1988 parallel_object_only();
1989
1990 // Need to clear containers first in case all elements of a
1991 // particular dimension/order/subdomain have been deleted.
1992 _elem_dims.clear();
1993 _elem_default_orders.clear();
1994 _mesh_subdomains.clear();
1996
1997 for (const auto & elem : this->active_element_ptr_range())
1998 {
1999 _elem_dims.insert(cast_int<unsigned char>(elem->dim()));
2000 _elem_default_orders.insert(elem->default_order());
2001 _mesh_subdomains.insert(elem->subdomain_id());
2003 static_cast<Order>
2004 (std::min(static_cast<int>(_supported_nodal_order),
2005 static_cast<int>(elem->supported_nodal_order())));
2006 }
2007
2008 if (!this->is_serial())
2009 {
2010 // Some different dimension/order/subdomain elements may only live
2011 // on other processors
2012 this->comm().set_union(_elem_dims);
2016 }
2017
2018 // If the largest element dimension found is larger than the current
2019 // _spatial_dimension, increase _spatial_dimension.
2020 unsigned int max_dim = this->mesh_dimension();
2021 if (max_dim > _spatial_dimension)
2022 _spatial_dimension = cast_int<unsigned char>(max_dim);
2023
2024 // _spatial_dimension may need to increase from 1->2 or 2->3 if the
2025 // mesh is full of 1D elements but they are not x-aligned, or the
2026 // mesh is full of 2D elements but they are not in the x-y plane.
2027 // If the mesh is x-aligned or x-y planar, we will end up checking
2028 // every node's coordinates and not breaking out of the loop
2029 // early...
2030 if (_spatial_dimension < LIBMESH_DIM)
2031 {
2032 for (const auto & node : this->node_ptr_range())
2033 {
2034 // Note: the exact floating point comparison is intentional,
2035 // we don't want to get tripped up by tolerances.
2036 if ((*node)(0) != 0. && _spatial_dimension < 1)
2038
2039 if ((*node)(1) != 0. && _spatial_dimension < 2)
2040 {
2042#if LIBMESH_DIM == 2
2043 // If libmesh is compiled in 2D mode, this is the
2044 // largest spatial dimension possible so we can break
2045 // out.
2046 break;
2047#endif
2048 }
2049
2050#if LIBMESH_DIM > 2
2051 if ((*node)(2) != 0.)
2052 {
2053 // Spatial dimension can't get any higher than this, so
2054 // we can break out.
2056 break;
2057 }
2058#endif
2059 }
2060 }
2061
2063}
2064
2065
2067{
2068 // This requires every processor
2069 parallel_object_only();
2070
2072
2074}
2075
2076
2078{
2079 LOG_SCOPE("detect_interior_parents()", "MeshBase");
2080
2081 // This requires an inspection on every processor
2082 parallel_object_only();
2083
2084 // This requires up-to-date mesh dimensions, but if we don't have
2085 // them cached then we can't update them without changing the mesh
2086 // in unexpected ways that interfere with our tests of
2087 // partially-prepared meshes in MeshTools::*valid_is_prepared
2088 std::set<unsigned char> elem_dims_copy;
2090 elem_dims_copy = this->elem_dimensions();
2091 else
2092 {
2093 for (const auto & elem : this->active_element_ptr_range())
2094 elem_dims_copy.insert(cast_int<unsigned char>(elem->dim()));
2095 if (!this->is_serial())
2096 this->comm().set_union(elem_dims_copy);
2097 }
2098
2099 // Early return if the mesh is empty or has elements of a single spatial dimension.
2100 if (elem_dims_copy.size() <= 1)
2101 {
2103 return;
2104 }
2105
2106 // Convenient elem_dimensions iterators
2107 const auto dim_start = elem_dims_copy.begin();
2108 const auto dim_end = elem_dims_copy.end();
2109
2110 // In this function we find only +1 dimensional interior parents,
2111 // (so, for a given element el, the interior parent p must satisfy p.dim() == el.dim() + 1).
2112 // Therefore, we can avoid checking the existence of interior parents
2113 // for all those elements el such there there is no p with p.dim() == el.dim() + 1.
2114 // We store whether to skip any given dimension in the construction of interior parents
2115 // inside the vector in dimensions_to_skip_for_interior_parents.
2116 std::vector<bool> skip_dimension_for_interior_parents(/*count=*/LIBMESH_DIM+1, /*value=*/false);
2117 skip_dimension_for_interior_parents.back() = true;
2118
2119 // Moreover, in the following, we will build a node-to-elem map.
2120 // It is among the elems of this map that we will look for interior parents.
2121 // Therefore, we can skip all elems p such that there is no el with el.dim() == p.dim() - 1.
2122 // We store whether to skip any given dimension in the construction of the node-to-elem map
2123 // in the vector skip_dimensions_for_node_to_el_map.
2124 std::vector<bool> skip_dimensions_for_node_to_el_map(/*count=*/LIBMESH_DIM+1, /*value=*/false);
2125 skip_dimensions_for_node_to_el_map[*dim_start] = true;
2126
2127 // We also create a flag to know if all dimensions should be skipped,
2128 // and if we should therefore return early.
2129 bool skip_all_dimensions = true;
2130
2131 // Fill dimensions_to_skip_for_interior_parents and dimensions_to_skip_for_node_to_el_map.
2132 for (auto [it, next] = std::make_tuple(dim_start, std::next(dim_start));
2133 next != dim_end; ++it, ++next)
2134 {
2135 if (*it + 1 != *next) // if sequential dimensions differ by exactly 1
2136 {
2137 skip_dimension_for_interior_parents[*it] = true;
2138 skip_dimensions_for_node_to_el_map[*next] = true;
2139 }
2140 else if (!skip_dimension_for_interior_parents[*it])
2141 skip_all_dimensions = false;
2142 }
2143
2144 // There is nothing to do if all dimensions should be
2145 // skipped. Before returning, we must also set the flag that says
2146 // "interior parent pointers have been set up" even though we
2147 // determined there was no work to be done.
2148 if (skip_all_dimensions)
2149 {
2151 return;
2152 }
2153
2154 // Do we have interior parent pointers going to a different mesh?
2155 // If so then we'll still check to make sure that's the only place
2156 // they go, so we can libmesh_not_implemented() if not.
2157 const bool separate_interior_mesh = (&(this->interior_mesh()) != this);
2158
2159 // This map will be used to set interior parents
2160 std::unordered_map<dof_id_type, std::vector<dof_id_type>> node_to_elem;
2161
2162 for (const auto & elem : this->element_ptr_range())
2163 {
2164 // Ignore element if it cannot be interior parent of any other elem.
2165 if (skip_dimensions_for_node_to_el_map[elem->dim()])
2166 continue;
2167
2168 // Populating the node_to_elem map, same as MeshTools::build_nodes_to_elem_map
2169 for (auto n : make_range(elem->n_vertices()))
2170 {
2171 libmesh_assert_less (elem->id(), this->max_elem_id());
2172
2173 node_to_elem[elem->node_id(n)].push_back(elem->id());
2174 }
2175 }
2176
2177 // Automatically set interior parents
2178 for (const auto & element : this->element_ptr_range())
2179 {
2180 // Ignore elements with dimensions to skip
2181 // or elements that already have an interior parent.
2182 if (skip_dimension_for_interior_parents[element->dim()] || element->interior_parent())
2183 continue;
2184
2185 // Start by generating sets of dim+1 dimensional elements that
2186 // touch each vertex of the current element. If we encounter a
2187 // vertex not connected to _any_ dim+1 dimensional elements,
2188 // then we can exit the loop without checking the remaining
2189 // vertices since an interior parent (if it exists) will be
2190 // connected to all vertices of the current element.
2191 std::vector<std::set<dof_id_type>> neighbors( element->n_vertices() );
2192
2193 bool found_interior_parents = true;
2194
2195 for (auto n : make_range(element->n_vertices()))
2196 {
2197 auto it = node_to_elem.find(element->node_id(n));
2198
2199 // Check at first that this node is not isolated.
2200 if (it == node_to_elem.end())
2201 {
2202 found_interior_parents = false;
2203 break; // out of n-loop
2204 }
2205
2206 for (const auto & vertex_neighbor_id : it->second)
2207 if (this->elem_ref(vertex_neighbor_id).dim() == element->dim()+1)
2208 neighbors[n].insert(vertex_neighbor_id);
2209
2210 if (neighbors[n].empty())
2211 {
2212 // We have found an empty set for one vertex, no reason
2213 // to continue.
2214 found_interior_parents = false;
2215 break; // out of n-loop
2216 }
2217 }
2218
2219 // If we have generated a non-empty set of elements for each
2220 // vertex, we will now look for a vertex_neighbor_id that
2221 // appears in _all_ of those sets. If found, this is our interior
2222 // parent id. If multiple such common ids are found, we will
2223 // take the lowest such id to be the interior parent id.
2224 if (found_interior_parents)
2225 {
2226 std::set<dof_id_type> & neighbors_0 = neighbors[0];
2227 for (const auto & interior_parent_id : neighbors_0)
2228 {
2229 found_interior_parents = false;
2230 for (auto n : make_range(1u, element->n_vertices()))
2231 {
2232 if (neighbors[n].count(interior_parent_id))
2233 {
2234 found_interior_parents = true;
2235 }
2236 else
2237 {
2238 found_interior_parents = false;
2239 break;
2240 }
2241 }
2242
2243 if (found_interior_parents)
2244 {
2245 element->set_interior_parent(this->elem_ptr(interior_parent_id));
2246 break;
2247 }
2248 }
2249
2250 // Do we have a mixed dimensional mesh that contains some of
2251 // its own interior parents, but we already expect to have
2252 // interior parents on a different mesh? That's going to
2253 // take some work to support if anyone needs it.
2254 if (separate_interior_mesh)
2255 libmesh_not_implemented_msg
2256 ("interior_parent() values in multiple meshes are unsupported.");
2257 }
2258 }
2259
2260 // This flag doesn't necessarily mean any Elems actually have
2261 // interior parent pointers, just that we did all the work to
2262 // determine whether or not they do.
2264}
2265
2266
2267
2268#ifdef LIBMESH_ENABLE_PERIODIC
2273 const boundary_id_type b2,
2274 const RealVectorValue & translation)
2275 {
2276 // Lazily allocate the container the first time it’s needed
2278 _disjoint_neighbor_boundary_pairs = std::make_unique<PeriodicBoundaries>();
2279
2281
2282 // Create forward and inverse boundary mappings
2283 PeriodicBoundary forward(translation);
2284 PeriodicBoundary inverse(translation * -1.0);
2285
2286 forward.myboundary = b1;
2287 forward.pairedboundary = b2;
2288 inverse.myboundary = b2;
2289 inverse.pairedboundary = b1;
2290
2291 // Add both directions into the container
2292 db.emplace(b1, forward.clone());
2293 db.emplace(b2, inverse.clone());
2294 }
2295
2300
2305
2307 const boundary_id_type b2)
2308 {
2309 // Nothing to remove if not allocated or empty
2311 return;
2312
2313 auto & pairs = *_disjoint_neighbor_boundary_pairs;
2314
2315 // Helper to check and erase both directions
2316 auto erase_if_match = [](boundary_id_type key,
2317 boundary_id_type pair,
2318 PeriodicBoundaries & pb_map)
2319 {
2320 auto it = pb_map.find(key);
2321 if (it != pb_map.end())
2322 {
2323 const auto & pb = *(it->second);
2324 // Check both directions
2325 if ((pb.myboundary == key && pb.pairedboundary == pair) ||
2326 (pb.pairedboundary == key && pb.myboundary == pair))
2327 pb_map.erase(it);
2328 }
2329 };
2330
2331 erase_if_match(b1, b2, pairs);
2332 erase_if_match(b2, b1, pairs);
2333 }
2334
2335
2336#endif
2337
2338
2339
2341{
2343 if (_point_locator)
2344 {
2345 if (val > 0.)
2346 _point_locator->set_close_to_point_tol(val);
2347 else
2348 _point_locator->unset_close_to_point_tol();
2349 }
2350}
2351
2352
2353
2358
2359
2360
2362{
2363 const std::size_t new_size = _elem_integer_names.size();
2364
2366 (this->element_stored_range(),
2367 [new_size, this](const ElemRange & range)
2368 {
2369 for (Elem * elem : range)
2370 elem->add_extra_integers(new_size, this->_elem_integer_default_values);
2371 });
2372}
2373
2374
2375
2377{
2378 const std::size_t new_size = _node_integer_names.size();
2379 for (auto node : this->node_ptr_range())
2380 node->add_extra_integers(new_size, _node_integer_default_values);
2381}
2382
2383
2384std::pair<std::vector<unsigned int>, std::vector<unsigned int>>
2386{
2387 std::pair<std::vector<unsigned int>, std::vector<unsigned int>> returnval;
2388 returnval.first = this->add_elem_integers(other._elem_integer_names, true, &other._elem_integer_default_values);
2389 returnval.second = this->add_node_integers(other._node_integer_names, true, &other._node_integer_default_values);
2390 return returnval;
2391}
2392
2393
2394
2395void
2397{
2398 // Now that all the DofObject moving is done, we can move the GhostingFunctor objects
2399 // which include the _default_ghosting,_ghosting_functors and _shared_functors. We also need
2400 // to set the mesh object associated with these functors to the assignee mesh.
2401
2402 // _default_ghosting
2403 _default_ghosting = std::move(other_mesh._default_ghosting);
2404 _default_ghosting->set_mesh(this);
2405
2406 // _ghosting_functors
2407 _ghosting_functors = std::move(other_mesh._ghosting_functors);
2408
2409 for (const auto gf : _ghosting_functors )
2410 {
2411 gf->set_mesh(this);
2412 }
2413
2414 // _shared_functors
2415 _shared_functors = std::move(other_mesh._shared_functors);
2416
2417 for (const auto & sf : _shared_functors )
2418 {
2419 (sf.second)->set_mesh(this);
2420 }
2421
2422 // _constraint_rows
2423 _constraint_rows = std::move(other_mesh._constraint_rows);
2424
2425 if (other_mesh.partitioner())
2426 _partitioner = std::move(other_mesh.partitioner());
2427}
2428
2429
2430void
2432{
2433 this->_spatial_dimension = other_mesh._spatial_dimension;
2434 this->_elem_dims = other_mesh._elem_dims;
2435 this->_elem_default_orders = other_mesh._elem_default_orders;
2437 this->_mesh_subdomains = other_mesh._mesh_subdomains;
2438}
2439
2440
2441bool MeshBase::nodes_and_elements_equal(const MeshBase & other_mesh) const
2442{
2443 for (const auto & other_node : other_mesh.node_ptr_range())
2444 {
2445 const Node * node = this->query_node_ptr(other_node->id());
2446 if (!node)
2447 return false;
2448 if (*other_node != *node)
2449 return false;
2450 }
2451 for (const auto & node : this->node_ptr_range())
2452 if (!other_mesh.query_node_ptr(node->id()))
2453 return false;
2454
2455 for (const auto & other_elem : other_mesh.element_ptr_range())
2456 {
2457 const Elem * elem = this->query_elem_ptr(other_elem->id());
2458 if (!elem)
2459 return false;
2460 if (!other_elem->topologically_equal(*elem))
2461 return false;
2462 }
2463 for (const auto & elem : this->element_ptr_range())
2464 if (!other_mesh.query_elem_ptr(elem->id()))
2465 return false;
2466
2467 return true;
2468}
2469
2470
2472{
2473 dof_id_type n_local_rows=0, n_unpartitioned_rows=0;
2474 for (const auto & [node, node_constraints] : _constraint_rows)
2475 {
2476 // Unpartitioned nodes
2477 if (node->processor_id() == DofObject::invalid_processor_id)
2478 n_unpartitioned_rows++;
2479 else if (node->processor_id() == this->processor_id())
2480 n_local_rows++;
2481 }
2482
2483 this->comm().sum(n_local_rows);
2484
2485 return n_unpartitioned_rows + n_local_rows;
2486}
2487
2488
2489void
2491{
2492 LOG_SCOPE("copy_constraint_rows(mesh)", "MeshBase");
2493
2494 _constraint_rows.clear();
2495
2496 const auto & other_constraint_rows = other_mesh.get_constraint_rows();
2497 for (const auto & [other_node, other_node_constraints] : other_constraint_rows)
2498 {
2499 const Node * const our_node = this->node_ptr(other_node->id());
2500 constraint_rows_mapped_type our_node_constraints;
2501 for (const auto & [other_inner_key_pair, constraint_value] : other_node_constraints)
2502 {
2503 const auto & [other_elem, local_node_id] = other_inner_key_pair;
2504 const Elem * const our_elem = this->elem_ptr(other_elem->id());
2505 our_node_constraints.emplace_back(std::make_pair(our_elem, local_node_id), constraint_value);
2506 }
2507 _constraint_rows[our_node] = std::move(our_node_constraints);
2508 }
2509}
2510
2511
2512template <typename T>
2513void
2515 bool precondition_constraint_operator)
2516{
2517 LOG_SCOPE("copy_constraint_rows(mat)", "MeshBase");
2518
2519 this->_constraint_rows.clear();
2520
2521 // We're not going to support doing this distributed yet; it'd be
2522 // pointless unless we temporarily had a linear partitioning to
2523 // better match the constraint operator.
2524 MeshSerializer serialize(*this);
2525
2526 // Our current mesh should already reflect the desired assembly space
2527 libmesh_error_msg_if(this->n_nodes() != constraint_operator.m(),
2528 "Constraint operator matrix with " <<
2529 constraint_operator.m() <<
2530 "rows does not match this mesh with " <<
2531 this->n_nodes() << " nodes");
2532
2533 // First, find what new unconstrained DoFs we need to add. We can't
2534 // iterate over columns in a SparseMatrix, so we'll iterate over
2535 // rows and keep track of columns.
2536
2537 // If we have nodes that will work unconstrained, keep track of
2538 // their node ids and corresponding column indices.
2539 // existing_unconstrained_nodes[column_id] = node_id
2540 std::map<dof_id_type, dof_id_type> existing_unconstrained_columns;
2541 std::set<dof_id_type> existing_unconstrained_nodes;
2542
2543 // In case we need new nodes, keep track of their columns.
2544 // columns[j][k] will be the kth row index and value of column j
2545 typedef
2546 std::unordered_map<dof_id_type,
2547 std::vector<std::pair<dof_id_type, Real>>>
2548 columns_type;
2549 columns_type columns(constraint_operator.n());
2550
2551 // If we need to precondition the constraint operator (e.g. it's an
2552 // unpreconditioned extraction operator for a Flex IGA matrix),
2553 // we'll want to keep track of the sum of each column, because we'll
2554 // be dividing each column by that sum (Jacobi preconditioning on
2555 // the right, which then leads to symmetric preconditioning on a
2556 // physics Jacobian).
2557 std::unordered_map<dof_id_type, Real> column_sums;
2558
2559 // Work in parallel, though we'll have to sync shortly
2560 for (auto i : make_range(constraint_operator.row_start(),
2561 constraint_operator.row_stop()))
2562 {
2563 std::vector<numeric_index_type> indices;
2564 std::vector<T> values;
2565
2566 constraint_operator.get_row(i, indices, values);
2567 libmesh_assert_equal_to(indices.size(), values.size());
2568
2569 if (indices.size() == 1 &&
2570 values[0] == T(1))
2571 {
2572 // If we have multiple simple Ui=Uj constraints, let the
2573 // first one be our "unconstrained" node and let the others
2574 // be constrained to it.
2575 if (existing_unconstrained_columns.find(indices[0]) !=
2576 existing_unconstrained_columns.end())
2577 {
2578 const auto j = indices[0];
2579 columns[j].emplace_back(i, 1);
2580 }
2581 else
2582 {
2583 existing_unconstrained_nodes.insert(i);
2584 existing_unconstrained_columns.emplace(indices[0],i);
2585 }
2586 }
2587 else
2588 for (auto jj : index_range(indices))
2589 {
2590 const auto j = indices[jj];
2591 const Real coef = libmesh_real(values[jj]);
2592 libmesh_assert_equal_to(coef, values[jj]);
2593 columns[j].emplace_back(i, coef);
2594 }
2595 }
2596
2597 // Merge data from different processors' slabs of the matrix
2598 this->comm().set_union(existing_unconstrained_nodes);
2599 this->comm().set_union(existing_unconstrained_columns);
2600
2601 std::vector<columns_type> all_columns;
2602 this->comm().allgather(columns, all_columns);
2603
2604 columns.clear();
2605 for (auto p : index_range(all_columns))
2606 for (auto & [j, subcol] : all_columns[p])
2607 for (auto [i, v] : subcol)
2608 columns[j].emplace_back(i,v);
2609
2610 // Keep track of elements on which unconstrained nodes exist, and
2611 // their local node indices.
2612 // node_to_elem_ptrs[node] = [elem_id, local_node_num]
2613 std::unordered_map<const Node *, std::pair<dof_id_type, unsigned int>> node_to_elem_ptrs;
2614
2615 // Find elements attached to any existing nodes that will stay
2616 // unconstrained. We'll also build a subdomain set here so we don't
2617 // have to assert that the mesh is already prepared before we pick a
2618 // new subdomain for any NodeElems we need to add.
2619 std::set<subdomain_id_type> subdomain_ids;
2620 for (const Elem * elem : this->element_ptr_range())
2621 {
2622 subdomain_ids.insert(elem->subdomain_id());
2623 for (auto n : make_range(elem->n_nodes()))
2624 {
2625 const Node * node = elem->node_ptr(n);
2626 if (existing_unconstrained_nodes.count(node->id()))
2627 node_to_elem_ptrs.emplace(node, std::make_pair(elem->id(), n));
2628 }
2629 }
2630
2631 const subdomain_id_type new_sbd_id = *subdomain_ids.rbegin() + 1;
2632
2633 for (auto j : make_range(constraint_operator.n()))
2634 {
2635 // If we already have a good node for this then we're done
2636 if (existing_unconstrained_columns.count(j))
2637 continue;
2638
2639 // Get a half-decent spot to place a new NodeElem for
2640 // unconstrained DoF(s) here. Getting a *fully*-decent spot
2641 // would require finding a Moore-Penrose pseudoinverse, and I'm
2642 // not going to do that, but scaling a transpose will at least
2643 // get us a little uniqueness to make visualization reasonable.
2644 Point newpt;
2645 Real total_scaling = 0;
2646 unsigned int total_entries = 0;
2647
2648 // We'll get a decent initial pid choice here too, if only to
2649 // aid in later repartitioning.
2650 std::map<processor_id_type, int> pids;
2651
2652 auto & column = columns[j];
2653 for (auto [i, r] : column)
2654 {
2655 Node & constrained_node = this->node_ref(i);
2656 const Point constrained_pt = constrained_node;
2657 newpt += r*constrained_pt;
2658 total_scaling += r;
2659 ++total_entries;
2660 ++pids[constrained_node.processor_id()];
2661 }
2662
2663 if (precondition_constraint_operator)
2664 column_sums[j] = total_scaling;
2665
2666 libmesh_error_msg_if
2667 (!total_entries,
2668 "Empty column " << j <<
2669 " found in constraint operator matrix");
2670
2671 // If we have *cancellation* here then we can end up dividing by
2672 // zero; try just evenly scaling across all constrained node
2673 // points instead.
2674 if (total_scaling > TOLERANCE)
2675 newpt /= total_scaling;
2676 else
2677 newpt /= total_entries;
2678
2679 Node *n = this->add_point(newpt);
2680 std::unique_ptr<Elem> elem = Elem::build(NODEELEM);
2681 elem->set_node(0, n);
2682 elem->subdomain_id() = new_sbd_id;
2683
2684 Elem * added_elem = this->add_elem(std::move(elem));
2685 this->_elem_dims.insert(0);
2686 this->_elem_default_orders.insert(added_elem->default_order());
2688 static_cast<Order>
2689 (std::min(static_cast<int>(this->_supported_nodal_order),
2690 static_cast<int>(added_elem->supported_nodal_order())));
2691 this->_mesh_subdomains.insert(new_sbd_id);
2692 node_to_elem_ptrs.emplace(n, std::make_pair(added_elem->id(), 0));
2693 existing_unconstrained_columns.emplace(j,n->id());
2694
2695 // Repartition the new objects *after* adding them, so a
2696 // DistributedMesh doesn't get confused and think you're not
2697 // adding them on all processors at once.
2698 int n_pids = 0;
2699 for (auto [pid, count] : pids)
2700 if (count >= n_pids)
2701 {
2702 n_pids = count;
2703 added_elem->processor_id() = pid;
2704 n->processor_id() = pid;
2705 }
2706 }
2707
2708 // Calculate constraint rows in an indexed form that's easy for us
2709 // to allgather
2710 std::unordered_map<dof_id_type,
2711 std::vector<std::pair<std::pair<dof_id_type, unsigned int>,Real>>>
2712 indexed_constraint_rows;
2713
2714 for (auto i : make_range(constraint_operator.row_start(),
2715 constraint_operator.row_stop()))
2716 {
2717 if (existing_unconstrained_nodes.count(i))
2718 continue;
2719
2720 std::vector<numeric_index_type> indices;
2721 std::vector<T> values;
2722
2723 constraint_operator.get_row(i, indices, values);
2724
2725 std::vector<std::pair<std::pair<dof_id_type, unsigned int>, Real>> constraint_row;
2726
2727 for (auto jj : index_range(indices))
2728 {
2729 const dof_id_type node_id =
2730 existing_unconstrained_columns[indices[jj]];
2731
2732 Node & constraining_node = this->node_ref(node_id);
2733
2734 libmesh_assert(node_to_elem_ptrs.count(&constraining_node));
2735
2736 auto p = node_to_elem_ptrs[&constraining_node];
2737
2738 Real coef = libmesh_real(values[jj]);
2739 libmesh_assert_equal_to(coef, values[jj]);
2740
2741 // If we're preconditioning and we created a nodeelem then
2742 // we can scale the meaning of that nodeelem's value to give
2743 // us a better-conditioned matrix after the constraints are
2744 // applied.
2745 if (precondition_constraint_operator)
2746 if (auto sum_it = column_sums.find(indices[jj]);
2747 sum_it != column_sums.end())
2748 {
2749 const Real scaling = sum_it->second;
2750
2751 if (scaling > TOLERANCE)
2752 coef /= scaling;
2753 }
2754
2755 constraint_row.emplace_back(std::make_pair(p, coef));
2756 }
2757
2758 indexed_constraint_rows.emplace(i, std::move(constraint_row));
2759 }
2760
2761 this->comm().set_union(indexed_constraint_rows);
2762
2763 // Add constraint rows as mesh constraint rows
2764 for (auto & [node_id, indexed_row] : indexed_constraint_rows)
2765 {
2766 Node * constrained_node = this->node_ptr(node_id);
2767
2768 constraint_rows_mapped_type constraint_row;
2769
2770 for (auto [p, coef] : indexed_row)
2771 {
2772 const Elem * elem = this->elem_ptr(p.first);
2773 constraint_row.emplace_back
2774 (std::make_pair(std::make_pair(elem, p.second), coef));
2775 }
2776
2777 this->_constraint_rows.emplace(constrained_node,
2778 std::move(constraint_row));
2779 }
2780}
2781
2782
2783void MeshBase::print_constraint_rows(std::ostream & os,
2784 bool print_nonlocal) const
2785{
2786 parallel_object_only();
2787
2788 std::string local_constraints =
2789 this->get_local_constraints(print_nonlocal);
2790
2791 if (this->processor_id())
2792 {
2793 this->comm().send(0, local_constraints);
2794 }
2795 else
2796 {
2797 os << "Processor 0:\n";
2798 os << local_constraints;
2799
2800 for (auto p : IntRange<processor_id_type>(1, this->n_processors()))
2801 {
2802 this->comm().receive(p, local_constraints);
2803 os << "Processor " << p << ":\n";
2804 os << local_constraints;
2805 }
2806 }
2807}
2808
2809
2810
2811std::string MeshBase::get_local_constraints(bool print_nonlocal) const
2812{
2813 std::ostringstream os;
2814
2815 if (print_nonlocal)
2816 os << "All ";
2817 else
2818 os << "Local ";
2819
2820 os << "Mesh Constraint Rows:"
2821 << std::endl;
2822
2823 for (const auto & [node, row] : _constraint_rows)
2824 {
2825 const bool local = (node->processor_id() == this->processor_id());
2826
2827 // Skip non-local dofs if requested
2828 if (!print_nonlocal && !local)
2829 continue;
2830
2831 os << "Constraints for " << (local ? "Local" : "Ghost") << " Node " << node->id()
2832 << ": \t";
2833
2834 for (const auto & [elem_and_node, coef] : row)
2835 os << " ((" << elem_and_node.first->id() << ',' << elem_and_node.second << "), " << coef << ")\t";
2836
2837 os << std::endl;
2838 }
2839
2840 return os.str();
2841}
2842
2844 is_partitioned(false),
2845 has_synched_id_counts(false),
2846 has_neighbor_ptrs(false),
2847 has_cached_elem_data(false),
2848 has_interior_parent_ptrs(false),
2849 has_removed_remote_elements(false),
2850 has_removed_orphaned_nodes(false),
2851 has_boundary_id_sets(false),
2852 has_reinit_ghosting_functors(false),
2853 has_synched_subdomain_name_map(false)
2854{}
2855
2856MeshBase::Preparation::operator bool() const
2857{
2858 return is_partitioned &&
2859 has_synched_id_counts &&
2860 has_neighbor_ptrs &&
2861 has_cached_elem_data &&
2862 has_interior_parent_ptrs &&
2863 has_removed_remote_elements &&
2864 has_removed_orphaned_nodes &&
2865 has_reinit_ghosting_functors &&
2866 has_boundary_id_sets &&
2867 has_synched_subdomain_name_map;
2868}
2869
2872{
2873 is_partitioned = set_all;
2874 has_synched_id_counts = set_all;
2875 has_neighbor_ptrs = set_all;
2876 has_cached_elem_data = set_all;
2877 has_interior_parent_ptrs = set_all;
2878 has_removed_remote_elements = set_all;
2879 has_removed_orphaned_nodes = set_all;
2880 has_reinit_ghosting_functors = set_all;
2881 has_boundary_id_sets = set_all;
2882 has_synched_subdomain_name_map = set_all;
2883
2884 return *this;
2885}
2886
2887bool
2889{
2890 if (is_partitioned != other.is_partitioned)
2891 return false;
2892 if (has_synched_id_counts != other.has_synched_id_counts)
2893 return false;
2894 if (has_neighbor_ptrs != other.has_neighbor_ptrs)
2895 return false;
2896 if (has_cached_elem_data != other.has_cached_elem_data)
2897 return false;
2898 if (has_interior_parent_ptrs != other.has_interior_parent_ptrs)
2899 return false;
2900 if (has_removed_remote_elements != other.has_removed_remote_elements)
2901 return false;
2902 if (has_removed_orphaned_nodes != other.has_removed_orphaned_nodes)
2903 return false;
2904 if (has_reinit_ghosting_functors != other.has_reinit_ghosting_functors)
2905 return false;
2906 if (has_boundary_id_sets != other.has_boundary_id_sets)
2907 return false;
2908 if (has_synched_subdomain_name_map != other.has_synched_subdomain_name_map)
2909 return false;
2910
2911 return true;
2912}
2913
2914bool
2916{
2917 return !(*this == other);
2918}
2919
2920
2921// Explicit instantiations for our template function
2922template LIBMESH_EXPORT void
2923MeshBase::copy_constraint_rows(const SparseMatrix<Real> & constraint_operator,
2924 bool precondition_constraint_operator);
2925
2926#ifdef LIBMESH_USE_COMPLEX_NUMBERS
2927template LIBMESH_EXPORT void
2928MeshBase::copy_constraint_rows(const SparseMatrix<Complex> & constraint_operator,
2929 bool precondition_constraint_operator);
2930#endif
2931
2932
2933} // namespace libMesh
void max(const T &r, T &o, Request &req) const
Status receive(const unsigned int dest_processor_id, T &buf, const MessageTag &tag=any_tag) const
void min(const T &r, T &o, Request &req) const
void set_union(T &data, const unsigned int root_id) const
void allgather(const T &send_data, std::vector< T, A > &recv_data) const
void send(const unsigned int dest_processor_id, const T &buf, const MessageTag &tag=no_tag) const
The BoundaryInfo class contains information relevant to boundary conditions including storing faces,...
const std::set< boundary_id_type > & get_edge_boundary_ids() const
const std::multimap< const Elem *, std::pair< unsigned short int, boundary_id_type > > & get_edgeset_map() const
const std::multimap< const Node *, boundary_id_type > & get_nodeset_map() const
const std::multimap< const Elem *, std::pair< unsigned short int, boundary_id_type > > & get_sideset_map() const
const std::set< boundary_id_type > & get_node_boundary_ids() const
const std::set< boundary_id_type > & get_side_boundary_ids() const
void regenerate_id_sets()
Clears and regenerates the cached sets of ids.
const std::map< boundary_id_type, std::string > & get_edgeset_name_map() const
const std::map< boundary_id_type, std::string > & get_nodeset_name_map() const
const std::map< boundary_id_type, std::string > & get_sideset_name_map() const
Defines a Cartesian bounding box by the two corner extremum.
The DofObject defines an abstract base class for objects that have degrees of freedom associated with...
Definition dof_object.h:55
processor_id_type processor_id() const
Definition dof_object.h:881
static constexpr dof_id_type invalid_id
An invalid id to distinguish an uninitialized DofObject.
Definition dof_object.h:473
dof_id_type id() const
Definition dof_object.h:819
static constexpr processor_id_type invalid_processor_id
An invalid processor_id to distinguish DoFs that have not been assigned to a processor.
Definition dof_object.h:484
Helper for building element sides that minimizes the construction of new elements.
This is the base class from which all geometric element types are derived.
Definition elem.h:96
virtual BoundingBox loose_bounding_box() const
Definition elem.C:3498
SimpleRange< NodeRefIter > node_ref_range()
Returns a range with all nodes of an element, usable in range-based for loops.
Definition elem.h:2682
virtual Order default_order() const =0
static std::unique_ptr< Elem > build(const ElemType type, Elem *p=nullptr)
Definition elem.C:442
virtual Order supported_nodal_order() const
Definition elem.h:1004
static constexpr subdomain_id_type invalid_subdomain_id
A static integral constant representing an invalid subdomain id.
Definition elem.h:246
virtual unsigned short dim() const =0
virtual std::unique_ptr< Elem > build_edge_ptr(const unsigned int i)=0
virtual Real volume() const
Definition elem.C:3462
virtual ElemType type() const =0
This class implements the original default geometry ghosting requirements in libMesh: point neighbors...
This abstract base class defines the interface by which library code and user code can report associa...
The IntRange templated class is intended to make it easy to loop over integers which are indices of a...
Definition int_range.h:54
This is the MeshBase class.
Definition mesh_base.h:81
void set_subdomain_name(subdomain_id_type id, const std::string &name, bool synchronous=false)
Sets the name for the provided id.
Definition mesh_base.C:1914
virtual const Node & node_ref(const dof_id_type i) const
Definition mesh_base.h:745
virtual bool is_serial() const
Definition mesh_base.h:357
void size_elem_extra_integers()
Size extra-integer arrays of all elements in the mesh.
Definition mesh_base.C:2361
const BoundaryInfo & get_boundary_info() const
The information about boundary ids on the mesh.
Definition mesh_base.h:170
void set_spatial_dimension(unsigned char d)
Sets the "spatial dimension" of the Mesh.
Definition mesh_base.C:613
bool is_prepared() const
Definition mesh_base.C:1064
virtual std::unique_ptr< Partitioner > & partitioner()
A partitioner to use at each partitioning.
Definition mesh_base.h:165
bool allow_renumbering() const
Definition mesh_base.h:1356
virtual const Node * node_ptr(const dof_id_type i) const =0
MeshBase::elemset_type _all_elemset_ids
Definition mesh_base.h:2359
MeshBase(const Parallel::Communicator &comm_in, unsigned char dim=1)
Constructor.
Definition mesh_base.C:64
unsigned int mesh_dimension() const
Definition mesh_base.C:430
bool _allow_node_and_elem_unique_id_overlap
The Exodus reader (and potentially other readers in the future?) now supports setting Node and Elem u...
Definition mesh_base.h:2301
virtual dof_id_type n_elem() const =0
virtual bool is_replicated() const
Definition mesh_base.h:379
void complete_preparation()
Definition mesh_base.C:874
bool _skip_detect_interior_parents
If this is true then we will skip detect_interior_parents in prepare_for_use.
Definition mesh_base.h:2280
bool operator==(const MeshBase &other_mesh) const
This tests for exactly-equal data in all the senses that a mathematician would care about (element co...
Definition mesh_base.C:271
void set_elem_dimensions(std::set< unsigned char > elem_dims)
Most of the time you should not need to call this, as the element dimensions will be set automaticall...
Definition mesh_base.C:439
const std::set< unsigned char > & elem_dimensions() const
Definition mesh_base.h:430
void print_constraint_rows(std::ostream &os=libMesh::out, bool print_nonlocal=false) const
Prints (from processor 0) all mesh constraint rows.
Definition mesh_base.C:2783
bool skip_partitioning() const
Definition mesh_base.h:1431
Order supported_nodal_order() const
Definition mesh_base.h:445
Order _supported_nodal_order
We cache the maximum nodal order supported by all the mesh's elements (the minimum supported_nodal_or...
Definition mesh_base.h:2328
void subdomain_ids(std::set< subdomain_id_type > &ids, const bool global=true) const
Constructs a list of all subdomain identifiers in the local mesh if global == false,...
Definition mesh_base.C:1126
unsigned int recalculate_n_partitions()
In a few (very rare) cases, the user may have manually tagged the elements with specific processor ID...
Definition mesh_base.C:1813
virtual void renumber_nodes_and_elements()=0
After partitioning a mesh it is useful to renumber the nodes and elements so that they lie in contigu...
void set_point_locator_close_to_point_tol(Real val)
Set value used by PointLocatorBase::close_to_point_tol().
Definition mesh_base.C:2340
dof_id_type n_active_elem_on_proc(const processor_id_type proc) const
Definition mesh_base.C:1242
bool has_node_integer(std::string_view name) const
Definition mesh_base.C:790
unsigned int spatial_dimension() const
Definition mesh_base.C:606
PeriodicBoundaries * get_disjoint_neighbor_boundary_pairs()
Definition mesh_base.C:2296
void change_elemset_id(elemset_id_type old_id, elemset_id_type new_id)
Replace elemset id "old_id" with "new_id".
Definition mesh_base.C:565
unsigned int get_elem_integer_index(std::string_view name) const
Definition mesh_base.C:689
const std::map< subdomain_id_type, std::string > & get_subdomain_name_map() const
Definition mesh_base.h:1926
subdomain_id_type n_local_subdomains() const
Definition mesh_base.C:1204
void all_second_order(const bool full_ordered=true)
Calls the range-based version of this function with a range consisting of all elements in the mesh.
Definition mesh_base.C:1803
virtual void delete_node(Node *n)=0
Removes the Node n from the mesh.
unsigned int get_node_integer_index(std::string_view name) const
Definition mesh_base.C:778
std::map< dof_id_type, const MeshBase::elemset_type * > _elemset_codes
Map from "element set code" to list of set ids to which that element belongs (and vice-versa).
Definition mesh_base.h:2357
dof_id_type get_elemset_code(const MeshBase::elemset_type &id_set) const
Definition mesh_base.C:497
dof_id_type n_sub_elem() const
Definition mesh_base.C:1251
const ConstElemRange & active_local_element_stored_range() const
Definition mesh_base.C:1954
virtual void redistribute()
Redistribute elements between processors.
Definition mesh_base.C:1168
const ElemRange & element_stored_range()
Definition mesh_base.C:1939
std::unique_ptr< PeriodicBoundaries > _disjoint_neighbor_boundary_pairs
The disjoint neighbor boundary id pairs.
Definition mesh_base.h:2119
std::vector< unsigned int > add_node_integers(const std::vector< std::string > &names, bool allocate_data=true, const std::vector< dof_id_type > *default_values=nullptr)
Register integer data (of type dof_id_type) to be added to each node in the mesh.
Definition mesh_base.C:735
unsigned char _spatial_dimension
The "spatial dimension" of the Mesh.
Definition mesh_base.h:2365
virtual ~MeshBase()
Destructor.
Definition mesh_base.C:421
std::map< MeshBase::elemset_type, dof_id_type > _elemset_codes_inverse_map
Definition mesh_base.h:2358
bool _count_lower_dim_elems_in_point_locator
Do we count lower dimensional elements in point locator refinement? This is relevant in tree-based po...
Definition mesh_base.h:2231
dof_id_type n_active_sub_elem() const
Same as n_sub_elem(), but only counts active elements.
Definition mesh_base.C:1263
bool _allow_remote_element_removal
If this is false then even on DistributedMesh remote elements will not be deleted during mesh prepara...
Definition mesh_base.h:2288
unsigned int add_node_integer(std::string name, bool allocate_data=true, dof_id_type default_value=DofObject::invalid_id)
Register an integer datum (of type dof_id_type) to be added to each node in the mesh.
Definition mesh_base.C:712
void change_elemset_code(dof_id_type old_code, dof_id_type new_code)
Replace elemset code "old_code" with "new_code".
Definition mesh_base.C:512
std::vector< dof_id_type > get_elemset_codes() const
Return a vector of all elemset codes defined on the mesh.
Definition mesh_base.C:503
virtual dof_id_type n_nodes() const =0
std::vector< dof_id_type > _node_integer_default_values
The array of default initialization values for integer data associated with each node in the mesh.
Definition mesh_base.h:2389
unique_id_type _next_unique_id
The next available unique id for assigning ids to DOF objects.
Definition mesh_base.h:2245
bool _skip_renumber_nodes_and_elements
If this is true then renumbering will be kept to a minimum.
Definition mesh_base.h:2270
void remove_ghosting_functor(GhostingFunctor &ghosting_functor)
Removes a functor which was previously added to the set of ghosting functors.
Definition mesh_base.C:1100
virtual const Node * query_node_ptr(const dof_id_type i) const =0
std::vector< std::pair< std::pair< const Elem *, unsigned int >, Real > > constraint_rows_mapped_type
Definition mesh_base.h:1929
virtual void all_second_order_range(const SimpleRange< element_iterator > &range, const bool full_ordered=true)=0
Converts a set of this Mesh's elements defined by range from FIRST order to SECOND order.
GhostingFunctorIterator ghosting_functors_begin() const
Beginning of range of ghosting functors.
Definition mesh_base.h:1472
void unset_has_synched_subdomain_name_map()
Tells this we have done some operation which may have left the subdomain id to name map inconsistent ...
Definition mesh_base.h:350
std::map< GhostingFunctor *, std::shared_ptr< GhostingFunctor > > _shared_functors
Hang on to references to any GhostingFunctor objects we were passed in shared_ptr form.
Definition mesh_base.h:2430
virtual Node * add_point(const Point &p, const dof_id_type id=DofObject::invalid_id, const processor_id_type proc_id=DofObject::invalid_processor_id)=0
Add a new Node at Point p to the end of the vertex array, with processor_id procid.
std::set< subdomain_id_type > _mesh_subdomains
We cache the subdomain ids of the elements present in the mesh.
Definition mesh_base.h:2333
dof_id_type n_unpartitioned_elem() const
Definition mesh_base.h:703
virtual void update_post_partitioning()
Recalculate any cached data (or invalidate any caches that are computed on the fly) after elements an...
Definition mesh_base.C:1180
bool get_count_lower_dim_elems_in_point_locator() const
Get the current value of _count_lower_dim_elems_in_point_locator.
Definition mesh_base.C:1880
std::unique_ptr< PointLocatorBase > _point_locator
A PointLocator class for this mesh.
Definition mesh_base.h:2225
virtual const Elem * elem_ptr(const dof_id_type i) const =0
unsigned int add_elem_integer(std::string name, bool allocate_data=true, dof_id_type default_value=DofObject::invalid_id)
Register an integer datum (of type dof_id_type) to be added to each element in the mesh.
Definition mesh_base.C:623
bool _skip_all_partitioning
If this is true then no partitioning should be done.
Definition mesh_base.h:2263
virtual void all_complete_order_range(const SimpleRange< element_iterator > &range)=0
Converts a set of elements in this (conforming, non-refined) mesh into "complete" order elements,...
std::vector< std::string > _elem_integer_names
The array of names for integer data associated with each element in the mesh.
Definition mesh_base.h:2371
void prepare_for_use()
Definition mesh_base.C:860
std::vector< dof_id_type > _elem_integer_default_values
The array of default initialization values for integer data associated with each element in the mesh.
Definition mesh_base.h:2377
void remove_orphaned_nodes()
Removes any orphaned nodes, nodes not connected to any elements.
Definition mesh_base.C:801
Real get_point_locator_close_to_point_tol() const
Definition mesh_base.C:2354
unsigned int n_partitions() const
Definition mesh_base.h:1526
bool has_elem_integer(std::string_view name) const
Definition mesh_base.C:701
dof_id_type n_local_elem() const
Definition mesh_base.h:697
dof_id_type n_unpartitioned_nodes() const
Definition mesh_base.h:597
bool _skip_noncritical_partitioning
If this is true then no partitioning should be done with the possible exception of orphaned nodes.
Definition mesh_base.h:2258
bool _skip_find_neighbors
If this is true then we will skip find_neighbors in prepare_for_use.
Definition mesh_base.h:2275
dof_id_type n_elem_on_proc(const processor_id_type proc) const
Definition mesh_base.C:1229
virtual void delete_remote_elements()
When supported, deletes all nonlocal elements of the mesh except for "ghosts" which touch a local ele...
Definition mesh_base.h:399
MeshBase & operator=(const MeshBase &)=delete
Copy and move assignment are not allowed because MeshBase subclasses manually manage memory (Elems an...
ElemMappingType _default_mapping_type
The default mapping type (typically Lagrange) between master and physical space to assign to newly ad...
Definition mesh_base.h:2183
std::unique_ptr< BoundaryInfo > boundary_info
This class holds the boundary information.
Definition mesh_base.h:2131
std::set< Order > _elem_default_orders
We cache the (default) order of the geometric elements present in the mesh.
Definition mesh_base.h:2322
bool skip_noncritical_partitioning() const
Definition mesh_base.h:1414
MeshBase * _interior_mesh
Defaulting to this, a pointer to the mesh used to generate boundary elements on this.
Definition mesh_base.h:2252
std::vector< GhostingFunctor * > _ghosting_functors
The list of all GhostingFunctor objects to be used when distributing a DistributedMesh.
Definition mesh_base.h:2424
virtual void clear()
Deletes all the element and node data that is currently stored.
Definition mesh_base.C:1036
std::pair< std::vector< unsigned int >, std::vector< unsigned int > > merge_extra_integer_names(const MeshBase &other)
Merge extra-integer arrays from an other mesh.
Definition mesh_base.C:2385
dof_id_type n_local_nodes() const
Definition mesh_base.h:591
std::unique_ptr< ConstElemRange > _const_active_local_element_stored_range
A cached ConstElemRange for threaded calculation on all local elements of this mesh.
Definition mesh_base.h:2216
std::map< subdomain_id_type, std::string > _block_id_to_name
This structure maintains the mapping of named blocks for file formats that support named blocks.
Definition mesh_base.h:2308
virtual bool subclass_locally_equals(const MeshBase &other_mesh) const =0
Shim to allow operator == (&) to behave like a virtual function without having to be one.
constraint_rows_type _constraint_rows
Definition mesh_base.h:2442
std::unique_ptr< Partitioner > _partitioner
A partitioner to use at each prepare_for_use().
Definition mesh_base.h:2239
unsigned int n_elemsets() const
Returns the number of unique elemset ids which have been added via add_elemset_code(),...
Definition mesh_base.C:482
constraint_rows_type & get_constraint_rows()
Constraint rows accessors.
Definition mesh_base.h:1935
const MeshBase & interior_mesh() const
Definition mesh_base.h:2040
std::unique_ptr< GhostingFunctor > _default_ghosting
The default geometric GhostingFunctor, used to implement standard libMesh element ghosting behavior.
Definition mesh_base.h:2415
void add_elemset_code(dof_id_type code, MeshBase::elemset_type id_set)
Tabulate a user-defined "code" for elements which belong to the element sets specified in id_set.
Definition mesh_base.C:456
unsigned char _default_mapping_data
The default mapping data (unused with Lagrange, used for nodal weight lookup index with rational base...
Definition mesh_base.h:2190
virtual const Elem * query_elem_ptr(const dof_id_type i) const =0
dof_id_type n_constraint_rows() const
Definition mesh_base.C:2471
virtual void all_complete_order()
Calls the range-based version of this function with a range consisting of all elements in the mesh.
Definition mesh_base.C:1808
virtual Elem * add_elem(Elem *e)=0
Add elem e to the end of the element array.
Real _point_locator_close_to_point_tol
If nonzero, we will call PointLocatorBase::set_close_to_point_tol() on any PointLocators that we crea...
Definition mesh_base.h:2448
dof_id_type n_nodes_on_proc(const processor_id_type proc) const
Definition mesh_base.C:1216
std::string & subdomain_name(subdomain_id_type id)
Definition mesh_base.C:1887
bool allow_find_neighbors() const
Definition mesh_base.h:1363
void sync_subdomain_name_map()
libMesh often expects all processors to know about names of all subdomain ids, but distributed mesh g...
Definition mesh_base.C:2066
virtual const Elem & elem_ref(const dof_id_type i) const
Definition mesh_base.h:788
void size_node_extra_integers()
Size extra-integer arrays of all nodes in the mesh.
Definition mesh_base.C:2376
unsigned int _n_parts
The number of partitions the mesh has.
Definition mesh_base.h:2177
std::map< const Node *, constraint_rows_mapped_type > constraint_rows_type
Definition mesh_base.h:1930
void get_elemsets(dof_id_type elemset_code, MeshBase::elemset_type &id_set_to_fill) const
Look up the element sets for a given elemset code and vice-versa.
Definition mesh_base.C:487
void remove_disjoint_boundary_pair(const boundary_id_type b1, const boundary_id_type b2)
Definition mesh_base.C:2306
void clear_point_locator()
Releases the current PointLocator object.
Definition mesh_base.C:1866
GhostingFunctorIterator ghosting_functors_end() const
End of range of ghosting functors.
Definition mesh_base.h:1478
void set_count_lower_dim_elems_in_point_locator(bool count_lower_dim_elems)
In the point locator, do we count lower dimensional elements when we refine point locator regions?...
Definition mesh_base.C:1873
void add_ghosting_functor(GhostingFunctor &ghosting_functor)
Adds a functor which can specify ghosting requirements for use on distributed meshes.
Definition mesh_base.C:1078
void copy_constraint_rows(const MeshBase &other_mesh)
Copy the constraints from the other mesh to this mesh.
Definition mesh_base.C:2490
std::set< elemset_id_type > elemset_type
Typedef for the "set" container used to store elemset ids.
Definition mesh_base.h:466
std::set< unsigned char > _elem_dims
We cache the dimension of the elements present in the mesh.
Definition mesh_base.h:2315
std::vector< unsigned int > add_elem_integers(const std::vector< std::string > &names, bool allocate_data=true, const std::vector< dof_id_type > *default_values=nullptr)
Register integer data (of type dof_id_type) to be added to each element in the mesh,...
Definition mesh_base.C:646
std::vector< std::string > _node_integer_names
The array of names for integer data associated with each node in the mesh.
Definition mesh_base.h:2383
std::unique_ptr< PointLocatorBase > sub_point_locator() const
Definition mesh_base.C:1833
void add_disjoint_neighbor_boundary_pairs(const boundary_id_type b1, const boundary_id_type b2, const RealVectorValue &translation)
Register a pair of boundaries as disjoint neighbor boundary pairs.
Definition mesh_base.C:2272
subdomain_id_type get_id_by_name(std::string_view name) const
Definition mesh_base.C:1926
void print_info(std::ostream &os=libMesh::out, const unsigned int verbosity=0, const bool global=true) const
Prints relevant information about the mesh.
Definition mesh_base.C:1755
virtual void update_parallel_id_counts()=0
Updates parallel caches so that methods like n_elem() accurately reflect changes on other processors.
bool locally_equals(const MeshBase &other_mesh) const
This behaves the same as operator==, but only for the local and ghosted aspects of the mesh; i....
Definition mesh_base.C:281
virtual dof_id_type n_active_elem() const =0
void reinit_ghosting_functors()
Loops over ghosting functors and calls mesh_reinit()
Definition mesh_base.C:1025
Preparation _preparation
Flags indicating in what ways this mesh has been prepared.
Definition mesh_base.h:2195
subdomain_id_type n_subdomains() const
Definition mesh_base.C:1190
std::string get_info(const unsigned int verbosity=0, const bool global=true) const
Definition mesh_base.C:1275
std::string get_local_constraints(bool print_nonlocal=false) const
Gets a string reporting all mesh constraint rows local to this processor.
Definition mesh_base.C:2811
bool nodes_and_elements_equal(const MeshBase &other_mesh) const
Tests for equality of all elements and nodes in the mesh.
Definition mesh_base.C:2441
void copy_cached_data(const MeshBase &other_mesh)
Helper class to copy cached data, to synchronize with a possibly unprepared other_mesh.
Definition mesh_base.C:2431
void unset_is_prepared()
Tells this we have done some operation where we should no longer consider ourself prepared.
Definition mesh_base.C:1070
virtual void find_neighbors(const bool reset_remote_elements=false, const bool reset_current_list=true, const bool assert_valid=true)=0
Locate element face (edge in 2D) neighbors.
void post_dofobject_moves(MeshBase &&other_mesh)
Moves any superclass data (e.g.
Definition mesh_base.C:2396
void detect_interior_parents()
Search the mesh for elements that have a neighboring element of dim+1 and set that element as the int...
Definition mesh_base.C:2077
void clear_stored_ranges()
Clears stored ranges, to indicate that the mesh has changed and they should be regenerated when next ...
Definition mesh_base.C:1969
std::unique_ptr< ElemRange > _element_stored_range
A cached ElemRange for threaded mutation of all semilocal elements of this mesh.
Definition mesh_base.h:2205
Temporarily serialize a DistributedMesh for non-distributed-mesh capable code paths.
A Node is like a Point, but with more information.
Definition node.h:55
An object whose state is distributed along a set of processors.
processor_id_type processor_id() const
ParallelObject & operator=(const ParallelObject &libmesh_dbg_var(other))
"Assignment" operator.
const Parallel::Communicator & comm() const
processor_id_type n_processors() const
We're using a class instead of a typedef to allow forward declarations and future flexibility.
boundary_id_type myboundary
The boundary ID of this boundary and its counterpart.
The definition of a periodic boundary.
virtual std::unique_ptr< PeriodicBoundaryBase > clone(TransformationType t=FORWARD) const override
If we want the DofMap to be able to make copies of references and store them in the underlying map,...
static std::unique_ptr< PointLocatorBase > build(PointLocatorType t, const MeshBase &mesh, const PointLocatorBase *master=nullptr)
Builds an PointLocator for the mesh mesh.
A Point defines a location in LIBMESH_DIM dimensional Real space.
Definition point.h:40
Generic sparse matrix.
virtual void get_row(numeric_index_type i, std::vector< numeric_index_type > &indices, std::vector< T > &values) const =0
Get a row from the matrix.
virtual numeric_index_type n() const =0
virtual numeric_index_type row_stop() const =0
virtual numeric_index_type m() const =0
virtual numeric_index_type row_start() const =0
The StoredRange class defines a contiguous, divisible set of objects.
Dummy "splitting object" used to distinguish splitting constructors from copy constructors.
void libmesh_assert_valid_unique_ids(const MeshBase &mesh)
A function for verifying that unique ids match across processors.
libMesh::BoundingBox create_bounding_box(const MeshBase &mesh)
Definition mesh_tools.C:566
libMesh::BoundingBox create_local_bounding_box(const MeshBase &mesh)
Definition mesh_tools.C:631
void libmesh_assert_valid_boundary_ids(const MeshBase &mesh)
A function for verifying that boundary condition ids match across processors.
void libmesh_assert_valid_constraint_rows(const MeshBase &mesh)
A function for verifying that all mesh constraint rows express relations between nodes and elements t...
void correct_node_proc_ids(MeshBase &)
Changes the processor ids on each node so be the same as the id of the lowest element touching that n...
bool in_threads
A boolean which is true iff we are in a Threads:: function It may be useful to assert(!...
Definition threads.C:33
void parallel_reduce(const Range &range, Body &body, unsigned int n_threads=libMesh::n_threads())
Execute the provided reduction operation in parallel on the specified range.
void parallel_for(const Range &range, const Body &body, unsigned int n_threads=libMesh::n_threads())
Execute the provided function object in parallel on the specified range.
std::string enum_to_string(const T e)
The libMesh namespace provides an interface to certain functionality in the library.
SimpleRange< IndexType > as_range(const std::pair< IndexType, IndexType > &p)
Helper function that allows us to treat a homogenous pair as a range.
auto index_range(const T &sizable)
Helper function that returns an IntRange<std::size_t> representing all the indices of the passed-in v...
Definition int_range.h:153
bool closed()
Checks that the library has been closed.
Definition libmesh.C:331
ElemType
Defines an enum for geometric element types.
int8_t boundary_id_type
Definition id_types.h:51
T libmesh_real(T a)
libmesh_assert(ctx)
std::ostream & operator<<(std::ostream &os, const OrderWrapper &order)
Overload stream operators.
Definition fe_type.h:182
const unsigned int invalid_uint
A number which is used quite often to represent an invalid or uninitialized value for an unsigned int...
Definition libmesh.h:303
bool initialized()
Checks that library initialization has been done.
Definition libmesh.C:324
static constexpr Real TOLERANCE
uint8_t dof_id_type
Definition id_types.h:67
bool on_command_line(std::string arg)
Definition libmesh.C:934
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
uint8_t processor_id_type
Definition id_types.h:104
IntRange< T > make_range(T beg, T end)
The 2-parameter make_range() helper function returns an IntRange<T> when both input parameters are of...
Definition int_range.h:176
unsigned int n_threads()
Flags indicating in what ways a mesh has been prepared for use.
Definition mesh_base.h:2080
bool operator!=(const Preparation &other) const
Definition mesh_base.C:2915
Preparation & operator=(bool set_all)
Set all flags to the "set_all" value.
Definition mesh_base.C:2871
bool operator==(const Preparation &other) const
Two Preparation objects are equivalent iff all the flags match, regardless of the true/false status o...
Definition mesh_base.C:2888