Skip to content

Commit

Permalink
abacus: fix bug of finding the final relax STRU (#1344)
Browse files Browse the repository at this point in the history
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
pxlxingliang and pre-commit-ci[bot] authored Oct 31, 2023
1 parent 4ec90ca commit 4b69200
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
31 changes: 16 additions & 15 deletions dpgen/auto_test/lib/abacus.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/python3
import glob
import os

import dpdata
Expand Down Expand Up @@ -309,23 +310,23 @@ def final_stru(abacus_path):
out_stru = bool(line.split()[1])
logf = os.path.join(abacus_path, f"OUT.{suffix}/running_{calculation}.log")
if calculation in ["relax", "cell-relax"]:
if not out_stru:
if os.path.isfile(os.path.join(abacus_path, "OUT.%s/STRU_ION_D" % suffix)):
return "OUT.%s/STRU_ION_D" % suffix
else:
with open(logf) as f1:
lines = f1.readlines()
for i in range(1, len(lines)):
max_step = ""
if "ALGORITHM --------------- ION=" in lines[-i]:
index_ben = lines[-i].index("ION=") + 4
index_end = lines[-i].index("ELEC")
max_step = int(lines[-i][index_ben:index_end])
if max_step < 2:
max_step = ""
else:
max_step -= 2
break
return f"OUT.{suffix}/STRU_ION{str(max_step)}_D"
# find the final name by STRU_ION*_D,
# for abacus version < v3.2.2, there has no STRU_ION_D file but has STRU_ION0_D STRU_ION1_D ... STRU_ION10_D ...
# so we need to find the last STRU_ION*_D file
stru_ions = glob.glob(
os.path.join(abacus_path, f"OUT.{suffix}/STRU_ION*_D")
)
if len(stru_ions) > 0:
# sort the file name by the number in the file name
stru_ions.sort(key=lambda x: int(x.split("_")[-2][3:]))
final_stru_ion = os.path.basename(stru_ions[-1])
return f"OUT.{suffix}/{final_stru_ion}"
else:
# if there has no STRU_ION_D, return the input STRU
return "STRU"
elif calculation == "md":
with open(logf) as f1:
lines = f1.readlines()
Expand Down
9 changes: 6 additions & 3 deletions dpgen/data/gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -710,13 +710,16 @@ def make_scale_ABACUS(jdata):
assert os.path.isfile(pos_src)
else:
try:
pos_src = os.path.join(
os.path.join(init_path, ii), "OUT.ABACUS/STRU_ION_D"
from dpgen.auto_test.lib.abacus import (
final_stru as abacus_final_stru,
)

pos_src = abacus_final_stru(os.path.join(init_path, ii))
pos_src = os.path.join(init_path, ii, pos_src)
assert os.path.isfile(pos_src)
except Exception:
raise RuntimeError(
"not file %s, vasp relaxation should be run before scale poscar"
"Can not find STRU_ION_D in OUT.ABACUS!!!\nABACUS relaxation should be run before scale poscar"
)
scale_path = os.path.join(work_path, ii)
scale_path = os.path.join(scale_path, "scale-%.3f" % jj)
Expand Down
1 change: 1 addition & 0 deletions tests/auto_test/test_abacus_property.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ def test_make_property_elastic(self):
os.remove(
os.path.realpath(os.path.join(self.equi_path, "OUT.ABACUS", "STRU_ION_D"))
)
os.remove(os.path.realpath(os.path.join(self.equi_path, "STRU")))
with self.assertRaises(RuntimeError):
elastic.make_confs(work_path, self.equi_path, refine=False)

Expand Down

0 comments on commit 4b69200

Please sign in to comment.