From 8805846cc384161ce79c6851024f500336084cb7 Mon Sep 17 00:00:00 2001 From: xfanak <41663661+xfanak@users.noreply.github.com> Date: Mon, 26 Jul 2021 15:10:34 +0800 Subject: [PATCH 1/9] Update dump.py --- dpdata/lammps/dump.py | 68 ++++++++++++++++++++----------------------- 1 file changed, 32 insertions(+), 36 deletions(-) diff --git a/dpdata/lammps/dump.py b/dpdata/lammps/dump.py index 74fdb6530..2d2367a03 100644 --- a/dpdata/lammps/dump.py +++ b/dpdata/lammps/dump.py @@ -51,45 +51,41 @@ def get_natoms_vec(lines) : assert (sum(natoms_vec) == get_natoms(lines)) return natoms_vec -def get_posi(lines) : +def get_coordtype_and_scalefactor(keys): + # 4 types in total,with different scaling factor + key_pc=['x','y','z'] # plain cartesian, sf = 1 + key_uc=['xu','yu','zu'] # unwraped cartesian, sf = 1 + key_s=['xs','ys','zs'] # scaled by lattice parameter, sf = lattice parameter + key_su = ['xsu','ysu','zsu'] #scaled and unfolded,sf = lattice parameter + lmp_coor_type = [key_pc,key_uc,key_s,key_su] + sf = [0,0,1,1] + for k in range(4): + if all(i in keys for i in lmp_coor_type[k]): + return lmp_coor_type[k],sf[k] + +def safe_get_posi(lines,cell,orig=np.zeros(3)) : blk, head = _get_block(lines, 'ATOMS') keys = head.split() + coord_tp_and_sf = get_coordtype_and_scalefactor(keys) + assert coord_tp_and_sf is not None, 'Dump file does not contain atomic coordinates!' + coordtype, sf = coord_tp_and_sf id_idx = keys.index('id') - 2 - xidx = keys.index('x') - 2 - yidx = keys.index('y') - 2 - zidx = keys.index('z') - 2 + xidx = keys.index(coordtype[0])-2 + yidx = keys.index(coordtype[1])-2 + zidx = keys.index(coordtype[2])-2 sel = (xidx, yidx, zidx) posis = [] + lp = np.linalg.norm(cell,axis=1) for ii in blk : words = ii.split() posis.append([float(words[id_idx]), float(words[xidx]), float(words[yidx]), float(words[zidx])]) posis.sort() - posis = np.array(posis) - return posis[:,1:4] - -def get_posi_frac(lines) : - blk, head = _get_block(lines, 'ATOMS') - keys = head.split() - id_idx = keys.index('id') - 2 - xidx = keys.index('xs') - 2 - yidx = keys.index('ys') - 2 - zidx = keys.index('zs') - 2 - sel = (xidx, yidx, zidx) - posis = [] - for ii in blk : - words = ii.split() - posis.append([float(words[id_idx]), float(words[xidx]), float(words[yidx]), float(words[zidx])]) - posis.sort() - posis = np.array(posis) - return posis[:,1:4] - -def safe_get_posi(lines, cell, orig = np.zeros(3)): - try: - posis = get_posi(lines) - orig - except ValueError: - fposis = get_posi_frac(lines) - posis = fposis @ cell - return posis + posis = np.array(posis)[:,1:4] + if sf: + posis = posis@cell + else: + posis = posis - orig + return posis%lp def get_dumpbox(lines) : blk, h = _get_block(lines, 'BOX BOUNDS') @@ -211,8 +207,6 @@ def split_traj(dump_lines) : # # print(get_natomtypes(lines)) # # print(get_natoms_vec(lines)) # posi = get_posi(lines) - # dbox, tilt = get_dumpbox(lines) - # orig, box = dumpbox2box(dbox, tilt) # dbox1, tilt1 = box2dumpbox(orig, box) # print(dbox - dbox1) # print(tilt - tilt1) @@ -220,7 +214,9 @@ def split_traj(dump_lines) : # print(box) # np.savetxt('tmp.out', posi - orig, fmt='%.6f') # print(system_data(lines)) - - lines = load_file('conf.5.dump', begin = 0, step = 2) - with open('tmp.out', 'w') as fp: - fp.write('\n'.join(lines)) + lines = load_file('conf_unfold.dump', begin = 0, step = 1) + al = split_traj(lines) + s = system_data(lines,['O','H']) + #l = np.linalg.norm(s['cells'][1],axis=1) + #p = s['coords'][0] + l + #np.savetxt('p',p,fmt='%1.10f') From c0a048e25bdb5e5a015584d7444be9a07b2320ed Mon Sep 17 00:00:00 2001 From: xfanak <41663661+xfanak@users.noreply.github.com> Date: Mon, 26 Jul 2021 15:12:09 +0800 Subject: [PATCH 2/9] Create test_lammps_dump_unfold.py --- tests/test_lammps_dump_unfold.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 tests/test_lammps_dump_unfold.py diff --git a/tests/test_lammps_dump_unfold.py b/tests/test_lammps_dump_unfold.py new file mode 100644 index 000000000..4ffd5f730 --- /dev/null +++ b/tests/test_lammps_dump_unfold.py @@ -0,0 +1,26 @@ +import os +import numpy as np +import unittest +from context import dpdata +from poscars.poscar_ref_oh import TestPOSCARoh + +class TestDump(unittest.TestCase, TestPOSCARoh): + + def setUp(self): + self.system = dpdata.System(os.path.join('poscars', 'conf_unfold.dump'), + type_map = ['O', 'H']) + +class TestDump2(unittest.TestCase, TestPOSCARoh): + + def setUp(self): + self.tmp_system = dpdata.System(os.path.join('poscars', 'conf_unfold.dump'), + type_map = ['O', 'H']) + self.system = self.tmp_system.sub_system([1]) + + def test_nframes (self) : + self.assertEqual(self.tmp_system.get_nframes(), 2) + + +if __name__ == '__main__': + unittest.main() + From e763bfced778f58be8b5494fc4ad99998faf86bc Mon Sep 17 00:00:00 2001 From: xfanak <41663661+xfanak@users.noreply.github.com> Date: Mon, 26 Jul 2021 15:12:54 +0800 Subject: [PATCH 3/9] Create conf_unfold.dump --- tests/poscars/conf_unfold.dump | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 tests/poscars/conf_unfold.dump diff --git a/tests/poscars/conf_unfold.dump b/tests/poscars/conf_unfold.dump new file mode 100644 index 000000000..f41c4870a --- /dev/null +++ b/tests/poscars/conf_unfold.dump @@ -0,0 +1,22 @@ +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 1 0.0 0.0 0.0 +2 2 1.2621856 0.7018028 0.5513885 +ITEM: TIMESTEP +1 +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 +1.0 3.2254033 0.7485898 +ITEM: ATOMS id type xu yu zu +1 1 2.524371299999999874e+00 2.401471736187592576e+00 3.677737941122612764e+00 +2 2 3.786556899999999892e+00 3.103274536187592414e+00 4.229126441122612601e+00 From f5e2b66188a2a6e8d57b859c709b8a624a985e22 Mon Sep 17 00:00:00 2001 From: xfanak <41663661+xfanak@users.noreply.github.com> Date: Tue, 27 Jul 2021 20:26:52 +0800 Subject: [PATCH 4/9] Update dump.py --- dpdata/lammps/dump.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/dpdata/lammps/dump.py b/dpdata/lammps/dump.py index 2d2367a03..9b160ccb6 100644 --- a/dpdata/lammps/dump.py +++ b/dpdata/lammps/dump.py @@ -75,17 +75,16 @@ def safe_get_posi(lines,cell,orig=np.zeros(3)) : zidx = keys.index(coordtype[2])-2 sel = (xidx, yidx, zidx) posis = [] - lp = np.linalg.norm(cell,axis=1) for ii in blk : words = ii.split() posis.append([float(words[id_idx]), float(words[xidx]), float(words[yidx]), float(words[zidx])]) posis.sort() posis = np.array(posis)[:,1:4] if sf: - posis = posis@cell + posis = (posis%1.)@cell # convert xsu to xs first else: - posis = posis - orig - return posis%lp + posis = (posis - orig)%np.linalg.norm(cell,axis=1) + return posis def get_dumpbox(lines) : blk, h = _get_block(lines, 'BOX BOUNDS') From b96a291c1519c43ec166a29625c94966a8d883a1 Mon Sep 17 00:00:00 2001 From: xfanak <41663661+xfanak@users.noreply.github.com> Date: Tue, 27 Jul 2021 20:28:12 +0800 Subject: [PATCH 5/9] Create conf_s_su.dump --- tests/poscars/conf_s_su.dump | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 tests/poscars/conf_s_su.dump diff --git a/tests/poscars/conf_s_su.dump b/tests/poscars/conf_s_su.dump new file mode 100644 index 000000000..8cc97b775 --- /dev/null +++ b/tests/poscars/conf_s_su.dump @@ -0,0 +1,22 @@ +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 xs ys zs +1 1 0.0 0.0 0.0 +2 2 0.2472745002 0.2527254533 0.2477701458 +ITEM: TIMESTEP +1 +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 xsu ysu zsu +1 1 0.0 0.0 0.0 +2 2 1.2472745002 1.2527254533 1.2477701458 From 90dbbd1810bda4446409c91c874301dd28e3fe00 Mon Sep 17 00:00:00 2001 From: xfanak <41663661+xfanak@users.noreply.github.com> Date: Tue, 27 Jul 2021 20:28:56 +0800 Subject: [PATCH 6/9] Create test_lammps_dump_s_su.py --- tests/poscars/test_lammps_dump_s_su.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 tests/poscars/test_lammps_dump_s_su.py diff --git a/tests/poscars/test_lammps_dump_s_su.py b/tests/poscars/test_lammps_dump_s_su.py new file mode 100644 index 000000000..2370cffc4 --- /dev/null +++ b/tests/poscars/test_lammps_dump_s_su.py @@ -0,0 +1,26 @@ +import os +import numpy as np +import unittest +from context import dpdata +from poscars.poscar_ref_oh import TestPOSCARoh + +class TestDump(unittest.TestCase, TestPOSCARoh): + + def setUp(self): + self.system = dpdata.System(os.path.join('poscars', 'conf_s_su.dump'), + type_map = ['O', 'H']) + +class TestDump2(unittest.TestCase, TestPOSCARoh): + + def setUp(self): + self.tmp_system = dpdata.System(os.path.join('poscars', 'conf_s_su.dump'), + type_map = ['O', 'H']) + self.system = self.tmp_system.sub_system([1]) + + def test_nframes (self) : + self.assertEqual(self.tmp_system.get_nframes(), 2) + + +if __name__ == '__main__': + unittest.main() + From 2a4172eff50966dd9183b9a3347b93f8a0f849c3 Mon Sep 17 00:00:00 2001 From: xfanak <41663661+xfanak@users.noreply.github.com> Date: Fri, 30 Jul 2021 10:02:18 +0800 Subject: [PATCH 7/9] Update dump.py --- dpdata/lammps/dump.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dpdata/lammps/dump.py b/dpdata/lammps/dump.py index 9b160ccb6..d75c8661a 100644 --- a/dpdata/lammps/dump.py +++ b/dpdata/lammps/dump.py @@ -83,7 +83,7 @@ def safe_get_posi(lines,cell,orig=np.zeros(3)) : if sf: posis = (posis%1.)@cell # convert xsu to xs first else: - posis = (posis - orig)%np.linalg.norm(cell,axis=1) + posis = (posis - orig)%cell.sum(axis=0) return posis def get_dumpbox(lines) : From bde97b166b66a3572d6c4e1ceeb0feafa0e16d8f Mon Sep 17 00:00:00 2001 From: xfanak <41663661+xfanak@users.noreply.github.com> Date: Fri, 30 Jul 2021 10:05:21 +0800 Subject: [PATCH 8/9] Update conf_unfold.dump --- tests/poscars/conf_unfold.dump | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/poscars/conf_unfold.dump b/tests/poscars/conf_unfold.dump index f41c4870a..3d46a9f44 100644 --- a/tests/poscars/conf_unfold.dump +++ b/tests/poscars/conf_unfold.dump @@ -6,11 +6,11 @@ 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 1 0.0 0.0 0.0 -2 2 1.2621856 0.7018028 0.5513885 +ITEM: ATOMS id type xu yu zu +1 1 5.073986099999999944e+00 2.791615499999999805e+00 2.225403299999999973e+00 +2 2 6.336171700000000406e+00 3.493418300000000087e+00 2.776791800099999818e+00 ITEM: TIMESTEP -1 +0 ITEM: NUMBER OF ATOMS 2 ITEM: BOX BOUNDS xy xz yz pp pp pp @@ -18,5 +18,5 @@ ITEM: BOX BOUNDS xy xz yz pp pp pp 0.0 2.7916155 1.2874292 1.0 3.2254033 0.7485898 ITEM: ATOMS id type xu yu zu -1 1 2.524371299999999874e+00 2.401471736187592576e+00 3.677737941122612764e+00 -2 2 3.786556899999999892e+00 3.103274536187592414e+00 4.229126441122612601e+00 +1 1 5.073986099999999944e+00 2.791615499999999805e+00 3.225403299999999973e+00 +2 2 6.336171700000000406e+00 3.493418300000000087e+00 3.776791800099999818e+00 From fc618b24098bd71e38edbae81334244bc6e60297 Mon Sep 17 00:00:00 2001 From: xfanak <41663661+xfanak@users.noreply.github.com> Date: Tue, 3 Aug 2021 20:27:12 +0800 Subject: [PATCH 9/9] Update dump.py --- dpdata/lammps/dump.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/dpdata/lammps/dump.py b/dpdata/lammps/dump.py index d75c8661a..7136d3e63 100644 --- a/dpdata/lammps/dump.py +++ b/dpdata/lammps/dump.py @@ -80,11 +80,9 @@ def safe_get_posi(lines,cell,orig=np.zeros(3)) : posis.append([float(words[id_idx]), float(words[xidx]), float(words[yidx]), float(words[zidx])]) posis.sort() posis = np.array(posis)[:,1:4] - if sf: - posis = (posis%1.)@cell # convert xsu to xs first - else: - posis = (posis - orig)%cell.sum(axis=0) - return posis + if not sf: + posis = (posis-orig)@np.linalg.inv(cell)# Convert to scaled coordinates for unscaled coordinates + return (posis%1)@cell # Convert scaled coordinates back to Cartesien coordinates def get_dumpbox(lines) : blk, h = _get_block(lines, 'BOX BOUNDS')