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>
34 #include <simgear/constants.h>
35 #include <simgear/debug/logstream.hxx>
36 #include <simgear/math/interpolater.hxx>
37 #include <simgear/props/props.hxx>
38 #include <simgear/environment/visual_enviro.hxx>
40 #include <Main/fg_props.hxx>
43 #include "environment.hxx"
44 #include "atmosphere.hxx"
47 ////////////////////////////////////////////////////////////////////////
49 ////////////////////////////////////////////////////////////////////////
53 // Calculated based on the ISA standard day, as found at e.g.
54 // http://www.av8n.com/physics/altimetry.htm
56 // Each line of data has 3 elements:
58 // temperature factor (dimensionless ratio of absolute temp),
59 // pressure factor (dimensionless ratio)
60 static double atmosphere_data[][3] = {
61 { -3000.00, 1.021, 1.1133 },
62 { 0.00, 1.000, 1.0000 },
63 { 2952.76, 0.980, 0.8978 },
64 { 5905.51, 0.959, 0.8042 },
65 { 8858.27, 0.939, 0.7187 },
66 { 11811.02, 0.919, 0.6407 },
67 { 14763.78, 0.898, 0.5697 },
68 { 17716.54, 0.878, 0.5052 },
69 { 20669.29, 0.858, 0.4468 },
70 { 23622.05, 0.838, 0.3940 },
71 { 26574.80, 0.817, 0.3463 },
72 { 29527.56, 0.797, 0.3034 },
73 { 32480.31, 0.777, 0.2649 },
74 { 35433.07, 0.756, 0.2305 },
75 { 38385.83, 0.752, 0.2000 },
76 { 41338.58, 0.752, 0.1736 },
77 { 44291.34, 0.752, 0.1506 },
78 { 47244.09, 0.752, 0.1307 },
79 { 50196.85, 0.752, 0.1134 },
80 { 53149.61, 0.752, 0.0984 },
81 { 56102.36, 0.752, 0.0854 },
82 { 59055.12, 0.752, 0.0741 },
83 { 62007.87, 0.752, 0.0643 },
84 { 65000.00, 0.752, 0.0557 },
85 { 68000.00, 0.754, 0.0482 },
86 { 71000.00, 0.758, 0.0418 },
87 { 74000.00, 0.761, 0.0362 },
88 { 77000.00, 0.764, 0.0314 },
89 { 80000.00, 0.767, 0.0273 },
90 { 83000.00, 0.770, 0.0237 },
91 { 86000.00, 0.773, 0.0206 },
92 { 89000.00, 0.777, 0.0179 },
93 { 92000.00, 0.780, 0.0156 },
94 { 95000.00, 0.783, 0.0135 },
95 { 98000.00, 0.786, 0.0118 },
96 { 101000.00, 0.789, 0.0103 },
100 static SGInterpTable * _temperature_degc_table = 0;
101 static SGInterpTable * _pressure_inhg_table = 0;
106 if (_temperature_degc_table != 0)
109 _temperature_degc_table = new SGInterpTable;
110 _pressure_inhg_table = new SGInterpTable;
112 for (int i = 0; atmosphere_data[i][0] != -1; i++) {
113 _temperature_degc_table->addEntry(atmosphere_data[i][0],
114 atmosphere_data[i][1]);
115 _pressure_inhg_table->addEntry(atmosphere_data[i][0],
116 atmosphere_data[i][2]);
122 ////////////////////////////////////////////////////////////////////////
123 // Implementation of FGEnvironment.
124 ////////////////////////////////////////////////////////////////////////
126 void FGEnvironment::_init()
129 visibility_m = 32000;
130 temperature_sea_level_degc = 15;
131 temperature_degc = 15;
132 dewpoint_sea_level_degc = 5; // guess
134 pressure_sea_level_inhg = 29.92;
135 pressure_inhg = 29.92;
136 turbulence_magnitude_norm = 0;
137 turbulence_rate_hz = 1;
138 wind_from_heading_deg = 0;
140 wind_from_north_fps = 0;
141 wind_from_east_fps = 0;
142 wind_from_down_fps = 0;
143 thermal_lift_fps = 0;
145 altitude_half_to_sun_m = 1000;
146 altitude_tropo_top_m = 10000;
151 _recalc_relative_humidity();
155 FGEnvironment::FGEnvironment()
160 FGEnvironment::FGEnvironment (const FGEnvironment &env)
166 FGEnvironment::~FGEnvironment()
171 FGEnvironment::copy (const FGEnvironment &env)
173 elevation_ft = env.elevation_ft;
174 visibility_m = env.visibility_m;
175 temperature_sea_level_degc = env.temperature_sea_level_degc;
176 temperature_degc = env.temperature_degc;
177 dewpoint_sea_level_degc = env.dewpoint_sea_level_degc;
178 dewpoint_degc = env.dewpoint_degc;
179 pressure_sea_level_inhg = env.pressure_sea_level_inhg;
180 wind_from_heading_deg = env.wind_from_heading_deg;
181 wind_speed_kt = env.wind_speed_kt;
182 wind_from_north_fps = env.wind_from_north_fps;
183 wind_from_east_fps = env.wind_from_east_fps;
184 wind_from_down_fps = env.wind_from_down_fps;
185 thermal_lift_fps = env.thermal_lift_fps;
186 ridge_lift_fps= env.ridge_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 if (!maybe_copy_value(this, node, "temperature-sea-level-degc",
215 &FGEnvironment::set_temperature_sea_level_degc))
216 maybe_copy_value(this, node, "temperature-degc",
217 &FGEnvironment::set_temperature_degc);
219 if (!maybe_copy_value(this, node, "dewpoint-sea-level-degc",
220 &FGEnvironment::set_dewpoint_sea_level_degc))
221 maybe_copy_value(this, node, "dewpoint-degc",
222 &FGEnvironment::set_dewpoint_degc);
224 if (!maybe_copy_value(this, node, "pressure-sea-level-inhg",
225 &FGEnvironment::set_pressure_sea_level_inhg))
226 maybe_copy_value(this, node, "pressure-inhg",
227 &FGEnvironment::set_pressure_inhg);
229 maybe_copy_value(this, node, "wind-from-heading-deg",
230 &FGEnvironment::set_wind_from_heading_deg);
232 maybe_copy_value(this, node, "wind-speed-kt",
233 &FGEnvironment::set_wind_speed_kt);
235 maybe_copy_value(this, node, "elevation-ft",
236 &FGEnvironment::set_elevation_ft);
238 maybe_copy_value(this, node, "turbulence/magnitude-norm",
239 &FGEnvironment::set_turbulence_magnitude_norm);
241 maybe_copy_value(this, node, "turbulence/rate-hz",
242 &FGEnvironment::set_turbulence_rate_hz);
243 // calculate derived properties here to avoid duplicate expensive computations
246 _recalc_alt_dewpoint();
248 _recalc_relative_humidity();
250 set_live_update(live_update);
255 FGEnvironment::get_visibility_m () const
261 FGEnvironment::get_temperature_sea_level_degc () const
263 return temperature_sea_level_degc;
267 FGEnvironment::get_temperature_degc () const
269 return temperature_degc;
273 FGEnvironment::get_temperature_degf () const
275 return (temperature_degc * 9.0 / 5.0) + 32.0;
279 FGEnvironment::get_dewpoint_sea_level_degc () const
281 return dewpoint_sea_level_degc;
285 FGEnvironment::get_dewpoint_degc () const
287 return dewpoint_degc;
291 FGEnvironment::get_pressure_sea_level_inhg () const
293 return pressure_sea_level_inhg;
297 FGEnvironment::get_pressure_inhg () const
299 return pressure_inhg;
303 FGEnvironment::get_density_slugft3 () const
305 return density_slugft3;
309 FGEnvironment::get_relative_humidity () const
311 return relative_humidity;
315 FGEnvironment::get_density_tropo_avg_kgm3 () const
317 return density_tropo_avg_kgm3;
321 FGEnvironment::get_altitude_half_to_sun_m () const
323 return altitude_half_to_sun_m;
327 FGEnvironment::get_altitude_tropo_top_m () const
329 return altitude_tropo_top_m;
333 FGEnvironment::get_wind_from_heading_deg () const
335 return wind_from_heading_deg;
339 FGEnvironment::get_wind_speed_kt () const
341 return wind_speed_kt;
345 FGEnvironment::get_wind_from_north_fps () const
347 return wind_from_north_fps;
351 FGEnvironment::get_wind_from_east_fps () const
353 return wind_from_east_fps;
357 FGEnvironment::get_wind_from_down_fps () const
359 return wind_from_down_fps;
363 FGEnvironment::get_thermal_lift_fps () const
365 return thermal_lift_fps;
369 FGEnvironment::get_ridge_lift_fps () const
371 return ridge_lift_fps;
375 FGEnvironment::get_turbulence_magnitude_norm () const
377 if( sgEnviro.get_turbulence_enable_state() )
378 if (fgGetBool("/environment/params/real-world-weather-fetch") == true)
379 return sgEnviro.get_cloud_turbulence();
380 return turbulence_magnitude_norm;
384 FGEnvironment::get_turbulence_rate_hz () const
386 return turbulence_rate_hz;
390 FGEnvironment::get_elevation_ft () const
396 FGEnvironment::set_visibility_m (double v)
402 FGEnvironment::set_temperature_sea_level_degc (double t)
404 temperature_sea_level_degc = t;
405 if (dewpoint_sea_level_degc > t)
406 dewpoint_sea_level_degc = t;
414 FGEnvironment::set_temperature_degc (double t)
416 temperature_degc = t;
418 _recalc_sl_temperature();
419 _recalc_sl_pressure();
422 _recalc_relative_humidity();
427 FGEnvironment::set_dewpoint_sea_level_degc (double t)
429 dewpoint_sea_level_degc = t;
430 if (temperature_sea_level_degc < t)
431 temperature_sea_level_degc = t;
433 _recalc_alt_dewpoint();
439 FGEnvironment::set_dewpoint_degc (double t)
443 _recalc_sl_dewpoint();
445 _recalc_relative_humidity();
450 FGEnvironment::set_pressure_sea_level_inhg (double p)
452 pressure_sea_level_inhg = p;
460 FGEnvironment::set_pressure_inhg (double p)
464 _recalc_sl_pressure();
470 FGEnvironment::set_wind_from_heading_deg (double h)
472 wind_from_heading_deg = h;
479 FGEnvironment::set_wind_speed_kt (double s)
488 FGEnvironment::set_wind_from_north_fps (double n)
490 wind_from_north_fps = n;
497 FGEnvironment::set_wind_from_east_fps (double e)
499 wind_from_east_fps = e;
506 FGEnvironment::set_wind_from_down_fps (double d)
508 wind_from_down_fps = d;
515 FGEnvironment::set_thermal_lift_fps (double th)
517 thermal_lift_fps = th;
524 FGEnvironment::set_ridge_lift_fps (double ri)
533 FGEnvironment::set_turbulence_magnitude_norm (double t)
535 turbulence_magnitude_norm = t;
539 FGEnvironment::set_turbulence_rate_hz (double r)
541 turbulence_rate_hz = r;
545 FGEnvironment::set_elevation_ft (double e)
549 _recalc_alt_dewpoint();
552 _recalc_relative_humidity();
557 FGEnvironment::set_altitude_half_to_sun_m (double alt)
559 altitude_half_to_sun_m = alt;
561 _recalc_density_tropo_avg_kgm3();
566 FGEnvironment::set_altitude_tropo_top_m (double alt)
568 altitude_tropo_top_m = alt;
570 _recalc_density_tropo_avg_kgm3();
576 FGEnvironment::_recalc_hdgspd ()
580 if (wind_from_east_fps == 0) {
581 angle_rad = (wind_from_north_fps >= 0 ? SGD_PI_2 : -SGD_PI_2);
583 angle_rad = atan(wind_from_north_fps/wind_from_east_fps);
585 wind_from_heading_deg = angle_rad * SGD_RADIANS_TO_DEGREES;
586 if (wind_from_east_fps >= 0)
587 wind_from_heading_deg = 90 - wind_from_heading_deg;
589 wind_from_heading_deg = 270 - wind_from_heading_deg;
592 // FIXME: Windspeed can become negative with these formulas.
593 // which can cause problems for animations that rely
594 // on the windspeed property.
596 wind_speed_kt = fabs(wind_from_east_fps
597 * SG_METER_TO_NM * SG_FEET_TO_METER * 3600);
599 wind_speed_kt = (wind_from_north_fps / sin(angle_rad))
600 * SG_METER_TO_NM * SG_FEET_TO_METER * 3600;
602 wind_speed_kt = sqrt(wind_from_north_fps * wind_from_north_fps +
603 wind_from_east_fps * wind_from_east_fps)
604 * SG_METER_TO_NM * SG_FEET_TO_METER * 3600;
609 FGEnvironment::_recalc_ne ()
612 wind_speed_kt * SG_NM_TO_METER * SG_METER_TO_FEET * (1.0/3600);
614 wind_from_north_fps = speed_fps *
615 cos(wind_from_heading_deg * SGD_DEGREES_TO_RADIANS);
616 wind_from_east_fps = speed_fps *
617 sin(wind_from_heading_deg * SGD_DEGREES_TO_RADIANS);
621 FGEnvironment::_recalc_updraft ()
623 wind_from_down_fps = thermal_lift_fps + ridge_lift_fps ;
626 // Intended to help with the interpretation of METAR data,
627 // not for random in-flight outside-air temperatures.
629 FGEnvironment::_recalc_sl_temperature ()
634 SG_LOG(SG_GENERAL, SG_DEBUG, "recalc_sl_temperature: using "
635 << temperature_degc << " @ " << elevation_ft << " :: " << this);
639 if (elevation_ft >= ISA_def[1].height) {
640 SG_LOG(SG_GENERAL, SG_ALERT, "recalc_sl_temperature: "
641 << "valid only in troposphere, not " << elevation_ft);
645 // Clamp: temperature of the stratosphere, in degrees C:
646 double t_strato = ISA_def[1].temp - atmodel::freezing;
647 if (temperature_degc < t_strato) temperature_sea_level_degc = t_strato;
648 else temperature_sea_level_degc =
649 temperature_degc - elevation_ft * ISA_def[0].lapse;
651 // Alternative implemenation:
652 // else temperature_sea_level_inhg = T_layer(0., elevation_ft * foot,
653 // pressure_inhg * inHg, temperature_degc + freezing, ISA_def[0].lapse) - freezing;
657 FGEnvironment::_recalc_sl_dewpoint ()
660 // FIXME: this will work only for low
662 dewpoint_sea_level_degc = dewpoint_degc + (elevation_ft * .0002);
663 if (dewpoint_sea_level_degc > temperature_sea_level_degc)
664 dewpoint_sea_level_degc = temperature_sea_level_degc;
668 FGEnvironment::_recalc_alt_dewpoint ()
671 // FIXME: this will work only for low
673 dewpoint_degc = dewpoint_sea_level_degc + (elevation_ft * .0002);
674 if (dewpoint_degc > temperature_degc)
675 dewpoint_degc = temperature_degc;
679 FGEnvironment::_recalc_sl_pressure ()
681 using namespace atmodel;
684 SG_LOG(SG_GENERAL, SG_ALERT, "recalc_sl_pressure: using "
685 << pressure_inhg << " and "
686 << temperature_degc << " @ " << elevation_ft << " :: " << this);
689 pressure_sea_level_inhg = P_layer(0., elevation_ft * foot,
690 pressure_inhg * inHg, temperature_degc + freezing, ISA_def[0].lapse) / inHg;
693 // This gets called at frame rate, to account for the aircraft's
694 // changing altitude.
695 // Called by set_elevation_ft() which is called by FGEnvironmentMgr::update
698 FGEnvironment::_recalc_alt_pt ()
700 using namespace atmodel;
704 if (++count % 1000 == 0) {
705 SG_LOG(SG_GENERAL, SG_ALERT,
706 "recalc_alt_pt for: " << elevation_ft
707 << " using " << pressure_sea_level_inhg
708 << " and " << temperature_sea_level_degc
715 boost::tie(press, temp) = PT_vs_hpt(elevation_ft * foot,
716 pressure_sea_level_inhg * inHg, temperature_sea_level_degc + freezing);
717 temperature_degc = temp - freezing;
718 pressure_inhg = press / inHg;
722 FGEnvironment::_recalc_density ()
724 double pressure_psf = pressure_inhg * 70.7487;
726 // adjust for humidity
727 // calculations taken from USA Today (oops!) at
728 // http://www.usatoday.com/weather/basics/density-calculations.htm
729 double temperature_degk = temperature_degc + 273.15;
730 double pressure_mb = pressure_inhg * 33.86;
731 double vapor_pressure_mb =
732 6.11 * pow(10.0, 7.5 * dewpoint_degc / (237.7 + dewpoint_degc));
733 double virtual_temperature_degk = temperature_degk / (1 - (vapor_pressure_mb / pressure_mb) * (1.0 - 0.622));
734 double virtual_temperature_degr = virtual_temperature_degk * 1.8;
736 density_slugft3 = pressure_psf / (virtual_temperature_degr * 1718);
737 _recalc_density_tropo_avg_kgm3();
740 // This is used to calculate the average density on the path
741 // of sunlight to the observer for calculating sun-color
743 FGEnvironment::_recalc_density_tropo_avg_kgm3 ()
745 double pressure_mb = pressure_inhg * 33.86;
746 double vaporpressure = 6.11 * pow(10.0, ((7.5 * dewpoint_degc) / (237.7 + dewpoint_degc)));
748 double virtual_temp = (temperature_degc + 273.15) / (1 - 0.379 * (vaporpressure/pressure_mb));
750 double density_half = (100 * pressure_mb * exp(-altitude_half_to_sun_m / 8000))
751 / (287.05 * virtual_temp);
752 double density_tropo = (100 * pressure_mb * exp((-1 * altitude_tropo_top_m) / 8000))
753 / ( 287.05 * virtual_temp);
755 density_tropo_avg_kgm3 = ((density_slugft3 * 515.379) + density_half + density_tropo) / 3;
759 FGEnvironment::_recalc_relative_humidity ()
762 double vaporpressure = 6.11 * pow(10.0, ((7.5 * dewpoint_degc) / ( 237.7 + dewpoint_degc)));
763 double sat_vaporpressure = 6.11 * pow(10.0, ((7.5 * temperature_degc)
764 / ( 237.7 + temperature_degc)) );
765 relative_humidity = 100 * vaporpressure / sat_vaporpressure ;
767 with a little algebra, this gets the same result and spares two multiplications and one pow()
769 double a = (7.5 * dewpoint_degc) / ( 237.7 + dewpoint_degc);
770 double b = (7.5 * temperature_degc) / ( 237.7 + temperature_degc);
771 relative_humidity = 100 * pow(10.0,a-b);
775 FGEnvironment::set_live_update( bool _live_update )
777 bool b = live_update;
778 live_update = _live_update;
783 ////////////////////////////////////////////////////////////////////////
785 ////////////////////////////////////////////////////////////////////////
788 do_interp (double a, double b, double fraction)
790 double retval = (a + ((b - a) * fraction));
795 do_interp_deg (double a, double b, double fraction)
799 if (fabs(b-a) > 180) {
805 return fmod(do_interp(a, b, fraction), 360);
809 interpolate (const FGEnvironment * env1, const FGEnvironment * env2,
810 double fraction, FGEnvironment * result)
812 // don't calculate each internal property every time we set a single value
813 // we trigger that at the end of the interpolation process
814 bool live_update = result->set_live_update( false );
816 result->set_visibility_m
817 (do_interp(env1->get_visibility_m(),
818 env2->get_visibility_m(),
821 result->set_temperature_sea_level_degc
822 (do_interp(env1->get_temperature_sea_level_degc(),
823 env2->get_temperature_sea_level_degc(),
826 result->set_dewpoint_degc
827 (do_interp(env1->get_dewpoint_sea_level_degc(),
828 env2->get_dewpoint_sea_level_degc(),
831 result->set_pressure_sea_level_inhg
832 (do_interp(env1->get_pressure_sea_level_inhg(),
833 env2->get_pressure_sea_level_inhg(),
836 result->set_wind_from_heading_deg
837 (do_interp_deg(env1->get_wind_from_heading_deg(),
838 env2->get_wind_from_heading_deg(),
841 result->set_wind_speed_kt
842 (do_interp(env1->get_wind_speed_kt(),
843 env2->get_wind_speed_kt(),
846 result->set_elevation_ft
847 (do_interp(env1->get_elevation_ft(),
848 env2->get_elevation_ft(),
851 result->set_turbulence_magnitude_norm
852 (do_interp(env1->get_turbulence_magnitude_norm(),
853 env2->get_turbulence_magnitude_norm(),
856 result->set_turbulence_rate_hz
857 (do_interp(env1->get_turbulence_rate_hz(),
858 env2->get_turbulence_rate_hz(),
861 // calculate derived properties here to avoid duplicate expensive computations
862 result->_recalc_ne();
863 result->_recalc_alt_pt();
864 result->_recalc_alt_dewpoint();
865 result->_recalc_density();
866 result->_recalc_relative_humidity();
868 result->set_live_update(live_update);
871 // end of environment.cxx