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

