Skip to content

Commit

Permalink
api: make custom coeff less forceful on spacing
Browse files Browse the repository at this point in the history
  • Loading branch information
mloubout committed Feb 6, 2025
1 parent e8bf1fb commit 2b9c9da
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
5 changes: 4 additions & 1 deletion devito/finite_differences/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,5 +339,8 @@ def process_weights(weights, expr, dim):
return shape[weights.dimensions.index(wdim)], wdim, False
else:
# Adimensional weight from custom coeffs need to be multiplied by h^order
scale = not all(sympify(w).has(dim.spacing) for w in weights if w != 0)
if all(sympify(w).is_Number for w in weights):
scale = True
else:
scale = False
return len(list(weights)), None, scale
28 changes: 28 additions & 0 deletions tests/test_symbolic_coefficients.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,3 +372,31 @@ def test_backward_compat_mixed(self):
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)

def test_backward_compat_array_of_func(self):
grid = Grid(shape=(11, 11, 11))
x, _, _ = grid.dimensions
hx = x.spacing

f = Function(name='f', grid=grid, space_order=16, coefficients='symbolic')

# Define stencil coefficients.
weights = Function(name="w", space_order=0, shape=(9,), dimensions=(x,))
wdx = [weights[0]]
for iq in range(1, weights.shape[0]):
wdx.append(weights[iq])
wdx.insert(0, weights[iq])

# Plain numbers for comparison
wdxn = np.random.rand(17)

# Number with spacing
wdxns = wdxn / hx

dexev = f.dx(weights=wdx).evaluate
dexevn = f.dx(weights=wdxn).evaluate
dexevns = f.dx(weights=wdxns).evaluate

assert all(a.as_coefficient(1/hx) for a in dexevn.args)
assert all(a.as_coefficient(1/hx) for a in dexevns.args)
assert all(not a.as_coefficient(1/hx) for a in dexev.args)

0 comments on commit 2b9c9da

Please sign in to comment.