]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGAtmosphere.cpp
Sync w. JSBSim CVS
[flightgear.git] / src / FDM / JSBSim / 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 (jsb@hal-pc.org) -------------
11
12  This program is free software; you can redistribute it and/or modify it under
13  the terms of the GNU 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 General Public License for more
20  details.
21
22  You should have received a copy of the GNU 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 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 "FGPropertyManager.h"
57
58 namespace JSBSim {
59
60 static const char *IdSrc = "$Id$";
61 static const char *IdHdr = ID_ATMOSPHERE;
62
63 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
64 CLASS IMPLEMENTATION
65 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
66
67
68 FGAtmosphere::FGAtmosphere(FGFDMExec* fdmex) : FGModel(fdmex)
69 {
70   Name = "FGAtmosphere";
71   lastIndex = 0;
72   h = 0.0;
73   psiw = 0.0;
74   htab[0]=0;
75   htab[1]=36089.239;
76   htab[2]=65616.798;
77   htab[3]=104986.878;
78   htab[4]=154199.475;
79   htab[5]=170603.675;
80   htab[6]=200131.234;
81   htab[7]=259186.352; //ft.
82
83   MagnitudedAccelDt = MagnitudeAccel = Magnitude = 0.0;
84 //   turbType = ttNone;
85   turbType = ttStandard;
86 //   turbType = ttBerndt;
87   TurbGain = 0.0;
88   TurbRate = 1.0;
89
90   bind();
91   Debug(0);
92 }
93
94 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
95
96 FGAtmosphere::~FGAtmosphere()
97 {
98   unbind();
99   Debug(1);
100 }
101
102 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
103
104 bool FGAtmosphere::InitModel(void)
105 {
106   FGModel::InitModel();
107
108   Calculate(h);
109   SLtemperature = intTemperature;
110   SLpressure    = intPressure;
111   SLdensity     = intDensity;
112   SLsoundspeed  = sqrt(SHRatio*Reng*intTemperature);
113   rSLtemperature = 1.0/intTemperature;
114   rSLpressure    = 1.0/intPressure;
115   rSLdensity     = 1.0/intDensity;
116   rSLsoundspeed  = 1.0/SLsoundspeed;
117   temperature=&intTemperature;
118   pressure=&intPressure;
119   density=&intDensity;
120
121   useExternal=false;
122
123   return true;
124 }
125
126 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
127
128 bool FGAtmosphere::Run(void)
129 {
130   if (!FGModel::Run()) {                 // if false then execute this Run()
131     //do temp, pressure, and density first
132     if (!useExternal) {
133       h = Propagate->Geth();
134       Calculate(h);
135     }
136
137     if (turbType != ttNone) {
138       Turbulence();
139       vWindNED += vTurbulence;
140     }
141
142     if (vWindNED(1) != 0.0) psiw = atan2( vWindNED(2), vWindNED(1) );
143
144     if (psiw < 0) psiw += 2*M_PI;
145
146     soundspeed = sqrt(SHRatio*Reng*(*temperature));
147
148     Debug(2);
149
150     return false;
151   } else {                               // skip Run() execution this time
152     return true;
153   }
154 }
155
156 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
157 //
158 // See reference 1
159
160 void FGAtmosphere::Calculate(double altitude)
161 {
162   double slope, reftemp, refpress;
163   int i = 0;
164
165   i = lastIndex;
166   if (altitude < htab[lastIndex]) {
167     if (altitude <= 0) {
168       i = 0;
169       altitude=0;
170     } else {
171        i = lastIndex-1;
172        while (htab[i] > altitude) i--;
173     }
174   } else if (altitude > htab[lastIndex+1]) {
175     if (altitude >= htab[7]) {
176       i = 7;
177       altitude = htab[7];
178     } else {
179       i = lastIndex+1;
180       while (htab[i+1] < altitude) i++;
181     }
182   }
183
184   switch(i) {
185   case 1:     // 36089 ft.
186     slope     = 0;
187     reftemp   = 389.97;
188     refpress  = 472.452;
189     //refdens   = 0.000706032;
190     break;
191   case 2:     // 65616 ft.
192     slope     = 0.00054864;
193     reftemp   = 389.97;
194     refpress  = 114.636;
195     //refdens   = 0.000171306;
196     break;
197   case 3:     // 104986 ft.
198     slope     = 0.00153619;
199     reftemp   = 411.57;
200     refpress  = 8.36364;
201     //refdens   = 1.18422e-05;
202     break;
203   case 4:     // 154199 ft.
204     slope     = 0;
205     reftemp   = 487.17;
206     refpress  = 0.334882;
207     //refdens   = 4.00585e-7;
208     break;
209   case 5:     // 170603 ft.
210     slope     = -0.00109728;
211     reftemp   = 487.17;
212     refpress  = 0.683084;
213     //refdens   = 8.17102e-7;
214     break;
215   case 6:     // 200131 ft.
216     slope     = -0.00219456;
217     reftemp   = 454.17;
218     refpress  = 0.00684986;
219     //refdens   = 8.77702e-9;
220     break;
221   case 7:     // 259186 ft.
222     slope     = 0;
223     reftemp   = 325.17;
224     refpress  = 0.000122276;
225     //refdens   = 2.19541e-10;
226     break;
227   case 0:
228   default:     // sea level
229     slope     = -0.00356616; // R/ft.
230     reftemp   = 518.67;    // R
231     refpress  = 2116.22;    // psf
232     //refdens   = 0.00237767;  // slugs/cubic ft.
233     break;
234
235   }
236
237   if (slope == 0) {
238     intTemperature = reftemp;
239     intPressure = refpress*exp(-Inertial->SLgravity()/(reftemp*Reng)*(altitude-htab[i]));
240     //intDensity = refdens*exp(-Inertial->SLgravity()/(reftemp*Reng)*(altitude-htab[i]));
241     intDensity = intPressure/(Reng*intTemperature);
242   } else {
243     intTemperature = reftemp+slope*(altitude-htab[i]);
244     intPressure = refpress*pow(intTemperature/reftemp,-Inertial->SLgravity()/(slope*Reng));
245     //intDensity = refdens*pow(intTemperature/reftemp,-(Inertial->SLgravity()/(slope*Reng)+1));
246     intDensity = intPressure/(Reng*intTemperature);
247   }
248   lastIndex=i;
249   //cout << "Atmosphere:  h=" << altitude << " rho= " << intDensity << endl;
250 }
251
252 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
253 // Return the pressure at an arbitrary altitude and then restore the internal state
254
255 double FGAtmosphere::GetPressure(double alt) {
256   Calculate(alt);
257   double p = *pressure;
258   // Reset the internal atmospheric state
259   Run();
260   return(p);
261 }
262
263 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
264 // square a value, but preserve the original sign
265
266 static inline double square_signed (double value)
267 {
268     if (value < 0)
269         return value * value * -1;
270     else
271         return value * value;
272 }
273
274 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
275
276 void FGAtmosphere::Turbulence(void)
277 {
278   switch (turbType) {
279   case ttStandard: {
280     vDirectiondAccelDt(eX) = 1 - 2.0*(double(rand())/double(RAND_MAX));
281     vDirectiondAccelDt(eY) = 1 - 2.0*(double(rand())/double(RAND_MAX));
282     vDirectiondAccelDt(eZ) = 1 - 2.0*(double(rand())/double(RAND_MAX));
283
284     MagnitudedAccelDt = 1 - 2.0*(double(rand())/double(RAND_MAX)) - Magnitude;
285                                 // Scale the magnitude so that it moves
286                                 // away from the peaks
287     MagnitudedAccelDt = ((MagnitudedAccelDt - Magnitude) /
288                          (1 + fabs(Magnitude)));
289     MagnitudeAccel    += MagnitudedAccelDt*rate*TurbRate*State->Getdt();
290     Magnitude         += MagnitudeAccel*rate*State->Getdt();
291
292     vDirectiondAccelDt.Normalize();
293
294                                 // deemphasise non-vertical forces
295     vDirectiondAccelDt(eX) = square_signed(vDirectiondAccelDt(eX));
296     vDirectiondAccelDt(eY) = square_signed(vDirectiondAccelDt(eY));
297
298     vDirectionAccel += vDirectiondAccelDt*rate*TurbRate*State->Getdt();
299     vDirectionAccel.Normalize();
300     vDirection      += vDirectionAccel*rate*State->Getdt();
301
302     vDirection.Normalize();
303
304                                 // Diminish turbulence within three wingspans
305                                 // of the ground
306     vTurbulence = TurbGain * Magnitude * vDirection;
307     double HOverBMAC = Auxiliary->GetHOverBMAC();
308     if (HOverBMAC < 3.0)
309         vTurbulence *= (HOverBMAC / 3.0) * (HOverBMAC / 3.0);
310
311     vTurbulenceGrad = TurbGain*MagnitudeAccel * vDirection;
312
313     vBodyTurbGrad = Propagate->GetTl2b()*vTurbulenceGrad;
314
315     if (Aircraft->GetWingSpan() > 0) {
316       vTurbPQR(eP) = vBodyTurbGrad(eY)/Aircraft->GetWingSpan();
317     } else {
318       vTurbPQR(eP) = vBodyTurbGrad(eY)/30.0;
319     }
320 //     if (Aircraft->GetHTailArm() != 0.0)
321 //       vTurbPQR(eQ) = vBodyTurbGrad(eZ)/Aircraft->GetHTailArm();
322 //     else
323 //       vTurbPQR(eQ) = vBodyTurbGrad(eZ)/10.0;
324
325     if (Aircraft->GetVTailArm() > 0)
326       vTurbPQR(eR) = vBodyTurbGrad(eX)/Aircraft->GetVTailArm();
327     else
328       vTurbPQR(eR) = vBodyTurbGrad(eX)/10.0;
329
330                                 // Clear the horizontal forces
331                                 // actually felt by the plane, now
332                                 // that we've used them to calculate
333                                 // moments.
334     vTurbulence(eX) = 0.0;
335     vTurbulence(eY) = 0.0;
336
337     break;
338   }
339   case ttBerndt: {
340     vDirectiondAccelDt(eX) = 1 - 2.0*(double(rand())/double(RAND_MAX));
341     vDirectiondAccelDt(eY) = 1 - 2.0*(double(rand())/double(RAND_MAX));
342     vDirectiondAccelDt(eZ) = 1 - 2.0*(double(rand())/double(RAND_MAX));
343
344
345     MagnitudedAccelDt = 1 - 2.0*(double(rand())/double(RAND_MAX)) - Magnitude;
346     MagnitudeAccel    += MagnitudedAccelDt*rate*State->Getdt();
347     Magnitude         += MagnitudeAccel*rate*State->Getdt();
348
349     vDirectiondAccelDt.Normalize();
350     vDirectionAccel += vDirectiondAccelDt*rate*State->Getdt();
351     vDirectionAccel.Normalize();
352     vDirection      += vDirectionAccel*rate*State->Getdt();
353
354                                 // Diminish z-vector within two wingspans
355                                 // of the ground
356     double HOverBMAC = Auxiliary->GetHOverBMAC();
357     if (HOverBMAC < 2.0)
358         vDirection(eZ) *= HOverBMAC / 2.0;
359
360     vDirection.Normalize();
361
362     vTurbulence = TurbGain*Magnitude * vDirection;
363     vTurbulenceGrad = TurbGain*MagnitudeAccel * vDirection;
364
365     vBodyTurbGrad = Propagate->GetTl2b()*vTurbulenceGrad;
366     vTurbPQR(eP) = vBodyTurbGrad(eY)/Aircraft->GetWingSpan();
367     if (Aircraft->GetHTailArm() > 0)
368       vTurbPQR(eQ) = vBodyTurbGrad(eZ)/Aircraft->GetHTailArm();
369     else
370       vTurbPQR(eQ) = vBodyTurbGrad(eZ)/10.0;
371
372     if (Aircraft->GetVTailArm() > 0)
373       vTurbPQR(eR) = vBodyTurbGrad(eX)/Aircraft->GetVTailArm();
374     else
375       vTurbPQR(eR) = vBodyTurbGrad(eX)/10.0;
376
377     break;
378   }
379   default:
380     break;
381   }
382 }
383
384 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
385
386 void FGAtmosphere::UseExternal(void) {
387   temperature=&exTemperature;
388   pressure=&exPressure;
389   density=&exDensity;
390   useExternal=true;
391 }
392
393 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
394
395 void FGAtmosphere::UseInternal(void) {
396   temperature=&intTemperature;
397   pressure=&intPressure;
398   density=&intDensity;
399   useExternal=false;
400 }
401
402 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
403
404 void FGAtmosphere::bind(void)
405 {
406   typedef double (FGAtmosphere::*PMF)(int) const;
407   PropertyManager->Tie("atmosphere/T-R", this,
408                        &FGAtmosphere::GetTemperature);
409   PropertyManager->Tie("atmosphere/rho-slugs_ft3", this,
410                        &FGAtmosphere::GetDensity);
411 //  PropertyManager->Tie("atmosphere/P-psf", this,
412 //                       &FGAtmosphere::GetPressure);
413   PropertyManager->Tie("atmosphere/a-fps", this,
414                        &FGAtmosphere::GetSoundSpeed);
415   PropertyManager->Tie("atmosphere/T-sl-R", this,
416                        &FGAtmosphere::GetTemperatureSL);
417   PropertyManager->Tie("atmosphere/rho-sl-slugs_ft3", this,
418                        &FGAtmosphere::GetDensitySL);
419   PropertyManager->Tie("atmosphere/P-sl-psf", this,
420                        &FGAtmosphere::GetPressureSL);
421   PropertyManager->Tie("atmosphere/a-sl-fps", this,
422                        &FGAtmosphere::GetSoundSpeedSL);
423   PropertyManager->Tie("atmosphere/theta-norm", this,
424                        &FGAtmosphere::GetTemperatureRatio);
425   PropertyManager->Tie("atmosphere/sigma-norm", this,
426                        &FGAtmosphere::GetDensityRatio);
427   PropertyManager->Tie("atmosphere/delta-norm", this,
428                        &FGAtmosphere::GetPressureRatio);
429   PropertyManager->Tie("atmosphere/a-norm", this,
430                        &FGAtmosphere::GetSoundSpeedRatio);
431   PropertyManager->Tie("atmosphere/psiw-rad", this,
432                        &FGAtmosphere::GetWindPsi);
433   PropertyManager->Tie("atmosphere/p-turb-rad_sec", this,1,
434                        (PMF)&FGAtmosphere::GetTurbPQR);
435   PropertyManager->Tie("atmosphere/q-turb-rad_sec", this,2,
436                        (PMF)&FGAtmosphere::GetTurbPQR);
437   PropertyManager->Tie("atmosphere/r-turb-rad_sec", this,3,
438                        (PMF)&FGAtmosphere::GetTurbPQR);
439 }
440
441 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
442
443 void FGAtmosphere::unbind(void)
444 {
445   PropertyManager->Untie("atmosphere/T-R");
446   PropertyManager->Untie("atmosphere/rho-slugs_ft3");
447 //  PropertyManager->Untie("atmosphere/P-psf");
448   PropertyManager->Untie("atmosphere/a-fps");
449   PropertyManager->Untie("atmosphere/T-sl-R");
450   PropertyManager->Untie("atmosphere/rho-sl-slugs_ft3");
451   PropertyManager->Untie("atmosphere/P-sl-psf");
452   PropertyManager->Untie("atmosphere/a-sl-fps");
453   PropertyManager->Untie("atmosphere/theta-norm");
454   PropertyManager->Untie("atmosphere/sigma-norm");
455   PropertyManager->Untie("atmosphere/delta-norm");
456   PropertyManager->Untie("atmosphere/a-norm");
457   PropertyManager->Untie("atmosphere/psiw-rad");
458   PropertyManager->Untie("atmosphere/p-turb-rad_sec");
459   PropertyManager->Untie("atmosphere/q-turb-rad_sec");
460   PropertyManager->Untie("atmosphere/r-turb-rad_sec");
461 }
462
463 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
464 //    The bitmasked value choices are as follows:
465 //    unset: In this case (the default) JSBSim would only print
466 //       out the normally expected messages, essentially echoing
467 //       the config files as they are read. If the environment
468 //       variable is not set, debug_lvl is set to 1 internally
469 //    0: This requests JSBSim not to output any messages
470 //       whatsoever.
471 //    1: This value explicity requests the normal JSBSim
472 //       startup messages
473 //    2: This value asks for a message to be printed out when
474 //       a class is instantiated
475 //    4: When this value is set, a message is displayed when a
476 //       FGModel object executes its Run() method
477 //    8: When this value is set, various runtime state variables
478 //       are printed out periodically
479 //    16: When set various parameters are sanity checked and
480 //       a message is printed out when they go out of bounds
481
482 void FGAtmosphere::Debug(int from)
483 {
484   if (debug_lvl <= 0) return;
485
486   if (debug_lvl & 1) { // Standard console startup message output
487     if (from == 0) { // Constructor
488     }
489   }
490   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
491     if (from == 0) cout << "Instantiated: FGAtmosphere" << endl;
492     if (from == 1) cout << "Destroyed:    FGAtmosphere" << endl;
493   }
494   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
495   }
496   if (debug_lvl & 8 ) { // Runtime state variables
497   }
498   if (debug_lvl & 16) { // Sanity checking
499   }
500   if (debug_lvl & 32) { // Turbulence
501     if (frame == 0 && from == 2) {
502       cout << "vTurbulence(X), vTurbulence(Y), vTurbulence(Z), "
503            << "vTurbulenceGrad(X), vTurbulenceGrad(Y), vTurbulenceGrad(Z), "
504            << "vDirection(X), vDirection(Y), vDirection(Z), "
505            << "Magnitude, "
506            << "vTurbPQR(P), vTurbPQR(Q), vTurbPQR(R), " << endl;
507     } else if (from == 2) {
508       cout << vTurbulence << ", " << vTurbulenceGrad << ", " << vDirection << ", " << Magnitude << ", " << vTurbPQR << endl;
509     }
510   }
511   if (debug_lvl & 64) {
512     if (from == 0) { // Constructor
513       cout << IdSrc << endl;
514       cout << IdHdr << endl;
515     }
516   }
517 }
518
519 } // namespace JSBSim