Skip to content

Commit 0ffee9a

Browse files
committed
Make the safe harmonic mean zero next to any zero point
The safe harmonic mean drops a zero point instead of dividing by it, then guarded its final inversion by the reference point alone, so it was zero only when that point was. Next to a zero elsewhere in the stencil it came out at 4/3 or 2 times the nonzero value, e.g., a shear modulus on faces touching a fluid on one side of every fluid-solid contact. Guard it by the smallest of the points the mean reads instead: zero wherever it reads a zero, as the harmonic mean is, and still no division by zero.
1 parent 2681402 commit 0ffee9a

2 files changed

Lines changed: 39 additions & 6 deletions

File tree

‎devito/types/basic.py‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
from devito.data import default_allocator
1717
from devito.parameters import configuration
1818
from devito.tools import (
19-
CustomDtype, Pickable, as_tuple, dtype_to_ctype, flatten, frozendict, memoized_meth,
20-
sympy_mutex
19+
CustomDtype, Pickable, as_tuple, dtype_to_ctype, filter_ordered, flatten, frozendict,
20+
memoized_meth, sympy_mutex
2121
)
2222
from devito.types.args import ArgProvider
2323
from devito.types.caching import Cached, Uncached
@@ -1111,9 +1111,17 @@ def _evaluate(self, **kwargs):
11111111
retval = retval._evaluate(**kwargs)
11121112
retval = retval.subs(subs)
11131113

1114-
# If harmonic averaging, invert at the end
1115-
if self.is_harmonic:
1116-
retval = retval._inv(self.function.subs(subs), safe=self.is_harmonic_safe)
1114+
# If harmonic averaging, invert at the end. The safe mean drops a zero
1115+
# point instead of dividing by it, so it is zero only where the guard of
1116+
# this inversion is: the smallest point it reads, so that it is zero
1117+
# next to any zero, as the harmonic mean is
1118+
if self.is_harmonic_safe:
1119+
from devito.finite_differences.elementary import Min # noqa
1120+
from devito.symbolics import retrieve_functions # noqa
1121+
points = filter_ordered(retrieve_functions(retval))
1122+
retval = retval._inv(Min(*points, evaluate=False), safe=True)
1123+
elif self.is_harmonic:
1124+
retval = retval._inv(self.function.subs(subs))
11171125

11181126
return retval
11191127

‎tests/test_differentiable.py‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,32 @@ def test_avg_mode(ndim, io):
160160
for c, arg in zip(ndcoeffs.flatten(), args, strict=True)))
161161
assert sympy.simplify(b_avg.args[0] - expected) == 0
162162
assert isinstance(b_avg, SafeInv)
163-
assert b_avg.base == b
163+
# Guarded by every point the mean reads
164+
assert {str(i) for i in b_avg.base.args} == {str(b.subs(arg)) for arg in args}
165+
166+
167+
@pytest.mark.parametrize('ndim', [2, 3])
168+
@pytest.mark.parametrize('io', [2, 4])
169+
def test_safe_harmonic_zero_neighbour(ndim, io):
170+
"""
171+
The safe harmonic mean is zero wherever it reads a zero, as the harmonic
172+
mean is, and never divides by it.
173+
"""
174+
grid = Grid([8]*ndim)
175+
v = Function(name='v', grid=grid, staggered=grid.dimensions, space_order=4)
176+
b = Function(name='b', grid=grid, space_order=4, interp_order=io,
177+
avg_mode='safe_harmonic')
178+
b.data_with_halo[:] = 2.
179+
b.data[(4,)*ndim] = 0.
180+
181+
Operator(Eq(v, b)).apply()
182+
183+
# Every point whose mean reads the zero one
184+
zeros = {tuple(i) for i in np.argwhere(v.data == 0)}
185+
assert zeros == set(product(range(4 - io//2, 4 + io//2), repeat=ndim))
186+
assert np.all(np.isfinite(v.data))
187+
if io == 2:
188+
assert np.allclose(v.data[v.data != 0], 2.)
164189

165190

166191
def test_no_interp():

0 commit comments

Comments
 (0)