forked from tdengg/elements
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfitev.py
367 lines (324 loc) · 14.4 KB
/
fitev.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
"""Fit energy-volume data with Birch-Murnaghan equation of states
contains: class::Birch()
def::minIn
def::fitev
def::minimization
def::minimization2
arguments: -param ........ parameters of calculation (eg. ngridk, swidth, scale ...)
type::dictionary
-calcnr ....... calculation number
type::string
-structure .... structure of unit cell
type::string
-verbose ...... verbosity of output (default = False)
type::boolean
returns: -----
output: -self.out0 .... equilibrium volume [Bohr^3]
-self.out1 .... bulk-modulus at equilibrium [GPa]
-self.out2 .... derivative of bulk-modulus at equilibrium
-self.out3 .... minimum of total energy
"""
import numpy.linalg as linalg
import numpy as np
try:
import matplotlib.pyplot as plt
mpl = True
except:
mpl = False
import random
import xml.etree.ElementTree as etree
import convert_latt_vol
class Birch(object):
def __init__(self, structure, *args):
verbose = False
self.verbose = verbose
a = []
ein = []
v = []
diff = []
vbad = []
ebad = []
## start parameters:
b0 = np.float32(0.004) # Bulk-Modulus
db0 = np.float32(3.) # derivative of Bulk-Modulus with respect to V
itmax = 500
epsilon = 0.00007
grad = -1
#read volune and energy
if len(args) == 0:
self.path = param['eospath']
readev = open(param['eospath'] + 'ev', 'r')
for line in readev:
la, enin = line.split()
a.append(float(la))
ein.append(float(enin))
readev.close()
conv = convert_latt_vol.Convert(structure)
l, v = conv.lattToVolume(param, a)
else:
l = args[0]
covera = args[1]
v = args[2]
ein = args[3]
calchome = args[4]
a=l
#check and remove points that do not seam to have reasonable energy values
'''
poly = []
res = []
devsq = []
rm = []
deg = 2
nstep = int(len(v)/(deg+1))
span = nstep*(deg+1)
remaining = len(v)-nstep*(deg+1)
for j in range(nstep):
vpoly = []
epoly = []
for i in range(deg+1):
vpoly.append(v[nstep*i+j])
epoly.append(ein[nstep*i+j])
coeff = np.polyfit(vpoly, epoly, deg)
poly.append(np.poly1d(coeff))
j=0
for p in poly:
ressq = 0
devsq.append([])
for i in range(len(v)):
ressq = ressq + (p(v[i])-ein[i])**2.
devsq[j].append((p(v[i])-ein[i])**2.)
res.append(ressq)
j=j+1
ind = res.index(min(res))
for i in range(len(v)):
if devsq[ind][i]*(len(v)) > 5*res[ind]:
rm.append(v[i])
#print devsq[ind][i]*(len(v)),res[ind]
for valv in rm:
index = v.index(valv)
vbad.append(valv)
ebad.append(ein[index])
v.remove(valv)
del ein[index],l[index]
x = np.linspace(min(v),max(v),100)
'''
#############
v0, emin = self.minIn(ein,v)
arec = np.array([[float(v0), float(b0), float(db0), float(emin)]])
norm_diff1 = linalg.norm(arec)
ressq0, fite0, res0 = (self.fitev(arec, v, ein))
i=0
while norm_diff1 > epsilon:
if i<itmax:# aad < 0:
diff0, parnew0 = self.minimization(arec, v, ein)
ressq, fite, res = self.fitev(parnew0, v, ein)
diff1, parnew1 = self.minimization(parnew0, v, ein)
ressq, fite, res = self.fitev(parnew1, v, ein)
arec = parnew1
norm_diff0 = linalg.norm(diff0)
norm_diff1 = linalg.norm(diff1)
grad = norm_diff1 - norm_diff0
#print norm_diff1
else:
break
i=i+1
parmin, deltamin = self.minimization2(parnew1, v, ein)
#print deltamin
if deltamin < norm_diff1:
parnew1 = parmin
print '2-Norm of residual vector: ' + str(deltamin)
else:
deltamin = norm_diff1
print '2-Norm of residual vector: ' + str(deltamin)
# convert volume to lattice parameter:
if structure == 'fcc':
latt = (4. * parnew1[0,0])**(1./3.)
elif structure == 'bcc':
latt = (4. * parnew1[0,0])**(1./3.)
#elif structure == 'hcp' and len(args) == 0:
#covera = input('c over a ratio: ')
#latta = (parnew1[0,0]/(param['covera']*0.866))**(1./3.)
#lattc = latta * param['covera']
"""if verbose == True:
print('---------------------------------------')
print('volume: ' + str(parnew1[0,0]) + ' Bohr')
print('---------------------------------------')
if structure == 'fcc' or structure == 'bcc':
print('equilibrium lattice parameter: ' + str(latt*0.529177) + ' Angstroem')
print(' ' + str(latt) + ' Bohr')
elif structure == 'hcp':
print('equilibrium lattice parameter a: ' + str(latta*0.529177) + ' Angstroem')
print(' ' + str(latta) + ' Bohr')
print(' c: ' + str(lattc*0.529177) + ' Angstroem')
print(' ' + str(lattc) + ' Bohr')
print('---------------------------------------')
print('Bulk-Modulus: ' + str(parnew1[0,1]) + ' au.')
print(' ' + str(parnew1[0,1]*2.942104*10**4.) + ' GPa')
print('---------------------------------------')
print('derivative of Bulk-Modulus: ' + str(parnew1[0,2]))
print('---------------------------------------')
print('minimal energy: ' + str(parnew1[0,3]) + ' Hartree')
print('---------------------------------------')"""
#else:
#polyfit of volue steps to determine (c/a)min:
if len(v) == len(covera):
coeff = np.polyfit(v, covera, 3)
poly = np.poly1d(coeff)
dpoly = np.poly1d.deriv(poly)
ddpoly = np.poly1d.deriv(dpoly)
coamin = poly(parnew1[0,0])
plt.plot(covera,v,coamin,parnew1[0,0])
#print coamin
#for vol in v:
if structure == 'fcc':
a0 = (4.*parnew1[0,0])**(1./3.)
print(('V0: ' + str(round(parnew1[0,0], 4))) + ('a0: ' + str(round(a0, 4)).rjust(16)+ ('B0: ' + str(round(parnew1[0,1]*2.942104*10**4., 4))).rjust(16) + ("B0': " + str(round(parnew1[0,2],4))).rjust(16) + ('E0: ' + str(round(parnew1[0,3], 4))).rjust(16)))
elif structure == 'hcp':
a0 = (2.*parnew1[0,0]/(3.**(1./2.)*float(coamin)))**(1./3.)
print(('V0: ' + str(round(parnew1[0,0], 4))) + ('a0: ' + str(a0).rjust(16)+ ('B0: ' + str(round(parnew1[0,1]*2.942104*10**4., 4))).rjust(16) + ("B0': " + str(round(parnew1[0,2],4))).rjust(16) + ('E0: ' + str(round(parnew1[0,3], 4))).rjust(16)))
#elif structure == 'hex':
# print(('V0: ' + str(round(parnew1[0,0], 4))) + ('a0: ' + str((2.*parnew1[0,0]/(3.**(1./2.)*float(covera[i])))**(1./3.)).rjust(16)+ ('B0: ' + str(round(parnew1[0,1]*2.942104*10**4., 4))).rjust(16) + ("B0': " + str(round(parnew1[0,2],4))).rjust(16) + ('E0: ' + str(round(parnew1[0,3], 4))).rjust(16)))
elif structure == 'bcc':
a0 = (2.*parnew1[0,0])**(1./3.)
print(('V0: ' + str(round(parnew1[0,0], 4))) + ('a0: ' + str(round((2*parnew1[0,0])**(1./3.), 4))).rjust(16)+ ('B0: ' + str(round(parnew1[0,1]*2.942104*10**4., 4))).rjust(16) + ("B0': " + str(round(parnew1[0,2],4))).rjust(16) + ('E0: ' + str(round(parnew1[0,3], 4))).rjust(16))
elif structure == 'diamond':
a0 = (8.*parnew1[0,0])**(1./3.)
print(('V0: ' + str(round(parnew1[0,0], 4))) + ('a0: ' + str(round((8*parnew1[0,0])**(1./3.), 4))).rjust(16)+ ('B0: ' + str(round(parnew1[0,1]*2.942104*10**4., 4))).rjust(16) + ("B0': " + str(round(parnew1[0,2],4))).rjust(16) + ('E0: ' + str(round(parnew1[0,3], 4))).rjust(16))
#plt.plot(v, fite0)#
lv = np.linspace(min(v),max(v),100)
dump, plote, dump = (self.fitev(parnew1, lv, ein))
#print lv, plote
if mpl:
plt.cla()
plt.plot(lv, plote, '')
plt.plot(v, ein, '.')
plt.xlabel(r'$volume$ $[{Bohr^3}]$')
plt.ylabel(r'$total$ $energy$ $[{Hartree}]$')
plt.legend(loc='best')
self.p = plt
#plt.savefig(calchome + 'eos.png')
#plt.show()
#results = etree.Element('plot')
###############reschild.set(,str(convpar[key]))##############
self.reschild = etree.Element('graph')
for i in range(len(lv)):
point = etree.SubElement(self.reschild, 'point')
point.set('volume',str(lv[i]))
point.set('energy',str(plote[i]))
self.reschild.set('energy_min',str(parnew1[0,3]))
try:
self.reschild.set('coa_min',str(coamin))
except:
print ''
self.reschild.set('vol_min',str(parnew1[0,0]))
self.reschild.set('B0',str(parnew1[0,1]*2.942104*10**4.))
self.reschild.set('dB0',str(parnew1[0,2]))
self.reschild2 = etree.Element('graph_exp')
for i in range(len(v)):
point2 = etree.SubElement(self.reschild2, 'point')
point2.set('volume',str(v[i]))
point2.set('energy',str(ein[i]))
self.reschild3 = etree.Element('graph_exp_bad')
for i in range(len(vbad)):
point3 = etree.SubElement(self.reschild3, 'point')
point3.set('volume',str(vbad[i]))
point3.set('energy',str(ebad[i]))
#results.append(reschild2)
#restree = etree.ElementTree(results)
#restree.write(calchome + 'eosplot.xml')
#############################################################
#plt.show()
self.out0 = parnew1[0,0]
self.out1 = parnew1[0,1]*2.942104*10**4. #bulk modulus in GPa
self.out2 = parnew1[0,2]
self.out3 = parnew1[0,3]
if len(v) == len(covera):
self.out4 = coamin
self.out5 = a0
self.deltamin = deltamin
self.a = a
self.v = v
self.ein = ein
def minIn(self, ein, vin):
""" find minimum of total energy
"""
emin = min(ein)
indexv = ein.index(emin)
v0 = np.float32(vin[indexv])
return v0, emin
def fitev(self, par, v, ein):
fite = []
deltasq = []
res = []
v0 = par[0,0]
b0 = par[0,1]
db0 = par[0,2]
emin = par[0,3]
i=0
while i < len(v):
vov = (v0/v[i])**(2./3.)
fite.append(float(emin + 9. * v0 * b0/16. * ((vov - 1.)**3. * db0 + (vov - 1.)**2. * (6. - 4. * vov))))
if len(v) == len(ein):
deltasq.append((fite[i] - ein[i])**2.)
res.append(fite[i] - ein[i])
#print (emin - ein[i])**2
i = i+1
return deltasq, fite, res
def minimization(self, aold, v, ein):
i=0
defit_dV = []
defit_dB = []
defit_ddB = []
defit_demin = []
jacobian = []
v0 = aold[0,0]
b0 = aold[0,1]
db0 = aold[0,2]
emin = aold[0,3]
while i < len(v):
## Jacobian:
# derivative of efit with respect to V0
a = 3. * db0 * ((v0 / v[i]**(2./3.) - v0**(1./3.) )**2. * (1. / v[i]**(2./3.) - 1./3. * v0**(-2./3.)))
b = 2. * ((v0**(7./6.) / v[i]**(2./3.) - v0**(1./2.)) * (7./6. * v0**(1./6.) / v[i]**(2./3.) - 0.5 * v0 ** (-1./2.)) * (6. - 4. * (v0/v[i])**(2./3.)))
c = (v0**(7./6.) / v[i]**(2./3.) - v0**(1./2.))**2. * (-8./3. * v0**(-1./3.) / v[i]**(2./3.))
defit_dV.append((-9.)/16.* b0 * (a + b + c))
# derivative of efit with respect to B0
vov = (v0/v[i])**(2./3.)
defit_dB.append((-9.) * v0 / 16. * ((vov - 1.)**3. * db0 + (vov - 1.)**2. * (6. - 4. * vov)))
# derivative of efit with respect to dB0
defit_ddB.append((-9.) / 16. * b0 * (v0 / v[i]**(2./3.) - v0**(1./3.))**3.)
# derivative of efit with respect to emin
defit_demin.append(-1)
jacobian.append([defit_dV[i],defit_dB[i],defit_ddB[i],defit_demin[i]])
##
## residuals:
ressq, fite, res = self.fitev(aold, v, ein)
i = i+1
A = np.matrix(jacobian)
r = np.array(res)
B = np.dot(np.transpose(A),A)
C = np.dot(np.transpose(A),(r))
delta = np.transpose(linalg.solve(B,np.transpose(C)))
anew = aold + 0.1*delta
return res, anew
def minimization2(self, aold, v, ein):
delta = []
anew = np.array([[0.,0.,0.,0.]])
i=0
while i < 50:
anew[0,0] = aold[0,0] * (random.uniform(-1,1) * 0.00000001 + 1.)
anew[0,1] = aold[0,1] * (random.uniform(-1,1) * 0.00000001 + 1.)
anew[0,2] = aold[0,2] * (random.uniform(-1,1) * 0.00000001 + 1.)
anew[0,3] = aold[0,3] * (random.uniform(-1,1) * 0.00000001 + 1.)
ressq, fite, res = self.fitev(anew, v, ein)
delta.append(linalg.norm(res))
if delta[i]<delta[i-1]:
deltamin = delta[i]
else:
amin = aold
deltamin = delta[0]
amin = anew
i+=1
return amin, deltamin
#a = Birch("/home/tom/test/",'calc1','fcc')
#print a.out0