-
Notifications
You must be signed in to change notification settings - Fork 1
/
lmpdump.py
119 lines (85 loc) · 2.52 KB
/
lmpdump.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# %load_ext autoreload
# %autoreload 2
import pandas as pd
import numpy as np
import time as tm
class lmpdump():
def __init__(self, *args, **kwargs):
"""
args: args[0] = name of the dump file
kwargs: loadmode - all: loads all snaps from the trj
"""
self.filename = args[0]
self.finaldict = {}
MAXNUMBEROFSNAPSHOTS = 10000000
loadmode = kwargs.get('loadmode')
if loadmode == "all":
tmpdata = {}
NumberOfAtomLines = self.pickNatoms()
starttime = tm.time()
f = open(self.filename, "r")
for nsnap in range(MAXNUMBEROFSNAPSHOTS):
rvalue = self.read_snapshot(f, NumberOfAtomLines)
if rvalue == 0:
print('Done with loading %d snapshots in %.2fs.'%(
nsnap+1, tm.time() - starttime))
break
elif rvalue == 1:
continue
f.close()
def read_snapshot(self, f, n):
NofAtoms = n
DUMPHEADER = 9
floatentry = ["x", "y", "z",
"xu", "yu", "zu",
"xs", "ys", "zs",
"xsu", "ysu", "zsu",
"vx", "vy", "vz",
"fx", "fy", "fz", "c_pepa"]
intentry = ["id", "type", "mol", "c_cn"]
# Load chunks of the file
chunk_size = NofAtoms+DUMPHEADER
snap_info = [next(f, []) for x in range(chunk_size)]
# check if snap is full or empty,
# if empty means dump is done
if snap_info[1] == []:
return 0
# Get snap time
ST = int(snap_info[1].split()[0])
# Get box extremes - x, y, z
xs = np.array(snap_info[5].split()).astype(float)
ys = np.array(snap_info[6].split()).astype(float)
zs = np.array(snap_info[7].split()).astype(float)
BE = (xs[0], xs[1], xs[2], ys[0], ys[1], ys[2], zs[0], zs[1], zs[2])
# Get snap atom info
SAI = {}
Columns = snap_info[8].split()[2:]
tmpinfo = []
infolen = len(snap_info) - DUMPHEADER
[tmpinfo.append(snap_info[DUMPHEADER + i].split()) for i in range(infolen)]
floatvalues = np.array(tmpinfo)
for index, head in enumerate(Columns):
if head in floatentry:
ToAdd = floatvalues[:,index].astype(float)
elif head in intentry:
ToAdd = floatvalues[:,index].astype(int)
else:
ToAdd = floatvalues[:,index]
SAI[head] = ToAdd
AtomInfo = pd.DataFrame(SAI)
# Updates the overall dictionary
# containing all important info
self.finaldict[ST] = (BE, AtomInfo)
return 1
def pickNatoms(self):
f = open(self.filename, "r")
for index, line in enumerate(f):
if index == 3:
f.close()
return int(line)
def IndexOrder(self):
for key in self.finaldict.keys():
tmp = self.finaldict[key][1].sort_values(
by="id",
ascending=True)
self.finaldict[key] = (self.finaldict[key][0], tmp)