]> git.mxchange.org Git - flightgear.git/blobdiff - src/Environment/environment.cxx
fix a pointer reference.
[flightgear.git] / src / Environment / environment.cxx
index 050bededb9d8a0dd2635129eb71ec6459fe54901..212e51d856aed4fa286ea947c63ffcd5d6e21eb3 100644 (file)
 #  include <config.h>
 #endif
 
-#ifdef HAVE_WINDOWS_H
-#  include <windows.h>                     
-#endif
-
 #include <math.h>
 
-#include <plib/sg.h>
+#include <boost/tuple/tuple.hpp>
 
 #include <simgear/constants.h>
 #include <simgear/debug/logstream.hxx>
 #include <simgear/environment/visual_enviro.hxx>
 
 #include <Main/fg_props.hxx>
+#include <signal.h>
 
 #include "environment.hxx"
-
+#include "atmosphere.hxx"
 
 \f
 ////////////////////////////////////////////////////////////////////////
 // Atmosphere model.
 ////////////////////////////////////////////////////////////////////////
 
-// Copied from YASim Atmosphere.cxx, with m converted to ft, degK
-// converted to degC, Pa converted to inHG, and kg/m^3 converted to
-// slug/ft^3; they were then converted to deltas from the sea-level
-// defaults (approx. 15degC, 29.92inHG, and 0.00237slugs/ft^3).
+#ifdef USING_TABLES
 
-// Original comment from YASim:
+// Calculated based on the ISA standard day, as found at e.g.
+// http://www.av8n.com/physics/altimetry.htm
 
-// Copied from McCormick, who got it from "The ARDC Model Atmosphere"
-// Note that there's an error in the text in the first entry,
-// McCormick lists 299.16/101325/1.22500, but those don't agree with
-// R=287.  I chose to correct the temperature to 288.20, since 79F is
-// pretty hot for a "standard" atmosphere.
-
-// Elevation (ft), temperature factor (degK), pressure factor (inHG)
+// Each line of data has 3 elements:
+//   Elevation (ft), 
+//   temperature factor (dimensionless ratio of absolute temp), 
+//   pressure factor (dimensionless ratio)
 static double atmosphere_data[][3] = {
- { 0.00, 1.00, 1.000 },
- { 2952.76, 0.98, 0.898 },
- { 5905.51, 0.96, 0.804 },
- { 8858.27, 0.94, 0.719 },
- { 11811.02, 0.92, 0.641 },
- { 14763.78, 0.90, 0.570 },
- { 17716.54, 0.88, 0.506 },
- { 20669.29, 0.86, 0.447 },
- { 23622.05, 0.84, 0.394 },
- { 26574.80, 0.82, 0.347 },
- { 29527.56, 0.80, 0.304 },
- { 32480.31, 0.78, 0.266 },
- { 35433.07, 0.76, 0.231 },
- { 38385.83, 0.75, 0.201 },
- { 41338.58, 0.75, 0.174 },
- { 44291.34, 0.75, 0.151 },
- { 47244.09, 0.75, 0.131 },
- { 50196.85, 0.75, 0.114 },
- { 53149.61, 0.75, 0.099 },
- { 56102.36, 0.75, 0.086 },
- { 59055.12, 0.75, 0.075 },
- { 62007.87, 0.75, 0.065 },
+ {  -3000.00,   1.021,  1.1133 },
+ {      0.00,   1.000,  1.0000 },
+ {   2952.76,   0.980,  0.8978 },
+ {   5905.51,   0.959,  0.8042 },
+ {   8858.27,   0.939,  0.7187 },
+ {  11811.02,   0.919,  0.6407 },
+ {  14763.78,   0.898,  0.5697 },
+ {  17716.54,   0.878,  0.5052 },
+ {  20669.29,   0.858,  0.4468 },
+ {  23622.05,   0.838,  0.3940 },
+ {  26574.80,   0.817,  0.3463 },
+ {  29527.56,   0.797,  0.3034 },
+ {  32480.31,   0.777,  0.2649 },
+ {  35433.07,   0.756,  0.2305 },
+ {  38385.83,   0.752,  0.2000 },
+ {  41338.58,   0.752,  0.1736 },
+ {  44291.34,   0.752,  0.1506 },
+ {  47244.09,   0.752,  0.1307 },
+ {  50196.85,   0.752,  0.1134 },
+ {  53149.61,   0.752,  0.0984 },
+ {  56102.36,   0.752,  0.0854 },
+ {  59055.12,   0.752,  0.0741 },
+ {  62007.87,   0.752,  0.0643 },
+ {  65000.00,   0.752,  0.0557 },
+ {  68000.00,   0.754,  0.0482 },
+ {  71000.00,   0.758,  0.0418 },
+ {  74000.00,   0.761,  0.0362 },
+ {  77000.00,   0.764,  0.0314 },
+ {  80000.00,   0.767,  0.0273 },
+ {  83000.00,   0.770,  0.0237 },
+ {  86000.00,   0.773,  0.0206 },
+ {  89000.00,   0.777,  0.0179 },
+ {  92000.00,   0.780,  0.0156 },
+ {  95000.00,   0.783,  0.0135 },
+ {  98000.00,   0.786,  0.0118 },
+ { 101000.00,   0.789,  0.0103 },
  { -1, -1, -1 }
 };
 
