From: Marc Fehling Date: Tue, 6 Oct 2020 01:31:06 +0000 (-0600) Subject: Added cell->child_iterators(). X-Git-Tag: v9.3.0-rc1~1026^2 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F11000%2Fhead;p=dealii.git Added cell->child_iterators(). --- diff --git a/doc/news/changes/minor/20201003Fehling b/doc/news/changes/minor/20201003Fehling new file mode 100644 index 0000000000..42ca8ad293 --- /dev/null +++ b/doc/news/changes/minor/20201003Fehling @@ -0,0 +1,5 @@ +New: Helper functions CellAccessor::child_iterators() and +DoFCellAccessor::child_iterators() which return iterators to children of +a cell via `cell->child_iterators()`. +
+(Marc Fehling, 2020/10/03) diff --git a/include/deal.II/dofs/dof_accessor.h b/include/deal.II/dofs/dof_accessor.h index 997e5befff..c382b74181 100644 --- a/include/deal.II/dofs/dof_accessor.h +++ b/include/deal.II/dofs/dof_accessor.h @@ -1470,6 +1470,15 @@ public: TriaIterator> child(const unsigned int i) const; + /** + * Return an array of iterators to all children of this cell. + */ + boost::container::small_vector< + TriaIterator< + DoFCellAccessor>, + GeometryInfo::max_children_per_cell> + child_iterators() const; + /** * Return an iterator to the @p ith face of this cell. * diff --git a/include/deal.II/dofs/dof_accessor.templates.h b/include/deal.II/dofs/dof_accessor.templates.h index 0a31333faa..3284b305fb 100644 --- a/include/deal.II/dofs/dof_accessor.templates.h +++ b/include/deal.II/dofs/dof_accessor.templates.h @@ -2445,6 +2445,26 @@ DoFCellAccessor::child( } +template +inline boost::container::small_vector< + TriaIterator>, + GeometryInfo::max_children_per_cell> +DoFCellAccessor:: + child_iterators() const +{ + boost::container::small_vector< + TriaIterator< + DoFCellAccessor>, + GeometryInfo::max_children_per_cell> + child_iterators(this->n_children()); + + for (unsigned int i = 0; i < this->n_children(); ++i) + child_iterators[i] = this->child(i); + + return child_iterators; +} + + template inline TriaIterator< DoFCellAccessor> diff --git a/include/deal.II/grid/tria_accessor.h b/include/deal.II/grid/tria_accessor.h index 3af0cabae7..0cedfaf582 100644 --- a/include/deal.II/grid/tria_accessor.h +++ b/include/deal.II/grid/tria_accessor.h @@ -2838,6 +2838,13 @@ public: TriaIterator> child(const unsigned int i) const; + /** + * Return an array of iterators to all children of this cell. + */ + boost::container::small_vector>, + GeometryInfo::max_children_per_cell> + child_iterators() const; + /** * Return an iterator to the @p ith face of this cell. */ diff --git a/include/deal.II/grid/tria_accessor.templates.h b/include/deal.II/grid/tria_accessor.templates.h index e38575a64a..4ab14d9b6a 100644 --- a/include/deal.II/grid/tria_accessor.templates.h +++ b/include/deal.II/grid/tria_accessor.templates.h @@ -3159,6 +3159,23 @@ CellAccessor::child(const unsigned int i) const +template +inline boost::container::small_vector>, + GeometryInfo::max_children_per_cell> +CellAccessor::child_iterators() const +{ + boost::container::small_vector>, + GeometryInfo::max_children_per_cell> + child_iterators(this->n_children()); + + for (unsigned int i = 0; i < this->n_children(); ++i) + child_iterators[i] = this->child(i); + + return child_iterators; +} + + + template inline TriaIterator> CellAccessor::face(const unsigned int i) const diff --git a/include/deal.II/numerics/cell_data_transfer.templates.h b/include/deal.II/numerics/cell_data_transfer.templates.h index 91deef8173..14dde6d97e 100644 --- a/include/deal.II/numerics/cell_data_transfer.templates.h +++ b/include/deal.II/numerics/cell_data_transfer.templates.h @@ -105,11 +105,8 @@ CellDataTransfer:: coarsened_cells_active_index.end()) { std::set indices_children; - for (unsigned int child_index = 0; - child_index < parent->n_children(); - ++child_index) + for (const auto &sibling : parent->child_iterators()) { - const auto &sibling = parent->child(child_index); Assert(sibling->is_active() && sibling->coarsen_flag_set(), typename dealii::Triangulation< dim>::ExcInconsistentCoarseningFlags()); diff --git a/source/distributed/error_predictor.cc b/source/distributed/error_predictor.cc index d38da55a55..ef12125b81 100644 --- a/source/distributed/error_predictor.cc +++ b/source/distributed/error_predictor.cc @@ -258,12 +258,8 @@ namespace parallel float sqrsum_of_predicted_errors = 0.; float predicted_error = 0.; int degree_difference = 0; - for (unsigned int child_index = 0; - child_index < cell->n_children(); - ++child_index) + for (const auto &child : cell->child_iterators()) { - const auto child = cell->child(child_index); - predicted_error = (**estimated_error_it)[child->active_cell_index()] / (gamma_h * std::pow(.5, future_fe_degree)); @@ -336,10 +332,8 @@ namespace parallel case parallel::distributed::Triangulation::CELL_REFINE: - for (unsigned int child_index = 0; - child_index < cell->n_children(); - ++child_index) - (**it_output)[cell->child(child_index)->active_cell_index()] = + for (const auto &child : cell->child_iterators()) + (**it_output)[child->active_cell_index()] = (*it_input) / std::sqrt(cell->n_children()); break; diff --git a/source/dofs/dof_handler.cc b/source/dofs/dof_handler.cc index 1335bd6f4c..22a0c26f5a 100644 --- a/source/dofs/dof_handler.cc +++ b/source/dofs/dof_handler.cc @@ -1951,11 +1951,8 @@ namespace internal { const auto &parent = refine.first; - for (unsigned int child_index = 0; - child_index < parent->n_children(); - ++child_index) + for (const auto &child : parent->child_iterators()) { - const auto &child = parent->child(child_index); Assert(child->is_locally_owned() && child->is_active(), ExcInternalError()); child->set_active_fe_index(refine.second); diff --git a/source/hp/refinement.cc b/source/hp/refinement.cc index f287b9a9b1..3e80a37954 100644 --- a/source/hp/refinement.cc +++ b/source/hp/refinement.cc @@ -679,10 +679,8 @@ namespace hp const unsigned int n_children = parent->n_children(); unsigned int h_flagged_children = 0, p_flagged_children = 0; - for (unsigned int child_index = 0; child_index < n_children; - ++child_index) + for (const auto &child : parent->child_iterators()) { - const auto &child = parent->child(child_index); if (child->is_active()) { if (child->is_locally_owned()) @@ -716,10 +714,8 @@ namespace hp { // Perform pure h coarsening and // drop all p adaptation flags. - for (unsigned int child_index = 0; child_index < n_children; - ++child_index) + for (const auto &child : parent->child_iterators()) { - const auto &child = parent->child(child_index); // h_flagged_children == n_children implies // that all children are active Assert(child->is_active(), ExcInternalError()); @@ -731,10 +727,8 @@ namespace hp { // Perform p adaptation on all children and // drop all h coarsening flags. - for (unsigned int child_index = 0; child_index < n_children; - ++child_index) + for (const auto &child : parent->child_iterators()) { - const auto &child = parent->child(child_index); if (child->is_active() && child->is_locally_owned()) child->clear_coarsen_flag(); } diff --git a/source/numerics/solution_transfer.cc b/source/numerics/solution_transfer.cc index 9766483eeb..67c25059ae 100644 --- a/source/numerics/solution_transfer.cc +++ b/source/numerics/solution_transfer.cc @@ -357,10 +357,8 @@ SolutionTransfer:: // case. we choose the 'least dominant fe' on all children from // the associated FECollection. std::set fe_indices_children; - for (unsigned int child_index = 0; child_index < cell->n_children(); - ++child_index) + for (const auto &child : cell->child_iterators()) { - const auto &child = cell->child(child_index); Assert(child->is_active() && child->coarsen_flag_set(), typename dealii::Triangulation< dim>::ExcInconsistentCoarseningFlags()); diff --git a/source/particles/particle_handler.cc b/source/particles/particle_handler.cc index dda0e7b6c6..a304691ffc 100644 --- a/source/particles/particle_handler.cc +++ b/source/particles/particle_handler.cc @@ -1693,23 +1693,15 @@ namespace Particles { unsigned int n_particles = 0; - for (unsigned int child_index = 0; - child_index < GeometryInfo::max_children_per_cell; - ++child_index) + for (const auto &child : cell->child_iterators()) { - const typename Triangulation::cell_iterator - child = cell->child(child_index); n_particles += n_particles_in_cell(child); } stored_particles_on_cell.reserve(n_particles); - for (unsigned int child_index = 0; - child_index < GeometryInfo::max_children_per_cell; - ++child_index) + for (const auto &child : cell->child_iterators()) { - const typename Triangulation::cell_iterator - child = cell->child(child_index); const internal::LevelInd level_index = {child->level(), child->index()}; const auto particles_in_cell = diff --git a/tests/grid/accessor_03.cc b/tests/grid/accessor_03.cc new file mode 100644 index 0000000000..2e779af15e --- /dev/null +++ b/tests/grid/accessor_03.cc @@ -0,0 +1,74 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2020 by the deal.II authors +// +// This file is part of the deal.II library. +// +// The deal.II library is free software; you can use it, redistribute +// it, and/or modify it under the terms of the GNU Lesser General +// Public License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// The full text of the license can be found in the file LICENSE.md at +// the top level directory of deal.II. +// +// --------------------------------------------------------------------- + + + +// Verify functionality of CellAccessor::child_iterators() and +// DoFCellAccessor:child_iterators() + +#include + +#include +#include + +#include +#include +#include + +#include "../tests.h" + + + +template +void +test() +{ + Triangulation tria; + GridGenerator::hyper_cube(tria); + tria.refine_global(1); + + { + const auto &parent = tria.begin(/*level=*/0); + + for (const auto &child : parent->child_iterators()) + deallog << child->id().to_string() << std::endl; + } + + { + DoFHandler dofh(tria); + + const auto &parent = dofh.begin(/*level=*/0); + + for (const auto &child : parent->child_iterators()) + deallog << child->id().to_string() << std::endl; + } +} + + +int +main() +{ + initlog(); + + deallog.push("1d"); + test<1>(); + deallog.pop(); + deallog.push("2d"); + test<2>(); + deallog.pop(); + deallog.push("3d"); + test<3>(); + deallog.pop(); +} diff --git a/tests/grid/accessor_03.output b/tests/grid/accessor_03.output new file mode 100644 index 0000000000..649733cbf9 --- /dev/null +++ b/tests/grid/accessor_03.output @@ -0,0 +1,29 @@ + +DEAL:1d::0_1:0 +DEAL:1d::0_1:1 +DEAL:1d::0_1:0 +DEAL:1d::0_1:1 +DEAL:2d::0_1:0 +DEAL:2d::0_1:1 +DEAL:2d::0_1:2 +DEAL:2d::0_1:3 +DEAL:2d::0_1:0 +DEAL:2d::0_1:1 +DEAL:2d::0_1:2 +DEAL:2d::0_1:3 +DEAL:3d::0_1:0 +DEAL:3d::0_1:1 +DEAL:3d::0_1:2 +DEAL:3d::0_1:3 +DEAL:3d::0_1:4 +DEAL:3d::0_1:5 +DEAL:3d::0_1:6 +DEAL:3d::0_1:7 +DEAL:3d::0_1:0 +DEAL:3d::0_1:1 +DEAL:3d::0_1:2 +DEAL:3d::0_1:3 +DEAL:3d::0_1:4 +DEAL:3d::0_1:5 +DEAL:3d::0_1:6 +DEAL:3d::0_1:7