From: Peter Munch Date: Tue, 1 Aug 2023 16:52:48 +0000 (+0200) Subject: Inroduce Triangulation::contains_cell() X-Git-Tag: relicensing~618^2 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F15816%2Fhead;p=dealii.git Inroduce Triangulation::contains_cell() --- diff --git a/doc/news/changes/minor/20230801Munch b/doc/news/changes/minor/20230801Munch new file mode 100644 index 0000000000..f1df78a228 --- /dev/null +++ b/doc/news/changes/minor/20230801Munch @@ -0,0 +1,5 @@ +New: The function Triangulation::contains_cell() +allows to check if Tringulation::create_cell_iterator() +can be called for a specific cell id. +
+(Peter Munch, 2023/08/01) diff --git a/include/deal.II/grid/tria.h b/include/deal.II/grid/tria.h index d65345be1a..5dac93b8a2 100644 --- a/include/deal.II/grid/tria.h +++ b/include/deal.II/grid/tria.h @@ -2994,16 +2994,24 @@ public: * Return an iterator to a cell of this Triangulation object constructed from * an independent CellId object. * - * If the given argument corresponds to a valid cell in this triangulation, - * this operation will always succeed for sequential triangulations where the - * current processor stores all cells that are part of the triangulation. On - * the other hand, if this is a parallel triangulation, then the current - * processor may not actually know about this cell. In this case, this - * operation will succeed for locally relevant cells, but may not for - * artificial cells that are less refined on the current processor. + * @note See the documentation of contains_cell() about which CellId objects + * are valid. */ cell_iterator create_cell_iterator(const CellId &cell_id) const; + + /** + * Check if the triangulation contains a cell with the id @p cell_id. + * If the given argument corresponds to a valid cell in this triangulation, + * this operation will always return true for sequential triangulations where + * the current processor stores all cells that are part of the triangulation. + * On the other hand, if this is a parallel triangulation, then the current + * processor may not actually know about this cell. In this case, this + * operation will return true for locally relevant cells, but may return false + * for artificial cells that are less refined on the current processor. + */ + bool + contains_cell(const CellId &cell_id) const; /** @} */ /** @@ -4422,7 +4430,9 @@ private: * * @note For serial and shared triangulation both id and index are the same. * For distributed triangulations setting both might differ, since the - * id might correspond to a global id and the index to a local id. + * id might correspond to a global id and the index to a local id. If + * a cell does not exist locally, the returned value is + * numbers::invalid_unsigned_int. * * @param coarse_cell_id Unique id of the coarse cell. * @return Index of the coarse cell within the current triangulation. diff --git a/source/distributed/fully_distributed_tria.cc b/source/distributed/fully_distributed_tria.cc index 17e7c2ff24..e3a7d82a59 100644 --- a/source/distributed/fully_distributed_tria.cc +++ b/source/distributed/fully_distributed_tria.cc @@ -410,10 +410,11 @@ namespace parallel coarse_cell_id, [](const std::pair &pair, const types::coarse_cell_id &val) { return pair.first < val; }); - Assert(coarse_cell_index != - coarse_cell_id_to_coarse_cell_index_vector.cend(), - ExcMessage("Coarse cell index not found!")); - return coarse_cell_index->second; + if (coarse_cell_index != + coarse_cell_id_to_coarse_cell_index_vector.cend()) + return coarse_cell_index->second; + else + return numbers::invalid_unsigned_int; // cell could no be found } diff --git a/source/grid/tria.cc b/source/grid/tria.cc index 09a5748f2f..dee32a5eb3 100644 --- a/source/grid/tria.cc +++ b/source/grid/tria.cc @@ -14213,23 +14213,48 @@ typename Triangulation::cell_iterator Triangulation::create_cell_iterator( const CellId &cell_id) const { + Assert( + this->contains_cell(cell_id), + ExcMessage( + "CellId is invalid for this triangulation.\n" + "Either the provided CellId does not correspond to a cell in this " + "triangulation object, or, in case you are using a parallel " + "triangulation, may correspond to an artificial cell that is less " + "refined on this processor. In the case of " + "parallel::fullydistributed::Triangulation, the corresponding coarse " + "cell might not be accessible by the current process.")); + cell_iterator cell( this, 0, coarse_cell_id_to_coarse_cell_index(cell_id.get_coarse_cell_id())); + for (const auto &child_index : cell_id.get_child_indices()) + cell = cell->child(static_cast(child_index)); + + return cell; +} + + + +template +DEAL_II_CXX20_REQUIRES((concepts::is_valid_dim_spacedim)) +bool Triangulation::contains_cell(const CellId &cell_id) const +{ + const auto coarse_cell_index = + coarse_cell_id_to_coarse_cell_index(cell_id.get_coarse_cell_id()); + + if (coarse_cell_index == numbers::invalid_unsigned_int) + return false; + + cell_iterator cell(this, 0, coarse_cell_index); + for (const auto &child_index : cell_id.get_child_indices()) { - Assert( - cell->has_children(), - ExcMessage( - "CellId is invalid for this triangulation.\n" - "Either the provided CellId does not correspond to a cell in this " - "triangulation object, or, in case you are using a parallel " - "triangulation, may correspond to an artificial cell that is less " - "refined on this processor.")); + if (cell->has_children() == false) + return false; cell = cell->child(static_cast(child_index)); } - return cell; + return true; }