diff --git a/.git_archival.txt b/.git_archival.txt new file mode 100644 index 00000000..8fb235d7 --- /dev/null +++ b/.git_archival.txt @@ -0,0 +1,4 @@ +node: $Format:%H$ +node-date: $Format:%cI$ +describe-name: $Format:%(describe:tags=true,match=*[0-9]*)$ +ref-names: $Format:%D$ diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 00000000..00a7b00c --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +.git_archival.txt export-subst diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 3e88b72b..e4d9a91f 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -12,10 +12,10 @@ jobs: python-version: ["3.7", "3.8", "3.12"] steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 # set up conda - name: Set up Python ${{ matrix.python-version }} - uses: actions/setup-python@v4 + uses: actions/setup-python@v5 with: python-version: ${{ matrix.python-version }} # install rdkit and openbabel diff --git a/.github/workflows/test_import.yml b/.github/workflows/test_import.yml index b04d05cd..6ab0dbe2 100644 --- a/.github/workflows/test_import.yml +++ b/.github/workflows/test_import.yml @@ -8,8 +8,8 @@ jobs: build: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 - - uses: actions/setup-python@v2 + - uses: actions/checkout@v4 + - uses: actions/setup-python@v5 with: python-version: '3.9' architecture: 'x64' diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index a7a2afa8..c199c7ad 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -2,7 +2,7 @@ # See https://pre-commit.com/hooks.html for more hooks repos: - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v4.5.0 + rev: v4.6.0 hooks: # there are many log files in tests # TODO: seperate py files and log files diff --git a/dpdata/plugins/pymatgen.py b/dpdata/plugins/pymatgen.py index 82b64e71..e7e527ff 100644 --- a/dpdata/plugins/pymatgen.py +++ b/dpdata/plugins/pymatgen.py @@ -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 = [] diff --git a/dpdata/pymatgen/structure.py b/dpdata/pymatgen/structure.py new file mode 100644 index 00000000..b6c148da --- /dev/null +++ b/dpdata/pymatgen/structure.py @@ -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 diff --git a/pyproject.toml b/pyproject.toml index 8fe408eb..3f09801a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,5 +1,5 @@ [build-system] -requires = ["setuptools>=61", "setuptools_scm[toml]>=6.2"] +requires = ["setuptools>=61", "setuptools_scm[toml]>=7"] build-backend = "setuptools.build_meta" [project] diff --git a/tests/test_from_pymatgen.py b/tests/test_from_pymatgen.py new file mode 100644 index 00000000..d3ddbe3e --- /dev/null +++ b/tests/test_from_pymatgen.py @@ -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() diff --git a/tests/test_to_pymatgen.py b/tests/test_to_pymatgen.py index d5815396..b55443d4 100644 --- a/tests/test_to_pymatgen.py +++ b/tests/test_to_pymatgen.py @@ -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: @@ -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