-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.py
95 lines (72 loc) · 2.47 KB
/
functions.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
import numpy as np
def uniquac_gamma(T, ind, x, a, q, r):
x = np.array(x)
a = np.array(a)
q = np.array(q)
r = np.array(r)
tau = np.exp(- a/T)
z = 10.0
l = (r - q) * (z/2) - (r - 1)
theta = (x * q) / np.sum(np.dot(x, q))
phi = (x * r) / np.sum(np.dot(x, r))
lngc = np.log(phi[ind] / x[ind]) + (z / 2) * q[ind] * np.log(theta[ind] / phi[ind]) \
+ l[ind] - (phi[ind] / x[ind]) * (np.sum(x * l))
lngr = q[ind] * (1 - np.log(np.sum(theta * tau[:, ind]))
- np.sum((theta * tau[ind, :]) / np.matmul(theta, tau)))
lng = lngc + lngr
return np.exp(lng)
def uniquac_delG_mix(T, x, a, q, r):
x = np.array(x)
gamma = np.empty(len(x))
for i in range(len(x)):
gamma[i] = uniquac_gamma(T, i, x, a, q, r)
R = 8.314472
delG_mix = R * T * (np.sum(x * np.log(x)) + np.sum(x * np.log(gamma)))
if delG_mix is np.nan:
delG_mix = np.inf
return delG_mix
def der_bin_uniquac_delG_mix(x, T, a, q, r):
# note that here x is the mole fraction of first compound only and not an array
h = 1e-7
return (uniquac_delG_mix(T,[x+h, 1-x-h], a, q, r) - uniquac_delG_mix(T,[x-h, 1-x+h], a, q, r)) / (2*h)
def der_uniquac_delG_mix(x, i, T, a, q, r):
# here input is array of x and i is the index along which the slope is required
h = 1e-7
xf = np.empty(len(x))
xb = np.empty(len(x))
for s in range(len(x)):
if s == i:
xf[s] = x[s] + h
xb[s] = x[s] - h
else:
xf[s] = x[s]
xb[s] = x[s]
return (uniquac_delG_mix(T, xf, a, q, r) - uniquac_delG_mix(T, xb, a, q, r)) / (2*h)
def flatten_array(a, asize, dsize):
f = np.empty(dsize * asize * (asize - 1))
k = 0
# commented code can be used when each a needs to be modified
# for m in range(dsize):
# for i in range(asize):
# for j in range(asize):
# if i != j:
# f[k] = a[m][i][j]
# k += 1
for i in range(asize):
for j in range(asize):
if i != j:
f[k] = a[i][j]
k += 1
return f
def revive_array(a, asize, dsize):
r = np.empty((dsize, asize, asize))
m = 0
for k in range(dsize):
for i in range(asize):
for j in range(asize):
if i == j:
r[k][i][j] = 0
else:
r[k][i][j] = a[m]
m += 1
return r