]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Add some functions to convert active cell iterators
authorJean-Paul Pelteret <jppelteret@gmail.com>
Fri, 15 Jul 2022 22:33:00 +0000 (00:33 +0200)
committerJean-Paul Pelteret <jppelteret@gmail.com>
Fri, 15 Jul 2022 23:03:41 +0000 (01:03 +0200)
doc/news/changes/minor/20220715Pelteret [new file with mode: 0644]
include/deal.II/dofs/dof_handler.h
tests/dofs/dof_iterators_01.cc [new file with mode: 0644]
tests/dofs/dof_iterators_01.output [new file with mode: 0644]
tests/dofs/dof_iterators_02.cc [new file with mode: 0644]
tests/dofs/dof_iterators_02.output [new file with mode: 0644]

diff --git a/doc/news/changes/minor/20220715Pelteret b/doc/news/changes/minor/20220715Pelteret
new file mode 100644 (file)
index 0000000..8cd72e4
--- /dev/null
@@ -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.
+<br>
+(Jean-Paul Pelteret, 2022/07/15)
index 87926405a4c696cafe7a78bf3c01e85558c2698b..f3beda6d13881c6167ddc0d898696f46390f33ed 100644 (file)
@@ -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 <int dim, int spacedim>
+typename DoFHandler<dim, spacedim>::active_cell_iterator
+convert_active_cell_iterator(
+  const typename Triangulation<dim, spacedim>::active_cell_iterator &iterator,
+  const DoFHandler<dim, spacedim> &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<dim, spacedim>::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 <int dim, int spacedim>
+typename DoFHandler<dim, spacedim>::active_cell_iterator
+convert_active_cell_iterator(
+  const typename DoFHandler<dim, spacedim>::active_cell_iterator &iterator,
+  const DoFHandler<dim, spacedim> &                               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<dim, spacedim>::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 (file)
index 0000000..862ab25
--- /dev/null
@@ -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 <deal.II/dofs/dof_handler.h>
+
+#include <deal.II/fe/fe_q.h>
+
+#include <deal.II/grid/grid_generator.h>
+#include <deal.II/grid/tria.h>
+
+#include "../tests.h"
+
+template <int dim, int spacedim>
+void
+test()
+{
+  Triangulation<dim, spacedim> triangulation;
+  GridGenerator::hyper_cube(triangulation);
+  triangulation.refine_global(2);
+  FE_Q<dim, spacedim>       fe(2);
+  DoFHandler<dim, spacedim> 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 (file)
index 0000000..0fd8fc1
--- /dev/null
@@ -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 (file)
index 0000000..98a1609
--- /dev/null
@@ -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 <deal.II/dofs/dof_handler.h>
+
+#include <deal.II/fe/fe_q.h>
+
+#include <deal.II/grid/grid_generator.h>
+#include <deal.II/grid/tria.h>
+
+#include "../tests.h"
+
+template <int dim, int spacedim>
+void
+test()
+{
+  Triangulation<dim, spacedim> triangulation;
+  GridGenerator::hyper_cube(triangulation);
+  triangulation.refine_global(2);
+  FE_Q<dim, spacedim>       fe_1(2);
+  FE_Q<dim, spacedim>       fe_2(3);
+  DoFHandler<dim, spacedim> dof_handler_1(triangulation);
+  DoFHandler<dim, spacedim> 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 (file)
index 0000000..0fd8fc1
--- /dev/null
@@ -0,0 +1,2 @@
+
+DEAL::OK

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.