LCOV - code coverage report
Current view: top level - src/mesh - replicated_mesh.C (source / functions) Hit Total Coverage
Test: libMesh/libmesh: #4667 (c50533) with base aa4ea8 Lines: 434 455 95.4 %
Date: 2026-11-17 18:48:15 Functions: 58 58 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : // The libMesh Finite Element Library.
       2             : // Copyright (C) 2002-2026 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
       3             : 
       4             : // This library is free software; you can redistribute 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             : 
      37             : namespace libMesh
      38             : {
      39             : 
      40             : // ------------------------------------------------------------
      41             : // ReplicatedMesh class member functions
      42       79414 : ReplicatedMesh::ReplicatedMesh (const Parallel::Communicator & comm_in,
      43       79414 :                                 unsigned char d) :
      44             :   UnstructuredMesh (comm_in,d),
      45       79414 :   _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.
      50       79414 :   _next_unique_id = 0;
      51             : #endif
      52             : 
      53       93911 :   const std::string default_partitioner = "metis";
      54             :   const std::string my_partitioner =
      55             :     libMesh::command_line_value("--default-partitioner",
      56      158828 :                                 default_partitioner);
      57             :   _partitioner = Partitioner::build
      58      144331 :     (Utility::string_to_enum<PartitionerType>(my_partitioner));
      59       79414 : }
      60             : 
      61             : 
      62       36897 : std::string_view ReplicatedMesh::subclass_first_difference_from (const MeshBase & other_mesh_base) const
      63             : {
      64       35540 :   const ReplicatedMesh * rep_mesh_ptr =
      65       36897 :     dynamic_cast<const ReplicatedMesh *>(&other_mesh_base);
      66       36897 :   if (!rep_mesh_ptr)
      67           0 :     return "ReplicatedMesh class";
      68       35540 :   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       36897 :   CHECK_MEMBER(_n_nodes);
      75       36897 :   CHECK_MEMBER(_n_elem);
      76             : #ifdef LIBMESH_ENABLE_UNIQUE_ID
      77       36897 :   CHECK_MEMBER(_next_unique_id);
      78             : #endif
      79       36897 :   if (!this->nodes_and_elements_equal(other_mesh))
      80          78 :     return "nodes and/or elements";
      81             : 
      82       36819 :   return "";
      83             : }
      84             : 
      85             : 
      86      128411 : ReplicatedMesh::~ReplicatedMesh ()
      87             : {
      88      100848 :   this->ReplicatedMesh::clear();  // Free nodes and elements
      89      128411 : }
      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.
      95       21363 : ReplicatedMesh::ReplicatedMesh (const ReplicatedMesh & other_mesh) :
      96       21363 :   ReplicatedMesh(static_cast<const MeshBase&>(other_mesh))
      97             : {
      98             : #ifdef LIBMESH_ENABLE_UNIQUE_ID
      99       21363 :   this->_next_unique_id = other_mesh._next_unique_id;
     100             : #endif
     101       21363 : }
     102             : 
     103             : 
     104       21434 : ReplicatedMesh::ReplicatedMesh (const MeshBase & other_mesh) :
     105             :   UnstructuredMesh (other_mesh),
     106       21434 :   _n_nodes(0), _n_elem(0) // copy_* will increment this
     107             : {
     108             :   // Just copy, skipping preparation
     109       21434 :   this->copy_nodes_and_elements(other_mesh, true, 0, 0, 0, nullptr, true);
     110             : 
     111       18672 :   this->allow_find_neighbors(other_mesh.allow_find_neighbors());
     112       18672 :   this->allow_detect_interior_parents(other_mesh.allow_detect_interior_parents());
     113       18672 :   this->allow_renumbering(other_mesh.allow_renumbering());
     114       18672 :   this->allow_remote_element_removal(other_mesh.allow_remote_element_removal());
     115       18672 :   this->skip_partitioning(other_mesh.skip_partitioning());
     116             : 
     117       21434 :   this->copy_constraint_rows(other_mesh);
     118             : 
     119       17974 :   auto & this_boundary_info = this->get_boundary_info();
     120       17974 :   const auto & other_boundary_info = other_mesh.get_boundary_info();
     121             : 
     122       21434 :   this_boundary_info = other_boundary_info;
     123             : 
     124       17974 :   this->set_subdomain_name_map() = other_mesh.get_subdomain_name_map();
     125             : 
     126       21434 :   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       21434 :   if (!other_mesh.is_serial())
     131          71 :     MeshCommunication().allgather(*this);
     132       21434 : }
     133             : 
     134         156 : ReplicatedMesh & ReplicatedMesh::operator= (ReplicatedMesh && other_mesh)
     135             : {
     136          16 :   LOG_SCOPE("operator=(&&)", "ReplicatedMesh");
     137             : 
     138             :   // Move assign as an UnstructuredMesh
     139           8 :   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         156 :   this->move_nodes_and_elements(std::move(other_mesh));
     145             : 
     146             :   // Handle those remaining moves.
     147         156 :   this->post_dofobject_moves(std::move(other_mesh));
     148             : 
     149         164 :   return *this;
     150             : }
     151             : 
     152         156 : MeshBase & ReplicatedMesh::assign(MeshBase && other_mesh)
     153             : {
     154         156 :   *this = std::move(cast_ref<ReplicatedMesh&>(other_mesh));
     155             : 
     156         156 :   return *this;
     157             : }
     158             : 
     159         156 : void ReplicatedMesh::move_nodes_and_elements(MeshBase && other_meshbase)
     160             : {
     161           8 :   ReplicatedMesh & other_mesh = cast_ref<ReplicatedMesh&>(other_meshbase);
     162             : 
     163         156 :   this->_nodes = std::move(other_mesh._nodes);
     164         156 :   this->_n_nodes = other_mesh.n_nodes();
     165             : 
     166         156 :   this->_elements = std::move(other_mesh._elements);
     167         156 :   this->_n_elem = other_mesh.n_elem();
     168         156 : }
     169             : 
     170             : 
     171    16102697 : const Point & ReplicatedMesh::point (const dof_id_type i) const
     172             : {
     173    16102697 :   return this->node_ref(i);
     174             : }
     175             : 
     176             : 
     177             : 
     178             : 
     179   101690672 : const Node * ReplicatedMesh::node_ptr (const dof_id_type i) const
     180             : {
     181     5301712 :   libmesh_assert_less (i, this->max_node_id());
     182     5301712 :   libmesh_assert(_nodes[i]);
     183     5301712 :   libmesh_assert_equal_to (_nodes[i]->id(), i); // This will change soon
     184             : 
     185   106942100 :   return _nodes[i];
     186             : }
     187             : 
     188             : 
     189             : 
     190             : 
     191   115057777 : Node * ReplicatedMesh::node_ptr (const dof_id_type i)
     192             : {
     193    20878789 :   libmesh_assert_less (i, this->max_node_id());
     194    20878789 :   libmesh_assert(_nodes[i]);
     195    20878789 :   libmesh_assert_equal_to (_nodes[i]->id(), i); // This will change soon
     196             : 
     197   124616532 :   return _nodes[i];
     198             : }
     199             : 
     200             : 
     201             : 
     202             : 
     203    36302218 : const Node * ReplicatedMesh::query_node_ptr (const dof_id_type i) const
     204             : {
     205    36302218 :   if (i >= this->max_node_id())
     206           0 :     return nullptr;
     207    36052213 :   libmesh_assert (_nodes[i] == nullptr ||
     208             :                   _nodes[i]->id() == i); // This will change soon
     209             : 
     210    36387400 :   return _nodes[i];
     211             : }
     212             : 
     213             : 
     214             : 
     215             : 
     216     5175021 : Node * ReplicatedMesh::query_node_ptr (const dof_id_type i)
     217             : {
     218     5175021 :   if (i >= this->max_node_id())
     219       90355 :     return nullptr;
     220     2577844 :   libmesh_assert (_nodes[i] == nullptr ||
     221             :                   _nodes[i]->id() == i); // This will change soon
     222             : 
     223     4515504 :   return _nodes[i];
     224             : }
     225             : 
     226             : 
     227             : 
     228             : 
     229     3234919 : const Elem * ReplicatedMesh::elem_ptr (const dof_id_type i) const
     230             : {
     231      546817 :   libmesh_assert_less (i, this->max_elem_id());
     232      546817 :   libmesh_assert(_elements[i]);
     233      546817 :   libmesh_assert_equal_to (_elements[i]->id(), i); // This will change soon
     234             : 
     235     3657227 :   return _elements[i];
     236             : }
     237             : 
     238             : 
     239             : 
     240             : 
     241    94098303 : Elem * ReplicatedMesh::elem_ptr (const dof_id_type i)
     242             : {
     243     6477585 :   libmesh_assert_less (i, this->max_elem_id());
     244     6477585 :   libmesh_assert(_elements[i]);
     245     6477585 :   libmesh_assert_equal_to (_elements[i]->id(), i); // This will change soon
     246             : 
     247    98608060 :   return _elements[i];
     248             : }
     249             : 
     250             : 
     251             : 
     252             : 
     253    27817654 : const Elem * ReplicatedMesh::query_elem_ptr (const dof_id_type i) const
     254             : {
     255    27817654 :   if (i >= this->max_elem_id())
     256           0 :     return nullptr;
     257    27733314 :   libmesh_assert (_elements[i] == nullptr ||
     258             :                   _elements[i]->id() == i); // This will change soon
     259             : 
     260    27847238 :   return _elements[i];
     261             : }
     262             : 
     263             : 
     264             : 
     265             : 
     266     2570461 : Elem * ReplicatedMesh::query_elem_ptr (const dof_id_type i)
     267             : {
     268     2570461 :   if (i >= this->max_elem_id())
     269       45361 :     return nullptr;
     270     1282332 :   libmesh_assert (_elements[i] == nullptr ||
     271             :                   _elements[i]->id() == i); // This will change soon
     272             : 
     273     1916130 :   return _elements[i];
     274             : }
     275             : 
     276             : 
     277             : 
     278             : 
     279    16879327 : Elem * ReplicatedMesh::add_elem (Elem * e)
     280             : {
     281     2537436 :   libmesh_assert(e);
     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    16879327 :   if (!e->valid_id())
     289      832232 :     e->set_id (cast_int<dof_id_type>(_elements.size()));
     290             : 
     291             : #ifdef LIBMESH_ENABLE_UNIQUE_ID
     292    16879327 :   if (!e->valid_unique_id())
     293     5262509 :     e->set_unique_id(_next_unique_id++);
     294             :   else
     295    12476453 :    _next_unique_id = std::max(_next_unique_id, e->unique_id()+1);
     296             : #endif
     297             : 
     298     3682800 :   const dof_id_type id = e->id();
     299             : 
     300    18024691 :   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      169990 :       if (e == _elements[id])
     306           0 :         return e;
     307             : 
     308             :       // Overwriting existing elements is still probably a mistake.
     309       17329 :       libmesh_assert(!_elements[id]);
     310             :     }
     311             :   else
     312             :     {
     313    16709337 :       _elements.resize(id+1, nullptr);
     314             :     }
     315             : 
     316    16879327 :   ++_n_elem;
     317    16879327 :   _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    16879327 :   this->clear_point_locator();
     322    16879327 :   this->clear_stored_ranges();
     323             : 
     324             :   // Make sure any new element is given space for any extra integers
     325             :   // we've requested
     326    16879327 :   e->add_extra_integers(_elem_integer_names.size(),
     327    16879327 :                         _elem_integer_default_values);
     328             : 
     329             :   // And set mapping type and data on any new element
     330     3682800 :   e->set_mapping_type(this->default_mapping_type());
     331     3682800 :   e->set_mapping_data(this->default_mapping_data());
     332             : 
     333    16879327 :   return e;
     334             : }
     335             : 
     336    16063616 : Elem * 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    16063616 :   return add_elem(e.release());
     342             : }
     343             : 
     344             : 
     345             : 
     346      636377 : Elem * ReplicatedMesh::insert_elem (Elem * e)
     347             : {
     348             : #ifdef LIBMESH_ENABLE_UNIQUE_ID
     349      636377 :   if (!e->valid_unique_id())
     350       18176 :     e->set_unique_id(_next_unique_id++);
     351             :   else
     352      618201 :    _next_unique_id = std::max(_next_unique_id, e->unique_id()+1);
     353             : #endif
     354             : 
     355      266100 :   dof_id_type eid = e->id();
     356      133050 :   libmesh_assert_less (eid, _elements.size());
     357      636377 :   Elem * oldelem = _elements[eid];
     358             : 
     359      636377 :   if (oldelem)
     360             :     {
     361      133050 :       libmesh_assert_equal_to (oldelem->id(), eid);
     362      636377 :       this->delete_elem(oldelem);
     363             :     }
     364             : 
     365      636377 :   ++_n_elem;
     366      636377 :   _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      636377 :   this->clear_point_locator();
     371      636377 :   this->clear_stored_ranges();
     372             : 
     373             :   // Make sure any new element is given space for any extra integers
     374             :   // we've requested
     375      636377 :   e->add_extra_integers(_elem_integer_names.size(),
     376      636377 :                         _elem_integer_default_values);
     377             : 
     378             :   // And set mapping type and data on any new element
     379      266100 :   e->set_mapping_type(this->default_mapping_type());
     380      266100 :   e->set_mapping_data(this->default_mapping_data());
     381             : 
     382      636377 :   return e;
     383             : }
     384             : 
     385      636377 : Elem * 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      636377 :   return insert_elem(e.release());
     391             : }
     392             : 
     393             : 
     394             : 
     395     2462374 : void ReplicatedMesh::delete_elem(Elem * e)
     396             : {
     397      251351 :   libmesh_assert(e);
     398             : 
     399             :   // Initialize an iterator to eventually point to the element we want to delete
     400      251351 :   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      251351 :   libmesh_assert_less (e->id(), _elements.size());
     406             : 
     407     2708349 :   if (_elements[e->id()] == e)
     408             :     {
     409             :       // We found it!
     410     2216399 :       pos = _elements.begin();
     411      251351 :       std::advance(pos, e->id());
     412             :     }
     413             : 
     414             :   else
     415             :     {
     416             :       // This search is O(n_elem)
     417           0 :       pos = std::find (_elements.begin(),
     418             :                        _elements.end(),
     419           0 :                        e);
     420             :     }
     421             : 
     422             :   // Huh? Element not in the vector?
     423      251351 :   libmesh_assert (pos != _elements.end());
     424             : 
     425             :   // Remove the element from the BoundaryInfo object
     426     2462374 :   this->get_boundary_info().remove(e);
     427             : 
     428             :   // delete the element
     429     2462374 :   --_n_elem;
     430     2462374 :   delete e;
     431             : 
     432             :   // explicitly zero the pointer
     433     2462374 :   *pos = nullptr;
     434             : 
     435             :   // Some of our caches might still be valid, but we should clear the
     436             :   // ones which definitely are not.
     437     2462374 :   this->clear_point_locator();
     438     2462374 :   this->clear_stored_ranges();
     439     2462374 : }
     440             : 
     441             : 
     442             : 
     443       21420 : void ReplicatedMesh::renumber_elem(const dof_id_type old_id,
     444             :                                    const dof_id_type new_id)
     445             : {
     446             :   // This could be a no-op
     447       21420 :   if (old_id == new_id)
     448           0 :     return;
     449             : 
     450             :   // This doesn't get used in serial yet
     451       21420 :   Elem * el = _elements[old_id];
     452        7140 :   libmesh_assert (el);
     453             : 
     454       28560 :   if (new_id >= _elements.size())
     455         288 :     _elements.resize(new_id+1, nullptr);
     456             : 
     457        7140 :   el->set_id(new_id);
     458        7140 :   libmesh_assert (!_elements[new_id]);
     459       28560 :   _elements[new_id] = el;
     460       21420 :   _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       21420 :   this->clear_stored_ranges();
     467             : }
     468             : 
     469             : 
     470             : 
     471    14401037 : Node * ReplicatedMesh::add_point (const Point & p,
     472             :                                   const dof_id_type id,
     473             :                                   const processor_id_type proc_id)
     474             : {
     475     4499770 :   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    14401037 :   if (id != DofObject::invalid_id)
     481    10438414 :     if (id < _nodes.size())
     482       14905 :       n = _nodes[id];
     483             :     else
     484     9476455 :       _nodes.resize(id+1);
     485             :   else
     486     4909677 :     _nodes.push_back (static_cast<Node *>(nullptr));
     487             : 
     488             :   // if the node already exists, then assign new (x,y,z) values
     489     4512509 :   if (n)
     490           2 :     *n = p;
     491             :   // otherwise build a new node, put it in the right spot, and return
     492             :   // a valid pointer.
     493             :   else
     494             :     {
     495    27854880 :       n = Node::build(p, (id == DofObject::invalid_id) ?
     496    10313388 :                       cast_int<dof_id_type>(_nodes.size()-1) : id).release();
     497    14400966 :       n->processor_id() = proc_id;
     498             : 
     499             :       // A new node invalidates any cached node ranges
     500    14400966 :       this->clear_stored_ranges();
     501             : 
     502    14400966 :       n->add_extra_integers(_node_integer_names.size(),
     503    14400966 :                             _node_integer_default_values);
     504             : 
     505             : #ifdef LIBMESH_ENABLE_UNIQUE_ID
     506    14400966 :       if (!n->valid_unique_id())
     507    14400966 :         n->set_unique_id(_next_unique_id++);
     508             :       else
     509           0 :        _next_unique_id = std::max(_next_unique_id, n->unique_id()+1);
     510             : #endif
     511             : 
     512    14400966 :       ++_n_nodes;
     513    14400966 :       if (id == DofObject::invalid_id)
     514     4909677 :         _nodes.back() = n;
     515             :       else
     516    10438341 :         _nodes[id] = n;
     517             :     }
     518             : 
     519             :   // better not pass back a nullptr.
     520     4499770 :   libmesh_assert (n);
     521             : 
     522    14401037 :   return n;
     523             : }
     524             : 
     525             : 
     526             : 
     527      949502 : Node * ReplicatedMesh::add_node (Node * n)
     528             : {
     529       97099 :   libmesh_assert(n);
     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      949502 :   if (n->valid_id())
     534             :     {
     535       97009 :       const dof_id_type id = n->id();
     536     1045995 :       if (id < _nodes.size())
     537       15080 :         libmesh_assert(!_nodes[id]);
     538             :       else
     539      752462 :         _nodes.resize(id+1); // default nullptr
     540             : 
     541     1045995 :       _nodes[id] = n;
     542             :     }
     543             :   else
     544             :     {
     545         180 :       n->set_id (cast_int<dof_id_type>(_nodes.size()));
     546         516 :       _nodes.push_back(n);
     547             :     }
     548             : 
     549      949502 :   ++_n_nodes;
     550             : 
     551             : #ifdef LIBMESH_ENABLE_UNIQUE_ID
     552      949502 :   if (!n->valid_unique_id())
     553         524 :     n->set_unique_id(_next_unique_id++);
     554             :   else
     555     1450625 :    _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      949502 :   this->clear_stored_ranges();
     560             : 
     561      949502 :   n->add_extra_integers(_node_integer_names.size(),
     562      949502 :                         _node_integer_default_values);
     563             : 
     564      949502 :   return n;
     565             : }
     566             : 
     567      949502 : Node * 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      949502 :   return add_node(n.release());
     573             : }
     574             : 
     575      250912 : void ReplicatedMesh::delete_node(Node * n)
     576             : {
     577       63787 :   libmesh_assert(n);
     578       63787 :   libmesh_assert_less (n->id(), _nodes.size());
     579             : 
     580             :   // Initialize an iterator to eventually point to the element we want
     581             :   // to delete
     582       63787 :   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      314699 :   if (_nodes[n->id()] == n)
     588             :     {
     589      187125 :       pos = _nodes.begin();
     590       63787 :       std::advance(pos, n->id());
     591             :     }
     592             :   else
     593             :     {
     594           0 :       pos = std::find (_nodes.begin(),
     595             :                        _nodes.end(),
     596           0 :                        n);
     597             :     }
     598             : 
     599             :   // Huh? Node not in the vector?
     600       63787 :   libmesh_assert (pos != _nodes.end());
     601             : 
     602             :   // Delete the node from the BoundaryInfo object
     603      250912 :   this->get_boundary_info().remove(n);
     604      127574 :   _constraint_rows.erase(n);
     605             : 
     606             :   // delete the node
     607      250912 :   --_n_nodes;
     608      438037 :   delete n;
     609             : 
     610             :   // explicitly zero the pointer
     611      250912 :   *pos = nullptr;
     612             : 
     613             :   // We've invalidated any ranges that contained this node
     614      250912 :   this->clear_stored_ranges();
     615      250912 : }
     616             : 
     617             : 
     618             : 
     619       70092 : void ReplicatedMesh::renumber_node(const dof_id_type old_id,
     620             :                                    const dof_id_type new_id)
     621             : {
     622             :   // This could be a no-op
     623       70092 :   if (old_id == new_id)
     624           0 :     return;
     625             : 
     626             :   // This doesn't get used in serial yet
     627       70092 :   Node * nd = _nodes[old_id];
     628       23364 :   libmesh_assert (nd);
     629             : 
     630       93456 :   if (new_id >= _nodes.size())
     631         288 :     _nodes.resize(new_id+1, nullptr);
     632             : 
     633       23364 :   nd->set_id(new_id);
     634       23364 :   libmesh_assert (!_nodes[new_id]);
     635       93456 :   _nodes[new_id] = nd;
     636       70092 :   _nodes[old_id] = nullptr;
     637             : }
     638             : 
     639             : 
     640             : 
     641      161905 : void ReplicatedMesh::clear ()
     642             : {
     643             :   // Call parent clear function
     644      161905 :   MeshBase::clear();
     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.
     650      161905 :   this->ReplicatedMesh::clear_elems();
     651             : 
     652    13688735 :   for (auto & node : _nodes)
     653    22634534 :     delete node;
     654             : 
     655      161905 :   _n_nodes = 0;
     656       46005 :   _nodes.clear();
     657      161905 : }
     658             : 
     659             : 
     660             : 
     661      162624 : void ReplicatedMesh::clear_elems ()
     662             : {
     663    15439720 :   for (auto & elem : _elements)
     664    15277096 :     delete elem;
     665             : 
     666      162624 :   _n_elem = 0;
     667       46213 :   _elements.clear();
     668      162624 : }
     669             : 
     670             : 
     671             : 
     672      279352 : void ReplicatedMesh::update_parallel_id_counts()
     673             : {
     674             : #ifdef LIBMESH_ENABLE_UNIQUE_ID
     675      279352 :   _next_unique_id = this->parallel_max_unique_id();
     676             : #endif
     677             : 
     678             :   // Implicitly get max_elem_id/max_node_id by trimming the vectors
     679      558704 :   auto trim_vec = [](auto & dof_vec) {
     680      719710 :     auto last_non_null = std::find_if(dof_vec.rbegin(), dof_vec.rend(), [](DofObject * d) { return (d != nullptr); });
     681      558704 :     dof_vec.resize(last_non_null.base()-dof_vec.begin());
     682      558704 :   };
     683             : 
     684      279352 :   trim_vec(this->_nodes);
     685      279352 :   trim_vec(this->_elements);
     686             : 
     687      279352 :   _n_constraint_rows = this->count_constraint_rows();
     688             : 
     689      279352 :   this->_preparation.has_synched_id_counts = true;
     690      279352 : }
     691             : 
     692             : 
     693             : 
     694             : #ifdef LIBMESH_ENABLE_UNIQUE_ID
     695      314395 : unique_id_type ReplicatedMesh::parallel_max_unique_id() const
     696             : {
     697             :   // This function must be run on all processors at once
     698       86171 :   parallel_object_only();
     699             : 
     700      314395 :   unique_id_type max_local = _next_unique_id;
     701      314395 :   this->comm().max(max_local);
     702      314395 :   return max_local;
     703             : }
     704             : 
     705             : 
     706             : 
     707       38740 : void ReplicatedMesh::set_next_unique_id(unique_id_type id)
     708             : {
     709       38740 :   _next_unique_id = id;
     710       38740 : }
     711             : #endif
     712             : 
     713             : 
     714             : 
     715      213067 : void ReplicatedMesh::renumber_nodes_and_elements ()
     716             : {
     717      129996 :   LOG_SCOPE("renumber_nodes_and_elem()", "Mesh");
     718             : 
     719             :   // node and element id counters
     720       43332 :   dof_id_type next_free_elem = 0;
     721       43332 :   dof_id_type next_free_node = 0;
     722             : 
     723             :   // Will hold the set of nodes that are currently connected to elements
     724       86664 :   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       43332 :     std::vector<Elem *>::iterator in        = _elements.begin();
     732       43332 :     std::vector<Elem *>::iterator out_iter  = _elements.begin();
     733       43332 :     const std::vector<Elem *>::iterator end = _elements.end();
     734             : 
     735    40115309 :     for (; in != end; ++in)
     736    39902242 :       if (*in != nullptr)
     737             :         {
     738     3149574 :           Elem * el = *in;
     739             : 
     740    38445074 :           *out_iter = *in;
     741     3149574 :           ++out_iter;
     742             : 
     743             :           // Increment the element counter
     744    38445074 :           el->set_id (next_free_elem++);
     745             : 
     746    38445074 :           if (_skip_renumber_nodes_and_elements)
     747             :             {
     748             :               // Add this elements nodes to the connected list
     749     2145255 :               for (auto & n : el->node_ref_range())
     750     1934776 :                 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   237436217 :               for (auto & n : el->node_ref_range())
     759   199201622 :                 if (n.id() == next_free_node)    // don't need to process
     760    37890247 :                   next_free_node++;                      // [(src == dst) below]
     761             : 
     762   161311375 :                 else if (n.id() > next_free_node) // need to process
     763             :                   {
     764             :                     // The source and destination indices
     765             :                     // for this node
     766     1227362 :                     const dof_id_type src_idx = n.id();
     767     9120363 :                     const dof_id_type dst_idx = next_free_node++;
     768             : 
     769             :                     // ensure we want to swap a valid nodes
     770     1227362 :                     libmesh_assert(_nodes[src_idx]);
     771             : 
     772             :                     // Swap the source and destination nodes
     773     3036020 :                     std::swap(_nodes[src_idx],
     774     3036020 :                               _nodes[dst_idx] );
     775             : 
     776             :                     // Set proper indices where that makes sense
     777     9120363 :                     if (_nodes[src_idx] != nullptr)
     778     1212604 :                       _nodes[src_idx]->set_id (src_idx);
     779     1227362 :                     _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      213067 :     _elements.erase (out_iter, end);
     788             :   }
     789             : 
     790             : 
     791      213067 :   if (_skip_renumber_nodes_and_elements)
     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          60 :       std::vector<Node *>::iterator in        = _nodes.begin();
     799          60 :       std::vector<Node *>::iterator out_iter  = _nodes.begin();
     800          60 :       const std::vector<Node *>::iterator end = _nodes.end();
     801             : 
     802      925936 :       for (; in != end; ++in)
     803      925541 :         if (*in != nullptr)
     804             :           {
     805             :             // This is a reference so that if we change the pointer it will change in the vector
     806      209684 :             Node * & nd = *in;
     807             : 
     808             :             // If this node is still connected to an elem, put it in the list
     809      419368 :             if (connected_nodes.count(nd))
     810             :               {
     811      673877 :                 *out_iter = nd;
     812      137780 :                 ++out_iter;
     813             : 
     814             :                 // Increment the node counter
     815      673877 :                 nd->set_id (next_free_node++);
     816             :               }
     817             :             else // This node is orphaned, delete it!
     818             :               {
     819      251664 :                 this->get_boundary_info().remove (nd);
     820      143808 :                 _constraint_rows.erase(nd);
     821             : 
     822             :                 // delete the node
     823      251664 :                 --_n_nodes;
     824      431424 :                 delete nd;
     825      251664 :                 nd = nullptr;
     826             :               }
     827             :           }
     828             : 
     829             :       // Erase any additional storage.  Whatever was
     830         395 :       _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      169400 :         std::vector<Node *>::iterator nd        = _nodes.begin();
     841       43272 :         const std::vector<Node *>::iterator end = _nodes.end();
     842             : 
     843       43272 :         std::advance (nd, next_free_node);
     844             : 
     845     1745197 :         for (auto & node : as_range(nd, end))
     846             :           {
     847             :             // Mesh modification code might have already deleted some
     848             :             // nodes
     849     1532525 :             if (node == nullptr)
     850      104431 :               continue;
     851             : 
     852             :             // remove any boundary information associated with
     853             :             // this node
     854     1392592 :             this->get_boundary_info().remove (node);
     855      227160 :             _constraint_rows.erase(node);
     856             : 
     857             :             // delete the node
     858     1392592 :             --_n_nodes;
     859     2671604 :             delete node;
     860     1392592 :             node = nullptr;
     861             :           }
     862             : 
     863      212672 :         _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      213067 :   this->clear_stored_ranges();
     870             : 
     871      213067 :   this->_preparation.has_removed_orphaned_nodes = true;
     872             : 
     873       43332 :   libmesh_assert_equal_to (next_free_elem, _elements.size());
     874       43332 :   libmesh_assert_equal_to (next_free_node, _nodes.size());
     875             : 
     876      213067 :   this->update_parallel_id_counts();
     877      213067 : }
     878             : 
     879             : 
     880             : 
     881        2220 : void ReplicatedMesh::fix_broken_node_and_element_numbering ()
     882             : {
     883             :   // Nodes first
     884     3180890 :   for (auto n : index_range(_nodes))
     885     3178670 :     if (this->_nodes[n] != nullptr)
     886     3178670 :       this->_nodes[n]->set_id() = cast_int<dof_id_type>(n);
     887             : 
     888             :   // Elements next
     889     3664918 :   for (auto e : index_range(_elements))
     890     3662698 :     if (this->_elements[e] != nullptr)
     891     3662698 :       this->_elements[e]->set_id() = cast_int<dof_id_type>(e);
     892        2220 : }
     893             : 
     894             : 
     895      123991 : dof_id_type ReplicatedMesh::n_active_elem () const
     896             : {
     897      226103 :   return static_cast<dof_id_type>(std::distance (this->active_elements_begin(),
     898      350094 :                                                  this->active_elements_end()));
     899             : }
     900             : 
     901             : std::vector<dof_id_type>
     902         142 : ReplicatedMesh::get_disconnected_subdomains(std::vector<subdomain_id_type> * subdomain_ids) const
     903             : {
     904             :   // find number of disconnected subdomains
     905           4 :   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           8 :   std::vector<subdomain_id_type> subdomains;
     911         142 :   if (!subdomain_ids)
     912           0 :     subdomain_ids = &subdomains;
     913           4 :   subdomain_ids->clear();
     914         142 :   subdomain_ids->resize(max_elem_id() + 1, Elem::invalid_subdomain_id);
     915             : 
     916             :   // counter of disconnected subdomains
     917           4 :   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           8 :   std::vector<const Elem *> list;
     922         142 :   list.reserve(n_elem());
     923             : 
     924             :   // counter of visited elements
     925           4 :   dof_id_type visited = 0;
     926         142 :   dof_id_type n_active = n_active_elem();
     927           4 :   do
     928             :   {
     929        1672 :     for (const auto & elem : active_element_ptr_range())
     930         876 :       if ((*subdomain_ids)[elem->id()] == Elem::invalid_subdomain_id)
     931             :       {
     932         284 :         list.push_back(elem);
     933         292 :         (*subdomain_ids)[elem->id()] = subdomain_counter;
     934         284 :         break;
     935         268 :       }
     936             :     // we should be able to find a seed here
     937           8 :     libmesh_assert(list.size() > 0);
     938             : 
     939         284 :     dof_id_type min_id = std::numeric_limits<dof_id_type>::max();
     940        1988 :     while (list.size() > 0)
     941             :     {
     942             :       // pop up an element
     943        1704 :       const Elem * elem = list.back(); list.pop_back(); ++visited;
     944             : 
     945        3084 :       min_id = std::min(elem->id(), min_id);
     946             : 
     947        8520 :       for (auto s : elem->side_index_range())
     948             :       {
     949        6816 :         const Elem * neighbor = elem->neighbor_ptr(s);
     950        6816 :         if (neighbor != nullptr && (*subdomain_ids)[neighbor->id()] == Elem::invalid_subdomain_id)
     951             :         {
     952             :           // neighbor must be active
     953          40 :           libmesh_assert(neighbor->active());
     954        1420 :           list.push_back(neighbor);
     955        1460 :           (*subdomain_ids)[neighbor->id()] = subdomain_counter;
     956             :         }
     957             :       }
     958             :     }
     959             : 
     960         284 :     representative_elem_ids.push_back(min_id);
     961         284 :     subdomain_counter++;
     962             :   }
     963         284 :   while (visited != n_active);
     964             : 
     965         146 :   return representative_elem_ids;
     966             : }
     967             : 
     968             : std::unordered_map<dof_id_type, std::vector<std::vector<Point>>>
     969         142 : ReplicatedMesh::get_boundary_points() const
     970             : {
     971         142 :   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           8 :   std::vector<subdomain_id_type> subdomains;
     977         146 :   std::vector<dof_id_type> elem_ids = get_disconnected_subdomains(&subdomains);
     978             : 
     979           4 :   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        1584 :     bool operator()(const std::pair<const Elem *, unsigned int> & lhs,
     986             :                     const std::pair<const Elem *, unsigned int> & rhs) const
     987             :       {
     988       42340 :         if (lhs.first->id() < rhs.first->id())
     989         328 :           return true;
     990       29298 :         else if (lhs.first->id() == rhs.first->id())
     991             :         {
     992       14668 :           if (lhs.second < rhs.second)
     993         112 :             return true;
     994             :         }
     995        1144 :         return false;
     996             :       }
     997             :   };
     998           8 :   std::set<std::pair<const Elem *, unsigned int>, boundary_side_compare> boundary_elements;
     999        3592 :   for (const auto & elem : active_element_ptr_range())
    1000        8568 :     for (auto s : elem->side_index_range())
    1001        7008 :       if (elem->neighbor_ptr(s) == nullptr)
    1002        3542 :         boundary_elements.insert(std::pair<const Elem *, unsigned int>(elem, s));
    1003             : 
    1004         568 :   while (!boundary_elements.empty())
    1005             :   {
    1006             :     // get the first entry as the seed
    1007         426 :     const Elem * eseed = boundary_elements.begin()->first;
    1008         426 :     unsigned int sseed = boundary_elements.begin()->second;
    1009             : 
    1010             :     // get the subdomain ID that these boundary sides attached to
    1011         438 :     subdomain_id_type subdomain_id = subdomains[eseed->id()];
    1012             : 
    1013             :     // start visiting the mesh to find all boundary nodes with the seed
    1014          24 :     std::vector<Point> bpoints;
    1015         426 :     const Elem * elem = eseed;
    1016          12 :     unsigned int s = sseed;
    1017         438 :     std::vector<unsigned int> local_side_nodes = elem->nodes_on_side(s);
    1018             :     while (true)
    1019             :     {
    1020          96 :       std::pair<const Elem *, unsigned int> side(elem, s);
    1021          96 :       libmesh_assert(boundary_elements.count(side));
    1022          96 :       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       11928 :       for (auto i : index_range(local_side_nodes))
    1026        8520 :         if (i != 1)
    1027        5400 :           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        3408 :       const Node * node = elem->node_ptr(local_side_nodes[1]);
    1031          96 :       std::set<const Elem *> neighbors;
    1032        3408 :       elem->find_point_neighbors(*node, neighbors);
    1033             : 
    1034             :       // if only one neighbor is found (itself), this node is a cornor node on boundary
    1035        3408 :       if (neighbors.size() != 1)
    1036          64 :         neighbors.erase(elem);
    1037             : 
    1038             :       // find the connecting side
    1039          96 :       bool found = false;
    1040        3661 :       for (const auto & neighbor : neighbors)
    1041             :       {
    1042        9886 :         for (auto ss : neighbor->side_index_range())
    1043        9792 :           if (neighbor->neighbor_ptr(ss) == nullptr && !(elem == neighbor && s == ss))
    1044             :           {
    1045        4908 :             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        5042 :             if (neighbor->node_ptr(local_side_nodes[0]) == node)
    1048             :             {
    1049        3408 :               elem = neighbor;
    1050          96 :               s = ss;
    1051          96 :               found = true;
    1052          96 :               break;
    1053             :             }
    1054        1538 :             else if (neighbor->node_ptr(local_side_nodes[1]) == node)
    1055             :             {
    1056           0 :               elem = neighbor;
    1057           0 :               s = ss;
    1058           0 :               found = true;
    1059             :               // flip nodes in local_side_nodes because the side is in an opposite direction
    1060           0 :               auto temp(local_side_nodes);
    1061           0 :               local_side_nodes[0] = temp[1];
    1062           0 :               local_side_nodes[1] = temp[0];
    1063           0 :               for (unsigned int i = 2; i < temp.size(); ++i)
    1064           0 :                 local_side_nodes[temp.size() + 1 - i] = temp[i];
    1065           0 :               break;
    1066             :             }
    1067             :           }
    1068         102 :         if (found)
    1069          96 :           break;
    1070             :       }
    1071             : 
    1072        3408 :       libmesh_error_msg_if(!found, "ERROR: mesh topology error on visiting boundary sides");
    1073             : 
    1074             :       // exit if we reach the starting point
    1075        3408 :       if (elem == eseed && s == sseed)
    1076          12 :         break;
    1077          84 :     }
    1078         426 :     boundary_points[elem_ids[subdomain_id]].push_back(bpoints);
    1079             :   }
    1080             : 
    1081         146 :   return boundary_points;
    1082             : }
    1083             : 
    1084             : } // namespace libMesh

Generated by: LCOV version 1.14