forked from CoolProp/CoolProp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractState.cpp
More file actions
1222 lines (1150 loc) · 51.6 KB
/
Copy pathAbstractState.cpp
File metadata and controls
1222 lines (1150 loc) · 51.6 KB
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
/*
* AbstractState.cpp
*
* Created on: 21 Dec 2013
* Author: jowr
*/
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <stdlib.h>
#include "math.h"
#include "AbstractState.h"
#include "DataStructures.h"
#include "Backends/IF97/IF97Backend.h"
#include "Backends/Cubics/CubicBackend.h"
#include "Backends/Cubics/VTPRBackend.h"
#include "Backends/Incompressible/IncompressibleBackend.h"
#include "Backends/PCSAFT/PCSAFTBackend.h"
#if !defined(NO_TABULAR_BACKENDS)
#include "Backends/Tabular/TTSEBackend.h"
#include "Backends/Tabular/BicubicBackend.h"
#endif
namespace CoolProp {
/// This tiny class holds pointers to generators for the backends and can be used to look up
/// generators at runtime. This class should be populated through the use of static initialized
class BackendLibrary
{
private:
std::map<backend_families, shared_ptr<AbstractStateGenerator>> backends;
public:
void add_backend(const backend_families& bg, const shared_ptr<AbstractStateGenerator>& asg) {
backends[bg] = asg;
};
void get_generator_iterators(const backend_families& bg,
std::map<backend_families, shared_ptr<AbstractStateGenerator>>::const_iterator& generator,
std::map<backend_families, shared_ptr<AbstractStateGenerator>>::const_iterator& end) {
generator = backends.find(bg);
end = backends.end();
};
std::size_t size() {
return backends.size();
};
};
inline BackendLibrary& get_backend_library() {
static BackendLibrary the_library;
return the_library;
}
void register_backend(const backend_families& bf, shared_ptr<AbstractStateGenerator> gen) {
get_backend_library().add_backend(bf, gen);
};
class IF97BackendGenerator : public AbstractStateGenerator
{
public:
AbstractState* get_AbstractState(const std::vector<std::string>& fluid_names) {
if (fluid_names.size() == 1) { // Check that fluid_names[0] has only one component
std::string str = fluid_names[0]; // Check that the fluid name is an alias for "Water"
if ((upper(str) == "WATER") || (upper(str) == "H2O")) {
return new IF97Backend();
} else {
throw ValueError(format("The IF97 backend returns Water props only; fluid name [%s] not allowed", fluid_names[0].c_str()));
}
} else {
throw ValueError(format("The IF97 backend does not support mixtures, only Water"));
};
};
};
// This static initialization will cause the generator to register
static GeneratorInitializer<IF97BackendGenerator> if97_gen(IF97_BACKEND_FAMILY);
class SRKGenerator : public AbstractStateGenerator
{
public:
AbstractState* get_AbstractState(const std::vector<std::string>& fluid_names) {
return new SRKBackend(fluid_names, get_config_double(R_U_CODATA));
};
};
static GeneratorInitializer<SRKGenerator> srk_gen(CoolProp::SRK_BACKEND_FAMILY);
class PRGenerator : public AbstractStateGenerator
{
public:
AbstractState* get_AbstractState(const std::vector<std::string>& fluid_names) {
return new PengRobinsonBackend(fluid_names, get_config_double(R_U_CODATA));
};
};
static GeneratorInitializer<PRGenerator> pr_gen(CoolProp::PR_BACKEND_FAMILY);
class IncompressibleBackendGenerator : public AbstractStateGenerator
{
public:
AbstractState* get_AbstractState(const std::vector<std::string>& fluid_names) {
if (fluid_names.size() != 1) {
throw ValueError(format("For INCOMP backend, name vector must be one element long"));
}
return new IncompressibleBackend(fluid_names[0]);
};
};
// This static initialization will cause the generator to register
static GeneratorInitializer<IncompressibleBackendGenerator> incomp_gen(INCOMP_BACKEND_FAMILY);
class VTPRGenerator : public CoolProp::AbstractStateGenerator
{
public:
CoolProp::AbstractState* get_AbstractState(const std::vector<std::string>& fluid_names) {
return new CoolProp::VTPRBackend(fluid_names, CoolProp::get_config_double(R_U_CODATA));
};
};
// This static initialization will cause the generator to register
static CoolProp::GeneratorInitializer<VTPRGenerator> vtpr_gen(CoolProp::VTPR_BACKEND_FAMILY);
class PCSAFTGenerator : public CoolProp::AbstractStateGenerator
{
public:
CoolProp::AbstractState* get_AbstractState(const std::vector<std::string>& fluid_names) {
return new CoolProp::PCSAFTBackend(fluid_names);
};
};
// This static initialization will cause the generator to register
static CoolProp::GeneratorInitializer<PCSAFTGenerator> pcsaft_gen(CoolProp::PCSAFT_BACKEND_FAMILY);
AbstractState* AbstractState::factory(const std::string& backend, const std::vector<std::string>& fluid_names) {
if (get_debug_level() > 0) {
std::cout << "AbstractState::factory(" << backend << "," << stringvec_to_string(fluid_names) << ")" << std::endl;
}
backend_families f1;
std::string f2;
extract_backend_families_string(backend, f1, f2);
std::map<backend_families, shared_ptr<AbstractStateGenerator>>::const_iterator gen, end;
get_backend_library().get_generator_iterators(f1, gen, end);
if (get_debug_level() > 0) {
std::cout << "AbstractState::factory backend_library size: " << get_backend_library().size() << std::endl;
}
if (gen != end) {
// One of the registered backends was able to match the given backend family
return gen->second->get_AbstractState(fluid_names);
}
#if !defined(NO_TABULAR_BACKENDS)
else if (f1 == TTSE_BACKEND_FAMILY) {
// Will throw if there is a problem with this backend
shared_ptr<AbstractState> AS(factory(f2, fluid_names));
return new TTSEBackend(AS);
} else if (f1 == BICUBIC_BACKEND_FAMILY) {
// Will throw if there is a problem with this backend
shared_ptr<AbstractState> AS(factory(f2, fluid_names));
return new BicubicBackend(AS);
}
#endif
else if (!backend.compare("?") || backend.empty()) {
std::size_t idel = fluid_names[0].find("::");
// Backend has not been specified, and we have to figure out what the backend is by parsing the string
if (idel == std::string::npos) // No '::' found, no backend specified, try HEOS, otherwise a failure
{
// Figure out what backend to use
return factory("HEOS", fluid_names);
} else {
// Split string at the '::' into two std::string, call again
return factory(std::string(fluid_names[0].begin(), fluid_names[0].begin() + idel),
std::string(fluid_names[0].begin() + (idel + 2), fluid_names[0].end()));
}
} else {
throw ValueError(format("Invalid backend name [%s] to factory function", backend.c_str()));
}
}
std::vector<std::string> AbstractState::fluid_names(void) {
return calc_fluid_names();
}
bool AbstractState::clear_comp_change() {
// Reset all instances of CachedElement and overwrite
// the internal double values with -_HUGE
this->_R = _HUGE;
this->_gas_constant.clear();
this->_molar_mass.clear();
this->_critical.fill(_HUGE);
this->_reducing.fill(_HUGE);
return true;
}
bool AbstractState::clear() {
// Reset all instances of CachedElement and overwrite
// the internal double values with -_HUGE
this->_R = _HUGE;
this->_gas_constant.clear();
this->_molar_mass.clear();
/// Ancillary curve values
this->_rhoLanc.clear();
this->_rhoVanc.clear();
this->_pVanc.clear();
this->_pLanc.clear();
this->_TVanc.clear();
this->_TLanc.clear();
this->_critical.fill(_HUGE);
this->_reducing.fill(_HUGE);
/// Bulk values
this->_rhomolar = -_HUGE;
this->_T = -_HUGE;
this->_p = -_HUGE;
this->_Q = -_HUGE;
this->_tau.clear();
this->_delta.clear();
this->_umolar.clear();
this->_cpmolar.clear();
this->_cp0molar.clear();
this->_cvmolar.clear();
this->_speed_sound.clear();
this->_hmolar.clear();
this->_smolar.clear();
this->_gibbsmolar.clear();
this->_helmholtzmolar.clear();
this->_logp.clear();
this->_logrhomolar.clear();
this->_hmolar_excess.clear();
this->_smolar_excess.clear();
this->_gibbsmolar_excess.clear();
this->_volumemolar_excess.clear();
this->_umolar_excess.clear();
this->_helmholtzmolar_excess.clear();
this->_hmolar_residual.clear();
this->_smolar_residual.clear();
this->_gibbsmolar_residual.clear();
/// Smoothing values
this->_rho_spline.clear();
this->_drho_spline_dh__constp.clear();
this->_drho_spline_dp__consth.clear();
/// Cached low-level elements for in-place calculation of other properties
this->_alpha0.clear();
this->_dalpha0_dTau.clear();
this->_dalpha0_dDelta.clear();
this->_d2alpha0_dTau2.clear();
this->_d2alpha0_dDelta_dTau.clear();
this->_d2alpha0_dDelta2.clear();
this->_d3alpha0_dTau3.clear();
this->_d3alpha0_dDelta_dTau2.clear();
this->_d3alpha0_dDelta2_dTau.clear();
this->_d3alpha0_dDelta3.clear();
this->_alphar.clear();
this->_dalphar_dTau.clear();
this->_dalphar_dDelta.clear();
this->_d2alphar_dTau2.clear();
this->_d2alphar_dDelta_dTau.clear();
this->_d2alphar_dDelta2.clear();
this->_d3alphar_dTau3.clear();
this->_d3alphar_dDelta_dTau2.clear();
this->_d3alphar_dDelta2_dTau.clear();
this->_d3alphar_dDelta3.clear();
this->_dalphar_dDelta_lim.clear();
this->_d2alphar_dDelta2_lim.clear();
this->_d2alphar_dDelta_dTau_lim.clear();
this->_d3alphar_dDelta2_dTau_lim.clear();
/// Two-Phase variables
this->_rhoLmolar.clear();
this->_rhoVmolar.clear();
/// Transport properties
this->_viscosity.clear();
this->_conductivity.clear();
this->_surface_tension.clear();
return true;
}
void AbstractState::mass_to_molar_inputs(CoolProp::input_pairs& input_pair, CoolPropDbl& value1, CoolPropDbl& value2) {
// Check if a mass based input, convert it to molar units
switch (input_pair) {
case DmassT_INPUTS: ///< Mass density in kg/m^3, Temperature in K
//case HmassT_INPUTS: ///< Enthalpy in J/kg, Temperature in K (NOT CURRENTLY IMPLEMENTED)
case SmassT_INPUTS: ///< Entropy in J/kg/K, Temperature in K
//case TUmass_INPUTS: ///< Temperature in K, Internal energy in J/kg (NOT CURRENTLY IMPLEMENTED)
case DmassP_INPUTS: ///< Mass density in kg/m^3, Pressure in Pa
case DmassQ_INPUTS: ///< Mass density in kg/m^3, molar quality
case HmassP_INPUTS: ///< Enthalpy in J/kg, Pressure in Pa
case PSmass_INPUTS: ///< Pressure in Pa, Entropy in J/kg/K
case PUmass_INPUTS: ///< Pressure in Pa, Internal energy in J/kg
case HmassSmass_INPUTS: ///< Enthalpy in J/kg, Entropy in J/kg/K
case SmassUmass_INPUTS: ///< Entropy in J/kg/K, Internal energy in J/kg
case DmassHmass_INPUTS: ///< Mass density in kg/m^3, Enthalpy in J/kg
case DmassSmass_INPUTS: ///< Mass density in kg/m^3, Entropy in J/kg/K
case DmassUmass_INPUTS: ///< Mass density in kg/m^3, Internal energy in J/kg
{
// Set the cache value for the molar mass if it hasn't been set yet
molar_mass();
// Molar mass (just for compactness of the following switch)
CoolPropDbl mm = static_cast<CoolPropDbl>(_molar_mass);
switch (input_pair) {
case DmassT_INPUTS:
input_pair = DmolarT_INPUTS;
value1 /= mm;
break;
//case HmassT_INPUTS: input_pair = HmolarT_INPUTS; value1 *= mm; break; (NOT CURRENTLY IMPLEMENTED)
case SmassT_INPUTS:
input_pair = SmolarT_INPUTS;
value1 *= mm;
break;
//case TUmass_INPUTS: input_pair = TUmolar_INPUTS; value2 *= mm; break; (NOT CURRENTLY IMPLEMENTED)
case DmassP_INPUTS:
input_pair = DmolarP_INPUTS;
value1 /= mm;
break;
case DmassQ_INPUTS:
input_pair = DmolarQ_INPUTS;
value1 /= mm;
break;
case HmassP_INPUTS:
input_pair = HmolarP_INPUTS;
value1 *= mm;
break;
case PSmass_INPUTS:
input_pair = PSmolar_INPUTS;
value2 *= mm;
break;
case PUmass_INPUTS:
input_pair = PUmolar_INPUTS;
value2 *= mm;
break;
case HmassSmass_INPUTS:
input_pair = HmolarSmolar_INPUTS;
value1 *= mm;
value2 *= mm;
break;
case SmassUmass_INPUTS:
input_pair = SmolarUmolar_INPUTS;
value1 *= mm;
value2 *= mm;
break;
case DmassHmass_INPUTS:
input_pair = DmolarHmolar_INPUTS;
value1 /= mm;
value2 *= mm;
break;
case DmassSmass_INPUTS:
input_pair = DmolarSmolar_INPUTS;
value1 /= mm;
value2 *= mm;
break;
case DmassUmass_INPUTS:
input_pair = DmolarUmolar_INPUTS;
value1 /= mm;
value2 *= mm;
break;
default:
break;
}
break;
}
default:
return;
}
}
double AbstractState::trivial_keyed_output(parameters key) {
if (get_debug_level() >= 50)
std::cout << format("AbstractState: trivial_keyed_output called for %s ", get_parameter_information(key, "short").c_str()) << std::endl;
switch (key) {
case imolar_mass:
return molar_mass();
case iacentric_factor:
return acentric_factor();
case igas_constant:
return gas_constant();
case iT_min:
return Tmin();
case iT_triple:
return Ttriple();
case iT_max:
return Tmax();
case iP_max:
return pmax();
case iP_min:
case iP_triple:
return this->p_triple();
case iT_reducing:
return calc_T_reducing();
case irhomolar_reducing:
return calc_rhomolar_reducing();
case iP_reducing:
return calc_p_reducing();
case iP_critical:
return this->p_critical();
case iT_critical:
return this->T_critical();
case irhomolar_critical:
return this->rhomolar_critical();
case irhomass_critical:
return this->rhomass_critical();
case iODP:
return this->calc_ODP();
case iGWP100:
return this->calc_GWP100();
case iGWP20:
return this->calc_GWP20();
case iGWP500:
return this->calc_GWP500();
case ifraction_min:
return this->calc_fraction_min();
case ifraction_max:
return this->calc_fraction_max();
case iT_freeze:
return this->calc_T_freeze();
case iFH:
return this->calc_flame_hazard();
case iHH:
return this->calc_health_hazard();
case iPH:
return this->calc_physical_hazard();
case idipole_moment:
return this->calc_dipole_moment();
default:
throw ValueError(
format("This input [%d: \"%s\"] is not valid for trivial_keyed_output", key, get_parameter_information(key, "short").c_str()));
}
}
double AbstractState::keyed_output(parameters key) {
if (get_debug_level() >= 50)
std::cout << format("AbstractState: keyed_output called for %s ", get_parameter_information(key, "short").c_str()) << std::endl;
// Handle trivial inputs
if (is_trivial_parameter(key)) {
return trivial_keyed_output(key);
}
switch (key) {
case iQ:
return Q();
case iT:
return T();
case iP:
return p();
case iDmolar:
return rhomolar();
case iDmass:
return rhomass();
case iHmolar:
return hmolar();
case iHmolar_residual:
return hmolar_residual();
case iHmass:
return hmass();
case iSmolar:
return smolar();
case iSmolar_residual:
return smolar_residual();
case iSmass:
return smass();
case iUmolar:
return umolar();
case iUmass:
return umass();
case iGmolar:
return gibbsmolar();
case iGmolar_residual:
return gibbsmolar_residual();
case iGmass:
return gibbsmass();
case iHelmholtzmolar:
return helmholtzmolar();
case iHelmholtzmass:
return helmholtzmass();
case iCvmolar:
return cvmolar();
case iCvmass:
return cvmass();
case iCpmolar:
return cpmolar();
case iCp0molar:
return cp0molar();
case iCpmass:
return cpmass();
case iCp0mass:
return cp0mass();
case imolar_mass:
return molar_mass();
case iT_reducing:
return get_reducing_state().T;
case irhomolar_reducing:
return get_reducing_state().rhomolar;
case ispeed_sound:
return speed_sound();
case ialphar:
return alphar();
case ialpha0:
return alpha0();
case idalpha0_ddelta_consttau:
return dalpha0_dDelta();
case id2alpha0_ddelta2_consttau:
return d2alpha0_dDelta2();
case id3alpha0_ddelta3_consttau:
return d3alpha0_dDelta3();
case idalpha0_dtau_constdelta:
return dalpha0_dTau();
case idalphar_ddelta_consttau:
return dalphar_dDelta();
case idalphar_dtau_constdelta:
return dalphar_dTau();
case iBvirial:
return Bvirial();
case idBvirial_dT:
return dBvirial_dT();
case iCvirial:
return Cvirial();
case idCvirial_dT:
return dCvirial_dT();
case iisothermal_compressibility:
return isothermal_compressibility();
case iisobaric_expansion_coefficient:
return isobaric_expansion_coefficient();
case iisentropic_expansion_coefficient:
return isentropic_expansion_coefficient();
case iviscosity:
return viscosity();
case iconductivity:
return conductivity();
case iPrandtl:
return Prandtl();
case isurface_tension:
return surface_tension();
case iPhase:
return phase();
case iZ:
return compressibility_factor();
case iPIP:
return PIP();
case ifundamental_derivative_of_gas_dynamics:
return fundamental_derivative_of_gas_dynamics();
default:
throw ValueError(format("This input [%d: \"%s\"] is not valid for keyed_output", key, get_parameter_information(key, "short").c_str()));
}
}
double AbstractState::tau(void) {
if (!_tau) _tau = calc_reciprocal_reduced_temperature();
return _tau;
}
double AbstractState::delta(void) {
if (!_delta) _delta = calc_reduced_density();
return _delta;
}
double AbstractState::Tmin(void) {
return calc_Tmin();
}
double AbstractState::Tmax(void) {
return calc_Tmax();
}
double AbstractState::Ttriple(void) {
return calc_Ttriple();
}
double AbstractState::pmax(void) {
return calc_pmax();
}
double AbstractState::T_critical(void) {
return calc_T_critical();
}
double AbstractState::T_reducing(void) {
if (!ValidNumber(_reducing.T)) {
calc_reducing_state();
}
return _reducing.T;
}
double AbstractState::p_critical(void) {
return calc_p_critical();
}
double AbstractState::p_triple(void) {
return calc_p_triple();
}
double AbstractState::rhomolar_critical(void) {
return calc_rhomolar_critical();
}
double AbstractState::rhomass_critical(void) {
return calc_rhomolar_critical() * molar_mass();
}
double AbstractState::rhomolar_reducing(void) {
if (!ValidNumber(_reducing.rhomolar)) {
calc_reducing_state();
}
return _reducing.rhomolar;
}
double AbstractState::rhomass_reducing(void) {
return rhomolar_reducing() * molar_mass();
}
double AbstractState::hmolar(void) {
if (!_hmolar) _hmolar = calc_hmolar();
return _hmolar;
}
double AbstractState::hmolar_residual(void) {
if (!_hmolar_residual) _hmolar_residual = calc_hmolar_residual();
return _hmolar_residual;
}
double AbstractState::hmolar_excess(void) {
if (!_hmolar_excess) calc_excess_properties();
return _hmolar_excess;
}
double AbstractState::smolar(void) {
if (!_smolar) _smolar = calc_smolar();
return _smolar;
}
double AbstractState::smolar_residual(void) {
if (!_smolar_residual) _smolar_residual = calc_smolar_residual();
return _smolar_residual;
}
double AbstractState::neff(void) {
double tau = calc_T_reducing()/_T;
double delta = _rhomolar/calc_rhomolar_reducing();
double Ar01 = delta*dalphar_dDelta();
double Ar11 = tau*delta*d2alphar_dDelta_dTau();
double Ar20 = tau*tau*d2alphar_dTau2();
return -3.0*(Ar01-Ar11)/Ar20;
}
double AbstractState::smolar_excess(void) {
if (!_smolar_excess) calc_excess_properties();
return _smolar_excess;
}
double AbstractState::umolar(void) {
if (!_umolar) _umolar = calc_umolar();
return _umolar;
}
double AbstractState::umolar_excess(void) {
if (!_umolar_excess) calc_excess_properties();
return _umolar_excess;
}
double AbstractState::gibbsmolar(void) {
if (!_gibbsmolar) _gibbsmolar = calc_gibbsmolar();
return _gibbsmolar;
}
double AbstractState::gibbsmolar_residual(void) {
if (!_gibbsmolar_residual) _gibbsmolar_residual = calc_gibbsmolar_residual();
return _gibbsmolar_residual;
}
double AbstractState::gibbsmolar_excess(void) {
if (!_gibbsmolar_excess) calc_excess_properties();
return _gibbsmolar_excess;
}
double AbstractState::helmholtzmolar(void) {
if (!_helmholtzmolar) _helmholtzmolar = calc_helmholtzmolar();
return _helmholtzmolar;
}
double AbstractState::helmholtzmolar_excess(void) {
if (!_helmholtzmolar_excess) calc_excess_properties();
return _helmholtzmolar_excess;
}
double AbstractState::volumemolar_excess(void) {
if (!_volumemolar_excess) calc_excess_properties();
return _volumemolar_excess;
}
double AbstractState::cpmolar(void) {
if (!_cpmolar) _cpmolar = calc_cpmolar();
return _cpmolar;
}
double AbstractState::cp0molar(void) {
return calc_cpmolar_idealgas();
}
double AbstractState::cvmolar(void) {
if (!_cvmolar) _cvmolar = calc_cvmolar();
return _cvmolar;
}
double AbstractState::speed_sound(void) {
if (!_speed_sound) _speed_sound = calc_speed_sound();
return _speed_sound;
}
double AbstractState::viscosity(void) {
if (!_viscosity) _viscosity = calc_viscosity();
return _viscosity;
}
double AbstractState::conductivity(void) {
if (!_conductivity) _conductivity = calc_conductivity();
return _conductivity;
}
double AbstractState::melting_line(int param, int given, double value) {
return calc_melting_line(param, given, value);
}
double AbstractState::acentric_factor() {
return calc_acentric_factor();
}
double AbstractState::saturation_ancillary(parameters param, int Q, parameters given, double value) {
return calc_saturation_ancillary(param, Q, given, value);
}
double AbstractState::surface_tension(void) {
if (!_surface_tension) _surface_tension = calc_surface_tension();
return _surface_tension;
}
double AbstractState::molar_mass(void) {
if (!_molar_mass) _molar_mass = calc_molar_mass();
return _molar_mass;
}
double AbstractState::gas_constant(void) {
if (!_gas_constant) _gas_constant = calc_gas_constant();
return _gas_constant;
}
double AbstractState::fugacity_coefficient(std::size_t i) {
// TODO: Cache the fug. coeff for each component
return calc_fugacity_coefficient(i);
}
std::vector<double> AbstractState::fugacity_coefficients() {
// TODO: Cache the fug. coeff for each component
return calc_fugacity_coefficients();
}
double AbstractState::fugacity(std::size_t i) {
// TODO: Cache the fug. coeff for each component
return calc_fugacity(i);
}
double AbstractState::chemical_potential(std::size_t i) {
// TODO: Cache the chemical potential for each component
return calc_chemical_potential(i);
}
void AbstractState::build_phase_envelope(const std::string& type) {
calc_phase_envelope(type);
}
double AbstractState::isothermal_compressibility(void) {
return 1.0 / _rhomolar * first_partial_deriv(iDmolar, iP, iT);
}
double AbstractState::isobaric_expansion_coefficient(void) {
return -1.0 / _rhomolar * first_partial_deriv(iDmolar, iT, iP);
}
double AbstractState::isentropic_expansion_coefficient(void) {
return _rhomolar / _p * first_partial_deriv(iP, iDmolar, iSmolar);
}
double AbstractState::Bvirial(void) {
return calc_Bvirial();
}
double AbstractState::Cvirial(void) {
return calc_Cvirial();
}
double AbstractState::dBvirial_dT(void) {
return calc_dBvirial_dT();
}
double AbstractState::dCvirial_dT(void) {
return calc_dCvirial_dT();
}
double AbstractState::compressibility_factor(void) {
return calc_compressibility_factor();
}
double AbstractState::fundamental_derivative_of_gas_dynamics() {
// See Colonna, FPE, 2010, Eq. 1
return 1 + this->second_partial_deriv(iP, iDmass, iSmolar, iDmass, iSmolar) * this->rhomass() / (2 * powInt(speed_sound(), 2));
};
// Get the derivatives of the parameters in the partial derivative with respect to T and rho
void get_dT_drho(AbstractState& AS, parameters index, CoolPropDbl& dT, CoolPropDbl& drho) {
CoolPropDbl T = AS.T(), rho = AS.rhomolar(), rhor = AS.rhomolar_reducing(), Tr = AS.T_reducing(), dT_dtau = -pow(T, 2) / Tr,
R = AS.gas_constant(), delta = rho / rhor, tau = Tr / T;
switch (index) {
case iT:
dT = 1;
drho = 0;
break;
case iDmolar:
dT = 0;
drho = 1;
break;
case iDmass:
dT = 0;
drho = AS.molar_mass();
break;
case iP: {
// dp/drho|T
drho = R * T * (1 + 2 * delta * AS.dalphar_dDelta() + pow(delta, 2) * AS.d2alphar_dDelta2());
// dp/dT|rho
dT = rho * R * (1 + delta * AS.dalphar_dDelta() - tau * delta * AS.d2alphar_dDelta_dTau());
break;
}
case iHmass:
case iHmolar: {
// dh/dT|rho
dT = R
* (-pow(tau, 2) * (AS.d2alpha0_dTau2() + AS.d2alphar_dTau2())
+ (1 + delta * AS.dalphar_dDelta() - tau * delta * AS.d2alphar_dDelta_dTau()));
// dh/drhomolar|T
drho = T * R / rho * (tau * delta * AS.d2alphar_dDelta_dTau() + delta * AS.dalphar_dDelta() + pow(delta, 2) * AS.d2alphar_dDelta2());
if (index == iHmass) {
// dhmolar/drhomolar|T * dhmass/dhmolar where dhmass/dhmolar = 1/mole mass
drho /= AS.molar_mass();
dT /= AS.molar_mass();
}
break;
}
case iSmass:
case iSmolar: {
// ds/dT|rho
dT = R / T * (-pow(tau, 2) * (AS.d2alpha0_dTau2() + AS.d2alphar_dTau2()));
// ds/drho|T
drho = R / rho * (-(1 + delta * AS.dalphar_dDelta() - tau * delta * AS.d2alphar_dDelta_dTau()));
if (index == iSmass) {
// ds/drho|T / drhomass/drhomolar where drhomass/drhomolar = mole mass
drho /= AS.molar_mass();
dT /= AS.molar_mass();
}
break;
}
case iUmass:
case iUmolar: {
// du/dT|rho
dT = R * (-pow(tau, 2) * (AS.d2alpha0_dTau2() + AS.d2alphar_dTau2()));
// du/drho|T
drho = AS.T() * R / rho * (tau * delta * AS.d2alphar_dDelta_dTau());
if (index == iUmass) {
// du/drho|T / drhomass/drhomolar where drhomass/drhomolar = mole mass
drho /= AS.molar_mass();
dT /= AS.molar_mass();
}
break;
}
case iGmass:
case iGmolar: {
// dg/dT|rho
double dTau_dT = 1 / dT_dtau;
dT = R * AS.T() * (AS.dalpha0_dTau() + AS.dalphar_dTau() + AS.delta() * AS.d2alphar_dDelta_dTau()) * dTau_dT
+ R * (1 + AS.alpha0() + AS.alphar() + AS.delta() * AS.dalphar_dDelta());
// dg/drho|T
double dDelta_drho = 1 / rhor;
drho = AS.T() * R * (AS.dalpha0_dDelta() + AS.dalphar_dDelta() + AS.delta() * AS.d2alphar_dDelta2() + AS.dalphar_dDelta()) * dDelta_drho;
if (index == iGmass) {
// dg/drho|T / drhomass/drhomolar where drhomass/drhomolar = mole mass
drho /= AS.molar_mass();
dT /= AS.molar_mass();
}
break;
}
case iTau:
dT = 1 / dT_dtau;
drho = 0;
break;
case iDelta:
dT = 0;
drho = 1 / rhor;
break;
case iCvmolar:
case iCvmass: {
// use the second order derivative of internal energy
// make it cleaner by using the function get_dT_drho_second_derivatives directly?
// dcvdT|rho = d2u/dT2|rho
dT = R / T * pow(tau, 2) * (tau * (AS.d3alpha0_dTau3() + AS.d3alphar_dTau3()) + 2 * (AS.d2alpha0_dTau2() + AS.d2alphar_dTau2()));
// dcvdrho|T = d2u/dT/drho
drho = R / rho * (-pow(tau, 2) * delta * AS.d3alphar_dDelta_dTau2());
if (index == iCvmass) {
drho /= AS.molar_mass();
dT /= AS.molar_mass();
}
break;
}
case iCpmolar:
case iCpmass: {
// dcp/dT|rho = d2h/dT2 + dh/drho * dP/dT * d2P/drhodT / ( dp/drho )^2 - ( d2h/dTdrho * dP/dT + dh/drho * d2P/dT2 ) / ( dP/drho )
dT = R / T * pow(tau, 2)
* (tau * (AS.d3alpha0_dTau3() + AS.d3alphar_dTau3()) + 2 * (AS.d2alpha0_dTau2() + AS.d2alphar_dTau2())
+ delta * AS.d3alphar_dDelta_dTau2());
dT += (T * R / rho * (tau * delta * AS.d2alphar_dDelta_dTau() + delta * AS.dalphar_dDelta() + pow(delta, 2) * AS.d2alphar_dDelta2()))
* (rho * R * (1 + delta * AS.dalphar_dDelta() - tau * delta * AS.d2alphar_dDelta_dTau()))
* (R
* (1 + 2 * delta * AS.dalphar_dDelta() + pow(delta, 2) * AS.d2alphar_dDelta2() - 2 * delta * tau * AS.d2alphar_dDelta_dTau()
- tau * pow(delta, 2) * AS.d3alphar_dDelta2_dTau()))
/ pow(R * T * (1 + 2 * delta * AS.dalphar_dDelta() + pow(delta, 2) * AS.d2alphar_dDelta2()), 2);
dT -= ((R / rho * delta
* (delta * AS.d2alphar_dDelta2() - pow(tau, 2) * AS.d3alphar_dDelta_dTau2() + AS.dalphar_dDelta()
- tau * delta * AS.d3alphar_dDelta2_dTau() - tau * AS.d2alphar_dDelta_dTau()))
* (rho * R * (1 + delta * AS.dalphar_dDelta() - tau * delta * AS.d2alphar_dDelta_dTau()))
+ (T * R / rho * (tau * delta * AS.d2alphar_dDelta_dTau() + delta * AS.dalphar_dDelta() + pow(delta, 2) * AS.d2alphar_dDelta2()))
* (rho * R / T * (pow(tau, 2) * delta * AS.d3alphar_dDelta_dTau2())))
/ (R * T * (1 + 2 * delta * AS.dalphar_dDelta() + pow(delta, 2) * AS.d2alphar_dDelta2()));
// dcpdrho|T = d2h/dTdrho + dh/drho * dP/dT * d2P/drho2 / ( dp/drho )^2 - ( d2h/drho2 * dP/dT + dh/drho * d2P/dTdrho ) / ( dP/drho )
drho = R / rho * delta
* (delta * AS.d2alphar_dDelta2() - pow(tau, 2) * AS.d3alphar_dDelta_dTau2() + AS.dalphar_dDelta()
- tau * delta * AS.d3alphar_dDelta2_dTau() - tau * AS.d2alphar_dDelta_dTau()); //d2h/dTdrho
drho +=
(T * R / rho * (tau * delta * AS.d2alphar_dDelta_dTau() + delta * AS.dalphar_dDelta() + pow(delta, 2) * AS.d2alphar_dDelta2()))
* (rho * R * (1 + delta * AS.dalphar_dDelta() - tau * delta * AS.d2alphar_dDelta_dTau()))
* (T * R / rho * (2 * delta * AS.dalphar_dDelta() + 4 * pow(delta, 2) * AS.d2alphar_dDelta2() + pow(delta, 3) * AS.d3alphar_dDelta3()))
/ pow(R * T * (1 + 2 * delta * AS.dalphar_dDelta() + pow(delta, 2) * AS.d2alphar_dDelta2()), 2);
drho -= ((R * T * pow(delta / rho, 2) * (tau * AS.d3alphar_dDelta2_dTau() + 2 * AS.d2alphar_dDelta2() + delta * AS.d3alphar_dDelta3()))
* (rho * R * (1 + delta * AS.dalphar_dDelta() - tau * delta * AS.d2alphar_dDelta_dTau()))
+ (T * R / rho * (tau * delta * AS.d2alphar_dDelta_dTau() + delta * AS.dalphar_dDelta() + pow(delta, 2) * AS.d2alphar_dDelta2()))
* (R
* (1 + 2 * delta * AS.dalphar_dDelta() + pow(delta, 2) * AS.d2alphar_dDelta2()
- 2 * delta * tau * AS.d2alphar_dDelta_dTau() - tau * pow(delta, 2) * AS.d3alphar_dDelta2_dTau())))
/ (R * T * (1 + 2 * delta * AS.dalphar_dDelta() + pow(delta, 2) * AS.d2alphar_dDelta2()));
if (index == iCpmass) {
drho /= AS.molar_mass();
dT /= AS.molar_mass();
}
break;
}
case ispeed_sound: {
//dwdT
double aa = 1.0 + delta * AS.dalphar_dDelta() - delta * tau * AS.d2alphar_dDelta_dTau();
double bb = pow(tau, 2) * (AS.d2alpha0_dTau2() + AS.d2alphar_dTau2());
double daa_dTau = -delta * tau * AS.d3alphar_dDelta_dTau2();
double dbb_dTau = pow(tau, 2) * (AS.d3alpha0_dTau3() + AS.d3alphar_dTau3()) + 2.0 * tau * (AS.d2alpha0_dTau2() + AS.d2alphar_dTau2());
double w = AS.speed_sound();
dT = 1.0 / 2.0 / w / T
* (pow(w, 2)
- R * Tr / AS.molar_mass()
* (2.0 * delta * AS.d2alphar_dDelta_dTau() + pow(delta, 2) * AS.d3alphar_dDelta2_dTau()
- (2 * aa / bb * daa_dTau - pow(aa / bb, 2) * dbb_dTau)));
//dwdrho
double daa_dDelta =
AS.dalphar_dDelta() + delta * AS.d2alphar_dDelta2() - tau * (AS.d2alphar_dDelta_dTau() + delta * AS.d3alphar_dDelta2_dTau());
double dbb_dDelta = pow(tau, 2) * (AS.d3alpha0_dDelta_dTau2() + AS.d3alphar_dDelta_dTau2());
drho = R * T / 2.0 / AS.molar_mass() / w / rhor
* (2.0 * (AS.dalphar_dDelta() + delta * AS.d2alphar_dDelta2())
+ (2.0 * delta * AS.d2alphar_dDelta2() + pow(delta, 2) * AS.d3alphar_dDelta3())
- (2 * aa / bb * daa_dDelta - pow(aa / bb, 2) * dbb_dDelta));
break;
}
default:
throw ValueError(format("input to get_dT_drho[%s] is invalid", get_parameter_information(index, "short").c_str()));
}
}
void get_dT_drho_second_derivatives(AbstractState& AS, int index, CoolPropDbl& dT2, CoolPropDbl& drho_dT, CoolPropDbl& drho2) {
CoolPropDbl T = AS.T(), rho = AS.rhomolar(), rhor = AS.rhomolar_reducing(), Tr = AS.T_reducing(), R = AS.gas_constant(), delta = rho / rhor,
tau = Tr / T;
// Here we use T and rho as independent variables since derivations are already done by Thorade, 2013,
// Partial derivatives of thermodynamic state propertiesfor dynamic simulation, DOI 10.1007/s12665-013-2394-z
switch (index) {
case iT:
case iDmass:
case iDmolar:
dT2 = 0; // d2rhomolar_dtau2
drho2 = 0;
drho_dT = 0;
break;
case iTau:
dT2 = 2 * Tr / pow(T, 3);
drho_dT = 0;
drho2 = 0;
break;
case iDelta:
dT2 = 0;
drho_dT = 0;
drho2 = 0;
break;
case iP: {
drho2 =
T * R / rho * (2 * delta * AS.dalphar_dDelta() + 4 * pow(delta, 2) * AS.d2alphar_dDelta2() + pow(delta, 3) * AS.d3alphar_dDelta3());
dT2 = rho * R / T * (pow(tau, 2) * delta * AS.d3alphar_dDelta_dTau2());
drho_dT = R
* (1 + 2 * delta * AS.dalphar_dDelta() + pow(delta, 2) * AS.d2alphar_dDelta2() - 2 * delta * tau * AS.d2alphar_dDelta_dTau()
- tau * pow(delta, 2) * AS.d3alphar_dDelta2_dTau());
break;
}
case iHmass:
case iHmolar: {
// d2h/drho2|T
drho2 = R * T * pow(delta / rho, 2) * (tau * AS.d3alphar_dDelta2_dTau() + 2 * AS.d2alphar_dDelta2() + delta * AS.d3alphar_dDelta3());
// d2h/dT2|rho
dT2 = R / T * pow(tau, 2)
* (tau * (AS.d3alpha0_dTau3() + AS.d3alphar_dTau3()) + 2 * (AS.d2alpha0_dTau2() + AS.d2alphar_dTau2())
+ delta * AS.d3alphar_dDelta_dTau2());
// d2h/drho/dT
drho_dT = R / rho * delta
* (delta * AS.d2alphar_dDelta2() - pow(tau, 2) * AS.d3alphar_dDelta_dTau2() + AS.dalphar_dDelta()
- tau * delta * AS.d3alphar_dDelta2_dTau() - tau * AS.d2alphar_dDelta_dTau());
if (index == iHmass) {
drho2 /= AS.molar_mass();
drho_dT /= AS.molar_mass();
dT2 /= AS.molar_mass();
}
break;
}
case iSmass:
case iSmolar: {
// d2s/rho2|T
drho2 = R / pow(rho, 2) * (1 - pow(delta, 2) * AS.d2alphar_dDelta2() + tau * pow(delta, 2) * AS.d3alphar_dDelta2_dTau());
// d2s/dT2|rho
dT2 = R * pow(tau / T, 2) * (tau * (AS.d3alpha0_dTau3() + AS.d3alphar_dTau3()) + 3 * (AS.d2alpha0_dTau2() + AS.d2alphar_dTau2()));
// d2s/drho/dT
drho_dT = R / (T * rho) * (-pow(tau, 2) * delta * AS.d3alphar_dDelta_dTau2());
if (index == iSmass) {
drho2 /= AS.molar_mass();
drho_dT /= AS.molar_mass();
dT2 /= AS.molar_mass();
}
break;
}
case iUmass:
case iUmolar: {
// d2u/rho2|T
drho2 = R * T * tau * pow(delta / rho, 2) * AS.d3alphar_dDelta2_dTau();
// d2u/dT2|rho
dT2 = R / T * pow(tau, 2) * (tau * (AS.d3alpha0_dTau3() + AS.d3alphar_dTau3()) + 2 * (AS.d2alpha0_dTau2() + AS.d2alphar_dTau2()));
// d2u/drho/dT
drho_dT = R / rho * (-pow(tau, 2) * delta * AS.d3alphar_dDelta_dTau2());
if (index == iUmass) {
drho2 /= AS.molar_mass();