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