@@ -108,40 +114,50 @@ _setup_tables ()
                                   atmosphere_data[i][2]);
   }
 }
-
+#endif
 
 \f
 ////////////////////////////////////////////////////////////////////////
 // Implementation of FGEnvironment.
 ////////////////////////////////////////////////////////////////////////
 
+void FGEnvironment::_init()
+{
+    elevation_ft = 0;
+    visibility_m = 32000;
+    temperature_sea_level_degc = 15;
+    temperature_degc = 15;
+    dewpoint_sea_level_degc = 5; // guess
+    dewpoint_degc = 5;
+    pressure_sea_level_inhg = 29.92;
+    pressure_inhg = 29.92;
+    turbulence_magnitude_norm = 0;
+    turbulence_rate_hz = 1;
+    wind_from_heading_deg = 0;
+    wind_speed_kt = 0;
+    wind_from_north_fps = 0;
+    wind_from_east_fps = 0;
+    wind_from_down_fps = 0;
+    thermal_lift_fps = 0;
+    ridge_lift_fps= 0;
+    altitude_half_to_sun_m = 1000;
+    altitude_tropo_top_m = 10000;
+#ifdef USING_TABLES
+    _setup_tables();
+#endif
+    _recalc_density();
+    _recalc_relative_humidity();
+    live_update = true;
+}
+
 FGEnvironment::FGEnvironment()
-  : elevation_ft(0),
-    visibility_m(32000),
-    temperature_sea_level_degc(15),
-    temperature_degc(15),
-    dewpoint_sea_level_degc(5), // guess
-    dewpoint_degc(5),
-    pressure_sea_level_inhg(29.92),
-    pressure_inhg(29.92),
-    turbulence_magnitude_norm(0),
-    turbulence_rate_hz(1),
-    wind_from_heading_deg(0),
-    wind_speed_kt(0),
-    wind_from_north_fps(0),
-    wind_from_east_fps(0),
-    wind_from_down_fps(0),
-    altitude_half_to_sun_m(1000),
-    altitude_tropo_top_m(10000)
-{
-  _setup_tables();
-  _recalc_density();
-  _recalc_relative_humidity();
+{
+    _init();
 }
 
 FGEnvironment::FGEnvironment (const FGEnvironment &env)
 {
-    FGEnvironment();
+    _init();
     copy(env);
 }
 
@@ -164,6 +180,8 @@ FGEnvironment::copy (const FGEnvironment &env)
     wind_from_north_fps = env.wind_from_north_fps;
     wind_from_east_fps = env.wind_from_east_fps;
     wind_from_down_fps = env.wind_from_down_fps;
+    thermal_lift_fps = env.thermal_lift_fps;
+    ridge_lift_fps= env.ridge_lift_fps;
     turbulence_magnitude_norm = env.turbulence_magnitude_norm;
     turbulence_rate_hz = env.turbulence_rate_hz;
 }
@@ -187,6 +205,7 @@ maybe_copy_value (FGEnvironment * env, const SGPropertyNode * node,
 void
 FGEnvironment::read (const SGPropertyNode * node)
 {
+    bool live_update = set_live_update( false );
     maybe_copy_value(this, node, "visibility-m",
                      &FGEnvironment::set_visibility_m);
 
@@ -219,6 +238,14 @@ FGEnvironment::read (const SGPropertyNode * node)
 
     maybe_copy_value(this, node, "turbulence/rate-hz",
                      &FGEnvironment::set_turbulence_rate_hz);
+    // calculate derived properties here to avoid duplicate expensive computations
+    _recalc_ne();
+    _recalc_alt_pt();
+    _recalc_alt_dewpoint();
+    _recalc_density();
+    _recalc_relative_humidity();
+
+    set_live_update(live_update);
 }
 
 
@@ -330,6 +357,18 @@ FGEnvironment::get_wind_from_down_fps () const
   return wind_from_down_fps;
 }
 
