Differentiate of CoefficientFunction w.r.t mesh
- domthom
- Topic Author
- New Member
Less
More
2 years 5 months ago #4299
by domthom
Differentiate of CoefficientFunction w.r.t mesh was created 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
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
2 years 5 months ago #4306
by joachim
Replied by joachim on topic Differentiate of CoefficientFunction w.r.t mesh
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
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
- domthom
- Topic Author
- New Member
Less
More
2 years 5 months ago #4311
by domthom
Replied by domthom on topic Differentiate of CoefficientFunction w.r.t mesh
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
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
2 years 5 months ago #4313
by joachim
Replied by joachim on topic Differentiate of CoefficientFunction w.r.t mesh
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
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.105 seconds