Skip to content

improves the robustness of VASP OUTCAR energy extraction#24

Merged
floatingCatty merged 4 commits intodeepmodeling:mainfrom
AsymmetryChou:etot_add
Nov 26, 2025
Merged

improves the robustness of VASP OUTCAR energy extraction#24
floatingCatty merged 4 commits intodeepmodeling:mainfrom
AsymmetryChou:etot_add

Conversation

@AsymmetryChou
Copy link
Collaborator

@AsymmetryChou AsymmetryChou commented Nov 23, 2025

This pull request improves the robustness of VASP OUTCAR energy extraction by adding a check for successful run completion and enhancing test coverage. Now, when extracting the total energy from an OUTCAR file, the parser logs a warning if the file does not indicate successful completion, helping users identify potentially unreliable results. The test suite is expanded to verify this new behavior.

VASP OUTCAR energy extraction improvements:

  • Updated read_total_energy in vasp_parser.py to check for the presence of the "Voluntary context switches" line, logging a warning if it is missing (indicating an unsuccessful run), while still extracting the energy if possible.

Test enhancements:

  • Added test_vasp_unsuccessful_completion_warning to verify that a warning is logged when OUTCAR does not indicate successful completion, and that energy extraction still works.
  • Added test_vasp_successful_completion_no_warning to confirm that no warning is logged when OUTCAR indicates a successful run.
  • Registered the new tests in the main test execution block.[Copilot is generating a summary...]

Summary by CodeRabbit

  • Bug Fixes

    • Energy extraction now detects incomplete calculation runs and warns users when energy values may be unreliable.
  • Tests

    • Added test coverage to verify warning behavior for complete and incomplete calculation runs.

✏️ Tip: You can customize this high-level summary in your review settings.

@codecov
Copy link

codecov bot commented Nov 23, 2025

Welcome to Codecov 🎉

Once you merge this PR into your default branch, you're all set! Codecov will compare coverage reports and display results in all future pull requests.

Thanks for integrating Codecov - We've got you covered ☂️

@coderabbitai
Copy link

coderabbitai bot commented Nov 23, 2025

📝 Walkthrough

Walkthrough

The VASP parser's energy reading method is enhanced to detect incomplete OUTCAR runs by checking for a "Voluntary context switches" completion indicator. A warning is issued when energy is extracted from files lacking this indicator. New tests verify the warning is issued or suppressed based on run completion status.

Changes

Cohort / File(s) Summary
VASP energy parsing logic
dftio/io/vasp/vasp_parser.py
Updated read_total_energy() method signature with default parameter file="OUTCAR" and return type annotation ->np.float64. Added success_completion flag detection scanning for "Voluntary context switches". Collects energy (sigma→0), selects last value if multiple found, and asserts its presence. Issues warning when energy is extracted but run completion is not indicated. Expanded docstring with parameter/return documentation and reliability note.
Test coverage
test/test_vasp_energy.py
Added test_vasp_unsuccessful_completion_warning() to verify warning is logged when OUTCAR lacks completion indicator. Added test_vasp_successful_completion_no_warning() to verify no warning when completion indicator is present. Both tests use StringIO-based logging handler to capture output from dftio.io.vasp.vasp_parser logger. Updated test invocation block to execute new tests.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

  • Single, focused feature addition (completion status detection) with straightforward conditional logic
  • Two parallel test functions with consistent structure testing complementary scenarios
  • Minimal cross-file dependencies; changes are localized to parser and its tests

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title directly describes the main change: improving robustness of VASP OUTCAR energy extraction by adding completion detection and warnings.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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 and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (3)
dftio/io/vasp/vasp_parser.py (1)

110-112: Consider memory efficiency for large OUTCAR files.

Reading the entire OUTCAR file into memory with readlines() may be inefficient for very large files. Consider iterating line-by-line instead:

-        with open(file, 'r') as f:
-            data = f.readlines()
-        for line in data:
+        with open(file, 'r') as f:
+            for line in f:

This approach processes the file line-by-line without loading it entirely into memory.

test/test_vasp_energy.py (2)

216-216: Remove unnecessary f-string prefix.

The f-string has no placeholders, making the prefix unnecessary.

Apply this diff:

-            print(f"Unsuccessful completion warning test passed")
+            print("Unsuccessful completion warning test passed")

253-253: Remove unnecessary f-string prefix.

The f-string has no placeholders, making the prefix unnecessary.

Apply this diff:

-            print(f"Successful completion (no warning) test passed")
+            print("Successful completion (no warning) test passed")
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between dd56aa1 and 317869d.

