-
Notifications
You must be signed in to change notification settings - Fork 1
/
thermo.py
1347 lines (1245 loc) · 50.3 KB
/
thermo.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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import wheelpy.muc as muc
un = muc.uReg
R = muc.R
from scipy.optimize import fsolve, curve_fit, root
from scipy.integrate import quad
import numpy as np
class EOS:
"""
Initialize the object:
obj = EOS(kind, T, P, Tc, Pc, omega, pint=True)
Then, call:
obj.calc_Z()
Afterwards, other functions are available: (if root is an argument, pass "liq" or "vap")
obj.calc_V(root="all")
obj.calc_fugacity(root="vap")
obj.calc_residG(root="liq")
obj.calc_residSH()
"""
def __init__(self, kind, T, P, Tc, Pc, omega, pint=True):
"""
Cubic kinds: ["vdWg", "RK", "SRK", "PR"] (vdWg is generalized)
Other kinds: ["Pitzer", "vdWn", "LeeKesler"] (vdWn is normal form)
pint = True: sets whether to account for using pint module. If True, removes units before using fsolve.
If pint=False, uses SI units for R.
"""
self.kind = kind
self.pint = pint
self.P = P
self.T = T
self.Pc = Pc
self.Tc = Tc
self.omega = omega
# Used to avoid recalculating Z unnecessarily
self.calc_finished = False
self.Pr = P/Pc
self.Tr = T/Tc
if pint:
self.Pr.ito(un.dimensionless)
self.Tr.ito(un.dimensionless)
self.cubic = ["vdWg", "RK", "SRK", "PR"]
self.other = ["Pitzer", "LeeKesler"]
if kind in self.cubic:
dat = np.array([
# sig eps Omega Psi Zc
[0 , 0 , 1/8 , 27/64 , 3/8 ],
[1 , 0 , .08664, .42748, 1/3 ],
[1 , 0 , .08664, .42748, 1/3 ],
[1+np.sqrt(2), 1-np.sqrt(2), .07780, .45724, .30740],
])
alphas = [
lambda omega, Tr: 1,
lambda omega, Tr: np.sqrt(Tr),
lambda omega, Tr: (1 + (.480 + 1.574*omega - .176*omega**2)*(1- np.sqrt(Tr)))**2,
lambda omega, Tr: (1 + (.37464 + 1.54226*omega - .26992*omega**2)*(1- np.sqrt(Tr)))**2,
]
if kind == "vdWg":
self.cnst = dat[0]
self.alph = alphas[0]
elif kind == "RK":
self.cnst = dat[1]
self.alph = alphas[1]
elif kind == "SRK":
self.cnst = dat[2]
self.alph = alphas[2]
elif kind == "PR":
self.cnst = dat[3]
self.alph = alphas[3]
self.sig, self.eps, self.Omega, self.Psi, self.Zc = self.cnst
# TODO
elif kind in self.other:
if kind == "LeeKesler":
"nothing to see here"
#Plan to iterate!
elif kind == "Pitzer":
self.B0 = 0.083 - .422/(self.Tr**1.6)
self.B1 = 0.139 - .172/(self.Tr**4.2)
else:
raise ValueError("Passed an invalid kind of EOS.")
# Intermediate functions for cubics
@staticmethod
def calc_beta(Omega, Pr, Tr):
return Omega*Pr/Tr
# @staticmethod
# def calc_q(Psi, alpha, omega, Tr, Omega):
# # alpha is a callable, alpha(omega, Tr)
# return Psi * alpha(omega, Tr)/Omega/Tr
def calc_q(self):
# alpha is a callable, alpha(omega, Tr)
return self.Psi * self.alph(self.omega, self.Tr)/self.Omega/self.Tr
# This has been tested and proven for vdWg, and gives similar answers for other methods.
def calc_Z(self, guess = np.logspace(-5, 2, 7)):
"""
Takes no inputs.
Returns a list of solutions.
Uses values defined when EOS object is initialized.
For cubic EOS:
Runs a function solver on the EOS to compute Z
Uses a default of 7 guess values from 10^-5 to 10^2
For Pitzer:
Returns the result of the calculation.
For Lee-Kesler:
Uses Tr, Pr to prompt user for values from the table. If has been calculated already, does not repeat.
"""
if self.kind in self.cubic:
cnst = self.cnst
# Define two separate functions, depending on if pint is used. Nearly identical.
if self.pint:
def z_solve(Z):
beta = self.calc_beta(self.cnst[2], self.Pr.magnitude, self.Tr.magnitude)
q = self.calc_q()
r1 = 1 + beta - q*beta*(Z-beta)/(Z+cnst[1]*beta)/(Z+cnst[0]*beta) - Z
return r1.magnitude
else:
def z_solve(Z):
beta = self.calc_beta(self.cnst[2], self.Pr, self.Tr)
q = self.calc_q()
r1 = 1 + beta - q*beta*(Z-beta)/(Z+cnst[1]*beta)/(Z+cnst[0]*beta) - Z
return r1
Z_guess = guess
Z_check = [fsolve(z_solve, z_g)[0] for z_g in Z_guess]
Z_sol = []
for z in Z_check:
add = True
for zj in Z_sol:
if np.abs(z-zj) < .001 :
add = False
if z < 0:
add = False
if add:
Z_sol.append(z)
Z_sol = np.sort(Z_sol)
if len(Z_sol) == 3:
self.Z_liq = np.min(Z_sol)
self.Z_vap = np.max(Z_sol)
self.Z_sol = Z_sol
b = self.Omega*muc.R*self.Tc/self.Pc
self.b = b
self.q = self.calc_q()
elif self.kind == "Pitzer":
self.Z_sol = [1 + self.Pr/self.Tr * (self.B0 + self.omega*self.B1)]
elif self.kind == "LeeKesler":
if self.calc_finished:
return self.Z_sol
refTr = np.array([.30, .35, .40, .45, .50, .55, .60, .65, .70, .75, .80, .85, .90,
.93, .95, .97, .98, .99, 1.00, 1.01, 1.02, 1.05, 1.10, 1.15, 1.20,
1.30, 1.40, 1.50, 1.60, 1.70, 1.80, 1.90, 2.00, 2.20, 2.40, 2.60,
2.80, 3.00, 3.50, 4.00])
refPr = np.array([.0100, .0500, .1000, .2000, .4000, .6000, .8000, 1.0000,
1.2000, 1.5000, 2.0000, 3.0000, 5.0000, 7.0000, 10.0000])
iTr = 0
jPr = 0
if (self.Tr < refTr[0] or self.Tr > refTr[-1]):
raise ValueError("Tr out of range for LeeKesler table.")
elif (self.Pr < refPr[0] or self.Pr > refPr[-1]):
raise ValueError("Pr out of range for Lee-Kesler table.")
for i, t in enumerate(refTr):
if t > self.Tr:
iTr = i-1
break
for j, p in enumerate(refPr):
if p > self.Pr:
jPr = j-1
break
Z0_vals = np.zeros((2,2))
for i in range(0,2):
for j in range(0,2):
Z0_vals[i, j] = float(input(f"Z0: Tr = {refTr[iTr + i]}, Pr = {refPr[jPr + j]}"))
Z0 = self.interp2d(self.Tr, self.Pr, refTr[iTr:iTr+2], refPr[jPr:jPr+2], Z0_vals )
Z1_vals = np.zeros((2,2))
for i in range(0,2):
for j in range(0,2):
Z1_vals[i, j] = float(input(f"Z1: Tr = {refTr[iTr + i]}, Pr = {refPr[jPr + j]}"))
Z1 = self.interp2d(self.Tr, self.Pr, refTr[iTr:iTr+2], refPr[jPr:jPr+2], Z1_vals )
self.Z_sol = [Z0 + self.omega*Z1]
else:
raise ValueError("Requested EOS not programmed yet.")
self.calc_finished = True
return self.Z_sol
def calc_Z_alt(self, V):
"""
Uses stored value of T and a given value of V to work out Z, which is explicit.
"""
if not self.calc_finished:
self.calc_Z()
if self.kind in self.cubic:
rho = 1/V
b = self.b
q = self.q
eps = self.eps
sig = self.sig
def rho_Z(rho):
r1 = 1/(1-rho*b)
r2 = q*rho*b / (1 + eps*rho*b) / (1 + sig*rho*b)
return (r1 - r2)
self.z_alt = rho_Z(rho)
return self.z_alt
else:
raise ValueError("EOS kind not implemented yet")
@staticmethod
def interp2d(x, y, xmp, ymp, vals):
"""
x: actual value
y: actual value
xmp: (lower x ref, upper x ref)
ymp: (lower y ref, upper y ref)
vals: ((z(xm, ym), z(xp, ym)), (z(xm, yp), z(xp, yp)))
"""
xm, xp = xmp
ym, yp = ymp
xs = (x-xm)/(xp-xm)
ys = (y-ym)/(yp-ym)
r1a = xs*vals[0][0] + (1-xs)*vals[1][0]
r1b = xs*vals[0][1] + (1-xs)*vals[1][1]
r2 = ys*r1a + (1-ys)*r1b
return r2
def calc_V(self, root="all"):
""""
Takes "liq" or "vap" as an argument, to determine which root; default returns all roots.
Uses values of Z computed previously if available; otherwise runs a Z calculation.
"""
if not self.calc_finished:
self.calc_Z()
if self.pint:
self.V = np.array(self.Z_sol)*muc.R*self.T/self.P
else:
self.V = np.array(self.Z_sol)*8.3145*self.T/self.P
if root == "vap":
return self.V[-1]
elif root == "liq":
return self.V[0]
else:
return self.V
def calc_residSH(self):
"""
Uses data as already passed to work out S_resid/R and H_resid/RT.
Uses the Z value which should correspond to vapor phase.
"""
if not self.calc_finished:
self.calc_Z()
if self.kind in self.cubic:
sig = self.cnst[0]
eps = self.cnst[1]
beta = self.calc_beta(self.cnst[2], self.Pr.magnitude, self.Tr.magnitude)
q = self.calc_q()
Z = self.Z_sol[-1]
if self.kind == "RK":
dlnaTr = -.5
else:
raise ValueError("EOS kind residuals not implemented yet")
# TODO
if self.kind == "vdW":
I = beta/(Z+eps*beta)
else:
I = 1/(sig+eps)*np.log((Z+sig*beta)/(Z+eps*beta))
self.HRRT = Z - 1 + (dlnaTr -1)*q*I
self.SRR = np.log(Z-beta) + dlnaTr*q*I
else:
raise ValueError("EOS kind residuals not implemented yet")
if self.pint:
self.HR = self.HRRT*muc.R*self.T
self.SR = self.SRR *muc.R
def calc_residG(self, root="liq"):
"""
Uses data as already passed to work out G_resid/RT.
Reference: Lecture 16, Eq. 6.58, Eq. 3.51, Eq. 3.44, Z equation not in book
"""
if not self.calc_finished:
self.calc_Z()
if root == "liq":
Z = np.min(self.Z_sol)
elif root == "vap":
Z = np.max(self.Z_sol)
else:
raise ValueError("Invalid root type given.")
if self.kind in self.cubic:
sig = self.cnst[0]
eps = self.cnst[1]
q = self.calc_q()
b = self.Omega*muc.R*self.Tc/self.Pc
self.b = b
rho = self.P/Z/muc.R/self.T
rho.ito("mol/m**3")
def rho_Z_int(rho):
r1 = 1/(1-rho*b)
r2 = q*rho*b / (1 + eps*rho*b) / (1 + sig*rho*b)
return (r1 - r2)
# def rho_Z_integrated(rho):
# r1 = -np.log(1-rho*b)
# r2 = q*(np.log(1+eps*rho*b)-np.log(1+sig*rho*b))/(eps-sig)
# return r1-r2
rho_unit = rho.units
rho_int, err = quad(lambda rh: (rho_Z_int(rh*rho_unit)-1)/rh, 0, rho.magnitude)
# rho_int = rho_Z_integrated(rho) - rho_Z_integrated(0*rho_unit)
# print(rho_int)
self.GRRT = rho_int + Z - 1 - np.log(Z)
else:
raise ValueError("EOS kind residuals not implemented yet")
if self.pint:
self.GR = self.GRRT*muc.R*self.T
else:
self.GR = self.GRRT*muc.R*self.T
return self.GR
def calc_fugacity(self, root="vap"):
self.calc_residG(root=root)
phi = np.exp(self.GRRT)
self.f = phi * self.P
return self.f
#-----------------------------------
# Leftovers from ChEn 273
class calc:
@staticmethod
def book_CpR(T, ABCD, pint=True):
"""
Provide T in K.
Takes coefficients ABCD in an iterable and unpacks them; should all be dimensionless.
Equation form: C_p/R = A + BT + CT^2 + DT^-2 with B*10^-3, C*10^-6, D*10^5 evaluated internally
Optional bool pint=True: if True, divides by muc.uReg.K to enforce dimensional consistency
Returns C_p/R, to simplify units problems
"""
A, B, C, D = ABCD
if pint:
return A + B*T/un.K*1E-3 + C*T*T/un.K/un.K*1E-6 + D/T/T*un.K*un.K*1E5
else:
return A + B*T*1E-3 + C*T*T*1E-6 + D/T/T*1E5
@staticmethod
def DS_ig(T1, T2, P1, P2, Cp, pint=True):
"""
Cp a function, takes T and returns Cp
pint: tells whether to strip units or not
"""
if pint:
t_part = quad(lambda t: (Cp(t*un.K)/t).magnitude, T1.to("K").magnitude, T2.to("K").magnitude)[0] * Cp(T1).units
p_part = muc.R*np.log(P2/P1)
else:
t_part = quad(lambda t: Cp(t)/t, T1, T2)[0]
p_part = 8.3145**np.log(P2/P1)
return t_part - p_part
@staticmethod
def DH_ig(T1, T2, Cp, pint=True):
if pint:
h_unit = (Cp(T1)*T1).units
t_part = quad(lambda t: Cp(t*un.K).to(Cp(T1).units).magnitude, T1.to("K").magnitude, T2.to("K").magnitude)[0]*(h_unit)
else:
t_part = quad(lambda t: Cp(t), T1, T2)
return t_part
@staticmethod
def B(T, Tc, omega):
"""
Computes second virial coefficient B.
Takes T, Tc, Pc, omega, as arguments; all should be in absolute units
Optional argument: R, to match units being used. Defaults to m^3*Pa/K/mol
Tr: reference temperature
Tc, Pc: critical temperature, pressure
omega: "Pitzer acentric factor", deals with geometry and polarity
"""
Tr = T / Tc
B0 = 0.083 - .422/(Tr**1.6)
B1 = 0.139 - .172/(Tr**4.2)
return B0 + omega*B1
@staticmethod
def dBdTr(T, Tc, omega):
"""
Computes second virial coefficient B.
Takes T, Tc, Pc, omega, as arguments; all should be in absolute units
Optional argument: R, to match units being used. Defaults to m^3*Pa/K/mol
Tr: reference temperature
Tc, Pc: critical temperature, pressure
omega: "Pitzer acentric factor", deals with geometry and polarity
"""
Tr = T / Tc
dB0dTr = .675/(Tr**2.6)
dB1dTr = .722/(Tr**5.2)
return dB0dTr + omega*dB1dTr
@staticmethod
def B_resid(T, Tc, P, Pc, omega):
Tr = T/Tc
Pr = P/Pc
HRRT = Pr * (calc.B(T, Tc, omega) - Tr*calc.dBdTr(T, Tc, omega))
SRR = -Pr * (calc.dBdTr(T, Tc, omega))
return HRRT, SRR
@staticmethod
def Tsat(T_guess, P, Tc, Pc, omega, pint=True):
mat_prop = (Tc, Pc, omega)
if pint:
def sol_T(T):
T = un.Quantity(T, un.K)
state = EOS("PR", T, P, *mat_prop, pint)
state.calc_Z()
vap = state.calc_fugacity(root="vap")
liq = state.calc_fugacity(root="liq")
return (vap - liq).magnitude
T_sol = fsolve(sol_T, T_guess.magnitude)[0]*T_guess.units
else:
def sol_T(T):
state = EOS("PR", T, P, *mat_prop, pint)
state.calc_Z(Z_guess)
vap = state.calc_fugacity(root="vap")
liq = state.calc_fugacity(root="liq")
return (vap - liq)
T_sol = fsolve(sol_T, T_guess)[0]
return T_sol
@staticmethod
def Psat(T, P_guess, Tc, Pc, omega, pint=True):
mat_prop = (Tc, Pc, omega)
if pint:
def sol_P(P):
P = un.Quantity(P, Pc.units)
state = EOS("PR", T, P, *mat_prop, pint)
state.calc_Z()
vap = state.calc_fugacity(root="vap")
liq = state.calc_fugacity(root="liq")
return (vap - liq).magnitude
P_sol = fsolve(sol_P, P_guess.magnitude)[0]*P_guess.units
else:
def sol_P(P):
state = EOS("PR", T, P, *mat_prop, pint)
state.calc_Z(Z_guess)
vap = state.calc_fugacity(root="vap")
liq = state.calc_fugacity(root="liq")
return (vap - liq)
P_sol = fsolve(sol_P, P_guess)[0]
return P_sol
@staticmethod
def DIPPR_Psat(T, coeff):
A, B, C, D, E = coeff
T = T.to(un.K).magnitude
Y = np.exp(A + B/T + C*np.log(T) + D*T**E)
return Y*un.Pa
@staticmethod
def Bmix1(nFracs, Bvals):
"""
Probably outdated; from a ChEn 273 problem.
Mixing rule: sum_i(sum_j(yi*yj*Bij)), with Bii pure species, Bij = .5(Bii+Bjj)
Takes two same-length iterables as arguments: nFracs, Bvals
nFracs: list of mole fractions by species
Bvals: list of B by pure species (second virial coefficient), matched with nFracs
"""
if len(nFracs) != len(Bvals):
raise Exception("Number of mole fractions does not equal number of B coefficients")
num = len(nFracs) # number of species, effectively
# List comprehensions!
Bij = [[.5 * (Bvals[i] + Bvals[j]) for j in range(num)] for i in range(num)]
Bmix = 0
for i in range(len(nFracs)):
for j in range(len(nFracs)):
Bmix += nFracs[i]*nFracs[j]*Bij[i][j]
return Bmix
@staticmethod
def multi_reac_equil(rxn_list, Ka_list, ext_guess, n0):
"""
Function for calculating equilibrium of multiple reactions.
Arguments: rxn_list, Ka_list, ext_guess, n0
rxn_list is a list of reac_equil objects, which already have everything set (phase, activity) and have the same species in common (just different activities).
Ka_list is a list of Ka values for each reaction, at the appropriate temperature.
ext_guess is a list of guess values for the extent of reaction for each reaction. Pass without units; moles assumed.
n0 is the overall inlet feed, as a dictionary by species with mole units.
"""
def sol_ee(ext_guess):
nn = {}
names = rxn_list[0].names
for nm in names:
nn[nm] = n0[nm] + sum([rx.nu[nm] * ex * un.mol for rx, ex in zip(rxn_list, ext_guess)])
Qa_list = np.array([rxn.calc_Qa_nn(nn, split=True) for rxn in rxn_list])
Qap_list = Qa_list[:,0]
Qar_list = Qa_list[:,1]
eq_list = [(Ka/Qar - Qap).magnitude for Ka, Qap, Qar in zip(Ka_list, Qap_list, Qar_list)]
return eq_list
ext_list = fsolve(sol_ee, ext_guess) *un.mol
return ext_list
class mixfit:
def __init__(self, x1_arr, M_arr, curve_func="Not Given" ):
"""
Takes x1_arr, M_arr corresponding to the mole fraction and total molar property.
Will optionally take a curve_func, for example a quadratic. Not yet implemented. Currently defaults to a quadratic fit.
"""
if curve_func == "Not Given":
self.curve_func = self.default_curve
self.nParams=3
else:
self.curve_func = curve_func
print("Warning: system is not currently set up to handle other curve fits.")
self.M1 = M_arr[-1]
self.M2 = M_arr[0]
self.fit, self.covar = curve_fit(lambda x1, p: self.M_nondim(x1, *p), x1_arr, M_arr.magnitude, p0=np.zeros(self.nParams))
@staticmethod
def default_curve(x1, a, b, c):
return (a + b*x1 + c*x1*x1)
# This function needs to have a function signature corresponding to the curve_func passed.
# I would like to automate that better, perhaps with the help of curve_fit.
def M_nondim(self, x1, param):
x2 = 1-x1
return x1*self.M1.magnitude + x2*self.M2.magnitude + x1*x2*self.curve_func(x1, *param)
@un.wraps("mL/mol", (None, ""), strict=False)
def calc_M(self, x1):
return self.M_nondim(x1, *self.fit)
def M_id_nondim(self, x1):
return x1*self.M1.magnitude + (1-x1)*self.M2.magnitude
@un.wraps("mL/mol", (None, ""), strict=False)
def calc_M_id(self, x1):
return self.M_id_nondim(x1)
def dMdx_nondim(self, x1):
return muc.ddx(lambda x: self.M_nondim(x, *self.fit), x1, .1)
@un.wraps("mL/mol", (None, ""), strict=False)
def calc_dMdx(self, x1):
return self.dMdx_nondim(x1)
def M1b_nondim(self, x1):
M = self.M_nondim(x1, *self.fit)
dMdx = self.dMdx_nondim(x1)
x2 = 1-x1
return M + x2*dMdx
@un.wraps("mL/mol", (None, ""), strict=False)
def calc_M1b(self, x1):
return self.M1b_nondim(x1)
def M2b_nondim(self, x1):
M = self.M_nondim(x1, *self.fit)
dMdx = self.dMdx_nondim(x1)
x2 = 1-x1
return M - x1*dMdx
@un.wraps("mL/mol", (None, ""), strict=False)
def calc_M2b(self, x1):
return self.M2b_nondim(x1)
class Activity:
def __init__(self, kind, params, phase="l", index=1):
"""
kind: kind of activity model
For phase='l' : 'mrg1', 'mrg2', 'Wilson12', 'WilsonLL',
'Wilson123', 'vanLaar', 'NRTL'
For phase='s' : '1'
For phase='g' : 'ig', 'im' . 'ig' indicates ideal gas, 'im' indicates ideal mixture but nonideal gas
params: single parameter or tuple of parameters to unpack.
mrg1: A
mrg2: A12, A21
Wilson12: a12, a21, V1, V2. #L12 and L21 are computed by calc_gamma12. (L for \Lambda)
WilsonLL: L12, L21 (L for \Lambda)
Wilson123: Multicomponent Wilson, with LL directly passed.
For N=3, 3 tuples of length 2: (L12, L13), (L21, L23), (L31, L32)
vanLaar: A12', A21'
ig: P
im: *either* fi0, fugacity, *or* fugacity_func(T,P). Checks if callable to decide which.
phase: defaults to l, for compatibility with older code.
index: defaults to 1. Used for binary liquid activities: should be either 1 or 2. At present assumes Poynting factor is 1.
No returns.
"""
self.kind = kind
self.args = (kind, params)
self.phase = phase
self.index = index
if self.phase == "l":
if self.kind == "mrg1":
self.A = params
self.calc_gamma12 = self.calc_gamma12_mrg1
elif self.kind == "mrg2":
self.A12, self.A21 = params
self.calc_gamma12 = self.calc_gamma12_mrg2
elif self.kind == "Wilson12":
self.V1, self.V2, self.a12, self.a21 = params
self.calc_gamma12 = self.calc_gamma12_Wilson
elif self.kind == "WilsonLL":
self.L12, self.L21 = params
self.calc_gamma12 = self.calc_gamma12_WilsonLL
elif self.kind == "Wilson123":
self.Lij = []
for i, p in enumerate(params):
p = list(p)
p.insert(i, 1)
self.Lij.append(p)
self.Lij = np.array(self.Lij)
self.calc_gamma_i = self.calc_gamma123_WilsonLL
elif self.kind == "vanLaar":
self.A12p, self.A21p = params
self.calc_gamma12 = self.calc_gamma12_vanLaar
elif self.kind == "NRTL":
self.A12, self.A21, self.alpha12 = params
self.calc_gamma12 = self.calc_gamma12_NRTL
else:
raise ValueError("Invalid type of activity model.")
self.calc_a = self.calc_a_liq
elif self.phase == "g":
if self.kind == "ig":
self.calc_a = self.calc_a_ig
self.P = params
elif self.kind == "im":
self.calc_a = self.calc_a_im
self.f_func = callable(params)
if self.f_func:
self.calc_f = params
else:
self.fi0 = params
else:
raise ValueError("Invalid type of activity model.")
elif self.phase == "s":
if self.kind == "1":
self.calc_a = self.calc_a_s1
else:
raise ValueError("Invalid type of activity model.")
else:
raise ValueError("Invalid phase passed to initialize activity model.")
def calc_a(self):
print("No function for activity was set, but it was called.")
raise NotImplementedError("Activity model not yet implemented: " + self.phase + self.kind)
# --------------------------------------------------
# Activity calculations (a, not gamma)
# Unlike the gamma calculations below, these do not assume a binary mixture, so they are performed
# for an individual species.
def calc_a_s1(self, xi, T="Not Used", P="Not Used"):
return 1
def calc_a_ig(self, xi, T="Not Used", P="Not Used"):
a = xi*self.P/(1*un.bar)
return a
def calc_a_im(self, xi, T="Not Given", P="Not Given"):
if not self.f_func:
a = xi*self.fi0/(1*un.bar)
return a
else:
if T=="Not Given" or P=="Not Given":
raise ValueError("Missing either T or P for calculating fugacity.")
f = self.calc_f(T, P)
a = xi*f/(1*un.bar)
return a
def calc_a_liq(self, xi, T="Not Given", P="Not Used"):
if self.index ==1:
x1 = xi
x2 = 1-x1
elif self.index == 2:
x2 = xi
x1 = 1-x2
gamma1, gamma2 = self.calc_gamma12(x1, T=T)
Poynting = 1
if self.index == 1:
a = xi*gamma1*Poynting
elif self.index == 2:
a = xi*gamma2*Poynting
else:
raise ValueError("Index other than 1 or 2 passed for a binary mixture of liquids.")
return a
# ---------------------------------------------------------
# Activity coefficient models for binary mixture
# All functions return both gamma1 and gamma2.
def calc_gamma12_mrg1(self, x1, T="Not Given"):
x2 = 1-x1
gam1 = np.exp(x2*x2*self.A)
gam2 = np.exp(x1*x1*self.A)
return gam1, gam2
def calc_gamma12_mrg2(self, x1, T="Not Given"):
x2 = 1-x1
gam1 = np.exp(x2*x2 * (self.A12 + 2*(self.A21 - self.A12)*x1))
gam2 = np.exp(x1*x1 * (self.A21 + 2*(self.A12 - self.A21)*x2))
return gam1, gam2
def calc_gamma12_Wilson(self, x1, T):
# if T == "Not Given":
# raise ValueError("Wilson VLE needs a temperature for gamma.")
x2 = 1-x1
L12, L21 = self.calc_Wilson_LL(self.V1, self.V2, self.a12, self.a21, T)
der = (L12/(x1 + x2*L12) - L21/(x2 + x1*L21))
ret1 = x1 + x2*L12
ret2 = x2 + x1*L21
gam1 = np.exp(x2*der)/ret1
gam2 = np.exp(-x1*der)/ret2
return gam1, gam2
@staticmethod
def calc_Wilson_LL(V1, V2, a12, a21, T):
L12 = V2/V1 * np.exp(-a12/muc.R/T)
L21 = V1/V2 * np.exp(-a21/muc.R/T)
return L12, L21
def calc_gamma12_WilsonLL(self, x1, T="Not Given"):
x2 = 1-x1
L12 = self.L12
L21 = self.L21
der = (L12/(x1 + x2*L12) - L21/(x2 + x1*L21))
ret1 = x1 + x2*L12
ret2 = x2 + x1*L21
gam1 = np.exp(x2*der)/ret1
gam2 = np.exp(-x1*der)/ret2
return gam1, gam2
def calc_gamma123_WilsonLL(self, xi_arr, T="Not Given"):
"""
Multicomponent Wilson
Takes an array [x1, x2, ..., xn].
Returns [gam1, gam2, ..., gamn].
Took some time to work through array syntax here; be careful with modifications.
"""
Lij = self.Lij
xjLij = xi_arr*Lij
xiLij = (xi_arr*Lij.T).T
n = len(xi_arr)
denom_k = np.sum(xjLij, axis=1) # Sum across j with axis 1
logterm = np.log(denom_k) # Sum across j with axis 1
sumterm = np.sum((xiLij.T/denom_k).T, axis=0) # Divide columnwise, sum through k
gam_i = np.exp(-logterm + 1 - sumterm)
return gam_i
def calc_gamma12_vanLaar(self, x1, T="Not Given"):
x2 = 1-x1
gam1 = np.exp(self.A12p *(1 + self.A12p*x1/self.A21p/x2)**-2)
gam2 = np.exp(self.A21p *(1 + self.A21p*x2/self.A12p/x1)**-2)
return gam1, gam2
def calc_gamma12_NRTL(self, x1, T):
# if T == "Not Given":
# raise ValueError("Wilson VLE needs a temperature for gamma.")
x2 = 1-x1
tau12 = self.A12/muc.R/T
tau21 = self.A21/muc.R/T
G12 = np.exp(-self.alpha12 * tau12)
G21 = np.exp(-self.alpha12 * tau21)
ret1 = x1 + x2*G21
ret2 = x2 + x1*G12
gam1 = np.exp(x2*x2 * (tau21*(G21/ret1)**2 + tau12*G12/ret2/ret2) )
gam2 = np.exp(x1*x1 * (tau12*(G12/ret2)**2 + tau21*G21/ret1/ret1) )
return gam1, gam2
def calc_GERT(self, *args):
x2 = 1-x1
gam1, gam2 = self.calc_gamma12(*args)
return x1*np.log(gam1) + x2*np.log(gam2)
def calc_GmixRT(self, x1):
x2 = 1-x1
GERT = self.calc_GERT(x1)
term = x1*np.log(x1) + x2*np.log(x2)
return GERT + term
class vle:
def __init__(self, kind, T="Not Given", P="Not Given"):
"""
Class to handle binary VLE calculations. To generate a Txy diagram, pass P; for a Pxy diagram, pass T.
Allowed kinds are baby, teen, and adult (pass a string).
Constructs an object, which will store values and use them.
If using teen or adult, next call [vle_obj].set_act_model.
For all, call [vle_obj].set_Psat .
After setting up activities and Psat functions, call:
[vle_obj].calc_Pxy()
or
[vle_obj].calc_Txy()
"""
self.kind = kind
self.Tbool=True
self.Pbool=True
self.T = T
self.P = P
if self.T == "Not Given":
self.Tbool = False
if self.P == "Not Given":
self.Pbool = False
self.x1_arr = np.linspace(0, 1)
if self.kind == "baby":
self.calc_P = self.calc_P_baby
elif self.kind == "teen":
self.calc_P = self.calc_P_teen
elif self.kind == "adult":
self.calc_P = self.calc_P_adult
else:
raise ValueError("Incorrect VLE kind given. Should be 'baby', 'teen', or 'adult'.")
def set_Psat(self, func1, func2):
"""
Takes two callables. Each should return Psat for the fluid.
Proper function signature: func(T)
"""
self.Psat1 = func1
self.Psat2 = func2
def set_act_model(self, kind, params):
"""
kind: 'mrg1', 'mrg2','Wilson', etc.
params: tuple of parameters to unpack.
For detailed info on implemented models and parameters to pass,
see help(thermo.Activity), and look at liquid models.
No returns.
Wraps the Activity initialization function, and calls it self.act.
"""
if self.kind == "baby":
print("Warning: Setting an unused activity model for VLE.")
self.act_kind = kind
self.act = Activity(self.act_kind, params)
def calc_P_baby(self, x1, T, calc_y=False):
"""
Takes x1 and T. Optional parameter calc_y=False.
If calc_y is True, also computes y1 and returns (P_tot, y1).
If calc_y is False, returns P_tot.
"""
x2 = 1-x1
P1 = x1*self.Psat1(T)
P2 = x2*self.Psat2(T)
if calc_y:
return P1 + P2, P1/(P1 + P2)
else:
return P1 + P2
def calc_P_teen(self, x1, T, calc_y=False):
"""
Takes x1 and T. Optional parameter calc_y=False.
If calc_y is True, also computes y1 and returns (P_tot, y1).
If calc_y is False, returns P_tot.
"""
x2 = 1-x1
gam1, gam2 = self.act.calc_gamma12(x1, T)
P1 = x1*self.Psat1(T) * gam1
P2 = x2*self.Psat2(T) * gam2
if calc_y:
return P1 + P2, P1/(P1 + P2)
else:
return P1 + P2
def calc_Pxy(self, numPoints=101, x1="Not Given", T="Not Given", xspan=(0,1)):
"""
All arguments optional.
Args:
x1=.5: point or array of points at which to calculate Pxy
numPoints=101 , number of points at which Txy are calculated.
Ignored if an array x1 is passed to x1
T, required if VLE object not initialized with T
xspan=(0,1), limiting values between which to calculate Pxy
If the solver is unstable, supply a guess value for P by initializing VLE object with T=...
"""
if T == "Not Given" and not self.Tbool:
raise ValueError("Cannot perform Pxy calc without T. Either initialize VLE with one, or pass to calc_Pxy.")
elif T == "Not Given":
T = self.T
if numPoints == 1:
P, y1 = self.calc_P(x1, T, True)
return P, x1, y1
elif type(x1) == type("Not Given"):
x1_arr = np.linspace(*xspan, numPoints)
P_arr, y1_arr = self.calc_P(x1_arr, T, True)
return P_arr, x1_arr, y1_arr
else:
P_arr, y1_arr = self.calc_P(x1, T, True)
return P_arr, x1, y1_arr
# This function was, very mysteriously, crashing the Jupyter kernel without throwing any Python errors.
# The end result is that I run a single fsolve across the entire x1 array, instead of individually.
# I do not know why this works and the alternatives (commented out below) did not.
def calc_Txy(self, x1=.5, numPoints=101, P="Not Given", xspan=(0,1)):
"""
All arguments optional.
Args:
x1=.5: a single point at which to calculate Txy if numPoints==1
numPoints=101 , number of points at which Txy are calculated
P, required if VLE object not initialized with P
xspan=(0,1), a range of values along which to calculate Txy
If the solver is unstable, supply a guess value for T by initializing VLE object with T=...
"""
if P == "Not Given" and not self.Pbool:
raise ValueError("Cannot perform Txy calc without P. Either initialize VLE with one, or pass to calc_Txy.")
elif P == "Not Given":
P = self.P
Tguess = self.T if self.Tbool else 300*un.K
if numPoints == 1:
T = fsolve(lambda t: (self.calc_P(x1, t*Tguess.units, False) - P).magnitude, Tguess.magnitude)[0]*Tguess.units
P, y1 = self.calc_P(x1, T, True)
return T, x1, y1
else:
x1_arr = np.linspace(*xspan, numPoints)
T_arr = fsolve(lambda t: (self.calc_P(x1_arr, t*Tguess.units, False) - P).magnitude, x1_arr+Tguess.magnitude)*Tguess.units
P_arr, y1_arr = self.calc_P(x1_arr, T_arr, True)
return T_arr, x1_arr, y1_arr
# T_list = []
# P_list = []
# y_list = []
# for x1 in x1_arr:
# T = fsolve(lambda t: (self.calc_P(x1, t*Tguess.units, False) - P).magnitude, Tguess.magnitude)[0]*Tguess.units
# P, y1 = self.calc_P(x1, T, True)
# T_list.append(T)
# P_list.append(P)
# y_list.append(y1)
# T_arr = muc.list_unit(T_list)
# P_arr = muc.list_unit(P_list)
# y1_arr = muc.list_unit(y_list)
# return T_arr, x1_arr, y1_arr
# def sol_T(t, x1):
# t *= Tguess.units
# P_RHS = self.calc_P(x1, t, False)
# return (P - P_RHS).magnitude
# T_arr = [fsolve(lambda t: sol_T(t, x1), Tguess.magnitude)[0]*Tguess.units for x1 in x1_arr]
# T_arr = muc.list_unit(T_arr)
# P_arr, y1_arr = self.calc_P(x1_arr, T_arr, True)
# return T_arr, x1_arr, y1_arr
def calc_bbl_yP(self, x1, guess, T="Not Given"):
"""
Takes x1, a guess for the solver, and optionally a value of T to use in the calculation.
The guess has the form (y1, P), without any units attached.
Returns y1, P .
"""
if T == "Not Given":
T = self.T
P, new_x, y1 = self.calc_Pxy(1, x1, T)
return y1, P
def calc_bbl_yT(self, x1, guess, P="Not Given"):
"""
Takes x1, a guess for the solver, and optionally a value of P to use in the calculation.
The guess has the form (y1, T), without any units attached.
Returns y1, T.
"""
if P == "Not Given":
P = self.P
T, new_x, y1 = self.calc_Txy(1, x1, P)
return y1, T
def calc_dew_xP(self, y1, guess, T="Not Given"):
"""
Takes x1, a guess for the solver, and optionally a value of T to use in the calculation.
The guess has the form (y1, P), without any units attached.
Returns x1, P.
"""
if T == "Not Given":
T = self.T
def sol_xP(x1P):
x1, P = x1P
P_RHS, y1_RHS = self.calc_P(x1, T, True)
eq1 = (P*P_RHS.units) - P_RHS
eq2 = y1 - y1_RHS
return eq1.magnitude, eq2.magnitude
xP_sol = fsolve(sol_xP, guess)
x1 = xP_sol[0]
P = self.calc_P(x1, T, False)
err = sol_xP((x1, P.magnitude))
P_err = (err[0]*P.units) / P
y_err = (err[1])/y1
if np.abs(P_err.to("").magnitude) > .001 or np.abs(y_err) > .001:
print(f"Dew P calc error: {P_err.magnitude*100:.1f}% in P, {y_err*100:.1f}% in y")
# P = Px_sol[1]*P_RHS.units
return x1, P
def calc_dew_xT(self, y1, guess, P="Not Given"):
"""
Takes x1, a guess for the solver, and optionally a value of T to use in the calculation.
The guess has the form (y1, P), without any units attached.
Returns x1, T.
"""
if P == "Not Given":
P = self.P
def sol_xT(x1T):
x1, T = x1T
T *= un.K
P_RHS, y1_RHS = self.calc_P(x1, T, True)
eq1 = P - P_RHS
eq2 = y1 - y1_RHS
return eq1.magnitude, eq2.magnitude
xT_sol = fsolve(sol_xT, guess)
x1 = xT_sol[0]
T = xT_sol[1]*un.K