1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 Module: FGAtmosphere.cpp
5 Implementation of 1959 Standard Atmosphere added by Tony Peden
7 Purpose: Models the atmosphere
10 ------------- Copyright (C) 1999 Jon S. Berndt (jsb@hal-pc.org) -------------
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
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
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.
26 Further information about the GNU General Public License can also be found on
27 the world wide web at http://www.gnu.org.
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.
35 --------------------------------------------------------------------------------
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
46 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
48 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
50 #include "FGAtmosphere.h"
52 #include "FGFDMExec.h"
53 #include "FGAircraft.h"
54 #include "FGPropagate.h"
55 #include "FGInertial.h"
56 #include "FGPropertyManager.h"
60 static const char *IdSrc = "$Id$";
61 static const char *IdHdr = ID_ATMOSPHERE;
63 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
65 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
68 FGAtmosphere::FGAtmosphere(FGFDMExec* fdmex) : FGModel(fdmex)
70 Name = "FGAtmosphere";
81 htab[7]=259186.352; //ft.
83 MagnitudedAccelDt = MagnitudeAccel = Magnitude = 0.0;
85 turbType = ttStandard;
86 // turbType = ttBerndt;
90 T_dev_sl = T_dev = delta_T = 0.0;
96 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
98 FGAtmosphere::~FGAtmosphere()
104 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
106 bool FGAtmosphere::InitModel(void)
108 FGModel::InitModel();
111 SLtemperature = intTemperature;
112 SLpressure = intPressure;
113 SLdensity = intDensity;
114 SLsoundspeed = sqrt(SHRatio*Reng*intTemperature);
115 rSLtemperature = 1.0/intTemperature;
116 rSLpressure = 1.0/intPressure;
117 rSLdensity = 1.0/intDensity;
118 rSLsoundspeed = 1.0/SLsoundspeed;
119 temperature=&intTemperature;
120 pressure=&intPressure;
128 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
130 bool FGAtmosphere::Run(void)
132 if (!FGModel::Run()) { // if false then execute this Run()
133 //do temp, pressure, and density first
135 h = Propagate->Geth();
139 if (turbType != ttNone) {
141 vWindNED += vTurbulence;
144 if (vWindNED(1) != 0.0) psiw = atan2( vWindNED(2), vWindNED(1) );
146 if (psiw < 0) psiw += 2*M_PI;
148 soundspeed = sqrt(SHRatio*Reng*(*temperature));
153 } else { // skip Run() execution this time
158 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
162 void FGAtmosphere::Calculate(double altitude)
164 double slope, reftemp, refpress;
168 if (altitude < htab[lastIndex]) {
174 while (htab[i] > altitude) i--;
176 } else if (altitude > htab[lastIndex+1]) {
177 if (altitude >= htab[7]) {
182 while (htab[i+1] < altitude) i++;
191 //refdens = 0.000706032;
197 //refdens = 0.000171306;
199 case 3: // 104986 ft.
203 //refdens = 1.18422e-05;
205 case 4: // 154199 ft.
209 //refdens = 4.00585e-7;
211 case 5: // 170603 ft.
215 //refdens = 8.17102e-7;
217 case 6: // 200131 ft.
220 refpress = 0.00684986;
221 //refdens = 8.77702e-9;
223 case 7: // 259186 ft.
226 refpress = 0.000122276;
227 //refdens = 2.19541e-10;
230 default: // sea level
231 slope = -0.00356616; // R/ft.
232 reftemp = 518.67; // R
233 refpress = 2116.22; // psf
234 //refdens = 0.00237767; // slugs/cubic ft.
240 if (delta_T != 0.0) {
243 if ((h < 36089.239) && (T_dev_sl != 0.0)) {
244 T_dev = T_dev_sl * ( 1.0 - (h/36089.239));
247 density_altitude = h + T_dev * 66.7;
251 intTemperature = reftemp;
252 intPressure = refpress*exp(-Inertial->SLgravity()/(reftemp*Reng)*(altitude-htab[i]));
253 //intDensity = refdens*exp(-Inertial->SLgravity()/(reftemp*Reng)*(altitude-htab[i]));
254 intDensity = intPressure/(Reng*intTemperature);
256 intTemperature = reftemp+slope*(altitude-htab[i]);
257 intPressure = refpress*pow(intTemperature/reftemp,-Inertial->SLgravity()/(slope*Reng));
258 //intDensity = refdens*pow(intTemperature/reftemp,-(Inertial->SLgravity()/(slope*Reng)+1));
259 intDensity = intPressure/(Reng*intTemperature);
262 //cout << "Atmosphere: h=" << altitude << " rho= " << intDensity << endl;
265 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
266 // Return the pressure at an arbitrary altitude and then restore the internal state
268 double FGAtmosphere::GetPressure(double alt) {
270 double p = *pressure;
271 // Reset the internal atmospheric state
276 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
277 // square a value, but preserve the original sign
279 static inline double square_signed (double value)
282 return value * value * -1;
284 return value * value;
287 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
289 void FGAtmosphere::Turbulence(void)
293 vDirectiondAccelDt(eX) = 1 - 2.0*(double(rand())/double(RAND_MAX));
294 vDirectiondAccelDt(eY) = 1 - 2.0*(double(rand())/double(RAND_MAX));
295 vDirectiondAccelDt(eZ) = 1 - 2.0*(double(rand())/double(RAND_MAX));
297 MagnitudedAccelDt = 1 - 2.0*(double(rand())/double(RAND_MAX)) - Magnitude;
298 // Scale the magnitude so that it moves
299 // away from the peaks
300 MagnitudedAccelDt = ((MagnitudedAccelDt - Magnitude) /
301 (1 + fabs(Magnitude)));
302 MagnitudeAccel += MagnitudedAccelDt*rate*TurbRate*State->Getdt();
303 Magnitude += MagnitudeAccel*rate*State->Getdt();
305 vDirectiondAccelDt.Normalize();
307 // deemphasise non-vertical forces
308 vDirectiondAccelDt(eX) = square_signed(vDirectiondAccelDt(eX));
309 vDirectiondAccelDt(eY) = square_signed(vDirectiondAccelDt(eY));
311 vDirectionAccel += vDirectiondAccelDt*rate*TurbRate*State->Getdt();
312 vDirectionAccel.Normalize();
313 vDirection += vDirectionAccel*rate*State->Getdt();
315 vDirection.Normalize();
317 // Diminish turbulence within three wingspans
319 vTurbulence = TurbGain * Magnitude * vDirection;
320 double HOverBMAC = Auxiliary->GetHOverBMAC();
322 vTurbulence *= (HOverBMAC / 3.0) * (HOverBMAC / 3.0);
324 vTurbulenceGrad = TurbGain*MagnitudeAccel * vDirection;
326 vBodyTurbGrad = Propagate->GetTl2b()*vTurbulenceGrad;
328 if (Aircraft->GetWingSpan() > 0) {
329 vTurbPQR(eP) = vBodyTurbGrad(eY)/Aircraft->GetWingSpan();
331 vTurbPQR(eP) = vBodyTurbGrad(eY)/30.0;
333 // if (Aircraft->GetHTailArm() != 0.0)
334 // vTurbPQR(eQ) = vBodyTurbGrad(eZ)/Aircraft->GetHTailArm();
336 // vTurbPQR(eQ) = vBodyTurbGrad(eZ)/10.0;
338 if (Aircraft->GetVTailArm() > 0)
339 vTurbPQR(eR) = vBodyTurbGrad(eX)/Aircraft->GetVTailArm();
341 vTurbPQR(eR) = vBodyTurbGrad(eX)/10.0;
343 // Clear the horizontal forces
344 // actually felt by the plane, now
345 // that we've used them to calculate
347 vTurbulence(eX) = 0.0;
348 vTurbulence(eY) = 0.0;
353 vDirectiondAccelDt(eX) = 1 - 2.0*(double(rand())/double(RAND_MAX));
354 vDirectiondAccelDt(eY) = 1 - 2.0*(double(rand())/double(RAND_MAX));
355 vDirectiondAccelDt(eZ) = 1 - 2.0*(double(rand())/double(RAND_MAX));
358 MagnitudedAccelDt = 1 - 2.0*(double(rand())/double(RAND_MAX)) - Magnitude;
359 MagnitudeAccel += MagnitudedAccelDt*rate*State->Getdt();
360 Magnitude += MagnitudeAccel*rate*State->Getdt();
362 vDirectiondAccelDt.Normalize();
363 vDirectionAccel += vDirectiondAccelDt*rate*State->Getdt();
364 vDirectionAccel.Normalize();
365 vDirection += vDirectionAccel*rate*State->Getdt();
367 // Diminish z-vector within two wingspans
369 double HOverBMAC = Auxiliary->GetHOverBMAC();
371 vDirection(eZ) *= HOverBMAC / 2.0;
373 vDirection.Normalize();
375 vTurbulence = TurbGain*Magnitude * vDirection;
376 vTurbulenceGrad = TurbGain*MagnitudeAccel * vDirection;
378 vBodyTurbGrad = Propagate->GetTl2b()*vTurbulenceGrad;
379 vTurbPQR(eP) = vBodyTurbGrad(eY)/Aircraft->GetWingSpan();
380 if (Aircraft->GetHTailArm() > 0)
381 vTurbPQR(eQ) = vBodyTurbGrad(eZ)/Aircraft->GetHTailArm();
383 vTurbPQR(eQ) = vBodyTurbGrad(eZ)/10.0;
385 if (Aircraft->GetVTailArm() > 0)
386 vTurbPQR(eR) = vBodyTurbGrad(eX)/Aircraft->GetVTailArm();
388 vTurbPQR(eR) = vBodyTurbGrad(eX)/10.0;
397 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
399 void FGAtmosphere::UseExternal(void) {
400 temperature=&exTemperature;
401 pressure=&exPressure;
406 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
408 void FGAtmosphere::UseInternal(void) {
409 temperature=&intTemperature;
410 pressure=&intPressure;
415 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
417 void FGAtmosphere::bind(void)
419 typedef double (FGAtmosphere::*PMF)(int) const;
420 PropertyManager->Tie("atmosphere/T-R", this,
421 &FGAtmosphere::GetTemperature);
422 PropertyManager->Tie("atmosphere/rho-slugs_ft3", this,
423 &FGAtmosphere::GetDensity);
424 // PropertyManager->Tie("atmosphere/P-psf", this,
425 // &FGAtmosphere::GetPressure);
426 PropertyManager->Tie("atmosphere/a-fps", this,
427 &FGAtmosphere::GetSoundSpeed);
428 PropertyManager->Tie("atmosphere/T-sl-R", this,
429 &FGAtmosphere::GetTemperatureSL);
430 PropertyManager->Tie("atmosphere/rho-sl-slugs_ft3", this,
431 &FGAtmosphere::GetDensitySL);
432 PropertyManager->Tie("atmosphere/P-sl-psf", this,
433 &FGAtmosphere::GetPressureSL);
434 PropertyManager->Tie("atmosphere/a-sl-fps", this,
435 &FGAtmosphere::GetSoundSpeedSL);
436 PropertyManager->Tie("atmosphere/theta-norm", this,
437 &FGAtmosphere::GetTemperatureRatio);
438 PropertyManager->Tie("atmosphere/sigma-norm", this,
439 &FGAtmosphere::GetDensityRatio);
440 PropertyManager->Tie("atmosphere/delta-norm", this,
441 &FGAtmosphere::GetPressureRatio);
442 PropertyManager->Tie("atmosphere/a-norm", this,
443 &FGAtmosphere::GetSoundSpeedRatio);
444 PropertyManager->Tie("atmosphere/psiw-rad", this,
445 &FGAtmosphere::GetWindPsi);
446 PropertyManager->Tie("atmosphere/delta-T", this,
447 &FGAtmosphere::GetDeltaT, &FGAtmosphere::SetDeltaT);
448 PropertyManager->Tie("atmosphere/T-sl-dev-F", this,
449 &FGAtmosphere::GetSLTempDev, &FGAtmosphere::SetSLTempDev);
450 PropertyManager->Tie("atmosphere/density-altitude", this,
451 &FGAtmosphere::GetDensityAltitude);
452 PropertyManager->Tie("atmosphere/p-turb-rad_sec", this,1,
453 (PMF)&FGAtmosphere::GetTurbPQR);
454 PropertyManager->Tie("atmosphere/q-turb-rad_sec", this,2,
455 (PMF)&FGAtmosphere::GetTurbPQR);
456 PropertyManager->Tie("atmosphere/r-turb-rad_sec", this,3,
457 (PMF)&FGAtmosphere::GetTurbPQR);
460 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
462 void FGAtmosphere::unbind(void)
464 PropertyManager->Untie("atmosphere/T-R");
465 PropertyManager->Untie("atmosphere/rho-slugs_ft3");
466 // PropertyManager->Untie("atmosphere/P-psf");
467 PropertyManager->Untie("atmosphere/a-fps");
468 PropertyManager->Untie("atmosphere/T-sl-R");
469 PropertyManager->Untie("atmosphere/rho-sl-slugs_ft3");
470 PropertyManager->Untie("atmosphere/P-sl-psf");
471 PropertyManager->Untie("atmosphere/a-sl-fps");
472 PropertyManager->Untie("atmosphere/delta-T");
473 PropertyManager->Untie("atmosphere/T-sl-dev-F");
474 PropertyManager->Untie("atmosphere/density-altitude");
475 PropertyManager->Untie("atmosphere/theta-norm");
476 PropertyManager->Untie("atmosphere/sigma-norm");
477 PropertyManager->Untie("atmosphere/delta-norm");
478 PropertyManager->Untie("atmosphere/a-norm");
479 PropertyManager->Untie("atmosphere/psiw-rad");
480 PropertyManager->Untie("atmosphere/p-turb-rad_sec");
481 PropertyManager->Untie("atmosphere/q-turb-rad_sec");
482 PropertyManager->Untie("atmosphere/r-turb-rad_sec");
485 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
486 // The bitmasked value choices are as follows:
487 // unset: In this case (the default) JSBSim would only print
488 // out the normally expected messages, essentially echoing
489 // the config files as they are read. If the environment
490 // variable is not set, debug_lvl is set to 1 internally
491 // 0: This requests JSBSim not to output any messages
493 // 1: This value explicity requests the normal JSBSim
495 // 2: This value asks for a message to be printed out when
496 // a class is instantiated
497 // 4: When this value is set, a message is displayed when a
498 // FGModel object executes its Run() method
499 // 8: When this value is set, various runtime state variables
500 // are printed out periodically
501 // 16: When set various parameters are sanity checked and
502 // a message is printed out when they go out of bounds
504 void FGAtmosphere::Debug(int from)
506 if (debug_lvl <= 0) return;
508 if (debug_lvl & 1) { // Standard console startup message output
509 if (from == 0) { // Constructor
512 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
513 if (from == 0) cout << "Instantiated: FGAtmosphere" << endl;
514 if (from == 1) cout << "Destroyed: FGAtmosphere" << endl;
516 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
518 if (debug_lvl & 8 ) { // Runtime state variables
520 if (debug_lvl & 16) { // Sanity checking
522 if (debug_lvl & 32) { // Turbulence
523 if (frame == 0 && from == 2) {
524 cout << "vTurbulence(X), vTurbulence(Y), vTurbulence(Z), "
525 << "vTurbulenceGrad(X), vTurbulenceGrad(Y), vTurbulenceGrad(Z), "
526 << "vDirection(X), vDirection(Y), vDirection(Z), "
528 << "vTurbPQR(P), vTurbPQR(Q), vTurbPQR(R), " << endl;
529 } else if (from == 2) {
530 cout << vTurbulence << ", " << vTurbulenceGrad << ", " << vDirection << ", " << Magnitude << ", " << vTurbPQR << endl;
533 if (debug_lvl & 64) {
534 if (from == 0) { // Constructor
535 cout << IdSrc << endl;
536 cout << IdHdr << endl;
541 } // namespace JSBSim