-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPC_SAFT_v2.py
507 lines (386 loc) · 14.1 KB
/
PC_SAFT_v2.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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
import numpy as np
import numdifftools as nd
from scipy.optimize import root, least_squares
from scipy.misc import derivative
from gekko import GEKKO
import pyomo.environ as pyo
class PCSAFT_v2:
a_ni = np.array([[0.9105631445, -0.3084016918, -0.0906148351],
[0.6361281449, 0.1860531159, 0.4527842806],
[2.6861347891, -2.5030047259, 0.5962700728],
[-26.547362491, 21.419793629, -1.7241829131],
[97.759208784, -65.255885330, -4.1302112531],
[-159.59154087, 83.318680481, 13.776631870],
[91.297774084, -33.746922930, -8.6728470368]])
a_ni = a_ni.T
b_ni = np.array([[0.7240946941, -0.5755498075, 0.0976883116],
[2.2382791861, 0.6995095521, -0.2557574982],
[-4.0025849485, 3.8925673390, -9.1558561530],
[-21.003576815, -17.215471648, 20.642075974],
[26.855641363, 192.67226447, -38.804430052],
[206.55133841, -161.82646165, 93.626774077],
[-355.60235612, -165.20769346, -29.666905585]])
b_ni = b_ni.T
kb = 1.380649e-23 # J/K
N_A = 6.0221e23 # 1/mol
R = 8.314 # J/mol-K
π = np.pi
def __init__(self, T, z, prop_dic, phase='liquid', η=None, P_sys=None):
m = prop_dic['m']
σ = prop_dic['s']
ϵ_k = prop_dic['e']
κ_AB = prop_dic['vol_a']
ϵ_AB_k = prop_dic['e_assoc']
k_ij = prop_dic['k_ij']
# Parameters
self.T = T
self.T_og = T
self.z = z
self.z_og = z
self.m = m
self.k = len(σ)
self.σ = σ
self.ϵ_k = ϵ_k
self.κ_AB = κ_AB
self.ϵ_AB_k = ϵ_AB_k
self.phase = phase
self.k = len(z)
self.η_diff = False
self.T_diff = False
self.x_diff = False
self.d_static = σ * (1 - .12 * np.exp(-3 * ϵ_k / T))
k = self.k
if κ_AB is None:
κ_AB = np.zeros(k)
if ϵ_AB_k is None:
ϵ_AB_k = np.zeros(k)
# --------------------------------------- Intermediates --------------------------------------- #
self.σ_ij = np.array([[1 / 2 * (σ[i] + σ[j]) for j in range(k)] for i in range(k)])
self.ϵ_ij = np.array([[(ϵ_k[i] * ϵ_k[j]) ** (1 / 2) * (1 - k_ij[i][j]) for j in range(k)] for i in range(k)])
self.κ_AB_ij = np.array(
[[(κ_AB[i] * κ_AB[j]) ** (1 / 2) * ((σ[i] * σ[j]) / (1 / 2 * (σ[i] * σ[j]))) ** 3 for j in range(k)] for i
in range(k)])
self.ϵ_AB_ij = np.array([[(ϵ_AB_k[i] + ϵ_AB_k[j]) / 2 for j in range(k)] for i in range(k)])
if P_sys is not None:
self.P_sys = P_sys
self.find_η()
elif P_sys is None and η is not None:
self.η = η
elif P_sys is None and η is None:
if phase == 'liquid':
self.η = .4
elif phase == 'vapor':
self.η = .01
print(
'Warning, a default η had to be defined based on the given phase since so system pressure was given to iteratively find η')
def m_bar(self):
z = self.z
m = self.m
return sum(z * m)
def d(self):
T = self.T
σ = self.σ
ϵ_k = self.ϵ_k
return σ * (1 - .12 * np.exp(-3 * ϵ_k / T))
def d_og(self):
T = self.T_og
σ = self.σ
ϵ_k = self.ϵ_k
return σ * (1 - .12 * np.exp(-3 * ϵ_k / T))
def ρ(self):
z = self.z_og
η = self.η
d = self.d_static
m = self.m
k = self.k
return 6 / self.π * η * (sum([z[i] * m[i] * d[i] ** 3 for i in range(k)])) ** (-1)
def v(self):
ρ = self.ρ()
return self.N_A * 10 ** -30 / ρ
def ξ(self):
z = self.z
d = self.d()
ρ = self.ρ()
m = self.m
return np.array([self.π / 6 * ρ * np.sum([z[i] * m[i] * d[i] ** n for i in range(self.k)]) for n in range(4)])
def g_hs_ij(self):
d = self.d()
ξ = self.ξ()
return np.array([[(1 / (1 - ξ[3])) +
((d[i] * d[j] / (d[i] + d[j])) * 3 * ξ[2] / (1 - ξ[3]) ** 2) +
((d[i] * d[j] / (d[i] + d[j])) ** 2 * 2 * ξ[2] ** 2 / (1 - ξ[3]) ** 3)
for j in range(self.k)]
for i in range(self.k)])
def d_ij(self):
d = self.d()
return np.array([[1 / 2 * (d[i] + d[j]) for j in range(self.k)] for i in range(self.k)])
def Δ_AB_ij(self):
T = self.T
d_ij = self.d_ij()
g_hs_ij = self.g_hs_ij()
return np.array([[d_ij[i][j] ** 3 * g_hs_ij[i][j] * self.κ_AB_ij[i][j] * (np.exp(self.ϵ_AB_ij[i][j] / T) - 1)
for j in range(self.k)] for i in range(self.k)])
def a_hs(self):
ξ = self.ξ()
return 1 / ξ[0] * (3 * ξ[1] * ξ[2] / (1 - ξ[3]) + ξ[2] ** 3 / (ξ[3] * (1 - ξ[3]) ** 2) + (
ξ[2] ** 3 / ξ[3] ** 2 - ξ[0]) * np.log(1 - ξ[3]))
def a_hc(self):
z = self.z
k = self.k
m = self.m
m_bar = self.m_bar()
g_hs_ij = self.g_hs_ij()
a_hs = self.a_hs()
return m_bar * a_hs - sum([z[i] * (m[i] - 1) * np.log(g_hs_ij[i][i]) for i in range(k)])
def a_disp(self):
T = self.T
z = self.z
η = self.ξ()[-1]
a_ni = self.a_ni
b_ni = self.b_ni
π = self.π
k = self.k
ρ = self.ρ()
m = self.m
m̄ = self.m_bar()
ϵ_ij = self.ϵ_ij
σ_ij = self.σ_ij
a = a_ni[0] + (m̄ - 1) / m̄ * a_ni[1] + (m̄ - 1) / m̄ * (m̄ - 2) / m̄ * a_ni[2]
b = b_ni[0] + (m̄ - 1) / m̄ * b_ni[1] + (m̄ - 1) / m̄ * (m̄ - 2) / m̄ * b_ni[2]
I1 = [a[i] * η ** i for i in range(7)]
I2 = [b[i] * η ** i for i in range(7)]
I1 = np.sum(I1)
I2 = np.sum(I2)
Σ_i = 0
for i in range(k):
Σ_j = 0
for j in range(k):
Σ_j += z[i] * z[j] * m[i] * m[j] * (ϵ_ij[i][j] / T) * σ_ij[i][j] ** 3
Σ_i += Σ_j
Σ_1 = Σ_i
Σ_i = 0
for i in range(k):
Σ_j = 0
for j in range(k):
Σ_j += z[i] * z[j] * m[i] * m[j] * (ϵ_ij[i][j] / T) ** 2 * σ_ij[i][j] ** 3
Σ_i += Σ_j
Σ_2 = Σ_i
C1 = (1 + m̄ * (8 * η - 2 * η ** 2) / (1 - η) ** 4 + (1 - m̄) * (
20 * η - 27 * η ** 2 + 12 * η ** 3 - 2 * η ** 4) / ((1 - η) * (2 - η)) ** 2) ** -1
return -2 * π * ρ * I1 * Σ_1 - π * ρ * m̄ * C1 * I2 * Σ_2
def a_assoc(self):
z = self.z
k = self.k
ρ = self.ρ()
ϵ_AB_k = self.ϵ_AB_k
Δ_AB_ij = self.Δ_AB_ij()
def XA_find(XA_guess, n, Δ_AB_ij, ρ, z):
# m = int(XA_guess.shape[1] / n)
AB_matrix = np.asarray([[0., 1.],
[1., 0.]])
Σ_2 = np.zeros((n,), dtype='float_')
XA = np.zeros_like(XA_guess)
for i in range(n):
Σ_2 = 0 * Σ_2
for j in range(n):
Σ_2 += z[j] * (XA_guess[j, :] @ (Δ_AB_ij[i][j] * AB_matrix))
XA[i, :] = 1 / (1 + ρ * Σ_2)
return XA
a_sites = 2 # 2B association?
i_assoc = []
for i in range(len(ϵ_AB_k)):
if ϵ_AB_k[i] != 0:
i_assoc.append(i)
z_new = []
for i in i_assoc:
z_new.append(z[i])
n_assoc = len(i_assoc)
if n_assoc == 0 or n_assoc == 1:
return 0
XA = np.zeros((n_assoc, a_sites), dtype='float_')
ctr = 0
dif = 1000.
XA_old = np.copy(XA)
while (ctr < 500) and (dif > 1e-9):
ctr += 1
XA = XA_find(XA, n_assoc, Δ_AB_ij, ρ, z_new)
dif = np.sum(abs(XA - XA_old))
XA_old[:] = XA
XA = XA.flatten()
return sum([z[i] * sum([np.log(XA[j] - 1 / 2 * XA[j] + 1 / 2) for j in range(len(XA))]) for i in range(k)])
def a_ion(self):
return 0
def a_res(self):
a_hc = self.a_hc()
a_disp = self.a_disp()
a_assoc = self.a_assoc()
a_ion = self.a_ion()
return a_hc + a_disp + a_assoc + a_ion
def da_dη(self):
η = self.η
self.η_og = self.η
def f(η_diff):
self.η = η_diff
return self.a_res()
self.η = self.η_og
return derivative(f, η, dx=1e-3)
def da_dx(self):
z = self.z
self.z_og = self.z
da_dx = []
for k in range(len(z)):
def f(zk):
z_new = []
for i in range(len(z)):
if i == k:
z_new.append(zk)
else:
z_new.append(z[i])
self.z = z_new
return self.a_res()
self.z = self.z_og
da_dx.append(derivative(f, z[k], dx=1e-3))
self.z = self.z_og
return np.array(da_dx)
def da_dT(self):
T = self.T
self.T_og = self.T
def f(T_diff):
self.T = T_diff
return self.a_res()
self.T = self.T_og
return derivative(f, T, dx=1e-3)
def Z(self):
η = self.η
da_dη = self.da_dη()
self.η = self.η_og
return 1 + η * da_dη
def P(self):
T = self.T
Z = self.Z()
ρ = self.ρ()
kb = self.kb
return Z * kb * T * ρ * 10 ** 30
def find_η(self):
def f(ηg):
self.η = float(ηg)
P = self.P()
P_sys = self.P_sys
return (P - P_sys) / 100000
phase = self.phase
if phase == 'liquid':
ηg = .5
elif phase == 'vapor':
ηg = 10e-10
else:
print('Phase spelling probably wrong or phase is missing')
ηg = .01
η = root(f, np.array([ηg])).x[0]
self.η = η
def h_res(self):
T = self.T
self.T_og = self.T
Z = self.Z()
da_dT = self.da_dT()
self.T = self.T_og
return -T * da_dT + (Z - 1)
def s_res(self):
T = self.T
self.T_og = self.T
a_res = self.a_res()
Z = self.Z()
da_dT = self.da_dT()
self.T = self.T_og
return -T * (da_dT + a_res / T) + np.log(Z)
def g_res(self):
a_res = self.a_res()
Z = self.Z()
return a_res + (Z - 1) - np.log(Z)
def μ_res(self):
z = self.z
T = self.T
a_res = self.a_res()
Z = self.Z()
da_dx = self.da_dx()
Σ = np.sum([z[j] * da_dx[j] for j in range(len(z))])
μ_res = [(a_res + (Z - 1) + da_dx[i] - Σ) * self.kb * T for i in range(len(z))]
return np.array(μ_res)
def φ(self):
T = self.T
μ_res = self.μ_res()
Z = self.Z()
return np.exp(μ_res / self.kb / T - np.log(Z))
def flash_v2(x, y, T, P, prop_dic, flash_type='Bubble_T'):
def eqs_to_solve(x, y, T, P):
# print(κ_AB)
mix_l = PCSAFT_v2(T, x, prop_dic, phase='liquid', P_sys=P)
mix_v = PCSAFT_v2(T, y, prop_dic, phase='vapor', P_sys=P)
φ_l = mix_l.φ()
φ_v = mix_v.φ()
# print(mix_l.a_assoc())
# print(mix_l.a_res(), mix_l.a_hc(), mix_l.a_disp(), mix_l.a_assoc(), mix_l.a_ion())
# print(mix_l.a_res(), mix_l.a_hc() + mix_l.a_disp(), mix_l.a_hc() + mix_l.a_disp() + mix_l.a_assoc())
eqs = [(y[i] * φ_v[i] - x[i] * φ_l[i]) for i in range(len(y))]
if flash_type[:-2] == 'Bubble':
eqs.append(1 - np.sum([y[i] for i in range(len(y))]))
elif flash_type[:-2] == 'Dew':
eqs.append(1 - np.sum([x[i] for i in range(len(x))]))
else:
print('Wrong Flash Type')
return eqs
def f(w):
if flash_type == 'Bubble_T':
return eqs_to_solve(x, w[:-1], T, w[-1])
elif flash_type == 'Bubble_P':
return eqs_to_solve(x, w[:-1], w[-1], P)
elif flash_type == 'Dew_T':
return eqs_to_solve(w[:-1], y, T, w[-1])
elif flash_type == 'Dew_P':
return eqs_to_solve(w[:-1], y, w[-1], P)
else:
print('Wrong Flash Type')
return None
if flash_type == 'Bubble_T':
guesses = list(y) + [P]
elif flash_type == 'Bubble_P':
guesses = list(y) + [T]
elif flash_type == 'Dew_T':
guesses = list(x) + [P]
elif flash_type == 'Dew_P':
guesses = list(x) + [T]
else:
print('Wrong Flash Type')
guesses = []
options = {'xtol': 1e-4, }
ans = root(f, np.array([guesses]), options=options).x
y_CO2, y_MEA, y_H2O = ans[:-1]
P = ans[-1]
P_CO2 = y_CO2*P
# mix_l = PCSAFT(T, x, m, σ, ϵ_k, k_ij, phase='liquid', P_sys=P, κ_AB=κ_AB, ϵ_AB_k=ϵ_AB_k)
# mix_v = PCSAFT(T, y, m, σ, ϵ_k, k_ij, phase='vapor', P_sys=P, κ_AB=κ_AB, ϵ_AB_k=ϵ_AB_k)
return P_CO2
if __name__ == '__main__':
x = [.1, .3, .6]
T = 233.15
yg = [.1, .3, .6]
Pg = 1e5
prop_dic = {
'CO2': {'m_seg': 1, 'sigma': 3.7039, 'u_K': 150.03,
'kappa_AB': 0,
'eps_AB_k': 0},
'MEA': {'m_seg': 1.6069, 'sigma': 3.5206, 'u_K': 191.42,
'kappa_AB': 0, # .037470,
'eps_AB_k': 0, # 2586.3,
},
'H2O': {'m_seg': 2.0020, 'sigma': 3.6184, 'u_K': 208.11,
'kappa_AB': 0, # .04509,
'eps_AB_k': 0, # 2425.67
},
}
m = np.array([1, 1.6069, 2.0020]) # Number of segments
σ = np.array([3.7039, 3.5206, 3.6184]) # Temperature-Independent segment diameter σ_i (Aᵒ)
ϵ_k = np.array([150.03, 191.42, 208.11]) # Depth of pair potential / Boltzmann constant (K)
k_ij = np.array([[0.00E+00, 3.00E-04, 1.15E-02],
[3.00E-04, 0.00E+00, 5.10E-03],
[1.15E-02, 5.10E-03, 0.00E+00]])
print(flash(x, yg, T, Pg, prop_dic, k_ij))