Skip to content

Guard against FP underflow#324

Merged
marvinfriede merged 4 commits intomainfrom
fix-fp-underflow
Dec 10, 2025
Merged

Guard against FP underflow#324
marvinfriede merged 4 commits intomainfrom
fix-fp-underflow

Conversation

@marvinfriede
Copy link
Copy Markdown
Member

For systems with coordination numbers much larger than the largest reference coordination number, the norm in the calculation of the Gaussian weights can become tiny. This can trigger floating-point underflows that lead to essentially random values. This also made the correctness depend on the compilation settings (ifort with -O3 was correct, -O0 was incorrect).

We now guard against tiny norms and set problematic cases to zero.

Related: grimme-lab/xtb#1190.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This pull request addresses floating-point underflow issues in Gaussian weight calculations for the D4 dispersion model. When coordination numbers are significantly larger than reference coordination numbers, the normalization values can become extremely small, triggering floating-point underflows that produce inconsistent results depending on compiler optimization levels (e.g., ifort -O3 vs -O0).

Key Changes:

  • Added guard against tiny norm values before inversion using eps_norm = tiny(1._wp)**0.5_wp
  • Set problematic norm values to zero to ensure consistent behavior across optimization levels
  • Added additional checks with norm < 1e-7_wp threshold in conditional logic
  • Included explanatory comment documenting the optimization-dependent behavior

Reviewed changes

Copilot reviewed 2 out of 3 changed files in this pull request and generated 6 comments.

File Description
src/dftd4/model/d4.f90 Added norm underflow guards and additional checks in weight_references subroutine to prevent FP underflow in C6 coefficient calculations
src/dftd4/model/d4s.f90 Applied same norm underflow guards and checks in system-specific D4 model variant
.gitignore Added .venv* pattern to ignore Python virtual environment directories

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown

Copilot AI commented Dec 9, 2025

@marvinfriede I've opened a new pull request, #326, to work on those changes. Once the pull request is ready, I'll request review from you.

* Replace norm < 1e-7_wp with norm == 0.0_wp for consistency

Co-authored-by: marvinfriede <[email protected]>

---------

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: marvinfriede <[email protected]>
@marvinfriede marvinfriede merged commit b2238de into main Dec 10, 2025
119 of 126 checks passed
@marvinfriede marvinfriede deleted the fix-fp-underflow branch December 10, 2025 06:59
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.

Large difference between D3 and D4 forces for inorganics

3 participants