Skip to content

Commit bf5cf3e

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 33b6364 commit bf5cf3e

2 files changed

Lines changed: 27 additions & 17 deletions

File tree

dpdata/abacus/relax.py

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

3-
import os, glob
3+
import glob
4+
import os
45

56
import numpy as np
67

@@ -30,15 +31,17 @@ def get_log_file(fname, inlines):
3031
logf = os.path.join(fname, f"OUT.{suffix}/running_{calculation}.log")
3132
return logf
3233

34+
3335
def get_relax_stru_files(output_dir):
3436
"""Find the STRU files in the output directory.
3537
3638
Args:
3739
output_dir (str): output directory
38-
39-
returns:
40+
41+
Returns
42+
-------
4043
strus: list of STRU files
41-
44+
4245
example:
4346
["STRU_ION1_D", "STRU_ION2_D"]
4447
"""
@@ -120,19 +123,20 @@ def get_coords_from_log(loglines, natoms, stru_files=[]):
120123
# we should read cell and coord from STRU_ION*_D files
121124
if len(energy) > 1 and len(coords) == 1:
122125
# the energies of all structrues are collected, but coords have only the first structure
123-
if len(stru_files) > 1: # if stru_files are not only STRU_ION_D
126+
if len(stru_files) > 1: # if stru_files are not only STRU_ION_D
124127
stru_file_name = [os.path.basename(i) for i in stru_files]
125-
coords = coords[:1] + [np.nan for i in range(len(energy)-1)]
126-
coord_direct = coord_direct[:1] + [False for i in range(len(energy)-1)]
127-
cells = cells[:1] + [np.nan for i in range(len(energy)-1)]
128+
coords = coords[:1] + [np.nan for i in range(len(energy) - 1)]
129+
coord_direct = coord_direct[:1] + [False for i in range(len(energy) - 1)]
130+
cells = cells[:1] + [np.nan for i in range(len(energy) - 1)]
128131
for iframe in range(1, len(energy)):
129132
if f"STRU_ION{iframe}_D" in stru_file_name:
130133
# read the structure from STRU_ION*_D
131-
stru_data = get_frame_from_stru(stru_files[stru_file_name.index(f"STRU_ION{iframe}_D")])
134+
stru_data = get_frame_from_stru(
135+
stru_files[stru_file_name.index(f"STRU_ION{iframe}_D")]
136+
)
132137
coords[iframe] = stru_data["coords"][0]
133138
cells[iframe] = stru_data["cells"][0]
134139

135-
136140
force = collect_force(loglines)
137141
stress = collect_stress(loglines)
138142

@@ -165,7 +169,11 @@ def get_coords_from_log(loglines, natoms, stru_files=[]):
165169

166170
# delete structures whose energy is np.nan
167171
for i in range(minl):
168-
if np.isnan(energy[i - minl]) or np.any(np.isnan(coords[i - minl])) or np.any(np.isnan(cells[i - minl])):
172+
if (
173+
np.isnan(energy[i - minl])
174+
or np.any(np.isnan(coords[i - minl]))
175+
or np.any(np.isnan(cells[i - minl]))
176+
):
169177
del energy[i - minl]
170178
del coords[i - minl]
171179
del cells[i - minl]
@@ -225,8 +233,9 @@ def get_frame(fname):
225233

226234
relax_stru_files = get_relax_stru_files(os.path.dirname(logf))
227235

228-
energy, cells, coords, force, stress, virial = get_coords_from_log(lines, natoms, stru_files=relax_stru_files)
229-
236+
energy, cells, coords, force, stress, virial = get_coords_from_log(
237+
lines, natoms, stru_files=relax_stru_files
238+
)
230239

231240
magmom, magforce = get_mag_force(lines)
232241

tests/test_abacus_relax.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,13 +187,16 @@ def tearDown(self):
187187
if os.path.isfile("abacus.relax/STRU"):
188188
os.remove("abacus.relax/STRU")
189189

190+
190191
class TestABACUSRelaxReadFromSTRUIOND(unittest.TestCase):
191192
# Since ABACUS v3.4.1, the output format of force and stress has been changed.
192193
def setUp(self):
193-
self.system = dpdata.LabeledSystem("abacus.relax.readFromSTRUIOND", fmt="abacus/relax")
194+
self.system = dpdata.LabeledSystem(
195+
"abacus.relax.readFromSTRUIOND", fmt="abacus/relax"
196+
)
194197

195198
# write results
196-
#for key in ["energies", "cells", "coords", "forces", "stress", "virials"]:
199+
# for key in ["energies", "cells", "coords", "forces", "stress", "virials"]:
197200
# np.save(f"abacus.relax.readFromSTRUIOND/{key}.npy", self.system.data[key])
198201

199202
def test_results(self):
@@ -203,8 +206,6 @@ def test_results(self):
203206
np.load(f"abacus.relax.readFromSTRUIOND/{key}.npy"),
204207
decimal=8,
205208
)
206-
207-
208209

209210

210211
if __name__ == "__main__":

0 commit comments

Comments
 (0)