41 template <
int dim,
int spacedim>
45 std::vector<bool> p_flags(
53 template <
int dim,
int spacedim>
56 const std::vector<bool> & p_flags)
62 if (cell->is_locally_owned() && p_flags[cell->active_cell_index()])
64 if (cell->refine_flag_set())
66 const unsigned int super_fe_index =
68 cell->active_fe_index());
71 if (super_fe_index != cell->active_fe_index())
72 cell->set_future_fe_index(super_fe_index);
74 else if (cell->coarsen_flag_set())
76 const unsigned int sub_fe_index =
78 cell->active_fe_index());
81 if (sub_fe_index != cell->active_fe_index())
82 cell->set_future_fe_index(sub_fe_index);
89 template <
int dim,
typename Number,
int spacedim>
93 const Vector<Number> & criteria,
94 const Number p_refine_threshold,
95 const Number p_coarsen_threshold,
103 std::vector<bool> p_flags(
107 if (cell->is_locally_owned() &&
108 ((cell->refine_flag_set() &&
109 compare_refine(criteria[cell->active_cell_index()],
110 p_refine_threshold)) ||
111 (cell->coarsen_flag_set() &&
112 compare_coarsen(criteria[cell->active_cell_index()],
113 p_coarsen_threshold))))
114 p_flags[cell->active_cell_index()] =
true;
121 template <
int dim,
typename Number,
int spacedim>
125 const Vector<Number> & criteria,
126 const double p_refine_fraction,
127 const double p_coarsen_fraction,
134 Assert((p_refine_fraction >= 0) && (p_refine_fraction <= 1),
136 Assert((p_coarsen_fraction >= 0) && (p_coarsen_fraction <= 1),
141 Number max_criterion_refine = std::numeric_limits<Number>::lowest(),
143 Number max_criterion_coarsen = max_criterion_refine,
144 min_criterion_coarsen = min_criterion_refine;
147 if (cell->is_locally_owned())
149 if (cell->refine_flag_set())
151 max_criterion_refine =
153 criteria(cell->active_cell_index()));
154 min_criterion_refine =
156 criteria(cell->active_cell_index()));
158 else if (cell->coarsen_flag_set())
160 max_criterion_coarsen =
162 criteria(cell->active_cell_index()));
163 min_criterion_coarsen =
165 criteria(cell->active_cell_index()));
172 if (parallel_tria !=
nullptr &&
176 max_criterion_refine =
179 min_criterion_refine =
182 max_criterion_coarsen =
185 min_criterion_coarsen =
192 const Number threshold_refine =
193 min_criterion_refine +
195 (max_criterion_refine - min_criterion_refine),
197 min_criterion_coarsen +
199 (max_criterion_coarsen - min_criterion_coarsen);
211 template <
int dim,
typename Number,
int spacedim>
215 const Vector<Number> & criteria,
216 const double p_refine_fraction,
217 const double p_coarsen_fraction,
224 Assert((p_refine_fraction >= 0) && (p_refine_fraction <= 1),
226 Assert((p_coarsen_fraction >= 0) && (p_coarsen_fraction <= 1),
234 [](
const Number &,
const Number &) {
return false; };
236 [](
const Number &,
const Number &) {
return true; };
240 unsigned int n_flags_refinement = 0;
241 unsigned int n_flags_coarsening = 0;
242 Vector<Number> indicators_refinement(
244 Vector<Number> indicators_coarsening(
246 for (
const auto &cell :
248 if (!cell->is_artificial() && cell->is_locally_owned())
250 if (cell->refine_flag_set())
251 indicators_refinement(n_flags_refinement++) =
252 criteria(cell->active_cell_index());
253 else if (cell->coarsen_flag_set())
254 indicators_coarsening(n_flags_coarsening++) =
255 criteria(cell->active_cell_index());
257 indicators_refinement.grow_or_shrink(n_flags_refinement);
258 indicators_coarsening.grow_or_shrink(n_flags_coarsening);
276 Number threshold_refinement = 0.;
277 Number threshold_coarsening = 0.;
278 auto reference_compare_refine = std::cref(compare_refine);
279 auto reference_compare_coarsen = std::cref(compare_coarsen);
284 if (parallel_tria !=
nullptr &&
288 #ifndef DEAL_II_WITH_P4EST
299 const unsigned int n_global_flags_refinement =
301 const unsigned int n_global_flags_coarsening =
304 const unsigned int target_index_refinement =
305 static_cast<unsigned int>(
306 std::floor(p_refine_fraction * n_global_flags_refinement));
307 const unsigned int target_index_coarsening =
308 static_cast<unsigned int>(
309 std::ceil((1 - p_coarsen_fraction) * n_global_flags_coarsening));
313 const std::pair<Number, Number> global_min_max_refinement =
318 const std::pair<Number, Number> global_min_max_coarsening =
324 if (target_index_refinement == 0)
325 reference_compare_refine = std::cref(compare_false);
326 else if (target_index_refinement == n_global_flags_refinement)
327 reference_compare_refine = std::cref(compare_true);
331 indicators_refinement,
332 global_min_max_refinement,
333 target_index_refinement,
336 if (target_index_coarsening == n_global_flags_coarsening)
337 reference_compare_coarsen = std::cref(compare_false);
338 else if (target_index_coarsening == 0)
339 reference_compare_coarsen = std::cref(compare_true);
343 indicators_coarsening,
344 global_min_max_coarsening,
345 target_index_coarsening,
356 const unsigned int n_p_refine_cells =
static_cast<unsigned int>(
357 std::floor(p_refine_fraction * n_flags_refinement));
358 const unsigned int n_p_coarsen_cells =
static_cast<unsigned int>(
359 std::floor(p_coarsen_fraction * n_flags_coarsening));
362 if (n_p_refine_cells == 0)
363 reference_compare_refine = std::cref(compare_false);
364 else if (n_p_refine_cells == n_flags_refinement)
365 reference_compare_refine = std::cref(compare_true);
368 std::nth_element(indicators_refinement.begin(),
369 indicators_refinement.begin() +
370 n_p_refine_cells - 1,
371 indicators_refinement.end(),
372 std::greater<Number>());
373 threshold_refinement =
374 *(indicators_refinement.begin() + n_p_refine_cells - 1);
377 if (n_p_coarsen_cells == 0)
378 reference_compare_coarsen = std::cref(compare_false);
379 else if (n_p_coarsen_cells == n_flags_coarsening)
380 reference_compare_coarsen = std::cref(compare_true);
383 std::nth_element(indicators_coarsening.begin(),
384 indicators_coarsening.begin() +
385 n_p_coarsen_cells - 1,
386 indicators_coarsening.end(),
387 std::less<Number>());
388 threshold_coarsening =
389 *(indicators_coarsening.begin() + n_p_coarsen_cells - 1);
396 threshold_refinement,
397 threshold_coarsening,
398 std::cref(reference_compare_refine),
400 reference_compare_coarsen));
405 template <
int dim,
typename Number,
int spacedim>
409 const Vector<Number> & sobolev_indices)
412 sobolev_indices.size());
415 if (cell->is_locally_owned())
417 if (cell->refine_flag_set())
419 const unsigned int super_fe_index =
421 cell->active_fe_index());
424 if (super_fe_index != cell->active_fe_index())
426 const unsigned int super_fe_degree =
429 if (sobolev_indices[cell->active_cell_index()] >
431 cell->set_future_fe_index(super_fe_index);
434 else if (cell->coarsen_flag_set())
436 const unsigned int sub_fe_index =
438 cell->active_fe_index());
441 if (sub_fe_index != cell->active_fe_index())
443 const unsigned int sub_fe_degree =
446 if (sobolev_indices[cell->active_cell_index()] <
448 cell->set_future_fe_index(sub_fe_index);
456 template <
int dim,
typename Number,
int spacedim>
460 const Vector<Number> & criteria,
461 const Vector<Number> & references,
471 std::vector<bool> p_flags(
475 if (cell->is_locally_owned() &&
476 ((cell->refine_flag_set() &&
477 compare_refine(criteria[cell->active_cell_index()],
478 references[cell->active_cell_index()])) ||
479 (cell->coarsen_flag_set() &&
480 compare_coarsen(criteria[cell->active_cell_index()],
481 references[cell->active_cell_index()]))))
482 p_flags[cell->active_cell_index()] =
true;
492 template <
int dim,
typename Number,
int spacedim>
495 const Vector<Number> & error_indicators,
496 Vector<Number> & predicted_errors,
497 const double gamma_p,
498 const double gamma_h,
499 const double gamma_n)
502 error_indicators.size());
504 predicted_errors.size());
505 Assert(0 < gamma_p && gamma_p < 1,
515 std::map<typename hp::DoFHandler<dim, spacedim>::cell_iterator,
517 future_fe_indices_on_coarsened_cells;
520 predicted_errors = error_indicators;
523 if (cell->is_locally_owned())
526 if (!(cell->future_fe_index_set()) && !(cell->refine_flag_set()) &&
527 !(cell->coarsen_flag_set()))
529 predicted_errors[cell->active_cell_index()] *= gamma_n;
535 if (cell->coarsen_flag_set())
539 const auto &parent = cell->parent();
540 if (future_fe_indices_on_coarsened_cells.find(parent) ==
541 future_fe_indices_on_coarsened_cells.end())
543 std::set<unsigned int> fe_indices_children;
544 for (
unsigned int child_index = 0;
545 child_index < parent->n_children();
548 const auto &child = parent->child(child_index);
549 Assert(child->is_active() && child->coarsen_flag_set(),
551 dim>::ExcInconsistentCoarseningFlags());
553 fe_indices_children.insert(child->future_fe_index());
557 parent_future_fe_index =
559 .find_dominated_fe_extended(fe_indices_children,
564 typename ::hp::FECollection<
565 dim>::ExcNoDominatedFiniteElementAmongstChildren());
567 future_fe_indices_on_coarsened_cells.insert(
568 {parent, parent_future_fe_index});
572 parent_future_fe_index =
573 future_fe_indices_on_coarsened_cells[parent];
589 if (cell->future_fe_index_set())
591 predicted_errors[cell->active_cell_index()] *=
592 std::pow(gamma_p, future_fe_degree - cell->get_fe().degree);
596 if (cell->refine_flag_set())
598 predicted_errors[cell->active_cell_index()] *=
599 (gamma_h * std::pow(.5, future_fe_degree));
604 else if (cell->coarsen_flag_set())
606 predicted_errors[cell->active_cell_index()] /=
607 (gamma_h * std::pow(.5, future_fe_degree));
620 template <
int dim,
int spacedim>
625 if (cell->is_locally_owned() && cell->future_fe_index_set())
627 cell->clear_refine_flag();
628 cell->clear_coarsen_flag();
634 template <
int dim,
int spacedim>
639 if (cell->is_locally_owned() && cell->future_fe_index_set())
641 cell->clear_refine_flag();
647 if (cell->coarsen_flag_set())
649 const auto & parent = cell->parent();
650 const unsigned int n_children = parent->n_children();
652 unsigned int h_flagged_children = 0, p_flagged_children = 0;
653 for (
unsigned int child_index = 0; child_index < n_children;
656 const auto &child = parent->child(child_index);
657 if (child->is_active())
659 if (child->coarsen_flag_set())
660 ++h_flagged_children;
661 if (child->future_fe_index_set())
662 ++p_flagged_children;
666 if (h_flagged_children == n_children &&
667 p_flagged_children != n_children)
670 for (
unsigned int child_index = 0; child_index < n_children;
672 parent->child(child_index)->clear_future_fe_index();
676 for (
unsigned int child_index = 0; child_index < n_children;
678 if (parent->child(child_index)->is_active())
679 parent->child(child_index)->clear_coarsen_flag();
688 #include "refinement.inst"