2"""A script that processes a velocity autocorrelation function into a
5Post-processes a vcorr file and creates a normalized spectrum.
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
16 vcorr2spectrum -f stockmayer.vcorr -o stockmayer.pspect
20__author__ = "Dan Gezelter (gezelter@nd.edu)"
21__copyright__ = "Copyright (c) 2004-present The University of Notre Dame. All Rights Reserved."
33def readVcorrFile(vcorrFileName):
39 vcorrFile = open(vcorrFileName, 'r')
40 line = vcorrFile.readline()
43 line = vcorrFile.readline()
46 if not line.startswith("#"):
49 time.append(float(L[0]))
50 vcorr.append(float(L[1]))
52 line = vcorrFile.readline()
57def computeSpectrum(outFileName, normalize):
63 # the speed of light in cm / fs
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] )
76 spect = np.fft.fft( vnew )
78 timestep = tnew[1] - tnew[0]
79 freq = np.fft.fftfreq(n, d=timestep)
81 # freq = np.fft.fftshift(freq)
82 # y = np.fft.fftshift(spect)
84 outFile = open(outFileName, 'w')
87 dfreq = (freq[1]-freq[0]) * freqScale
90 for i in range(int(n/2)):
91 s = s + np.abs(spect[i])*dfreq
93 for i in range(int(n/2)):
95 outFile.write("%lf\t%lf\n" % (freq[i] * freqScale, np.abs(spect[i])/s))
97 outFile.write("%lf\t%lf\n" % (freq[i] * freqScale, np.abs(spect[i])))
101 global haveVcorrFileName
102 global haveOutputFileName
104 haveVcorrFileName = False
105 haveOutputFileName = False
110 opts, args = getopt.getopt(argv, "hnf:o:", ["help", "normalize", "vcorr-file=", "output-file="])
111 except getopt.GetoptError:
114 for opt, arg in opts:
115 if opt in ("-h", "--help"):
118 elif opt in ("-f", "--vcorr-file"):
120 haveVcorrFileName = True
121 elif opt in ("-o", "--output-file"):
123 haveOutputFileName = True
124 elif opt in ("-n", "--normalize"):
126 if (not haveVcorrFileName):
128 print("No vcorr file was specified")
130 if (not haveOutputFileName):
132 print("No output file was specified")
136 readVcorrFile(vcorrFileName)
137 computeSpectrum(outputFileName, normalize)
140if __name__ == "__main__":
141 if len(sys.argv) == 1: