Skip to content

Commit 8263565

Browse files
wanghan-iapcmHan Wang
andauthored
fix: raise runtime error when the vasp long ions per type bug is triggered (#861)
<!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Improved error handling to detect and alert users when there is a mismatch between atom counts and types in VASP OUTCAR files, preventing silent data inconsistencies. * **Refactor** * Enhanced file handling for frame extraction to ensure files are properly closed and code is more maintainable. * **Tests** * Added a new test to verify correct error reporting for OUTCAR files with more than 10 atom types. <!-- end of auto-generated comment: release notes by coderabbit.ai --> Co-authored-by: Han Wang <[email protected]>
1 parent c129cc3 commit 8263565

File tree

3 files changed

+7473
-2
lines changed

3 files changed

+7473
-2
lines changed

dpdata/vasp/outcar.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,13 @@ def system_info(
9595
atom_names = atom_names_potcar[: nnames // 2]
9696
assert nelm is not None, "cannot find maximum steps for each SC iteration"
9797
assert atom_numbs is not None, "cannot find ion type info in OUTCAR"
98+
if len(atom_numbs) != len(atom_names):
99+
raise RuntimeError(
100+
f"The number of the atom numbers per each type ({len(atom_numbs)}) "
101+
f"does not match that of the atom types ({len(atom_names)}) detected "
102+
f"from the OUTCAR. This issue may be cause by a bug in vasp <= 6.3. "
103+
f"Please try to convert data from vasprun.xml instead."
104+
)
98105
atom_names = atom_names[: len(atom_numbs)]
99106
atom_types = []
100107
for idx, ii in enumerate(atom_numbs):
@@ -131,7 +138,18 @@ def check_outputs(coord, cell, force):
131138

132139
# we assume that the force is printed ...
133140
def get_frames(fname, begin=0, step=1, ml=False, convergence_check=True):
134-
fp = open(fname)
141+
with open(fname) as fp:
142+
return _get_frames_lower(
143+
fp,
144+
fname,
145+
begin=begin,
146+
step=step,
147+
ml=ml,
148+
convergence_check=convergence_check,
149+
)
150+
151+
152+
def _get_frames_lower(fp, fname, begin=0, step=1, ml=False, convergence_check=True):
135153
blk = get_outcar_block(fp)
136154

137155
atom_names, atom_numbs, atom_types, nelm, nwrite = system_info(
@@ -187,7 +205,6 @@ def get_frames(fname, begin=0, step=1, ml=False, convergence_check=True):
187205
all_virials = None
188206
else:
189207
all_virials = np.array(all_virials)
190-
fp.close()
191208
return (
192209
atom_names,
193210
atom_numbs,

0 commit comments

Comments
 (0)