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

Can FEniCS take a derivative of an Expression?

0 votes

I have defined a Function U and an Expression V as follows:

import fenics as fe
nelements = 8
degree = 2
m = fe.UnitIntervalMesh(nelements)
DE = fe.FiniteElement('DG', m.ufl_cell(), degree)
DS = fe.FunctionSpace(m, DE)
U = fe.Function(DS)
wU = fe.TestFunction(DS)
Vstr = '-beta*log(alpha + U)'
V = fe.Expression(Vstr, degree = degree, alpha=1, beta=1, U=U)

This works exactly as I hoped it would. V depends on U. For instance, if I do this:

U.vector()[1] = 1

then plot V, I find it has changed. Also,

dxx = fe.Measure('dx', m)
fe.assemble(V*dxx)

gives the entirely plausible result -0.014440566261665524.

Now I want to compute the derivative of the integral of V with respect to the degrees of freedom of U. So, with consummate naivete, I tried this:

fe.derivative(V*dxx, U)
dV = fe.assemble(fe.derivative(V*dxx, U))
dV.array()

array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
        0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.])

It sort of works, in that I, now getting a vector result, but this is not the answer I was hoping for... Is there a way to get the differentiation to propagate through the Expression?

asked Jul 2, 2017 by lavery FEniCS Novice (350 points)
edited Jul 2, 2017 by lavery

Why do you not just do

V = 1.*fe.ln(1.+U)

?

That works? Yes, I see it does. OK, I will have to do some things differently from the way I was planning, but I can work with this. ¡Muchas gracias!

...