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 =
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 if (future_fe_degree > cell->get_fe().degree)
637 predicted_errors[cell->active_cell_index()] *=
639 future_fe_degree - cell->get_fe().degree);
640 else if (future_fe_degree < cell->get_fe().degree)
641 predicted_errors[cell->active_cell_index()] /=
643 cell->get_fe().degree - future_fe_degree);
652 if (cell->refine_flag_set())
654 predicted_errors[cell->active_cell_index()] *=
660 else if (cell->coarsen_flag_set())
662 predicted_errors[cell->active_cell_index()] /=
676 template <
int dim,
int spacedim>
688 if (cell->is_locally_owned() && cell->future_fe_index_set())
690 cell->clear_refine_flag();
691 cell->clear_coarsen_flag();
697 template <
int dim,
int spacedim>
726 if (cell->is_locally_owned() && cell->future_fe_index_set())
731 cell->clear_refine_flag();
737 if (cell->coarsen_flag_set())
739 if (cell->level() == 0)
744 cell->clear_coarsen_flag();
748 const auto &parent = cell->parent();
749 const unsigned int n_children = parent->n_children();
751 unsigned int h_flagged_children = 0, p_flagged_children = 0;
752 for (
const auto &child : parent->child_iterators())
754 if (child->is_active())
756 Assert(child->is_artificial() ==
false,
759 if (child->coarsen_flag_set())
760 ++h_flagged_children;
767 if (internal::DoFCellAccessorImplementation::
769 future_fe_index_set<dim, spacedim, false>(
771 ++p_flagged_children;
775 if (h_flagged_children == n_children &&
776 p_flagged_children != n_children)
780 for (
const auto &child : parent->child_iterators())
785 if (child->is_locally_owned())
786 child->clear_future_fe_index();
793 for (
const auto &child : parent->child_iterators())
795 if (child->is_active() && child->is_locally_owned())
796 child->clear_coarsen_flag();
808 template <
int dim,
int spacedim>
811 const unsigned int max_difference,
812 const unsigned int contains_fe_index)
823 "This function does not serve any purpose for max_difference = 0."));
835 const auto invalid_level =
static_cast<level_type
>(-1);
839 const std::vector<unsigned int> fe_index_for_hierarchy_level =
846 std::vector<unsigned int> hierarchy_level_for_fe_index(
848 for (
unsigned int l = 0; l < fe_index_for_hierarchy_level.size(); ++l)
849 hierarchy_level_for_fe_index[fe_index_for_hierarchy_level[l]] = l;
862 if (
const auto parallel_tria =
867 parallel_tria->global_active_cell_index_partitioner().lock());
877 future_levels[cell->global_active_cell_index()] =
878 hierarchy_level_for_fe_index[cell->future_fe_index()];
893 const auto update_neighbor_level =
894 [&future_levels, max_difference, invalid_level](
895 const auto &neighbor,
const level_type cell_level) ->
bool {
900 if (neighbor->is_locally_owned())
902 const level_type neighbor_level =
static_cast<level_type
>(
903 future_levels[neighbor->global_active_cell_index()]);
906 if (neighbor_level == invalid_level)
909 if ((cell_level - max_difference) > neighbor_level)
911 future_levels[neighbor->global_active_cell_index()] =
912 cell_level - max_difference;
936 const auto prepare_level_for_parent = [&](
const auto &neighbor) ->
bool {
938 if (neighbor->coarsen_flag_set() && neighbor->is_locally_owned())
940 const auto parent = neighbor->parent();
942 std::vector<unsigned int> future_levels_children;
943 future_levels_children.reserve(parent->n_children());
944 for (
const auto &child : parent->child_iterators())
946 Assert(child->is_active() && child->coarsen_flag_set(),
948 ExcInconsistentCoarseningFlags()));
950 const level_type child_level =
static_cast<level_type
>(
951 future_levels[child->global_active_cell_index()]);
952 Assert(child_level != invalid_level,
954 "The FiniteElement on one of the siblings of "
955 "a cell you are trying to coarsen is not part "
956 "of the registered p-adaptation hierarchy."));
957 future_levels_children.push_back(child_level);
961 const unsigned int max_level_children =
962 *std::max_element(future_levels_children.begin(),
963 future_levels_children.end());
965 bool children_changed =
false;
966 for (
const auto &child : parent->child_iterators())
970 if (child->is_locally_owned() &&
971 future_levels[child->global_active_cell_index()] !=
974 future_levels[child->global_active_cell_index()] =
976 children_changed =
true;
978 return children_changed;
984 bool levels_changed =
false;
985 bool levels_changed_in_cycle;
988 levels_changed_in_cycle =
false;
993 if (!cell->is_artificial())
995 const level_type cell_level =
static_cast<level_type
>(
996 future_levels[cell->global_active_cell_index()]);
999 if (cell_level == invalid_level)
1004 if (cell_level <= max_difference)
1007 for (
unsigned int f = 0; f < cell->n_faces(); ++f)
1008 if (cell->face(f)->at_boundary() ==
false)
1010 if (cell->face(f)->has_children())
1012 for (
unsigned int sf = 0;
1013 sf < cell->face(f)->n_children();
1016 const auto neighbor =
1017 cell->neighbor_child_on_subface(f, sf);
1019 levels_changed_in_cycle |=
1020 update_neighbor_level(neighbor, cell_level);
1022 levels_changed_in_cycle |=
1023 prepare_level_for_parent(neighbor);
1028 const auto neighbor = cell->neighbor(f);
1030 levels_changed_in_cycle |=
1031 update_neighbor_level(neighbor, cell_level);
1033 levels_changed_in_cycle |=
1034 prepare_level_for_parent(neighbor);
1039 levels_changed_in_cycle =
1042 levels_changed |= levels_changed_in_cycle;
1044 while (levels_changed_in_cycle);
1050 const level_type cell_level =
static_cast<level_type
>(
1051 future_levels[cell->global_active_cell_index()]);
1053 if (cell_level != invalid_level)
1055 const unsigned int fe_index =
1056 fe_index_for_hierarchy_level[cell_level];
1058 if (fe_index != cell->active_fe_index())
1059 cell->set_future_fe_index(fe_index);
1061 cell->clear_future_fe_index();
1065 return levels_changed;
1072#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
virtual size_type size() const override
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)
#define DEAL_II_ASSERT_UNREACHABLE()
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)
constexpr T pow(const T base, const int iexp)
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)
unsigned int dominated_future_fe_on_children(const typename DoFHandler< dim, spacedim >::cell_iterator &parent)
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)
void exchange_refinement_flags(::parallel::distributed::Triangulation< dim, spacedim > &tria)
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 > &)
unsigned short int fe_index