This is a read only copy of the old FEniCS QA forum. Please visit the new QA forum to ask questions

Jacobian matrix of ufl expression on mixed function space

+1 vote

How to calculate to the jacobian of $n(u, u_n, u_p)$? That is

$$dn_{ij} = \frac{\delta n_i}{\delta (u, u_n, u_p)_j}$$

I am considering the following snippet

Q = FunctionSpace(mesh,'CG',1)
element = Q.ufl_element()
mixed_element = MixedElement(element,element,element)
W = FunctionSpace(mesh, mixed_element)
u_mixed = Function(W)
v_mixed = TestFunction(W)
v, vn, vp = split(v_mixed)
du_mixed = TrialFunction(W)
u, un, up = split(u_mixed)


n = exp((u-un))
dx = Measure("dx")

I'd like to calculate the jacobian with something like this but I don't know if that's correct:

dn = derivative(n*v*dx(), u_mixed, du_mixed)
dn += derivative(n*vn*dx(), u_mixed, du_mixed)
J = assemble(dn)

Any help is appreciated.

asked Jul 3, 2017 by chaffra FEniCS User (1,830 points)
edited Jul 3, 2017 by chaffra

1 Answer

+1 vote

Hello,

I think you might be looking for something like this:

F0 = n*v*dx
F1 = n*vn*dx 
F = F0+F1

J = derivative(F,u_mixed, du_mixed)

Regards,
Leonardo

answered Jul 5, 2017 by lhdamiani FEniCS User (2,580 points)

Thanks Leonardo,

this seems to work. For example, is derivative(n*v*dx,u_mixed,du_mixed) the derivative on the integral of n with respect to a degree of freedom in u_mixed or is it the derivative of n or are they both the same thing in this case? I am still confused why this works.

Hello,

They are the same thing.. I checked now, one problem before on your script was the parenthesis on the dx term... you don't need to add "()" when instantiating the weak form of your equations...

as a test, you could run something like:

...
J = derivative(F,u_mixed, du_mixed)
dn = derivative(n*v*dx, u_mixed, du_mixed)
dn += derivative(n*vn*dx, u_mixed, du_mixed)
diff = J-dn
print assemble(diff).array()
...

This would obviously be zero since F = F0+F1.
But I now think the term n*v*dx corresponds to the projection of n onto the basis functions, the assembly of which will give a vector. In that case the assembly of derivative(n*v*dx, u_mixed, du_mixed) would give the Jacobian matrix (I think...). Thanks again for your help!

exactly! this way it's for sure the differents approach resulting on the same result..
Regards,Leonardo

...