| 1261 | } |
| 1262 | |
| 1263 | double DewpointTemperature(double T, double p, double psi_w) { |
| 1264 | int iter = 0; |
| 1265 | double p_w = NAN, eps = NAN, resid = NAN, Tdp = 0, x1 = 0, x2 = 0, x3 = NAN, y1 = 0, y2 = NAN, T0 = NAN; |
| 1266 | double p_ws_dp = NAN, f_dp = NAN; |
| 1267 | |
| 1268 | // Make sure it isn't dry air, return an impossible temperature otherwise |
| 1269 | if ((1 - psi_w) < 1e-16) { |
| 1270 | return -1; |
| 1271 | } |
| 1272 | // ------------------------------------------ |
| 1273 | // Iteratively find the dewpoint temperature |
| 1274 | // ------------------------------------------ |
| 1275 | |
| 1276 | // The highest dewpoint temperature possible is the dry-bulb temperature. |
| 1277 | // When they are equal, the air is saturated (R=1) |
| 1278 | |
| 1279 | p_w = psi_w * p; |
| 1280 | |
| 1281 | // 611.65... is the triple point pressure of water in Pa |
| 1282 | if (p_w > 611.6547241637944) { |
| 1283 | T0 = IF97::Tsat97(p) - 1; |
| 1284 | } else { |
| 1285 | T0 = 268; |
| 1286 | } |
| 1287 | // A good guess for Tdp is that enhancement factor is unity, which yields |
| 1288 | // p_w_s = p_w, and get guess for T from saturation temperature |
| 1289 | |
| 1290 | iter = 1; |
| 1291 | eps = 1e-5; |
| 1292 | resid = 999; |
| 1293 | while ((iter <= 3 || std::abs(resid) > eps) && iter < 100) { |
| 1294 | if (iter == 1) { |
| 1295 | x1 = T0; |
| 1296 | Tdp = x1; |
| 1297 | } |
| 1298 | if (iter == 2) { |
| 1299 | x2 = x1 + 0.1; |
| 1300 | Tdp = x2; |
| 1301 | } |
| 1302 | if (iter > 2) { |
| 1303 | Tdp = x2; |
| 1304 | } |
| 1305 | |
| 1306 | if (Tdp >= 273.16) { |
| 1307 | // Saturation pressure at dewpoint [Pa] |
| 1308 | p_ws_dp = IF97::psat97(Tdp); |
| 1309 | } else { |
| 1310 | // Sublimation pressure at icepoint [Pa] |
| 1311 | p_ws_dp = psub_Ice(Tdp); |
| 1312 | } |
| 1313 | // Enhancement Factor at dewpoint temperature [-] |
| 1314 | f_dp = f_factor(Tdp, p); |
| 1315 | // Error between target and actual pressure [Pa] |
| 1316 | resid = p_w - p_ws_dp * f_dp; |
| 1317 | |
| 1318 | if (iter == 1) { |
| 1319 | y1 = resid; |
| 1320 | } |
no test coverage detected