-
Notifications
You must be signed in to change notification settings - Fork 0
/
eosCode.ts
5330 lines (4259 loc) · 243 KB
/
eosCode.ts
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
import { getCubicRoot } from "./findCubicRoots.js";
import * as Math from "mathjs";
/* This code was developed in VSCode and can be found here : https://github.com/stevecalderone/PPR1978_Typescript */
enum idx{
mw = 0,
tc,
pc,
omega,
zc,
ki,
bi,
tb,
hvap,
hf298,
s298,
gf298,
cpData,
NIST_TMN1, NIST_TMX1, NIST_A1, NIST_B1, NIST_C1, NIST_D1, NIST_E1, NIST_F1, NIST_G1, NIST_H1,
NIST_TMN2, NIST_TMX2, NIST_A2, NIST_B2, NIST_C2, NIST_D2, NIST_E2, NIST_F2, NIST_G2, NIST_H2,
NIST_TMN3, NIST_TMX3, NIST_A3, NIST_B3, NIST_C3, NIST_D3, NIST_E3, NIST_F3, NIST_G3, NIST_H3,
NIST_TMN4, NIST_TMX4, NIST_A4, NIST_B4, NIST_C4, NIST_D4, NIST_E4, NIST_F4, NIST_G4, NIST_H4,
NIST_TMN5, NIST_TMX5, NIST_A5, NIST_B5, NIST_C5, NIST_D5, NIST_E5, NIST_F5, NIST_G5, NIST_H5,
NIST_TMN6, NIST_TMX6, NIST_A6, NIST_B6, NIST_C6, NIST_D6, NIST_E6, NIST_F6, NIST_G6, NIST_H6,
alphaType, //'<= This is a flag for processing create_alphaiArray normally or using Twu volume tranlation method
errMsgsOn, //'<= This is a flag indicating if 'error messages on' was found in the top left most cell of the dataSet.
liquidsFound, //'<=
liquidIndex, //'<= The base zero index of the the liquid first liquid species
iSpecies, //'<= Storage for the upper bound for the rows of the dataSet. This is equal to the number of species minus one.
globalErrmsg, //'<= This is to store private function error message to pass along to the public function.
localWarnings,
binariesUsed,
predictive,
finalIndex,
lastCpIndex = NIST_H6,
iMoles = 0, //'<= Used in the validateMoles function
iMoleFraction, //'<= Used in the validateMoles function
tempK = 0, //'<= These indexes are for the seleceCpDataRange, Enthalpy and Entropy functions. this stores NIST-MNT index for tempK
Vap298, //'<= These indexes are for the seleceCpDataRange, Enthalpy and Entropy functions. This stores NIST-MNT index for Vapor at 298K
NBPVap, //'<= These indexes are for the seleceCpDataRange, Enthalpy and Entropy functions. This stores NIST-MNT for index vapor at normal boiling point
NBPLiq, //'<= These indexes are for the seleceCpDataRange, Enthalpy and Entropy functions. This stores NIST-MNT for index liquid at normal boiling point
dadT_constV = 0,
dPdv_constT,
dPdT_constV,
dadT_constP,
dBdT_constP,
dZdT_constP,
dVdT_constP,
sumb,
suma,
a,
b,
Z,
vol,
T = 0,
P,
phase,
binariesOn,
errsOn,
validBinaries,
guessT,
valuesArray = 0, // these idexes are used in the validateData() function
datasetArray,
moleCompArray,
kij0Array,
kijTArray,
decompArray,
alphaArray,
aiArray
};
const gasLawR: number = 0.000083144621; // m3-bar/K-mol
const currentNumberOfGroups = 31; /*"Groups","CH3","CH2","CH","C","CH4","C2H6","CHaro","Caro","C, aro-fused rings","CH2, cyclic","CH/C, cyclic","CO2","N2","H2S","SH","H2O","C2H4","CH2/CH, alkenic","C, alkenic","CH/C, cycloalkenic","H2","CO","He","Ar","SO2","O2","NO","COS","NH3","NO2/N2O4","N2O"],*/
/*"Groups","(GROUP 1)","(GROUP 2)","(GROUP 3)","(GROUP 4)","(GROUP 5)","(GROUP 6)","(GROUP 7)","(GROUP 8)","(GROUP 9)","(GROUP 10)","(GROUP 11)","(GROUP 12)","(GROUP 13)","(GROUP 14)","(GROUP 15)","(GROUP 16)","(GROUP 17)","(GROUP 18)","(GROUP 19)","(GROUP 20)","(GROUP 21)","(GROUP 22)","(GROUP 23)","(GROUP 24)","(GROUP 25)","(GROUP 26)","(GROUP 27)","(GROUP 28)","(GROUP 29)","(GROUP 30)","(GROUP 31)"*/
/***********************************************************************************************/
function create_alphaiArray(dataSet, tempK: number): number[] {
const fcnName: string = "create_alphaiArray";
try {
/*'***************************************************************************
'The function is called by all PR1978 functions
'Calculates an array of values. If the dataSet range contains 'Twu alpha' in cell (1,1) then
'the Twu volume translation method is used otherwise it calculates normally
'***************************************************************************/
let outputArray: (number)[] = [];
let myErrorMsg: string = "";
if(dataSet[0][idx.alphaType] === false) { //'<= alphaType is a variable created in the validateDataset function. Set in the (0,0) index of the dataSet
for(let i: number = 0; i < dataSet[0][idx.iSpecies]; i++) {
if(dataSet[i][idx.tc] !== 0) {
outputArray.push(Math.pow(1 + dataSet[i][idx.ki] * (1 - Math.pow(tempK / dataSet[i][idx.tc], 0.5)), 2));
}else {
myErrorMsg = `The critical temperature of species ${i} is zero.`;
throw new Error(`${fcnName}: ${myErrorMsg}`);
}
}
}else {
let alpha0: number = 0;
let alpha1: number = 0;
let Tr: number = 0; //'<= Twu type alpha is being used - cell (1,1) of dataSet contains the value' Twu_alpha'
const LMN_Array = [
[0.272838, 0.625701, 0.373949, 0.0239035],
[0.924779, 0.792014, 4.7302, 1.24615],
[1.19764, 2.46022, -0.2, -8]
];
for(let i: number = 0; i < dataSet[0][idx.iSpecies]; i++) {
Tr = tempK / dataSet[i][idx.tc];
if(Tr <= 1) {
alpha0 = Math.pow(Tr, (1.19764 * (0.924779 - 1))) * Math.exp(0.272838 * (1 - Math.pow(Tr, (1.19764 * 0.924779))));
alpha1 = Math.pow(Tr, (2.46022 * (0.792014 - 1))) * Math.exp(0.625701 * (1 - Math.pow(Tr, (2.46022 * 0.792014))));
}else {
alpha0 = Math.pow(Tr, (1.19764 * (0.924779 - 1))) * Math.exp(0.272838 * (1 - Math.pow(Tr, (1.19764 * 0.924779))));
alpha1 = Math.pow(Tr, (2.46022 * (0.792014 - 1))) * Math.exp(0.625701 * (1 - Math.pow(Tr, (2.46022 * 0.792014))));
}
outputArray.push(alpha0 + dataSet[i][idx.omega]*(alpha1 - alpha0));
}
}
dataSet[0][idx.globalErrmsg] = myErrorMsg; //Used to transfer warning messages to calling function
return outputArray;
}
catch(myErrorHandler) {
dataSet[0][idx.globalErrmsg] = `${fcnName}: ${myErrorHandler.message}`;
return [-500];
}
}
/****************************************************************************************************/
function create_aiArray(dataSet, alpha_aiArray: (number)[] ): (number)[] {
const fcnName: string = "create_aiArray";
try {
/*'***************************************************************************
'The function is called by all PR1978 functions
'Calculates an array of values
'***************************************************************************/
let outputArray: (number)[] = [];
let myErrorMsg: string = "";
for(let i: number = 0; i < dataSet[0][idx.iSpecies]; i++) {
outputArray.push(0);
}
for(let i: number = 0; i < dataSet[0][idx.iSpecies]; i++) {
if(dataSet[i][idx.pc] > 0 && dataSet[i][idx.pc] !== 0) {
outputArray[i] = 0.457235529 * alpha_aiArray[i] * (gasLawR * dataSet[i][idx.tc])**2 / dataSet[i][idx.pc] ;
}else {
myErrorMsg = `The critical pressure of species ${i} is less than or equal to zero`;
throw new Error(`${fcnName}: ${myErrorMsg}`);
}
}
dataSet[0][idx.globalErrmsg] = myErrorMsg; //Used to transfer warning messages to calling function
return outputArray;
}
catch(myErrorHandler) {
dataSet[0][idx.globalErrmsg] = `${fcnName}: ${myErrorHandler.message}`;
return [-500];
}
}
/*************************************************************************************/
function create_aijArray(dataSet, binariesUsed: boolean, aiArray: (number)[], binaries: (number)[][], tempK: number): (number)[][] {
const fcnName: string = "create_aijArray";
try {
/*'***************************************************************************
'The function is called by all of the PR1978 functions
'Calculates an array of values
'***************************************************************************/
let outputArray: (number)[][] = [];
let myErrorMsg: string;
for(let i: number = 0; i < dataSet[0][idx.iSpecies]; i++) {
outputArray.push([])
for(let j: number = 0; j < dataSet[0][idx.iSpecies]; j++) {
if(binariesUsed === true) {
outputArray[i].push(Math.pow(aiArray[i] * aiArray[j], 0.5) * (1 - binaries[i][j]));
}else {
outputArray[i].push(Math.pow(aiArray[i] * aiArray[j], 0.5));
}
}
}
dataSet[0][idx.globalErrmsg] = myErrorMsg; //Used to transfer warning messages to calling function
return outputArray;
}
catch(myErrorHandler) {
dataSet[0][idx.globalErrmsg] = `${fcnName}: ${myErrorHandler.message}`;
return [[-500,-500],[-500,-500]];
}
}
/********************************************************************************************************************* */
function create_xi_aijArray(dataSet, aij_Array: (number)[][], molarComp: (number)[]): (number)[] {
const fcnName: string = "create_xi_aijArray"
try{
/*'***************************************************************************
'The function is called by calculate_Phi function
'Calculates an array of values
'***************************************************************************/
let xj_aijArray: (number)[] = []
let Aij: (number)[][] = []
let myErrorMsg: string = ""
for(let i: number = 0; i < dataSet[0][idx.iSpecies]; i++) {
Aij.push([])
xj_aijArray[i] = 0
for(let j: number = 0; j < dataSet[0][idx.iSpecies]; j++) {
Aij[i].push(molarComp[j] * aij_Array[i][j])
xj_aijArray[i] = Aij[i][j] + xj_aijArray[i]
}
}
dataSet[0][idx.globalErrmsg] = myErrorMsg; //Used to transfer warning messages to calling function
return xj_aijArray
}
catch(myErrorHandler){
dataSet[0][idx.globalErrmsg] = `${fcnName}: ${myErrorHandler.message}`;
return [-500]
}
}
/*************************************************************************************************** */
function calculate_dadt(dataSet, tempK: number, moleComp: (number)[], aij_Array: (number)[][], alpha_aiArray: (number)[]): number {
const fcnName: string = "calculate_dadt";
try {
/***************************************************************************
'This function is called by the Enthalpy, Entropy and Derivatives function
'This function calculates the PR1978 EOS da/dT
'***************************************************************************/
let dadt_Array: (number)[][] = [];
let myErrorMsg: string = "";
let dadT: number = 0;
for(let i: number = 0; i < dataSet[0][idx.iSpecies]; i++) {
dadt_Array.push([]);
for(let j: number = 0; j < dataSet[0][idx.iSpecies]; j++) {
dadt_Array[i][j] = -(moleComp[i] * moleComp[j] * aij_Array[i][j] / (2 * Math.pow(tempK, 0.5))) * ((dataSet[j][idx.ki] / (Math.pow(alpha_aiArray[j] * dataSet[j][idx.tc], 0.5))) + dataSet[i][idx.ki]/Math.pow(alpha_aiArray[i] * dataSet[i][idx.tc], 0.5));
if(dadt_Array[i][j] === Infinity){
myErrorMsg = "(alpha_aiArray(i or j) * dataSet(i or j, iColumns.tc)) ^ 0.5 = 0. Divide by zero error.";
throw new Error(`${fcnName}: ${myErrorMsg}`);
}
dadT = dadT + dadt_Array[i][j];
}
}
dataSet[0][idx.globalErrmsg] = myErrorMsg; //Used to transfer warning messages to calling function
return dadT;
} catch(myErrorHandler) {
dataSet[0][idx.globalErrmsg] = `${fcnName}: ${myErrorHandler.message}`;
return -500;
}
}
/****************************************************************************** */
/**
*
* Calculates constant pressure heat capacity (j/mol/K) given T (C), P (pbara), mole amounts, phase (vapor or liquid) and a dataSet.
*/
export function phaseCp(dataRange, temperature, pressure, moles, inputPhase, useBinaries?, kij0?, kijT?, decomposition?, errMsgsOn?): (number)[]{
try{
let CpIG: number = 0;
let CvResidual: number = 0;
let CpResidual: number = 0;
let d2adT2: number = 0;
let myErrorMsg: string = "";
let derivatives: (number)[] = [];
let CpRanges: (number)[][] = [];
let d2aidT2Array: (number)[] = [];
let daidTArray: (number)[] = [];
let outputArray: (number)[] = [];
let binaries: (number)[][] = [];
let inputDataArray: (any)[] = [];
const fcnName: string = "phaseCp"
inputDataArray = validateData(dataRange, temperature, pressure, moles, inputPhase, useBinaries, kij0, kijT, decomposition, errMsgsOn, -500, true);
let errorTest: any = inputDataArray[0][0]
if (typeof (errorTest) === "string") {
myErrorMsg = errorTest
throw new Error(`${fcnName}: ${myErrorMsg}`);
}
let dataSet = inputDataArray[idx.datasetArray];
if(dataSet[0][idx.globalErrmsg] !== ""){
myErrorMsg = dataSet[0].toString();
throw new Error(`${fcnName}: ${myErrorMsg}`);
}
const tempK: number = inputDataArray[idx.valuesArray][idx.T].valueOf();
const pBara: number = inputDataArray[idx.valuesArray][idx.P].valueOf();
const phase: string = inputDataArray[idx.valuesArray][idx.phase].valueOf();
const moleComp: (number)[] = inputDataArray[idx.moleCompArray];
const binariesUsed = inputDataArray[idx.valuesArray][idx.binariesOn].valueOf();
const aiArray = inputDataArray[idx.aiArray];
const kij0Array: (number)[][] = inputDataArray[idx.kij0Array];
const kijTArray: (number)[][] = inputDataArray[idx.kijTArray];
const decompArray: (number)[][] = inputDataArray[idx.decompArray];
CpRanges = selectCpDataRanges(dataSet, tempK, phase);
CpIG = calculate_Cp_IGorLiquid(dataSet, moleComp, tempK, CpRanges, phase);
if(phase === "liquid"){
outputArray.push(CpIG)
}else{
if(phase === "vapor" && pBara <= 1){
outputArray.push(CpIG)
outputArray.push(CpIG)
outputArray.push(0)
}else{
if(binariesUsed && !inputDataArray[idx.valuesArray][idx.validBinaries]){
throw new Error(`${fcnName}: ${dataSet[0][idx.globalErrmsg]}`);
}
if(binariesUsed){
if(inputDataArray[idx.valuesArray][idx.validBinaries]) {
binaries = calculate_binaries(dataSet, tempK, kij0Array, kijTArray, aiArray, decompArray);
if(binaries[0][0] === -500 && dataSet[0][idx.binariesUsed] === true){
throw new Error(`${fcnName}: ${dataSet[0][idx.globalErrmsg]}`);
}
}
binaries = calculate_binaries(dataSet, tempK, kij0Array, kijTArray, aiArray, decompArray);
}
derivatives = calculate_Derivatives(dataSet, tempK, pBara, moleComp, phase, binariesUsed, aiArray, binaries);
if(derivatives[0] !== 987654321.12345){
CpRanges = selectCpDataRanges(dataSet, tempK, phase);
d2aidT2Array = create_d2aidT2Array(dataSet, tempK);
daidTArray = create_daidTArray(dataSet, aiArray, tempK);
d2adT2 = calculate_d2adT2(dataSet, tempK, moleComp, aiArray, daidTArray, d2aidT2Array, binariesUsed, binaries);
if((derivatives[idx.Z] + derivatives[idx.b] * (1 + Math.pow(2, 0.5) )) / (derivatives[idx.Z] + derivatives[idx.b] * (1 - Math.pow(2, 0.5) )) <= 0){
myErrorMsg = "The natural log term in the Cv residual equation retun an error. Check phase.";
throw new Error(`${fcnName}: ${myErrorMsg}`);
}
if(derivatives[idx.sumb] <= 0){
myErrorMsg = "The term sum_b is less than or equal to zero. Check phase."
throw new Error(`${fcnName}: ${myErrorMsg}`);
}
CvResidual = 100000 * tempK * d2adT2 * Math.log((derivatives[idx.Z] + derivatives[idx.b] * (1 + Math.pow(2, 0.5))) / (derivatives[idx.Z] + derivatives[idx.b] * (1 - Math.pow(2, 0.5) ))) / (derivatives[idx.sumb] * Math.pow(8, 0.5) );
CpResidual = CvResidual + (tempK * (derivatives[idx.dPdT_constV]) * derivatives[idx.dVdT_constP] - gasLawR) * 100000
outputArray[0] = CpIG + CpResidual;
outputArray[1] = CpIG;
outputArray[2] = CpResidual;
}else{
myErrorMsg = "Calculate_Derivatives returned an error.";
throw new Error(`${fcnName}: ${myErrorMsg}`);
}
}
}
if(myErrorMsg = ""){
myErrorMsg = dataSet[0][idx.globalErrmsg];
}
return outputArray;
}catch(myErrorHandler){
return myErrorHandler.message;
}
}
/****************************************************************** */
function create_d2aidT2Array(dataSet, tempK): (number)[] {
const fcnName: string = "create_d2aidT2Array";
try{
/*'***************************************************************************
'The function is called directly of indirectly by vaporCv, PhaseCp, Derivatives, SpeedOfSound and JTCoef
'Calculates an array of values
'***************************************************************************/
let outputArray: (number)[] = [];
let myErrorMsg: string = "";
let tempValue: number = 0;
for(let i = 0; i < dataSet[0][idx.iSpecies]; i++) {
tempValue = (0.45724 * (Math.pow(gasLawR * (dataSet[i][idx.tc]), 2) / (dataSet[i][idx.pc]))) * dataSet[i][idx.ki];
tempValue = tempValue * Math.pow(dataSet[i][idx.tc] / tempK, 0.5) * (1 + dataSet[i][idx.ki]);
tempValue = tempValue / (2 * tempK * dataSet[i][idx.tc]);
outputArray.push(tempValue);
}
dataSet[0][idx.globalErrmsg] = myErrorMsg; //Used to transfer warning messages to calling function
return outputArray;
} catch(myErrorHandler) {
dataSet[0][idx.globalErrmsg] = `${fcnName}: ${myErrorHandler.message}`;
return [-500];
}
}
/******************************************************************************************/
function create_daidTArray(dataSet, aiArray: (number)[], tempK: number): (number)[] {
const fcnName: string = "create_daidTArray";
try{
/***************************************************************************
'The function is called directly of indirectly by vaporCv, PhaseCp, Derivatives, SpeedOfSound and JTCoef
'Calculates an array of values
'***************************************************************************/
let outputArray: (number)[] = [];
let myErrorMsg: string = "";
for(let i: number = 0; i < dataSet[0][idx.iSpecies]; i++) {
outputArray.push(-dataSet[i][idx.ki] * aiArray[i] / ((1 + dataSet[i][idx.ki] * (1 - Math.pow(tempK / dataSet[i][idx.tc], 0.5))) * Math.pow(tempK * dataSet[i][idx.tc], 0.5)));
}
dataSet[0][idx.globalErrmsg] = myErrorMsg; //Used to transfer warning messages to calling function
return outputArray;
}
catch(myErrorHandler) {
dataSet[0][idx.globalErrmsg] = `${fcnName}: ${myErrorHandler.message}`;
return [-500] ;
}
}
/*********************************************************************************************************************/
function calculate_sum_a(dataSet, aij_Array: (number)[][], molarComp:(number)[]): number {
const fcnName: string = "calculate_sum_a";
try {
/*'***************************************************************************
'The function is called by all of the PR1978 functions
'Calculates sum(ai) (see reference 1) from PR1978 EOS
'****************************************************************************/
let sum_a: number = 0;
let myErrorMsg: string = "";
for(let i: number = 0; i < dataSet[0][idx.iSpecies]; i++) {
for(let j: number = 0; j < dataSet[0][idx.iSpecies]; j++) {
sum_a = sum_a + aij_Array[i][j] * molarComp[i] * molarComp[j];
}
}
dataSet[0][idx.globalErrmsg] = myErrorMsg; //Used to transfer warning messages to calling function
return sum_a;
}
catch(myErrorHandler) {
dataSet[0][idx.globalErrmsg] = `${fcnName}: ${myErrorHandler.message}`;
return -500;
}
}
/**************************************************************************/
function calculate_sum_b(dataSet, molarComp: (number)[]): number {
const fcnName: string = "calculate_sum_b";
try {
/*'***************************************************************************
'The function is called by all of the PR1978 functions
'Calculates sum(bi) (see reference 1) from PR1978 EOS
'***************************************************************************/
let sum_b: number = 0;
let myErrorMsg: string ="";
for(let i: number = 0; i < dataSet[0][idx.iSpecies]; i++) {
sum_b = sum_b + dataSet[i][idx.bi] * molarComp[i];
}
dataSet[0][idx.globalErrmsg] = myErrorMsg; //Used to transfer warning messages to calling function
return sum_b;
}
catch(myErrorHandler) {
dataSet[0][idx.globalErrmsg] = `${fcnName}: ${myErrorHandler.message}`;
return -500;
}
}
/**************************************************************************************/
function calculate_A(dataSet, sum_a: number, tempK: number, pbara: number): number {
const fcnName: string = "calculate_A";
try {
/*'***************************************************************************
'The function is called by all of the PR1978 functions
'Calculates A (see reference 1) from PR1978 EOS
'***************************************************************************/
let myErrorMsg: string = "";
dataSet[0][idx.globalErrmsg] = myErrorMsg; //Used to transfer warning messages to calling function
return sum_a * pbara / Math.pow((gasLawR * tempK), 2);
}
catch(myErrorHandler) {
dataSet[0][idx.globalErrmsg] = `${fcnName}: ${myErrorHandler.message}`;
return -500;
}
}
/*******************************************************************************/
function calculate_B(dataSet, sum_b: number, tempK:number, pbara: number): number {
const fcnName: string = "calculate_B";
try {
/*'***************************************************************************
'The function is called by the 'CreateDataset' function
'Calculates B (see reference 1) from PR1978 EOS
'***************************************************************************/
let myErrorMsg: string ="";
dataSet[0][idx.globalErrmsg] = myErrorMsg; //Used to transfer warning messages to calling function
return sum_b * pbara / (gasLawR * tempK);
}
catch(myErrorHandler) {
dataSet[0][idx.globalErrmsg] = `${fcnName}: ${myErrorHandler.message}`;
return -500;
}
}
/***********************************************************************************************/
function validateBinariyArrays(dataSet, kij0, kijT, decomposition): (any)[] {
try {
let myErrorMsg: string = "";
const fcnName: string = "validateBinariyArrays";
let testSum: number = 0;
let decompTest: number = 0;
let decompArray: (number)[][] = [];
if(dataSet[0][idx.predictive] === true && !decomposition && decomposition[0][0] !== -500) {
dataSet[0][idx.globalErrmsg] = `${fcnName}: Predictive and binariesUsed parameters equal true but no decomposition is provided.`;
throw new Error(`${fcnName}: ${myErrorMsg}`);
}else{
if(decomposition.length !== dataSet[0][idx.iSpecies] + 1){
dataSet[0][idx.globalErrmsg] = `${fcnName}: The supplied decomposition row count must equal the number of species plus one for the header row. Correct decomposition.`;
return [false]
}else{
for(let i: number = 1; i < decomposition.length; i++){
if(decomposition[i].length !== currentNumberOfGroups + 1) {
dataSet[0][idx.globalErrmsg] = `${fcnName}: The column count for species ${i} does not equal equal 32 (31 groups plus 1 for the species name). Correct decomposition.`;
return [false]
}
}
for(let i: number = 0; i < dataSet[0][idx.iSpecies]; i++){
decompArray.push([])
for(let j: number = 0; j < currentNumberOfGroups; j++){
decompArray[i].push(decomposition[i+1][j+1]);
}
}
for(let i: number = 0; i < dataSet[0][idx.iSpecies]; i++) {
for(let j: number = 0; j < currentNumberOfGroups; j++) {
testSum = testSum + decompArray[i][j];
}
if(testSum === 0){
decompTest = decompTest + 1;
}else if(testSum - 1 < Math.pow(10, -5)) {
decompTest = decompTest + 1;
}else{
dataSet[0][idx.globalErrmsg] = `${fcnName}: The decomposition for species ${i} does not equal either zero or 1. Correct decomposition.`;
return [false];
}
testSum = 0;
}
}
}
if(dataSet[0][idx.predictive] === false){
if(dataSet[0][idx.binariesUsed] && !kij0) {
dataSet[0][idx.globalErrmsg] = `${fcnName}: The parameter binariesUsed equals true but no binaries are provided.`;
throw new Error(`${fcnName}: ${myErrorMsg}`);
}else{
if(kij0.length !== dataSet[0][idx.iSpecies] && kij0[0].length !== dataSet[0][idx.iSpecies]){
dataSet[0][idx.globalErrmsg] = `${fcnName}: kij0 binaries must be an array of size equal to the number of species by the number of species.`;
return [false];
}
testSum = 0
if(!dataSet[0][idx.predictive] && kij0) {
for(let i: number = 0; i < dataSet[0][idx.iSpecies]; i++) {
for(let j: number = 0; j < dataSet[0][idx.iSpecies]; j++) {
testSum = testSum + kij0[i][j] - kij0[j][i];
}
}
if(testSum !== 0) {
dataSet[0][idx.globalErrmsg] = `${fcnName}: kij0 binaries are not symetrical. The condition Kij0(i,j) = Kij0(j,i) must be true.`;
return [false];
}
}
}
if(dataSet[0][idx.predictive] === false && !kijT && kij0.length !== dataSet[0][idx.iSpecies] + 1 && kij0[0].length !== dataSet[0][idx.iSpecies] + 1 ) {
dataSet[0][idx.globalErrmsg] = `${fcnName}: The parameter binariesUsed equals true but no binaries are provided.`;
throw new Error(`${fcnName}: ${myErrorMsg}`);
}else{
if(kijT.length !== dataSet[0][idx.iSpecies] && kijT[0].length !== dataSet[0][idx.iSpecies]){
dataSet[0][idx.globalErrmsg] = `${fcnName}: kijT binaries must be an array of size equal to the number of species by the number of species.`;
return [false];
}
testSum = 0
for(let i: number = 0; i < dataSet[0][idx.iSpecies]; i++) {
for(let j: number = 0; j < dataSet[0][idx.iSpecies]; j++) {
testSum = testSum + kijT[i][j] - kijT[j][i];
}
}
if(testSum !== 0) {
dataSet[0][idx.globalErrmsg] = `${fcnName}: Warning: kijT binaries are imbalanced. The condition KijT(i,j) = KijT(j,i) must be true.`;
return [false];
}
}
}
return [true, kij0, kijT, decompArray];
} catch(myErrorHandler) {
dataSet[0][idx.globalErrmsg] = myErrorHandler.message;
return [false];
}
}
/****************************************************************************************/
function calculate_binaries(dataSet, tempK: number, kij0: (number)[][], kijT: (number)[][], passed_aiArray: (number)[], deComp: (number)[][]): (number)[][] {
try {
let myErrorMsg: string = "";
let errorSum: number = 0;
const fcnName: string = "calculate_binaries";
let passedTempK: number = 0;
let alpha_iArray: (number)[] = [];
let aiArray: (number)[] = [];
let binaries: (number)[][] = [];
if(dataSet[0][idx.predictive] === false && !kij0) {
dataSet[0][idx.globalErrmsg] = "The parameter binariesUsed equals true but kij0 is not provided.";
binaries = [[-500],[-500]];
return binaries;
}else if(dataSet[0][idx.predictive] === false && kij0){
for(let i: number = 0; i < dataSet[0][idx.iSpecies]; i++) {
for(let j: number = 0; j < dataSet[0][idx.iSpecies]; j++) {
errorSum = errorSum + kij0[i][j] - kij0[j][i];
}
}
if(errorSum !== 0) {
myErrorMsg = `${fcnName}: kij0 binaries are imbalanced. The condition Kij0(i,j) = Kij0(j,i) and kij0(i,i) = 0 must be true.`;
throw new Error(`${fcnName}: ${myErrorMsg}`);
}
if(kijT){
errorSum = 0;
for(let i: number = 0; i < dataSet[0][idx.iSpecies]; i++) {
for(let j: number = 0; j < dataSet[0][idx.iSpecies]; j++) {
errorSum = errorSum + kijT[i][j] - kijT[j][i];
}
}
if(errorSum !== 0) {
myErrorMsg = `${fcnName}: kijT binaries are imbalanced. The condition KijT(i,j) = KijT(j,i) must be true.`;
throw new Error(`${fcnName}: ${myErrorMsg}`);
}
}
if(kij0.length - 1 === dataSet[0][idx.iSpecies] && kij0[0].length - 1 === dataSet[0][idx.iSpecies]) {
for(let i: number = 0; i< dataSet[0][idx.iSpecies]; i++) {
binaries.push([]);
for(let j: number = 0; j < dataSet[0][idx.iSpecies]; j++) {
if(kijT) {
binaries[i].push(kij0[i][j] + tempK * kijT[i][j]);
}else {
binaries[i].push(kij0[i][j]);
}
}
}
}else {
myErrorMsg = `${fcnName}: Binary array must be a two dimensional array with each dimension equal to the number of species.`;
throw new Error(`${fcnName}: ${myErrorMsg}`);
}
}
if(dataSet[0][idx.predictive] && !deComp) {
dataSet[0][idx.globalErrmsg] = `${fcnName}: binariesUsed equals true but no decomposition is provided.`;
binaries = [[-500],[-500]];
}else{
passedTempK = tempK;
if(!passed_aiArray) {
alpha_iArray = create_alphaiArray(dataSet, passedTempK);
aiArray = create_aiArray(dataSet, alpha_iArray);
}else {
aiArray = passed_aiArray.slice(0);
}
binaries = createPredictiveBinaries(dataSet, deComp, passedTempK, aiArray);
}
dataSet[0][idx.globalErrmsg] = myErrorMsg; //Used to transfer warning messages to calling function
return binaries;
}
catch(myErrorHandler) {
dataSet[0][idx.globalErrmsg] = myErrorHandler.message;
return [[-500],[-500]];
}
}
/******************************************************************** */
function createPredictiveBinaries(dataSet, deComp: (number)[][], tempK: number, aiArray: (number)[]): (number)[][] {
const fcnName: string = "createPredictiveBinaries";
try{
let array_1to17: (number)[][] = [];
let array_18to31: (number)[][] = [];
let grpInteractionParamA: (number)[][] = [];
let grpInteractionParamB: (number)[][] = [];
let DoubleSum: (number)[][] = [];
let binaries: (number)[][] = [];
let myErrorMsg: string = "";
let currentNumberOfGroups = 31; // as of 2017, includes O2, SO2 and NO groups
grpInteractionParamA = initial2D_Array(currentNumberOfGroups, currentNumberOfGroups);
grpInteractionParamB = initial2D_Array(currentNumberOfGroups, currentNumberOfGroups);
DoubleSum = initial2D_Array(dataSet[0][idx.iSpecies ], dataSet[0][idx.iSpecies ]);
binaries = initial2D_Array(dataSet[0][idx.iSpecies ], dataSet[0][idx.iSpecies ]);
if(deComp.length !== dataSet[0][idx.iSpecies ] || deComp[1].length !== currentNumberOfGroups ) {
myErrorMsg = "The supplied decomposition range row count must equal the number of species and the column count must equal 31 groups plus 1 for the species column.";
throw new Error(`${fcnName}: ${myErrorMsg}`);
}
array_1to17 = [
[0, 65.54, 214.9, 431.6, 28.48, 3.775, 98.83, 103.6, 624.9, 43.58, 293.4, 144.8, 38.09, 159.6, 789.6, 3557, 7.892, 48.73, 102.6, 47.01, 174, 91.24, 416.3, 11.27, 322.2, 86.1, 0, 0, 0, 0, 0],
[65.54, 0, 39.05, 134.5, 37.75, 29.85, 25.05, 5.147, -17.84, 8.579, 63.48, 141.4, 83.73, 136.6, 439.9, 4324, 59.71, 9.608, 64.85, 34.31, 155.4, 44, 520.52, 113.6, 55.9, 107.4, 0, 0, 0, 0, 0],
[214.9, 39.05, 0, -86.13, 131.4, 156.1, 56.62, 48.73, 0, 73.09, -120.8, 191.8, 383.6, 192.5, 374, 971.4, 147.9, 84.76, 91.62, 0, 326, 0, 728.1, 185.8, -70, 0, 0, 0, 0, 0, 0],
[431.6, 134.5, -86.13, 0, 309.5, 388.1, 170.5, 128.3, 0, 208.6, 25.05, 377.5, 341.8, 330.8, 685.9, 0, 366.8, 181.2, 0, 0, 548.3, 0, 0, 899, 0, 0, 0, 0, 0, 0, 0],
[28.48, 37.75, 131.4, 309.5, 0, 0, 9.951, 67.26, 106.7, 249.1, 33.97, 188, 136.57, 30.88, 190.1, 701.7, 2277.12, 19.22, 48.73, 0, 0, 156.1, 14.43, 394.5, 15.97, 205.89, 0, 0, 44.61, 436.14, 0],
[3.775, 29.85, 156.1, 388.1, 9.951, 0, 41.18, 67.94, 0, 12.7, 118, 136.2, 61.59, 157.2, 0, 2333, 7.549, 26.77, 0, 0, 137.6, 15.42, 581.3, 43.81, 0, 0, 0, 0, 0, 0, 0],
[98.83, 25.05, 56.62, 170.5, 67.26, 41.18, 0, -16.47, 52.5, 28.82, 129, 98.48, 185.3, 21.28, 277.6, 2268, 25.74, 9.951, -16.47, 3.775, 288.9, 153.4, 753.6, 195.6, 37.1, 233.4, 0, 0, 0, 0, 0],
[103.6, 5.147, 48.73, 128.3, 106.7, 67.94, -16.47, 0, -328, 37.4, -99.17, 154.4, 343.8, 9.608, 1002, 543.5, 97.8, -48.38, 343.1, 242.9, 400.1, 125.77, 753.6, 0, -196.6, 177.1, 0, 0, 0, 0, 0],
[624.9, -17.84, 0, 0, 249.1, 0, 52.5, -328, 0, 140.7, -99.17, 331.1, 702.4, 9.608, 1002, 1340, 209.7, 669.8, 0, 0, 602.9, 197, 753.6, 0, 0, 0, 0, 0, 0, 0, 0],
[43.58, 8.579, 73.09, 208.6, 33.97, 12.7, 28.82, 37.4, 140.7, 0, 139, 144.1, 179.5, 117.4, 493.1, 4211, 35.34, -15.44, 159.6, 31.91, 236.1, 113.1, 0, 1269, 0, 181.2, -27.5, 0, 0, 0, 0],
[293.4, 63.48, -120.8, 25.05, 188, 118, 129, -99.17, -99.17, 139, 0, 216.2, 331.5, 71.37, 463.2, 244, 297.2, 260.1, 0, 151.3, -51.82, 0, 0, 0, 0, 102.3, 0, 0, 0, 0, 0],
[144.8, 141.4, 191.8, 377.5, 136.57, 136.2, 98.48, 154.4, 331.1, 144.1, 216.2, 0, 113.92, 135.2, 0, 559.3, 73.09, 60.74, 74.81, 87.85, 261.13, 87.85, 685.9, 177.75, 54.9, 154.42, 5.1, 83.04, 0, 124.91, 3.77],
[38.09, 83.73, 383.6, 341.8, 30.88, 61.59, 185.3, 343.8, 702.4, 179.5, 331.5, 113.92, 0, 319.5, 0, 2574, 45.3, 59.71, 541.5, 0, 65.2, 23.33, 204.7, 6.488, 282.4, 2.4, 258.73, 0, 585.75, 263.54, 101.57],
[159.6, 136.6, 192.5, 330.8, 190.1, 157.2, 21.28, 9.608, 9.608, 117.4, 71.37, 135.2, 319.5, 0, 0, -157, 603.94, 0, 0, 0, 0, 145.84, 278.63, 0, 0, 0, 0, 0, 101.91, 0, 0],
[789.6, 439.9, 374, 685.9, 701.7, 0, 277.6, 1002, 1002, 493.1, 463.2, 0, 0, -157, 0, 30.88, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[3557, 4324, 971.4, 0, 2277.12, 2333, 2268, 543.5, 1340, 4211, 244, 559.3, 2574, 603.94, 30.88, 0, 1650, 2243, 0, 0, 830.76, 278.63, 0, 0, 374.4, 1376, 0, 0, -550.06, 0, 568.94],
[7.892, 59.71, 147.9, 366.8, 19.22, 7.549, 25.74, 97.8, 209.7, 35.34, 297.2, 73.09, 45.3, 0, 0, 1650, 0, 14.76, -518, -98.8, 151.3, 84.55, 569.6, 0, 0, 0, 0, 0, 0, 0, 0]
];
array_18to31 = [
[48.73, 9.608, 84.76, 181.2, 48.73, 26.77, 9.951, -48.38, 669.8, -15.44, 260.1, 60.74, 59.71, 0, 0, 2243, 14.76, 0, 24.71, 14.07, 175.7, 0, 644.3, 203, 26.8, 0, 0, 0, 0, 0, 0],
[102.6, 64.85, 91.62, 0, 0, 0, -16.47, 343.1, 0, 159.6, 0, 74.81, 541.5, 0, 0, 0, -518, 24.71, 0, 23.68, 621.4, 0, 0, 0, -141, 0, 0, 0, 0, 0, 0],
[47.01, 34.31, 0, 0, 0, 0, 3.775, 242.9, 0, 31.91, 151.3, 87.85, 0, 0, 0, 0, -98.8, 14.07, 23.68, 0, 460.8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[174, 155.4, 326, 548.3, 156.1, 137.6, 288.9, 400.1, 602.9, 236.1, -51.82, 261.13, 65.2, 145.84, 0, 830.76, 151.3, 175.7, 621.4, 460.8, 0, 75.84, 138.7, 128.2, 0, 0, 0, 0, 701.73, 0, 0],
[91.24, 44, 0, 0, 14.43, 15.42, 153.4, 125.77, 197, 113.1, 0, 87.85, 23.33, 278.63, 0, 278.63, 84.55, 0, 0, 0, 75.84, 0, 260.1, 4.042, 0, 0, 309.17, 0, 0, 0, 0],
[416.3, 520.52, 728.1, 0, 394.5, 581.3, 753.6, 753.6, 753.6, 0, 0, 685.9, 204.7, 0, 0, 0, 569.6, 644.3, 0, 0, 138.7, 260.1, 0, 243.1, 0, 0, 0, 0, 0, 0, 0],
[11.27, 113.6, 185.8, 899, 15.97, 43.81, 195.6, 0, 0, 1269, 0, 177.75, 6.488, 0, 0, 0, 0, 203, 0, 0, 128.2, 4.042, 243.1, 0, 299.91, 4.8, 110.84, 0, 630.02, 278.63, 0],
[322.2, 55.9, -70, 0, 205.89, 0, 37.1, -196.6, 0, 0, 0, 54.9, 282.4, 0, 0, 374.4, 0, 26.8, -141, 0, 0, 0, 0, 299.91, 0, 0, 339.94, 172.26, 0, 0, 0],
[86.1, 107.4, 0, 0, 0, 0, 233.4, 177.1, 0, 181.2, 102.3, 154.42, 2.4, 0, 0, 1376, 0, 0, 0, 0, 0, 0, 0, 4.8, 339.94, 0, 0, 0, 0, 271.09, 120.1],
[0, 0, 0, 0, 0, 0, 0, 0, 0, -27.5, 0, 5.1, 258.73, 0, 0, 0, 0, 0, 0, 0, 0, 309.17, 0, 110.84, 172.26, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 44.61, 0, 0, 0, 0, 0, 0, 83.04, 0, 101.91, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 436.14, 0, 0, 0, 0, 0, 0, 0, 585.75, 0, 0, -550.06, 0, 0, 0, 0, 701.73, 0, 0, 630.02, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 124.91, 263.54, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 278.63, 0, 271.09, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 74.81, 0, 0, 0, 0, 0, 0, 3.77, 101.57, 0, 0, 568.94, 0, 0, 0, 0, 0, 0, 0, 0, 0, 120.1, 0, 0, 0, 0, 0]
];
for(let i: number = 0; i < 17; i++) {
for(let j: number = 0; j < currentNumberOfGroups; j++) {
grpInteractionParamA[i][j] = array_1to17[i][j] * 10; //<= Convert from MPa to bara
}
}
for(let i: number = 0; i < 31 - 17; i++) {
for(let j: number = 0; j < currentNumberOfGroups; j++) {
grpInteractionParamA[i + 16][j] = array_18to31[i][j] * 10; //<= Convert from MPa to bara
}
}
array_1to17 = [
[0, 105.7, 249.9, 575, 20.25, 8.922, 136.2, 103.6, 774.1, 60.05, 170.9, 401.5, 88.19, 227.8, 1829, 11195, 35, 44.27, 260.1, 169.5, 239.5, 94.24, 513.4, 55.48, 201.4, 87.5, 0, 0, 0, 0, 0],
[105.7, 0, 41.59, 183.9, 74.81, 65.88, 64.51, -7.549, -4.118, 27.79, -74.46, 237.1, 188.7, 124.6, 504.8, 12126, 82.35, 50.79, 51.82, 51.13, 240.9, 45.55, 673.22, 231.6, -28.5, 200.8, 0, 0, 0, 0, 0],
[249.9, 41.59, 0, 85.1, 157.5, 96.77, 129.7, -89.22, 0, 71.37, 18.53, 380.9, 375.4, 562.8, 520.9, 567.6, -55.59, 193.2, 54.9, 0, 287.9, 0, 750.9, 634.2, 233.7, 0, 0, 0, 0, 0, 0],
[575, 183.9, 85.1, 0, 35.69, -224.8, 284.1, 189.1, 0, 294.4, 81.33, 162.7, 635.2, -297.2, 1547, 0, -219.3, 419, 0, 0, 2343, 0, 0, 4655, 0, 0, 0, 0, 0, 0, 0],
[20.25, 74.81, 157.5, 35.69, 0, 13.37, 167.5, 190.8, 408.3, 5.49, 473.9, 214.81, 37.06, 307.46, 1318, 4719.63, 33.29, 68.29, 0, 0, 92.99, 20.92, 378.1, 24.48, 323.59, 0, 0, -95.05, 958.75, 0, 107.06],
[8.922, 65.88, 96.77, -224.8, 13.37, 0, 50.79, 210.7, 0, 73.43, -212.8, 235.7, 84.92, 217.1, 0, 5147, 20.93, -5.147, 0, 0, 150, 33.3, 517.1, 53.1, 0, 0, 0, 0, 0, 0, 0],
[136.2, 64.51, 129.7, 284.1, 167.5, 50.79, 0, 16.47, 251.2, 65.54, 36.72, 253.6, 490.7, 6.177, 449.5, 62.18, 78.92, 19.9, 61.42, 1.716, 189.1, 153.4, 590.5, 361.3, -23.7, 404.9, 0, 0, 0, 0, 0],
[103.6, -7.549, -89.22, 189.1, 190.8, 210.7, 16.47, 0, -569.3, 53.53, -193.5, 374.4, 1712, -36.72, -736.4, 411.8, 67.94, 27.79, 880.2, -7.206, 1201, -231.1, 590.5, 0, -397.4, 2559.4, 0, 0, 0, 0, 0],
[774.1, -4.118, 0, 0, 408.3, 0, 251.2, -569.3, 0, 277.6, -193.5, 276.6, 1889, -36.72, -736.4, -65.88, 3819, 589.5, 0, 0, 1463, -238.8, 590.5, 0, 0, 0, 0, 0, 0, 0, 0],
[60.05, 27.79, 71.37, 294.4, 5.49, 73.43, 65.54, 53.53, 277.6, 0, 35.69, 354.1, 546.6, 166.4, 832.1, 13031, 52.5, 24.36, 140.7, 69.32, 192.5, 143.6, 0, 18666, 0, 281.4, 50.1, 0, 0, 0, 0],
[170.9, -74.46, 18.53, 81.33, 473.9, -212.8, 36.72, -193.5, -193.5, 35.69, 0, -132.8, 389.8, -127.7, -337.7, -60.39, -647.2, 134.9, 0, 2.745, 34.31, 0, 0, 0, 0, 988, 0, 0, 0, 0, 0],
[401.5, 237.1, 380.9, 162.7, 214.81, 235.7, 253.6, 374.4, 276.6, 354.1, -132.8, 0, 212.41, 199.02, 0, 277.95, 106.7, 183.9, -266.6, 66.91, 300.94, 190.79, 559.3, 86.82, 59.02, 109.81, 48.38, 165.74, 0, 241.57, 14.07],
[88.19, 188.7, 375.4, 635.2, 37.06, 84.92, 490.7, 1712, 1889, 546.6, 389.8, 212.41, 0, 550.06, 0, 5490.33, 92.65, 227.2, 94.71, 0, 70.1, -25.4, 222.8, 8.77, 362.71, 4.8, 100.54, 0, 1011.25, 255.99, 230.94],
[227.8, 124.6, 562.8, -297.2, 307.46, 217.1, 6.177, -36.72, -36.72, 166.4, -127.7, 199.02, 550.06, 0, 153.7, 599.13, 0, 0, 0, 0, 823.55, 404.23, 0, 0, 0, 0, 0, 98.14, 0, 0, 0],
[1829, 504.8, 520.9, 1547, 1318, 0, 449.5, -736.4, -736.4, 832.1, -337.7, 0, 0, 153.7, 0, -113, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[11195, 12126, 567.6, 0, 4719.63, 5147, 62.18, 411.8, -65.88, 13031, -60.39, 277.95, 5490.33, 599.13, -113, 0, 1661, 5199, 0, 0, -137.94, -89.9, 0, 1211.3, 148.58, 1609.35, 0, 0, -1404.15, 0, -144.81],
[35, 82.35, -55.59, -219.3, 33.29, 20.93, 78.92, 67.94, 3819, 52.5, -647.2, 106.7, 92.65, 0, 0, 1661, 0, 11.32, 6815, 1809, 165.1, -7.51, 536.7, 0, 0, 0, 0, 0, 0, 0, 0]
];
array_18to31 = [
[44.27, 50.79, 193.2, 419, 68.29, -5.147, 19.9, 27.79, 589.5, 24.36, 134.9, 183.9, 227.2, 0, 0, 5199, 11.32, 0, 121.8, -12.3, 373, 0, 687.7, -11.7, 26.8, 0, 0, 0, 0, 0, 0],
[260.1, 51.82, 54.9, 0, 0, 0, 61.42, 880.2, 0, 140.7, 0, -266.6, 94.71, 0, 0, 0, 6815, 121.8, 0, 87.5, 873.6, 0, 0, 0, -151, 0, 0, 0, 0, 0, 0],
[169.5, 51.13, 0, 0, 0, 0, 1.716, -7.206, 0, 69.32, 2.745, 66.91, 0, 0, 0, 0, 1809, -12.3, 87.5, 0, 2167, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[239.5, 240.9, 287.9, 2343, 92.99, 150, 189.1, 1201, 1463, 192.5, 34.31, 300.94, 70.1, 823.55, 0, -137.94, 165.1, 373, 873.6, 2167, 0, 74.81, 95.49, 102.9, 0, 0, 0, 0, 931.3, 0, 0],
[94.24, 45.55, 0, 0, 20.92, 33.3, 153.4, -231.1, -238.8, 143.6, 0, 190.79, -25.4, 404.23, 0, -89.9, -7.51, 0, 0, 0, 74.81, 0, 259.9, 8.18, 0, 0, 28.82, 0, 0, 0, 0],
[513.4, 673.22, 750.9, 0, 378.1, 517.1, 590.5, 590.5, 590.5, 0, 0, 559.3, 222.8, 0, 0, 0, 536.7, 687.7, 0, 0, 95.49, 259.9, 0, 305.6, 0, 0, 0, 0, 0, 0, 0],
[55.48, 231.6, 634.2, 4655, 24.48, 53.1, 361.3, 0, 0, 18666, 0, 86.82, 8.77, 0, 0, 1211.3, 0, -11.7, 0, 0, 102.9, 8.18, 305.6, 0, 354.13, 7.89, 155.45, 0, 1793.97, 274.52, 0],
[201.4, -28.5, 233.7, 0, 323.59, 0, -23.7, -397.4, 0, 0, 0, 59.02, 362.71, 0, 0, 148.58, 0, 26.8, -151, 0, 0, 0, 0, 354.13, 0, 665.7, 1343, 0, 0, 0, 0],
[87.5, 200.8, 0, 0, 0, 0, 404.9, 2559.4, 0, 281.4, 988, 109.81, 4.8, 0, 0, 1609.35, 0, 0, 0, 0, 0, 0, 0, 7.89, 665.7, 0, 0, 0, 0, 362.36, 105.69],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 50.1, 0, 48.38, 100.54, 0, 0, 0, 0, 0, 0, 0, 0, 28.82, 0, 155.45, 1343, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, -95.05, 0, 0, 0, 0, 0, 0, 165.74, 0, 98.14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 958.75, 0, 0, 0, 0, 0, 0, 0, 1011.25, 0, 0, -1404.15, 0, 0, 0, 0, 931.3, 0, 0, 1793.97, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 241.57, 255.99, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 274.52, 0, 362.36, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 107.06, 0, 0, 0, 0, 0, 0, 14.07, 230.94, 0, 0, -144.81, 0, 0, 0, 0, 0, 0, 0, 0, 0, 105.69, 0, 0, 0, 0, 0]
];
for(let i: number = 0; i < 17; i++) {
for(let j: number = 0; j < currentNumberOfGroups; j++) {
grpInteractionParamB[i][j] = array_1to17[i][j] * 10; //<= Convert from MPa to bara
}
}
for(let i: number = 0; i < 31 - 17; i++) {
for(let j: number = 0; j < currentNumberOfGroups; j++) {
grpInteractionParamB[i + 16][j] = array_18to31[i][j] * 10; //<= Convert from MPa to bara
}
}
for(let i: number = 0; i < dataSet[0][idx.iSpecies]; i++) {
for(let j: number = 0; j < dataSet[0][idx.iSpecies]; j++) {
if(i === j) {
binaries[i][j] = 0;
binaries[j][i] = 0;
}else {
if(j > i) {
for(let k: number = 0; k < currentNumberOfGroups; k++) {
for(let l: number = 0; l < currentNumberOfGroups; l++) {
if(l !== k) {
if(grpInteractionParamA[k][l] !== 0 && grpInteractionParamB[k][l] !== 0) {
DoubleSum[i][j] = DoubleSum[i][j] + (deComp[i][k] - deComp[j][k]) * (deComp[i][l] - deComp[j][l]) * grpInteractionParamA[k][l] *Math.pow((298.15 / tempK), ((grpInteractionParamB[k][l] / grpInteractionParamA[k][l]) - 1));
}
}
}
}
if(j > i && DoubleSum[i][j] !== 0) {
binaries[i][j] = -(1 / 2) * DoubleSum[i][j];
binaries[i][j] = binaries[i][j] - Math.pow((Math.pow(aiArray[i], 1 / 2) / dataSet[i][idx.bi] - Math.pow(aiArray[j], 1 / 2) / dataSet[j][idx.bi]), 2);
binaries[i][j] = binaries[i][j] / (2 * Math.pow((aiArray[i] * aiArray[j]), 1 / 2) / (dataSet[i][idx.bi] * dataSet[j][idx.bi]));
binaries[j][i] = binaries[i][j];
}
}
}
}
}
dataSet[0][idx.globalErrmsg] = myErrorMsg; //Used to transfer warning messages to calling function
return binaries;
}
catch(myErrorHandler) {
dataSet[0][idx.globalErrmsg] = `${fcnName}: ${myErrorHandler.message}`;
return [[-500],[-500]];
}
}
/********************************************************************* */
function calculate_Derivatives(dataSet, tempK: number, pBara: number, moleComp: (number)[], phase: string, binariesUsed: boolean, passed_aiArray: (number)[] = [-500], binaries: (number)[][]): (number)[] {
try {
/***************************************************************************
'The function calculates various derivatives of the PR1978 EOS
'Calculates/returns an array of values
'***************************************************************************/
let a: number = 0;
let b: number = 0;
let Z: number = 0;
let alpha_aiArray: (number)[] = [];
let aiArray: (number)[] = [];
let aij_Array: (number)[][] = [];
let sum_a: number = 0;
let sum_b: number = 0;
let vol: number = 0;
let dPdT_V: number = 0;
let dadT_V: number = 0;
let dadT_P: number = 0;
let dBdT_P: number = 0;
let dZdT_P: number = 0;
let dPdv_T: number = 0;
let dVdT_P: number = 0;
let outputArray: (number)[] = [];
let myErrorMsg: string = "";
const fcnName: string = "calculate_Derivatives";
alpha_aiArray = create_alphaiArray(dataSet, tempK);
if(alpha_aiArray[0] === -500) {
myErrorMsg = "create_alphaiArray returned an error.";
throw new Error(`${fcnName}: ${myErrorMsg}`);
}
if(passed_aiArray[0] === -500){
aiArray = create_aiArray(dataSet, alpha_aiArray);
if(aiArray[0] === -500) {
myErrorMsg = "create_aiArray returned an error.";
throw new Error(`${fcnName}: ${myErrorMsg}`);
}
}else{
aiArray = passed_aiArray.slice(0);
};
aij_Array = create_aijArray(dataSet, binariesUsed, aiArray, binaries, tempK);
if(aij_Array[0][0] === -500 ) {
myErrorMsg = "create_aijArray returned an error.";