fix(vasp): improve robustness of OUTCAR ML parsing for cell data extraction
This PR addresses an issue where VASP machine learning AIMD simulations were not extracting the expected number of structures from OUTCAR files when using dpdata. The problem was caused by fragile cell data extraction logic that could fail with slight variations in OUTCAR format.
Problem
Users reported that when parsing ML AIMD OUTCAR files, dpdata would extract fewer structures than expected, particularly when encountering lines with "VOLUME and BASIS-vectors are now included." instead of the standard "VOLUME and BASIS-vectors are now :". The original parsing logic was not robust to format variations and could fail when cell data lines were malformed.
Solution
The fix improves the robustness of cell data extraction in the analyze_block function within dpdata/vasp/outcar.py:
- Enhanced error handling: Added try/catch blocks around float conversions to gracefully handle malformed cell data lines
- Improved parsing logic: More robust extraction of cell vectors that can handle various line format variations
- Better bounds checking: Added safety checks to prevent array access errors
Example
import dpdata
# Before: Could fail or miss frames with format variations
system = dpdata.LabeledSystem("OUTCAR.ml", fmt="vasp/outcar", ml=True)
# After: Robust parsing handles format variations gracefully
system = dpdata.LabeledSystem("OUTCAR.ml", fmt="vasp/outcar", ml=True)
print(f"Successfully extracted {len(system['energies'])} frames")
Testing
- All existing VASP OUTCAR tests continue to pass (67 tests)
- Added comprehensive robustness testing to verify the improvements
- Verified that both ML and non-ML parsing modes work correctly
- Confirmed backward compatibility with existing OUTCAR files
The changes maintain full backward compatibility while making the parser more resilient to format variations that can occur in different VASP versions or configurations.
Fixes #522.
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.