forked from QijingZheng/UsefulPythonSnippet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxdatcar.py
268 lines (223 loc) · 8.77 KB
/
xdatcar.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#!/usr/bin/env python
# This script was created by zqj
# <Tue May 26 09:05:25 CST 2015>
##################################### NOTES #####################################
# 1. Set NBLOCK = 1 in the INCAR, so that all the configuration is wrtten to
# XDATCAR.
#################################################################################
import os
import numpy as np
import matplotlib.pyplot as plt
from scipy.fftpack import fft, ifft, fftfreq, fftshift
# Boltzmann Constant in [eV/K]
kb = 8.617332478E-5
# electron volt in [Joule]
ev = 1.60217733E-19
# Avogadro's Constant
Navogadro = 6.0221412927E23
################################################################################
class xdatcar:
""" Python Class for VASP XDATCAR """
def __init__(self, File=None):
if File is None:
self.xdatcar = 'XDATCAR'
else:
self.xdatcar = File
# time step of MD
self.potim = None
# mass per type
self.mtype = None
self.readoutcar()
self.TypeName = None
self.ChemSymb = None
self.Ntype = None
self.Nions = None
self.Nelem = None
self.Niter = None
# position in Direct Coordinate
self.position = None
# position in Cartesian Coordinate
self.positionC = None
# Velocity in Angstrom per Femtosecond
self.velocity = None
self.readxdat()
self.mass_and_name_per_ion()
# Temperature
self.Temp = np.zeros(self.Niter-1)
# Kinetic Energy
self.Ken = np.zeros(self.Niter-1)
# Time in femtosecond
self.Time = np.arange(self.Niter-1) * self.potim
self.getTemp()
# Velocity Autocorrelation Function
self.VAF = None
self.VAF2= None
# Pair Correlation Function
# self.PCF = None
def mass_and_name_per_ion(self):
# mass per ion
self.mions = []
self.ChemSymb = []
if self.TypeName is None:
self.TypeName = [chr(i) for i in range(65,91)][:self.Ntype]
for i in range(self.Ntype):
self.mions += [np.tile(self.mtype[i], self.Nelem[i])]
self.ChemSymb += [np.tile(self.TypeName[i], self.Nelem[i])]
self.mions = np.concatenate(self.mions)
self.ChemSymb = np.concatenate(self.ChemSymb)
def readxdat(self):
""" Read VASP XDATCAR """
inp = [line for line in open(self.xdatcar) if line.strip()]
scale = float(inp[1])
self.cell = np.array([line.split() for line in inp[2:5]],
dtype=float)
self.cell *= scale
ta = inp[5].split()
tb = inp[6].split()
# print ta, tb
if ta[0].isalpha():
self.TypeName = ta
self.Ntype = len(ta)
self.Nelem = np.array(tb, dtype=int)
self.Nions = self.Nelem.sum()
else:
print "VASP 4.X Format encountered..."
self.Nelem = np.array(tb, type=int)
self.Nions = self.Nelem.sum()
self.Ntype = len(tb)
self.TypeName = None
pos = np.array([line.split() for line in inp[7:]
if not line.split()[0].isalpha()],
dtype=float)
self.position = pos.ravel().reshape((-1,self.Nions,3))
self.Niter = self.position.shape[0]
dpos = np.diff(self.position, axis=0)
self.positionC = np.zeros_like(self.position)
# apply periodic boundary condition
dpos[dpos > 0.5] -= 1.0
dpos[dpos <-0.5] += 1.0
# Velocity in Angstrom per femtosecond
for i in range(self.Niter-1):
self.positionC[i,:,:] = np.dot(self.cell, self.position[i,:,:].T).T
dpos[i,:,:] = np.dot(self.cell, dpos[i,:,:].T).T / self.potim
self.velocity = dpos
def readoutcar(self):
""" read POTIM and POMASS from OUTCAR """
if os.path.isfile("OUTCAR"):
# print "OUTCAR found!"
# print "Reading POTIM & POMASS from OUTCAR..."
outcar = [line.strip() for line in open('OUTCAR')]
lp = 0; lm = 0;
for ll, line in enumerate(outcar):
if 'POTIM' in line:
lp = ll
if 'Mass of Ions in am' in line:
lm = ll + 1
if lp and lm:
break
# print outcar[lp].split(), lp, lm
self.potim = float(outcar[lp].split()[2])
self.mtype = np.array(outcar[lm].split()[2:], dtype=float)
def getTemp(self, Nfree=None):
""" Temp vs Time """
for i in range(self.Niter-1):
ke = np.sum(np.sum(self.velocity[i,:,:]**2, axis=1) * self.mions / 2.)
self.Ken[i] = ke * 1E7 / Navogadro / ev
if Nfree is None:
Nfree = 3 * (self.Nions - 1)
self.Temp[i] = 2 * self.Ken[i] / (kb * Nfree)
def getVAF(self):
""" Velocity Autocorrelation Function """
# VAF definitions
# VAF(t) = Natoms^-1 * \sum_i <V_i(0) V_i(t)>
############################################################
# Fast Fourier Transform Method to calculate VAF
############################################################
# The cross-correlation theorem for the two-sided correlation:
# corr(a,b) = ifft(fft(a)*fft(b).conj()
# If a == b, then this reduces to the special case of the
# Wiener-Khinchin theorem (autocorrelation of a):
# corr(a,a) = ifft(abs(fft(a))**2)
# where the power spectrum of a is simply:
# fft(corr(a,a)) == abs(fft(a))**2
############################################################
# in this function, numpy.correlate is used to calculate the VAF
self.VAF2 = np.zeros((self.Niter-1)*2 - 1)
for i in range(self.Nions):
for j in range(3):
self.VAF2 += np.correlate(self.velocity[:,i,j],
self.velocity[:,i,j],
'full')
# two-sided VAF
self.VAF2 /= np.sum(self.velocity**2)
self.VAF = self.VAF2[self.Niter-2:]
def phononDos(self, unit='THz', sigma=5):
""" Phonon DOS from VAF """
N = self.Niter - 1
# Frequency in THz
omega = fftfreq(2*N-1, self.potim) * 1E3
# Frequency in cm^-1
if unit.lower() == 'cm-1':
omega *= 33.35640951981521
if unit.lower() == 'mev':
omega *= 4.13567
# from scipy.ndimage.filters import gaussian_filter1d as gaussian
# smVAF = gaussian(self.VAF2, sigma=sigma)
# pdos = np.abs(fft(smVAF))**2
pdos = np.abs(fft(self.VAF2))**2
return omega[:N], pdos[:N]
def PCF(self, bins=50, Niter=10, A='', B=''):
""" Pair Correlation Function """
if not A:
A = self.TypeName[0]
if not B:
B = A
whichA = self.ChemSymb == A
whichB = self.ChemSymb == B
indexA = np.arange(self.Nions)[whichA]
indexB = np.arange(self.Nions)[whichB]
posA = self.position[:,whichA,:]
posB = self.position[:,whichB,:]
steps = range(0, self.Niter, Niter)
rABs = np.array([posA[i,k,:]-posB[i,j,:]
for k in range(indexA.size)
for j in range(indexB.size)
for i in steps
if indexA[k] != indexB[j]])
# periodic boundary condition
rABs[rABs > 0.5] -= 1.0
rABs[rABs <-0.5] += 1.0
# from direct to cartesian coordinate
rABs = np.linalg.norm(np.dot(self.cell, rABs.T), axis=0)
# histogram of pair distances
val, b = np.histogram(rABs, bins=bins)
# density of the system
rho = self.Nions / np.linalg.det(self.cell)
# Number of A type atom
Na = self.Nelem[self.TypeName.index(A)]
# Number of B type atom
Nb = self.Nelem[self.TypeName.index(B)]
dr = b[1] - b[0]
val = val * self.Nions / (4*np.pi*b[1:]**2 * dr) / (Na * Nb * rho) / len(steps)
return val, b[1:]
################################################################################
# test code of the above class
if __name__ == '__main__':
inp = xdatcar()
inp.getVAF()
# plt.plot((np.abs(fft(inp.VAF[inp.Niter-2:]))**2))
# print inp.VAF.shape
# plt.plot(inp.Time, inp.VAF, 'ko-', lw=1.0, ms=2,
# markeredgecolor='r', markerfacecolor='red')
#
# plt.xlabel('Time [fs]')
# plt.ylabel('Velocity Autocorrelation Function')
# x, y = inp.phononDos('cm-1')
# plt.plot(x, y, 'ko-')
# plt.xlim(0, 5000)
# # plt.ylim(-0.5, 1.0)
val, b = inp.PCF(100, 1)
plt.plot(b, val)
plt.axhline(y=1, color='r')
plt.xlim(0, 5)
plt.show()