📒 Files selected for processing (2)
  • dftio/io/vasp/vasp_parser.py (1 hunks)
  • test/test_vasp_energy.py (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
test/test_vasp_energy.py (1)
dftio/io/vasp/vasp_parser.py (2)
  • VASPParser (18-125)
  • read_total_energy (94-125)
🪛 Ruff (0.14.5)
test/test_vasp_energy.py

216-216: f-string without any placeholders

Remove extraneous f prefix

(F541)


253-253: f-string without any placeholders

Remove extraneous f prefix

(F541)

🔇 Additional comments (6)
dftio/io/vasp/vasp_parser.py (3)

94-94: LGTM: Good addition of default parameter and return type annotation.

The default parameter and type hint improve the method's usability and clarity.


95-107: LGTM: Comprehensive docstring with appropriate warnings.

The expanded docstring clearly documents the method's behavior and warns users about potential reliability issues with unsuccessful runs.


121-122: LGTM: Clear warning message for unsuccessful completions.

The warning appropriately alerts users when energy is extracted from potentially incomplete runs, including the filename for context.

test/test_vasp_energy.py (3)

184-218: LGTM: Comprehensive test for unsuccessful completion detection.

The test properly verifies that:

  1. Energy extraction still functions for incomplete runs
  2. A warning is logged when the completion indicator is absent

The test structure with log capture is well-implemented.


221-255: LGTM: Thorough test for successful completion (no warning case).

The test properly verifies the absence of warnings when the completion indicator is present, complementing the unsuccessful completion test nicely.


265-266: LGTM: New tests properly registered.

Both new test functions are correctly added to the test execution block.

Comment on lines +108 to +114
success_completion = False
energy = []
with open(file, 'r') as f:
data = f.readlines()
for line in data:
if 'Voluntary context switches' in line:
success_completion = True
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

Verify the reliability of "Voluntary context switches" as a completion indicator.

The implementation uses "Voluntary context switches" as the indicator for successful VASP completion. Please confirm this is a reliable and documented indicator in VASP OUTCAR files.


🌐 Web query:

Is "Voluntary context switches" a reliable indicator of successful completion in VASP OUTCAR files?

💡 Result:

Short answer: It's a useful but not foolproof heuristic — the presence of the final "Voluntary context switches" (the last line of the VASP timing/accounting block) usually means VASP reached its normal end and printed the final accounting, and many scripts rely on it to mark a finished run. However, it is not guaranteed: some abnormal terminations may omit the accounting block (so the line will be absent even for failed jobs), and other checks (e.g., explicit convergence messages in OUTCAR/OSZICAR, existence/contents of CONTCAR/CHGCAR/OSZICAR, "General timing and accounting"/Total CPU time used) are recommended for robust detection. See examples in storq and common VASP utilities that use this line but also note they treat it as a heuristic. [1][2]

Sources:

  • storq reader: read_outcar_validity uses "Voluntary context switches" to mark proper finish and notes cancelled/error runs may not print it. [1]
  • ASE/Jasp and community examples showing many tools/scripts check the OUTCAR tail for "Voluntary context switches" as a finished-job indicator (but other checks are commonly used). [2]

Add more robust completion detection beyond "Voluntary context switches" line.

The "Voluntary context switches" line is a commonly-used heuristic in VASP tools (storq, ASE/Jasp), but it's not reliable for all cases. Abnormal terminations may omit the accounting block entirely, and some failed jobs may still contain this line.

Improve robustness by checking additional signals: explicit convergence messages in OUTCAR/OSZICAR, existence/contents of CONTCAR/CHGCAR/OSZICAR, or "General timing and accounting"/Total CPU time. At minimum, add a comment noting this is a heuristic and document the limitations.

🤖 Prompt for AI Agents
In dftio/io/vasp/vasp_parser.py around lines 108 to 114, the current completion
detection relies solely on the "Voluntary context switches" line which is a
heuristic and can be false-positive/negative; update the logic to be more robust
by: keep the existing check but also scan the OUTCAR/OSZICAR contents for
explicit convergence/termination phrases (e.g., "reached required accuracy",
"reached required accuracy - stopping structural energy minimisation", "General
timing and accounting", "Total CPU time"), verify existence and non-empty status
of key output files (CONTCAR, OSZICAR, CHGCAR) as additional success signals,
and treat completion as true only if one or more of these indicators are
present; if full checks are not implemented, add a clear inline comment
describing this heuristic and its limitations.

@floatingCatty floatingCatty merged commit 75c66b5 into deepmodeling:main Nov 26, 2025
5 checks passed
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.

2 participants