Skip to content

Commit 118cab2

Browse files
Copilotnjzjz
andcommitted
fix: Filter pseudopotentials by system atom types for SIESTA
Co-authored-by: njzjz <9496702+njzjz@users.noreply.github.com>
1 parent f2bf3a6 commit 118cab2

3 files changed

Lines changed: 73 additions & 5 deletions

File tree

dpgen/generator/run.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3720,7 +3720,10 @@ def make_fp_siesta(iter_index, jdata):
37203720
for ii in fp_tasks:
37213721
os.chdir(ii)
37223722
sys_data = dpdata.System("POSCAR").data
3723-
ret = make_siesta_input(sys_data, fp_pp_files, fp_params)
3723+
pps = []
3724+
for iii in sys_data["atom_names"]:
3725+
pps.append(fp_pp_files[jdata["type_map"].index(iii)])
3726+
ret = make_siesta_input(sys_data, pps, fp_params)
37243727
with open("input", "w") as fp:
37253728
fp.write(ret)
37263729
os.chdir(cwd)

dpgen/tools/relabel.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,14 @@ def make_pwscf(tdir, fp_params, mass_map, fp_pp_path, fp_pp_files, user_input):
8181
os.chdir(cwd)
8282

8383

84-
def make_siesta(tdir, fp_params, fp_pp_path, fp_pp_files):
84+
def make_siesta(tdir, fp_params, fp_pp_path, fp_pp_files, type_map):
8585
cwd = os.getcwd()
8686
os.chdir(tdir)
8787
sys_data = dpdata.System("POSCAR").data
88-
ret = make_siesta_input(sys_data, fp_pp_files, fp_params)
88+
pps = []
89+
for iii in sys_data["atom_names"]:
90+
pps.append(fp_pp_files[type_map.index(iii)])
91+
ret = make_siesta_input(sys_data, pps, fp_params)
8992
open("input", "w").write(ret)
9093
os.chdir(cwd)
9194

@@ -149,7 +152,7 @@ def create_init_tasks(target_folder, param_file, output, fp_json, verbose=True):
149152
".", fp_params, mass_map, fp_pp_files, fp_pp_files, user_input
150153
)
151154
elif fp_style == "siesta":
152-
make_siesta(".", fp_params, fp_pp_files, fp_pp_files)
155+
make_siesta(".", fp_params, fp_pp_files, fp_pp_files, type_map)
153156
os.chdir(cwd_)
154157

155158

@@ -170,6 +173,7 @@ def create_tasks(
170173
sys = jdata["sys_configs"]
171174
# fp settings
172175
mass_map = jdata["mass_map"]
176+
type_map = jdata["type_map"]
173177
fp_style = fp_jdata["fp_style"]
174178
fp_pp_path = fp_jdata["fp_pp_path"]
175179
fp_pp_files = fp_jdata["fp_pp_files"]
@@ -284,7 +288,7 @@ def create_tasks(
284288
".", fp_params, mass_map, fp_pp_files, fp_pp_files, user_input
285289
)
286290
elif fp_style == "siesta":
287-
make_siesta(".", fp_params, mass_map, fp_pp_files, fp_pp_files)
291+
make_siesta(".", fp_params, mass_map, fp_pp_files, type_map)
288292
os.chdir(cwd_)
289293
os.chdir(cwd)
290294

tests/generator/test_make_fp.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1480,5 +1480,66 @@ def test_make_fp_custom(self):
14801480
shutil.rmtree("iter.000000")
14811481

14821482

1483+
class TestMakeFPSIESTASubsetElements(unittest.TestCase):
1484+
"""Test SIESTA with subset of elements (e.g., only C when C and H pseudopotentials are provided)."""
1485+
1486+
def test_make_fp_siesta_subset_elements(self):
1487+
setUpModule()
1488+
if os.path.isdir("iter.000000"):
1489+
shutil.rmtree("iter.000000")
1490+
with open(param_siesta_file) as fp:
1491+
jdata = json.load(fp)
1492+
with open(machine_file) as fp:
1493+
mdata = json.load(fp)
1494+
1495+
# Create a system with only C atoms (type 0), even though type_map includes C, H, N
1496+
md_descript = []
1497+
nsys = 2
1498+
nmd = 3
1499+
n_frame = 10
1500+
for ii in range(nsys):
1501+
tmp = []
1502+
for jj in range(nmd):
1503+
tmp.append(np.arange(0, 0.29, 0.29 / 10))
1504+
md_descript.append(tmp)
1505+
1506+
# Only C atoms (all type 0) - this is the key part of the test
1507+
atom_types = [0, 0, 0, 0, 0, 0]
1508+
type_map = jdata["type_map"] # This is ["C", "H", "N"]
1509+
1510+
_make_fake_md(0, md_descript, atom_types, type_map)
1511+
make_fp(0, jdata, {})
1512+
1513+
# Verify that the input file was created successfully
1514+
fp_path = os.path.join("iter.000000", "02.fp")
1515+
tasks = glob.glob(os.path.join(fp_path, "task.*"))
1516+
self.assertGreater(len(tasks), 0, "No FP tasks were created")
1517+
1518+
# Check that input files exist and contain only C species
1519+
for task in tasks:
1520+
input_file = os.path.join(task, "input")
1521+
self.assertTrue(os.path.isfile(input_file), f"Input file not found in {task}")
1522+
1523+
with open(input_file) as fp:
1524+
content = fp.read()
1525+
# Verify the input contains only C in the species block
1526+
self.assertIn("%block Chemical_Species_label", content)
1527+
self.assertIn("C", content)
1528+
# The species block should only have 1 species (C), not 3 (C, H, N)
1529+
lines = content.split("\n")
1530+
in_species_block = False
1531+
species_count = 0
1532+
for line in lines:
1533+
if "%block Chemical_Species_label" in line:
1534+
in_species_block = True
1535+
elif "%endblock Chemical_Species_label" in line:
1536+
in_species_block = False
1537+
elif in_species_block and line.strip():
1538+
species_count += 1
1539+
self.assertEqual(species_count, 1, f"Expected 1 species (C), but found {species_count}")
1540+
1541+
shutil.rmtree("iter.000000")
1542+
1543+
14831544
if __name__ == "__main__":
14841545
unittest.main()

0 commit comments

Comments
 (0)