--- /dev/null
+New: The function Triangulation::contains_cell()
+allows to check if Tringulation::create_cell_iterator()
+can be called for a specific cell id.
+<br>
+(Peter Munch, 2023/08/01)
* 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;
/** @} */
/**
*
* @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.
coarse_cell_id,
[](const std::pair<types::coarse_cell_id, unsigned int> &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
}
Triangulation<dim, spacedim>::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<unsigned int>(child_index));
+
+ return cell;
+}
+
+
+
+template <int dim, int spacedim>
+DEAL_II_CXX20_REQUIRES((concepts::is_valid_dim_spacedim<dim, spacedim>))
+bool Triangulation<dim, spacedim>::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<unsigned int>(child_index));
}
- return cell;
+ return true;
}