Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions dpdata/plugins/pymatgen.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,29 @@
import numpy as np

import dpdata.pymatgen.molecule
import dpdata.pymatgen.structure
from dpdata.format import Format


@Format.register("pymatgen/structure")
class PyMatgenStructureFormat(Format):
def from_system(self, structure, **kwargs) -> dict:
"""Convert pymatgen.core.Structure to System.

Parameters
----------
structure : pymatgen.core.Structure
a Pymatgen Structure, containing a structure
**kwargs : dict
other parameters

Returns
-------
dict
data dict
"""
return dpdata.pymatgen.structure.from_system_data(structure)

def to_system(self, data, **kwargs):
"""Convert System to Pymatgen Structure obj."""
structures = []
Expand Down
24 changes: 24 additions & 0 deletions dpdata/pymatgen/structure.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import numpy as np

try:
from pymatgen.core import Structure # noqa: F401
except ImportError:
pass


def from_system_data(structure) -> dict:
symbols = [site.species_string for site 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

info_dict = {
"atom_names": atom_names,
"atom_numbs": atom_numbs,
"atom_types": atom_types,
"coords": np.array([coords]),
"cells": np.array([cells]),
}
return info_dict
30 changes: 30 additions & 0 deletions tests/test_from_pymatgen.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
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()
4 changes: 2 additions & 2 deletions tests/test_to_pymatgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from context import dpdata

try:
from pymatgen import Structure # noqa: F401
from pymatgen.core import Structure # noqa: F401

exist_module = True
except Exception:
Expand All @@ -19,7 +19,7 @@ def setUp(self):
system_1.from_lammps_lmp(
os.path.join("poscars", "conf.lmp"), type_map=["O", "H"]
)
system_1.to_pymatgen_structure()[0].to("poscar", "tmp.POSCAR")
system_1.to_pymatgen_structure()[0].to(filename="tmp.POSCAR", fmt="poscar")
self.system_1 = system_1
self.system_2 = dpdata.System("tmp.POSCAR")
self.places = 6
Expand Down