forked from sr76/excitingscripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CHECKFIT-energy-vs-volume.py
executable file
·215 lines (159 loc) · 6.39 KB
/
CHECKFIT-energy-vs-volume.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
#_______________________________________________________________________________
from sys import stdin
from math import sqrt
from math import factorial
from pylab import *
import matplotlib.pyplot as plt
import matplotlib.ticker as ptk
import pylab as pyl
import numpy
import sys
import os
#-------------------------------------------------------------------------------
def sortstrain(s,e):
ss=[]
ee=[]
ww=[]
for i in range(len(s)): ww.append(s[i])
ww.sort()
for i in range(len(s)):
ss.append(s[s.index(ww[i])])
ee.append(e[s.index(ww[i])])
return ss, ee
#-------------------------------------------------------------------------------
def shell_value(variable,vlist,default):
v = default
e = False
for i in range(len(vlist)):
if ( vlist[i] == variable ): v = os.environ[variable] ; e = True ; break
return v, e
#-------------------------------------------------------------------------------
if (str(os.path.exists('energy-vs-volume'))=='False'):
sys.exit("ERROR: file energy-vs-volume not found!\n")
#-------------------------------------------------------------------------------
order_of_fit = \
input("\nEnter the order of polynomial to be used in the fit >>>> ")
if (order_of_fit < 0):
sys.exit("ERROR: Order of polynomial must be positive!\n")
print
print "==============================="
print "Lattice symmetry codes"
print "-------------------------------"
print "1 --> Simple cubic (sc)"
print "2 --> Body-centered cubic (bcc)"
print "3 --> Face-centered cubic (fcc)"
print "-------------------------------"
print "0 --> Others"
print "===============================\n"
scheck = \
raw_input("Enter lattice symmetry code [default 0] >>>> ").replace(" ", "")
isym = 0
factor = 1
if ( scheck == "1" ): isym = 1 ; factor=1 ; slabel = "(sc) "
if ( scheck == "2" ): isym = 2 ; factor=2 ; slabel = "(bcc)"
if ( scheck == "3" ): isym = 3 ; factor=4 ; slabel = "(fcc)"
print "Verification lattice symmetry code >>>>", isym
#-------------------------------------------------------------------------------
energy = []
strain = []
#-------------------------------------------------------------------------------
input_energy = open('energy-vs-volume',"r")
while True:
line = input_energy.readline()
line = line.strip()
if len(line) == 0: break
energy.append(float(line.split()[1]))
strain.append(float(line.split()[0]))
strain,energy=sortstrain(strain,energy)
#-------------------------------------------------------------------------------
bohr_radius = 0.529177
joule2hartree = 4.3597482
joule2rydberg = joule2hartree/2.
unitconv = joule2hartree/bohr_radius**3*10.**3
#-------------------------------------------------------------------------------
fitr = numpy.polyfit(strain,energy,order_of_fit)
curv = numpy.poly1d(fitr)
bulk = numpy.poly1d(numpy.polyder(fitr,2))
vmin = numpy.roots(numpy.polyder(fitr))
dmin=[]
for i in range(len(vmin)):
if (abs(vmin[i].imag) < 1.e-10):
if (strain[0] <= vmin[i] and vmin[i] <= strain[-1]):
if(bulk(vmin[i]) > 0): dmin.append(vmin[i].real)
xvol = numpy.linspace(strain[0],strain[-1],100)
#-------------------------------------------------------------------------------
current = os.environ['PWD']
ev_list = os.environ.keys()
rundir = shell_value('EXCITINGRUNDIR',ev_list,current)[0]
rlabel = shell_value('RLABEL',ev_list,"rundir-")[0]
showpyplot = shell_value('SHOWPYPLOT',ev_list,"")[1]
dpipng = int(shell_value('DPIPNG',ev_list,300)[0])
#-------------------------------------------------------------------------------
xlabel = u'Volume [Bohr\u00B3]'
ylabel = r'Energy [Ha]'
if (os.path.exists('quantum-espresso')): ylabel = r'Energy [Ry]'
if (os.path.exists('vasp')): ylabel = r'Energy [Ry]'
#-------------------------------------------------------------------------------
fontlabel=20
fonttick=16
params = {'ytick.minor.size': 6,
'xtick.major.pad': 8,
'ytick.major.pad': 4,
'patch.linewidth': 2.,
'axes.linewidth': 2.,
'lines.linewidth': 1.8,
'lines.markersize': 8.0,
'axes.formatter.limits': (-4, 6)}
plt.rcParams.update(params)
plt.subplots_adjust(left=0.21, right=0.93,
bottom=0.18, top=0.88,
wspace=None, hspace=None)
yfmt = ptk.ScalarFormatter(useOffset=True,useMathText=True)
figure = plt.figure(1, figsize=(8,5.5))
ax = figure.add_subplot(111)
ax.text(0.5,-0.13,xlabel,size=fontlabel,
transform=ax.transAxes,ha='center',va='center')
ax.text(-0.19,0.5,ylabel,size=fontlabel,
transform=ax.transAxes,ha='center',va='center',rotation=90)
for line in ax.get_xticklines() + ax.get_yticklines():
line.set_markersize(6)
line.set_markeredgewidth(2)
plt.xticks(size=fonttick)
plt.yticks(size=fonttick)
pyl.grid(True)
plt.plot(xvol,curv(xvol),'b-',label='n='+str(order_of_fit)+' fit')
plt.plot(strain,energy,'go',label='calculated')
plt.plot(dmin,curv(dmin),'ro')
plt.legend(loc=9,borderaxespad=.8,numpoints=1)
ymax = max(max(curv(xvol)),max(energy))
ymin = min(min(curv(xvol)),min(energy))
dxx = abs(max(xvol)-min(xvol))/18
dyy = abs(ymax-ymin)/18
ax.yaxis.set_major_formatter(yfmt)
ax.set_xlim(min(xvol)-dxx,max(xvol)+dxx)
ax.set_ylim(ymin-dyy,ymax+dyy)
ax.xaxis.set_major_locator(MaxNLocator(7))
plt.savefig('PLOT.ps', orientation='portrait',format='eps')
plt.savefig('PLOT.png',orientation='portrait',format='png',dpi=dpipng)
#-------------------------------------------------------------------------------
print
print "##############################################\n"
if (len(dmin) > 1):
print "WARNING: Multiple minima are found!\n"
print "##############################################\n"
fmt='%17.6f'
afmt='%12.6f'
for i in range(len(dmin)):
v0=dmin[len(dmin)-1-i]
a0=(factor*v0)**(0.33333333333)
b0=bulk(v0)*v0*unitconv
print 'Optimal volume = ', fmt%(v0), '[Bohr^3]'
if (isym > 0): print 'Lattice constant =', slabel, afmt%(a0), '[Bohr]'
print 'Bulk modulus = ', fmt%(b0), '[GPa]'
print
if ( len(dmin) == 0): print "WARNING: No minimum in the given xrange!\n"
print "##############################################\n"
if (showpyplot): plt.show()
#-------------------------------------------------------------------------------