28#include <simgear/props/props.hxx>
29#include <simgear/math/SGMath.hxx>
50static 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 },
90static SGInterpTable * _temperature_degc_table = 0;
91static SGInterpTable * _pressure_inhg_table = 0;
96 if (_temperature_degc_table != 0)
99 _temperature_degc_table =
new SGInterpTable;
100 _pressure_inhg_table =
new SGInterpTable;
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]);
116void FGEnvironment::_init()
121 visibility_m = 32000;
122 temperature_sea_level_degc = 15;
123 temperature_degc = 15;
124 dewpoint_sea_level_degc = 5;
126 pressure_sea_level_inhg = 29.92;
127 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 altitude_half_to_sun_m = 1000;
137 altitude_tropo_top_m = 10000;
142 _recalc_relative_humidity();
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;
199 const SGPropertyNode * child = node->getNode(
name);
202 if (child != 0 && child->hasValue() &&
203 child->getStringValue()[0] !=
'\0') {
204 (env->*setter)(child->getDoubleValue());
225 _recalc_sl_temperature();
233 _recalc_sl_dewpoint();
241 _recalc_sl_pressure();
257 const SGPropertyNode* child = node->getNode(
"atmosphere/is-isa");
258 if (child != 0 && child->hasValue()) {
265 _recalc_alt_dewpoint();
267 _recalc_relative_humidity();
274 _tiedProperties.setRoot( base );
276 _tiedProperties.Tie(
"visibility-m",
this,
280 _tiedProperties.Tie(
"elevation-ft",
this,
284 _tiedProperties.Tie(
"temperature-sea-level-degc",
this,
288 _tiedProperties.Tie(
"temperature-degc",
this,
292 _tiedProperties.Tie(
"dewpoint-sea-level-degc",
this,
296 _tiedProperties.Tie(
"dewpoint-degc",
this,
300 _tiedProperties.Tie(
"pressure-sea-level-inhg",
this,
304 _tiedProperties.Tie(
"pressure-inhg",
this,
308 _tiedProperties.Tie(
"atmosphere/altitude-half-to-sun",
this,
312 _tiedProperties.Tie(
"atmosphere/altitude-troposphere-top",
this,
316 _tiedProperties.Tie(
"wind-from-heading-deg",
this,
320 _tiedProperties.Tie(
"wind-speed-kt",
this,
324 _tiedProperties.Tie(
"wind-from-north-fps",
this,
328 _tiedProperties.Tie(
"wind-from-east-fps",
this,
332 _tiedProperties.Tie(
"wind-from-down-fps",
this,
336 _tiedProperties.Tie(
"turbulence/magnitude-norm",
this,
340 _tiedProperties.Tie(
"turbulence/rate-hz",
this,
344 _tiedProperties.setAttribute( SGPropertyNode::ARCHIVE, archivable );
346 _tiedProperties.Tie(
"temperature-degf",
this,
349 _tiedProperties.Tie(
"density-slugft3",
this,
352 _tiedProperties.Tie(
"relative-humidity",
this,
355 _tiedProperties.Tie(
"atmosphere/density-tropo-avg",
this,
358 _tiedProperties.Tie(
"atmosphere/is-isa",
this,
365 _tiedProperties.Untie();
377 return temperature_sea_level_degc;
383 return temperature_degc;
389 return (temperature_degc * 9.0 / 5.0) + 32.0;
395 return dewpoint_sea_level_degc;
401 return dewpoint_degc;
407 return pressure_sea_level_inhg;
413 return pressure_inhg;
419 return density_slugft3;
425 return relative_humidity;
431 return density_tropo_avg_kgm3;
437 return altitude_half_to_sun_m;
443 return altitude_tropo_top_m;
449 return wind_from_heading_deg;
455 return wind_speed_kt;
461 return wind_from_north_fps;
467 return wind_from_east_fps;
473 return wind_from_down_fps;
479 return turbulence_magnitude_norm;
485 return turbulence_rate_hz;
508 temperature_sea_level_degc = t;
509 if (dewpoint_sea_level_degc > t)
510 dewpoint_sea_level_degc = t;
520 temperature_degc = t;
522 _recalc_sl_temperature();
523 _recalc_sl_pressure();
526 _recalc_relative_humidity();
533 dewpoint_sea_level_degc = t;
534 if (temperature_sea_level_degc < t)
535 temperature_sea_level_degc = t;
537 _recalc_alt_dewpoint();
547 _recalc_sl_dewpoint();
549 _recalc_relative_humidity();
556 pressure_sea_level_inhg =
p;
568 _recalc_sl_pressure();
576 wind_from_heading_deg = h;
594 wind_from_north_fps = n;
603 wind_from_east_fps = e;
612 wind_from_down_fps = d;
621 turbulence_magnitude_norm = t;
627 turbulence_rate_hz = r;
636 _recalc_alt_dewpoint();
638 _recalc_relative_humidity();
645 altitude_half_to_sun_m = alt;
647 _recalc_density_tropo_avg_kgm3();
654 altitude_tropo_top_m = alt;
656 _recalc_density_tropo_avg_kgm3();
666FGEnvironment::_recalc_hdgspd ()
668 wind_from_heading_deg =
669 atan2(wind_from_east_fps, wind_from_north_fps) * SGD_RADIANS_TO_DEGREES;
671 if( wind_from_heading_deg < 0 )
672 wind_from_heading_deg += 360.0;
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;
680FGEnvironment::_recalc_ne ()
683 wind_speed_kt * SG_NM_TO_METER * SG_METER_TO_FEET * (1.0/3600);
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);
694FGEnvironment::_recalc_sl_temperature ()
699 SG_LOG(SG_ENVIRONMENT, SG_DEBUG,
"recalc_sl_temperature: using "
700 << temperature_degc <<
" @ " << elevation_ft <<
" :: " <<
this);
705 SG_LOG(SG_ENVIRONMENT, SG_ALERT,
"recalc_sl_temperature: "
706 <<
"valid only in troposphere, not " << elevation_ft);
714 temperature_sea_level_degc = 15.0;
715 temperature_degc = temperature_sea_level_degc - temperature_shift;
719 temperature_sea_level_degc = temperature_degc < t_strato
721 : temperature_degc + temperature_shift;
730FGEnvironment::_recalc_sl_dewpoint ()
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;
741FGEnvironment::_recalc_alt_dewpoint ()
746 dewpoint_degc = dewpoint_sea_level_degc + (elevation_ft * .0002);
747 if (dewpoint_degc > temperature_degc)
748 dewpoint_degc = temperature_degc;
752FGEnvironment::_recalc_sl_pressure ()
754 using namespace atmodel;
757 SG_LOG(SG_ENVIRONMENT, SG_ALERT,
"recalc_sl_pressure: using "
758 << pressure_inhg <<
" and "
759 << temperature_degc <<
" @ " << elevation_ft <<
" :: " <<
this);
762 pressure_sea_level_inhg =
P_layer(0., elevation_ft * foot,
763 pressure_inhg * inHg, temperature_degc + freezing,
ISA_def[0].lapse) /
inHg;
771FGEnvironment::_recalc_alt_pt ()
773 using namespace atmodel;
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
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);
792 pressure_inhg = press /
inHg;
796FGEnvironment::_recalc_density ()
798 const double pressure_psf = pressure_inhg * 70.7487;
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));
808 if ((pressure_mb <= 0.0) || (vapor_pressure_mb <= 0.0)) {
809 density_slugft3 = 0.0;
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;
816 density_slugft3 = pressure_psf / (virtual_temperature_degr * 1718);
817 _recalc_density_tropo_avg_kgm3();
823FGEnvironment::_recalc_density_tropo_avg_kgm3 ()
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));
829 if ((pressure_mb <= 0.0) || (virtual_temp <= 0.0)) {
830 density_tropo_avg_kgm3 = 0.0;
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);
839 density_tropo_avg_kgm3 = ((density_slugft3 * 515.379) + density_half + density_tropo) / 3;
843FGEnvironment::_recalc_relative_humidity ()
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);
861 bool b = live_update;
862 live_update = _live_update;
874 double retval = (a + ((b - a) * fraction));
883 if (fabs(b-a) > 180) {
889 return fmod(
do_interp(a, b, fraction), 360);
946 result->_recalc_ne();
947 result->_recalc_alt_pt();
948 result->_recalc_alt_dewpoint();
949 result->_recalc_density();
950 result->_recalc_relative_humidity();
const ISA_layer ISA_def[]
double P_layer(const double height, const double href, const double Pref, const double Tref, const double lapse)
pair< double, double > PT_vs_hpt(const double hh, const double _p0, const double _t0)
Model the natural environment.
virtual double get_turbulence_rate_hz() const
virtual bool set_live_update(bool live_update)
virtual double get_dewpoint_degc() const
virtual void set_dewpoint_degc(double d)
virtual double get_relative_humidity() const
virtual void set_is_isa(bool isa)
virtual void set_turbulence_magnitude_norm(double t)
virtual double get_temperature_degf() const
virtual double get_pressure_inhg() const
virtual void set_wind_speed_kt(double s)
virtual double get_visibility_m() const
virtual void set_altitude_half_to_sun_m(double alt)
virtual void set_wind_from_east_fps(double e)
virtual double get_dewpoint_sea_level_degc() const
virtual void Tie(SGPropertyNode_ptr base, bool setArchivable=true)
virtual void set_dewpoint_sea_level_degc(double d)
FGEnvironment & operator=(const FGEnvironment &other)
virtual double get_wind_from_north_fps() const
virtual void set_wind_from_heading_deg(double h)
virtual void set_wind_from_down_fps(double d)
virtual void set_altitude_tropo_top_m(double alt)
virtual bool get_is_isa() const
virtual double get_wind_speed_kt() const
virtual double get_altitude_tropo_top_m() const
virtual double get_temperature_sea_level_degc() const
virtual double get_wind_from_heading_deg() const
virtual void read(const SGPropertyNode *node)
virtual double get_temperature_degc() const
virtual void set_wind_from_north_fps(double n)
FGEnvironment & interpolate(const FGEnvironment &env2, double fraction, FGEnvironment *result) const
virtual double get_density_tropo_avg_kgm3() const
virtual double get_wind_from_east_fps() const
virtual void set_elevation_ft(double elevation_ft)
virtual double get_pressure_sea_level_inhg() const
virtual double get_elevation_ft() const
virtual void set_pressure_sea_level_inhg(double p)
virtual double get_altitude_half_to_sun_m() const
virtual void set_pressure_inhg(double p)
virtual void set_visibility_m(double v)
virtual double get_wind_from_down_fps() const
virtual double get_density_slugft3() const
virtual double get_turbulence_magnitude_norm() const
virtual void set_turbulence_rate_hz(double t)
virtual void set_temperature_sea_level_degc(double t)
virtual void set_temperature_degc(double t)
static bool maybe_copy_value(FGEnvironment *env, const SGPropertyNode *node, const char *name, void(FGEnvironment::*setter)(double))
static double do_interp_deg(double a, double b, double fraction)
static double do_interp(double a, double b, double fraction)
const double inHg(101325.0/760 *1000 *inch)
const double freezing(273.15)
const double foot(12 *inch)