devito icon indicating copy to clipboard operation
devito copied to clipboard

Custom stencils not fully lowered if not made into EvalDerivative

Open EdCaunt opened this issue 1 year ago • 0 comments

Stencils specified as the weighted sum of terms need to be assembled into an EvalDerivative to ensure that symbolic flop reduction is fully applied. An example of such a stencil is:

coeffs = [1, 2, 1]
inds = [-1. 0, 1]

expr = sum([coeff*f.subs({x: x+ind*h_x}) for coeff, ind in zip(coeffs/inds)])
stencil = expr/h_x**2

In this case, flop reduction will not be carried out fully, as the resulting stencil object is of type Mul. The correct way to form such a stencil is:

coeffs = [1, 2, 1]
inds = [-1. 0, 1]

terms = [coeff*f.subs({x: x+ind*h_x})/h_x**2 for coeff, ind in zip(coeffs/inds)])
stencil = EvalDerivative(*terms, base=f)

In the case of nested derivatives, an EvalDerivative needs to be used at each layer

EdCaunt avatar Aug 04 '22 09:08 EdCaunt