From b41d5957334b5c2920357884ed6f04f58ff38f3f Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Fri, 2 Dec 2022 04:24:36 -0500 Subject: [PATCH 1/2] sort by atom idx for dump (#388) Signed-off-by: Jinzhe Zeng --- dpdata/lammps/dump.py | 16 +++++++--------- tests/poscars/conf2.dump | 11 +++++++++++ tests/test_lammps_dump_idx.py | 21 +++++++++++++++++++++ tests/test_lammps_read_from_trajs.py | 1 + 4 files changed, 40 insertions(+), 9 deletions(-) create mode 100644 tests/poscars/conf2.dump create mode 100644 tests/test_lammps_dump_idx.py diff --git a/dpdata/lammps/dump.py b/dpdata/lammps/dump.py index 9de71954..135fe051 100644 --- a/dpdata/lammps/dump.py +++ b/dpdata/lammps/dump.py @@ -31,11 +31,9 @@ def get_atype(lines, type_idx_zero = False) : tidx = keys.index('type') - 2 atype = [] for ii in blk : - atype.append([int(ii.split()[tidx]), int(ii.split()[id_idx])]) - # sort with type id + atype.append([int(ii.split()[id_idx]), int(ii.split()[tidx])]) atype.sort() atype = np.array(atype, dtype = int) - atype = atype[:, ::-1] if type_idx_zero : return atype[:,1] - 1 else : @@ -78,17 +76,15 @@ def safe_get_posi(lines,cell,orig=np.zeros(3), unwrap=False) : assert coord_tp_and_sf is not None, 'Dump file does not contain atomic coordinates!' coordtype, sf, uw = coord_tp_and_sf id_idx = keys.index('id') - 2 - tidx = keys.index('type') - 2 xidx = keys.index(coordtype[0])-2 yidx = keys.index(coordtype[1])-2 zidx = keys.index(coordtype[2])-2 - sel = (xidx, yidx, zidx) posis = [] for ii in blk : words = ii.split() - posis.append([float(words[tidx]), float(words[id_idx]), float(words[xidx]), float(words[yidx]), float(words[zidx])]) + posis.append([float(words[id_idx]), float(words[xidx]), float(words[yidx]), float(words[zidx])]) posis.sort() - posis = np.array(posis)[:,2:5] + posis = np.array(posis)[:,1:4] if not sf: posis = (posis - orig) @ np.linalg.inv(cell) # Convert to scaled coordinates for unscaled coordinates if uw and unwrap: @@ -178,14 +174,16 @@ def system_data(lines, type_map = None, type_idx_zero = True, unwrap=False) : orig, cell = dumpbox2box(bounds, tilt) system['orig'] = np.array(orig) - np.array(orig) system['cells'] = [np.array(cell)] - natoms = sum(system['atom_numbs']) system['atom_types'] = get_atype(lines, type_idx_zero = type_idx_zero) system['coords'] = [safe_get_posi(lines, cell, np.array(orig), unwrap)] for ii in range(1, len(array_lines)) : bounds, tilt = get_dumpbox(array_lines[ii]) orig, cell = dumpbox2box(bounds, tilt) system['cells'].append(cell) - system['coords'].append(safe_get_posi(array_lines[ii], cell, np.array(orig), unwrap)) + atype = get_atype(array_lines[ii], type_idx_zero = type_idx_zero) + # map atom type; a[as[a][as[as[b]]]] = b[as[b][as^{-1}[b]]] = b[id] + idx = np.argsort(atype)[np.argsort(np.argsort(system['atom_types']))] + system['coords'].append(safe_get_posi(array_lines[ii], cell, np.array(orig), unwrap)[idx]) system['cells'] = np.array(system['cells']) system['coords'] = np.array(system['coords']) return system diff --git a/tests/poscars/conf2.dump b/tests/poscars/conf2.dump new file mode 100644 index 00000000..5c9970a6 --- /dev/null +++ b/tests/poscars/conf2.dump @@ -0,0 +1,11 @@ +ITEM: TIMESTEP +0 +ITEM: NUMBER OF ATOMS +2 +ITEM: BOX BOUNDS xy xz yz pp pp pp +0.0 5.0739861 1.2621856 +0.0 2.7916155 1.2874292 +0.0 2.2254033 0.7485898 +ITEM: ATOMS id type x y z +1 2 0.0 0.0 0.0 +2 1 1.2621856 0.7018028 0.5513885 \ No newline at end of file diff --git a/tests/test_lammps_dump_idx.py b/tests/test_lammps_dump_idx.py new file mode 100644 index 00000000..7f834e81 --- /dev/null +++ b/tests/test_lammps_dump_idx.py @@ -0,0 +1,21 @@ +# The index should map to that in the dump file + +import os +import numpy as np +import unittest +from context import dpdata + +class TestLmpDumpIdx(unittest.TestCase): + def setUp(self): + self.system = dpdata.System(os.path.join('poscars', 'conf2.dump')) + + def test_coords(self): + np.testing.assert_allclose(self.system['coords'], np.array( + [[[0., 0., 0.], + [1.2621856, 0.7018028, 0.5513885]]] + )) + + def test_type(self): + np.testing.assert_allclose(self.system.get_atom_types(), np.array( + [1, 0], dtype=int, + )) diff --git a/tests/test_lammps_read_from_trajs.py b/tests/test_lammps_read_from_trajs.py index 6f27ffd2..338ea1c3 100644 --- a/tests/test_lammps_read_from_trajs.py +++ b/tests/test_lammps_read_from_trajs.py @@ -10,6 +10,7 @@ def setUp(self): dpdata.System(os.path.join('lammps', 'traj_with_random_type_id.dump'), fmt = 'lammps/dump', type_map = ["Ta","Nb","W","Mo","V","Al"]) def test_nframes (self) : + self.system.sort_atom_types() atype = self.system['atom_types'].tolist() self.assertTrue(atype == [1, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 5]) From d76b098eff430623a9d9f001a97a37aad8ea1625 Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Fri, 2 Dec 2022 04:26:40 -0500 Subject: [PATCH 2/2] docs: update information about Python requirements (#386) --- README.md | 2 +- docs/index.rst | 1 + pyproject.toml | 6 +++++- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a5ad27bd..02a955be 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ **dpdata** is a python package for manipulating data formats of software in computational science, including DeePMD-kit, VASP, LAMMPS, GROMACS, Gaussian. -dpdata only works with python 3.x. +dpdata only works with python 3.7 or above. # Installation diff --git a/docs/index.rst b/docs/index.rst index e4b34f1d..6b0c5344 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -10,6 +10,7 @@ Welcome to dpdata's documentation! :maxdepth: 2 :caption: Contents: + Overview cli formats api/api diff --git a/pyproject.toml b/pyproject.toml index 87151506..ee926aae 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -12,7 +12,11 @@ authors = [ ] license = {file = "LICENSE"} classifiers = [ - "Programming Language :: Python :: 3.6", + "Programming Language :: Python :: 3.7", + "Programming Language :: Python :: 3.8", + "Programming Language :: Python :: 3.9", + "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", "License :: OSI Approved :: GNU Lesser General Public License v3 (LGPLv3)", ] dependencies = [