-
Notifications
You must be signed in to change notification settings - Fork 10
/
thermo.py
5161 lines (4421 loc) · 204 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
"""
This is the core of the python interface to ThermoPack. All equation of state classes on the python side inherit from
the thermo class in this file. Please note that the docstrings of the methods in this file are used to generate
the markdown-documentation found elsewhere (the ThermoTools wiki, etc.). Therefore, new methods that are implemented
must conform to the following style guide for docstrings:
1 : The first line of the docstring must include a "section name" (i.e. No leading blank line)
2 : The leading description of the method must follow directly on the line following the "section name"
(i.e. no intermediate blank line).
3 : There must be (at least) one blank line following the leading description
4 : The argument list, and return list, must be preceded by a line containing the word "Args" or "Returns"
5 : The argument list, and return list, must be written as 'param_name (type) : Description', where the essential
part is the colon is included as a separator.
An example is:
def myfunc(self, p1, p2, p3, p4=None, p5=<something>):
'''Section name
Description of what this function does (a kind of header). We can write lots of stuff here
NOTE the double lineshift here:
Args:
p1 (int) : The lineshift before 'Args' is necessary.
p2 (float) : The colons here are also necessary.
p3 (bool) : SomethingSomething
p4 (Optional, list) : etc.
p5 (Optional, <Something>) : This is the last argument
Returns:
(float) : The colon here is also necessary.
'''
"""
import sys
from ctypes import *
from os import path
import copy
import numpy as np
from . import plotutils, utils, platform_specifics
import warnings
c_len_type = c_size_t # c_size_t on GCC > 7 else c_len_type = c_int
n_max_cp = 21
class thermo(object):
"""
Interface to thermopack
"""
def __new__(cls, *args, **kwargs):
"""Internal
Get platform specifics and Load libthermopack.(so/dll/dylib)
"""
pf_specifics = platform_specifics.get_platform_specifics()
dyn_lib_path = path.join(path.dirname(__file__), pf_specifics["dyn_lib"])
tp = cdll.LoadLibrary(dyn_lib_path)
self = object.__new__(cls)
self.pf_specifics = pf_specifics
self.dyn_lib_path = dyn_lib_path
self.tp = tp
return self
def __init__(self):
"""Internal
Initialize function pointers
"""
self.prefix = self.pf_specifics["prefix"]
self.module = self.pf_specifics["module"]
self.postfix = self.pf_specifics["postfix"]
self.postfix_nm = self.pf_specifics["postfix_no_module"]
# Set phase flags
self.s_get_phase_flags = self.tp.get_phase_flags_c
self.get_phase_flags()
# Model control
self.s_add_eos = getattr(
self.tp, self.get_export_name("thermopack_var", "add_eos"))
self.s_delete_eos = getattr(
self.tp, self.get_export_name("thermopack_var", "delete_eos"))
self.s_delete_eos.argtypes = [POINTER(c_int)]
self.s_activate_model = getattr(
self.tp, self.get_export_name("thermopack_var", "activate_model"))
# Information
self.s_get_model_id = getattr(self.tp, self.get_export_name(
"thermopack_var", "get_eos_identification"))
# Init methods
self.eoslibinit_init_thermo = getattr(
self.tp, self.get_export_name("eoslibinit", "init_thermo"))
self.s_get_rgas = getattr(
self.tp, self.get_export_name("thermopack_var", "get_rgas"))
self.nc = None
self.s_get_numerical_robustness_level = getattr(
self.tp, self.get_export_name("thermopack_var", "get_numerical_robustness_level"))
self.s_set_numerical_robustness_level = getattr(
self.tp, self.get_export_name("thermopack_var", "set_numerical_robustness_level"))
self.s_get_tmin = getattr(
self.tp, self.get_export_name("thermopack_var", "get_tmin"))
self.s_set_tmin = getattr(
self.tp, self.get_export_name("thermopack_var", "set_tmin"))
self.s_get_tmax = getattr(
self.tp, self.get_export_name("thermopack_var", "get_tmax"))
self.s_set_tmax = getattr(
self.tp, self.get_export_name("thermopack_var", "set_tmax"))
self.s_get_pmin = getattr(
self.tp, self.get_export_name("thermopack_var", "get_pmin"))
self.s_set_pmin = getattr(
self.tp, self.get_export_name("thermopack_var", "set_pmin"))
self.s_get_pmax = getattr(
self.tp, self.get_export_name("thermopack_var", "get_pmax"))
self.s_set_pmax = getattr(
self.tp, self.get_export_name("thermopack_var", "set_pmax"))
self.solideos_solid_init = getattr(
self.tp, self.get_export_name("solideos", "solid_init"))
self.solideos_solid_volume = getattr(
self.tp, self.get_export_name("solideos", "solid_specificvolume"))
self.solideos_solid_enthalpy = getattr(
self.tp, self.get_export_name("solideos", "solid_enthalpy"))
self.solideos_solid_entropy = getattr(
self.tp, self.get_export_name("solideos", "solid_entropy"))
self.eoslibinit_init_volume_translation = getattr(
self.tp, self.get_export_name("eoslibinit", "init_volume_translation"))
self.eoslibinit_redefine_critical_parameters = getattr(
self.tp, self.get_export_name("eoslibinit", "redefine_critical_parameters"))
# Eos interface
self.s_eos_specificvolume = getattr(
self.tp, self.get_export_name("eos", "specificvolume"))
self.s_eos_zfac = getattr(self.tp, self.get_export_name("eos", "zfac"))
self.s_eos_thermo = getattr(
self.tp, self.get_export_name("eos", "thermo"))
self.s_eos_entropy = getattr(
self.tp, self.get_export_name("eos", "entropy"))
self.s_eos_enthalpy = getattr(
self.tp, self.get_export_name("eos", "enthalpy"))
self.s_eos_compmoleweight = getattr(
self.tp, self.get_export_name("eos", "compmoleweight"))
self.s_eos_moleweight = getattr(
self.tp, self.get_export_name("eos", "moleweight"))
self.s_eos_getCriticalParam = getattr(
self.tp, self.get_export_name("eos", "getcriticalparam"))
# Ideal property interface
self.s_ideal_idealenthalpysingle = getattr(self.tp, self.get_export_name(
"eos", "ideal_enthalpy_single"))
self.s_eos_idealentropysingle = getattr(self.tp, self.get_export_name(
"eos", "ideal_entropy_single"))
self.s_ideal_get_standard_entropy = getattr(self.tp, self.get_export_name(
"ideal", "get_standard_entropy"))
self.s_ideal_set_standard_entropy = getattr(self.tp, self.get_export_name(
"ideal", "set_standard_entropy"))
self.s_ideal_get_enthalpy_of_formation = getattr(self.tp, self.get_export_name(
"ideal", "get_enthalpy_of_formation"))
self.s_ideal_set_enthalpy_of_formation = getattr(self.tp, self.get_export_name(
"ideal", "set_enthalpy_of_formation"))
self.s_get_ideal_cp_correlation = getattr(self.tp, self.get_export_name(
"compdata", "get_ideal_cp_correlation"))
self.s_set_ideal_cp_correlation = getattr(self.tp, self.get_export_name(
"compdata", "set_ideal_cp_correlation"))
# Speed of sound
self.s_sos_sound_velocity_2ph = getattr(
self.tp, self.get_export_name("speed_of_sound", "sound_velocity_2ph"))
self.s_speed_of_sound_tv = getattr(
self.tp, self.get_export_name("speed_of_sound", "speed_of_sound_tv"))
# Component info
self.s_compdata_compindex = getattr(
self.tp, self.get_export_name("compdata", "comp_index_active"))
self.s_compdata_compname = getattr(
self.tp, self.get_export_name("compdata", "comp_name_active"))
self.s_compdata_structure = getattr(
self.tp, self.get_export_name("compdata", "comp_structure"))
# Flashes
self.s_set_ph_tolerance = getattr(
self.tp, self.get_export_name("ph_solver", "setphtolerance"))
self.s_twophasetpflash = getattr(
self.tp, self.get_export_name("tp_solver", "twophasetpflash"))
self.s_psflash_twophase = getattr(
self.tp, self.get_export_name("ps_solver", "twophasepsflash"))
#self.tpflash_multiphase = getattr(self.tp, '__mp_tp_solver_MOD_mp_flash_tp')
self.s_uvflash_twophase = getattr(
self.tp, self.get_export_name("uv_solver", "twophaseuvflash"))
self.s_phflash_twophase = getattr(
self.tp, self.get_export_name("ph_solver", "twophasephflash"))
#self.s_svflash_twophase = getattr(self.tp, self.get_export_name("sv_solver", "twophasesvflash"))
self.s_guess_phase = getattr(
self.tp, self.get_export_name("thermo_utils", "guessphase"))
# TV interfaces
self.s_internal_energy_tv = getattr(
self.tp, self.get_export_name("eostv", "internal_energy_tv"))
self.s_entropy_tv = getattr(
self.tp, self.get_export_name("eostv", "entropy_tv"))
self.s_pressure_tv = getattr(
self.tp, self.get_export_name("eostv", "pressure"))
self.s_lnphi_tv = getattr(
self.tp, self.get_export_name("eostv", "thermo_tv"))
self.s_enthalpy_tv = getattr(
self.tp, self.get_export_name("eostv", "enthalpy_tv"))
self.s_helmholtz_energy = getattr(
self.tp, self.get_export_name("eostv", "free_energy_tv"))
self.s_chempot = getattr(self.tp, self.get_export_name(
"eostv", "chemical_potential_tv"))
# TVP interfaces
self.s_entropy_tvp = getattr(
self.tp, self.get_export_name("eostv", "entropy_tvp"))
self.s_thermo_tvp = getattr(
self.tp, self.get_export_name("eostv", "thermo_tvp"))
self.s_enthalpy_tvp = getattr(
self.tp, self.get_export_name("eostv", "enthalpy_tvp"))
# Saturation properties
self.s_bubble_t = getattr(
self.tp, self.get_export_name("saturation", "safe_bubt"))
self.s_bubble_p = getattr(
self.tp, self.get_export_name("saturation", "safe_bubp"))
self.s_dew_t = getattr(
self.tp, self.get_export_name("saturation", "safe_dewt"))
self.s_dew_p = getattr(
self.tp, self.get_export_name("saturation", "safe_dewp"))
self.s_envelope_plot = getattr(
self.tp, self.get_export_name("saturation_curve", "envelopeplot"))
self.s_pure_fluid_saturation_wrapper = getattr(
self.tp, self.get_export_name("saturation_curve",
"pure_fluid_saturation_wrapper"))
self.s_binary_plot = getattr(
self.tp, self.get_export_name("binaryplot", "vllebinaryxy"))
self.s_global_binary_plot = getattr(
self.tp, self.get_export_name("binaryplot", "global_binary_plot"))
self.s_get_bp_term = getattr(
self.tp, self.get_export_name("binaryplot", "get_bp_term"))
self.s_three_phase_line = getattr(
self.tp, self.get_export_name("binaryplot", "threephaseline"))
self.s_solid_envelope_plot = getattr(
self.tp, self.get_export_name("solid_saturation", "solidenvelopeplot"))
self.s_melting_pressure_correlation = getattr(
self.tp, self.get_export_name("solid_saturation", "melting_pressure_correlation"))
self.s_sublimation_pressure_correlation = getattr(
self.tp, self.get_export_name("solid_saturation", "sublimation_pressure_correlation"))
self.s_isotherm = getattr(
self.tp, self.get_export_name("isolines", "isotherm"))
self.s_isobar = getattr(
self.tp, self.get_export_name("isolines", "isobar"))
self.s_isenthalp = getattr(
self.tp, self.get_export_name("isolines", "isenthalp"))
self.s_isentrope = getattr(
self.tp, self.get_export_name("isolines", "isentrope"))
self.s_envelope_isentrope_cross = getattr(
self.tp, self.get_export_name("saturation_curve", "envelope_isentrope_cross"))
self.s_property_index_from_string = getattr(
self.tp, self.get_export_name("saturation_point_locators", "property_index_from_string"))
self.s_sat_points_based_on_prop = getattr(
self.tp, self.get_export_name("saturation_point_locators", "sat_points_based_on_prop"))
self.s_locate_saturation_property = getattr(
self.tp, self.get_export_name("saturation_point_locators", "locate_saturation_property"))
# Stability
self.s_crit_tv = getattr(
self.tp, self.get_export_name("critical", "calccriticaltv"))
self.s_map_stability_limit = getattr(
self.tp, self.get_export_name("spinodal", "map_stability_limit"))
self.s_initial_stab_limit_point = getattr(
self.tp, self.get_export_name("spinodal", "initial_stab_limit_point"))
self.s_map_meta_isentrope = getattr(
self.tp, self.get_export_name("spinodal", "map_meta_isentrope"))
self.s_tv_meta_ps = getattr(
self.tp, self.get_export_name("spinodal", "tv_meta_ps"))
self.s_solve_mu_t = getattr(self.tp, self.get_export_name(
"mut_solver", "solve_mu_t"))
self.s_solve_lnf_t = getattr(self.tp, self.get_export_name(
"mut_solver", "solve_lnf_t"))
self.s_map_meta_isotherm = getattr(self.tp, self.get_export_name(
"mut_solver", "map_meta_isotherm"))
# Virials
self.s_virial_coeffcients = getattr(
self.tp, self.get_export_name("eostv", "virial_coefficients"))
self.s_second_virial_matrix = getattr(
self.tp, self.get_export_name("eostv", "secondvirialcoeffmatrix"))
self.s_binary_third_virial_matrix = getattr(
self.tp, self.get_export_name("eostv", "binarythirdvirialcoeffmatrix"))
# Joule-Thompson inversion
self.s_joule_thompson_inversion = getattr(
self.tp, self.get_export_name("joule_thompson_inversion", "map_jt_inversion"))
# Utility
self.s_get_true = getattr(self.tp, self.get_export_name(
"thermopack_constants", "get_true"))
# Get int representation of true value
self._true_int_value = self._get_true_int_value()
self.add_eos()
def __del__(self):
"""Internal
Delete FORTRAN memory allocated for this instance
"""
self.delete_eos()
def _get_true_int_value(self):
"""Internal
Intel FORTRAN uses True=-1, while gfortran uses True=1
Returns:
int: Integer representing True of the logical value
"""
int_true_c = c_int(0)
self.s_get_true.argtypes = [POINTER(c_int)]
self.s_get_true.restype = None
self.s_get_true(byref(int_true_c))
return int_true_c.value
def activate(self):
"""Internal
Activate this instance of thermopack parameters for calculation
"""
self.s_activate_model.argtypes = [POINTER(c_int)]
self.s_activate_model.restype = None
self.s_activate_model(self.model_index_c)
def add_eos(self):
"""Internal
Allocate FORTRAN memory for this class instance
"""
self.s_add_eos.argtypes = None
self.s_add_eos.restype = c_int
self.model_index_c = c_int(self.s_add_eos())
def delete_eos(self):
"""Internal
de-allocate FORTRAN memory for this class instance
"""
self.s_delete_eos.restype = None
self.s_delete_eos(self.model_index_c)
self.model_index_c = None
def get_model_id(self):
"""Internal
Get model identification
Returns:
str: Eos name
"""
self.activate()
eosid_len = 40
eosid_c = c_char_p(b" " * eosid_len)
eosid_len_c = c_len_type(eosid_len)
self.s_get_model_id.argtypes = [c_char_p, c_len_type]
self.s_get_model_id.restype = None
self.s_get_model_id(eosid_c, eosid_len_c)
eosid = eosid_c.value.decode('ascii').strip()
return eosid
def get_export_name(self, module, method):
"""Internal
Generate library export name based on module and method name
Args:
module (str): Name of module
method (str): Name of method
Returns:
str: Library export name
"""
if len(module) > 0:
export_name = self.prefix + module + self.module + method + self.postfix
else:
export_name = method + self.postfix_nm
return export_name
#################################
# Init
#################################
def init_thermo(self, eos, mixing, alpha, comps, nphases,
liq_vap_discr_method=None, csp_eos=None, csp_ref_comp=None,
kij_ref="Default", alpha_ref="Default", saft_ref="Default",
b_exponent=None, TrendEosForCp=None, cptype=None,
silent=None):
"""Internal
Initialize thermopack
Args:
eos (str): Equation of state
mixing (str): Mixture model for cubic eos
alpha (str): Alpha formulations for cubic EOS
comps (string): Comma separated list of components
nphases (int): Maximum number of phases considered during multi-phase flash calculations
liq_vap_discr_method (int, optional): Method to discriminate between liquid and vapor in case of an undefined single phase. Defaults to None.
csp_eos (str, optional): Corresponding state equation. Defaults to None.
csp_ref_comp (str, optional): CSP reference component. Defaults to None.
kij_ref (str, optional): Data set identifiers. Defaults to "Default".
alpha_ref (str, optional): Data set identifiers. Defaults to "Default".
saft_ref (str, optional): Data set identifiers. Defaults to "Default".
b_exponent (float, optional): Exponent used in co-volume mixing. Defaults to None.
TrendEosForCp (str, optional): Option to init trend for ideal gas properties. Defaults to None.
cptype (int array, optional): Equation type number for Cp. Defaults to None.
silent (bool, optional): Suppress messages during init?. Defaults to None.
"""
self.activate()
self.nc = max(len(comps.split(" ")), len(comps.split(",")))
null_pointer = POINTER(c_int)()
eos_c = c_char_p(eos.encode('ascii'))
eos_len = c_len_type(len(eos))
mixing_c = c_char_p(mixing.encode('ascii'))
mixing_len = c_len_type(len(mixing))
alpha_c = c_char_p(alpha.encode('ascii'))
alpha_len = c_len_type(len(alpha))
comp_string_c = c_char_p(comps.encode('ascii'))
comp_string_len = c_len_type(len(comps))
nphases_c = c_int(nphases)
if liq_vap_discr_method is None:
liq_vap_discr_method_c = null_pointer
else:
liq_vap_discr_method_c = POINTER(
c_int)(c_int(liq_vap_discr_method))
if csp_eos is None:
csp_eos_c = c_char_p()
csp_eos_len = c_len_type(0)
else:
csp_eos_c = c_char_p(csp_eos.encode('ascii'))
csp_eos_len = c_len_type(len(csp_eos))
if csp_ref_comp is None:
csp_ref_comp_c = c_char_p()
csp_ref_comp_len = c_len_type(0)
else:
csp_ref_comp_c = c_char_p(csp_ref_comp.encode('ascii'))
csp_ref_comp_len = c_len_type(len(csp_ref_comp))
kij_ref_len = c_len_type(len(kij_ref))
kij_ref_c = c_char_p(kij_ref.encode('ascii'))
alpha_ref_len = c_len_type(len(alpha_ref))
alpha_ref_c = c_char_p(alpha_ref.encode('ascii'))
saft_ref_len = c_len_type(len(saft_ref))
saft_ref_c = c_char_p(saft_ref.encode('ascii'))
if b_exponent is None:
b_exponent_c = POINTER(c_double)()
else:
b_exponent_c = POINTER(c_double)(c_double(b_exponent))
if TrendEosForCp is None:
TrendEosForCp_c = c_char_p()
TrendEosForCp_len = c_len_type(0)
else:
TrendEosForCp_c = c_char_p(TrendEosForCp.encode('ascii'))
TrendEosForCp_len = c_len_type(len(TrendEosForCp))
if cptype is None:
cptype_c = null_pointer
else:
cptype_c = (c_int * self.nc)(*cptype)
if silent is None:
silent_c = null_pointer
else:
if silent:
silent_int = self._true_int_value
else:
silent_int = 0
silent_c = POINTER(c_int)(c_int(silent_int))
self.eoslibinit_init_thermo.argtypes = [c_char_p,
c_char_p,
c_char_p,
c_char_p,
POINTER(c_int),
POINTER(c_int),
c_char_p,
c_char_p,
c_char_p,
c_char_p,
c_char_p,
POINTER(c_double),
c_char_p,
POINTER(c_int),
POINTER(c_int),
c_len_type, c_len_type,
c_len_type, c_len_type,
c_len_type, c_len_type,
c_len_type, c_len_type,
c_len_type, c_len_type]
self.eoslibinit_init_thermo.restype = None
self.eoslibinit_init_thermo(eos_c,
mixing_c,
alpha_c,
comp_string_c,
byref(nphases_c),
liq_vap_discr_method_c,
csp_eos_c,
csp_ref_comp_c,
kij_ref_c,
alpha_ref_c,
saft_ref_c,
b_exponent_c,
TrendEosForCp_c,
cptype_c,
silent_c,
eos_len,
mixing_len,
alpha_len,
comp_string_len,
csp_eos_len,
csp_ref_comp_len,
kij_ref_len,
alpha_ref_len,
saft_ref_len,
TrendEosForCp_len)
def init_peneloux_volume_translation(self, parameter_reference="Default"):
"""Internal
Initialize Peneloux volume translations
Args:
parameter_reference (str): String defining parameter set, Defaults to "Default"
"""
self.activate()
volume_trans_model = "PENELOUX"
volume_trans_model_c = c_char_p(volume_trans_model.encode('ascii'))
volume_trans_model_len = c_len_type(len(volume_trans_model))
ref_string_c = c_char_p(parameter_reference.encode('ascii'))
ref_string_len = c_len_type(len(parameter_reference))
self.eoslibinit_init_volume_translation.argtypes = [c_char_p,
c_char_p,
c_len_type,
c_len_type]
self.eoslibinit_init_volume_translation.restype = None
self.eoslibinit_init_volume_translation(volume_trans_model_c,
ref_string_c,
volume_trans_model_len,
ref_string_len)
def disable_volume_translation(self):
"""Internal
Disable volume translations
"""
self.activate()
volume_trans_model = "NOSHIFT"
volume_trans_model_c = c_char_p(volume_trans_model.encode('ascii'))
volume_trans_model_len = c_len_type(len(volume_trans_model))
ref_string = "Default"
ref_string_c = c_char_p(ref_string.encode('ascii'))
ref_string_len = c_len_type(len(ref_string))
self.eoslibinit_init_volume_translation.argtypes = [c_char_p,
c_char_p,
c_len_type,
c_len_type]
self.eoslibinit_init_volume_translation.restype = None
self.eoslibinit_init_volume_translation(volume_trans_model_c,
ref_string_c,
volume_trans_model_len,
ref_string_len)
def redefine_critical_parameters(self, silent=True, Tc_initials=None, vc_initials=None):
"""Utility
Recalculate critical properties of pure fluids
Args:
silent (bool): Ignore warnings? Defaults to True
Tc_initials (array_like): Initial value for pure fluid critical temperatures (K). Negative values will trigger use of SRK values from data base.
vc_initials (array_like): Initial value for pure fluid critical volumes (m3/mol). Negative values will trigger use of SRK values from data base.
"""
self.activate()
if silent:
silent_c = c_int(self._true_int_value)
else:
silent_c = c_int(0)
null_pointer = POINTER(c_double)()
if Tc_initials is None:
Tc_initials_c = null_pointer
else:
Tc_initials_c = (c_double * len(Tc_initials))(*Tc_initials)
if vc_initials is None:
vc_initials_c = null_pointer
else:
vc_initials_c = (c_double * len(vc_initials))(*vc_initials)
self.eoslibinit_redefine_critical_parameters.argtypes = [POINTER(c_int),
POINTER(
c_double),
POINTER(c_double)]
self.eoslibinit_redefine_critical_parameters.restype = None
self.eoslibinit_redefine_critical_parameters(
byref(silent_c), Tc_initials_c, vc_initials_c)
#################################
# Solids
#################################
def init_solid(self, scomp):
"""Internal
Initialize pure solid
Args:
scomp (str): Component name
"""
self.activate()
scomp_c = c_char_p(scomp.encode('ascii'))
scomp_len = c_len_type(len(scomp))
self.solideos_solid_init.argtypes = [c_char_p, c_len_type]
self.solideos_solid_init.restype = None
self.solideos_solid_init(scomp_c, scomp_len)
def solid_enthalpy(self, temp, press, x, dhdt=None, dhdp=None):
"""Tp-property
Calculate specific solid-phase enthalpy
Note that the order of the output match the default order of input for the differentials.
Note further that dhdt, dhdp only are flags to enable calculation.
Args:
temp (float): Temperature (K)
press (float): Pressure (Pa)
x (array_like): Molar composition
dhdt (logical, optional): Calculate enthalpy differentials with respect to temperature while pressure and composition are held constant. Defaults to None.
dhdp (logical, optional): Calculate enthalpy differentials with respect to pressure while temperature and composition are held constant. Defaults to None.
Returns:
float: Specific enthalpy (J/mol), and optionally differentials
"""
self.activate()
null_pointer = POINTER(c_double)()
temp_c = c_double(temp)
press_c = c_double(press)
x_c = (c_double * len(x))(*x)
h_c = c_double(0.0)
if dhdt is None:
dhdt_c = null_pointer
else:
dhdt_c = POINTER(c_double)(c_double(0.0))
if dhdp is None:
dhdp_c = null_pointer
else:
dhdp_c = POINTER(c_double)(c_double(0.0))
self.solideos_solid_enthalpy.argtypes = [POINTER(c_double),
POINTER(c_double),
POINTER(c_double),
POINTER(c_double),
POINTER(c_double),
POINTER(c_double)]
self.solideos_solid_enthalpy.restype = None
self.solideos_solid_enthalpy(byref(temp_c),
byref(press_c),
x_c,
byref(h_c),
dhdt_c,
dhdp_c)
return_tuple = (h_c.value, )
if not dhdt is None:
return_tuple += (dhdt_c[0], )
if not dhdp is None:
return_tuple += (dhdp_c[0], )
prop = utils.Property.from_return_tuple(return_tuple, (dhdt, dhdp, None), 'tpn')
return prop.unpack()
def solid_entropy(self, temp, press, x, dsdt=None, dsdp=None):
"""Tp-property
Calculate specific solid-phase entropy
Note that the order of the output match the default order of input for the differentials.
Note further that dsdt, dsdp only are flags to enable calculation.
Args:
temp (float): Temperature (K)
press (float): Pressure (Pa)
x (array_like): Molar composition
dsdt (logical, optional): Calculate entropy differentials with respect to temperature while pressure and composition are held constant. Defaults to None.
dsdp (logical, optional): Calculate entropy differentials with respect to pressure while temperature and composition are held constant. Defaults to None.
Returns:
float: Specific entropy (J/mol.K), and optionally differentials
"""
self.activate()
null_pointer = POINTER(c_double)()
temp_c = c_double(temp)
press_c = c_double(press)
x_c = (c_double * len(x))(*x)
s_c = c_double(0.0)
if dsdt is None:
dsdt_c = null_pointer
else:
dsdt_c = POINTER(c_double)(c_double(0.0))
if dsdp is None:
dsdp_c = null_pointer
else:
dsdp_c = POINTER(c_double)(c_double(0.0))
self.solideos_solid_entropy.argtypes = [POINTER(c_double),
POINTER(c_double),
POINTER(c_double),
POINTER(c_double),
POINTER(c_double),
POINTER(c_double)]
self.solideos_solid_entropy.restype = None
self.solideos_solid_entropy(byref(temp_c),
byref(press_c),
x_c,
byref(s_c),
dsdt_c,
dsdp_c)
return_tuple = (s_c.value, )
if not dsdt is None:
return_tuple += (dsdt_c[0], )
if not dsdp is None:
return_tuple += (dsdp_c[0], )
prop = utils.Property.from_return_tuple(return_tuple, (dsdt, dsdp, None), 'tpn')
return prop.unpack()
def solid_volume(self, temp, press, x, dvdt=None, dvdp=None):
"""Tp-property
Calculate specific solid-phase volume
Note that the order of the output match the default order of input for the differentials.
Note further that dsdt, dsdp only are flags to enable calculation.
Args:
temp (float): Temperature (K)
press (float): Pressure (Pa)
x (array_like): Molar composition
dvdt (logical, optional): Calculate volume differentials with respect to temperature while pressure and composition are held constant. Defaults to None.
dvdp (logical, optional): Calculate volume differentials with respect to pressure while temperature and composition are held constant. Defaults to None.
Returns:
float: Specific volume (m3/mol), and optionally differentials
"""
self.activate()
null_pointer = POINTER(c_double)()
temp_c = c_double(temp)
press_c = c_double(press)
x_c = (c_double * len(x))(*x)
v_c = c_double(0.0)
if dvdt is None:
dvdt_c = null_pointer
else:
dvdt_c = POINTER(c_double)(c_double(0.0))
if dvdp is None:
dvdp_c = null_pointer
else:
dvdp_c = POINTER(c_double)(c_double(0.0))
self.solideos_solid_volume.argtypes = [POINTER(c_double),
POINTER(c_double),
POINTER(c_double),
POINTER(c_double),
POINTER(c_double),
POINTER(c_double)]
self.solideos_solid_volume.restype = None
self.solideos_solid_volume(byref(temp_c),
byref(press_c),
x_c,
byref(v_c),
dvdt_c,
dvdp_c)
return_tuple = (v_c.value, )
if not dvdt is None:
return_tuple += (dvdt_c[0], )
if not dvdp is None:
return_tuple += (dvdp_c[0], )
prop = utils.Property.from_return_tuple(return_tuple, (dvdt, dvdp, None), 'tpn')
return prop.unpack()
#################################
# Utility
#################################
def getcompindex(self, comp):
"""Utility
Get component index
Args:
comp (str): Component name
Returns:
int: Component FORTRAN index
"""
self.activate()
comp_c = c_char_p(comp.encode('ascii'))
comp_len = c_len_type(len(comp))
self.s_compdata_compindex.argtypes = [c_char_p, c_len_type]
self.s_compdata_compindex.restype = c_int
idx = self.s_compdata_compindex(comp_c, comp_len)
return idx
def get_comp_name(self, index, get_comp_identifier=False):
"""Utility
Get component name/identifier
Args:
index (int): Component FORTRAN index
get_comp_identifier (bool): Get component identifier instead of full name? Default False.
Returns:
comp (str): Component name/identifier
"""
self.activate()
comp_len = 40
comp_c = c_char_p(b" " * comp_len)
comp_len_c = c_len_type(comp_len)
index_c = c_int(index)
comp_id_c = c_int(self._true_int_value) if get_comp_identifier else c_int(0)
self.s_compdata_compname.argtypes = [
POINTER(c_int), POINTER(c_int), c_char_p, c_len_type]
self.s_compdata_compname.restype = None
self.s_compdata_compname(byref(index_c), byref(comp_id_c), comp_c, comp_len_c)
compname = comp_c.value.decode('ascii').strip()
return compname
def get_comp_structure(self, comp_name):
"""Utility
Get component atom structure
Args:
comp_name (str): Component name
Returns:
(dict): Dict with atom as key names and number of atoms as values
"""
self.activate()
comp_c = c_char_p(comp_name.encode('ascii'))
comp_len = c_len_type(len(comp_name))
structure_len = 40
structure_c = c_char_p(b" " * structure_len)
structure_len_c = c_len_type(structure_len)
self.s_compdata_structure.argtypes = [
c_char_p, c_char_p, c_len_type, c_len_type]
self.s_compdata_structure.restype = None
self.s_compdata_structure(comp_c, structure_c, comp_len, structure_len_c)
structure = structure_c.value.decode('ascii').strip()
if "ERROR" in structure:
raise Exception("Fluid not found in database")
structure_dict = {}
for pair in structure.split(";"):
(key, value) = pair.split(":")
structure_dict[key] = int(value)
return structure_dict
def compmoleweight(self, comp, si_units=False):
"""Utility
Get component mole weight (g/mol)
Args:
comp (int): Component FORTRAN index
si_units (bool, optional) : true for output in kg/mol, false for g/mol
Returns:
float: Component mole weight (g/mol)
"""
self.activate()
comp_c = c_int(comp)
self.s_eos_compmoleweight.argtypes = [POINTER(c_int)]
self.s_eos_compmoleweight.restype = c_double
mw_i = self.s_eos_compmoleweight(byref(comp_c))
if si_units:
mw_i = mw_i*1e-3 # kg/mol
return mw_i
def moleweight(self, z, si_units=True):
"""Utility
Get mole weight (kg/mol)
Args:
z (array_like): Molar composition
si_units (bool, optional) : true for output in kg/mol, false for g/mol
Returns:
float: mixture mole weight (kg/mol)
"""
self.activate()
z_c = (c_double * len(z))(*z)
self.s_eos_moleweight.argtypes = [POINTER(c_double)]
self.s_eos_moleweight.restype = c_double
mw = self.s_eos_moleweight(z_c)
if si_units:
mw = mw*1e-3 # kg/mol
return mw
def acentric_factor(self, i):
'''Utility
Get acentric factor of component i
Args:
i (int) component FORTRAN index
returns:
float: acentric factor
'''
self.activate()
comp_c = c_int(i)
w = c_double(0.0)
tci = c_double(0.0)
pci = c_double(0.0)
vci = c_double(0.0)
tnbi = c_double(0.0)
self.s_eos_getCriticalParam.argtypes = [POINTER(c_int),
POINTER(c_double),
POINTER(c_double),
POINTER(c_double),
POINTER(c_double),
POINTER(c_double)]
self.s_eos_getCriticalParam.restype = None
self.s_eos_getCriticalParam(byref(comp_c),
byref(tci),
byref(pci),
byref(w),
byref(vci),
byref(tnbi))
return w.value
def get_phase_flags(self):
"""Utility
Get phase identifiers used by thermopack
Returns:
int: Phase int identifiers
"""
iTWOPH = c_int()
iLIQPH = c_int()
iVAPPH = c_int()
iMINGIBBSPH = c_int()
iSINGLEPH = c_int()
iSOLIDPH = c_int()
iFAKEPH = c_int()
self.s_get_phase_flags.argtypes = [POINTER(c_int),
POINTER(c_int),
POINTER(c_int),
POINTER(c_int),
POINTER(c_int),
POINTER(c_int),
POINTER(c_int)]
self.s_get_phase_flags.restype = None
self.s_get_phase_flags(byref(iTWOPH),
byref(iLIQPH),
byref(iVAPPH),
byref(iMINGIBBSPH),
byref(iSINGLEPH),
byref(iSOLIDPH),
byref(iFAKEPH))
self.TWOPH = iTWOPH.value
self.LIQPH = iLIQPH.value
self.VAPPH = iVAPPH.value
self.MINGIBBSPH = iMINGIBBSPH.value
self.SINGLEPH = iSINGLEPH.value
self.SOLIDPH = iSOLIDPH.value
self.FAKEPH = iFAKEPH.value
def get_phase_type(self, i_phase):
"""Utility
Get phase type
Args:
i_phase (int): Phase flag returned by thermopack
Returns:
str: Phase type
"""
phase_string_list = ["TWO_PHASE", "LIQUID", "VAPOR",
"MINIMUM_GIBBS", "SINGLE", "SOLID", "FAKE"]
return phase_string_list[i_phase]
@property
def Rgas(self):
self.activate()
self.s_get_rgas.argtypes = []
self.s_get_rgas.restype = c_double
rgas = self.s_get_rgas()