Skip to content

Commit 3bbd2f8

Browse files
authored
Merge pull request #104 from haidi-ustc/devel
convert LabeledSystem to ComputedStructureEntry
2 parents 20f810b + 4a0d7ee commit 3bbd2f8

File tree

3 files changed

+276
-0
lines changed

3 files changed

+276
-0
lines changed

dpdata/system.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,7 @@ def to_pymatgen_structure(self):
496496
structure=Structure(system.data['cells'][0],species,system.data['coords'][0],coords_are_cartesian=True)
497497
structures.append(structure)
498498
return structures
499+
499500

500501
@register_to_funcs.register_funcs("ase/structure")
501502
def to_ase_structure(self):
@@ -1329,6 +1330,26 @@ def shuffle(self):
13291330
self.data[ii] = self.data[ii][idx]
13301331
return idx
13311332

1333+
def to_pymatgen_ComputedStructureEntry(self):
1334+
'''
1335+
convert System to Pymagen ComputedStructureEntry obj
1336+
1337+
'''
1338+
try:
1339+
from pymatgen.entries.computed_entries import ComputedStructureEntry
1340+
except:
1341+
raise ImportError('No module ComputedStructureEntry in pymatgen.entries.computed_entries')
1342+
1343+
entries=[]
1344+
for system in self.to_list():
1345+
structure=system.to_pymatgen_structure()[0]
1346+
energy=system.data['energies'][0]
1347+
data={'forces':system.data['forces'][0],
1348+
'virials':system.data['virials'][0]}
1349+
1350+
entry=ComputedStructureEntry(structure,energy,data=data)
1351+
entries.append(entry)
1352+
return entries
13321353

13331354
class MultiSystems:
13341355
'''A set containing several systems.'''

