-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGERG2008.hpp
More file actions
1602 lines (1493 loc) · 89.5 KB
/
GERG2008.hpp
File metadata and controls
1602 lines (1493 loc) · 89.5 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
#ifndef __GERG2008__
#define __GERG2008__
// We add both math headers to placate some non-standards-compliant compilers
#include <math.h>
#include "MathUtil.h"
#include <cmath>
#include <iostream>
#include <map>
#include <string>
// ********** This code is preliminary, and will be updated. **********
// ********** Use only for beta testing. **********
// Version 2.01 of routines for the calculation of thermodynamic
// properties from the AGA 8 Part 2 GERG-2008 equation of state.
// April, 2017
// Written by Eric W. Lemmon
// Applied Chemicals and Materials Division
// National Institute of Standards and Technology (NIST)
// Boulder, Colorado, USA
// Eric.Lemmon@nist.gov
// 303-497-7939
// C++ translation by Ian H. Bell
// Applied Chemicals and Materials Division
// National Institute of Standards and Technology (NIST)
// Boulder, Colorado, USA
// ian.bell@nist.gov
// Other contributors:
// Volker Heinemann, RMG Messtechnik GmbH
// Jason Lu, Thermo Fisher Scientific
// Ian Bell, NIST
// The publication for the AGA 8 equation of state is available from AGA
// and the Transmission Measurement Committee.
// The GERG-2008 equation of state was developed by Oliver Kunz and Wolfgang Wagner;
// Kunz, O. and Wagner, W.
// The GERG-2008 Wide-Range Equation of State for Natural Gases and Other Mixtures;
// An Expansion of GERG-2004
// J. Chem. Eng. Data, 57(11):3032-3091, 2012.
// Kunz, O., Klimeck, R., Wagner, W., and Jaeschke, M.
// The GERG-2004 Wide-Range Equation of State for Natural Gases and Other Mixtures
// GERG Technical Monograph 15
// Fortschr.-Ber. VDI, Reihe 6, Nr. 557, VDI Verlag, Düsseldorf, 2007.
// http://www.gerg.eu/public/uploads/files/publications/technical_monographs/tm15_04.pdf
// Subroutines contained here for property calculations:
// ***** Subroutine SetupGERG must be called once before calling other routines. ******
// Sub MolarMassGERG(x, Mm)
// Sub PressureGERG(T, D, x, P, Z)
// Sub DensityGERG(iFlag, T, P, x, D, ierr, herr)
// Sub PropertiesGERG(T, D, x, P, Z, dPdD, d2PdD2, d2PdTD, dPdT, U, H, S, Cv, Cp, W, G, JT, Kappa)
// Sub SetupGERG()
// The compositions in the x() array use the following order and must be sent as mole fractions:
// 1 - Methane
// 2 - Nitrogen
// 3 - Carbon dioxide
// 4 - Ethane
// 5 - Propane
// 6 - Isobutane
// 7 - n-Butane
// 8 - Isopentane
// 9 - n-Pentane
// 10 - n-Hexane
// 11 - n-Heptane
// 12 - n-Octane
// 13 - n-Nonane
// 14 - n-Decane
// 15 - Hydrogen
// 16 - Oxygen
// 17 - Carbon monoxide
// 18 - Water
// 19 - Hydrogen sulfide
// 20 - Helium
// 21 - Argon
// For example, a mixture (in moles) of 94% methane, 5% CO2, and 1% helium would be (in mole fractions):
// x(1)=0.94, x(3)=0.05, x(20)=0.01
class GERG2008{
private:
// Variables containing the common parameters in the GERG-2008 equations
double RGERG;
enum { NcGERG=21, MaxFlds=21, MaxMdl=10, MaxTrmM=12, MaxTrmP=24};
//const int NcGERG = 21, MaxFlds = 21, MaxMdl = 10, MaxTrmM = 12, MaxTrmP = 24;
int coik[MaxFlds+1][MaxTrmP+1], doik[MaxFlds+1][MaxTrmP+1], dijk[MaxMdl+1][MaxTrmM+1];
double Drold, Trold, Told, Trold2, xold[MaxFlds+1];
int mNumb[MaxFlds+1][MaxFlds+1], kpol[MaxFlds+1], kexp[MaxFlds+1], kpolij[MaxMdl+1], kexpij[MaxMdl+1];
double Dc[MaxFlds+1], Tc[MaxFlds+1], MMiGERG[MaxFlds+1], Vc3[MaxFlds+1], Tc2[MaxFlds+1];
double noik[MaxFlds+1][MaxTrmP+1], toik[MaxFlds+1][MaxTrmP+1];
double cijk[MaxMdl+1][MaxTrmM+1];
double eijk[MaxMdl+1][MaxTrmM+1], gijk[MaxMdl+1][MaxTrmM+1], nijk[MaxMdl+1][MaxTrmM+1], tijk[MaxMdl+1][MaxTrmM+1];
double btij[MaxFlds+1][MaxFlds+1], bvij[MaxFlds+1][MaxFlds+1], gtij[MaxFlds+1][MaxFlds+1], gvij[MaxFlds+1][MaxFlds+1];
double fij[MaxFlds+1][MaxFlds+1], th0i[MaxFlds+1][7+1], n0i[MaxFlds+1][7+1];
double taup[MaxFlds+1][MaxTrmP+1], taupijk[MaxFlds+1][MaxTrmM+1];
double dPdDsave; //Calculated in the PressureGERG subroutine, but not included as an argument since it is only used internally in the density algorithm.
//std::string gas[NcGERG+1] = {"", "Methane", "Nitrogen", "Carbon_dioxide", "Ethane", "Propane", "iso-Butane", "n-Butane", "iso-Pentane", "n-Pentane", "n-Hexane",
// "n-Heptane", "n-Octane", "n-Nonane", "n-Decane", "Hydrogen", "Oxygen", "Carbon_monoxide", "Water", "Hydrogen_sulfide", "Helium", "Argon"};
std::vector<std::string> gas = {"", "74-82-8", "7727-37-9", "124-38-9", "74-84-0", "74-98-6",
"75-28-5", "106-97-8", "78-78-4", "109-66-0", "110-54-3",
"142-82-5", "111-65-9", "111-84-2", "124-18-5", "1333-74-0",
"7782-44-7", "630-08-0", "7732-18-5", "7783-06-4", "7440-59-7", "7440-37-1"};
double x[NcGERG+1];
double Mm; //Mm - Molar mass (g/mol)
int ArrayZero=1;
double Tr, Dr;
double Tcx, Dcx;
inline double Tanh(double xx){ return (exp(xx) - exp(-xx)) / (exp(xx) + exp(-xx)); }
inline double Sinh(double xx){ return (exp(xx) - exp(-xx)) / 2; }
inline double Cosh(double xx){ return (exp(xx) + exp(-xx)) / 2; }
public:
GERG2008(){SetupGERG();}
~GERG2008(){}
void setArrayZero(int az){if(az==1)ArrayZero=1;else ArrayZero=0;}
double GetMW(){return Mm;}
double GetMi(int i){if(i>=0 && i<=NcGERG) return MMiGERG[i+ArrayZero]; return 0.0;}
double GetXi(int i){if(i>=0 && i<=NcGERG) return x[i+ArrayZero]; return 0.0;}
std::string GetName(int i){if(i>=0 && i<=NcGERG) return gas[i+ArrayZero];return "";}
double MolarMass(std::map<std::string, double>& mix, bool normalize = true)
{
// Sub MolarMassGERG(x, Mm)
// Calculate molar mass of the mixture with the compositions contained in the x() input array
// Inputs:
// x() - Composition (mole fraction)
// Do not send mole percents or mass fractions in the x() array, otherwise the output will be incorrect.
// The sum of the compositions in the x() array must be equal to one.
// The order of the fluids in this array is given at the top of this module.
// Outputs:
// Mm - Molar mass (g/mol)
x[0] = 0.0;
for(std::size_t i = 1; i <= NcGERG; ++i) x[i] = 0.0;
for(std::size_t i = 1; i <= NcGERG; ++i)
x[i] = mix[gas[i]]; //x is ONE based, if xi is 0 based delete one
double xiTot = 0.0;
for(std::size_t i = 1; i <= NcGERG; ++i) xiTot += x[i];
if (normalize && abs(xiTot)>=epsD) for(std::size_t i = 1; i <= NcGERG; ++i) x[i] /= xiTot;
Mm = 0;
for(std::size_t i = 1; i <= NcGERG; ++i) Mm += x[i]*MMiGERG[i];
ReducingParametersGERG();
void PseudoCriticalPointGERG();
return Mm;
}
void PressureGERG(const double T, const double D, double &P, double &Z)
{
// Sub PressureGERG(T, D, x, P, Z)
// Calculate pressure as a function of temperature and density. The derivative d(P)/d(D) is also calculated
// for use in the iterative DensityGERG subroutine (and is only returned as a common variable).
// Inputs:
// T - Temperature (K)
// D - Density (mol/l)
// x() - Composition (mole fraction)
// Do not send mole percents or mass fractions in the x() array, otherwise the output will be incorrect.
// The sum of the compositions in the x() array must be equal to one.
// Outputs:
// P - Pressure (kPa)
// Z - Compressibility factor
// dPdDsave - d(P)/d(D) [kPa/(mol/l)] (at constant temperature)
// - This variable is cached in the common variables for use in the iterative density solver, but not returned as an argument.
double ar[4][4];
AlpharGERG(0, 0, T, D, ar);
Z = 1 + ar[0][1];
P = D * RGERG * T * Z;
dPdDsave = RGERG * T * (1 + 2 * ar[0][1] + ar[0][2]);
}
void Density(const int iFlag, const double T, const double P, double &D, int &ierr, std::string &herr)
{
// Sub DensityGERG(iFlag, T, P, x, D, ierr, herr)
// Calculate density as a function of temperature and pressure. This is an iterative routine that calls PressureGERG
// to find the correct state point. Generally only 6 iterations at most are required.
// If the iteration fails to converge, the ideal gas density and an error message are returned.
// No checks are made to determine the phase boundary, which would have guaranteed that the output is in the gas phase (or liquid phase when iFlag=2).
// It is up to the user to locate the phase boundary, and thus identify the phase of the T and P inputs.
// If the state point is 2-phase, the output density will represent a metastable state.
// Inputs:
// iFlag - Set to 0 for strict pressure solver in the gas phase without checks (fastest mode, but output state may not be stable single phase)
// Set to 1 to make checks for possible 2-phase states (result may still not be stable single phase, but many unstable states will be identified)
// Set to 2 to search for liquid phase (and make the same checks when iFlag=1)
// T - Temperature (K)
// P - Pressure (kPa)
// x() - Composition (mole fraction)
// (An initial guess for the density can be sent in D as the negative of the guess for roots that are in the liquid phase instead of using iFlag=2)
// Outputs:
// D - Density (mol/l)
// For the liquid phase, an initial value can be sent to the routine to avoid
// a solution in the metastable or gas phases.
// The initial value should be sent as a negative number.
// ierr - Error number (0 indicates no error)
// herr - Error message if ierr is not equal to zero
int nFail, iFail;
double plog, vlog, P2, Z, dpdlv, vdiff, tolr, vinc;
double dPdD, d2PdD2, d2PdTD, dPdT, U, H, S, A;
double Cv, Cp, W, G, JT, Kappa, PP;
ierr = 0;
herr = "";
nFail = 0;
iFail = 0;
if (P < epsD) { D = 0; return; }
tolr = 0.0000001;
if (D > -epsD){
D = P / RGERG / T; // Ideal gas estimate for vapor phase
if (iFlag == 2){ D = Dcx*3; } // Initial estimate for liquid phase
}
else{
D = std::abs(D); // If D<0, then use as initial estimate
}
plog = log(P);
vlog = -log(D);
for (int it = 1; it <= 50; ++it){
if (vlog < -7 || vlog > 100 || it == 20 || it == 30 || it == 40 || iFail == 1){
//Current state is bad or iteration is taking too long. Restart with completely different initial state
iFail = 0;
if (nFail > 2) {
// Iteration failed (above loop did not find a solution or checks made below indicate possible 2-phase state)
ierr = 1;
herr = "Calculation failed to converge in GERG method, ideal gas density returned.";
D = P / RGERG / T;
}
nFail++;
if (nFail == 1){
D = Dcx * 3; // If vapor phase search fails, look for root in liquid region
}
else if (nFail == 2) {
D = Dcx * 2.5; // If liquid phase search fails, look for root between liquid and critical regions
}
else if (nFail == 3) {
D = Dcx * 2; // If search fails, look for root in critical region
}
vlog = -log(D);
}
D = exp(-vlog);
PressureGERG(T, D, P2, Z);
if (dPdDsave < epsD || P2 < epsD){
// Current state is 2-phase, try locating a different state that is single phase
vinc = 0.1;
if (D > Dcx) { vinc = -0.1; }
if (it > 5) { vinc = vinc / 2; }
if (it > 10 && it < 20) { vinc = vinc / 5; }
vlog += vinc;
}
else{
// Find the next density with a first order Newton's type iterative scheme, with
// log(P) as the known variable and log(v) as the unknown property.
// See AGA 8 publication for further information.
dpdlv = -D * dPdDsave; // d(p)/d[log(v)]
vdiff = (log(P2) - plog) * P2 / dpdlv;
vlog += - vdiff;
if (std::abs(vdiff) < tolr) {
// Check to see if state is possibly 2-phase, and if so restart
if (dPdDsave < 0){
iFail = 1;
}
else{
D = exp(-vlog);
// If requested, check to see if point is possibly 2-phase
if (iFlag > 0){
Properties(T, D, PP, Z, dPdD, d2PdD2, d2PdTD, dPdT, U, H, S, Cv, Cp, W, G, JT, Kappa, A);
if ((PP <= 0 || dPdD <= 0 || d2PdTD <= 0) || (Cv <= 0 || Cp <= 0 || W <= 0)) {
// Iteration failed (above loop did find a solution or checks made below indicate possible 2-phase state)
ierr = 1;
herr = "Calculation failed to converge in GERG method, ideal gas density returned.";
D = P / RGERG / T;
}
return;
}
return; // Iteration converged
}
}
}
}
// Iteration failed (above loop did not find a solution or checks made below indicate possible 2-phase state)
ierr = 1;
herr = "Calculation failed to converge in GERG method, ideal gas density returned.";
D = P / RGERG / T;
}
void PropertiesIdeal(const double T, const double D, double &a0,double &Da0Dt,double &Da0Dtt){
// Inputs:
// T - Temperature (K)
// D - Density (mol/l)
// x() - Composition (mole fraction)
// Outputs:
// a0(0) - Ideal gas Helmholtz energy (all dimensionless [i.e., divided by RT])
// a0(1) - tau*partial(a0)/partial(tau)
// a0(2) - tau^2*partial^2(a0)/partial(tau)^2
double a0_[2+1];
// Calculate the ideal gas Helmholtz energy, and its first and second derivatives with respect to temperature.
Alpha0GERG(T, D, a0_);
a0 = a0_[0];
Da0Dt = a0_[1];
Da0Dtt = a0_[2];
}
void Properties(const double T, const double D, double &P, double &Z, double &dPdD, double &d2PdD2, double &d2PdTD, double &dPdT, double &U, double &H, double &S, double &Cv, double &Cp, double &W, double &G, double &JT, double &Kappa, double &A)
{
// Sub PropertiesGERG(T, D, x, P, Z, dPdD, d2PdD2, d2PdTD, dPdT, U, H, S, Cv, Cp, W, G, JT, Kappa, A)
// Calculate thermodynamic properties as a function of temperature and density. Calls are made to the subroutines
// ReducingParametersGERG, IdealGERG, and ResidualGERG. If the density is not known, call subroutine DENSITY first
// with the known values of pressure and temperature.
// Inputs:
// T - Temperature (K)
// D - Density (mol/l)
// x() - Composition (mole fraction)
// Outputs:
// P - Pressure (kPa)
// Z - Compressibility factor
// dPdD - First derivative of pressure with respect to density at constant temperature [kPa/(mol/l)]
// d2PdD2 - Second derivative of pressure with respect to density at constant temperature [kPa/(mol/l)^2]
// d2PdTD - Second derivative of pressure with respect to temperature and density [kPa/(mol/l)/K]
// dPdT - First derivative of pressure with respect to temperature at constant density (kPa/K)
// U - Internal energy (J/mol)
// H - Enthalpy (J/mol)
// S - Entropy [J/(mol-K)]
// Cv - Isochoric heat capacity [J/(mol-K)]
// Cp - Isobaric heat capacity [J/(mol-K)]
// W - Speed of sound (m/s)
// G - Gibbs energy (J/mol)
// JT - Joule-Thomson coefficient (K/kPa)
// Kappa - Isentropic Exponent
// A - Helmholtz energy (J/mol)
double a0[2+1], ar[3+1][3+1], R, RT;
// Calculate the ideal gas Helmholtz energy, and its first and second derivatives with respect to temperature.
Alpha0GERG(T, D, a0);
// Calculate the real gas Helmholtz energy, and its derivatives with respect to temperature and/or density.
AlpharGERG(1, 0, T, D, ar);
R = RGERG;
RT = R * T;
Z = 1 + ar[0][1];
P = D * RT * Z;
dPdD = RT * (1 + 2 * ar[0][1] + ar[0][2]);
dPdT = D * R * (1 + ar[0][1] - ar[1][1]);
d2PdTD = R * (1 + 2 * ar[0][1] + ar[0][2] - 2 * ar[1][1] - ar[1][2]);
A = RT * (a0[0] + ar[0][0]);
G = RT * (1 + ar[0][1] + a0[0] + ar[0][0]);
U = RT * (a0[1] + ar[1][0]);
H = RT * (1 + ar[0][1] + a0[1] + ar[1][0]);
S = R * (a0[1] + ar[1][0] - a0[0] - ar[0][0]);
Cv = -R * (a0[2] + ar[2][0]);
if (D > epsD){
Cp = Cv + T * (dPdT / D) * (dPdT / D) / dPdD;
d2PdD2 = RT * (2 * ar[0][1] + 4 * ar[0][2] + ar[0][3]) / D;
JT = (T / D * dPdT / dPdD - 1) / Cp / D; // '=(dB/dT*T-B)/Cp for an ideal gas, but dB/dT is not known
}
else{
Cp = Cv + R;
d2PdD2 = 0;
JT = 1E+20;
}
W = 1000 * Cp / Cv * dPdD / Mm;
if (W < 0) { W = 0; }
W = sqrt(W);
Kappa = pow(W, 2) * Mm / (RT * 1000 * Z);
}
private:
// The following routines are low-level routines that should not be called outside of this code.
void ReducingParametersGERG()
{
// Private Sub ReducingParametersGERG(x, Tr, Dr)
// Calculate reducing variables. Only need to call this if the composition has changed.
// Inputs:
// x() - Composition (mole fraction)
// Outputs:
// Tr - Reducing temperature (K)
// Dr - Reducing density (mol/l)
double Vr, xij, F;
int icheck;
// Check to see if a component fraction has changed. If x is the same as the previous call, then exit.
icheck = 0;
for (int i = 1; i <= NcGERG; ++i){
if (std::abs(x[i] - xold[i]) > 0.0000001){ icheck = 1; }
xold[i] = x[i];
}
if (icheck == 0){
Dr = Drold;
Tr = Trold;
return;
}
Told = 0;
Trold2 = 0;
// Calculate reducing variables for T and D
Dr = 0;
Vr = 0;
Tr = 0;
for (int i = 1; i <= NcGERG; ++i){
if (x[i] > epsD){
F = 1;
for (int j = i; j <= NcGERG; ++j){
if (x[j] > epsD){
xij = F * (x[i] * x[j]) * (x[i] + x[j]);
Vr = Vr + xij * gvij[i][j] / (bvij[i][j] * x[i] + x[j]);
Tr = Tr + xij * gtij[i][j] / (btij[i][j] * x[i] + x[j]);
F = 2;
}
}
}
}
if (Vr > epsD){ Dr = 1 / Vr; }
Drold = Dr;
Trold = Tr;
}
void Alpha0GERG(const double T, const double D, double a0[3])
{
// Private Sub Alpha0GERG(T, D, x, a0)
// Calculate the ideal gas Helmholtz energy and its derivatives with respect to tau and delta.
// This routine is not needed when only P (or Z) is calculated.
// Inputs:
// T - Temperature (K)
// D - Density (mol/l)
// x() - Composition (mole fraction)
// Outputs:
// a0(0) - Ideal gas Helmholtz energy (all dimensionless [i.e., divided by RT])
// a0(1) - tau*partial(a0)/partial(tau)
// a0(2) - tau^2*partial^2(a0)/partial(tau)^2
double LogT, LogD, LogHyp, th0T, LogxD;
double SumHyp0, SumHyp1, SumHyp2;
double em, ep, hcn, hsn;
a0[0] = 0; a0[1] = 0; a0[2] = 0;
if (D > epsD) {LogD = log(D);} else {LogD = log(epsD);}
LogT = log(T);
for (int i = 1; i <= NcGERG; ++i){
if (x[i] > epsD){
LogxD = LogD + log(x[i]);
SumHyp0 = 0;
SumHyp1 = 0;
SumHyp2 = 0;
for (int j = 4; j <= 7; ++j){
if (th0i[i][j] > epsD){
th0T = th0i[i][j] / T;
ep = exp(th0T);
em = 1 / ep;
hsn = (ep - em) / 2;
hcn = (ep + em) / 2;
if (j == 4 || j == 6){
LogHyp = log(std::abs(hsn));
SumHyp0 = SumHyp0 + n0i[i][j] * LogHyp;
SumHyp1 = SumHyp1 + n0i[i][j] * th0T * hcn / hsn;
SumHyp2 = SumHyp2 + n0i[i][j] * (th0T / hsn)* (th0T / hsn);
}
else{
LogHyp = log(std::abs(hcn));
SumHyp0 = SumHyp0 - n0i[i][j] * LogHyp;
SumHyp1 = SumHyp1 - n0i[i][j] * th0T * hsn / hcn;
SumHyp2 = SumHyp2 + n0i[i][j] * (th0T / hcn) * (th0T / hcn);
}
}
}
a0[0] += +x[i] * (LogxD + n0i[i][1] + n0i[i][2] / T - n0i[i][3] * LogT + SumHyp0);
a0[1] += +x[i] * (n0i[i][3] + n0i[i][2] / T + SumHyp1);
a0[2] += -x[i] * (n0i[i][3] + SumHyp2);
}
}
}
void AlpharGERG(const int itau, const int idelta, const double T, const double D, double ar[4][4])
{
// Private Sub AlpharGERG(itau, idelta, T, D, x, ar)
// Calculate dimensionless residual Helmholtz energy and its derivatives with respect to tau and delta.
// Inputs:
// itau - Set this to 1 to calculate "ar" derivatives with respect to tau [i.e., ar(1,0), ar(1,1), and ar(2,0)], otherwise set it to 0.
// idelta - Currently not used, but kept as an input for future use in specifing the highest density derivative needed.
// T - Temperature (K)
// D - Density (mol/l)
// x() - Composition (mole fraction)
// Outputs:
// ar(0,0) - Residual Helmholtz energy (dimensionless, =a/RT)
// ar(0,1) - delta*partial (ar)/partial(delta)
// ar(0,2) - delta^2*partial^2(ar)/partial(delta)^2
// ar(0,3) - delta^3*partial^3(ar)/partial(delta)^3
// ar(1,0) - tau*partial (ar)/partial(tau)
// ar(1,1) - tau*delta*partial^2(ar)/partial(tau)/partial(delta)
// ar(2,0) - tau^2*partial^2(ar)/partial(tau)^2
int mn;
double del, tau;
double lntau, ex, ex2, ex3, cij0, eij0;
double delp[7+1], Expd[7+1], ndt, ndtd, ndtt, xijf;
for (int i = 0; i <= 3; ++i){ for (int j = 0; j <= 3; ++j){ ar[i][j] = 0; } }
//Set up del, tau, log(tau), and the first 7 calculations for del^i
del = D / Dr;
tau = Tr / T;
lntau = log(tau);
delp[1] = del;
Expd[1] = exp(-delp[1]);
for (int i = 2; i <= 7; ++i){
delp[i] = delp[i - 1] * del;
Expd[i] = exp(-delp[i]);
}
// If temperature has changed, calculate temperature dependent parts
if (std::abs(T - Told) > 0.0000001 || std::abs(Tr - Trold2) > 0.0000001) {
tTermsGERG(lntau);
}
Told = T;
Trold2 = Tr;
// Calculate pure fluid contributions
for (int i = 1; i <= NcGERG; ++i){
if (x[i] > epsD){
for (int k = 1; k <= kpol[i]; ++k){
ndt = x[i] * delp[doik[i][k]] * taup[i][k];
ndtd = ndt * doik[i][k];
ar[0][1] += ndtd;
ar[0][2] += ndtd * (doik[i][k] - 1);
if (itau > 0){
ndtt = ndt * toik[i][k];
ar[0][0] += ndt;
ar[1][0] += ndtt;
ar[2][0] += ndtt * (toik[i][k] - 1);
ar[1][1] += ndtt * doik[i][k];
ar[1][2] += ndtt * doik[i][k] * (doik[i][k] - 1);
ar[0][3] += ndtd * (doik[i][k] - 1) * (doik[i][k] - 2);
}
}
for (int k = 1 + kpol[i]; k <= kpol[i] + kexp[i]; ++k){
ndt = x[i] * delp[doik[i][k]] * taup[i][k]*Expd[coik[i][k]];
ex = coik[i][k] * delp[coik[i][k]];
ex2 = doik[i][k] - ex;
ex3 = ex2 * (ex2 - 1);
ar[0][1] += ndt * ex2;
ar[0][2] += ndt * (ex3 - coik[i][k] * ex);
if (itau > 0){
ndtt = ndt * toik[i][k];
ar[0][0] += ndt;
ar[1][0] += ndtt;
ar[2][0] += ndtt * (toik[i][k] - 1);
ar[1][1] += ndtt * ex2;
ar[1][2] += ndtt * (ex3 - coik[i][k] * ex);
ar[0][3] += ndt * (ex3 * (ex2 - 2) - ex * (3 * ex2 - 3 + coik[i][k]) * coik[i][k]);
}
}
}
}
// Calculate mixture contributions
for (int i = 1; i <= NcGERG - 1; ++i){
if (x[i] > epsD){
for (int j = i + 1; j <= NcGERG; ++j){
if (x[j] > epsD){
mn = mNumb[i][j];
if (mn >= 0){
xijf = x[i] * x[j] * fij[i][j];
for (int k = 1; k <= kpolij[mn]; ++k){
ndt = xijf * delp[dijk[mn][k]] * taupijk[mn][k];
ndtd = ndt * dijk[mn][k];
ar[0][1] += ndtd;
ar[0][2] += ndtd * (dijk[mn][k] - 1);
if (itau > 0){
ndtt = ndt * tijk[mn][k];
ar[0][0] += ndt;
ar[1][0] += ndtt;
ar[2][0] += ndtt * (tijk[mn][k] - 1);
ar[1][1] += ndtt * dijk[mn][k];
ar[1][2] += ndtt * dijk[mn][k] * (dijk[mn][k] - 1);
ar[0][3] += ndtd * (dijk[mn][k] - 1) * (dijk[mn][k] - 2);
}
}
for (int k = 1 + kpolij[mn]; k <= kpolij[mn] + kexpij[mn]; ++k){
cij0 = cijk[mn][k] * delp[2];
eij0 = eijk[mn][k] * del;
ndt = xijf * nijk[mn][k] * delp[dijk[mn][k]] * exp(cij0 + eij0 + gijk[mn][k] + tijk[mn][k] * lntau);
ex = dijk[mn][k] + 2 * cij0 + eij0;
ex2 = (ex * ex - dijk[mn][k] + 2 * cij0);
ar[0][1] += ndt * ex;
ar[0][2] += ndt * ex2;
if(itau > 0){
ndtt = ndt * tijk[mn][k];
ar[0][0] += ndt;
ar[1][0] += ndtt;
ar[2][0] += ndtt * (tijk[mn][k] - 1);
ar[1][1] += ndtt * ex;
ar[1][2] += ndtt * ex2;
ar[0][3] += ndt * (ex * (ex2 - 2 * (dijk[mn][k] - 2 * cij0)) + 2 * dijk[mn][k]);
}
}
}
}
}
}
}
}
void tTermsGERG(const double lntau)
{
// Private Sub tTermsGERG(lntau, x)
// Calculate temperature dependent parts of the GERG-2008 equation of state
int i, mn;
double taup0[12+1];
i = 5; // Use propane to get exponents for short form of EOS
for (int k = 1; k <= kpol[i] + kexp[i]; ++k){
taup0[k] = exp(toik[i][k] * lntau);
}
for (int i = 1; i <= NcGERG; ++i){
if (x[i] > epsD){
if (i > 4 && i != 15 && i != 18 && i != 20 ) {
for (int k = 1; k <= kpol[i] + kexp[i]; ++k){
taup[i][k] = noik[i][k] * taup0[k];
}
}
else{
for (int k = 1; k <= kpol[i] + kexp[i]; ++k){
taup[i][k] = noik[i][k] * exp(toik[i][k] * lntau);
}
}
}
}
for (int i = 1; i <= NcGERG - 1; ++i) {
if (x[i] > epsD){
for (int j = i + 1; j <= NcGERG; ++j) {
if (x[j] > epsD) {
mn = mNumb[i][j];
if (mn >= 0) {
for (int k = 1; k <= kpolij[mn]; ++k) {
taupijk[mn][k] = nijk[mn][k] * exp(tijk[mn][k] * lntau);
}
}
}
}
}
}
}
void PseudoCriticalPointGERG()
{
// PseudoCriticalPointGERG(x, Tcx, Dcx)
// Calculate a pseudo critical point as the mole fraction average of the critical temperatures and critical volumes
double Vcx;
Tcx = 0;
Vcx = 0;
Dcx = 0;
for (int i = 1; i <= NcGERG; ++i){
Tcx = Tcx + x[i] * Tc[i];
Vcx = Vcx + x[i] / Dc[i];
}
if (Vcx > epsD){ Dcx = 1 / Vcx; }
}
// The following routine must be called once before any other routine.
void SetupGERG()
{
// Initialize all the constants and parameters in the GERG-2008 model.
// Some values are modified for calculations that do not depend on T, D, and x in order to speed up the program.
double o13, bijk[MaxMdl+1][MaxTrmM+1], Rs, Rsr;
double T0, d0;
RGERG = 8.314472;
Rs = 8.31451;
Rsr = Rs / RGERG;
o13 = 1.0 / 3.0;
for (int i = 1; i <= MaxFlds; ++i){
xold[i] = 0;
}
Told = 0;
// Molar masses [g/mol]
MMiGERG[1] = 16.04246; // Methane
MMiGERG[2] = 28.0134; // Nitrogen
MMiGERG[3] = 44.0095; // Carbon dioxide
MMiGERG[4] = 30.06904; // Ethane
MMiGERG[5] = 44.09562; // Propane
MMiGERG[6] = 58.1222; // Isobutane
MMiGERG[7] = 58.1222; // n-Butane
MMiGERG[8] = 72.14878; // Isopentane
MMiGERG[9] = 72.14878; // n-Pentane
MMiGERG[10] = 86.17536; // Hexane
MMiGERG[11] = 100.20194; // Heptane
MMiGERG[12] = 114.22852; // Octane
MMiGERG[13] = 128.2551; // Nonane
MMiGERG[14] = 142.28168; // Decane
MMiGERG[15] = 2.01588; // Hydrogen
MMiGERG[16] = 31.9988; // Oxygen
MMiGERG[17] = 28.0101; // Carbon monoxide
MMiGERG[18] = 18.01528; // Water
MMiGERG[19] = 34.08088; // Hydrogen sulfide
MMiGERG[20] = 4.002602; // Helium
MMiGERG[21] = 39.948; // Argon
// Number of polynomial and exponential terms
for(int i = 1; i <= MaxFlds; ++i){
kpol[i] = 6;
kexp[i] = 6;
}
kexp[1] = 18;
kexp[2] = 18;
kexp[4] = 18;
kpol[3] = 4; kexp[3] = 18;
kpol[15] = 5; kexp[15] = 9;
kpol[18] = 7; kexp[18] = 9;
kpol[20] = 4; kexp[20] = 8;
kpolij[1] = 2; kexpij[1] = 10;
kpolij[2] = 5; kexpij[2] = 4;
kpolij[3] = 2; kexpij[3] = 7;
kpolij[4] = 3; kexpij[4] = 3;
kpolij[5] = 2; kexpij[5] = 4;
kpolij[6] = 3; kexpij[6] = 3;
kpolij[7] = 4; kexpij[7] = 0;
kpolij[10] = 10; kexpij[10] = 0;
// Critical densities [mol/l]
Dc[1] = 10.139342719;
Dc[2] = 11.1839;
Dc[3] = 10.624978698;
Dc[4] = 6.87085454;
Dc[5] = 5.000043088;
Dc[6] = 3.86014294;
Dc[7] = 3.920016792;
Dc[8] = 3.271;
Dc[9] = 3.215577588;
Dc[10] = 2.705877875;
Dc[11] = 2.315324434;
Dc[12] = 2.056404127;
Dc[13] = 1.81;
Dc[14] = 1.64;
Dc[15] = 14.94;
Dc[16] = 13.63;
Dc[17] = 10.85;
Dc[18] = 17.87371609;
Dc[19] = 10.19;
Dc[20] = 17.399;
Dc[21] = 13.407429659;
// Critical temperatures [K]
Tc[1] = 190.564;
Tc[2] = 126.192;
Tc[3] = 304.1282;
Tc[4] = 305.322;
Tc[5] = 369.825;
Tc[6] = 407.817;
Tc[7] = 425.125;
Tc[8] = 460.35;
Tc[9] = 469.7;
Tc[10] = 507.82;
Tc[11] = 540.13;
Tc[12] = 569.32;
Tc[13] = 594.55;
Tc[14] = 617.7;
Tc[15] = 33.19;
Tc[16] = 154.595;
Tc[17] = 132.86;
Tc[18] = 647.096;
Tc[19] = 373.1;
Tc[20] = 5.1953;
Tc[21] = 150.687;
// Exponents in pure fluid equations
for(int i = 1; i <= MaxFlds; ++i){
Vc3[i] = 1 / pow(Dc[i], o13) / 2;
Tc2[i] = sqrt(Tc[i]);
coik[i][1] = 0; doik[i][1] = 1; toik[i][1] = 0.25;
coik[i][2] = 0; doik[i][2] = 1; toik[i][2] = 1.125;
coik[i][3] = 0; doik[i][3] = 1; toik[i][3] = 1.5;
coik[i][4] = 0; doik[i][4] = 2; toik[i][4] = 1.375;
coik[i][5] = 0; doik[i][5] = 3; toik[i][5] = 0.25;
coik[i][6] = 0; doik[i][6] = 7; toik[i][6] = 0.875;
coik[i][7] = 1; doik[i][7] = 2; toik[i][7] = 0.625;
coik[i][8] = 1; doik[i][8] = 5; toik[i][8] = 1.75;
coik[i][9] = 2; doik[i][9] = 1; toik[i][9] = 3.625;
coik[i][10] = 2; doik[i][10] = 4; toik[i][10] = 3.625;
coik[i][11] = 3; doik[i][11] = 3; toik[i][11] = 14.5;
coik[i][12] = 3; doik[i][12] = 4; toik[i][12] = 12;
}
for (int i = 1; i <= 4; ++i){
if (i != 3){
coik[i][1] = 0; doik[i][1] = 1; toik[i][1] = 0.125;
coik[i][2] = 0; doik[i][2] = 1; toik[i][2] = 1.125;
coik[i][3] = 0; doik[i][3] = 2; toik[i][3] = 0.375;
coik[i][4] = 0; doik[i][4] = 2; toik[i][4] = 1.125;
coik[i][5] = 0; doik[i][5] = 4; toik[i][5] = 0.625;
coik[i][6] = 0; doik[i][6] = 4; toik[i][6] = 1.5;
coik[i][7] = 1; doik[i][7] = 1; toik[i][7] = 0.625;
coik[i][8] = 1; doik[i][8] = 1; toik[i][8] = 2.625;
coik[i][9] = 1; doik[i][9] = 1; toik[i][9] = 2.75;
coik[i][10] = 1; doik[i][10] = 2; toik[i][10] = 2.125;
coik[i][11] = 1; doik[i][11] = 3; toik[i][11] = 2;
coik[i][12] = 1; doik[i][12] = 6; toik[i][12] = 1.75;
coik[i][13] = 2; doik[i][13] = 2; toik[i][13] = 4.5;
coik[i][14] = 2; doik[i][14] = 3; toik[i][14] = 4.75;
coik[i][15] = 2; doik[i][15] = 3; toik[i][15] = 5;
coik[i][16] = 2; doik[i][16] = 4; toik[i][16] = 4;
coik[i][17] = 2; doik[i][17] = 4; toik[i][17] = 4.5;
coik[i][18] = 3; doik[i][18] = 2; toik[i][18] = 7.5;
coik[i][19] = 3; doik[i][19] = 3; toik[i][19] = 14;
coik[i][20] = 3; doik[i][20] = 4; toik[i][20] = 11.5;
coik[i][21] = 6; doik[i][21] = 5; toik[i][21] = 26;
coik[i][22] = 6; doik[i][22] = 6; toik[i][22] = 28;
coik[i][23] = 6; doik[i][23] = 6; toik[i][23] = 30;
coik[i][24] = 6; doik[i][24] = 7; toik[i][24] = 16;
}
}
// Coefficients of pure fluid equations
// Methane
noik[1][1] = 0.57335704239162;
noik[1][2] = -1.676068752373;
noik[1][3] = 0.23405291834916;
noik[1][4] = -0.21947376343441;
noik[1][5] = 0.016369201404128;
noik[1][6] = 0.01500440638928;
noik[1][7] = 0.098990489492918;
noik[1][8] = 0.58382770929055;
noik[1][9] = -0.7478686756039;
noik[1][10] = 0.30033302857974;
noik[1][11] = 0.20985543806568;
noik[1][12] = -0.018590151133061;
noik[1][13] = -0.15782558339049;
noik[1][14] = 0.12716735220791;
noik[1][15] = -0.032019743894346;
noik[1][16] = -0.068049729364536;
noik[1][17] = 0.024291412853736;
noik[1][18] = 5.1440451639444E-03;
noik[1][19] = -0.019084949733532;
noik[1][20] = 5.5229677241291E-03;
noik[1][21] = -4.4197392976085E-03;
noik[1][22] = 0.040061416708429;
noik[1][23] = -0.033752085907575;
noik[1][24] = -2.5127658213357E-03;
// Nitrogen
noik[2][1] = 0.59889711801201;
noik[2][2] = -1.6941557480731;
noik[2][3] = 0.24579736191718;
noik[2][4] = -0.23722456755175;
noik[2][5] = 0.017954918715141;
noik[2][6] = 0.014592875720215;
noik[2][7] = 0.10008065936206;
noik[2][8] = 0.73157115385532;
noik[2][9] = -0.88372272336366;
noik[2][10] = 0.31887660246708;
noik[2][11] = 0.20766491728799;
noik[2][12] = -0.019379315454158;
noik[2][13] = -0.16936641554983;
noik[2][14] = 0.13546846041701;
noik[2][15] = -0.033066712095307;
noik[2][16] = -0.060690817018557;
noik[2][17] = 0.012797548292871;
noik[2][18] = 5.8743664107299E-03;
noik[2][19] = -0.018451951971969;
noik[2][20] = 4.7226622042472E-03;
noik[2][21] = -5.2024079680599E-03;
noik[2][22] = 0.043563505956635;
noik[2][23] = -0.036251690750939;
noik[2][24] = -2.8974026866543E-03;
// Ethane
noik[4][1] = 0.63596780450714;
noik[4][2] = -1.7377981785459;
noik[4][3] = 0.28914060926272;
noik[4][4] = -0.33714276845694;
noik[4][5] = 0.022405964699561;
noik[4][6] = 0.015715424886913;
noik[4][7] = 0.11450634253745;
noik[4][8] = 1.0612049379745;
noik[4][9] = -1.2855224439423;
noik[4][10] = 0.39414630777652;
noik[4][11] = 0.31390924682041;
noik[4][12] = -0.021592277117247;
noik[4][13] = -0.21723666564905;
noik[4][14] = -0.28999574439489;
noik[4][15] = 0.42321173025732;
noik[4][16] = 0.04643410025926;
noik[4][17] = -0.13138398329741;
noik[4][18] = 0.011492850364368;
noik[4][19] = -0.033387688429909;
noik[4][20] = 0.015183171583644;
noik[4][21] = -4.7610805647657E-03;
noik[4][22] = 0.046917166277885;
noik[4][23] = -0.039401755804649;
noik[4][24] = -3.2569956247611E-03;
// Propane
noik[5][1] = 1.0403973107358;
noik[5][2] = -2.8318404081403;
noik[5][3] = 0.84393809606294;
noik[5][4] = -0.076559591850023;
noik[5][5] = 0.09469737305728;
noik[5][6] = 2.4796475497006E-04;
noik[5][7] = 0.2774376042287;
noik[5][8] = -0.043846000648377;
noik[5][9] = -0.2699106478435;
noik[5][10] = -0.06931341308986;
noik[5][11] = -0.029632145981653;
noik[5][12] = 0.01404012675138;
// Isobutane
noik[6][1] = 1.04293315891;
noik[6][2] = -2.8184272548892;
noik[6][3] = 0.8617623239785;
noik[6][4] = -0.10613619452487;
noik[6][5] = 0.098615749302134;
noik[6][6] = 2.3948208682322E-04;
noik[6][7] = 0.3033000485695;
noik[6][8] = -0.041598156135099;
noik[6][9] = -0.29991937470058;
noik[6][10] = -0.080369342764109;
noik[6][11] = -0.029761373251151;
noik[6][12] = 0.01305963030314;
// n-Butane
noik[7][1] = 1.0626277411455;
noik[7][2] = -2.862095182835;
noik[7][3] = 0.88738233403777;
noik[7][4] = -0.12570581155345;
noik[7][5] = 0.10286308708106;
noik[7][6] = 2.5358040602654E-04;
noik[7][7] = 0.32325200233982;
noik[7][8] = -0.037950761057432;
noik[7][9] = -0.32534802014452;
noik[7][10] = -0.079050969051011;
noik[7][11] = -0.020636720547775;
noik[7][12] = 0.005705380933475;
// Isopentane
noik[8][1] = 1.0963;
noik[8][2] = -3.0402;
noik[8][3] = 1.0317;
noik[8][4] = -0.1541;
noik[8][5] = 0.11535;
noik[8][6] = 0.00029809;
noik[8][7] = 0.39571;
noik[8][8] = -0.045881;
noik[8][9] = -0.35804;
noik[8][10] = -0.10107;
noik[8][11] = -0.035484;
noik[8][12] = 0.018156;
// n-Pentane
noik[9][1] = 1.0968643098001;
noik[9][2] = -2.9988888298061;
noik[9][3] = 0.99516886799212;
noik[9][4] = -0.16170708558539;
noik[9][5] = 0.11334460072775;
noik[9][6] = 2.6760595150748E-04;
noik[9][7] = 0.40979881986931;
noik[9][8] = -0.040876423083075;
noik[9][9] = -0.38169482469447;
noik[9][10] = -0.10931956843993;
noik[9][11] = -0.03207322332799;
noik[9][12] = 0.016877016216975;