SEGV fault when interpolating function onto different mesh

I am trying to interpolate a function defined on one mesh onto another mesh in dolfinx 0.7.3. When doing so, I get the following error:

[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.
--------------------------------------------------------------------------
MPI_ABORT was invoked on rank 0 in communicator MPI_COMM_WORLD
with errorcode 59.

NOTE: invoking MPI_ABORT causes Open MPI to kill all MPI processes.
You may or may not see output from other processes, depending on
exactly when Open MPI kills them.
--------------------------------------------------------------------------

I have installed dolfinx (with complex numbers) using conda: conda create -n testenv -c conda-forge fenics-dolfinx petsc=*=complex*
Here is a minimal example reproducing the error:

import dolfinx
from mpi4py import MPI


mesh = dolfinx.mesh.create_rectangle(MPI.COMM_WORLD,[[-10,-10],[10,10]],[20,20])
V = dolfinx.fem.functionspace(mesh,('CG',1,))
F=dolfinx.fem.Function(V)
F.interpolate(lambda x: x[0],)

new_mesh = dolfinx.mesh.create_rectangle(MPI.COMM_WORLD,[[-10,-10],[10,10]],[10,10])
Vnew = dolfinx.fem.functionspace(new_mesh,('CG',1,))
Fnew=dolfinx.fem.Function(Vnew)

Fnew.interpolate(F) # SEGV happens here.

See: dolfinx/python/test/unit/fem/test_interpolation.py at v0.7.3 · FEniCS/dolfinx · GitHub for the up to date syntax

1 Like

Thanks for the quick reply!
I managed to not get an exception anymore, but the resulting interpolated function does not at all look like what one would expect.

import dolfinx
from mpi4py import MPI

mesh = dolfinx.mesh.create_rectangle(MPI.COMM_WORLD,[[-10,-10],[10,10]],[100,100])
V = dolfinx.fem.functionspace(mesh,('CG',1,))
F=dolfinx.fem.Function(V)
import numpy as np
F.interpolate(lambda x: x[0])

new_mesh = dolfinx.mesh.create_rectangle(MPI.COMM_WORLD,[[-10,-10],[10,10]],[4,4])
Vnew = dolfinx.fem.functionspace(new_mesh,('CG',1,))
Fnew=dolfinx.fem.Function(Vnew)

interp_data = dolfinx.fem.create_nonmatching_meshes_interpolation_data(F.function_space.mesh._cpp_object,F.function_space.element,Fnew.function_space.mesh._cpp_object,padding=1e-14)

Fnew.interpolate(F,nmm_interpolation_data=interp_data) 
Fnew.x.scatter_forward()

print(np.real(Fnew.vector.getArray()))
print(new_mesh.geometry.x[:,0])

This outputs

[ 9.8 10.  10.   9.8  9.6 10.   9.6  9.8  9.4 10.   9.2  9.4  9.6  9.2
 10.   9.6  9.2  9.4  9.2  9.6  9.  10.   9.   9.2  9.2]

[  5.  10.  10.   5.   0.  10.   0.   5.  -5.  10.  -5.   0.   5. -10.
  10. -10.  -5.   0.   5. -10.  -5.   0. -10.  -5. -10.]

Am I doing something wrong? In my real code I want to interpolate a F from a large domain with many points to a Fnew on a smaller domain (with fewer points) and in that case I still get a SEGV fault when I increase the resolution of the smaller mesh. For low resolution, where it doesn’t throw an error, the values of Fnew are orders of magnitude larger than the values of F (both the real and imaginary components of Fnew although F only produces real numbers). Increasing the resolution of the smaller mesh slightly increases the values of Fnew as well…

I would increase the padding to 1e-6 or something of that magnitude and see if it helps.

Unfortunately, increasing the padding did not change anything.
I managed to make a MWE that gives a SEGV fault when the resolution of the smaller mesh is increased: The following code,

import dolfinx
from mpi4py import MPI

mesh = dolfinx.mesh.create_rectangle(MPI.COMM_WORLD,[[-100,-100],[100,100]],[100,100])
V = dolfinx.fem.functionspace(mesh,('CG',1,))
F=dolfinx.fem.Function(V)
F.interpolate(lambda x: x[0])

N_points = 100
new_mesh = dolfinx.mesh.create_rectangle(MPI.COMM_WORLD,[[-10,-10],[10,10]],[N_points,N_points])
Vnew = dolfinx.fem.functionspace(new_mesh,('CG',1,))
Fnew=dolfinx.fem.Function(Vnew)

interp_data = dolfinx.fem.create_nonmatching_meshes_interpolation_data(F.function_space.mesh._cpp_object,F.function_space.element,Fnew.function_space.mesh._cpp_object,padding=1e-6)

Fnew.interpolate(F,nmm_interpolation_data=interp_data) # SEGV fault happens here if N_points > 100
Fnew.x.scatter_forward()


import pyvista
import numpy as np
p = pyvista.Plotter()
topology, cell_types, x = dolfinx.plot.vtk_mesh(Vnew)
grid = pyvista.UnstructuredGrid(topology, cell_types, x)

grid['u'] =np.real(Fnew.vector.getArray()[:])

grid.set_active_scalars('u')
warped=grid.warp_by_scalar('u')
p.show_bounds(use_3d_text=False)
p.add_mesh(warped,scalars='u')
p.show()

produces the plot:


But I get the following error if I set N_points to 101 (or anything above 100)

[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

Am I somehow mixing up the meshes unintentionally?

nvm this,

should have been,

interp_data = dolfinx.fem.create_nonmatching_meshes_interpolation_data(Fnew.function_space.mesh._cpp_object,Fnew.function_space.element,F.function_space.mesh._cpp_object,padding=1e-6)

It works with this change. Thanks again!