-
Notifications
You must be signed in to change notification settings - Fork 6
/
ElaStic_Analyze_B0
executable file
·205 lines (175 loc) · 7.1 KB
/
ElaStic_Analyze_B0
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
#!/usr/bin/env python
#%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%#
#%!%!% --------------------------------- ElaStic_Analyze_B0 -------------------------------- %!%!%#
#%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%#
#
# AUTHORS:
# Rostam Golesorkhtabar and Pasquale Pavone
# r.golesorkhtabar@gmail.com
#
# DATE:
# Sun Jan 01 00:00:00 2012
#
# SYNTAX:
# python ElaStic_Analyze_B0.py
# ElaStic_Analyze_B0
#
# EXPLANATION:
# The parameters in equation of states:
# --- Murnaghan ---
# E(V) = E0 + B0*V/B' * [(V0/V)^B' / (B'-1) + 1] - B0 V0 / (B'-1)
# P(V) = B0/B'*((V0/V)**B' -1)
#
# --- Birch-Murnaghan ---
# E(V) = E0 + 9B0V0/16{[(V0/V)^(2/3)-1]^3 B' + [(V0/V)^(2/3)-1]^2 [6-4(V0/V)^(2/3)]}
# P(V) = 3/2*B0*((V0/V)**(7/3) - (V0/V)**(5/3))*(1 + 3/4*(B'-4)*[(V0/V)**(2/3) - 1])
#
# E0: Energy in equilibrium
# V0: Volume in equilibrium
# B0: Bulk modulus in equilibrium
# B': Pressure derivative of B
#
# See also: http://en.wikipedia.org/wiki/Birch-Murnaghan_equation_of_state
#__________________________________________________________________________________________________
import os
import sys
import copy
import scipy
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import fmin_powell
#%!%!%--- CONSTANTS ---%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!
_e = 1.602176565e-19 # elementary charge
Bohr = 5.291772086e-11 # Bohr to meter
Ryd2eV= 13.605698066 # Ryd to eV
Ha2Ry = 2. # Ha to Ryd
ToGPa = (_e*Ryd2eV)/(1e9*Bohr**3)# Ryd/[Bohr]^3 to GPa
#__________________________________________________________________________________________________
#%!%!%--- SUBROUTINS AND FUNCTIONS ---%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%
def E_eos(p0, V):
if (eos=='M'):
""" Murnaghan Energy"""
E0, V0, B0, Bp = p0
E = E0 + (B0*V/Bp*(1/(Bp-1)*(V0/V)**Bp +1)-B0*V0/(Bp-1))
else:
""" Birch-Murnaghan Energy"""
E0, V0, B0, Bp = p0
E = E0 + (9.*B0*V0/16)*(((((V0/V)**(2./3))-1.)**3.)*Bp \
+ ((((V0/V)**(2/3.))-1.)**2.)*(6.-4.*((V0/V)**(2./3.))))
return E
#--------------------------------------------------------------------------------------------------
def snr(p0, v, e):
""" Squared norm of residue vector calculation """
return np.sum((e - E_eos(p0, v))**2.)
#--------------------------------------------------------------------------------------------------
def sortarray(ary1, ary2, ary3):
lst1 = ary1.tolist()
lst2 = ary2.tolist()
lst3 = ary3.tolist()
lst4 = []
lst5 = []
lst6 = []
temp = copy.copy(lst1)
temp.sort()
for i in range(len(lst1)):
lst4.append(lst1[lst1.index(temp[i])])
lst5.append(lst2[lst1.index(temp[i])])
lst6.append(lst3[lst1.index(temp[i])])
ary4 = scipy.array(lst4)
ary5 = scipy.array(lst5)
ary6 = scipy.array(lst6)
return ary4, ary5, ary6
#--------------------------------------------------------------------------------------------------
#%!%!%--- INPUT FILE READING ---%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%
print'\
\n +-----------------------------------------------------------------+\
\n |*****************************************************************|\
\n |* *|\
\n |* WELCOME TO THE "ElaStic_Analyze_B0" PROGRAM *|\
\n |* ElaStic Version 1.0.0, Release Date: 2012-01-01 *|\
\n |* *|\
\n |*****************************************************************|\
\n +-----------------------------------------------------------------+'
INF = input('\n>>>> Enter "strain volume energy" file name: ')
if (os.path.exists(INF)==False):
sys.exit('\n.... Oops ERROR: There is NO '+ INF +' file !?!?!? \n')
si, vi, ei = np.loadtxt(INF).T
si, vi, ei = sortarray(si, vi, ei)
if (len(ei) < 3): sys.exit('\n.... Oops ERROR: EOS fit needs at least 3 points. \n')
#%!%!%--- DAIMENTION READING ---%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%
print( '\
\n Please specify the "volume unit".\
\n [Bohr^3] ---=> 1 \
\n [Ang.^3] ---=> 2 ')
num = int(input(">>>> Choose '1' or '2': "))
if (num != 1 and num != 2 ): sys.exit("\n.... Oops ERROR: Choose '1' or '2' \n")
if (num == 1): v_unit = 'Bohr^3'; v2Bhr= 1.
if (num == 2): v_unit = 'Ang.^3'; v2Bhr= (Bohr*1e+10)**-3.
print( '\
\n Please specify the "energy unit".\
\n Rydberg -------=> 1 \
\n Hartree -------=> 2 \
\n electron Volt --=> 3 ')
num = int(input(">>>> Choose '1', '2', or '3': "))
if (num != 1 and num != 2 and num != 3 ):
sys.exit("\n.... Oops ERROR: Choose '1', '2', or '3' \n")
if (num == 1): e_unit = 'Ryd'; e2Ryd= 1.
if (num == 2): e_unit = 'Ha' ; e2Ryd= Ha2Ry
if (num == 3): e_unit = 'eV' ; e2Ryd= 1./Ryd2eV
vi = vi*v2Bhr
ei = ei*e2Ryd
sii = copy.copy(si)
vii = copy.copy(vi)
eii = copy.copy(ei)
#--------------------------------------------------------------------------------------------------
#%!%!%--- BULK MODULUS CALCULATIONS ---%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!
BM_out = open('B0-vs-strain.dat', 'w')
Bp_out = open('Bp-vs-strain.dat', 'w')
print('# Max. eta Bulk modulus (GPa)\n#',file=BM_out)
print('# Max. eta dB0/dp \n#',file=Bp_out)
for eos in ['M', 'BM']:
if (eos == 'M' ):
print('\n# Murnaghan fit.',file=BM_out)
print('\n# Murnaghan fit.',file=Bp_out)
if (eos == 'BM'):
print('\n# Birch-Murnaghan fit.',file=BM_out)
print('\n# Birch-Murnaghan fit.',file=Bp_out)
si = copy.copy(sii)
vi = copy.copy(vii)
ei = copy.copy(eii)
while(len(si) > 3):
emax= max(si)
emin= min(si)
emax= max(abs(emin), abs(emax))
a2, a1, a0 = np.polyfit(vi, ei, 2)
V0 = -a1/(2.*a2)
E0 = a2*V0**2. + a1*V0 + a0
B0 = a2*V0
Bp = 2.0
p0 = [E0, V0, B0, Bp]
viei = sorted([zip(vi, ei)])
v, e = np.array(viei).T
p1, fopt, direc, n_iter, n_funcalls, warnflag = \
fmin_powell(snr, p0, args=(v, e), full_output=True)
E0, V0, B0, Bp = p1
B0_GPa = B0*ToGPa
print('%13.10f'%emax, '%18.6f'%B0_GPa,file=BM_out)
print('%13.10f'%emax, '%18.6f'%Bp,file=Bp_out)
si = si.tolist()
vi = vi.tolist()
ei = ei.tolist()
if (abs(si[0]+emax) < 1.e-7):
si.pop(0)
vi.pop(0)
ei.pop(0)
if (abs(si[len(si)-1]-emax) < 1.e-7):
si.pop()
vi.pop()
ei.pop()
si = scipy.array(si)
vi = scipy.array(vi)
ei = scipy.array(ei)
BM_out.close()
Bp_out.close()
#--------------------------------------------------------------------------------------------------
#%!%!%--- PLOT PREPARATION ---%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%!%