Derivative of a scalar function wrt a tensor function

Hello,

I am following this post (Derivative with respect to a tensor - FEniCS Q&A ) to calculate the derivative of energy function wrt a tensor. I am adding following lines to the hyperelasticity demo (Hyperelasticity — DOLFIN documentation):

T = TensorFunctionSpace(mesh, "Lagrange", 1)
F1 = I + grad(u) 
F1 = variable(F1)

dF = diff(psi, F1)
stress = Function(T)
stress = project (dF, T)
stress.rename("stress", "stress")
fileS = File("stress.pvd")
fileS << stress

However, when I view the stress.pvd file, I get stress equal to zero all over the body. I was wondering what I am doing wrong in here?

Thanks for the help!

You would need to declare F as a variable before defining psi in terms of it, i.e.,

# Kinematics
d = len(u)
I = Identity(d)             # Identity tensor
F = I + grad(u)             # Deformation gradient
############
F = variable(F)
############
C = F.T*F                   # Right Cauchy-Green tensor

then compute

dF = diff(psi,F)

Because F1 is defined as a variable after psi, UFL does not know that psi depends on it, so the zero derivative makes sense.

Thank you so much for your response. My problem is resolved now!