Skip to content

Fix(cell): reject invalid k-point counts and Monkhorst-Pack grid dimensions - #8353

Merged
mohanchen merged 5 commits into
deepmodeling:developfrom
DanielZhao0432:fix/kpt_mp_grid_validation
Nov 22, 2026
Merged

mohanchen merged 5 commits into
deepmodeling:developfrom
DanielZhao0432:fix/kpt_mp_grid_validation

Conversation

@DanielZhao0432

Copy link
Copy Markdown
Collaborator

Reminder

  • I have read AGENTS.md and docs/developers_guide/agent_governance.md.
  • I have linked an issue or explained why this PR does not need one.
  • I have added adequate unit tests and/or case tests, or explained why not.
  • I have listed the exact verification commands run and their results.
  • I have described user-visible behavior changes, including INPUT parameter changes.
  • I have explained core-module impact for source/ changes.
  • I have requested any needed governance exception below.

Linked Issue

Fixes #8352.

What's changed?

  • K_Vectors::parse_kfile(): reject nkstot < 0 next to the existing MAX_KPOINTS check (previously a negative count fell through both branches and the function returned success);
  • K_Vectors::read_mp_mesh(): require successful extraction of the three Monkhorst-Pack grid dimensions, all positive, with the product in int range;
  • QList::read_from_file(): the same grid validation, in its existing warning-and-reset style.

Bug confirmation (empirical)

A small throwaway driver through the public read_kpoints path, compiled directly against the sources, confirmed on current develop before the fix:

  • total count -4 -> success with nkstot=-4
  • grid 0 4 4 -> success with nkstot=0
  • grid x 4 4 -> success (extraction failure unnoticed, nmp stays {0,0,0})
  • grid -1 4 4 -> uncaught std::length_error crash in renew()

After the fix all four are rejected; a valid 2 2 2 grid still yields 8 k-points.

Unit Tests and/or Case Tests

  • New source/source_cell/unittests/test_klist.cpp (7 cases: negative total count, zero/non-numeric/negative MP dimension, grid product overflow, valid grid control, malformed QPT grid reset).
  • Commands run locally: the throwaway driver above; python3 tools/03_code_analysis/agent_governance_check.py --staged — pass.
  • No full local build or ctest run was performed; that verification is delegated to CI.

User-visible behavior changes

Malformed KPT/QPT files are now rejected with a clear message instead of being silently accepted or crashing the program. Valid files are unaffected. No INPUT/STRU changes.

Governance Notes

  • Core module impact: source_cell input parsing only; no interface changes.
  • Documentation: not updated — the documented KPT/QPT format is unchanged; this PR only adds validation of malformed files.
  • Exceptions requested: none.

Confirmed against current develop with a small driver through the public
read_kpoints path: a negative k-point count is silently accepted
(nkstot=-4 returns success), a zero or non-numeric Monkhorst-Pack grid
dimension is accepted with nkstot=0, and a negative dimension crashes
with an uncaught std::length_error from renew().

- parse_kfile(): reject nkstot < 0 next to the existing MAX_KPOINTS check;
- read_mp_mesh(): require successful extraction of all three grid
  dimensions, all positive, with the product within int range;
- QList::read_from_file(): apply the same grid validation, following its
  existing warning-and-reset style.

Refs deepmodeling#8352.
@DanielZhao0432 DanielZhao0432 changed the title Fix(source_cell): reject invalid k-point counts and Monkhorst-Pack grid dimensions Fix(cell): reject invalid k-point counts and Monkhorst-Pack grid dimensions Nov 21, 2026
@DanielZhao0432 DanielZhao0432 added the Refactor Refactor ABACUS codes label Nov 21, 2026
The cell and domain_decomposition object libraries reference
NeighborSearch::init/build_neighbors/refresh_mdcell; mirror the
mdcell_adapter test target, which compiles the three module_neighlist
sources into the test executable.

@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.

Two malformed-grid cases remain unhandled: two negative dimensions pass the guard, and the product can overflow before validation. Details and a regression-test suggestion are inline.

Static source review at 3da188d2e5bf9712e8c0f05d453eb47f3c79e461; no builds or tests were run, and CI was not independently verified.

Comment thread source/source_cell/klist.cpp Resolved
Comment thread source/source_cell/klist.cpp Resolved
Address review on deepmodeling#8353: a negative dimension pair (-1 -1 4) has a
positive product and passed the product-sign check, and the long long
product itself can overflow for extreme dimensions. Validate each
dimension individually and compare before multiplying
(nmp[0] <= INT_MAX/nmp[1], then nmp[0]*nmp[1] <= INT_MAX/nmp[2]), in
both the KPT and QPT readers. Verified with the local driver: -1 -1 4
and 2097152^3 are now rejected, valid grids unaffected.

Also make the QPT regression test discriminating: a negative dimension
crashed the old code, instead of the zero-dimension case that left
nkstot==0 in both old and new implementations.
2097152^3 = 2^63 overflows even a 64-bit signed product; the guard
compares before multiplying, and these cases pin that behavior down.
@mohanchen mohanchen added the Input&Output Suitable for coders without knowing too many DFT details label Nov 22, 2026
@mohanchen
mohanchen merged commit a0e295b into deepmodeling:develop Nov 22, 2026
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Input&Output Suitable for coders without knowing too many DFT details Refactor Refactor ABACUS codes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: [source_cell] KPT/QPT parsing accepts negative k-point counts and malformed Monkhorst-Pack grids

3 participants