Skip to content

Commit 4b69200

Browse files
abacus: fix bug of finding the final relax STRU (#1344)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 4ec90ca commit 4b69200

File tree

3 files changed

+23
-18
lines changed

3 files changed

+23
-18
lines changed

dpgen/auto_test/lib/abacus.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/python3
2+
import glob
23
import os
34

45
import dpdata
@@ -309,23 +310,23 @@ def final_stru(abacus_path):
309310
out_stru = bool(line.split()[1])
310311
logf = os.path.join(abacus_path, f"OUT.{suffix}/running_{calculation}.log")
311312
if calculation in ["relax", "cell-relax"]:
312-
if not out_stru:
313+
if os.path.isfile(os.path.join(abacus_path, "OUT.%s/STRU_ION_D" % suffix)):
313314
return "OUT.%s/STRU_ION_D" % suffix
314315
else:
315-
with open(logf) as f1:
316-
lines = f1.readlines()
317-
for i in range(1, len(lines)):
318-
max_step = ""
319-
if "ALGORITHM --------------- ION=" in lines[-i]:
320-
index_ben = lines[-i].index("ION=") + 4
321-
index_end = lines[-i].index("ELEC")
322-
max_step = int(lines[-i][index_ben:index_end])
323-
if max_step < 2:
324-
max_step = ""
325-
else:
326-
max_step -= 2
327-
break
328-
return f"OUT.{suffix}/STRU_ION{str(max_step)}_D"
316+
# find the final name by STRU_ION*_D,
317+
# for abacus version < v3.2.2, there has no STRU_ION_D file but has STRU_ION0_D STRU_ION1_D ... STRU_ION10_D ...
318+
# so we need to find the last STRU_ION*_D file
319+
stru_ions = glob.glob(
320+
os.path.join(abacus_path, f"OUT.{suffix}/STRU_ION*_D")
321+
)
322+
if len(stru_ions) > 0:
323+
# sort the file name by the number in the file name
324+
stru_ions.sort(key=lambda x: int(x.split("_")[-2][3:]))
325+
final_stru_ion = os.path.basename(stru_ions[-1])
326+
return f"OUT.{suffix}/{final_stru_ion}"
327+
else:
328+
# if there has no STRU_ION_D, return the input STRU
329+
return "STRU"
329330
elif calculation == "md":
330331
with open(logf) as f1:
331332
lines = f1.readlines()

dpgen/data/gen.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -710,13 +710,16 @@ def make_scale_ABACUS(jdata):
710710
assert os.path.isfile(pos_src)
711711
else:
712712
try:
713-
pos_src = os.path.join(
714-
os.path.join(init_path, ii), "OUT.ABACUS/STRU_ION_D"
713+
from dpgen.auto_test.lib.abacus import (
714+
final_stru as abacus_final_stru,
715715
)
716+
717+
pos_src = abacus_final_stru(os.path.join(init_path, ii))
718+
pos_src = os.path.join(init_path, ii, pos_src)
716719
assert os.path.isfile(pos_src)
717720
except Exception:
718721
raise RuntimeError(
719-
"not file %s, vasp relaxation should be run before scale poscar"
722+
"Can not find STRU_ION_D in OUT.ABACUS!!!\nABACUS relaxation should be run before scale poscar"
720723
)
721724
scale_path = os.path.join(work_path, ii)
722725
scale_path = os.path.join(scale_path, "scale-%.3f" % jj)

tests/auto_test/test_abacus_property.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ def test_make_property_elastic(self):
174174
os.remove(
175175
os.path.realpath(os.path.join(self.equi_path, "OUT.ABACUS", "STRU_ION_D"))
176176
)
177+
os.remove(os.path.realpath(os.path.join(self.equi_path, "STRU")))
177178
with self.assertRaises(RuntimeError):
178179
elastic.make_confs(work_path, self.equi_path, refine=False)
179180

0 commit comments

Comments
 (0)