fix(vasp): add robust error handling for float conversions in POSCAR and OUTCAR parsing
This PR addresses a critical issue where dpdata would throw unhelpful ValueError: could not convert string to float errors when parsing malformed VASP files, making it difficult for users to identify and fix problems in their input files.
Problem
When VASP files contain corrupted or malformed numeric data, dpdata would fail with generic error messages like:
from dpdata import System
# This would throw: ValueError: could not convert string to float: 'INVALID_SCALE'
sys = System('corrupted.poscar', fmt='vasp/poscar')
The error provided no context about which file, which line, or which component was problematic, making debugging very difficult for users.
Solution
Added robust error handling around all float conversions in the VASP parsing modules:
Enhanced Error Messages:
-
Before:
ValueError: could not convert string to float: 'INVALID_SCALE' -
After:
ValueError: Failed to parse scale factor from POSCAR line 2: could not convert string to float: 'INVALID_SCALE'
Key Improvements:
- Added
_safe_float()helper function to bothposcar.pyandoutcar.py - Wrapped all float conversions with informative error handling
- Error messages now include:
- Context: What component was being parsed (scale factor, cell vector, coordinate, etc.)
- Location: Which line in the file caused the issue
- Value: The exact problematic value that couldn't be converted
- File type: Whether it's a POSCAR or OUTCAR parsing issue
Files Modified:
-
dpdata/vasp/poscar.py: Enhanced error handling for scale factors, cell vectors, and coordinates -
dpdata/vasp/outcar.py: Enhanced error handling for energies, forces, virials, and cell vectors -
tests/test_vasp_float_conversion_fix.py: Comprehensive test suite covering malformed file scenarios
Testing
- ✅ All 212 existing VASP tests continue to pass
- ✅ Added 7 new tests covering various malformed file scenarios
- ✅ Verified that valid VASP files continue to work correctly
- ✅ Code passes all linting checks
Impact
Users will now receive clear, actionable error messages when encountering malformed VASP files, enabling them to quickly identify and fix issues in their input data instead of struggling with cryptic error messages.
Fixes #611.
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.