Differentiation of Form

I try to solve viscoporoelasticity problem in FEniCS, and the variational form is obtained with minimizing the incremental potential, which is derivative(potential,u,u_) in ufl. But there is an issue when u is tensor function, and error message is “Cannot take length of non-vector expression”. Following is part of my code:

V = VectorElement(‘CG’,mesh.ufl_cell(),2)
Q = FiniteElement(‘CG’,mesh.ufl_cell(),1)
Qe = TensorElement(“DG”,mesh.ufl_cell(),0)
W = FunctionSpace(mesh,MixedElement([V,Q,Qe]))

w = Function(W, name=“Variables at current step”)
(u, p, epsv) = split(w)
w_old = Function(W, name=“Variables at previous step”)
(u_old, p_old, epsv_old) = split(w_old)
w_ = TestFunction(W)
(u_,p_,epsv_) = split(w_)
dw = TrialFunction(W)
(du,dp,depsv) = split(dw)

incremental_potential=strain_energy(u,p,epsv)dx + dtdissipation_potential((epsv-epsv_old)/dt)*dx-dot(f,u)*dx-dot(Ts, u)*ds(3)
F = derivative(incremental_potential,epsv,epsv_)

error message:

Traceback (most recent call last):
File “2D_dry_linear_visco_potential.py”, line 116, in
F = derivative(incremental_potential,epsv,epsv_)
File “/usr/lib/python3/dist-packages/dolfin/fem/formmanipulations.py”, line 82, in derivative
return ufl.derivative(form, u, du, coefficient_derivatives)
File “/usr/lib/python3/dist-packages/ufl/formoperators.py”, line 281, in derivative
argument)
File “/usr/lib/python3/dist-packages/ufl/formoperators.py”, line 167, in _handle_derivative_arguments
coefficients = tuple(coefficient)
File “/usr/lib/python3/dist-packages/ufl/core/expr.py”, line 406, in len
raise NotImplementedError(“Cannot take length of non-vector expression.”)
NotImplementedError: Cannot take length of non-vector expression.

Could anyone help me to figure it out?

If you’re solving in the mixed space W, you would typically want to take the derivative with respect to all unknowns, i.e., derivative(incremental_potential, w), to obtain the residual. In the code sample given, it looks like you’re attempting to only differentiate with respect to one component of w.

Yes, I only want to take the derivative with respect to one component of w. Is there any way to achieve it? Also, I try to take the derivative with respect to u or p, it doesn’t have this kind of error message.
Like,

F = derivative(incremental_potential, u, u_)
F = derivative(incremental_potential, p, p_)