]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/models/FGAtmosphere.cpp
Merge branch 'ehofman/sound'
[flightgear.git] / src / FDM / JSBSim / models / FGAtmosphere.cpp
1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2
3  Module:       FGAtmosphere.cpp
4  Author:       Jon Berndt
5                Implementation of 1959 Standard Atmosphere added by Tony Peden
6  Date started: 11/24/98
7  Purpose:      Models the atmosphere
8  Called by:    FGSimExec
9
10  ------------- Copyright (C) 1999  Jon S. Berndt (jon@jsbsim.org) -------------
11
12  This program is free software; you can redistribute it and/or modify it under
13  the terms of the GNU Lesser General Public License as published by the Free Software
14  Foundation; either version 2 of the License, or (at your option) any later
15  version.
16
17  This program is distributed in the hope that it will be useful, but WITHOUT
18  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19  FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
20  details.
21
22  You should have received a copy of the GNU Lesser General Public License along with
23  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
24  Place - Suite 330, Boston, MA  02111-1307, USA.
25
26  Further information about the GNU Lesser General Public License can also be found on
27  the world wide web at http://www.gnu.org.
28
29 FUNCTIONAL DESCRIPTION
30 --------------------------------------------------------------------------------
31 Models the atmosphere. The equation used below was determined by a third order
32 curve fit using Excel. The data is from the ICAO atmosphere model.
33
34 HISTORY
35 --------------------------------------------------------------------------------
36 11/24/98   JSB   Created
37 07/23/99   TP    Added implementation of 1959 Standard Atmosphere
38                  Moved calculation of Mach number to FGPropagate
39                  Later updated to '76 model
40 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
41 COMMENTS, REFERENCES,  and NOTES
42 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
43 [1]   Anderson, John D. "Introduction to Flight, Third Edition", McGraw-Hill,
44       1989, ISBN 0-07-001641-0
45
46 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
47 INCLUDES
48 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
49
50 #include "FGAtmosphere.h"
51 #include "FGState.h"
52 #include "FGFDMExec.h"
53 #include "FGAircraft.h"
54 #include "FGPropagate.h"
55 #include "FGInertial.h"
56 #include "input_output/FGPropertyManager.h"
57 #include <iostream>
58 #include <cstdlib>
59
60 using namespace std;
61
62 namespace JSBSim {
63
64 static const char *IdSrc = "$Id$";
65 static const char *IdHdr = ID_ATMOSPHERE;
66
67 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
68 CLASS IMPLEMENTATION
69 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
70
71 FGAtmosphere::FGAtmosphere(FGFDMExec* fdmex) : FGModel(fdmex)
72 {
73   Name = "FGAtmosphere";
74   lastIndex = 0;
75   h = 0.0;
76   psiw = 0.0;
77   htab[0]=0;
78   htab[1]= 36089.0;
79   htab[2]= 65617.0;
80   htab[3]=104987.0;
81   htab[4]=154199.0;
82   htab[5]=167322.0;
83   htab[6]=232940.0;
84   htab[7]=278385.0; //ft.
85
86   MagnitudedAccelDt = MagnitudeAccel = Magnitude = 0.0;
87 //  SetTurbType( ttCulp );
88   SetTurbType( ttNone );
89   TurbGain = 1.0;
90   TurbRate = 10.0;
91   Rhythmicity = 0.1;
92   spike = target_time = strength = 0.0;
93   wind_from_clockwise = 0.0;
94   SutherlandConstant = 198.72; // deg Rankine
95   Beta = 2.269690E-08; // slug/(sec ft R^0.5)
96
97   T_dev_sl = T_dev = delta_T = 0.0;
98   StandardTempOnly = false;
99   first_pass = true;
100   vGustNED.InitMatrix();
101   vTurbulenceNED.InitMatrix();
102
103   bind();
104   Debug(0);
105 }
106
107 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
108
109 FGAtmosphere::~FGAtmosphere()
110 {
111   Debug(1);
112 }
113
114 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
115
116 bool FGAtmosphere::InitModel(void)
117 {
118   if (!FGModel::InitModel()) return false;
119
120   UseInternal();  // this is the default
121
122   Calculate(h);
123   StdSLtemperature = SLtemperature = 518.67;
124   StdSLpressure    = SLpressure = 2116.22;
125   StdSLdensity     = SLdensity = 0.00237767;
126   StdSLsoundspeed  = SLsoundspeed = sqrt(SHRatio*Reng*StdSLtemperature);
127   rSLtemperature = 1.0/StdSLtemperature;
128   rSLpressure    = 1.0/StdSLpressure;
129   rSLdensity     = 1.0/StdSLdensity;
130   rSLsoundspeed  = 1.0/StdSLsoundspeed;
131
132   return true;
133 }
134
135 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
136
137 bool FGAtmosphere::Run(void)
138 {
139   if (FGModel::Run()) return true;
140   if (FDMExec->Holding()) return false;
141
142   RunPreFunctions();
143
144   T_dev = 0.0;
145   h = Propagate->GetAltitudeASL();
146
147   if (!useExternal) {
148     Calculate(h);
149     CalculateDerived();
150   } else {
151     CalculateDerived();
152   }
153
154   RunPostFunctions();
155
156   Debug(2);
157   return false;
158 }
159
160 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
161 //
162 // See reference 1
163
164 void FGAtmosphere::Calculate(double altitude)
165 {
166   double slope, reftemp, refpress;
167   int i = lastIndex;
168
169   if (altitude < htab[lastIndex]) {
170     if (altitude <= 0) {
171       i = 0;
172       altitude=0;
173     } else {
174        i = lastIndex-1;
175        while (htab[i] > altitude) i--;
176     }
177   } else if (altitude > htab[lastIndex+1]) {
178     if (altitude >= htab[7]) {
179       i = 7;
180       altitude = htab[7];
181     } else {
182       i = lastIndex+1;
183       while (htab[i+1] < altitude) i++;
184     }
185   }
186
187   switch(i) {
188   case 0: // Sea level
189     slope     = -0.00356616; // R/ft.
190     reftemp   = 518.67;   // in degrees Rankine, 288.15 Kelvin
191     refpress  = 2116.22;    // psf
192     //refdens   = 0.00237767;  // slugs/cubic ft.
193     break;
194   case 1:     // 36089 ft. or 11 km
195     slope     = 0;
196     reftemp   = 389.97; // in degrees Rankine, 216.65 Kelvin
197     refpress  = 472.763;
198     //refdens   = 0.000706032;
199     break;
200   case 2:     // 65616 ft. or 20 km
201     slope     = 0.00054864;
202     reftemp   = 389.97; // in degrees Rankine, 216.65 Kelvin
203     refpress  = 114.636;
204     //refdens   = 0.000171306;
205     break;
206   case 3:     // 104986 ft. or 32 km
207     slope     = 0.001536192;
208     reftemp   = 411.57; // in degrees Rankine, 228.65 Kelvin
209     refpress  = 18.128;
210     //refdens   = 1.18422e-05;
211     break;
212   case 4:     // 154199 ft. 47 km
213     slope     = 0;
214     reftemp   = 487.17; // in degrees Rankine, 270.65 Kelvin
215     refpress  = 2.316;
216     //refdens   = 4.00585e-7;
217     break;
218   case 5:     // 167322 ft. or 51 km
219     slope     = -0.001536192;
220     reftemp   = 487.17; // in degrees Rankine, 270.65 Kelvin
221     refpress  = 1.398;
222     //refdens   = 8.17102e-7;
223     break;
224   case 6:     // 232940 ft. or 71 km
225     slope     = -0.00109728;
226     reftemp   = 386.368; // in degrees Rankine, 214.649 Kelvin
227     refpress  = 0.0826;
228     //refdens   = 8.77702e-9;
229     break;
230   case 7:     // 278385 ft. or 84.8520 km
231     slope     = 0;
232     reftemp   = 336.5; // in degrees Rankine, 186.94 Kelvin
233     refpress  = 0.00831;
234     //refdens   = 2.19541e-10;
235     break;
236   default:     // sea level
237     slope     = -0.00356616; // R/ft.
238     reftemp   = 518.67;   // in degrees Rankine, 288.15 Kelvin
239     refpress  = 2116.22;    // psf
240     //refdens   = 0.00237767;  // slugs/cubic ft.
241     break;
242
243   }
244
245   // If delta_T is set, then that is our temperature deviation at any altitude.
246   // If not, then we'll estimate a deviation based on the sea level deviation (if set).
247
248   if(!StandardTempOnly) {
249     T_dev = 0.0;
250     if (delta_T != 0.0) {
251       T_dev = delta_T;
252     } else {
253       if ((altitude < 36089.239) && (T_dev_sl != 0.0)) {
254         T_dev = T_dev_sl * ( 1.0 - (altitude/36089.239));
255       }
256     }
257     reftemp+=T_dev;
258   }
259
260   if (slope == 0) {
261     intTemperature = reftemp;
262     intPressure = refpress*exp(-Inertial->SLgravity()/(reftemp*Reng)*(altitude-htab[i]));
263     intDensity = intPressure/(Reng*intTemperature);
264   } else {
265     intTemperature = reftemp+slope*(altitude-htab[i]);
266     intPressure = refpress*pow(intTemperature/reftemp,-Inertial->SLgravity()/(slope*Reng));
267     intDensity = intPressure/(Reng*intTemperature);
268   }
269   
270   lastIndex=i;
271 }
272
273 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
274 // Calculate parameters derived from T, P and rho
275 // Sum gust and turbulence values in NED frame into the wind vector.
276
277 void FGAtmosphere::CalculateDerived(void)
278 {
279   T_dev = (*temperature) - GetTemperature(h);
280   density_altitude = h + T_dev * 66.7;
281
282   if (turbType != ttNone) Turbulence();
283
284   vTotalWindNED = vWindNED + vGustNED + vTurbulenceNED;
285
286    // psiw (Wind heading) is the direction the wind is blowing towards
287   if (vWindNED(eX) != 0.0) psiw = atan2( vWindNED(eY), vWindNED(eX) );
288   if (psiw < 0) psiw += 2*M_PI;
289
290   soundspeed = sqrt(SHRatio*Reng*(*temperature));
291
292   intViscosity = Beta * pow(intTemperature, 1.5) / (SutherlandConstant + intTemperature);
293   intKinematicViscosity = intViscosity / intDensity;
294 }
295
296
297 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
298 // Get the standard atmospheric properties at a specified altitude
299
300 void FGAtmosphere::GetStdAtmosphere(double altitude) {
301   StandardTempOnly = true;
302   Calculate(altitude);
303   StandardTempOnly = false;
304   atmosphere.Temperature = intTemperature;
305   atmosphere.Pressure = intPressure;
306   atmosphere.Density = intDensity;
307
308   // Reset the internal atmospheric state
309   Calculate(h);
310 }
311
312 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
313 // Get the standard pressure at a specified altitude
314
315 double FGAtmosphere::GetPressure(double altitude) {
316   GetStdAtmosphere(altitude);
317   return atmosphere.Pressure;
318 }
319
320 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
321 // Get the standard temperature at a specified altitude
322
323 double FGAtmosphere::GetTemperature(double altitude) {
324   GetStdAtmosphere(altitude);
325   return atmosphere.Temperature;
326 }
327
328 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
329 // Get the standard density at a specified altitude
330
331 double FGAtmosphere::GetDensity(double altitude) {
332   GetStdAtmosphere(altitude);
333   return atmosphere.Density;
334 }
335
336
337 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
338 // square a value, but preserve the original sign
339
340 static inline double square_signed (double value)
341 {
342     if (value < 0)
343         return value * value * -1;
344     else
345         return value * value;
346 }
347
348 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
349 //
350 // psi is the angle that the wind is blowing *towards*
351
352 void FGAtmosphere::SetWindspeed(double speed)
353 {
354   if (vWindNED.Magnitude() == 0.0) {
355     psiw = 0.0;
356     vWindNED(eNorth) = speed;
357   } else {
358     vWindNED(eNorth) = speed * cos(psiw);
359     vWindNED(eEast) = speed * sin(psiw);
360     vWindNED(eDown) = 0.0;
361   }
362 }
363
364 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
365
366 double FGAtmosphere::GetWindspeed(void) const
367 {
368   return vWindNED.Magnitude();
369 }
370
371 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
372 //
373 // psi is the angle that the wind is blowing *towards*
374
375 void FGAtmosphere::SetWindPsi(double dir)
376 {
377   double mag = GetWindspeed();
378   psiw = dir;
379   SetWindspeed(mag);  
380 }
381
382 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
383
384 void FGAtmosphere::Turbulence(void)
385 {
386   double DeltaT = rate*State->Getdt();
387
388   switch (turbType) {
389   case ttStandard: {
390     // TurbGain = TurbGain * TurbGain * 100.0; // what is this!?
391
392     vDirectiondAccelDt(eX) = 1 - 2.0*(double(rand())/double(RAND_MAX));
393     vDirectiondAccelDt(eY) = 1 - 2.0*(double(rand())/double(RAND_MAX));
394     vDirectiondAccelDt(eZ) = 1 - 2.0*(double(rand())/double(RAND_MAX));
395
396     MagnitudedAccelDt = 1 - 2.0*(double(rand())/double(RAND_MAX)) - Magnitude;
397                                 // Scale the magnitude so that it moves
398                                 // away from the peaks
399     MagnitudedAccelDt = ((MagnitudedAccelDt - Magnitude) /
400                          (1 + fabs(Magnitude)));
401     MagnitudeAccel    += MagnitudedAccelDt*TurbRate*DeltaT;
402     Magnitude         += MagnitudeAccel*DeltaT;
403     Magnitude          = fabs(Magnitude);
404
405     vDirectiondAccelDt.Normalize();
406
407                                 // deemphasise non-vertical forces
408     vDirectiondAccelDt(eX) = square_signed(vDirectiondAccelDt(eX));
409     vDirectiondAccelDt(eY) = square_signed(vDirectiondAccelDt(eY));
410
411     vDirectionAccel += vDirectiondAccelDt*TurbRate*DeltaT;
412     vDirectionAccel.Normalize();
413     vDirection      += vDirectionAccel*DeltaT;
414
415     vDirection.Normalize();
416
417                                 // Diminish turbulence within three wingspans
418                                 // of the ground
419     vTurbulenceNED = TurbGain * Magnitude * vDirection;
420     double HOverBMAC = Auxiliary->GetHOverBMAC();
421     if (HOverBMAC < 3.0)
422         vTurbulenceNED *= (HOverBMAC / 3.0) * (HOverBMAC / 3.0);
423
424     // I don't believe these next two statements calculate the proper gradient over
425     // the aircraft body. One reason is because this has no relationship with the
426     // orientation or velocity of the aircraft, which it must have. What is vTurbulenceGrad
427     // supposed to represent? And the direction and magnitude of the turbulence can change,
428     // so both accelerations need to be accounted for, no?
429
430     // Need to determine the turbulence change in body axes between two time points.
431
432     vTurbulenceGrad = TurbGain*MagnitudeAccel * vDirection;
433     vBodyTurbGrad = Propagate->GetTl2b()*vTurbulenceGrad;
434
435     if (Aircraft->GetWingSpan() > 0) {
436       vTurbPQR(eP) = vBodyTurbGrad(eY)/Aircraft->GetWingSpan();
437     } else {
438       vTurbPQR(eP) = vBodyTurbGrad(eY)/30.0;
439     }
440 //     if (Aircraft->GetHTailArm() != 0.0)
441 //       vTurbPQR(eQ) = vBodyTurbGrad(eZ)/Aircraft->GetHTailArm();
442 //     else
443 //       vTurbPQR(eQ) = vBodyTurbGrad(eZ)/10.0;
444
445     if (Aircraft->GetVTailArm() > 0)
446       vTurbPQR(eR) = vBodyTurbGrad(eX)/Aircraft->GetVTailArm();
447     else
448       vTurbPQR(eR) = vBodyTurbGrad(eX)/10.0;
449
450                                 // Clear the horizontal forces
451                                 // actually felt by the plane, now
452                                 // that we've used them to calculate
453                                 // moments.
454                                 // Why? (JSB)
455 //    vTurbulenceNED(eX) = 0.0;
456 //    vTurbulenceNED(eY) = 0.0;
457
458     break;
459   }
460   case ttBerndt: { // This is very experimental and incomplete at the moment.
461
462     vDirectiondAccelDt(eX) = GaussianRandomNumber();
463     vDirectiondAccelDt(eY) = GaussianRandomNumber();
464     vDirectiondAccelDt(eZ) = GaussianRandomNumber();
465 /*
466     MagnitudedAccelDt = GaussianRandomNumber();
467     MagnitudeAccel    += MagnitudedAccelDt * DeltaT;
468     Magnitude         += MagnitudeAccel * DeltaT;
469 */
470     Magnitude         += GaussianRandomNumber() * DeltaT;
471
472     vDirectiondAccelDt.Normalize();
473     vDirectionAccel += TurbRate * vDirectiondAccelDt * DeltaT;
474     vDirectionAccel.Normalize();
475     vDirection      += vDirectionAccel*DeltaT;
476
477     // Diminish z-vector within two wingspans of the ground
478     double HOverBMAC = Auxiliary->GetHOverBMAC();
479     if (HOverBMAC < 2.0) vDirection(eZ) *= HOverBMAC / 2.0;
480
481     vDirection.Normalize();
482
483     vTurbulenceNED = TurbGain*Magnitude * vDirection;
484     vTurbulenceGrad = TurbGain*MagnitudeAccel * vDirection;
485
486     vBodyTurbGrad = Propagate->GetTl2b() * vTurbulenceGrad;
487     vTurbPQR(eP) = vBodyTurbGrad(eY) / Aircraft->GetWingSpan();
488     if (Aircraft->GetHTailArm() > 0)
489       vTurbPQR(eQ) = vBodyTurbGrad(eZ) / Aircraft->GetHTailArm();
490     else
491       vTurbPQR(eQ) = vBodyTurbGrad(eZ) / 10.0;
492
493     if (Aircraft->GetVTailArm() > 0)
494       vTurbPQR(eR) = vBodyTurbGrad(eX) / Aircraft->GetVTailArm();
495     else
496       vTurbPQR(eR) = vBodyTurbGrad(eX)/10.0;
497
498     break;
499   }
500   case ttCulp: { 
501
502     vTurbPQR(eP) = wind_from_clockwise;
503     if (TurbGain == 0.0) return;
504   
505     // keep the inputs within allowable limts for this model
506     if (TurbGain < 0.0) TurbGain = 0.0;
507     if (TurbGain > 1.0) TurbGain = 1.0;
508     if (TurbRate < 0.0) TurbRate = 0.0;
509     if (TurbRate > 30.0) TurbRate = 30.0;
510     if (Rhythmicity < 0.0) Rhythmicity = 0.0;
511     if (Rhythmicity > 1.0) Rhythmicity = 1.0;
512
513     // generate a sine wave corresponding to turbulence rate in hertz
514     double time = FDMExec->GetSimTime();
515     double sinewave = sin( time * TurbRate * 6.283185307 );
516
517     double random = 0.0;
518     if (target_time == 0.0) {
519       strength = random = 1 - 2.0*(double(rand())/double(RAND_MAX));
520       target_time = time + 0.71 + (random * 0.5);
521     }
522     if (time > target_time) {
523       spike = 1.0;
524       target_time = 0.0;
525     }    
526
527     // max vertical wind speed in fps, corresponds to TurbGain = 1.0
528     double max_vs = 40;
529
530     vTurbulenceNED(1) = vTurbulenceNED(2) = vTurbulenceNED(3) = 0.0;
531     double delta = strength * max_vs * TurbGain * (1-Rhythmicity) * spike;
532
533     // Vertical component of turbulence.
534     vTurbulenceNED(3) = sinewave * max_vs * TurbGain * Rhythmicity;
535     vTurbulenceNED(3)+= delta;
536     double HOverBMAC = Auxiliary->GetHOverBMAC();
537     if (HOverBMAC < 3.0)
538         vTurbulenceNED(3) *= HOverBMAC * 0.3333;
539  
540     // Yaw component of turbulence.
541     vTurbulenceNED(1) = sin( delta * 3.0 );
542     vTurbulenceNED(2) = cos( delta * 3.0 );
543
544     // Roll component of turbulence. Clockwise vortex causes left roll.
545     vTurbPQR(eP) += delta * 0.04;
546
547     spike = spike * 0.9;
548     break;
549   }
550   default:
551     break;
552   }
553 }
554
555 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
556
557 void FGAtmosphere::UseExternal(void)
558 {
559   temperature=&exTemperature;
560   pressure=&exPressure;
561   density=&exDensity;
562   useExternal=true;
563 }
564
565 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
566
567 void FGAtmosphere::UseInternal(void)
568 {
569   temperature=&intTemperature;
570   pressure=&intPressure;
571   density=&intDensity;
572   useExternal=false;
573 }
574
575 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
576
577 void FGAtmosphere::bind(void)
578 {
579   typedef double (FGAtmosphere::*PMF)(int) const;
580   typedef double (FGAtmosphere::*PMFv)(void) const;
581   typedef int (FGAtmosphere::*PMFt)(void) const;
582   typedef void   (FGAtmosphere::*PMFd)(int,double);
583   typedef void   (FGAtmosphere::*PMFi)(int);
584   PropertyManager->Tie("atmosphere/T-R", this, (PMFv)&FGAtmosphere::GetTemperature);
585   PropertyManager->Tie("atmosphere/rho-slugs_ft3", this, (PMFv)&FGAtmosphere::GetDensity);
586   PropertyManager->Tie("atmosphere/P-psf", this, (PMFv)&FGAtmosphere::GetPressure);
587   PropertyManager->Tie("atmosphere/a-fps", this, &FGAtmosphere::GetSoundSpeed);
588   PropertyManager->Tie("atmosphere/T-sl-R", this, &FGAtmosphere::GetTemperatureSL);
589   PropertyManager->Tie("atmosphere/rho-sl-slugs_ft3", this, &FGAtmosphere::GetDensitySL);
590   PropertyManager->Tie("atmosphere/P-sl-psf", this, &FGAtmosphere::GetPressureSL);
591   PropertyManager->Tie("atmosphere/a-sl-fps", this, &FGAtmosphere::GetSoundSpeedSL);
592   PropertyManager->Tie("atmosphere/theta", this, &FGAtmosphere::GetTemperatureRatio);
593   PropertyManager->Tie("atmosphere/sigma", this, &FGAtmosphere::GetDensityRatio);
594   PropertyManager->Tie("atmosphere/delta", this, &FGAtmosphere::GetPressureRatio);
595   PropertyManager->Tie("atmosphere/a-ratio", this, &FGAtmosphere::GetSoundSpeedRatio);
596   PropertyManager->Tie("atmosphere/psiw-rad", this, &FGAtmosphere::GetWindPsi, &FGAtmosphere::SetWindPsi);
597   PropertyManager->Tie("atmosphere/delta-T", this, &FGAtmosphere::GetDeltaT, &FGAtmosphere::SetDeltaT);
598   PropertyManager->Tie("atmosphere/T-sl-dev-F", this, &FGAtmosphere::GetSLTempDev, &FGAtmosphere::SetSLTempDev);
599   PropertyManager->Tie("atmosphere/density-altitude", this, &FGAtmosphere::GetDensityAltitude);
600
601   PropertyManager->Tie("atmosphere/wind-north-fps", this, eNorth, (PMF)&FGAtmosphere::GetWindNED,
602                                                           (PMFd)&FGAtmosphere::SetWindNED);
603   PropertyManager->Tie("atmosphere/wind-east-fps",  this, eEast, (PMF)&FGAtmosphere::GetWindNED,
604                                                           (PMFd)&FGAtmosphere::SetWindNED);
605   PropertyManager->Tie("atmosphere/wind-down-fps",  this, eDown, (PMF)&FGAtmosphere::GetWindNED,
606                                                           (PMFd)&FGAtmosphere::SetWindNED);
607   PropertyManager->Tie("atmosphere/wind-mag-fps", this, &FGAtmosphere::GetWindspeed,
608                                                         &FGAtmosphere::SetWindspeed);
609   PropertyManager->Tie("atmosphere/total-wind-north-fps", this, eNorth, (PMF)&FGAtmosphere::GetTotalWindNED);
610   PropertyManager->Tie("atmosphere/total-wind-east-fps",  this, eEast,  (PMF)&FGAtmosphere::GetTotalWindNED);
611   PropertyManager->Tie("atmosphere/total-wind-down-fps",  this, eDown,  (PMF)&FGAtmosphere::GetTotalWindNED);
612
613   PropertyManager->Tie("atmosphere/gust-north-fps", this, eNorth, (PMF)&FGAtmosphere::GetGustNED,
614                                                           (PMFd)&FGAtmosphere::SetGustNED);
615   PropertyManager->Tie("atmosphere/gust-east-fps",  this, eEast, (PMF)&FGAtmosphere::GetGustNED,
616                                                           (PMFd)&FGAtmosphere::SetGustNED);
617   PropertyManager->Tie("atmosphere/gust-down-fps",  this, eDown, (PMF)&FGAtmosphere::GetGustNED,
618                                                           (PMFd)&FGAtmosphere::SetGustNED);
619
620   PropertyManager->Tie("atmosphere/turb-north-fps", this, eNorth, (PMF)&FGAtmosphere::GetTurbNED,
621                                                           (PMFd)&FGAtmosphere::SetTurbNED);
622   PropertyManager->Tie("atmosphere/turb-east-fps",  this, eEast, (PMF)&FGAtmosphere::GetTurbNED,
623                                                           (PMFd)&FGAtmosphere::SetTurbNED);
624   PropertyManager->Tie("atmosphere/turb-down-fps",  this, eDown, (PMF)&FGAtmosphere::GetTurbNED,
625                                                           (PMFd)&FGAtmosphere::SetTurbNED);
626
627   PropertyManager->Tie("atmosphere/p-turb-rad_sec", this,1, (PMF)&FGAtmosphere::GetTurbPQR);
628   PropertyManager->Tie("atmosphere/q-turb-rad_sec", this,2, (PMF)&FGAtmosphere::GetTurbPQR);
629   PropertyManager->Tie("atmosphere/r-turb-rad_sec", this,3, (PMF)&FGAtmosphere::GetTurbPQR);
630   PropertyManager->Tie("atmosphere/turb-type", this, (PMFt)&FGAtmosphere::GetTurbType, (PMFi)&FGAtmosphere::SetTurbType);
631   PropertyManager->Tie("atmosphere/turb-rate", this, &FGAtmosphere::GetTurbRate, &FGAtmosphere::SetTurbRate);
632   PropertyManager->Tie("atmosphere/turb-gain", this, &FGAtmosphere::GetTurbGain, &FGAtmosphere::SetTurbGain);
633   PropertyManager->Tie("atmosphere/turb-rhythmicity", this, &FGAtmosphere::GetRhythmicity,
634                                                             &FGAtmosphere::SetRhythmicity);
635 }
636
637 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
638 //    The bitmasked value choices are as follows:
639 //    unset: In this case (the default) JSBSim would only print
640 //       out the normally expected messages, essentially echoing
641 //       the config files as they are read. If the environment
642 //       variable is not set, debug_lvl is set to 1 internally
643 //    0: This requests JSBSim not to output any messages
644 //       whatsoever.
645 //    1: This value explicity requests the normal JSBSim
646 //       startup messages
647 //    2: This value asks for a message to be printed out when
648 //       a class is instantiated
649 //    4: When this value is set, a message is displayed when a
650 //       FGModel object executes its Run() method
651 //    8: When this value is set, various runtime state variables
652 //       are printed out periodically
653 //    16: When set various parameters are sanity checked and
654 //       a message is printed out when they go out of bounds
655
656 void FGAtmosphere::Debug(int from)
657 {
658   if (debug_lvl <= 0) return;
659
660   if (debug_lvl & 1) { // Standard console startup message output
661     if (from == 0) { // Constructor
662     }
663   }
664   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
665     if (from == 0) cout << "Instantiated: FGAtmosphere" << endl;
666     if (from == 1) cout << "Destroyed:    FGAtmosphere" << endl;
667   }
668   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
669   }
670   if (debug_lvl & 8 ) { // Runtime state variables
671   }
672   if (debug_lvl & 16) { // Sanity checking
673   }
674   if (debug_lvl & 128) { // Turbulence
675     if (first_pass && from == 2) {
676       first_pass = false;
677       cout << "vTurbulenceNED(X), vTurbulenceNED(Y), vTurbulenceNED(Z), "
678            << "vTurbulenceGrad(X), vTurbulenceGrad(Y), vTurbulenceGrad(Z), "
679            << "vDirection(X), vDirection(Y), vDirection(Z), "
680            << "Magnitude, "
681            << "vTurbPQR(P), vTurbPQR(Q), vTurbPQR(R), " << endl;
682     } 
683     if (from == 2) {
684       cout << vTurbulenceNED << ", " << vTurbulenceGrad << ", " << vDirection << ", " << Magnitude << ", " << vTurbPQR << endl;
685     }
686   }
687   if (debug_lvl & 64) {
688     if (from == 0) { // Constructor
689       cout << IdSrc << endl;
690       cout << IdHdr << endl;
691     }
692   }
693 }
694
695 } // namespace JSBSim