From: Peter Munch Date: Wed, 11 Sep 2019 19:00:27 +0000 (+0200) Subject: Add CellId::is_parent_of X-Git-Tag: v9.2.0-rc1~1118^2 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bf6f882b6c3586722f3cba96b73daf2d6e1b3adf;p=dealii.git Add CellId::is_parent_of --- diff --git a/doc/news/changes/minor/20190911PeterMunch b/doc/news/changes/minor/20190911PeterMunch new file mode 100644 index 0000000000..d06245dc46 --- /dev/null +++ b/doc/news/changes/minor/20190911PeterMunch @@ -0,0 +1,3 @@ +New: Add method is_parent_of to CellId. +
+(Peter Munch, 2019/09/11) diff --git a/include/deal.II/grid/cell_id.h b/include/deal.II/grid/cell_id.h index 169295818c..3787b97801 100644 --- a/include/deal.II/grid/cell_id.h +++ b/include/deal.II/grid/cell_id.h @@ -156,6 +156,12 @@ public: bool operator<(const CellId &other) const; + /** + * Determine if this cell id is the direct parent of the input cell id. + */ + bool + is_parent_of(const CellId &other) const; + /** * Boost serialization function */ @@ -317,6 +323,24 @@ CellId::operator<(const CellId &other) const } + +inline bool +CellId::is_parent_of(const CellId &other) const +{ + if (this->coarse_cell_id != other.coarse_cell_id) + return false; + + if (n_child_indices + 1 != 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 new file mode 100644 index 0000000000..f1b5354770 --- /dev/null +++ b/tests/grid/cell_id_06.cc @@ -0,0 +1,50 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2019 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. +// +// --------------------------------------------------------------------- + + +// testing parent and child relationship of CellIds + +#include + +#include "../tests.h" + +int +main() +{ + initlog(); + + // create some arbitrary CellIds + CellId id0(0, {0}); + CellId id1(0, {1}); + CellId id2(0, {0, 0}); + 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; +} diff --git a/tests/grid/cell_id_06.output b/tests/grid/cell_id_06.output new file mode 100644 index 0000000000..3c5613acaa --- /dev/null +++ b/tests/grid/cell_id_06.output @@ -0,0 +1,6 @@ + +DEAL::false +DEAL::false +DEAL::true +DEAL::false +DEAL::false