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