fix(vasp): add robust error handling for float conversions in POSCAR and OUTCAR parsing #890
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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:
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:
ValueError: could not convert string to float: 'INVALID_SCALE'
ValueError: Failed to parse scale factor from POSCAR line 2: could not convert string to float: 'INVALID_SCALE'
Key Improvements:
_safe_float()
helper function to bothposcar.py
andoutcar.py
Files Modified:
dpdata/vasp/poscar.py
: Enhanced error handling for scale factors, cell vectors, and coordinatesdpdata/vasp/outcar.py
: Enhanced error handling for energies, forces, virials, and cell vectorstests/test_vasp_float_conversion_fix.py
: Comprehensive test suite covering malformed file scenariosTesting
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.