tests/computed_structure_entry.json

Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
{
2+
"@module": "pymatgen.entries.computed_entries",
3+
"@class": "ComputedStructureEntry",
4+
"energy": -28.39848822,
5+
"composition": {
6+
"O": 2.0,
7+
"H": 4.0
8+
},
9+
"parameters": {},
10+
"data": {
11+
"forces": {
12+
"@module": "numpy",
13+
"@class": "array",
14+
"dtype": "float64",
15+
"data": [
16+
[
17+
-0.211386,
18+
-1.770018,
19+
0.507873
20+
],
21+
[
22+
-1.47897,
23+
-0.663468,
24+
-0.917499
25+
],
26+
[
27+
-0.178977,
28+
0.946024,
29+
0.351008
30+
],
31+
[
32+
0.38587,
33+
0.822681,
34+
-0.844953
35+
],
36+
[
37+
1.156783,
38+
0.157322,
39+
-0.138993
40+
],
41+
[
42+
0.32185,
43+
0.505766,
44+
1.049144
45+
]
46+
]
47+
},
48+
"virials": {
49+
"@module": "numpy",
50+
"@class": "array",
51+
"dtype": "float64",
52+
"data": [
53+
[
54+
-1.3720085358803906,
55+
-0.2485681009072721,
56+
0.4575213433975083
57+
],
58+
[
59+
-0.2485681009072721,
60+
-1.408552571807875,
61+
-0.2783962730161448
62+
],
63+
[
64+
0.4575213433975083,
65+
-0.2783962730161448,
66+
-1.8782261334719614
67+
]
68+
]
69+
}
70+
},
71+
"entry_id": null,
72+
"correction": 0.0,
73+
"structure": {
74+
"@module": "pymatgen.core.structure",
75+
"@class": "Structure",
76+
"charge": null,
77+
"lattice": {
78+
"matrix": [
79+
[
80+
10.0,
81+
0.0,
82+
0.0
83+
],
84+
[
85+
-0.011409,
86+
10.0,
87+
0.0
88+
],
89+
[
90+
0.1411083,
91+
-0.0595569,
92+
10.0
93+
]
94+
],
95+
"a": 10.0,
96+
"b": 10.000006508261933,
97+
"c": 10.00117286005329,
98+
"alpha": 90.34211999314414,
99+
"beta": 89.19157698565051,
100+
"gamma": 90.06536872648402,
101+
"volume": 1000.0
102+
},
103+
"sites": [
104+
{
105+
"species": [
106+
{
107+
"element": "O",
108+
"occu": 1
109+
}
110+
],
111+
"abc": [
112+
0.4280678471391986,
113+
0.42481761932567,
114+
0.519943
115+
],
116+
"xyz": [
117+
4.3492,
118+
4.217210000000001,
119+
5.19943
120+
],
121+
"label": "O",
122+
"properties": {}
123+
},
124+
{
125+
"species": [
126+
{
127+
"element": "O",
128+
"occu": 1
129+
}
130+
],
131+
"abc": [
132+
0.23060991326548744,
133+
0.62824882136683,
134+
0.113307
135+
],
136+
"xyz": [
137+
2.31492,
138+
6.27574,
139+
1.13307
140+
],
141+
"label": "O",
142+
"properties": {}
143+
},
144+
{
145+
"species": [
146+
{
147+
"element": "H",
148+
"occu": 1
149+
}
150+
],
151+
"abc": [
152+
0.454949233514069,
153+
0.34706581743272,
154+
0.46288799999999997
155+
],
156+
"xyz": [
157+
4.61085,
158+
3.44309,
159+
4.62888
160+
],
161+
"label": "H",
162+
"properties": {}
163+
},
164+
{
165+
"species": [
166+
{
167+
"element": "H",
168+
"occu": 1
169+
}
170+
],
171+
"abc": [
172+
0.39089296133220036,
173+
0.37595081080755,
174+
0.599395
175+
],
176+
"xyz": [
177+
3.98922,
178+
3.72381,
179+
5.99395
180+
],
181+
"label": "H",
182+
"properties": {}
183+
},
184+
{
185+
"species": [
186+
{
187+
"element": "H",
188+
"occu": 1
189+
}
190+
],
191+
"abc": [
192+
0.13515111964029505,
193+
0.62260772656242,
194+
0.144018
195+
],
196+
"xyz": [
197+
1.36473,
198+
6.2175,
199+
1.44018
200+
],
201+
"label": "H",
202+
"properties": {}
203+
},
204+
{
205+
"species": [
206+
{
207+
"element": "H",
208+
"occu": 1
209+
}
210+
],
211+
"abc": [
212+
0.22316222152283632,
213+
0.58830814318196,
214+
0.021684000000000002
215+
],
216+
"xyz": [
217+
2.2279700000000005,
218+
5.8817900000000005,
219+
0.21684000000000003
220+
],
221+
"label": "H",
222+
"properties": {}
223+
}
224+
]
225+
}
226+
}

tests/test_to_pymatgen_entry.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import os
2+
import numpy as np
3+
import unittest
4+
from context import dpdata
5+
from comp_sys import CompSys, IsPBC
6+
from monty.serialization import loadfn
7+
try:
8+
from pymatgen.entries.computed_entries import ComputedStructureEntry
9+
exist_module=True
10+
except:
11+
exist_module=False
12+
13+
@unittest.skipIf(not exist_module,"skip pymatgen")
14+
class TestPymatgen(unittest.TestCase):
15+
16+
def test(self):
17+
ls1= dpdata.LabeledSystem(os.path.join('poscars', 'OUTCAR.ch4.1step'),fmt='OUTCAR')
18+
entry1=ls1.to_pymatgen_ComputedStructureEntry()
19+
self.assertEqual(entry1,[])
20+
ls2= dpdata.LabeledSystem(os.path.join('poscars', 'OUTCAR.h2o.md.10'),fmt='OUTCAR')
21+
entry2=ls2.to_pymatgen_ComputedStructureEntry()
22+
self.assertEqual(len(entry2),10)
23+
last_entry=loadfn("computed_structure_entry.json")
24+
self.assertEqual(last_entry.as_dict(),entry2[-1].as_dict())
25+
26+
27+
if __name__ == '__main__':
28+
unittest.main()
29+

0 commit comments

Comments
 (0)