Skip to content

Commit e6cbaab

Browse files
authored
Fix(abacus): add judgment on the existence of INPUT/running_scf.log/STRU files in abacus/scf (#405)
return a null dict but not raiseError when INPUT/running_scf.log/STRU files are not exist in abacus/scf.
1 parent b92d073 commit e6cbaab

File tree

1 file changed

+27
-12
lines changed

1 file changed

+27
-12
lines changed

dpdata/abacus/scf.py

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
ry2ev = EnergyConversion("rydberg", "eV").value()
77
kbar2evperang3 = PressureConversion("kbar", "eV/angstrom^3").value()
88

9+
def CheckFile(ifile):
10+
if not os.path.isfile(ifile):
11+
print("Can not find file %s" % ifile)
12+
return False
13+
return True
14+
915
def get_block (lines, keyword, skip = 0, nlines = None):
1016
ret = []
1117
found = False
@@ -108,7 +114,8 @@ def get_force (outlines, natoms):
108114
force = []
109115
force_inlines = get_block (outlines, "TOTAL-FORCE (eV/Angstrom)", skip = 4, nlines=np.sum(natoms))
110116
if force_inlines is None:
111-
raise RuntimeError("TOTAL-FORCE (eV/Angstrom) is not found in running_scf.log. Please check.")
117+
print("TOTAL-FORCE (eV/Angstrom) is not found in OUT.XXX/running_scf.log. May be you haven't set 'cal_force 1' in the INPUT.")
118+
return [[]]
112119
for line in force_inlines:
113120
force.append([float(f) for f in line.split()[1:4]])
114121
force = np.array(force)
@@ -127,43 +134,51 @@ def get_stress(outlines):
127134

128135

129136
def get_frame (fname):
137+
data = {'atom_names':[],\
138+
'atom_numbs':[],\
139+
'atom_types':[],\
140+
'cells':[],\
141+
'coords':[],\
142+
'energies':[],\
143+
'forces':[]}
144+
130145
if type(fname) == str:
131146
# if the input parameter is only one string, it is assumed that it is the
132147
# base directory containing INPUT file;
133148
path_in = os.path.join(fname, "INPUT")
134149
else:
135150
raise RuntimeError('invalid input')
151+
152+
if not CheckFile(path_in):
153+
return data
154+
136155
with open(path_in, 'r') as fp:
137156
inlines = fp.read().split('\n')
138157

139158
geometry_path_in = get_geometry_in(fname, inlines)
140159
path_out = get_path_out(fname, inlines)
160+
if not (CheckFile(geometry_path_in) and CheckFile(path_out)):
161+
return data
162+
141163
with open(geometry_path_in, 'r') as fp:
142164
geometry_inlines = fp.read().split('\n')
143165
with open(path_out, 'r') as fp:
144166
outlines = fp.read().split('\n')
145167

146168
celldm, cell = get_cell(geometry_inlines)
147169
atom_names, natoms, types, coords = get_coords(celldm, cell, geometry_inlines, inlines)
170+
data['atom_names'] = atom_names
171+
data['atom_numbs'] = natoms
172+
data['atom_types'] = types
148173

149174
energy,converge = get_energy(outlines)
150175
if not converge:
151-
return {'atom_names':atom_names,\
152-
'atom_numbs':natoms,\
153-
'atom_types':types,\
154-
'cells':[],\
155-
'coords':[],\
156-
'energies':[],\
157-
'forces':[]}
176+
return data
158177
force = get_force (outlines, natoms)
159178
stress = get_stress(outlines)
160179
if stress is not None:
161180
stress *= np.abs(np.linalg.det(cell))
162181

163-
data = {}
164-
data['atom_names'] = atom_names
165-
data['atom_numbs'] = natoms
166-
data['atom_types'] = types
167182
data['cells'] = cell[np.newaxis, :, :]
168183
data['coords'] = coords[np.newaxis, :, :]
169184
data['energies'] = np.array(energy)[np.newaxis]

0 commit comments

Comments
 (0)