-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathpsychrolib.bas
More file actions
1853 lines (1542 loc) · 56.5 KB
/
Copy pathpsychrolib.bas
File metadata and controls
1853 lines (1542 loc) · 56.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
' PsychroLib (version 2.5.0) (https://github.com/psychrometrics/psychrolib).
' Copyright (c) 2018-2020 The PsychroLib Contributors for the current library implementation.
' Copyright (c) 2017 ASHRAE Handbook — Fundamentals for ASHRAE equations and coefficients.
' Licensed under the MIT License.
'
' psychrolib.vba
'
' Contains functions for calculating thermodynamic properties of gas-vapor mixtures
' and standard atmosphere suitable for most engineering, physical and meteorological
' applications.
'
' Most of the functions are an implementation of the formulae found in the
' 2017 ASHRAE Handbook - Fundamentals, in both International System (SI),
' and Imperial (IP) units. Please refer to the information included in
' each function for their respective reference.
'
' Example
' ' Set the unit system, for example to SI (can be either ' SI' or ' IP' )
' ' by uncommenting the following line in the psychrolib module
' Const PSYCHROLIB_UNITS = UnitSystem.SI
'
' ' Calculate the dew point temperature for a dry bulb temperature of 25 C and a relative humidity of 80%
' TDewPoint = GetTDewPointFromRelHum(25.0, 0.80)
' Debug.Print(TDewPoint)
' 21.309397163661785
'
' Copyright
' - For the current library implementation
' Copyright (c) 2018-2020 The PsychroLib Contributors.
' - For equations and coefficients published ASHRAE Handbook — Fundamentals, Chapter 1
' Copyright (c) 2017 ASHRAE Handbook — Fundamentals (https://www.ashrae.org)
'
' License
' MIT (https://github.com/psychrometrics/psychrolib/LICENSE.txt)
'
' Note from the Authors
' We have made every effort to ensure that the code is adequate, however, we make no
' representation with respect to its accuracy. Use at your own risk. Should you notice
' an error, or if you have a suggestion, please notify us through GitHub at
' https://github.com/psychrometrics/psychrolib/issues.
'
Option Explicit
'******************************************************************************************************
' IMPORTANT: Manually uncomment the system of units to use
'******************************************************************************************************
'Enumeration to define systems of units
Enum UnitSystem
IP = 1
SI = 2
End Enum
' Uncomment one of these two lines to define the system of units ("IP" or "SI")
'Const PSYCHROLIB_UNITS = UnitSystem.IP
'Const PSYCHROLIB_UNITS = UnitSystem.SI
'******************************************************************************************************
' Global constants
'******************************************************************************************************
Private Const ZERO_FAHRENHEIT_AS_RANKINE = 459.67 ' Zero degree Fahrenheit (°F) expressed as degree Rankine (°R).
'Reference: ASHRAE Handbook - Fundamentals (2017) ch. 39.
Private Const ZERO_CELSIUS_AS_KELVIN = 273.15 ' Zero degree Celsius (°C) expressed as Kelvin (K).
' Reference: ASHRAE Handbook - Fundamentals (2017) ch. 39.
Private Const R_DA_IP = 53.35 ' Universal gas constant for dry air (IP version) in ft lbf/lb_DryAir/R.
Private Const R_DA_SI = 287.042 ' Universal gas constant for dry air (SI version) in J/kg_DryAir/K.
Private Const MAX_ITER_COUNT = 100 ' Maximum number of iterations before exiting while loops.
Private Const MIN_HUM_RATIO = 1e-7 ' Minimum acceptable humidity ratio used/returned by any functions.
' Any value above 0 or below the MIN_HUM_RATIO will be reset to this value.
Private Const FREEZING_POINT_WATER_IP = 32.0 ' Freezing point of water, in °F
Private Const FREEZING_POINT_WATER_SI = 0.0 ' Freezing point of water, in °C
Private Const TRIPLE_POINT_WATER_IP = 32.018 ' Triple point of water, in °F
Private Const TRIPLE_POINT_WATER_SI = 0.01 ' Triple point of water, in °C
'******************************************************************************************************
' Helper functions
'******************************************************************************************************
Function GetUnitSystem() As UnitSystem
'
' This function returns the system of units currently in use (SI or IP).
'
' Args:
' none
'
' Returns:
' The system of units currently in use ('SI' or 'IP')
'
' Note:
'
' If you get an error here, it's because you have not uncommented one of the two lines
' defining PSYCHROLIB_UNITS (see Global Constants section)
'
GetUnitSystem = PSYCHROLIB_UNITS
End Function
Private Function isIP() As Variant
'
' This function checks whether the system of units currently in use is IP or SI.
'
' Args:
' none
'
' Returns:
' True if IP, False if SI, and raises error if undefined
'
If (PSYCHROLIB_UNITS = UnitSystem.IP) Then
isIP = True
ElseIf (PSYCHROLIB_UNITS = UnitSystem.SI) Then
isIP = False
Else
MsgBox ("The system of units has not been defined.")
isIP = CVErr(xlErrNA)
End If
End Function
Private Function GetTol() As Variant
'
' This function returns the tolerance on temperatures used for iterative solving.
' The value is physically the same in IP or SI.
'
' Args:
' none
'
' Returns:
' Tolerance on temperatures
'
If (PSYCHROLIB_UNITS = UnitSystem.IP) Then
GetTol = 0.001 * 9 / 5
Else
GetTol = 0.001
End If
End Function
Private Sub MyMsgBox(ByVal ErrMsg As String)
'
' Error message output
' Override this function with your own if needed, or comment its code out if you don't want to see the messages
'
' Message disabled by default
' MsgBox (ErrMsg)
End Sub
Private Function Min(ByVal Num1 As Variant, ByVal Num2 As Variant) As Variant
'
' Min function to return minimum of two numbers
'
If (Num1 <= Num2) Then
Min = Num1
Else
Min = Num2
End If
End Function
Private Function Max(ByVal Num1 As Variant, ByVal Num2 As Variant) As Variant
'
' Max function to return maximum of two numbers
'
If (Num1 >= Num2) Then
Max = Num1
Else
Max = Num2
End If
End Function
'*****************************************************************************
' Conversions between temperature units
'*****************************************************************************
Function GetTRankineFromTFahrenheit(ByVal T_Fahrenheit As Variant) As Variant
'
' Utility function to convert temperature to degree Rankine (°R)
' given temperature in degree Fahrenheit (°F).
'
'Args:
' T_Fahrenheit: Temperature in degree Fahrenheit (°F)
'
'Returns:
' Temperature in degree Rankine (°R)
'
'Reference:
' Reference: ASHRAE Handbook - Fundamentals (2017) ch. 1 section 3
'
'Notes:
' Exact conversion.
'
On Error GoTo ErrHandler
GetTRankineFromTFahrenheit = (T_Fahrenheit + ZERO_FAHRENHEIT_AS_RANKINE)
Exit Function
ErrHandler:
GetTRankineFromTFahrenheit = CVErr(xlErrNA)
End Function
Function GetTFahrenheitFromTRankine(ByVal T_Rankine As Variant) As Variant
'
' Utility function to convert temperature to degree Fahrenheit (°F)
' given temperature in degree Rankine (°R).
'
'Args:
' TRankine: Temperature in degree Rankine (°R)
'
'Returns:
' Temperature in degree Fahrenheit (°F)
'
'Reference:
' Reference: ASHRAE Handbook - Fundamentals (2017) ch. 1 section 3
'
'Notes:
' Exact conversion.
'
On Error GoTo ErrHandler
GetTFahrenheitFromTRankine = (T_Rankine - ZERO_FAHRENHEIT_AS_RANKINE)
Exit Function
ErrHandler:
GetTFahrenheitFromTRankine = CVErr(xlErrNA)
End Function
Function GetTKelvinFromTCelsius(ByVal T_Celsius As Variant) As Variant
'
' Utility function to convert temperature to Kelvin (K)
' given temperature in degree Celsius (°C).
'
'Args:
' TCelsius: Temperature in degree Celsius (°C)
'
'Returns:
' Temperature in Kelvin (K)
'
'Reference:
' Reference: ASHRAE Handbook - Fundamentals (2017) ch. 1 section 3
'
'Notes:
' Exact conversion.
'
On Error GoTo ErrHandler
GetTKelvinFromTCelsius = (T_Celsius + ZERO_CELSIUS_AS_KELVIN)
Exit Function
ErrHandler:
GetTKelvinFromTCelsius = CVErr(xlErrNA)
End Function
Function GetTCelsiusFromTKelvin(ByVal T_Kelvin As Variant) As Variant
'
' Utility function to convert temperature to degree Celsius (°C)
' given temperature in Kelvin (K).
'
'Args:
' TKelvin: Temperature in Kelvin (K)
'
'Returns:
' Temperature in degree Celsius (°C)
'
'Reference:
' Reference: ASHRAE Handbook - Fundamentals (2017) ch. 1 section 3
'
'Notes:
' Exact conversion.
'
On Error GoTo ErrHandler
GetTCelsiusFromTKelvin = (T_Kelvin - ZERO_CELSIUS_AS_KELVIN)
Exit Function
ErrHandler:
GetTCelsiusFromTKelvin = CVErr(xlErrNA)
End Function
'******************************************************************************************************
' Conversions between dew point, wet bulb, and relative humidity
'******************************************************************************************************
Function GetTWetBulbFromTDewPoint(ByVal TDryBulb As Variant, ByVal TDewPoint As Variant, ByVal Pressure As Variant) As Variant
'
' Return wet-bulb temperature given dry-bulb temperature, dew-point temperature, and pressure.
'
' Args:
' TDryBulb : Dry-bulb temperature in °F [IP] or °C [SI]
' TDewPoint : Dew-point temperature in °F [IP] or °C [SI]
' Pressure : Atmospheric pressure in Psi [IP] or Pa [SI]
'
' Returns:
' Wet-bulb temperature in °F [IP] or °C [SI]
'
' Reference:
' ASHRAE Handbook - Fundamentals (2017) ch. 1
'
Dim HumRatio As Variant
On Error GoTo ErrHandler
If TDewPoint > TDryBulb Then
MyMsgBox ("Dew point temperature is above dry bulb temperature")
GoTo ErrHandler
End If
HumRatio = GetHumRatioFromTDewPoint(TDewPoint, Pressure)
GetTWetBulbFromTDewPoint = GetTWetBulbFromHumRatio(TDryBulb, HumRatio, Pressure)
Exit Function
ErrHandler:
GetTWetBulbFromTDewPoint = CVErr(xlErrNA)
End Function
Function GetTWetBulbFromRelHum(ByVal TDryBulb As Variant, ByVal RelHum As Variant, ByVal Pressure As Variant) As Variant
'
' Return wet-bulb temperature given dry-bulb temperature, relative humidity, and pressure.
'
' Args:
' TDryBulb : Dry-bulb temperature in °F [IP] or °C [SI]
' RelHum : Relative humidity in range [0, 1]
' Pressure : Atmospheric pressure in Psi [IP] or Pa [SI]
'
' Returns:
' Wet-bulb temperature in °F [IP] or °C [SI]
'
' Reference:
' ASHRAE Handbook - Fundamentals (2017) ch. 1
'
Dim HumRatio As Variant
On Error GoTo ErrHandler
If (RelHum < 0 Or RelHum > 1) Then
MyMsgBox ("Relative humidity is outside range [0,1]")
GoTo ErrHandler
End If
HumRatio = GetHumRatioFromRelHum(TDryBulb, RelHum, Pressure)
GetTWetBulbFromRelHum = GetTWetBulbFromHumRatio(TDryBulb, HumRatio, Pressure)
Exit Function
ErrHandler:
GetTWetBulbFromRelHum = CVErr(xlErrNA)
End Function
Function GetRelHumFromTDewPoint(ByVal TDryBulb As Variant, ByVal TDewPoint As Variant) As Variant
'
' Return relative humidity given dry-bulb temperature and dew-point temperature.
'
' Args:
' TDryBulb : Dry-bulb temperature in °F [IP] or °C [SI]
' TDewPoint : Dew-point temperature in °F [IP] or °C [SI]
'
' Returns:
' Relative humidity in range [0, 1]
'
' Reference:
' ASHRAE Handbook - Fundamentals (2017) ch. 1 eqn 22
'
Dim VapPres As Variant
Dim SatVapPres As Variant
On Error GoTo ErrHandler
If (TDewPoint > TDryBulb) Then
MyMsgBox ("Dew point temperature is above dry bulb temperature")
GoTo ErrHandler
End If
VapPres = GetSatVapPres(TDewPoint)
SatVapPres = GetSatVapPres(TDryBulb)
GetRelHumFromTDewPoint = VapPres / SatVapPres
Exit Function
ErrHandler:
GetRelHumFromTDewPoint = CVErr(xlErrNA)
End Function
Function GetRelHumFromTWetBulb(ByVal TDryBulb As Variant, ByVal TWetBulb As Variant, ByVal Pressure As Variant) As Variant
'
' Return relative humidity given dry-bulb temperature, wet bulb temperature and pressure.
'
' Args:
' TDryBulb : Dry-bulb temperature in °F [IP] or °C [SI]
' TWetBulb : Wet-bulb temperature in °F [IP] or °C [SI]
' Pressure : Atmospheric pressure in Psi [IP] or Pa [SI]
'
' Returns:
' Relative humidity in range [0, 1]
'
' Reference:
' ASHRAE Handbook - Fundamentals (2017) ch. 1
'
Dim HumRatio As Variant
On Error GoTo ErrHandler
If TWetBulb > TDryBulb Then
MyMsgBox ("Wet bulb temperature is above dry bulb temperature")
GoTo ErrHandler
End If
HumRatio = GetHumRatioFromTWetBulb(TDryBulb, TWetBulb, Pressure)
GetRelHumFromTWetBulb = GetRelHumFromHumRatio(TDryBulb, HumRatio, Pressure)
Exit Function
ErrHandler:
GetRelHumFromTWetBulb = CVErr(xlErrNA)
End Function
Function GetTDewPointFromRelHum(ByVal TDryBulb As Variant, ByVal RelHum As Variant) As Variant
'
' Return dew-point temperature given dry-bulb temperature and relative humidity.
'
' Args:
' TDryBulb : Dry-bulb temperature in °F [IP] or °C [SI]
' RelHum: Relative humidity in range [0, 1]
'
' Returns:
' Dew-point temperature in °F [IP] or °C [SI]
'
' Reference:
' ASHRAE Handbook - Fundamentals (2017) ch. 1
'
Dim VapPres As Variant
On Error GoTo ErrHandler
If RelHum < 0 Or RelHum > 1 Then
MyMsgBox ("Relative humidity is outside range [0, 1]")
GoTo ErrHandler
End If
VapPres = GetVapPresFromRelHum(TDryBulb, RelHum)
GetTDewPointFromRelHum = GetTDewPointFromVapPres(TDryBulb, VapPres)
Exit Function
ErrHandler:
GetTDewPointFromRelHum = CVErr(xlErrNA)
End Function
Function GetTDewPointFromTWetBulb(ByVal TDryBulb As Variant, ByVal TWetBulb As Variant, ByVal Pressure As Variant) As Variant
'
' Return dew-point temperature given dry-bulb temperature, wet-bulb temperature, and pressure.
'
' Args:
' TDryBulb : Dry-bulb temperature in °F [IP] or °C [SI]
' TWetBulb : Wet-bulb temperature in °F [IP] or °C [SI]
' Pressure : Atmospheric pressure in Psi [IP] or Pa [SI]
'
' Returns:
' Dew-point temperature in °F [IP] or °C [SI]
'
' Reference:
' ASHRAE Handbook - Fundamentals (2017) ch. 1
'
Dim HumRatio As Variant
On Error GoTo ErrHandler
If TWetBulb > TDryBulb Then
MyMsgBox ("Wet bulb temperature is above dry bulb temperature")
GoTo ErrHandler
End If
HumRatio = GetHumRatioFromTWetBulb(TDryBulb, TWetBulb, Pressure)
GetTDewPointFromTWetBulb = GetTDewPointFromHumRatio(TDryBulb, HumRatio, Pressure)
Exit Function
ErrHandler:
GetTDewPointFromTWetBulb = CVErr(xlErrNA)
End Function
'******************************************************************************************************
' Conversions between dew point, or relative humidity and vapor pressure
'******************************************************************************************************
Function GetVapPresFromRelHum(ByVal TDryBulb As Variant, ByVal RelHum As Variant) As Variant
'
' Return partial pressure of water vapor as a function of relative humidity and temperature.
'
' Args:
' TDryBulb : Dry-bulb temperature in °F [IP] or °C [SI]
' RelHum : Relative humidity in range [0, 1]
'
' Returns:
' Partial pressure of water vapor in moist air in Psi [IP] or Pa [SI]
'
' Reference:
' ASHRAE Handbook - Fundamentals (2017) ch. 1 eqn 12, 22
'
On Error GoTo ErrHandler
If RelHum < 0 Or RelHum > 1 Then
MyMsgBox ("Relative humidity is outside range [0, 1]")
GoTo ErrHandler
End If
GetVapPresFromRelHum = RelHum * GetSatVapPres(TDryBulb)
Exit Function
ErrHandler:
GetVapPresFromRelHum = CVErr(xlErrNA)
End Function
Function GetRelHumFromVapPres(ByVal TDryBulb As Variant, ByVal VapPres As Variant) As Variant
' Return relative humidity given dry-bulb temperature and vapor pressure.
'
' Args:
' TDryBulb : Dry-bulb temperature in °F [IP] or °C [SI]
' VapPres: Partial pressure of water vapor in moist air in Psi [IP] or Pa [SI]
'
' Returns:
' Relative humidity in range [0, 1]
'
' Reference:
' ASHRAE Handbook - Fundamentals (2017) ch. 1 eqn 12, 22
'
On Error GoTo ErrHandler
If (VapPres < 0) Then
MyMsgBox ("Partial pressure of water vapor in moist air is negative")
GoTo ErrHandler
End If
GetRelHumFromVapPres = VapPres / GetSatVapPres(TDryBulb)
Exit Function
ErrHandler:
GetRelHumFromVapPres = CVErr(xlErrNA)
End Function
Private Function dLnPws_(TDryBulb As Variant) As Variant
'
' Helper function returning the derivative of the natural log of the saturation vapor pressure
' as a function of dry-bulb temperature.
'
' Args:
' TDryBulb : Dry-bulb temperature in °F [IP] or °C [SI]
'
' Returns:
' Derivative of natural log of vapor pressure of saturated air in Psi [IP] or Pa [SI]
'
' Reference:
' ASHRAE Handbook - Fundamentals (2017) ch. 1 eqn 5 & 6
'
Dim T As Variant
If (isIP()) Then
T = GetTRankineFromTFahrenheit(TDryBulb)
If (TDryBulb <= TRIPLE_POINT_WATER_IP) Then
dLnPws_ = 10214.165 / T ^ 2 - 0.0053765794 + 2 * 0.00000019202377 * T _
+ 3 * 3.5575832E-10 * T ^ 2 - 4 * 9.0344688E-14 * T ^ 3 + 4.1635019 / T
Else
dLnPws_ = 10440.397 / T ^ 2 - 0.027022355 + 2 * 0.00001289036 * T _
- 3 * 2.4780681E-09 * T ^ 2 + 6.5459673 / T
End If
Else
T = GetTKelvinFromTCelsius(TDryBulb)
If (TDryBulb <= TRIPLE_POINT_WATER_SI) Then
dLnPws_ = 5674.5359 / T ^ 2 - 0.009677843 + 2 * 0.00000062215701 * T _
+ 3 * 2.0747825E-09 * T ^ 2 - 4 * 9.484024E-13 * T ^ 3 + 4.1635019 / T
Else
dLnPws_ = 5800.2206 / T ^ 2 - 0.048640239 + 2 * 0.000041764768 * T _
- 3 * 0.000000014452093 * T ^ 2 + 6.5459673 / T
End If
End If
End Function
Function GetTDewPointFromVapPres(ByVal TDryBulb As Variant, ByVal VapPres As Variant) As Variant
'
' Return dew-point temperature given dry-bulb temperature and vapor pressure.
'
' Args:
' TDryBulb : Dry-bulb temperature in °F [IP] or °C [SI]
' VapPres: Partial pressure of water vapor in moist air in Psi [IP] or Pa [SI]
'
' Returns:
' Dew-point temperature in °F [IP] or °C [SI]
'
' Reference:
' ASHRAE Handbook - Fundamentals (2017) ch. 1 eqn. 5 and 6
'
' Notes:
' The dew point temperature is solved by inverting the equation giving water vapor pressure
' at saturation from temperature rather than using the regressions provided
' by ASHRAE (eqn. 37 and 38) which are much less accurate and have a
' narrower range of validity.
' The Newton-Raphson (NR) method is used on the logarithm of water vapour
' pressure as a function of temperature, which is a very smooth function
' Convergence is usually achieved in 3 to 5 iterations.
' TDryBulb is not really needed here, just used for convenience.
'
Dim BOUNDS(2) As Variant
Dim PSYCHROLIB_TOLERANCE As Variant
If (isIP()) Then
BOUNDS(1) = -148.
BOUNDS(2) = 392.
Else
BOUNDS(1) = -100.
BOUNDS(2) = 200.
End If
On Error GoTo ErrHandler
If ((VapPres < GetSatVapPres(BOUNDS(1))) Or (VapPres > GetSatVapPres(BOUNDS(2)))) Then
MyMsgBox ("Partial pressure of water vapor is outside range of validity of equations")
GoTo ErrHandler
End If
PSYCHROLIB_TOLERANCE = GetTol()
Dim TDewPoint As Variant
Dim lnVP As Variant
Dim d_lnVP As Variant
Dim TDewPoint_iter As Variant
Dim lnVP_iter
Dim index As Variant
index = 1
' We use NR to approximate the solution.
' First guess
TDewPoint = TDryBulb ' Calculated value of dew point temperatures, solved for iteratively
lnVP = Log(VapPres) ' Partial pressure of water vapor in moist air
' Iteration
Do
TDewPoint_iter = TDewPoint ' Value of Tdp used in NR calculation
lnVP_iter = Log(GetSatVapPres(TDewPoint_iter))
' Derivative of function, calculated analytically
d_lnVP = dLnPws_(TDewPoint_iter)
' New estimate, bounded by domain of validity of eqn. 5 and 6 and by the freezing point
TDewPoint = TDewPoint_iter - (lnVP_iter - lnVP) / d_lnVP
TDewPoint = Max(TDewPoint, BOUNDS(1))
TDewPoint = Min(TDewPoint, BOUNDS(2))
If (index > MAX_ITER_COUNT) Then
GoTo ErrHandler
End If
index = index + 1
Loop While (Abs(TDewPoint - TDewPoint_iter) > PSYCHROLIB_TOLERANCE)
TDewPoint = Min(TDewPoint, TDryBulb)
GetTDewPointFromVapPres = TDewPoint
Exit Function
ErrHandler:
GetTDewPointFromVapPres = CVErr(xlErrNA)
End Function
Function GetVapPresFromTDewPoint(ByVal TDewPoint As Variant) As Variant
'
' Return vapor pressure given dew point temperature.
'
' Args:
' TDewPoint : Dew-point temperature in °F [IP] or °C [SI]
'
' Returns:
' Partial pressure of water vapor in moist air in Psi [IP] or Pa [SI]
'
' Reference:
' ASHRAE Handbook - Fundamentals (2017) ch. 1 eqn 36
'
On Error GoTo ErrHandler
GetVapPresFromTDewPoint = GetSatVapPres(TDewPoint)
Exit Function
ErrHandler:
GetVapPresFromTDewPoint = CVErr(xlErrNA)
End Function
'******************************************************************************************************
' Conversions from wet-bulb temperature, dew-point temperature, or relative humidity to humidity ratio
'******************************************************************************************************
Function GetTWetBulbFromHumRatio(ByVal TDryBulb As Variant, ByVal HumRatio As Variant, ByVal Pressure As Variant) As Variant
'
' Return wet-bulb temperature given dry-bulb temperature, humidity ratio, and pressure.
'
' Args:
' TDryBulb : Dry-bulb temperature in °F [IP] or °C [SI]
' HumRatio : Humidity ratio in lb_H2O/lb_Air [IP] or kg_H2O/kg_Air [SI]
' Pressure : Atmospheric pressure in Psi [IP] or Pa [SI]
'
' Returns:
' Wet-bulb temperature in °F [IP] or °C [SI]
'
' Reference:
' ASHRAE Handbook - Fundamentals (2017) ch. 1 eqn 33 and 35 solved for Tstar
'
' Declarations
Dim Wstar As Variant
Dim TDewPoint As Variant, TWetBulb As Variant, TWetBulbSup As Variant, TWetBulbInf As Variant
Dim Tol As Variant, BoundedHumRatio As Variant, index As Variant
On Error GoTo ErrHandler
If HumRatio < 0 Then
MyMsgBox ("Humidity ratio cannot be negative")
GoTo ErrHandler
End If
BoundedHumRatio = max(HumRatio, MIN_HUM_RATIO)
TDewPoint = GetTDewPointFromHumRatio(TDryBulb, BoundedHumRatio, Pressure)
' Initial guesses
TWetBulbSup = TDryBulb
TWetBulbInf = TDewPoint
TWetBulb = (TWetBulbInf + TWetBulbSup) / 2
' Bisection loop
Tol = GetTol()
index = 0
While ((TWetBulbSup - TWetBulbInf) > Tol)
' Compute humidity ratio at temperature Tstar
Wstar = GetHumRatioFromTWetBulb(TDryBulb, TWetBulb, Pressure)
' Get new bounds
If (Wstar > BoundedHumRatio) Then
TWetBulbSup = TWetBulb
Else
TWetBulbInf = TWetBulb
End If
' New guess of wet bulb temperature
TWetBulb = (TWetBulbSup + TWetBulbInf) / 2
If (index > MAX_ITER_COUNT) Then
GoTo ErrHandler
End If
index = index + 1
Wend
GetTWetBulbFromHumRatio = TWetBulb
Exit Function
ErrHandler:
GetTWetBulbFromHumRatio = CVErr(xlErrNA)
End Function
Function GetHumRatioFromTWetBulb(ByVal TDryBulb As Variant, ByVal TWetBulb As Variant, ByVal Pressure As Variant) As Variant
'
' Return humidity ratio given dry-bulb temperature, wet-bulb temperature, and pressure.
'
' Args:
' TDryBulb : Dry-bulb temperature in °F [IP] or °C [SI]
' TWetBulb : Wet-bulb temperature in °F [IP] or °C [SI]
' Pressure : Atmospheric pressure in Psi [IP] or Pa [SI]
'
' Returns:
' Humidity ratio in lb_H2O/lb_Air [IP] or kg_H2O/kg_Air [SI]
'
' Reference:
' ASHRAE Handbook - Fundamentals (2017) ch. 1 eqn 33 and 35
Dim Wsstar As Variant, HumRatio As Variant
Wsstar = GetSatHumRatio(TWetBulb, Pressure)
On Error GoTo ErrHandler
If TWetBulb > TDryBulb Then
MyMsgBox ("Wet bulb temperature is above dry bulb temperature")
GoTo ErrHandler
End If
If isIP() Then
If (TWetBulb >= FREEZING_POINT_WATER_IP) Then
HumRatio = ((1093 - 0.556 * TWetBulb) * Wsstar - 0.24 * (TDryBulb - TWetBulb)) / (1093 + 0.444 * TDryBulb - TWetBulb)
Else
HumRatio = ((1220 - 0.04 * TWetBulb) * Wsstar - 0.24 * (TDryBulb - TWetBulb)) / (1220 + 0.444 * TDryBulb - 0.48 * TWetBulb)
End If
Else
If (TWetBulb >= FREEZING_POINT_WATER_SI) Then
HumRatio = ((2501 - 2.326 * TWetBulb) * Wsstar - 1.006 * (TDryBulb - TWetBulb)) / (2501 + 1.86 * TDryBulb - 4.186 * TWetBulb)
Else
HumRatio = ((2830 - 0.24 * TWetBulb) * Wsstar - 1.006 * (TDryBulb - TWetBulb)) / (2830 + 1.86 * TDryBulb - 2.1 * TWetBulb)
End If
End If
' Validity check.
GetHumRatioFromTWetBulb = max(HumRatio, MIN_HUM_RATIO)
Exit Function
ErrHandler:
GetHumRatioFromTWetBulb = CVErr(xlErrNA)
End Function
Function GetHumRatioFromRelHum(ByVal TDryBulb As Variant, ByVal RelHum As Variant, ByVal Pressure As Variant) As Variant
'
' Return humidity ratio given dry-bulb temperature, relative humidity, and pressure.
'
' Args:
' TDryBulb : Dry-bulb temperature in °F [IP] or °C [SI]
' RelHum : Relative humidity in range [0, 1]
' Pressure : Atmospheric pressure in Psi [IP] or Pa [SI]
'
' Returns:
' Humidity ratio in lb_H2O/lb_Air [IP] or kg_H2O/kg_Air [SI]
'
' Reference:
' ASHRAE Handbook - Fundamentals (2017) ch. 1
'
Dim VapPres As Variant
On Error GoTo ErrHandler
If RelHum < 0 Or RelHum > 1 Then
MyMsgBox ("Relative humidity is outside range [0, 1]")
GoTo ErrHandler
End If
VapPres = GetVapPresFromRelHum(TDryBulb, RelHum)
GetHumRatioFromRelHum = GetHumRatioFromVapPres(VapPres, Pressure)
Exit Function
ErrHandler:
GetHumRatioFromRelHum = CVErr(xlErrNA)
End Function
Function GetRelHumFromHumRatio(ByVal TDryBulb As Variant, ByVal HumRatio As Variant, ByVal Pressure As Variant) As Variant
'
' Return relative humidity given dry-bulb temperature, humidity ratio, and pressure.
'
' Args:
' TDryBulb : Dry-bulb temperature in °F [IP] or °C [SI]
' HumRatio : Humidity ratio in lb_H2O/lb_Air [IP] or kg_H2O/kg_Air [SI]
' Pressure : Atmospheric pressure in Psi [IP] or Pa [SI]
'
' Returns:
' Relative humidity in range [0, 1]
'
' Reference:
' ASHRAE Handbook - Fundamentals (2017) ch. 1
'
Dim VapPres As Variant
On Error GoTo ErrHandler
If HumRatio < 0 Then
MyMsgBox ("Humidity ratio is negative")
GoTo ErrHandler
End If
VapPres = GetVapPresFromHumRatio(HumRatio, Pressure)
GetRelHumFromHumRatio = GetRelHumFromVapPres(TDryBulb, VapPres)
Exit Function
ErrHandler:
GetRelHumFromHumRatio = CVErr(xlErrNA)
End Function
Function GetHumRatioFromTDewPoint(ByVal TDewPoint As Variant, ByVal Pressure As Variant) As Variant
'
' Return humidity ratio given dew-point temperature and pressure.
'
' Args:
' TDewPoint : Dew-point temperature in °F [IP] or °C [SI]
' Pressure : Atmospheric pressure in Psi [IP] or Pa [SI]
'
' Returns:
' Humidity ratio in lb_H2O/lb_Air [IP] or kg_H2O/kg_Air [SI]
'
' Reference:
' ASHRAE Handbook - Fundamentals (2017) ch. 1 eqn 13
'
Dim VapPres As Variant
On Error GoTo ErrHandler
VapPres = GetSatVapPres(TDewPoint)
GetHumRatioFromTDewPoint = GetHumRatioFromVapPres(VapPres, Pressure)
Exit Function
ErrHandler:
GetHumRatioFromTDewPoint = CVErr(xlErrNA)
End Function
Function GetTDewPointFromHumRatio(ByVal TDryBulb As Variant, ByVal HumRatio As Variant, ByVal Pressure As Variant) As Variant
'
' Return dew-point temperature given dry-bulb temperature, humidity ratio, and pressure.
'
' Args:
' TDryBulb : Dry-bulb temperature in °F [IP] or °C [SI]
' HumRatio : Humidity ratio in lb_H2O/lb_Air [IP] or kg_H2O/kg_Air [SI]
' Pressure : Atmospheric pressure in Psi [IP] or Pa [SI]
'
' Returns:
' Dew-point temperature in °F [IP] or °C [SI]
'
' Reference:
' ASHRAE Handbook - Fundamentals (2017) ch. 1
'
Dim VapPres As Variant
On Error GoTo ErrHandler
If HumRatio < 0 Then
MyMsgBox ("Humidity ratio is negative")
GoTo ErrHandler
End If
VapPres = GetVapPresFromHumRatio(HumRatio, Pressure)
GetTDewPointFromHumRatio = GetTDewPointFromVapPres(TDryBulb, VapPres)
Exit Function
ErrHandler:
GetTDewPointFromHumRatio = CVErr(xlErrNA)
End Function
'******************************************************************************************************
' Conversions between humidity ratio and vapor pressure
'******************************************************************************************************
Function GetHumRatioFromVapPres(ByVal VapPres As Variant, ByVal Pressure As Variant) As Variant
'
' Return humidity ratio given water vapor pressure and atmospheric pressure.
'
' Args:
' VapPres : Partial pressure of water vapor in moist air in Psi [IP] or Pa [SI]
' Pressure : Atmospheric pressure in Psi [IP] or Pa [SI]
'
' Returns:
' Humidity ratio in lb_H2O/lb_Air [IP] or kg_H2O/kg_Air [SI]
'
' Reference:
' ASHRAE Handbook - Fundamentals (2017) ch. 1 eqn 20
'
Dim HumRatio As Variant
On Error GoTo ErrHandler
If VapPres < 0 Then
MyMsgBox ("Partial pressure of water vapor in moist air is negative")
GoTo ErrHandler
End If
HumRatio = 0.621945 * VapPres / (Pressure - VapPres)
' Validity check.
GetHumRatioFromVapPres = max(HumRatio, MIN_HUM_RATIO)
Exit Function
ErrHandler:
GetHumRatioFromVapPres = CVErr(xlErrNA)
End Function
Function GetVapPresFromHumRatio(ByVal HumRatio As Variant, ByVal Pressure As Variant) As Variant
'
' Return vapor pressure given humidity ratio and pressure.
'
' Args:
' HumRatio : Humidity ratio in lb_H2O/lb_Air [IP] or kg_H2O/kg_Air [SI]
' Pressure : Atmospheric pressure in Psi [IP] or Pa [SI]