An output / interpolation problem in parallel

Following code output the gradient of a function.

from pathlib import Path

from mpi4py import MPI
import numpy as np

from dolfinx.io import XDMFFile
from dolfinx.fem import Function, Expression, functionspace
from dolfinx.mesh import create_rectangle

from ufl import grad

msh = create_rectangle(MPI.COMM_WORLD, ((-1, -1), (1, 1)), [100, 100])
space = functionspace(msh, ('Lagrange', 1))

u = Function(space)
u1 = Function(space)

u.interpolate(lambda x: np.tanh(5 * x[0]))
u.x.scatter_forward()
u1.interpolate(Expression(grad(u)[0], space.element.interpolation_points))
u1.x.scatter_forward()

folder = Path("test")

with XDMFFile(MPI.COMM_WORLD, folder / 'u1.xdmf', 'w') as file:
    file.write_mesh(msh)
    file.write_function(u1)

The output is correct in single process,

while there’s some things like cracks in the output when I run the code with parallel.

It seems that this problem occurs when the degree of the expression and the function(u1) does not match. Can anyone tell me what’s wrong with it?

Besides, I want to know when should I use the function u.x.scatter_forward()?

Dolfinx version: 0.10.0 installed with docker

The gradient of `u’ is not in the space you are interpolating into,

Thus this will introduce a dependence on the order of elements traversed. If your u is a P-1 function, the gradient is a DG-0 function.

Scatter forward is needed whenever thou modify the owns part of the DOF vector, but not all ghost entries. For standard interpolation, this is not needed. However, for interpolation onto a subset of cells, it should be called to ensure consistency