-
Notifications
You must be signed in to change notification settings - Fork 4
/
analyse.py
executable file
·298 lines (280 loc) · 14.3 KB
/
analyse.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
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
#!/usr/bin/env python3
import os, shutil, glob, sys, math
from molecule_db import *
def input_dens(moldb):
for compound in moldb.keys():
dip = "editconf.txt"
newpdb = "new.pdb"
os.system("gmx editconf -f box/solid/%s.pdb -density 100 -o %s > %s 2>&1" % ( compound, newpdb, dip))
if os.path.exists(newpdb):
with open(dip, "r") as inf:
for line in inf:
if line.find("Density of input") >= 0:
moldb[compound]["solid_density"] = float(line.split()[3])
os.unlink(newpdb)
if os.path.exists(dip):
os.unlink(dip)
return moldb
def get_averages(infile:str, begin:int, end: int) -> list:
ptxt = "koko.txt"
os.system("gmx analyze -f %s -b %d -e %d > %s 2>&1" % ( infile, begin, end, ptxt ))
myaver = None
myerror = None
with open(ptxt, "r") as inf:
for line in inf:
if line.find("SS1") >= 0:
words = line.split()
if len(words) >= 2:
try:
myaver = float(words[1])
myerror = float(words[2])
except ValueError:
print("Invalid line '%s'" % line)
else:
print("No words on line '%'" % line)
elif False and line.find("err.est") >= 0:
words = line.split()
if len(words) >= 4:
try:
myerror = float(words[3])
except ValueError:
print("Invalid line '%s'" % line)
return myaver, myerror
def grogzsize(grofile):
gzfile = grofile+".gz"
os.system("gzip %s" % grofile)
gzsize = os.path.getsize(gzfile)
os.system("gunzip %s" % gzfile)
return gzsize
def get_tail(rfiles:list, begin:float):
S0 = {}
for rotacf in rfiles:
if os.path.exists(rotacf):
koko = "kokok"
os.system("gmx analyze -f %s -b %s > %s" % ( rotacf, begin, koko ))
with open(koko, "r") as inf:
for line in inf:
if line.find("SS1") >= 0:
try:
words = line.split()
taurot = float(words[1])
S0[rotacf] = taurot
except ValueError:
print("Incomprehensible line '%s'" % line)
if len(S0.keys()) == 2:
return (2*S0[rfiles[0]]+S0[rfiles[1]])/3.0
elif len(S0.keys()) == 1:
return S0[rfiles[0]]
else:
return None
def analyse_solid(outf, moldb):
solids = {}
for compound in moldb.keys():
print("Looking for %s" % compound)
if os.path.isdir(compound):
os.chdir(compound)
soliddens = "solid_density"
sdens = 0
if soliddens in moldb[compound]:
sdens = moldb[compound][soliddens]
mysolid = {}
outf.write("%s %s %s solid density %g\n" % ( compound, phase, top, sdens ))
outf.write("%-10s %10s %10s %10s %10s %10s\n" % ( "Temperature", "Px(NVT)", "Py(NVT)", "Pz(NVT)", "Rho(NPT)", "Epot(NPT)" ) )
mytemps = []
for temp in glob.glob("*"):
if os.path.isdir(str(temp)):
try:
mytemps.append(int(temp))
except ValueError:
print("%s is not a directory with an integer name" % temp)
print(mytemps)
for mytemp in sorted(mytemps):
tempstr = str(mytemp)
if os.path.isdir(tempstr):
os.chdir(tempstr)
mysolid[tempstr] = {}
outf.write("%-10s" % tempstr)
if os.path.exists("NVT.gro"):
for direction in [ "XX", "YY", "ZZ" ]:
pnvt = ( "pres-%s-nvt.xvg" % direction )
sel = ( "Pres-%s" % direction )
if not os.path.exists(pnvt):
os.system("echo %s | gmx energy -f NVT.edr -o %s" % ( sel, pnvt ))
aver,error = get_averages(pnvt, 500, 1000)
if None != aver and None != error:
mysolid[tempstr][sel] = [ aver, error ]
outf.write(" %10g (%g)" % ( aver, error))
final_gro = "NPT2.gro"
if os.path.exists(final_gro):
tbegin = 10000
tend = 15000
rhonpt = "density-npt.xvg"
if not os.path.exists(rhonpt):
os.system("echo Density | gmx energy -f NPT.edr -o %s" % rhonpt)
aver, error = get_averages(rhonpt, tbegin, tend)
if None != aver and None != error:
mysolid[tempstr]["rhonpt"] = [ aver, error ]
outf.write(" %10g (%g)" % ( aver, error ))
epotnpt = "epot-npt.xvg"
if not os.path.exists(epotnpt):
os.system("echo Potential | gmx energy -f NPT.edr -o %s -nmol %d" % ( epotnpt, moldb[compound]["nsolid"]) )
aver, error = get_averages(epotnpt, tbegin, tend)
if None != aver and None != error:
mysolid[tempstr]["epotnpt"] = [ aver, error ]
outf.write(" %10g (%10g)" % ( aver, error ))
gzsize = grogzsize(final_gro)
mysolid[tempstr]["gzsize"] = [ gzsize ]
outf.write(" %10d" % gzsize)
tbegin = 250 # ps
mysolid[tempstr]["rotacf"] = [ get_tail([ "rotacf.xvg", "rotplane.xvg" ], tbegin ) ]
msd = "msd.xvg"
mysolid[tempstr]["msd"] = []
if os.path.exists(msd):
with open(msd, "r") as inf:
for line in inf:
if line.find("D") >= 0 and line.find("rotaxis") >= 0:
try:
words = line.split()
dmsd = float(words[4])
mysolid[tempstr]["msd"] = [ dmsd ]
outf.write(" %10g" % dmsd)
except ValueError:
print("Incomprehensible line '%s'" % line)
mysolid[tempstr]["phase"] = [ "unknown" ]
if len(mysolid[tempstr]["rotacf"]) == 1 and len(mysolid[tempstr]["msd"]) == 1:
if mysolid[tempstr]["msd"][0] > 0.02:
mysolid[tempstr]["phase"] = [ "liquid" ]
elif mysolid[tempstr]["rotacf"][0] < 0.9:
mysolid[tempstr]["phase"] = [ "plastic" ]
else:
mysolid[tempstr]["phase"] = [ "crystal" ]
outf.write(" %s" % mysolid[tempstr]["phase"][0])
rms = "allrmsd.xvg"
mysolid[tempstr]["rmsd"] = []
if os.path.exists(rms):
rmstot = 0
nrms = 0
with open(rms, "r") as inf:
for line in inf:
try:
rmstot += float(line.strip())
nrms += 1
except ValueError:
print("Strange line '%s'" % line)
if nrms > 0:
mysolid[tempstr]["rmsd"] = [ rmstot/nrms ]
outf.write("\n")
os.chdir("..")
os.chdir("..")
solids[compound] = mysolid
return solids
def analyse_gas(outf, moldb):
gas = {}
for compound in moldb.keys():
print("Looking for %s" % compound)
if os.path.isdir(compound):
os.chdir(compound)
mygas = {}
outf.write("%s %s %s\n" % ( compound, phase, top ))
outf.write("%-10s %10s\n" % ( "Temperature", "Epot(NVT)" ) )
for temp in glob.glob("*"):
tempstr = str(temp)
if os.path.isdir(tempstr):
os.chdir(tempstr)
mygas[tempstr] = {}
outf.write("%-10s" % temp)
final_gro = "NVT.gro"
if os.path.exists(final_gro):
epotnvt = "epot-nvt.xvg"
if not os.path.exists(epotnvt):
os.system("echo Potential | gmx energy -f NVT.edr -o %s" % ( epotnvt ) )
aver, error = get_averages(epotnvt, 10000, 15000)
if None != aver and None != error:
mygas[tempstr]["epotgas"] = [ aver, error ]
outf.write(" %10g (%g)" % (aver, error))
outf.write("\n")
os.chdir("..")
os.chdir("..")
gas[compound] = mygas
return gas
def get_str(allresults, top:str, phase:str, compound:str, myT:str, prop:str, expectError:bool, floatVar=True):
if myT in allresults[top][phase][compound]:
if prop in allresults[top][phase][compound][myT]:
allres = allresults[top][phase][compound][myT][prop]
if len(allres) == 2:
return ("%g,%g" % ( allres[0], allres[1] ))
elif len(allres) == 1:
if floatVar:
return ("%g" % ( allres[0] ))
else:
return allres[0]
if expectError:
return ","
else:
return ""
if __name__ == '__main__':
if shutil.which("gmx") == None:
sys.exit("Please load the gromacs environment using\nml load GCC/10.2.0 OpenMPI/4.0.5 GROMACS/2021")
moldb = get_moldb(False)
moldb = input_dens(moldb)
output = "results.txt"
outf = open(output, "w")
allresults = {}
for top in [ "bcc", "resp" ]:
print("Looking for %s" % top)
if os.path.isdir(top):
os.chdir(top)
allresults[top] = {}
for phase in [ "solid", "gas" ]:
if os.path.isdir(phase):
os.chdir(phase)
if "solid" == phase:
allresults[top][phase] = analyse_solid(outf, moldb)
else:
allresults[top][phase] = analyse_gas(outf, moldb)
os.chdir("..")
os.chdir("..")
outf.close()
solid = "solid"
gas = "gas"
with open("allresults.csv", "w") as csvf:
csvf.write(",,bcc,,,,,,,,,,,,,,,,,,,resp,,,,,,,,,,,,,,,,,,\n")
csvf.write("Compound,Temperature,Px(NVT),sigmaPx,Py(NVT),sigmaPy,Pz(NVT),sigmaPz,Rho(NPT),sigmaRho,Epot(NPT),sigmaE,Epot(gas),sigmaE,DHsub,sigmaH,gzip_size,D,S0,Phase,RMSD,Px(NVT),sigmaPx,Py(NVT),sigmaPy,Pz(NVT),sigmaPz,Rho(NPT),sigmaRho,Epot(NPT),sigmaE,Epot(gas),sigmaE,DHsub,sigmaH,gzip_size,D,S0,RMSD,Phase\n")
for compound in moldb.keys():
alltemps = []
# Fetch all the temperatures from all compounds
for top in [ "bcc", "resp" ]:
for phase in [ solid, gas ]:
for myt in allresults[top][phase][compound].keys():
if not int(myt) in alltemps and not myt == "0":
alltemps.append(int(myt))
# Now loop over them and print what data we have
Boltz = 0.00831415
for myT in sorted(alltemps):
csvf.write("%s,%d" % ( compound, myT ))
for top in [ "bcc", "resp" ]:
myTstr = str(myT)
epotnpt = get_str(allresults, top, solid, compound, myTstr, "epotnpt", True)
epotgas = get_str(allresults, top, gas, compound, myTstr, "epotgas", True)
dhsub = ","
try:
epn = epotnpt.split(",")
epg = epotgas.split(",")
epsolid = float(epn[0])
epgas = float(epg[0])
errsub = math.sqrt(float(epn[1])**2 + float(epg[1])**2)
dhsub = ("%g,%g" % ( epgas-epsolid-2*myT*Boltz, errsub ))
except ValueError:
# do nothing
print("Missing value")
csvf.write(",%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s" % ( get_str(allresults, top, solid, compound, myTstr, "Pres-XX", True),
get_str(allresults, top, solid, compound, myTstr, "Pres-YY", True),
get_str(allresults, top, solid, compound, myTstr, "Pres-ZZ", True),
get_str(allresults, top, solid, compound, myTstr, "rhonpt", True),
epotnpt, epotgas, dhsub,
get_str(allresults, top, solid, compound, myTstr, "gzsize", False),
get_str(allresults, top, solid, compound, myTstr, "msd", False),
get_str(allresults, top, solid, compound, myTstr, "rotacf", False),
get_str(allresults, top, solid, compound, myTstr, "rmsd", False),
get_str(allresults, top, solid, compound, myTstr, "phase", False, False) ) )
csvf.write("\n")