forked from jautschbach/dynpy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparseMD.py
More file actions
executable file
·329 lines (297 loc) · 16 KB
/
parseMD.py
File metadata and controls
executable file
·329 lines (297 loc) · 16 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
import pandas as pd
import numpy as np
import scipy as sp
import os
import itertools
import sys
import signal
from universe import Universe, Atom, Frame, Molecule, compute_frame_from_atom
def PARSE_MD(PD,traj):
if 'parse_vel' not in PD.__dict__.keys():
PD.parse_vel = False
if 'sample_freq' not in PD.__dict__.keys():
PD.sample_freq = 1
if PD.MD_format == "QE":
if 'symbols' not in PD.__dict__.keys():
print("Error: Missing required input variable symbols in class ParseDynamics for parsing QE.")
sys.exit(2)
#for i,traj in enumerate(PD.trajs):
u,vel = parse_qe_md(traj_dir=traj, symbols=PD.symbols, sample_freq=PD.sample_freq, celldm=PD.celldm, start_prod=PD.start_prod, end_prod=PD.end_prod,parse_vel=PD.parse_vel)
#us[i] = u
#vels[i] = vel
#elif PD.MD_format == "CP2K":
# try:
# md_print_freq = PD.md_print_freq
# except AttributeError:
# print("Missing required input variable md_print_freq in class ParseDynamics for parsing CP2K. See dynpy_params.py")
# sys.exit(2)
#for i,traj in enumerate(PD.trajs):
#u,vel = parse_cp2k_md(traj, PD.sample_freq, md_print_freq, start_prod, end_prod)
#us[i] = u
#vels[i] = vel
elif PD.MD_format == "Tinker":
#print("Tinker")
#us = {}
#vels = {}
#for i,traj in enumerate(PD.trajs):
traj_dir = PD.traj_dir+traj
u,vel = parse_tinker_md(traj_dir,sample_freq=PD.sample_freq, md_print_freq=PD.md_print_freq, nat=PD.nat, start_prod=PD.start_prod, end_prod=PD.end_prod, parse_vel=PD.parse_vel)
#us[i] = u
#vels[i] = vel
#vel.to_csv("./vel.csv")
#if parse_vel:
# vel = parse_tinker_vel(traj_dir,PD.sample_freq, md_print_freq, nat, start_prod, end_prod)
#elif PD.MD_ENGINE == "prepared":
# u, vel = _prepared(pd.read_csv(traj_dir+"methane-01-atom-table.csv")
elif PD.MD_format == "xyz":
#us = {}
#vels = {}
#print(PD.traj_dir+PD.trajs[0],PD.sample_freq, md_print_freq, nat, start_prod, end_prod)
#for i,traj in enumerate(PD.trajs):
traj_dir = PD.traj_dir+traj
u,vel = parse_xyz(traj_dir, sample_freq=PD.sample_freq, md_print_freq=PD.md_print_freq, nat=PD.nat, start_prod=PD.start_prod, end_prod=PD.end_prod,parse_vel=PD.parse_vel)
#us[i] = u
#vels[i] = vel
else:
print("MD_format not provided or not recognized. Implemented formats are 'QE', 'Tinker', and 'xyz'. Do you need to parse MD trajectories?")
sys.exit(2)
#print(us[0].atom)
return u,vel
def parse_qe_md(traj_dir,symbols,sample_freq,celldm,start_prod,end_prod,parse_vel=False):
try:
pos = list(filter(lambda x: ".pos" in x, os.listdir(traj_dir)))[0]
except:
print("Did not find .pos trajectory file at " + traj_dir + " for parsing QE dynamics. Is this what you wanted?")
sys.exit(2)
#if start_prod == None:
# start_prod = atom['frame'].iloc[0]
#if end_prod == None:
# end_prod = atom['frame'].iloc[-1]
nat = len(symbols)
cols = ['x','y','z']
#read from .xyz and eliminate comment lines
atom = pd.read_csv(traj_dir+'/'+pos, sep=r'\s+', usecols=[0,1,2],names=cols,header=None,na_filter=False,skiprows=lambda x: (x<=(start_prod-1)*(nat+1)) | (x%(nat+1)==0) | ((x//(nat+1)-start_prod+1)%sample_freq!=0),dtype={'symbol':str,'x':str,'y':str,'z':str},nrows=nat*((end_prod-start_prod)+sample_freq)/sample_freq)
with open(traj_dir+'/'+pos, 'r') as f:
lines = f.readlines()
frames = np.empty((end_prod-start_prod+sample_freq)//sample_freq)
f = 0
for i,line in enumerate(lines):
if ((i>=(start_prod-1)*(nat+1)) & (i%(nat+1)==0) & (i<=(end_prod)*(nat+1)) & ((i//(nat+1)-start_prod+1)%sample_freq==0)):
#print(i)
#print(line.split())
frames[f] = line.split()[0]
f += 1
fframes = [[frame]*nat for frame in frames]
full_frames = list(itertools.chain.from_iterable(fframes))
atom.loc[:,'symbol']=symbols*(len(atom)//nat)
atom.loc[:,'frame']= full_frames #atom.index//nat
#print(atom.tail())
atom.loc[:,'x'] = atom.loc[:,'x']
atom.loc[:,'y'] = atom.loc[:,'y']
atom.loc[:,'z'] = atom.loc[:,'z']
#nat_frames = [nat]*len(atom.frame.unique())
#frame = pd.DataFrame(nat_frames,index = atom.frame.unique(),columns=['atom_count'])
u = Universe(Atom(atom))
#u.atom = atom
u.atom.loc[:,'label'] = u.atom.get_atom_labels()
u.atom.loc[:,'label'] = u.atom['label'].astype(int)
u.atom.drop_duplicates(['frame','label'], keep='last',inplace=True)
# Add the unit cell dimensions to the frame table of the universe
u.frame = compute_frame_from_atom(u.atom)
u.frame.add_cell_dm(celldm = celldm)
u.compute_unit_atom()
#for i, q in enumerate(("x", "y", "z")):
# for j, r in enumerate(("i", "j", "k")):
# if i == j:
# frame[q+r] = celldm
# else:
# frame[q+r] = 0.0
# frame["o"+q] = 0.0
#frame['periodic'] = True
#u.atom.loc[:,['x','y','z']] = u.atom[['x','y','z']].astype(float)
#print(u.atom.tail())
vel=pd.DataFrame()
if parse_vel:
vel_file = list(filter(lambda x: ".vel" in x, os.listdir(traj_dir)))[0]
cols = ['x','y','z']
#read from .vel and eliminate comment lines
velatom = pd.read_csv(traj_dir+'/'+vel_file, sep=r'\s+', usecols=[1,2,3],names=cols,header=None,na_filter=False,skiprows=lambda x: (x<=(start_prod-1)*(nat+1)) | (x%(nat+1)==0) | ((x//(nat+1)-start_prod+1)%sample_freq!=0),dtype={'symbol':str,'x':str,'y':str,'z':str},nrows=nat*((end_prod-start_prod)+sample_freq)/sample_freq)
velatom.loc[:,'symbol']=symbols
velatom.loc[:,'frame']=full_frames
velatom.loc[:,'x'] = velatom.loc[:,'x']
velatom.loc[:,'y'] = velatom.loc[:,'y']
velatom.loc[:,'z'] = velatom.loc[:,'z']
velu = Universe(Atom(velatom))
#velu.atom = velatom
velu.atom.loc[:,'label'] = velu.atom.get_atom_labels()
velu.atom.loc[:,'label'] = velu.atom['label'].astype(int)
velu.atom.drop_duplicates(['frame','label'], keep='last',inplace=True)
vel = velu.atom
#vel.to_csv("vel.csv")
#print(u.atom.tail())
#print(vel.tail())
return u, vel
# def parse_cp2k_md(traj_dir, sample_freq, md_print_freq, start_prod=None, end_prod=None, parse_vel=False):
# print("Reading trajectory output from CP2K...")
# pos = list(filter(lambda x: "pos" in x, os.listdir(traj_dir)))[0]
# xyz = XYZ.from_file(traj_dir+'/'+pos)
# u = XYZ.to_universe(xyz)
# u.atom['label'] = u.atom.get_atom_labels()
# u.atom.drop_duplicates(['frame','label'], keep='last',inplace=True)
# u.atom.loc[:,'frame'] = u.atom['frame'].astype(int)
# u.atom.loc[:,'frame'] = u.atom['frame']*md_print_freq
# if start_prod == None:
# start_prod = u.atom['frame'].iloc[0]
# if end_prod == None:
# end_prod = u.atom['frame'].iloc[-1]
# u.atom = u.atom[(u.atom['frame'] >= start_prod) & (u.atom['frame'] <= end_prod) & ((u.atom['frame']-start_prod)%sample_freq==0)]
# vel=None
# if parse_vel:
# vel_file = list(filter(lambda x: "vel" in x, os.listdir(traj_dir)))[0]
# velxyz = exatomic.XYZ.from_file(traj_dir+'/'+vel_file)
# velu = exatomic.XYZ.to_universe(velxyz)
# velu.atom['label'] = velu.atom.get_atom_labels()
# velu.atom.drop_duplicates(['frame','label'], keep='last',inplace=True)
# velu.atom.loc[:,'frame'] = velu.atom['frame'].astype(int)
# velu.atom.loc[:,'frame'] = velu.atom['frame']*md_print_freq
# velu.atom = velu.atom[(velu.atom['frame'] >= start_prod) & (velu.atom['frame'] <= end_prod) & ((velu.atom['frame']-start_prod)%sample_freq==0)]
# return u,velu.atom
def parse_tinker_md(traj_dir, sample_freq, md_print_freq, nat, start_prod, end_prod, parse_vel=False):
try:
arc = list(filter(lambda x: "arc" in x, os.listdir(traj_dir)))[0]
except:
print("Did not find .arc trajectory file at " + traj_dir + " for parsing Tinker dynamics. Is this what you wanted?")
sys.exit(2)
print("reading "+ arc)
cols = ['symbol','x','y','z']
#read from .arc and eliminate comment lines
atom = pd.read_csv(traj_dir+'/'+arc, sep=r'\s+', usecols=[1,2,3,4],names=cols,header=None,na_filter=False,skiprows=lambda x: (x<=(start_prod-1)*(nat+2)) | (x%(nat+2)==0) | (x%(nat+2)==1) | ((x//(nat+2)-start_prod+1)%sample_freq!=0),dtype={'symbol':str,'x':str,'y':str,'z':str},nrows=nat*((end_prod-start_prod)+sample_freq)/sample_freq)
atom.loc[:,'symbol']=atom.loc[:,'symbol'].apply(normsym)
#print(atom)
frames = np.arange(start_prod,end_prod+sample_freq,sample_freq)
frames *= md_print_freq
#print(frames)
fframes = [[frame]*nat for frame in frames]
full_frames = list(itertools.chain.from_iterable(fframes))
atom.loc[:,'frame']=full_frames
#print(atom.head())
atom.loc[:,'x'] = atom.loc[:,'x'].apply(d_to_e)/0.529177
atom.loc[:,'y'] = atom.loc[:,'y'].apply(d_to_e)/0.529177
atom.loc[:,'z'] = atom.loc[:,'z'].apply(d_to_e)/0.529177
u = Universe(Atom(atom))
#u.atom = atom
u.atom.loc[:,'label'] = u.atom.get_atom_labels()
u.atom.loc[:,'label'] = u.atom['label'].astype(int)
u.atom.drop_duplicates(['frame','label'], keep='last',inplace=True)
#u.atom.loc[:,['x','y','z']] = u.atom[['x','y','z']].astype(float)
#print(u.atom.head())
vel=pd.DataFrame()
if parse_vel:
vel_file = list(filter(lambda x: ".vel" in x, os.listdir(traj_dir)))[0]
#print(vel_file)
cols = ['symbol','x','y','z']
#read from .vel and eliminate comment lines
velatom = pd.read_csv(traj_dir+'/'+vel_file, sep=r'\s+', usecols=[1,2,3,4],names=cols,header=None,na_filter=False,skiprows=lambda x: (x<=(start_prod-1)*(nat+2)) | (x%(nat+2)==0) | (x%(nat+2)==1) | ((x//(nat+2)-start_prod+1)%sample_freq!=0),dtype={'symbol':str,'x':str,'y':str,'z':str},nrows=nat*((end_prod-start_prod)+sample_freq)/sample_freq)
#print(velatom.head())
velatom.loc[:,'symbol']=velatom.loc[:,'symbol'].apply(normsym)
velatom.loc[:,'frame']=full_frames
velatom.loc[:,'x'] = velatom.loc[:,'x'].apply(d_to_e)/0.529177
velatom.loc[:,'y'] = velatom.loc[:,'y'].apply(d_to_e)/0.529177
velatom.loc[:,'z'] = velatom.loc[:,'z'].apply(d_to_e)/0.529177
velu = Universe(Atom(velatom))
#velu.atom = velatom
velu.atom.loc[:,'label'] = velu.atom.get_atom_labels()
velu.atom.loc[:,'label'] = velu.atom['label'].astype(int)
velu.atom.drop_duplicates(['frame','label'], keep='last',inplace=True)
vel = velu.atom
#vel.to_csv("vel.csv")
return u, vel
def parse_xyz(traj_dir, sample_freq, md_print_freq, nat, start_prod=None, end_prod=None, parse_vel=True):
try:
xyz = list(filter(lambda x: ".xyz" in x, os.listdir(traj_dir)))[0]
except:
print("Did not find .xyz trajectory file at " + traj_dir + " for parsing dynamics. Is this what you wanted?")
sys.exit(2)
cols = ['symbol','x','y','z']
#read from .xyz and eliminate comment lines
atom = pd.read_csv(traj_dir+'/'+xyz, sep=r'\s+', usecols=[0,1,2,3],names=cols,header=None,na_filter=False,skiprows=lambda x: (x<=(start_prod-1)*(nat+2)) | (x%(nat+2)==0) | (x%(nat+2)==1) | ((x//(nat+2)-start_prod+1)%sample_freq!=0),dtype={'symbol':str,'x':str,'y':str,'z':str},nrows=nat*((end_prod-start_prod)+sample_freq)/sample_freq)
atom.loc[:,'symbol']=atom.loc[:,'symbol'].apply(normsym)
frames = np.arange(start_prod,end_prod+sample_freq,sample_freq)
frames *= md_print_freq
fframes = [[frame]*nat for frame in frames]
full_frames = list(itertools.chain.from_iterable(fframes))
atom.loc[:,'frame']=full_frames
#print(atom.tail())
atom.loc[:,'x'] = atom.loc[:,'x'].apply(d_to_e)/0.529177
atom.loc[:,'y'] = atom.loc[:,'y'].apply(d_to_e)/0.529177
atom.loc[:,'z'] = atom.loc[:,'z'].apply(d_to_e)/0.529177
u = Universe(Atom(atom))
#u.atom = atom
u.atom.loc[:,'label'] = u.atom.get_atom_labels()
u.atom.loc[:,'label'] = u.atom['label'].astype(int)
u.atom.drop_duplicates(['frame','label'], keep='last',inplace=True)
#u.atom.loc[:,['x','y','z']] = u.atom[['x','y','z']].astype(float)
#print(u.atom.tail())
vel=pd.DataFrame()
if parse_vel:
vel_file = list(filter(lambda x: ".vel" in x, os.listdir(traj_dir)))[0]
cols = ['symbol','x','y','z']
#read from .vel and eliminate comment lines
velatom = pd.read_csv(traj_dir+'/'+vel_file, sep=r'\s+', usecols=[0,1,2,3],names=cols,header=None,na_filter=False,skiprows=lambda x: (x<=(start_prod-1)*(nat+2)) | (x%(nat+2)==0) | (x%(nat+2)==1) | ((x//(nat+2)-start_prod+1)%sample_freq!=0),dtype={'symbol':str,'x':str,'y':str,'z':str},nrows=nat*((end_prod-start_prod)+sample_freq)/sample_freq)
velatom.loc[:,'symbol']=velatom.loc[:,'symbol'].apply(normsym)
velatom.loc[:,'frame']=full_frames
velatom.loc[:,'x'] = velatom.loc[:,'x'].apply(d_to_e)/0.529177
velatom.loc[:,'y'] = velatom.loc[:,'y'].apply(d_to_e)/0.529177
velatom.loc[:,'z'] = velatom.loc[:,'z'].apply(d_to_e)/0.529177
velu = Universe(Atom(velatom))
#velu.atom = velatom
velu.atom.loc[:,'label'] = velu.atom.get_atom_labels()
velu.atom.loc[:,'label'] = velu.atom['label'].astype(int)
velu.atom.drop_duplicates(['frame','label'], keep='last',inplace=True)
vel = velu.atom
#vel.to_csv("vel.csv")
#print(u.atom.tail())
#print(vel.tail())
return u, vel
def _prepared(atom_data,timestep,start_prod,end_prod,celldm,units='Angstrom'):
u = Universe(atom_data)
if units=='Angstrom':
u.atom.loc[:,'x'] = u.atom.loc[:,'x']/0.529177
u.atom.loc[:,'y'] = u.atom.loc[:,'y']/0.529177
u.atom.loc[:,'z'] = u.atom.loc[:,'z']/0.529177
u.atom.loc[:,'label'] = u.atom.get_atom_labels()
u.atom.loc[:,'label'] = u.atom['label'].astype(int)
u.atom.loc[:,'frame'] = u.atom['frame'].astype(int)
#u.atom = u.atom[(u.atom['frame'] >= start_prod) & (u.atom['frame'] <= end_prod)]
vel = pd.DataFrame()
#vel.loc[:,['x','y','z']] = u.atom.groupby('label',group_keys=False)[['x','y','z']].apply(pd.DataFrame.diff)
#vel.loc[:,['x','y','z']] = vel.loc[:,['x','y','z']]/(u.atom.frame.diff().unique()[-1]*timestep)
#vel = vel.dropna(how='any')
# Add the unit cell dimensions to the frame table of the universe
u.frame = compute_frame_from_atom(u.atom)
u.frame.add_cell_dm(celldm)
u.compute_unit_atom()
#u.compute_atom_two(vector=True,bond_extra=0.9)
return u, vel
# def parse_tinker_vel(traj_dir,nat,start,end,mod):
# vel_dat = list(filter(lambda x: "vel" in x, os.listdir(traj_dir)))[0]
# cols = ['symbol','x','y','z']
# #read from .arc and eliminate comment lines
# vel = pd.read_csv(traj_dir+vel_dat,delim_whitespace=True,usecols=[1,2,3,4],names=cols,header=None,na_filter=False,skiprows=lambda x: (x<(start-1)*(nat+1)) | (x%(nat+1)==0) | ((x//(nat+1)-start+1)%mod!=0) ,converters={'x':d_to_e,'y':d_to_e,'z':d_to_e},dtype={'symbol':'category'},nrows=nat*((end-start+1)//mod+1))
# vel.loc[:,'symbol']=vel.loc[:,'symbol'].apply(normsym)
# vel.loc[:,'frame']=vel.index//nat
# vel.loc[:,'x'] = vel.loc[:,'x']/0.529177
# vel.loc[:,'y'] = vel.loc[:,'y']/0.529177
# vel.loc[:,'z'] = vel.loc[:,'z']/0.529177
# return vel
def normsym(sym):
sym=str(sym)
if len(sym) > 1:
if((sym[1].isupper()) | sym[1].isnumeric()):
sym = sym[0]
return sym
def d_to_e(val):
if 'D' in val:
return float(val.replace('D', 'E'))
else:
return float(val)