21 #include "libmesh/libmesh_config.h"
24 #ifdef LIBMESH_ENABLE_AMR
30 #include "libmesh/elem.h"
31 #include "libmesh/error_vector.h"
32 #include "libmesh/mesh_refinement.h"
33 #include "libmesh/mesh_base.h"
34 #include "libmesh/parallel.h"
35 #include "libmesh/remote_elem.h"
45 const Real refine_frac,
46 const Real coarsen_frac,
47 const unsigned int max_l)
49 parallel_object_only();
53 libmesh_assert(this->
comm().verify(
dynamic_cast<const std::vector<ErrorVectorReal> &
>(error_per_cell)));
61 if (refine_frac != 0.3 ||
62 coarsen_frac != 0.0 ||
84 Real error_min = 1.e30;
88 Real parent_error_min = 1.e30;
89 Real parent_error_max = 0.;
105 libmesh_assert_less (
id, error_per_cell.size());
107 error_max = std::max (error_max, error_per_cell[
id]);
108 error_min = std::min (error_min, error_per_cell[
id]);
110 this->
comm().max(error_max);
111 this->
comm().min(error_min);
114 const Real error_delta = (error_max - error_min);
115 const Real parent_error_delta = parent_error_max - parent_error_min;
138 libmesh_assert_less (
id, error_per_cell.size());
148 if (error_per_parent[parentid] >= 0. &&
149 error_per_parent[parentid] <= parent_cutoff)
155 else if (elem_error <= coarsen_cutoff)
162 if (elem_error >= refine_cutoff)
172 parallel_object_only();
176 libmesh_assert(this->
comm().verify(
dynamic_cast<const std::vector<ErrorVectorReal> &
>(error_per_cell_in)));
188 const Real local_refinement_tolerance =
190 const Real local_coarsening_tolerance =
197 Real parent_error_min, parent_error_max;
211 if (elem_error > local_refinement_tolerance &&
216 local_coarsening_tolerance)
222 if (parent_error >= 0.)
224 const Real parent_coarsening_tolerance =
226 local_coarsening_tolerance *
227 local_coarsening_tolerance);
228 if (parent_error < parent_coarsening_tolerance)
239 parallel_object_only();
243 libmesh_assert(this->
comm().verify(
dynamic_cast<const std::vector<ErrorVectorReal> &
>(error_per_cell)));
274 const std::ptrdiff_t n_elem_new =
275 std::ptrdiff_t(
_nelem_target) - std::ptrdiff_t(n_active_elem);
280 std::vector<std::pair<ErrorVectorReal, dof_id_type>> sorted_error;
282 sorted_error.reserve (n_active_elem);
287 std::vector<bool> is_active(max_elem_id,
false);
292 is_active[eid] =
true;
293 libmesh_assert_less (eid, error_per_cell.size());
294 sorted_error.push_back
295 (std::make_pair(error_per_cell[eid], eid));
298 this->
comm().max(is_active);
300 this->
comm().allgather(sorted_error);
304 std::sort (sorted_error.begin(), sorted_error.end());
305 std::reverse (sorted_error.begin(), sorted_error.end());
310 std::vector<std::pair<ErrorVectorReal, dof_id_type>> sorted_parent_error;
311 Real parent_error_min, parent_error_max;
321 if (error_per_parent[i] != -1)
322 sorted_parent_error.push_back(std::make_pair(error_per_parent[i], i));
324 std::sort (sorted_parent_error.begin(), sorted_parent_error.end());
331 unsigned int twotodim = 1;
332 for (
unsigned int i=0; i!=
dim; ++i)
341 std::min(cast_int<dof_id_type>(n_elem_new / (twotodim-1)),
349 std::min(cast_int<dof_id_type>(-n_elem_new / (twotodim-1)),
354 while (coarsen_count < max_elem_coarsen &&
355 refine_count < max_elem_refine &&
356 coarsen_count < sorted_parent_error.size() &&
357 refine_count < sorted_error.size() &&
358 sorted_error[refine_count].first >
369 std::vector<bool> is_refinable(max_elem_id,
false);
371 for (
const auto & pr : sorted_error)
376 is_refinable[eid] =
true;
378 this->
comm().max(is_refinable);
380 if (refine_count > max_elem_refine)
381 refine_count = max_elem_refine;
382 for (
const auto & pr : sorted_error)
384 if (successful_refine_count >= refine_count)
389 if (is_refinable[eid])
393 successful_refine_count++;
400 if (coarsen_count < (refine_count - successful_refine_count))
403 coarsen_count -= (refine_count - successful_refine_count);
405 if (coarsen_count > max_elem_coarsen)
406 coarsen_count = max_elem_coarsen;
411 for (
const auto & pr : sorted_parent_error)
413 if (successful_coarsen_count >= coarsen_count * twotodim)
430 successful_coarsen_count++;
437 if (!successful_coarsen_count &&
438 !successful_refine_count)
447 const Real refine_frac,
448 const Real coarsen_frac,
449 const unsigned int max_l)
451 parallel_object_only();
455 libmesh_assert(this->
comm().verify(
dynamic_cast<const std::vector<ErrorVectorReal> &
>(error_per_cell)));
463 if (refine_frac != 0.3 ||
464 coarsen_frac != 0.0 ||
466 libmesh_deprecated();
501 std::vector<ErrorVectorReal> sorted_error;
503 sorted_error.reserve (n_active_elem);
508 sorted_error.push_back (error_per_cell[elem->id()]);
510 this->
comm().allgather(sorted_error);
513 std::sort (sorted_error.begin(), sorted_error.end());
521 Real parent_error_min, parent_error_max;
528 sorted_parent_error = error_per_parent;
529 std::sort (sorted_parent_error.begin(), sorted_parent_error.end());
532 sorted_parent_error.erase (std::remove(sorted_parent_error.begin(),
533 sorted_parent_error.end(), 0.),
534 sorted_parent_error.end());
545 unsigned int twotodim = 1;
546 for (
unsigned int i=0; i!=
dim; ++i)
549 dof_id_type n_parent_coarsen = n_elem_coarsen / (twotodim - 1);
551 if (n_parent_coarsen)
552 bottom_error = sorted_parent_error[n_parent_coarsen - 1];
554 else if (n_elem_coarsen)
556 bottom_error = sorted_error[n_elem_coarsen - 1];
560 top_error = sorted_error[sorted_error.size() - n_elem_refine];
568 error_per_parent[parent->
id()] <= bottom_error)
572 error_per_cell[elem->id()] <= bottom_error)
577 error_per_cell[elem->id()] >= top_error)
585 const Real refine_frac,
586 const Real coarsen_frac,
587 const unsigned int max_l)
591 libmesh_assert(this->
comm().verify(
dynamic_cast<const std::vector<ErrorVectorReal> &
>(error_per_cell)));
599 if (refine_frac != 0.3 ||
600 coarsen_frac != 0.0 ||
602 libmesh_deprecated();
610 const Real mean = error_per_cell.
mean();
632 libmesh_assert_less (
id, error_per_cell.size());
637 if (elem_error <= coarsen_cutoff)
641 if ((elem_error >= refine_cutoff) && (elem->level() <
_max_h_level))
661 elem->set_p_refinement_flag(elem->refinement_flag());
666 elem->set_p_refinement_flag(elem->refinement_flag());
677 elem->set_p_refinement_flag(elem->refinement_flag());