Skip to content

fix(lammps): validate dplr and spin options#5842

Merged
njzjz merged 3 commits into
deepmodeling:masterfrom
njzjz-bot:fix/lammps-dplr-spin-option-parsing-5645
Jul 24, 2026
Merged

fix(lammps): validate dplr and spin options#5842
njzjz merged 3 commits into
deepmodeling:masterfrom
njzjz-bot:fix/lammps-dplr-spin-option-parsing-5645

Conversation

@njzjz-bot

@njzjz-bot njzjz-bot commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

Summary

  • validate fix dplr model and electric-field values before reading them
  • report odd type_associate lists through a normal LAMMPS input error instead of an assertion
  • validate relative, relative_v, virtual_len, and spin_norm values in both pair parsers and parse numeric values strictly
  • add subprocess-isolated regressions for truncated options and keywords supplied where values are required

Why existing tests missed the bugs

  • DPLR tests supplied a model path, all three efield components, and paired type_associate entries, so they never reached the off-by-one reads or assertion path.
  • DeepMD and DeepSpin model-deviation tests always supplied values after relative and relative_v, so the parsers never read beyond the final argument.
  • No parser tests exercised virtual_len or spin_norm with missing values or a following keyword, leaving their unchecked loops uncovered.
  • The new negative tests run LAMMPS in subprocesses because the old out-of-bounds behavior could abort or segfault instead of raising a catchable input error.

Validation

  • cmake --build source/build --target deepmd_lmp -j2
  • pytest source/lmp/tests/test_lammps_option_parsers.py -q (18 passed)
  • representative existing DPLR, DeepMD relative-virial, and DeepSpin tests (4 passed)
  • ruff format .
  • ruff check .

Fixes #5645

Coding agent: Codex
Codex version: codex-cli 0.144.4
Model: gpt-5.6-sol
Reasoning effort: xhigh

Summary by CodeRabbit

  • Bug Fixes

    • Improved validation for DeePMD/DeepSpin and related options, so truncated or malformed argument lists are rejected safely instead of being mis-parsed.
    • Updated error messages to clearly identify the offending fix dplr options and invalid values.
    • Enforced stricter numeric parsing for relative quantities and spin-related vectors, and added a runtime check requiring an even number of associated atom types.
  • Tests

    • Added regression tests covering malformed/incomplete DeePMD, DeepSpin, and fix dplr option lists, while confirming valid DeepSpin configurations still succeed.

Reject truncated or keyword-valued DPLR and model-deviation options before reading them. Replace the type_associate assertion with a normal LAMMPS input error and use strict numeric conversion for spin parser values.

Coding-Agent: Codex
Codex-Version: codex-cli 0.144.4
Model: gpt-5.6-sol
Reasoning-Effort: xhigh
@dosubot dosubot Bot added the bug label Jul 16, 2026
@coderabbitai

coderabbitai Bot commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: c4dd26d4-b0e7-47d7-8a8d-0a56330a5e8c

📥 Commits

Reviewing files that changed from the base of the PR and between 5f65a39 and 085f9e6.

📒 Files selected for processing (4)
  • source/lmp/pair_base.cpp
  • source/lmp/pair_base.h
  • source/lmp/pair_deepmd.cpp
  • source/lmp/pair_deepspin.cpp
🚧 Files skipped from review as they are similar to previous changes (2)
  • source/lmp/pair_deepmd.cpp
  • source/lmp/pair_deepspin.cpp

📝 Walkthrough

Walkthrough

LAMMPS DeePMD, DeepSpin, and DPLR option parsing now validates required values, rejects keyword tokens in value positions, uses shared numeric parsing, and includes subprocess-based regression tests.

Changes

LAMMPS parser hardening

Layer / File(s) Summary
DeePMD and DeepSpin option validation
source/lmp/pair_base.*, source/lmp/pair_deepmd.cpp, source/lmp/pair_deepspin.cpp
Scalar options use utils::numeric; spin-vector options use shared dimension-checked parsing with missing and keyword-value validation.
DPLR option validation
source/lmp/fix_dplr.cpp
model and efield validate required values and keyword boundaries, while type_associate reports odd argument counts through a runtime error.
Malformed option regression tests
source/lmp/tests/test_lammps_option_parsers.py
Isolated LAMMPS subprocess tests verify diagnostic failures for malformed options and successful parsing of complete DeepSpin values.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Suggested labels: Python, C++

