Skip to content

Fix(md): reject invalid NPT barostat velocity scaling - #8425

Open
samomoyed wants to merge 7 commits into
deepmodeling:developfrom
samomoyed:fix-npt-barostat-scale
Open

samomoyed wants to merge 7 commits into
deepmodeling:developfrom
samomoyed:fix-npt-barostat-scale

Conversation

@samomoyed

@samomoyed samomoyed commented Nov 27, 2026 •

Copy link
Copy Markdown

Linked Issue

Refs #6681

What's changed?

The NPT barostat scale can underflow to zero and later produce NaN when multiplied by an overflowing exponential. The invalid value then propagates to the cell, causing an assertion failure during the next MD step.

This adds checks for a non-positive or non-finite scale and non-finite barostat kinetic energy. The calculation exits with a message asking the user to check md_pfreq and md_dt.

Unit Tests and/or Case Tests for my changes

Tested the attached test.zip with a patched v3.11.0-beta13 CUDA build on an RTX 4090, using one MPI process and 12 OpenMP threads.

  • With the original md_pfreq = 1.0, the new check caught the invalid scale and exited with code 1.
  • Changing only md_pfreq to 0.005 allowed all 10 MD steps to finish with exit code 0. SCF did not converge at steps 7–9.
  • Added four regression tests in test_nhchain.cpp. All 10 NHC tests passed locally.
  • Removing the guards made all four new tests fail. Restoring them made all tests pass.
  • All CI checks on commit 646e50e passed.

Governance Notes

The change adds runtime checks in nhchain.cpp and regression tests in test_nhchain.cpp. A friend declaration in nhchain.h gives the existing test fixture access to the required internal state. No new INPUT parameters are added.

Added checks for finite scale and kinetic energy in NPT barostat calculations.
@Stardust0831

Copy link
Copy Markdown
Collaborator

Thanks for tracking down the underflow in scale. I suspect the underlying issue is numerical instability in the barostat update. Exiting early prevents NaN from propagating, but it doesn’t address what causes the underflow. I’ll use the test case from #6681 to reproduce the issue and trace where the update starts to become unstable.

@mohanchen mohanchen added MD & LAM MD and Larege Atomic Models Refactor Refactor ABACUS codes labels Nov 27, 2026

@ABACUS-PR-BOT ABACUS-PR-BOT 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.

The new guards need the stray source + prefixes removed before this revision can compile. A focused regression-test suggestion is also included inline.

Static review only; no builds or runtime tests were run.

Comment thread source/source_md/nhchain.cpp Outdated
scale *= exp(-v_peta[0] * delta / 2.0);
kecurrent = ke_omega * scale * scale;
scale *= exp(-v_peta[0] * delta / 2.0);
+ if (!std::isfinite(scale) || scale <= 0.0)

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.

The committed source contains ten literal leading + characters in the two new guard blocks, including + if here. These are invalid C++ tokens, and nhchain.cpp is compiled by the unconditional md target even when NPT is unused. Please remove the stray patch markers from both blocks.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Removed the stray patch markers from both blocks. The build checks are now passing.

Comment thread source/source_md/nhchain.cpp Outdated
+ "Invalid NPT barostat velocity scale. Check md_pfreq and md_dt.");
+ }
kecurrent = ke_omega * scale * scale;
+ if (!std::isfinite(kecurrent))

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.

Non-blocking: please add focused cases in test_nhchain.cpp for zero/non-finite scale and non-finite kecurrent with a finite scale. The existing tests cover ordinary integration and restart behavior; assert the expected nonzero exit for each rejection path. Retain the existing finite-trajectory reference checks.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Added four tests in test_nhchain.cpp covering zero, infinite and NaN scale, and non-finite kinetic energy with a finite scale. Each checks for exit code 1. The existing trajectory and restart tests are retained.

All 10 NHC tests passed locally. I also checked that removing the guards makes the four new tests fail. The latest CI checks are all passing.

@Stardust0831

Copy link
Copy Markdown
Collaborator

Thanks for sharing the test case. After reproducing it on beta13, I agree with your diagnosis of md_pfreq. With the original input, the cell expanded to about six times its initial volume in the first 0.5 fs, before scale underflowed. Given that behavior, I think stopping at the invalid scale and pointing users to md_pfreq and md_dt is helpful. It gives a much clearer diagnosis than the cell error on the following step.

Out of curiosity, I also tried adaptive substeps for the barostat thermostat while keeping the original md_pfreq. This got past the NaN at 1.0 fs, but the cell volume still oscillated sharply and the run stopped at 1.5 fs. That reinforced my view that reducing md_pfreq is the right first step for this case. The instability we observed was triggered by the overly aggressive barostat setting. I’ve shared the experiment here for reference: experimental branch.

@samomoyed

Copy link
Copy Markdown
Author

Thanks for tracking down the underflow in scale. I suspect the underlying issue is numerical instability in the barostat update. Exiting early prevents NaN from propagating, but it doesn’t address what causes the underflow. I’ll use the test case from #6681 to reproduce the issue and trace where the update starts to become unstable.

Thanks for looking into this. I tested the same case with only md_pfreq changed from 1.0 to 0.005. It completed all 10 MD steps, though SCF did not converge at steps 7–9. I have also added regression tests for the new checks, and all CI checks are now passing.

@samomoyed

Copy link
Copy Markdown
Author

Thanks for sharing the test case. After reproducing it on beta13, I agree with your diagnosis of md_pfreq. With the original input, the cell expanded to about six times its initial volume in the first 0.5 fs, before scale underflowed. Given that behavior, I think stopping at the invalid scale and pointing users to md_pfreq and md_dt is helpful. It gives a much clearer diagnosis than the cell error on the following step.

Out of curiosity, I also tried adaptive substeps for the barostat thermostat while keeping the original md_pfreq. This got past the NaN at 1.0 fs, but the cell volume still oscillated sharply and the run stopped at 1.5 fs. That reinforced my view that reducing md_pfreq is the right first step for this case. The instability we observed was triggered by the overly aggressive barostat setting. I’ve shared the experiment here for reference: experimental branch.

Thanks for reproducing the case and trying the adaptive substeps. Your results agree with the comparison using md_pfreq = 0.005. I have also added four regression tests for the rejection paths. All checks on the latest commit are now passing.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

MD & LAM MD and Larege Atomic Models Refactor Refactor ABACUS codes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants