Skip to content

Commit 5ebe959

Browse files
wanghan-iapcmHan Wang
andauthored
fix: 781 pymatgen structure bug (#782)
<!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Enhanced support for PyMatGen structure format conversion - Improved handling of periodic boundary conditions (PBC) - **Tests** - Added new test class for PyMatGen structure conversion - Expanded test data with additional element types (Fe, Li, O, P) - **Bug Fixes** - Refined atomic species list generation - Improved error handling for structure periodicity <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: Han Wang <[email protected]>
1 parent 961b591 commit 5ebe959

File tree

11 files changed

+177
-38
lines changed

11 files changed

+177
-38
lines changed

dpdata/plugins/pymatgen.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,15 @@ def to_system(self, data, **kwargs):
3030
"""Convert System to Pymatgen Structure obj."""
3131
structures = []
3232
try:
33-
from pymatgen.core import Structure
33+
from pymatgen.core import Lattice, Structure
3434
except ModuleNotFoundError as e:
3535
raise ImportError("No module pymatgen.Structure") from e
3636

37-
species = []
38-
for name, numb in zip(data["atom_names"], data["atom_numbs"]):
39-
species.extend([name] * numb)
37+
species = [data["atom_names"][tt] for tt in data["atom_types"]]
38+
pbc = not (data.get("nopbc", False))
4039
for ii in range(data["coords"].shape[0]):
4140
structure = Structure(
42-
data["cells"][ii],
41+
Lattice(data["cells"][ii], pbc=[pbc] * 3),
4342
species,
4443
data["coords"][ii],
4544
coords_are_cartesian=True,

dpdata/pymatgen/structure.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,27 @@
44

55

66
def from_system_data(structure) -> dict:
7-
symbols = [site.species_string for site in structure]
7+
"""Convert one pymatgen structure to dpdata's datadict."""
8+
symbols = [ii.specie.symbol for ii in structure]
89
atom_names = list(structure.symbol_set)
910
atom_numbs = [symbols.count(symbol) for symbol in atom_names]
1011
atom_types = np.array([atom_names.index(symbol) for symbol in symbols]).astype(int)
1112
coords = structure.cart_coords
1213
cells = structure.lattice.matrix
14+
if all(structure.pbc):
15+
pbc = True
16+
elif not any(structure.pbc):
17+
pbc = False
18+
else:
19+
raise ValueError(f"Partial pbc condition {structure.pbc} is not supported")
1320

1421
info_dict = {
1522
"atom_names": atom_names,
1623
"atom_numbs": atom_numbs,
1724
"atom_types": atom_types,
1825
"coords": np.array([coords]),
1926
"cells": np.array([cells]),
27+
"orig": np.zeros(3),
28+
"nopbc": not pbc,
2029
}
2130
return info_dict
200 Bytes
Binary file not shown.
2.42 KB
Binary file not shown.
136 Bytes
Binary file not shown.
2.42 KB
Binary file not shown.
200 Bytes
Binary file not shown.

tests/pymatgen_data/deepmd/type.raw

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
1
2+
1
3+
0
4+
0
5+
0
6+
0
7+
0
8+
0
9+
0
10+
0
11+
0
12+
0
13+
0
14+
0
15+
0
16+
0
17+
0
18+
0
19+
3
20+
3
21+
3
22+
3
23+
3
24+
3
25+
3
26+
3
27+
3
28+
3
29+
3
30+
3
31+
3
32+
3
33+
3
34+
3
35+
2
36+
2
37+
2
38+
2
39+
2
40+
2
41+
2
42+
2
43+
2
44+
2
45+
2
46+
2
47+
2
48+
2
49+
2
50+
2
51+
2
52+
2
53+
2
54+
2
55+
2
56+
2
57+
2
58+
2
59+
2
60+
2
61+
2
62+
2
63+
2
64+
2
65+
2
66+
2
67+
2
68+
2
69+
2
70+
2
71+
2
72+
2
73+
2
74+
2
75+
2
76+
2
77+
2
78+
2
79+
2
80+
2
81+
2
82+
2
83+
2
84+
2
85+
2
86+
2
87+
2
88+
2
89+
2
90+
2
91+
2
92+
2
93+
2
94+
2
95+
2
96+
2
97+
2
98+
2
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Fe
2+
Li
3+
O
4+
P

tests/test_from_pymatgen.py

Lines changed: 0 additions & 32 deletions
This file was deleted.

0 commit comments

Comments
 (0)