-
Notifications
You must be signed in to change notification settings - Fork 18
/
pcsaft.pyx
1357 lines (1231 loc) · 56.1 KB
/
pcsaft.pyx
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 -*-
# setuptools: language=c++
import numpy as np
from libcpp.vector cimport vector
from copy import deepcopy
cimport pcsaft
class InputError(Exception):
# Exception raised for errors in the input.
def __init__(self, message):
self.message = message
class SolutionError(Exception):
# Exception raised when a solver does not return a value.
def __init__(self, message):
self.message = message
def check_input(x, vars):
if abs(np.sum(x) - 1) > 1e-7:
raise InputError('The mole fractions do not sum to 1. x = {}'.format(x))
if 'temperature' in vars:
if vars['temperature'] <= 0:
raise InputError('The {} must be a positive number. {} = {}'.format('temperature', 'temperature', vars['temperature']))
if 'density' in vars:
if vars['density'] <= 0:
raise InputError('The {} must be a positive number. {} = {}'.format('density', 'density', vars['density']))
if 'pressure' in vars:
if vars['pressure'] <= 0:
raise InputError('The {} must be a positive number. {} = {}'.format('pressure', 'pressure', vars['pressure']))
if 'Q' in vars:
if (vars['Q'] < 0) or (vars['Q'] > 1):
raise InputError('{} must be <= 1 and >= 0. {} = {}'.format('Q', 'Q', vars['Q']))
def check_association(params):
if ('e_assoc' in params) and ('vol_a' not in params):
raise InputError('e_assoc was given, but not vol_a.')
elif ('vol_a' in params) and ('e_assoc' not in params):
raise InputError('vol_a was given, but not e_assoc.')
if ('e_assoc' in params) and ('assoc_scheme' not in params):
params['assoc_scheme'] = []
for a in params['vol_a']:
if a != 0:
params['assoc_scheme'].append('2b')
else:
params['assoc_scheme'].append(None)
if ('e_assoc' in params):
params = create_assoc_matrix(params)
return params
def create_assoc_matrix(params):
charge = [] # whether the association site has a partial positive charge (i.e. hydrogen), negative charge, or elements of both (e.g. for acids modelled as type 1)
scheme_charges = {
'1': [0],
'2a': [0, 0],
'2b': [-1, 1],
'3a': [0, 0, 0],
'3b': [-1, -1, 1],
'4a': [0, 0, 0, 0],
'4b': [1, 1, 1, -1],
'4c': [-1, -1, 1, 1]
}
assoc_num = []
for comp in params['assoc_scheme']:
if comp is None:
assoc_num.append(0)
pass
elif type(comp) is list:
num = 0
for site in comp:
if site.lower() not in scheme_charges:
raise InputError('{} is not a valid association type.'.format(site))
charge.extend(scheme_charges[site.lower()])
num += len(scheme_charges[site.lower()])
assoc_num.append(num)
else:
if comp.lower() not in scheme_charges:
raise InputError('{} is not a valid association type.'.format(comp))
charge.extend(scheme_charges[comp.lower()])
assoc_num.append(len(scheme_charges[comp.lower()]))
params['assoc_num'] = np.asarray(assoc_num)
params['assoc_matrix'] = np.zeros((len(charge)*len(charge)))
ctr = 0
for c1 in charge:
for c2 in charge:
if (c1 == 0 or c2 == 0):
params['assoc_matrix'][ctr] = 1;
elif (c1 == 1 and c2 == -1):
params['assoc_matrix'][ctr] = 1;
elif (c1 == -1 and c2 == 1):
params['assoc_matrix'][ctr] = 1;
else:
params['assoc_matrix'][ctr] = 0;
ctr += 1
return params
def ensure_numpy_input(x, params):
if type(x) == np.float_:
x = np.asarray([x])
if type(params['m']) == np.float_:
params['m'] = np.asarray([params['m']])
if type(params['s']) == np.float_:
params['s'] = np.asarray([params['s']])
if type(params['e']) == np.float_:
params['e'] = np.asarray([params['e']])
return x, params
def pcsaft_p(t, rho, x, params):
"""
Calculate pressure.
Parameters
----------
t : float
Temperature (K)
rho : float
Molar density (mol m\ :sup:`-3`)
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.
params : dict
A dictionary containing PC-SAFT parameters that can be passed for
use in PC-SAFT:
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.
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. Generally this is set to 1, but 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.
assoc_scheme : list, shape (n,)
The types of association sites for each component. Use `None` for molecules
without association sites. If a molecule has multiple association sites,
use a nested list for that component to specify the association scheme for
each site. The accepted association schemes are those given by Huang and
Radosz (1990): 1, 2A, 2B, 3A, 3B, 4A, 4B, 4C. If `e_assoc` and `vol_a` are
given but `assoc_scheme` is not, the 2B association scheme is assumed (which
would, for example, correspond to one hydroxyl functional group).
Returns
-------
P : float
Pressure (Pa)
"""
x, params = ensure_numpy_input(x, params)
check_input(x, {'density':rho, 'temperature':t})
params = check_association(params)
cppargs = create_struct(params)
return pcsaft_p_cpp(t, rho, x, cppargs)
def pcsaft_lnfugcoef(t, rho, x, params):
"""
Calculate the natural logarithm of the fugacity coefficients for one phase of the system.
Parameters
----------
t : float
Temperature (K)
rho : float
Molar density (mol m\ :sup:`-3`)
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.
params : dict
A dictionary containing PC-SAFT parameters that can be passed for
use in PC-SAFT:
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.
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. Generally this is set to 1, but 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.
assoc_scheme : list, shape (n,)
The types of association sites for each component. Use `None` for molecules
without association sites. If a molecule has multiple association sites,
use a nested list for that component to specify the association scheme for
each site. The accepted association schemes are those given by Huang and
Radosz (1990): 1, 2A, 2B, 3A, 3B, 4A, 4B, 4C. If `e_assoc` and `vol_a` are
given but `assoc_scheme` is not, the 2B association scheme is assumed (which
would, for example, correspond to one hydroxyl functional group).
Returns
-------
lnfugcoef : ndarray, shape (n,)
Natural logarithm of the fugacity coefficients for each component.
"""
x, params = ensure_numpy_input(x, params)
check_input(x, {'density':rho, 'temperature':t})
params = check_association(params)
cppargs = create_struct(params)
return np.asarray(pcsaft_lnfug_cpp(t, rho, x, cppargs))
def pcsaft_fugcoef(t, rho, x, params):
"""
Calculate the fugacity coefficients for one phase of the system.
Parameters
----------
t : float
Temperature (K)
rho : float
Molar density (mol m\ :sup:`-3`)
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.
params : dict
A dictionary containing PC-SAFT parameters that can be passed for
use in PC-SAFT:
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.
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. Generally this is set to 1, but 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.
assoc_scheme : list, shape (n,)
The types of association sites for each component. Use `None` for molecules
without association sites. If a molecule has multiple association sites,
use a nested list for that component to specify the association scheme for
each site. The accepted association schemes are those given by Huang and
Radosz (1990): 1, 2A, 2B, 3A, 3B, 4A, 4B, 4C. If `e_assoc` and `vol_a` are
given but `assoc_scheme` is not, the 2B association scheme is assumed (which
would, for example, correspond to one hydroxyl functional group).
Returns
-------
fugcoef : ndarray, shape (n,)
Fugacity coefficients of each component.
"""
x, params = ensure_numpy_input(x, params)
check_input(x, {'density':rho, 'temperature':t})
params = check_association(params)
cppargs = create_struct(params)
return np.asarray(pcsaft_fugcoef_cpp(t, rho, x, cppargs))
def pcsaft_Z(t, rho, x, params):
"""
Calculate the compressibility factor.
Parameters
----------
t : float
Temperature (K)
rho : float
Molar density (mol m\ :sup:`-3`)
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.
params : dict
A dictionary containing PC-SAFT parameters that can be passed for
use in PC-SAFT:
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.
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. Generally this is set to 1, but 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.
assoc_scheme : list, shape (n,)
The types of association sites for each component. Use `None` for molecules
without association sites. If a molecule has multiple association sites,
use a nested list for that component to specify the association scheme for
each site. The accepted association schemes are those given by Huang and
Radosz (1990): 1, 2A, 2B, 3A, 3B, 4A, 4B, 4C. If `e_assoc` and `vol_a` are
given but `assoc_scheme` is not, the 2B association scheme is assumed (which
would, for example, correspond to one hydroxyl functional group).
Returns
-------
Z : float
Compressibility factor
"""
x, params = ensure_numpy_input(x, params)
check_input(x, {'density':rho, 'temperature':t})
params = check_association(params)
cppargs = create_struct(params)
return pcsaft_Z_cpp(t, rho, x, cppargs)
def flashPQ(p, q, x, params, t_guess=None):
"""
Calculate the temperature of the system where vapor and liquid phases are in equilibrium.
Parameters
----------
p : float
Pressure (Pa)
q : float
Mole fraction of the fluid in the vapor phase
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.
params : dict
A dictionary containing PC-SAFT parameters that can be passed for
use in PC-SAFT:
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.
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. Generally this is set to 1, but 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.
assoc_scheme : list, shape (n,)
The types of association sites for each component. Use `None` for molecules
without association sites. If a molecule has multiple association sites,
use a nested list for that component to specify the association scheme for
each site. The accepted association schemes are those given by Huang and
Radosz (1990): 1, 2A, 2B, 3A, 3B, 4A, 4B, 4C. If `e_assoc` and `vol_a` are
given but `assoc_scheme` is not, the 2B association scheme is assumed (which
would, for example, correspond to one hydroxyl functional group).
t_guess : float
Initial guess for the temperature (K) (optional)
Returns
-------
t : float
Temperature (K)
xl : ndarray, shape (n,)
Liquid mole fractions after flash
xv : ndarray, shape (n,)
Vapor mole fractions after flash
Notes
-----
To solve the PQ flash the temperature must be varied. This adds additional complexity
for water and electrolyte mixtures. For water, a temperature dependent sigma is often
used. However, there does not appear to be a way to pass a Python function to the C++
code without requiring the user to compile it using Cython. To avoid this, the `flashPQ`
function uses the following relationship internally to calculate sigma for water as a
function of temperature: ::
3.8395 + 1.2828 * exp(-0.0074944 * t) - 1.3939 * exp(-0.00056029 * t);
For electrolyte solutions the dielectric constant is calculated using the `dielc_water`
function. This means that the sigma value for water and the dielectric constant given by
the user are not used by the `flashPQ` function.
The code identifies which component is water by the epsilon/k value. Therefore, when
using `flashPQ` with water `e` must be exactly 353.9449, if you want the temperature
dependence of sigma to be accounted for.
If you want to use different functions for temperature dependent parameters with `flashPQ`
then you will need to modify the source code and recompile it.
"""
x, params = ensure_numpy_input(x, params)
check_input(x, {'pressure':p, 'Q':q})
params = check_association(params)
cppargs = create_struct(params)
try:
if t_guess is not None:
result = flashPQ_cpp(p, q, x, cppargs, t_guess)
else:
result = flashPQ_cpp(p, q, x, cppargs)
except:
raise SolutionError('A solution was not found for flashPQ. P={}'.format(p))
t = result[0]
xl = np.asarray(result[1:])
xl, xv = np.split(xl, 2)
return t, xl, xv
def flashTQ(t, q, x, params, p_guess=None):
"""
Calculate the pressure of the system where vapor and liquid phases are in equilibrium.
Parameters
----------
t : float
Temperature (K)
q : float
Mole fraction of the fluid in the vapor phase
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.
params : dict
A dictionary containing PC-SAFT parameters that can be passed for
use in PC-SAFT:
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.
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. Generally this is set to 1, but 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.
assoc_scheme : list, shape (n,)
The types of association sites for each component. Use `None` for molecules
without association sites. If a molecule has multiple association sites,
use a nested list for that component to specify the association scheme for
each site. The accepted association schemes are those given by Huang and
Radosz (1990): 1, 2A, 2B, 3A, 3B, 4A, 4B, 4C. If `e_assoc` and `vol_a` are
given but `assoc_scheme` is not, the 2B association scheme is assumed (which
would, for example, correspond to one hydroxyl functional group).
p_guess : float
Initial guess for the pressure (Pa) (optional)
Returns
-------
p : float
Pressure (Pa)
xl : ndarray, shape (n,)
Liquid mole fractions after flash
xv : ndarray, shape (n,)
Vapor mole fractions after flash
"""
x, params = ensure_numpy_input(x, params)
check_input(x, {'temperature':t, 'Q':q})
params = check_association(params)
cppargs = create_struct(params)
try:
if p_guess is not None:
result = flashTQ_cpp(t, q, x, cppargs, p_guess)
else:
result = flashTQ_cpp(t, q, x, cppargs)
except:
raise SolutionError('A solution was not found for flashTQ. T={}'.format(t))
p = result[0]
xl = np.asarray(result[1:])
xl, xv = np.split(xl, 2)
return p, xl, xv
def pcsaft_Hvap(t, x, params, p_guess=None):
"""
Calculate the enthalpy of vaporization.
Parameters
----------
t : float
Temperature (K)
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.
params : dict
A dictionary containing PC-SAFT parameters that can be passed for
use in PC-SAFT:
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.
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. Generally this is set to 1, but 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.
assoc_scheme : list, shape (n,)
The types of association sites for each component. Use `None` for molecules
without association sites. If a molecule has multiple association sites,
use a nested list for that component to specify the association scheme for
each site. The accepted association schemes are those given by Huang and
Radosz (1990): 1, 2A, 2B, 3A, 3B, 4A, 4B, 4C. If `e_assoc` and `vol_a` are
given but `assoc_scheme` is not, the 2B association scheme is assumed (which
would, for example, correspond to one hydroxyl functional group).
p_guess : float
Guess for the vapor pressure (Pa) (optional)
Returns
-------
output : list
A list containing the following results:
0 : enthalpy of vaporization (J/mol), float
1 : vapor pressure (Pa), float
"""
x, params = ensure_numpy_input(x, params)
check_input(x, {'temperature': t})
params = check_association(params)
cppargs = create_struct(params)
q = 0
try:
if p_guess is not None:
result = np.asarray(flashTQ_cpp(t, q, x, cppargs, p_guess))
Pvap = result[0]
else:
result = np.asarray(flashTQ_cpp(t, q, x, cppargs))
Pvap = result[0]
except:
raise SolutionError('A solution was not found for flashTQ. T={}'.format(t))
rho = pcsaft_den_cpp(t, Pvap, x, 0, cppargs)
hres_l = pcsaft_hres_cpp(t, rho, x, cppargs)
rho = pcsaft_den_cpp(t, Pvap, x, 1, cppargs)
hres_v = pcsaft_hres_cpp(t, rho, x, cppargs)
Hvap = hres_v - hres_l
output = [Hvap, Pvap]
return output
def pcsaft_osmoticC(t, rho, x, params):
"""
Calculate the osmotic coefficient.
Parameters
----------
t : float
Temperature (K)
rho : float
Molar density (mol m\ :sup:`-3`)
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.
params : dict
A dictionary containing PC-SAFT parameters that can be passed for
use in PC-SAFT:
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.
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. Generally this is set to 1, but 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.
assoc_scheme : list, shape (n,)
The types of association sites for each component. Use `None` for molecules
without association sites. If a molecule has multiple association sites,
use a nested list for that component to specify the association scheme for
each site. The accepted association schemes are those given by Huang and
Radosz (1990): 1, 2A, 2B, 3A, 3B, 4A, 4B, 4C. If `e_assoc` and `vol_a` are
given but `assoc_scheme` is not, the 2B association scheme is assumed (which
would, for example, correspond to one hydroxyl functional group).
Returns
-------
osmC : float
Molal osmotic coefficient
"""
x, params = ensure_numpy_input(x, params)
check_input(x, {'density':rho, 'temperature':t})
params = check_association(params)
cppargs = create_struct(params)
indx_water = np.where(params['e'] == 353.9449)[0] # to find index for water
molality = x/(x[indx_water]*18.0153/1000.)
molality[indx_water] = 0
x0 = np.zeros_like(x)
x0[indx_water] = 1.
fugcoef = np.asarray(pcsaft_fugcoef_cpp(t, rho, x, cppargs))
p = pcsaft_p_cpp(t, rho, x, cppargs)
if rho < 900:
ph = 1
else:
ph = 0
rho0 = pcsaft_den_cpp(t, p, x0, ph, cppargs)
fugcoef0 = np.asarray(pcsaft_fugcoef_cpp(t, rho0, x0, cppargs))
gamma = fugcoef[indx_water]/fugcoef0[indx_water]
osmC = -1000*np.log(x[indx_water]*gamma)/18.0153/np.sum(molality)
return osmC
def pcsaft_cp(t, rho, aly_lee_params, x, params):
"""
Calculate the specific molar isobaric heat capacity.
Parameters
----------
t : float
Temperature (K)
rho : float
Molar density (mol m\ :sup:`-3`)
aly_lee_params : ndarray, shape (5,)
Constants for the Aly-Lee equation. Can be substituted with parameters for
another equation if the ideal gas heat capacity is given using a different
equation.
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.
params : dict
A dictionary containing PC-SAFT parameters that can be passed for
use in PC-SAFT:
m : ndarray, shape (n,)
Segment number for each component.
s : ndarray, shape (n,)
Segment diameter for each compopynent. 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.
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. Generally this is set to 1, but 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.
assoc_scheme : list, shape (n,)
The types of association sites for each component. Use `None` for molecules
without association sites. If a molecule has multiple association sites,
use a nested list for that component to specify the association scheme for
each site. The accepted association schemes are those given by Huang and
Radosz (1990): 1, 2A, 2B, 3A, 3B, 4A, 4B, 4C. If `e_assoc` and `vol_a` are
given but `assoc_scheme` is not, the 2B association scheme is assumed (which
would, for example, correspond to one hydroxyl functional group).
Returns
-------
cp : float
Specific molar isobaric heat capacity (J mol\ :sup:`-1` K\ :sup:`-1`)
"""
x, params = ensure_numpy_input(x, params)
check_input(x, {'density':rho, 'temperature':t})
params = check_association(params)
if rho > 900:
ph = 0
else:
ph = 1
cppargs = create_struct(params)
cp_ideal = aly_lee(t, aly_lee_params)
p = pcsaft_p_cpp(t, rho, x, cppargs)
rho0 = pcsaft_den_cpp(t-0.001, p, x, ph, cppargs)
hres0 = pcsaft_hres_cpp(t-0.001, rho0, x, cppargs)
rho1 = pcsaft_den_cpp(t+0.001, p, x, ph, cppargs)
hres1 = pcsaft_hres_cpp(t+0.001, rho1, x, cppargs)
dhdt = (hres1-hres0)/0.002 # a numerical derivative is used for now until analytical derivatives are ready
return cp_ideal + dhdt
def pcsaft_den(t, p, x, params, phase='liq'):
"""
Calculate the molar density.
Parameters
----------
t : float
Temperature (K)
p : float
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.
params : dict
A dictionary containing PC-SAFT parameters that can be passed for
use in PC-SAFT:
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.
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. Generally this is set to 1, but 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.
assoc_scheme : list, shape (n,)
The types of association sites for each component. Use `None` for molecules
without association sites. If a molecule has multiple association sites,
use a nested list for that component to specify the association scheme for
each site. The accepted association schemes are those given by Huang and
Radosz (1990): 1, 2A, 2B, 3A, 3B, 4A, 4B, 4C. If `e_assoc` and `vol_a` are
given but `assoc_scheme` is not, the 2B association scheme is assumed (which
would, for example, correspond to one hydroxyl functional group).
phase : string
The phase for which the calculation is performed. Options: "liq" (liquid),
"vap" (vapor).
Returns
-------
rho : float
Molar density (mol m\ :sup:`-3`)
"""
x, params = ensure_numpy_input(x, params)
check_input(x, {'pressure':p, 'temperature':t})
params = check_association(params)
cppargs = create_struct(params)
if phase == 'liq':
phase_num = 0
else:
phase_num = 1
return pcsaft_den_cpp(t, p, x, phase_num, cppargs)
def pcsaft_hres(t, rho, x, params):
"""
Calculate the residual enthalpy for one phase of the system.
Parameters
----------
t : float
Temperature (K)
rho : float
Molar density (mol m\ :sup:`-3`)
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.
params : dict
A dictionary containing PC-SAFT parameters that can be passed for
use in PC-SAFT:
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.
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. Generally this is set to 1, but 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.
assoc_scheme : list, shape (n,)
The types of association sites for each component. Use `None` for molecules
without association sites. If a molecule has multiple association sites,
use a nested list for that component to specify the association scheme for
each site. The accepted association schemes are those given by Huang and
Radosz (1990): 1, 2A, 2B, 3A, 3B, 4A, 4B, 4C. If `e_assoc` and `vol_a` are
given but `assoc_scheme` is not, the 2B association scheme is assumed (which
would, for example, correspond to one hydroxyl functional group).
Returns
-------
hres : float
Residual enthalpy (J mol\ :sup:`-1`)
"""
x, params = ensure_numpy_input(x, params)
check_input(x, {'density':rho, 'temperature':t})
params = check_association(params)
cppargs = create_struct(params)
return pcsaft_hres_cpp(t, rho, x, cppargs)
def pcsaft_sres(t, rho, x, params):
"""
Calculate the residual entropy (constant volume) for one phase of the system.
Parameters
----------
t : float
Temperature (K)
rho : float
Molar density (mol m\ :sup:`-3`)
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.
params : dict
A dictionary containing PC-SAFT parameters that can be passed for
use in PC-SAFT:
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.
k_ij : ndarray, shape (n,n)
Binary interaction parameters between components in the mixture.