Skip to content

Commit

Permalink
fix(abacus): fix deepks bug in autotest (deepmodeling#1163)
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 Mar 23, 2023
1 parent 8453131 commit e3b8232
Show file tree
Hide file tree
Showing 9 changed files with 212 additions and 39 deletions.
5 changes: 3 additions & 2 deletions doc/autotest/Auto-test.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ The default of `potcar_prefix` is "".
```
**ABACUS**:

The default of `potcar_prefix` is "". The path of potcars/orb_files/deepks_desc is `potcar_prefix` + `potcars`/`orb_files`/`deepks_desc`.
The default of `potcar_prefix` is "". The path of potcars/orb_files/deepks_desc is `potcar_prefix` + `potcars`/`orb_files`/`deepks_desc`/`deepks_model`.
```json
"interaction": {
"type": "abacus",
Expand All @@ -52,7 +52,8 @@ The default of `potcar_prefix` is "". The path of potcars/orb_files/deepks_desc
"potcars": {"Al": "pseudo_potential.al", "Mg": "pseudo_potential.mg"},
"orb_files": {"Al": "numerical_orb.al", "Mg": "numerical_orb.mg"},
"atom_masses": {"Al": 26.9815, "Mg":24.305},
"deepks_desc": "jle.orb"
"deepks_desc": "jle.orb",
"deepks_model": "model.ptg"
}
```
**deepmd**:
Expand Down
94 changes: 66 additions & 28 deletions dpgen/auto_test/ABACUS.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def __init__(self, inter_parameter, path_to_poscar):
self.potcars = inter_parameter.get("potcars", None)
self.orbfile = inter_parameter.get("orb_files", None)
self.deepks = inter_parameter.get("deepks_desc", None)
self.deepks_model = inter_parameter.get("deepks_model", None)
self.path_to_poscar = path_to_poscar
self.if_define_orb_file = False if self.orbfile == None else True

Expand Down Expand Up @@ -48,35 +49,69 @@ def make_potential_files(self, output_dir):
os.chdir(output_dir)
if not os.path.isdir("./pp_orb"):
os.mkdir("./pp_orb")
for i in range(len(atom_names)):
pp_orb_file = [[pp_files[i], self.potcars]]
if orb_files != None:
pp_orb_file.append([orb_files[i], self.orbfile])
elif self.orbfile != None:
assert atom_names[i] in self.orbfile, (
"orb_file of %s is not defined" % atom_names[i]

pp_orb_file = []
for iatom, atomname in enumerate(atom_names):
# pseudopotential file
if not self.potcars:
raise RuntimeError(
"please specify the pseudopotential file for each atom type in 'potcars'"
)
pp_orb_file.append([self.orbfile[atom_names[i]], self.orbfile])

if dpks_descriptor != None:
pp_orb_file.append([dpks_descriptor[i], self.deepks])
elif self.deepks != None:
pp_orb_file.append([self.deepks, self.deepks])

for tmpf, tmpdict in pp_orb_file:
atom = atom_names[i]
if os.path.isfile(os.path.join(stru_path, tmpf)):
linked_file = os.path.join(stru_path, tmpf)
elif tmpdict != None and os.path.isfile(
os.path.join(pp_dir, tmpdict[atom])
):
linked_file = os.path.join(pp_dir, tmpdict[atom])
else:
raise RuntimeError("Can not find file %s" % tmpf.split("/")[-1])
target_file = os.path.join("./pp_orb/", tmpf.split("/")[-1])
if os.path.isfile(target_file):
os.remove(target_file)
os.symlink(linked_file, target_file)
if atomname not in self.potcars:
raise RuntimeError(
"please specify the pseudopotential file of '%s'" % atomname
)
pp_orb_file.append([pp_files[iatom], self.potcars[atomname]])

# orbital file
if orb_files:
if not self.orbfile:
raise RuntimeError(
"Orbital file is defined in STRU, so please specify the orbital file for each atom type in parameter setting file by 'orb_files'"
)
if atomname not in self.orbfile:
raise RuntimeError(
"please specify the orbital file of '%s'" % atomname
)
pp_orb_file.append([orb_files[iatom], self.orbfile[atomname]])
elif self.orbfile:
dlog.warning(
"Orbital is not needed by STRU, so ignore the setting of 'orb_files' in parameter setting file"
)

# dpks_descriptor
if dpks_descriptor:
if not self.deepks:
raise RuntimeError(
"Deepks descriptor file is defined in STRU, so please specify in parameter setting file by 'deepks_desc'"
)
pp_orb_file.append([dpks_descriptor, self.deepks])
elif self.deepks:
dlog.warning(
"Deepks descriptor is not needed by STRU, so ignore the setting of 'deepks_desc' in parameter setting file"
)

# dpks model
if self.deepks_model:
pp_orb_file.append([self.deepks_model, self.deepks_model])

# link the files
for file_stru, file_param in pp_orb_file:
filename_in_stru = os.path.split(file_stru)[1]
filename_in_para = os.path.split(file_param)[1]
if filename_in_stru != filename_in_para:
dlog.warning(
"file name in STRU is not match that defined in parameter setting file: '%s', '%s'."
% (filename_in_stru, filename_in_para)
)

src_file = os.path.join(pp_dir, file_param)
if not os.path.isfile(src_file):
raise RuntimeError("Can not find file %s" % src_file)
tar_file = os.path.join("pp_orb", filename_in_stru)
if os.path.isfile(tar_file):
os.remove(tar_file)
os.symlink(src_file, tar_file)

os.chdir(cwd)

Expand Down Expand Up @@ -167,6 +202,9 @@ def make_input_file(self, output_dir, task_type, task_param):
% incar["basis_type"]
)
raise RuntimeError(mess)
if "deepks_model" in incar:
model_file = os.path.split(incar["deepks_model"])[1]
self.modify_input(incar, "deepks_model", os.path.join("pp_orb", model_file))
abacus.write_input(os.path.join(output_dir, "../INPUT"), incar)
cwd = os.getcwd()
os.chdir(output_dir)
Expand Down
27 changes: 27 additions & 0 deletions tests/auto_test/abacus_input/INPUT.dpks
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
INPUT_PARAMETERS
#Parameters (1.General)
suffix ABACUS
calculation relax
symmetry 1
pseudo_type upf201

#Parameters (2.Iteration)
ecutwfc 60
scf_thr 1e-7
scf_nmax 100
#Parameters (3.Basis)
basis_type pw

#Parameters (4.Smearing)
smearing_method gauss
smearing_sigma 0.002

#Parameters (5.Mixing)
mixing_type pulay
mixing_beta 0.3
ks_solver cg

cal_stress 1
kspacing 0.2

deepks_model model.ptg
Empty file.
Empty file.
26 changes: 26 additions & 0 deletions tests/auto_test/confs/fcc-Al-deepks/STRU
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
ATOMIC_SPECIES
Al 26.9815 ./pp_orb/Al_ONCV_PBE-1.0.upf

NUMERICAL_ORBITAL
pp_orb/Al_gga_9au_100Ry_4s4p1d.orb

LATTICE_CONSTANT
1.8897261254578281

LATTICE_VECTORS
4.05 0.0 0.0
0.0 4.05 0.0
0.0 0.0 4.05

ATOMIC_POSITIONS
Cartesian # Cartesian(Unit is LATTICE_CONSTANT)
Al
0.0
4
0.000000000000 0.000000000000 0.000000000000 1 1 1
0.000000000000 2.025000000000 2.025000000000 1 1 1
2.025000000000 0.000000000000 2.025000000000 1 1 1
2.025000000000 2.025000000000 0.000000000000 1 1 1

NUMERICAL_DESCRIPTOR
pp_orb/jle.orb
87 changes: 86 additions & 1 deletion tests/auto_test/test_abacus.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def test_make_potential_files(self):
self.assertTrue(
os.path.isfile(os.path.join(self.equi_path, "pp_orb/Al_ONCV_PBE-1.0.upf"))
)
self.assertTrue(
self.assertFalse(
os.path.isfile(
os.path.join(self.equi_path, "pp_orb/Al_gga_9au_100Ry_4s4p1d.orb")
)
Expand Down Expand Up @@ -232,3 +232,88 @@ def compare_dict(dict1, dict2):
self.assertTrue(dict1[key] == dict2[key])

compare_dict(ret, ret_ref.as_dict())


class TestABACUSDeepKS(unittest.TestCase):
def setUp(self):
self.jdata = {
"structures": ["confs/fcc-Al-deepks"],
"interaction": {
"type": "abacus",
"incar": "abacus_input/INPUT.dpks",
"potcar_prefix": "abacus_input",
"potcars": {"Al": "Al_ONCV_PBE-1.0.upf"},
"orb_files": {"Al": "Al_gga_9au_100Ry_4s4p1d.orb"},
"deepks_desc": "jle.orb",
"deepks_model": "model.ptg",
},
"relaxation": {
"cal_type": "relaxation",
"cal_setting": {
"relax_pos": True,
"relax_shape": True,
"relax_vol": True,
},
},
}

self.conf_path = "confs/fcc-Al-deepks"
self.equi_path = "confs/fcc-Al-deepks/relaxation/relax_task"
self.source_path = "equi/abacus"
if not os.path.exists(self.equi_path):
os.makedirs(self.equi_path)

self.confs = self.jdata["structures"]
inter_param = self.jdata["interaction"]
self.task_param = self.jdata["relaxation"]
self.ABACUS = ABACUS(inter_param, os.path.join(self.conf_path, "STRU"))

def tearDown(self):
if os.path.exists("confs/fcc-Al-deepks/relaxation"):
shutil.rmtree("confs/fcc-Al-deepks/relaxation")

def test_make_potential_files(self):
if not os.path.exists(os.path.join(self.equi_path, "STRU")):
with self.assertRaises(FileNotFoundError):
self.ABACUS.make_potential_files(self.equi_path)
shutil.copy(
os.path.join(self.conf_path, "STRU"), os.path.join(self.equi_path, "STRU")
)
self.ABACUS.make_potential_files(self.equi_path)
self.assertTrue(
os.path.isfile(os.path.join(self.equi_path, "pp_orb/Al_ONCV_PBE-1.0.upf"))
)
self.assertTrue(
os.path.isfile(
os.path.join(self.equi_path, "pp_orb/Al_gga_9au_100Ry_4s4p1d.orb")
)
)
self.assertTrue(os.path.isfile(os.path.join(self.equi_path, "pp_orb/jle.orb")))
self.assertTrue(
os.path.isfile(os.path.join(self.equi_path, "pp_orb/model.ptg"))
)
self.assertTrue(os.path.isfile(os.path.join(self.equi_path, "inter.json")))

def test_make_input_file_1(self):
param = self.task_param.copy()
param["cal_setting"] = {
"relax_pos": True,
"relax_shape": True,
"relax_vol": False,
}
shutil.copy(
os.path.join(self.conf_path, "STRU"), os.path.join(self.equi_path, "STRU")
)
self.ABACUS.make_input_file(self.equi_path, "relaxation", param)
self.assertTrue(os.path.isfile(os.path.join(self.equi_path, "task.json")))
self.assertTrue(os.path.isfile(os.path.join(self.equi_path, "KPT")))
self.assertTrue(os.path.isfile(os.path.join(self.equi_path, "INPUT")))
abacus_input = abacus_scf.get_abacus_input_parameters(
os.path.join(self.equi_path, "INPUT")
)
self.assertEqual(abacus_input["calculation"].lower(), "cell-relax")
self.assertEqual(abacus_input["fixed_axes"].lower(), "volume")
self.assertEqual(abacus_input["deepks_model"].lower(), "pp_orb/model.ptg")
self.assertTrue(
abacus.check_stru_fixed(os.path.join(self.equi_path, "STRU"), fixed=False)
)
10 changes: 2 additions & 8 deletions tests/auto_test/test_abacus_equi.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class TestEqui(unittest.TestCase):
def tearDown(self):
if os.path.exists("confs/fcc-Al/relaxation"):
shutil.rmtree("confs/fcc-Al/relaxation")
if os.path.exists("confs/fcc-Al/STRU.bk"):
os.remove("confs/fcc-Al/STRU.bk")

def test_make_equi(self):
confs = self.jdata["structures"]
Expand All @@ -57,14 +59,6 @@ def test_make_equi(self):
pot1 = fp.read()
self.assertEqual(pot0, pot1)

with open(os.path.join("abacus_input", "Al_gga_9au_100Ry_4s4p1d.orb")) as fp:
pot0 = fp.read()
with open(
os.path.join(target_path, "pp_orb", "Al_gga_9au_100Ry_4s4p1d.orb")
) as fp:
pot1 = fp.read()
self.assertEqual(pot0, pot1)

self.assertTrue(os.path.isfile(os.path.join(target_path, "KPT")))

task_json_file = os.path.join(target_path, "task.json")
Expand Down
2 changes: 2 additions & 0 deletions tests/auto_test/test_abacus_property.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ def tearDown(self):
shutil.rmtree("confs/fcc-Al/interstitial_00")
if os.path.exists("confs/fcc-Al/surface_00"):
shutil.rmtree("confs/fcc-Al/surface_00")
if os.path.exists("confs/fcc-Al/gamma_00"):
shutil.rmtree("confs/fcc-Al/gamma_00")

def test_make_property(self):
property = {"type": "eos", "vol_start": 0.85, "vol_end": 1.15, "vol_step": 0.01}
Expand Down

0 comments on commit e3b8232

Please sign in to comment.