-
Notifications
You must be signed in to change notification settings - Fork 11
/
ChemE_Functions.bas
8849 lines (6766 loc) · 344 KB
/
ChemE_Functions.bas
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
Attribute VB_Name = "ChemE_Functions"
Option Explicit
Option Base 0
'*********************************************************************************************
'License
'Copyright (c) 2016, Steve Calderone, [email protected]
'All rights reserved.
'
'Redistribution and use in source and binary forms, with or without
'modification, are permitted provided that the following conditions are
'met:
'
'Redistributions of source code must retain the above copyright
'notice, this list of conditions and the following disclaimer.
'* Redistributions in binary form must reproduce the above copyright
'notice, this list of conditions and the following disclaimer in
'the documentation and/or other materials provided with the distribution
'* Neither the name of the www.stevecalderone.com nor the names
'of its contributors may be used to endorse or promote products derived
'from this software without specific prior written permission.
'
'THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
'AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
'IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
'ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
'LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
'CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
'SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
'INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
'CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
'ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
'POSSIBILITY OF SUCH DAMAGE.
'*********************************************************************************************
'References
'
'1
'Peng Robinson Equation of State
'A New Two-Constant Equation of State
'Ding-Yu Peng, Donald B. Robinson
'Ind. Eng. Chem. Fundamen., 1976, 15 (1), pp 59–64
'DOI: 10 0.1021 / i160057a011
'Publication Date: February 1976
'
'2
'Spreadsheet for Thermodynamics Instruction
'Phillip Savage - University of Michigan
''ChE classroom'
'Fall 1995
'http://ufdcimages.uflib.ufl.edu/AA/00/00/03/83/00128/AA00000383_00128_00262.pdf
'
'3
'Flash Routine Reference:
'CHEMENG 120 taught by Professor Musgrave during the Spring '04 term at Stanford.
'http://documentslide.com/documents/lecture-5-isothermal-flash-calculations.html
'
'4
'Implementation of Departure Functions on worksheets Departure_Pt1, 2, 3 & 4
'Dr. Phillip Savage - Penn State University
'http://www.che.psu.edu/department/directory-detail.aspx?LandOn=Gen&q=pes15
'
'5
'Bubble and Dew Point Routines adapted from:
'It’s not as easy as it looks - Revisiting Peng–Robinson equation of state convergence issues for dew point, bubble point and flash calculations
'
'Vamshi Krishna Kandula,(a) John C. Telotte (b) and F. Carl Knopf (corresponding author) (a)
'(a) Chemical Engineering Department, Louisiana State University, USA
'e -mail: cknopf@ southalabama.edu
'(b) Chemical Engineering Department, Florida A&M University – Florida State University, USA
'
'International Journal of Mechanical Engineering Education, Volume 41, Number 3 (July 2013), © Manchester University Press
' http://journals.sagepub.com/doi/pdf/10.7227/IJMEE.41.3.2
'
'6
'Cubic Equation VBA Code
'Dr.Tomas b.Co
'Michigan Tech University
'https://www.mtu.edu/chemical/department/faculty/co/
'
'7
'Bicubic Interpolation Code (for LeeKeslerZ() function)
'https://mathformeremortals.wordpress.com/
'
'8
'modArraySupport module and array handling techniques
'Chip Pearson, [email protected], www.cpearson.com
'
'9
'Derivation of the enthalpy departure function
'https://shareok.org/bitstream/handle/11244/12606/Thesis-1996-R231e.pdf?sequence=1
'by Abhishek Rastogi
'APPENDIX a
'A DETAILED DERIVATION OF PENG-ROBINSON EQUATION OF STATE ENTHALPY DEPARTURE FUNCTION
'
'10
'PData worksheet physical properties adapted from:
'Properties Databank 1.0
'Pedro Fajardo
'ppfk@ yahoo.com
'http://www.cheresources.com/invision/files/file/125-physical-properties-ms-excel-add-in/
'
'11
'Thermodynamic Properties Involving Derivatives
'Using the Peng-Robinson Equation of State
'R.M. Pratt Ph. D.
'The National University of Malaysia
'(now Associate Professor of Mathematics and Sciences, Fresno Pacific University)
'http://ufdcimages.uflib.ufl.edu/AA/00/00/03/83/00150/AA00000383_00150_00112.pdf
'
'12
'Chemical Equilibrium by Gibbs Energy Minimization on Spreadsheets
'Int. J. Engng Ed. Vol. 16, No. 4, pp. 335±339, 2000
'Y.LWIN
'Department of Chemical Engineering, Rangoon Institute of Technology, Insein P. O., Rangoon, Burma.
'e -mail: ylwin@ yahoo.com
'http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.476.5931&rep=rep1&type=pdf
'
'13
'Getting a Handle on Advanced Cubic Equations of State
'www.cepmagazine.org, November 2002, CEP
'Chorng H. Twu, Wayne D. Sim and Vince Tassone, Aspen Technology, Inc.
'http://people.clarkson.edu/~wwilcox/Design/adv-ceos.pdf
'14
'VLE predictions with the Peng.Robinson equation of state and
'temperature dependent kij calculated through a group contribution method
'Jean-No¡§el Jaubert., Fabrice Mutelet
'Laboratoire de Thermodynamique des Milieux Polyphas¢¥es, Institut National Polytechnique de Lorraine, Ecole Nationale Sup¢¥erieure des Industries
'Chimiques, 1rue Grandville, 54000 Nancy, France
'Received 24 January 2004; accepted 25 June 2004
'15
'Prediction of Thermodynamic Properties of Alkyne-Containing
'Mixtures with the E.PPR78 Model
'Xiaochun Xu,õ Jean-Noe.l Jaubert,*,õ Romain Privat,õ and Philippe Arpentinierö
'õEcole Nationale Supe.rieure des Industries Chimiques, Laboratoire Re.actions et Ge.nie des Proce.de.s (UMR CNRS 7274),
'Universite. de Lorraine, 1 rue Grandville, 54000 Nancy, France
'öCentre de Recherche Paris Saclay, Air Liquide, 1 chemin de la porte des loges, BP 126, 78354 Jouy-en-Josas, France
'16
'Efficient flash calculations for chemical process
'design — extension of the Boston–Britt
'‘‘Inside–out’’ flash algorithm to extreme
'conditions and new flash types
'Vipul S. Parekh and Paul M. Mathias
'Computers Chem. Engng Vol. 22, No. 10, pp. 1371—1380, 1998
'( 1998 Published by Elsevier Science Ltd.
'All rights reserved. Printed in Great Britain
'*********************************************************************************************
Const GasLawR As Double = 0.000083144621 'Units = m3-bar/gmol/K, GasLawR * 100,000 = 8.3144621 kJ/kg-mole/K = 8.3144621 J/g-mole/K
Enum iColumns '<= This enumeration is used to manage the range and array indexes used in many of these user defined functions
MW = 0 '<= References are found of this enumeration in the code
tc = 1 'for example: 'iColumns.MW' refers to the molecular weight column (zero base index) of the dataset
pc = 2
omega = 3
zc = 4
Ki = 5
bi = 6
tb = 7
hvap = 8
iHf298 = 9
iS298 = 10
iGf298 = 11
CpDataType = 12 '<= These indexes are for the Cp range index selection
NIST_Mn1 = 13 '<= These indexes are for the Cp range index selection
NIST_Mx1 = 14 '<= These are for information processed in the validateDataset function and required by other functions
NIST_MN2 = NIST_Mn1 + 10
NIST_MX2 = NIST_Mx1 + 10
NIST_MN3 = NIST_MN2 + 10
NIST_MX3 = NIST_MX2 + 10
NIST_MN4 = NIST_MN3 + 10
NIST_MX4 = NIST_MX3 + 10
NIST_MN5 = NIST_MN4 + 10
NIST_MX5 = NIST_MX4 + 10
NIST_MN6 = NIST_MN5 + 10
NIST_Mx6 = NIST_MX5 + 10
lastCpIndex = 72
alphaType = 73 '<= This is a flag for processing create_alphaiArray normally or using Twu volume tranlation method
errMsgsOn = 74 '<= This is a flag indicating if 'error messages on' was found in the top left most cell of the dataset.
LiquidsFound = 75 '<=
LiquidIndex = 76 '<= The base zero index of the the liquid first liquid species
iSpecies = 77 '<= Storage for the upper bound for the rows of the dataset. This is equal to the number of species minus one.
globalErrmsg = 78 '<= This is to store private function error message to pass along to the public function.
predictive = 79
FinalIndex = 80
MoleFraction = 0 '<= Used in the validateMoles function
moles = 1 '<= 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 = 1 '<= These indexes are for the seleceCpDataRange, Enthalpy and Entropy functions. This stores NIST-MNT index for Vapor at 298K
NBPVap = 2 '<= These indexes are for the seleceCpDataRange, Enthalpy and Entropy functions. This stores NIST-MNT for index vapor at normal boiling point
NBPLiq = 3 '<= 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 = 1
dPdT_constV = 2
dadT_constP = 3
dBdT_constP = 4
dZdT_constP = 5
dVdT_constP = 6
sumb = 7
suma = 8
a = 9
b = 10
Z = 11
vol = 12
End Enum
Private Function calculate_T_BubDew_Est(dataset As Variant, moleComp() As Double, pbara As Double) As Double()
'***************************************************************************
'This function is called by BubbleT and DewT functions to calculate the initial guess of the dew or bubble temperature
'More on this function can be found in Reference 5 and Reference 15
'***************************************************************************
On Error GoTo myErrorHandler:
Dim T_Lo As Double
Dim T_Hi As Double
Dim T_New As Double
Dim T_NBP As Double
Dim y_Sum_Lo As Double
Dim y_Sum_Hi As Double
Dim y_Sum As Double
Dim x_Sum_Lo As Double
Dim x_Sum_Hi As Double
Dim x_Sum As Double
Dim Ki() As Double
Dim outputArray() As Double
Dim i As Integer
Dim T_Dew_Est As Double
Dim T_Bub_Est As Double
Dim Counter As Integer
Dim T_Bub_RoughEst_Temp As Double
Dim T_Dew_RoughEst_Temp As Double
Dim BubTempFound As Boolean
Dim DewTempFound As Boolean
Dim myErrorMsg As String
Dim fcnName As String
fcnName = "calculate_T_BubDew_Est"
ReDim Ki(dataset(0, iColumns.iSpecies))
T_Bub_Est = 0
T_Dew_Est = 0
T_Lo = 0
T_Hi = 0
y_Sum = 0
For i = 0 To dataset(0, iColumns.iSpecies)
T_Bub_Est = T_Bub_Est + moleComp(i) * dataset(i, iColumns.tc)
Next i
T_Bub_Est = T_Bub_Est * 0.7
T_Bub_RoughEst_Temp = T_Bub_Est
Counter = 0
Do While BubTempFound = False
For i = 0 To dataset(0, iColumns.iSpecies)
If T_Bub_Est = 0 And dataset(i, iColumns.tb) >= 0 And dataset(i, iColumns.tc) = 0 Or pbara = 0 Or (1 / dataset(i, iColumns.tc) - 1 / dataset(i, iColumns.tb)) = 0 Then
myErrorMsg = "Species " & i & " error: The supplied pressure or TC equals zero or there is a problem with the boiling point."
GoTo myErrorHandler
End If
On Error Resume Next
'Not sure how to derive the Ki(i) estimating equation below. Need to contact the author as it's derivation is not presented in the referenced paper. Maybe it is only valid at the Dew and Bubble points. Check if it can be adopted for FlashTP.
Ki(i) = dataset(i, iColumns.pc) ^ (((1 / T_Bub_Est - 1 / dataset(i, iColumns.tb)) / _
((1 / dataset(i, iColumns.tc)) - (1 / dataset(i, iColumns.tb))))) 'Ki etimate & Dew/Bubble point adapted from => It’s not as easy as it looks: revisiting Peng–Robinson equation of state convergence issues for dew point, bubble point and flash calculations
Ki(i) = Ki(i) / pbara 'Vamshi Krishna Kandula (a), John C. Telotteb (b) and F. Carl Knopf - E-mail: [email protected] (a) (corresponding author)
'a - Chemical Engineering Department, Louisiana State University, USA
If Err.Number = 6 Then 'This is an overflow error often caused by 'b - Chemical Engineering Department, Florida A&M University – Florida State University, USA
ReDim outputArray(1) 'dividing a number by a very small number
outputArray(0) = T_Bub_RoughEst_Temp
outputArray(1) = 1.1 * T_Bub_RoughEst_Temp
calculate_T_BubDew_Est = outputArray()
Err.Clear
myErrorMsg = "Overflow error. Tried to divided a number by a very small number in Ki estimate calc found in LSU/FAM Dew & Bubble T paper."
GoTo myErrorHandler
End If
Next i
y_Sum = 0
For i = 0 To dataset(0, iColumns.iSpecies)
y_Sum = y_Sum + moleComp(i) * Ki(i)
Next i
If y_Sum < 1 Then
T_Lo = T_Bub_Est
y_Sum_Lo = y_Sum - 1
T_New = T_Bub_Est * 1.1
End If
If y_Sum > 1 Then
T_Hi = T_Bub_Est
y_Sum_Hi = y_Sum - 1
T_New = T_Bub_Est / 1.1
End If
If y_Sum = 1 Then
BubTempFound = True
End If
If T_Lo * T_Hi > 0 Then
T_New = (y_Sum_Hi * T_Lo - y_Sum_Lo * T_Hi) / (y_Sum_Hi - y_Sum_Lo)
End If
If Abs(T_Bub_Est - T_New) < 0.001 Then
BubTempFound = True
End If
If Abs(y_Sum - 1) < 0.00001 Then
BubTempFound = True
End If
Counter = Counter + 1
T_Bub_Est = T_New
If Counter = 1000 Then
myErrorMsg = "Counter is 100 iterations."
GoTo myErrorHandler
End If
Loop
Counter = 0
T_Dew_Est = 1.1 * T_Bub_Est
T_Dew_RoughEst_Temp = T_Dew_Est
T_Lo = 0
T_Hi = 0
Do While DewTempFound = False
For i = 0 To dataset(0, iColumns.iSpecies)
Ki(i) = dataset(i, iColumns.pc) ^ ((((1 / T_Dew_Est) - (1 / dataset(i, iColumns.tb))) / _
(1 / dataset(i, iColumns.tc) - 1 / dataset(i, iColumns.tb))))
Ki(i) = Ki(i) / pbara
Next i
x_Sum = 0
For i = 0 To dataset(0, iColumns.iSpecies)
x_Sum = x_Sum + moleComp(i) / Ki(i)
Next i
If x_Sum < 1 Then
T_Lo = T_Dew_Est
x_Sum_Lo = x_Sum - 1
T_New = T_Dew_Est / 1.1
End If
If x_Sum > 1 Then
T_Hi = T_Dew_Est
x_Sum_Hi = x_Sum - 1
T_New = T_Dew_Est * 1.1
End If
If x_Sum = 1 Then
DewTempFound = True
End If
If T_Lo * T_Hi > 0 Then
T_New = (T_Lo * x_Sum_Hi - T_Hi * x_Sum_Lo) / (x_Sum_Hi - x_Sum_Lo)
End If
If Abs(T_Dew_Est - T_New) < 0.001 Then
DewTempFound = True
End If
If Abs(x_Sum - 1) < 0.00001 Then
DewTempFound = True
End If
Counter = Counter + 1
T_Dew_Est = T_New
If Counter = 1000 Then
myErrorMsg = "More than 100 iterations!"
GoTo myErrorHandler
End If
Loop
ReDim outputArray(1)
outputArray(0) = T_Bub_Est
outputArray(1) = T_Dew_Est
calculate_T_BubDew_Est = outputArray()
Exit Function
myErrorHandler:
dataset(0, iColumns.globalErrmsg) = fcnName & ": " & myErrorMsg
ReDim outputArray(1)
outputArray(0) = T_Bub_RoughEst_Temp '<= Provide very rough estimate of bubble point temp if this function fails
outputArray(1) = T_Bub_RoughEst_Temp * 1.1 '<= Provide very rough estimate of dew point temp if this function fails
calculate_T_BubDew_Est = outputArray()
End Function
Public Function AntoineVP(Species As Variant, temperature As Variant, Optional errMsgsOn As Boolean = False) As Double
'***************************************************************************
'This function does not utilize a dataset.
'This function calculates the pure component vapor pressure given temperature
'based upon the Antoin coefficients found in the PData worksheet.
'***************************************************************************
On Error GoTo myErrorHandler
Application.ScreenUpdating = False
Dim TempK As Double
Dim minT As Double
Dim maxT As Double
Dim a As Double
Dim b As Double
Dim c As Double
Dim UDF_Range As Range
Dim i As Integer
Dim Name As String
Dim myErrorMsg As String
Dim fcnName As String
Dim TempC As Double
Dim PData As Worksheet
Dim SpeciesName As String
fcnName = "AntoineVP"
myErrorMsg = ""
If TypeName(Species) = "Range" Then
If Species.Rows.Count <> 1 Or Species.Columns.Count <> 1 Then
myErrorMsg = "The supplied species name should be a string or a single cell reference to a string equal to 'Vapor' or 'Liquid'."
GoTo myErrorHandler
End If
End If
SpeciesName = CStr(Species)
i = getPdataWorksheetIndex
Set PData = ThisWorkbook.Worksheets(i)
Set UDF_Range = Application.Caller
TempC = checkInputTemperature(temperature)
If TempC <> -273.15 Then
TempK = TempC + 273.15
Else
myErrorMsg = "There is a problem with the supplied temperature."
GoTo myErrorHandler
End If
On Error Resume Next
i = Application.WorksheetFunction.Match("(Species)", PData.Range("PData_PropertyNames"), 0)
If Err.Number = 1004 Then
myErrorMsg = "The '(Species)' column could not be found."
GoTo myErrorHandler
Else
If SpeciesName <> Application.WorksheetFunction.VLookup(SpeciesName, PData.Range("PData_Properties"), i, 0) Then
myErrorMsg = "The species could not be found."
GoTo myErrorHandler
End If
End If
i = Application.WorksheetFunction.Match("(ANT-TMN)", PData.Range("PData_PropertyNames"), 0)
If Err.Number = 1004 Then
myErrorMsg = "Could not find (ANT-TMN) column in PData worksheet."
GoTo myErrorHandler
Else
minT = Application.WorksheetFunction.VLookup(SpeciesName, PData.Range("PData_Properties"), i, 0)
If IsNumeric(minT) = False Then
myErrorMsg = "(ANT-tmn) in PData worksheet is not numeric." & myErrorMsg
GoTo myErrorHandler
End If
End If
i = Application.WorksheetFunction.Match("(ANT-TMX)", PData.Range("PData_PropertyNames"), 0)
If Err.Number = 1004 Then
myErrorMsg = "Could not find (ANT-TMX) column in PData worksheet." & myErrorMsg
GoTo myErrorHandler
Else
maxT = Application.WorksheetFunction.VLookup(SpeciesName, PData.Range("PData_Properties"), i, 0)
If IsNumeric(maxT) = False Then
myErrorMsg = "(ANT-tmx) in PData worksheet is not numeric." & myErrorMsg
GoTo myErrorHandler
End If
End If
i = Application.WorksheetFunction.Match("(ANT-A)", PData.Range("PData_PropertyNames"), 0)
If Err.Number = 1004 Then
myErrorMsg = "Could not find (ANT-A) column in PData worksheet." & myErrorMsg
GoTo myErrorHandler
Else
a = Application.WorksheetFunction.VLookup(SpeciesName, PData.Range("PData_Properties"), i, 0)
If IsNumeric(a) = False Then
myErrorMsg = "(ANT-A) in PData worksheet is not numeric." & myErrorMsg
GoTo myErrorHandler
End If
End If
i = Application.WorksheetFunction.Match("(ANT-B)", PData.Range("PData_PropertyNames"), 0)
If Err.Number = 1004 Then
myErrorMsg = "Could not find (ANT-B) column in PData worksheet." & myErrorMsg
GoTo myErrorHandler
Else
b = Application.WorksheetFunction.VLookup(SpeciesName, PData.Range("PData_Properties"), i, 0)
If IsNumeric(b) = False Then
myErrorMsg = "(ANT-B) in PData worksheet is not numeric." & myErrorMsg
GoTo myErrorHandler
End If
End If
i = Application.WorksheetFunction.Match("(ANT-C)", PData.Range("PData_PropertyNames"), 0)
If Err.Number = 1004 Then
myErrorMsg = "Could not find (ANT-C) column in PData worksheet." & myErrorMsg
GoTo myErrorHandler
Else
c = Application.WorksheetFunction.VLookup(SpeciesName, PData.Range("PData_Properties"), i, 0)
If IsNumeric(c) = False Then
myErrorMsg = "(ANT-C) in PData worksheet is not numeric." & myErrorMsg
GoTo myErrorHandler
End If
End If
On Error GoTo myErrorHandler
If TempK > minT And TempK < maxT And (TempK) + c > 10 ^ -12 Then
AntoineVP = 14.5038 * Exp(a - b / ((TempK) + c)) / 760# / 14.5038 '<convert from mmHG to atm to bara
Else
myErrorMsg = "The supplied temperature (" & TempC & " C) is outside of the valid temperature range. Tmin = " & minT - 273.15 & " C and Tmax = " & maxT - 273.15 & " C"
GoTo myErrorHandler
End If
On Error GoTo 0
Call errorSub(UDF_Range, fcnName & " Warning: ", myErrorMsg, errMsgsOn) '<=Used for warnings and to clear comments when errors are eliminated.
Application.ScreenUpdating = True
Exit Function
myErrorHandler:
Call errorSub(UDF_Range, fcnName & " Error: ", myErrorMsg, errMsgsOn)
AntoineVP = 0
Application.ScreenUpdating = True
End Function
Public Function LiquidWaterViscCP(temperature As Variant, Optional errMsgsOn As Boolean = False) As Double '<= dynamic viscosity of water
Dim myErrorMsg As String
'***************************************************************************
'The constants below are created from data found here:
'http://www.viscopedia.com/viscosity-tables/substances/water/
'***************************************************************************
On Error GoTo myErrorHandler
Application.ScreenUpdating = False
Dim UDF_Range As Range
Dim fcnName As String
Dim TempC As Double
Dim TempK As Double
fcnName = "LiquidWaterViscCP"
myErrorMsg = ""
Set UDF_Range = Application.Caller
TempC = checkInputTemperature(temperature)
If TempC <> -273.15 Then
TempK = TempC + 273.15
Else
myErrorMsg = "There is something wrong with the supplied temperature."
GoTo myErrorHandler
End If
If TempC < 1 Or TempC > 80 Then
myErrorMsg = "The supplied temperature is outside the valid range. Tmin = 1 C and Tmax = 80 C."
GoTo myErrorHandler
End If
LiquidWaterViscCP = -7.00115362651037 * 10 ^ -10 * TempC ^ 5 + 1.90681026542577 * 10 ^ -7 * TempC ^ 4 - 2.16713835341039E-05 * TempC ^ 3 + 1.39734725302587E-03 * TempC ^ 2 _
- 5.99578184253486E-02 * TempC + 1.78622754441384
Call errorSub(UDF_Range, fcnName & " Warning: ", myErrorMsg, errMsgsOn) '<=Used for warnings and to clear comments when errors are eliminated.
Application.ScreenUpdating = True
Exit Function
myErrorHandler:
Call errorSub(UDF_Range, fcnName & " Error: ", myErrorMsg, errMsgsOn)
LiquidWaterViscCP = 0
Application.ScreenUpdating = True
End Function
Public Function LiquidWaterViscM2BySec(temperature As Variant, Optional errMsgsOn As Boolean = False) As Double '<= kinematic viscosity of water
'***************************************************************************
'The constants below are created from data found here:
'http://www.viscopedia.com/viscosity-tables/substances/water/
'***************************************************************************
On Error GoTo myErrorHandler
Application.ScreenUpdating = False
Dim myErrorMsg As String
Dim UDF_Range As Range
Dim fcnName As String
Dim TempC As Double
Dim TempK As Double
fcnName = "LiquidWaterViscM2BySec"
myErrorMsg = ""
Set UDF_Range = Application.Caller
TempC = checkInputTemperature(temperature)
If TempC <> -273.15 Then
TempK = TempC + 273.15
Else
myErrorMsg = "There is something wrong with the supplied temperature."
GoTo myErrorHandler
End If
If TempC < 1 Or TempC > 80 Then
myErrorMsg = "The supplied temperature is outside the valid range. Tmin = 1 C and Tmax = 80 C."
GoTo myErrorHandler
End If
LiquidWaterViscM2BySec = (-7.12697702590059 * 10 ^ -10 * TempC ^ 5 + 1.93812004157535 * 10 ^ -7 * TempC ^ 4 - 2.19695476816367E-05 * TempC ^ 3 + _
1.41020562534985E-03 * TempC ^ 2 - 6.00397258025756E-02 * TempC + 1.78642374134197) * 10 ^ -6
Call errorSub(UDF_Range, fcnName & " Warning: ", myErrorMsg, errMsgsOn) '<=Used for warnings and to clear comments when errors are eliminated.
Application.ScreenUpdating = True
Exit Function
myErrorHandler:
Call errorSub(UDF_Range, fcnName & " Error: ", myErrorMsg, errMsgsOn)
LiquidWaterViscM2BySec = 0
Application.ScreenUpdating = True
End Function
Public Function LiquidWaterDensity(temperature As Variant, Optional errMsgsOn As Boolean = False) As Double
'***************************************************************************
'The constants below are created from data found here:
'http://www.viscopedia.com/viscosity-tables/substances/water/
'***************************************************************************
On Error GoTo myErrorHandler
Application.ScreenUpdating = False
Dim myErrorMsg As String
Dim UDF_Range As Range
Dim fcnName As String
Dim TempC As Double
Dim TempK As Double
fcnName = "LiquidWaterDensity"
myErrorMsg = ""
Set UDF_Range = Application.Caller
TempC = checkInputTemperature(temperature)
If TempC <> -273.15 Then
TempK = TempC + 273.15
Else
myErrorMsg = "There is something wrong with the supplied temperature."
GoTo myErrorHandler
End If
If TempC < 1 Or TempC > 80 Then
myErrorMsg = "The supplied temperature is outside the valid range. Tmin = 1 C and Tmax = 80 C."
GoTo myErrorHandler
End If
LiquidWaterDensity = 2.48894114505504E-12 * TempC ^ 5 - 7.01566908289643E-10 * TempC ^ 4 + 8.73643506922288E-08 * TempC ^ 3 - _
9.00827374796632E-06 * TempC ^ 2 + 6.8062375849856E-05 * TempC + 0.999845097660858
Call errorSub(UDF_Range, fcnName & " Warning: ", myErrorMsg, errMsgsOn) '<=Used for warnings and to clear comments when errors are eliminated.
Application.ScreenUpdating = True
Exit Function
myErrorHandler:
Call errorSub(UDF_Range, fcnName & " Error: ", myErrorMsg, errMsgsOn)
LiquidWaterDensity = 0
Application.ScreenUpdating = True
End Function
Public Function LeeKeslerZ(Species As Variant, temperature As Variant, pressure As Double, Optional errMsgsOn As Boolean = False) As Double
'***************************************************************************
'This function utilizes the PData_LK1NonPolar_Z0 & PData_LK1NonPolar_Z1 named ranges found in the PData worksheet
'and the BicubicInterpolation function in the Math module
'This function calculates the pure component compressibility given T and P.
'This function does not utilize a dataset.
'***************************************************************************
On Error GoTo myErrorHandler
Application.ScreenUpdating = False
'valid for non-polar to slightly polar gases
Dim TempK As Double
Dim z0 As Double
Dim z1 As Double
Dim omega As Double
Dim PR As Double
Dim pc As Double
Dim Tr As Double
Dim tc As Double
Dim i As Integer
Dim sourceBook As Workbook
Dim PDataExists As Boolean
Dim PropertyNamesExist As Boolean
Dim int_TC As Integer
Dim int_PC As Integer
Dim int_Species As Integer
Dim int_Omega As Integer
Dim sourceSheet As Worksheet
Dim PDataName As Name
Dim PropertiesExist As Boolean
Dim PData_LK1NonPolar_Z0 As Boolean
Dim PData_LK1NonPolar_Z1 As Boolean
Dim myErrorMsg As String
Dim UDF_Range As Range
Dim fcnName As String
Dim TempC As Double
Dim pbara As Double
Dim PData As Worksheet
Dim SpeciesName As String
i = getPdataWorksheetIndex
Set PData = ThisWorkbook.Worksheets(i)
If TypeName(Species) = "Range" Then
If Species.Rows.Count <> 1 Or Species.Columns.Count <> 1 Then
myErrorMsg = "The supplied species name should be a string or a single cell reference to a string equal to 'Vapor' or 'Liquid'."
GoTo myErrorHandler
End If
End If
SpeciesName = CStr(Species)
fcnName = "LeeKeslerZ"
myErrorMsg = ""
Set UDF_Range = Application.Caller
TempC = checkInputTemperature(temperature)
If TempC <> -273.15 Then
TempK = TempC + 273.15
Else
myErrorMsg = "There is something wrong with the supplied temperature."
GoTo myErrorHandler
End If
pbara = checkInputPressure(pressure)
If pbara = -1 Then
myErrorMsg = "There is something wrong with the supplied pressure."
GoTo myErrorHandler
End If
If ValidateWorkbook = False Then
myErrorMsg = "Workbook failed validation."
GoTo myErrorHandler
End If
Set sourceSheet = ThisWorkbook.Worksheets(getPdataWorksheetIndex)
i = 1
For Each PDataName In ThisWorkbook.Names '<=ValidateWorkbook function does not check for existance of 'PData_LK1NonPolar_Z0' or 'PData_LK1NonPolar_Z1' named ranges.
'<=so check for them here'
If ThisWorkbook.Names(i).Name = "PData_LK1NonPolar_Z0" Then
PData_LK1NonPolar_Z0 = True
End If
If ThisWorkbook.Names(i).Name = "PData_LK1NonPolar_Z1" Then
PData_LK1NonPolar_Z1 = True
End If
i = i + 1
Next PDataName
If PData_LK1NonPolar_Z0 = True And PData_LK1NonPolar_Z1 = False Then
myErrorMsg = "the 'PData_LK1NonPolar_Z0' and 'PData_LK1NonPolar_Z1' named ranges do not exist."
GoTo myErrorHandler
End If
On Error Resume Next
int_Species = Application.WorksheetFunction.Match("(Species)", PData.Range("PData_PropertyNames"), 0)
If Err.Number = 1004 Then
myErrorMsg = "The '(species)' column could not be found."
GoTo myErrorHandler
Else
If SpeciesName <> Application.WorksheetFunction.VLookup(SpeciesName, PData.Range("PData_Properties"), int_Species, 0) Then
myErrorMsg = "The species could not be found."
GoTo myErrorHandler
End If
End If
int_Species = Application.WorksheetFunction.Match("(Species)", PData.Range("PData_PropertyNames"), 0)
If Err.Number = 1004 Then
myErrorMsg = "The '(species)' column could not be found."
GoTo myErrorHandler
End If
int_TC = Application.WorksheetFunction.Match("(TC, K)", sourceSheet.Range("PData_PropertyNames"), 0)
If Err.Number = 1004 Then
myErrorMsg = "Column with label '(TC)' not found in PData worksheet"
GoTo myErrorHandler
End If
int_PC = Application.WorksheetFunction.Match("(PC, bara)", sourceSheet.Range("PData_PropertyNames"), 0)
If Err.Number = 1004 Then
myErrorMsg = "Column with label '(PC)' not found in PData worksheet"
GoTo myErrorHandler
End If
int_Omega = Application.WorksheetFunction.Match("(OMEGA)", sourceSheet.Range("PData_PropertyNames"), 0)
If Err.Number = 1004 Then
myErrorMsg = "Column with label '(OMEGA)' not found in PData worksheet"
GoTo myErrorHandler
End If
On Error GoTo myErrorHandler
TempK = TempC + 273.15
pc = Application.WorksheetFunction.VLookup(SpeciesName, sourceSheet.Range("PData_Properties"), int_PC, 0)
If IsNumeric(pc) = False Then
myErrorMsg = "The critical pressure is not numeric."
GoTo myErrorHandler
End If
tc = Application.WorksheetFunction.VLookup(SpeciesName, sourceSheet.Range("PData_Properties"), int_TC, 0)
If IsNumeric(tc) = False Then
myErrorMsg = "The critical pressure is not numeric."
GoTo myErrorHandler
End If
omega = Application.WorksheetFunction.VLookup(SpeciesName, sourceSheet.Range("PData_Properties"), int_Omega, 0)
If IsNumeric(omega) = False Then
myErrorMsg = "The critical pressure is not numeric."
GoTo myErrorHandler
End If
If pc = 0 Or tc = 0 Then
myErrorMsg = "Some critical properties are zero."
GoTo myErrorHandler
End If
PR = pbara / pc
Tr = TempK / tc
z0 = BicubicInterpolation(sourceSheet.Range("PData_LK1NonPolar_Z0"), PR, Tr)
z1 = BicubicInterpolation(sourceSheet.Range("PData_LK1NonPolar_Z1"), PR, Tr)
LeeKeslerZ = z0 + omega * z1
Call errorSub(UDF_Range, fcnName & " Warning: ", myErrorMsg, errMsgsOn) '<=Used for warnings and to clear comments when errors are eliminated.
Application.ScreenUpdating = True
Exit Function
myErrorHandler:
LeeKeslerZ = 0
Call errorSub(UDF_Range, fcnName & " Error: ", myErrorMsg, errMsgsOn)
Application.ScreenUpdating = True
End Function
Public Function LeeKeslerVP(Species As Variant, temperature As Variant, Optional errMsgsOn As Boolean = False) As Double
'***************************************************************************
'This function returns pure component vapor pressure in bar given temp in C
'valid for non-polar species
'For more informaton - https://en.wikipedia.org/wiki/Lee%E2%80%93Kesler_method
'The prediction error can be up to 10% for polar components and small pressures and the calculated pressure is typically too low.
'For pressures above 1 bar, that means, above the normal boiling point, the typical errors are below 2%.
'***************************************************************************
On Error GoTo myErrorHandler
Application.ScreenUpdating = False
Dim TempK As Double
Dim z0 As Double
Dim z1 As Double
Dim omega As Variant
Dim PR As Double
Dim pc As Variant
Dim Tr As Double
Dim tc As Variant
Dim N, i As Integer
Dim LKConstants() As Double
Dim f1 As Double
Dim f2 As Double
Dim sourceBook As Workbook
Dim PDataExists As Boolean
Dim PropertyNamesExist As Boolean
Dim int_Species As Integer
Dim int_TC As Integer
Dim int_PC As Integer
Dim int_Omega As Integer
Dim int_TB As Integer
Dim myErrorMsg As String
Dim sourceSheet As Worksheet
Dim tb As Double
Dim UDF_Range As Range
Dim fcnName As String
Dim TempC As Double
Dim PData As Worksheet
Dim SpeciesName As String
i = getPdataWorksheetIndex
Set PData = ThisWorkbook.Worksheets(i)
fcnName = "LeeKeslerVP"
myErrorMsg = ""
Set UDF_Range = Application.Caller
If TypeName(Species) = "Range" Then
If Species.Rows.Count <> 1 Or Species.Columns.Count <> 1 Then
myErrorMsg = "The supplied species name should be a string or a single cell reference to a string equal to 'Vapor' or 'Liquid'."
GoTo myErrorHandler
End If
End If
SpeciesName = CStr(Species)
TempC = checkInputTemperature(temperature)
If TempC <> -273.15 Then
TempK = TempC + 273.15
Else
myErrorMsg = "There is something wrong with the supplied temperature."
GoTo myErrorHandler
End If
If ValidateWorkbook = False Then
myErrorMsg = "Workbook validation failed"
GoTo myErrorHandler
End If
Set sourceSheet = ThisWorkbook.Worksheets(getPdataWorksheetIndex)
On Error Resume Next
int_Species = Application.WorksheetFunction.Match("(Species)", PData.Range("PData_PropertyNames"), 0)