Mitigating catastrophic cancellation error in density-based gradient calculation#424
Merged
Luthaf merged 2 commits intometatensor:mainfrom Mar 18, 2026
Merged
Conversation
Member
|
Thanks for the PR @TheophaneBernhard! I'll run CI to make sure all tests are passing and then I can merge this. |
Member
|
Ok, I know what's causing the CI failure, and it is unrelated to your code. I'll fix it ASAP |
Contributor
Author
|
Ah ok, thanks! |
This was referenced Feb 27, 2026
Member
|
OK, so the CI error is relevant here, the failing test (you can reproduce locally with I'll need to find some time to dig deeper in the actual error to see if it is safe to ignore or not |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Decreasing the threshold value for
sin_thetafor the gradient calculation of spherical harmonics seem to increase the accuracy of the computed atom-density spherical expansion coefficient for atoms close in XY (small theta angle). This suggests that the original formula is numerically more accurate than the one used whensin_theta<threshold. We think that the latter suffers from "catastrophic cancellation" of digits when subtracting pairs of associated Legendre polynomials, hitting numerical precision.I report here the error between the analytical gradient provided by old and new featomic implementations and the gradient calculated using a central finite difference scheme performed with decreasing atomic displacements. The plots show the absolute error between representative spherical harmonics components, taking the mean over radial channels, for l=3,6 angular orders, at different m values, and for the specific case where the displaced atom is close in XY with respect to the central atom.
One can observe that the convergence is vastly improved when decreasing the threshold value.
The python script to perform these tests is attached.
analysis.py