Question regarding compute_gradient()

Hey all, I have been using FEniCs for my project and I am new to FEM/FEniCs. I am trying to use this to solve an inverse problem related to fluid simulation. The fluid simulation code I am using is as here: The Navier–Stokes equations Tutorial.

The control parameter is u_n and I am trying to minimize the point-wise difference between the predicted velocity magnitude and observation velocity magnitude:

ux, uy = u_n.split(deepcopy=False)
c = ux**2+uy**2
J = sum([(observation[i]-c[i])**2 for i in range(len(points))])/len(points)
control = Control(u)
dJd0 = compute_gradient(J, control)

Any help on how I may dubug this, or any comments would be super helpful!