diff --git a/cpp/dolfinx/mesh/Topology.cpp b/cpp/dolfinx/mesh/Topology.cpp index 7e2d94ac351..0773aefbcfa 100644 --- a/cpp/dolfinx/mesh/Topology.cpp +++ b/cpp/dolfinx/mesh/Topology.cpp @@ -825,7 +825,14 @@ std::shared_ptr Topology::index_map(int dim) const if (_entity_types[dim].size() > 1) throw std::runtime_error( "Multiple index maps of this dimension. Call index_maps instead."); - return this->index_maps(dim).at(0); + auto im = index_maps(dim); + if (im.empty()) + { + throw std::runtime_error(std::format( + "Missing IndexMap in Topology. Maybe you need to create_entities({}).", + dim)); + } + return im.at(0); } //----------------------------------------------------------------------------- std::shared_ptr> diff --git a/python/dolfinx/mesh.py b/python/dolfinx/mesh.py index ac1ab105f77..a7bba1addb0 100644 --- a/python/dolfinx/mesh.py +++ b/python/dolfinx/mesh.py @@ -183,13 +183,7 @@ def index_map(self, dim: int) -> _cpp.common.IndexMap: Returns: Index map for the entities of dimension ``dim``. """ - if (imap := self._cpp_object.index_map(dim)) is not None: - return imap - else: - raise RuntimeError( - f"Entities of dimension {dim} has not been computed." - f"Call `dolfinx.mesh.Topology.create_entities({dim}) first." - ) + return self._cpp_object.index_map(dim) def index_maps(self, dim: int) -> list[_cpp.common.IndexMap]: """Get the IndexMaps that describes the parallel distribution of @@ -200,14 +194,9 @@ def index_maps(self, dim: int) -> list[_cpp.common.IndexMap]: Returns: List of IndexMaps for the entities of dimension ``dim``. + May be empty if not yet computed. """ - if (imaps := self._cpp_object.index_maps(dim)) is not None: - return imaps - else: - raise RuntimeError( - f"Entities of dimension {dim} have not been computed." - f"Call `dolfinx.mesh.Topology.create_entities({dim}) first." - ) + return self._cpp_object.index_maps(dim) def interprocess_facets(self) -> npt.NDArray[np.int32]: """List of inter-process facets, if facet topology has been