UFLValueError("Invalid type conversion: %s can not be converted")

Hi,

I am trying to perform a gradient descent method for minimizing an energy function. For doing so, I have attached below minimal code. My problem is I don’t know how I should derive the strong form (like Function(V) ) of u1 (calculated in line 17) so that I can use it as the starting point in line 18.
Also, let’s assume I have a boundary condition for u as defined in line 9. How should I impose the boundary condition on my problem?

from dolfin import *
from ufl import replace
mesh = UnitSquareMesh(4,4)
V = VectorFunctionSpace(mesh, "Lagrange", 1)
du, v = TrialFunction(V) , TestFunction(V)                     
u, u0  = Function(V), Function(V)

top = CompiledSubDomain("near(x[1], 1) && on_boundary")
bc = [DirichletBC(V, Constant((1.0,0.0)), top)]

u0_weak = dot(u0, v)*dx
F = Identity(len(u)) + grad(u)
Pi = (tr(F.T*F) + (det(F)-1)**2) *dx
dPi = derivative(Pi, u, v) 
dPi_val = replace(dPi,{u:u0})
deltax = 1.e-6
u1 = u0_weak -deltax *dPi_val 
dPi_val2 = replace(dPi,{u:u1})

Thank you so much!