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 46333 : MeshRefinement::MeshRefinement (MeshBase & m) :
96 : ParallelObject(m),
97 19373 : _mesh(m),
98 19373 : _use_member_parameters(false),
99 19373 : _coarsen_by_parents(false),
100 19373 : _refine_fraction(0.3),
101 19373 : _coarsen_fraction(0.0),
102 19373 : _max_h_level(libMesh::invalid_uint),
103 19373 : _coarsen_threshold(10),
104 19373 : _nelem_target(0),
105 19373 : _absolute_global_tolerance(0.0),
106 19373 : _face_level_mismatch_limit(1),
107 19373 : _edge_level_mismatch_limit(0),
108 19373 : _node_level_mismatch_limit(0),
109 19373 : _overrefined_boundary_limit(0),
110 19373 : _underrefined_boundary_limit(0),
111 19373 : _allow_unrefined_patches(false),
112 19373 : _enforce_mismatch_limit_prior_to_refinement(false)
113 : #ifdef LIBMESH_ENABLE_PERIODIC
114 59813 : , _periodic_boundaries(nullptr)
115 : #endif
116 : {
117 46333 : }
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 19432 : MeshRefinement::~MeshRefinement () = default;
131 :
132 :
133 :
134 6912 : void MeshRefinement::clear ()
135 : {
136 2134 : _new_nodes_map.clear();
137 6912 : }
138 :
139 :
140 :
141 8785257 : 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 8785257 : unsigned int parent_n = parent.as_parent_node(child, node);
149 :
150 8785257 : if (parent_n != libMesh::invalid_uint)
151 3353853 : return parent.node_ptr(parent_n);
152 :
153 : const std::vector<std::pair<dof_id_type, dof_id_type>>
154 8109472 : 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 6202304 : if (const auto new_node_id = _new_nodes_map.find(bracketing_nodes);
166 : new_node_id != DofObject::invalid_id)
167 4436522 : 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 20267679 : for (auto n : parent.node_index_range())
176 : {
177 : // The value from the embedding matrix
178 18501897 : const Real em_val = parent.embedding_matrix(child,node,n);
179 :
180 18501897 : if (em_val != 0.)
181 : {
182 9394435 : 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 1765782 : 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 1765782 : new_node->processor_id() = DofObject::invalid_processor_id;
198 :
199 : // Add the node to the map.
200 1765782 : _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 1246441 : Elem * MeshRefinement::add_elem (std::unique_ptr<Elem> elem)
215 : {
216 390142 : libmesh_assert(elem);
217 2026725 : return _mesh.add_elem(std::move(elem));
218 : }
219 :
220 :
221 246 : 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 246 : error_per_parent.clear();
248 328 : error_per_parent.resize(error_per_cell.size(), 0.0);
249 :
250 : {
251 : // Find which elements are uncoarsenable
252 24100 : for (auto & elem : _mesh.active_local_element_ptr_range())
253 : {
254 16338 : Elem * parent = elem->parent();
255 :
256 : // Active elements are uncoarsenable
257 20831 : error_per_parent[elem->id()] = -1.0;
258 :
259 : // Grandparents and up are uncoarsenable
260 51336 : while (parent)
261 : {
262 22384 : parent = parent->parent();
263 41361 : if (parent)
264 : {
265 14446 : const dof_id_type parentid = parent->id();
266 7223 : libmesh_assert_less (parentid, error_per_parent.size());
267 34283 : error_per_parent[parentid] = -1.0;
268 : }
269 : }
270 82 : }
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 246 : 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 246 : (_mesh.active_local_element_stored_range(),
289 37508 : [&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 16338 : 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 18270 : if (error_per_parent[parentid] != -1.0)
303 19584 : error_per_parent[parentid] += (error_per_cell[elem->id()] *
304 3496 : error_per_cell[elem->id()]);
305 : }
306 16338 : });
307 :
308 : // Sum the vector across all processors
309 246 : this->comm().sum(static_cast<std::vector<ErrorVectorReal> &>(error_per_parent));
310 :
311 : // Calculate the min and max as we loop
312 246 : parent_error_min = std::numeric_limits<double>::max();
313 246 : parent_error_max = 0.;
314 :
315 37412 : 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 48674 : if (error_per_parent[i] < 0.)
323 : {
324 31500 : error_per_parent[i] = -1.;
325 31500 : 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 7430 : 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 5666 : error_per_parent[i] = std::sqrt(error_per_parent[i]);
339 :
340 5666 : parent_error_min = std::min (parent_error_min,
341 1764 : error_per_parent[i]);
342 6518 : parent_error_max = std::max (parent_error_max,
343 1764 : error_per_parent[i]);
344 : }
345 246 : }
346 :
347 :
348 :
349 6912 : void MeshRefinement::update_nodes_map ()
350 : {
351 6912 : this->_new_nodes_map.init(_mesh);
352 6912 : }
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 2288 : 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 2288 : std::atomic<bool> elements_flagged{false};
522 :
523 : Threads::parallel_for_each
524 2288 : (_mesh.element_stored_range(),
525 1898134 : [&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 815753 : else if (flag == Elem::JUST_REFINED)
538 0 : elem->set_refinement_flag(Elem::DO_NOTHING);
539 815753 : else if (!elements_flagged)
540 : {
541 10489 : 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 8468 : if (pflag == Elem::REFINE || pflag == Elem::COARSEN)
548 48 : elements_flagged = true;
549 : }
550 : }
551 1080191 : });
552 :
553 : // Did *any* processor find elements flagged for AMR/C?
554 2288 : bool any_elements_flagged = elements_flagged;
555 2288 : _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 2288 : 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 2190 : _smooth_flags(true, true);
573 :
574 : // First coarsen the flagged elements.
575 : const bool coarsening_changed_mesh =
576 2190 : 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 2190 : this->_refine_elements();
600 :
601 : // First coarsen the flagged elements.
602 : // Finally, the new mesh needs to be prepared for use
603 2190 : if (coarsening_changed_mesh || refining_changed_mesh)
604 : {
605 : #ifdef DEBUG
606 674 : _mesh.libmesh_assert_valid_parallel_ids();
607 : #endif
608 :
609 2166 : _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 2166 : 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 2789 : 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 2789 : (_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 1727698 : 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 2789 : 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 2789 : _smooth_flags(false, true);
691 :
692 : // Coarsen the flagged elements.
693 : const bool mesh_changed =
694 2789 : 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 2789 : if (mesh_changed)
709 21 : _mesh.prepare_for_use ();
710 :
711 2789 : return mesh_changed;
712 : }
713 :
714 :
715 :
716 :
717 :
718 :
719 :
720 2886 : 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 2886 : if (_mesh.get_disjoint_neighbor_boundary_pairs())
736 : {
737 19 : 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 2879 : (_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 1524791 : 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 2879 : 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 2879 : _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 2879 : 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 2879 : if (mesh_changed)
798 223 : _mesh.prepare_for_use ();
799 :
800 2879 : return mesh_changed;
801 : }
802 :
803 :
804 :
805 6497 : 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 6497 : &Elem::set_refinement_flag);
814 : Parallel::sync_dofobject_data_by_id
815 10528 : (this->comm(), _mesh.elements_begin(), _mesh.elements_end(), hsync);
816 :
817 : SyncRefinementFlags psync(_mesh, &Elem::p_refinement_flag,
818 6497 : &Elem::set_p_refinement_flag);
819 : Parallel::sync_dofobject_data_by_id
820 10528 : (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 11187 : bool parallel_consistent = hsync.parallel_consistent &&
825 6463 : psync.parallel_consistent;
826 6497 : this->comm().min(parallel_consistent);
827 :
828 8963 : return parallel_consistent;
829 : }
830 :
831 :
832 :
833 7143 : 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 5409 : std::unique_ptr<PointLocatorBase> point_locator;
842 :
843 : #ifdef LIBMESH_ENABLE_PERIODIC
844 : bool has_periodic_boundaries =
845 7143 : _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 7143 : bool compatible_with_refinement = true;
862 :
863 :
864 : // find the maximum h and p levels in the mesh
865 7143 : unsigned int max_level = 0;
866 7143 : 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 3824616 : for (auto & elem : _mesh.active_element_ptr_range())
873 : {
874 2951422 : max_level = std::max(max_level, elem->level());
875 2951422 : max_p_level =
876 2951422 : std::max(max_p_level,
877 3684594 : static_cast<unsigned int>(elem->p_level()));
878 :
879 2965268 : if ((elem->level() == 0) &&
880 31630 : (elem->refinement_flag() == Elem::COARSEN))
881 1040 : elem->set_refinement_flag(Elem::DO_NOTHING);
882 :
883 4302328 : if ((elem->p_level() == 0) &&
884 1350906 : (elem->p_refinement_flag() == Elem::COARSEN))
885 10 : elem->set_p_refinement_flag(Elem::DO_NOTHING);
886 2135 : }
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 7143 : if (_face_level_mismatch_limit)
910 : {
911 :
912 3514 : repeat:
913 1938 : level_one_satisfied = true;
914 :
915 376 : do
916 : {
917 2314 : level_one_satisfied = true;
918 :
919 3843476 : for (auto & elem : _mesh.active_element_ptr_range())
920 : {
921 1096474 : bool my_flag_changed = false;
922 :
923 3711311 : if (elem->refinement_flag() == Elem::COARSEN) // If the element is active and
924 : // the coarsen flag is set
925 : {
926 588421 : const unsigned int my_level = elem->level();
927 :
928 2752019 : for (auto n : elem->side_index_range())
929 : {
930 : const Elem * neighbor =
931 2006985 : topological_neighbor(elem, point_locator.get(), n);
932 :
933 2006985 : if (neighbor != nullptr && // I have a
934 1972079 : neighbor != remote_elem) // neighbor here
935 : {
936 1265988 : if (neighbor->active()) // and it is active
937 : {
938 2488545 : 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 3521 : elem->set_refinement_flag(Elem::DO_NOTHING);
943 1094 : my_flag_changed = true;
944 3521 : 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 26754 : elem->set_refinement_flag(Elem::DO_NOTHING);
953 8904 : my_flag_changed = true;
954 26754 : break;
955 : }
956 : }
957 : }
958 : }
959 3711311 : 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 1092 : for (auto n : elem->side_index_range())
965 : {
966 : const Elem * neighbor =
967 924 : topological_neighbor(elem, point_locator.get(), n);
968 :
969 924 : if (neighbor != nullptr && // I have a
970 679 : 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 725 : || (neighbor->p_level() == my_p_level &&
977 116 : neighbor->p_refinement_flag() == Elem::REFINE))
978 : {
979 49 : 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 245 : for (auto & subneighbor : neighbor->child_ref_range())
994 341 : if (&subneighbor != remote_elem &&
995 434 : subneighbor.active() &&
996 217 : has_topological_neighbor(&subneighbor, point_locator.get(), elem))
997 119 : if ((subneighbor.p_level() > my_p_level &&
998 34 : subneighbor.p_refinement_flag() != Elem::COARSEN)
999 208 : || (subneighbor.p_level() == my_p_level &&
1000 0 : subneighbor.p_refinement_flag() == Elem::REFINE))
1001 : {
1002 42 : elem->set_p_refinement_flag(Elem::DO_NOTHING);
1003 12 : my_flag_changed = true;
1004 12 : break;
1005 : }
1006 40 : 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 2880602 : 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 1937439 : if (my_flag_changed && !_mesh.is_serial())
1024 0 : for (auto n : elem->side_index_range())
1025 : {
1026 : Elem * neigh =
1027 0 : topological_neighbor(elem, point_locator.get(), n);
1028 :
1029 0 : if (!neigh)
1030 0 : continue;
1031 0 : if (neigh == remote_elem ||
1032 0 : neigh->processor_id() !=
1033 0 : this->processor_id())
1034 : {
1035 0 : compatible_with_refinement = false;
1036 0 : break;
1037 : }
1038 : // FIXME - for non-level one meshes we should
1039 : // test all descendants
1040 0 : if (neigh->has_children())
1041 0 : for (auto & child : neigh->child_ref_range())
1042 0 : if (&child == remote_elem ||
1043 0 : child.processor_id() !=
1044 0 : this->processor_id())
1045 : {
1046 0 : compatible_with_refinement = false;
1047 0 : break;
1048 : }
1049 : }
1050 1890 : }
1051 : }
1052 5832 : 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 45436 : for (int level = max_level; level >= 0; level--)
1064 6780274 : for (auto & elem : as_range(_mesh.level_elements_begin(level), _mesh.level_elements_end(level)))
1065 5144646 : 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 6312908 : for (auto & child : elem->child_ref_range())
1073 : {
1074 4809956 : if (&child == remote_elem)
1075 40 : found_remote_child = true;
1076 4939242 : else if ((child.refinement_flag() != Elem::COARSEN) ||
1077 129366 : !child.active() )
1078 1939646 : is_a_candidate = false;
1079 : }
1080 :
1081 1188914 : if (!is_a_candidate && !found_remote_child)
1082 : {
1083 1107924 : elem->set_refinement_flag(Elem::INACTIVE);
1084 :
1085 5592576 : for (auto & child : elem->child_ref_range())
1086 : {
1087 4484652 : if (&child == remote_elem)
1088 0 : continue;
1089 4484652 : 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 11036 : }
1097 :
1098 8294 : 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 7143 : const processor_id_type n_proc = _mesh.n_processors();
1114 3274 : const processor_id_type my_proc_id = _mesh.processor_id();
1115 7143 : const bool distributed_mesh = !_mesh.is_replicated();
1116 :
1117 : std::vector<std::vector<dof_id_type>>
1118 7143 : uncoarsenable_parents(n_proc);
1119 :
1120 1231357 : 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 1346510 : for (auto & child : elem->child_ref_range())
1127 : {
1128 1540115 : if (&child != remote_elem &&
1129 470964 : child.refinement_flag() != Elem::COARSEN)
1130 : {
1131 417532 : all_children_flagged_for_coarsening = false;
1132 898931 : if (!distributed_mesh)
1133 417380 : break;
1134 360 : if (child.processor_id() != elem->processor_id())
1135 : {
1136 0 : uncoarsenable_parents[elem->processor_id()].push_back(elem->id());
1137 0 : break;
1138 : }
1139 : }
1140 : }
1141 :
1142 941048 : if (all_children_flagged_for_coarsening)
1143 42367 : elem->set_refinement_flag(Elem::COARSEN_INACTIVE);
1144 : else
1145 898681 : elem->set_refinement_flag(Elem::INACTIVE);
1146 2135 : }
1147 :
1148 : // If we have a distributed mesh, we might need to sync up
1149 : // INACTIVE vs. COARSEN_INACTIVE flags.
1150 7143 : if (distributed_mesh)
1151 : {
1152 : // We'd better still be in sync here
1153 8 : parallel_object_only();
1154 :
1155 : Parallel::MessageTag
1156 30 : uncoarsenable_tag = this->comm().get_unique_tag();
1157 18 : std::vector<Parallel::Request> uncoarsenable_push_requests(n_proc-1);
1158 :
1159 52 : for (processor_id_type p = 0; p != n_proc; ++p)
1160 : {
1161 34 : if (p == my_proc_id)
1162 14 : continue;
1163 :
1164 : Parallel::Request &request =
1165 16 : uncoarsenable_push_requests[p - (p > my_proc_id)];
1166 :
1167 16 : _mesh.comm().send
1168 16 : (p, uncoarsenable_parents[p], request, uncoarsenable_tag);
1169 : }
1170 :
1171 34 : for (processor_id_type p = 1; p != n_proc; ++p)
1172 : {
1173 16 : std::vector<dof_id_type> my_uncoarsenable_parents;
1174 16 : _mesh.comm().receive
1175 16 : (Parallel::any_source, my_uncoarsenable_parents,
1176 12 : uncoarsenable_tag);
1177 :
1178 16 : for (const auto & id : my_uncoarsenable_parents)
1179 : {
1180 0 : 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 18 : Parallel::wait(uncoarsenable_push_requests);
1188 :
1189 : SyncRefinementFlags hsync(_mesh, &Elem::refinement_flag,
1190 18 : &Elem::set_refinement_flag);
1191 : sync_dofobject_data_by_id
1192 28 : (this->comm(), _mesh.not_local_elements_begin(),
1193 30 : _mesh.not_local_elements_end(),
1194 : // We'd like a smaller sync, but this leads to bugs?
1195 : // SyncCoarsenInactive(),
1196 : hsync);
1197 6 : }
1198 :
1199 : // If one processor finds an incompatibility, we're globally
1200 : // incompatible
1201 7143 : this->comm().min(compatible_with_refinement);
1202 :
1203 14286 : return compatible_with_refinement;
1204 2135 : }
1205 :
1206 :
1207 :
1208 :
1209 :
1210 :
1211 :
1212 :
1213 7266 : 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 5504 : std::unique_ptr<PointLocatorBase> point_locator;
1222 :
1223 : #ifdef LIBMESH_ENABLE_PERIODIC
1224 : bool has_periodic_boundaries =
1225 7266 : _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 7266 : 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 7266 : 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 2625234 : for (auto & elem : _mesh.active_element_ptr_range())
1251 : {
1252 1986482 : const unsigned short n_sides = elem->n_sides();
1253 :
1254 2519000 : if (elem->refinement_flag() == Elem::REFINE) // If the element is active and the
1255 : // h refinement flag is set
1256 : {
1257 71775 : const unsigned int my_level = elem->level();
1258 :
1259 356020 : for (unsigned short side = 0; side != n_sides;
1260 : ++side)
1261 : {
1262 : Elem * neighbor =
1263 284245 : topological_neighbor(elem, point_locator.get(), side);
1264 :
1265 354860 : if (neighbor != nullptr && // I have a
1266 366721 : 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 221641 : if (neighbor->level() == my_level)
1274 : {
1275 204474 : if (neighbor->refinement_flag() == Elem::COARSEN)
1276 : {
1277 2 : neighbor->set_refinement_flag(Elem::DO_NOTHING);
1278 10 : if (neighbor->parent())
1279 2 : neighbor->parent()->set_refinement_flag(Elem::INACTIVE);
1280 8 : 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 17167 : else if ((neighbor->level()+1) == my_level)
1293 : {
1294 17167 : if (neighbor->refinement_flag() != Elem::REFINE)
1295 : {
1296 620 : neighbor->set_refinement_flag(Elem::REFINE);
1297 2564 : if (neighbor->parent())
1298 460 : neighbor->parent()->set_refinement_flag(Elem::INACTIVE);
1299 1944 : 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 2519000 : 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 31012 : for (unsigned int side=0; side != n_sides; side++)
1319 : {
1320 : Elem * neighbor =
1321 26116 : topological_neighbor(elem, point_locator.get(), side);
1322 :
1323 26116 : if (neighbor != nullptr && // I have a
1324 20842 : neighbor != remote_elem) // neighbor here
1325 : {
1326 11884 : if (neighbor->active()) // and it is active
1327 : {
1328 26574 : 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 9 : compatible_with_coarsening = false;
1334 : }
1335 32358 : 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 534 : for (auto & subneighbor : neighbor->child_ref_range())
1347 663 : if (&subneighbor != remote_elem && subneighbor.active() &&
1348 307 : has_topological_neighbor(&subneighbor, point_locator.get(), elem))
1349 : {
1350 266 : 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 21 : compatible_with_coarsening = false;
1360 : }
1361 240 : 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 1362 : }
1374 : }
1375 :
1376 4326 : 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 7266 : this->comm().min(compatible_with_coarsening);
1382 :
1383 12356 : return compatible_with_coarsening;
1384 2176 : }
1385 :
1386 :
1387 :
1388 :
1389 5087 : 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 5087 : bool mesh_changed = false;
1398 5087 : 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 2860450 : for (auto & elem : _mesh.element_ptr_range())
1413 2729786 : if (elem->refinement_flag() == Elem::COARSEN)
1414 : {
1415 885 : mesh_changed = true;
1416 885 : break;
1417 1943 : }
1418 :
1419 : // If the mesh changed on any processor, it changed globally
1420 5087 : this->comm().max(mesh_changed);
1421 :
1422 : // And then we may need to widen the ghosting layers.
1423 5087 : if (mesh_changed)
1424 885 : MeshCommunication().send_coarse_ghosts(_mesh);
1425 :
1426 4027816 : 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 3853327 : _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 3853327 : 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 2931467 : else if (elem->refinement_flag() == Elem::COARSEN_INACTIVE)
1464 : {
1465 60627 : elem->coarsen();
1466 18488 : libmesh_assert (elem->active());
1467 :
1468 : // the mesh has certainly changed
1469 60627 : mesh_changed = true;
1470 : }
1471 3853327 : if (elem->p_refinement_flag() == Elem::COARSEN)
1472 : {
1473 63 : if (elem->p_level() > 0)
1474 : {
1475 14 : elem->set_p_refinement_flag(Elem::JUST_COARSENED);
1476 49 : elem->set_p_level(elem->p_level() - 1);
1477 49 : mesh_p_changed = true;
1478 : }
1479 : else
1480 : {
1481 0 : elem->set_p_refinement_flag(Elem::DO_NOTHING);
1482 : }
1483 : }
1484 1943 : }
1485 :
1486 5087 : this->comm().max(mesh_p_changed);
1487 :
1488 : // And we may need to update cached values reflecting the changes
1489 5087 : if (mesh_changed)
1490 : {
1491 885 : _mesh.clear_stored_ranges();
1492 885 : _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 5087 : if (mesh_changed && !_mesh.is_serial())
1498 : {
1499 : // Update the _new_nodes_map so that processors can
1500 : // find requested nodes
1501 12 : this->update_nodes_map ();
1502 :
1503 12 : MeshCommunication().make_nodes_parallel_consistent (_mesh);
1504 :
1505 : // Clear the _new_nodes_map
1506 12 : 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 5087 : if (mesh_p_changed && !_mesh.is_serial())
1516 : {
1517 0 : MeshCommunication().make_p_levels_parallel_consistent (_mesh);
1518 : }
1519 :
1520 8231 : return (mesh_changed || mesh_p_changed);
1521 : }
1522 :
1523 :
1524 :
1525 6900 : 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 6900 : 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 3921246 : for (auto & elem : _mesh.element_ptr_range())
1543 3765084 : if (elem->refinement_flag() == Elem::REFINE)
1544 243141 : 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 6900 : 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 6900 : 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 2831412 : for (auto & elem : _mesh.is_replicated() ? _mesh.active_element_ptr_range() : _mesh.active_local_element_ptr_range())
1582 : {
1583 2713507 : if (elem->refinement_flag() == Elem::REFINE)
1584 239793 : local_copy_of_elements.push_back(elem);
1585 2714805 : if (elem->p_refinement_flag() == Elem::REFINE &&
1586 2596 : elem->active())
1587 : {
1588 5852 : elem->set_p_level(elem->p_level()+1);
1589 4554 : elem->set_p_refinement_flag(Elem::JUST_REFINED);
1590 4554 : mesh_p_changed = true;
1591 : }
1592 2640 : }
1593 :
1594 6900 : if (!_mesh.is_replicated())
1595 : {
1596 496 : for (auto & elem : as_range(_mesh.active_not_local_elements_begin(),
1597 1748 : _mesh.active_not_local_elements_end()))
1598 : {
1599 1008 : if (elem->refinement_flag() == Elem::REFINE)
1600 708 : local_copy_of_elements.push_back(elem);
1601 1008 : if (elem->p_refinement_flag() == Elem::REFINE &&
1602 0 : elem->active())
1603 : {
1604 0 : elem->set_p_level(elem->p_level()+1);
1605 0 : elem->set_p_refinement_flag(Elem::JUST_REFINED);
1606 0 : mesh_p_changed = true;
1607 : }
1608 59 : }
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 247401 : for (auto & elem : local_copy_of_elements)
1615 240501 : elem->refine(*this);
1616 :
1617 : // The mesh changed if there were elements h refined
1618 6900 : bool mesh_changed = !local_copy_of_elements.empty();
1619 :
1620 : // If the mesh changed on any processor, it changed globally
1621 6900 : this->comm().max(mesh_changed);
1622 6900 : this->comm().max(mesh_p_changed);
1623 :
1624 : // And we may need to update cached values reflecting the changes
1625 6900 : if (mesh_changed)
1626 : {
1627 4075 : _mesh.clear_stored_ranges();
1628 4075 : _mesh.update_parallel_id_counts();
1629 : }
1630 :
1631 6900 : if (mesh_changed && !_mesh.is_replicated())
1632 : {
1633 129 : MeshCommunication().make_elems_parallel_consistent (_mesh);
1634 129 : 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 9344 : if (mesh_changed && _mesh.is_replicated() &&
1644 10846 : (_mesh.unpartitioned_elements_begin() ==
1645 13570 : _mesh.unpartitioned_elements_end()))
1646 3811 : Partitioner::set_node_processor_ids(_mesh);
1647 :
1648 6900 : if (mesh_p_changed && !_mesh.is_replicated())
1649 : {
1650 0 : MeshCommunication().make_p_levels_parallel_consistent (_mesh);
1651 : }
1652 :
1653 : // Clear the _new_nodes_map and _unused_elements data structures.
1654 6900 : this->clear();
1655 :
1656 13800 : return (mesh_changed || mesh_p_changed);
1657 : }
1658 :
1659 :
1660 7858 : 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 14092 : !coarsening ||
1678 5603 : this->make_coarsening_compatible();
1679 :
1680 : const bool refinement_satisfied =
1681 14189 : !refining ||
1682 5700 : this->make_refinement_compatible();
1683 :
1684 : bool smoothing_satisfied =
1685 8489 : !this->eliminate_unrefined_patches();// &&
1686 :
1687 8489 : 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 8489 : 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 8489 : if (_overrefined_boundary_limit>=0)
1696 5714 : smoothing_satisfied = smoothing_satisfied &&
1697 2775 : !this->limit_overrefined_boundary(_overrefined_boundary_limit);
1698 :
1699 8489 : if (_underrefined_boundary_limit>=0)
1700 5658 : smoothing_satisfied = smoothing_satisfied &&
1701 2719 : !this->limit_underrefined_boundary(_underrefined_boundary_limit);
1702 :
1703 8489 : satisfied = (coarsening_satisfied &&
1704 11109 : refinement_satisfied &&
1705 : smoothing_satisfied);
1706 :
1707 2620 : libmesh_assert(this->comm().verify(satisfied));
1708 : }
1709 5869 : while (!satisfied);
1710 : }
1711 10868 : while (!_mesh.is_serial() && !this->make_flags_parallel_consistent());
1712 7858 : }
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 1508 : 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 3339 : for (unsigned int rstep=0; rstep<n; rstep++)
1750 : {
1751 : // Clean up the refinement flags
1752 1831 : this->clean_refinement_flags();
1753 :
1754 : // Flag all the active elements for refinement.
1755 270870 : for (auto & elem : _mesh.active_element_ptr_range())
1756 195869 : elem->set_refinement_flag(Elem::REFINE);
1757 :
1758 : // Refine all the elements we just flagged.
1759 1831 : this->_refine_elements();
1760 : }
1761 :
1762 : // Finally, the new mesh probably needs to be prepared for use
1763 1508 : if (n > 0)
1764 1411 : _mesh.prepare_for_use ();
1765 1508 : }
1766 :
1767 :
1768 :
1769 108 : void MeshRefinement::uniformly_coarsen (unsigned int n)
1770 : {
1771 : // Coarsen n times
1772 216 : for (unsigned int rstep=0; rstep<n; rstep++)
1773 : {
1774 : // Clean up the refinement flags
1775 108 : this->clean_refinement_flags();
1776 :
1777 : // Flag all the active elements for coarsening.
1778 126712 : for (auto & elem : _mesh.active_element_ptr_range())
1779 : {
1780 89016 : elem->set_refinement_flag(Elem::COARSEN);
1781 114768 : if (elem->parent())
1782 25752 : elem->parent()->set_refinement_flag(Elem::COARSEN_INACTIVE);
1783 44 : }
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 108 : if (!_mesh.is_replicated())
1789 : {
1790 12 : 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 20 : parents_to_coarsen(n_proc);
1795 :
1796 1084 : for (const auto & elem : as_range(_mesh.ancestor_elements_begin(), _mesh.ancestor_elements_end()))
1797 840 : if (elem->processor_id() != my_proc_id &&
1798 48 : elem->refinement_flag() == Elem::COARSEN_INACTIVE)
1799 148 : parents_to_coarsen[elem->processor_id()].push_back(elem->id());
1800 :
1801 : Parallel::MessageTag
1802 20 : coarsen_tag = this->comm().get_unique_tag();
1803 12 : std::vector<Parallel::Request> coarsen_push_requests(n_proc-1);
1804 :
1805 36 : for (processor_id_type p = 0; p != n_proc; ++p)
1806 : {
1807 24 : if (p == my_proc_id)
1808 8 : continue;
1809 :
1810 : Parallel::Request &request =
1811 12 : coarsen_push_requests[p - (p > my_proc_id)];
1812 :
1813 12 : _mesh.comm().send
1814 12 : (p, parents_to_coarsen[p], request, coarsen_tag);
1815 : }
1816 :
1817 24 : for (processor_id_type p = 1; p != n_proc; ++p)
1818 : {
1819 8 : std::vector<dof_id_type> my_parents_to_coarsen;
1820 12 : _mesh.comm().receive
1821 12 : (Parallel::any_source, my_parents_to_coarsen,
1822 8 : coarsen_tag);
1823 :
1824 120 : for (const auto & id : my_parents_to_coarsen)
1825 : {
1826 108 : 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 12 : Parallel::wait(coarsen_push_requests);
1834 :
1835 : SyncRefinementFlags hsync(_mesh, &Elem::refinement_flag,
1836 12 : &Elem::set_refinement_flag);
1837 : sync_dofobject_data_by_id
1838 20 : (this->comm(), _mesh.not_local_elements_begin(),
1839 20 : _mesh.not_local_elements_end(),
1840 : // We'd like a smaller sync, but this leads to bugs?
1841 : // SyncCoarsenInactive(),
1842 : hsync);
1843 4 : }
1844 :
1845 : // Coarsen all the elements we just flagged.
1846 108 : this->_coarsen_elements();
1847 : }
1848 :
1849 :
1850 : // Finally, the new mesh probably needs to be prepared for use
1851 108 : if (n > 0)
1852 108 : _mesh.prepare_for_use ();
1853 108 : }
1854 :
1855 :
1856 :
1857 3396048 : Elem * MeshRefinement::topological_neighbor(Elem * elem,
1858 : const PointLocatorBase * point_locator,
1859 : const unsigned int side) const
1860 : {
1861 : #ifdef LIBMESH_ENABLE_PERIODIC
1862 3396048 : 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 3149455 : return elem->neighbor_ptr(side);
1869 : }
1870 :
1871 :
1872 :
1873 524 : 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 524 : 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 376 : return elem->has_neighbor(neighbor);
1885 : }
1886 :
1887 :
1888 :
1889 : } // namespace libMesh
1890 :
1891 :
1892 : #endif
|