Skip to content

fix: prevent floating-point truncation in spatial location indexing - #122

Closed
tee2s wants to merge 1 commit into
ar4:masterfrom
tee2s:fix/location-interpolation-truncation
Closed

fix: prevent floating-point truncation in spatial location indexing#122
tee2s wants to merge 1 commit into
ar4:masterfrom
tee2s:fix/location-interpolation-truncation

Conversation

@tee2s

@tee2s tee2s commented Aug 28, 2026

Copy link
Copy Markdown

Summary

Fixes a spatial grid indexing bug where floating-point rounding errors cause location indices to drop/shift by an entire grid cell during interpolation.

Root Cause

In location_interpolation.py, locations is derived directly from floating-point distances via (location + x).long(). Floating-point inaccuracies can yield values like -0.99999994 instead of -1.0. Because PyTorch’s .long() truncates toward zero rather than flooring, the resulting index truncates to 0 instead of -1.

Fix

Decouple integer grid indices from the floating-point distance tensor x. Calculate locations directly using integer offsets via torch.arange(...) + int(location).

Minimal Reproduction

import torch

halfwidth = 2
dtype = torch.float32 
location = 31.4
    
# 1. The library's approach
x = torch.arange(-halfwidth+1, halfwidth+1, dtype=dtype) - location + int(location)
buggy_locations = (location + x).long()
    
# 2. Correct alternative approach
correct_locations = torch.arange(-halfwidth+1, halfwidth+1) + int(location)
    
print(f"Raw float math:  {(location + x).tolist()}")
print(f"Buggy indices:   {buggy_locations.tolist()}")
print(f"Correct indices: {correct_locations.tolist()}")

Floating-point precision errors in (location + x).long() can produce values like -0.99999994 instead of -1.0. Because PyTorch's .long() truncates toward zero, index values shift by an entire grid cell.

Decouple integer grid indices from the floating-point distance calculations by computing locations via torch.arange(...) + int(location).
@ar4

ar4 commented Aug 30, 2026

Copy link
Copy Markdown
Owner

Hello @tee2s and thank you for this clear explanation of the problem and clean fix. I hope that the bug did not cause problems for you.

I am in the final stages of major revision of Deepwave. One of the areas that has received the most attention is this location interpolation feature. In addition to issues such as the one you identified, I think the current implementation was also unnecessarily complicated for users, especially for the staggered grid propagators (acoustic and elastic).

This issue is already fixed in the revision, but I will add your new test, which I am grateful for.

Thank you again. I hope that you continue to find Deepwave useful.

@ar4 ar4 closed this Aug 30, 2026
@tee2s

tee2s commented Aug 31, 2026

Copy link
Copy Markdown
Author

Hi @ar4 , thanks for the fast reply! Glad to hear this is already resolved in the new revision. I really appreciate your work on Deepwave, it’s been a great tool to use. Also, this bug didn't cause any major issues for me!

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants