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., 675 Mass Ave, Cambridge, MA 02139, USA.
36 #include <simgear/constants.h>
37 #include <simgear/debug/logstream.hxx>
38 #include <simgear/math/interpolater.hxx>
40 #include <Main/fg_props.hxx>
41 #include "environment.hxx"
44 FGEnvironment::FGEnvironment()
45 : _temperature_degc_table(new SGInterpTable),
46 _pressure_inhg_table(new SGInterpTable),
47 _density_slugft3_table(new SGInterpTable),
49 temperature_sea_level_degc(20),
50 pressure_sea_level_inhg(28),
51 density_sea_level_slugft3(0.0024),
52 wind_from_heading_deg(0),
54 wind_from_north_fps(0),
55 wind_from_east_fps(0),
61 FGEnvironment::FGEnvironment (const FGEnvironment &env)
62 : _temperature_degc_table(new SGInterpTable),
63 _pressure_inhg_table(new SGInterpTable),
64 _density_slugft3_table(new SGInterpTable),
65 elevation_ft(env.elevation_ft),
66 visibility_m(env.visibility_m),
67 temperature_sea_level_degc(env.temperature_sea_level_degc),
68 pressure_sea_level_inhg(env.pressure_sea_level_inhg),
69 density_sea_level_slugft3(env.density_sea_level_slugft3),
70 wind_from_heading_deg(env.wind_from_heading_deg),
71 wind_speed_kt(env.wind_speed_kt),
72 wind_from_north_fps(env.wind_from_north_fps),
73 wind_from_east_fps(env.wind_from_east_fps),
74 wind_from_down_fps(env.wind_from_down_fps)
79 FGEnvironment::~FGEnvironment()
81 delete _temperature_degc_table;
82 delete _pressure_inhg_table;
83 delete _density_slugft3_table;
88 FGEnvironment::get_visibility_m () const
94 FGEnvironment::get_temperature_sea_level_degc () const
96 return temperature_sea_level_degc;
100 FGEnvironment::get_temperature_degc () const
102 return temperature_degc;
106 FGEnvironment::get_pressure_sea_level_inhg () const
108 return pressure_sea_level_inhg;
112 FGEnvironment::get_pressure_inhg () const
114 return pressure_inhg;
118 FGEnvironment::get_density_sea_level_slugft3 () const
120 return density_sea_level_slugft3;
124 FGEnvironment::get_density_slugft3 () const
126 return density_slugft3;
130 FGEnvironment::get_wind_from_heading_deg () const
132 return wind_from_heading_deg;
136 FGEnvironment::get_wind_speed_kt () const
138 return wind_speed_kt;
142 FGEnvironment::get_wind_from_north_fps () const
144 return wind_from_north_fps;
148 FGEnvironment::get_wind_from_east_fps () const
150 return wind_from_east_fps;
154 FGEnvironment::get_wind_from_down_fps () const
156 return wind_from_down_fps;
160 FGEnvironment::get_elevation_ft () const
166 FGEnvironment::set_visibility_m (double v)
172 FGEnvironment::set_temperature_sea_level_degc (double t)
174 temperature_sea_level_degc = t;
175 _recalc_alt_temperature();
179 FGEnvironment::set_temperature_degc (double t)
181 temperature_degc = t;
182 _recalc_sl_temperature();
186 FGEnvironment::set_pressure_sea_level_inhg (double p)
188 pressure_sea_level_inhg = p;
189 _recalc_alt_pressure();
193 FGEnvironment::set_pressure_inhg (double p)
196 _recalc_sl_pressure();
200 FGEnvironment::set_density_sea_level_slugft3 (double d)
202 density_sea_level_slugft3 = d;
203 _recalc_alt_density();
207 FGEnvironment::set_density_slugft3 (double d)
210 _recalc_sl_density();
214 FGEnvironment::set_wind_from_heading_deg (double h)
216 wind_from_heading_deg = h;
221 FGEnvironment::set_wind_speed_kt (double s)
228 FGEnvironment::set_wind_from_north_fps (double n)
230 wind_from_north_fps = n;
235 FGEnvironment::set_wind_from_east_fps (double e)
237 wind_from_east_fps = e;
242 FGEnvironment::set_wind_from_down_fps (double d)
244 wind_from_down_fps = d;
249 FGEnvironment::set_elevation_ft (double e)
252 _recalc_alt_temperature();
253 _recalc_alt_pressure();
254 _recalc_alt_density();
259 // Copied from YASim Atmosphere.cxx, with m converted to ft, degK
260 // converted to degC, Pa converted to inHG, and kg/m^3 converted to
263 // Original comment from YASim:
265 // Copied from McCormick, who got it from "The ARDC Model Atmosphere"
266 // Note that there's an error in the text in the first entry,
267 // McCormick lists 299.16/101325/1.22500, but those don't agree with
268 // R=287. I chose to correct the temperature to 288.20, since 79F is
269 // pretty hot for a "standard" atmosphere.
271 // Elevation (ft), temperature (degC), pressure (inHG), density (slug/ft^3)
272 static double atmosphere_data[][4] = {
273 0, 0.00, 0.000, 0.000000,
274 2952, -5.89, -3.058, -0.000198,
275 5905, -11.74, -5.856, -0.000384,
276 8858, -17.58, -8.413, -0.000556,
277 11811, -23.43, -10.745, -0.000717,
278 14763, -29.27, -12.867, -0.000867,
279 17716, -35.11, -14.794, -0.001006,
280 20669, -40.95, -16.541, -0.001136,
281 23622, -46.79, -18.120, -0.001255,
282 26574, -52.62, -19.544, -0.001366,
283 29527, -58.46, -20.826, -0.001467,
284 32480, -64.29, -21.976, -0.001561,
285 35433, -70.12, -23.005, -0.001647,
286 38385, -71.54, -23.916, -0.001739,
287 41338, -71.54, -24.708, -0.001822,
288 44291, -71.54, -25.395, -0.001894,
289 47244, -71.54, -25.991, -0.001957,
290 50196, -71.54, -26.509, -0.002012,
291 53149, -71.54, -26.959, -0.002059,
292 56102, -71.54, -27.349, -0.002100,
293 59055, -71.54, -27.687, -0.002136,
294 62007, -71.54, -27.981, -0.002167,
299 FGEnvironment::_setup_tables ()
301 for (int i = 0; atmosphere_data[i][0] != -1; i++) {
302 _temperature_degc_table->addEntry(atmosphere_data[i][0],
303 atmosphere_data[i][1]);
304 _pressure_inhg_table->addEntry(atmosphere_data[i][0],
305 atmosphere_data[i][2]);
306 _density_slugft3_table->addEntry(atmosphere_data[i][0],
307 atmosphere_data[i][3]);
312 FGEnvironment::_recalc_hdgspd ()
316 if (wind_from_east_fps == 0) {
317 angle_rad = (wind_from_north_fps >= 0 ? SGD_PI/2 : -SGD_PI/2);
319 angle_rad = atan(wind_from_north_fps/wind_from_east_fps);
321 wind_from_heading_deg = angle_rad * SGD_RADIANS_TO_DEGREES;
322 if (wind_from_east_fps >= 0)
323 wind_from_heading_deg = 90 - wind_from_heading_deg;
325 wind_from_heading_deg = 270 - wind_from_heading_deg;
327 wind_speed_kt = fabs(wind_from_east_fps
328 * SG_METER_TO_NM * SG_FEET_TO_METER * 3600);
330 wind_speed_kt = (wind_from_north_fps / sin(angle_rad))
331 * SG_METER_TO_NM * SG_FEET_TO_METER * 3600;
335 FGEnvironment::_recalc_ne ()
338 wind_speed_kt * SG_NM_TO_METER * SG_METER_TO_FEET * (1.0/3600);
340 wind_from_north_fps = speed_fps *
341 cos(wind_from_heading_deg * SGD_DEGREES_TO_RADIANS);
342 wind_from_east_fps = speed_fps *
343 sin(wind_from_heading_deg * SGD_DEGREES_TO_RADIANS);
347 FGEnvironment::_recalc_sl_temperature ()
349 if (elevation_ft < 38000) {
350 temperature_sea_level_degc =
351 temperature_degc - _temperature_degc_table->interpolate(elevation_ft);
353 // If we're in the stratosphere, leave sea-level temp alone
357 FGEnvironment::_recalc_alt_temperature ()
360 temperature_sea_level_degc +
361 _temperature_degc_table->interpolate(elevation_ft);
365 FGEnvironment::_recalc_sl_pressure ()
367 pressure_sea_level_inhg =
368 pressure_inhg - _pressure_inhg_table->interpolate(elevation_ft);
372 FGEnvironment::_recalc_alt_pressure ()
375 pressure_sea_level_inhg + _pressure_inhg_table->interpolate(elevation_ft);
379 FGEnvironment::_recalc_sl_density ()
381 density_sea_level_slugft3 =
382 density_slugft3 - _density_slugft3_table->interpolate(elevation_ft);
386 FGEnvironment::_recalc_alt_density ()
389 density_sea_level_slugft3 +
390 _density_slugft3_table->interpolate(elevation_ft);
393 // end of environment.cxx