diff --git a/dpdata/plugins/pymatgen.py b/dpdata/plugins/pymatgen.py index 322298c3c..b8099a3ab 100644 --- a/dpdata/plugins/pymatgen.py +++ b/dpdata/plugins/pymatgen.py @@ -30,16 +30,15 @@ def to_system(self, data, **kwargs): """Convert System to Pymatgen Structure obj.""" structures = [] try: - from pymatgen.core import Structure + from pymatgen.core import Lattice, Structure except ModuleNotFoundError as e: raise ImportError("No module pymatgen.Structure") from e - species = [] - for name, numb in zip(data["atom_names"], data["atom_numbs"]): - species.extend([name] * numb) + species = [data["atom_names"][tt] for tt in data["atom_types"]] + pbc = not (data.get("nopbc", False)) for ii in range(data["coords"].shape[0]): structure = Structure( - data["cells"][ii], + Lattice(data["cells"][ii], pbc=[pbc] * 3), species, data["coords"][ii], coords_are_cartesian=True, diff --git a/dpdata/pymatgen/structure.py b/dpdata/pymatgen/structure.py index 36e411c02..1f74dbdd0 100644 --- a/dpdata/pymatgen/structure.py +++ b/dpdata/pymatgen/structure.py @@ -4,12 +4,19 @@ def from_system_data(structure) -> dict: - symbols = [site.species_string for site in structure] + """Convert one pymatgen structure to dpdata's datadict.""" + symbols = [ii.specie.symbol for ii in structure] atom_names = list(structure.symbol_set) atom_numbs = [symbols.count(symbol) for symbol in atom_names] atom_types = np.array([atom_names.index(symbol) for symbol in symbols]).astype(int) coords = structure.cart_coords cells = structure.lattice.matrix + if all(structure.pbc): + pbc = True + elif not any(structure.pbc): + pbc = False + else: + raise ValueError(f"Partial pbc condition {structure.pbc} is not supported") info_dict = { "atom_names": atom_names, @@ -17,5 +24,7 @@ def from_system_data(structure) -> dict: "atom_types": atom_types, "coords": np.array([coords]), "cells": np.array([cells]), + "orig": np.zeros(3), + "nopbc": not pbc, } return info_dict diff --git a/tests/pymatgen_data/deepmd/set.000/box.npy b/tests/pymatgen_data/deepmd/set.000/box.npy new file mode 100644 index 000000000..566749c13 Binary files /dev/null and b/tests/pymatgen_data/deepmd/set.000/box.npy differ diff --git a/tests/pymatgen_data/deepmd/set.000/coord.npy b/tests/pymatgen_data/deepmd/set.000/coord.npy new file mode 100644 index 000000000..f30e67882 Binary files /dev/null and b/tests/pymatgen_data/deepmd/set.000/coord.npy differ diff --git a/tests/pymatgen_data/deepmd/set.000/energy.npy b/tests/pymatgen_data/deepmd/set.000/energy.npy new file mode 100644 index 000000000..3e591429a Binary files /dev/null and b/tests/pymatgen_data/deepmd/set.000/energy.npy differ diff --git a/tests/pymatgen_data/deepmd/set.000/force.npy b/tests/pymatgen_data/deepmd/set.000/force.npy new file mode 100644 index 000000000..4546a8143 Binary files /dev/null and b/tests/pymatgen_data/deepmd/set.000/force.npy differ diff --git a/tests/pymatgen_data/deepmd/set.000/virial.npy b/tests/pymatgen_data/deepmd/set.000/virial.npy new file mode 100644 index 000000000..74292ab5f Binary files /dev/null and b/tests/pymatgen_data/deepmd/set.000/virial.npy differ diff --git a/tests/pymatgen_data/deepmd/type.raw b/tests/pymatgen_data/deepmd/type.raw new file mode 100644 index 000000000..479b28ae4 --- /dev/null +++ b/tests/pymatgen_data/deepmd/type.raw @@ -0,0 +1,98 @@ +1 +1 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +3 +3 +3 +3 +3 +3 +3 +3 +3 +3 +3 +3 +3 +3 +3 +3 +2 +2 +2 +2 +2 +2 +2 +2 +2 +2 +2 +2 +2 +2 +2 +2 +2 +2 +2 +2 +2 +2 +2 +2 +2 +2 +2 +2 +2 +2 +2 +2 +2 +2 +2 +2 +2 +2 +2 +2 +2 +2 +2 +2 +2 +2 +2 +2 +2 +2 +2 +2 +2 +2 +2 +2 +2 +2 +2 +2 +2 +2 +2 +2 diff --git a/tests/pymatgen_data/deepmd/type_map.raw b/tests/pymatgen_data/deepmd/type_map.raw new file mode 100644 index 000000000..e2400b8fe --- /dev/null +++ b/tests/pymatgen_data/deepmd/type_map.raw @@ -0,0 +1,4 @@ +Fe +Li +O +P diff --git a/tests/test_from_pymatgen.py b/tests/test_from_pymatgen.py deleted file mode 100644 index 7689a9d5e..000000000 --- a/tests/test_from_pymatgen.py +++ /dev/null @@ -1,32 +0,0 @@ -from __future__ import annotations - -import os -import unittest - -from comp_sys import CompSys -from context import dpdata - -try: - from pymatgen.core import Structure # noqa: F401 - - exist_module = True -except Exception: - exist_module = False - - -@unittest.skipIf(not exist_module, "skip pymatgen") -class TestFormPytmatgen(unittest.TestCase, CompSys): - def setUp(self): - structure = Structure.from_file(os.path.join("poscars", "POSCAR.P42nmc")) - self.system_1 = dpdata.System(structure, fmt="pymatgen/structure") - self.system_2 = dpdata.System( - os.path.join("poscars", "POSCAR.P42nmc"), fmt="poscar" - ) - self.places = 6 - self.e_places = 6 - self.f_places = 6 - self.v_places = 6 - - -if __name__ == "__main__": - unittest.main() diff --git a/tests/test_pymatgen_structure.py b/tests/test_pymatgen_structure.py new file mode 100644 index 000000000..1e93829e1 --- /dev/null +++ b/tests/test_pymatgen_structure.py @@ -0,0 +1,61 @@ +from __future__ import annotations + +import os +import unittest + +from comp_sys import CompSys, IsNoPBC, IsPBC +from context import dpdata + +try: + from pymatgen.core import Structure # noqa: F401 + + exist_module = True +except Exception: + exist_module = False + + +@unittest.skipIf(not exist_module, "skip pymatgen") +class TestFormPytmatgen(unittest.TestCase, CompSys): + def setUp(self): + structure = Structure.from_file(os.path.join("poscars", "POSCAR.P42nmc")) + self.system_1 = dpdata.System(structure, fmt="pymatgen/structure") + self.system_2 = dpdata.System( + os.path.join("poscars", "POSCAR.P42nmc"), fmt="poscar" + ) + self.places = 6 + self.e_places = 6 + self.f_places = 6 + self.v_places = 6 + + +@unittest.skipIf(not exist_module, "skip pymatgen") +class TestFormToPytmatgen(unittest.TestCase, CompSys, IsPBC): + def setUp(self): + self.system = dpdata.System("pymatgen_data/deepmd/", fmt="deepmd/npy") + self.system_1 = self.system + self.system_2 = dpdata.System().from_pymatgen_structure( + self.system.to("pymatgen/structure")[0] + ) + self.places = 6 + self.e_places = 6 + self.f_places = 6 + self.v_places = 6 + + +@unittest.skipIf(not exist_module, "skip pymatgen") +class TestFormToPytmatgenNopbc(unittest.TestCase, CompSys, IsNoPBC): + def setUp(self): + self.system = dpdata.System("pymatgen_data/deepmd/", fmt="deepmd/npy") + self.system.data["nopbc"] = True + self.system_1 = self.system + self.system_2 = dpdata.System().from_pymatgen_structure( + self.system.to("pymatgen/structure")[0] + ) + self.places = 6 + self.e_places = 6 + self.f_places = 6 + self.v_places = 6 + + +if __name__ == "__main__": + unittest.main()