+double
+FGEnvironment::get_thermal_lift_fps () const
+{
+  return thermal_lift_fps;
+}
+
+double
+FGEnvironment::get_ridge_lift_fps () const
+{
+  return ridge_lift_fps;
+}
+
 double
 FGEnvironment::get_turbulence_magnitude_norm () const
 {
@@ -363,17 +402,23 @@ FGEnvironment::set_temperature_sea_level_degc (double t)
   temperature_sea_level_degc = t;
   if (dewpoint_sea_level_degc > t)
       dewpoint_sea_level_degc = t;
-  _recalc_alt_temperature();
-  _recalc_density();
+  if( live_update ) {
+    _recalc_alt_pt();
+    _recalc_density();
+  }
 }
 
 void
 FGEnvironment::set_temperature_degc (double t)
 {
   temperature_degc = t;
-  _recalc_sl_temperature();
-  _recalc_density();
-  _recalc_relative_humidity();
+  if( live_update ) {
+    _recalc_sl_temperature();
+    _recalc_sl_pressure();
+    _recalc_alt_pt();
+    _recalc_density();
+    _recalc_relative_humidity();
+  }
 }
 
 void
@@ -382,68 +427,104 @@ FGEnvironment::set_dewpoint_sea_level_degc (double t)
   dewpoint_sea_level_degc = t;
   if (temperature_sea_level_degc < t)
       temperature_sea_level_degc = t;
-  _recalc_alt_dewpoint();
-  _recalc_density();
+  if( live_update ) {
+    _recalc_alt_dewpoint();
+    _recalc_density();
+  }
 }
 
 void
 FGEnvironment::set_dewpoint_degc (double t)
 {
   dewpoint_degc = t;
-  _recalc_sl_dewpoint();
-  _recalc_density();
-  _recalc_relative_humidity();
+  if( live_update ) {
+    _recalc_sl_dewpoint();
+    _recalc_density();
+    _recalc_relative_humidity();
+  }
 }
 
 void
 FGEnvironment::set_pressure_sea_level_inhg (double p)
 {
   pressure_sea_level_inhg = p;
-  _recalc_alt_pressure();
-  _recalc_density();
+  if( live_update ) {
+    _recalc_alt_pt();
+    _recalc_density();
+  }
 }
 
 void
 FGEnvironment::set_pressure_inhg (double p)
 {
   pressure_inhg = p;
-  _recalc_sl_pressure();
-  _recalc_density();
+  if( live_update ) {
+    _recalc_sl_pressure();
+    _recalc_density();
+  }
 }
 
 void
 FGEnvironment::set_wind_from_heading_deg (double h)
 {
   wind_from_heading_deg = h;
-  _recalc_ne();
+  if( live_update ) {
+    _recalc_ne();
+  }
 }
 
 void
 FGEnvironment::set_wind_speed_kt (double s)
 {
   wind_speed_kt = s;
-  _recalc_ne();
+  if( live_update ) {
+    _recalc_ne();
+  }
 }
 
 void
 FGEnvironment::set_wind_from_north_fps (double n)
 {
   wind_from_north_fps = n;
-  _recalc_hdgspd();
+  if( live_update ) {
+    _recalc_hdgspd();
+  }
 }
 
 void
 FGEnvironment::set_wind_from_east_fps (double e)
 {
   wind_from_east_fps = e;
-  _recalc_hdgspd();
+  if( live_update ) {
+    _recalc_hdgspd();
+  }
 }
 
 void
 FGEnvironment::set_wind_from_down_fps (double d)
 {
   wind_from_down_fps = d;
-  _recalc_hdgspd();
+  if( live_update ) {
+    _recalc_hdgspd();
+  }
+}
+
+void
+FGEnvironment::set_thermal_lift_fps (double th)
+{
+  thermal_lift_fps = th;
+  if( live_update ) {
+    _recalc_updraft();
+  }
+}
+
+void
+FGEnvironment::set_ridge_lift_fps (double ri)
+{
+  ridge_lift_fps = ri;
+  if( live_update ) {
+    _recalc_updraft();
+}
 }
 
 void
