]> git.mxchange.org Git - flightgear.git/blob - src/Environment/environment.cxx
fix a pointer reference.
[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 #include <math.h>
29
30 #include <boost/tuple/tuple.hpp>
31
32 #include <simgear/constants.h>
33 #include <simgear/debug/logstream.hxx>
34 #include <simgear/math/interpolater.hxx>
35 #include <simgear/props/props.hxx>
36 #include <simgear/environment/visual_enviro.hxx>
37
38 #include <Main/fg_props.hxx>
39 #include <signal.h>
40
41 #include "environment.hxx"
42 #include "atmosphere.hxx"
43
44 \f
45 ////////////////////////////////////////////////////////////////////////
46 // Atmosphere model.
47 ////////////////////////////////////////////////////////////////////////
48
49 #ifdef USING_TABLES
50
51 // Calculated based on the ISA standard day, as found at e.g.
52 // http://www.av8n.com/physics/altimetry.htm
53
54 // Each line of data has 3 elements:
55 //   Elevation (ft), 
56 //   temperature factor (dimensionless ratio of absolute temp), 
57 //   pressure factor (dimensionless ratio)
58 static double atmosphere_data[][3] = {
59  {  -3000.00,   1.021,  1.1133 },
60  {      0.00,   1.000,  1.0000 },
61  {   2952.76,   0.980,  0.8978 },
62  {   5905.51,   0.959,  0.8042 },
63  {   8858.27,   0.939,  0.7187 },
64  {  11811.02,   0.919,  0.6407 },
65  {  14763.78,   0.898,  0.5697 },
66  {  17716.54,   0.878,  0.5052 },
67  {  20669.29,   0.858,  0.4468 },
68  {  23622.05,   0.838,  0.3940 },
69  {  26574.80,   0.817,  0.3463 },
70  {  29527.56,   0.797,  0.3034 },
71  {  32480.31,   0.777,  0.2649 },
72  {  35433.07,   0.756,  0.2305 },
73  {  38385.83,   0.752,  0.2000 },
74  {  41338.58,   0.752,  0.1736 },
75  {  44291.34,   0.752,  0.1506 },
76  {  47244.09,   0.752,  0.1307 },
77  {  50196.85,   0.752,  0.1134 },
78  {  53149.61,   0.752,  0.0984 },
79  {  56102.36,   0.752,  0.0854 },
80  {  59055.12,   0.752,  0.0741 },
81  {  62007.87,   0.752,  0.0643 },
82  {  65000.00,   0.752,  0.0557 },
83  {  68000.00,   0.754,  0.0482 },
84  {  71000.00,   0.758,  0.0418 },
85  {  74000.00,   0.761,  0.0362 },
86  {  77000.00,   0.764,  0.0314 },
87  {  80000.00,   0.767,  0.0273 },
88  {  83000.00,   0.770,  0.0237 },
89  {  86000.00,   0.773,  0.0206 },
90  {  89000.00,   0.777,  0.0179 },
91  {  92000.00,   0.780,  0.0156 },
92  {  95000.00,   0.783,  0.0135 },
93  {  98000.00,   0.786,  0.0118 },
94  { 101000.00,   0.789,  0.0103 },
95  { -1, -1, -1 }
96 };
97
98 static SGInterpTable * _temperature_degc_table = 0;
99 static SGInterpTable * _pressure_inhg_table = 0;
100
101 static void
102 _setup_tables ()
103 {
104   if (_temperature_degc_table != 0)
105       return;
106
107   _temperature_degc_table = new SGInterpTable;
108   _pressure_inhg_table = new SGInterpTable;
109
110   for (int i = 0; atmosphere_data[i][0] != -1; i++) {
111     _temperature_degc_table->addEntry(atmosphere_data[i][0],
112                                       atmosphere_data[i][1]);
113     _pressure_inhg_table->addEntry(atmosphere_data[i][0],
114                                    atmosphere_data[i][2]);
115   }
116 }
117 #endif
118
119 \f
120 ////////////////////////////////////////////////////////////////////////
121 // Implementation of FGEnvironment.
122 ////////////////////////////////////////////////////////////////////////
123
124 void FGEnvironment::_init()
125 {
126     elevation_ft = 0;
127     visibility_m = 32000;
128     temperature_sea_level_degc = 15;
129     temperature_degc = 15;
130     dewpoint_sea_level_degc = 5; // guess
131     dewpoint_degc = 5;
132     pressure_sea_level_inhg = 29.92;
133     pressure_inhg = 29.92;
134     turbulence_magnitude_norm = 0;
135     turbulence_rate_hz = 1;
136     wind_from_heading_deg = 0;
137     wind_speed_kt = 0;
138     wind_from_north_fps = 0;
139     wind_from_east_fps = 0;
140     wind_from_down_fps = 0;
141     thermal_lift_fps = 0;
142     ridge_lift_fps= 0;
143     altitude_half_to_sun_m = 1000;
144     altitude_tropo_top_m = 10000;
145 #ifdef USING_TABLES
146     _setup_tables();
147 #endif
148     _recalc_density();
149     _recalc_relative_humidity();
150     live_update = true;
151 }
152
153 FGEnvironment::FGEnvironment()
154 {
155     _init();
156 }
157
158 FGEnvironment::FGEnvironment (const FGEnvironment &env)
159 {
160     _init();
161     copy(env);
162 }
163
164 FGEnvironment::~FGEnvironment()
165 {
166 }
167
168 void
169 FGEnvironment::copy (const FGEnvironment &env)
170 {
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     thermal_lift_fps = env.thermal_lift_fps;
184     ridge_lift_fps= env.ridge_lift_fps;
185     turbulence_magnitude_norm = env.turbulence_magnitude_norm;
186     turbulence_rate_hz = env.turbulence_rate_hz;
187 }
188
189 static inline bool
190 maybe_copy_value (FGEnvironment * env, const SGPropertyNode * node,
191                   const char * name, void (FGEnvironment::*setter)(double))
192 {
193     const SGPropertyNode * child = node->getNode(name);
194                                 // fragile: depends on not being typed
195                                 // as a number
196     if (child != 0 && child->hasValue() &&
197         child->getStringValue()[0] != '\0') {
198         (env->*setter)(child->getDoubleValue());
199         return true;
200     } else {
201         return false;
202     }
203 }
204
205 void
206 FGEnvironment::read (const SGPropertyNode * node)
207 {
208     bool live_update = set_live_update( false );
209     maybe_copy_value(this, node, "visibility-m",
210                      &FGEnvironment::set_visibility_m);
211
212     if (!maybe_copy_value(this, node, "temperature-sea-level-degc",
213                           &FGEnvironment::set_temperature_sea_level_degc))
214         maybe_copy_value(this, node, "temperature-degc",
215                          &FGEnvironment::set_temperature_degc);
216
217     if (!maybe_copy_value(this, node, "dewpoint-sea-level-degc",
218                           &FGEnvironment::set_dewpoint_sea_level_degc))
219         maybe_copy_value(this, node, "dewpoint-degc",
220                          &FGEnvironment::set_dewpoint_degc);
221
222     if (!maybe_copy_value(this, node, "pressure-sea-level-inhg",
223                           &FGEnvironment::set_pressure_sea_level_inhg))
224         maybe_copy_value(this, node, "pressure-inhg",
225                          &FGEnvironment::set_pressure_inhg);
226
227     maybe_copy_value(this, node, "wind-from-heading-deg",
228                      &FGEnvironment::set_wind_from_heading_deg);
229
230     maybe_copy_value(this, node, "wind-speed-kt",
231                      &FGEnvironment::set_wind_speed_kt);
232
233     maybe_copy_value(this, node, "elevation-ft",
234                      &FGEnvironment::set_elevation_ft);
235
236     maybe_copy_value(this, node, "turbulence/magnitude-norm",
237                      &FGEnvironment::set_turbulence_magnitude_norm);
238
239     maybe_copy_value(this, node, "turbulence/rate-hz",
240                      &FGEnvironment::set_turbulence_rate_hz);
241     // calculate derived properties here to avoid duplicate expensive computations
242     _recalc_ne();
243     _recalc_alt_pt();
244     _recalc_alt_dewpoint();
245     _recalc_density();
246     _recalc_relative_humidity();
247
248     set_live_update(live_update);
249 }
250
251
252 double
253 FGEnvironment::get_visibility_m () const
254 {
255   return visibility_m;
256 }
257
258 double
259 FGEnvironment::get_temperature_sea_level_degc () const
260 {
261   return temperature_sea_level_degc;
262 }
263
264 double
265 FGEnvironment::get_temperature_degc () const
266 {
267   return temperature_degc;
268 }
269
270 double
271 FGEnvironment::get_temperature_degf () const
272 {
273   return (temperature_degc * 9.0 / 5.0) + 32.0;
274 }
275
276 double
277 FGEnvironment::get_dewpoint_sea_level_degc () const
278 {
279   return dewpoint_sea_level_degc;
280 }
281
282 double
283 FGEnvironment::get_dewpoint_degc () const
284 {
285   return dewpoint_degc;
286 }
287
288 double
289 FGEnvironment::get_pressure_sea_level_inhg () const
290 {
291   return pressure_sea_level_inhg;
292 }
293
294 double
295 FGEnvironment::get_pressure_inhg () const
296 {
297   return pressure_inhg;
298 }
299
300 double
301 FGEnvironment::get_density_slugft3 () const
302 {
303   return density_slugft3;
304 }
305
306 double
307 FGEnvironment::get_relative_humidity () const
308 {
309   return relative_humidity;
310 }
311
312 double
313 FGEnvironment::get_density_tropo_avg_kgm3 () const
314 {
315   return density_tropo_avg_kgm3;
316 }
317
318 double
319 FGEnvironment::get_altitude_half_to_sun_m () const
320 {
321   return altitude_half_to_sun_m;
322 }
323
324 double
325 FGEnvironment::get_altitude_tropo_top_m () const
326 {
327   return altitude_tropo_top_m;
328 }
329
330 double
331 FGEnvironment::get_wind_from_heading_deg () const
332 {
333   return wind_from_heading_deg;
334 }
335
336 double
337 FGEnvironment::get_wind_speed_kt () const
338 {
339   return wind_speed_kt;
340 }
341
342 double
343 FGEnvironment::get_wind_from_north_fps () const
344 {
345   return wind_from_north_fps;
346 }
347
348 double
349 FGEnvironment::get_wind_from_east_fps () const
350 {
351   return wind_from_east_fps;
352 }
353
354 double
355 FGEnvironment::get_wind_from_down_fps () const
356 {
357   return wind_from_down_fps;
358 }
359
360 double
361 FGEnvironment::get_thermal_lift_fps () const
362 {
363   return thermal_lift_fps;
364 }
365
366 double
367 FGEnvironment::get_ridge_lift_fps () const
368 {
369   return ridge_lift_fps;
370 }
371
372 double
373 FGEnvironment::get_turbulence_magnitude_norm () const
374 {
375   if( sgEnviro.get_turbulence_enable_state() )
376     if (fgGetBool("/environment/params/real-world-weather-fetch") == true)
377       return sgEnviro.get_cloud_turbulence();
378   return turbulence_magnitude_norm;
379 }
380
381 double
382 FGEnvironment::get_turbulence_rate_hz () const
383 {
384   return turbulence_rate_hz;
385 }
386
387 double
388 FGEnvironment::get_elevation_ft () const
389 {
390   return elevation_ft;
391 }
392
393 void
394 FGEnvironment::set_visibility_m (double v)
395 {
396   visibility_m = v;
397 }
398
399 void
400 FGEnvironment::set_temperature_sea_level_degc (double t)
401 {
402   temperature_sea_level_degc = t;
403   if (dewpoint_sea_level_degc > t)
404       dewpoint_sea_level_degc = t;
405   if( live_update ) {
406     _recalc_alt_pt();
407     _recalc_density();
408   }
409 }
410
411 void
412 FGEnvironment::set_temperature_degc (double t)
413 {
414   temperature_degc = t;
415   if( live_update ) {
416     _recalc_sl_temperature();
417     _recalc_sl_pressure();
418     _recalc_alt_pt();
419     _recalc_density();
420     _recalc_relative_humidity();
421   }
422 }
423
424 void
425 FGEnvironment::set_dewpoint_sea_level_degc (double t)
426 {
427   dewpoint_sea_level_degc = t;
428   if (temperature_sea_level_degc < t)
429       temperature_sea_level_degc = t;
430   if( live_update ) {
431     _recalc_alt_dewpoint();
432     _recalc_density();
433   }
434 }
435
436 void
437 FGEnvironment::set_dewpoint_degc (double t)
438 {
439   dewpoint_degc = t;
440   if( live_update ) {
441     _recalc_sl_dewpoint();
442     _recalc_density();
443     _recalc_relative_humidity();
444   }
445 }
446
447 void
448 FGEnvironment::set_pressure_sea_level_inhg (double p)
449 {
450   pressure_sea_level_inhg = p;
451   if( live_update ) {
452     _recalc_alt_pt();
453     _recalc_density();
454   }
455 }
456
457 void
458 FGEnvironment::set_pressure_inhg (double p)
459 {
460   pressure_inhg = p;
461   if( live_update ) {
462     _recalc_sl_pressure();
463     _recalc_density();
464   }
465 }
466
467 void
468 FGEnvironment::set_wind_from_heading_deg (double h)
469 {
470   wind_from_heading_deg = h;
471   if( live_update ) {
472     _recalc_ne();
473   }
474 }
475
476 void
477 FGEnvironment::set_wind_speed_kt (double s)
478 {
479   wind_speed_kt = s;
480   if( live_update ) {
481     _recalc_ne();
482   }
483 }
484
485 void
486 FGEnvironment::set_wind_from_north_fps (double n)
487 {
488   wind_from_north_fps = n;
489   if( live_update ) {
490     _recalc_hdgspd();
491   }
492 }
493
494 void
495 FGEnvironment::set_wind_from_east_fps (double e)
496 {
497   wind_from_east_fps = e;
498   if( live_update ) {
499     _recalc_hdgspd();
500   }
501 }
502
503 void
504 FGEnvironment::set_wind_from_down_fps (double d)
505 {
506   wind_from_down_fps = d;
507   if( live_update ) {
508     _recalc_hdgspd();
509   }
510 }
511
512 void
513 FGEnvironment::set_thermal_lift_fps (double th)
514 {
515   thermal_lift_fps = th;
516   if( live_update ) {
517     _recalc_updraft();
518   }
519 }
520
521 void
522 FGEnvironment::set_ridge_lift_fps (double ri)
523 {
524   ridge_lift_fps = ri;
525   if( live_update ) {
526     _recalc_updraft();
527 }
528 }
529
530 void
531 FGEnvironment::set_turbulence_magnitude_norm (double t)
532 {
533   turbulence_magnitude_norm = t;
534 }
535
536 void
537 FGEnvironment::set_turbulence_rate_hz (double r)
538 {
539   turbulence_rate_hz = r;
540 }
541
542 void
543 FGEnvironment::set_elevation_ft (double e)
544 {
545   elevation_ft = e;
546   if( live_update ) {
547     _recalc_alt_pt();
548     _recalc_alt_dewpoint();
549     _recalc_density();
550     _recalc_relative_humidity();
551   }
552 }
553
554 void
555 FGEnvironment::set_altitude_half_to_sun_m (double alt)
556 {
557   altitude_half_to_sun_m = alt;
558   if( live_update ) {
559     _recalc_density_tropo_avg_kgm3();
560   }
561 }
562
563 void
564 FGEnvironment::set_altitude_tropo_top_m (double alt)
565 {
566   altitude_tropo_top_m = alt;
567   if( live_update ) {
568     _recalc_density_tropo_avg_kgm3();
569   }
570 }
571
572
573 void
574 FGEnvironment::_recalc_hdgspd ()
575 {
576   double angle_rad;
577
578   if (wind_from_east_fps == 0) {
579     angle_rad = (wind_from_north_fps >= 0 ? SGD_PI_2 : -SGD_PI_2);
580   } else {
581     angle_rad = atan(wind_from_north_fps/wind_from_east_fps);
582   }
583   wind_from_heading_deg = angle_rad * SGD_RADIANS_TO_DEGREES;
584   if (wind_from_east_fps >= 0)
585     wind_from_heading_deg = 90 - wind_from_heading_deg;
586   else
587     wind_from_heading_deg = 270 - wind_from_heading_deg;
588
589 #if 0
590   // FIXME: Windspeed can become negative with these formulas.
591   //        which can cause problems for animations that rely
592   //        on the windspeed property.
593   if (angle_rad == 0)
594     wind_speed_kt = fabs(wind_from_east_fps
595                          * SG_METER_TO_NM * SG_FEET_TO_METER * 3600);
596   else
597     wind_speed_kt = (wind_from_north_fps / sin(angle_rad))
598       * SG_METER_TO_NM * SG_FEET_TO_METER * 3600;
599 #else
600   wind_speed_kt = sqrt(wind_from_north_fps * wind_from_north_fps +
601                        wind_from_east_fps * wind_from_east_fps) 
602                   * SG_METER_TO_NM * SG_FEET_TO_METER * 3600;
603 #endif
604 }
605
606 void
607 FGEnvironment::_recalc_ne ()
608 {
609   double speed_fps =
610     wind_speed_kt * SG_NM_TO_METER * SG_METER_TO_FEET * (1.0/3600);
611
612   wind_from_north_fps = speed_fps *
613     cos(wind_from_heading_deg * SGD_DEGREES_TO_RADIANS);
614   wind_from_east_fps = speed_fps *
615     sin(wind_from_heading_deg * SGD_DEGREES_TO_RADIANS);
616 }
617
618 void
619 FGEnvironment::_recalc_updraft ()
620 {
621   wind_from_down_fps = thermal_lift_fps + ridge_lift_fps ;
622 }
623
624 // Intended to help with the interpretation of METAR data,
625 // not for random in-flight outside-air temperatures.
626 void
627 FGEnvironment::_recalc_sl_temperature ()
628 {
629
630 #if 0
631   {
632     SG_LOG(SG_GENERAL, SG_DEBUG, "recalc_sl_temperature: using "
633       << temperature_degc << " @ " << elevation_ft << " :: " << this);
634   }
635 #endif
636
637   if (elevation_ft >= ISA_def[1].height) {
638     SG_LOG(SG_GENERAL, SG_ALERT, "recalc_sl_temperature: "
639         << "valid only in troposphere, not " << elevation_ft);
640     return;
641   }
642
643 // Clamp: temperature of the stratosphere, in degrees C:
644   double t_strato = ISA_def[1].temp - atmodel::freezing;
645   if (temperature_degc < t_strato) temperature_sea_level_degc = t_strato;
646   else temperature_sea_level_degc = 
647       temperature_degc + elevation_ft * atmodel::foot * ISA_def[0].lapse;
648
649 // Alternative implemenation:
650 //  else temperature_sea_level_inhg = T_layer(0., elevation_ft * foot,
651 //      pressure_inhg * inHg, temperature_degc + freezing, ISA_def[0].lapse) - freezing;
652 }
653
654 void
655 FGEnvironment::_recalc_sl_dewpoint ()
656 {
657                                 // 0.2degC/1000ft
658                                 // FIXME: this will work only for low
659                                 // elevations
660   dewpoint_sea_level_degc = dewpoint_degc + (elevation_ft * .0002);
661   if (dewpoint_sea_level_degc > temperature_sea_level_degc)
662     dewpoint_sea_level_degc = temperature_sea_level_degc;
663 }
664
665 void
666 FGEnvironment::_recalc_alt_dewpoint ()
667 {
668                                 // 0.2degC/1000ft
669                                 // FIXME: this will work only for low
670                                 // elevations
671   dewpoint_degc = dewpoint_sea_level_degc + (elevation_ft * .0002);
672   if (dewpoint_degc > temperature_degc)
673     dewpoint_degc = temperature_degc;
674 }
675
676 void
677 FGEnvironment::_recalc_sl_pressure ()
678 {
679   using namespace atmodel;
680 #if 0
681   {
682     SG_LOG(SG_GENERAL, SG_ALERT, "recalc_sl_pressure: using "
683       << pressure_inhg << " and "
684       << temperature_degc << " @ " << elevation_ft << " :: " << this);
685   }
686 #endif
687   pressure_sea_level_inhg = P_layer(0., elevation_ft * foot,
688       pressure_inhg * inHg, temperature_degc + freezing, ISA_def[0].lapse) / inHg;
689 }
690
691 // This gets called at frame rate, to account for the aircraft's
692 // changing altitude. 
693 // Called by set_elevation_ft() which is called by FGEnvironmentMgr::update
694
695 void
696 FGEnvironment::_recalc_alt_pt ()
697 {
698   using namespace atmodel;
699 #if 0
700   {
701     static int count(0);
702     if (++count % 1000 == 0) {
703       SG_LOG(SG_GENERAL, SG_ALERT, 
704            "recalc_alt_pt for: " << elevation_ft
705         << "  using "  << pressure_sea_level_inhg 
706         << "  and "  << temperature_sea_level_degc
707         << " :: " << this
708         << "  # " << count);
709     }
710   }
711 #endif
712   double press, temp;
713   boost::tie(press, temp) = PT_vs_hpt(elevation_ft * foot, 
714         pressure_sea_level_inhg * inHg, temperature_sea_level_degc + freezing);
715   temperature_degc = temp - freezing;
716   pressure_inhg = press / inHg;
717 }
718
719 void
720 FGEnvironment::_recalc_density ()
721 {
722   double pressure_psf = pressure_inhg * 70.7487;
723   
724   // adjust for humidity
725   // calculations taken from USA Today (oops!) at
726   // http://www.usatoday.com/weather/basics/density-calculations.htm
727   double temperature_degk = temperature_degc + 273.15;
728   double pressure_mb = pressure_inhg * 33.86;
729   double vapor_pressure_mb =
730     6.11 * pow(10.0, 7.5 * dewpoint_degc / (237.7 + dewpoint_degc));
731   double virtual_temperature_degk = temperature_degk / (1 - (vapor_pressure_mb / pressure_mb) * (1.0 - 0.622));
732   double virtual_temperature_degr = virtual_temperature_degk * 1.8;
733
734   density_slugft3 = pressure_psf / (virtual_temperature_degr * 1718);
735   _recalc_density_tropo_avg_kgm3();
736 }
737
738 // This is used to calculate the average density on the path 
739 // of sunlight to the observer for calculating sun-color
740 void
741 FGEnvironment::_recalc_density_tropo_avg_kgm3 ()
742 {
743   double pressure_mb = pressure_inhg * 33.86;
744   double vaporpressure = 6.11 * pow(10.0, ((7.5 * dewpoint_degc) / (237.7 + dewpoint_degc)));
745
746   double virtual_temp = (temperature_degc + 273.15) / (1 - 0.379 * (vaporpressure/pressure_mb));
747
748   double density_half = (100 * pressure_mb * exp(-altitude_half_to_sun_m / 8000))
749       / (287.05 * virtual_temp);
750   double density_tropo = (100 * pressure_mb * exp((-1 * altitude_tropo_top_m) / 8000))
751       / ( 287.05 * virtual_temp);
752
753   density_tropo_avg_kgm3 = ((density_slugft3 * 515.379) + density_half + density_tropo) / 3;
754 }
755
756 void
757 FGEnvironment::_recalc_relative_humidity ()
758 {
759 /*
760   double vaporpressure = 6.11 * pow(10.0, ((7.5 * dewpoint_degc) / ( 237.7 + dewpoint_degc)));
761   double sat_vaporpressure = 6.11 * pow(10.0, ((7.5 * temperature_degc)
762       / ( 237.7 + temperature_degc)) );
763   relative_humidity = 100 * vaporpressure / sat_vaporpressure ;
764
765   with a little algebra, this gets the same result and spares two multiplications and one pow()
766 */
767   double a = (7.5 * dewpoint_degc)    / ( 237.7 + dewpoint_degc);
768   double b = (7.5 * temperature_degc) / ( 237.7 + temperature_degc);
769   relative_humidity = 100 * pow(10.0,a-b); 
770 }
771
772 bool
773 FGEnvironment::set_live_update( bool _live_update )
774 {
775   bool b = live_update;
776   live_update = _live_update;
777   return b;
778 }
779
780
781 ////////////////////////////////////////////////////////////////////////
782 // Functions.
783 ////////////////////////////////////////////////////////////////////////
784
785 static inline double
786 do_interp (double a, double b, double fraction)
787 {
788     double retval = (a + ((b - a) * fraction));
789     return retval;
790 }
791
792 static inline double
793 do_interp_deg (double a, double b, double fraction)
794 {
795     a = fmod(a, 360);
796     b = fmod(b, 360);
797     if (fabs(b-a) > 180) {
798         if (a < b)
799             a += 360;
800         else
801             b += 360;
802     }
803     return fmod(do_interp(a, b, fraction), 360);
804 }
805
806 void
807 interpolate (const FGEnvironment * env1, const FGEnvironment * env2,
808              double fraction, FGEnvironment * result)
809 {
810     // don't calculate each internal property every time we set a single value
811     // we trigger that at the end of the interpolation process
812     bool live_update = result->set_live_update( false );
813
814     result->set_visibility_m
815         (do_interp(env1->get_visibility_m(),
816                    env2->get_visibility_m(),
817                    fraction));
818
819     result->set_temperature_sea_level_degc
820         (do_interp(env1->get_temperature_sea_level_degc(),
821                    env2->get_temperature_sea_level_degc(),
822                    fraction));
823
824     result->set_dewpoint_sea_level_degc
825         (do_interp(env1->get_dewpoint_sea_level_degc(),
826                    env2->get_dewpoint_sea_level_degc(),
827                    fraction));
828
829     result->set_pressure_sea_level_inhg
830         (do_interp(env1->get_pressure_sea_level_inhg(),
831                    env2->get_pressure_sea_level_inhg(),
832                    fraction));
833
834     result->set_wind_from_heading_deg
835         (do_interp_deg(env1->get_wind_from_heading_deg(),
836                        env2->get_wind_from_heading_deg(),
837                        fraction));
838
839     result->set_wind_speed_kt
840         (do_interp(env1->get_wind_speed_kt(),
841                    env2->get_wind_speed_kt(),
842                    fraction));
843
844     result->set_elevation_ft
845         (do_interp(env1->get_elevation_ft(),
846                    env2->get_elevation_ft(),
847                    fraction));
848
849     result->set_turbulence_magnitude_norm
850         (do_interp(env1->get_turbulence_magnitude_norm(),
851                    env2->get_turbulence_magnitude_norm(),
852                    fraction));
853
854     result->set_turbulence_rate_hz
855         (do_interp(env1->get_turbulence_rate_hz(),
856                    env2->get_turbulence_rate_hz(),
857                    fraction));
858
859     // calculate derived properties here to avoid duplicate expensive computations
860     result->_recalc_ne();
861     result->_recalc_alt_pt();
862     result->_recalc_alt_dewpoint();
863     result->_recalc_density();
864     result->_recalc_relative_humidity();
865
866     result->set_live_update(live_update);
867 }
868
869 // end of environment.cxx