Directional derivative of UFL expression

Hi,
does any one know a way to differentiate a UFL expression in a specified direction ? e.g. if I have something like

expr = dot(grad(u), grad(u))

I would like to do something like

dexpr = derivative(expr, u, v)

where dexpr should be 2*dot(grad(u), grad(v)). Basically like the usual derivative on forms but I would like to be able to do it on an expression directly…

Your example code appears to work as-is for me:

from dolfin import *
mesh = UnitCubeMesh(1,1,1)
V = FunctionSpace(mesh,"CG",1)
u = Function(V)
u.rename("u","u")

# Runs without error:
expr = dot(grad(u),grad(u))
v = TestFunction(V)
dexpr = derivative(expr,u,v)

# Printed result makes sense:
from ufl import replace
dexpr = replace(dexpr,{v:Function(V,name="v")})
print(dexpr)
1 Like

Indeed! Thanks David, I must have done something wrong when testing that. Good to know.