| 1 | // environment.cxx -- routines to model the natural environment |
| 2 | // |
| 3 | // Written by David Megginson, started February 2002. |
| 4 | // |
| 5 | // Copyright (C) 2002 David Megginson - [email protected] |
| 6 | // |
| 7 | // This program is free software; you can redistribute it and/or |
| 8 | // modify it under the terms of the GNU General Public License as |
| 9 | // published by the Free Software Foundation; either version 2 of the |
| 10 | // License, or (at your option) any later version. |
| 11 | // |
| 12 | // This program is distributed in the hope that it will be useful, but |
| 13 | // WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | // General Public License for more details. |
| 16 | // |
| 17 | // You should have received a copy of the GNU General Public License |
| 18 | // along with this program; if not, write to the Free Software |
| 19 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 20 | // |
| 21 | |
| 22 | #ifdef HAVE_CONFIG_H |
| 23 | # include <config.h> |
| 24 | #endif |
| 25 | |
| 26 | #include <cmath> |
| 27 | |
| 28 | #include <simgear/props/props.hxx> |
| 29 | #include <simgear/math/SGMath.hxx> |
| 30 | |
| 31 | #include <Main/fg_props.hxx> |
| 32 | |
| 33 | #include "environment.hxx" |
| 34 | #include "atmosphere.hxx" |
| 35 | |
| 36 | |
| 37 | //////////////////////////////////////////////////////////////////////// |
| 38 | // Atmosphere model. |
| 39 | //////////////////////////////////////////////////////////////////////// |
| 40 | |
| 41 | #ifdef USING_TABLES |
| 42 | |
| 43 | // Calculated based on the ISA standard day, as found at e.g. |
| 44 | // http://www.av8n.com/physics/altimetry.htm |
| 45 | |
| 46 | // Each line of data has 3 elements: |
| 47 | // Elevation (ft), |
| 48 | // temperature factor (dimensionless ratio of absolute temp), |
| 49 | // pressure factor (dimensionless ratio) |
| 50 | static double atmosphere_data[][3] = { |
| 51 | { -3000.00, 1.021, 1.1133 }, |
| 52 | { 0.00, 1.000, 1.0000 }, |
| 53 | { 2952.76, 0.980, 0.8978 }, |
| 54 | { 5905.51, 0.959, 0.8042 }, |
| 55 | { 8858.27, 0.939, 0.7187 }, |
| 56 | { 11811.02, 0.919, 0.6407 }, |
| 57 | { 14763.78, 0.898, 0.5697 }, |
| 58 | { 17716.54, 0.878, 0.5052 }, |
| 59 | { 20669.29, 0.858, 0.4468 }, |
| 60 | { 23622.05, 0.838, 0.3940 }, |
| 61 | { 26574.80, 0.817, 0.3463 }, |
| 62 | { 29527.56, 0.797, 0.3034 }, |
| 63 | { 32480.31, 0.777, 0.2649 }, |
| 64 | { 35433.07, 0.756, 0.2305 }, |
| 65 | { 38385.83, 0.752, 0.2000 }, |
| 66 | { 41338.58, 0.752, 0.1736 }, |
| 67 | { 44291.34, 0.752, 0.1506 }, |
| 68 | { 47244.09, 0.752, 0.1307 }, |
| 69 | { 50196.85, 0.752, 0.1134 }, |
| 70 | { 53149.61, 0.752, 0.0984 }, |
| 71 | { 56102.36, 0.752, 0.0854 }, |
| 72 | { 59055.12, 0.752, 0.0741 }, |
| 73 | { 62007.87, 0.752, 0.0643 }, |
| 74 | { 65000.00, 0.752, 0.0557 }, |
| 75 | { 68000.00, 0.754, 0.0482 }, |
| 76 | { 71000.00, 0.758, 0.0418 }, |
| 77 | { 74000.00, 0.761, 0.0362 }, |
| 78 | { 77000.00, 0.764, 0.0314 }, |
| 79 | { 80000.00, 0.767, 0.0273 }, |
| 80 | { 83000.00, 0.770, 0.0237 }, |
| 81 | { 86000.00, 0.773, 0.0206 }, |
| 82 | { 89000.00, 0.777, 0.0179 }, |
| 83 | { 92000.00, 0.780, 0.0156 }, |
| 84 | { 95000.00, 0.783, 0.0135 }, |
| 85 | { 98000.00, 0.786, 0.0118 }, |
| 86 | { 101000.00, 0.789, 0.0103 }, |
| 87 | { -1, -1, -1 } |
| 88 | }; |
| 89 | |
| 90 | static SGInterpTable * _temperature_degc_table = 0; |
| 91 | static SGInterpTable * _pressure_inhg_table = 0; |
| 92 | |
| 93 | static void |
| 94 | _setup_tables () |
| 95 | { |
| 96 | if (_temperature_degc_table != 0) |
| 97 | return; |
| 98 | |
| 99 | _temperature_degc_table = new SGInterpTable; |
| 100 | _pressure_inhg_table = new SGInterpTable; |
| 101 | |
| 102 | for (int i = 0; atmosphere_data[i][0] != -1; i++) { |
| 103 | _temperature_degc_table->addEntry(atmosphere_data[i][0], |
| 104 | atmosphere_data[i][1]); |
| 105 | _pressure_inhg_table->addEntry(atmosphere_data[i][0], |
| 106 | atmosphere_data[i][2]); |
| 107 | } |
| 108 | } |
| 109 | #endif |
| 110 | |
| 111 | |
| 112 | //////////////////////////////////////////////////////////////////////// |
| 113 | // Implementation of FGEnvironment. |
| 114 | //////////////////////////////////////////////////////////////////////// |
| 115 | |
| 116 | void FGEnvironment::_init() |
| 117 | { |
| 118 | is_isa = false; |
| 119 | live_update = false; |
| 120 | elevation_ft = 0; |
| 121 | visibility_m = 32000; |
| 122 | temperature_sea_level_degc = 15; |
| 123 | temperature_degc = 15; |
| 124 | dewpoint_sea_level_degc = 5; // guess |
| 125 | dewpoint_degc = 5; |
| 126 | pressure_sea_level_inhg = 29.92; |
| 127 | pressure_inhg = 29.92; |
| 128 | density_slugft3 = 0; |
| 129 | turbulence_magnitude_norm = 0; |
| 130 | turbulence_rate_hz = 1; |
| 131 | wind_from_heading_deg = 0; |
| 132 | wind_speed_kt = 0; |
| 133 | wind_from_north_fps = 0; |
| 134 | wind_from_east_fps = 0; |
| 135 | wind_from_down_fps = 0; |
| 136 | altitude_half_to_sun_m = 1000; |
| 137 | altitude_tropo_top_m = 10000; |
| 138 | #ifdef USING_TABLES |
| 139 | _setup_tables(); |
| 140 | #endif |
| 141 | _recalc_density(); |
| 142 | _recalc_relative_humidity(); |
| 143 | live_update = true; |
| 144 | } |
| 145 | |
| 146 | FGEnvironment::FGEnvironment() |
| 147 | { |
| 148 | _init(); |
| 149 | } |
| 150 | |
| 151 | FGEnvironment::FGEnvironment (const FGEnvironment &env) |
| 152 | { |
| 153 | _init(); |
| 154 | copy(env); |
| 155 | } |
| 156 | |
| 157 | FGEnvironment::~FGEnvironment() |
| 158 | { |
| 159 | Untie(); |
| 160 | } |
| 161 | |
| 162 | FGEnvironment & FGEnvironment::operator = ( const FGEnvironment & other ) |
| 163 | { |
| 164 | copy( other ); |
| 165 | return *this; |
| 166 | } |
| 167 | |
| 168 | void |
| 169 | FGEnvironment::copy (const FGEnvironment &env) |
| 170 | { |
| 171 | elevation_ft = env.elevation_ft; |
| 172 | visibility_m = env.visibility_m; |
| 173 | temperature_sea_level_degc = env.temperature_sea_level_degc; |
| 174 | temperature_degc = env.temperature_degc; |
| 175 | dewpoint_sea_level_degc = env.dewpoint_sea_level_degc; |
| 176 | dewpoint_degc = env.dewpoint_degc; |
| 177 | pressure_sea_level_inhg = env.pressure_sea_level_inhg; |
| 178 | wind_from_heading_deg = env.wind_from_heading_deg; |
| 179 | wind_speed_kt = env.wind_speed_kt; |
| 180 | wind_from_north_fps = env.wind_from_north_fps; |
| 181 | wind_from_east_fps = env.wind_from_east_fps; |
| 182 | wind_from_down_fps = env.wind_from_down_fps; |
| 183 | turbulence_magnitude_norm = env.turbulence_magnitude_norm; |
| 184 | turbulence_rate_hz = env.turbulence_rate_hz; |
| 185 | pressure_inhg = env.pressure_inhg; |
| 186 | density_slugft3 = env.density_slugft3; |
| 187 | density_tropo_avg_kgm3 = env.density_tropo_avg_kgm3; |
| 188 | relative_humidity = env.relative_humidity; |
| 189 | altitude_half_to_sun_m = env.altitude_half_to_sun_m; |
| 190 | altitude_tropo_top_m = env.altitude_tropo_top_m; |
| 191 | live_update = env.live_update; |
| 192 | is_isa = env.is_isa; |
| 193 | } |
| 194 | |
| 195 | static inline bool |
| 196 | maybe_copy_value (FGEnvironment * env, const SGPropertyNode * node, |
| 197 | const char * name, void (FGEnvironment::*setter)(double)) |
| 198 | { |
| 199 | const SGPropertyNode * child = node->getNode(name); |
| 200 | // fragile: depends on not being typed |
| 201 | // as a number |
| 202 | if (child != 0 && child->hasValue() && |
| 203 | child->getStringValue()[0] != '\0') { |
| 204 | (env->*setter)(child->getDoubleValue()); |
| 205 | return true; |
| 206 | } else { |
| 207 | return false; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | void |
| 212 | FGEnvironment::read (const SGPropertyNode * node) |
| 213 | { |
| 214 | bool live_update = set_live_update( false ); |
| 215 | maybe_copy_value(this, node, "visibility-m", |
| 216 | &FGEnvironment::set_visibility_m); |
| 217 | |
| 218 | maybe_copy_value(this, node, "elevation-ft", |
| 219 | &FGEnvironment::set_elevation_ft); |
| 220 | |
| 221 | if (!maybe_copy_value(this, node, "temperature-sea-level-degc", |
| 222 | &FGEnvironment::set_temperature_sea_level_degc)) { |
| 223 | if( maybe_copy_value(this, node, "temperature-degc", |
| 224 | &FGEnvironment::set_temperature_degc)) { |
| 225 | _recalc_sl_temperature(); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | if (!maybe_copy_value(this, node, "dewpoint-sea-level-degc", |
| 230 | &FGEnvironment::set_dewpoint_sea_level_degc)) { |
| 231 | if( maybe_copy_value(this, node, "dewpoint-degc", |
| 232 | &FGEnvironment::set_dewpoint_degc)) { |
| 233 | _recalc_sl_dewpoint(); |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | if (!maybe_copy_value(this, node, "pressure-sea-level-inhg", |
| 238 | &FGEnvironment::set_pressure_sea_level_inhg)) { |
| 239 | if( maybe_copy_value(this, node, "pressure-inhg", |
| 240 | &FGEnvironment::set_pressure_inhg)) { |
| 241 | _recalc_sl_pressure(); |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | maybe_copy_value(this, node, "wind-from-heading-deg", |
| 246 | &FGEnvironment::set_wind_from_heading_deg); |
| 247 | |
| 248 | maybe_copy_value(this, node, "wind-speed-kt", |
| 249 | &FGEnvironment::set_wind_speed_kt); |
| 250 | |
| 251 | maybe_copy_value(this, node, "turbulence/magnitude-norm", |
| 252 | &FGEnvironment::set_turbulence_magnitude_norm); |
| 253 | |
| 254 | maybe_copy_value(this, node, "turbulence/rate-hz", |
| 255 | &FGEnvironment::set_turbulence_rate_hz); |
| 256 | |
| 257 | const SGPropertyNode* child = node->getNode("atmosphere/is-isa"); |
| 258 | if (child != 0 && child->hasValue()) { |
| 259 | set_is_isa(child->getBoolValue()); |
| 260 | } |
| 261 | |
| 262 | // calculate derived properties here to avoid duplicate expensive computations |
| 263 | _recalc_ne(); |
| 264 | _recalc_alt_pt(); |
| 265 | _recalc_alt_dewpoint(); |
| 266 | _recalc_density(); |
| 267 | _recalc_relative_humidity(); |
| 268 | |
| 269 | set_live_update(live_update); |
| 270 | } |
| 271 | |
| 272 | void FGEnvironment::Tie( SGPropertyNode_ptr base, bool archivable ) |
| 273 | { |
| 274 | _tiedProperties.setRoot( base ); |
| 275 | |
| 276 | _tiedProperties.Tie( "visibility-m", this, |
| 277 | &FGEnvironment::get_visibility_m, |
| 278 | &FGEnvironment::set_visibility_m); |
| 279 | |
| 280 | _tiedProperties.Tie("elevation-ft", this, |
| 281 | &FGEnvironment::get_elevation_ft, |
| 282 | &FGEnvironment::set_elevation_ft); |
| 283 | |
| 284 | _tiedProperties.Tie("temperature-sea-level-degc", this, |
| 285 | &FGEnvironment::get_temperature_sea_level_degc, |
| 286 | &FGEnvironment::set_temperature_sea_level_degc); |
| 287 | |
| 288 | _tiedProperties.Tie("temperature-degc", this, |
| 289 | &FGEnvironment::get_temperature_degc, |
| 290 | &FGEnvironment::set_temperature_degc); |
| 291 | |
| 292 | _tiedProperties.Tie("dewpoint-sea-level-degc", this, |
| 293 | &FGEnvironment::get_dewpoint_sea_level_degc, |
| 294 | &FGEnvironment::set_dewpoint_sea_level_degc); |
| 295 | |
| 296 | _tiedProperties.Tie("dewpoint-degc", this, |
| 297 | &FGEnvironment::get_dewpoint_degc, |
| 298 | &FGEnvironment::set_dewpoint_degc); |
| 299 | |
| 300 | _tiedProperties.Tie("pressure-sea-level-inhg", this, |
| 301 | &FGEnvironment::get_pressure_sea_level_inhg, |
| 302 | &FGEnvironment::set_pressure_sea_level_inhg); |
| 303 | |
| 304 | _tiedProperties.Tie("pressure-inhg", this, |
| 305 | &FGEnvironment::get_pressure_inhg, |
| 306 | &FGEnvironment::set_pressure_inhg); |
| 307 | |
| 308 | _tiedProperties.Tie("atmosphere/altitude-half-to-sun", this, |
| 309 | &FGEnvironment::get_altitude_half_to_sun_m, |
| 310 | &FGEnvironment::set_altitude_half_to_sun_m); |
| 311 | |
| 312 | _tiedProperties.Tie("atmosphere/altitude-troposphere-top", this, |
| 313 | &FGEnvironment::get_altitude_tropo_top_m, |
| 314 | &FGEnvironment::set_altitude_tropo_top_m); |
| 315 | |
| 316 | _tiedProperties.Tie("wind-from-heading-deg", this, |
| 317 | &FGEnvironment::get_wind_from_heading_deg, |
| 318 | &FGEnvironment::set_wind_from_heading_deg); |
| 319 | |
| 320 | _tiedProperties.Tie("wind-speed-kt", this, |
| 321 | &FGEnvironment::get_wind_speed_kt, |
| 322 | &FGEnvironment::set_wind_speed_kt); |
| 323 | |
| 324 | _tiedProperties.Tie("wind-from-north-fps", this, |
| 325 | &FGEnvironment::get_wind_from_north_fps, |
| 326 | &FGEnvironment::set_wind_from_north_fps); |
| 327 | |
| 328 | _tiedProperties.Tie("wind-from-east-fps", this, |
| 329 | &FGEnvironment::get_wind_from_east_fps, |
| 330 | &FGEnvironment::set_wind_from_east_fps); |
| 331 | |
| 332 | _tiedProperties.Tie("wind-from-down-fps", this, |
| 333 | &FGEnvironment::get_wind_from_down_fps, |
| 334 | &FGEnvironment::set_wind_from_down_fps); |
| 335 | |
| 336 | _tiedProperties.Tie("turbulence/magnitude-norm", this, |
| 337 | &FGEnvironment::get_turbulence_magnitude_norm, |
| 338 | &FGEnvironment::set_turbulence_magnitude_norm); |
| 339 | |
| 340 | _tiedProperties.Tie("turbulence/rate-hz", this, |
| 341 | &FGEnvironment::get_turbulence_rate_hz, |
| 342 | &FGEnvironment::set_turbulence_rate_hz); |
| 343 | |
| 344 | _tiedProperties.setAttribute( SGPropertyNode::ARCHIVE, archivable ); |
| 345 | |
| 346 | _tiedProperties.Tie("temperature-degf", this, |
| 347 | &FGEnvironment::get_temperature_degf); |
| 348 | |
| 349 | _tiedProperties.Tie("density-slugft3", this, |
| 350 | &FGEnvironment::get_density_slugft3); // read-only |
| 351 | |
| 352 | _tiedProperties.Tie("relative-humidity", this, |
| 353 | &FGEnvironment::get_relative_humidity); //ro |
| 354 | |
| 355 | _tiedProperties.Tie("atmosphere/density-tropo-avg", this, |
| 356 | &FGEnvironment::get_density_tropo_avg_kgm3); //ro |
| 357 | |
| 358 | _tiedProperties.Tie("atmosphere/is-isa", this, |
| 359 | &FGEnvironment::get_is_isa, |
| 360 | &FGEnvironment::set_is_isa); |
| 361 | } |
| 362 | |
| 363 | void FGEnvironment::Untie() |
| 364 | { |
| 365 | _tiedProperties.Untie(); |
| 366 | } |
| 367 | |
| 368 | double |
| 369 | FGEnvironment::get_visibility_m () const |
| 370 | { |
| 371 | return visibility_m; |
| 372 | } |
| 373 | |
| 374 | double |
| 375 | FGEnvironment::get_temperature_sea_level_degc () const |
| 376 | { |
| 377 | return temperature_sea_level_degc; |
| 378 | } |
| 379 | |
| 380 | double |
| 381 | FGEnvironment::get_temperature_degc () const |
| 382 | { |
| 383 | return temperature_degc; |
| 384 | } |
| 385 | |
| 386 | double |
| 387 | FGEnvironment::get_temperature_degf () const |
| 388 | { |
| 389 | return (temperature_degc * 9.0 / 5.0) + 32.0; |
| 390 | } |
| 391 | |
| 392 | double |
| 393 | FGEnvironment::get_dewpoint_sea_level_degc () const |
| 394 | { |
| 395 | return dewpoint_sea_level_degc; |
| 396 | } |
| 397 | |
| 398 | double |
| 399 | FGEnvironment::get_dewpoint_degc () const |
| 400 | { |
| 401 | return dewpoint_degc; |
| 402 | } |
| 403 | |
| 404 | double |
| 405 | FGEnvironment::get_pressure_sea_level_inhg () const |
| 406 | { |
| 407 | return pressure_sea_level_inhg; |
| 408 | } |
| 409 | |
| 410 | double |
| 411 | FGEnvironment::get_pressure_inhg () const |
| 412 | { |
| 413 | return pressure_inhg; |
| 414 | } |
| 415 | |
| 416 | double |
| 417 | FGEnvironment::get_density_slugft3 () const |
| 418 | { |
| 419 | return density_slugft3; |
| 420 | } |
| 421 | |
| 422 | double |
| 423 | FGEnvironment::get_relative_humidity () const |
| 424 | { |
| 425 | return relative_humidity; |
| 426 | } |
| 427 | |
| 428 | double |
| 429 | FGEnvironment::get_density_tropo_avg_kgm3 () const |
| 430 | { |
| 431 | return density_tropo_avg_kgm3; |
| 432 | } |
| 433 | |
| 434 | double |
| 435 | FGEnvironment::get_altitude_half_to_sun_m () const |
| 436 | { |
| 437 | return altitude_half_to_sun_m; |
| 438 | } |
| 439 | |
| 440 | double |
| 441 | FGEnvironment::get_altitude_tropo_top_m () const |
| 442 | { |
| 443 | return altitude_tropo_top_m; |
| 444 | } |
| 445 | |
| 446 | double |
| 447 | FGEnvironment::get_wind_from_heading_deg () const |
| 448 | { |
| 449 | return wind_from_heading_deg; |
| 450 | } |
| 451 | |
| 452 | double |
| 453 | FGEnvironment::get_wind_speed_kt () const |
| 454 | { |
| 455 | return wind_speed_kt; |
| 456 | } |
| 457 | |
| 458 | double |
| 459 | FGEnvironment::get_wind_from_north_fps () const |
| 460 | { |
| 461 | return wind_from_north_fps; |
| 462 | } |
| 463 | |
| 464 | double |
| 465 | FGEnvironment::get_wind_from_east_fps () const |
| 466 | { |
| 467 | return wind_from_east_fps; |
| 468 | } |
| 469 | |
| 470 | double |
| 471 | FGEnvironment::get_wind_from_down_fps () const |
| 472 | { |
| 473 | return wind_from_down_fps; |
| 474 | } |
| 475 | |
| 476 | double |
| 477 | FGEnvironment::get_turbulence_magnitude_norm () const |
| 478 | { |
| 479 | return turbulence_magnitude_norm; |
| 480 | } |
| 481 | |
| 482 | double |
| 483 | FGEnvironment::get_turbulence_rate_hz () const |
| 484 | { |
| 485 | return turbulence_rate_hz; |
| 486 | } |
| 487 | |
| 488 | double |
| 489 | FGEnvironment::get_elevation_ft () const |
| 490 | { |
| 491 | return elevation_ft; |
| 492 | } |
| 493 | |
| 494 | bool FGEnvironment::get_is_isa() const |
| 495 | { |
| 496 | return is_isa; |
| 497 | } |
| 498 | |
| 499 | void |
| 500 | FGEnvironment::set_visibility_m (double v) |
| 501 | { |
| 502 | visibility_m = v; |
| 503 | } |
| 504 | |
| 505 | void |
| 506 | FGEnvironment::set_temperature_sea_level_degc (double t) |
| 507 | { |
| 508 | temperature_sea_level_degc = t; |
| 509 | if (dewpoint_sea_level_degc > t) |
| 510 | dewpoint_sea_level_degc = t; |
| 511 | if( live_update ) { |
| 512 | _recalc_alt_pt(); |
| 513 | _recalc_density(); |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | void |
| 518 | FGEnvironment::set_temperature_degc (double t) |
| 519 | { |
| 520 | temperature_degc = t; |
| 521 | if( live_update ) { |
| 522 | _recalc_sl_temperature(); |
| 523 | _recalc_sl_pressure(); |
| 524 | _recalc_alt_pt(); |
| 525 | _recalc_density(); |
| 526 | _recalc_relative_humidity(); |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | void |
| 531 | FGEnvironment::set_dewpoint_sea_level_degc (double t) |
| 532 | { |
| 533 | dewpoint_sea_level_degc = t; |
| 534 | if (temperature_sea_level_degc < t) |
| 535 | temperature_sea_level_degc = t; |
| 536 | if( live_update ) { |
| 537 | _recalc_alt_dewpoint(); |
| 538 | _recalc_density(); |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | void |
| 543 | FGEnvironment::set_dewpoint_degc (double t) |
| 544 | { |
| 545 | dewpoint_degc = t; |
| 546 | if( live_update ) { |
| 547 | _recalc_sl_dewpoint(); |
| 548 | _recalc_density(); |
| 549 | _recalc_relative_humidity(); |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | void |
| 554 | FGEnvironment::set_pressure_sea_level_inhg (double p) |
| 555 | { |
| 556 | pressure_sea_level_inhg = p; |
| 557 | if( live_update ) { |
| 558 | _recalc_alt_pt(); |
| 559 | _recalc_density(); |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | void |
| 564 | FGEnvironment::set_pressure_inhg (double p) |
| 565 | { |
| 566 | pressure_inhg = p; |
| 567 | if( live_update ) { |
| 568 | _recalc_sl_pressure(); |
| 569 | _recalc_density(); |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | void |
| 574 | FGEnvironment::set_wind_from_heading_deg (double h) |
| 575 | { |
| 576 | wind_from_heading_deg = h; |
| 577 | if( live_update ) { |
| 578 | _recalc_ne(); |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | void |
| 583 | FGEnvironment::set_wind_speed_kt (double s) |
| 584 | { |
| 585 | wind_speed_kt = s; |
| 586 | if( live_update ) { |
| 587 | _recalc_ne(); |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | void |
| 592 | FGEnvironment::set_wind_from_north_fps (double n) |
| 593 | { |
| 594 | wind_from_north_fps = n; |
| 595 | if( live_update ) { |
| 596 | _recalc_hdgspd(); |
| 597 | } |
| 598 | } |
| 599 | |
| 600 | void |
| 601 | FGEnvironment::set_wind_from_east_fps (double e) |
| 602 | { |
| 603 | wind_from_east_fps = e; |
| 604 | if( live_update ) { |
| 605 | _recalc_hdgspd(); |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | void |
| 610 | FGEnvironment::set_wind_from_down_fps (double d) |
| 611 | { |
| 612 | wind_from_down_fps = d; |
| 613 | if( live_update ) { |
| 614 | _recalc_hdgspd(); |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | void |
| 619 | FGEnvironment::set_turbulence_magnitude_norm (double t) |
| 620 | { |
| 621 | turbulence_magnitude_norm = t; |
| 622 | } |
| 623 | |
| 624 | void |
| 625 | FGEnvironment::set_turbulence_rate_hz (double r) |
| 626 | { |
| 627 | turbulence_rate_hz = r; |
| 628 | } |
| 629 | |
| 630 | void |
| 631 | FGEnvironment::set_elevation_ft (double e) |
| 632 | { |
| 633 | elevation_ft = e; |
| 634 | if( live_update ) { |
| 635 | _recalc_alt_pt(); |
| 636 | _recalc_alt_dewpoint(); |
| 637 | _recalc_density(); |
| 638 | _recalc_relative_humidity(); |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | void |
| 643 | FGEnvironment::set_altitude_half_to_sun_m (double alt) |
| 644 | { |
| 645 | altitude_half_to_sun_m = alt; |
| 646 | if( live_update ) { |
| 647 | _recalc_density_tropo_avg_kgm3(); |
| 648 | } |
| 649 | } |
| 650 | |
| 651 | void |
| 652 | FGEnvironment::set_altitude_tropo_top_m (double alt) |
| 653 | { |
| 654 | altitude_tropo_top_m = alt; |
| 655 | if( live_update ) { |
| 656 | _recalc_density_tropo_avg_kgm3(); |
| 657 | } |
| 658 | } |
| 659 | |
| 660 | void FGEnvironment::set_is_isa(bool isa) |
| 661 | { |
| 662 | is_isa = isa; |
| 663 | } |
| 664 | |
| 665 | void |
| 666 | FGEnvironment::_recalc_hdgspd () |
| 667 | { |
| 668 | wind_from_heading_deg = |
| 669 | atan2(wind_from_east_fps, wind_from_north_fps) * SGD_RADIANS_TO_DEGREES; |
| 670 | |
| 671 | if( wind_from_heading_deg < 0 ) |
| 672 | wind_from_heading_deg += 360.0; |
| 673 | |
| 674 | wind_speed_kt = sqrt(wind_from_north_fps * wind_from_north_fps + |
| 675 | wind_from_east_fps * wind_from_east_fps) |
| 676 | * SG_METER_TO_NM * SG_FEET_TO_METER * 3600; |
| 677 | } |
| 678 | |
| 679 | void |
| 680 | FGEnvironment::_recalc_ne () |
| 681 | { |
| 682 | double speed_fps = |
| 683 | wind_speed_kt * SG_NM_TO_METER * SG_METER_TO_FEET * (1.0/3600); |
| 684 | |
| 685 | wind_from_north_fps = speed_fps * |
| 686 | cos(wind_from_heading_deg * SGD_DEGREES_TO_RADIANS); |
| 687 | wind_from_east_fps = speed_fps * |
| 688 | sin(wind_from_heading_deg * SGD_DEGREES_TO_RADIANS); |
| 689 | } |
| 690 | |
| 691 | // Intended to help with the interpretation of METAR data, |
| 692 | // not for random in-flight outside-air temperatures. |
| 693 | void |
| 694 | FGEnvironment::_recalc_sl_temperature () |
| 695 | { |
| 696 | |
| 697 | #if 0 |
| 698 | { |
| 699 | SG_LOG(SG_ENVIRONMENT, SG_DEBUG, "recalc_sl_temperature: using " |
| 700 | << temperature_degc << " @ " << elevation_ft << " :: " << this); |
| 701 | } |
| 702 | #endif |
| 703 | |
| 704 | if (elevation_ft * atmodel::foot >= ISA_def[1].height) { |
| 705 | SG_LOG(SG_ENVIRONMENT, SG_ALERT, "recalc_sl_temperature: " |
| 706 | << "valid only in troposphere, not " << elevation_ft); |
| 707 | return; |
| 708 | } |
| 709 | |
| 710 | double temperature_shift = elevation_ft * atmodel::foot * ISA_def[0].lapse; |
| 711 | |
| 712 | if (is_isa) { |
| 713 | // The "International Standard Atmosphere" METAR scenario in use |
| 714 | temperature_sea_level_degc = 15.0; |
| 715 | temperature_degc = temperature_sea_level_degc - temperature_shift; |
| 716 | } else { |
| 717 | // Clamp: temperature of the stratosphere, in degrees C: |
| 718 | double t_strato = ISA_def[1].temp - atmodel::freezing; |
| 719 | temperature_sea_level_degc = temperature_degc < t_strato |
| 720 | ? t_strato |
| 721 | : temperature_degc + temperature_shift; |
| 722 | |
| 723 | // Alternative implemenation: |
| 724 | // temperature_sea_level_inhg = T_layer(0., elevation_ft * foot, |
| 725 | // pressure_inhg * inHg, temperature_degc + freezing, ISA_def[0].lapse) - freezing; |
| 726 | } |
| 727 | } |
| 728 | |
| 729 | void |
| 730 | FGEnvironment::_recalc_sl_dewpoint () |
| 731 | { |
| 732 | // 0.2degC/1000ft |
| 733 | // FIXME: this will work only for low |
| 734 | // elevations |
| 735 | dewpoint_sea_level_degc = dewpoint_degc + (elevation_ft * .0002); |
| 736 | if (dewpoint_sea_level_degc > temperature_sea_level_degc) |
| 737 | dewpoint_sea_level_degc = temperature_sea_level_degc; |
| 738 | } |
| 739 | |
| 740 | void |
| 741 | FGEnvironment::_recalc_alt_dewpoint () |
| 742 | { |
| 743 | // 0.2degC/1000ft |
| 744 | // FIXME: this will work only for low |
| 745 | // elevations |
| 746 | dewpoint_degc = dewpoint_sea_level_degc + (elevation_ft * .0002); |
| 747 | if (dewpoint_degc > temperature_degc) |
| 748 | dewpoint_degc = temperature_degc; |
| 749 | } |
| 750 | |
| 751 | void |
| 752 | FGEnvironment::_recalc_sl_pressure () |
| 753 | { |
| 754 | using namespace atmodel; |
| 755 | #if 0 |
| 756 | { |
| 757 | SG_LOG(SG_ENVIRONMENT, SG_ALERT, "recalc_sl_pressure: using " |
| 758 | << pressure_inhg << " and " |
| 759 | << temperature_degc << " @ " << elevation_ft << " :: " << this); |
| 760 | } |
| 761 | #endif |
| 762 | pressure_sea_level_inhg = P_layer(0., elevation_ft * foot, |
| 763 | pressure_inhg * inHg, temperature_degc + freezing, ISA_def[0].lapse) / inHg; |
| 764 | } |
| 765 | |
| 766 | // This gets called at frame rate, to account for the aircraft's |
| 767 | // changing altitude. |
| 768 | // Called by set_elevation_ft() which is called by FGEnvironmentMgr::update |
| 769 | |
| 770 | void |
| 771 | FGEnvironment::_recalc_alt_pt () |
| 772 | { |
| 773 | using namespace atmodel; |
| 774 | #if 0 |
| 775 | { |
| 776 | static int count(0); |
| 777 | if (++count % 1000 == 0) { |
| 778 | SG_LOG(SG_ENVIRONMENT, SG_ALERT, |
| 779 | "recalc_alt_pt for: " << elevation_ft |
| 780 | << " using " << pressure_sea_level_inhg |
| 781 | << " and " << temperature_sea_level_degc |
| 782 | << " :: " << this |
| 783 | << " # " << count); |
| 784 | } |
| 785 | } |
| 786 | #endif |
| 787 | double press = pressure_inhg * inHg; |
| 788 | double temp = temperature_degc + freezing; |
| 789 | std::tie(press, temp) = PT_vs_hpt(elevation_ft * foot, |
| 790 | pressure_sea_level_inhg * inHg, temperature_sea_level_degc + freezing); |
| 791 | temperature_degc = temp - freezing; |
| 792 | pressure_inhg = press / inHg; |
| 793 | } |
| 794 | |
| 795 | void |
| 796 | FGEnvironment::_recalc_density () |
| 797 | { |
| 798 | const double pressure_psf = pressure_inhg * 70.7487; |
| 799 | |
| 800 | // adjust for humidity |
| 801 | // calculations taken from USA Today (oops!) at |
| 802 | // http://www.usatoday.com/weather/basics/density-calculations.htm |
| 803 | const double temperature_degk = temperature_degc + 273.15; |
| 804 | const double pressure_mb = pressure_inhg * 33.86; |
| 805 | const double vapor_pressure_mb = |
| 806 | 6.11 * pow(10.0, 7.5 * dewpoint_degc / (237.7 + dewpoint_degc)); |
| 807 | |
| 808 | if ((pressure_mb <= 0.0) || (vapor_pressure_mb <= 0.0)) { |
| 809 | density_slugft3 = 0.0; |
| 810 | return; |
| 811 | } |
| 812 | |
| 813 | double virtual_temperature_degk = temperature_degk / (1 - (vapor_pressure_mb / pressure_mb) * (1.0 - 0.622)); |
| 814 | double virtual_temperature_degr = virtual_temperature_degk * 1.8; |
| 815 | |
| 816 | density_slugft3 = pressure_psf / (virtual_temperature_degr * 1718); |
| 817 | _recalc_density_tropo_avg_kgm3(); |
| 818 | } |
| 819 | |
| 820 | // This is used to calculate the average density on the path |
| 821 | // of sunlight to the observer for calculating sun-color |
| 822 | void |
| 823 | FGEnvironment::_recalc_density_tropo_avg_kgm3 () |
| 824 | { |
| 825 | const double pressure_mb = pressure_inhg * 33.86; |
| 826 | const double vaporpressure = 6.11 * pow(10.0, ((7.5 * dewpoint_degc) / (237.7 + dewpoint_degc))); |
| 827 | const double virtual_temp = (temperature_degc + 273.15) / (1 - 0.379 * (vaporpressure/pressure_mb)); |
| 828 | |
| 829 | if ((pressure_mb <= 0.0) || (virtual_temp <= 0.0)) { |
| 830 | density_tropo_avg_kgm3 = 0.0; |
| 831 | return; |
| 832 | } |
| 833 | |
| 834 | double density_half = (100 * pressure_mb * exp(-altitude_half_to_sun_m / 8000)) |
| 835 | / (287.05 * virtual_temp); |
| 836 | double density_tropo = (100 * pressure_mb * exp((-1 * altitude_tropo_top_m) / 8000)) |
| 837 | / ( 287.05 * virtual_temp); |
| 838 | |
| 839 | density_tropo_avg_kgm3 = ((density_slugft3 * 515.379) + density_half + density_tropo) / 3; |
| 840 | } |
| 841 | |
| 842 | void |
| 843 | FGEnvironment::_recalc_relative_humidity () |
| 844 | { |
| 845 | /* |
| 846 | double vaporpressure = 6.11 * pow(10.0, ((7.5 * dewpoint_degc) / ( 237.7 + dewpoint_degc))); |
| 847 | double sat_vaporpressure = 6.11 * pow(10.0, ((7.5 * temperature_degc) |
| 848 | / ( 237.7 + temperature_degc)) ); |
| 849 | relative_humidity = 100 * vaporpressure / sat_vaporpressure ; |
| 850 | |
| 851 | with a little algebra, this gets the same result and spares two multiplications and one pow() |
| 852 | */ |
| 853 | double a = (7.5 * dewpoint_degc) / ( 237.7 + dewpoint_degc); |
| 854 | double b = (7.5 * temperature_degc) / ( 237.7 + temperature_degc); |
| 855 | relative_humidity = 100 * pow(10.0,a-b); |
| 856 | } |
| 857 | |
| 858 | bool |
| 859 | FGEnvironment::set_live_update( bool _live_update ) |
| 860 | { |
| 861 | bool b = live_update; |
| 862 | live_update = _live_update; |
| 863 | return b; |
| 864 | } |
| 865 | |
| 866 | |
| 867 | //////////////////////////////////////////////////////////////////////// |
| 868 | // Functions. |
| 869 | //////////////////////////////////////////////////////////////////////// |
| 870 | |
| 871 | static inline double |
| 872 | do_interp (double a, double b, double fraction) |
| 873 | { |
| 874 | double retval = (a + ((b - a) * fraction)); |
| 875 | return retval; |
| 876 | } |
| 877 | |
| 878 | static inline double |
| 879 | do_interp_deg (double a, double b, double fraction) |
| 880 | { |
| 881 | a = fmod(a, 360); |
| 882 | b = fmod(b, 360); |
| 883 | if (fabs(b-a) > 180) { |
| 884 | if (a < b) |
| 885 | a += 360; |
| 886 | else |
| 887 | b += 360; |
| 888 | } |
| 889 | return fmod(do_interp(a, b, fraction), 360); |
| 890 | } |
| 891 | |
| 892 | FGEnvironment & |
| 893 | FGEnvironment::interpolate( const FGEnvironment & env2, |
| 894 | double fraction, FGEnvironment * result) const |
| 895 | { |
| 896 | // don't calculate each internal property every time we set a single value |
| 897 | // we trigger that at the end of the interpolation process |
| 898 | bool live_update = result->set_live_update( false ); |
| 899 | |
| 900 | result->set_visibility_m |
| 901 | (do_interp(get_visibility_m(), |
| 902 | env2.get_visibility_m(), |
| 903 | fraction)); |
| 904 | |
| 905 | result->set_temperature_sea_level_degc |
| 906 | (do_interp(get_temperature_sea_level_degc(), |
| 907 | env2.get_temperature_sea_level_degc(), |
| 908 | fraction)); |
| 909 | |
| 910 | result->set_dewpoint_sea_level_degc |
| 911 | (do_interp(get_dewpoint_sea_level_degc(), |
| 912 | env2.get_dewpoint_sea_level_degc(), |
| 913 | fraction)); |
| 914 | |
| 915 | result->set_pressure_sea_level_inhg |
| 916 | (do_interp(get_pressure_sea_level_inhg(), |
| 917 | env2.get_pressure_sea_level_inhg(), |
| 918 | fraction)); |
| 919 | |
| 920 | result->set_wind_from_heading_deg |
| 921 | (do_interp_deg(get_wind_from_heading_deg(), |
| 922 | env2.get_wind_from_heading_deg(), |
| 923 | fraction)); |
| 924 | |
| 925 | result->set_wind_speed_kt |
| 926 | (do_interp(get_wind_speed_kt(), |
| 927 | env2.get_wind_speed_kt(), |
| 928 | fraction)); |
| 929 | |
| 930 | result->set_elevation_ft |
| 931 | (do_interp(get_elevation_ft(), |
| 932 | env2.get_elevation_ft(), |
| 933 | fraction)); |
| 934 | |
| 935 | result->set_turbulence_magnitude_norm |
| 936 | (do_interp(get_turbulence_magnitude_norm(), |
| 937 | env2.get_turbulence_magnitude_norm(), |
| 938 | fraction)); |
| 939 | |
| 940 | result->set_turbulence_rate_hz |
| 941 | (do_interp(get_turbulence_rate_hz(), |
| 942 | env2.get_turbulence_rate_hz(), |
| 943 | fraction)); |
| 944 | |
| 945 | // calculate derived properties here to avoid duplicate expensive computations |
| 946 | result->_recalc_ne(); |
| 947 | result->_recalc_alt_pt(); |
| 948 | result->_recalc_alt_dewpoint(); |
| 949 | result->_recalc_density(); |
| 950 | result->_recalc_relative_humidity(); |
| 951 | |
| 952 | result->set_live_update(live_update); |
| 953 | |
| 954 | return *result; |
| 955 | } |
| 956 | |
| 957 | // end of environment.cxx |