Skip to content

Commit 5a6e2e4

Browse files
authored
dsl: Fix up functions on subdomains staggered derivs and cross derivs with halo=0 (#3103)
* dsl: Fix up staggerd derivative evaluation for functions on subdomains * dsl: Fixup halo=0 with cross-derivatives * dsl: Normalise staggers and fixup test which passed due to stagger of function on subdomain being dropped * dsl: Express x0 in the same Dimension as the indices * misc: Key staggering DimensionTuple off root dimensions * dsl: Normalise x0
1 parent d1baa3b commit 5a6e2e4

5 files changed

Lines changed: 57 additions & 13 deletions

File tree

‎devito/finite_differences/finite_difference.py‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from devito.logger import warning
88

9-
from .differentiable import DiffDerivative, EvalDerivative, Weights
9+
from .differentiable import DiffDerivative, EvalDerivative, Weights, merge_growth
1010
from .tools import (
1111
centered, check_input, direct, fd_weights_registry, generate_indices, left,
1212
process_weights, right, transpose
@@ -236,8 +236,12 @@ def make_derivative(expr, dim, fd_order, deriv_order, side, matvec, x0, coeffici
236236
# With a `subdomain`, `expr` is treated as zero outside it: every stencil tap
237237
# is multiplied by the indicator of the point it reads, 1 if `subdomain`
238238
# spans the whole of `dim`. The derivative then extends past `subdomain` by
239-
# the stencil radius
240-
growth = {dim.root: indices.radius} if subdomain is not None else {}
239+
# the stencil radius, on top of the growth of `expr` itself (e.g., the inner
240+
# derivative of a cross derivative)
241+
if subdomain is not None:
242+
growth = merge_growth([expr._growth, {dim.root: indices.radius}])
243+
else:
244+
growth = {}
241245

242246
if not expand and indices.expr is not None:
243247
weights = Weights(name='w', dimensions=indices.free_dim,

‎devito/finite_differences/tools.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,10 @@ def generate_indices(expr, dim, order, side=None, matvec=None, x0=None, nweights
299299
iexpr = x0 + d * dim.spacing
300300
return IndexSet(dim, expr=iexpr), x0
301301

302+
# Normalize x0 `Dimension` to match index if the latter is a `SubDimension` of
303+
# the former
304+
x0 = x0.subs({d.root: d for d in expr.dimensions if d.root is dim and d.is_Sub})
305+
302306
# Evaluation point relative to the expression's grid
303307
mid = (x0 - expr.indices_ref[dim]).subs({dim: 0, dim.spacing: 1})
304308

‎devito/types/dense.py‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,16 +1165,18 @@ def __staggered_setup__(cls, dimensions, staggered=None, **kwargs):
11651165
processed = staggered
11661166
else:
11671167
# Staggering is not NODE or CELL or None
1168-
# therefore it's a tuple of dimensions
1168+
# therefore it's a tuple of dimensions. These may be the roots of
1169+
# `dimensions` (e.g., `staggered=x` for a Function on a SubDomain)
1170+
staggered = set(as_tuple(staggered))
11691171
processed = []
11701172
for d in dimensions:
1171-
if d in as_tuple(staggered):
1173+
if {d, d.root} & staggered:
11721174
processed.append(sympy.S.One)
1173-
elif -d in as_tuple(staggered):
1175+
elif {-d, -d.root} & staggered:
11741176
processed.append(sympy.S.NegativeOne)
11751177
else:
11761178
processed.append(sympy.S.Zero)
1177-
return Staggering(*processed, getters=dimensions)
1179+
return Staggering(*processed, getters=[d.root for d in dimensions])
11781180

11791181
@classmethod
11801182
def __indices_setup__(cls, *args, **kwargs):

‎tests/test_derivatives.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1861,7 +1861,7 @@ def test_adjoint_unexpanded_3d(self):
18611861

18621862
assert self.dot_test(grid, subdomain, 'dy', 8, opt=opt) < 1e-12
18631863

1864-
@pytest.mark.parametrize('deriv', ['dx', 'dy'])
1864+
@pytest.mark.parametrize('deriv', ['dx', 'dy', 'dxdy'])
18651865
def test_adjoint_2d(self, deriv):
18661866
grid = Grid(shape=(20, 22), extent=(19., 21.), dtype=np.float64)
18671867
subdomain = Region('s', {'x': ('left', 6), 'y': ('middle', 4, 5)}, grid=grid)

‎tests/test_subdomains.py‎

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
from conftest import assert_structure, opts_tiling
88
from devito import (
9-
Border, Buffer, ConditionalDimension, Constant, Eq, Function, Grid, Lt, Operator,
10-
SparseFunction, SparseTimeFunction, SubDimension, SubDomain, SubDomainSet,
9+
Border, Buffer, ConditionalDimension, Constant, Derivative, Eq, Function, Grid, Lt,
10+
Operator, SparseFunction, SparseTimeFunction, SubDimension, SubDomain, SubDomainSet,
1111
TensorFunction, TimeFunction, VectorFunction, solve
1212
)
1313
from devito.exceptions import InvalidArgument
@@ -1601,6 +1601,40 @@ def define(self, dimensions):
16011601
assert np.all(np.isclose(fdx.data[:], gdx.data[2:-2, 3:-1]))
16021602
assert np.all(np.isclose(fdy.data[:], gdy.data[2:-2, 3:-1]))
16031603

1604+
@pytest.mark.parametrize('s_o', [2, 4])
1605+
@pytest.mark.parametrize('axis', [0, 1])
1606+
def test_derivatives_staggered(self, s_o, axis):
1607+
"""
1608+
Derivatives of Functions on SubDomains can be evaluated at points staggered
1609+
along the derivative Dimension
1610+
"""
1611+
1612+
class Middle(SubDomain):
1613+
1614+
name = 'middle'
1615+
1616+
def define(self, dimensions):
1617+
x, y = dimensions
1618+
return {x: ('middle', 2, 2), y: ('middle', 3, 1)}
1619+
1620+
grid = Grid(shape=(10, 10), extent=(9., 9.))
1621+
d = grid.dimensions[axis]
1622+
mid = Middle(grid=grid)
1623+
1624+
f = Function(name='f', grid=mid, space_order=s_o)
1625+
g = Function(name='g', grid=grid, space_order=s_o)
1626+
fd, gd = (Function(name=i, grid=grid, staggered=d) for i in ('fd', 'gd'))
1627+
1628+
f.data[:] = np.random.default_rng(0).random(f.shape)
1629+
g.data[2:-2, 3:-1] = f.data
1630+
1631+
Operator([Eq(fd, Derivative(f, d), subdomain=mid),
1632+
Eq(gd, Derivative(g, d), subdomain=mid)])()
1633+
1634+
# Same stencil, so bitwise identical
1635+
assert np.any(fd.data != 0)
1636+
assert np.all(fd.data == gd.data)
1637+
16041638
def test_operator_shortcuts(self):
16051639
"""
16061640
Test that shortcuts for Laplacian, divergence, etc all work with Functions
@@ -2007,14 +2041,14 @@ def define(self, dimensions):
20072041
return {x: ('left', 2), y: y, z: z}
20082042

20092043
sd1 = SD1(grid=grid)
2010-
f = TimeFunction(name='f', grid=grid, time_order=1, space_order=2,
2011-
staggered=(x,))
2044+
f = TimeFunction(name='f', grid=grid, time_order=1, space_order=2)
20122045
g = TimeFunction(name='g', grid=sd1, time_order=1, space_order=2,
20132046
staggered=(x,))
20142047

20152048
eq = Eq(g, g + f.dx)
20162049
eqe = eq.evaluate
2017-
assert eqe.rhs == g + f.dx(x0=x).evaluate._subs(x, g.dimensions[1])
2050+
x0 = x + x.spacing/2
2051+
assert eqe.rhs == g + f.dx(x0=x0).evaluate._subs(x, g.dimensions[1])
20182052

20192053

20202054
class TestSubDomainArguments:

0 commit comments

Comments
 (0)