-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread_colvar.py
More file actions
238 lines (223 loc) · 6.21 KB
/
Copy pathread_colvar.py
File metadata and controls
238 lines (223 loc) · 6.21 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
import numpy as np
import matplotlib.pyplot as plt
import math
#Python scripts to read,plot 2D_umbrella_sampling result.
#Add WHAM-2d read and plot.
def ReadColvar_Coord(f):
'''
Parameters: f: Input file head
Read the x,y coordinates directly from plumed ouput dat file.
#! FIELDS time p1.sss p1.zzz restraint-phi.bias restraint-psi.bias
0.00 1.11 0.001 1.105 0.0002
Returns: Coord: array_like coordinates (x,y) pairs
x: array_like x coordinates
y: array_like y coordinates
'''
print(f)
## Skip first row.
next(f)
coord = []
x = []
y = []
for line in f:
temp1 = float(line.split()[1])
temp2 = float(line.split()[2])
x.append(temp1)
y.append(temp2)
coord.append([temp1,temp2])
#y.append(float(temp))
return coord,x,y
def ReadColvar_Bias(f):
'''
Parameters:
f: Input file head
Read the bias potential directly from plumed ouput dat file.
#! FIELDS time p1.sss p1.zzz restraint-phi.bias restraint-psi.bias
0.00 1.11 0.001 1.105 0.0002
Returns:
Bias: array_like bias_energy (phi_bias,psi_bias) pairs
phi_bias: array_like x coordinates
psi_bias: array_like y coordinates
'''
print(f)
## Skip first row.
next(f)
Bias = []
phi_bias = []
psi_bias = []
for line in f:
temp3 = float(line.split()[3])
temp4 = float(line.split()[4])
phi_bias.append(temp3)
psi_bias.append(temp4)
Bias.append([temp3,temp4])
#y.append(float(temp))
return Bias,phi_bias,psi_bias
def ReadWHAM(f):
'''
Parameters:
f: Input file head
Read free energy and coordinates from WHAM output
#X Y Free Pro
0.00 1.11 0.001 0.00
Returns:
Coord: array_like coordinates (x,y,Free) pairs
Free: array_like Free energy
x: array_like x coordinates
y: array_like y coordinates
'''
print(f)
## Skip first row
next(f)
coord = []
#x = []
#y = []
Free = []
## Line counter: WHAM output skip 1 line after 50 lines
Line_counter = 0
for line in f:
if Line_counter == 50:
Line_counter = 0
continue
temp1 = float(line.split()[0])
temp2 = float(line.split()[1])
temp3 = float(line.split()[2])
#x.append(temp1)
#y.append(temp2)
Free.append(temp3)
coord.append([temp1,temp2,temp3])
#print ('ok'+str(i))
Line_counter = Line_counter + 1
return coord,Free
def PlotWHAM_2D(f,binx=1000,biny=50,minx=1.,maxx=11.,miny=-0.005,maxy=0.06):
'''
Read WHAM-2D output and generate 'contour' ready outputs
Parameters: f: Input file head
binx: Number of bins in x axis
biny: Number of bins in y axis
minx: Minium in x axis
maxx: Maxium in x axis
miny: Minium in y axis
maxy: Maxium in y axis
Returns: H: 2D meshgrid for histogram
xv: 2D meshgrid on x
yv: 2D meshgrid on y
'''
x = []
y = []
## Set x,y interval
xint = (maxx-minx)/binx
yint = (maxy-miny)/biny
## Get 2D meshgrid on x,y
x = np.linspace(minx,maxx,binx)
y = np.linspace(miny,maxy,biny)
xv,yv = np.meshgrid(x,y)
## Initialize H
H = [[0]*binx for _ in range(biny)]
print ('Following are the input parameters:_______________________________________________________________')
print ('X,Y interval have been set to:'+str(xint)+' '+str(yint))
print ('binx:'+str(binx),'biny:'+str(biny))
#print (np.shape(H))
#xtemp = []
#ytemp = []
Coord_temp,Free = ReadWHAM(f)
#Print dimension of Coord_temp
num = np.shape(Coord_temp)
print (num)
for k in Coord_temp:
xind = int(math.ceil((k[0]-minx)/xint)-1)
yind = int(math.ceil((k[1]-miny)/yint)-1)
if k[2] >= 1000000:
## Assign NAN for non-sampled region
H[yind][xind] = np.NAN
else:
H[yind][xind] = k[2]
return H,xv,yv
def plotcolvar(x,y,binx,biny):
## 2D- histogram plot (Use contourcolvar instead)
fig = plt.figure()
plt.hist2d(x,y,bins=(binx,biny))
plt.xlabel('s')
plt.xlabel('z')
cbar = plt.colorbar()
cbar.ax.set_ylabel('Counts')
plt.show()
return 0
def contourcolvar(x,y,binx,biny):
H,xedge,yedge = np.histogram2d(x,y,bins=(binx,biny))
xedge = 0.5*(xedge[:-1] + xedge[1:])
yedge = 0.5*(yedge[:-1] + yedge[1:])
#CS = plt.contourf(yedge,xedge,H,30,cmap=plt.cm.bone)
#plt.show()
#print xdim,ydim,Hdim
return xedge,yedge,H
def ReadWindows(window_sn,window_zn,binx,biny,minx=1.,maxx=11.,miny=-0.005,maxy=0.07):
## Input file name as index_sn(1-11) , index_zn(1,2,3)
## Bin number binx and biny
##_________________________________________________________________________________________________________
## Return H,x,y ready for any contour plot.
x = []
y = []
print ('Following are the input parameters:_______________________________________________________________')
print ('window_sn:'+str(window_sn),'window_zn:'+str(window_zn),'binx:'+str(binx),'biny:'+str(biny))
xint = (maxx-minx)/binx
yint = (maxy-miny)/biny
print ('X,Y interval have been set to:'+str(xint)+' '+str(yint))
x = np.linspace(minx,maxx,binx)
y = np.linspace(miny,maxy,biny)
xv,yv = np.meshgrid(x,y)
H = [[0]*binx for _ in range(biny)]
#print (np.shape(H))
#xtemp = []
#ytemp = []
for i in window_sn:
if i == maxx:
continue
for j in window_zn:
## -----------------------------------------------------------
if i == 2 and j == 3:
continue
## -----------------------------------------------------------
with open('colvar_reference_'+str(i)+'_'+str(j)+'_20ns') as f:
Coord_temp,xtemp,ytemp = ReadColvar_Coord(f)
num = np.shape(Coord_temp)
print (num)
for k in Coord_temp:
xind = int(math.ceil((k[0]-minx)/xint)-1)
yind = int(math.ceil((k[1]-miny)/yint)-1)
if k[1]<miny or k[1]>maxy:
print (k[1])
continue
elif k[0]<minx or k[0]>maxx:
continue
H[yind][xind] = H[yind][xind]+1
#if xind >= 1000 or xind <= 0:
# print(k)
#if yind >= 50 or yind <= 0:
# print(k)
#print(xind,yind)
#print (num)
return H,xv,yv
def FindFWHM(histogram):
difference = max(histogram) - min(histogram)
H = np.array(histogram)
nearest = np.abs(H - difference/2)
fmin = nearest[0]
smin = nearest[0]
fminn = 0
sminn = 0
n = 0
for i in nearest:
n = n+1
if i < fmin:
smin = fmin
fmin = i
sminn = fminn
fminn = n
elif i > fmin and i < smin:
smin = i
sminn = n
#print (nearest)
FWHM = np.abs(fminn-sminn)
return FWHM
##