OpenMD 3.1
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
stat2dipolecorr
1#!@Python3_EXECUTABLE@
2"""A script that computes the system dipole correlation function
3
4Options:
5 -h, --help show this help
6 -f, --stat-file =... use specified stat file
7 -o, --output-file =... use specified output (.spectrum) file
8 -p, --dipole_pos =... column number of x-component of dipole in the stat. file. Note: the count starts from 0
9
10Example:
11 stat2dipolecorr -f stockmayer.stat -o stockmayer.spectrum -p 9
12
13"""
14
15__author__ = "Dan Gezelter (gezelter@nd.edu), Hemanta Bhattarai (hbhattar@nd.edu)"
16__copyright__ = "Copyright (c) 2004-present The University of Notre Dame. All Rights Reserved."
17__license__ = "OpenMD"
18
19import sys
20import getopt
21import string
22import math
23
24def usage():
25 print(__doc__)
26
27def readStatFile(statFileName, dipole_pos):
28 global time
29 global boxDipole
30 time = []
31 boxDipole = []
32
33 statFile = open(statFileName, 'r')
34
35 print("reading File")
36 line = statFile.readline()
37 while ("#" in line):
38 line = statFile.readline()
39
40 while True:
41 L = line.split()
42 time.append(float(L[0]))
43 dipX = float(L[dipole_pos])
44 dipY = float(L[dipole_pos + 1])
45 dipZ = float(L[dipole_pos + 2])
46 boxDipole.append([dipX, dipY, dipZ])
47
48 line = statFile.readline()
49 if not line: break
50
51 statFile.close()
52
53def dot(L1, L2):
54 dotprod = 0
55 for index in range(len(L1)):
56 dotprod += L1[index] * L2[index]
57 return(dotprod)
58
59def compute_auto_correlation(outFile):
60 outSpectrum = open(outFile, 'w')
61 outSpectrum.write("# System dipole correlation function\n")
62 outSpectrum.write("# time <P(0).P(t)>\n");
63 N = len(time)
64 t = []
65 auto_corr = []
66 for step in range(N):
67 count = 0
68 local_auto_corr = []
69 for start in range(N-step):
70 local_auto_corr.append(dot(boxDipole[start], boxDipole[start + step]))
71 count += 1
72 outSpectrum.write("%f\t%e\n"%(time[step], (sum(local_auto_corr)/count)))
73
74 outSpectrum.close()
75
76
77
78
79
80
81
82def main(argv):
83 global haveStatFileName
84 global haveOutputFileName
85 global haveDipoleColumn
86
87 haveStatFileName = False
88 haveOutputFileName = False
89 haveDipoleColumn = False
90
91 try:
92 opts, args = getopt.getopt(argv, "hp:f:o:", ["help", "=", "stat-file=", "output-file="])
93 except getopt.GetoptError:
94 usage()
95 sys.exit(2)
96 for opt, arg in opts:
97 if opt in ("-h", "--help"):
98 usage()
99 sys.exit()
100 elif opt in ("-p", "--dipole_pos="):
101 dipole_pos = int(arg)
102 haveDipoleColumn = True
103 elif opt in ("-f", "--stat-file"):
104 statFileName = arg
105 haveStatFileName = True
106 elif opt in ("-o", "--output-file"):
107 outputFileName = arg
108 haveOutputFileName = True
109 if (not haveStatFileName):
110 usage()
111 print("No stat file was specified")
112 sys.exit()
113 if (not haveOutputFileName):
114 usage()
115 print("No output file was specified")
116 sys.exit()
117 if (not haveDipoleColumn):
118 usage()
119 print("Coulumn number of x-component of dipole not specified")
120 sys.exit()
121
122 readStatFile(statFileName, dipole_pos)
123 compute_auto_correlation(outputFileName)
124
125if __name__ == "__main__":
126 if len(sys.argv) == 1:
127 usage()
128 sys.exit()
129 main(sys.argv[1:])