forked from njohner/ost_pymodules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
colvar.py
106 lines (100 loc) · 3.23 KB
/
colvar.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
#------------------------------------------------------------------------------------------------
#This file is part of the ost_pymodules project (https://github.com/njohner/ost_pymodules).
#
#Copyright 2015 Niklaus Johner
#
#ost_pymodules is free software: you can redistribute it and/or modify
#it under the terms of the GNU Lesser General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#
#ost_pymodules is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU Lesser General Public License for more details.
#
#You should have received a copy of the GNU Lesser General Public License
#along with ost_pymodules. If not, see <http://www.gnu.org/licenses/>.
#------------------------------------------------------------------------------------------------
"""
.. codeauthor:: Niklaus Johner <[email protected]>
This module is a collection of functions to work in conjunction with namd's colvar module.
It mainly contains functions to read som of the files output by namd.
"""
from ost import *
__all__=('ReadNamdSMDFile','IntegrateFloatList','ReadColvarFile')
def ReadNamdSMDFile(filename):
"""
This function opens and reads a namd ouput file
It looks for the SMD ouput and returns it in a dictionary
with keys ['timestep','cm_pos','force']
"""
file=open(filename,'r')
file.seek(0)
l=file.next()
s=l.split()
fields=['timestep','cm_pos','force']
ts=FloatList()
cm_pos=geom.Vec3List()
force=geom.Vec3List()
for l in file:
if not l.startswith('SMD '):continue
s=l.split()
ts.append(float(s[1]))
cm_pos.append(geom.Vec3(float(s[2]),float(s[3]),float(s[4])))
force.append(geom.Vec3(float(s[5]),float(s[6]),float(s[7])))
out_vecs=[ts,cm_pos,force]
out_dict={}
for f,v in zip(fields,out_vecs):
out_dict[f]=v
return out_dict
def IntegrateFloatList(x_list,y_list,lim=-1):
"""
Function to integrate a FloatList y over x
from x_list[0] to x_list[lim]
"""
if not len(x_list)==len(y_list):
print 'x and y don\'t have the same length'
return
if lim==-1:lim=len(x_list)
sum=0
for i in range(lim-1):
y=0.5*(y_list[i+1]+y_list[i])
#y=y_list[i]
dx=x_list[i+1]-x_list[i]
sum+=dx*y
return sum
def ReadColvarFile(filename,skip_first_n=1):
"""
This function opens and reads a namd colvar trajectory file
It searches for the fields in the first line of the file and then generates
a dictionary with these fields as keys
"""
file=open(filename,'r')
file.seek(0)
l=file.next()
s=l.split()
fields=[]
out_vecs=[]
#We skip the first line as it is for t=0, not in the trajectory
for i in range(skip_first_n):
file.next()
for el in s[1:]:
fields.append(el)
out_vecs.append(FloatList())
print 'fields in the colvar file:',fields
for l in file:
if l.startswith('#'):continue
s=l.split()
v_list=[]
for el in s:
try:
v_list.append(float(el))
except:
print 'error with line',l
for i,el in enumerate(v_list):
out_vecs[i].append(el)
out_dict={}
for f,v in zip(fields,out_vecs):
out_dict[f]=v
return out_dict