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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
30 #include <simgear/constants.h>
31 #include <simgear/debug/logstream.hxx>
32 #include <simgear/math/interpolater.hxx>
33 #include <simgear/props/props.hxx>
34 #include <simgear/environment/visual_enviro.hxx>
36 #include <Main/fg_props.hxx>
38 #include "environment.hxx"
42 ////////////////////////////////////////////////////////////////////////
44 ////////////////////////////////////////////////////////////////////////
46 // Calculated based on the ISA standard day, as found at e.g.
47 // http://www.av8n.com/physics/altimetry.htm
49 // Each line of data has 3 elements:
51 // temperature factor (dimensionless ratio of absolute temp),
52 // pressure factor (dimensionless ratio)
53 static double atmosphere_data[][3] = {
54 { -3000.00, 1.021, 1.1133 },
55 { 0.00, 1.000, 1.0000 },
56 { 2952.76, 0.980, 0.8978 },
57 { 5905.51, 0.959, 0.8042 },
58 { 8858.27, 0.939, 0.7187 },
59 { 11811.02, 0.919, 0.6407 },
60 { 14763.78, 0.898, 0.5697 },
61 { 17716.54, 0.878, 0.5052 },
62 { 20669.29, 0.858, 0.4468 },
63 { 23622.05, 0.838, 0.3940 },
64 { 26574.80, 0.817, 0.3463 },
65 { 29527.56, 0.797, 0.3034 },
66 { 32480.31, 0.777, 0.2649 },
67 { 35433.07, 0.756, 0.2305 },
68 { 38385.83, 0.752, 0.2000 },
69 { 41338.58, 0.752, 0.1736 },
70 { 44291.34, 0.752, 0.1506 },
71 { 47244.09, 0.752, 0.1307 },
72 { 50196.85, 0.752, 0.1134 },
73 { 53149.61, 0.752, 0.0984 },
74 { 56102.36, 0.752, 0.0854 },
75 { 59055.12, 0.752, 0.0741 },
76 { 62007.87, 0.752, 0.0643 },
77 { 65000.00, 0.752, 0.0557 },
78 { 68000.00, 0.754, 0.0482 },
79 { 71000.00, 0.758, 0.0418 },
80 { 74000.00, 0.761, 0.0362 },
81 { 77000.00, 0.764, 0.0314 },
82 { 80000.00, 0.767, 0.0273 },
83 { 83000.00, 0.770, 0.0237 },
84 { 86000.00, 0.773, 0.0206 },
85 { 89000.00, 0.777, 0.0179 },
86 { 92000.00, 0.780, 0.0156 },
87 { 95000.00, 0.783, 0.0135 },
88 { 98000.00, 0.786, 0.0118 },
89 { 101000.00, 0.789, 0.0103 },
93 static SGInterpTable * _temperature_degc_table = 0;
94 static SGInterpTable * _pressure_inhg_table = 0;
99 if (_temperature_degc_table != 0)
102 _temperature_degc_table = new SGInterpTable;
103 _pressure_inhg_table = new SGInterpTable;
105 for (int i = 0; atmosphere_data[i][0] != -1; i++) {
106 _temperature_degc_table->addEntry(atmosphere_data[i][0],
107 atmosphere_data[i][1]);
108 _pressure_inhg_table->addEntry(atmosphere_data[i][0],
109 atmosphere_data[i][2]);
115 ////////////////////////////////////////////////////////////////////////
116 // Implementation of FGEnvironment.
117 ////////////////////////////////////////////////////////////////////////
119 void FGEnvironment::_init()
122 visibility_m = 32000;
123 temperature_sea_level_degc = 15;
124 temperature_degc = 15;
125 dewpoint_sea_level_degc = 5; // guess
127 pressure_sea_level_inhg = 29.92;
128 pressure_inhg = 29.92;
129 turbulence_magnitude_norm = 0;
130 turbulence_rate_hz = 1;
131 wind_from_heading_deg = 0;
133 wind_from_north_fps = 0;
134 wind_from_east_fps = 0;
135 wind_from_down_fps = 0;
136 thermal_lift_fps = 0;
138 altitude_half_to_sun_m = 1000;
139 altitude_tropo_top_m = 10000;
142 _recalc_relative_humidity();
146 FGEnvironment::FGEnvironment()
151 FGEnvironment::FGEnvironment (const FGEnvironment &env)
157 FGEnvironment::~FGEnvironment()
162 FGEnvironment::copy (const FGEnvironment &env)
164 elevation_ft = env.elevation_ft;
165 visibility_m = env.visibility_m;
166 temperature_sea_level_degc = env.temperature_sea_level_degc;
167 temperature_degc = env.temperature_degc;
168 dewpoint_sea_level_degc = env.dewpoint_sea_level_degc;
169 dewpoint_degc = env.dewpoint_degc;
170 pressure_sea_level_inhg = env.pressure_sea_level_inhg;
171 wind_from_heading_deg = env.wind_from_heading_deg;
172 wind_speed_kt = env.wind_speed_kt;
173 wind_from_north_fps = env.wind_from_north_fps;
174 wind_from_east_fps = env.wind_from_east_fps;
175 wind_from_down_fps = env.wind_from_down_fps;
176 thermal_lift_fps = env.thermal_lift_fps;
177 ridge_lift_fps= env.ridge_lift_fps;
178 turbulence_magnitude_norm = env.turbulence_magnitude_norm;
179 turbulence_rate_hz = env.turbulence_rate_hz;
183 maybe_copy_value (FGEnvironment * env, const SGPropertyNode * node,
184 const char * name, void (FGEnvironment::*setter)(double))
186 const SGPropertyNode * child = node->getNode(name);
187 // fragile: depends on not being typed
189 if (child != 0 && child->hasValue() &&
190 child->getStringValue()[0] != '\0') {
191 (env->*setter)(child->getDoubleValue());
199 FGEnvironment::read (const SGPropertyNode * node)
201 maybe_copy_value(this, node, "visibility-m",
202 &FGEnvironment::set_visibility_m);
204 if (!maybe_copy_value(this, node, "temperature-sea-level-degc",
205 &FGEnvironment::set_temperature_sea_level_degc))
206 maybe_copy_value(this, node, "temperature-degc",
207 &FGEnvironment::set_temperature_degc);
209 if (!maybe_copy_value(this, node, "dewpoint-sea-level-degc",
210 &FGEnvironment::set_dewpoint_sea_level_degc))
211 maybe_copy_value(this, node, "dewpoint-degc",
212 &FGEnvironment::set_dewpoint_degc);
214 if (!maybe_copy_value(this, node, "pressure-sea-level-inhg",
215 &FGEnvironment::set_pressure_sea_level_inhg))
216 maybe_copy_value(this, node, "pressure-inhg",
217 &FGEnvironment::set_pressure_inhg);
219 maybe_copy_value(this, node, "wind-from-heading-deg",
220 &FGEnvironment::set_wind_from_heading_deg);
222 maybe_copy_value(this, node, "wind-speed-kt",
223 &FGEnvironment::set_wind_speed_kt);
225 maybe_copy_value(this, node, "elevation-ft",
226 &FGEnvironment::set_elevation_ft);
228 maybe_copy_value(this, node, "turbulence/magnitude-norm",
229 &FGEnvironment::set_turbulence_magnitude_norm);
231 maybe_copy_value(this, node, "turbulence/rate-hz",
232 &FGEnvironment::set_turbulence_rate_hz);
237 FGEnvironment::get_visibility_m () const
243 FGEnvironment::get_temperature_sea_level_degc () const
245 return temperature_sea_level_degc;
249 FGEnvironment::get_temperature_degc () const
251 return temperature_degc;
255 FGEnvironment::get_temperature_degf () const
257 return (temperature_degc * 9.0 / 5.0) + 32.0;
261 FGEnvironment::get_dewpoint_sea_level_degc () const
263 return dewpoint_sea_level_degc;
267 FGEnvironment::get_dewpoint_degc () const
269 return dewpoint_degc;
273 FGEnvironment::get_pressure_sea_level_inhg () const
275 return pressure_sea_level_inhg;
279 FGEnvironment::get_pressure_inhg () const
281 return pressure_inhg;
285 FGEnvironment::get_density_slugft3 () const
287 return density_slugft3;
291 FGEnvironment::get_relative_humidity () const
293 return relative_humidity;
297 FGEnvironment::get_density_tropo_avg_kgm3 () const
299 return density_tropo_avg_kgm3;
303 FGEnvironment::get_altitude_half_to_sun_m () const
305 return altitude_half_to_sun_m;
309 FGEnvironment::get_altitude_tropo_top_m () const
311 return altitude_tropo_top_m;
315 FGEnvironment::get_wind_from_heading_deg () const
317 return wind_from_heading_deg;
321 FGEnvironment::get_wind_speed_kt () const
323 return wind_speed_kt;
327 FGEnvironment::get_wind_from_north_fps () const
329 return wind_from_north_fps;
333 FGEnvironment::get_wind_from_east_fps () const
335 return wind_from_east_fps;
339 FGEnvironment::get_wind_from_down_fps () const
341 return wind_from_down_fps;
345 FGEnvironment::get_thermal_lift_fps () const
347 return thermal_lift_fps;
351 FGEnvironment::get_ridge_lift_fps () const
353 return ridge_lift_fps;
357 FGEnvironment::get_turbulence_magnitude_norm () const
359 if( sgEnviro.get_turbulence_enable_state() )
360 if (fgGetBool("/environment/params/real-world-weather-fetch") == true)
361 return sgEnviro.get_cloud_turbulence();
362 return turbulence_magnitude_norm;
366 FGEnvironment::get_turbulence_rate_hz () const
368 return turbulence_rate_hz;
372 FGEnvironment::get_elevation_ft () const
378 FGEnvironment::set_visibility_m (double v)
384 FGEnvironment::set_temperature_sea_level_degc (double t)
386 temperature_sea_level_degc = t;
387 if (dewpoint_sea_level_degc > t)
388 dewpoint_sea_level_degc = t;
389 _recalc_alt_temperature();
394 FGEnvironment::set_temperature_degc (double t)
396 temperature_degc = t;
397 _recalc_sl_temperature();
399 _recalc_relative_humidity();
403 FGEnvironment::set_dewpoint_sea_level_degc (double t)
405 dewpoint_sea_level_degc = t;
406 if (temperature_sea_level_degc < t)
407 temperature_sea_level_degc = t;
408 _recalc_alt_dewpoint();
413 FGEnvironment::set_dewpoint_degc (double t)
416 _recalc_sl_dewpoint();
418 _recalc_relative_humidity();
422 FGEnvironment::set_pressure_sea_level_inhg (double p)
424 pressure_sea_level_inhg = p;
425 _recalc_alt_pressure();
430 FGEnvironment::set_pressure_inhg (double p)
433 _recalc_sl_pressure();
438 FGEnvironment::set_wind_from_heading_deg (double h)
440 wind_from_heading_deg = h;
445 FGEnvironment::set_wind_speed_kt (double s)
452 FGEnvironment::set_wind_from_north_fps (double n)
454 wind_from_north_fps = n;
459 FGEnvironment::set_wind_from_east_fps (double e)
461 wind_from_east_fps = e;
466 FGEnvironment::set_wind_from_down_fps (double d)
468 wind_from_down_fps = d;
473 FGEnvironment::set_thermal_lift_fps (double th)
475 thermal_lift_fps = th;
480 FGEnvironment::set_ridge_lift_fps (double ri)
487 FGEnvironment::set_turbulence_magnitude_norm (double t)
489 turbulence_magnitude_norm = t;
493 FGEnvironment::set_turbulence_rate_hz (double r)
495 turbulence_rate_hz = r;
499 FGEnvironment::set_elevation_ft (double e)
502 _recalc_alt_temperature();
503 _recalc_alt_dewpoint();
504 _recalc_alt_pressure();
506 _recalc_relative_humidity();
510 FGEnvironment::set_altitude_half_to_sun_m (double alt)
512 altitude_half_to_sun_m = alt;
513 _recalc_density_tropo_avg_kgm3();
517 FGEnvironment::set_altitude_tropo_top_m (double alt)
519 altitude_tropo_top_m = alt;
520 _recalc_density_tropo_avg_kgm3();
525 FGEnvironment::_recalc_hdgspd ()
529 if (wind_from_east_fps == 0) {
530 angle_rad = (wind_from_north_fps >= 0 ? SGD_PI_2 : -SGD_PI_2);
532 angle_rad = atan(wind_from_north_fps/wind_from_east_fps);
534 wind_from_heading_deg = angle_rad * SGD_RADIANS_TO_DEGREES;
535 if (wind_from_east_fps >= 0)
536 wind_from_heading_deg = 90 - wind_from_heading_deg;
538 wind_from_heading_deg = 270 - wind_from_heading_deg;
541 // FIXME: Windspeed can become negative with these formulas.
542 // which can cause problems for animations that rely
543 // on the windspeed property.
545 wind_speed_kt = fabs(wind_from_east_fps
546 * SG_METER_TO_NM * SG_FEET_TO_METER * 3600);
548 wind_speed_kt = (wind_from_north_fps / sin(angle_rad))
549 * SG_METER_TO_NM * SG_FEET_TO_METER * 3600;
551 wind_speed_kt = sqrt(wind_from_north_fps * wind_from_north_fps +
552 wind_from_east_fps * wind_from_east_fps)
553 * SG_METER_TO_NM * SG_FEET_TO_METER * 3600;
558 FGEnvironment::_recalc_ne ()
561 wind_speed_kt * SG_NM_TO_METER * SG_METER_TO_FEET * (1.0/3600);
563 wind_from_north_fps = speed_fps *
564 cos(wind_from_heading_deg * SGD_DEGREES_TO_RADIANS);
565 wind_from_east_fps = speed_fps *
566 sin(wind_from_heading_deg * SGD_DEGREES_TO_RADIANS);
570 FGEnvironment::_recalc_updraft ()
572 wind_from_down_fps = thermal_lift_fps + ridge_lift_fps ;
576 FGEnvironment::_recalc_sl_temperature ()
578 // If we're in the stratosphere, leave sea-level temp alone
579 if (elevation_ft < 38000) {
580 temperature_sea_level_degc = (temperature_degc + 273.15)
581 / _temperature_degc_table->interpolate(elevation_ft)
587 FGEnvironment::_recalc_alt_temperature ()
589 if (elevation_ft < 38000) {
590 temperature_degc = (temperature_sea_level_degc + 273.15) *
591 _temperature_degc_table->interpolate(elevation_ft) - 273.15;
593 temperature_degc = -56.49; // Stratosphere is constant
598 FGEnvironment::_recalc_sl_dewpoint ()
601 // FIXME: this will work only for low
603 dewpoint_sea_level_degc = dewpoint_degc + (elevation_ft * .0002);
604 if (dewpoint_sea_level_degc > temperature_sea_level_degc)
605 dewpoint_sea_level_degc = temperature_sea_level_degc;
609 FGEnvironment::_recalc_alt_dewpoint ()
612 // FIXME: this will work only for low
614 dewpoint_degc = dewpoint_sea_level_degc + (elevation_ft * .0002);
615 if (dewpoint_degc > temperature_degc)
616 dewpoint_degc = temperature_degc;
620 FGEnvironment::_recalc_sl_pressure ()
622 pressure_sea_level_inhg =
623 pressure_inhg / _pressure_inhg_table->interpolate(elevation_ft);
627 FGEnvironment::_recalc_alt_pressure ()
630 pressure_sea_level_inhg * _pressure_inhg_table->interpolate(elevation_ft);
634 FGEnvironment::_recalc_density ()
636 double pressure_psf = pressure_inhg * 70.7487;
638 // adjust for humidity
639 // calculations taken from USA Today (oops!) at
640 // http://www.usatoday.com/weather/basics/density-calculations.htm
641 double temperature_degk = temperature_degc + 273.15;
642 double pressure_mb = pressure_inhg * 33.86;
643 double vapor_pressure_mb =
644 6.11 * pow(10.0, 7.5 * dewpoint_degc / (237.7 + dewpoint_degc));
645 double virtual_temperature_degk = temperature_degk / (1 - (vapor_pressure_mb / pressure_mb) * (1.0 - 0.622));
646 double virtual_temperature_degr = virtual_temperature_degk * 1.8;
648 density_slugft3 = pressure_psf / (virtual_temperature_degr * 1718);
649 _recalc_density_tropo_avg_kgm3();
652 // This is used to calculate the average density on the path
653 // of sunlight to the observer for calculating sun-color
655 FGEnvironment::_recalc_density_tropo_avg_kgm3 ()
657 double pressure_mb = pressure_inhg * 33.86;
658 double vaporpressure = 6.11 * pow(10.0, ((7.5 * dewpoint_degc) / (237.7 + dewpoint_degc)));
660 double virtual_temp = (temperature_degc + 273.15) / (1 - 0.379 * (vaporpressure/pressure_mb));
662 double density_half = (100 * pressure_mb * exp(-altitude_half_to_sun_m / 8000))
663 / (287.05 * virtual_temp);
664 double density_tropo = (100 * pressure_mb * exp((-1 * altitude_tropo_top_m) / 8000))
665 / ( 287.05 * virtual_temp);
667 density_tropo_avg_kgm3 = ((density_slugft3 * 515.379) + density_half + density_tropo) / 3;
671 FGEnvironment::_recalc_relative_humidity ()
673 double vaporpressure = 6.11 * pow(10.0, ((7.5 * dewpoint_degc) / ( 237.7 + dewpoint_degc)));
674 double sat_vaporpressure = 6.11 * pow(10.0, ((7.5 * temperature_degc)
675 / ( 237.7 + temperature_degc)) );
676 relative_humidity = 100 * vaporpressure / sat_vaporpressure ;
681 ////////////////////////////////////////////////////////////////////////
683 ////////////////////////////////////////////////////////////////////////
686 do_interp (double a, double b, double fraction)
688 double retval = (a + ((b - a) * fraction));
693 do_interp_deg (double a, double b, double fraction)
697 if (fabs(b-a) > 180) {
703 return fmod(do_interp(a, b, fraction), 360);
707 interpolate (const FGEnvironment * env1, const FGEnvironment * env2,
708 double fraction, FGEnvironment * result)
710 result->set_visibility_m
711 (do_interp(env1->get_visibility_m(),
712 env2->get_visibility_m(),
715 result->set_temperature_sea_level_degc
716 (do_interp(env1->get_temperature_sea_level_degc(),
717 env2->get_temperature_sea_level_degc(),
720 result->set_dewpoint_degc
721 (do_interp(env1->get_dewpoint_sea_level_degc(),
722 env2->get_dewpoint_sea_level_degc(),
725 result->set_pressure_sea_level_inhg
726 (do_interp(env1->get_pressure_sea_level_inhg(),
727 env2->get_pressure_sea_level_inhg(),
730 result->set_wind_from_heading_deg
731 (do_interp_deg(env1->get_wind_from_heading_deg(),
732 env2->get_wind_from_heading_deg(),
735 result->set_wind_speed_kt
736 (do_interp(env1->get_wind_speed_kt(),
737 env2->get_wind_speed_kt(),
740 result->set_elevation_ft
741 (do_interp(env1->get_elevation_ft(),
742 env2->get_elevation_ft(),
745 result->set_turbulence_magnitude_norm
746 (do_interp(env1->get_turbulence_magnitude_norm(),
747 env2->get_turbulence_magnitude_norm(),
750 result->set_turbulence_rate_hz
751 (do_interp(env1->get_turbulence_rate_hz(),
752 env2->get_turbulence_rate_hz(),
756 // end of environment.cxx