#include "environment.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).
+
+// Original comment from YASim:
+
+// 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)
+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 },
+ { -1, -1, -1 }
+};
+
+static SGInterpTable * _temperature_degc_table = 0;
+static SGInterpTable * _pressure_inhg_table = 0;
+
+static void
+_setup_tables ()
+{
+ if (_temperature_degc_table != 0)
+ return;
+
+ _temperature_degc_table = new SGInterpTable;
+ _pressure_inhg_table = new SGInterpTable;
+
+ for (int i = 0; atmosphere_data[i][0] != -1; i++) {
+ _temperature_degc_table->addEntry(atmosphere_data[i][0],
+ atmosphere_data[i][1]);
+ _pressure_inhg_table->addEntry(atmosphere_data[i][0],
+ atmosphere_data[i][2]);
+ }
+}
+
+
+\f
+////////////////////////////////////////////////////////////////////////
+// Implementation of FGEnvironment.
+////////////////////////////////////////////////////////////////////////
+
FGEnvironment::FGEnvironment()
- : _temperature_degc_table(new SGInterpTable),
- _pressure_inhg_table(new SGInterpTable),
- elevation_ft(0),
+ : elevation_ft(0),
visibility_m(32000),
temperature_sea_level_degc(15),
temperature_degc(15),
wind_speed_kt(0),
wind_from_north_fps(0),
wind_from_east_fps(0),
- wind_from_down_fps(0)
+ wind_from_down_fps(0),
+ turbulence_norm(0)
{
_setup_tables();
}
FGEnvironment::FGEnvironment (const FGEnvironment &env)
- : _temperature_degc_table(new SGInterpTable),
- _pressure_inhg_table(new SGInterpTable),
- elevation_ft(env.elevation_ft),
- visibility_m(env.visibility_m),
- temperature_sea_level_degc(env.temperature_sea_level_degc),
- dewpoint_sea_level_degc(env.dewpoint_sea_level_degc),
- pressure_sea_level_inhg(env.pressure_sea_level_inhg),
- wind_from_heading_deg(env.wind_from_heading_deg),
- wind_speed_kt(env.wind_speed_kt),
- 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)
{
- _setup_tables();
+ FGEnvironment();
+ copy(env);
}
FGEnvironment::~FGEnvironment()
{
- delete _temperature_degc_table;
- delete _pressure_inhg_table;
+}
+
+void
+FGEnvironment::copy (const FGEnvironment &env)
+{
+ elevation_ft = env.elevation_ft;
+ visibility_m = env.visibility_m;
+ temperature_sea_level_degc = env.temperature_sea_level_degc;
+ dewpoint_sea_level_degc = env.dewpoint_sea_level_degc;
+ pressure_sea_level_inhg = env.pressure_sea_level_inhg;
+ wind_from_heading_deg = env.wind_from_heading_deg;
+ wind_speed_kt = env.wind_speed_kt;
+ 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;
+ turbulence_norm = env.turbulence_norm;
+}
+
+static inline bool
+maybe_copy_value (FGEnvironment * env, const SGPropertyNode * node,
+ const char * name, void (FGEnvironment::*setter)(double))
+{
+ const SGPropertyNode * child = node->getChild(name);
+ // fragile: depends on not being typed
+ // as a number
+ if (child != 0 && child->getStringValue()[0] != '\0') {
+ (env->*setter)(child->getDoubleValue());
+ return true;
+ } else {
+ return false;
+ }
+}
+
+void
+FGEnvironment::read (const SGPropertyNode * node)
+{
+ maybe_copy_value(this, node, "visibility-m",
+ &FGEnvironment::set_visibility_m);
+
+ if (!maybe_copy_value(this, node, "temperature-sea-level-degc",
+ &FGEnvironment::set_temperature_sea_level_degc))
+ maybe_copy_value(this, node, "temperature-degc",
+ &FGEnvironment::set_temperature_degc);
+
+ if (!maybe_copy_value(this, node, "dewpoint-sea-level-degc",
+ &FGEnvironment::set_dewpoint_sea_level_degc))
+ maybe_copy_value(this, node, "dewpoint-degc",
+ &FGEnvironment::set_dewpoint_degc);
+
+ if (!maybe_copy_value(this, node, "pressure-sea-level-inhg",
+ &FGEnvironment::set_pressure_sea_level_inhg))
+ maybe_copy_value(this, node, "pressure-inhg",
+ &FGEnvironment::set_pressure_inhg);
+
+ maybe_copy_value(this, node, "wind-from-heading-deg",
+ &FGEnvironment::set_wind_from_heading_deg);
+
+ maybe_copy_value(this, node, "wind-speed-kt",
+ &FGEnvironment::set_wind_speed_kt);
+
+ maybe_copy_value(this, node, "elevation-ft",
+ &FGEnvironment::set_elevation_ft);
+
+ maybe_copy_value(this, node, "turbulence-norm",
+ &FGEnvironment::set_turbulence_norm);
}
return wind_from_down_fps;
}
+double
+FGEnvironment::get_turbulence_norm () const
+{
+ return turbulence_norm;
+}
+
double
FGEnvironment::get_elevation_ft () const
{
_recalc_hdgspd();
}
+void
+FGEnvironment::set_turbulence_norm (double t)
+{
+ turbulence_norm = t;
+}
+
void
FGEnvironment::set_elevation_ft (double e)
{
_recalc_density();
}
-// 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).
-
-// Original comment from YASim:
-
-// 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)
-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 },
- { -1, -1, -1 }
-};
-
-void
-FGEnvironment::_setup_tables ()
-{
- for (int i = 0; atmosphere_data[i][0] != -1; i++) {
- _temperature_degc_table->addEntry(atmosphere_data[i][0],
- atmosphere_data[i][1]);
- _pressure_inhg_table->addEntry(atmosphere_data[i][0],
- atmosphere_data[i][2]);
- }
-}
-
void
FGEnvironment::_recalc_hdgspd ()
{
density_slugft3 = pressure_psf / (virtual_temperature_degr * 1718);
}
+
+\f
+////////////////////////////////////////////////////////////////////////
+// Functions.
+////////////////////////////////////////////////////////////////////////
+
+static inline double
+do_interp (double a, double b, double fraction)
+{
+ double retval = (a + ((b - a) * fraction));
+ return retval;
+}
+
+static inline double
+do_interp_deg (double a, double b, double fraction)
+{
+ a = fmod(a, 360);
+ b = fmod(b, 360);
+ if (fabs(b-a) > 180) {
+ if (a < b)
+ a += 360;
+ else
+ b += 360;
+ }
+ return fmod(do_interp(a, b, fraction), 360);
+}
+
+void
+interpolate (const FGEnvironment * env1, const FGEnvironment * env2,
+ double fraction, FGEnvironment * result)
+{
+ result->set_visibility_m
+ (do_interp(env1->get_visibility_m(),
+ env2->get_visibility_m(),
+ fraction));
+
+ result->set_temperature_sea_level_degc
+ (do_interp(env1->get_temperature_sea_level_degc(),
+ env2->get_temperature_sea_level_degc(),
+ fraction));
+
+ result->set_dewpoint_sea_level_degc
+ (do_interp(env1->get_dewpoint_sea_level_degc(),
+ env2->get_dewpoint_sea_level_degc(),
+ fraction));
+
+ result->set_pressure_sea_level_inhg
+ (do_interp(env1->get_pressure_sea_level_inhg(),
+ env2->get_pressure_sea_level_inhg(),
+ fraction));
+
+ result->set_wind_from_heading_deg
+ (do_interp_deg(env1->get_wind_from_heading_deg(),
+ env2->get_wind_from_heading_deg(),
+ fraction));
+
+ result->set_wind_speed_kt
+ (do_interp(env1->get_wind_speed_kt(),
+ env2->get_wind_speed_kt(),
+ fraction));
+
+ result->set_elevation_ft
+ (do_interp(env1->get_elevation_ft(),
+ env2->get_elevation_ft(),
+ fraction));
+
+ result->set_turbulence_norm
+ (do_interp(env1->get_turbulence_norm(),
+ env2->get_turbulence_norm(),
+ fraction));
+}
+
// end of environment.cxx
_environment->set_wind_speed_kt(_current_wind_speed_kt);
}
+
+\f
+////////////////////////////////////////////////////////////////////////
+// Implementation of FGInterpolateEnvironmentCtrl.
+////////////////////////////////////////////////////////////////////////
+
+FGInterpolateEnvironmentCtrl::FGInterpolateEnvironmentCtrl ()
+{
+}
+
+FGInterpolateEnvironmentCtrl::~FGInterpolateEnvironmentCtrl ()
+{
+ for (int i = 0; i < _boundary_table.size(); i++)
+ delete _boundary_table[i];
+ for (int i = 0; i < _aloft_table.size(); i++)
+ delete _aloft_table[i];
+}
+
+
+
+void
+FGInterpolateEnvironmentCtrl::init ()
+{
+ read_table(fgGetNode("/environment/config/boundary", true),
+ _boundary_table);
+ read_table(fgGetNode("/environment/config/aloft", true),
+ _aloft_table);
+}
+
+void
+FGInterpolateEnvironmentCtrl::reinit ()
+{
+ for (int i = 0; i < _boundary_table.size(); i++)
+ delete _boundary_table[i];
+ for (int i = 0; i < _aloft_table.size(); i++)
+ delete _aloft_table[i];
+ _boundary_table.clear();
+ _aloft_table.clear();
+ init();
+}
+
+void
+FGInterpolateEnvironmentCtrl::read_table (const SGPropertyNode * node,
+ vector<bucket *> &table)
+{
+ for (int i = 0; i < node->nChildren(); i++) {
+ const SGPropertyNode * child = node->getChild(i);
+ if (!strcmp(child->getName(), "entry") &&
+ child->getStringValue("elevation-ft", "")[0] != '\0') {
+ bucket * b = new bucket;
+ if (i > 0)
+ b->environment.copy(table[i-1]->environment);
+ b->environment.read(child);
+ b->altitude_ft = b->environment.get_elevation_ft();
+ table.push_back(b);
+ }
+ }
+ sort(table.begin(), table.end());
+}
+
+void
+FGInterpolateEnvironmentCtrl::update (double delta_time_sec)
+{
+ // FIXME
+ double altitude_ft = fgGetDouble("/position/altitude-ft");
+ double altitude_agl_ft = fgGetDouble("/position/altitude-agl-ft");
+ double boundary_transition =
+ fgGetDouble("/environment/config/boundary-transition-ft", 500);
+
+ double ground_elevation_ft = altitude_ft - altitude_agl_ft;
+
+ int length = _boundary_table.size();
+
+ if (length > 0) {
+ // boundary table
+ double boundary_limit = _boundary_table[length-1]->altitude_ft;
+ if (boundary_limit >= altitude_agl_ft) {
+ do_interpolate(_boundary_table, altitude_agl_ft,
+ _environment);
+ return;
+ } else if ((boundary_limit + boundary_transition) >= altitude_agl_ft) {
+ // both tables
+ do_interpolate(_boundary_table, altitude_agl_ft, &env1);
+ do_interpolate(_aloft_table, altitude_ft, &env2);
+ double fraction =
+ (altitude_agl_ft - boundary_limit) / boundary_transition;
+ interpolate(&env1, &env2, fraction, _environment);
+ return;
+ }
+ }
+
+ // aloft table
+ do_interpolate(_aloft_table, altitude_ft, _environment);
+}
+
+void
+FGInterpolateEnvironmentCtrl::do_interpolate (vector<bucket *> &table,
+ double altitude_ft,
+ FGEnvironment * environment)
+{
+ int length = table.size();
+ if (length == 0)
+ return;
+
+ // Boundary conditions
+ if ((length == 1) || (table[0]->altitude_ft >= altitude_ft)) {
+ environment->copy(table[0]->environment);
+ return;
+ } else if (table[length-1]->altitude_ft <= altitude_ft) {
+ environment->copy(table[length-1]->environment);
+ return;
+ }
+
+ // Search the interpolation table
+ for (int i = 0; i < length - 1; i++) {
+ if ((i == length - 1) || (table[i]->altitude_ft <= altitude_ft)) {
+ FGEnvironment * env1 = &(table[i]->environment);
+ FGEnvironment * env2 = &(table[i+1]->environment);
+ double fraction;
+ if (table[i]->altitude_ft == table[i+1]->altitude_ft)
+ fraction = 1.0;
+ else
+ fraction =
+ ((altitude_ft - table[i]->altitude_ft) /
+ (table[i+1]->altitude_ft - table[i]->altitude_ft));
+ interpolate(env1, env2, fraction, environment);
+
+ return;
+ }
+ }
+}
+
+bool
+FGInterpolateEnvironmentCtrl::bucket::operator< (const bucket &b) const
+{
+ return (altitude_ft < b.altitude_ft);
+}
+
+
// end of environment_ctrl.cxx