From 4a0d7eee4ef77166599144dcc76a53039f41ba83 Mon Sep 17 00:00:00 2001 From: haidi Date: Mon, 29 Jun 2020 00:44:02 +0800 Subject: [PATCH] convert LabeledSystem to ComputedStructureEntry --- dpdata/system.py | 21 +++ tests/computed_structure_entry.json | 226 ++++++++++++++++++++++++++++ tests/test_to_pymatgen_entry.py | 29 ++++ 3 files changed, 276 insertions(+) create mode 100644 tests/computed_structure_entry.json create mode 100644 tests/test_to_pymatgen_entry.py diff --git a/dpdata/system.py b/dpdata/system.py index 167c29b47..a74a27f9d 100644 --- a/dpdata/system.py +++ b/dpdata/system.py @@ -496,6 +496,7 @@ def to_pymatgen_structure(self): structure=Structure(system.data['cells'][0],species,system.data['coords'][0],coords_are_cartesian=True) structures.append(structure) return structures + @register_to_funcs.register_funcs("ase/structure") def to_ase_structure(self): @@ -1329,6 +1330,26 @@ def shuffle(self): self.data[ii] = self.data[ii][idx] return idx + def to_pymatgen_ComputedStructureEntry(self): + ''' + convert System to Pymagen ComputedStructureEntry obj + + ''' + try: + from pymatgen.entries.computed_entries import ComputedStructureEntry + except: + raise ImportError('No module ComputedStructureEntry in pymatgen.entries.computed_entries') + + entries=[] + for system in self.to_list(): + structure=system.to_pymatgen_structure()[0] + energy=system.data['energies'][0] + data={'forces':system.data['forces'][0], + 'virials':system.data['virials'][0]} + + entry=ComputedStructureEntry(structure,energy,data=data) + entries.append(entry) + return entries class MultiSystems: '''A set containing several systems.''' diff --git a/tests/computed_structure_entry.json b/tests/computed_structure_entry.json new file mode 100644 index 000000000..4ce92276e --- /dev/null +++ b/tests/computed_structure_entry.json @@ -0,0 +1,226 @@ +{ + "@module": "pymatgen.entries.computed_entries", + "@class": "ComputedStructureEntry", + "energy": -28.39848822, + "composition": { + "O": 2.0, + "H": 4.0 + }, + "parameters": {}, + "data": { + "forces": { + "@module": "numpy", + "@class": "array", + "dtype": "float64", + "data": [ + [ + -0.211386, + -1.770018, + 0.507873 + ], + [ + -1.47897, + -0.663468, + -0.917499 + ], + [ + -0.178977, + 0.946024, + 0.351008 + ], + [ + 0.38587, + 0.822681, + -0.844953 + ], + [ + 1.156783, + 0.157322, + -0.138993 + ], + [ + 0.32185, + 0.505766, + 1.049144 + ] + ] + }, + "virials": { + "@module": "numpy", + "@class": "array", + "dtype": "float64", + "data": [ + [ + -1.3720085358803906, + -0.2485681009072721, + 0.4575213433975083 + ], + [ + -0.2485681009072721, + -1.408552571807875, + -0.2783962730161448 + ], + [ + 0.4575213433975083, + -0.2783962730161448, + -1.8782261334719614 + ] + ] + } + }, + "entry_id": null, + "correction": 0.0, + "structure": { + "@module": "pymatgen.core.structure", + "@class": "Structure", + "charge": null, + "lattice": { + "matrix": [ + [ + 10.0, + 0.0, + 0.0 + ], + [ + -0.011409, + 10.0, + 0.0 + ], + [ + 0.1411083, + -0.0595569, + 10.0 + ] + ], + "a": 10.0, + "b": 10.000006508261933, + "c": 10.00117286005329, + "alpha": 90.34211999314414, + "beta": 89.19157698565051, + "gamma": 90.06536872648402, + "volume": 1000.0 + }, + "sites": [ + { + "species": [ + { + "element": "O", + "occu": 1 + } + ], + "abc": [ + 0.4280678471391986, + 0.42481761932567, + 0.519943 + ], + "xyz": [ + 4.3492, + 4.217210000000001, + 5.19943 + ], + "label": "O", + "properties": {} + }, + { + "species": [ + { + "element": "O", + "occu": 1 + } + ], + "abc": [ + 0.23060991326548744, + 0.62824882136683, + 0.113307 + ], + "xyz": [ + 2.31492, + 6.27574, + 1.13307 + ], + "label": "O", + "properties": {} + }, + { + "species": [ + { + "element": "H", + "occu": 1 + } + ], + "abc": [ + 0.454949233514069, + 0.34706581743272, + 0.46288799999999997 + ], + "xyz": [ + 4.61085, + 3.44309, + 4.62888 + ], + "label": "H", + "properties": {} + }, + { + "species": [ + { + "element": "H", + "occu": 1 + } + ], + "abc": [ + 0.39089296133220036, + 0.37595081080755, + 0.599395 + ], + "xyz": [ + 3.98922, + 3.72381, + 5.99395 + ], + "label": "H", + "properties": {} + }, + { + "species": [ + { + "element": "H", + "occu": 1 + } + ], + "abc": [ + 0.13515111964029505, + 0.62260772656242, + 0.144018 + ], + "xyz": [ + 1.36473, + 6.2175, + 1.44018 + ], + "label": "H", + "properties": {} + }, + { + "species": [ + { + "element": "H", + "occu": 1 + } + ], + "abc": [ + 0.22316222152283632, + 0.58830814318196, + 0.021684000000000002 + ], + "xyz": [ + 2.2279700000000005, + 5.8817900000000005, + 0.21684000000000003 + ], + "label": "H", + "properties": {} + } + ] + } +} \ No newline at end of file diff --git a/tests/test_to_pymatgen_entry.py b/tests/test_to_pymatgen_entry.py new file mode 100644 index 000000000..ef7cb942f --- /dev/null +++ b/tests/test_to_pymatgen_entry.py @@ -0,0 +1,29 @@ +import os +import numpy as np +import unittest +from context import dpdata +from comp_sys import CompSys, IsPBC +from monty.serialization import loadfn +try: + from pymatgen.entries.computed_entries import ComputedStructureEntry + exist_module=True +except: + exist_module=False + +@unittest.skipIf(not exist_module,"skip pymatgen") +class TestPymatgen(unittest.TestCase): + + def test(self): + ls1= dpdata.LabeledSystem(os.path.join('poscars', 'OUTCAR.ch4.1step'),fmt='OUTCAR') + entry1=ls1.to_pymatgen_ComputedStructureEntry() + self.assertEqual(entry1,[]) + ls2= dpdata.LabeledSystem(os.path.join('poscars', 'OUTCAR.h2o.md.10'),fmt='OUTCAR') + entry2=ls2.to_pymatgen_ComputedStructureEntry() + self.assertEqual(len(entry2),10) + last_entry=loadfn("computed_structure_entry.json") + self.assertEqual(last_entry.as_dict(),entry2[-1].as_dict()) + + +if __name__ == '__main__': + unittest.main() +