From: Peter Munch Date: Thu, 2 Jan 2020 17:16:18 +0000 (+0100) Subject: Add CellId::is_ancestor_of X-Git-Tag: v9.2.0-rc1~750^2 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F9214%2Fhead;p=dealii.git Add CellId::is_ancestor_of --- diff --git a/doc/news/changes/minor/20200102PeterMunch b/doc/news/changes/minor/20200102PeterMunch new file mode 100644 index 0000000000..e2997f3179 --- /dev/null +++ b/doc/news/changes/minor/20200102PeterMunch @@ -0,0 +1,3 @@ +New: Add method is_ancestor_of to CellId. +
+(Peter Munch, 2020/01/02) diff --git a/include/deal.II/grid/cell_id.h b/include/deal.II/grid/cell_id.h index 721e51b416..f628f0b920 100644 --- a/include/deal.II/grid/cell_id.h +++ b/include/deal.II/grid/cell_id.h @@ -171,6 +171,12 @@ public: bool is_parent_of(const CellId &other) const; + /** + * Determine if this cell id is the ancestor of the input cell id. + */ + bool + is_ancestor_of(const CellId &other) const; + /** * Boost serialization function */ @@ -350,6 +356,24 @@ CellId::is_parent_of(const CellId &other) const } + +inline bool +CellId::is_ancestor_of(const CellId &other) const +{ + if (this->coarse_cell_id != other.coarse_cell_id) + return false; + + if (n_child_indices >= other.n_child_indices) + return false; + + for (unsigned int idx = 0; idx < n_child_indices; ++idx) + if (child_indices[idx] != other.child_indices[idx]) + return false; + + return true; // other.id is longer +} + + inline types::coarse_cell_id CellId::get_coarse_cell_id() const { diff --git a/tests/grid/cell_id_06.cc b/tests/grid/cell_id_06.cc index f1b5354770..091563cad0 100644 --- a/tests/grid/cell_id_06.cc +++ b/tests/grid/cell_id_06.cc @@ -14,7 +14,7 @@ // --------------------------------------------------------------------- -// testing parent and child relationship of CellIds +// testing parent/ancestor and child relationship of CellIds #include @@ -32,19 +32,16 @@ main() CellId id3(0, {0, 0, 0}); CellId id4(1, {0}); - // same cell (expected: false) deallog << std::boolalpha; - deallog << id0.is_parent_of(id0) << std::endl; - // same level (false) - deallog << id0.is_parent_of(id1) << std::endl; - - // child (true) - deallog << id0.is_parent_of(id2) << std::endl; - - // grand child (false) - deallog << id0.is_parent_of(id3) << std::endl; - - // cell with different coarse-cell id (false) - deallog << id0.is_parent_of(id4) << std::endl; + for (const auto &pair : + std::vector>{ + {id0, id0}, // same cell (expected: false, false) + {id0, id1}, // same level (false, false) + {id0, id2}, // child (true, true) + {id0, id3}, // grand child (false, true) + {id0, id4}} // cell with different coarse-cell id (false, false) + ) + deallog << pair.first.is_parent_of(pair.second) << " " + << pair.first.is_ancestor_of(pair.second) << std::endl; } diff --git a/tests/grid/cell_id_06.output b/tests/grid/cell_id_06.output index 3c5613acaa..99b001189b 100644 --- a/tests/grid/cell_id_06.output +++ b/tests/grid/cell_id_06.output @@ -1,6 +1,6 @@ -DEAL::false -DEAL::false -DEAL::true -DEAL::false -DEAL::false +DEAL::false false +DEAL::false false +DEAL::true true +DEAL::false true +DEAL::false false