Skip to content

Commit efbaae6

Browse files
authored
fix bug in reading trajs with random tid (#377)
fix #376 Co-authored-by: Sikai Yao <[email protected]>
1 parent afc02c4 commit efbaae6

File tree

3 files changed

+100
-3
lines changed

3 files changed

+100
-3
lines changed

dpdata/lammps/dump.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@ def get_atype(lines, type_idx_zero = False) :
3131
tidx = keys.index('type') - 2
3232
atype = []
3333
for ii in blk :
34-
atype.append([int(ii.split()[id_idx]), int(ii.split()[tidx])])
34+
atype.append([int(ii.split()[tidx]), int(ii.split()[id_idx])])
35+
# sort with type id
3536
atype.sort()
3637
atype = np.array(atype, dtype = int)
38+
atype = atype[:, ::-1]
3739
if type_idx_zero :
3840
return atype[:,1] - 1
3941
else :
@@ -76,16 +78,17 @@ def safe_get_posi(lines,cell,orig=np.zeros(3), unwrap=False) :
7678
assert coord_tp_and_sf is not None, 'Dump file does not contain atomic coordinates!'
7779
coordtype, sf, uw = coord_tp_and_sf
7880
id_idx = keys.index('id') - 2
81+
tidx = keys.index('type') - 2
7982
xidx = keys.index(coordtype[0])-2
8083
yidx = keys.index(coordtype[1])-2
8184
zidx = keys.index(coordtype[2])-2
8285
sel = (xidx, yidx, zidx)
8386
posis = []
8487
for ii in blk :
8588
words = ii.split()
86-
posis.append([float(words[id_idx]), float(words[xidx]), float(words[yidx]), float(words[zidx])])
89+
posis.append([float(words[tidx]), float(words[id_idx]), float(words[xidx]), float(words[yidx]), float(words[zidx])])
8790
posis.sort()
88-
posis = np.array(posis)[:,1:4]
91+
posis = np.array(posis)[:,2:5]
8992
if not sf:
9093
posis = (posis - orig) @ np.linalg.inv(cell) # Convert to scaled coordinates for unscaled coordinates
9194
if uw and unwrap:
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
ITEM: TIMESTEP
2+
0
3+
ITEM: NUMBER OF ATOMS
4+
16
5+
ITEM: BOX BOUNDS xy xz yz pp pp pp
6+
0.0000000000000000e+00 6.8043376809999998e+00 2.5385198599999999e-02
7+
0.0000000000000000e+00 6.7821075796999999e+00 1.8630761460000000e-01
8+
0.0000000000000000e+00 6.6801861338000004e+00 6.5204177000000002e-02
9+
ITEM: ATOMS id type x y z
10+
1 4 3.48873 0.0697213 6.67774
11+
2 4 3.38621 0.033338 3.34239
12+
3 4 1.79424 1.7281 5.01015
13+
4 4 3.48973 3.42896 6.67795
14+
5 4 3.40064 3.39148 3.34188
15+
6 2 6.69832 3.39136 3.34005
16+
7 3 1.80744 5.08708 5.01099
17+
8 3 5.10512 5.08007 5.01272
18+
9 3 1.70086 1.69544 1.66979
19+
10 4 5.09069 1.72876 5.00917
20+
11 4 0.119885 6.74841 3.33869
21+
12 4 4.99379 1.69262 1.67183
22+
13 4 0.199838 3.4185 6.67565
23+
14 4 1.7213 5.05235 1.66373
24+
15 4 0.21494 6.77616 6.67623
25+
16 6 5.00691 5.05 1.66532
26+
ITEM: TIMESTEP
27+
10
28+
ITEM: NUMBER OF ATOMS
29+
16
30+
ITEM: BOX BOUNDS xy xz yz pp pp pp
31+
3.0951719137647604e-02 6.7713982144168243e+00 2.5146837349522749e-02
32+
3.1535098850918430e-02 6.7499602284333751e+00 1.8455822840494820e-01
33+
3.1362715442244227e-02 6.6488234183577575e+00 6.4591924584292706e-02
34+
ITEM: ATOMS id type x y z
35+
1 6 3.52465 0.174767 6.64949
36+
2 4 3.44881 6.57204 3.4593
37+
3 3 1.85127 1.64364 4.94466
38+
4 4 3.4242 3.50712 6.53701
39+
5 4 3.46382 3.47183 3.41008
40+
6 4 6.63593 3.49936 3.46086
41+
7 4 1.85117 5.11268 4.96295
42+
8 3 5.18664 4.9445 5.05251
43+
9 3 1.70736 1.6591 1.6432
44+
10 4 5.23527 1.84126 5.03198
45+
11 4 6.59374 6.65872 3.38238
46+
12 4 5.00141 1.8369 1.76495
47+
13 4 6.64861 3.4664 6.51584
48+
14 4 1.60995 4.92415 1.66769
49+
15 4 6.62681 0.172193 6.54904
50+
16 2 4.88678 5.15478 1.589

tests/test_lammps_read_from_trajs.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import os
2+
import numpy as np
3+
import unittest
4+
from context import dpdata
5+
6+
class TestLmpReadFromTrajsWithRandomTypeId(unittest.TestCase):
7+
8+
def setUp(self):
9+
self.system = \
10+
dpdata.System(os.path.join('lammps', 'traj_with_random_type_id.dump'), fmt = 'lammps/dump', type_map = ["Ta","Nb","W","Mo","V","Al"])
11+
12+
def test_nframes (self) :
13+
atype = self.system['atom_types'].tolist()
14+
self.assertTrue(atype == [1, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 5])
15+
16+
coord = self.system['coords'].reshape([2, -1])
17+
18+
coord0_std = np.array([6.69832 , 3.39136 , 3.34005 , 1.80744 , 5.08708 , 5.01099 ,
19+
5.10512 , 5.08007 , 5.01272 , 1.70086 , 1.69544 , 1.66979 ,
20+
3.48873 , 0.0697213, 6.67774 , 3.38621 , 0.033338 , 3.34239 ,
21+
1.79424 , 1.7281 , 5.01015 , 3.48973 , 3.42896 , 6.67795 ,
22+
3.40064 , 3.39148 , 3.34188 , 5.09069 , 1.72876 , 5.00917 ,
23+
0.119885 , 6.74841 , 3.33869 , 4.99379 , 1.69262 , 1.67183 ,
24+
0.199838 , 3.4185 , 6.67565 , 1.7213 , 5.05235 , 1.66373 ,
25+
0.21494 , 6.77616 , 6.67623 , 5.00691 , 5.05 , 1.66532 ])
26+
self.assertTrue(np.allclose(coord[0, ...], coord0_std))
27+
28+
coord1_std = np.array([4.85582828e+00, 5.12324490e+00, 1.55763728e+00, 1.82031828e+00,
29+
1.61210490e+00, 4.91329728e+00, 5.15568828e+00, 4.91296490e+00,
30+
5.02114728e+00, 1.67640828e+00, 1.62756490e+00, 1.61183728e+00,
31+
3.41785828e+00, 6.54050490e+00, 3.42793728e+00, 3.39324828e+00,
32+
3.47558490e+00, 6.50564728e+00, 3.43286828e+00, 3.44029490e+00,
33+
3.37871728e+00, 6.60497828e+00, 3.46782490e+00, 3.42949728e+00,
34+
1.82021828e+00, 5.08114490e+00, 4.93158728e+00, 5.20431828e+00,
35+
1.80972490e+00, 5.00061728e+00, 6.56278828e+00, 6.62718490e+00,
36+
3.35101728e+00, 4.97045828e+00, 1.80536490e+00, 1.73358728e+00,
37+
6.61765828e+00, 3.43486490e+00, 6.48447728e+00, 1.57899828e+00,
38+
4.89261490e+00, 1.63632728e+00, 6.59585828e+00, 1.40657901e-01,
39+
6.51767728e+00, 3.30914005e+00, 7.86399766e-02, 6.66581642e-04])
40+
self.assertTrue(np.allclose(coord[1, ...], coord1_std))
41+
42+
if __name__ == '__main__':
43+
unittest.main()
44+

0 commit comments

Comments
 (0)