forked from simonmb/PC-SAFT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpcsaft_electrolyte.py
2050 lines (1794 loc) · 90.7 KB
/
pcsaft_electrolyte.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
# -*- coding: utf-8 -*-
"""
PC-SAFT with electrolyte term
These functions implement the PC-SAFT equation of state. In addition to the
hard chain and dispersion terms, these functions also include dipole,
association and ion terms for use with these types of compounds.
@author: Zach Baird
Functions
---------
- pcsaft_vaporP : calculate the vapor pressure
- pcsaft_bubbleP : calculate the bubble point pressure of a mixture
- pcsaft_Hvap : calculate the enthalpy of vaporization
- pcsaft_PTz : allows PTz data to be used for parameter fitting
- pcsaft_den : calculate the molar density
- pcsaft_p : calculate the pressure
- pcsaft_hres : calculate the residual enthalpy
- pcsaft_sres : calculate the residual entropy
- pcsaft_gres : calculate the residual Gibbs free energy
- pcsaft_fugcoef : calculate the fugacity coefficients
- pcsaft_Z : calculate the compressibility factor
- pcsaft_ares : calculate the residual Helmholtz energy
- XA_find : used internally to solve for XA
- dXA_find : used internally to solve for the derivative of XA wrt density
- dXAdt_find : used internally to solve for the derivative of XA wrt temperature
- bubblePfit : used internally to solve for the bubble point pressure
- vaporPfit : used internally to solve for the vapor pressure
- denfit : used internally to solve for the density
- PTzfit : used internally to solve for pressure and compositions
- dielc_water : returns the dielectric constant of water
- pcsaft_fit_pure : can be used when fitting PC-SAFT parameters to data
References
----------
* J. Gross and G. Sadowski, “Perturbed-Chain SAFT: An Equation of State
Based on a Perturbation Theory for Chain Molecules,” Ind. Eng. Chem.
Res., vol. 40, no. 4, pp. 1244–1260, Feb. 2001.
* M. Kleiner and G. Sadowski, “Modeling of Polar Systems Using PCP-SAFT:
An Approach to Account for Induced-Association Interactions,” J. Phys.
Chem. C, vol. 111, no. 43, pp. 15544–15553, Nov. 2007.
* Gross Joachim and Vrabec Jadran, “An equation‐of‐state contribution
for polar components: Dipolar molecules,” AIChE J., vol. 52, no. 3,
pp. 1194–1204, Feb. 2006.
* A. J. de Villiers, C. E. Schwarz, and A. J. Burger, “Improving
vapour–liquid-equilibria predictions for mixtures with non-associating polar
components using sPC-SAFT extended with two dipolar terms,” Fluid Phase
Equilibria, vol. 305, no. 2, pp. 174–184, Jun. 2011.
* S. H. Huang and M. Radosz, “Equation of state for small, large,
polydisperse, and associating molecules,” Ind. Eng. Chem. Res., vol. 29,
no. 11, pp. 2284–2294, Nov. 1990.
* S. H. Huang and M. Radosz, “Equation of state for small, large,
polydisperse, and associating molecules: extension to fluid mixtures,”
Ind. Eng. Chem. Res., vol. 30, no. 8, pp. 1994–2005, Aug. 1991.
* S. H. Huang and M. Radosz, “Equation of state for small, large,
polydisperse, and associating molecules: extension to fluid mixtures.
[Erratum to document cited in CA115(8):79950j],” Ind. Eng. Chem. Res.,
vol. 32, no. 4, pp. 762–762, Apr. 1993.
* J. Gross and G. Sadowski, “Application of the Perturbed-Chain SAFT
Equation of State to Associating Systems,” Ind. Eng. Chem. Res., vol.
41, no. 22, pp. 5510–5515, Oct. 2002.
* L. F. Cameretti, G. Sadowski, and J. M. Mollerup, “Modeling of Aqueous
Electrolyte Solutions with Perturbed-Chain Statistical Associated Fluid
Theory,” Ind. Eng. Chem. Res., vol. 44, no. 9, pp. 3355–3362, Apr. 2005.
* L. F. Cameretti, G. Sadowski, and J. M. Mollerup, “Modeling of Aqueous
Electrolyte Solutions with Perturbed-Chain Statistical Association Fluid
Theory,” Ind. Eng. Chem. Res., vol. 44, no. 23, pp. 8944–8944, Nov. 2005.
* C. Held, L. F. Cameretti, and G. Sadowski, “Modeling aqueous
electrolyte solutions: Part 1. Fully dissociated electrolytes,” Fluid
Phase Equilibria, vol. 270, no. 1, pp. 87–96, Aug. 2008.
* C. Held, T. Reschke, S. Mohammad, A. Luza, and G. Sadowski, “ePC-SAFT
revised,” Chem. Eng. Res. Des., vol. 92, no. 12, pp. 2884–2897, Dec. 2014.
"""
import numpy as np
from scipy.optimize import fsolve
from scipy.optimize import minimize
def pcsaft_vaporP(p_guess, x, m, s, e, t, **kwargs):
"""
Wrapper around solver that determines the vapor pressure.
Parameters
----------
p_guess : float
Guess for the vapor pressure (Pa)
x : ndarray, shape (n,)
Mole fractions of each component. It has a length of n, where n is
the number of components in the system.
m : ndarray, shape (n,)
Segment number for each component.
s : ndarray, shape (n,)
Segment diameter for each component. For ions this is the diameter of
the hydrated ion. Units of Angstrom.
e : ndarray, shape (n,)
Dispersion energy of each component. For ions this is the dispersion
energy of the hydrated ion. Units of K.
t : float
Temperature (K)
kwargs : dict
Additional parameters that can be passed through to the PC-SAFT functions.
The PC-SAFT functions can take the following additional parameters:
k_ij : ndarray, shape (n,n)
Binary interaction parameters between components in the mixture.
(dimensions: ncomp x ncomp)
e_assoc : ndarray, shape (n,)
Association energy of the associating components. For non associating
compounds this is set to 0. Units of K.
vol_a : ndarray, shape (n,)
Effective association volume of the associating components. For non
associating compounds this is set to 0.
dipm : ndarray, shape (n,)
Dipole moment of the polar components. For components where the dipole
term is not used this is set to 0. Units of Debye.
dip_num : ndarray, shape (n,)
The effective number of dipole functional groups on each component
molecule. Some implementations use this as an adjustable parameter
that is fit to data.
z : ndarray, shape (n,)
Charge number of the ions
dielc : float
Dielectric constant of the medium to be used for electrolyte
calculations.
Returns
-------
Pvap : float
Vapor pressure (Pa)
"""
Pvap = minimize(vaporPfit, p_guess, args=(x, m, s, e, t, kwargs), tol=1e-10, method='Nelder-Mead', options={'maxiter': 100}).x
return Pvap
def pcsaft_bubbleP(p_guess, xv_guess, x, m, s, e, t, **kwargs):
"""
Calculate the bubble point pressure of a mixture and the vapor composition.
Parameters
----------
p_guess : float
Guess for the vapor pressure (Pa)
x : ndarray, shape (n,)
Mole fractions of each component. It has a length of n, where n is
the number of components in the system.
m : ndarray, shape (n,)
Segment number for each component.
s : ndarray, shape (n,)
Segment diameter for each component. For ions this is the diameter of
the hydrated ion. Units of Angstrom.
e : ndarray, shape (n,)
Dispersion energy of each component. For ions this is the dispersion
energy of the hydrated ion. Units of K.
t : float
Temperature (K)
kwargs : dict
Additional parameters that can be passed through to the PC-SAFT functions.
The PC-SAFT functions can take the following additional parameters:
k_ij : ndarray, shape (n,n)
Binary interaction parameters between components in the mixture.
(dimensions: ncomp x ncomp)
e_assoc : ndarray, shape (n,)
Association energy of the associating components. For non associating
compounds this is set to 0. Units of K.
vol_a : ndarray, shape (n,)
Effective association volume of the associating components. For non
associating compounds this is set to 0.
dipm : ndarray, shape (n,)
Dipole moment of the polar components. For components where the dipole
term is not used this is set to 0. Units of Debye.
dip_num : ndarray, shape (n,)
The effective number of dipole functional groups on each component
molecule. Some implementations use this as an adjustable parameter
that is fit to data.
z : ndarray, shape (n,)
Charge number of the ions
dielc : float
Dielectric constant of the medium to be used for electrolyte
calculations.
Returns
-------
results : list
A list containing the following results:
0 : Bubble point pressure (Pa)
1 : Composition of the liquid phase
"""
result = minimize(bubblePfit, p_guess, args=(xv_guess, x, m, s, e, t, kwargs), tol=1e-10, method='Nelder-Mead', options={'maxiter': 100})
bubP = result.x
# Determine vapor phase composition at bubble pressure
if not ('z' in kwargs): # Check that the mixture does not contain electrolytes. For electrolytes, a different equilibrium criterion should be used.
rho = pcsaft_den(x, m, s, e, t, p_guess, phase='liq', **kwargs)
fugcoef_l = pcsaft_fugcoef(x, m, s, e, t, rho, **kwargs)
itr = 0
dif = 10000.
xv = np.copy(xv_guess)
xv_old = np.zeros_like(xv)
while (dif>1e-9) and (itr<100):
xv_old[:] = xv
rho = pcsaft_den(xv, m, s, e, t, p_guess, phase='vap', **kwargs)
fugcoef_v = pcsaft_fugcoef(xv, m, s, e, t, rho, **kwargs)
xv = fugcoef_l*x/fugcoef_v
xv = xv/np.sum(xv)
dif = np.sum(abs(xv - xv_old))
itr += 1
else:
z = kwargs['z']
rho = pcsaft_den(x, m, s, e, t, p_guess, phase='liq', **kwargs)
fugcoef_l = pcsaft_fugcoef(x, m, s, e, t, rho, **kwargs)
itr = 0
dif = 10000.
xv = np.copy(xv_guess)
xv_old = np.zeros_like(xv)
while (dif>1e-9) and (itr<100):
xv_old[:] = xv
rho = pcsaft_den(xv, m, s, e, t, p_guess, phase='vap', **kwargs)
fugcoef_v = pcsaft_fugcoef(xv, m, s, e, t, rho, **kwargs)
xv[np.where(z == 0)[0]] = (fugcoef_l*x/fugcoef_v)[np.where(z == 0)[0]] # here it is assumed that the ionic compounds are nonvolatile
xv = xv/np.sum(xv)
dif = np.sum(abs(xv - xv_old))
itr += 1
results = [bubP, xv]
return results
def pcsaft_Hvap(p_guess, x, m, s, e, t, **kwargs):
"""
Calculate the enthalpy of vaporization.
Parameters
----------
p_guess : float
Guess for the vapor pressure (Pa)
x : ndarray, shape (n,)
Mole fractions of each component. It has a length of n, where n is
the number of components in the system.
m : ndarray, shape (n,)
Segment number for each component.
s : ndarray, shape (n,)
Segment diameter for each component. For ions this is the diameter of
the hydrated ion. Units of Angstrom.
e : ndarray, shape (n,)
Dispersion energy of each component. For ions this is the dispersion
energy of the hydrated ion. Units of K.
t : float
Temperature (K)
kwargs : dict
Additional parameters that can be passed through to the PC-SAFT functions.
The PC-SAFT functions can take the following additional parameters:
k_ij : ndarray, shape (n,n)
Binary interaction parameters between components in the mixture.
(dimensions: ncomp x ncomp)
e_assoc : ndarray, shape (n,)
Association energy of the associating components. For non associating
compounds this is set to 0. Units of K.
vol_a : ndarray, shape (n,)
Effective association volume of the associating components. For non
associating compounds this is set to 0.
dipm : ndarray, shape (n,)
Dipole moment of the polar components. For components where the dipole
term is not used this is set to 0. Units of Debye.
dip_num : ndarray, shape (n,)
The effective number of dipole functional groups on each component
molecule. Some implementations use this as an adjustable parameter
that is fit to data.
z : ndarray, shape (n,)
Charge number of the ions
dielc : float
Dielectric constant of the medium to be used for electrolyte
calculations.
Returns
-------
output : list
A list containing the following results:
0 : enthalpy of vaporization (J/mol), float
1 : vapor pressure (Pa), float
"""
Pvap = minimize(vaporPfit, p_guess, args=(x, m, s, e, t, kwargs), tol=1e-10, method='Nelder-Mead', options={'maxiter': 100}).x
rho = pcsaft_den(x, m, s, e, t, Pvap, phase='liq', **kwargs)
hres_l = pcsaft_hres(x, m, s, e, t, rho, **kwargs)
rho = pcsaft_den(x, m, s, e, t, Pvap, phase='vap', **kwargs)
hres_v = pcsaft_hres(x, m, s, e, t, rho, **kwargs)
Hvap = hres_v - hres_l
output = [Hvap, Pvap]
return output
def pcsaft_PTz(p_guess, x_guess, beta_guess, mol, vol, x_total, m, s, e, t, **kwargs):
"""
Calculate the pressure and compositions of each phase when given the overall
composition and the total volume and number of moles. This allows PTz data
to be used in fitting PC-SAFT parameters.
Parameters
----------
p_guess : float
Guess for the pressure of the system (Pa)
x_guess : ndarray, shape (n,)
Guess for the liquid phase composition
beta_guess : float
Guess for the mole fraction of the system in the vapor phase
mol : float
Total number of moles in the system (mol)
vol : float
Total volume of the system (m^{3})
x_total : ndarray, shape (n,)
Overall mole fraction of each component in the system as a whole. It
has a length of n, where n is the number of components in the system.
m : ndarray, shape (n,)
Segment number for each component.
s : ndarray, shape (n,)
Segment diameter for each component. For ions this is the diameter of
the hydrated ion. Units of Angstrom.
e : ndarray, shape (n,)
Dispersion energy of each component. For ions this is the dispersion
energy of the hydrated ion. Units of K.
t : float
Temperature (K)
kwargs : dict
Additional parameters that can be passed through to the PC-SAFT functions.
The PC-SAFT functions can take the following additional parameters:
k_ij : ndarray, shape (n,n)
Binary interaction parameters between components in the mixture.
(dimensions: ncomp x ncomp)
e_assoc : ndarray, shape (n,)
Association energy of the associating components. For non associating
compounds this is set to 0. Units of K.
vol_a : ndarray, shape (n,)
Effective association volume of the associating components. For non
associating compounds this is set to 0.
dipm : ndarray, shape (n,)
Dipole moment of the polar components. For components where the dipole
term is not used this is set to 0. Units of Debye.
dip_num : ndarray, shape (n,)
The effective number of dipole functional groups on each component
molecule. Some implementations use this as an adjustable parameter
that is fit to data.
z : ndarray, shape (n,)
Charge number of the ions
dielc : float
Dielectric constant of the medium to be used for electrolyte
calculations.
Returns
-------
output : list
A list containing the following results:
0 : pressure in the system (Pa), float
1 : composition of the liquid phase, ndarray, shape (n,)
2 : composition of the vapor phase, ndarray, shape (n,)
3 : mole fraction of the mixture vaporized
"""
result = minimize(PTzfit, p_guess, args=(x_guess, beta_guess, mol, vol, x_total, m, s, e, t, kwargs), tol=1e-10, method='Nelder-Mead', options={'maxiter': 100})
p = result.x
if not ('z' in kwargs): # Check that the mixture does not contain electrolytes. For electrolytes, a different equilibrium criterion should be used.
itr = 0
dif = 10000.
xl = np.copy(x_guess)
beta = beta_guess
xv = (mol*x_total - (1-beta)*mol*xl)/beta/mol
while (dif>1e-9) and (itr<100):
beta_old = beta
rhol = pcsaft_den(xl, m, s, e, t, p, phase='liq', **kwargs)
fugcoef_l = pcsaft_fugcoef(xl, m, s, e, t, rhol, **kwargs)
rhov = pcsaft_den(xv, m, s, e, t, p, phase='vap', **kwargs)
fugcoef_v = pcsaft_fugcoef(xv, m, s, e, t, rhov, **kwargs)
xl = fugcoef_v*xv/fugcoef_l
xl = xl/np.sum(xl)
xv = (mol*x_total - (1-beta)*mol*xl)/beta/mol
beta = (vol/mol-rhol)/(rhov-rhol)
dif = np.sum(abs(beta - beta_old))
itr += 1
else:
z = kwargs['z']
# internal iteration loop to solve for compositions
itr = 0
dif = 10000.
xl = np.copy(x_guess)
beta = beta_guess
xv = (mol*x_total - (1-beta)*mol*xl)/beta/mol
xv[np.where(z != 0)[0]] = 0.
xv = xv/np.sum(xv)
while (dif>1e-9) and (itr<100):
beta_old = beta
rhol = pcsaft_den(xl, m, s, e, t, p, phase='liq', **kwargs)
fugcoef_l = pcsaft_fugcoef(xl, m, s, e, t, rhol, **kwargs)
rhov = pcsaft_den(xv, m, s, e, t, p, phase='vap', **kwargs)
fugcoef_v = pcsaft_fugcoef(xv, m, s, e, t, rhov, **kwargs)
xl = fugcoef_v*xv/fugcoef_l
xl = xl/np.sum(xl)
xv = (mol*x_total - (1-beta)*mol*xl)/beta/mol
xv[np.where(z != 0)[0]] = 0. # here it is assumed that the ionic compounds are nonvolatile
xv = xv/np.sum(xv)
beta = (vol/mol-rhol)/(rhov-rhol)
dif = np.sum(abs(beta - beta_old))
itr += 1
output = [p, xl, xv, beta]
return output
def pcsaft_den(x, m, s, e, t, p, phase='liq', **kwargs):
"""
Solve for the molar density when temperature and pressure are given.
Parameters
----------
x : ndarray, shape (n,)
Mole fractions of each component. It has a length of n, where n is
the number of components in the system.
m : ndarray, shape (n,)
Segment number for each component.
s : ndarray, shape (n,)
Segment diameter for each component. For ions this is the diameter of
the hydrated ion. Units of Angstrom.
e : ndarray, shape (n,)
Dispersion energy of each component. For ions this is the dispersion
energy of the hydrated ion. Units of K.
t : float
Temperature (K)
p : float
Pressure (Pa)
phase : string
The phase for which the calculation is performed. Options: "liq" (liquid),
"vap" (vapor).
kwargs : dict
Additional parameters that can be passed through to the PC-SAFT functions.
The PC-SAFT functions can take the following additional parameters:
k_ij : ndarray, shape (n,n)
Binary interaction parameters between components in the mixture.
(dimensions: ncomp x ncomp)
e_assoc : ndarray, shape (n,)
Association energy of the associating components. For non associating
compounds this is set to 0. Units of K.
vol_a : ndarray, shape (n,)
Effective association volume of the associating components. For non
associating compounds this is set to 0.
dipm : ndarray, shape (n,)
Dipole moment of the polar components. For components where the dipole
term is not used this is set to 0. Units of Debye.
dip_num : ndarray, shape (n,)
The effective number of dipole functional groups on each component
molecule. Some implementations use this as an adjustable parameter
that is fit to data.
z : ndarray, shape (n,)
Charge number of the ions
dielc : float
Dielectric constant of the medium to be used for electrolyte
calculations.
Returns
-------
rho : float
Molar density (mol m^{-3})
"""
if phase == 'liq':
eta_guess = 0.5
elif phase == 'vap':
eta_guess = 1e-9
else:
ValueError('The phase must be specified as either "liq" or "vap".')
N_AV = 6.022140857e23 # Avagadro's number
d = s*(1-0.12*np.exp(-3*e/t))
den_guess = 6/np.pi*eta_guess/np.sum(x*m*d**3)
rho_guess = den_guess*1.0e30/N_AV
rho = fsolve(denfit, rho_guess, args=(x, m, s, e, t, p, kwargs), full_output=True)[0]
return rho
def pcsaft_p(x, m, s, e, t, rho, k_ij=None, e_assoc=None, vol_a=None, dipm=None, \
dip_num=None, z=None, dielc=None):
"""
Calculate pressure.
Parameters
----------
x : ndarray, shape (n,)
Mole fractions of each component. It has a length of n, where n is
the number of components in the system.
m : ndarray, shape (n,)
Segment number for each component.
s : ndarray, shape (n,)
Segment diameter for each component. For ions this is the diameter of
the hydrated ion. Units of Angstrom.
e : ndarray, shape (n,)
Dispersion energy of each component. For ions this is the dispersion
energy of the hydrated ion. Units of K.
t : float
Temperature (K)
rho : float
Molar density (mol m^{-3})
k_ij : ndarray, shape (n,n)
Binary interaction parameters between components in the mixture.
(dimensions: ncomp x ncomp)
e_assoc : ndarray, shape (n,)
Association energy of the associating components. For non associating
compounds this is set to 0. Units of K.
vol_a : ndarray, shape (n,)
Effective association volume of the associating components. For non
associating compounds this is set to 0.
dipm : ndarray, shape (n,)
Dipole moment of the polar components. For components where the dipole
term is not used this is set to 0. Units of Debye.
dip_num : ndarray, shape (n,)
The effective number of dipole functional groups on each component
molecule. Some implementations use this as an adjustable parameter
that is fit to data.
z : ndarray, shape (n,)
Charge number of the ions
dielc : float
Dielectric constant of the medium to be used for electrolyte
calculations.
Returns
-------
P : float
Pressure (Pa)
"""
kb = 1.380648465952442093e-23 # Boltzmann constant, J K^-1
N_AV = 6.022140857e23 # Avagadro's number
den = rho*N_AV/1.0e30 # number density, units of Angstrom^-3
Z = pcsaft_Z(x, m, s, e, t, rho, k_ij, e_assoc, vol_a, dipm, dip_num, z, dielc)
P = Z*kb*t*den*1.0e30 # Pa
return P
def pcsaft_hres(x, m, s, e, t, rho, k_ij=None, e_assoc=None, vol_a=None, dipm=None, \
dip_num=None, z=None, dielc=None):
"""
Calculate the residual enthalpy for one phase of the system.
Parameters
----------
x : ndarray, shape (n,)
Mole fractions of each component. It has a length of n, where n is
the number of components in the system.
m : ndarray, shape (n,)
Segment number for each component.
s : ndarray, shape (n,)
Segment diameter for each component. For ions this is the diameter of
the hydrated ion. Units of Angstrom.
e : ndarray, shape (n,)
Dispersion energy of each component. For ions this is the dispersion
energy of the hydrated ion. Units of K.
t : float
Temperature (K)
rho : float
Molar density (mol m^{-3})
k_ij : ndarray, shape (n,n)
Binary interaction parameters between components in the mixture.
(dimensions: ncomp x ncomp)
e_assoc : ndarray, shape (n,)
Association energy of the associating components. For non associating
compounds this is set to 0. Units of K.
vol_a : ndarray, shape (n,)
Effective association volume of the associating components. For non
associating compounds this is set to 0.
dipm : ndarray, shape (n,)
Dipole moment of the polar components. For components where the dipole
term is not used this is set to 0. Units of Debye.
dip_num : ndarray, shape (n,)
The effective number of dipole functional groups on each component
molecule. Some implementations use this as an adjustable parameter
that is fit to data.
z : ndarray, shape (n,)
Charge number of the ions
dielc : float
Dielectric constant of the medium to be used for electrolyte
calculations.
Returns
-------
hres : float
Residual enthalpy (J mol^{-1})
"""
ncomp = x.shape[0] # number of components
kb = 1.380648465952442093e-23 # Boltzmann constant, J K^-1
N_AV = 6.022140857e23 # Avagadro's number
d = s*(1-0.12*np.exp(-3*e/t))
dd_dt = s*-3*e/t/t*0.12*np.exp(-3*e/t)
if type(z) == np.ndarray:
d[np.where(z != 0)[0]] = s[np.where(z != 0)[0]]*(1-0.12) # for ions the diameter is assumed to be temperature independent (see Held et al. 2014)
dd_dt[np.where(z != 0)[0]] = 0.
den = rho*N_AV/1.0e30
if type(k_ij) != np.ndarray:
k_ij = np.zeros((ncomp,ncomp), dtype='float_')
zeta = np.zeros((4,), dtype='float_')
dzeta_dt = np.zeros_like(zeta)
ghs = np.zeros((ncomp,ncomp), dtype='float_')
dghs_dt = np.zeros_like(ghs)
e_ij = np.zeros_like(ghs)
s_ij = np.zeros_like(ghs)
m2es3_avg = 0.
m2e2s3_avg = 0.
a0 = np.asarray([0.910563145, 0.636128145, 2.686134789, -26.54736249, 97.75920878, -159.5915409, 91.29777408])
a1 = np.asarray([-0.308401692, 0.186053116, -2.503004726, 21.41979363, -65.25588533, 83.31868048, -33.74692293])
a2 = np.asarray([-0.090614835, 0.452784281, 0.596270073, -1.724182913, -4.130211253, 13.77663187, -8.672847037])
b0 = np.asarray([0.724094694, 2.238279186, -4.002584949, -21.00357682, 26.85564136, 206.5513384, -355.6023561])
b1 = np.asarray([-0.575549808, 0.699509552, 3.892567339, -17.21547165, 192.6722645, -161.8264617, -165.2076935])
b2 = np.asarray([0.097688312, -0.255757498, -9.155856153, 20.64207597, -38.80443005, 93.62677408, -29.66690559])
for i in range(4):
zeta[i] = np.pi/6.*den*np.sum(x*m*d**i)
for i in range(1,4):
dzeta_dt[i] = np.pi/6.*den*np.sum(x*m*i*dd_dt*d**(i-1))
eta = zeta[3]
m_avg = np.sum(x*m)
for i in range(ncomp):
for j in range(ncomp):
s_ij[i,j] = (s[i] + s[j])/2.
if type(z) == np.ndarray:
if z[i]*z[j] <= 0: # for two cations or two anions e_ij is kept at zero to avoid dispersion between like ions (see Held et al. 2014)
e_ij[i,j] = np.sqrt(e[i]*e[j])*(1-k_ij[i,j])
else:
e_ij[i,j] = np.sqrt(e[i]*e[j])*(1-k_ij[i,j])
m2es3_avg = m2es3_avg + x[i]*x[j]*m[i]*m[j]*e_ij[i,j]/t*s_ij[i,j]**3
m2e2s3_avg = m2e2s3_avg + x[i]*x[j]*m[i]*m[j]*(e_ij[i,j]/t)**2*s_ij[i,j]**3
ghs[i,j] = 1/(1-zeta[3]) + (d[i]*d[j]/(d[i]+d[j]))*3*zeta[2]/(1-zeta[3])**2 + \
(d[i]*d[j]/(d[i]+d[j]))**2*2*zeta[2]**2/(1-zeta[3])**3
ddij_dt = (d[i]*d[j]/(d[i]+d[j]))*(dd_dt[i]/d[i]+dd_dt[j]/d[j]-(dd_dt[i]+dd_dt[j])/(d[i]+d[j]))
dghs_dt[i,j] = dzeta_dt[3]/(1-zeta[3])**2 \
+ 3*(ddij_dt*zeta[2]+(d[i]*d[j]/(d[i]+d[j]))*dzeta_dt[2])/(1-zeta[3])**2 \
+ 4*(d[i]*d[j]/(d[i]+d[j]))*zeta[2]*(1.5*dzeta_dt[3]+ddij_dt*zeta[2] \
+(d[i]*d[j]/(d[i]+d[j]))*dzeta_dt[2])/(1-zeta[3])**3 \
+ 6*((d[i]*d[j]/(d[i]+d[j]))*zeta[2])**2*dzeta_dt[3]/(1-zeta[3])**4
dadt_hs = 1/zeta[0]*(3*(dzeta_dt[1]*zeta[2] + zeta[1]*dzeta_dt[2])/(1-zeta[3]) \
+ 3*zeta[1]*zeta[2]*dzeta_dt[3]/(1-zeta[3])**2 \
+ 3*zeta[2]**2*dzeta_dt[2]/zeta[3]/(1-zeta[3])**2 \
+ zeta[2]**3*dzeta_dt[3]*(3*zeta[3]-1)/zeta[3]**2/(1-zeta[3])**3 \
+ (3*zeta[2]**2*dzeta_dt[2]*zeta[3] - 2*zeta[2]**3*dzeta_dt[3])/zeta[3]**3 \
* np.log(1-zeta[3]) \
+ (zeta[0]-zeta[2]**3/zeta[3]**2)*dzeta_dt[3]/(1-zeta[3]))
a = a0 + (m_avg-1)/m_avg*a1 + (m_avg-1)/m_avg*(m_avg-2)/m_avg*a2
b = b0 + (m_avg-1)/m_avg*b1 + (m_avg-1)/m_avg*(m_avg-2)/m_avg*b2
idx = np.arange(7)
I1 = np.sum(a*eta**idx)
I2 = np.sum(b*eta**idx)
C1 = 1/(1 + m_avg*(8*eta-2*eta**2)/(1-eta)**4 + (1-m_avg)*(20*eta-27*eta**2+12*eta**3-2*eta**4)/((1-eta)*(2-eta))**2)
C2 = -1*C1**2*(m_avg*(-4*eta**2+20*eta+8)/(1-eta)**5 + (1-m_avg)*(2*eta**3+12*eta**2-48*eta+40)/((1-eta)*(2-eta))**3)
dI1_dt = np.sum(a*dzeta_dt[3]*idx*eta**(idx-1))
dI2_dt =np.sum(b*dzeta_dt[3]*idx*eta**(idx-1))
dC1_dt = C2*dzeta_dt[3]
summ = 0.
for i in range(ncomp):
summ += x[i]*(m[i]-1)*dghs_dt[i,i]/ghs[i,i]
dadt_hc = m_avg*dadt_hs - summ
dadt_disp = -2*np.pi*den*(dI1_dt-I1/t)*m2es3_avg - np.pi*den*m_avg*(dC1_dt*I2+C1*dI2_dt-2*C1*I2/t)*m2e2s3_avg
# Dipole term (Gross and Vrabec term) --------------------------------------
if type(dipm) != np.ndarray:
dadt_polar = 0
else:
a0dip = np.asarray([0.3043504, -0.1358588, 1.4493329, 0.3556977, -2.0653308])
a1dip = np.asarray([0.9534641, -1.8396383, 2.0131180, -7.3724958, 8.2374135])
a2dip = np.asarray([-1.1610080, 4.5258607, 0.9751222, -12.281038, 5.9397575])
b0dip = np.asarray([0.2187939, -1.1896431, 1.1626889, 0, 0])
b1dip = np.asarray([-0.5873164, 1.2489132, -0.5085280, 0, 0])
b2dip = np.asarray([3.4869576, -14.915974, 15.372022, 0, 0])
c0dip = np.asarray([-0.0646774, 0.1975882, -0.8087562, 0.6902849, 0])
c1dip = np.asarray([-0.9520876, 2.9924258, -2.3802636, -0.2701261, 0])
c2dip = np.asarray([-0.6260979, 1.2924686, 1.6542783, -3.4396744, 0])
A2 = 0.
A3 = 0.
dA2_dt = 0.
dA3_dt = 0.
idxd = np.arange(5)
if type(dip_num) != np.ndarray:
dip_num = np.ones_like(x)
conv = 7242.702976750923 # conversion factor, see the note below Table 2 in Gross and Vrabec 2006
dipmSQ = dipm**2/(m*e*s**3)*conv
for i in range(ncomp):
for j in range(ncomp):
m_ij = np.sqrt(m[i]*m[j])
if m_ij > 2:
m_ij = 2
adip = a0dip + (m_ij-1)/m_ij*a1dip + (m_ij-1)/m_ij*(m_ij-2)/m_ij*a2dip
bdip = b0dip + (m_ij-1)/m_ij*b1dip + (m_ij-1)/m_ij*(m_ij-2)/m_ij*b2dip
J2 = np.sum((adip + bdip*e_ij[j,j]/t)*eta**idxd)
dJ2_dt = np.sum(-bdip*e_ij[j,j]/t**2*eta**idxd)
A2 += x[i]*x[j]*e_ij[i,i]/t*e_ij[j,j]/t*s_ij[i,i]**3*s_ij[j,j]**3 \
/s_ij[i,j]**3*dip_num[i]*dip_num[j]*dipmSQ[i]*dipmSQ[j]*J2
dA2_dt += x[i]*x[j]*e_ij[i,i]*e_ij[j,j]*s_ij[i,i]**3*s_ij[j,j]**3 \
/s_ij[i,j]**3*dip_num[i]*dip_num[j]*dipmSQ[i]*dipmSQ[j]* \
(dJ2_dt/t**2-2*J2/t**3)
for i in range(ncomp):
for j in range(ncomp):
for k in range(ncomp):
m_ijk = (m[i]*m[j]*m[k])**(1/3.)
if m_ijk > 2:
m_ijk = 2
cdip = c0dip + (m_ijk-1)/m_ijk*c1dip + (m_ijk-1)/m_ijk*(m_ijk-2)/m_ijk*c2dip
J3 = np.sum(cdip*eta**idxd)
A3 += x[i]*x[j]*x[k]*e_ij[i,i]/t*e_ij[j,j]/t*e_ij[k,k]/t* \
s_ij[i,i]**3*s_ij[j,j]**3*s_ij[k,k]**3/s_ij[i,j]/s_ij[i,k] \
/s_ij[j,k]*dip_num[i]*dip_num[j]*dip_num[k]*dipmSQ[i] \
*dipmSQ[j]*dipmSQ[k]*J3
dA3_dt += -3*x[i]*x[j]*x[k]*e_ij[i,i]*e_ij[j,j]*e_ij[k,k]* \
s_ij[i,i]**3*s_ij[j,j]**3*s_ij[k,k]**3/s_ij[i,j]/s_ij[i,k] \
/s_ij[j,k]*dip_num[i]*dip_num[j]*dip_num[k]*dipmSQ[i] \
*dipmSQ[j]*dipmSQ[k]*J3/t**4
A2 = -np.pi*den*A2
A3 = -4/3.*np.pi**2*den**2*A3
dA2_dt = -np.pi*den*dA2_dt
dA3_dt = -4/3.*np.pi**2*den**2*dA3_dt
dadt_polar = (dA2_dt*(1-A3/A2) + (dA3_dt*A2 - A3*dA2_dt)/A2)/(1-A3/A2)**2
# Association term -------------------------------------------------------
# only the 2B association type is currently implemented
if type(e_assoc) != np.ndarray:
dadt_assoc = 0
else:
a_sites = 2
iA = np.nonzero(e_assoc)[0] #indicies of associating compounds
ncA = iA.shape[0] # number of associating compounds in the fluid
XA = np.zeros((ncA,a_sites), dtype='float_')
eABij = np.zeros((ncA,ncA), dtype='float_')
volABij = np.zeros_like(eABij)
delta_ij = np.zeros_like(eABij)
ddelta_dt = np.zeros_like(eABij)
for i in range(ncA):
for j in range(ncA):
eABij[i,j] = (e_assoc[iA[i]]+e_assoc[iA[j]])/2.
volABij[i,j] = np.sqrt(vol_a[iA[i]]*vol_a[iA[j]])*(np.sqrt(s_ij[iA[i],iA[i]] \
*s_ij[iA[j],iA[j]])/(0.5*(s_ij[iA[i],iA[i]]+s_ij[iA[j],iA[j]])))**3
delta_ij[i,j] = ghs[iA[j],iA[j]]*(np.exp(eABij[i,j]/t)-1)*s_ij[iA[i],iA[j]]**3*volABij[i,j]
XA[i,:] = (-1 + np.sqrt(1+8*den*delta_ij[i,i]))/(4*den*delta_ij[i,i])
ddelta_dt[i,j] = s_ij[iA[j],iA[j]]**3*volABij[i,j]*(-eABij[i,j]/t**2 \
*np.exp(eABij[i,j]/t)*ghs[iA[j],iA[j]] + dghs_dt[iA[j],iA[j]] \
*(np.exp(eABij[i,j]/t)-1))
ctr = 0
dif = 1000.
XA_old = np.copy(XA)
while (ctr < 500) and (dif > 1e-9):
ctr += 1
XA = XA_find(XA, ncA, delta_ij, den, x[iA])
dif = np.sum(abs(XA - XA_old))
XA_old[:] = XA
XA = XA.flatten('F')
dXA_dt = dXAdt_find(ncA, ncomp, delta_ij, den, XA, ddelta_dt, x[iA], a_sites)
dadt_assoc = 0.
idx = -1
for i in range(ncA):
for j in range(a_sites):
idx += 1
dadt_assoc += x[iA[i]]*(1/XA[idx]-0.5)*dXA_dt[idx]
# Ion term ---------------------------------------------------------------
if type(z) != np.ndarray:
dadt_ion = 0
else:
if dielc == None:
ValueError('A value for the dielectric constant of the medium must be given when including the electrolyte term.')
E_CHRG = 1.6022e-19 # elementary charge, units of coulomb
perm_vac = 8.85416e-22 #permittivity in vacuum, C V^-1 Angstrom^-1
q = z*E_CHRG
kappa = np.sqrt(den*E_CHRG**2/kb/t/(dielc*perm_vac)*np.sum(z**2*x)) # the inverse Debye screening length. Equation 4 in Held et al. 2008.
if kappa == 0:
dadt_ion = 0
else:
dkappa_dt = -0.5*den*E_CHRG**2/kb/t**2/(dielc*perm_vac)*np.sum(z**2*x)/kappa
chi = 3/(kappa*s)**3*(1.5 + np.log(1+kappa*s) - 2*(1+kappa*s) + \
0.5*(1+kappa*s)**2)
dchikap_dk = (s*kappa*(6+3*s*kappa)-(6+6*s*kappa)*np.log(1+s*kappa)) \
/(s**3*kappa**3*(1+s*kappa))
dadt_ion = -1/12./np.pi/kb/(dielc*perm_vac)*np.sum(x*q**2* \
(dchikap_dk*dkappa_dt/t-kappa*chi/t**2))
dares_dt = dadt_hc + dadt_disp + dadt_assoc + dadt_polar + dadt_ion
Z = pcsaft_Z(x, m, s, e, t, rho, k_ij, e_assoc, vol_a, dipm, dip_num, z, dielc)
hres = (-t*dares_dt + (Z-1))*kb*N_AV*t # Equation A.46 from Gross and Sadowski 2001
return hres
def pcsaft_sres(x, m, s, e, t, rho, k_ij=None, e_assoc=None, vol_a=None, dipm=None, \
dip_num=None, z=None, dielc=None):
"""
Calculate the residual entropy (constant volume) for one phase of the system.
Parameters
----------
x : ndarray, shape (n,)
Mole fractions of each component. It has a length of n, where n is
the number of components in the system.
m : ndarray, shape (n,)
Segment number for each component.
s : ndarray, shape (n,)
Segment diameter for each component. For ions this is the diameter of
the hydrated ion. Units of Angstrom.
e : ndarray, shape (n,)
Dispersion energy of each component. For ions this is the dispersion
energy of the hydrated ion. Units of K.
t : float
Temperature (K)
rho : float
Molar density (mol m^{-3})
k_ij : ndarray, shape (n,n)
Binary interaction parameters between components in the mixture.
(dimensions: ncomp x ncomp)
e_assoc : ndarray, shape (n,)
Association energy of the associating components. For non associating
compounds this is set to 0. Units of K.
vol_a : ndarray, shape (n,)
Effective association volume of the associating components. For non
associating compounds this is set to 0.
dipm : ndarray, shape (n,)
Dipole moment of the polar components. For components where the dipole
term is not used this is set to 0. Units of Debye.
dip_num : ndarray, shape (n,)
The effective number of dipole functional groups on each component
molecule. Some implementations use this as an adjustable parameter
that is fit to data.
z : ndarray, shape (n,)
Charge number of the ions
dielc : float
Dielectric constant of the medium to be used for electrolyte
calculations.
Returns
-------
sres : float
Residual entropy (J mol^{-1} K^{-1})
"""
gres = pcsaft_gres(x, m, s, e, t, rho, k_ij, e_assoc, vol_a, dipm, dip_num, z, dielc)
hres = pcsaft_hres(x, m, s, e, t, rho, k_ij, e_assoc, vol_a, dipm, dip_num, z, dielc)
sres = (hres - gres)/t
return sres
def pcsaft_gres(x, m, s, e, t, rho, k_ij=None, e_assoc=None, vol_a=None, dipm=None, \
dip_num=None, z=None, dielc=None):
"""
Calculate the residual Gibbs energy for one phase of the system.
Parameters
----------
x : ndarray, shape (n,)
Mole fractions of each component. It has a length of n, where n is
the number of components in the system.
m : ndarray, shape (n,)
Segment number for each component.
s : ndarray, shape (n,)
Segment diameter for each component. For ions this is the diameter of
the hydrated ion. Units of Angstrom.
e : ndarray, shape (n,)
Dispersion energy of each component. For ions this is the dispersion
energy of the hydrated ion. Units of K.
t : float
Temperature (K)
rho : float
Molar density (mol m^{-3})
k_ij : ndarray, shape (n,n)
Binary interaction parameters between components in the mixture.
(dimensions: ncomp x ncomp)
e_assoc : ndarray, shape (n,)
Association energy of the associating components. For non associating
compounds this is set to 0. Units of K.
vol_a : ndarray, shape (n,)
Effective association volume of the associating components. For non
associating compounds this is set to 0.
dipm : ndarray, shape (n,)
Dipole moment of the polar components. For components where the dipole
term is not used this is set to 0. Units of Debye.
dip_num : ndarray, shape (n,)
The effective number of dipole functional groups on each component
molecule. Some implementations use this as an adjustable parameter
that is fit to data.
z : ndarray, shape (n,)
Charge number of the ions
dielc : float
Dielectric constant of the medium to be used for electrolyte
calculations.
Returns
-------
gres : float
Residual Gibbs energy (J mol^{-1})
"""
kb = 1.380648465952442093e-23 # Boltzmann constant, J K^-1
N_AV = 6.022140857e23 # Avagadro's number
ares = pcsaft_ares(x, m, s, e, t, rho, k_ij, e_assoc, vol_a, dipm, dip_num, z, dielc)
Z = pcsaft_Z(x, m, s, e, t, rho, k_ij, e_assoc, vol_a, dipm, dip_num, z, dielc)
gres = (ares + (Z - 1) - np.log(Z))*kb*N_AV*t # Equation A.50 from Gross and Sadowski 2001
return gres
def pcsaft_fugcoef(x, m, s, e, t, rho, k_ij=None, e_assoc=None, vol_a=None, dipm=None, \
dip_num=None, z=None, dielc=None):
"""
Calculate the fugacity coefficients for one phase of the system.
Parameters
----------
x : ndarray, shape (n,)
Mole fractions of each component. It has a length of n, where n is
the number of components in the system.
m : ndarray, shape (n,)
Segment number for each component.
s : ndarray, shape (n,)
Segment diameter for each component. For ions this is the diameter of
the hydrated ion. Units of Angstrom.
e : ndarray, shape (n,)
Dispersion energy of each component. For ions this is the dispersion
energy of the hydrated ion. Units of K.
t : float
Temperature (K)
rho : float
Molar density (mol m^{-3})
k_ij : ndarray, shape (n,n)
Binary interaction parameters between components in the mixture.
(dimensions: ncomp x ncomp)
e_assoc : ndarray, shape (n,)
Association energy of the associating components. For non associating
compounds this is set to 0. Units of K.
vol_a : ndarray, shape (n,)
Effective association volume of the associating components. For non
associating compounds this is set to 0.
dipm : ndarray, shape (n,)
Dipole moment of the polar components. For components where the dipole
term is not used this is set to 0. Units of Debye.
dip_num : ndarray, shape (n,)
The effective number of dipole functional groups on each component
molecule. Some implementations use this as an adjustable parameter
that is fit to data.
z : ndarray, shape (n,)
Charge number of the ions
dielc : float
Dielectric constant of the medium to be used for electrolyte
calculations.
Returns
-------
fugcoef : ndarray, shape (n,)
Fugacity coefficients of each component.
"""
ncomp = x.shape[0] # number of components
kb = 1.380648465952442093e-23 # Boltzmann constant, J K^-1
N_AV = 6.022140857e23 # Avagadro's number
d = s*(1-0.12*np.exp(-3*e/t))
if type(z) == np.ndarray:
d[np.where(z != 0)[0]] = s[np.where(z != 0)[0]]*(1-0.12) # for ions the diameter is assumed to be temperature independent (see Held et al. 2014)
den = rho*N_AV/1.0e30
if type(k_ij) != np.ndarray:
k_ij = np.zeros((ncomp,ncomp), dtype='float_')
zeta = np.zeros((4,), dtype='float_')
ghs = np.zeros((ncomp,ncomp), dtype='float_')
e_ij = np.zeros_like(ghs)
s_ij = np.zeros_like(ghs)
denghs = np.zeros_like(ghs)