Skip to content

Commit 46e117d

Browse files
committed
api: add fallback from wrong sized custom coeffs
1 parent 636f70e commit 46e117d

5 files changed

Lines changed: 31 additions & 2 deletions

File tree

devito/finite_differences/finite_difference.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from sympy import sympify
22

3+
from devito.logger import warning
34
from .differentiable import EvalDerivative, DiffDerivative, Weights
45
from .tools import (left, right, generate_indices, centered, direct, transpose,
56
check_input, fd_weights_registry, process_weights)
@@ -158,6 +159,12 @@ def make_derivative(expr, dim, fd_order, deriv_order, side, matvec, x0, coeffici
158159
# `coefficients` method (`taylor` or `symbolic`)
159160
if weights is None:
160161
weights = fd_weights_registry[coefficients](expr, deriv_order, indices, x0)
162+
if len(weights) != len(indices):
163+
warning(f"Number of weights ({len(weights)}) does not match "
164+
f"number of indices ({len(indices)}), reverting to Taylor")
165+
scale = False
166+
weights = fd_weights_registry['taylor'](expr, deriv_order, indices, x0)
167+
161168
# Did fd_weights_registry return a new Function/Expression instead of a values?
162169
_, wdim, _ = process_weights(weights, expr, dim)
163170
if wdim is not None:

requirements-optional.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ pillow>11,<11.3.1
33
pyrevolve==2.2.6
44
scipy<1.15.4
55
distributed<2025.7.1
6-
click<9.0
6+
click<9.0
7+
cloudpickle<3.1.2

requirements-testing.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ nbval<0.11.1
66
scipy<1.15.4
77
pooch<1.8.3
88
click<9.0
9+
cloudpickle<3.1.2

requirements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,4 @@ cgen>=2020.1,<2026
77
codepy>=2019.1,<2025
88
multidict<6.3
99
anytree>=2.4.3,<=2.13.0
10-
cloudpickle<3.1.2
1110
packaging<25.1

tests/test_derivatives.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,3 +1229,24 @@ def test_expand_product_rule(self):
12291229
+ 10*self.f*Derivative(self.v, self.x)*Derivative(self.u, self.x) \
12301230
+ 5*self.f*self.u*Derivative(self.v, (self.x, 2))
12311231
assert diffify(expr.expand(product_rule=True)) == expanded
1232+
1233+
def test_fallback_wrong_custom_size(self):
1234+
"""
1235+
Check an exception is raised when a custom size is not compatible
1236+
with the derivative order
1237+
"""
1238+
grid = Grid((10,))
1239+
x, = grid.dimensions
1240+
u = Function(name="u", grid=grid, space_order=2, staggered=x)
1241+
v = Function(name="v", grid=grid, space_order=2, staggered=NODE)
1242+
1243+
w = [-2, 2] # Should 4 coeff since this is staggered
1244+
1245+
eq0 = Eq(u, v.dx(w=w)).evaluate
1246+
exp0 = -2 * v / x.spacing + 2 * v._subs(x, x + x.spacing)/x.spacing
1247+
# This one should fallback to taylor coeffs sinc w is too short
1248+
# for a centered derivative
1249+
eq1 = Eq(v, v.dx(w=w)).evaluate
1250+
exp1 = - .5 * (v._subs(x, x - x.spacing) - v._subs(x, x + x.spacing))/x.spacing
1251+
assert simplify(eq0.rhs - exp0) == 0
1252+
assert simplify(eq1.rhs - exp1) == 0

0 commit comments

Comments
 (0)