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 <boost/tuple/tuple.hpp>
32 #include <simgear/constants.h>
33 #include <simgear/debug/logstream.hxx>
34 #include <simgear/math/interpolater.hxx>
35 #include <simgear/props/props.hxx>
36 #include <simgear/environment/visual_enviro.hxx>
38 #include <Main/fg_props.hxx>
41 #include "environment.hxx"
42 #include "atmosphere.hxx"
45 ////////////////////////////////////////////////////////////////////////
47 ////////////////////////////////////////////////////////////////////////
51 // Calculated based on the ISA standard day, as found at e.g.
52 // http://www.av8n.com/physics/altimetry.htm
54 // Each line of data has 3 elements:
56 // temperature factor (dimensionless ratio of absolute temp),
57 // pressure factor (dimensionless ratio)
58 static double atmosphere_data[][3] = {
59 { -3000.00, 1.021, 1.1133 },
60 { 0.00, 1.000, 1.0000 },
61 { 2952.76, 0.980, 0.8978 },
62 { 5905.51, 0.959, 0.8042 },
63 { 8858.27, 0.939, 0.7187 },
64 { 11811.02, 0.919, 0.6407 },
65 { 14763.78, 0.898, 0.5697 },
66 { 17716.54, 0.878, 0.5052 },
67 { 20669.29, 0.858, 0.4468 },
68 { 23622.05, 0.838, 0.3940 },
69 { 26574.80, 0.817, 0.3463 },
70 { 29527.56, 0.797, 0.3034 },
71 { 32480.31, 0.777, 0.2649 },
72 { 35433.07, 0.756, 0.2305 },
73 { 38385.83, 0.752, 0.2000 },
74 { 41338.58, 0.752, 0.1736 },
75 { 44291.34, 0.752, 0.1506 },
76 { 47244.09, 0.752, 0.1307 },
77 { 50196.85, 0.752, 0.1134 },
78 { 53149.61, 0.752, 0.0984 },
79 { 56102.36, 0.752, 0.0854 },
80 { 59055.12, 0.752, 0.0741 },
81 { 62007.87, 0.752, 0.0643 },
82 { 65000.00, 0.752, 0.0557 },
83 { 68000.00, 0.754, 0.0482 },
84 { 71000.00, 0.758, 0.0418 },
85 { 74000.00, 0.761, 0.0362 },
86 { 77000.00, 0.764, 0.0314 },
87 { 80000.00, 0.767, 0.0273 },
88 { 83000.00, 0.770, 0.0237 },
89 { 86000.00, 0.773, 0.0206 },
90 { 89000.00, 0.777, 0.0179 },
91 { 92000.00, 0.780, 0.0156 },
92 { 95000.00, 0.783, 0.0135 },
93 { 98000.00, 0.786, 0.0118 },
94 { 101000.00, 0.789, 0.0103 },
98 static SGInterpTable * _temperature_degc_table = 0;
99 static SGInterpTable * _pressure_inhg_table = 0;
104 if (_temperature_degc_table != 0)
107 _temperature_degc_table = new SGInterpTable;
108 _pressure_inhg_table = new SGInterpTable;
110 for (int i = 0; atmosphere_data[i][0] != -1; i++) {
111 _temperature_degc_table->addEntry(atmosphere_data[i][0],
112 atmosphere_data[i][1]);
113 _pressure_inhg_table->addEntry(atmosphere_data[i][0],
114 atmosphere_data[i][2]);
120 ////////////////////////////////////////////////////////////////////////
121 // Implementation of FGEnvironment.
122 ////////////////////////////////////////////////////////////////////////
124 void FGEnvironment::_init()
127 visibility_m = 32000;
128 temperature_sea_level_degc = 15;
129 temperature_degc = 15;
130 dewpoint_sea_level_degc = 5; // guess
132 pressure_sea_level_inhg = 29.92;
133 pressure_inhg = 29.92;
134 turbulence_magnitude_norm = 0;
135 turbulence_rate_hz = 1;
136 wind_from_heading_deg = 0;
138 wind_from_north_fps = 0;
139 wind_from_east_fps = 0;
140 wind_from_down_fps = 0;
141 thermal_lift_fps = 0;
143 local_weather_lift_fps=0;
144 altitude_half_to_sun_m = 1000;
145 altitude_tropo_top_m = 10000;
150 _recalc_relative_humidity();
154 FGEnvironment::FGEnvironment()
159 FGEnvironment::FGEnvironment (const FGEnvironment &env)
165 FGEnvironment::~FGEnvironment()
170 FGEnvironment::copy (const FGEnvironment &env)
172 elevation_ft = env.elevation_ft;
173 visibility_m = env.visibility_m;
174 temperature_sea_level_degc = env.temperature_sea_level_degc;
175 temperature_degc = env.temperature_degc;
176 dewpoint_sea_level_degc = env.dewpoint_sea_level_degc;
177 dewpoint_degc = env.dewpoint_degc;
178 pressure_sea_level_inhg = env.pressure_sea_level_inhg;
179 wind_from_heading_deg = env.wind_from_heading_deg;
180 wind_speed_kt = env.wind_speed_kt;
181 wind_from_north_fps = env.wind_from_north_fps;
182 wind_from_east_fps = env.wind_from_east_fps;
183 wind_from_down_fps = env.wind_from_down_fps;
184 thermal_lift_fps = env.thermal_lift_fps;
185 ridge_lift_fps= env.ridge_lift_fps;
186 local_weather_lift_fps = env.local_weather_lift_fps;
187 turbulence_magnitude_norm = env.turbulence_magnitude_norm;
188 turbulence_rate_hz = env.turbulence_rate_hz;
192 maybe_copy_value (FGEnvironment * env, const SGPropertyNode * node,
193 const char * name, void (FGEnvironment::*setter)(double))
195 const SGPropertyNode * child = node->getNode(name);
196 // fragile: depends on not being typed
198 if (child != 0 && child->hasValue() &&
199 child->getStringValue()[0] != '\0') {
200 (env->*setter)(child->getDoubleValue());
208 FGEnvironment::read (const SGPropertyNode * node)
210 bool live_update = set_live_update( false );
211 maybe_copy_value(this, node, "visibility-m",
212 &FGEnvironment::set_visibility_m);
214 maybe_copy_value(this, node, "elevation-ft",
215 &FGEnvironment::set_elevation_ft);
217 if (!maybe_copy_value(this, node, "temperature-sea-level-degc",
218 &FGEnvironment::set_temperature_sea_level_degc)) {
219 if( maybe_copy_value(this, node, "temperature-degc",
220 &FGEnvironment::set_temperature_degc)) {
221 _recalc_sl_temperature();
225 if (!maybe_copy_value(this, node, "dewpoint-sea-level-degc",
226 &FGEnvironment::set_dewpoint_sea_level_degc)) {
227 if( maybe_copy_value(this, node, "dewpoint-degc",
228 &FGEnvironment::set_dewpoint_degc)) {
229 _recalc_sl_dewpoint();
233 if (!maybe_copy_value(this, node, "pressure-sea-level-inhg",
234 &FGEnvironment::set_pressure_sea_level_inhg)) {
235 if( maybe_copy_value(this, node, "pressure-inhg",
236 &FGEnvironment::set_pressure_inhg)) {
237 _recalc_sl_pressure();
241 maybe_copy_value(this, node, "wind-from-heading-deg",
242 &FGEnvironment::set_wind_from_heading_deg);
244 maybe_copy_value(this, node, "wind-speed-kt",
245 &FGEnvironment::set_wind_speed_kt);
247 maybe_copy_value(this, node, "turbulence/magnitude-norm",
248 &FGEnvironment::set_turbulence_magnitude_norm);
250 maybe_copy_value(this, node, "turbulence/rate-hz",
251 &FGEnvironment::set_turbulence_rate_hz);
253 // calculate derived properties here to avoid duplicate expensive computations
256 _recalc_alt_dewpoint();
258 _recalc_relative_humidity();
260 set_live_update(live_update);
265 FGEnvironment::get_visibility_m () const
271 FGEnvironment::get_temperature_sea_level_degc () const
273 return temperature_sea_level_degc;
277 FGEnvironment::get_temperature_degc () const
279 return temperature_degc;
283 FGEnvironment::get_temperature_degf () const
285 return (temperature_degc * 9.0 / 5.0) + 32.0;
289 FGEnvironment::get_dewpoint_sea_level_degc () const
291 return dewpoint_sea_level_degc;
295 FGEnvironment::get_dewpoint_degc () const
297 return dewpoint_degc;
301 FGEnvironment::get_pressure_sea_level_inhg () const
303 return pressure_sea_level_inhg;
307 FGEnvironment::get_pressure_inhg () const
309 return pressure_inhg;
313 FGEnvironment::get_density_slugft3 () const
315 return density_slugft3;
319 FGEnvironment::get_relative_humidity () const
321 return relative_humidity;
325 FGEnvironment::get_density_tropo_avg_kgm3 () const
327 return density_tropo_avg_kgm3;
331 FGEnvironment::get_altitude_half_to_sun_m () const
333 return altitude_half_to_sun_m;
337 FGEnvironment::get_altitude_tropo_top_m () const
339 return altitude_tropo_top_m;
343 FGEnvironment::get_wind_from_heading_deg () const
345 return wind_from_heading_deg;
349 FGEnvironment::get_wind_speed_kt () const
351 return wind_speed_kt;
355 FGEnvironment::get_wind_from_north_fps () const
357 return wind_from_north_fps;
361 FGEnvironment::get_wind_from_east_fps () const
363 return wind_from_east_fps;
367 FGEnvironment::get_wind_from_down_fps () const
369 return wind_from_down_fps;
373 FGEnvironment::get_thermal_lift_fps () const
375 return thermal_lift_fps;
379 FGEnvironment::get_ridge_lift_fps () const
381 return ridge_lift_fps;
385 FGEnvironment::get_local_weather_lift_fps () const
387 return local_weather_lift_fps;
391 FGEnvironment::get_turbulence_magnitude_norm () const
393 if( sgEnviro.get_turbulence_enable_state() )
394 if (fgGetBool("/environment/params/real-world-weather-fetch") == true)
395 return sgEnviro.get_cloud_turbulence();
396 return turbulence_magnitude_norm;
400 FGEnvironment::get_turbulence_rate_hz () const
402 return turbulence_rate_hz;
406 FGEnvironment::get_elevation_ft () const
412 FGEnvironment::set_visibility_m (double v)
418 FGEnvironment::set_temperature_sea_level_degc (double t)
420 temperature_sea_level_degc = t;
421 if (dewpoint_sea_level_degc > t)
422 dewpoint_sea_level_degc = t;
430 FGEnvironment::set_temperature_degc (double t)
432 temperature_degc = t;
434 _recalc_sl_temperature();
435 _recalc_sl_pressure();
438 _recalc_relative_humidity();
443 FGEnvironment::set_dewpoint_sea_level_degc (double t)
445 dewpoint_sea_level_degc = t;
446 if (temperature_sea_level_degc < t)
447 temperature_sea_level_degc = t;
449 _recalc_alt_dewpoint();
455 FGEnvironment::set_dewpoint_degc (double t)
459 _recalc_sl_dewpoint();
461 _recalc_relative_humidity();
466 FGEnvironment::set_pressure_sea_level_inhg (double p)
468 pressure_sea_level_inhg = p;
476 FGEnvironment::set_pressure_inhg (double p)
480 _recalc_sl_pressure();
486 FGEnvironment::set_wind_from_heading_deg (double h)
488 wind_from_heading_deg = h;
495 FGEnvironment::set_wind_speed_kt (double s)
504 FGEnvironment::set_wind_from_north_fps (double n)
506 wind_from_north_fps = n;
513 FGEnvironment::set_wind_from_east_fps (double e)
515 wind_from_east_fps = e;
522 FGEnvironment::set_wind_from_down_fps (double d)
524 wind_from_down_fps = d;
531 FGEnvironment::set_thermal_lift_fps (double th)
533 thermal_lift_fps = th;
540 FGEnvironment::set_ridge_lift_fps (double ri)
549 FGEnvironment::set_local_weather_lift_fps (double lwl)
551 local_weather_lift_fps = lwl;
558 FGEnvironment::set_turbulence_magnitude_norm (double t)
560 turbulence_magnitude_norm = t;
564 FGEnvironment::set_turbulence_rate_hz (double r)
566 turbulence_rate_hz = r;
570 FGEnvironment::set_elevation_ft (double e)
575 _recalc_alt_dewpoint();
577 _recalc_relative_humidity();
582 FGEnvironment::set_altitude_half_to_sun_m (double alt)
584 altitude_half_to_sun_m = alt;
586 _recalc_density_tropo_avg_kgm3();
591 FGEnvironment::set_altitude_tropo_top_m (double alt)
593 altitude_tropo_top_m = alt;
595 _recalc_density_tropo_avg_kgm3();
601 FGEnvironment::_recalc_hdgspd ()
605 if (wind_from_east_fps == 0) {
606 angle_rad = (wind_from_north_fps >= 0 ? SGD_PI_2 : -SGD_PI_2);
608 angle_rad = atan(wind_from_north_fps/wind_from_east_fps);
610 wind_from_heading_deg = angle_rad * SGD_RADIANS_TO_DEGREES;
611 if (wind_from_east_fps >= 0)
612 wind_from_heading_deg = 90 - wind_from_heading_deg;
614 wind_from_heading_deg = 270 - wind_from_heading_deg;
617 // FIXME: Windspeed can become negative with these formulas.
618 // which can cause problems for animations that rely
619 // on the windspeed property.
621 wind_speed_kt = fabs(wind_from_east_fps
622 * SG_METER_TO_NM * SG_FEET_TO_METER * 3600);
624 wind_speed_kt = (wind_from_north_fps / sin(angle_rad))
625 * SG_METER_TO_NM * SG_FEET_TO_METER * 3600;
627 wind_speed_kt = sqrt(wind_from_north_fps * wind_from_north_fps +
628 wind_from_east_fps * wind_from_east_fps)
629 * SG_METER_TO_NM * SG_FEET_TO_METER * 3600;
634 FGEnvironment::_recalc_ne ()
637 wind_speed_kt * SG_NM_TO_METER * SG_METER_TO_FEET * (1.0/3600);
639 wind_from_north_fps = speed_fps *
640 cos(wind_from_heading_deg * SGD_DEGREES_TO_RADIANS);
641 wind_from_east_fps = speed_fps *
642 sin(wind_from_heading_deg * SGD_DEGREES_TO_RADIANS);
646 FGEnvironment::_recalc_updraft ()
648 wind_from_down_fps = thermal_lift_fps + ridge_lift_fps + local_weather_lift_fps ;
651 // Intended to help with the interpretation of METAR data,
652 // not for random in-flight outside-air temperatures.
654 FGEnvironment::_recalc_sl_temperature ()
659 SG_LOG(SG_GENERAL, SG_DEBUG, "recalc_sl_temperature: using "
660 << temperature_degc << " @ " << elevation_ft << " :: " << this);
664 if (elevation_ft * atmodel::foot >= ISA_def[1].height) {
665 SG_LOG(SG_GENERAL, SG_ALERT, "recalc_sl_temperature: "
666 << "valid only in troposphere, not " << elevation_ft);
670 // Clamp: temperature of the stratosphere, in degrees C:
671 double t_strato = ISA_def[1].temp - atmodel::freezing;
672 if (temperature_degc < t_strato) temperature_sea_level_degc = t_strato;
673 else temperature_sea_level_degc =
674 temperature_degc + elevation_ft * atmodel::foot * ISA_def[0].lapse;
676 // Alternative implemenation:
677 // else temperature_sea_level_inhg = T_layer(0., elevation_ft * foot,
678 // pressure_inhg * inHg, temperature_degc + freezing, ISA_def[0].lapse) - freezing;
682 FGEnvironment::_recalc_sl_dewpoint ()
685 // FIXME: this will work only for low
687 dewpoint_sea_level_degc = dewpoint_degc + (elevation_ft * .0002);
688 if (dewpoint_sea_level_degc > temperature_sea_level_degc)
689 dewpoint_sea_level_degc = temperature_sea_level_degc;
693 FGEnvironment::_recalc_alt_dewpoint ()
696 // FIXME: this will work only for low
698 dewpoint_degc = dewpoint_sea_level_degc + (elevation_ft * .0002);
699 if (dewpoint_degc > temperature_degc)
700 dewpoint_degc = temperature_degc;
704 FGEnvironment::_recalc_sl_pressure ()
706 using namespace atmodel;
709 SG_LOG(SG_GENERAL, SG_ALERT, "recalc_sl_pressure: using "
710 << pressure_inhg << " and "
711 << temperature_degc << " @ " << elevation_ft << " :: " << this);
714 pressure_sea_level_inhg = P_layer(0., elevation_ft * foot,
715 pressure_inhg * inHg, temperature_degc + freezing, ISA_def[0].lapse) / inHg;
718 // This gets called at frame rate, to account for the aircraft's
719 // changing altitude.
720 // Called by set_elevation_ft() which is called by FGEnvironmentMgr::update
723 FGEnvironment::_recalc_alt_pt ()
725 using namespace atmodel;
729 if (++count % 1000 == 0) {
730 SG_LOG(SG_GENERAL, SG_ALERT,
731 "recalc_alt_pt for: " << elevation_ft
732 << " using " << pressure_sea_level_inhg
733 << " and " << temperature_sea_level_degc
740 boost::tie(press, temp) = PT_vs_hpt(elevation_ft * foot,
741 pressure_sea_level_inhg * inHg, temperature_sea_level_degc + freezing);
742 temperature_degc = temp - freezing;
743 pressure_inhg = press / inHg;
747 FGEnvironment::_recalc_density ()
749 double pressure_psf = pressure_inhg * 70.7487;
751 // adjust for humidity
752 // calculations taken from USA Today (oops!) at
753 // http://www.usatoday.com/weather/basics/density-calculations.htm
754 double temperature_degk = temperature_degc + 273.15;
755 double pressure_mb = pressure_inhg * 33.86;
756 double vapor_pressure_mb =
757 6.11 * pow(10.0, 7.5 * dewpoint_degc / (237.7 + dewpoint_degc));
758 double virtual_temperature_degk = temperature_degk / (1 - (vapor_pressure_mb / pressure_mb) * (1.0 - 0.622));
759 double virtual_temperature_degr = virtual_temperature_degk * 1.8;
761 density_slugft3 = pressure_psf / (virtual_temperature_degr * 1718);
762 _recalc_density_tropo_avg_kgm3();
765 // This is used to calculate the average density on the path
766 // of sunlight to the observer for calculating sun-color
768 FGEnvironment::_recalc_density_tropo_avg_kgm3 ()
770 double pressure_mb = pressure_inhg * 33.86;
771 double vaporpressure = 6.11 * pow(10.0, ((7.5 * dewpoint_degc) / (237.7 + dewpoint_degc)));
773 double virtual_temp = (temperature_degc + 273.15) / (1 - 0.379 * (vaporpressure/pressure_mb));
775 double density_half = (100 * pressure_mb * exp(-altitude_half_to_sun_m / 8000))
776 / (287.05 * virtual_temp);
777 double density_tropo = (100 * pressure_mb * exp((-1 * altitude_tropo_top_m) / 8000))
778 / ( 287.05 * virtual_temp);
780 density_tropo_avg_kgm3 = ((density_slugft3 * 515.379) + density_half + density_tropo) / 3;
784 FGEnvironment::_recalc_relative_humidity ()
787 double vaporpressure = 6.11 * pow(10.0, ((7.5 * dewpoint_degc) / ( 237.7 + dewpoint_degc)));
788 double sat_vaporpressure = 6.11 * pow(10.0, ((7.5 * temperature_degc)
789 / ( 237.7 + temperature_degc)) );
790 relative_humidity = 100 * vaporpressure / sat_vaporpressure ;
792 with a little algebra, this gets the same result and spares two multiplications and one pow()
794 double a = (7.5 * dewpoint_degc) / ( 237.7 + dewpoint_degc);
795 double b = (7.5 * temperature_degc) / ( 237.7 + temperature_degc);
796 relative_humidity = 100 * pow(10.0,a-b);
800 FGEnvironment::set_live_update( bool _live_update )
802 bool b = live_update;
803 live_update = _live_update;
808 ////////////////////////////////////////////////////////////////////////
810 ////////////////////////////////////////////////////////////////////////
813 do_interp (double a, double b, double fraction)
815 double retval = (a + ((b - a) * fraction));
820 do_interp_deg (double a, double b, double fraction)
824 if (fabs(b-a) > 180) {
830 return fmod(do_interp(a, b, fraction), 360);
834 interpolate (const FGEnvironment * env1, const FGEnvironment * env2,
835 double fraction, FGEnvironment * result)
837 // don't calculate each internal property every time we set a single value
838 // we trigger that at the end of the interpolation process
839 bool live_update = result->set_live_update( false );
841 result->set_visibility_m
842 (do_interp(env1->get_visibility_m(),
843 env2->get_visibility_m(),
846 result->set_temperature_sea_level_degc
847 (do_interp(env1->get_temperature_sea_level_degc(),
848 env2->get_temperature_sea_level_degc(),
851 result->set_dewpoint_sea_level_degc
852 (do_interp(env1->get_dewpoint_sea_level_degc(),
853 env2->get_dewpoint_sea_level_degc(),
856 result->set_pressure_sea_level_inhg
857 (do_interp(env1->get_pressure_sea_level_inhg(),
858 env2->get_pressure_sea_level_inhg(),
861 result->set_wind_from_heading_deg
862 (do_interp_deg(env1->get_wind_from_heading_deg(),
863 env2->get_wind_from_heading_deg(),
866 result->set_wind_speed_kt
867 (do_interp(env1->get_wind_speed_kt(),
868 env2->get_wind_speed_kt(),
871 result->set_elevation_ft
872 (do_interp(env1->get_elevation_ft(),
873 env2->get_elevation_ft(),
876 result->set_turbulence_magnitude_norm
877 (do_interp(env1->get_turbulence_magnitude_norm(),
878 env2->get_turbulence_magnitude_norm(),
881 result->set_turbulence_rate_hz
882 (do_interp(env1->get_turbulence_rate_hz(),
883 env2->get_turbulence_rate_hz(),
886 // calculate derived properties here to avoid duplicate expensive computations
887 result->_recalc_ne();
888 result->_recalc_alt_pt();
889 result->_recalc_alt_dewpoint();
890 result->_recalc_density();
891 result->_recalc_relative_humidity();
893 result->set_live_update(live_update);
896 // end of environment.cxx