Skip to content

Commit 3ae18a5

Browse files
authored
Merge pull request #29 from deepmodeling/devel
Improve function `pert_scaled` (deepmodeling#1563)
2 parents 85ea6de + e285ab3 commit 3ae18a5

File tree

2 files changed

+48
-48
lines changed

2 files changed

+48
-48
lines changed

dpgen/data/gen.py

+24-21
Original file line numberDiff line numberDiff line change
@@ -728,8 +728,7 @@ def make_scale_ABACUS(jdata):
728728

729729

730730
def pert_scaled(jdata):
731-
if "init_fp_style" not in jdata:
732-
jdata["init_fp_style"] = "VASP"
731+
### Extract data from jdata
733732
out_dir = jdata["out_dir"]
734733
scale = jdata["scale"]
735734
pert_box = jdata["pert_box"]
@@ -746,6 +745,7 @@ def pert_scaled(jdata):
746745
if "from_poscar" in jdata:
747746
from_poscar = jdata["from_poscar"]
748747

748+
### Get the current working directory and the system path
749749
cwd = os.getcwd()
750750
path_sp = os.path.join(out_dir, global_dirname_03)
751751
assert os.path.isdir(path_sp)
@@ -754,35 +754,35 @@ def pert_scaled(jdata):
754754
sys_pe.sort()
755755
os.chdir(cwd)
756756

757-
pert_cmd = os.path.dirname(__file__)
758-
pert_cmd = os.path.join(pert_cmd, "tools")
759-
pert_cmd = os.path.join(pert_cmd, "create_random_disturb.py")
760-
fp_style = "vasp"
761-
poscar_name = "POSCAR"
762-
if jdata["init_fp_style"] == "ABACUS":
757+
### Construct the perturbation command
758+
init_fp_style = jdata.get("init_fp_style", "VASP")
759+
if init_fp_style == "VASP":
760+
fp_style = "vasp"
761+
poscar_name = "POSCAR"
762+
elif init_fp_style == "ABACUS":
763763
fp_style = "abacus"
764764
poscar_name = "STRU"
765-
pert_cmd = (
766-
sys.executable
767-
+ " "
768-
+ pert_cmd
769-
+ " -etmax %f -ofmt %s %s %d %f > /dev/null"
770-
% (pert_box, fp_style, poscar_name, pert_numb, pert_atom)
765+
766+
python_exec = os.path.join(
767+
os.path.dirname(__file__), "tools", "create_random_disturb.py"
771768
)
769+
pert_cmd = f"{sys.executable} {python_exec} -etmax {pert_box} -ofmt {fp_style} {poscar_name} {pert_numb} {pert_atom} > /dev/null"
770+
771+
### Loop over each system and scale
772772
for ii in sys_pe:
773773
for jj in scale:
774-
path_work = path_sp
775-
path_work = os.path.join(path_work, ii)
776-
path_work = os.path.join(path_work, f"scale-{jj:.3f}")
774+
path_work = os.path.join(path_sp, ii, f"scale-{jj:.3f}")
777775
assert os.path.isdir(path_work)
778776
os.chdir(path_work)
779777
sp.check_call(pert_cmd, shell=True)
778+
779+
### Loop over each perturbation
780780
for kk in range(pert_numb):
781781
if fp_style == "vasp":
782-
pos_in = "POSCAR%d.vasp" % (kk + 1)
782+
pos_in = f"POSCAR{kk+1}.vasp"
783783
elif fp_style == "abacus":
784-
pos_in = "STRU%d.abacus" % (kk + 1)
785-
dir_out = "%06d" % (kk + 1)
784+
pos_in = f"STRU{kk+1}.abacus"
785+
dir_out = f"{kk+1:06d}"
786786
create_path(dir_out)
787787
if fp_style == "vasp":
788788
pos_out = os.path.join(dir_out, "POSCAR")
@@ -807,12 +807,14 @@ def pert_scaled(jdata):
807807
else:
808808
shutil.copy2(pos_in, pos_out)
809809
os.remove(pos_in)
810+
811+
### Handle special case (unperturbed ?)
810812
kk = -1
811813
if fp_style == "vasp":
812814
pos_in = "POSCAR"
813815
elif fp_style == "abacus":
814816
pos_in = "STRU"
815-
dir_out = "%06d" % (kk + 1)
817+
dir_out = f"{kk+1:06d}"
816818
create_path(dir_out)
817819
if fp_style == "vasp":
818820
pos_out = os.path.join(dir_out, "POSCAR")
@@ -836,6 +838,7 @@ def pert_scaled(jdata):
836838
)
837839
else:
838840
shutil.copy2(pos_in, pos_out)
841+
839842
os.chdir(cwd)
840843

841844

dpgen/data/surf.py

+24-27
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import dpgen.data.tools.fcc as fcc
1919
import dpgen.data.tools.hcp as hcp
2020
import dpgen.data.tools.sc as sc
21-
from dpgen import ROOT_PATH, dlog
21+
from dpgen import dlog
2222
from dpgen.dispatcher.Dispatcher import make_submission_compat
2323
from dpgen.generator.lib.utils import symlink_user_forward_files
2424
from dpgen.remote.decide_machine import convert_mdata
@@ -354,15 +354,16 @@ def make_vasp_relax(jdata):
354354
out_dir = jdata["out_dir"]
355355
potcars = jdata["potcars"]
356356
cwd = os.getcwd()
357-
358357
work_dir = os.path.join(out_dir, global_dirname_02)
359358
assert os.path.isdir(work_dir)
360359
work_dir = os.path.abspath(work_dir)
360+
361361
if os.path.isfile(os.path.join(work_dir, "INCAR")):
362362
os.remove(os.path.join(work_dir, "INCAR"))
363363
if os.path.isfile(os.path.join(work_dir, "POTCAR")):
364364
os.remove(os.path.join(work_dir, "POTCAR"))
365365
shutil.copy2(jdata["relax_incar"], os.path.join(work_dir, "INCAR"))
366+
366367
out_potcar = os.path.join(work_dir, "POTCAR")
367368
with open(out_potcar, "w") as outfile:
368369
for fname in potcars:
@@ -442,15 +443,12 @@ def make_scale(jdata):
442443
for jj in scale:
443444
if skip_relax:
444445
pos_src = os.path.join(os.path.join(init_path, ii), "POSCAR")
445-
assert os.path.isfile(pos_src)
446446
else:
447-
try:
448-
pos_src = os.path.join(os.path.join(init_path, ii), "CONTCAR")
449-
assert os.path.isfile(pos_src)
450-
except Exception:
451-
raise RuntimeError(
452-
"not file %s, vasp relaxation should be run before scale poscar"
453-
)
447+
pos_src = os.path.join(os.path.join(init_path, ii), "CONTCAR")
448+
if not os.path.isfile(pos_src):
449+
raise RuntimeError(
450+
f"file {pos_src} not found, vasp relaxation should be run before scale poscar"
451+
)
454452
scale_path = os.path.join(work_path, ii)
455453
scale_path = os.path.join(scale_path, f"scale-{jj:.3f}")
456454
create_path(scale_path)
@@ -503,46 +501,45 @@ def pert_scaled(jdata):
503501
sys_pe.sort()
504502
os.chdir(cwd)
505503

506-
pert_cmd = (
507-
sys.executable
508-
+ " "
509-
+ os.path.join(ROOT_PATH, "data/tools/create_random_disturb.py")
510-
)
511-
pert_cmd += " -etmax %f -ofmt vasp POSCAR %d %f > /dev/null" % (
512-
pert_box,
513-
pert_numb,
514-
pert_atom,
504+
### Construct the perturbation command
505+
python_exec = os.path.join(
506+
os.path.dirname(__file__), "tools", "create_random_disturb.py"
515507
)
508+
pert_cmd = f"{sys.executable} {python_exec} -etmax {pert_box} -ofmt vasp POSCAR {pert_numb} {pert_atom} > /dev/null"
509+
510+
### Loop over each system and scale
516511
for ii in sys_pe:
517512
for jj in scale:
518-
path_scale = path_sp
519-
path_scale = os.path.join(path_scale, ii)
520-
path_scale = os.path.join(path_scale, f"scale-{jj:.3f}")
513+
path_scale = os.path.join(path_sp, ii, f"scale-{jj:.3f}")
521514
assert os.path.isdir(path_scale)
522515
os.chdir(path_scale)
523516
dlog.info(os.getcwd())
524517
poscar_in = os.path.join(path_scale, "POSCAR")
525518
assert os.path.isfile(poscar_in)
519+
520+
### Loop over each perturbation
526521
for ll in elongs:
527-
path_elong = path_scale
528-
path_elong = os.path.join(path_elong, f"elong-{ll:3.3f}")
522+
path_elong = os.path.join(path_scale, f"elong-{ll:3.3f}")
529523
create_path(path_elong)
530524
os.chdir(path_elong)
531525
poscar_elong(poscar_in, "POSCAR", ll)
532526
sp.check_call(pert_cmd, shell=True)
533527
for kk in range(pert_numb):
534-
pos_in = "POSCAR%d.vasp" % (kk + 1)
535-
dir_out = "%06d" % (kk + 1)
528+
pos_in = f"POSCAR{kk+1}.vasp"
529+
dir_out = f"{kk+1:06d}"
536530
create_path(dir_out)
537531
pos_out = os.path.join(dir_out, "POSCAR")
538532
poscar_shuffle(pos_in, pos_out)
539533
os.remove(pos_in)
534+
535+
### Handle special case (unperturbed ?)
540536
kk = -1
541537
pos_in = "POSCAR"
542-
dir_out = "%06d" % (kk + 1)
538+
dir_out = f"{kk+1:06d}"
543539
create_path(dir_out)
544540
pos_out = os.path.join(dir_out, "POSCAR")
545541
poscar_shuffle(pos_in, pos_out)
542+
546543
os.chdir(cwd)
547544

548545

0 commit comments

Comments
 (0)