-
Notifications
You must be signed in to change notification settings - Fork 114
/
XDATCAR_manipulation.py
215 lines (193 loc) · 8.56 KB
/
XDATCAR_manipulation.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
#!/bin/python
# -*- coding:utf-8 -*-
import os
import numpy as np
import matplotlib as mpl
mpl.use('Agg') #silent mode
from matplotlib import pyplot as plt
class debug(object):
def __init__(self,info='debug'):
self.info=info
def __call__(self,func):
def wrapper(*args,**kwargs):
print('[{info}] Now entering function {function}.....' \
.format(info=self.info,function=getattr(func,"__name__")))
return func(*args,**kwargs)
return wrapper
class Energy_Temp(object):
def __init__(self):
'Read vasp MD energies and temperature.'
self.temp=[]
self.energy=[]
self.energy_extract()
def energy_extract(self):
print('Now reading vasp MD energies and temperature.')
if os.path.exists("OSZICAR"):
oszicar=open("OSZICAR",'r')
for index,line in enumerate(oszicar):
if "E0=" in line:
self.temp.append(float(line.split()[2]))
self.energy.append(float(line.split()[4]))
oszicar.close()
#return (self.temp,self.energy)
else:
raise IOError('OSZICAR does not exist!')
class XDATCAR(Energy_Temp):
def __init__(self):
'Read lattice information and atomic coordinate.'
super(XDATCAR,self).__init__()
print('Now reading vasp XDATCAR.')
self.lattice=np.zeros((3,3))
self.NPT=False
self.frames=0
self._timestep=1 # timestep 1fs
@property
def timestep(self):
return self._timestep
@timestep.setter
def timestep(self,new_time_step):
self._timestep=new_time_step
if os.path.exists("XDATCAR"):
self.XDATCAR=open("XDATCAR",'r')
else:
raise IOError('XDATCAR does not exist!')
title=self.XDATCAR.readline().strip()
for index,line in enumerate(self.XDATCAR):
if "Direct" in line:
self.frames+=1
elif title in line:
self.NPT=True
#self.frames=len(['Direct' for line in self.XDATCAR if "Direct" in line])
print('Total frames {0}, NpT is {1}'.format(self.frames,self.NPT))
assert len(self.energy)==self.frames, \
'Number of XDATCAR frames does not equal to that of Energy terms in OSZICAR'
self.XDATCAR.seek(0)
self.lowrange=0;self.uprange=self.frames-1
if self.NPT == False: self.lattice_read()
def step_select(self,selected_step): # 't>100ps and t < 1000ps'
'eg. t > 100 and t < 1000'
assert isinstance(selected_step,str),'Selected timestep must be in a "string"'
if 'and' in selected_step:
conditions=selected_step.split("and")
else:
conditions=[selected_step]
for condition in conditions:
try:
if '>=' in condition:
ranges=int(condition.split('>=')[1].strip())
self.lowrange=ranges-1
elif '<=' in condition:
ranges=int(condition.split('<=')[1].strip())
self.uprange=ranges-1
elif '>' in condition:
ranges=int(condition.split('>')[1].strip())
self.lowrange=ranges
elif '<' in condition:
ranges=int(condition.split('<')[1].strip())
self.uprange=ranges-2
else:
print('Selected timestep is invaid!');continue
except ValueError:
print('Selected timestep is invaid!')
if (self.lowrange >= self.frames-1) or (self.uprange < 0):
raise ValueError('Selected timestep is wrong!')
if self.lowrange < 0: self.lowrange= 0
if self.uprange > self.frames-1: self.uprange= self.frames-1
def lattice_read(self):
self.title=self.XDATCAR.readline().rstrip('\r\n').rstrip('\n')
self.scaling_factor=float(self.XDATCAR.readline())
for i in range(3):
self.lattice[i]=np.array([float(j) for j in self.XDATCAR.readline().split()])
#self.lattice*=self.scaling_factor
self.element_list=[j for j in self.XDATCAR.readline().split()]
try:
self.element_amount=[int(j) for j in self.XDATCAR.readline().split()]
except ValueError:
raise ValueError('VASP 5.x XDATCAR is needed!')
self.total_atom=sum(self.element_amount)
self.atomic_position=np.zeros((self.total_atom,3))
def __iter__(self):
return self
def __next__(self):
self.next()
def next(self):
if self.NPT == True: self.lattice_read()
self.XDATCAR.readline()
for i in range(self.total_atom):
line_tmp=self.XDATCAR.readline()
self.atomic_position[i]=np.array([float(j) for j in line_tmp.split()[0:3]])
self.cartesian_position=np.dot(self.atomic_position,self.lattice)
self.cartesian_position*=self.scaling_factor
return self.cartesian_position
def __call__(self,selected_step):
self.step_select(selected_step)
def __str__(self):
return ('Read lattice information and atomic coordinate.')
__repr__=__str__
class plot(object):
'Plot MD temperature and energy profile'
def __init__(self,lwd,font,dpi,figsize,XDATCAR_inst=None):
self.XDATCAR_inst=XDATCAR_inst;self.timestep=self.XDATCAR_inst.timestep
self.time_range=(self.XDATCAR_inst.lowrange,self.XDATCAR_inst.uprange+1)
self.lwd=lwd;self.font=font;self.dpi=dpi;self.figsize=figsize
@debug(info='debug')
def plotfigure(self,title='MD temperature and energy profile'):
from matplotlib import pyplot as plt
self.newtemp=self.XDATCAR_inst.temp;self.newenergy=self.XDATCAR_inst.energy
xdata=np.arange(self.time_range[0],self.time_range[1])*self.timestep
axe = plt.subplot(121)
self.newtemp=self.newtemp[self.time_range[0]:self.time_range[1]]
axe.plot(xdata,self.newtemp, \
color='black', lw=self.lwd, linestyle='-', alpha=1)
with open("Temperature.dat",'w') as writer:
writer.write("Time(fs) Temperature(K)")
for line in range(len(xdata)):
writer.write("{0:d} {1:f}\n" .format(xdata[line],self.newtemp[line]))
axe.set_xlabel(r'$Time$ (fs)',fontdict=self.font)
axe.set_ylabel(r'$Temperature$ (K)',fontdict=self.font)
axe.set_xlim((self.time_range[0], self.time_range[1]))
axe.set_title('MD temperature profile')
axe1 = plt.subplot(122)
self.newenergy=self.newenergy[self.time_range[0]:self.time_range[1]]
axe1.plot(xdata,self.newenergy, \
color='black', lw=self.lwd, linestyle='-', alpha=1)
with open("Energy.dat",'w') as writer:
writer.write("Time(fs) Energy(ev)")
for line in range(len(xdata)):
writer.write("{0:d} {1:f}\n" .format(xdata[line],self.newenergy[line]))
axe1.set_xlabel(r'$Time$ (fs)',fontdict=self.font)
axe1.set_ylabel(r'$Energy$ (ev)',fontdict=self.font)
axe1.set_xlim((self.time_range[0], self.time_range[1]))
axe1.set_title('MD energy profile')
#plt.suptitle(title)
fig = plt.gcf()
fig.set_size_inches(self.figsize)
plt.tight_layout()
plt.savefig('ENERGY.png',bbox_inches='tight',pad_inches=0.1,dpi=self.dpi)
if __name__ == "__main__":
XDATCAR_inst=XDATCAR()
XDATCAR_iter=iter(XDATCAR_inst)
XDATCAR_inst.timestep=1 #timestep 2fs
XDATCAR_inst('t>= 0 and t < 100000') # frame 10~300 corresponding to 20~600fs
for i in range(XDATCAR_inst.frames):
if (i>=XDATCAR_inst.lowrange) and (i<=XDATCAR_inst.uprange):
cartesian_position=XDATCAR_iter.next()
else:
XDATCAR_iter.next()
newtemp=XDATCAR_inst.temp
newenergy=XDATCAR_inst.energy
print('Finish reading XDATCAR.')
timestep=XDATCAR_inst.timestep
print('Selected time-range:{0}~{1}fs'.format((XDATCAR_inst.lowrange+1)*timestep,\
(XDATCAR_inst.uprange+1)*timestep))
XDATCAR_inst.XDATCAR.close()
lwd = 0.2 # Control width of line
dpi=300 # figure_resolution
figsize=(5,4) #figure_inches
font = {'family' : 'arial',
'color' : 'black',
'weight' : 'normal',
'size' : 13.0,
} #FONT_setup
plots=plot(lwd,font,dpi,figsize,XDATCAR_inst)
plots.plotfigure()