1 // environment.cxx -- routines to model the natural environment
3 // Written by David Megginson, started February 2002.
5 // Copyright (C) 2002 David Megginson - david@megginson.com
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.
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.
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., 675 Mass Ave, Cambridge, MA 02139, USA.
36 #include <simgear/constants.h>
37 #include <simgear/debug/logstream.hxx>
38 #include <simgear/math/interpolater.hxx>
40 #include <Main/fg_props.hxx>
41 #include "environment.hxx"
45 ////////////////////////////////////////////////////////////////////////
47 ////////////////////////////////////////////////////////////////////////
49 // Copied from YASim Atmosphere.cxx, with m converted to ft, degK
50 // converted to degC, Pa converted to inHG, and kg/m^3 converted to
51 // slug/ft^3; they were then converted to deltas from the sea-level
52 // defaults (approx. 15degC, 29.92inHG, and 0.00237slugs/ft^3).
54 // Original comment from YASim:
56 // Copied from McCormick, who got it from "The ARDC Model Atmosphere"
57 // Note that there's an error in the text in the first entry,
58 // McCormick lists 299.16/101325/1.22500, but those don't agree with
59 // R=287. I chose to correct the temperature to 288.20, since 79F is
60 // pretty hot for a "standard" atmosphere.
62 // Elevation (ft), temperature factor (degK), pressure factor (inHG)
63 static double atmosphere_data[][3] = {
64 { 0.00, 1.00, 1.000 },
65 { 2952.76, 0.98, 0.898 },
66 { 5905.51, 0.96, 0.804 },
67 { 8858.27, 0.94, 0.719 },
68 { 11811.02, 0.92, 0.641 },
69 { 14763.78, 0.90, 0.570 },
70 { 17716.54, 0.88, 0.506 },
71 { 20669.29, 0.86, 0.447 },
72 { 23622.05, 0.84, 0.394 },
73 { 26574.80, 0.82, 0.347 },
74 { 29527.56, 0.80, 0.304 },
75 { 32480.31, 0.78, 0.266 },
76 { 35433.07, 0.76, 0.231 },
77 { 38385.83, 0.75, 0.201 },
78 { 41338.58, 0.75, 0.174 },
79 { 44291.34, 0.75, 0.151 },
80 { 47244.09, 0.75, 0.131 },
81 { 50196.85, 0.75, 0.114 },
82 { 53149.61, 0.75, 0.099 },
83 { 56102.36, 0.75, 0.086 },
84 { 59055.12, 0.75, 0.075 },
85 { 62007.87, 0.75, 0.065 },
89 static SGInterpTable * _temperature_degc_table = 0;
90 static SGInterpTable * _pressure_inhg_table = 0;
95 if (_temperature_degc_table != 0)
98 _temperature_degc_table = new SGInterpTable;
99 _pressure_inhg_table = new SGInterpTable;
101 for (int i = 0; atmosphere_data[i][0] != -1; i++) {
102 _temperature_degc_table->addEntry(atmosphere_data[i][0],
103 atmosphere_data[i][1]);
104 _pressure_inhg_table->addEntry(atmosphere_data[i][0],
105 atmosphere_data[i][2]);
111 ////////////////////////////////////////////////////////////////////////
112 // Implementation of FGEnvironment.
113 ////////////////////////////////////////////////////////////////////////
115 FGEnvironment::FGEnvironment()
118 temperature_sea_level_degc(15),
119 temperature_degc(15),
120 dewpoint_sea_level_degc(5), // guess
122 pressure_sea_level_inhg(29.92),
123 pressure_inhg(29.92),
124 turbulence_magnitude_norm(0),
125 turbulence_rate_hz(1),
126 wind_from_heading_deg(0),
128 wind_from_north_fps(0),
129 wind_from_east_fps(0),
130 wind_from_down_fps(0)
136 FGEnvironment::FGEnvironment (const FGEnvironment &env)
142 FGEnvironment::~FGEnvironment()
147 FGEnvironment::copy (const FGEnvironment &env)
149 elevation_ft = env.elevation_ft;
150 visibility_m = env.visibility_m;
151 temperature_sea_level_degc = env.temperature_sea_level_degc;
152 temperature_degc = env.temperature_degc;
153 dewpoint_sea_level_degc = env.dewpoint_sea_level_degc;
154 dewpoint_degc = env.dewpoint_degc;
155 pressure_sea_level_inhg = env.pressure_sea_level_inhg;
156 wind_from_heading_deg = env.wind_from_heading_deg;
157 wind_speed_kt = env.wind_speed_kt;
158 wind_from_north_fps = env.wind_from_north_fps;
159 wind_from_east_fps = env.wind_from_east_fps;
160 wind_from_down_fps = env.wind_from_down_fps;
161 turbulence_magnitude_norm = env.turbulence_magnitude_norm;
162 turbulence_rate_hz = env.turbulence_rate_hz;
166 maybe_copy_value (FGEnvironment * env, const SGPropertyNode * node,
167 const char * name, void (FGEnvironment::*setter)(double))
169 const SGPropertyNode * child = node->getNode(name);
170 // fragile: depends on not being typed
172 if (child != 0 && child->hasValue() &&
173 child->getStringValue()[0] != '\0') {
174 (env->*setter)(child->getDoubleValue());
182 FGEnvironment::read (const SGPropertyNode * node)
184 maybe_copy_value(this, node, "visibility-m",
185 &FGEnvironment::set_visibility_m);
187 if (!maybe_copy_value(this, node, "temperature-sea-level-degc",
188 &FGEnvironment::set_temperature_sea_level_degc))
189 maybe_copy_value(this, node, "temperature-degc",
190 &FGEnvironment::set_temperature_degc);
192 if (!maybe_copy_value(this, node, "dewpoint-sea-level-degc",
193 &FGEnvironment::set_dewpoint_sea_level_degc))
194 maybe_copy_value(this, node, "dewpoint-degc",
195 &FGEnvironment::set_dewpoint_degc);
197 if (!maybe_copy_value(this, node, "pressure-sea-level-inhg",
198 &FGEnvironment::set_pressure_sea_level_inhg))
199 maybe_copy_value(this, node, "pressure-inhg",
200 &FGEnvironment::set_pressure_inhg);
202 maybe_copy_value(this, node, "wind-from-heading-deg",
203 &FGEnvironment::set_wind_from_heading_deg);
205 maybe_copy_value(this, node, "wind-speed-kt",
206 &FGEnvironment::set_wind_speed_kt);
208 maybe_copy_value(this, node, "elevation-ft",
209 &FGEnvironment::set_elevation_ft);
211 maybe_copy_value(this, node, "turbulence/magnitude-norm",
212 &FGEnvironment::set_turbulence_magnitude_norm);
214 maybe_copy_value(this, node, "turbulence/rate-hz",
215 &FGEnvironment::set_turbulence_rate_hz);
220 FGEnvironment::get_visibility_m () const
226 FGEnvironment::get_temperature_sea_level_degc () const
228 return temperature_sea_level_degc;
232 FGEnvironment::get_temperature_degc () const
234 return temperature_degc;
238 FGEnvironment::get_dewpoint_sea_level_degc () const
240 return dewpoint_sea_level_degc;
244 FGEnvironment::get_dewpoint_degc () const
246 return dewpoint_degc;
250 FGEnvironment::get_pressure_sea_level_inhg () const
252 return pressure_sea_level_inhg;
256 FGEnvironment::get_pressure_inhg () const
258 return pressure_inhg;
262 FGEnvironment::get_density_slugft3 () const
264 return density_slugft3;
268 FGEnvironment::get_wind_from_heading_deg () const
270 return wind_from_heading_deg;
274 FGEnvironment::get_wind_speed_kt () const
276 return wind_speed_kt;
280 FGEnvironment::get_wind_from_north_fps () const
282 return wind_from_north_fps;
286 FGEnvironment::get_wind_from_east_fps () const
288 return wind_from_east_fps;
292 FGEnvironment::get_wind_from_down_fps () const
294 return wind_from_down_fps;
298 FGEnvironment::get_turbulence_magnitude_norm () const
300 return turbulence_magnitude_norm;
304 FGEnvironment::get_turbulence_rate_hz () const
306 return turbulence_rate_hz;
310 FGEnvironment::get_elevation_ft () const
316 FGEnvironment::set_visibility_m (double v)
322 FGEnvironment::set_temperature_sea_level_degc (double t)
324 temperature_sea_level_degc = t;
325 if (dewpoint_sea_level_degc > t)
326 dewpoint_sea_level_degc = t;
327 _recalc_alt_temperature();
332 FGEnvironment::set_temperature_degc (double t)
334 temperature_degc = t;
335 _recalc_sl_temperature();
340 FGEnvironment::set_dewpoint_sea_level_degc (double t)
342 dewpoint_sea_level_degc = t;
343 if (temperature_sea_level_degc < t)
344 temperature_sea_level_degc = t;
345 _recalc_alt_dewpoint();
350 FGEnvironment::set_dewpoint_degc (double t)
353 _recalc_sl_dewpoint();
358 FGEnvironment::set_pressure_sea_level_inhg (double p)
360 pressure_sea_level_inhg = p;
361 _recalc_alt_pressure();
366 FGEnvironment::set_pressure_inhg (double p)
369 _recalc_sl_pressure();
374 FGEnvironment::set_wind_from_heading_deg (double h)
376 wind_from_heading_deg = h;
381 FGEnvironment::set_wind_speed_kt (double s)
388 FGEnvironment::set_wind_from_north_fps (double n)
390 wind_from_north_fps = n;
395 FGEnvironment::set_wind_from_east_fps (double e)
397 wind_from_east_fps = e;
402 FGEnvironment::set_wind_from_down_fps (double d)
404 wind_from_down_fps = d;
409 FGEnvironment::set_turbulence_magnitude_norm (double t)
411 turbulence_magnitude_norm = t;
415 FGEnvironment::set_turbulence_rate_hz (double r)
417 turbulence_rate_hz = r;
421 FGEnvironment::set_elevation_ft (double e)
424 _recalc_alt_temperature();
425 _recalc_alt_dewpoint();
426 _recalc_alt_pressure();
431 FGEnvironment::_recalc_hdgspd ()
435 if (wind_from_east_fps == 0) {
436 angle_rad = (wind_from_north_fps >= 0 ? SGD_PI/2 : -SGD_PI/2);
438 angle_rad = atan(wind_from_north_fps/wind_from_east_fps);
440 wind_from_heading_deg = angle_rad * SGD_RADIANS_TO_DEGREES;
441 if (wind_from_east_fps >= 0)
442 wind_from_heading_deg = 90 - wind_from_heading_deg;
444 wind_from_heading_deg = 270 - wind_from_heading_deg;
447 // FIXME: Windspeed can become negative with these formulas.
448 // which can cause problems for animations that rely
449 // on the windspeed property.
451 wind_speed_kt = fabs(wind_from_east_fps
452 * SG_METER_TO_NM * SG_FEET_TO_METER * 3600);
454 wind_speed_kt = (wind_from_north_fps / sin(angle_rad))
455 * SG_METER_TO_NM * SG_FEET_TO_METER * 3600;
457 wind_speed_kt = sqrt(wind_from_north_fps * wind_from_north_fps +
458 wind_from_east_fps * wind_from_east_fps)
459 * SG_METER_TO_NM * SG_FEET_TO_METER * 3600;
464 FGEnvironment::_recalc_ne ()
467 wind_speed_kt * SG_NM_TO_METER * SG_METER_TO_FEET * (1.0/3600);
469 wind_from_north_fps = speed_fps *
470 cos(wind_from_heading_deg * SGD_DEGREES_TO_RADIANS);
471 wind_from_east_fps = speed_fps *
472 sin(wind_from_heading_deg * SGD_DEGREES_TO_RADIANS);
476 FGEnvironment::_recalc_sl_temperature ()
478 // If we're in the stratosphere, leave sea-level temp alone
479 if (elevation_ft < 38000) {
480 temperature_sea_level_degc =
481 (temperature_degc + 273.15)
482 /_temperature_degc_table->interpolate(elevation_ft)
488 FGEnvironment::_recalc_alt_temperature ()
490 if (elevation_ft < 38000) {
492 (temperature_sea_level_degc + 273.15) *
493 _temperature_degc_table->interpolate(elevation_ft) - 273.15;
495 temperature_degc = -56.49; // Stratosphere is constant
500 FGEnvironment::_recalc_sl_dewpoint ()
503 // FIXME: this will work only for low
505 dewpoint_sea_level_degc = dewpoint_degc + (elevation_ft * .0002);
506 if (dewpoint_sea_level_degc > temperature_sea_level_degc)
507 dewpoint_sea_level_degc = temperature_sea_level_degc;
511 FGEnvironment::_recalc_alt_dewpoint ()
514 // FIXME: this will work only for low
516 dewpoint_degc = dewpoint_sea_level_degc + (elevation_ft * .0002);
517 if (dewpoint_degc > temperature_degc)
518 dewpoint_degc = temperature_degc;
522 FGEnvironment::_recalc_sl_pressure ()
524 pressure_sea_level_inhg =
525 pressure_inhg / _pressure_inhg_table->interpolate(elevation_ft);
529 FGEnvironment::_recalc_alt_pressure ()
532 pressure_sea_level_inhg * _pressure_inhg_table->interpolate(elevation_ft);
536 FGEnvironment::_recalc_density ()
538 double pressure_psf = pressure_inhg * 70.7487;
540 // adjust for humidity
541 // calculations taken from USA Today (oops!) at
542 // http://www.usatoday.com/weather/basics/density-calculations.htm
543 double temperature_degk = temperature_degc + 273.15;
544 double pressure_mb = pressure_inhg * 33.86;
545 double vapor_pressure_mb =
546 6.11 * pow(10.0, 7.5 * dewpoint_degc / (237.7 + dewpoint_degc));
547 double virtual_temperature_degk = temperature_degk / (1 - (vapor_pressure_mb / pressure_mb) * (1.0 - 0.622));
548 double virtual_temperature_degr = virtual_temperature_degk * 1.8;
550 density_slugft3 = pressure_psf / (virtual_temperature_degr * 1718);
555 ////////////////////////////////////////////////////////////////////////
557 ////////////////////////////////////////////////////////////////////////
560 do_interp (double a, double b, double fraction)
562 double retval = (a + ((b - a) * fraction));
567 do_interp_deg (double a, double b, double fraction)
571 if (fabs(b-a) > 180) {
577 return fmod(do_interp(a, b, fraction), 360);
581 interpolate (const FGEnvironment * env1, const FGEnvironment * env2,
582 double fraction, FGEnvironment * result)
584 result->set_visibility_m
585 (do_interp(env1->get_visibility_m(),
586 env2->get_visibility_m(),
589 result->set_temperature_sea_level_degc
590 (do_interp(env1->get_temperature_sea_level_degc(),
591 env2->get_temperature_sea_level_degc(),
594 result->set_dewpoint_degc
595 (do_interp(env1->get_dewpoint_sea_level_degc(),
596 env2->get_dewpoint_sea_level_degc(),
599 result->set_pressure_sea_level_inhg
600 (do_interp(env1->get_pressure_sea_level_inhg(),
601 env2->get_pressure_sea_level_inhg(),
604 result->set_wind_from_heading_deg
605 (do_interp_deg(env1->get_wind_from_heading_deg(),
606 env2->get_wind_from_heading_deg(),
609 result->set_wind_speed_kt
610 (do_interp(env1->get_wind_speed_kt(),
611 env2->get_wind_speed_kt(),
614 result->set_elevation_ft
615 (do_interp(env1->get_elevation_ft(),
616 env2->get_elevation_ft(),
619 result->set_turbulence_magnitude_norm
620 (do_interp(env1->get_turbulence_magnitude_norm(),
621 env2->get_turbulence_magnitude_norm(),
624 result->set_turbulence_rate_hz
625 (do_interp(env1->get_turbulence_rate_hz(),
626 env2->get_turbulence_rate_hz(),
630 // end of environment.cxx