Differentiate of CoefficientFunction w.r.t mesh

More
2 years 7 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
More
2 years 7 months ago #4306 by joachim
Hi,

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)


Joachim

 
More
2 years 7 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
More
2 years 7 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()
(U*U).Diff(U) 
is already fixed.

Joachim



 
Time to create page: 0.101 seconds