Interpolate the gradient of a dolfin function

Hi there,

Is it possible to interpolate the gradient of a dolfin function?
My idea was:

J     = interpolate(grad(u), V)

But that doesn’t work.

The gradient of a function has to be interpolated into an appropriate space.
Thus is V = FunctionSpace(mesh, "CG", 1), the appropriate space for the gradient is W=VectorFunctionSpace(mesh, "DG", 0).

Next time, add a minimal code illustrating your problem, that can be copy-pasted in to any editor an executed without editing of the code. As grad(u) is not a function, but a UFL expression, you should use project. An example of a minimal code:

from dolfin import *
mesh = UnitSquareMesh(10, 10)
degree = 3
V = FunctionSpace(mesh, "CG", degree)
u = project(Expression("x[0]*x[1]", degree=degree), V)
W = VectorFunctionSpace(mesh, "DG", degree-1)
Du = project(grad(u), W)
2 Likes

Okay, I will. Thank you dokken, you’re a hero.