From 26843c8cbc3baaba0b502ec3515dbc2fcd6a73fa Mon Sep 17 00:00:00 2001 From: Jean-Paul Pelteret Date: Sat, 16 Jul 2022 00:33:00 +0200 Subject: [PATCH] Add some functions to convert active cell iterators --- doc/news/changes/minor/20220715Pelteret | 5 ++ include/deal.II/dofs/dof_handler.h | 60 ++++++++++++++++++++ tests/dofs/dof_iterators_01.cc | 70 ++++++++++++++++++++++++ tests/dofs/dof_iterators_01.output | 2 + tests/dofs/dof_iterators_02.cc | 73 +++++++++++++++++++++++++ tests/dofs/dof_iterators_02.output | 2 + 6 files changed, 212 insertions(+) create mode 100644 doc/news/changes/minor/20220715Pelteret create mode 100644 tests/dofs/dof_iterators_01.cc create mode 100644 tests/dofs/dof_iterators_01.output create mode 100644 tests/dofs/dof_iterators_02.cc create mode 100644 tests/dofs/dof_iterators_02.output diff --git a/doc/news/changes/minor/20220715Pelteret b/doc/news/changes/minor/20220715Pelteret new file mode 100644 index 0000000000..8cd72e453a --- /dev/null +++ b/doc/news/changes/minor/20220715Pelteret @@ -0,0 +1,5 @@ +New: The convert_active_cell_iterator() function can be used to convert from a +Triangulation active cell iterator to a DoFHandler active cell iterator, or +from an active cell iterator of one DoFHandler to that of another DoFHandler. +
+(Jean-Paul Pelteret, 2022/07/15) diff --git a/include/deal.II/dofs/dof_handler.h b/include/deal.II/dofs/dof_handler.h index 87926405a4..f3beda6d13 100644 --- a/include/deal.II/dofs/dof_handler.h +++ b/include/deal.II/dofs/dof_handler.h @@ -1674,6 +1674,66 @@ private: #endif }; + +/** + * A function that converts a Triangulation active cell iterator to a + * DoFHandler active cell iterator. The triangulation @p iterator must be + * associated with the triangulation of the @p dof_handler. + * + * @param iterator The input iterator to be converted. + * @param dof_handler The DoFHandler for the output active cell iterator. + * @return An active cell iterator for the @p dof_handler, matching the cell + * referenced by the input triangulation @p iterator. + */ +template +typename DoFHandler::active_cell_iterator +convert_active_cell_iterator( + const typename Triangulation::active_cell_iterator &iterator, + const DoFHandler &dof_handler) +{ + Assert( + &iterator->get_triangulation() == &dof_handler.get_triangulation(), + ExcMessage( + "The triangulation associated with the iterator does not match that of the dof_handler.")); + + return typename DoFHandler::active_cell_iterator( + &dof_handler.get_triangulation(), + iterator->level(), + iterator->index(), + &dof_handler); +} + + +/** + * A function that converts a DoFHandler active cell iterator to an active + * cell iterator of another DoFHandler. The degree-of-freedom handler to which + * @p iterator is associated must have the same triangulation as the + * second @p dof_handler. + * + * @param iterator The input iterator to be converted. + * @param dof_handler The DoFHandler for the output active cell iterator. + * @return An active cell iterator for the @p dof_handler, matching the cell + * referenced by the input @p iterator for another DoFHandler. + */ +template +typename DoFHandler::active_cell_iterator +convert_active_cell_iterator( + const typename DoFHandler::active_cell_iterator &iterator, + const DoFHandler & dof_handler) +{ + Assert( + &iterator->get_triangulation() == &dof_handler.get_triangulation(), + ExcMessage( + "The triangulation associated with the iterator does not match that of the dof_handler.")); + + return typename DoFHandler::active_cell_iterator( + &dof_handler.get_triangulation(), + iterator->level(), + iterator->index(), + &dof_handler); +} + + namespace internal { namespace hp diff --git a/tests/dofs/dof_iterators_01.cc b/tests/dofs/dof_iterators_01.cc new file mode 100644 index 0000000000..862ab254cf --- /dev/null +++ b/tests/dofs/dof_iterators_01.cc @@ -0,0 +1,70 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2022 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. +// +// --------------------------------------------------------------------- + + + +// check that conversion between Triangulation iterator and DoFHandler iterator +// works + + +#include + +#include + +#include +#include + +#include "../tests.h" + +template +void +test() +{ + Triangulation triangulation; + GridGenerator::hyper_cube(triangulation); + triangulation.refine_global(2); + FE_Q fe(2); + DoFHandler dof_handler(triangulation); + dof_handler.distribute_dofs(fe); + + for (const auto &it_tria : triangulation.active_cell_iterators()) + { + const auto it_dh = convert_active_cell_iterator(it_tria, dof_handler); + Assert(it_tria->level() == it_dh->level(), + ExcMessage("Iterator conversion failed: Level.")); + Assert(it_tria->index() == it_dh->index(), + ExcMessage("Iterator conversion failed: Index.")); + Assert(it_tria->id() == it_dh->id(), + ExcMessage("Iterator conversion failed: Id.")); + } +} + + + +int +main() +{ + initlog(); + + test<1, 1>(); + test<1, 2>(); + test<2, 2>(); + test<2, 3>(); + test<3, 3>(); + + deallog << "OK" << std::endl; + + return 0; +} diff --git a/tests/dofs/dof_iterators_01.output b/tests/dofs/dof_iterators_01.output new file mode 100644 index 0000000000..0fd8fc12f0 --- /dev/null +++ b/tests/dofs/dof_iterators_01.output @@ -0,0 +1,2 @@ + +DEAL::OK diff --git a/tests/dofs/dof_iterators_02.cc b/tests/dofs/dof_iterators_02.cc new file mode 100644 index 0000000000..98a1609b1d --- /dev/null +++ b/tests/dofs/dof_iterators_02.cc @@ -0,0 +1,73 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2022 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. +// +// --------------------------------------------------------------------- + + + +// check that conversion DoFHandler iterator to another DoFHandler iterator +// works + + +#include + +#include + +#include +#include + +#include "../tests.h" + +template +void +test() +{ + Triangulation triangulation; + GridGenerator::hyper_cube(triangulation); + triangulation.refine_global(2); + FE_Q fe_1(2); + FE_Q fe_2(3); + DoFHandler dof_handler_1(triangulation); + DoFHandler dof_handler_2(triangulation); + dof_handler_1.distribute_dofs(fe_1); + dof_handler_2.distribute_dofs(fe_2); + + for (const auto &it_dh_1 : dof_handler_1.active_cell_iterators()) + { + const auto it_dh_2 = convert_active_cell_iterator(it_dh_1, dof_handler_2); + Assert(it_dh_1->level() == it_dh_2->level(), + ExcMessage("Iterator conversion failed: Level.")); + Assert(it_dh_1->index() == it_dh_2->index(), + ExcMessage("Iterator conversion failed: Index.")); + Assert(it_dh_1->id() == it_dh_2->id(), + ExcMessage("Iterator conversion failed: Id.")); + } +} + + + +int +main() +{ + initlog(); + + test<1, 1>(); + test<1, 2>(); + test<2, 2>(); + test<2, 3>(); + test<3, 3>(); + + deallog << "OK" << std::endl; + + return 0; +} diff --git a/tests/dofs/dof_iterators_02.output b/tests/dofs/dof_iterators_02.output new file mode 100644 index 0000000000..0fd8fc12f0 --- /dev/null +++ b/tests/dofs/dof_iterators_02.output @@ -0,0 +1,2 @@ + +DEAL::OK -- 2.39.5