Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

api: fix backward compatibility of Substitution, missing function check #2522

Merged
merged 1 commit into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion devito/finite_differences/derivative.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,8 @@ def _xreplace(self, subs):
if self in subs:
new = subs.pop(self)
try:
return new._xreplace(subs)
new, flag = new._xreplace(subs)
return new, True
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: this was actually incorrectly returning False flag in lots of cases (since subs is then empy here) so new._xreplace(subs) would return new, False

except AttributeError:
return new, True

Expand Down
2 changes: 1 addition & 1 deletion devito/finite_differences/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ def generate_indices(expr, dim, order, side=None, matvec=None, x0=None, nweights
if nweights > 0:
do, dw = order + 1 + order % 2, nweights
if do < dw:
raise ValueError(f"More weights ({nweights}) provided than the maximum"
raise ValueError(f"More weights ({nweights}) provided than the maximum "
f"stencil size ({order + 1}) for order {order} scheme")
elif do > dw:
warning(f"Less weights ({nweights}) provided than the stencil size"
Expand Down
1 change: 1 addition & 0 deletions devito/types/equation.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ def _apply_coeffs(cls, expr, coefficients):
for coeff in coefficients.coefficients:
derivs = [d for d in retrieve_derivatives(expr)
if coeff.dimension in d.dims and
coeff.function in d.expr._functions and
coeff.deriv_order == d.deriv_order.get(coeff.dimension, None)]
if not derivs:
continue
Expand Down
28 changes: 28 additions & 0 deletions tests/test_symbolic_coefficients.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,3 +344,31 @@ def test_spacing(self):
df_s = f.dx(weights=coeffs1)

assert sp.simplify(df_s.evaluate - df.evaluate) == 0

def test_backward_compat_mixed(self):

grid = Grid(shape=(11,))
x, = grid.dimensions

f = Function(name='f', grid=grid, space_order=8)
g = Function(name='g', grid=grid, space_order=2)

coeffs0 = np.arange(0, 9)

coeffs = Coefficient(1, f, x, coeffs0)

eq = Eq(f, f.dx * g.dxc, coefficients=Substitutions(coeffs))

derivs = retrieve_derivatives(eq.rhs)

assert len(derivs) == 2
df = [d for d in derivs if d.expr == f][0]
dg = [d for d in derivs if d.expr == g][0]

assert np.all(df.weights == coeffs0)
assert dg.weights is None

eqe = eq.evaluate
assert '7.0*f(x + 3*h_x)' in str(eqe.rhs)
assert '0.5*g(x + h_x)' in str(eqe.rhs)
assert 'g(x + 2*h_x)' not in str(eqe.rhs)
Loading