PETSC ERROR:Segmentation Violation, probably memory access out of range

Hi, I seem to meet same problem with PETSC ERROR when running in mpi. When I change MPI.COMM_WORLD parameter to MPI.COMM_SELF, it still can not work.

from mpi4py import MPI
from petsc4py.PETSc import ScalarType  
import numpy as np
import ufl
from dolfinx import fem, io, mesh, plot
from dolfinx.fem.petsc import LinearProblem
from ufl import ds, dx, grad, inner

msh = mesh.create_box(comm=MPI.COMM_SELF, points=((0, 0, 0), (1.0, 1.0, 1.0)), n=(10, 10, 10), cell_type=mesh.CellType.tetrahedron)
V = fem.functionspace(msh, ("Lagrange", 1))


marker_condition = lambda x: np.logical_or.reduce((np.isclose(x[0], 0), np.isclose(x[0], 1.0),
                                                    np.isclose(x[1], 0), np.isclose(x[1], 1.0),
                                                    np.isclose(x[2], 0), np.isclose(x[2], 1.0)))

facets = mesh.locate_entities_boundary(msh, dim=(msh.topology.dim - 1), marker=marker_condition)

dofs = fem.locate_dofs_topological(V=V, entity_dim=1, entities=facets)

bc = fem.dirichletbc(value=ScalarType(0), dofs=dofs, V=V)

u = ufl.TrialFunction(V)
v = ufl.TestFunction(V)
x = ufl.SpatialCoordinate(msh)
f = 10 * ufl.exp(-((x[0] - 0.5) ** 2 + (x[1] - 0.5) ** 2 + (x[2] - 0.5) ** 2) / 0.02)
g = ufl.sin(5 * x[0])
a = inner(grad(u), grad(v)) * dx
L = inner(f, v) * dx + inner(g, v) * ds


problem = LinearProblem(a, L, bcs=[bc], petsc_options={"ksp_type": "preonly", "pc_type": "lu"})
uh = problem.solve()
[0]PETSC ERROR: ------------------------------------------------------------------------
[0]PETSC ERROR: Caught signal number 11 SEGV: Segmentation Violation, probably memory access out of range
[0]PETSC ERROR: Try option -start_in_debugger or -on_error_attach_debugger
[0]PETSC ERROR: or see https://petsc.org/release/faq/#valgrind and https://petsc.org/release/faq/
[0]PETSC ERROR: configure using --with-debugging=yes, recompile, link, and run 
[0]PETSC ERROR: to get more information on the crash.
[0]PETSC ERROR: Run with -malloc_debug to check if memory corruption is causing the crash.
Abort(59) on node 0 (rank 0 in comm 0): application called MPI_Abort(MPI_COMM_WORLD, 59) - process 0

It seems that I will meet this problem, while using create_box.

dofs = fem.locate_dofs_topological(V=V, entity_dim=1, entities=facets)

is wrong. facets are two-dimension objects, while you are looking for one-dimensional objects with their IDs. The DOFs you will obtain will make no sense whatsover.

Fix is

dofs = fem.locate_dofs_topological(V=V, entity_dim=msh.topology.dim - 1, entities=facets)

Thank you. It works :heart: