devito icon indicating copy to clipboard operation
devito copied to clipboard

Derivatives get duplicated when a derivative is substituted for expression containing derivatives

Open EdCaunt opened this issue 1 year ago • 0 comments

If an unevaluated derivative is substituted for some expression containing derivatives, any derivatives in the substituted expression are duplicated unless they are the original derivative. As the Derivative object gets duplicated, subsequent algebraic simplifications are missed during the lowering process as they are considered separate symbols and thus not canceled.

If this expression is then used in building a kernel, it will take much longer than usual to build as a result of the increased number of derivative objects.

MFE:

from devito import Grid, Function, Derivative

grid = Grid(shape=(11,), extent=(10.,))
f = Function(name='f', grid=grid, space_order=4)

# To make sure that derivative objects are the same
fdx = f.dx
fdx2 = f.dx2

expr = fdx + fdx2

assert expr.find(Derivative) == expr.subs({fdx: fdx + fdx2, fdx2: fdx2}).find(Derivative)

EdCaunt avatar Sep 02 '22 15:09 EdCaunt