diff --git a/cpp/dolfinx/fem/DirichletBC.cpp b/cpp/dolfinx/fem/DirichletBC.cpp index e43658839f..fc8729f4d9 100644 --- a/cpp/dolfinx/fem/DirichletBC.cpp +++ b/cpp/dolfinx/fem/DirichletBC.cpp @@ -55,9 +55,17 @@ find_local_entity_index(const mesh::Topology& topology, std::vector> entity_indices; entity_indices.reserve(entities.size()); + std::int32_t num_entities_local = e_to_c->num_nodes(); for (std::int32_t e : entities) { // Get first attached cell + if (num_entities_local <= e) + { + throw std::runtime_error( + "Input entity " + std::to_string(e) + + " is bigger than the number of entities on this process (" + + std::to_string(num_entities_local) + ")."); + } assert(e_to_c->num_links(e) > 0); const int cell = e_to_c->links(e).front(); diff --git a/python/test/unit/fem/test_bcs.py b/python/test/unit/fem/test_bcs.py index 983cf9af3c..b2bcbb20f9 100644 --- a/python/test/unit/fem/test_bcs.py +++ b/python/test/unit/fem/test_bcs.py @@ -406,3 +406,16 @@ def test_blocked_dof_ownership(shape): owned_sub_dofs = boundary_dofs_V[boundary_dofs_V < num_owned_blocked * bs] assert len(owned_sub_dofs) == num_owned_sub assert len(unrolled_dofs_sub) == len(boundary_dofs_V) + + +def test_bc_index_out_of_range(): + mesh = create_unit_square(MPI.COMM_WORLD, 4, 4) + mesh.topology.create_connectivity(mesh.topology.dim - 1, mesh.topology.dim) + facet_map = mesh.topology.index_map(mesh.topology.dim - 1) + num_facets_on_proc = facet_map.size_local + facet_map.num_ghosts + facets = np.arange(num_facets_on_proc + 1, dtype=np.int32) + V = functionspace(mesh, ("Lagrange", 1)) + with pytest.raises( + RuntimeError, match=r".*is bigger than the number of entities on this process.*" + ): + locate_dofs_topological(V, mesh.topology.dim - 1, facets)