On 5 Nov 2015, at 09:49, Buesing, Henrik <HBuesing@eonerc.rwth-aachen.de> wrote:
Dear all,
I’m having a variable Sw, which I calculate pointwise in a routine calc_Sw (see attachment). This variable depends on my primary unknown h: Sw = (h-hn)/(hw-hn) (hn,hw known values).
But now it seems like automatic differentiation for this routine does not work. I’m getting zero entries for the Jacobian, whereas d(Sw)/dh = 1.0 should hold.
Yes, this is because the AD doesn't know about the relationship between Sw and dh. UFL has a facility for this, but I notice we don't expose it in firedrake (however, it is straightforward to do): We want derivative(F, u) But F contains a coefficient, S, whose derivative wrt u is 1.0 (however, they are not symbolically related in a way UFL understands). So we build a mapping from this coefficient to its derivative wrt u: coefficient_derivatives = {S: 1.0} and then pass this additional information to the derivative call. derivative(F, u, coefficient_derivatives=coefficient_derivatives) Firedrake uses the UFL derivative function, but does not expose this extra argument in the interface. It is straightforward to alter the definition in firedrake/ufl_expr.py to take this extra argument and pass it through. If this works for you, do you want to propose a patch that adds this functionality? Cheers, Lawrence