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