libMesh
Loading...
Searching...
No Matches
replicated_mesh.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 it and/or
5// modify it under the terms of the GNU Lesser General Public
6// License as published by the Free Software Foundation; either
7// version 2.1 of the License, or (at your option) any later version.
8
9// This library is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12// Lesser General Public License for more details.
13
14// You should have received a copy of the GNU Lesser General Public
15// License along with this library; if not, write to the Free Software
16// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
18
19
20// Local includes
21#include "libmesh/replicated_mesh.h"
22
23#include "libmesh/boundary_info.h"
24#include "libmesh/elem.h"
25#include "libmesh/libmesh_logging.h"
26#include "libmesh/mesh_communication.h"
27#include "libmesh/parallel_implementation.h"
28#include "libmesh/partitioner.h"
29#include "libmesh/point.h"
30#include "libmesh/string_to_enum.h"
31#include "libmesh/utility.h"
32
33// C++ includes
34#include <unordered_map>
35#include <unordered_set>
36
37namespace libMesh
38{
39
40// ------------------------------------------------------------
41// ReplicatedMesh class member functions
43 unsigned char d) :
44 UnstructuredMesh (comm_in,d),
45 _n_nodes(0), _n_elem(0)
46{
47#ifdef LIBMESH_ENABLE_UNIQUE_ID
48 // In serial we just need to reset the next unique id to zero
49 // here in the constructor.
51#endif
52
53 const std::string default_partitioner = "metis";
54 const std::string my_partitioner =
55 libMesh::command_line_value("--default-partitioner",
56 default_partitioner);
58 (Utility::string_to_enum<PartitionerType>(my_partitioner));
59}
60
61
62std::string_view ReplicatedMesh::subclass_first_difference_from (const MeshBase & other_mesh_base) const
63{
64 const ReplicatedMesh * rep_mesh_ptr =
65 dynamic_cast<const ReplicatedMesh *>(&other_mesh_base);
66 if (!rep_mesh_ptr)
67 return "ReplicatedMesh class";
68 const ReplicatedMesh & other_mesh = *rep_mesh_ptr;
69
70#define CHECK_MEMBER(member_name) \
71 if (member_name != other_mesh.member_name) \
72 return #member_name;
73
74 CHECK_MEMBER(_n_nodes);
75 CHECK_MEMBER(_n_elem);
76#ifdef LIBMESH_ENABLE_UNIQUE_ID
77 CHECK_MEMBER(_next_unique_id);
78#endif
79 if (!this->nodes_and_elements_equal(other_mesh))
80 return "nodes and/or elements";
81
82 return "";
83}
84
85
87{
88 this->ReplicatedMesh::clear(); // Free nodes and elements
89}
90
91
92// This might be specialized later, but right now it's just here to
93// make sure the compiler doesn't give us a default (non-deep) copy
94// constructor instead.
96 ReplicatedMesh(static_cast<const MeshBase&>(other_mesh))
97{
98#ifdef LIBMESH_ENABLE_UNIQUE_ID
99 this->_next_unique_id = other_mesh._next_unique_id;
100#endif
101}
102
103
105 UnstructuredMesh (other_mesh),
106 _n_nodes(0), _n_elem(0) // copy_* will increment this
107{
108 // Just copy, skipping preparation
109 this->copy_nodes_and_elements(other_mesh, true, 0, 0, 0, nullptr, true);
110
111 this->allow_find_neighbors(other_mesh.allow_find_neighbors());
113 this->allow_renumbering(other_mesh.allow_renumbering());
115 this->skip_partitioning(other_mesh.skip_partitioning());
116
117 this->copy_constraint_rows(other_mesh);
118
119 auto & this_boundary_info = this->get_boundary_info();
120 const auto & other_boundary_info = other_mesh.get_boundary_info();
121
122 this_boundary_info = other_boundary_info;
123
124 this->set_subdomain_name_map() = other_mesh.get_subdomain_name_map();
125
126 this->_preparation = other_mesh.preparation();
127
128 // If other_mesh is distributed, then we've got parts of it on each
129 // processor but we're not replicated yet; fix that.
130 if (!other_mesh.is_serial())
132}
133
135{
136 LOG_SCOPE("operator=(&&)", "ReplicatedMesh");
137
138 // Move assign as an UnstructuredMesh
139 this->UnstructuredMesh::operator=(std::move(other_mesh));
140
141 // Nodes and elements belong to ReplicatedMesh and have to be
142 // moved before we can move arbitrary GhostingFunctor, Partitioner,
143 // etc. subclasses.
144 this->move_nodes_and_elements(std::move(other_mesh));
145
146 // Handle those remaining moves.
147 this->post_dofobject_moves(std::move(other_mesh));
148
149 return *this;
150}
151
153{
154 *this = std::move(cast_ref<ReplicatedMesh&>(other_mesh));
155
156 return *this;
157}
158
160{
161 ReplicatedMesh & other_mesh = cast_ref<ReplicatedMesh&>(other_meshbase);
162
163 this->_nodes = std::move(other_mesh._nodes);
164 this->_n_nodes = other_mesh.n_nodes();
165
166 this->_elements = std::move(other_mesh._elements);
167 this->_n_elem = other_mesh.n_elem();
168}
169
170
172{
173 return this->node_ref(i);
174}
175
176
177
178
180{
181 libmesh_assert_less (i, this->max_node_id());
182 libmesh_assert(_nodes[i]);
183 libmesh_assert_equal_to (_nodes[i]->id(), i); // This will change soon
184
185 return _nodes[i];
186}
187
188
189
190
192{
193 libmesh_assert_less (i, this->max_node_id());
194 libmesh_assert(_nodes[i]);
195 libmesh_assert_equal_to (_nodes[i]->id(), i); // This will change soon
196
197 return _nodes[i];
198}
199
200
201
202
204{
205 if (i >= this->max_node_id())
206 return nullptr;
207 libmesh_assert (_nodes[i] == nullptr ||
208 _nodes[i]->id() == i); // This will change soon
209
210 return _nodes[i];
211}
212
213
214
215
217{
218 if (i >= this->max_node_id())
219 return nullptr;
220 libmesh_assert (_nodes[i] == nullptr ||
221 _nodes[i]->id() == i); // This will change soon
222
223 return _nodes[i];
224}
225
226
227
228
230{
231 libmesh_assert_less (i, this->max_elem_id());
233 libmesh_assert_equal_to (_elements[i]->id(), i); // This will change soon
234
235 return _elements[i];
236}
237
238
239
240
242{
243 libmesh_assert_less (i, this->max_elem_id());
245 libmesh_assert_equal_to (_elements[i]->id(), i); // This will change soon
246
247 return _elements[i];
248}
249
250
251
252
254{
255 if (i >= this->max_elem_id())
256 return nullptr;
257 libmesh_assert (_elements[i] == nullptr ||
258 _elements[i]->id() == i); // This will change soon
259
260 return _elements[i];
261}
262
263
264
265
267{
268 if (i >= this->max_elem_id())
269 return nullptr;
270 libmesh_assert (_elements[i] == nullptr ||
271 _elements[i]->id() == i); // This will change soon
272
273 return _elements[i];
274}
275
276
277
278
280{
282
283 // We no longer merely append elements with ReplicatedMesh
284
285 // If the user requests a valid id that doesn't correspond to an
286 // existing element, let's give them that id, resizing the elements
287 // container if necessary.
288 if (!e->valid_id())
289 e->set_id (cast_int<dof_id_type>(_elements.size()));
290
291#ifdef LIBMESH_ENABLE_UNIQUE_ID
292 if (!e->valid_unique_id())
294 else
295 _next_unique_id = std::max(_next_unique_id, e->unique_id()+1);
296#endif
297
298 const dof_id_type id = e->id();
299
300 if (id < _elements.size())
301 {
302 // This should *almost* never happen, but we rely on it when
303 // using allgather to replicate a not-yet-actually-replicated
304 // ReplicatedMesh under construction in parallel.
305 if (e == _elements[id])
306 return e;
307
308 // Overwriting existing elements is still probably a mistake.
310 }
311 else
312 {
313 _elements.resize(id+1, nullptr);
314 }
315
316 ++_n_elem;
317 _elements[id] = e;
318
319 // We actually added a new element. Some of our caches might still
320 // be valid, but we should clear the ones which definitely are not.
321 this->clear_point_locator();
322 this->clear_stored_ranges();
323
324 // Make sure any new element is given space for any extra integers
325 // we've requested
328
329 // And set mapping type and data on any new element
332
333 return e;
334}
335
336Elem * ReplicatedMesh::add_elem (std::unique_ptr<Elem> e)
337{
338 // The mesh now takes ownership of the Elem. Eventually the guts of
339 // add_elem() will get moved to a private helper function, and
340 // calling add_elem() directly will be deprecated.
341 return add_elem(e.release());
342}
343
344
345
347{
348#ifdef LIBMESH_ENABLE_UNIQUE_ID
349 if (!e->valid_unique_id())
351 else
352 _next_unique_id = std::max(_next_unique_id, e->unique_id()+1);
353#endif
354
355 dof_id_type eid = e->id();
356 libmesh_assert_less (eid, _elements.size());
357 Elem * oldelem = _elements[eid];
358
359 if (oldelem)
360 {
361 libmesh_assert_equal_to (oldelem->id(), eid);
362 this->delete_elem(oldelem);
363 }
364
365 ++_n_elem;
366 _elements[eid] = e;
367
368 // We actually added a new element. Some of our caches might still
369 // be valid, but we should clear the ones which definitely are not.
370 this->clear_point_locator();
371 this->clear_stored_ranges();
372
373 // Make sure any new element is given space for any extra integers
374 // we've requested
377
378 // And set mapping type and data on any new element
381
382 return e;
383}
384
385Elem * ReplicatedMesh::insert_elem (std::unique_ptr<Elem> e)
386{
387 // The mesh now takes ownership of the Elem. Eventually the guts of
388 // insert_elem(Elem*) will get moved to a private helper function, and
389 // calling insert_elem(Elem*) directly will be deprecated.
390 return insert_elem(e.release());
391}
392
393
394
396{
398
399 // Initialize an iterator to eventually point to the element we want to delete
400 std::vector<Elem *>::iterator pos = _elements.end();
401
402 // In many cases, e->id() gives us a clue as to where e
403 // is located in the _elements vector. Try that first
404 // before trying the O(n_elem) search.
405 libmesh_assert_less (e->id(), _elements.size());
406
407 if (_elements[e->id()] == e)
408 {
409 // We found it!
410 pos = _elements.begin();
411 std::advance(pos, e->id());
412 }
413
414 else
415 {
416 // This search is O(n_elem)
417 pos = std::find (_elements.begin(),
418 _elements.end(),
419 e);
420 }
421
422 // Huh? Element not in the vector?
423 libmesh_assert (pos != _elements.end());
424
425 // Remove the element from the BoundaryInfo object
426 this->get_boundary_info().remove(e);
427
428 // delete the element
429 --_n_elem;
430 delete e;
431
432 // explicitly zero the pointer
433 *pos = nullptr;
434
435 // Some of our caches might still be valid, but we should clear the
436 // ones which definitely are not.
437 this->clear_point_locator();
438 this->clear_stored_ranges();
439}
440
441
442
444 const dof_id_type new_id)
445{
446 // This could be a no-op
447 if (old_id == new_id)
448 return;
449
450 // This doesn't get used in serial yet
451 Elem * el = _elements[old_id];
452 libmesh_assert (el);
453
454 if (new_id >= _elements.size())
455 _elements.resize(new_id+1, nullptr);
456
457 el->set_id(new_id);
458 libmesh_assert (!_elements[new_id]);
459 _elements[new_id] = el;
460 _elements[old_id] = nullptr;
461
462 // Should we delete any caches here? Our point locator indexes by
463 // element pointer and should be fine with an id change. Our stored
464 // ranges are no longer sorted, which is *probably* fine, but let's
465 // just be safe.
466 this->clear_stored_ranges();
467}
468
469
470
472 const dof_id_type id,
473 const processor_id_type proc_id)
474{
475 Node * n = nullptr;
476
477 // If the user requests a valid id, either
478 // provide the existing node or resize the container
479 // to fit the new node.
480 if (id != DofObject::invalid_id)
481 if (id < _nodes.size())
482 n = _nodes[id];
483 else
484 _nodes.resize(id+1);
485 else
486 _nodes.push_back (static_cast<Node *>(nullptr));
487
488 // if the node already exists, then assign new (x,y,z) values
489 if (n)
490 *n = p;
491 // otherwise build a new node, put it in the right spot, and return
492 // a valid pointer.
493 else
494 {
495 n = Node::build(p, (id == DofObject::invalid_id) ?
496 cast_int<dof_id_type>(_nodes.size()-1) : id).release();
497 n->processor_id() = proc_id;
498
499 // A new node invalidates any cached node ranges
500 this->clear_stored_ranges();
501
504
505#ifdef LIBMESH_ENABLE_UNIQUE_ID
506 if (!n->valid_unique_id())
508 else
509 _next_unique_id = std::max(_next_unique_id, n->unique_id()+1);
510#endif
511
512 ++_n_nodes;
513 if (id == DofObject::invalid_id)
514 _nodes.back() = n;
515 else
516 _nodes[id] = n;
517 }
518
519 // better not pass back a nullptr.
520 libmesh_assert (n);
521
522 return n;
523}
524
525
526
528{
530
531 // If the user requests a valid id, either set the existing
532 // container entry or resize the container to fit the new node.
533 if (n->valid_id())
534 {
535 const dof_id_type id = n->id();
536 if (id < _nodes.size())
537 libmesh_assert(!_nodes[id]);
538 else
539 _nodes.resize(id+1); // default nullptr
540
541 _nodes[id] = n;
542 }
543 else
544 {
545 n->set_id (cast_int<dof_id_type>(_nodes.size()));
546 _nodes.push_back(n);
547 }
548
549 ++_n_nodes;
550
551#ifdef LIBMESH_ENABLE_UNIQUE_ID
552 if (!n->valid_unique_id())
554 else
555 _next_unique_id = std::max(_next_unique_id, n->unique_id()+1);
556#endif
557
558 // A new node invalidates any cached node ranges
559 this->clear_stored_ranges();
560
563
564 return n;
565}
566
567Node * ReplicatedMesh::add_node (std::unique_ptr<Node> n)
568{
569 // The mesh now takes ownership of the Node. Eventually the guts of
570 // add_node() will get moved to a private helper function, and
571 // calling add_node() directly will be deprecated.
572 return add_node(n.release());
573}
574
576{
578 libmesh_assert_less (n->id(), _nodes.size());
579
580 // Initialize an iterator to eventually point to the element we want
581 // to delete
582 std::vector<Node *>::iterator pos;
583
584 // In many cases, e->id() gives us a clue as to where e
585 // is located in the _elements vector. Try that first
586 // before trying the O(n_elem) search.
587 if (_nodes[n->id()] == n)
588 {
589 pos = _nodes.begin();
590 std::advance(pos, n->id());
591 }
592 else
593 {
594 pos = std::find (_nodes.begin(),
595 _nodes.end(),
596 n);
597 }
598
599 // Huh? Node not in the vector?
600 libmesh_assert (pos != _nodes.end());
601
602 // Delete the node from the BoundaryInfo object
603 this->get_boundary_info().remove(n);
604 _constraint_rows.erase(n);
605
606 // delete the node
607 --_n_nodes;
608 delete n;
609
610 // explicitly zero the pointer
611 *pos = nullptr;
612
613 // We've invalidated any ranges that contained this node
614 this->clear_stored_ranges();
615}
616
617
618
620 const dof_id_type new_id)
621{
622 // This could be a no-op
623 if (old_id == new_id)
624 return;
625
626 // This doesn't get used in serial yet
627 Node * nd = _nodes[old_id];
628 libmesh_assert (nd);
629
630 if (new_id >= _nodes.size())
631 _nodes.resize(new_id+1, nullptr);
632
633 nd->set_id(new_id);
634 libmesh_assert (!_nodes[new_id]);
635 _nodes[new_id] = nd;
636 _nodes[old_id] = nullptr;
637}
638
639
640
642{
643 // Call parent clear function
645
646 // Clear our elements and nodes
647 // There is no need to remove them from
648 // the BoundaryInfo data structure since we
649 // already cleared it.
651
652 for (auto & node : _nodes)
653 delete node;
654
655 _n_nodes = 0;
656 _nodes.clear();
657}
658
659
660
662{
663 for (auto & elem : _elements)
664 delete elem;
665
666 _n_elem = 0;
667 _elements.clear();
668}
669
670
671
673{
674#ifdef LIBMESH_ENABLE_UNIQUE_ID
676#endif
677
678 // Implicitly get max_elem_id/max_node_id by trimming the vectors
679 auto trim_vec = [](auto & dof_vec) {
680 auto last_non_null = std::find_if(dof_vec.rbegin(), dof_vec.rend(), [](DofObject * d) { return (d != nullptr); });
681 dof_vec.resize(last_non_null.base()-dof_vec.begin());
682 };
683
684 trim_vec(this->_nodes);
685 trim_vec(this->_elements);
686
688
690}
691
692
693
694#ifdef LIBMESH_ENABLE_UNIQUE_ID
696{
697 // This function must be run on all processors at once
698 parallel_object_only();
699
701 this->comm().max(max_local);
702 return max_local;
703}
704
705
706
711#endif
712
713
714
716{
717 LOG_SCOPE("renumber_nodes_and_elem()", "Mesh");
718
719 // node and element id counters
720 dof_id_type next_free_elem = 0;
721 dof_id_type next_free_node = 0;
722
723 // Will hold the set of nodes that are currently connected to elements
724 std::unordered_set<Node *> connected_nodes;
725
726 // Loop over the elements. Note that there may
727 // be nullptrs in the _elements vector from the coarsening
728 // process. Pack the elements in to a contiguous array
729 // and then trim any excess.
730 {
731 std::vector<Elem *>::iterator in = _elements.begin();
732 std::vector<Elem *>::iterator out_iter = _elements.begin();
733 const std::vector<Elem *>::iterator end = _elements.end();
734
735 for (; in != end; ++in)
736 if (*in != nullptr)
737 {
738 Elem * el = *in;
739
740 *out_iter = *in;
741 ++out_iter;
742
743 // Increment the element counter
744 el->set_id (next_free_elem++);
745
747 {
748 // Add this elements nodes to the connected list
749 for (auto & n : el->node_ref_range())
750 connected_nodes.insert(&n);
751 }
752 else // We DO want node renumbering
753 {
754 // Loop over this element's nodes. Number them,
755 // if they have not been numbered already. Also,
756 // position them in the _nodes vector so that they
757 // are packed contiguously from the beginning.
758 for (auto & n : el->node_ref_range())
759 if (n.id() == next_free_node) // don't need to process
760 next_free_node++; // [(src == dst) below]
761
762 else if (n.id() > next_free_node) // need to process
763 {
764 // The source and destination indices
765 // for this node
766 const dof_id_type src_idx = n.id();
767 const dof_id_type dst_idx = next_free_node++;
768
769 // ensure we want to swap a valid nodes
770 libmesh_assert(_nodes[src_idx]);
771
772 // Swap the source and destination nodes
773 std::swap(_nodes[src_idx],
774 _nodes[dst_idx] );
775
776 // Set proper indices where that makes sense
777 if (_nodes[src_idx] != nullptr)
778 _nodes[src_idx]->set_id (src_idx);
779 _nodes[dst_idx]->set_id (dst_idx);
780 }
781 }
782 }
783
784 // Erase any additional storage. These elements have been
785 // copied into nullptr voids by the procedure above, and are
786 // thus repeated and unnecessary.
787 _elements.erase (out_iter, end);
788 }
789
790
792 {
793 // Loop over the nodes. Note that there may
794 // be nullptrs in the _nodes vector from the coarsening
795 // process. Pack the nodes in to a contiguous array
796 // and then trim any excess.
797
798 std::vector<Node *>::iterator in = _nodes.begin();
799 std::vector<Node *>::iterator out_iter = _nodes.begin();
800 const std::vector<Node *>::iterator end = _nodes.end();
801
802 for (; in != end; ++in)
803 if (*in != nullptr)
804 {
805 // This is a reference so that if we change the pointer it will change in the vector
806 Node * & nd = *in;
807
808 // If this node is still connected to an elem, put it in the list
809 if (connected_nodes.count(nd))
810 {
811 *out_iter = nd;
812 ++out_iter;
813
814 // Increment the node counter
815 nd->set_id (next_free_node++);
816 }
817 else // This node is orphaned, delete it!
818 {
819 this->get_boundary_info().remove (nd);
820 _constraint_rows.erase(nd);
821
822 // delete the node
823 --_n_nodes;
824 delete nd;
825 nd = nullptr;
826 }
827 }
828
829 // Erase any additional storage. Whatever was
830 _nodes.erase (out_iter, end);
831 }
832 else // We really DO want node renumbering
833 {
834 // Any nodes in the vector >= _nodes[next_free_node]
835 // are not connected to any elements and may be deleted
836 // if desired.
837
838 // Now, delete the unused nodes
839 {
840 std::vector<Node *>::iterator nd = _nodes.begin();
841 const std::vector<Node *>::iterator end = _nodes.end();
842
843 std::advance (nd, next_free_node);
844
845 for (auto & node : as_range(nd, end))
846 {
847 // Mesh modification code might have already deleted some
848 // nodes
849 if (node == nullptr)
850 continue;
851
852 // remove any boundary information associated with
853 // this node
854 this->get_boundary_info().remove (node);
855 _constraint_rows.erase(node);
856
857 // delete the node
858 --_n_nodes;
859 delete node;
860 node = nullptr;
861 }
862
863 _nodes.erase (nd, end);
864 }
865 }
866
867 // We've probably done *some* node removal and/or renumbering; let's
868 // clear caches to be safe.
869 this->clear_stored_ranges();
870
872
873 libmesh_assert_equal_to (next_free_elem, _elements.size());
874 libmesh_assert_equal_to (next_free_node, _nodes.size());
875
877}
878
879
880
882{
883 // Nodes first
884 for (auto n : index_range(_nodes))
885 if (this->_nodes[n] != nullptr)
886 this->_nodes[n]->set_id() = cast_int<dof_id_type>(n);
887
888 // Elements next
889 for (auto e : index_range(_elements))
890 if (this->_elements[e] != nullptr)
891 this->_elements[e]->set_id() = cast_int<dof_id_type>(e);
892}
893
894
896{
897 return static_cast<dof_id_type>(std::distance (this->active_elements_begin(),
898 this->active_elements_end()));
899}
900
901std::vector<dof_id_type>
902ReplicatedMesh::get_disconnected_subdomains(std::vector<subdomain_id_type> * subdomain_ids) const
903{
904 // find number of disconnected subdomains
905 std::vector<dof_id_type> representative_elem_ids;
906
907 // use subdomain_ids as markers for all elements to indicate if the elements
908 // have been visited. Note: here subdomain ID is unrelated with element
909 // subdomain_id().
910 std::vector<subdomain_id_type> subdomains;
911 if (!subdomain_ids)
912 subdomain_ids = &subdomains;
913 subdomain_ids->clear();
915
916 // counter of disconnected subdomains
917 subdomain_id_type subdomain_counter = 0;
918
919 // a stack for visiting elements, make its capacity sufficiently large to avoid
920 // memory allocation and deallocation when the vector size changes
921 std::vector<const Elem *> list;
922 list.reserve(n_elem());
923
924 // counter of visited elements
925 dof_id_type visited = 0;
926 dof_id_type n_active = n_active_elem();
927 do
928 {
929 for (const auto & elem : active_element_ptr_range())
930 if ((*subdomain_ids)[elem->id()] == Elem::invalid_subdomain_id)
931 {
932 list.push_back(elem);
933 (*subdomain_ids)[elem->id()] = subdomain_counter;
934 break;
935 }
936 // we should be able to find a seed here
937 libmesh_assert(list.size() > 0);
938
939 dof_id_type min_id = std::numeric_limits<dof_id_type>::max();
940 while (list.size() > 0)
941 {
942 // pop up an element
943 const Elem * elem = list.back(); list.pop_back(); ++visited;
944
945 min_id = std::min(elem->id(), min_id);
946
947 for (auto s : elem->side_index_range())
948 {
949 const Elem * neighbor = elem->neighbor_ptr(s);
950 if (neighbor != nullptr && (*subdomain_ids)[neighbor->id()] == Elem::invalid_subdomain_id)
951 {
952 // neighbor must be active
953 libmesh_assert(neighbor->active());
954 list.push_back(neighbor);
955 (*subdomain_ids)[neighbor->id()] = subdomain_counter;
956 }
957 }
958 }
959
960 representative_elem_ids.push_back(min_id);
961 subdomain_counter++;
962 }
963 while (visited != n_active);
964
965 return representative_elem_ids;
966}
967
968std::unordered_map<dof_id_type, std::vector<std::vector<Point>>>
970{
971 libmesh_error_msg_if(mesh_dimension() != 2,
972 "Error: get_boundary_points only works for 2D now");
973
974 // find number of disconnected subdomains
975 // subdomains will hold the IDs of disconnected subdomains for all elements.
976 std::vector<subdomain_id_type> subdomains;
977 std::vector<dof_id_type> elem_ids = get_disconnected_subdomains(&subdomains);
978
979 std::unordered_map<dof_id_type, std::vector<std::vector<Point>>> boundary_points;
980
981 // get all boundary sides that are to be erased later during visiting
982 // use a comparison functor to avoid run-time randomness due to pointers
983 struct boundary_side_compare
984 {
985 bool operator()(const std::pair<const Elem *, unsigned int> & lhs,
986 const std::pair<const Elem *, unsigned int> & rhs) const
987 {
988 if (lhs.first->id() < rhs.first->id())
989 return true;
990 else if (lhs.first->id() == rhs.first->id())
991 {
992 if (lhs.second < rhs.second)
993 return true;
994 }
995 return false;
996 }
997 };
998 std::set<std::pair<const Elem *, unsigned int>, boundary_side_compare> boundary_elements;
999 for (const auto & elem : active_element_ptr_range())
1000 for (auto s : elem->side_index_range())
1001 if (elem->neighbor_ptr(s) == nullptr)
1002 boundary_elements.insert(std::pair<const Elem *, unsigned int>(elem, s));
1003
1004 while (!boundary_elements.empty())
1005 {
1006 // get the first entry as the seed
1007 const Elem * eseed = boundary_elements.begin()->first;
1008 unsigned int sseed = boundary_elements.begin()->second;
1009
1010 // get the subdomain ID that these boundary sides attached to
1011 subdomain_id_type subdomain_id = subdomains[eseed->id()];
1012
1013 // start visiting the mesh to find all boundary nodes with the seed
1014 std::vector<Point> bpoints;
1015 const Elem * elem = eseed;
1016 unsigned int s = sseed;
1017 std::vector<unsigned int> local_side_nodes = elem->nodes_on_side(s);
1018 while (true)
1019 {
1020 std::pair<const Elem *, unsigned int> side(elem, s);
1021 libmesh_assert(boundary_elements.count(side));
1022 boundary_elements.erase(side);
1023
1024 // push all nodes on the side except the node on the other end of the side (index 1)
1025 for (auto i : index_range(local_side_nodes))
1026 if (i != 1)
1027 bpoints.push_back(*static_cast<const Point *>(elem->node_ptr(local_side_nodes[i])));
1028
1029 // use the last node to find next element and side
1030 const Node * node = elem->node_ptr(local_side_nodes[1]);
1031 std::set<const Elem *> neighbors;
1032 elem->find_point_neighbors(*node, neighbors);
1033
1034 // if only one neighbor is found (itself), this node is a cornor node on boundary
1035 if (neighbors.size() != 1)
1036 neighbors.erase(elem);
1037
1038 // find the connecting side
1039 bool found = false;
1040 for (const auto & neighbor : neighbors)
1041 {
1042 for (auto ss : neighbor->side_index_range())
1043 if (neighbor->neighbor_ptr(ss) == nullptr && !(elem == neighbor && s == ss))
1044 {
1045 local_side_nodes = neighbor->nodes_on_side(ss);
1046 // we expect the starting point of the side to be the same as the end of the previous side
1047 if (neighbor->node_ptr(local_side_nodes[0]) == node)
1048 {
1049 elem = neighbor;
1050 s = ss;
1051 found = true;
1052 break;
1053 }
1054 else if (neighbor->node_ptr(local_side_nodes[1]) == node)
1055 {
1056 elem = neighbor;
1057 s = ss;
1058 found = true;
1059 // flip nodes in local_side_nodes because the side is in an opposite direction
1060 auto temp(local_side_nodes);
1061 local_side_nodes[0] = temp[1];
1062 local_side_nodes[1] = temp[0];
1063 for (unsigned int i = 2; i < temp.size(); ++i)
1064 local_side_nodes[temp.size() + 1 - i] = temp[i];
1065 break;
1066 }
1067 }
1068 if (found)
1069 break;
1070 }
1071
1072 libmesh_error_msg_if(!found, "ERROR: mesh topology error on visiting boundary sides");
1073
1074 // exit if we reach the starting point
1075 if (elem == eseed && s == sseed)
1076 break;
1077 }
1078 boundary_points[elem_ids[subdomain_id]].push_back(bpoints);
1079 }
1080
1081 return boundary_points;
1082}
1083
1084} // namespace libMesh
void max(const T &r, T &o, Request &req) const
void remove(const Node *node)
Removes the boundary conditions associated with node node, if any exist.
virtual void clear() override
Free all new memory associated with the object, but restore its original state, with the mesh pointer...
Definition dof_map.C:892
The DofObject defines an abstract base class for objects that have degrees of freedom associated with...
Definition dof_object.h:55
bool valid_id() const
Definition dof_object.h:875
processor_id_type processor_id() const
Definition dof_object.h:895
dof_id_type & set_id()
Definition dof_object.h:841
void add_extra_integers(const unsigned int n_integers)
Assigns a set of extra integers to this DofObject.
Definition dof_object.C:482
static constexpr dof_id_type invalid_id
An invalid id to distinguish an uninitialized DofObject.
Definition dof_object.h:473
unique_id_type unique_id() const
Definition dof_object.h:849
bool valid_unique_id() const
Definition dof_object.h:883
dof_id_type id() const
Definition dof_object.h:833
void set_unique_id(unique_id_type new_id)
Sets the unique_id for this DofObject.
Definition dof_object.h:862
This is the base class from which all geometric element types are derived.
Definition elem.h:96
bool active() const
Definition elem.h:3079
SimpleRange< NodeRefIter > node_ref_range()
Returns a range with all nodes of an element, usable in range-based for loops.
Definition elem.h:2803
virtual std::vector< unsigned int > nodes_on_side(const unsigned int) const =0
void find_point_neighbors(const Point &p, std::set< const Elem * > &neighbor_set) const
This function finds all active elements (including this one) which are in the same manifold as this e...
Definition elem.C:907
static constexpr subdomain_id_type invalid_subdomain_id
A static integral constant representing an invalid subdomain id.
Definition elem.h:246
const Node * node_ptr(const unsigned int i) const
Definition elem.h:2637
void set_mapping_type(const ElemMappingType type)
Sets the value of the mapping type for the element.
Definition elem.h:3266
void set_mapping_data(const unsigned char data)
Sets the value of the mapping data for the element.
Definition elem.h:3282
const Elem * neighbor_ptr(unsigned int i) const
Definition elem.h:2736
IntRange< unsigned short > side_index_range() const
Definition elem.h:2848
This is the MeshBase class.
Definition mesh_base.h:81
void allow_remote_element_removal(bool allow)
If false is passed in then this mesh will no longer have remote elements deleted when being prepared ...
Definition mesh_base.h:1390
virtual const Node & node_ref(const dof_id_type i) const
Definition mesh_base.h:750
virtual bool is_serial() const
Definition mesh_base.h:365
const BoundaryInfo & get_boundary_info() const
The information about boundary ids on the mesh.
Definition mesh_base.h:178
Preparation preparation() const
Definition mesh_base.h:221
bool allow_renumbering() const
Definition mesh_base.h:1368
unsigned int mesh_dimension() const
Definition mesh_base.C:515
bool skip_partitioning() const
Definition mesh_base.h:1450
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:1214
bool allow_detect_interior_parents() const
Definition mesh_base.h:1382
void allow_find_neighbors(bool allow)
If false is passed then this mesh will no longer work to find element neighbors when being prepared f...
Definition mesh_base.h:1374
void allow_renumbering(bool allow)
If false is passed in then this mesh will no longer be renumbered when being prepared for use.
Definition mesh_base.h:1367
const std::map< subdomain_id_type, std::string > & get_subdomain_name_map() const
Definition mesh_base.h:1984
ElemMappingType default_mapping_type() const
Returns the default master space to physical space mapping basis functions to be used on newly added ...
Definition mesh_base.h:946
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:2552
unique_id_type _next_unique_id
The next available unique id for assigning ids to DOF objects.
Definition mesh_base.h:2402
bool _skip_renumber_nodes_and_elements
If this is true then renumbering will be kept to a minimum.
Definition mesh_base.h:2427
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:2534
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:2540
virtual void clear()
Deletes all the element and node data that is currently stored.
Definition mesh_base.C:1123
constraint_rows_type _constraint_rows
Definition mesh_base.h:2605
std::unique_ptr< Partitioner > _partitioner
A partitioner to use at each prepare_for_use().
Definition mesh_base.h:2396
bool allow_remote_element_removal() const
Definition mesh_base.h:1391
bool allow_find_neighbors() const
Definition mesh_base.h:1375
dof_id_type _n_constraint_rows
Definition mesh_base.h:2607
unsigned char default_mapping_data() const
Returns any default data value used by the master space to physical space mapping.
Definition mesh_base.h:964
void clear_point_locator()
Releases the current PointLocator object.
Definition mesh_base.C:1985
void copy_constraint_rows(const MeshBase &other_mesh)
Copy the constraints from the other mesh to this mesh.
Definition mesh_base.C:2838
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:2546
std::map< subdomain_id_type, std::string > & set_subdomain_name_map()
Definition mesh_base.h:1982
dof_id_type count_constraint_rows()
Helper function for subclasses to use to set _n_constraint_rows.
Definition mesh_base.C:2819
Preparation _preparation
Flags indicating in what ways this mesh has been prepared.
Definition mesh_base.h:2278
void skip_partitioning(bool skip)
If true is passed in then nothing on this mesh will be (re)partitioned.
Definition mesh_base.h:1448
void allow_detect_interior_parents(bool allow)
If false is passed then this mesh will no longer work to detect interior parents when being prepared ...
Definition mesh_base.h:1381
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:2789
void post_dofobject_moves(MeshBase &&other_mesh)
Moves any superclass data (e.g.
Definition mesh_base.C:2743
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:2302
This is the MeshCommunication class.
void allgather(MeshBase &mesh) const
This method takes an input DistributedMesh which may be distributed among all the processors.
A Node is like a Point, but with more information.
Definition node.h:55
static std::unique_ptr< Node > build(const Node &n)
Definition node.h:315
const Parallel::Communicator & comm() const
static std::unique_ptr< Partitioner > build(const PartitionerType solver_package)
Builds a Partitioner of the type specified by partitioner_type.
A Point defines a location in LIBMESH_DIM dimensional Real space.
Definition point.h:41
The ReplicatedMesh class is derived from the MeshBase class, and is used to store identical copies of...
virtual void clear() override
Clear all internal data.
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) override final
functions for adding /deleting nodes elements.
virtual std::string_view subclass_first_difference_from(const MeshBase &other_mesh_base) const override
Shim to allow operator == (&) to behave like a virtual function without having to be one.
std::vector< Elem * > _elements
The elements in the mesh.
virtual Node * add_node(Node *n) override final
Add Node n to the end of the vertex array.
virtual void update_parallel_id_counts() override
Updates parallel caches so that methods like n_elem() accurately reflect changes on other processors.
virtual void move_nodes_and_elements(MeshBase &&other_mesh) override
Move node and elements from a ReplicatedMesh.
virtual void delete_node(Node *n) override final
Removes the Node n from the mesh.
ReplicatedMesh(const Parallel::Communicator &comm_in, unsigned char dim=1)
Constructor.
virtual void renumber_node(dof_id_type old_id, dof_id_type new_id) override final
Changes the id of node old_id, both by changing node(old_id)->id() and by moving node(old_id) in the ...
virtual dof_id_type max_node_id() const override final
virtual dof_id_type max_elem_id() const override final
virtual const Node * node_ptr(const dof_id_type i) const override final
virtual void renumber_elem(dof_id_type old_id, dof_id_type new_id) override final
Changes the id of element old_id, both by changing elem(old_id)->id() and by moving elem(old_id) in t...
const DofMap &dof_map LIBMESH_COMMA unsigned int dof_map LIBMESH_COMMA var_num unsigned char rflag processor_id_type pid const DofMap &dof_map LIBMESH_COMMA unsigned int dof_map LIBMESH_COMMA var_num DECLARE_NODE_ITERATORS(multi_evaluable_, std::vector< const DofMap * > dof_maps, dof_maps) protected dof_id_typ _n_nodes)
The vertices (spatial coordinates) of the mesh.
virtual dof_id_type n_active_elem() const override final
virtual Elem * insert_elem(Elem *e) override final
Insert elem e to the element array, preserving its id and replacing/deleting any existing element wit...
virtual dof_id_type n_elem() const override final
virtual void delete_elem(Elem *e) override final
Removes element e from the mesh.
virtual const Elem * query_elem_ptr(const dof_id_type i) const override final
std::unordered_map< dof_id_type, std::vector< std::vector< Point > > > get_boundary_points() const
Return all points on boundary.
virtual const Elem * elem_ptr(const dof_id_type i) const override final
virtual Elem * add_elem(Elem *e) override final
Add elem e to the end of the element array.
ReplicatedMesh & operator=(const ReplicatedMesh &)=delete
Copy assignment is not allowed.
virtual dof_id_type n_nodes() const override final
unsigned int level ElemType type std::set< subdomain_id_type > ss
std::vector< dof_id_type > get_disconnected_subdomains(std::vector< subdomain_id_type > *subdomain_ids=nullptr) const
Return IDs of representative elements of all disconnected subdomains.
virtual MeshBase & assign(MeshBase &&other_mesh) override
Shim to call the move assignment operator for this class.
virtual void clear_elems() override
Clear internal Elem data.
virtual void fix_broken_node_and_element_numbering() override
There is no reason for a user to ever call this function.
virtual const Point & point(const dof_id_type i) const override final
virtual void set_next_unique_id(unique_id_type id) override final
Sets the next available unique id to be used.
virtual unique_id_type parallel_max_unique_id() const override final
virtual const Node * query_node_ptr(const dof_id_type i) const override final
virtual void renumber_nodes_and_elements() override
After partitioning a mesh it is useful to renumber the nodes and elements so that they lie in contigu...
virtual ~ReplicatedMesh()
Destructor.
The UnstructuredMesh class is derived from the MeshBase class.
virtual void copy_nodes_and_elements(const MeshBase &other_mesh, const bool skip_find_neighbors=false, dof_id_type element_id_offset=0, dof_id_type node_id_offset=0, unique_id_type unique_id_offset=0, std::unordered_map< subdomain_id_type, subdomain_id_type > *id_remapping=nullptr, const bool skip_preparation=false)
Deep copy of nodes and elements from another mesh object (used by subclass copy constructors and by m...
UnstructuredMesh & operator=(const UnstructuredMesh &)=delete
Copy assignment is not allowed.
The libMesh namespace provides an interface to certain functionality in the library.
uint8_t unique_id_type
Definition id_types.h:86
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
libmesh_assert(ctx)
T command_line_value(const std::string &, T)
Definition libmesh.C:971
uint8_t dof_id_type
Definition id_types.h:67
uint8_t processor_id_type
Definition id_types.h:104