2"""A script that processes a charge velocity autocorrelation function into a
5Post-processes a wcorr file and creates a normalized spectrum.
10 -h, --help show this help
11 -f, --wcorr-file=... use specified vcorr file
12 -o, --output-file=... use specified output (.pspect) file
13 -n, --normalize normalize the power spectrum
16 wcorr2spectrum -f Pt_nanoparticle.wcorr -o Pt_nanoparticle.pspect
20__author__ = "Dan Gezelter (gezelter@nd.edu), Hemanta Bhattarai (hbhattar@nd.edu)"
21__copyright__ = "Copyright (c) 2004-present The University of Notre Dame. All Rights Reserved."
33def readWcorrFile(wcorrFileName):
40 wcorrFile = open(wcorrFileName, 'r')
41 line = wcorrFile.readline()
44 line = wcorrFile.readline()
47 if not line.startswith("#"):
50 time.append(float(L[0]))
51 wcorr.append(float(L[1]))
53 line = wcorrFile.readline()
58def computeSpectrum(outFileName, normalize):
63 # the speed of light in cm / fs
70 for i in range(1, len(time)):
71 tnew.append( -time[tlen-i] )
72 vnew.append( wcorr[tlen-i] )
73 for i in range(len(time)):
74 tnew.append( time[i] )
75 vnew.append( wcorr[i] )
77 spect = np.fft.fft( vnew )
79 timestep = tnew[1] - tnew[0]
80 freq = np.fft.fftfreq(n, d=timestep)
82 # freq = np.fft.fftshift(freq)
83 # y = np.fft.fftshift(spect)
85 outFile = open(outFileName, 'w')
88 dfreq = (freq[1]-freq[0]) * freqScale
93 for i in range(int(n/2)):
94 s = s + np.abs(spect[i])*dfreq
97 outFile.write('#frequency\tspectrum\n')
98 for i in range(int(n/2)):
99 s = s + np.abs(spect[i])*dfreq
101 for i in range(int(n/2)):
103 outFile.write("%lf\t%lf\n" % (freq[i] * freqScale, np.abs(spect[i])/s))
105 outFile.write("%lf\t%lf\n" % (freq[i] * freqScale, np.abs(spect[i])))
109 global haveWcorrFileName
110 global haveOutputFileName
112 haveWcorrFileName = False
113 haveOutputFileName = False
118 opts, args = getopt.getopt(argv, "hnf:o:", ["help", "normalize", "wcorr-file=", "output-file="])
119 except getopt.GetoptError:
122 for opt, arg in opts:
123 if opt in ("-h", "--help"):
126 elif opt in ("-f", "--wcorr-file"):
128 haveWcorrFileName = True
129 elif opt in ("-o", "--output-file"):
131 haveOutputFileName = True
132 elif opt in ("-n", "--normalize"):
134 if (not haveWcorrFileName):
136 print("No wcorr file was specified")
138 if (not haveOutputFileName):
140 print("No output file was specified")
144 readWcorrFile(wcorrFileName)
145 computeSpectrum(outputFileName, normalize)
148if __name__ == "__main__":
149 if len(sys.argv) == 1: