Forum Message



We have moved the forum to . This is an archived version of the topics until 05/05/23. All the topics were moved to the new forum and conversations can be continued there. This forum is just kept as legacy to not invalidate old links. If you want to continue a conversation just look for the topic in the new forum.


The forum is in read only mode.

Differentiate of CoefficientFunction w.r.t mesh

1 year 8 months ago #4299 by domthom
Hi all,
I want to use the result of a simulation as input to another simulation (after a few mathematical manipulations).
For example, I obtain the result U of simulation #1 as GridFunction. Here, I can take the derivative w.r.t. to the spatial coordinates using U.Deriv().
However, when I take for example U*U, the result is a CoefficientFunction, which can only be differentiated symbolically (which would result in 0, as the data is only numeric). In order to perform something like .Deriv(), I would have to interpolate the CoefficientFunction on the grid using gfu.Set() first. But this is very slow, because I have to do it for many functions. Is there any way around this, so that the differentiation w.r.t. to the spatial coordinates is possible directly?

Best regards
1 year 8 months ago #4306 by joachim

you have to apply the chain rule manually:
grad(f(U)) = f^prime(U) * grad(U)

func = U*U
dfdU = func.Diff(U)      #  returns 2*U
gradfunc = dfdU * grad(U)

currently we have an issue differentiating w.r.t a GridFunction. This workaround does the job:

wrapU = 1*U
func = wrapU*wrapU
dfdU = func.Diff(wrapU)       -> retuns 2*U
gradfunc = dfdU * grad(U)


1 year 8 months ago #4311 by domthom
Hello Joachim,

thank you very much for your fast reply! It already helped a lot in understanding how the software works.
One additional question arose: Does this also work for nested structures?
So for example if I replace the definition of dfdU with the following
dfdU = func.Diff(1*U) ,
the variable dfdU contains only the ZeroCoefficientFunction.
This would make things easier for larger calculations.

Best regards
1 year 8 months ago #4313 by joachim
func = some_function(U)

builds a symbolic expression tree, try print (func)

func.Diff (U)

traverses the tree, and check if the tree-node is the same object as the expression you differentiate after.

1*U will give you a new object, which cannot be found in the tree.

Differentiation of expressions is currently very active.
Suggestions and test-cases are very welcome.
Stay up to date with the nightly branch. The problem from yesterday,
U = GridFunction()
is already fixed.


Time to create page: 0.120 seconds