Suggested reviewers: outisli, wanghan-iapcm

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title is concise and accurately reflects the main change: hardening DPLR and spin option validation.
Linked Issues check ✅ Passed The PR addresses the required DPLR and spin parser bounds checks, keyword rejection, runtime errors, and regression tests.
Out of Scope Changes check ✅ Passed The shared spin-option helper and new tests are directly tied to the parser hardening goals.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@codecov

codecov Bot commented Jul 17, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 43.24324% with 21 lines in your changes missing coverage. Please review.
✅ Project coverage is 78.64%. Comparing base (6c3b985) to head (085f9e6).
⚠️ Report is 27 commits behind head on master.

Files with missing lines Patch % Lines
source/lmp/pair_base.cpp 25.00% 8 Missing and 1 partial ⚠️
source/lmp/pair_deepmd.cpp 33.33% 5 Missing and 1 partial ⚠️
source/lmp/pair_deepspin.cpp 55.55% 2 Missing and 2 partials ⚠️
source/lmp/fix_dplr.cpp 71.42% 0 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #5842      +/-   ##
==========================================
+ Coverage   78.58%   78.64%   +0.06%     
==========================================
  Files        1050     1054       +4     
  Lines      120637   121768    +1131     
  Branches     4356     4408      +52     
==========================================
+ Hits        94801    95769     +968     
- Misses      24278    24426     +148     
- Partials     1558     1573      +15     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@njzjz

njzjz commented Jul 18, 2026

Copy link
Copy Markdown
Member

Possible reviewers based on changed lines, exact file history, and exact-file review history:

  • @wanghan-iapcm — 12 commits on changed files; 16 reviews on exact changed files (source/lmp/fix_dplr.cpp, source/lmp/pair_deepmd.cpp, source/lmp/pair_deepspin.cpp).
  • @OutisLi — 24 reviews on exact changed files (source/lmp/pair_deepmd.cpp, source/lmp/pair_deepspin.cpp).

No review request was made automatically.

Coding agent: Codex
Codex version: codex-cli 0.144.4
Model: gpt-5.6-sol
Reasoning effort: xhigh

@njzjz
njzjz requested review from OutisLi and wanghan-iapcm and removed request for OutisLi and wanghan-iapcm July 18, 2026 07:19

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

LGTM. Correctly hardens the LAMMPS option parsers: the bounds guards fix the real iarg+N > narg off-by-one that allowed an arg[narg] overread, is_key uses fixed exact-string keyword lists (so no numeric value or v_ variable is misclassified), the atof->utils::numeric and assert->error->all migrations are correct, and pair_deepmd/pair_deepspin stay byte-for-byte parallel. The subprocess-isolated negative tests genuinely fail pre-fix. Minor sub-threshold note for a follow-up: the virtual_len/spin_norm validation is byte-identical in both pair files but only exercised for pair_style deepspin — a pair_style deepmd case (or a shared helper) would guard against future drift.

Move virtual_len and spin_norm bounds, keyword, and numeric validation into PairDeepBaseModel so pair_style deepmd and deepspin cannot drift. Existing deepspin subprocess tests now exercise the shared implementation.

Coding-Agent: Codex
Codex-Version: codex-cli 0.144.6
Model: gpt-5.6-sol
Reasoning-Effort: xhigh
@njzjz

njzjz commented Jul 24, 2026

Copy link
Copy Markdown
Member

Followed up on the approved review’s drift note in 085f9e6. A direct pair_style deepmd dimension-error case is not currently reachable because the DeepPot C++ backends report numb_types_spin == 0; the parser therefore has no spin-vector values to validate. I used the review’s suggested alternative instead: virtual_len and spin_norm bounds checks, keyword rejection, numeric parsing, and argument advancement now live in one PairDeepBaseModel::parse_spin_vector_option helper called by both pair styles.

Validation: rebuilt the deepmd_lmp target successfully and ran the complete source/lmp/tests/test_lammps_option_parsers.py suite with the rebuilt plugin (18 passed). The clang-format hook, ruff format ., ruff check ., and git diff --check also passed.

Coding agent: Codex
Codex version: codex-cli 0.144.6
Model: gpt-5.6-sol
Reasoning effort: xhigh

@njzjz
njzjz enabled auto-merge July 24, 2026 15:13
@njzjz
njzjz added this pull request to the merge queue Jul 24, 2026
Merged via the queue into deepmodeling:master with commit e5fdff0 Jul 24, 2026
57 of 58 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Code scan] Harden LAMMPS DPLR and spin option argument parsing

3 participants