LCOV - code coverage report
Current view: top level - src/mesh - mesh_refinement.C (source / functions) Hit Total Coverage
Test: libMesh/libmesh: #4417 (f0ff5e) with base 9bbd75 Lines: 635 712 89.2 %
Date: 2026-11-16 16:30:47 Functions: 32 36 88.9 %
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             : // Local includes
      20             : #include "libmesh/libmesh_config.h"
      21             : 
      22             : // only compile these functions if the user requests AMR support
      23             : #ifdef LIBMESH_ENABLE_AMR
      24             : 
      25             : #include "libmesh/boundary_info.h"
      26             : #include "libmesh/elem_range.h"
      27             : #include "libmesh/error_vector.h"
      28             : #include "libmesh/libmesh_logging.h"
      29             : #include "libmesh/mesh_base.h"
      30             : #include "libmesh/mesh_communication.h"
      31             : #include "libmesh/mesh_refinement.h"
      32             : #include "libmesh/parallel.h"
      33             : #include "libmesh/parallel_ghost_sync.h"
      34             : #include "libmesh/partitioner.h"
      35             : #include "libmesh/remote_elem.h"
      36             : #include "libmesh/sync_refinement_flags.h"
      37             : #include "libmesh/int_range.h"
      38             : 
      39             : #ifdef DEBUG
      40             : // Some extra validation for DistributedMesh
      41             : #include "libmesh/mesh_tools.h"
      42             : #endif // DEBUG
      43             : 
      44             : #ifdef LIBMESH_ENABLE_PERIODIC
      45             : #include "libmesh/periodic_boundaries.h"
      46             : #endif
      47             : 
      48             : // C++ includes
      49             : #include <atomic>
      50             : #include <cstdlib> // *must* precede <cmath> for proper std:abs() on PGI, Sun Studio CC
      51             : #include <cmath> // for isnan(), when it's defined
      52             : #include <limits>
      53             : 
      54             : 
      55             : // Anonymous namespace for helper functions
      56             : // namespace {
      57             : //
      58             : // using namespace libMesh;
      59             : //
      60             : // struct SyncCoarsenInactive
      61             : // {
      62             : //   bool operator() (const Elem * elem) const {
      63             : //     // If we're not an ancestor, there's no chance our coarsening
      64             : //     // settings need to be changed.
      65             : //     if (!elem->ancestor())
      66             : //       return false;
      67             : //
      68             : //     // If we don't have any remote children, we already know enough to
      69             : //     // determine the correct refinement flag ourselves.
      70             : //     //
      71             : //     // If we know we have a child that isn't being coarsened, that
      72             : //     // also forces a specific flag.
      73             : //     //
      74             : //     // Either way there's nothing we need to communicate.
      75             : //     bool found_remote_child = false;
      76             : //     for (auto & child : elem->child_ref_range())
      77             : //       {
      78             : //         if (child.refinement_flag() != Elem::COARSEN)
      79             : //           return false;
      80             : //         if (&child == remote_elem)
      81             : //           found_remote_child = true;
      82             : //       }
      83             : //     return found_remote_child;
      84             : //   }
      85             : // };
      86             : // }
      87             : 
      88             : 
      89             : 
      90             : namespace libMesh
      91             : {
      92             : 
      93             : //-----------------------------------------------------------------
      94             : // Mesh refinement methods
      95      441727 : MeshRefinement::MeshRefinement (MeshBase & m) :
      96             :   ParallelObject(m),
      97      416311 :   _mesh(m),
      98      416311 :   _use_member_parameters(false),
      99      416311 :   _coarsen_by_parents(false),
     100      416311 :   _refine_fraction(0.3),
     101      416311 :   _coarsen_fraction(0.0),
     102      416311 :   _max_h_level(libMesh::invalid_uint),
     103      416311 :   _coarsen_threshold(10),
     104      416311 :   _nelem_target(0),
     105      416311 :   _absolute_global_tolerance(0.0),
     106      416311 :   _face_level_mismatch_limit(1),
     107      416311 :   _edge_level_mismatch_limit(0),
     108      416311 :   _node_level_mismatch_limit(0),
     109      416311 :   _overrefined_boundary_limit(0),
     110      416311 :   _underrefined_boundary_limit(0),
     111      416311 :   _allow_unrefined_patches(false),
     112      416311 :   _enforce_mismatch_limit_prior_to_refinement(false)
     113             : #ifdef LIBMESH_ENABLE_PERIODIC
     114      454435 :   , _periodic_boundaries(nullptr)
     115             : #endif
     116             : {
     117      441727 : }
     118             : 
     119             : 
     120             : 
     121             : #ifdef LIBMESH_ENABLE_PERIODIC
     122          10 : void MeshRefinement::set_periodic_boundaries_ptr(PeriodicBoundaries * pb_ptr)
     123             : {
     124          10 :   _periodic_boundaries = pb_ptr;
     125          10 : }
     126             : #endif
     127             : 
     128             : 
     129             : 
     130      417778 : MeshRefinement::~MeshRefinement () = default;
     131             : 
     132             : 
     133             : 
     134       64256 : void MeshRefinement::clear ()
     135             : {
     136        2134 :   _new_nodes_map.clear();
     137       64256 : }
     138             : 
     139             : 
     140             : 
     141    47511457 : Node * MeshRefinement::add_node(Elem & parent,
     142             :                                 unsigned int child,
     143             :                                 unsigned int node,
     144             :                                 processor_id_type proc_id)
     145             : {
     146     8034204 :   LOG_ONE_ENTITY_SCOPE("add_node()", "MeshRefinement");
     147             : 
     148    47511457 :   unsigned int parent_n = parent.as_parent_node(child, node);
     149             : 
     150    47511457 :   if (parent_n != libMesh::invalid_uint)
     151    14923042 :     return parent.node_ptr(parent_n);
     152             : 
     153             :   const std::vector<std::pair<dof_id_type, dof_id_type>>
     154    35266483 :     bracketing_nodes = parent.bracketing_nodes(child, node);
     155             : 
     156             :   // If we're not a parent node, we *must* be bracketed by at least
     157             :   // one pair of parent nodes
     158     1907168 :   libmesh_assert(bracketing_nodes.size());
     159             : 
     160             :   // Return the node if it already exists.
     161             :   //
     162             :   // We'll leave the processor_id untouched in this case - if we're
     163             :   // repartitioning later or if this is a new unpartitioned node,
     164             :   // we'll update it then, and if not then we don't want to update it.
     165    33359315 :   if (const auto new_node_id = _new_nodes_map.find(bracketing_nodes);
     166             :       new_node_id != DofObject::invalid_id)
     167    23780082 :     return _mesh.node_ptr(new_node_id);
     168             : 
     169             :   // Otherwise we need to add a new node.
     170             :   //
     171             :   // Figure out where to add the point:
     172             : 
     173      527999 :   Point p; // defaults to 0,0,0
     174             : 
     175   118548499 :   for (auto n : parent.node_index_range())
     176             :     {
     177             :       // The value from the embedding matrix
     178   108969266 :       const Real em_val = parent.embedding_matrix(child,node,n);
     179             : 
     180   108969266 :       if (em_val != 0.)
     181             :         {
     182    53984553 :           p.add_scaled (parent.point(n), em_val);
     183             : 
     184             :           // If we'd already found the node we shouldn't be here
     185     2759270 :           libmesh_assert_not_equal_to (em_val, 1);
     186             :         }
     187             :     }
     188             : 
     189             :   // Although we're leaving new nodes unpartitioned at first, with a
     190             :   // DistributedMesh we would need a default id based on the numbering
     191             :   // scheme for the requested processor_id.
     192     9579233 :   Node * new_node = _mesh.add_point (p, DofObject::invalid_id, proc_id);
     193             : 
     194      527999 :   libmesh_assert(new_node);
     195             : 
     196             :   // But then we'll make sure this node is marked as unpartitioned.
     197     9579233 :   new_node->processor_id() = DofObject::invalid_processor_id;
     198             : 
     199             :   // Add the node to the map.
     200     9579233 :   _new_nodes_map.add_node(*new_node, bracketing_nodes);
     201             : 
     202             :   // Return the address of the new node
     203      527999 :   return new_node;
     204             : }
     205             : 
     206             : 
     207             : 
     208           0 : Elem * MeshRefinement::add_elem (Elem * elem)
     209             : {
     210           0 :   libmesh_assert(elem);
     211           0 :   return _mesh.add_elem (elem);
     212             : }
     213             : 
     214     6527865 : Elem * MeshRefinement::add_elem (std::unique_ptr<Elem> elem)
     215             : {
     216      390142 :   libmesh_assert(elem);
     217     7308149 :   return _mesh.add_elem(std::move(elem));
     218             : }
     219             : 
     220             : 
     221        1590 : void MeshRefinement::create_parent_error_vector(const ErrorVector & error_per_cell,
     222             :                                                 ErrorVector & error_per_parent,
     223             :                                                 Real & parent_error_min,
     224             :                                                 Real & parent_error_max)
     225             : {
     226             :   // This function must be run on all processors at once
     227          82 :   parallel_object_only();
     228             : 
     229             :   // Make sure the input error vector is valid
     230             : #ifdef DEBUG
     231       11590 :   for (const auto & val : error_per_cell)
     232             :     {
     233       11508 :       libmesh_assert_greater_equal (val, 0);
     234             :       // isnan() isn't standard C++ yet
     235             : #ifdef isnan
     236             :       libmesh_assert(!isnan(val));
     237             : #endif
     238             :     }
     239             : 
     240             :   // Use a reference to std::vector to avoid confusing
     241             :   // this->comm().verify
     242          82 :   std::vector<ErrorVectorReal> & epc = error_per_parent;
     243          82 :   libmesh_assert(this->comm().verify(epc));
     244             : #endif // #ifdef DEBUG
     245             : 
     246             :   // error values on uncoarsenable elements will be left at -1
     247        1590 :   error_per_parent.clear();
     248        1672 :   error_per_parent.resize(error_per_cell.size(), 0.0);
     249             : 
     250             :   {
     251             :     // Find which elements are uncoarsenable
     252       78930 :     for (auto & elem : _mesh.active_local_element_ptr_range())
     253             :       {
     254       42409 :         Elem * parent = elem->parent();
     255             : 
     256             :         // Active elements are uncoarsenable
     257       46902 :         error_per_parent[elem->id()] = -1.0;
     258             : 
     259             :         // Grandparents and up are uncoarsenable
     260      121584 :         while (parent)
     261             :           {
     262       22384 :             parent = parent->parent();
     263      107426 :             if (parent)
     264             :               {
     265       14446 :                 const dof_id_type parentid  = parent->id();
     266        7223 :                 libmesh_assert_less (parentid, error_per_parent.size());
     267       78460 :                 error_per_parent[parentid] = -1.0;
     268             :               }
     269             :           }
     270        1426 :       }
     271             : 
     272             :     // Sync between processors.
     273             :     // Use a reference to std::vector to avoid confusing
     274             :     // this->comm().min
     275          82 :     std::vector<ErrorVectorReal> & epp = error_per_parent;
     276        1590 :     this->comm().min(epp);
     277             :   }
     278             : 
     279             :   // The parent's error is defined as the square root of the
     280             :   // sum of the children's errors squared, so errors that are
     281             :   // Hilbert norms remain Hilbert norms.
     282             :   //
     283             :   // Because the children may be on different processors, we
     284             :   // calculate local contributions to the parents' errors squared
     285             :   // first, then sum across processors and take the square roots
     286             :   // second.
     287             :   Threads::parallel_for_each
     288        1590 :     (_mesh.active_local_element_stored_range(),
     289       63579 :      [&error_per_cell, &error_per_parent](const Elem * elem)
     290             :      {
     291        8986 :        const Elem * parent = elem->parent();
     292             : 
     293             :        // Calculate each contribution to parent cells
     294       42409 :        if (parent)
     295             :          {
     296        7938 :            const dof_id_type parentid  = parent->id();
     297        3969 :            libmesh_assert_less (parentid, error_per_parent.size());
     298             : 
     299             :            // If the parent has grandchildren we won't be able to
     300             :            // coarsen it, so forget it.  Otherwise, add this child's
     301             :            // contribution to the sum of the squared child errors
     302       40158 :            if (error_per_parent[parentid] != -1.0)
     303       38608 :              error_per_parent[parentid] += (error_per_cell[elem->id()] *
     304        3496 :                                             error_per_cell[elem->id()]);
     305             :          }
     306       42409 :      });
     307             : 
     308             :   // Sum the vector across all processors
     309        1590 :   this->comm().sum(static_cast<std::vector<ErrorVectorReal> &>(error_per_parent));
     310             : 
     311             :   // Calculate the min and max as we loop
     312        1590 :   parent_error_min = std::numeric_limits<double>::max();
     313        1590 :   parent_error_max = 0.;
     314             : 
     315      302728 :   for (auto i : index_range(error_per_parent))
     316             :     {
     317             :       // If this element isn't a coarsenable parent with error, we
     318             :       // have nothing to do.  Just flag it as -1 and move on
     319             :       // Note that this->comm().sum might have left uncoarsenable
     320             :       // elements with error_per_parent=-n_proc, so reset it to
     321             :       // error_per_parent=-1
     322      312646 :       if (error_per_parent[i] < 0.)
     323             :         {
     324      257098 :           error_per_parent[i] = -1.;
     325      257098 :           continue;
     326             :         }
     327             : 
     328             :       // The error estimator might have already given us an
     329             :       // estimate on the coarsenable parent elements; if so then
     330             :       // we want to retain that estimate
     331       45804 :       if (error_per_cell[i])
     332             :         {
     333           0 :           error_per_parent[i] = error_per_cell[i];
     334           0 :           continue;
     335             :         }
     336             :       // if not, then e_parent = sqrt(sum(e_child^2))
     337             :       else
     338       44040 :         error_per_parent[i] = std::sqrt(error_per_parent[i]);
     339             : 
     340       44040 :       parent_error_min = std::min (parent_error_min,
     341        1764 :                                    error_per_parent[i]);
     342       50759 :       parent_error_max = std::max (parent_error_max,
     343        1764 :                                    error_per_parent[i]);
     344             :     }
     345        1590 : }
     346             : 
     347             : 
     348             : 
     349       64256 : void MeshRefinement::update_nodes_map ()
     350             : {
     351       64256 :   this->_new_nodes_map.init(_mesh);
     352       64256 : }
     353             : 
     354             : 
     355             : 
     356        1460 : bool MeshRefinement::test_level_one (bool libmesh_dbg_var(libmesh_assert_pass)) const
     357             : {
     358             :   // This function must be run on all processors at once
     359        1460 :   parallel_object_only();
     360             : 
     361             :   // We may need a PointLocator for topological_neighbor() tests
     362             :   // later, which we need to make sure gets constructed on all
     363             :   // ranks at once, before we split into threads.
     364        1460 :   bool has_periodic_boundaries = false;
     365             : #ifdef LIBMESH_ENABLE_PERIODIC
     366        1460 :   has_periodic_boundaries =
     367        1460 :     _periodic_boundaries && !_periodic_boundaries->empty();
     368        1460 :   libmesh_assert(this->comm().verify(has_periodic_boundaries));
     369             : 
     370             :   // Make sure we have a master point locator if needed
     371        1460 :   if (has_periodic_boundaries)
     372         200 :     _mesh.sub_point_locator();
     373             : #endif
     374             : 
     375        1460 :   std::atomic<bool> failure = false;
     376             : 
     377             : #ifndef NDEBUG
     378        1460 :   std::atomic<const Elem *> failed_elem = nullptr;
     379        1460 :   std::atomic<const Elem *> failed_neighbor = nullptr;
     380             : #endif // !NDEBUG
     381             : 
     382             :   Threads::parallel_for
     383        1460 :     (_mesh.active_local_element_stored_range(),
     384           0 :      [this, &failure,
     385             : #ifndef NDEBUG
     386             :      &failed_elem, &failed_neighbor,
     387             : #endif
     388             :      has_periodic_boundaries]
     389     1080912 :      (const ElemRange & range)
     390             :      {
     391        1466 :        std::unique_ptr<PointLocatorBase> point_locator;
     392             : 
     393        1466 :        if (has_periodic_boundaries)
     394         202 :          point_locator = _mesh.sub_point_locator();
     395             : 
     396      281496 :        for (Elem * elem : range)
     397     1357808 :          for (auto n : elem->side_index_range())
     398             :            {
     399             :              Elem * neighbor =
     400     1077778 :                topological_neighbor(elem, point_locator.get(), n);
     401             : 
     402     2049463 :              if (!neighbor || !neighbor->active() ||
     403      971685 :                  neighbor == remote_elem)
     404      106093 :                continue;
     405             : 
     406      971685 :              if ((neighbor->level() + 1 < elem->level()) ||
     407     1943370 :                  (neighbor->p_level() + 1 < elem->p_level()) ||
     408      971685 :                  (neighbor->p_level() > elem->p_level() + 1))
     409             :                {
     410           0 :                  failure = true;
     411             : #ifndef NDEBUG
     412           0 :                  failed_elem = elem;
     413           0 :                  failed_neighbor = neighbor;
     414             : #endif // !NDEBUG
     415           0 :                  break;
     416             :                }
     417             :            }
     418        1466 :      });
     419             : 
     420             :   // If any processor failed, we failed globally
     421        1460 :   bool some_failure = failure;
     422        1460 :   this->comm().max(some_failure);
     423             : 
     424        1460 :   if (some_failure)
     425             :     {
     426             :       // We didn't pass the level one test, so libmesh_assert that
     427             :       // we're allowed not to
     428             : #ifndef NDEBUG
     429           0 :       if (libmesh_assert_pass && failed_elem)
     430             :         {
     431           0 :           libMesh::out << "MeshRefinement Level one failure, element: "
     432           0 :                        << *failed_elem
     433           0 :                        << std::endl;
     434           0 :           libMesh::out << "MeshRefinement Level one failure, neighbor: "
     435           0 :                        << *failed_neighbor
     436           0 :                        << std::endl;
     437             :         }
     438           0 :       if (libmesh_assert_pass)
     439           0 :         this->comm().barrier(); // Wait for output
     440             : #endif // !NDEBUG
     441           0 :       libmesh_assert(!libmesh_assert_pass);
     442           0 :       return false;
     443             :     }
     444        1460 :   return true;
     445             : }
     446             : 
     447             : 
     448             : 
     449         682 : bool MeshRefinement::test_unflagged (bool libmesh_dbg_var(libmesh_assert_pass)) const
     450             : {
     451             :   // This function must be run on all processors at once
     452         682 :   parallel_object_only();
     453             : 
     454         682 :   std::atomic<bool> found_flag = false;
     455             : 
     456             : #ifndef NDEBUG
     457         682 :   std::atomic<const Elem *> failed_elem = nullptr;
     458             : #endif
     459             : 
     460             :   // Search for local flags
     461             :   Threads::parallel_for
     462         682 :     (_mesh.active_local_element_stored_range(),
     463           0 :      [&found_flag
     464             : #ifndef NDEBUG
     465             :      , &failed_elem
     466             : #endif
     467         682 :      ] (const ElemRange & range)
     468             :      {
     469      133578 :        for (Elem * elem : range)
     470      132896 :          if (elem->refinement_flag() == Elem::REFINE ||
     471      132896 :              elem->refinement_flag() == Elem::COARSEN ||
     472      398688 :              elem->p_refinement_flag() == Elem::REFINE ||
     473      132896 :              elem->p_refinement_flag() == Elem::COARSEN)
     474             :            {
     475           0 :              found_flag = true;
     476             : #ifndef NDEBUG
     477           0 :              failed_elem = elem;
     478             : #endif
     479           0 :              break;
     480             :            }
     481         682 :      });
     482             : 
     483             :   // If we found a flag on any processor, it counts
     484         682 :   bool found_some_flag = found_flag;
     485         682 :   this->comm().max(found_some_flag);
     486             : 
     487         682 :   if (found_some_flag)
     488             :     {
     489             : #ifndef NDEBUG
     490           0 :       if (libmesh_assert_pass && failed_elem)
     491             :         {
     492             :           libMesh::out <<
     493           0 :             "MeshRefinement test_unflagged failure, element: " <<
     494           0 :             *failed_elem << std::endl;
     495             :         }
     496           0 :       if (libmesh_assert_pass)
     497           0 :         this->comm().barrier(); // Wait for output
     498             : #endif
     499             :       // We didn't pass the "elements are unflagged" test,
     500             :       // so libmesh_assert that we're allowed not to
     501           0 :       libmesh_assert(!libmesh_assert_pass);
     502           0 :       return false;
     503             :     }
     504         682 :   return true;
     505             : }
     506             : 
     507             : 
     508             : 
     509       20400 : bool MeshRefinement::refine_and_coarsen_elements ()
     510             : {
     511             :   // This function must be run on all processors at once
     512         710 :   parallel_object_only();
     513             : 
     514             :   // We can't yet turn a non-level-one mesh into a level-one mesh
     515         710 :   if (_face_level_mismatch_limit)
     516         710 :     libmesh_assert(test_level_one(true));
     517             : 
     518             :   // Possibly clean up the refinement flags from
     519             :   // a previous step.  While we're at it, see if this method should be
     520             :   // a no-op.
     521       20400 :   std::atomic<bool> elements_flagged{false};
     522             : 
     523             :   Threads::parallel_for_each
     524       20400 :     (_mesh.element_stored_range(),
     525    14360063 :      [&elements_flagged](Elem * elem)
     526             :      {
     527             :        // This might be left over from the last step
     528      344246 :        const Elem::RefinementState flag = elem->refinement_flag();
     529             : 
     530             :        // Set refinement flag to INACTIVE if the
     531             :        // element isn't active
     532      344246 :        if (!elem->active())
     533             :          {
     534       84278 :            elem->set_refinement_flag(Elem::INACTIVE);
     535       84278 :            elem->set_p_refinement_flag(Elem::INACTIVE);
     536             :          }
     537     6156127 :        else if (flag == Elem::JUST_REFINED)
     538           0 :          elem->set_refinement_flag(Elem::DO_NOTHING);
     539     6156127 :        else if (!elements_flagged)
     540             :          {
     541       48095 :            if (flag == Elem::REFINE || flag == Elem::COARSEN)
     542         634 :              elements_flagged = true;
     543             :            else
     544             :              {
     545             :                const Elem::RefinementState pflag =
     546        2574 :                  elem->p_refinement_flag();
     547       32690 :                if (pflag == Elem::REFINE || pflag == Elem::COARSEN)
     548          48 :                  elements_flagged = true;
     549             :              }
     550             :          }
     551     8187898 :      });
     552             : 
     553             :   // Did *any* processor find elements flagged for AMR/C?
     554       20400 :   bool any_elements_flagged = elements_flagged;
     555       20400 :   _mesh.comm().max(any_elements_flagged);
     556             : 
     557             :   // If we have nothing to do, let's not bother verifying that nothing
     558             :   // is compatible with nothing.
     559       20400 :   if (!any_elements_flagged)
     560          28 :     return false;
     561             : 
     562             :   // Parallel consistency has to come first, or coarsening
     563             :   // along processor boundaries might occasionally be falsely
     564             :   // prevented
     565             : #ifdef DEBUG
     566         682 :   bool flags_were_consistent = this->make_flags_parallel_consistent();
     567             : 
     568         682 :   libmesh_assert (flags_were_consistent);
     569             : #endif
     570             : 
     571             :   // Smooth refinement and coarsening flags
     572       19406 :   _smooth_flags(true, true);
     573             : 
     574             :   // First coarsen the flagged elements.
     575             :   const bool coarsening_changed_mesh =
     576       19406 :     this->_coarsen_elements ();
     577             : 
     578             :   // First coarsen the flagged elements.
     579             :   // FIXME: test_level_one now tests consistency across periodic
     580             :   // boundaries, which requires a point_locator, which just got
     581             :   // invalidated by _coarsen_elements() and hasn't yet been cleared by
     582             :   // prepare_for_use().
     583             : 
     584             :   //  libmesh_assert(this->make_coarsening_compatible());
     585             :   //  libmesh_assert(this->make_refinement_compatible());
     586             : 
     587             :   // FIXME: This won't pass unless we add a redundant find_neighbors()
     588             :   // call or replace find_neighbors() with on-the-fly neighbor updating
     589             :   // libmesh_assert(!this->eliminate_unrefined_patches());
     590             : 
     591             :   // We can't contract the mesh ourselves anymore - a System might
     592             :   // need to restrict old coefficient vectors first
     593             :   // _mesh.contract();
     594             : 
     595             :   // First coarsen the flagged elements.
     596             :   // Now refine the flagged elements.  This will
     597             :   // take up some space, maybe more than what was freed.
     598             :   const bool refining_changed_mesh =
     599       19406 :     this->_refine_elements();
     600             : 
     601             :   // First coarsen the flagged elements.
     602             :   // Finally, the new mesh needs to be prepared for use
     603       19406 :   if (coarsening_changed_mesh || refining_changed_mesh)
     604             :     {
     605             : #ifdef DEBUG
     606         674 :       _mesh.libmesh_assert_valid_parallel_ids();
     607             : #endif
     608             : 
     609       19154 :       _mesh.prepare_for_use ();
     610             : 
     611         674 :       if (_face_level_mismatch_limit)
     612         674 :         libmesh_assert(test_level_one(true));
     613         674 :       libmesh_assert(test_unflagged(true));
     614         674 :       libmesh_assert(this->make_coarsening_compatible());
     615         674 :       libmesh_assert(this->make_refinement_compatible());
     616             :       // FIXME: This won't pass unless we add a redundant find_neighbors()
     617             :       // call or replace find_neighbors() with on-the-fly neighbor updating
     618             :       // libmesh_assert(!this->eliminate_unrefined_patches());
     619             : 
     620       19154 :       return true;
     621             :     }
     622             :   else
     623             :     {
     624           8 :       if (_face_level_mismatch_limit)
     625           8 :         libmesh_assert(test_level_one(true));
     626           8 :       libmesh_assert(test_unflagged(true));
     627           8 :       libmesh_assert(this->make_coarsening_compatible());
     628           8 :       libmesh_assert(this->make_refinement_compatible());
     629             :     }
     630             : 
     631             :   // Otherwise there was no change in the mesh,
     632             :   // let the user know.  Also, there is no need
     633             :   // to prepare the mesh for use since it did not change.
     634           8 :   return false;
     635             : 
     636             : }
     637             : 
     638             : 
     639             : 
     640             : 
     641             : 
     642             : 
     643             : 
     644       25829 : bool MeshRefinement::coarsen_elements ()
     645             : {
     646             :   // This function must be run on all processors at once
     647         858 :   parallel_object_only();
     648             : 
     649             :   // We can't yet turn a non-level-one mesh into a level-one mesh
     650         858 :   if (_face_level_mismatch_limit)
     651           4 :     libmesh_assert(test_level_one(true));
     652             : 
     653             :   // Possibly clean up the refinement flags from
     654             :   // a previous step
     655             :   Threads::parallel_for_each
     656       25829 :     (_mesh.element_stored_range(),
     657      541634 :      [](Elem * elem)
     658             :      {
     659             :        // Set refinement flag to INACTIVE if the
     660             :        // element isn't active
     661     1083268 :        if (!elem->active())
     662             :          {
     663      184494 :            elem->set_refinement_flag(Elem::INACTIVE);
     664      184494 :            elem->set_p_refinement_flag(Elem::INACTIVE);
     665             :          }
     666             : 
     667             :        // This might be left over from the last step
     668    10276842 :        if (elem->refinement_flag() == Elem::JUST_REFINED)
     669      135816 :          elem->set_refinement_flag(Elem::DO_NOTHING);
     670      541634 :      });
     671             : 
     672             :   // Parallel consistency has to come first, or coarsening
     673             :   // along processor boundaries might occasionally be falsely
     674             :   // prevented
     675       25829 :   bool flags_were_consistent = this->make_flags_parallel_consistent();
     676             : 
     677             :   // In theory, we should be able to remove the above call, which can
     678             :   // be expensive and should be unnecessary.  In practice, doing
     679             :   // consistent flagging in parallel is hard, it's impossible to
     680             :   // verify at the library level if it's being done by user code, and
     681             :   // we don't want to abort large parallel runs in opt mode... but we
     682             :   // do want to warn that they should be fixed.
     683         858 :   libmesh_assert(flags_were_consistent);
     684             : 
     685         858 :   if (!flags_were_consistent)
     686             :     libmesh_warning("Warning: Refinement flags were not consistent between processors! "
     687             :                     "Correcting and continuing.\n");
     688             : 
     689             :   // Smooth coarsening flags
     690       25829 :   _smooth_flags(false, true);
     691             : 
     692             :   // Coarsen the flagged elements.
     693             :   const bool mesh_changed =
     694       25829 :     this->_coarsen_elements ();
     695             : 
     696         858 :   if (_face_level_mismatch_limit)
     697           4 :     libmesh_assert(test_level_one(true));
     698         858 :   libmesh_assert(this->make_coarsening_compatible());
     699             :   // FIXME: This won't pass unless we add a redundant find_neighbors()
     700             :   // call or replace find_neighbors() with on-the-fly neighbor updating
     701             :   // libmesh_assert(!this->eliminate_unrefined_patches());
     702             : 
     703             :   // We can't contract the mesh ourselves anymore - a System might
     704             :   // need to restrict old coefficient vectors first
     705             :   // _mesh.contract();
     706             : 
     707             :   // Finally, the new mesh may need to be prepared for use
     708       25829 :   if (mesh_changed)
     709         213 :     _mesh.prepare_for_use ();
     710             : 
     711       25829 :   return mesh_changed;
     712             : }
     713             : 
     714             : 
     715             : 
     716             : 
     717             : 
     718             : 
     719             : 
     720       26886 : bool MeshRefinement::refine_elements ()
     721             : {
     722             :   // This function must be run on all processors at once
     723         886 :   parallel_object_only();
     724             : 
     725             :   // Prevent refinement when the mesh has disjoint neighbor boundary pairs.
     726             :   //
     727             :   // Disjoint neighbor boundary interfaces violate the topological continuity
     728             :   // assumed by the refinement algorithm. Refinement on such meshes can
     729             :   // produce invalid neighbor relationships (for example reverse indices
     730             :   // equal to invalid_uint), which may trigger assertions like
     731             :   // `rev < neigh->n_neighbors()` or corrupt neighbor data.
     732             :   //
     733             :   // For safety, refinement is disabled while disjoint neighbor boundary pairs are
     734             :   // present.
     735       26886 :   if (_mesh.get_disjoint_neighbor_boundary_pairs())
     736             :     {
     737         211 :       libmesh_not_implemented_msg(
     738             :         "Mesh refinement is not yet implemented for meshes with disjoint neighbor boundary interfaces.\n");
     739             :     }
     740             : 
     741         884 :   if (_face_level_mismatch_limit)
     742          30 :     libmesh_assert(test_level_one(true));
     743             : 
     744             :   // Possibly clean up the refinement flags from
     745             :   // a previous step
     746             :   Threads::parallel_for_each
     747       26815 :     (_mesh.element_stored_range(),
     748      480630 :      [](Elem * elem)
     749             :      {
     750             :        // Set refinement flag to INACTIVE if the
     751             :        // element isn't active
     752      961260 :        if (!elem->active())
     753             :          {
     754      112978 :            elem->set_refinement_flag(Elem::INACTIVE);
     755      112978 :            elem->set_p_refinement_flag(Elem::INACTIVE);
     756             :          }
     757             : 
     758             :        // This might be left over from the last step
     759     8888471 :        if (elem->refinement_flag() == Elem::JUST_REFINED)
     760           6 :          elem->set_refinement_flag(Elem::DO_NOTHING);
     761      480630 :      });
     762             : 
     763             :   // Parallel consistency has to come first, or coarsening
     764             :   // along processor boundaries might occasionally be falsely
     765             :   // prevented
     766       26815 :   bool flags_were_consistent = this->make_flags_parallel_consistent();
     767             : 
     768             :   // In theory, we should be able to remove the above call, which can
     769             :   // be expensive and should be unnecessary.  In practice, doing
     770             :   // consistent flagging in parallel is hard, it's impossible to
     771             :   // verify at the library level if it's being done by user code, and
     772             :   // we don't want to abort large parallel runs in opt mode... but we
     773             :   // do want to warn that they should be fixed.
     774         884 :   libmesh_assert(flags_were_consistent);
     775             : 
     776         884 :   if (!flags_were_consistent)
     777             :     libmesh_warning("Warning: Refinement flags were not consistent between processors! "
     778             :                     "Correcting and continuing.\n");
     779             : 
     780             :   // Smooth refinement flags
     781       26815 :   _smooth_flags(true, false);
     782             : 
     783             :   // Now refine the flagged elements.  This will
     784             :   // take up some space, maybe more than what was freed.
     785             :   const bool mesh_changed =
     786       26815 :     this->_refine_elements();
     787             : 
     788         884 :   if (_face_level_mismatch_limit)
     789          30 :     libmesh_assert(test_level_one(true));
     790         884 :   libmesh_assert(this->make_refinement_compatible());
     791             : 
     792             :   // FIXME: This won't pass unless we add a redundant find_neighbors()
     793             :   // call or replace find_neighbors() with on-the-fly neighbor updating
     794             :   // libmesh_assert(!this->eliminate_unrefined_patches());
     795             : 
     796             :   // Finally, the new mesh needs to be prepared for use
     797       26815 :   if (mesh_changed)
     798        2335 :     _mesh.prepare_for_use ();
     799             : 
     800       26815 :   return mesh_changed;
     801             : }
     802             : 
     803             : 
     804             : 
     805       98439 : bool MeshRefinement::make_flags_parallel_consistent()
     806             : {
     807             :   // This function must be run on all processors at once
     808        2466 :   parallel_object_only();
     809             : 
     810        4250 :   LOG_SCOPE ("make_flags_parallel_consistent()", "MeshRefinement");
     811             : 
     812             :   SyncRefinementFlags hsync(_mesh, &Elem::refinement_flag,
     813       98439 :                             &Elem::set_refinement_flag);
     814             :   Parallel::sync_dofobject_data_by_id
     815      194412 :     (this->comm(), _mesh.elements_begin(), _mesh.elements_end(), hsync);
     816             : 
     817             :   SyncRefinementFlags psync(_mesh, &Elem::p_refinement_flag,
     818       98439 :                             &Elem::set_p_refinement_flag);
     819             :   Parallel::sync_dofobject_data_by_id
     820      194412 :     (this->comm(), _mesh.elements_begin(), _mesh.elements_end(), psync);
     821             : 
     822             :   // If we weren't consistent in both h and p on every processor then
     823             :   // we weren't globally consistent
     824      194855 :   bool parallel_consistent = hsync.parallel_consistent &&
     825       98189 :     psync.parallel_consistent;
     826       98439 :   this->comm().min(parallel_consistent);
     827             : 
     828      100905 :   return parallel_consistent;
     829             : }
     830             : 
     831             : 
     832             : 
     833       55052 : bool MeshRefinement::make_coarsening_compatible()
     834             : {
     835             :   // This function must be run on all processors at once
     836        3274 :   parallel_object_only();
     837             : 
     838             :   // We may need a PointLocator for topological_neighbor() tests
     839             :   // later, which we need to make sure gets constructed on all
     840             :   // processors at once.
     841       53318 :   std::unique_ptr<PointLocatorBase> point_locator;
     842             : 
     843             : #ifdef LIBMESH_ENABLE_PERIODIC
     844             :   bool has_periodic_boundaries =
     845       55052 :     _periodic_boundaries && !_periodic_boundaries->empty();
     846        3274 :   libmesh_assert(this->comm().verify(has_periodic_boundaries));
     847             : 
     848        3274 :   if (has_periodic_boundaries)
     849         532 :     point_locator = _mesh.sub_point_locator();
     850             : #endif
     851             : 
     852        8282 :   LOG_SCOPE ("make_coarsening_compatible()", "MeshRefinement");
     853             : 
     854             :   // Unless we encounter a specific situation level-one
     855             :   // will be satisfied after executing this loop just once
     856        3274 :   bool level_one_satisfied = true;
     857             : 
     858             : 
     859             :   // Unless we encounter a specific situation we will be compatible
     860             :   // with any selected refinement flags
     861       55052 :   bool compatible_with_refinement = true;
     862             : 
     863             : 
     864             :   // find the maximum h and p levels in the mesh
     865       55052 :   unsigned int max_level = 0;
     866       55052 :   unsigned int max_p_level = 0;
     867             : 
     868             :   {
     869             :     // First we look at all the active level-0 elements.  Since it doesn't make
     870             :     // sense to coarsen them we must un-set their coarsen flags if
     871             :     // they are set.
     872    30889418 :     for (auto & elem : _mesh.active_element_ptr_range())
     873             :       {
     874    16435914 :         max_level = std::max(max_level, elem->level());
     875    16435914 :         max_p_level =
     876    16435914 :           std::max(max_p_level,
     877    17169086 :                    static_cast<unsigned int>(elem->p_level()));
     878             : 
     879    16449760 :         if ((elem->level() == 0) &&
     880      127657 :             (elem->refinement_flag() == Elem::COARSEN))
     881        1040 :           elem->set_refinement_flag(Elem::DO_NOTHING);
     882             : 
     883    17786820 :         if ((elem->p_level() == 0) &&
     884     1350906 :             (elem->p_refinement_flag() == Elem::COARSEN))
     885          10 :           elem->set_p_refinement_flag(Elem::DO_NOTHING);
     886       50044 :       }
     887             :   }
     888             : 
     889             :   // Even if there are no refined elements on this processor then
     890             :   // there may still be work for us to do on e.g. ancestor elements.
     891             :   // At the very least we need to be in the loop if a distributed mesh
     892             :   // needs to synchronize data.
     893             : #if 0
     894             :   if (max_level == 0 && max_p_level == 0)
     895             :     {
     896             :       // But we still have to check with other processors
     897             :       this->comm().min(compatible_with_refinement);
     898             : 
     899             :       return compatible_with_refinement;
     900             :     }
     901             : #endif
     902             : 
     903             :   // Loop over all the active elements.  If an element is marked
     904             :   // for coarsening we better check its neighbors.  If ANY of these neighbors
     905             :   // are marked for refinement AND are at the same level then there is a
     906             :   // conflict.  By convention refinement wins, so we un-mark the element for
     907             :   // coarsening.  Level-one would be violated in this case so we need to re-run
     908             :   // the loop.
     909       55052 :   if (_face_level_mismatch_limit)
     910             :     {
     911             : 
     912       28440 :     repeat:
     913        1938 :       level_one_satisfied = true;
     914             : 
     915         376 :       do
     916             :         {
     917        2314 :           level_one_satisfied = true;
     918             : 
     919    39572686 :           for (auto & elem : _mesh.active_element_ptr_range())
     920             :             {
     921     1096474 :               bool my_flag_changed = false;
     922             : 
     923    21534630 :               if (elem->refinement_flag() == Elem::COARSEN) // If the element is active and
     924             :                 // the coarsen flag is set
     925             :                 {
     926     4931128 :                   const unsigned int my_level = elem->level();
     927             : 
     928    21073536 :                   for (auto n : elem->side_index_range())
     929             :                     {
     930             :                       const Elem * neighbor =
     931    16180579 :                         topological_neighbor(elem, point_locator.get(), n);
     932             : 
     933    16180579 :                       if (neighbor != nullptr &&      // I have a
     934    15854552 :                           neighbor != remote_elem) // neighbor here
     935             :                         {
     936     1265988 :                           if (neighbor->active()) // and it is active
     937             :                             {
     938    16181538 :                               if ((neighbor->level() == my_level) &&
     939      543220 :                                   (neighbor->refinement_flag() == Elem::REFINE)) // the neighbor is at my level
     940             :                                 // and wants to be refined
     941             :                                 {
     942       24242 :                                   elem->set_refinement_flag(Elem::DO_NOTHING);
     943        1094 :                                   my_flag_changed = true;
     944       24242 :                                   break;
     945             :                                 }
     946             :                             }
     947             :                           else // I have a neighbor and it is not active. That means it has children.
     948             :                             {  // While it _may_ be possible to coarsen us if all the children of
     949             :                               // that element want to be coarsened, it is impossible to know at this
     950             :                               // stage.  Forget about it for the moment...  This can be handled in
     951             :                               // two steps.
     952      200817 :                               elem->set_refinement_flag(Elem::DO_NOTHING);
     953        8904 :                               my_flag_changed = true;
     954      200817 :                               break;
     955             :                             }
     956             :                         }
     957             :                     }
     958             :                 }
     959    21534630 :               if (elem->p_refinement_flag() == Elem::COARSEN) // If
     960             :                 // the element is active and the order reduction flag is set
     961             :                 {
     962         164 :                   const unsigned int my_p_level = elem->p_level();
     963             : 
     964        7129 :                   for (auto n : elem->side_index_range())
     965             :                     {
     966             :                       const Elem * neighbor =
     967        5896 :                         topological_neighbor(elem, point_locator.get(), n);
     968             : 
     969        5896 :                       if (neighbor != nullptr &&      // I have a
     970        4637 :                           neighbor != remote_elem) // neighbor here
     971             :                         {
     972         388 :                           if (neighbor->active()) // and it is active
     973             :                             {
     974         348 :                               if ((neighbor->p_level() > my_p_level &&
     975           0 :                                    neighbor->p_refinement_flag() != Elem::COARSEN)
     976        3772 :                                   || (neighbor->p_level() == my_p_level &&
     977         116 :                                       neighbor->p_refinement_flag() == Elem::REFINE))
     978             :                                 {
     979         217 :                                   elem->set_p_refinement_flag(Elem::DO_NOTHING);
     980          14 :                                   my_flag_changed = true;
     981          14 :                                   break;
     982             :                                 }
     983             :                             }
     984             :                           else // I have a neighbor and it is not active.
     985             :                             {  // We need to find which of its children
     986             :                               // have me as a neighbor, and maintain
     987             :                               // level one p compatibility with them.
     988             :                               // Because we currently have level one h
     989             :                               // compatibility, we don't need to check
     990             :                               // grandchildren
     991             : 
     992          20 :                               libmesh_assert(neighbor->has_children());
     993        1616 :                               for (auto & subneighbor : neighbor->child_ref_range())
     994        1513 :                                 if (&subneighbor != remote_elem &&
     995        2362 :                                     subneighbor.active() &&
     996         973 :                                     has_topological_neighbor(&subneighbor, point_locator.get(), elem))
     997         598 :                                   if ((subneighbor.p_level() > my_p_level &&
     998          34 :                                        subneighbor.p_refinement_flag() != Elem::COARSEN)
     999        1014 :                                       || (subneighbor.p_level() == my_p_level &&
    1000           0 :                                           subneighbor.p_refinement_flag() == Elem::REFINE))
    1001             :                                     {
    1002         194 :                                       elem->set_p_refinement_flag(Elem::DO_NOTHING);
    1003          12 :                                       my_flag_changed = true;
    1004          12 :                                       break;
    1005             :                                     }
    1006         239 :                               if (my_flag_changed)
    1007          20 :                                 break;
    1008             :                             }
    1009             :                         }
    1010             :                     }
    1011             :                 }
    1012             : 
    1013             :               // If the current element's flag changed, we hadn't
    1014             :               // satisfied the level one rule.
    1015    20703753 :               if (my_flag_changed)
    1016       10006 :                 level_one_satisfied = false;
    1017             : 
    1018             :               // Additionally, if it has non-local neighbors, and
    1019             :               // we're not in serial, then we'll eventually have to
    1020             :               // return compatible_with_refinement = false, because
    1021             :               // our change has to propagate to neighboring
    1022             :               // processors.
    1023     2132306 :               if (my_flag_changed && !_mesh.is_serial())
    1024       15964 :                 for (auto n : elem->side_index_range())
    1025             :                   {
    1026             :                     Elem * neigh =
    1027       14898 :                       topological_neighbor(elem, point_locator.get(), n);
    1028             : 
    1029       14898 :                     if (!neigh)
    1030        2941 :                       continue;
    1031       11957 :                     if (neigh == remote_elem ||
    1032       11002 :                         neigh->processor_id() !=
    1033           0 :                         this->processor_id())
    1034             :                       {
    1035        5350 :                         compatible_with_refinement = false;
    1036        5350 :                         break;
    1037             :                       }
    1038             :                     // FIXME - for non-level one meshes we should
    1039             :                     // test all descendants
    1040           0 :                     if (neigh->has_children())
    1041        2074 :                       for (auto & child : neigh->child_ref_range())
    1042        1732 :                         if (&child == remote_elem ||
    1043        1732 :                             child.processor_id() !=
    1044           0 :                             this->processor_id())
    1045             :                           {
    1046         258 :                             compatible_with_refinement = false;
    1047         258 :                             break;
    1048             :                           }
    1049             :                   }
    1050       43176 :             }
    1051             :         }
    1052       47118 :       while (!level_one_satisfied);
    1053             : 
    1054             :     } // end if (_face_level_mismatch_limit)
    1055             : 
    1056             : 
    1057             :   // Next we look at all of the ancestor cells.
    1058             :   // If there is a parent cell with all of its children
    1059             :   // wanting to be unrefined then the element is a candidate
    1060             :   // for unrefinement.  If all the children don't
    1061             :   // all want to be unrefined then ALL of them need to have their
    1062             :   // unrefinement flags cleared.
    1063      329304 :   for (int level = max_level; level >= 0; level--)
    1064    59591478 :     for (auto & elem : as_range(_mesh.level_elements_begin(level), _mesh.level_elements_end(level)))
    1065    31208692 :       if (elem->ancestor())
    1066             :         {
    1067             :           // right now the element hasn't been disqualified
    1068             :           // as a candidate for unrefinement
    1069      509894 :           bool is_a_candidate = true;
    1070      509894 :           bool found_remote_child = false;
    1071             : 
    1072    37644566 :           for (auto & child : elem->child_ref_range())
    1073             :             {
    1074    29913852 :               if (&child == remote_elem)
    1075          40 :                 found_remote_child = true;
    1076    29493352 :               else if ((child.refinement_flag() != Elem::COARSEN) ||
    1077      129366 :                        !child.active() )
    1078     1939646 :                 is_a_candidate = false;
    1079             :             }
    1080             : 
    1081     7416676 :           if (!is_a_candidate && !found_remote_child)
    1082             :             {
    1083     6525162 :               elem->set_refinement_flag(Elem::INACTIVE);
    1084             : 
    1085    32724418 :               for (auto & child : elem->child_ref_range())
    1086             :                 {
    1087    26199256 :                   if (&child == remote_elem)
    1088           0 :                     continue;
    1089    26199256 :                   if (child.refinement_flag() == Elem::COARSEN)
    1090             :                     {
    1091       27278 :                       level_one_satisfied = false;
    1092       27278 :                       child.set_refinement_flag(Elem::DO_NOTHING);
    1093             :                     }
    1094             :                 }
    1095             :             }
    1096      238740 :         }
    1097             : 
    1098       64458 :   if (!level_one_satisfied && _face_level_mismatch_limit) goto repeat;
    1099             : 
    1100             : 
    1101             :   // If all the children of a parent are set to be coarsened
    1102             :   // then flag the parent so that they can kill their kids.
    1103             : 
    1104             :   // On a distributed mesh, we won't always be able to determine this
    1105             :   // on parent elements with remote children, even if we own the
    1106             :   // parent, without communication.
    1107             :   //
    1108             :   // We'll first communicate *to* parents' owners when we determine
    1109             :   // they cannot be coarsened, then we'll sync the final refinement
    1110             :   // flag *from* the parents.
    1111             : 
    1112             :   // uncoarsenable_parents[p] live on processor id p
    1113       55052 :   const processor_id_type n_proc     = _mesh.n_processors();
    1114        3274 :   const processor_id_type my_proc_id = _mesh.processor_id();
    1115       55052 :   const bool distributed_mesh = !_mesh.is_replicated();
    1116             : 
    1117             :   std::vector<std::vector<dof_id_type>>
    1118       55052 :     uncoarsenable_parents(n_proc);
    1119             : 
    1120    10390104 :   for (auto & elem : as_range(_mesh.ancestor_elements_begin(), _mesh.ancestor_elements_end()))
    1121             :     {
    1122             :       // Presume all the children are flagged for coarsening and
    1123             :       // then look for a contradiction
    1124      430738 :       bool all_children_flagged_for_coarsening = true;
    1125             : 
    1126     8688027 :       for (auto & child : elem->child_ref_range())
    1127             :         {
    1128     8234776 :           if (&child != remote_elem &&
    1129      470964 :               child.refinement_flag() != Elem::COARSEN)
    1130             :             {
    1131      417532 :               all_children_flagged_for_coarsening = false;
    1132     5850963 :               if (!distributed_mesh)
    1133      417380 :                 break;
    1134     1187551 :               if (child.processor_id() != elem->processor_id())
    1135             :                 {
    1136       95813 :                   uncoarsenable_parents[elem->processor_id()].push_back(elem->id());
    1137           0 :                   break;
    1138             :                 }
    1139             :             }
    1140             :         }
    1141             : 
    1142     5352745 :       if (all_children_flagged_for_coarsening)
    1143      364316 :         elem->set_refinement_flag(Elem::COARSEN_INACTIVE);
    1144             :       else
    1145     5084242 :         elem->set_refinement_flag(Elem::INACTIVE);
    1146       50044 :     }
    1147             : 
    1148             :   // If we have a distributed mesh, we might need to sync up
    1149             :   // INACTIVE vs. COARSEN_INACTIVE flags.
    1150       55052 :   if (distributed_mesh)
    1151             :     {
    1152             :       // We'd better still be in sync here
    1153           8 :       parallel_object_only();
    1154             : 
    1155             :       Parallel::MessageTag
    1156       30485 :         uncoarsenable_tag = this->comm().get_unique_tag();
    1157       30473 :       std::vector<Parallel::Request> uncoarsenable_push_requests(n_proc-1);
    1158             : 
    1159      354526 :       for (processor_id_type p = 0; p != n_proc; ++p)
    1160             :         {
    1161      324053 :           if (p == my_proc_id)
    1162       30469 :             continue;
    1163             : 
    1164             :           Parallel::Request &request =
    1165      293580 :             uncoarsenable_push_requests[p - (p > my_proc_id)];
    1166             : 
    1167      293580 :           _mesh.comm().send
    1168      293580 :             (p, uncoarsenable_parents[p], request, uncoarsenable_tag);
    1169             :         }
    1170             : 
    1171      324053 :       for (processor_id_type p = 1; p != n_proc; ++p)
    1172             :         {
    1173          16 :           std::vector<dof_id_type> my_uncoarsenable_parents;
    1174      293580 :           _mesh.comm().receive
    1175      293580 :             (Parallel::any_source, my_uncoarsenable_parents,
    1176          12 :              uncoarsenable_tag);
    1177             : 
    1178      365027 :           for (const auto & id : my_uncoarsenable_parents)
    1179             :             {
    1180       71447 :               Elem & elem = _mesh.elem_ref(id);
    1181           0 :               libmesh_assert(elem.refinement_flag() == Elem::INACTIVE ||
    1182             :                              elem.refinement_flag() == Elem::COARSEN_INACTIVE);
    1183           0 :               elem.set_refinement_flag(Elem::INACTIVE);
    1184             :             }
    1185             :         }
    1186             : 
    1187       30473 :       Parallel::wait(uncoarsenable_push_requests);
    1188             : 
    1189             :       SyncRefinementFlags hsync(_mesh, &Elem::refinement_flag,
    1190       30473 :                                 &Elem::set_refinement_flag);
    1191             :       sync_dofobject_data_by_id
    1192       60938 :         (this->comm(), _mesh.not_local_elements_begin(),
    1193       30485 :          _mesh.not_local_elements_end(),
    1194             :          // We'd like a smaller sync, but this leads to bugs?
    1195             :          // SyncCoarsenInactive(),
    1196             :          hsync);
    1197       30461 :     }
    1198             : 
    1199             :   // If one processor finds an incompatibility, we're globally
    1200             :   // incompatible
    1201       55052 :   this->comm().min(compatible_with_refinement);
    1202             : 
    1203      110104 :   return compatible_with_refinement;
    1204       50044 : }
    1205             : 
    1206             : 
    1207             : 
    1208             : 
    1209             : 
    1210             : 
    1211             : 
    1212             : 
    1213       56188 : bool MeshRefinement::make_refinement_compatible()
    1214             : {
    1215             :   // This function must be run on all processors at once
    1216        3328 :   parallel_object_only();
    1217             : 
    1218             :   // We may need a PointLocator for topological_neighbor() tests
    1219             :   // later, which we need to make sure gets constructed on all
    1220             :   // processors at once.
    1221       54426 :   std::unique_ptr<PointLocatorBase> point_locator;
    1222             : 
    1223             : #ifdef LIBMESH_ENABLE_PERIODIC
    1224             :   bool has_periodic_boundaries =
    1225       56188 :     _periodic_boundaries && !_periodic_boundaries->empty();
    1226        3328 :   libmesh_assert(this->comm().verify(has_periodic_boundaries));
    1227             : 
    1228        3328 :   if (has_periodic_boundaries)
    1229         532 :     point_locator = _mesh.sub_point_locator();
    1230             : #endif
    1231             : 
    1232        3328 :   LOG_SCOPE ("make_refinement_compatible()", "MeshRefinement");
    1233             : 
    1234             :   // Unless we encounter a specific situation we will be compatible
    1235             :   // with any selected coarsening flags
    1236       56188 :   bool compatible_with_coarsening = true;
    1237             : 
    1238             :   // This loop enforces the level-1 rule.  We should only
    1239             :   // execute it if the user indeed wants level-1 satisfied!
    1240       56188 :   if (_face_level_mismatch_limit)
    1241             :     {
    1242             :       // Unless we encounter a specific situation level-one
    1243             :       // will be satisfied after executing this loop just once
    1244        1620 :       bool level_one_satisfied = true;
    1245             : 
    1246         218 :       do
    1247             :         {
    1248        1838 :           level_one_satisfied = true;
    1249             : 
    1250    25473484 :           for (auto & elem : _mesh.active_element_ptr_range())
    1251             :             {
    1252    13380209 :               const unsigned short n_sides = elem->n_sides();
    1253             : 
    1254    13912727 :               if (elem->refinement_flag() == Elem::REFINE)  // If the element is active and the
    1255             :                 // h refinement flag is set
    1256             :                 {
    1257      446631 :                   const unsigned int my_level = elem->level();
    1258             : 
    1259     2174218 :                   for (unsigned short side = 0; side != n_sides;
    1260             :                        ++side)
    1261             :                     {
    1262             :                       Elem * neighbor =
    1263     1727587 :                         topological_neighbor(elem, point_locator.get(), side);
    1264             : 
    1265     1729745 :                       if (neighbor != nullptr        && // I have a
    1266     1810063 :                           neighbor != remote_elem && // neighbor here
    1267      164952 :                           neighbor->active()) // and it is active
    1268             :                         {
    1269             :                           // Case 1:  The neighbor is at the same level I am.
    1270             :                           //        1a: The neighbor will be refined       -> NO PROBLEM
    1271             :                           //        1b: The neighbor won't be refined      -> NO PROBLEM
    1272             :                           //        1c: The neighbor wants to be coarsened -> PROBLEM
    1273     1230289 :                           if (neighbor->level() == my_level)
    1274             :                             {
    1275     1112198 :                               if (neighbor->refinement_flag() == Elem::COARSEN)
    1276             :                                 {
    1277           2 :                                   neighbor->set_refinement_flag(Elem::DO_NOTHING);
    1278          79 :                                   if (neighbor->parent())
    1279           2 :                                     neighbor->parent()->set_refinement_flag(Elem::INACTIVE);
    1280          77 :                                   compatible_with_coarsening = false;
    1281           2 :                                   level_one_satisfied = false;
    1282             :                                 }
    1283             :                             }
    1284             : 
    1285             : 
    1286             :                           // Case 2: The neighbor is one level lower than I am.
    1287             :                           //         The neighbor thus MUST be refined to satisfy
    1288             :                           //         the level-one rule, regardless of whether it
    1289             :                           //         was originally flagged for refinement. If it
    1290             :                           //         wasn't flagged already we need to repeat
    1291             :                           //         this process.
    1292      118091 :                           else if ((neighbor->level()+1) == my_level)
    1293             :                             {
    1294      118091 :                               if (neighbor->refinement_flag() != Elem::REFINE)
    1295             :                                 {
    1296         620 :                                   neighbor->set_refinement_flag(Elem::REFINE);
    1297       13349 :                                   if (neighbor->parent())
    1298         460 :                                     neighbor->parent()->set_refinement_flag(Elem::INACTIVE);
    1299       12729 :                                   compatible_with_coarsening = false;
    1300         620 :                                   level_one_satisfied = false;
    1301             :                                 }
    1302             :                             }
    1303             : #ifdef DEBUG
    1304             :                           // Note that the only other possibility is that the
    1305             :                           // neighbor is already refined, in which case it isn't
    1306             :                           // active and we should never get here.
    1307             :                           else
    1308           0 :                             libmesh_error_msg("ERROR: Neighbor level must be equal or 1 higher than mine.");
    1309             : #endif
    1310             :                         }
    1311             :                     }
    1312             :                 }
    1313    13912727 :               if (elem->p_refinement_flag() == Elem::REFINE)  // If the element is active and the
    1314             :                 // p refinement flag is set
    1315             :                 {
    1316        2784 :                   const unsigned int my_p_level = elem->p_level();
    1317             : 
    1318      129835 :                   for (unsigned int side=0; side != n_sides; side++)
    1319             :                     {
    1320             :                       Elem * neighbor =
    1321      109604 :                         topological_neighbor(elem, point_locator.get(), side);
    1322             : 
    1323      109604 :                       if (neighbor != nullptr &&      // I have a
    1324       90721 :                           neighbor != remote_elem) // neighbor here
    1325             :                         {
    1326       11884 :                           if (neighbor->active()) // and it is active
    1327             :                             {
    1328       86179 :                               if (neighbor->p_level() < my_p_level &&
    1329          18 :                                   neighbor->p_refinement_flag() != Elem::REFINE)
    1330             :                                 {
    1331           2 :                                   neighbor->set_p_refinement_flag(Elem::REFINE);
    1332           2 :                                   level_one_satisfied = false;
    1333          24 :                                   compatible_with_coarsening = false;
    1334             :                                 }
    1335       91963 :                               if (neighbor->p_level() == my_p_level &&
    1336        5802 :                                   neighbor->p_refinement_flag() == Elem::COARSEN)
    1337             :                                 {
    1338           0 :                                   neighbor->set_p_refinement_flag(Elem::DO_NOTHING);
    1339           0 :                                   level_one_satisfied = false;
    1340           0 :                                   compatible_with_coarsening = false;
    1341             :                                 }
    1342             :                             }
    1343             :                           else // I have an inactive neighbor
    1344             :                             {
    1345          50 :                               libmesh_assert(neighbor->has_children());
    1346        1728 :                               for (auto & subneighbor : neighbor->child_ref_range())
    1347        2099 :                                 if (&subneighbor != remote_elem && subneighbor.active() &&
    1348         947 :                                     has_topological_neighbor(&subneighbor, point_locator.get(), elem))
    1349             :                                   {
    1350         664 :                                     if (subneighbor.p_level() < my_p_level &&
    1351          38 :                                         subneighbor.p_refinement_flag() != Elem::REFINE)
    1352             :                                       {
    1353             :                                         // We should already be level one
    1354             :                                         // compatible
    1355           6 :                                         libmesh_assert_greater (subneighbor.p_level() + 2u,
    1356             :                                                                 my_p_level);
    1357           6 :                                         subneighbor.set_p_refinement_flag(Elem::REFINE);
    1358           6 :                                         level_one_satisfied = false;
    1359          68 :                                         compatible_with_coarsening = false;
    1360             :                                       }
    1361         638 :                                     if (subneighbor.p_level() == my_p_level &&
    1362          12 :                                         subneighbor.p_refinement_flag() == Elem::COARSEN)
    1363             :                                       {
    1364           0 :                                         subneighbor.set_p_refinement_flag(Elem::DO_NOTHING);
    1365           0 :                                         level_one_satisfied = false;
    1366           0 :                                         compatible_with_coarsening = false;
    1367             :                                       }
    1368             :                                   }
    1369             :                             }
    1370             :                         }
    1371             :                     }
    1372             :                 }
    1373       31760 :             }
    1374             :         }
    1375             : 
    1376       34724 :       while (!level_one_satisfied);
    1377             :     } // end if (_face_level_mismatch_limit)
    1378             : 
    1379             :   // If we're not compatible on one processor, we're globally not
    1380             :   // compatible
    1381       56188 :   this->comm().min(compatible_with_coarsening);
    1382             : 
    1383       61278 :   return compatible_with_coarsening;
    1384       51098 : }
    1385             : 
    1386             : 
    1387             : 
    1388             : 
    1389       46367 : bool MeshRefinement::_coarsen_elements ()
    1390             : {
    1391             :   // This function must be run on all processors at once
    1392        1572 :   parallel_object_only();
    1393             : 
    1394        3144 :   LOG_SCOPE ("_coarsen_elements()", "MeshRefinement");
    1395             : 
    1396             :   // Flags indicating if this call actually changes the mesh
    1397       46367 :   bool mesh_changed = false;
    1398       46367 :   bool mesh_p_changed = false;
    1399             : 
    1400             :   // Clear the unused_elements data structure.
    1401             :   // The elements have been packed since it was built,
    1402             :   // so there are _no_ unused elements.  We cannot trust
    1403             :   // any iterators currently in this data structure.
    1404             :   // _unused_elements.clear();
    1405             : 
    1406             :   // Loop over the elements first to determine if the mesh will
    1407             :   // undergo h-coarsening.  If it will, then we'll need to communicate
    1408             :   // more ghosted elements.  We need to communicate them *before* we
    1409             :   // do the coarsening; otherwise it is possible to coarsen away a
    1410             :   // one-element-thick layer partition and leave the partitions on
    1411             :   // either side unable to figure out how to talk to each other.
    1412    24228922 :   for (auto & elem : _mesh.element_ptr_range())
    1413    13378874 :     if (elem->refinement_flag() == Elem::COARSEN)
    1414             :       {
    1415        7017 :         mesh_changed = true;
    1416        7017 :         break;
    1417       43223 :       }
    1418             : 
    1419             :   // If the mesh changed on any processor, it changed globally
    1420       46367 :   this->comm().max(mesh_changed);
    1421             : 
    1422             :   // And then we may need to widen the ghosting layers.
    1423       46367 :   if (mesh_changed)
    1424        7110 :     MeshCommunication().send_coarse_ghosts(_mesh);
    1425             : 
    1426    35997214 :   for (auto & elem : _mesh.element_ptr_range())
    1427             :     {
    1428             :       // Make sure we transfer the children's boundary id(s)
    1429             :       // up to its parent when necessary before coarsening.
    1430    19796746 :       _mesh.get_boundary_info().transfer_boundary_ids_from_children(elem);
    1431             : 
    1432             :       // active elements flagged for coarsening will
    1433             :       // no longer be deleted until MeshRefinement::contract()
    1434    19796746 :       if (elem->refinement_flag() == Elem::COARSEN)
    1435             :         {
    1436             :           // Huh?  no level-0 element should be active
    1437             :           // and flagged for coarsening.
    1438           0 :           libmesh_assert_not_equal_to (elem->level(), 0);
    1439             : 
    1440             :           // Remove this element from any neighbor
    1441             :           // lists that point to it.
    1442           0 :           elem->nullify_neighbors();
    1443             : 
    1444             :           // Remove any boundary information associated
    1445             :           // with this element if we do not allow children to have boundary info.
    1446             :           // Otherwise, we will do the removal in `transfer_boundary_ids_from_children`
    1447             :           // to make sure we don't delete the information before it is transferred
    1448           0 :           if (!_mesh.get_boundary_info().is_children_on_boundary_side())
    1449           0 :             _mesh.get_boundary_info().remove (elem);
    1450             : 
    1451             :           // Add this iterator to the _unused_elements
    1452             :           // data structure so we might fill it.
    1453             :           // The _unused_elements optimization is currently off.
    1454             :           // _unused_elements.push_back (it);
    1455             : 
    1456             :           // Don't delete the element until
    1457             :           // MeshRefinement::contract()
    1458             :           // _mesh.delete_elem(elem);
    1459             :         }
    1460             : 
    1461             :       // inactive elements flagged for coarsening
    1462             :       // will become active
    1463    18874886 :       else if (elem->refinement_flag() == Elem::COARSEN_INACTIVE)
    1464             :         {
    1465      394936 :           elem->coarsen();
    1466       18488 :           libmesh_assert (elem->active());
    1467             : 
    1468             :           // the mesh has certainly changed
    1469      394936 :           mesh_changed = true;
    1470             :         }
    1471    19796746 :       if (elem->p_refinement_flag() == Elem::COARSEN)
    1472             :         {
    1473         220 :           if (elem->p_level() > 0)
    1474             :             {
    1475          14 :               elem->set_p_refinement_flag(Elem::JUST_COARSENED);
    1476         206 :               elem->set_p_level(elem->p_level() - 1);
    1477         206 :               mesh_p_changed = true;
    1478             :             }
    1479             :           else
    1480             :             {
    1481           0 :               elem->set_p_refinement_flag(Elem::DO_NOTHING);
    1482             :             }
    1483             :         }
    1484       43223 :     }
    1485             : 
    1486       46367 :   this->comm().max(mesh_p_changed);
    1487             : 
    1488             :   // And we may need to update cached values reflecting the changes
    1489       46367 :   if (mesh_changed)
    1490             :     {
    1491        7110 :       _mesh.clear_stored_ranges();
    1492        7110 :       _mesh.update_parallel_id_counts();
    1493             :     }
    1494             : 
    1495             :   // Node processor ids may need to change if an element of that id
    1496             :   // was coarsened away
    1497       46367 :   if (mesh_changed && !_mesh.is_serial())
    1498             :     {
    1499             :       // Update the _new_nodes_map so that processors can
    1500             :       // find requested nodes
    1501        1228 :       this->update_nodes_map ();
    1502             : 
    1503        1228 :       MeshCommunication().make_nodes_parallel_consistent (_mesh);
    1504             : 
    1505             :       // Clear the _new_nodes_map
    1506        1228 :       this->clear();
    1507             : 
    1508             : #ifdef DEBUG
    1509           4 :       MeshTools::libmesh_assert_valid_procids<Node>(_mesh);
    1510             : #endif
    1511             :     }
    1512             : 
    1513             :   // If p levels changed all we need to do is make sure that parent p
    1514             :   // levels changed in sync
    1515       46367 :   if (mesh_p_changed && !_mesh.is_serial())
    1516             :     {
    1517         192 :       MeshCommunication().make_p_levels_parallel_consistent (_mesh);
    1518             :     }
    1519             : 
    1520       49511 :   return (mesh_changed || mesh_p_changed);
    1521             : }
    1522             : 
    1523             : 
    1524             : 
    1525       63028 : bool MeshRefinement::_refine_elements ()
    1526             : {
    1527        2130 :   libmesh_assert(_mesh.is_prepared() || _mesh.is_replicated());
    1528             : 
    1529             :   // This function must be run on all processors at once
    1530        2130 :   parallel_object_only();
    1531             : 
    1532             :   // Update the _new_nodes_map so that elements can
    1533             :   // find nodes to connect to.
    1534       63028 :   this->update_nodes_map ();
    1535             : 
    1536        6390 :   LOG_SCOPE ("_refine_elements()", "MeshRefinement");
    1537             : 
    1538             :   // Iterate over the elements, counting the elements
    1539             :   // flagged for h refinement.
    1540        2130 :   dof_id_type n_elems_flagged = 0;
    1541             : 
    1542    35228668 :   for (auto & elem : _mesh.element_ptr_range())
    1543    19362667 :     if (elem->refinement_flag() == Elem::REFINE)
    1544     1305352 :       n_elems_flagged++;
    1545             : 
    1546             :   // Construct a local vector of Elem * which have been
    1547             :   // previously marked for refinement.  We reserve enough
    1548             :   // space to allow for every element to be refined.
    1549        2130 :   std::vector<Elem *> local_copy_of_elements;
    1550       63028 :   local_copy_of_elements.reserve(n_elems_flagged);
    1551             : 
    1552             :   // If mesh p levels changed, we might need to synchronize parent p
    1553             :   // levels on a distributed mesh.
    1554       63028 :   bool mesh_p_changed = false;
    1555             : 
    1556             :   // Iterate over the elements, looking for elements flagged for
    1557             :   // refinement.
    1558             : 
    1559             :   // If we are on a ReplicatedMesh, then we just do the refinement in
    1560             :   // the same order on every processor and everything stays in sync.
    1561             : 
    1562             :   // If we are on a DistributedMesh, that's impossible.
    1563             :   //
    1564             :   // If the mesh is distributed, we need to make sure that if we end
    1565             :   // up as the owner of a new node, which might happen if that node is
    1566             :   // attached to one of our own elements, then we have given it a
    1567             :   // legitimate node id and our own processor id.  We generate
    1568             :   // legitimate node ids and use our own processor id when we are
    1569             :   // refining our own elements but not when we refine others'
    1570             :   // elements.  Therefore we want to refine our own elements *first*,
    1571             :   // thereby generating all nodes which might belong to us, and then
    1572             :   // refine others' elements *after*, thereby generating nodes with
    1573             :   // temporary ids which we know we will discard.
    1574             :   //
    1575             :   // Even if the DistributedMesh is serialized, we can't just treat it
    1576             :   // like a ReplicatedMesh, because DistributedMesh doesn't *trust*
    1577             :   // users to refine partitioned elements in a serialized way, so it
    1578             :   // assigns temporary ids, so we need to synchronize ids afterward to
    1579             :   // be safe anyway, so we might as well use the distributed mesh code
    1580             :   // path.
    1581    23106322 :   for (auto & elem : _mesh.is_replicated() ? _mesh.active_element_ptr_range() : _mesh.active_local_element_ptr_range())
    1582             :     {
    1583    12794834 :       if (elem->refinement_flag() == Elem::REFINE)
    1584      833903 :         local_copy_of_elements.push_back(elem);
    1585    12796132 :       if (elem->p_refinement_flag() == Elem::REFINE &&
    1586        2596 :           elem->active())
    1587             :         {
    1588       11033 :           elem->set_p_level(elem->p_level()+1);
    1589        9735 :           elem->set_p_refinement_flag(Elem::JUST_REFINED);
    1590        9735 :           mesh_p_changed = true;
    1591             :         }
    1592       58768 :     }
    1593             : 
    1594       63028 :   if (!_mesh.is_replicated())
    1595             :     {
    1596       79600 :       for (auto & elem : as_range(_mesh.active_not_local_elements_begin(),
    1597     1636840 :                                   _mesh.active_not_local_elements_end()))
    1598             :         {
    1599      739450 :           if (elem->refinement_flag() == Elem::REFINE)
    1600      412681 :             local_copy_of_elements.push_back(elem);
    1601      739450 :           if (elem->p_refinement_flag() == Elem::REFINE &&
    1602           0 :               elem->active())
    1603             :             {
    1604        8163 :               elem->set_p_level(elem->p_level()+1);
    1605        8163 :               elem->set_p_refinement_flag(Elem::JUST_REFINED);
    1606        8163 :               mesh_p_changed = true;
    1607             :             }
    1608       39611 :         }
    1609             :     }
    1610             : 
    1611             :   // Now iterate over the local copies and refine each one.
    1612             :   // This may resize the mesh's internal container and invalidate
    1613             :   // any existing iterators.
    1614     1309612 :   for (auto & elem : local_copy_of_elements)
    1615     1246584 :     elem->refine(*this);
    1616             : 
    1617             :   // The mesh changed if there were elements h refined
    1618       63028 :   bool mesh_changed = !local_copy_of_elements.empty();
    1619             : 
    1620             :   // If the mesh changed on any processor, it changed globally
    1621       63028 :   this->comm().max(mesh_changed);
    1622       63028 :   this->comm().max(mesh_p_changed);
    1623             : 
    1624             :   // And we may need to update cached values reflecting the changes
    1625       63028 :   if (mesh_changed)
    1626             :     {
    1627       36843 :       _mesh.clear_stored_ranges();
    1628       36843 :       _mesh.update_parallel_id_counts();
    1629             :     }
    1630             : 
    1631       63028 :   if (mesh_changed && !_mesh.is_replicated())
    1632             :     {
    1633       23617 :       MeshCommunication().make_elems_parallel_consistent (_mesh);
    1634       23617 :       MeshCommunication().make_new_nodes_parallel_consistent (_mesh);
    1635             : #ifdef DEBUG
    1636          38 :       _mesh.libmesh_assert_valid_parallel_ids();
    1637             : #endif
    1638             :     }
    1639             : 
    1640             :   // If we're refining a ReplicatedMesh, then we haven't yet assigned
    1641             :   // node processor ids.  But if we're refining a partitioned
    1642             :   // ReplicatedMesh, then we *need* to assign node processor ids.
    1643       65472 :   if (mesh_changed && _mesh.is_replicated() &&
    1644       76254 :       (_mesh.unpartitioned_elements_begin() ==
    1645       88258 :        _mesh.unpartitioned_elements_end()))
    1646       12835 :     Partitioner::set_node_processor_ids(_mesh);
    1647             : 
    1648       63028 :   if (mesh_p_changed && !_mesh.is_replicated())
    1649             :     {
    1650        2368 :       MeshCommunication().make_p_levels_parallel_consistent (_mesh);
    1651             :     }
    1652             : 
    1653             :   // Clear the _new_nodes_map and _unused_elements data structures.
    1654       63028 :   this->clear();
    1655             : 
    1656      126056 :   return (mesh_changed || mesh_p_changed);
    1657             : }
    1658             : 
    1659             : 
    1660       72050 : void MeshRefinement::_smooth_flags(bool refining, bool coarsening)
    1661             : {
    1662             :   // Smoothing can break in weird ways on a mesh with broken topology
    1663             : #ifdef DEBUG
    1664        2424 :   MeshTools::libmesh_assert_valid_neighbors(_mesh);
    1665             : #endif
    1666             : 
    1667             :   // Repeat until flag changes match on every processor
    1668           0 :   do
    1669             :     {
    1670             :       // Repeat until coarsening & refinement flags jive
    1671        2424 :       bool satisfied = false;
    1672         196 :       do
    1673             :         {
    1674             :           // If we're refining or coarsening, hit the corresponding
    1675             :           // face level test code.  Short-circuiting || is our friend
    1676             :           const bool coarsening_satisfied =
    1677      134034 :             !coarsening ||
    1678       53512 :             this->make_coarsening_compatible();
    1679             : 
    1680             :           const bool refinement_satisfied =
    1681      135144 :             !refining ||
    1682       54622 :             this->make_refinement_compatible();
    1683             : 
    1684             :           bool smoothing_satisfied =
    1685       80522 :             !this->eliminate_unrefined_patches();// &&
    1686             : 
    1687       80522 :           if (_edge_level_mismatch_limit)
    1688           0 :             smoothing_satisfied = smoothing_satisfied &&
    1689           0 :               !this->limit_level_mismatch_at_edge (_edge_level_mismatch_limit);
    1690             : 
    1691       80522 :           if (_node_level_mismatch_limit)
    1692           0 :             smoothing_satisfied = smoothing_satisfied &&
    1693           0 :               !this->limit_level_mismatch_at_node (_node_level_mismatch_limit);
    1694             : 
    1695       80522 :           if (_overrefined_boundary_limit>=0)
    1696       56294 :             smoothing_satisfied = smoothing_satisfied &&
    1697       27217 :               !this->limit_overrefined_boundary(_overrefined_boundary_limit);
    1698             : 
    1699       80522 :           if (_underrefined_boundary_limit>=0)
    1700       55726 :             smoothing_satisfied = smoothing_satisfied &&
    1701       26649 :               !this->limit_underrefined_boundary(_underrefined_boundary_limit);
    1702             : 
    1703       80522 :           satisfied = (coarsening_satisfied &&
    1704       83142 :                        refinement_satisfied &&
    1705             :                        smoothing_satisfied);
    1706             : 
    1707        2620 :           libmesh_assert(this->comm().verify(satisfied));
    1708             :         }
    1709       77902 :       while (!satisfied);
    1710             :     }
    1711      139674 :   while (!_mesh.is_serial() && !this->make_flags_parallel_consistent());
    1712       72050 : }
    1713             : 
    1714             : 
    1715           0 : void MeshRefinement::uniformly_p_refine (unsigned int n)
    1716             : {
    1717             :   // Refine n times
    1718           0 :   for (unsigned int rstep=0; rstep<n; rstep++)
    1719           0 :     for (auto & elem : _mesh.active_element_ptr_range())
    1720             :       {
    1721             :         // P refine all the active elements
    1722           0 :         elem->set_p_level(elem->p_level()+1);
    1723           0 :         elem->set_p_refinement_flag(Elem::JUST_REFINED);
    1724           0 :       }
    1725           0 : }
    1726             : 
    1727             : 
    1728             : 
    1729           0 : void MeshRefinement::uniformly_p_coarsen (unsigned int n)
    1730             : {
    1731             :   // Coarsen p times
    1732           0 :   for (unsigned int rstep=0; rstep<n; rstep++)
    1733           0 :     for (auto & elem : _mesh.active_element_ptr_range())
    1734           0 :       if (elem->p_level() > 0)
    1735             :         {
    1736             :           // P coarsen all the active elements
    1737           0 :           elem->set_p_level(elem->p_level()-1);
    1738           0 :           elem->set_p_refinement_flag(Elem::JUST_COARSENED);
    1739           0 :         }
    1740           0 : }
    1741             : 
    1742             : 
    1743             : 
    1744       14628 : void MeshRefinement::uniformly_refine (unsigned int n)
    1745             : {
    1746             :   // Refine n times
    1747             :   // FIXME - this won't work if n>1 and the mesh
    1748             :   // has already been attached to an equation system
    1749       31435 :   for (unsigned int rstep=0; rstep<n; rstep++)
    1750             :     {
    1751             :       // Clean up the refinement flags
    1752       16807 :       this->clean_refinement_flags();
    1753             : 
    1754             :       // Flag all the active elements for refinement.
    1755     1938664 :       for (auto & elem : _mesh.active_element_ptr_range())
    1756     1029766 :         elem->set_refinement_flag(Elem::REFINE);
    1757             : 
    1758             :       // Refine all the elements we just flagged.
    1759       16807 :       this->_refine_elements();
    1760             :     }
    1761             : 
    1762             :   // Finally, the new mesh probably needs to be prepared for use
    1763       14628 :   if (n > 0)
    1764       13443 :     _mesh.prepare_for_use ();
    1765       14628 : }
    1766             : 
    1767             : 
    1768             : 
    1769        1132 : void MeshRefinement::uniformly_coarsen (unsigned int n)
    1770             : {
    1771             :   // Coarsen n times
    1772        2264 :   for (unsigned int rstep=0; rstep<n; rstep++)
    1773             :     {
    1774             :       // Clean up the refinement flags
    1775        1132 :       this->clean_refinement_flags();
    1776             : 
    1777             :       // Flag all the active elements for coarsening.
    1778      471746 :       for (auto & elem : _mesh.active_element_ptr_range())
    1779             :         {
    1780      260509 :           elem->set_refinement_flag(Elem::COARSEN);
    1781      286261 :           if (elem->parent())
    1782       25752 :             elem->parent()->set_refinement_flag(Elem::COARSEN_INACTIVE);
    1783        1068 :         }
    1784             : 
    1785             :       // On a distributed mesh, we may have parent elements with
    1786             :       // remote active children.  To keep flags consistent, we'll need
    1787             :       // a communication step.
    1788        1132 :       if (!_mesh.is_replicated())
    1789             :         {
    1790         908 :           const processor_id_type n_proc = _mesh.n_processors();
    1791           4 :           const processor_id_type my_proc_id = _mesh.processor_id();
    1792             : 
    1793             :           std::vector<std::vector<dof_id_type>>
    1794         916 :             parents_to_coarsen(n_proc);
    1795             : 
    1796      117848 :           for (const auto & elem : as_range(_mesh.ancestor_elements_begin(), _mesh.ancestor_elements_end()))
    1797       57878 :             if (elem->processor_id() != my_proc_id &&
    1798          48 :                 elem->refinement_flag() == Elem::COARSEN_INACTIVE)
    1799       14280 :               parents_to_coarsen[elem->processor_id()].push_back(elem->id());
    1800             : 
    1801             :           Parallel::MessageTag
    1802         916 :             coarsen_tag = this->comm().get_unique_tag();
    1803         908 :           std::vector<Parallel::Request> coarsen_push_requests(n_proc-1);
    1804             : 
    1805       10452 :           for (processor_id_type p = 0; p != n_proc; ++p)
    1806             :             {
    1807        9544 :               if (p == my_proc_id)
    1808         904 :                 continue;
    1809             : 
    1810             :               Parallel::Request &request =
    1811        8636 :                 coarsen_push_requests[p - (p > my_proc_id)];
    1812             : 
    1813        8636 :               _mesh.comm().send
    1814        8636 :                 (p, parents_to_coarsen[p], request, coarsen_tag);
    1815             :             }
    1816             : 
    1817        9544 :           for (processor_id_type p = 1; p != n_proc; ++p)
    1818             :             {
    1819           8 :               std::vector<dof_id_type> my_parents_to_coarsen;
    1820        8636 :               _mesh.comm().receive
    1821        8636 :                 (Parallel::any_source, my_parents_to_coarsen,
    1822           8 :                  coarsen_tag);
    1823             : 
    1824       21980 :               for (const auto & id : my_parents_to_coarsen)
    1825             :                 {
    1826       13344 :                   Elem & elem = _mesh.elem_ref(id);
    1827          36 :                   libmesh_assert(elem.refinement_flag() == Elem::INACTIVE ||
    1828             :                                  elem.refinement_flag() == Elem::COARSEN_INACTIVE);
    1829          36 :                   elem.set_refinement_flag(Elem::COARSEN_INACTIVE);
    1830             :                 }
    1831             :             }
    1832             : 
    1833         908 :           Parallel::wait(coarsen_push_requests);
    1834             : 
    1835             :           SyncRefinementFlags hsync(_mesh, &Elem::refinement_flag,
    1836         908 :                                     &Elem::set_refinement_flag);
    1837             :           sync_dofobject_data_by_id
    1838        1812 :             (this->comm(), _mesh.not_local_elements_begin(),
    1839         916 :              _mesh.not_local_elements_end(),
    1840             :              // We'd like a smaller sync, but this leads to bugs?
    1841             :              // SyncCoarsenInactive(),
    1842             :              hsync);
    1843         900 :         }
    1844             : 
    1845             :       // Coarsen all the elements we just flagged.
    1846        1132 :       this->_coarsen_elements();
    1847             :     }
    1848             : 
    1849             : 
    1850             :   // Finally, the new mesh probably needs to be prepared for use
    1851        1132 :   if (n > 0)
    1852        1132 :     _mesh.prepare_for_use ();
    1853        1132 : }
    1854             : 
    1855             : 
    1856             : 
    1857    19116342 : Elem * MeshRefinement::topological_neighbor(Elem * elem,
    1858             :                                             const PointLocatorBase * point_locator,
    1859             :                                             const unsigned int side) const
    1860             : {
    1861             : #ifdef LIBMESH_ENABLE_PERIODIC
    1862    19116342 :   if (_periodic_boundaries && !_periodic_boundaries->empty())
    1863             :     {
    1864      488472 :       libmesh_assert(point_locator);
    1865      786435 :       return elem->topological_neighbor(side, _mesh, *point_locator, _periodic_boundaries);
    1866             :     }
    1867             : #endif
    1868    18869749 :   return elem->neighbor_ptr(side);
    1869             : }
    1870             : 
    1871             : 
    1872             : 
    1873        1920 : bool MeshRefinement::has_topological_neighbor(const Elem * elem,
    1874             :                                               const PointLocatorBase * point_locator,
    1875             :                                               const Elem * neighbor) const
    1876             : {
    1877             : #ifdef LIBMESH_ENABLE_PERIODIC
    1878        1920 :   if (_periodic_boundaries && !_periodic_boundaries->empty())
    1879             :     {
    1880           0 :       libmesh_assert(point_locator);
    1881           0 :       return elem->has_topological_neighbor(neighbor, _mesh, *point_locator, _periodic_boundaries);
    1882             :     }
    1883             : #endif
    1884        1772 :   return elem->has_neighbor(neighbor);
    1885             : }
    1886             : 
    1887             : 
    1888             : 
    1889             : } // namespace libMesh
    1890             : 
    1891             : 
    1892             : #endif

Generated by: LCOV version 1.14