Automatic differentiation unexpected behavior

Hi all,

When I make a CoefficientFunction(x) then Diff(x) returns the expected result of 1.
However, when I make a CoefficientFunction from a GridFunction and then call Diff(x) it returns 0. Is this expected behavior?

Is there a function which I can call that returns the derivative with respect to x of a CoefficientFunction that consists of a GridFunction? Or is the only way to manually specify the derivative using Grad of the GridFunction?

In the attachments there is a small example.

Best,
Gotya

Attachment: DerivativeUnexpected.py

Symbolic diff uses pointer comparision and diff rules. So when you for example build a coefficientfunction cf = xx then cf.Diff(x) = (Product rule) xx.Diff(x) + x.Diff(x) * x = (Pointer comparison) x1 + 1x.
The gridfunction doesn’t “know” anything about the cf x.
You can build the derivative you want by using the chain rule, for example:

func = sin(gf)
dfunc = func.Diff(gf) * grad(gf)

Best