From 7db3ddf0998c335b73514614a60744250c870a30 Mon Sep 17 00:00:00 2001 From: tuoping Date: Mon, 23 Aug 2021 13:49:54 +0800 Subject: [PATCH 01/12] add .github/workflows/mirror_gitee.yml --- .github/workflows/mirror_gitee.yml | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 .github/workflows/mirror_gitee.yml diff --git a/.github/workflows/mirror_gitee.yml b/.github/workflows/mirror_gitee.yml new file mode 100644 index 000000000..c947be2ee --- /dev/null +++ b/.github/workflows/mirror_gitee.yml @@ -0,0 +1,18 @@ +name: Mirror to Gitee Repo + +on: [ push, delete, create ] + +# Ensures that only one mirror task will run at a time. +concurrency: + group: git-mirror + +jobs: + git-mirror: + runs-on: ubuntu-latest + steps: + - uses: wearerequired/git-mirror-action@v1 + env: + SSH_PRIVATE_KEY: ${{ secrets.SYNC_GITEE_PRIVATE_KEY }} + with: + source-repo: "git@github.com:deepmodeling/dpdata.git" + destination-repo: "git@gitee.com:deepmodeling/dpdata.git" From 76107ebdf11ed2dcd13f07907ffb8c6b070b0045 Mon Sep 17 00:00:00 2001 From: tuoping Date: Mon, 13 Sep 2021 14:36:00 +0800 Subject: [PATCH 02/12] implement support for pymatgen.core.Molecule --- dpdata/plugins/pymatgen.py | 33 +++++++++++++++++++++++++++++++++ dpdata/pymatgen/__init__.py | 0 dpdata/pymatgen/molecule.py | 25 +++++++++++++++++++++++++ 3 files changed, 58 insertions(+) create mode 100644 dpdata/pymatgen/__init__.py create mode 100644 dpdata/pymatgen/molecule.py diff --git a/dpdata/plugins/pymatgen.py b/dpdata/plugins/pymatgen.py index 84d71f0e4..e8fd86af3 100644 --- a/dpdata/plugins/pymatgen.py +++ b/dpdata/plugins/pymatgen.py @@ -1,4 +1,6 @@ from dpdata.format import Format +import dpdata.pymatgen.molecule +from collections import Counter @Format.register("pymatgen/structure") @@ -22,6 +24,35 @@ def to_system(self, data, **kwargs): return structures +@Format.register("pymatgen/molecule") +class PyMatgenMoleculeFormat(Format): + def from_system(self, file_name, **kwargs): + try: + from pymatgen.core import Molecule + except ModuleNotFoundError as e: + raise ImportError('No module pymatgen.Molecule') from e + + return dpdata.pymatgen.molecule.to_system_data(file_name) + + def to_system(self, data, **kwargs): + """convert System to Pymatgen Structure obj + """ + molecules = [] + try: + from pymatgen.core import Molecule + except ModuleNotFoundError as e: + raise ImportError('No module pymatgen.Molecule') from e + + species = [] + for name, numb in zip(data['atom_names'], data['atom_numbs']): + species.extend([name]*numb) + for ii in range(data['coords'].shape[0]): + molecule = Molecule( + species, data['coords'][ii]) + molecules.append(molecule) + return molecules + + @Format.register("pymatgen/computedstructureentry") @Format.register_to("to_pymatgen_ComputedStructureEntry") class PyMatgenCSEFormat(Format): @@ -44,3 +75,5 @@ def to_labeled_system(self, data, *args, **kwargs): entry = ComputedStructureEntry(structure, energy, data=csedata) entries.append(entry) return entries + + diff --git a/dpdata/pymatgen/__init__.py b/dpdata/pymatgen/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/dpdata/pymatgen/molecule.py b/dpdata/pymatgen/molecule.py new file mode 100644 index 000000000..2b591b373 --- /dev/null +++ b/dpdata/pymatgen/molecule.py @@ -0,0 +1,25 @@ +from pymatgen.core import Molecule + +def to_system_data(file_name) : + mol = Molecule.from_file(file_name) + elem_mol = list(str(site.species.elements[0]) for site in mol.sites) + elem_counter = Counter(elem_mol) + atom_names = elem_counter.keys() + atom_numbs = elem_counter.values() + + atom_types = [] + for idx,ii in enumerate(atom_numbs) : + for jj in range(ii) : + atom_types.append(idx) + + system = {} + system['atom_names'] = atom_names + system['atom_numbs'] = atom_numbs + system['atom_types'] = np.array(atom_types, dtype = int) + system['orig'] = np.array([0, 0, 0]) + system['cells'] = [] + + mol.get_centered_molecule() + system['coords'] = [np.copy(mol.cart_coords)] + + return system From b7f36757565d0669fa2af81025be63a02e6edb93 Mon Sep 17 00:00:00 2001 From: tuoping Date: Mon, 13 Sep 2021 14:58:40 +0800 Subject: [PATCH 03/12] implement support for pymatgen.core.Molecule --- dpdata/plugins/pymatgen.py | 1 - dpdata/pymatgen/molecule.py | 4 +++- tests/pymatgen/FA-001.xyz | 10 ++++++++++ tests/test_pymatgen_molecule.py | 20 ++++++++++++++++++++ 4 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 tests/pymatgen/FA-001.xyz create mode 100644 tests/test_pymatgen_molecule.py diff --git a/dpdata/plugins/pymatgen.py b/dpdata/plugins/pymatgen.py index e8fd86af3..e1d543cb2 100644 --- a/dpdata/plugins/pymatgen.py +++ b/dpdata/plugins/pymatgen.py @@ -1,6 +1,5 @@ from dpdata.format import Format import dpdata.pymatgen.molecule -from collections import Counter @Format.register("pymatgen/structure") diff --git a/dpdata/pymatgen/molecule.py b/dpdata/pymatgen/molecule.py index 2b591b373..4010467e9 100644 --- a/dpdata/pymatgen/molecule.py +++ b/dpdata/pymatgen/molecule.py @@ -1,4 +1,6 @@ +import numpy as np from pymatgen.core import Molecule +from collections import Counter def to_system_data(file_name) : mol = Molecule.from_file(file_name) @@ -20,6 +22,6 @@ def to_system_data(file_name) : system['cells'] = [] mol.get_centered_molecule() - system['coords'] = [np.copy(mol.cart_coords)] + system['coords'] = np.array([np.copy(mol.cart_coords)]) return system diff --git a/tests/pymatgen/FA-001.xyz b/tests/pymatgen/FA-001.xyz new file mode 100644 index 000000000..4a5cce329 --- /dev/null +++ b/tests/pymatgen/FA-001.xyz @@ -0,0 +1,10 @@ +8 +Lattice="10 0.0 0.0 0.0 10 0.0 0.0 0.0 10" Properties=species:S:1:pos:R:3:Z:I:1 +C 3.1742845125747904e-16 5.342900276 5.184000015 6 +N 3.938218942178196e-16 4.492199898 6.431599617 7 +N 2.4265764993669136e-16 4.454699993 3.962900162 7 +H 4.568238577398768e-16 4.908699989 7.460499763 1 +H 4.022535923897687e-16 3.390799999 6.569299698 1 +H 1.834827076007373e-16 5.000800133 2.996500015 1 +H 2.4585396828529157e-16 3.345999956 4.015100002 1 +H 3.1742845125747904e-16 6.482899666 5.184000015 1 diff --git a/tests/test_pymatgen_molecule.py b/tests/test_pymatgen_molecule.py new file mode 100644 index 000000000..c092578d3 --- /dev/null +++ b/tests/test_pymatgen_molecule.py @@ -0,0 +1,20 @@ +import os +import numpy as np +import unittest +from context import dpdata + +class TestPOSCARCart(unittest.TestCase): + + def setUp(self): + self.system = dpdata.System() + self.system.from_pymatgen_molecule(os.path.join('pymatgen', 'FA-001.xyz')) + + def test_to_molecule(self): + mols = self.system.to_pymatgen_molecule() + print(mols[-1]) + + + +if __name__ == '__main__': + unittest.main() + From b0ad014b6dc6590fb1a5380e94166cfb86651bf7 Mon Sep 17 00:00:00 2001 From: tuoping Date: Tue, 14 Sep 2021 11:34:29 +0800 Subject: [PATCH 04/12] Change atom_types in dpdata/pymatgen/molecule.py --- dpdata/pymatgen/molecule.py | 6 +----- tests/pymatgen/FA-001.xyz | 4 ++-- tests/test_pymatgen_molecule.py | 4 ++-- 3 files changed, 5 insertions(+), 9 deletions(-) diff --git a/dpdata/pymatgen/molecule.py b/dpdata/pymatgen/molecule.py index 4010467e9..50d3e2b5d 100644 --- a/dpdata/pymatgen/molecule.py +++ b/dpdata/pymatgen/molecule.py @@ -8,11 +8,7 @@ def to_system_data(file_name) : elem_counter = Counter(elem_mol) atom_names = elem_counter.keys() atom_numbs = elem_counter.values() - - atom_types = [] - for idx,ii in enumerate(atom_numbs) : - for jj in range(ii) : - atom_types.append(idx) + atom_types = [list(atom_names).index(e) for e in elem_mol] system = {} system['atom_names'] = atom_names diff --git a/tests/pymatgen/FA-001.xyz b/tests/pymatgen/FA-001.xyz index 4a5cce329..ae5b55695 100644 --- a/tests/pymatgen/FA-001.xyz +++ b/tests/pymatgen/FA-001.xyz @@ -1,10 +1,10 @@ 8 Lattice="10 0.0 0.0 0.0 10 0.0 0.0 0.0 10" Properties=species:S:1:pos:R:3:Z:I:1 C 3.1742845125747904e-16 5.342900276 5.184000015 6 -N 3.938218942178196e-16 4.492199898 6.431599617 7 -N 2.4265764993669136e-16 4.454699993 3.962900162 7 H 4.568238577398768e-16 4.908699989 7.460499763 1 +N 3.938218942178196e-16 4.492199898 6.431599617 7 H 4.022535923897687e-16 3.390799999 6.569299698 1 H 1.834827076007373e-16 5.000800133 2.996500015 1 +N 2.4265764993669136e-16 4.454699993 3.962900162 7 H 2.4585396828529157e-16 3.345999956 4.015100002 1 H 3.1742845125747904e-16 6.482899666 5.184000015 1 diff --git a/tests/test_pymatgen_molecule.py b/tests/test_pymatgen_molecule.py index c092578d3..411ec255f 100644 --- a/tests/test_pymatgen_molecule.py +++ b/tests/test_pymatgen_molecule.py @@ -8,13 +8,13 @@ class TestPOSCARCart(unittest.TestCase): def setUp(self): self.system = dpdata.System() self.system.from_pymatgen_molecule(os.path.join('pymatgen', 'FA-001.xyz')) + self.assertEqual(list(self.system["atom_types"]), [0, 1, 2, 1, 1, 2, 1, 1]) def test_to_molecule(self): mols = self.system.to_pymatgen_molecule() - print(mols[-1]) + self.assertEqual(len(mols), 1) if __name__ == '__main__': unittest.main() - From b2bfd6f785b30b8697c14a0dceaa30a9d5a5e605 Mon Sep 17 00:00:00 2001 From: tuoping Date: Tue, 14 Sep 2021 11:38:22 +0800 Subject: [PATCH 05/12] Change doc string in dpdata/plugins/pymatgen.py --- dpdata/plugins/pymatgen.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dpdata/plugins/pymatgen.py b/dpdata/plugins/pymatgen.py index e1d543cb2..74693ad4b 100644 --- a/dpdata/plugins/pymatgen.py +++ b/dpdata/plugins/pymatgen.py @@ -34,7 +34,7 @@ def from_system(self, file_name, **kwargs): return dpdata.pymatgen.molecule.to_system_data(file_name) def to_system(self, data, **kwargs): - """convert System to Pymatgen Structure obj + """convert System to Pymatgen Molecule obj """ molecules = [] try: From f2aeb472fd48ea28cbf0b285d31b5b6a404becc7 Mon Sep 17 00:00:00 2001 From: tuoping Date: Tue, 14 Sep 2021 12:49:21 +0800 Subject: [PATCH 06/12] add dpdata/pymatgin to setup.py --- setup.py | 1 + 1 file changed, 1 insertion(+) diff --git a/setup.py b/setup.py index c0ded7819..ea2e154e1 100644 --- a/setup.py +++ b/setup.py @@ -41,6 +41,7 @@ 'dpdata/abacus', 'dpdata/rdkit', 'dpdata/plugins', + 'dpdata/pymatgen', ], package_data={'dpdata':['*.json']}, classifiers=[ From dbba82e382cdb425171169ac6d5f9f338a7f8221 Mon Sep 17 00:00:00 2001 From: tuoping Date: Fri, 24 Sep 2021 17:05:55 +0800 Subject: [PATCH 07/12] modify molecule.py --- dpdata/pymatgen/molecule.py | 1 - dpdata/system.py | 1 + tests/pymatgen/mol2-new.vasp | 16 ++++++++++++++++ tests/pymatgen/mol2.vasp | 16 ++++++++++++++++ tests/test_pymatgen_molecule.py | 6 ++++++ 5 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 tests/pymatgen/mol2-new.vasp create mode 100644 tests/pymatgen/mol2.vasp diff --git a/dpdata/pymatgen/molecule.py b/dpdata/pymatgen/molecule.py index 50d3e2b5d..a6bf1833b 100644 --- a/dpdata/pymatgen/molecule.py +++ b/dpdata/pymatgen/molecule.py @@ -15,7 +15,6 @@ def to_system_data(file_name) : system['atom_numbs'] = atom_numbs system['atom_types'] = np.array(atom_types, dtype = int) system['orig'] = np.array([0, 0, 0]) - system['cells'] = [] mol.get_centered_molecule() system['coords'] = np.array([np.copy(mol.cart_coords)]) diff --git a/dpdata/system.py b/dpdata/system.py index b0b9345b3..dc5649ff4 100644 --- a/dpdata/system.py +++ b/dpdata/system.py @@ -428,6 +428,7 @@ def apply_pbc(self) : self.data['coords'] = np.matmul(ncoord, self.data['cells']) + @post_funcs.register("remove_pbc") def remove_pbc(self, protect_layer = 9): """ This method does NOT delete the definition of the cells, it diff --git a/tests/pymatgen/mol2-new.vasp b/tests/pymatgen/mol2-new.vasp new file mode 100644 index 000000000..2e14597e3 --- /dev/null +++ b/tests/pymatgen/mol2-new.vasp @@ -0,0 +1,16 @@ +H5 C1 N2 +1.0 +10.000000 0.000000 0.000000 +0.000000 10.000000 0.000000 +0.000000 0.000000 10.000000 +H C N +5 1 2 +direct +0.577397 0.631332 0.482529 H +0.440377 0.395634 0.605171 H +0.522686 0.529099 1.077268 H +0.521505 0.528798 0.684766 H +0.441491 0.395684 0.358562 H +0.521999 0.537270 0.482268 C +0.490955 0.484741 0.365937 N +0.489933 0.484700 0.598299 N diff --git a/tests/pymatgen/mol2.vasp b/tests/pymatgen/mol2.vasp new file mode 100644 index 000000000..7cee8d859 --- /dev/null +++ b/tests/pymatgen/mol2.vasp @@ -0,0 +1,16 @@ +hexagonal-PbI3 + 1.00000000000000 + 8.4261398403369476 -0.0207094818066008 -0.0031014314051307 + -4.2326852079273163 7.4079517949230027 0.0021007606821090 + -0.0033015270376727 0.0018001191064931 7.9750946862221301 + N C H + 2 1 5 +Direct + 0.3230984076298932 0.6454769144393403 0.1041651474646342 + 0.3220267174078323 0.6453279258574751 0.3955245499807875 + 0.3958479883967422 0.7164380537464232 0.2500334235810864 + 0.5257412318253072 0.8435919770693032 0.2503606409552951 + 0.2024793011832460 0.5249247262853158 0.4041424751787161 + 0.3915235842243226 0.7051542887367509 0.9961063077723067 + 0.3896311952726235 0.7049043576463694 0.5039461714481914 + 0.2036571496859804 0.5250922648687301 0.0949178563751044 diff --git a/tests/test_pymatgen_molecule.py b/tests/test_pymatgen_molecule.py index 411ec255f..c44723fdf 100644 --- a/tests/test_pymatgen_molecule.py +++ b/tests/test_pymatgen_molecule.py @@ -14,6 +14,12 @@ def test_to_molecule(self): mols = self.system.to_pymatgen_molecule() self.assertEqual(len(mols), 1) + def test_remove_pbc(self): + tmp_system = dpdata.System() + tmp_system.from_vasp_poscar(os.path.join('pymatgen', 'mol2.vasp')) + mols = tmp_system.to("pymatgen/molecule") + struct = mols[-1].get_boxed_structure(10.0, 10.0, 10.0) + struct.to(fmt = "poscar", filename = os.path.join('pymatgen', 'mol2-new.vasp')) if __name__ == '__main__': From 810bfff2b9bb584b6492d3557523a34b952a2e8d Mon Sep 17 00:00:00 2001 From: tuoping Date: Fri, 24 Sep 2021 17:08:27 +0800 Subject: [PATCH 08/12] modify molecule.py --- dpdata/plugins/pymatgen.py | 1 + dpdata/pymatgen/molecule.py | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/dpdata/plugins/pymatgen.py b/dpdata/plugins/pymatgen.py index 74693ad4b..0d34396d5 100644 --- a/dpdata/plugins/pymatgen.py +++ b/dpdata/plugins/pymatgen.py @@ -25,6 +25,7 @@ def to_system(self, data, **kwargs): @Format.register("pymatgen/molecule") class PyMatgenMoleculeFormat(Format): + @Format.post("shift_orig_zero") def from_system(self, file_name, **kwargs): try: from pymatgen.core import Molecule diff --git a/dpdata/pymatgen/molecule.py b/dpdata/pymatgen/molecule.py index a6bf1833b..84610dad7 100644 --- a/dpdata/pymatgen/molecule.py +++ b/dpdata/pymatgen/molecule.py @@ -14,7 +14,8 @@ def to_system_data(file_name) : system['atom_names'] = atom_names system['atom_numbs'] = atom_numbs system['atom_types'] = np.array(atom_types, dtype = int) - system['orig'] = np.array([0, 0, 0]) + center = mol.center_of_mass + system['orig'] = np.array(center) mol.get_centered_molecule() system['coords'] = np.array([np.copy(mol.cart_coords)]) From 5ed8b9d5a119d1fd93a1c96a34e0d195c802b43c Mon Sep 17 00:00:00 2001 From: tuoping Date: Fri, 24 Sep 2021 18:05:06 +0800 Subject: [PATCH 09/12] modify molecule.py --- dpdata/plugins/pymatgen.py | 1 + dpdata/pymatgen/molecule.py | 22 ++++++++++++++++++---- tests/test_pymatgen_molecule.py | 3 ++- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/dpdata/plugins/pymatgen.py b/dpdata/plugins/pymatgen.py index 0d34396d5..54c34eb9e 100644 --- a/dpdata/plugins/pymatgen.py +++ b/dpdata/plugins/pymatgen.py @@ -34,6 +34,7 @@ def from_system(self, file_name, **kwargs): return dpdata.pymatgen.molecule.to_system_data(file_name) + @Format.post("remove_pbc") def to_system(self, data, **kwargs): """convert System to Pymatgen Molecule obj """ diff --git a/dpdata/pymatgen/molecule.py b/dpdata/pymatgen/molecule.py index 84610dad7..0cb59ff8c 100644 --- a/dpdata/pymatgen/molecule.py +++ b/dpdata/pymatgen/molecule.py @@ -2,14 +2,27 @@ from pymatgen.core import Molecule from collections import Counter -def to_system_data(file_name) : +def to_system_data(file_name, protect_layer = 9) : mol = Molecule.from_file(file_name) elem_mol = list(str(site.species.elements[0]) for site in mol.sites) elem_counter = Counter(elem_mol) - atom_names = elem_counter.keys() - atom_numbs = elem_counter.values() + atom_names = list(elem_counter.keys()) + atom_numbs = list(elem_counter.values()) atom_types = [list(atom_names).index(e) for e in elem_mol] + natoms = np.sum(atom_numbs) + print(atom_numbs) + print(natoms) + tmpcoord = np.copy(mol.cart_coords) + cog = np.average(tmpcoord, axis = 0) + dist = tmpcoord - np.tile(cog, [natoms, 1]) + max_dist = np.max(np.linalg.norm(dist, axis = 1)) + h_cell_size = max_dist + protect_layer + cell_size = h_cell_size * 2 + shift = np.array([1,1,1]) * h_cell_size - cog + tmpcoord = tmpcoord + np.tile(shift, [natoms, 1]) + tmpcell = cell_size * np.eye(3) + system = {} system['atom_names'] = atom_names system['atom_numbs'] = atom_numbs @@ -18,6 +31,7 @@ def to_system_data(file_name) : system['orig'] = np.array(center) mol.get_centered_molecule() - system['coords'] = np.array([np.copy(mol.cart_coords)]) + system['coords'] = tmpcoord + system['cells'] = tmpcell return system diff --git a/tests/test_pymatgen_molecule.py b/tests/test_pymatgen_molecule.py index c44723fdf..12df18bdc 100644 --- a/tests/test_pymatgen_molecule.py +++ b/tests/test_pymatgen_molecule.py @@ -8,13 +8,14 @@ class TestPOSCARCart(unittest.TestCase): def setUp(self): self.system = dpdata.System() self.system.from_pymatgen_molecule(os.path.join('pymatgen', 'FA-001.xyz')) + self.system.to("vasp/poscar", "FA-001.vasp") self.assertEqual(list(self.system["atom_types"]), [0, 1, 2, 1, 1, 2, 1, 1]) def test_to_molecule(self): mols = self.system.to_pymatgen_molecule() self.assertEqual(len(mols), 1) - def test_remove_pbc(self): + def test_to_vasp(self): tmp_system = dpdata.System() tmp_system.from_vasp_poscar(os.path.join('pymatgen', 'mol2.vasp')) mols = tmp_system.to("pymatgen/molecule") From aa665bc9d0fae4417624688a9cf06ccb25dc2ba8 Mon Sep 17 00:00:00 2001 From: tuoping Date: Wed, 29 Sep 2021 16:34:15 +0800 Subject: [PATCH 10/12] Support of pymatgen.Molecule in dpdata --- dpdata/plugins/pymatgen.py | 7 ++++--- dpdata/pymatgen/molecule.py | 34 ++++++++++++++++++--------------- dpdata/system.py | 16 +++------------- tests/pymatgen/FA-001.vasp | 16 ++++++++++++++++ tests/pymatgen/mol2-new.vasp | 16 ++++++++-------- tests/pymatgen/mol2.vasp | 2 +- tests/test_pymatgen_molecule.py | 17 ++++++++++------- 7 files changed, 61 insertions(+), 47 deletions(-) create mode 100644 tests/pymatgen/FA-001.vasp diff --git a/dpdata/plugins/pymatgen.py b/dpdata/plugins/pymatgen.py index 54c34eb9e..838223f0f 100644 --- a/dpdata/plugins/pymatgen.py +++ b/dpdata/plugins/pymatgen.py @@ -1,5 +1,6 @@ from dpdata.format import Format import dpdata.pymatgen.molecule +import numpy as np @Format.register("pymatgen/structure") @@ -25,7 +26,7 @@ def to_system(self, data, **kwargs): @Format.register("pymatgen/molecule") class PyMatgenMoleculeFormat(Format): - @Format.post("shift_orig_zero") + @Format.post("remove_pbc") def from_system(self, file_name, **kwargs): try: from pymatgen.core import Molecule @@ -34,7 +35,6 @@ def from_system(self, file_name, **kwargs): return dpdata.pymatgen.molecule.to_system_data(file_name) - @Format.post("remove_pbc") def to_system(self, data, **kwargs): """convert System to Pymatgen Molecule obj """ @@ -47,7 +47,8 @@ def to_system(self, data, **kwargs): species = [] for name, numb in zip(data['atom_names'], data['atom_numbs']): species.extend([name]*numb) - for ii in range(data['coords'].shape[0]): + data = dpdata.pymatgen.molecule.remove_pbc(data) + for ii in range(np.array(data['coords']).shape[0]): molecule = Molecule( species, data['coords'][ii]) molecules.append(molecule) diff --git a/dpdata/pymatgen/molecule.py b/dpdata/pymatgen/molecule.py index 0cb59ff8c..bef3708b1 100644 --- a/dpdata/pymatgen/molecule.py +++ b/dpdata/pymatgen/molecule.py @@ -1,6 +1,7 @@ import numpy as np from pymatgen.core import Molecule from collections import Counter +import dpdata def to_system_data(file_name, protect_layer = 9) : mol = Molecule.from_file(file_name) @@ -10,28 +11,31 @@ def to_system_data(file_name, protect_layer = 9) : atom_numbs = list(elem_counter.values()) atom_types = [list(atom_names).index(e) for e in elem_mol] natoms = np.sum(atom_numbs) - print(atom_numbs) - print(natoms) tmpcoord = np.copy(mol.cart_coords) - cog = np.average(tmpcoord, axis = 0) - dist = tmpcoord - np.tile(cog, [natoms, 1]) - max_dist = np.max(np.linalg.norm(dist, axis = 1)) - h_cell_size = max_dist + protect_layer - cell_size = h_cell_size * 2 - shift = np.array([1,1,1]) * h_cell_size - cog - tmpcoord = tmpcoord + np.tile(shift, [natoms, 1]) - tmpcell = cell_size * np.eye(3) system = {} system['atom_names'] = atom_names system['atom_numbs'] = atom_numbs system['atom_types'] = np.array(atom_types, dtype = int) - center = mol.center_of_mass - system['orig'] = np.array(center) + # center = [c - h_cell_size for c in mol.center_of_mass] + system['orig'] = np.array([0, 0, 0]) - mol.get_centered_molecule() - system['coords'] = tmpcoord - system['cells'] = tmpcell + system['coords'] = [tmpcoord] + system['cells'] = [10.0 * np.eye(3)] + return system +def remove_pbc(system, protect_layer = 9): + nframes = len(system["coords"]) + natoms = len(system['coords'][0]) + for ff in range(nframes): + tmpcoord = system['coords'][ff] + cog = np.average(tmpcoord, axis = 0) + dist = tmpcoord - np.tile(cog, [natoms, 1]) + max_dist = np.max(np.linalg.norm(dist, axis = 1)) + h_cell_size = max_dist + protect_layer + cell_size = h_cell_size * 2 + shift = np.array([1,1,1]) * h_cell_size - cog + system['coords'][ff] = system['coords'][ff] + np.tile(shift, [natoms, 1]) + system['cells'][ff] = cell_size * np.eye(3) return system diff --git a/dpdata/system.py b/dpdata/system.py index dc5649ff4..8195c1063 100644 --- a/dpdata/system.py +++ b/dpdata/system.py @@ -9,6 +9,7 @@ from monty.serialization import loadfn,dumpfn from dpdata.periodic_table import Element from dpdata.amber.mask import pick_by_amber_mask, load_param_file +import dpdata # ensure all plugins are loaded! import dpdata.plugins @@ -418,7 +419,7 @@ def extend(self, systems): for system in systems: self.append(system.copy()) - + def apply_pbc(self) : """ Append periodic boundary condition @@ -442,19 +443,8 @@ def remove_pbc(self, protect_layer = 9): protect_layer : the protect layer between the atoms and the cell boundary """ - nframes = self.get_nframes() - natoms = self.get_natoms() assert(protect_layer >= 0), "the protect_layer should be no less than 0" - for ff in range(nframes): - tmpcoord = self.data['coords'][ff] - cog = np.average(tmpcoord, axis = 0) - dist = tmpcoord - np.tile(cog, [natoms, 1]) - max_dist = np.max(np.linalg.norm(dist, axis = 1)) - h_cell_size = max_dist + protect_layer - cell_size = h_cell_size * 2 - shift = np.array([1,1,1]) * h_cell_size - cog - self.data['coords'][ff] = self.data['coords'][ff] + np.tile(shift, [natoms, 1]) - self.data['cells'][ff] = cell_size * np.eye(3) + dpdata.pymatgen.molecule.remove_pbc(self.data, protect_layer) def affine_map(self, trans, f_idx = 0) : assert(np.linalg.det(trans) != 0) diff --git a/tests/pymatgen/FA-001.vasp b/tests/pymatgen/FA-001.vasp new file mode 100644 index 000000000..1b267adf9 --- /dev/null +++ b/tests/pymatgen/FA-001.vasp @@ -0,0 +1,16 @@ +C1 H5 N2 +1.0 +2.2504659203492643e+01 0.0000000000000000e+00 0.0000000000000000e+00 +0.0000000000000000e+00 2.2504659203492643e+01 0.0000000000000000e+00 +0.0000000000000000e+00 0.0000000000000000e+00 2.2504659203492643e+01 +C H N +1 5 2 +Cartesian + 11.2523296017 11.9178548890 11.2108422059 + 11.2523296017 11.4836546020 13.4873419539 + 11.2523296017 9.9657546120 12.5961418889 + 11.2523296017 11.5757547460 9.0233422059 + 11.2523296017 9.9209545690 10.0419421929 + 11.2523296017 13.0578542790 11.2108422059 + 11.2523296017 11.0671545110 12.4584418079 + 11.2523296017 11.0296546060 9.9897423529 diff --git a/tests/pymatgen/mol2-new.vasp b/tests/pymatgen/mol2-new.vasp index 2e14597e3..22db2fa9a 100644 --- a/tests/pymatgen/mol2-new.vasp +++ b/tests/pymatgen/mol2-new.vasp @@ -6,11 +6,11 @@ H5 C1 N2 H C N 5 1 2 direct -0.577397 0.631332 0.482529 H -0.440377 0.395634 0.605171 H -0.522686 0.529099 1.077268 H -0.521505 0.528798 0.684766 H -0.441491 0.395684 0.358562 H -0.521999 0.537270 0.482268 C -0.490955 0.484741 0.365937 N -0.489933 0.484700 0.598299 N +0.577383 0.631337 0.500367 H +0.440363 0.395639 0.623009 H +0.523296 0.528867 0.297597 H +0.521491 0.528803 0.702604 H +0.441477 0.395690 0.376400 H +0.521985 0.537275 0.500106 C +0.490941 0.484747 0.383775 N +0.489919 0.484705 0.616136 N diff --git a/tests/pymatgen/mol2.vasp b/tests/pymatgen/mol2.vasp index 7cee8d859..ea4ec2baf 100644 --- a/tests/pymatgen/mol2.vasp +++ b/tests/pymatgen/mol2.vasp @@ -11,6 +11,6 @@ Direct 0.3958479883967422 0.7164380537464232 0.2500334235810864 0.5257412318253072 0.8435919770693032 0.2503606409552951 0.2024793011832460 0.5249247262853158 0.4041424751787161 - 0.3915235842243226 0.7051542887367509 0.9961063077723067 + 0.3915235842243226 0.7051542887367509 -0.0038936922276933 0.9961063077723067 0.3896311952726235 0.7049043576463694 0.5039461714481914 0.2036571496859804 0.5250922648687301 0.0949178563751044 diff --git a/tests/test_pymatgen_molecule.py b/tests/test_pymatgen_molecule.py index 12df18bdc..7f4a0abc8 100644 --- a/tests/test_pymatgen_molecule.py +++ b/tests/test_pymatgen_molecule.py @@ -6,16 +6,19 @@ class TestPOSCARCart(unittest.TestCase): def setUp(self): - self.system = dpdata.System() - self.system.from_pymatgen_molecule(os.path.join('pymatgen', 'FA-001.xyz')) - self.system.to("vasp/poscar", "FA-001.vasp") - self.assertEqual(list(self.system["atom_types"]), [0, 1, 2, 1, 1, 2, 1, 1]) + # self.system = dpdata.System() + # self.system.from_pymatgen_molecule(os.path.join('pymatgen', 'FA-001.xyz')) + # self.system.to("vasp/poscar", os.path.join('pymatgen', "FA-001.vasp")) + # self.assertEqual(list(self.system["atom_types"]), [0, 1, 2, 1, 1, 2, 1, 1]) + pass def test_to_molecule(self): - mols = self.system.to_pymatgen_molecule() - self.assertEqual(len(mols), 1) + # mols = self.system.to_pymatgen_molecule() + # self.assertEqual(len(mols), 1) + pass - def test_to_vasp(self): + def test_poscar_to_molecule(self): + print("------------test_poscar_to_molecule---------------------\n") tmp_system = dpdata.System() tmp_system.from_vasp_poscar(os.path.join('pymatgen', 'mol2.vasp')) mols = tmp_system.to("pymatgen/molecule") From 5088fcbe0e5ef495daa51b96dd930025963fb5a3 Mon Sep 17 00:00:00 2001 From: tuoping Date: Wed, 29 Sep 2021 17:07:56 +0800 Subject: [PATCH 11/12] Update tests/test_pymatgen_molecule.py --- tests/test_pymatgen_molecule.py | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/tests/test_pymatgen_molecule.py b/tests/test_pymatgen_molecule.py index 7f4a0abc8..4a1eb0291 100644 --- a/tests/test_pymatgen_molecule.py +++ b/tests/test_pymatgen_molecule.py @@ -6,24 +6,25 @@ class TestPOSCARCart(unittest.TestCase): def setUp(self): - # self.system = dpdata.System() - # self.system.from_pymatgen_molecule(os.path.join('pymatgen', 'FA-001.xyz')) - # self.system.to("vasp/poscar", os.path.join('pymatgen', "FA-001.vasp")) - # self.assertEqual(list(self.system["atom_types"]), [0, 1, 2, 1, 1, 2, 1, 1]) - pass - - def test_to_molecule(self): - # mols = self.system.to_pymatgen_molecule() - # self.assertEqual(len(mols), 1) - pass + self.system = dpdata.System() + self.system.from_pymatgen_molecule(os.path.join('pymatgen', 'FA-001.xyz')) + self.assertEqual(list(self.system["atom_types"]), [0, 1, 2, 1, 1, 2, 1, 1]) def test_poscar_to_molecule(self): - print("------------test_poscar_to_molecule---------------------\n") tmp_system = dpdata.System() tmp_system.from_vasp_poscar(os.path.join('pymatgen', 'mol2.vasp')) + natoms = len(tmp_system['coords'][0]) + tmpcoord = tmp_system['coords'][0] + cog = np.average(tmpcoord, axis = 0) + dist = tmpcoord - np.tile(cog, [natoms, 1]) + max_dist_0 = np.max(np.linalg.norm(dist, axis = 1)) + mols = tmp_system.to("pymatgen/molecule") - struct = mols[-1].get_boxed_structure(10.0, 10.0, 10.0) - struct.to(fmt = "poscar", filename = os.path.join('pymatgen', 'mol2-new.vasp')) + cog = np.average(mols[-1].cart_coords, axis = 0) + dist = mols[-1].cart_coords - np.tile(cog, [natoms, 1]) + max_dist_1 = np.max(np.linalg.norm(dist, axis = 1)) + self.assertAlmostEqual(max_dist_0, max_dist_1) + if __name__ == '__main__': From 1e248cef82e780caeb83964bad56c6216b484dda Mon Sep 17 00:00:00 2001 From: tuoping Date: Mon, 4 Oct 2021 13:12:05 +0800 Subject: [PATCH 12/12] Moved "remove_pbc" from pymatgen/molecule.py to system.py --- dpdata/plugins/pymatgen.py | 2 +- dpdata/pymatgen/molecule.py | 15 --------------- dpdata/system.py | 17 ++++++++++++++++- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/dpdata/plugins/pymatgen.py b/dpdata/plugins/pymatgen.py index 838223f0f..f29ac382b 100644 --- a/dpdata/plugins/pymatgen.py +++ b/dpdata/plugins/pymatgen.py @@ -47,7 +47,7 @@ def to_system(self, data, **kwargs): species = [] for name, numb in zip(data['atom_names'], data['atom_numbs']): species.extend([name]*numb) - data = dpdata.pymatgen.molecule.remove_pbc(data) + data = dpdata.system.remove_pbc(data) for ii in range(np.array(data['coords']).shape[0]): molecule = Molecule( species, data['coords'][ii]) diff --git a/dpdata/pymatgen/molecule.py b/dpdata/pymatgen/molecule.py index bef3708b1..f671d6ea8 100644 --- a/dpdata/pymatgen/molecule.py +++ b/dpdata/pymatgen/molecule.py @@ -24,18 +24,3 @@ def to_system_data(file_name, protect_layer = 9) : system['coords'] = [tmpcoord] system['cells'] = [10.0 * np.eye(3)] return system - -def remove_pbc(system, protect_layer = 9): - nframes = len(system["coords"]) - natoms = len(system['coords'][0]) - for ff in range(nframes): - tmpcoord = system['coords'][ff] - cog = np.average(tmpcoord, axis = 0) - dist = tmpcoord - np.tile(cog, [natoms, 1]) - max_dist = np.max(np.linalg.norm(dist, axis = 1)) - h_cell_size = max_dist + protect_layer - cell_size = h_cell_size * 2 - shift = np.array([1,1,1]) * h_cell_size - cog - system['coords'][ff] = system['coords'][ff] + np.tile(shift, [natoms, 1]) - system['cells'][ff] = cell_size * np.eye(3) - return system diff --git a/dpdata/system.py b/dpdata/system.py index 8195c1063..011ef354f 100644 --- a/dpdata/system.py +++ b/dpdata/system.py @@ -444,7 +444,7 @@ def remove_pbc(self, protect_layer = 9): boundary """ assert(protect_layer >= 0), "the protect_layer should be no less than 0" - dpdata.pymatgen.molecule.remove_pbc(self.data, protect_layer) + remove_pbc(self.data, protect_layer) def affine_map(self, trans, f_idx = 0) : assert(np.linalg.det(trans) != 0) @@ -1291,3 +1291,18 @@ def elements_index_map(elements,standard=False,inverse=False): else: return dict(zip(elements,range(len(elements)))) # %% + +def remove_pbc(system, protect_layer = 9): + nframes = len(system["coords"]) + natoms = len(system['coords'][0]) + for ff in range(nframes): + tmpcoord = system['coords'][ff] + cog = np.average(tmpcoord, axis = 0) + dist = tmpcoord - np.tile(cog, [natoms, 1]) + max_dist = np.max(np.linalg.norm(dist, axis = 1)) + h_cell_size = max_dist + protect_layer + cell_size = h_cell_size * 2 + shift = np.array([1,1,1]) * h_cell_size - cog + system['coords'][ff] = system['coords'][ff] + np.tile(shift, [natoms, 1]) + system['cells'][ff] = cell_size * np.eye(3) + return system