Skip to content

Make the safe harmonic mean zero next to any zero point - #3129

Open
mloubout wants to merge 1 commit into
mainfrom
safe-harmonic-contact
Open

mloubout wants to merge 1 commit into
mainfrom
safe-harmonic-contact

Conversation

@mloubout

@mloubout mloubout commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

safe_harmonic averaging returned a nonzero value next to a zero point unless that point was the reference one, the point the off-grid location is indexed from.

The mean drops a zero point instead of dividing by it: SafeInv(f, f) is 0 there. The final inversion was then guarded by the reference point alone, SafeInv(avg, f(x, y)). So a zero elsewhere in the stencil just left the mean over the other points, inflated by the missing term. With one zero cell in a field of ones, three of the four staggered corners around it come out at 4/3 instead of 0:

grid = Grid(shape=(4, 4))
mu = Function(name='mu', grid=grid, avg_mode='safe_harmonic')
mu.data[:] = 1.
mu.data[2, 2] = 0.
out = Function(name='out', grid=grid, staggered=grid.dimensions)
Operator(Eq(out, mu)).apply()
# out.data[1:3, 1:3] was [[4/3, 4/3], [4/3, 0]], now all 0

The harmonic mean is zero next to any zero, and that is what the averaging is used for: the shear modulus of an elastic model with a fluid in it, which must vanish on every face that touches the fluid. With the reference-point guard it vanished only on faces whose reference node is the fluid one, so one side of every fluid–solid step carried a spurious shear modulus. On a stepped seafloor in an elastic model this changed the recorded gather by about 20% against SOFI2D, which zeroes all four (av_mue.c). With this change the two agree to 0.3–0.7% at three grid spacings.

Change. The final inversion of the safe mean is now guarded by the smallest of the points the mean reads: Min over the function accesses of the interpolated expression, instead of the reference point. The per-point SafeInv guard is unchanged, so nothing is ever divided by zero and no NaN can appear, whatever the interpolation weights. Plain harmonic is untouched. At higher interpolation orders the mean is now zero wherever its stencil reads a zero, which is also where plain harmonic returns zero.

Tests.

  • New test_safe_harmonic_zero_neighbour, 2D and 3D, interpolation orders 2 and 4: one zero point must give zero at every staggered point whose mean reads it, no NaN, and the plain value elsewhere. It fails on main and passes here.
  • test_avg_mode now checks that the guard covers every point the mean reads.
  • tests/test_differentiable.py and the elastic and viscoelastic seismic examples pass. Their reference norms are unchanged: the example models have no zero shear modulus.

@codecov

codecov Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 90.90909% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 84.00%. Comparing base (2681402) to head (0ffee9a).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
devito/types/basic.py 71.42% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3129      +/-   ##
==========================================
+ Coverage   83.96%   84.00%   +0.04%     
==========================================
  Files         261      261              
  Lines       60017    60036      +19     
  Branches     5065     5067       +2     
==========================================
+ Hits        50393    50435      +42     
+ Misses       8780     8757      -23     
  Partials      844      844              
Flag Coverage Δ
pytest-gpu-aomp-amdgpuX 68.25% <0.00%> (-0.02%) ⬇️
pytest-gpu-gcc- 78.80% <90.90%> (+0.09%) ⬆️
pytest-gpu-icx- 78.76% <90.90%> (+36.50%) ⬆️
pytest-gpu-nvc-nvidiaX 69.08% <0.00%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@mloubout
mloubout force-pushed the safe-harmonic-contact branch 2 times, most recently from 0bf32b4 to f8349b5 Compare December 1, 2026 03:28
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.
@mloubout
mloubout force-pushed the safe-harmonic-contact branch from f8349b5 to 0ffee9a Compare December 1, 2026 16:30
@mloubout mloubout added the API api (symbolics, types, ...) label Dec 1, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

API api (symbolics, types, ...)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant