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