OpenMD 3.0
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
vcorr2spectrum
1#!@Python3_EXECUTABLE@
2"""A script that processes a velocity autocorrelation function into a
3amplitude spectrum
4
5Post-processes a vcorr file and creates a normalized spectrum.
6
7Usage: vcorr2spectrum
8
9Options:
10 -h, --help show this help
11 -f, --vcorr-file=... use specified vcorr file
12 -o, --output-file=... use specified output (.pspect) file
13 -n, --normalize normalize the power spectrum
14
15Example:
16 vcorr2spectrum -f stockmayer.vcorr -o stockmayer.pspect
17
18"""
19
20__author__ = "Dan Gezelter (gezelter@nd.edu)"
21__copyright__ = "Copyright (c) 2004-present The University of Notre Dame. All Rights Reserved."
22__license__ = "OpenMD"
23
24import sys
25import getopt
26import string
27import math
28import numpy as np
29
30def usage():
31 print(__doc__)
32
33def readVcorrFile(vcorrFileName):
34 global time
35 global vcorr
36 time = []
37 vcorr = []
38
39 vcorrFile = open(vcorrFileName, 'r')
40 line = vcorrFile.readline()
41
42 print("reading File")
43 line = vcorrFile.readline()
44 while True:
45
46 if not line.startswith("#"):
47 L = line.split()
48
49 time.append(float(L[0]))
50 vcorr.append(float(L[1]))
51
52 line = vcorrFile.readline()
53 if not line: break
54
55 vcorrFile.close()
56
57def computeSpectrum(outFileName, normalize):
58 global tnew
59 global vnew
60 tnew = []
61 vnew = []
62
63 # the speed of light in cm / fs
64 c = 2.99792458e-5
65
66 # symmetrize arrays
67
68 tlen = len(time)
69 for i in range(1, len(time)):
70 tnew.append( -time[tlen-i] )
71 vnew.append( vcorr[tlen-i] )
72 for i in range(len(time)):
73 tnew.append( time[i] )
74 vnew.append( vcorr[i] )
75
76 spect = np.fft.fft( vnew )
77 n = len(vnew)
78 timestep = tnew[1] - tnew[0]
79 freq = np.fft.fftfreq(n, d=timestep)
80
81 # freq = np.fft.fftshift(freq)
82 # y = np.fft.fftshift(spect)
83
84 outFile = open(outFileName, 'w')
85
86 freqScale = 1.0 / c
87 dfreq = (freq[1]-freq[0]) * freqScale
88 s = 0.0
89
90 for i in range(int(n/2)):
91 s = s + np.abs(spect[i])*dfreq
92
93 for i in range(int(n/2)):
94 if normalize:
95 outFile.write("%lf\t%lf\n" % (freq[i] * freqScale, np.abs(spect[i])/s))
96 else:
97 outFile.write("%lf\t%lf\n" % (freq[i] * freqScale, np.abs(spect[i])))
98 outFile.close()
99
100def main(argv):
101 global haveVcorrFileName
102 global haveOutputFileName
103
104 haveVcorrFileName = False
105 haveOutputFileName = False
106 haveQValue = False
107 normalize = False
108
109 try:
110 opts, args = getopt.getopt(argv, "hnf:o:", ["help", "normalize", "vcorr-file=", "output-file="])
111 except getopt.GetoptError:
112 usage()
113 sys.exit(2)
114 for opt, arg in opts:
115 if opt in ("-h", "--help"):
116 usage()
117 sys.exit()
118 elif opt in ("-f", "--vcorr-file"):
119 vcorrFileName = arg
120 haveVcorrFileName = True
121 elif opt in ("-o", "--output-file"):
122 outputFileName = arg
123 haveOutputFileName = True
124 elif opt in ("-n", "--normalize"):
125 normalize = True
126 if (not haveVcorrFileName):
127 usage()
128 print("No vcorr file was specified")
129 sys.exit()
130 if (not haveOutputFileName):
131 usage()
132 print("No output file was specified")
133 sys.exit()
134
135
136 readVcorrFile(vcorrFileName)
137 computeSpectrum(outputFileName, normalize)
138
139
140if __name__ == "__main__":
141 if len(sys.argv) == 1:
142 usage()
143 sys.exit()
144 main(sys.argv[1:])