Skip to content

Fix numerical cancellation in RectLattice::distance for large pitch values#3853

Open
MatteoZammataro wants to merge 3 commits intoopenmc-dev:developfrom
MatteoZammataro:rectlattice_fix
Open

Fix numerical cancellation in RectLattice::distance for large pitch values#3853
MatteoZammataro wants to merge 3 commits intoopenmc-dev:developfrom
MatteoZammataro:rectlattice_fix

Conversation

@MatteoZammataro
Copy link
Contributor

@MatteoZammataro MatteoZammataro commented Mar 5, 2026

This PR fixes a segmentation fault in RectLattice::distance that was triggered by large lattice pitch values (see #3852). This bug occurred due to floating-point cancellation in the boundary-crossing detection logic.

As a simple fix, I suggest computing each axis distance (dx, dy, dz) independently and updating lattice_trans as the minimum is determined. This should eliminate the risk of floating-point cancellation.

Credits to @MatteoF29 for the help.

Checklist

  • I have performed a self-review of my own code
  • I have run clang-format (version 18) on any C++ source files (if applicable)
  • I have followed the style guidelines for Python source files (if applicable)
  • I have made corresponding changes to the documentation (if applicable)
  • I have added tests that prove my fix is effective or that my feature works (if applicable)

Changed the boundary-crossing detection logic to avoid cancellation issues.
@MatteoZammataro MatteoZammataro marked this pull request as ready for review March 5, 2026 16:32
@GuySten
Copy link
Contributor

GuySten commented Mar 7, 2026

This problem is very similar to #3792.
I suggest we refactor that solution into an isclose function and use it here and there.

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