25#include <deal.II/dofs/dof_accessor.templates.h>
47 template <
int dim,
int spacedim>
58 std::vector<bool> p_flags(
66 template <
int dim,
int spacedim>
69 const std::vector<bool> & p_flags)
81 if (cell->is_locally_owned() && p_flags[cell->active_cell_index()])
83 if (cell->refine_flag_set())
85 const unsigned int super_fe_index =
87 cell->active_fe_index());
90 if (super_fe_index != cell->active_fe_index())
91 cell->set_future_fe_index(super_fe_index);
93 else if (cell->coarsen_flag_set())
95 const unsigned int sub_fe_index =
97 cell->active_fe_index());
100 if (sub_fe_index != cell->active_fe_index())
101 cell->set_future_fe_index(sub_fe_index);
108 template <
int dim,
typename Number,
int spacedim>
113 const Number p_refine_threshold,
114 const Number p_coarsen_threshold,
129 std::vector<bool> p_flags(
133 if (cell->is_locally_owned() &&
134 ((cell->refine_flag_set() &&
135 compare_refine(criteria[cell->active_cell_index()],
136 p_refine_threshold)) ||
137 (cell->coarsen_flag_set() &&
138 compare_coarsen(criteria[cell->active_cell_index()],
139 p_coarsen_threshold))))
140 p_flags[cell->active_cell_index()] =
true;
147 template <
int dim,
typename Number,
int spacedim>
152 const double p_refine_fraction,
153 const double p_coarsen_fraction,
167 Assert((p_refine_fraction >= 0) && (p_refine_fraction <= 1),
169 Assert((p_coarsen_fraction >= 0) && (p_coarsen_fraction <= 1),
174 Number max_criterion_refine = std::numeric_limits<Number>::lowest(),
175 min_criterion_refine = std::numeric_limits<Number>::max();
176 Number max_criterion_coarsen = max_criterion_refine,
177 min_criterion_coarsen = min_criterion_refine;
180 if (cell->is_locally_owned())
182 if (cell->refine_flag_set())
184 max_criterion_refine =
186 criteria(cell->active_cell_index()));
187 min_criterion_refine =
189 criteria(cell->active_cell_index()));
191 else if (cell->coarsen_flag_set())
193 max_criterion_coarsen =
195 criteria(cell->active_cell_index()));
196 min_criterion_coarsen =
198 criteria(cell->active_cell_index()));
205 if (parallel_tria !=
nullptr &&
209 max_criterion_refine =
212 min_criterion_refine =
215 max_criterion_coarsen =
218 min_criterion_coarsen =
225 const Number threshold_refine =
226 min_criterion_refine +
228 (max_criterion_refine - min_criterion_refine),
230 min_criterion_coarsen +
232 (max_criterion_coarsen - min_criterion_coarsen);
244 template <
int dim,
typename Number,
int spacedim>
249 const double p_refine_fraction,
250 const double p_coarsen_fraction,
264 Assert((p_refine_fraction >= 0) && (p_refine_fraction <= 1),
266 Assert((p_coarsen_fraction >= 0) && (p_coarsen_fraction <= 1),
274 [](
const Number &,
const Number &) {
return false; };
276 [](
const Number &,
const Number &) {
return true; };
280 unsigned int n_flags_refinement = 0;
281 unsigned int n_flags_coarsening = 0;
286 for (
const auto &cell :
288 if (!cell->is_artificial() && cell->is_locally_owned())
290 if (cell->refine_flag_set())
291 indicators_refinement(n_flags_refinement++) =
292 criteria(cell->active_cell_index());
293 else if (cell->coarsen_flag_set())
294 indicators_coarsening(n_flags_coarsening++) =
295 criteria(cell->active_cell_index());
316 Number threshold_refinement = 0.;
317 Number threshold_coarsening = 0.;
318 auto reference_compare_refine = std::cref(compare_refine);
319 auto reference_compare_coarsen = std::cref(compare_coarsen);
324 if (parallel_tria !=
nullptr &&
328#ifndef DEAL_II_WITH_P4EST
339 const unsigned int n_global_flags_refinement =
341 const unsigned int n_global_flags_coarsening =
344 const unsigned int target_index_refinement =
345 static_cast<unsigned int>(
346 std::floor(p_refine_fraction * n_global_flags_refinement));
347 const unsigned int target_index_coarsening =
348 static_cast<unsigned int>(
349 std::ceil((1 - p_coarsen_fraction) * n_global_flags_coarsening));
353 const std::pair<Number, Number> global_min_max_refinement =
358 const std::pair<Number, Number> global_min_max_coarsening =
364 if (target_index_refinement == 0)
365 reference_compare_refine = std::cref(compare_false);
366 else if (target_index_refinement == n_global_flags_refinement)
367 reference_compare_refine = std::cref(compare_true);
371 indicators_refinement,
372 global_min_max_refinement,
373 target_index_refinement,
376 if (target_index_coarsening == n_global_flags_coarsening)
377 reference_compare_coarsen = std::cref(compare_false);
378 else if (target_index_coarsening == 0)
379 reference_compare_coarsen = std::cref(compare_true);
383 indicators_coarsening,
384 global_min_max_coarsening,
385 target_index_coarsening,
396 const unsigned int n_p_refine_cells =
static_cast<unsigned int>(
397 std::floor(p_refine_fraction * n_flags_refinement));
398 const unsigned int n_p_coarsen_cells =
static_cast<unsigned int>(
399 std::floor(p_coarsen_fraction * n_flags_coarsening));
402 if (n_p_refine_cells == 0)
403 reference_compare_refine = std::cref(compare_false);
404 else if (n_p_refine_cells == n_flags_refinement)
405 reference_compare_refine = std::cref(compare_true);
408 std::nth_element(indicators_refinement.
begin(),
409 indicators_refinement.
begin() +
410 n_p_refine_cells - 1,
411 indicators_refinement.
end(),
412 std::greater<Number>());
413 threshold_refinement =
414 *(indicators_refinement.
begin() + n_p_refine_cells - 1);
417 if (n_p_coarsen_cells == 0)
418 reference_compare_coarsen = std::cref(compare_false);
419 else if (n_p_coarsen_cells == n_flags_coarsening)
420 reference_compare_coarsen = std::cref(compare_true);
423 std::nth_element(indicators_coarsening.
begin(),
424 indicators_coarsening.
begin() +
425 n_p_coarsen_cells - 1,
426 indicators_coarsening.
end(),
427 std::less<Number>());
428 threshold_coarsening =
429 *(indicators_coarsening.
begin() + n_p_coarsen_cells - 1);
436 threshold_refinement,
437 threshold_coarsening,
438 std::cref(reference_compare_refine),
440 reference_compare_coarsen));
445 template <
int dim,
typename Number,
int spacedim>
457 sobolev_indices.
size());
460 if (cell->is_locally_owned())
462 if (cell->refine_flag_set())
464 const unsigned int super_fe_index =
466 cell->active_fe_index());
469 if (super_fe_index != cell->active_fe_index())
471 const unsigned int super_fe_degree =
474 if (sobolev_indices[cell->active_cell_index()] >
476 cell->set_future_fe_index(super_fe_index);
479 else if (cell->coarsen_flag_set())
481 const unsigned int sub_fe_index =
483 cell->active_fe_index());
486 if (sub_fe_index != cell->active_fe_index())
488 const unsigned int sub_fe_degree =
491 if (sobolev_indices[cell->active_cell_index()] <
493 cell->set_future_fe_index(sub_fe_index);
501 template <
int dim,
typename Number,
int spacedim>
523 std::vector<bool> p_flags(
527 if (cell->is_locally_owned() &&
528 ((cell->refine_flag_set() &&
529 compare_refine(criteria[cell->active_cell_index()],
530 references[cell->active_cell_index()])) ||
531 (cell->coarsen_flag_set() &&
532 compare_coarsen(criteria[cell->active_cell_index()],
533 references[cell->active_cell_index()]))))
534 p_flags[cell->active_cell_index()] =
true;
544 template <
int dim,
typename Number,
int spacedim>
549 const double gamma_p,
550 const double gamma_h,
551 const double gamma_n)
558 error_indicators.
size());
560 predicted_errors.
size());
561 Assert(0 < gamma_p && gamma_p < 1,
571 std::map<typename DoFHandler<dim, spacedim>::cell_iterator,
unsigned int>
572 future_fe_indices_on_coarsened_cells;
575 predicted_errors = error_indicators;
581 if (!(cell->future_fe_index_set()) && !(cell->refine_flag_set()) &&
582 !(cell->coarsen_flag_set()))
584 predicted_errors[cell->active_cell_index()] *= gamma_n;
590 if (cell->coarsen_flag_set())
593 ExcMessage(
"A coarse cell is flagged for coarsening. "
594 "Please read the note in the documentation "
595 "of predict_error()."));
599 const auto &parent = cell->parent();
600 if (future_fe_indices_on_coarsened_cells.find(parent) ==
601 future_fe_indices_on_coarsened_cells.end())
604 for (
const auto &child : parent->child_iterators())
605 Assert(child->is_active() && child->coarsen_flag_set(),
607 dim>::ExcInconsistentCoarseningFlags());
610 parent_future_fe_index =
611 internal::hp::DoFHandlerImplementation::
612 dominated_future_fe_on_children<dim, spacedim>(parent);
614 future_fe_indices_on_coarsened_cells.insert(
615 {parent, parent_future_fe_index});
619 parent_future_fe_index =
620 future_fe_indices_on_coarsened_cells[parent];
634 if (cell->future_fe_index_set())
636 predicted_errors[cell->active_cell_index()] *=
638 int(future_fe_degree) -
int(cell->get_fe().degree));
642 if (cell->refine_flag_set())
644 predicted_errors[cell->active_cell_index()] *=
645 (gamma_h *
std::pow(.5, future_fe_degree));
650 else if (cell->coarsen_flag_set())
652 predicted_errors[cell->active_cell_index()] /=
653 (gamma_h *
std::pow(.5, future_fe_degree));
666 template <
int dim,
int spacedim>
678 if (cell->is_locally_owned() && cell->future_fe_index_set())
680 cell->clear_refine_flag();
681 cell->clear_coarsen_flag();
687 template <
int dim,
int spacedim>
707 if (cell->is_locally_owned() && cell->future_fe_index_set())
709 cell->clear_refine_flag();
715 if (cell->coarsen_flag_set())
717 if (cell->level() == 0)
722 cell->clear_coarsen_flag();
726 const auto & parent = cell->parent();
727 const unsigned int n_children = parent->n_children();
729 unsigned int h_flagged_children = 0, p_flagged_children = 0;
730 for (
const auto &child : parent->child_iterators())
732 if (child->is_active())
734 if (child->is_locally_owned())
736 if (child->coarsen_flag_set())
737 ++h_flagged_children;
738 if (child->future_fe_index_set())
739 ++p_flagged_children;
741 else if (child->is_ghost())
747 (
dynamic_cast<const parallel::shared::
748 Triangulation<dim, spacedim> *
>(
752 if (child->coarsen_flag_set())
753 ++h_flagged_children;
759 if (internal::DoFCellAccessorImplementation::
761 future_fe_index_set<dim, spacedim, false>(
763 ++p_flagged_children;
774 if (h_flagged_children == n_children &&
775 p_flagged_children != n_children)
779 for (
const auto &child : parent->child_iterators())
784 if (child->is_locally_owned())
785 child->clear_future_fe_index();
792 for (
const auto &child : parent->child_iterators())
794 if (child->is_active() && child->is_locally_owned())
795 child->clear_coarsen_flag();
807 template <
int dim,
int spacedim>
810 const unsigned int max_difference,
811 const unsigned int contains_fe_index)
822 "This function does not serve any purpose for max_difference = 0."));
834 const auto invalid_level =
static_cast<level_type
>(-1);
838 const std::vector<unsigned int> fe_index_for_hierarchy_level =
845 std::vector<unsigned int> hierarchy_level_for_fe_index(
847 for (
unsigned int l = 0; l < fe_index_for_hierarchy_level.size(); ++l)
848 hierarchy_level_for_fe_index[fe_index_for_hierarchy_level[l]] = l;
861 if (
const auto parallel_tria =
866 parallel_tria->global_active_cell_index_partitioner().lock());
876 future_levels[cell->global_active_cell_index()] =
877 hierarchy_level_for_fe_index[cell->future_fe_index()];
892 const auto update_neighbor_level =
893 [&future_levels, max_difference, invalid_level](
894 const auto &neighbor,
const level_type cell_level) ->
bool {
899 if (neighbor->is_locally_owned())
901 const level_type neighbor_level =
static_cast<level_type
>(
902 future_levels[neighbor->global_active_cell_index()]);
905 if (neighbor_level == invalid_level)
908 if ((cell_level - max_difference) > neighbor_level)
910 future_levels[neighbor->global_active_cell_index()] =
911 cell_level - max_difference;
935 const auto prepare_level_for_parent = [&](
const auto &neighbor) ->
bool {
937 if (neighbor->coarsen_flag_set() && neighbor->is_locally_owned())
939 const auto parent = neighbor->parent();
941 std::vector<unsigned int> future_levels_children;
942 future_levels_children.reserve(parent->n_children());
943 for (
const auto &child : parent->child_iterators())
945 Assert(child->is_active() && child->coarsen_flag_set(),
947 ExcInconsistentCoarseningFlags()));
949 const level_type child_level =
static_cast<level_type
>(
950 future_levels[child->global_active_cell_index()]);
951 Assert(child_level != invalid_level,
953 "The FiniteElement on one of the siblings of "
954 "a cell you are trying to coarsen is not part "
955 "of the registered p-adaptation hierarchy."));
956 future_levels_children.push_back(child_level);
960 const unsigned int max_level_children =
961 *std::max_element(future_levels_children.begin(),
962 future_levels_children.end());
964 bool children_changed =
false;
965 for (
const auto &child : parent->child_iterators())
969 if (child->is_locally_owned() &&
970 future_levels[child->global_active_cell_index()] !=
973 future_levels[child->global_active_cell_index()] =
975 children_changed =
true;
977 return children_changed;
983 bool levels_changed =
false;
984 bool levels_changed_in_cycle;
987 levels_changed_in_cycle =
false;
992 if (!cell->is_artificial())
994 const level_type cell_level =
static_cast<level_type
>(
995 future_levels[cell->global_active_cell_index()]);
998 if (cell_level == invalid_level)
1003 if (cell_level <= max_difference)
1006 for (
unsigned int f = 0; f < cell->n_faces(); ++f)
1007 if (cell->face(f)->at_boundary() ==
false)
1009 if (cell->face(f)->has_children())
1011 for (
unsigned int sf = 0;
1012 sf < cell->face(f)->n_children();
1015 const auto neighbor =
1016 cell->neighbor_child_on_subface(f, sf);
1018 levels_changed_in_cycle |=
1019 update_neighbor_level(neighbor, cell_level);
1021 levels_changed_in_cycle |=
1022 prepare_level_for_parent(neighbor);
1027 const auto neighbor = cell->neighbor(f);
1029 levels_changed_in_cycle |=
1030 update_neighbor_level(neighbor, cell_level);
1032 levels_changed_in_cycle |=
1033 prepare_level_for_parent(neighbor);
1038 levels_changed_in_cycle =
1041 levels_changed |= levels_changed_in_cycle;
1043 while (levels_changed_in_cycle);
1049 const level_type cell_level =
static_cast<level_type
>(
1050 future_levels[cell->global_active_cell_index()]);
1052 if (cell_level != invalid_level)
1054 const unsigned int fe_index =
1055 fe_index_for_hierarchy_level[cell_level];
1058 if (fe_index != cell->active_fe_index())
1059 cell->set_future_fe_index(fe_index);
1063 return levels_changed;
1070#include "refinement.inst"
const hp::FECollection< dim, spacedim > & get_fe_collection() const
const Triangulation< dim, spacedim > & get_triangulation() const
bool has_hp_capabilities() const
MPI_Comm get_communicator() const
void update_ghost_values() const
void reinit(const size_type size, const bool omit_zeroing_entries=false)
unsigned int n_active_cells() const
void grow_or_shrink(const size_type N)
unsigned int size() const
unsigned int previous_in_hierarchy(const unsigned int fe_index) const
std::vector< unsigned int > get_hierarchy_sequence(const unsigned int fe_index=0) const
unsigned int next_in_hierarchy(const unsigned int fe_index) const
virtual MPI_Comm get_communicator() const override
#define DEAL_II_NAMESPACE_OPEN
#define DEAL_II_NAMESPACE_CLOSE
IteratorRange< active_cell_iterator > active_cell_iterators() const
IteratorRange< active_cell_iterator > active_cell_iterators() const
#define Assert(cond, exc)
#define AssertDimension(dim1, dim2)
static ::ExceptionBase & ExcInvalidParameterValue()
#define AssertIndexRange(index, range)
static ::ExceptionBase & ExcInternalError()
static ::ExceptionBase & ExcMessage(std::string arg1)
T sum(const T &t, const MPI_Comm mpi_communicator)
T logical_or(const T &t, const MPI_Comm mpi_communicator)
T max(const T &t, const MPI_Comm mpi_communicator)
T min(const T &t, const MPI_Comm mpi_communicator)
void force_p_over_h(const DoFHandler< dim, spacedim > &dof_handler)
void p_adaptivity_from_reference(const DoFHandler< dim, spacedim > &dof_handler, const Vector< Number > &criteria, const Vector< Number > &references, const ComparisonFunction< std_cxx20::type_identity_t< Number > > &compare_refine, const ComparisonFunction< std_cxx20::type_identity_t< Number > > &compare_coarsen)
void full_p_adaptivity(const DoFHandler< dim, spacedim > &dof_handler)
void p_adaptivity_from_regularity(const DoFHandler< dim, spacedim > &dof_handler, const Vector< Number > &sobolev_indices)
bool limit_p_level_difference(const DoFHandler< dim, spacedim > &dof_handler, const unsigned int max_difference=1, const unsigned int contains_fe_index=0)
void predict_error(const DoFHandler< dim, spacedim > &dof_handler, const Vector< Number > &error_indicators, Vector< Number > &predicted_errors, const double gamma_p=std::sqrt(0.4), const double gamma_h=2., const double gamma_n=1.)
void p_adaptivity_from_absolute_threshold(const DoFHandler< dim, spacedim > &dof_handler, const Vector< Number > &criteria, const Number p_refine_threshold, const Number p_coarsen_threshold, const ComparisonFunction< std_cxx20::type_identity_t< Number > > &compare_refine=std::greater_equal< Number >(), const ComparisonFunction< std_cxx20::type_identity_t< Number > > &compare_coarsen=std::less_equal< Number >())
void choose_p_over_h(const DoFHandler< dim, spacedim > &dof_handler)
void p_adaptivity_fixed_number(const DoFHandler< dim, spacedim > &dof_handler, const Vector< Number > &criteria, const double p_refine_fraction=0.5, const double p_coarsen_fraction=0.5, const ComparisonFunction< std_cxx20::type_identity_t< Number > > &compare_refine=std::greater_equal< Number >(), const ComparisonFunction< std_cxx20::type_identity_t< Number > > &compare_coarsen=std::less_equal< Number >())
std::function< bool(const Number &, const Number &)> ComparisonFunction
void p_adaptivity_from_relative_threshold(const DoFHandler< dim, spacedim > &dof_handler, const Vector< Number > &criteria, const double p_refine_fraction=0.5, const double p_coarsen_fraction=0.5, const ComparisonFunction< std_cxx20::type_identity_t< Number > > &compare_refine=std::greater_equal< Number >(), const ComparisonFunction< std_cxx20::type_identity_t< Number > > &compare_coarsen=std::less_equal< Number >())
void p_adaptivity_from_flags(const DoFHandler< dim, spacedim > &dof_handler, const std::vector< bool > &p_flags)
void communicate_future_fe_indices(DoFHandler< dim, spacedim > &dof_handler)
number compute_threshold(const ::Vector< number > &criteria, const std::pair< double, double > &global_min_and_max, const types::global_cell_index n_target_cells, const MPI_Comm mpi_communicator)
std::pair< number, number > compute_global_min_and_max_at_root(const ::Vector< number > &criteria, const MPI_Comm mpi_communicator)
static const unsigned int invalid_unsigned_int
typename type_identity< T >::type type_identity_t
::VectorizedArray< Number, width > min(const ::VectorizedArray< Number, width > &, const ::VectorizedArray< Number, width > &)
::VectorizedArray< Number, width > max(const ::VectorizedArray< Number, width > &, const ::VectorizedArray< Number, width > &)
::VectorizedArray< Number, width > pow(const ::VectorizedArray< Number, width > &, const Number p)
unsigned short int fe_index