Fix numerical cancellation in RectLattice::distance for large pitch values#3853
Fix numerical cancellation in RectLattice::distance for large pitch values#3853MatteoZammataro wants to merge 5 commits intoopenmc-dev:developfrom
Conversation
Changed the boundary-crossing detection logic to avoid cancellation issues.
5120892 to
caf3747
Compare
|
This problem is very similar to #3792. |
|
Good idea, thanks for the suggestion! We could refactor the logic into a small utility like: The boundary-crossing detection in lattice.cpp would then become: and similarly in mesh.cpp, e.g. Does this look close to what you had in mind? If so, do you have any suggestion on where the isclose function should be placed? |
|
It is close to what I had in mind. |
|
Done! I have a doubt about the relative tolerance that should be used in the lattice. At the moment, I used 1E-12, but it is a guess value. We could eventually consider adding a constant variable named something like "RECT_LATTICE_TOL" to be placed in constants.h. |
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 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)