Skip to content

Fix numerical instability in D3 reference-weight derivatives - #8103

Merged
mohanchen merged 1 commit into
deepmodeling:developfrom
Growl1234:vdW
Oct 11, 2026
Merged

mohanchen merged 1 commit into
deepmodeling:developfrom
Growl1234:vdW

Conversation

@Growl1234

@Growl1234 Growl1234 commented Oct 9, 2026 •

Copy link
Copy Markdown

Fixes #8106.

The current D3 implementation calculates the derivatives of normalized reference weights as follows:

double derivative = raw_derivative * normalization
    - raw[reference] * normalization_derivative
        * normalization * normalization;

This expression is mathematically correct. However, when the coordination number (CN) is much higher than the reference values, the raw weights become extremely small. Floating-point underflow and cancellation can then produce incorrect derivatives, leading to unphysically large forces. This issue can be reproduced with an Mg slab (CN ≈ 12).

This PR replaces the calculation with log-sum-exp normalization and a numerically stable analytical derivative. By subtracting the maximum exponent before evaluating the weights, at least one scaled weight is exactly 1, preventing the normalization sum from underflowing. The derivative is then expressed directly in terms of normalized weights and the weighted average reference CN, avoiding the subtraction of large intermediate terms that should mathematically cancel. This ensures numerically stable derivatives even when the actual CN is far beyond the reference range. A regression test for high-CN Mg is also added.

Copilot AI balanced review requested due to automatic review settings October 9, 2026 10:34

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@mohanchen mohanchen added the Refactor Refactor ABACUS codes label Oct 9, 2026

@ZhouXY-PKU ZhouXY-PKU left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can link an issue with wrong results of Mg slabs?

Comment thread source/source_hamilt/module_vdw/vdwd3_evaluator.cpp
Comment thread source/source_hamilt/module_vdw/vdwd3_evaluator.cpp Outdated
Comment thread source/source_hamilt/module_vdw/test/vdwd3_evaluator_test.cpp
Comment thread source/source_hamilt/module_vdw/test/vdwd3_evaluator_test.cpp
Comment thread source/source_hamilt/module_vdw/test/vdwd3_evaluator_test.cpp

@ZhouXY-PKU ZhouXY-PKU left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice job!

@mohanchen mohanchen added the Bugs Bugs that only solvable with sufficient knowledge of DFT label Oct 11, 2026
@mohanchen
mohanchen merged commit 403a40c into deepmodeling:develop Oct 11, 2026
17 checks passed
@Growl1234
Growl1234 deleted the vdW branch October 11, 2026 11:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bugs Bugs that only solvable with sufficient knowledge of DFT Refactor Refactor ABACUS codes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] wrong forces and stress from DFT-D3

4 participants