-
Notifications
You must be signed in to change notification settings - Fork 301
/
HelmholtzEOSMixtureBackend.cpp
3400 lines (3155 loc) · 140 KB
/
HelmholtzEOSMixtureBackend.cpp
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
/*
* AbstractBackend.cpp
*
* Created on: 20 Dec 2013
* Author: jowr
*/
#include <memory>
#if defined(_MSC_VER)
#define _CRTDBG_MAP_ALLOC
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <crtdbg.h>
#include <sys/stat.h>
#else
#include <sys/stat.h>
#endif
#include <string>
//#include "CoolProp.h"
#include "HelmholtzEOSMixtureBackend.h"
#include "HelmholtzEOSBackend.h"
#include "Fluids/FluidLibrary.h"
#include "Solvers.h"
#include "MatrixMath.h"
#include "VLERoutines.h"
#include "FlashRoutines.h"
#include "TransportRoutines.h"
#include "MixtureDerivatives.h"
#include "PhaseEnvelopeRoutines.h"
#include "ReducingFunctions.h"
#include "MixtureParameters.h"
#include "IdealCurves.h"
#include "MixtureParameters.h"
#include <stdlib.h>
static int deriv_counter = 0;
namespace CoolProp {
HelmholtzEOSMixtureBackend::HelmholtzEOSMixtureBackend(){
imposed_phase_index = iphase_not_imposed;
is_pure_or_pseudopure = false;
N = 0;
_phase = iphase_unknown;
// Reset the residual Helmholtz energy class
residual_helmholtz.reset(new ResidualHelmholtz());
}
HelmholtzEOSMixtureBackend::HelmholtzEOSMixtureBackend(const std::vector<std::string> &component_names, bool generate_SatL_and_SatV) {
std::vector<CoolPropFluid> components(component_names.size());
for (unsigned int i = 0; i < components.size(); ++i){
components[i] = get_library().get(component_names[i]);
}
// Reset the residual Helmholtz energy class
residual_helmholtz.reset(new ResidualHelmholtz());
// Set the components and associated flags
set_components(components, generate_SatL_and_SatV);
// Set the phase to default unknown value
_phase = iphase_unknown;
}
HelmholtzEOSMixtureBackend::HelmholtzEOSMixtureBackend(const std::vector<CoolPropFluid> &components, bool generate_SatL_and_SatV) {
// Reset the residual Helmholtz energy class
residual_helmholtz.reset(new ResidualHelmholtz());
// Set the components and associated flags
set_components(components, generate_SatL_and_SatV);
// Set the phase to default unknown value
_phase = iphase_unknown;
}
void HelmholtzEOSMixtureBackend::set_components(const std::vector<CoolPropFluid> &components, bool generate_SatL_and_SatV) {
// Copy the components
this->components = components;
this->N = components.size();
is_pure_or_pseudopure = (components.size() == 1);
if (is_pure_or_pseudopure){
mole_fractions = std::vector<CoolPropDbl>(1, 1);
}
else{
// Set the mixture parameters - binary pair reducing functions, departure functions, F_ij, etc.
set_mixture_parameters();
}
imposed_phase_index = iphase_not_imposed;
// Top-level class can hold copies of the base saturation classes,
// saturation classes cannot hold copies of the saturation classes
if (generate_SatL_and_SatV)
{
SatL.reset(new HelmholtzEOSMixtureBackend(components, false));
SatL->specify_phase(iphase_liquid);
SatV.reset(new HelmholtzEOSMixtureBackend(components, false));
SatV->specify_phase(iphase_gas);
}
}
void HelmholtzEOSMixtureBackend::set_mole_fractions(const std::vector<CoolPropDbl> &mole_fractions)
{
if (mole_fractions.size() != N)
{
throw ValueError(format("size of mole fraction vector [%d] does not equal that of component vector [%d]",mole_fractions.size(), N));
}
// Copy values without reallocating memory
this->mole_fractions = mole_fractions; // Most effective copy
this->resize(N); // No reallocation of this->mole_fractions happens
// Resize the vectors for the liquid and vapor, but only if they are in use
if (this->SatL) this->SatL->resize(N);
if (this->SatV) this->SatV->resize(N);
// Also store the mole fractions as doubles
this->mole_fractions_double = std::vector<double>(mole_fractions.begin(), mole_fractions.end());
};
void HelmholtzEOSMixtureBackend::resize(std::size_t N)
{
this->mole_fractions.resize(N);
this->K.resize(N);
this->lnK.resize(N);
}
void HelmholtzEOSMixtureBackend::recalculate_singlephase_phase()
{
if (p() > p_critical()){
if (T() > T_critical()){
_phase = iphase_supercritical;
}
else{
_phase = iphase_supercritical_liquid;
}
}
else{
if (T() > T_critical()){
_phase = iphase_supercritical_gas;
}
else{
// Liquid or vapor
if (rhomolar() > rhomolar_critical()){
_phase = iphase_liquid;
}
else{
_phase = iphase_gas;
}
}
}
}
std::string HelmholtzEOSMixtureBackend::fluid_param_string(const std::string &ParamName)
{
CoolProp::CoolPropFluid cpfluid = get_components()[0];
if (!ParamName.compare("aliases")){
return strjoin(cpfluid.aliases, ", ");
}
else if (!ParamName.compare("CAS") || !ParamName.compare("CAS_number")){
return cpfluid.CAS;
}
else if (!ParamName.compare("formula")){
return cpfluid.formula;
}
else if (!ParamName.compare("ASHRAE34")){
return cpfluid.environment.ASHRAE34;
}
else if (!ParamName.compare("REFPROPName") || !ParamName.compare("REFPROP_name") || !ParamName.compare("REFPROPname")){
return cpfluid.REFPROPname;
}
else if (ParamName.find("BibTeX") == 0) // Starts with "BibTeX"
{
std::vector<std::string> parts = strsplit(ParamName,'-');
if (parts.size() != 2){ throw ValueError(format("Unable to parse BibTeX string %s",ParamName.c_str()));}
std::string key = parts[1];
if (!key.compare("EOS")){ return cpfluid.EOS().BibTeX_EOS; }
else if (!key.compare("CP0")){ return cpfluid.EOS().BibTeX_CP0; }
else if (!key.compare("VISCOSITY")){ return cpfluid.transport.BibTeX_viscosity; }
else if (!key.compare("CONDUCTIVITY")){ return cpfluid.transport.BibTeX_conductivity; }
else if (!key.compare("ECS_LENNARD_JONES")){ throw NotImplementedError(); }
else if (!key.compare("ECS_VISCOSITY_FITS")){ throw NotImplementedError(); }
else if (!key.compare("ECS_CONDUCTIVITY_FITS")){ throw NotImplementedError(); }
else if (!key.compare("SURFACE_TENSION")){ return cpfluid.ancillaries.surface_tension.BibTeX;}
else if (!key.compare("MELTING_LINE")){ return cpfluid.ancillaries.melting_line.BibTeX;}
else{ throw CoolProp::KeyError(format("Bad key to get_BibTeXKey [%s]", key.c_str()));}
}
else if (ParamName.find("pure") == 0){
if (is_pure()){
return "true";
}
else{
return "false";
}
}
else{
throw ValueError(format("fluid parameter [%s] is invalid",ParamName.c_str()));
}
}
/// Set binary mixture floating point parameter for this instance
void HelmholtzEOSMixtureBackend::set_binary_interaction_double(const std::size_t i, const std::size_t j, const std::string ¶meter, const double value){
if (parameter == "Fij"){
residual_helmholtz->Excess.F[i][j] = value;
residual_helmholtz->Excess.F[j][i] = value;
}
else{
Reducing->set_binary_interaction_double(i,j,parameter,value);
}
};
/// Get binary mixture floating point parameter for this instance
double HelmholtzEOSMixtureBackend::get_binary_interaction_double(const std::size_t i, const std::size_t j, const std::string ¶meter){
if (parameter == "Fij"){
return residual_helmholtz->Excess.F[i][j];
}
else{
return Reducing->get_binary_interaction_double(i,j,parameter);
}
};
///// Get binary mixture string value
//std::string HelmholtzEOSMixtureBackend::get_binary_interaction_string(const std::string &CAS1, const std::string &CAS2, const std::string ¶meter){
// return CoolProp::get_mixture_binary_pair_data(CAS1, CAS2, parameter);
//}
void HelmholtzEOSMixtureBackend::calc_change_EOS(const std::size_t i, const std::string &EOS_name){
if (i < components.size()){
CoolPropFluid &fluid = components[i];
EquationOfState &EOS = fluid.EOSVector[0];
if (EOS_name == "SRK"){
// Get the parameters for the cubic EOS
CoolPropDbl Tc = EOS.reduce.T;
CoolPropDbl pc = EOS.reduce.p;
CoolPropDbl rhomolarc = EOS.reduce.rhomolar;
CoolPropDbl acentric = EOS.acentric;
CoolPropDbl R = 8.3144598;
// Remove the residual part
EOS.alphar.empty_the_EOS();
// Set the SRK contribution
EOS.alphar.SRK = ResidualHelmholtzSRK(Tc, pc, rhomolarc, acentric, R);
}
else if (EOS_name == "XiangDeiters"){
// Get the parameters for the EOS
CoolPropDbl Tc = EOS.reduce.T;
CoolPropDbl pc = EOS.reduce.p;
CoolPropDbl rhomolarc = EOS.reduce.rhomolar;
CoolPropDbl acentric = EOS.acentric;
CoolPropDbl R = 8.3144598;
// Remove the residual part
EOS.alphar.empty_the_EOS();
// Set the Xiang & Deiters contribution
EOS.alphar.XiangDeiters = ResidualHelmholtzXiangDeiters(Tc, pc, rhomolarc, acentric, R);
}
}
else{
throw ValueError(format("Index [%d] is invalid", i));
}
// Now do the same thing to the saturated liquid and vapor instances if possible
if (this->SatL) SatL->change_EOS(i, EOS_name);
if (this->SatV) SatV->change_EOS(i, EOS_name);
}
void HelmholtzEOSMixtureBackend::calc_phase_envelope(const std::string &type)
{
// Clear the phase envelope data
PhaseEnvelope = PhaseEnvelopeData();
// Build the phase envelope
PhaseEnvelopeRoutines::build(*this, type);
// Finalize the phase envelope
PhaseEnvelopeRoutines::finalize(*this);
};
void HelmholtzEOSMixtureBackend::set_mixture_parameters()
{
// Build the matrix of binary-pair reducing functions
MixtureParameters::set_mixture_parameters(*this);
}
void HelmholtzEOSMixtureBackend::update_states(void)
{
CoolPropFluid &component = components[0];
EquationOfState &EOS = component.EOSVector[0];
// Clear the state class
clear();
// Calculate the new enthalpy and entropy values
update(DmolarT_INPUTS, EOS.hs_anchor.rhomolar, EOS.hs_anchor.T);
EOS.hs_anchor.hmolar = hmolar();
EOS.hs_anchor.smolar = smolar();
// Calculate the new enthalpy and entropy values at the reducing state
update(DmolarT_INPUTS, EOS.reduce.rhomolar, EOS.reduce.T);
EOS.reduce.hmolar = hmolar();
EOS.reduce.smolar = smolar();
// Clear again just to be sure
clear();
}
const CoolProp::SimpleState & HelmholtzEOSMixtureBackend::calc_state(const std::string &state)
{
if (is_pure_or_pseudopure)
{
if (!state.compare("hs_anchor")){
return components[0].EOS().hs_anchor;
}
else if (!state.compare("max_sat_T")){
return components[0].EOS().max_sat_T;
}
else if (!state.compare("max_sat_p")){
return components[0].EOS().max_sat_p;
}
else if (!state.compare("reducing")){
return components[0].EOS().reduce;
}
else if (!state.compare("critical")){
return components[0].crit;
}
else if (!state.compare("triple_liquid")){
return components[0].triple_liquid;
}
else if (!state.compare("triple_vapor")){
return components[0].triple_vapor;
}
else{
throw ValueError(format("This state [%s] is invalid to calc_state",state.c_str()));
}
}
else{
if (!state.compare("critical")){
return _critical;
}
else{
throw ValueError(format("calc_state not supported for mixtures"));
}
}
};
CoolPropDbl HelmholtzEOSMixtureBackend::calc_acentric_factor(void)
{
if (is_pure_or_pseudopure){
return components[0].EOS().acentric;
}
else{
throw ValueError("acentric factor cannot be calculated for mixtures");
}
}
CoolPropDbl HelmholtzEOSMixtureBackend::calc_gas_constant(void)
{
if (is_pure_or_pseudopure){
return components[0].gas_constant();
}
else{
if (get_config_bool(NORMALIZE_GAS_CONSTANTS)){
return get_config_double(R_U_CODATA);
}
else{
// mass fraction weighted average of the components
double summer = 0;
for (unsigned int i = 0; i < components.size(); ++i)
{
summer += mole_fractions[i]*components[i].gas_constant();
}
return summer;
}
}
}
CoolPropDbl HelmholtzEOSMixtureBackend::calc_molar_mass(void)
{
double summer = 0;
for (unsigned int i = 0; i < components.size(); ++i)
{
summer += mole_fractions[i]*components[i].molar_mass();
}
return summer;
}
CoolPropDbl HelmholtzEOSMixtureBackend::calc_saturation_ancillary(parameters param, int Q, parameters given, double value)
{
if (is_pure_or_pseudopure)
{
if (param == iP && given == iT){
// p = f(T), direct evaluation
switch (Q)
{
case 0:
return components[0].ancillaries.pL.evaluate(value);
case 1:
return components[0].ancillaries.pV.evaluate(value);
}
}
else if (param == iT && given == iP){
// T = f(p), inverse evaluation
switch (Q)
{
case 0:
return components[0].ancillaries.pL.invert(value);
case 1:
return components[0].ancillaries.pV.invert(value);
}
}
else if (param == iDmolar && given == iT){
// rho = f(T), inverse evaluation
switch (Q)
{
case 0:
return components[0].ancillaries.rhoL.evaluate(value);
case 1:
return components[0].ancillaries.rhoV.evaluate(value);
}
}
else if (param == iT && given == iDmolar){
// T = f(rho), inverse evaluation
switch (Q)
{
case 0:
return components[0].ancillaries.rhoL.invert(value);
case 1:
return components[0].ancillaries.rhoV.invert(value);
}
}
else if (param == isurface_tension && given == iT){
return components[0].ancillaries.surface_tension.evaluate(value);
}
else{
throw ValueError(format("calc of %s given %s is invalid in calc_saturation_ancillary",
get_parameter_information(param,"short").c_str(),
get_parameter_information(given,"short").c_str()));
}
throw ValueError(format("Q [%d] is invalid in calc_saturation_ancillary", Q));
}
else
{
throw NotImplementedError(format("calc_saturation_ancillary not implemented for mixtures"));
}
}
CoolPropDbl HelmholtzEOSMixtureBackend::calc_melting_line(int param, int given, CoolPropDbl value)
{
if (is_pure_or_pseudopure)
{
return components[0].ancillaries.melting_line.evaluate(param, given, value);
}
else
{
throw NotImplementedError(format("calc_melting_line not implemented for mixtures"));
}
}
CoolPropDbl HelmholtzEOSMixtureBackend::calc_surface_tension(void)
{
if (is_pure_or_pseudopure){
return components[0].ancillaries.surface_tension.evaluate(T());
}
else{
throw NotImplementedError(format("surface tension not implemented for mixtures"));
}
}
CoolPropDbl HelmholtzEOSMixtureBackend::calc_viscosity_dilute(void)
{
if (is_pure_or_pseudopure)
{
CoolPropDbl eta_dilute;
switch(components[0].transport.viscosity_dilute.type)
{
case ViscosityDiluteVariables::VISCOSITY_DILUTE_KINETIC_THEORY:
eta_dilute = TransportRoutines::viscosity_dilute_kinetic_theory(*this); break;
case ViscosityDiluteVariables::VISCOSITY_DILUTE_COLLISION_INTEGRAL:
eta_dilute = TransportRoutines::viscosity_dilute_collision_integral(*this); break;
case ViscosityDiluteVariables::VISCOSITY_DILUTE_POWERS_OF_T:
eta_dilute = TransportRoutines::viscosity_dilute_powers_of_T(*this); break;
case ViscosityDiluteVariables::VISCOSITY_DILUTE_POWERS_OF_TR:
eta_dilute = TransportRoutines::viscosity_dilute_powers_of_Tr(*this); break;
case ViscosityDiluteVariables::VISCOSITY_DILUTE_COLLISION_INTEGRAL_POWERS_OF_TSTAR:
eta_dilute = TransportRoutines::viscosity_dilute_collision_integral_powers_of_T(*this); break;
case ViscosityDiluteVariables::VISCOSITY_DILUTE_ETHANE:
eta_dilute = TransportRoutines::viscosity_dilute_ethane(*this); break;
case ViscosityDiluteVariables::VISCOSITY_DILUTE_CYCLOHEXANE:
eta_dilute = TransportRoutines::viscosity_dilute_cyclohexane(*this); break;
default:
throw ValueError(format("dilute viscosity type [%d] is invalid for fluid %s", components[0].transport.viscosity_dilute.type, name().c_str()));
}
return eta_dilute;
}
else
{
throw NotImplementedError(format("dilute viscosity not implemented for mixtures"));
}
}
CoolPropDbl HelmholtzEOSMixtureBackend::calc_viscosity_background()
{
CoolPropDbl eta_dilute = calc_viscosity_dilute(), initial_density = 0, residual = 0;
return calc_viscosity_background(eta_dilute, initial_density, residual);
}
CoolPropDbl HelmholtzEOSMixtureBackend::calc_viscosity_background(CoolPropDbl eta_dilute, CoolPropDbl &initial_density, CoolPropDbl &residual)
{
switch(components[0].transport.viscosity_initial.type){
case ViscosityInitialDensityVariables::VISCOSITY_INITIAL_DENSITY_RAINWATER_FRIEND:
{
CoolPropDbl B_eta_initial = TransportRoutines::viscosity_initial_density_dependence_Rainwater_Friend(*this);
CoolPropDbl rho = rhomolar();
initial_density = eta_dilute*B_eta_initial*rho;
break;
}
case ViscosityInitialDensityVariables::VISCOSITY_INITIAL_DENSITY_EMPIRICAL:
{
initial_density = TransportRoutines::viscosity_initial_density_dependence_empirical(*this);
break;
}
case ViscosityInitialDensityVariables::VISCOSITY_INITIAL_DENSITY_NOT_SET:
{
break;
}
}
// Higher order terms
switch(components[0].transport.viscosity_higher_order.type)
{
case ViscosityHigherOrderVariables::VISCOSITY_HIGHER_ORDER_BATSCHINKI_HILDEBRAND:
residual = TransportRoutines::viscosity_higher_order_modified_Batschinski_Hildebrand(*this); break;
case ViscosityHigherOrderVariables::VISCOSITY_HIGHER_ORDER_FRICTION_THEORY:
residual = TransportRoutines::viscosity_higher_order_friction_theory(*this); break;
case ViscosityHigherOrderVariables::VISCOSITY_HIGHER_ORDER_HYDROGEN:
residual = TransportRoutines::viscosity_hydrogen_higher_order_hardcoded(*this); break;
case ViscosityHigherOrderVariables::VISCOSITY_HIGHER_ORDER_TOLUENE:
residual = TransportRoutines::viscosity_toluene_higher_order_hardcoded(*this); break;
case ViscosityHigherOrderVariables::VISCOSITY_HIGHER_ORDER_HEXANE:
residual = TransportRoutines::viscosity_hexane_higher_order_hardcoded(*this); break;
case ViscosityHigherOrderVariables::VISCOSITY_HIGHER_ORDER_HEPTANE:
residual = TransportRoutines::viscosity_heptane_higher_order_hardcoded(*this); break;
case ViscosityHigherOrderVariables::VISCOSITY_HIGHER_ORDER_ETHANE:
residual = TransportRoutines::viscosity_ethane_higher_order_hardcoded(*this); break;
case ViscosityHigherOrderVariables::VISCOSITY_HIGHER_ORDER_BENZENE:
residual = TransportRoutines::viscosity_benzene_higher_order_hardcoded(*this); break;
default:
throw ValueError(format("higher order viscosity type [%d] is invalid for fluid %s", components[0].transport.viscosity_dilute.type, name().c_str()));
}
return initial_density + residual;
}
CoolPropDbl HelmholtzEOSMixtureBackend::calc_viscosity(void)
{
if (is_pure_or_pseudopure)
{
CoolPropDbl dilute = 0, initial_density = 0, residual = 0, critical = 0;
calc_viscosity_contributions(dilute, initial_density, residual, critical);
return dilute + initial_density + residual + critical;
}
else
{
set_warning_string("Mixture model for viscosity is highly approximate");
CoolPropDbl summer = 0;
for (std::size_t i = 0; i < mole_fractions.size(); ++i){
shared_ptr<HelmholtzEOSBackend> HEOS(new HelmholtzEOSBackend(components[i]));
HEOS->update(DmolarT_INPUTS, _rhomolar, _T);
summer += mole_fractions[i]*log(HEOS->viscosity());
}
return exp(summer);
}
}
void HelmholtzEOSMixtureBackend::calc_viscosity_contributions(CoolPropDbl &dilute, CoolPropDbl &initial_density, CoolPropDbl &residual, CoolPropDbl &critical){
if (is_pure_or_pseudopure)
{
// Reset the variables
dilute = 0; initial_density = 0; residual = 0; critical = 0;
// Get a reference for code cleanness
CoolPropFluid &component = components[0];
if (!component.transport.viscosity_model_provided){
throw ValueError(format("Viscosity model is not available for this fluid"));
}
// Check if using ECS
if (component.transport.viscosity_using_ECS)
{
// Get reference fluid name
std::string fluid_name = component.transport.viscosity_ecs.reference_fluid;
std::vector<std::string> names(1, fluid_name);
// Get a managed pointer to the reference fluid for ECS
shared_ptr<HelmholtzEOSMixtureBackend> ref_fluid(new HelmholtzEOSMixtureBackend(names));
// Get the viscosity using ECS and stick in the critical value
critical = TransportRoutines::viscosity_ECS(*this, *ref_fluid);
return;
}
// Check if using Chung model
if (component.transport.viscosity_using_Chung)
{
// Get the viscosity using ECS and stick in the critical value
critical = TransportRoutines::viscosity_Chung(*this);
return;
}
if (component.transport.hardcoded_viscosity != CoolProp::TransportPropertyData::VISCOSITY_NOT_HARDCODED)
{
// Evaluate hardcoded model and stick in the critical value
switch(component.transport.hardcoded_viscosity)
{
case CoolProp::TransportPropertyData::VISCOSITY_HARDCODED_WATER:
critical = TransportRoutines::viscosity_water_hardcoded(*this); break;
case CoolProp::TransportPropertyData::VISCOSITY_HARDCODED_HEAVYWATER:
critical = TransportRoutines::viscosity_heavywater_hardcoded(*this); break;
case CoolProp::TransportPropertyData::VISCOSITY_HARDCODED_HELIUM:
critical = TransportRoutines::viscosity_helium_hardcoded(*this); break;
case CoolProp::TransportPropertyData::VISCOSITY_HARDCODED_R23:
critical = TransportRoutines::viscosity_R23_hardcoded(*this); break;
case CoolProp::TransportPropertyData::VISCOSITY_HARDCODED_METHANOL:
critical = TransportRoutines::viscosity_methanol_hardcoded(*this); break;
default:
throw ValueError(format("hardcoded viscosity type [%d] is invalid for fluid %s", component.transport.hardcoded_viscosity, name().c_str()));
}
return;
}
// -------------------------
// Normal evaluation
// -------------------------
// Dilute part
dilute = calc_viscosity_dilute();
// Background viscosity given by the sum of the initial density dependence and higher order terms
calc_viscosity_background(dilute, initial_density, residual);
// Critical part (no fluids have critical enhancement for viscosity currently)
critical = 0;
}
else
{
throw ValueError("calc_viscosity_contributions invalid for mixtures");
}
}
void HelmholtzEOSMixtureBackend::calc_conductivity_contributions(CoolPropDbl &dilute, CoolPropDbl &initial_density, CoolPropDbl &residual, CoolPropDbl &critical)
{
if (is_pure_or_pseudopure)
{
// Reset the variables
dilute = 0; initial_density = 0; residual = 0; critical = 0;
// Get a reference for code cleanness
CoolPropFluid &component = components[0];
if (!component.transport.conductivity_model_provided){
throw ValueError(format("Thermal conductivity model is not available for this fluid"));
}
// Check if using ECS
if (component.transport.conductivity_using_ECS)
{
// Get reference fluid name
std::string fluid_name = component.transport.conductivity_ecs.reference_fluid;
std::vector<std::string> name(1, fluid_name);
// Get a managed pointer to the reference fluid for ECS
shared_ptr<HelmholtzEOSMixtureBackend> ref_fluid(new HelmholtzEOSMixtureBackend(name));
// Get the viscosity using ECS and store in initial_density (not normally used);
initial_density = TransportRoutines::conductivity_ECS(*this, *ref_fluid); // Warning: not actually initial_density
return;
}
if (component.transport.hardcoded_conductivity != CoolProp::TransportPropertyData::CONDUCTIVITY_NOT_HARDCODED)
{
// Evaluate hardcoded model and deposit in initial_density variable
// Warning: not actually initial_density
switch(component.transport.hardcoded_conductivity)
{
case CoolProp::TransportPropertyData::CONDUCTIVITY_HARDCODED_WATER:
initial_density = TransportRoutines::conductivity_hardcoded_water(*this); break;
case CoolProp::TransportPropertyData::CONDUCTIVITY_HARDCODED_HEAVYWATER:
initial_density = TransportRoutines::conductivity_hardcoded_heavywater(*this); break;
case CoolProp::TransportPropertyData::CONDUCTIVITY_HARDCODED_R23:
initial_density = TransportRoutines::conductivity_hardcoded_R23(*this); break;
case CoolProp::TransportPropertyData::CONDUCTIVITY_HARDCODED_HELIUM:
initial_density = TransportRoutines::conductivity_hardcoded_helium(*this); break;
case CoolProp::TransportPropertyData::CONDUCTIVITY_HARDCODED_METHANE:
initial_density = TransportRoutines::conductivity_hardcoded_methane(*this); break;
default:
throw ValueError(format("hardcoded conductivity type [%d] is invalid for fluid %s", components[0].transport.hardcoded_conductivity, name().c_str()));
}
return;
}
// -------------------------
// Normal evaluation
// -------------------------
// Dilute part
switch(component.transport.conductivity_dilute.type)
{
case ConductivityDiluteVariables::CONDUCTIVITY_DILUTE_RATIO_POLYNOMIALS:
dilute = TransportRoutines::conductivity_dilute_ratio_polynomials(*this); break;
case ConductivityDiluteVariables::CONDUCTIVITY_DILUTE_ETA0_AND_POLY:
dilute = TransportRoutines::conductivity_dilute_eta0_and_poly(*this); break;
case ConductivityDiluteVariables::CONDUCTIVITY_DILUTE_CO2:
dilute = TransportRoutines::conductivity_dilute_hardcoded_CO2(*this); break;
case ConductivityDiluteVariables::CONDUCTIVITY_DILUTE_ETHANE:
dilute = TransportRoutines::conductivity_dilute_hardcoded_ethane(*this); break;
case ConductivityDiluteVariables::CONDUCTIVITY_DILUTE_NONE:
dilute = 0.0; break;
default:
throw ValueError(format("dilute conductivity type [%d] is invalid for fluid %s", components[0].transport.conductivity_dilute.type, name().c_str()));
}
// Residual part
residual = calc_conductivity_background();
// Critical part
switch(component.transport.conductivity_critical.type)
{
case ConductivityCriticalVariables::CONDUCTIVITY_CRITICAL_SIMPLIFIED_OLCHOWY_SENGERS:
critical = TransportRoutines::conductivity_critical_simplified_Olchowy_Sengers(*this); break;
case ConductivityCriticalVariables::CONDUCTIVITY_CRITICAL_R123:
critical = TransportRoutines::conductivity_critical_hardcoded_R123(*this); break;
case ConductivityCriticalVariables::CONDUCTIVITY_CRITICAL_AMMONIA:
critical = TransportRoutines::conductivity_critical_hardcoded_ammonia(*this); break;
case ConductivityCriticalVariables::CONDUCTIVITY_CRITICAL_NONE:
critical = 0.0; break;
case ConductivityCriticalVariables::CONDUCTIVITY_CRITICAL_CARBONDIOXIDE_SCALABRIN_JPCRD_2006:
critical = TransportRoutines::conductivity_critical_hardcoded_CO2_ScalabrinJPCRD2006(*this); break;
default:
throw ValueError(format("critical conductivity type [%d] is invalid for fluid %s", components[0].transport.viscosity_dilute.type, name().c_str()));
}
}
else{
throw ValueError("calc_conductivity_contributions invalid for mixtures");
}
};
CoolPropDbl HelmholtzEOSMixtureBackend::calc_conductivity_background(void)
{
// Residual part
CoolPropDbl lambda_residual = _HUGE;
switch(components[0].transport.conductivity_residual.type)
{
case ConductivityResidualVariables::CONDUCTIVITY_RESIDUAL_POLYNOMIAL:
lambda_residual = TransportRoutines::conductivity_residual_polynomial(*this); break;
case ConductivityResidualVariables::CONDUCTIVITY_RESIDUAL_POLYNOMIAL_AND_EXPONENTIAL:
lambda_residual = TransportRoutines::conductivity_residual_polynomial_and_exponential(*this); break;
default:
throw ValueError(format("residual conductivity type [%d] is invalid for fluid %s", components[0].transport.conductivity_residual.type, name().c_str()));
}
return lambda_residual;
}
CoolPropDbl HelmholtzEOSMixtureBackend::calc_conductivity(void)
{
if (is_pure_or_pseudopure)
{
CoolPropDbl dilute = 0, initial_density = 0, residual = 0, critical = 0;
calc_conductivity_contributions(dilute, initial_density, residual, critical);
return dilute + initial_density + residual + critical;
}
else
{
set_warning_string("Mixture model for conductivity is highly approximate");
CoolPropDbl summer = 0;
for (std::size_t i = 0; i < mole_fractions.size(); ++i){
shared_ptr<HelmholtzEOSBackend> HEOS(new HelmholtzEOSBackend(components[i]));
HEOS->update(DmolarT_INPUTS, _rhomolar, _T);
summer += mole_fractions[i]*HEOS->conductivity();
}
return summer;
}
}
void HelmholtzEOSMixtureBackend::calc_conformal_state(const std::string &reference_fluid, CoolPropDbl &T, CoolPropDbl &rhomolar){
shared_ptr<CoolProp::HelmholtzEOSMixtureBackend> REF(new CoolProp::HelmholtzEOSBackend(reference_fluid));
if (T < 0 && rhomolar < 0){
// Collect some parameters
CoolPropDbl Tc = T_critical(),
Tc0 = REF->T_critical(),
rhocmolar = rhomolar_critical(),
rhocmolar0 = REF->rhomolar_critical();
// Starting guess values for shape factors
CoolPropDbl theta = 1;
CoolPropDbl phi = 1;
// The equivalent substance reducing ratios
CoolPropDbl f = Tc/Tc0*theta;
CoolPropDbl h = rhocmolar0/rhocmolar*phi; // Must be the ratio of MOLAR densities!!
// Starting guesses for conformal state
T = this->T()/f;
rhomolar = this->rhomolar()*h;
}
TransportRoutines::conformal_state_solver(*this, *REF, T, rhomolar);
}
CoolPropDbl HelmholtzEOSMixtureBackend::calc_Ttriple(void)
{
double summer = 0;
for (unsigned int i = 0; i < components.size(); ++i){
summer += mole_fractions[i]*components[i].EOS().Ttriple;
}
return summer;
}
CoolPropDbl HelmholtzEOSMixtureBackend::calc_p_triple(void)
{
double summer = 0;
for (unsigned int i = 0; i < components.size(); ++i){
summer += mole_fractions[i]*components[i].EOS().ptriple;
}
return summer;
}
std::string HelmholtzEOSMixtureBackend::calc_name(void)
{
if (components.size() != 1){
throw ValueError(format("calc_name is only valid for pure and pseudo-pure fluids, %d components", components.size()));
}
else{
return components[0].name;
}
}
CoolPropDbl HelmholtzEOSMixtureBackend::calc_saturated_liquid_keyed_output(parameters key) {
if ((key == iDmolar) && _rhoLmolar) return _rhoLmolar;
if (!SatL) throw ValueError("The saturated liquid state has not been set.");
return SatL->keyed_output(key);
}
CoolPropDbl HelmholtzEOSMixtureBackend::calc_saturated_vapor_keyed_output(parameters key) {
if ((key == iDmolar) && _rhoVmolar) return _rhoVmolar;
if (!SatV) throw ValueError("The saturated vapor state has not been set.");
return SatV->keyed_output(key);
}
void HelmholtzEOSMixtureBackend::calc_ideal_curve(const std::string &type, std::vector<double> &T, std::vector<double> &p){
if (type == "Joule-Thomson"){
JouleThomsonCurveTracer JTCT(this, 1e5, 800);
JTCT.trace(T, p);
}
else if (type == "Joule-Inversion"){
JouleInversionCurveTracer JICT(this, 1e5, 800);
JICT.trace(T, p);
}
else if (type == "Ideal"){
IdealCurveTracer ICT(this, 1e5, 800);
ICT.trace(T, p);
}
else if (type == "Boyle"){
BoyleCurveTracer BCT(this, 1e5, 800);
BCT.trace(T, p);
}
else{
throw ValueError(format("Invalid ideal curve type: %s", type.c_str()));
}
};
std::vector<std::string> HelmholtzEOSMixtureBackend::calc_fluid_names(void)
{
std::vector<std::string> out;
for (std::size_t i = 0; i < components.size(); ++i)
{
out.push_back(components[i].name);
}
return out;
}
CoolPropDbl HelmholtzEOSMixtureBackend::calc_ODP(void)
{
if (components.size() != 1){
throw ValueError(format("For now, calc_ODP is only valid for pure and pseudo-pure fluids, %d components", components.size()));
}
else{
CoolPropDbl v = components[0].environment.ODP;
if (!ValidNumber(v) || v < 0){ throw ValueError(format("ODP value is not specified or invalid")); }
return v;
}
}
CoolPropDbl HelmholtzEOSMixtureBackend::calc_GWP20(void)
{
if (components.size() != 1){
throw ValueError(format("For now, calc_GWP20 is only valid for pure and pseudo-pure fluids, %d components", components.size()));
}
else{
CoolPropDbl v = components[0].environment.GWP20;
if (!ValidNumber(v) || v < 0){ throw ValueError(format("GWP20 value is not specified or invalid"));}
return v;
}
}
CoolPropDbl HelmholtzEOSMixtureBackend::calc_GWP100(void)
{
if (components.size() != 1){
throw ValueError(format("For now, calc_GWP100 is only valid for pure and pseudo-pure fluids, %d components", components.size()));
}
else{
CoolPropDbl v = components[0].environment.GWP100;
if (!ValidNumber(v) || v < 0){ throw ValueError(format("GWP100 value is not specified or invalid")); }
return v;
}
}
CoolPropDbl HelmholtzEOSMixtureBackend::calc_GWP500(void)
{
if (components.size() != 1){
throw ValueError(format("For now, calc_GWP500 is only valid for pure and pseudo-pure fluids, %d components", components.size()));
}
else{
CoolPropDbl v = components[0].environment.GWP500;
if (!ValidNumber(v) || v < 0){ throw ValueError(format("GWP500 value is not specified or invalid")); }
return v;
}
}
CoolPropDbl HelmholtzEOSMixtureBackend::calc_T_critical(void)
{
if (components.size() != 1){
std::vector<CriticalState> critpts = calc_all_critical_points();
if (critpts.size() == 1){
//if (!critpts[0].stable){ throw ValueError(format("found one critical point but critical point is not stable")); }
return critpts[0].T;
}
else{
throw ValueError(format("critical point finding routine found %d critical points", critpts.size()));
}
}
else{
return components[0].crit.T;
}
}
CoolPropDbl HelmholtzEOSMixtureBackend::calc_p_critical(void)
{
if (components.size() != 1){
std::vector<CriticalState> critpts = calc_all_critical_points();
if (critpts.size() == 1){
//if (!critpts[0].stable){ throw ValueError(format("found one critical point but critical point is not stable")); }
return critpts[0].p;
}
else{
throw ValueError(format("critical point finding routine found %d critical points", critpts.size()));
}
}
else{
return components[0].crit.p;
}
}
CoolPropDbl HelmholtzEOSMixtureBackend::calc_rhomolar_critical(void)
{
if (components.size() != 1){
std::vector<CriticalState> critpts = calc_all_critical_points();
if (critpts.size() == 1){
//if (!critpts[0].stable){ throw ValueError(format("found one critical point but critical point is not stable")); }
return critpts[0].rhomolar;
}
else{
throw ValueError(format("critical point finding routine found %d critical points", critpts.size()));
}
}
else{
return components[0].crit.rhomolar;
}
}
CoolPropDbl HelmholtzEOSMixtureBackend::calc_pmax_sat(void)
{
if (is_pure_or_pseudopure)
{
if (components[0].EOS().pseudo_pure)
{
return components[0].EOS().max_sat_p.p;
}
else{
return p_critical();
}
}
else{
throw ValueError("calc_pmax_sat not yet defined for mixtures");
}
}
CoolPropDbl HelmholtzEOSMixtureBackend::calc_Tmax_sat(void)
{
if (is_pure_or_pseudopure)
{
if (components[0].EOS().pseudo_pure)
{
double Tmax_sat = components[0].EOS().max_sat_T.T;
if (!ValidNumber(Tmax_sat)){
return T_critical();
}
else{
return Tmax_sat;
}
}
else{
return T_critical();
}
}
else{
throw ValueError("calc_Tmax_sat not yet defined for mixtures");
}
}
void HelmholtzEOSMixtureBackend::calc_Tmin_sat(CoolPropDbl &Tmin_satL, CoolPropDbl &Tmin_satV)
{
if (is_pure_or_pseudopure)
{
Tmin_satL = components[0].EOS().sat_min_liquid.T;
Tmin_satV = components[0].EOS().sat_min_vapor.T;
return;
}
else{
throw ValueError("calc_Tmin_sat not yet defined for mixtures");
}
}
void HelmholtzEOSMixtureBackend::calc_pmin_sat(CoolPropDbl &pmin_satL, CoolPropDbl &pmin_satV)