Skip to content
Closed
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
00a9d48
fortran: tighten validity checks
dmey Feb 14, 2019
295f1b0
fix typo and add max iter count
dmey Feb 18, 2019
9bc075c
fix fortran
dmey Feb 23, 2019
d335075
C: tighten val checks
dmey Feb 23, 2019
08e8586
fix c
dmey Feb 23, 2019
e3dfe51
js: tighten up val checks
dmey Feb 23, 2019
fc06d99
fix fortran
dmey Feb 23, 2019
c11a5a4
fix fortran
dmey Feb 23, 2019
7df076f
fix logic
dmey Feb 23, 2019
1f2297c
VBA: tighten val checks
dmey Feb 24, 2019
72f5f63
add test for clamping humratio to 10e-7
dmey Feb 24, 2019
155670f
minor: 1E-7 -> 1e-7
dmey Feb 24, 2019
7cd1bed
start index at 0 on all langs
dmey Feb 24, 2019
7b09d0d
reduce value of MIN_ITER_COUNT: 5 -> 3.
dmey Feb 24, 2019
d24a332
remove MIN_ITER_COUNT
dmey Mar 4, 2019
52232e2
fix convergence in GetTDewPointFromVapPres
dmey Mar 12, 2019
4573c17
typo
dmey Mar 12, 2019
e1feda8
Merge remote-tracking branch 'origin/master' into dmey/tighten-validi…
dmey Mar 16, 2019
398c83b
GetTDewPointFromVapPres: add simple test to check convergence (#27)
dmey Mar 16, 2019
2983b23
add conv tests for js, py, f and c
dmey Mar 16, 2019
bc4393c
remove discontinuity code in saturated pressure func
dmey Mar 16, 2019
5cbae56
py finish adding BoundedHumRatio + modify range in tests for conv in IP
dmey Mar 17, 2019
e9519e2
fix js tests by inreasing the timelimit in mocha from 2 to 40 s
dmey Mar 17, 2019
05c5fef
C: integrate chnages from python version
dmey Mar 17, 2019
65fba5f
F, c, js: integrate changes from python version [wip]
dmey Mar 18, 2019
d56465a
fortran make loop in GetTDewPointFromVapPres same in other impl
dmey Mar 19, 2019
4f63e28
Merge remote-tracking branch 'origin/master' into dmey/tighten-validi…
dmey Mar 19, 2019
474e724
Add convergence test for VBA/Excel (#28)
didierthevenard Mar 19, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
js: tighten up val checks
  • Loading branch information
dmey committed Feb 23, 2019
commit e3dfe51df49819839d75ea9189967c7901673703
84 changes: 60 additions & 24 deletions src/js/psychrolib.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,18 @@ function Psychrometrics() {
* Global constants
*****************************************************************************************************/

var R_DA_IP = 53.350; // Universal gas constant for dry air (IP version) in ft lb_Force lb_DryAir⁻¹ R⁻¹
// Reference: ASHRAE Handbook - Fundamentals (2017) ch. 1
var R_DA_SI = 287.042; // Universal gas constant for dry air (SI version) in J kg_DryAir⁻¹ K⁻¹
// Reference: ASHRAE Handbook - Fundamentals (2017) ch. 1
var R_DA_IP = 53.350; // Universal gas constant for dry air (IP version) in ft lb_Force lb_DryAir⁻¹ R⁻¹
// Reference: ASHRAE Handbook - Fundamentals (2017) ch. 1
var R_DA_SI = 287.042; // Universal gas constant for dry air (SI version) in J kg_DryAir⁻¹ K⁻¹
// Reference: ASHRAE Handbook - Fundamentals (2017) ch. 1
var INVALID = -99999; // Invalid value (dimensionless)

var INVALID = -99999; // Invalid value (dimensionless)
var MIN_ITER_COUNT = 5 // Minimum number of iterations before exiting while loops.

var MAX_ITER_COUNT = 1E+3 // Maximum number of iterations before exiting while loops.

var 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.


/******************************************************************************************************
Expand Down Expand Up @@ -292,6 +298,7 @@ function Psychrometrics() {
var Tdp_c; // Value of Tdp used in NR calculation
var lnVP_c; // Value of log of vapor water pressure used in NR calculation
var d_Tdp; // Value of temperature step used in NR calculation
var index = 0;
do
{
// Current point
Expand All @@ -310,8 +317,9 @@ function Psychrometrics() {
Tdp = Tdp_c - (lnVP_c - lnVP) / d_lnVP;
Tdp = max(Tdp, _BOUNDS[0]);
Tdp = min(Tdp, _BOUNDS[1]);
index = index + 1;
}
while (abs(Tdp - Tdp_c) > PSYCHROLIB_TOLERANCE);
while (((abs(Tdp - Tdp_c) > PSYCHROLIB_TOLERANCE || (index < MIN_ITER_COUNT)) && (index < MAX_ITER_COUNT)));
return min(Tdp, TDryBulb);
}

Expand All @@ -337,31 +345,34 @@ function Psychrometrics() {
) {
// Declarations
var Wstar;
var TDewPoint, TWetBulb, TWetBulbSup, TWetBulbInf;
var TDewPoint, TWetBulb, TWetBulbSup, TWetBulbInf, BoundedHumRatio;
var index = 1;

if (!(HumRatio >= 0.))
throw new Error("Humidity ratio is negative");
BoundedHumRatio = max(HumRatio, MIN_HUM_RATIO);

TDewPoint = this.GetTDewPointFromHumRatio(TDryBulb, HumRatio, Pressure);
TDewPoint = this.GetTDewPointFromHumRatio(TDryBulb, BoundedHumRatio, Pressure);

// Initial guesses
TWetBulbSup = TDryBulb;
TWetBulbInf = TDewPoint;
TWetBulb = (TWetBulbInf + TWetBulbSup) / 2.;

// Bisection loop
while (TWetBulbSup - TWetBulbInf > PSYCHROLIB_TOLERANCE) {
while (((TWetBulbSup - TWetBulbInf > PSYCHROLIB_TOLERANCE || (index < MIN_ITER_COUNT)) && (index < MAX_ITER_COUNT))); {
// Compute humidity ratio at temperature Tstar
Wstar = this.GetHumRatioFromTWetBulb(TDryBulb, TWetBulb, Pressure);

// Get new bounds
if (Wstar > HumRatio)
if (Wstar > BoundedHumRatio)
TWetBulbSup = TWetBulb;
else
TWetBulbInf = TWetBulb;

// New guess of wet bulb temperature
TWetBulb = (TWetBulbSup + TWetBulbInf) / 2.;
index = index + 1;
}

return TWetBulb;
Expand Down Expand Up @@ -400,8 +411,8 @@ function Psychrometrics() {
HumRatio = ((2830. - 0.24 * TWetBulb) * Wsstar - 1.006 * (TDryBulb - TWetBulb))
/ (2830. + 1.86 * TDryBulb - 2.1 * TWetBulb);
}

return HumRatio;
// Validity check.
return max(HumRatio, MIN_HUM_RATIO);
}

// Return humidity ratio given dry-bulb temperature, relative humidity, and pressure.
Expand Down Expand Up @@ -475,10 +486,15 @@ function Psychrometrics() {
( VapPres // (i) Partial pressure of water vapor in moist air in Psi [IP] or Pa [SI]
, Pressure // (i) Atmospheric pressure in Psi [IP] or Pa [SI]
) {
var HumRatio;

if (!(VapPres >= 0.))
throw new Error("Partial pressure of water vapor in moist air is negative");

return 0.621945 * VapPres / (Pressure - VapPres);
HumRatio = 0.621945 * VapPres / (Pressure - VapPres);

// Validity check.
return max(HumRatio, MIN_HUM_RATIO);
}

// Return vapor pressure given humidity ratio and pressure.
Expand All @@ -487,12 +503,13 @@ function Psychrometrics() {
( HumRatio // (i) Humidity ratio in lb_H₂O lb_Air⁻¹ [IP] or kg_H₂O kg_Air⁻¹ [SI]
, Pressure // (i) Atmospheric pressure in Psi [IP] or Pa [SI]
) {
var VapPres;
var VapPres, BoundedHumRatio;

if (!(HumRatio >= 0.))
throw new Error("Humidity ratio is negative");
BoundedHumRatio = max(HumRatio, MIN_HUM_RATIO);

VapPres = Pressure * HumRatio / (0.621945 + HumRatio);
VapPres = Pressure * BoundedHumRatio / (0.621945 + BoundedHumRatio);
return VapPres;
}

Expand All @@ -517,10 +534,15 @@ function Psychrometrics() {
this.GetHumRatioFromSpecificHum = function // (o) Humidity ratio in lb_H₂O lb_Dry_Air⁻¹ [IP] or kg_H₂O kg_Dry_Air⁻¹ [SI]
( SpecificHum // (i) Specific humidity ratio in lb_H₂O lb_Air⁻¹ [IP] or kg_H₂O kg_Air⁻¹ [SI]
) {
var HumRatio;

if (!(SpecificHum >= 0.0 && SpecificHum < 1.0))
throw new Error("Specific humidity is outside range [0, 1[");

return SpecificHum / (1.0 - SpecificHum);
HumRatio = SpecificHum / (1.0 - SpecificHum);

// Validity check
return max(HumRatio, MIN_HUM_RATIO);
}


Expand Down Expand Up @@ -622,10 +644,13 @@ function Psychrometrics() {
( TDryBulb // (i) Dry bulb temperature in °F [IP] or °C [SI]
, Pressure // (i) Atmospheric pressure in Psi [IP] or Pa [SI]
) {
var SatVaporPres;
var SatVaporPres, SatHumRatio;

SatVaporPres = this.GetSatVapPres(TDryBulb);
return 0.621945 * SatVaporPres / (Pressure - SatVaporPres);
SatHumRatio = 0.621945 * SatVaporPres / (Pressure - SatVaporPres);

// Validity check.
return max(SatHumRatio, MIN_HUM_RATIO);
}

// Return saturated air enthalpy given dry-bulb temperature and pressure.
Expand Down Expand Up @@ -667,11 +692,13 @@ function Psychrometrics() {
, HumRatio // (i) Humidity ratio in lb_H₂O lb_Air⁻¹ [IP] or kg_H₂O kg_Air⁻¹ [SI]
, Pressure // (i) Atmospheric pressure in Psi [IP] or Pa [SI]
) {
var BoundedHumRatio;

if (!(HumRatio >= 0.))
throw new Error("Humidity ratio is negative");
BoundedHumRatio = max(HumRatio, MIN_HUM_RATIO);

return HumRatio / this.GetSatHumRatio(TDryBulb, Pressure);
return BoundedHumRatio / this.GetSatHumRatio(TDryBulb, Pressure);
}

// Return moist air enthalpy given dry-bulb temperature and humidity ratio.
Expand All @@ -680,11 +707,16 @@ function Psychrometrics() {
( TDryBulb // (i) Dry bulb temperature in °F [IP] or °C [SI]
, HumRatio // (i) Humidity ratio in lb_H₂O lb_Air⁻¹ [IP] or kg_H₂O kg_Air⁻¹ [SI]
) {
var BoundedHumRatio;

if (!(HumRatio >= 0.))
throw new Error("Humidity ratio is negative");
BoundedHumRatio = max(HumRatio, MIN_HUM_RATIO);

if (this.isIP())
return 0.240 * TDryBulb + HumRatio * (1061. + 0.444 * TDryBulb);
return 0.240 * TDryBulb + BoundedHumRatio * (1061. + 0.444 * TDryBulb);
else
return (1.006 * TDryBulb + HumRatio * (2501. + 1.86 * TDryBulb)) * 1000.;
return (1.006 * TDryBulb + BoundedHumRatio * (2501. + 1.86 * TDryBulb)) * 1000.;
}

// Return moist air specific volume given dry-bulb temperature, humidity ratio, and pressure.
Expand All @@ -696,14 +728,16 @@ function Psychrometrics() {
, HumRatio // (i) Humidity ratio in lb_H₂O lb_Air⁻¹ [IP] or kg_H₂O kg_Air⁻¹ [SI]
, Pressure // (i) Atmospheric pressure in Psi [IP] or Pa [SI]
) {
var BoundedHumRatio;

if (!(HumRatio >= 0.))
throw new Error("Humidity ratio is negative");
BoundedHumRatio = max(HumRatio, MIN_HUM_RATIO);

if (this.isIP())
return R_DA_IP * this.GetTRankineFromTFahrenheit(TDryBulb) * (1. + 1.607858 * HumRatio) / (144. * Pressure);
return R_DA_IP * this.GetTRankineFromTFahrenheit(TDryBulb) * (1. + 1.607858 * BoundedHumRatio) / (144. * Pressure);
else
return R_DA_SI * this.GetTKelvinFromTCelsius(TDryBulb) * (1. + 1.607858 * HumRatio) / Pressure;
return R_DA_SI * this.GetTKelvinFromTCelsius(TDryBulb) * (1. + 1.607858 * BoundedHumRatio) / Pressure;
}

// Return moist air density given humidity ratio, dry bulb temperature, and pressure.
Expand All @@ -713,11 +747,13 @@ function Psychrometrics() {
, HumRatio // (i) Humidity ratio in lb_H₂O lb_Air⁻¹ [IP] or kg_H₂O kg_Air⁻¹ [SI]
, Pressure // (i) Atmospheric pressure in Psi [IP] or Pa [SI]
) {
var BoundedHumRatio;

if (!(HumRatio >= 0.))
throw new Error("Humidity ratio is negative");
BoundedHumRatio = max(HumRatio, MIN_HUM_RATIO);

return (1. + HumRatio) / this.GetMoistAirVolume(TDryBulb, HumRatio, Pressure);
return (1. + BoundedHumRatio) / this.GetMoistAirVolume(TDryBulb, BoundedHumRatio, Pressure);
}


Expand Down