]> git.mxchange.org Git - flightgear.git/blob - src/Environment/environment.cxx
fix no-hole ladder (not that I think this mode will enjoy a long life)
[flightgear.git] / src / Environment / environment.cxx
1 // environment.cxx -- routines to model the natural environment
2 //
3 // Written by David Megginson, started February 2002.
4 //
5 // Copyright (C) 2002  David Megginson - david@megginson.com
6 //
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.
11 //
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.
16 //
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.
20 //
21 // $Id$
22
23
24 #ifdef HAVE_CONFIG_H
25 #  include <config.h>
26 #endif
27
28 #ifdef HAVE_WINDOWS_H
29 #  include <windows.h>                     
30 #endif
31
32 #include <math.h>
33
34 #include <plib/sg.h>
35
36 #include <simgear/constants.h>
37 #include <simgear/debug/logstream.hxx>
38 #include <simgear/math/interpolater.hxx>
39 #include <simgear/props/props.hxx>
40 #include <simgear/environment/visual_enviro.hxx>
41
42 #include <Main/fg_props.hxx>
43
44 #include "environment.hxx"
45
46
47 \f
48 ////////////////////////////////////////////////////////////////////////
49 // Atmosphere model.
50 ////////////////////////////////////////////////////////////////////////
51
52 // Copied from YASim Atmosphere.cxx, with m converted to ft, degK
53 // converted to degC, Pa converted to inHG, and kg/m^3 converted to
54 // slug/ft^3; they were then converted to deltas from the sea-level
55 // defaults (approx. 15degC, 29.92inHG, and 0.00237slugs/ft^3).
56
57 // Original comment from YASim:
58
59 // Copied from McCormick, who got it from "The ARDC Model Atmosphere"
60 // Note that there's an error in the text in the first entry,
61 // McCormick lists 299.16/101325/1.22500, but those don't agree with
62 // R=287.  I chose to correct the temperature to 288.20, since 79F is
63 // pretty hot for a "standard" atmosphere.
64
65 // Elevation (ft), temperature factor (degK), pressure factor (inHG)
66 static double atmosphere_data[][3] = {
67  { 0.00, 1.00, 1.000 },
68  { 2952.76, 0.98, 0.898 },
69  { 5905.51, 0.96, 0.804 },
70  { 8858.27, 0.94, 0.719 },
71  { 11811.02, 0.92, 0.641 },
72  { 14763.78, 0.90, 0.570 },
73  { 17716.54, 0.88, 0.506 },
74  { 20669.29, 0.86, 0.447 },
75  { 23622.05, 0.84, 0.394 },
76  { 26574.80, 0.82, 0.347 },
77  { 29527.56, 0.80, 0.304 },
78  { 32480.31, 0.78, 0.266 },
79  { 35433.07, 0.76, 0.231 },
80  { 38385.83, 0.75, 0.201 },
81  { 41338.58, 0.75, 0.174 },
82  { 44291.34, 0.75, 0.151 },
83  { 47244.09, 0.75, 0.131 },
84  { 50196.85, 0.75, 0.114 },
85  { 53149.61, 0.75, 0.099 },
86  { 56102.36, 0.75, 0.086 },
87  { 59055.12, 0.75, 0.075 },
88  { 62007.87, 0.75, 0.065 },
89  { -1, -1, -1 }
90 };
91
92 static SGInterpTable * _temperature_degc_table = 0;
93 static SGInterpTable * _pressure_inhg_table = 0;
94
95 static void
96 _setup_tables ()
97 {
98   if (_temperature_degc_table != 0)
99       return;
100
101   _temperature_degc_table = new SGInterpTable;
102   _pressure_inhg_table = new SGInterpTable;
103
104   for (int i = 0; atmosphere_data[i][0] != -1; i++) {
105     _temperature_degc_table->addEntry(atmosphere_data[i][0],
106                                       atmosphere_data[i][1]);
107     _pressure_inhg_table->addEntry(atmosphere_data[i][0],
108                                    atmosphere_data[i][2]);
109   }
110 }
111
112
113 \f
114 ////////////////////////////////////////////////////////////////////////
115 // Implementation of FGEnvironment.
116 ////////////////////////////////////////////////////////////////////////
117
118 FGEnvironment::FGEnvironment()
119   : elevation_ft(0),
120     visibility_m(32000),
121     temperature_sea_level_degc(15),
122     temperature_degc(15),
123     dewpoint_sea_level_degc(5), // guess
124     dewpoint_degc(5),
125     pressure_sea_level_inhg(29.92),
126     pressure_inhg(29.92),
127     turbulence_magnitude_norm(0),
128     turbulence_rate_hz(1),
129     wind_from_heading_deg(0),
130     wind_speed_kt(0),
131     wind_from_north_fps(0),
132     wind_from_east_fps(0),
133     wind_from_down_fps(0),
134     altitude_half_to_sun_m(1000),
135     altitude_tropo_top_m(10000)
136 {
137   _setup_tables();
138   _recalc_density();
139   _recalc_relative_humidity();
140 }
141
142 FGEnvironment::FGEnvironment (const FGEnvironment &env)
143 {
144     FGEnvironment();
145     copy(env);
146 }
147
148 FGEnvironment::~FGEnvironment()
149 {
150 }
151
152 void
153 FGEnvironment::copy (const FGEnvironment &env)
154 {
155     elevation_ft = env.elevation_ft;
156     visibility_m = env.visibility_m;
157     temperature_sea_level_degc = env.temperature_sea_level_degc;
158     temperature_degc = env.temperature_degc;
159     dewpoint_sea_level_degc = env.dewpoint_sea_level_degc;
160     dewpoint_degc = env.dewpoint_degc;
161     pressure_sea_level_inhg = env.pressure_sea_level_inhg;
162     wind_from_heading_deg = env.wind_from_heading_deg;
163     wind_speed_kt = env.wind_speed_kt;
164     wind_from_north_fps = env.wind_from_north_fps;
165     wind_from_east_fps = env.wind_from_east_fps;
166     wind_from_down_fps = env.wind_from_down_fps;
167     turbulence_magnitude_norm = env.turbulence_magnitude_norm;
168     turbulence_rate_hz = env.turbulence_rate_hz;
169 }
170
171 static inline bool
172 maybe_copy_value (FGEnvironment * env, const SGPropertyNode * node,
173                   const char * name, void (FGEnvironment::*setter)(double))
174 {
175     const SGPropertyNode * child = node->getNode(name);
176                                 // fragile: depends on not being typed
177                                 // as a number
178     if (child != 0 && child->hasValue() &&
179         child->getStringValue()[0] != '\0') {
180         (env->*setter)(child->getDoubleValue());
181         return true;
182     } else {
183         return false;
184     }
185 }
186
187 void
188 FGEnvironment::read (const SGPropertyNode * node)
189 {
190     maybe_copy_value(this, node, "visibility-m",
191                      &FGEnvironment::set_visibility_m);
192
193     if (!maybe_copy_value(this, node, "temperature-sea-level-degc",
194                           &FGEnvironment::set_temperature_sea_level_degc))
195         maybe_copy_value(this, node, "temperature-degc",
196                          &FGEnvironment::set_temperature_degc);
197
198     if (!maybe_copy_value(this, node, "dewpoint-sea-level-degc",
199                           &FGEnvironment::set_dewpoint_sea_level_degc))
200         maybe_copy_value(this, node, "dewpoint-degc",
201                          &FGEnvironment::set_dewpoint_degc);
202
203     if (!maybe_copy_value(this, node, "pressure-sea-level-inhg",
204                           &FGEnvironment::set_pressure_sea_level_inhg))
205         maybe_copy_value(this, node, "pressure-inhg",
206                          &FGEnvironment::set_pressure_inhg);
207
208     maybe_copy_value(this, node, "wind-from-heading-deg",
209                      &FGEnvironment::set_wind_from_heading_deg);
210
211     maybe_copy_value(this, node, "wind-speed-kt",
212                      &FGEnvironment::set_wind_speed_kt);
213
214     maybe_copy_value(this, node, "elevation-ft",
215                      &FGEnvironment::set_elevation_ft);
216
217     maybe_copy_value(this, node, "turbulence/magnitude-norm",
218                      &FGEnvironment::set_turbulence_magnitude_norm);
219
220     maybe_copy_value(this, node, "turbulence/rate-hz",
221                      &FGEnvironment::set_turbulence_rate_hz);
222 }
223
224
225 double
226 FGEnvironment::get_visibility_m () const
227 {
228   return visibility_m;
229 }
230
231 double
232 FGEnvironment::get_temperature_sea_level_degc () const
233 {
234   return temperature_sea_level_degc;
235 }
236
237 double
238 FGEnvironment::get_temperature_degc () const
239 {
240   return temperature_degc;
241 }
242
243 double
244 FGEnvironment::get_temperature_degf () const
245 {
246   return (temperature_degc * 9.0 / 5.0) + 32.0;
247 }
248
249 double
250 FGEnvironment::get_dewpoint_sea_level_degc () const
251 {
252   return dewpoint_sea_level_degc;
253 }
254
255 double
256 FGEnvironment::get_dewpoint_degc () const
257 {
258   return dewpoint_degc;
259 }
260
261 double
262 FGEnvironment::get_pressure_sea_level_inhg () const
263 {
264   return pressure_sea_level_inhg;
265 }
266
267 double
268 FGEnvironment::get_pressure_inhg () const
269 {
270   return pressure_inhg;
271 }
272
273 double
274 FGEnvironment::get_density_slugft3 () const
275 {
276   return density_slugft3;
277 }
278
279 double
280 FGEnvironment::get_relative_humidity () const
281 {
282   return relative_humidity;
283 }
284
285 double
286 FGEnvironment::get_density_tropo_avg_kgm3 () const
287 {
288   return density_tropo_avg_kgm3;
289 }
290
291 double
292 FGEnvironment::get_altitude_half_to_sun_m () const
293 {
294   return altitude_half_to_sun_m;
295 }
296
297 double
298 FGEnvironment::get_altitude_tropo_top_m () const
299 {
300   return altitude_tropo_top_m;
301 }
302
303 double
304 FGEnvironment::get_wind_from_heading_deg () const
305 {
306   return wind_from_heading_deg;
307 }
308
309 double
310 FGEnvironment::get_wind_speed_kt () const
311 {
312   return wind_speed_kt;
313 }
314
315 double
316 FGEnvironment::get_wind_from_north_fps () const
317 {
318   return wind_from_north_fps;
319 }
320
321 double
322 FGEnvironment::get_wind_from_east_fps () const
323 {
324   return wind_from_east_fps;
325 }
326
327 double
328 FGEnvironment::get_wind_from_down_fps () const
329 {
330   return wind_from_down_fps;
331 }
332
333 double
334 FGEnvironment::get_turbulence_magnitude_norm () const
335 {
336   if( sgEnviro.get_turbulence_enable_state() )
337     if (fgGetBool("/environment/params/real-world-weather-fetch") == true)
338       return sgEnviro.get_cloud_turbulence();
339   return turbulence_magnitude_norm;
340 }
341
342 double
343 FGEnvironment::get_turbulence_rate_hz () const
344 {
345   return turbulence_rate_hz;
346 }
347
348 double
349 FGEnvironment::get_elevation_ft () const
350 {
351   return elevation_ft;
352 }
353
354 void
355 FGEnvironment::set_visibility_m (double v)
356 {
357   visibility_m = v;
358 }
359
360 void
361 FGEnvironment::set_temperature_sea_level_degc (double t)
362 {
363   temperature_sea_level_degc = t;
364   if (dewpoint_sea_level_degc > t)
365       dewpoint_sea_level_degc = t;
366   _recalc_alt_temperature();
367   _recalc_density();
368 }
369
370 void
371 FGEnvironment::set_temperature_degc (double t)
372 {
373   temperature_degc = t;
374   _recalc_sl_temperature();
375   _recalc_density();
376   _recalc_relative_humidity();
377 }
378
379 void
380 FGEnvironment::set_dewpoint_sea_level_degc (double t)
381 {
382   dewpoint_sea_level_degc = t;
383   if (temperature_sea_level_degc < t)
384       temperature_sea_level_degc = t;
385   _recalc_alt_dewpoint();
386   _recalc_density();
387 }
388
389 void
390 FGEnvironment::set_dewpoint_degc (double t)
391 {
392   dewpoint_degc = t;
393   _recalc_sl_dewpoint();
394   _recalc_density();
395   _recalc_relative_humidity();
396 }
397
398 void
399 FGEnvironment::set_pressure_sea_level_inhg (double p)
400 {
401   pressure_sea_level_inhg = p;
402   _recalc_alt_pressure();
403   _recalc_density();
404 }
405
406 void
407 FGEnvironment::set_pressure_inhg (double p)
408 {
409   pressure_inhg = p;
410   _recalc_sl_pressure();
411   _recalc_density();
412 }
413
414 void
415 FGEnvironment::set_wind_from_heading_deg (double h)
416 {
417   wind_from_heading_deg = h;
418   _recalc_ne();
419 }
420
421 void
422 FGEnvironment::set_wind_speed_kt (double s)
423 {
424   wind_speed_kt = s;
425   _recalc_ne();
426 }
427
428 void
429 FGEnvironment::set_wind_from_north_fps (double n)
430 {
431   wind_from_north_fps = n;
432   _recalc_hdgspd();
433 }
434
435 void
436 FGEnvironment::set_wind_from_east_fps (double e)
437 {
438   wind_from_east_fps = e;
439   _recalc_hdgspd();
440 }
441
442 void
443 FGEnvironment::set_wind_from_down_fps (double d)
444 {
445   wind_from_down_fps = d;
446   _recalc_hdgspd();
447 }
448
449 void
450 FGEnvironment::set_turbulence_magnitude_norm (double t)
451 {
452   turbulence_magnitude_norm = t;
453 }
454
455 void
456 FGEnvironment::set_turbulence_rate_hz (double r)
457 {
458   turbulence_rate_hz = r;
459 }
460
461 void
462 FGEnvironment::set_elevation_ft (double e)
463 {
464   elevation_ft = e;
465   _recalc_alt_temperature();
466   _recalc_alt_dewpoint();
467   _recalc_alt_pressure();
468   _recalc_density();
469   _recalc_relative_humidity();
470 }
471
472 void
473 FGEnvironment::set_altitude_half_to_sun_m (double alt)
474 {
475  altitude_half_to_sun_m = alt;
476  _recalc_density_tropo_avg_kgm3();
477 }
478
479 void
480 FGEnvironment::set_altitude_tropo_top_m (double alt)
481 {
482  altitude_tropo_top_m = alt;
483  _recalc_density_tropo_avg_kgm3();
484 }
485
486
487 void
488 FGEnvironment::_recalc_hdgspd ()
489 {
490   double angle_rad;
491
492   if (wind_from_east_fps == 0) {
493     angle_rad = (wind_from_north_fps >= 0 ? SGD_PI_2 : -SGD_PI_2);
494   } else {
495     angle_rad = atan(wind_from_north_fps/wind_from_east_fps);
496   }
497   wind_from_heading_deg = angle_rad * SGD_RADIANS_TO_DEGREES;
498   if (wind_from_east_fps >= 0)
499     wind_from_heading_deg = 90 - wind_from_heading_deg;
500   else
501     wind_from_heading_deg = 270 - wind_from_heading_deg;
502
503 #if 0
504   // FIXME: Windspeed can become negative with these formulas.
505   //        which can cause problems for animations that rely
506   //        on the windspeed property.
507   if (angle_rad == 0)
508     wind_speed_kt = fabs(wind_from_east_fps
509                          * SG_METER_TO_NM * SG_FEET_TO_METER * 3600);
510   else
511     wind_speed_kt = (wind_from_north_fps / sin(angle_rad))
512       * SG_METER_TO_NM * SG_FEET_TO_METER * 3600;
513 #else
514   wind_speed_kt = sqrt(wind_from_north_fps * wind_from_north_fps +
515                        wind_from_east_fps * wind_from_east_fps) 
516                   * SG_METER_TO_NM * SG_FEET_TO_METER * 3600;
517 #endif
518 }
519
520 void
521 FGEnvironment::_recalc_ne ()
522 {
523   double speed_fps =
524     wind_speed_kt * SG_NM_TO_METER * SG_METER_TO_FEET * (1.0/3600);
525
526   wind_from_north_fps = speed_fps *
527     cos(wind_from_heading_deg * SGD_DEGREES_TO_RADIANS);
528   wind_from_east_fps = speed_fps *
529     sin(wind_from_heading_deg * SGD_DEGREES_TO_RADIANS);
530 }
531
532 void
533 FGEnvironment::_recalc_sl_temperature ()
534 {
535   // If we're in the stratosphere, leave sea-level temp alone
536   if (elevation_ft < 38000) {
537     temperature_sea_level_degc = (temperature_degc + 273.15)
538         / _temperature_degc_table->interpolate(elevation_ft)
539       - 273.15;
540   }
541 }
542
543 void
544 FGEnvironment::_recalc_alt_temperature ()
545 {
546   if (elevation_ft < 38000) {
547     temperature_degc = (temperature_sea_level_degc + 273.15) *
548         _temperature_degc_table->interpolate(elevation_ft) - 273.15;
549   } else {
550     temperature_degc = -56.49;  // Stratosphere is constant
551   }
552 }
553
554 void
555 FGEnvironment::_recalc_sl_dewpoint ()
556 {
557                                 // 0.2degC/1000ft
558                                 // FIXME: this will work only for low
559                                 // elevations
560   dewpoint_sea_level_degc = dewpoint_degc + (elevation_ft * .0002);
561   if (dewpoint_sea_level_degc > temperature_sea_level_degc)
562     dewpoint_sea_level_degc = temperature_sea_level_degc;
563 }
564
565 void
566 FGEnvironment::_recalc_alt_dewpoint ()
567 {
568                                 // 0.2degC/1000ft
569                                 // FIXME: this will work only for low
570                                 // elevations
571   dewpoint_degc = dewpoint_sea_level_degc + (elevation_ft * .0002);
572   if (dewpoint_degc > temperature_degc)
573     dewpoint_degc = temperature_degc;
574 }
575
576 void
577 FGEnvironment::_recalc_sl_pressure ()
578 {
579   pressure_sea_level_inhg =
580     pressure_inhg / _pressure_inhg_table->interpolate(elevation_ft);
581 }
582
583 void
584 FGEnvironment::_recalc_alt_pressure ()
585 {
586   pressure_inhg =
587     pressure_sea_level_inhg * _pressure_inhg_table->interpolate(elevation_ft);
588 }
589
590 void
591 FGEnvironment::_recalc_density ()
592 {
593   double pressure_psf = pressure_inhg * 70.7487;
594   
595   // adjust for humidity
596   // calculations taken from USA Today (oops!) at
597   // http://www.usatoday.com/weather/basics/density-calculations.htm
598   double temperature_degk = temperature_degc + 273.15;
599   double pressure_mb = pressure_inhg * 33.86;
600   double vapor_pressure_mb =
601     6.11 * pow(10.0, 7.5 * dewpoint_degc / (237.7 + dewpoint_degc));
602   double virtual_temperature_degk = temperature_degk / (1 - (vapor_pressure_mb / pressure_mb) * (1.0 - 0.622));
603   double virtual_temperature_degr = virtual_temperature_degk * 1.8;
604
605   density_slugft3 = pressure_psf / (virtual_temperature_degr * 1718);
606   _recalc_density_tropo_avg_kgm3();
607 }
608
609 // This is used to calculate the average density on the path 
610 // of sunlight to the observer for calculating sun-color
611 void
612 FGEnvironment::_recalc_density_tropo_avg_kgm3 ()
613 {
614   double pressure_mb = pressure_inhg * 33.86;
615   double vaporpressure = 6.11 * pow(10.0, ((7.5 * dewpoint_degc) / (237.7 + dewpoint_degc)));
616
617   double virtual_temp = (temperature_degc + 273.15) / (1 - 0.379 * (vaporpressure/pressure_mb));
618
619   double density_half = (100 * pressure_mb * exp(-altitude_half_to_sun_m / 8000))
620       / (287.05 * virtual_temp);
621   double density_tropo = (100 * pressure_mb * exp((-1 * altitude_tropo_top_m) / 8000))
622       / ( 287.05 * virtual_temp);
623
624   density_tropo_avg_kgm3 = ((density_slugft3 * 515.379) + density_half + density_tropo) / 3;
625 }
626
627 void
628 FGEnvironment::_recalc_relative_humidity ()
629 {
630   double vaporpressure = 6.11 * pow(10.0, ((7.5 * dewpoint_degc) / ( 237.7 + dewpoint_degc)));
631   double sat_vaporpressure = 6.11 * pow(10.0, ((7.5 * temperature_degc)
632       / ( 237.7 + temperature_degc)) );
633   relative_humidity = 100 * vaporpressure / sat_vaporpressure ;
634 }
635
636
637
638 ////////////////////////////////////////////////////////////////////////
639 // Functions.
640 ////////////////////////////////////////////////////////////////////////
641
642 static inline double
643 do_interp (double a, double b, double fraction)
644 {
645     double retval = (a + ((b - a) * fraction));
646     return retval;
647 }
648
649 static inline double
650 do_interp_deg (double a, double b, double fraction)
651 {
652     a = fmod(a, 360);
653     b = fmod(b, 360);
654     if (fabs(b-a) > 180) {
655         if (a < b)
656             a += 360;
657         else
658             b += 360;
659     }
660     return fmod(do_interp(a, b, fraction), 360);
661 }
662
663 void
664 interpolate (const FGEnvironment * env1, const FGEnvironment * env2,
665              double fraction, FGEnvironment * result)
666 {
667     result->set_visibility_m
668         (do_interp(env1->get_visibility_m(),
669                    env2->get_visibility_m(),
670                    fraction));
671
672     result->set_temperature_sea_level_degc
673         (do_interp(env1->get_temperature_sea_level_degc(),
674                    env2->get_temperature_sea_level_degc(),
675                    fraction));
676
677     result->set_dewpoint_degc
678         (do_interp(env1->get_dewpoint_sea_level_degc(),
679                    env2->get_dewpoint_sea_level_degc(),
680                    fraction));
681
682     result->set_pressure_sea_level_inhg
683         (do_interp(env1->get_pressure_sea_level_inhg(),
684                    env2->get_pressure_sea_level_inhg(),
685                    fraction));
686
687     result->set_wind_from_heading_deg
688         (do_interp_deg(env1->get_wind_from_heading_deg(),
689                        env2->get_wind_from_heading_deg(),
690                        fraction));
691
692     result->set_wind_speed_kt
693         (do_interp(env1->get_wind_speed_kt(),
694                    env2->get_wind_speed_kt(),
695                    fraction));
696
697     result->set_elevation_ft
698         (do_interp(env1->get_elevation_ft(),
699                    env2->get_elevation_ft(),
700                    fraction));
701
702     result->set_turbulence_magnitude_norm
703         (do_interp(env1->get_turbulence_magnitude_norm(),
704                    env2->get_turbulence_magnitude_norm(),
705                    fraction));
706
707     result->set_turbulence_rate_hz
708         (do_interp(env1->get_turbulence_rate_hz(),
709                    env2->get_turbulence_rate_hz(),
710                    fraction));
711 }
712
713 // end of environment.cxx