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

Derivative of form wrt some parameter

+3 votes

Hello
I have some form like

p = 1.0
u = Function(X)
v = TestFunction(X)
a = f(u,p)*v*ds

f(u,p) is some complicated nonlinear function.

I want the derivative of the form wrt to the parameter "p"., which I then want to assemble. How can I compute this ?

asked Dec 14, 2013 by praveen FEniCS User (2,760 points)

1 Answer

+3 votes
 
Best answer

Its is possible. You nee to mark the variable that you wish to take a derivative with respect to and the use diff, e.g to compute $df/de$ where $f = \exp(e^{2})$:

e = variable(e)
f = exp(e**2)
df = diff(f, e)
answered Dec 14, 2013 by Garth N. Wells FEniCS Expert (35,930 points)
selected Dec 15, 2013 by praveen

I have some issues. Here is an example code

from dolfin import *
mesh = IntervalMesh(10,0,1)
V = FunctionSpace(mesh, "DG", 0)
v = TestFunction(V)
p = 0.0
p = variable(p)
a = v*p**2*ds
da = diff(a, p)
M = assemble(da)

This works if "p" is nonzero but fails if p=0.0

UFL optimises away zero terms.

The code I listed above does not even run if p=0. I get error

All terms must have same rank.

It works if I use Constant(0.0)

from dolfin import *
mesh = IntervalMesh(10,0,1)
V = FunctionSpace(mesh, "DG", 0)
v = TestFunction(V)
p = Constant(0.0)
p = variable(p)
a = v*p**2*ds
da = diff(a, p)
M = assemble(da)
...