@@ -462,25 +543,30 @@ void
 FGEnvironment::set_elevation_ft (double e)
 {
   elevation_ft = e;
-  _recalc_alt_temperature();
-  _recalc_alt_dewpoint();
-  _recalc_alt_pressure();
-  _recalc_density();
-  _recalc_relative_humidity();
+  if( live_update ) {
+    _recalc_alt_pt();
+    _recalc_alt_dewpoint();
+    _recalc_density();
+    _recalc_relative_humidity();
+  }
 }
 
 void
 FGEnvironment::set_altitude_half_to_sun_m (double alt)
 {
- altitude_half_to_sun_m = alt;
- _recalc_density_tropo_avg_kgm3();
+  altitude_half_to_sun_m = alt;
+  if( live_update ) {
+    _recalc_density_tropo_avg_kgm3();
+  }
 }
 
 void
 FGEnvironment::set_altitude_tropo_top_m (double alt)
 {
- altitude_tropo_top_m = alt;
- _recalc_density_tropo_avg_kgm3();
+  altitude_tropo_top_m = alt;
+  if( live_update ) {
+    _recalc_density_tropo_avg_kgm3();
+  }
 }
 
 
@@ -530,25 +616,39 @@ FGEnvironment::_recalc_ne ()
 }
 
 void
-FGEnvironment::_recalc_sl_temperature ()
+FGEnvironment::_recalc_updraft ()
 {
-  // If we're in the stratosphere, leave sea-level temp alone
-  if (elevation_ft < 38000) {
-    temperature_sea_level_degc = (temperature_degc + 273.15)
-        / _temperature_degc_table->interpolate(elevation_ft)
-      - 273.15;
-  }
+  wind_from_down_fps = thermal_lift_fps + ridge_lift_fps ;
 }
 
+// Intended to help with the interpretation of METAR data,
+// not for random in-flight outside-air temperatures.
 void
