]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/models/FGAtmosphere.cpp
Merge branch 'next' of http://git.gitorious.org/fg/flightgear into next
[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 "FGAircraft.h"
52 #include "FGPropagate.h"
53 #include "FGInertial.h"
54 #include "FGAuxiliary.h"
55 #include "FGFDMExec.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: FGAtmosphere.cpp,v 1.45 2011/05/20 03:18:36 jberndt Exp $";
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( ttMilspec );
88   TurbGain = 1.0;
89   TurbRate = 10.0;
90   Rhythmicity = 0.1;
91   spike = target_time = strength = 0.0;
92   wind_from_clockwise = 0.0;
93   SutherlandConstant = 198.72; // deg Rankine
94   Beta = 2.269690E-08; // slug/(sec ft R^0.5)
95
96   T_dev_sl = T_dev = delta_T = 0.0;
97   StandardTempOnly = false;
98   first_pass = true;
99   vGustNED.InitMatrix();
100   vTurbulenceNED.InitMatrix();
101
102   // Milspec turbulence model
103   windspeed_at_20ft = 0.;
104   probability_of_exceedence_index = 0;
105   POE_Table = new FGTable(7,12);
106   // this is Figure 7 from p. 49 of MIL-F-8785C
107   // rows: probability of exceedance curve index, cols: altitude in ft
108   *POE_Table
109            << 500.0 << 1750.0 << 3750.0 << 7500.0 << 15000.0 << 25000.0 << 35000.0 << 45000.0 << 55000.0 << 65000.0 << 75000.0 << 80000.0
110     << 1   <<   3.2 <<    2.2 <<    1.5 <<    0.0 <<     0.0 <<     0.0 <<     0.0 <<     0.0 <<     0.0 <<     0.0 <<     0.0 <<     0.0
111     << 2   <<   4.2 <<    3.6 <<    3.3 <<    1.6 <<     0.0 <<     0.0 <<     0.0 <<     0.0 <<     0.0 <<     0.0 <<     0.0 <<     0.0
112     << 3   <<   6.6 <<    6.9 <<    7.4 <<    6.7 <<     4.6 <<     2.7 <<     0.4 <<     0.0 <<     0.0 <<     0.0 <<     0.0 <<     0.0
113     << 4   <<   8.6 <<    9.6 <<   10.6 <<   10.1 <<     8.0 <<     6.6 <<     5.0 <<     4.2 <<     2.7 <<     0.0 <<     0.0 <<     0.0
114     << 5   <<  11.8 <<   13.0 <<   16.0 <<   15.1 <<    11.6 <<     9.7 <<     8.1 <<     8.2 <<     7.9 <<     4.9 <<     3.2 <<     2.1
115     << 6   <<  15.6 <<   17.6 <<   23.0 <<   23.6 <<    22.1 <<    20.0 <<    16.0 <<    15.1 <<    12.1 <<     7.9 <<     6.2 <<     5.1
116     << 7   <<  18.7 <<   21.5 <<   28.4 <<   30.2 <<    30.7 <<    31.0 <<    25.2 <<    23.1 <<    17.5 <<    10.7 <<     8.4 <<     7.2;
117
118   bind();
119   Debug(0);
120 }
121
122 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
123
124 FGAtmosphere::~FGAtmosphere()
125 {
126   delete(POE_Table);
127   Debug(1);
128 }
129
130 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
131
132 bool FGAtmosphere::InitModel(void)
133 {
134   UseInternal();  // this is the default
135
136   Calculate(h);
137   StdSLtemperature = SLtemperature = 518.67;
138   StdSLpressure    = SLpressure = 2116.22;
139   StdSLdensity     = SLdensity = 0.00237767;
140   StdSLsoundspeed  = SLsoundspeed = sqrt(SHRatio*Reng*StdSLtemperature);
141   rSLtemperature = 1.0/StdSLtemperature;
142   rSLpressure    = 1.0/StdSLpressure;
143   rSLdensity     = 1.0/StdSLdensity;
144   rSLsoundspeed  = 1.0/StdSLsoundspeed;
145
146   return true;
147 }
148
149 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
150
151 bool FGAtmosphere::Run(bool Holding)
152 {
153   if (FGModel::Run(Holding)) return true;
154   if (Holding) return false;
155
156   RunPreFunctions();
157
158   T_dev = 0.0;
159   h = FDMExec->GetPropagate()->GetAltitudeASL();
160
161   if (!useExternal) {
162     Calculate(h);
163     CalculateDerived();
164   } else {
165     CalculateDerived();
166   }
167
168   RunPostFunctions();
169
170   Debug(2);
171   return false;
172 }
173
174 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
175 //
176 // See reference 1
177
178 void FGAtmosphere::Calculate(double altitude)
179 {
180   double slope, reftemp, refpress;
181   int i = lastIndex;
182
183   if (altitude < htab[lastIndex]) {
184     if (altitude <= 0) {
185       i = 0;
186       altitude=0;
187     } else {
188        i = lastIndex-1;
189        while (htab[i] > altitude) i--;
190     }
191   } else if (altitude > htab[lastIndex+1]) {
192     if (altitude >= htab[7]) {
193       i = 7;
194       altitude = htab[7];
195     } else {
196       i = lastIndex+1;
197       while (htab[i+1] < altitude) i++;
198     }
199   }
200
201   switch(i) {
202   case 0: // Sea level
203     slope     = -0.00356616; // R/ft.
204     reftemp   = 518.67;   // in degrees Rankine, 288.15 Kelvin
205     refpress  = 2116.22;    // psf
206     //refdens   = 0.00237767;  // slugs/cubic ft.
207     break;
208   case 1:     // 36089 ft. or 11 km
209     slope     = 0;
210     reftemp   = 389.97; // in degrees Rankine, 216.65 Kelvin
211     refpress  = 472.763;
212     //refdens   = 0.000706032;
213     break;
214   case 2:     // 65616 ft. or 20 km
215     slope     = 0.00054864;
216     reftemp   = 389.97; // in degrees Rankine, 216.65 Kelvin
217     refpress  = 114.636;
218     //refdens   = 0.000171306;
219     break;
220   case 3:     // 104986 ft. or 32 km
221     slope     = 0.001536192;
222     reftemp   = 411.57; // in degrees Rankine, 228.65 Kelvin
223     refpress  = 18.128;
224     //refdens   = 1.18422e-05;
225     break;
226   case 4:     // 154199 ft. 47 km
227     slope     = 0;
228     reftemp   = 487.17; // in degrees Rankine, 270.65 Kelvin
229     refpress  = 2.316;
230     //refdens   = 4.00585e-7;
231     break;
232   case 5:     // 167322 ft. or 51 km
233     slope     = -0.001536192;
234     reftemp   = 487.17; // in degrees Rankine, 270.65 Kelvin
235     refpress  = 1.398;
236     //refdens   = 8.17102e-7;
237     break;
238   case 6:     // 232940 ft. or 71 km
239     slope     = -0.00109728;
240     reftemp   = 386.368; // in degrees Rankine, 214.649 Kelvin
241     refpress  = 0.0826;
242     //refdens   = 8.77702e-9;
243     break;
244   case 7:     // 278385 ft. or 84.8520 km
245     slope     = 0;
246     reftemp   = 336.5; // in degrees Rankine, 186.94 Kelvin
247     refpress  = 0.00831;
248     //refdens   = 2.19541e-10;
249     break;
250   default:     // sea level
251     slope     = -0.00356616; // R/ft.
252     reftemp   = 518.67;   // in degrees Rankine, 288.15 Kelvin
253     refpress  = 2116.22;    // psf
254     //refdens   = 0.00237767;  // slugs/cubic ft.
255     break;
256
257   }
258
259   // If delta_T is set, then that is our temperature deviation at any altitude.
260   // If not, then we'll estimate a deviation based on the sea level deviation (if set).
261
262   if(!StandardTempOnly) {
263     T_dev = 0.0;
264     if (delta_T != 0.0) {
265       T_dev = delta_T;
266     } else {
267       if ((altitude < 36089.239) && (T_dev_sl != 0.0)) {
268         T_dev = T_dev_sl * ( 1.0 - (altitude/36089.239));
269       }
270     }
271     reftemp+=T_dev;
272   }
273
274   if (slope == 0) {
275     intTemperature = reftemp;
276     intPressure = refpress*exp(-FDMExec->GetInertial()->SLgravity()/(reftemp*Reng)*(altitude-htab[i]));
277     intDensity = intPressure/(Reng*intTemperature);
278   } else {
279     intTemperature = reftemp+slope*(altitude-htab[i]);
280     intPressure = refpress*pow(intTemperature/reftemp,-FDMExec->GetInertial()->SLgravity()/(slope*Reng));
281     intDensity = intPressure/(Reng*intTemperature);
282   }
283   
284   lastIndex=i;
285 }
286
287 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
288 // Calculate parameters derived from T, P and rho
289 // Sum gust and turbulence values in NED frame into the wind vector.
290
291 void FGAtmosphere::CalculateDerived(void)
292 {
293   T_dev = (*temperature) - GetTemperature(h);
294
295   if (T_dev == 0.0) density_altitude = h;
296   else              density_altitude = 518.67/0.00356616 * (1.0 - pow(GetDensityRatio(),0.235));
297
298   if (turbType != ttNone) Turbulence();
299
300   vTotalWindNED = vWindNED + vGustNED + vTurbulenceNED;
301
302    // psiw (Wind heading) is the direction the wind is blowing towards
303   if (vWindNED(eX) != 0.0) psiw = atan2( vWindNED(eY), vWindNED(eX) );
304   if (psiw < 0) psiw += 2*M_PI;
305
306   soundspeed = sqrt(SHRatio*Reng*(*temperature));
307
308   intViscosity = Beta * pow(intTemperature, 1.5) / (SutherlandConstant + intTemperature);
309   intKinematicViscosity = intViscosity / intDensity;
310 }
311
312
313 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
314 // Get the standard atmospheric properties at a specified altitude
315
316 void FGAtmosphere::GetStdAtmosphere(double altitude) {
317   StandardTempOnly = true;
318   Calculate(altitude);
319   StandardTempOnly = false;
320   atmosphere.Temperature = intTemperature;
321   atmosphere.Pressure = intPressure;
322   atmosphere.Density = intDensity;
323
324   // Reset the internal atmospheric state
325   Calculate(h);
326 }
327
328 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
329 // Get the standard pressure at a specified altitude
330
331 double FGAtmosphere::GetPressure(double altitude) {
332   GetStdAtmosphere(altitude);
333   return atmosphere.Pressure;
334 }
335
336 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
337 // Get the standard temperature at a specified altitude
338
339 double FGAtmosphere::GetTemperature(double altitude) {
340   GetStdAtmosphere(altitude);
341   return atmosphere.Temperature;
342 }
343
344 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
345 // Get the standard density at a specified altitude
346
347 double FGAtmosphere::GetDensity(double altitude) {
348   GetStdAtmosphere(altitude);
349   return atmosphere.Density;
350 }
351
352
353 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
354 // square a value, but preserve the original sign
355
356 static inline double square_signed (double value)
357 {
358     if (value < 0)
359         return value * value * -1;
360     else
361         return value * value;
362 }
363
364 /// simply square a value
365 static inline double sqr(double x) { return x*x; }
366
367 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
368 //
369 // psi is the angle that the wind is blowing *towards*
370
371 void FGAtmosphere::SetWindspeed(double speed)
372 {
373   if (vWindNED.Magnitude() == 0.0) {
374     psiw = 0.0;
375     vWindNED(eNorth) = speed;
376   } else {
377     vWindNED(eNorth) = speed * cos(psiw);
378     vWindNED(eEast) = speed * sin(psiw);
379     vWindNED(eDown) = 0.0;
380   }
381 }
382
383 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
384
385 double FGAtmosphere::GetWindspeed(void) const
386 {
387   return vWindNED.Magnitude();
388 }
389
390 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
391 //
392 // psi is the angle that the wind is blowing *towards*
393
394 void FGAtmosphere::SetWindPsi(double dir)
395 {
396   double mag = GetWindspeed();
397   psiw = dir;
398   SetWindspeed(mag);  
399 }
400
401 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
402
403 void FGAtmosphere::Turbulence(void)
404 {
405   const double DeltaT = rate*FDMExec->GetDeltaT();
406   const double wingspan = FDMExec->GetAircraft()->GetWingSpan();
407   const double HOverBMAC = FDMExec->GetAuxiliary()->GetHOverBMAC();
408   const FGMatrix33& Tl2b = FDMExec->GetPropagate()->GetTl2b();
409   const double HTailArm = FDMExec->GetAircraft()->GetHTailArm();
410   const double VTailArm = FDMExec->GetAircraft()->GetVTailArm();
411
412   switch (turbType) {
413   case ttStandard: {
414     // TurbGain = TurbGain * TurbGain * 100.0; // what is this!?
415
416     vDirectiondAccelDt(eX) = 1 - 2.0*(double(rand())/double(RAND_MAX));
417     vDirectiondAccelDt(eY) = 1 - 2.0*(double(rand())/double(RAND_MAX));
418     vDirectiondAccelDt(eZ) = 1 - 2.0*(double(rand())/double(RAND_MAX));
419
420     MagnitudedAccelDt = 1 - 2.0*(double(rand())/double(RAND_MAX)) - Magnitude;
421                                 // Scale the magnitude so that it moves
422                                 // away from the peaks
423     MagnitudedAccelDt = ((MagnitudedAccelDt - Magnitude) /
424                          (1 + fabs(Magnitude)));
425     MagnitudeAccel    += MagnitudedAccelDt*TurbRate*DeltaT;
426     Magnitude         += MagnitudeAccel*DeltaT;
427     Magnitude          = fabs(Magnitude);
428
429     vDirectiondAccelDt.Normalize();
430
431                                 // deemphasise non-vertical forces
432     vDirectiondAccelDt(eX) = square_signed(vDirectiondAccelDt(eX));
433     vDirectiondAccelDt(eY) = square_signed(vDirectiondAccelDt(eY));
434
435     vDirectionAccel += vDirectiondAccelDt*TurbRate*DeltaT;
436     vDirectionAccel.Normalize();
437     vDirection      += vDirectionAccel*DeltaT;
438
439     vDirection.Normalize();
440
441                                 // Diminish turbulence within three wingspans
442                                 // of the ground
443     vTurbulenceNED = TurbGain * Magnitude * vDirection;
444     if (HOverBMAC < 3.0)
445         vTurbulenceNED *= (HOverBMAC / 3.0) * (HOverBMAC / 3.0);
446
447     // I don't believe these next two statements calculate the proper gradient over
448     // the aircraft body. One reason is because this has no relationship with the
449     // orientation or velocity of the aircraft, which it must have. What is vTurbulenceGrad
450     // supposed to represent? And the direction and magnitude of the turbulence can change,
451     // so both accelerations need to be accounted for, no?
452
453     // Need to determine the turbulence change in body axes between two time points.
454
455     vTurbulenceGrad = TurbGain*MagnitudeAccel * vDirection;
456     vBodyTurbGrad = Tl2b*vTurbulenceGrad;
457
458     if (wingspan > 0) {
459       vTurbPQR(eP) = vBodyTurbGrad(eY)/wingspan;
460     } else {
461       vTurbPQR(eP) = vBodyTurbGrad(eY)/30.0;
462     }
463 //     if (HTailArm != 0.0)
464 //       vTurbPQR(eQ) = vBodyTurbGrad(eZ)/HTailArm;
465 //     else
466 //       vTurbPQR(eQ) = vBodyTurbGrad(eZ)/10.0;
467
468     if (VTailArm > 0)
469       vTurbPQR(eR) = vBodyTurbGrad(eX)/VTailArm;
470     else
471       vTurbPQR(eR) = vBodyTurbGrad(eX)/10.0;
472
473                                 // Clear the horizontal forces
474                                 // actually felt by the plane, now
475                                 // that we've used them to calculate
476                                 // moments.
477                                 // Why? (JSB)
478 //    vTurbulenceNED(eX) = 0.0;
479 //    vTurbulenceNED(eY) = 0.0;
480
481     break;
482   }
483
484   case ttCulp: { 
485
486     vTurbPQR(eP) = wind_from_clockwise;
487     if (TurbGain == 0.0) return;
488   
489     // keep the inputs within allowable limts for this model
490     if (TurbGain < 0.0) TurbGain = 0.0;
491     if (TurbGain > 1.0) TurbGain = 1.0;
492     if (TurbRate < 0.0) TurbRate = 0.0;
493     if (TurbRate > 30.0) TurbRate = 30.0;
494     if (Rhythmicity < 0.0) Rhythmicity = 0.0;
495     if (Rhythmicity > 1.0) Rhythmicity = 1.0;
496
497     // generate a sine wave corresponding to turbulence rate in hertz
498     double time = FDMExec->GetSimTime();
499     double sinewave = sin( time * TurbRate * 6.283185307 );
500
501     double random = 0.0;
502     if (target_time == 0.0) {
503       strength = random = 1 - 2.0*(double(rand())/double(RAND_MAX));
504       target_time = time + 0.71 + (random * 0.5);
505     }
506     if (time > target_time) {
507       spike = 1.0;
508       target_time = 0.0;
509     }    
510
511     // max vertical wind speed in fps, corresponds to TurbGain = 1.0
512     double max_vs = 40;
513
514     vTurbulenceNED(1) = vTurbulenceNED(2) = vTurbulenceNED(3) = 0.0;
515     double delta = strength * max_vs * TurbGain * (1-Rhythmicity) * spike;
516
517     // Vertical component of turbulence.
518     vTurbulenceNED(3) = sinewave * max_vs * TurbGain * Rhythmicity;
519     vTurbulenceNED(3)+= delta;
520     if (HOverBMAC < 3.0)
521         vTurbulenceNED(3) *= HOverBMAC * 0.3333;
522  
523     // Yaw component of turbulence.
524     vTurbulenceNED(1) = sin( delta * 3.0 );
525     vTurbulenceNED(2) = cos( delta * 3.0 );
526
527     // Roll component of turbulence. Clockwise vortex causes left roll.
528     vTurbPQR(eP) += delta * 0.04;
529
530     spike = spike * 0.9;
531     break;
532   }
533   case ttMilspec:
534   case ttTustin: {
535     double V = FDMExec->GetAuxiliary()->GetVt(); // true airspeed in ft/s
536
537     // an index of zero means turbulence is disabled
538     // airspeed occurs as divisor in the code below
539     if (probability_of_exceedence_index == 0 || V == 0) {
540       vTurbulenceNED(1) = vTurbulenceNED(2) = vTurbulenceNED(3) = 0.0;
541       vTurbPQR(1) = vTurbPQR(2) = vTurbPQR(3) = 0.0;
542       return;
543     }
544
545     // Turbulence model according to MIL-F-8785C (Flying Qualities of Piloted Aircraft)
546     double
547       h = FDMExec->GetPropagate()->GetDistanceAGL(),
548       b_w = wingspan,
549       L_u, L_w, sig_u, sig_w;
550
551       if (b_w == 0.) b_w = 30.;
552
553     // clip height functions at 10 ft
554     if (h <= 10.) h = 10;
555
556     // Scale lengths L and amplitudes sigma as function of height
557     if (h <= 1000) {
558       L_u = h/pow(0.177 + 0.000823*h, 1.2); // MIL-F-8785c, Fig. 10, p. 55
559       L_w = h;
560       sig_w = 0.1*windspeed_at_20ft;
561       sig_u = sig_w/pow(0.177 + 0.000823*h, 0.4); // MIL-F-8785c, Fig. 11, p. 56
562     } else if (h <= 2000) {
563       // linear interpolation between low altitude and high altitude models
564       L_u = L_w = 1000 + (h-1000.)/1000.*750.;
565       sig_u = sig_w = 0.1*windspeed_at_20ft
566                     + (h-1000.)/1000.*(POE_Table->GetValue(probability_of_exceedence_index, h) - 0.1*windspeed_at_20ft);
567     } else {
568       L_u = L_w = 1750.; //  MIL-F-8785c, Sec. 3.7.2.1, p. 48
569       sig_u = sig_w = POE_Table->GetValue(probability_of_exceedence_index, h);
570     }
571
572     // keep values from last timesteps
573     // TODO maybe use deque?
574     static double
575       xi_u_km1 = 0, nu_u_km1 = 0,
576       xi_v_km1 = 0, xi_v_km2 = 0, nu_v_km1 = 0, nu_v_km2 = 0,
577       xi_w_km1 = 0, xi_w_km2 = 0, nu_w_km1 = 0, nu_w_km2 = 0,
578       xi_p_km1 = 0, nu_p_km1 = 0,
579       xi_q_km1 = 0, xi_r_km1 = 0;
580
581
582     double
583       T_V = DeltaT, // for compatibility of nomenclature
584       sig_p = 1.9/sqrt(L_w*b_w)*sig_w, // Yeager1998, eq. (8)
585       sig_q = sqrt(M_PI/2/L_w/b_w), // eq. (14)
586       sig_r = sqrt(2*M_PI/3/L_w/b_w), // eq. (17)
587       L_p = sqrt(L_w*b_w)/2.6, // eq. (10)
588       tau_u = L_u/V, // eq. (6)
589       tau_w = L_w/V, // eq. (3)
590       tau_p = L_p/V, // eq. (9)
591       tau_q = 4*b_w/M_PI/V, // eq. (13)
592       tau_r =3*b_w/M_PI/V, // eq. (17)
593       nu_u = GaussianRandomNumber(),
594       nu_v = GaussianRandomNumber(),
595       nu_w = GaussianRandomNumber(),
596       nu_p = GaussianRandomNumber(),
597       xi_u=0, xi_v=0, xi_w=0, xi_p=0, xi_q=0, xi_r=0;
598
599     // values of turbulence NED velocities
600
601     if (turbType == ttTustin) {
602       // the following is the Tustin formulation of Yeager's report
603       double
604         omega_w = V/L_w, // hidden in nomenclature p. 3
605         omega_v = V/L_u, // this is defined nowhere
606         C_BL  = 1/tau_u/tan(T_V/2/tau_u), // eq. (19)
607         C_BLp = 1/tau_p/tan(T_V/2/tau_p), // eq. (22)
608         C_BLq = 1/tau_q/tan(T_V/2/tau_q), // eq. (24)
609         C_BLr = 1/tau_r/tan(T_V/2/tau_r); // eq. (26)
610
611       // all values calculated so far are strictly positive, except for
612       // the random numbers nu_*. This means that in the code below, all
613       // divisors are strictly positive, too, and no floating point
614       // exception should occur.
615       xi_u = -(1 - C_BL*tau_u)/(1 + C_BL*tau_u)*xi_u_km1
616            + sig_u*sqrt(2*tau_u/T_V)/(1 + C_BL*tau_u)*(nu_u + nu_u_km1); // eq. (18)
617       xi_v = -2*(sqr(omega_v) - sqr(C_BL))/sqr(omega_v + C_BL)*xi_v_km1
618            - sqr(omega_v - C_BL)/sqr(omega_v + C_BL) * xi_v_km2
619            + sig_u*sqrt(3*omega_v/T_V)/sqr(omega_v + C_BL)*(
620                  (C_BL + omega_v/sqrt(3.))*nu_v
621                + 2/sqrt(3.)*omega_v*nu_v_km1
622                + (omega_v/sqrt(3.) - C_BL)*nu_v_km2); // eq. (20) for v
623       xi_w = -2*(sqr(omega_w) - sqr(C_BL))/sqr(omega_w + C_BL)*xi_w_km1
624            - sqr(omega_w - C_BL)/sqr(omega_w + C_BL) * xi_w_km2
625            + sig_w*sqrt(3*omega_w/T_V)/sqr(omega_w + C_BL)*(
626                  (C_BL + omega_w/sqrt(3.))*nu_w
627                + 2/sqrt(3.)*omega_w*nu_w_km1
628                + (omega_w/sqrt(3.) - C_BL)*nu_w_km2); // eq. (20) for w
629       xi_p = -(1 - C_BLp*tau_p)/(1 + C_BLp*tau_p)*xi_p_km1
630            + sig_p*sqrt(2*tau_p/T_V)/(1 + C_BLp*tau_p) * (nu_p + nu_p_km1); // eq. (21)
631       xi_q = -(1 - 4*b_w*C_BLq/M_PI/V)/(1 + 4*b_w*C_BLq/M_PI/V) * xi_q_km1
632            + C_BLq/V/(1 + 4*b_w*C_BLq/M_PI/V) * (xi_w - xi_w_km1); // eq. (23)
633       xi_r = - (1 - 3*b_w*C_BLr/M_PI/V)/(1 + 3*b_w*C_BLr/M_PI/V) * xi_r_km1
634            + C_BLr/V/(1 + 3*b_w*C_BLr/M_PI/V) * (xi_v - xi_v_km1); // eq. (25)
635
636     } else if (turbType == ttMilspec) {
637       // the following is the MIL-STD-1797A formulation
638       // as cited in Yeager's report
639       xi_u = (1 - T_V/tau_u)  *xi_u_km1 + sig_u*sqrt(2*T_V/tau_u)*nu_u;  // eq. (30)
640       xi_v = (1 - 2*T_V/tau_u)*xi_v_km1 + sig_u*sqrt(4*T_V/tau_u)*nu_v;  // eq. (31)
641       xi_w = (1 - 2*T_V/tau_w)*xi_w_km1 + sig_w*sqrt(4*T_V/tau_w)*nu_w;  // eq. (32)
642       xi_p = (1 - T_V/tau_p)  *xi_p_km1 + sig_p*sqrt(2*T_V/tau_p)*nu_p;  // eq. (33)
643       xi_q = (1 - T_V/tau_q)  *xi_q_km1 + M_PI/4/b_w*(xi_w - xi_w_km1);  // eq. (34)
644       xi_r = (1 - T_V/tau_r)  *xi_r_km1 + M_PI/3/b_w*(xi_v - xi_v_km1);  // eq. (35)
645     }
646
647     // rotate by wind azimuth and assign the velocities
648     double cospsi = cos(psiw), sinpsi = sin(psiw);
649     vTurbulenceNED(1) =  cospsi*xi_u + sinpsi*xi_v;
650     vTurbulenceNED(2) = -sinpsi*xi_u + cospsi*xi_v;
651     vTurbulenceNED(3) = xi_w;
652
653     vTurbPQR(1) =  cospsi*xi_p + sinpsi*xi_q;
654     vTurbPQR(2) = -sinpsi*xi_p + cospsi*xi_q;
655     vTurbPQR(3) = xi_r;
656
657     // vTurbPQR is in the body fixed frame, not NED
658     vTurbPQR = Tl2b*vTurbPQR;
659
660     // hand on the values for the next timestep
661     xi_u_km1 = xi_u; nu_u_km1 = nu_u;
662     xi_v_km2 = xi_v_km1; xi_v_km1 = xi_v; nu_v_km2 = nu_v_km1; nu_v_km1 = nu_v;
663     xi_w_km2 = xi_w_km1; xi_w_km1 = xi_w; nu_w_km2 = nu_w_km1; nu_w_km1 = nu_w;
664     xi_p_km1 = xi_p; nu_p_km1 = nu_p;
665     xi_q_km1 = xi_q;
666     xi_r_km1 = xi_r;
667
668   }
669   default:
670     break;
671   }
672 }
673
674 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
675
676 void FGAtmosphere::UseExternal(void)
677 {
678   temperature=&exTemperature;
679   pressure=&exPressure;
680   density=&exDensity;
681   useExternal=true;
682 }
683
684 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
685
686 void FGAtmosphere::UseInternal(void)
687 {
688   temperature=&intTemperature;
689   pressure=&intPressure;
690   density=&intDensity;
691   useExternal=false;
692 }
693
694 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
695
696 void FGAtmosphere::bind(void)
697 {
698   typedef double (FGAtmosphere::*PMF)(int) const;
699   typedef double (FGAtmosphere::*PMFv)(void) const;
700   typedef int (FGAtmosphere::*PMFt)(void) const;
701   typedef void   (FGAtmosphere::*PMFd)(int,double);
702   typedef void   (FGAtmosphere::*PMFi)(int);
703   PropertyManager->Tie("atmosphere/T-R", this, (PMFv)&FGAtmosphere::GetTemperature);
704   PropertyManager->Tie("atmosphere/rho-slugs_ft3", this, (PMFv)&FGAtmosphere::GetDensity);
705   PropertyManager->Tie("atmosphere/P-psf", this, (PMFv)&FGAtmosphere::GetPressure);
706   PropertyManager->Tie("atmosphere/a-fps", this, &FGAtmosphere::GetSoundSpeed);
707   PropertyManager->Tie("atmosphere/T-sl-R", this, &FGAtmosphere::GetTemperatureSL);
708   PropertyManager->Tie("atmosphere/rho-sl-slugs_ft3", this, &FGAtmosphere::GetDensitySL);
709   PropertyManager->Tie("atmosphere/P-sl-psf", this, &FGAtmosphere::GetPressureSL);
710   PropertyManager->Tie("atmosphere/a-sl-fps", this, &FGAtmosphere::GetSoundSpeedSL);
711   PropertyManager->Tie("atmosphere/theta", this, &FGAtmosphere::GetTemperatureRatio);
712   PropertyManager->Tie("atmosphere/sigma", this, &FGAtmosphere::GetDensityRatio);
713   PropertyManager->Tie("atmosphere/delta", this, &FGAtmosphere::GetPressureRatio);
714   PropertyManager->Tie("atmosphere/a-ratio", this, &FGAtmosphere::GetSoundSpeedRatio);
715   PropertyManager->Tie("atmosphere/psiw-rad", this, &FGAtmosphere::GetWindPsi, &FGAtmosphere::SetWindPsi);
716   PropertyManager->Tie("atmosphere/delta-T", this, &FGAtmosphere::GetDeltaT, &FGAtmosphere::SetDeltaT);
717   PropertyManager->Tie("atmosphere/T-sl-dev-F", this, &FGAtmosphere::GetSLTempDev, &FGAtmosphere::SetSLTempDev);
718   PropertyManager->Tie("atmosphere/density-altitude", this, &FGAtmosphere::GetDensityAltitude);
719
720   PropertyManager->Tie("atmosphere/wind-north-fps", this, eNorth, (PMF)&FGAtmosphere::GetWindNED,
721                                                           (PMFd)&FGAtmosphere::SetWindNED);
722   PropertyManager->Tie("atmosphere/wind-east-fps",  this, eEast, (PMF)&FGAtmosphere::GetWindNED,
723                                                           (PMFd)&FGAtmosphere::SetWindNED);
724   PropertyManager->Tie("atmosphere/wind-down-fps",  this, eDown, (PMF)&FGAtmosphere::GetWindNED,
725                                                           (PMFd)&FGAtmosphere::SetWindNED);
726   PropertyManager->Tie("atmosphere/wind-mag-fps", this, &FGAtmosphere::GetWindspeed,
727                                                         &FGAtmosphere::SetWindspeed);
728   PropertyManager->Tie("atmosphere/total-wind-north-fps", this, eNorth, (PMF)&FGAtmosphere::GetTotalWindNED);
729   PropertyManager->Tie("atmosphere/total-wind-east-fps",  this, eEast,  (PMF)&FGAtmosphere::GetTotalWindNED);
730   PropertyManager->Tie("atmosphere/total-wind-down-fps",  this, eDown,  (PMF)&FGAtmosphere::GetTotalWindNED);
731
732   PropertyManager->Tie("atmosphere/gust-north-fps", this, eNorth, (PMF)&FGAtmosphere::GetGustNED,
733                                                           (PMFd)&FGAtmosphere::SetGustNED);
734   PropertyManager->Tie("atmosphere/gust-east-fps",  this, eEast, (PMF)&FGAtmosphere::GetGustNED,
735                                                           (PMFd)&FGAtmosphere::SetGustNED);
736   PropertyManager->Tie("atmosphere/gust-down-fps",  this, eDown, (PMF)&FGAtmosphere::GetGustNED,
737                                                           (PMFd)&FGAtmosphere::SetGustNED);
738
739   PropertyManager->Tie("atmosphere/turb-north-fps", this, eNorth, (PMF)&FGAtmosphere::GetTurbNED,
740                                                           (PMFd)&FGAtmosphere::SetTurbNED);
741   PropertyManager->Tie("atmosphere/turb-east-fps",  this, eEast, (PMF)&FGAtmosphere::GetTurbNED,
742                                                           (PMFd)&FGAtmosphere::SetTurbNED);
743   PropertyManager->Tie("atmosphere/turb-down-fps",  this, eDown, (PMF)&FGAtmosphere::GetTurbNED,
744                                                           (PMFd)&FGAtmosphere::SetTurbNED);
745
746   PropertyManager->Tie("atmosphere/p-turb-rad_sec", this,1, (PMF)&FGAtmosphere::GetTurbPQR);
747   PropertyManager->Tie("atmosphere/q-turb-rad_sec", this,2, (PMF)&FGAtmosphere::GetTurbPQR);
748   PropertyManager->Tie("atmosphere/r-turb-rad_sec", this,3, (PMF)&FGAtmosphere::GetTurbPQR);
749   PropertyManager->Tie("atmosphere/turb-type", this, (PMFt)&FGAtmosphere::GetTurbType, (PMFi)&FGAtmosphere::SetTurbType);
750   PropertyManager->Tie("atmosphere/turb-rate", this, &FGAtmosphere::GetTurbRate, &FGAtmosphere::SetTurbRate);
751   PropertyManager->Tie("atmosphere/turb-gain", this, &FGAtmosphere::GetTurbGain, &FGAtmosphere::SetTurbGain);
752   PropertyManager->Tie("atmosphere/turb-rhythmicity", this, &FGAtmosphere::GetRhythmicity,
753                                                             &FGAtmosphere::SetRhythmicity);
754
755   PropertyManager->Tie("atmosphere/turbulence/milspec/windspeed_at_20ft_AGL-fps",
756                        this, &FGAtmosphere::GetWindspeed20ft,
757                              &FGAtmosphere::SetWindspeed20ft);
758   PropertyManager->Tie("atmosphere/turbulence/milspec/severity",
759                        this, &FGAtmosphere::GetProbabilityOfExceedence,
760                              &FGAtmosphere::SetProbabilityOfExceedence);
761
762 }
763
764 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
765 //    The bitmasked value choices are as follows:
766 //    unset: In this case (the default) JSBSim would only print
767 //       out the normally expected messages, essentially echoing
768 //       the config files as they are read. If the environment
769 //       variable is not set, debug_lvl is set to 1 internally
770 //    0: This requests JSBSim not to output any messages
771 //       whatsoever.
772 //    1: This value explicity requests the normal JSBSim
773 //       startup messages
774 //    2: This value asks for a message to be printed out when
775 //       a class is instantiated
776 //    4: When this value is set, a message is displayed when a
777 //       FGModel object executes its Run() method
778 //    8: When this value is set, various runtime state variables
779 //       are printed out periodically
780 //    16: When set various parameters are sanity checked and
781 //       a message is printed out when they go out of bounds
782
783 void FGAtmosphere::Debug(int from)
784 {
785   if (debug_lvl <= 0) return;
786
787   if (debug_lvl & 1) { // Standard console startup message output
788     if (from == 0) { // Constructor
789     }
790   }
791   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
792     if (from == 0) cout << "Instantiated: FGAtmosphere" << endl;
793     if (from == 1) cout << "Destroyed:    FGAtmosphere" << endl;
794   }
795   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
796   }
797   if (debug_lvl & 8 ) { // Runtime state variables
798   }
799   if (debug_lvl & 16) { // Sanity checking
800   }
801   if (debug_lvl & 128) { // Turbulence
802     if (first_pass && from == 2) {
803       first_pass = false;
804       cout << "vTurbulenceNED(X), vTurbulenceNED(Y), vTurbulenceNED(Z), "
805            << "vTurbulenceGrad(X), vTurbulenceGrad(Y), vTurbulenceGrad(Z), "
806            << "vDirection(X), vDirection(Y), vDirection(Z), "
807            << "Magnitude, "
808            << "vTurbPQR(P), vTurbPQR(Q), vTurbPQR(R), " << endl;
809     } 
810     if (from == 2) {
811       cout << vTurbulenceNED << ", " << vTurbulenceGrad << ", " << vDirection << ", " << Magnitude << ", " << vTurbPQR << endl;
812     }
813   }
814   if (debug_lvl & 64) {
815     if (from == 0) { // Constructor
816       cout << IdSrc << endl;
817       cout << IdHdr << endl;
818     }
819   }
820 }
821
822 } // namespace JSBSim