OpenMD 3.0
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
stat2pcorr
1#!@Python3_EXECUTABLE@
2"""Pressure Correlation function
3
4Computes various correlation functions of the pressure
5that have been stored in a stat file.
6
7Usage: stat2pcorr
8
9Options:
10 -h, --help show this help
11 -f, --stat-file=... use specified stat file
12 -o, --output-file=... use specified output (.pcorr) file
13
14The script will compute: <(P(t)-<P>)*(P(0)-<P>)> / < (P(0) - <P>)^2 >
15
16Example:
17 stat2pcorr -f ring5.stat -o ring5.pcorr
18
19"""
20
21__author__ = "Dan Gezelter (gezelter@nd.edu)"
22__version__ = "$Revision$"
23__date__ = "$Date$"
24
25__copyright__ = "Copyright (c) 2004-present The University of Notre Dame. All Rights Reserved."
26__license__ = "OpenMD"
27
28import sys
29import getopt
30import string
31import math
32
33def usage():
34 print(__doc__)
35
36def readStatFile(statFileName):
37
38 global time
39 global temperature
40 global pressure
41 global volume
42 time = []
43 temperature = []
44 pressure = []
45 volume = []
46
47 statFile = open(statFileName, 'r')
48
49 print("reading File")
50 pressSum = 0.0
51 volSum = 0.0
52 tempSum = 0.0
53
54 line = statFile.readline()
55 while ("#" in line):
56 line = statFile.readline()
57
58 while True:
59 L = line.split()
60 time.append(float(L[0]))
61 temperature.append(float(L[4]))
62 #
63 # OpenMD prints out pressure in units of atm.
64 #
65 pressure.append(float(L[5]))
66 volume.append(float(L[6]))
67
68 line = statFile.readline()
69 if not line: break
70
71 statFile.close()
72
73def computeAverages():
74
75 global tempAve
76 global pressAve
77 global volAve
78 global pvAve
79
80 print("computing Averages")
81
82 tempSum = 0.0
83 pressSum = 0.0
84 volSum = 0.0
85 pvSum = 0.0
86
87 temp2Sum = 0.0
88 press2Sum = 0.0
89 vol2Sum = 0.0
90 pv2Sum = 0.0
91
92 # converts amu*fs^-2*Ang^-1 -> atm
93 pressureConvert = 1.63882576e8
94
95 for i in range(len(time)):
96 tempSum = tempSum + temperature[i]
97 pressSum = pressSum + pressure[i]
98 volSum = volSum + volume[i]
99 # in units of amu Ang^2 fs^-1
100 pvTerm = pressure[i]*volume[i] / pressureConvert
101 pvSum = pvSum + pvTerm
102 temp2Sum = temp2Sum + math.pow(temperature[i], 2)
103 press2Sum = press2Sum + math.pow(pressure[i], 2)
104 vol2Sum = vol2Sum + math.pow(volume[i], 2)
105 pv2Sum = pv2Sum + math.pow(pvTerm, 2)
106
107 tempAve = tempSum / float(len(time))
108 pressAve = pressSum / float(len(time))
109 volAve = volSum / float(len(time))
110 pvAve = pvSum / float(len(time))
111
112 tempSdev = math.sqrt(temp2Sum / float(len(time)) - math.pow(tempAve, 2))
113 pressSdev = math.sqrt(press2Sum / float(len(time)) - math.pow(pressAve, 2))
114 if (vol2Sum / float(len(time)) < math.pow(volAve, 2)):
115 volSdev = 0.0
116 else:
117 volSdev = math.sqrt(vol2Sum / float(len(time)) - math.pow(volAve, 2))
118 pvSdev = math.sqrt(pv2Sum / float(len(time)) - math.pow(pvAve, 2))
119
120 print(" Average pressure = %f +/- %f (atm)" % (pressAve, pressSdev))
121 print(" Average volume = %f +/- %f (Angst^3)" % (volAve, volSdev))
122 print("Average temperature = %f +/- %f (K)" % (tempAve, tempSdev))
123 print(" Average PV product = %f +/- %f (amu Angst^2 fs^-1)" % (pvAve, pvSdev))
124
125def computeCorrelations(outputFileName):
126
127 # converts amu*fs^-2*Ang^-1 -> atm
128 pressureConvert = 1.63882576e8
129
130 # Boltzmann's constant amu*Ang^2*fs^-2/K
131 kB = 8.31451e-7
132
133 if doCorrelation:
134 Pcorr = []
135 print("computing Pressure Correlation Function")
136 # i corresponds to dt
137 for i in range(len(time)):
138 # j is the starting time for the correlation
139 pp = 0.0
140 for j in range( len(time) - i ):
141 deltaP1 = pressure[j] - pressAve
142 deltaP2 = pressure[j+i] - pressAve
143 pp = pp + deltaP1*deltaP2
144
145 Pcorr.append(pp / float(len(time) - i))
146
147 outputFile = open(outputFileName, 'w')
148 for i in range(len(time)):
149 if doCorrelation:
150 outputFile.write("%f\t%13e\n" % (time[i], Pcorr[i] / Pcorr[0]))
151
152 outputFile.close()
153
154def main(argv):
155 global doCorrelation
156 global haveStatFileName
157 global haveOutputFileName
158
159 haveStatFileName = False
160 haveOutputFileName = False
161 doCorrelation = True
162
163 try:
164 opts, args = getopt.getopt(argv, "hf:o:", ["help", "stat-file=", "output-file="])
165 except getopt.GetoptError:
166 usage()
167 sys.exit(2)
168 for opt, arg in opts:
169 if opt in ("-h", "--help"):
170 usage()
171 sys.exit()
172 elif opt in ("-f", "--stat-file"):
173 statFileName = arg
174 haveStatFileName = True
175 elif opt in ("-o", "--output-file"):
176 outputFileName = arg
177 haveOutputFileName = True
178 if (not haveStatFileName):
179 usage()
180 print("No stat file was specified")
181 sys.exit()
182 if (not haveOutputFileName):
183 usage()
184 print("No output file was specified")
185 sys.exit()
186
187 readStatFile(statFileName);
188 computeAverages();
189 computeCorrelations(outputFileName);
190
191if __name__ == "__main__":
192 if len(sys.argv) == 1:
193 usage()
194 sys.exit()
195 main(sys.argv[1:])