-FGEnvironment::_recalc_alt_temperature ()
+FGEnvironment::_recalc_sl_temperature ()
 {
-  if (elevation_ft < 38000) {
-    temperature_degc = (temperature_sea_level_degc + 273.15) *
-        _temperature_degc_table->interpolate(elevation_ft) - 273.15;
-  } else {
-    temperature_degc = -56.49; // Stratosphere is constant
+
+#if 0
+  {
+    SG_LOG(SG_GENERAL, SG_DEBUG, "recalc_sl_temperature: using "
+      << temperature_degc << " @ " << elevation_ft << " :: " << this);
+  }
+#endif
+
+  if (elevation_ft >= ISA_def[1].height) {
+    SG_LOG(SG_GENERAL, SG_ALERT, "recalc_sl_temperature: "
+        << "valid only in troposphere, not " << elevation_ft);
+    return;
   }
+
+// Clamp: temperature of the stratosphere, in degrees C:
+  double t_strato = ISA_def[1].temp - atmodel::freezing;
+  if (temperature_degc < t_strato) temperature_sea_level_degc = t_strato;
+  else temperature_sea_level_degc = 
+      temperature_degc + elevation_ft * atmodel::foot * ISA_def[0].lapse;
+
+// Alternative implemenation:
+//  else temperature_sea_level_inhg = T_layer(0., elevation_ft * foot,
+//      pressure_inhg * inHg, temperature_degc + freezing, ISA_def[0].lapse) - freezing;
 }
 
 void
@@ -576,15 +676,44 @@ FGEnvironment::_recalc_alt_dewpoint ()
 void
 FGEnvironment::_recalc_sl_pressure ()
 {
-  pressure_sea_level_inhg =
-    pressure_inhg / _pressure_inhg_table->interpolate(elevation_ft);
+  using namespace atmodel;
+#if 0
+  {
+    SG_LOG(SG_GENERAL, SG_ALERT, "recalc_sl_pressure: using "
+      << pressure_inhg << " and "
+      << temperature_degc << " @ " << elevation_ft << " :: " << this);
+  }
+#endif
+  pressure_sea_level_inhg = P_layer(0., elevation_ft * foot,
+      pressure_inhg * inHg, temperature_degc + freezing, ISA_def[0].lapse) / inHg;
 }
 
+// This gets called at frame rate, to account for the aircraft's
+// changing altitude. 
+// Called by set_elevation_ft() which is called by FGEnvironmentMgr::update
+
 void
-FGEnvironment::_recalc_alt_pressure ()
+FGEnvironment::_recalc_alt_pt ()
 {
-  pressure_inhg =
-    pressure_sea_level_inhg * _pressure_inhg_table->interpolate(elevation_ft);
+  using namespace atmodel;
+#if 0
+  {
+    static int count(0);
+    if (++count % 1000 == 0) {
+      SG_LOG(SG_GENERAL, SG_ALERT, 
+           "recalc_alt_pt for: " << elevation_ft
+        << "  using "  << pressure_sea_level_inhg 
+        << "  and "  << temperature_sea_level_degc
+        << " :: " << this
+        << "  # " << count);
+    }
+  }
+#endif
+  double press, temp;
+  boost::tie(press, temp) = PT_vs_hpt(elevation_ft * foot, 
+        pressure_sea_level_inhg * inHg, temperature_sea_level_degc + freezing);
+  temperature_degc = temp - freezing;
+  pressure_inhg = press / inHg;
 }
 
 void
@@ -612,7 +741,7 @@ void
 FGEnvironment::_recalc_density_tropo_avg_kgm3 ()
 {
   double pressure_mb = pressure_inhg * 33.86;
-  double vaporpressure = 6.11 * pow(10, ((7.5 * dewpoint_degc) / (237.7 + dewpoint_degc)));
+  double vaporpressure = 6.11 * pow(10.0, ((7.5 * dewpoint_degc) / (237.7 + dewpoint_degc)));
 
   double virtual_temp = (temperature_degc + 273.15) / (1 - 0.379 * (vaporpressure/pressure_mb));
 
@@ -627,12 +756,26 @@ FGEnvironment::_recalc_density_tropo_avg_kgm3 ()
 void
 FGEnvironment::_recalc_relative_humidity ()
 {
-  double vaporpressure = 6.11 * pow(10, ((7.5 * dewpoint_degc) / ( 237.7 + dewpoint_degc)));
-  double sat_vaporpressure = 6.11 * pow(10, ((7.5 * temperature_degc)
+/*
+  double vaporpressure = 6.11 * pow(10.0, ((7.5 * dewpoint_degc) / ( 237.7 + dewpoint_degc)));
+  double sat_vaporpressure = 6.11 * pow(10.0, ((7.5 * temperature_degc)
       / ( 237.7 + temperature_degc)) );
   relative_humidity = 100 * vaporpressure / sat_vaporpressure ;
+
+  with a little algebra, this gets the same result and spares two multiplications and one pow()
+*/
+  double a = (7.5 * dewpoint_degc)    / ( 237.7 + dewpoint_degc);
+  double b = (7.5 * temperature_degc) / ( 237.7 + temperature_degc);
+  relative_humidity = 100 * pow(10.0,a-b); 
 }
 
+bool
+FGEnvironment::set_live_update( bool _live_update )
+{
+  bool b = live_update;
+  live_update = _live_update;
+  return b;
+}
 
 
 ////////////////////////////////////////////////////////////////////////
@@ -664,6 +807,10 @@ void
 interpolate (const FGEnvironment * env1, const FGEnvironment * env2,
              double fraction, FGEnvironment * result)
 {
+    // don't calculate each internal property every time we set a single value
+    // we trigger that at the end of the interpolation process
+    bool live_update = result->set_live_update( false );
+
     result->set_visibility_m
         (do_interp(env1->get_visibility_m(),
                    env2->get_visibility_m(),
@@ -674,7 +821,7 @@ interpolate (const FGEnvironment * env1, const FGEnvironment * env2,
                    env2->get_temperature_sea_level_degc(),
                    fraction));
 
-    result->set_dewpoint_degc
+    result->set_dewpoint_sea_level_degc
         (do_interp(env1->get_dewpoint_sea_level_degc(),
                    env2->get_dewpoint_sea_level_degc(),
                    fraction));
@@ -708,6 +855,15 @@ interpolate (const FGEnvironment * env1, const FGEnvironment * env2,
         (do_interp(env1->get_turbulence_rate_hz(),
                    env2->get_turbulence_rate_hz(),
                    fraction));
+
+    // calculate derived properties here to avoid duplicate expensive computations
+    result->_recalc_ne();
+    result->_recalc_alt_pt();
+    result->_recalc_alt_dewpoint();
+    result->_recalc_density();
+    result->_recalc_relative_humidity();
+
+    result->set_live_update(live_update);
 }
 
 // end of environment.cxx