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 <input_output/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;
91 StandardTempOnly = false;
97 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
99 FGAtmosphere::~FGAtmosphere()
105 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
107 bool FGAtmosphere::InitModel(void)
109 FGModel::InitModel();
111 UseInternal(); // this is the default
114 StdSLtemperature = SLtemperature = 518.67;
115 StdSLpressure = SLpressure = 2116.22;
116 StdSLdensity = SLdensity = 0.00237767;
117 StdSLsoundspeed = SLsoundspeed = sqrt(SHRatio*Reng*StdSLtemperature);
118 rSLtemperature = 1.0/StdSLtemperature;
119 rSLpressure = 1.0/StdSLpressure;
120 rSLdensity = 1.0/StdSLdensity;
121 rSLsoundspeed = 1.0/StdSLsoundspeed;
126 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
128 bool FGAtmosphere::Run(void)
130 if (FGModel::Run()) return true;
131 if (FDMExec->Holding()) return false;
134 h = Propagate->Geth();
147 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
151 void FGAtmosphere::Calculate(double altitude)
153 double slope, reftemp, refpress;
157 if (altitude < htab[lastIndex]) {
163 while (htab[i] > altitude) i--;
165 } else if (altitude > htab[lastIndex+1]) {
166 if (altitude >= htab[7]) {
171 while (htab[i+1] < altitude) i++;
180 //refdens = 0.000706032;
186 //refdens = 0.000171306;
188 case 3: // 104986 ft.
192 //refdens = 1.18422e-05;
194 case 4: // 154199 ft.
198 //refdens = 4.00585e-7;
200 case 5: // 170603 ft.
204 //refdens = 8.17102e-7;
206 case 6: // 200131 ft.
209 refpress = 0.00684986;
210 //refdens = 8.77702e-9;
212 case 7: // 259186 ft.
215 refpress = 0.000122276;
216 //refdens = 2.19541e-10;
219 default: // sea level
220 slope = -0.00356616; // R/ft.
221 reftemp = 518.67; // R
222 refpress = 2116.22; // psf
223 //refdens = 0.00237767; // slugs/cubic ft.
228 // If delta_T is set, then that is our temperature deviation at any altitude.
229 // If not, then we'll estimate a deviation based on the sea level deviation (if set).
231 if(!StandardTempOnly) {
233 if (delta_T != 0.0) {
236 if ((altitude < 36089.239) && (T_dev_sl != 0.0)) {
237 T_dev = T_dev_sl * ( 1.0 - (altitude/36089.239));
244 intTemperature = reftemp;
245 intPressure = refpress*exp(-Inertial->SLgravity()/(reftemp*Reng)*(altitude-htab[i]));
246 intDensity = intPressure/(Reng*intTemperature);
248 intTemperature = reftemp+slope*(altitude-htab[i]);
249 intPressure = refpress*pow(intTemperature/reftemp,-Inertial->SLgravity()/(slope*Reng));
250 intDensity = intPressure/(Reng*intTemperature);
256 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
257 // Calculate parameters derived from T, P and rho
259 void FGAtmosphere::CalculateDerived(void)
261 T_dev = (*temperature) - GetTemperature(h);
262 density_altitude = h + T_dev * 66.7;
264 if (turbType != ttStandard) {
266 vWindNED += vTurbulence;
268 if (vWindNED(1) != 0.0) psiw = atan2( vWindNED(2), vWindNED(1) );
269 if (psiw < 0) psiw += 2*M_PI;
271 soundspeed = sqrt(SHRatio*Reng*(*temperature));
275 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
276 // Get the standard atmospheric properties at a specified altitude
278 void FGAtmosphere::GetStdAtmosphere(double altitude) {
279 StandardTempOnly = true;
281 StandardTempOnly = false;
282 atmosphere.Temperature = intTemperature;
283 atmosphere.Pressure = intPressure;
284 atmosphere.Density = intDensity;
286 // Reset the internal atmospheric state
290 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
291 // Get the standard pressure at a specified altitude
293 double FGAtmosphere::GetPressure(double altitude) {
294 GetStdAtmosphere(altitude);
295 return atmosphere.Pressure;
298 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
299 // Get the standard temperature at a specified altitude
301 double FGAtmosphere::GetTemperature(double altitude) {
302 GetStdAtmosphere(altitude);
303 return atmosphere.Temperature;
306 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
307 // Get the standard density at a specified altitude
309 double FGAtmosphere::GetDensity(double altitude) {
310 GetStdAtmosphere(altitude);
311 return atmosphere.Density;
315 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
316 // square a value, but preserve the original sign
318 static inline double square_signed (double value)
321 return value * value * -1;
323 return value * value;
326 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
328 void FGAtmosphere::Turbulence(void)
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));
336 MagnitudedAccelDt = 1 - 2.0*(double(rand())/double(RAND_MAX)) - Magnitude;
337 // Scale the magnitude so that it moves
338 // away from the peaks
339 MagnitudedAccelDt = ((MagnitudedAccelDt - Magnitude) /
340 (1 + fabs(Magnitude)));
341 MagnitudeAccel += MagnitudedAccelDt*rate*TurbRate*State->Getdt();
342 Magnitude += MagnitudeAccel*rate*State->Getdt();
344 vDirectiondAccelDt.Normalize();
346 // deemphasise non-vertical forces
347 vDirectiondAccelDt(eX) = square_signed(vDirectiondAccelDt(eX));
348 vDirectiondAccelDt(eY) = square_signed(vDirectiondAccelDt(eY));
350 vDirectionAccel += vDirectiondAccelDt*rate*TurbRate*State->Getdt();
351 vDirectionAccel.Normalize();
352 vDirection += vDirectionAccel*rate*State->Getdt();
354 vDirection.Normalize();
356 // Diminish turbulence within three wingspans
358 vTurbulence = TurbGain * Magnitude * vDirection;
359 double HOverBMAC = Auxiliary->GetHOverBMAC();
361 vTurbulence *= (HOverBMAC / 3.0) * (HOverBMAC / 3.0);
363 vTurbulenceGrad = TurbGain*MagnitudeAccel * vDirection;
365 vBodyTurbGrad = Propagate->GetTl2b()*vTurbulenceGrad;
367 if (Aircraft->GetWingSpan() > 0) {
368 vTurbPQR(eP) = vBodyTurbGrad(eY)/Aircraft->GetWingSpan();
370 vTurbPQR(eP) = vBodyTurbGrad(eY)/30.0;
372 // if (Aircraft->GetHTailArm() != 0.0)
373 // vTurbPQR(eQ) = vBodyTurbGrad(eZ)/Aircraft->GetHTailArm();
375 // vTurbPQR(eQ) = vBodyTurbGrad(eZ)/10.0;
377 if (Aircraft->GetVTailArm() > 0)
378 vTurbPQR(eR) = vBodyTurbGrad(eX)/Aircraft->GetVTailArm();
380 vTurbPQR(eR) = vBodyTurbGrad(eX)/10.0;
382 // Clear the horizontal forces
383 // actually felt by the plane, now
384 // that we've used them to calculate
386 vTurbulence(eX) = 0.0;
387 vTurbulence(eY) = 0.0;
392 vDirectiondAccelDt(eX) = 1 - 2.0*(double(rand())/double(RAND_MAX));
393 vDirectiondAccelDt(eY) = 1 - 2.0*(double(rand())/double(RAND_MAX));
394 vDirectiondAccelDt(eZ) = 1 - 2.0*(double(rand())/double(RAND_MAX));
397 MagnitudedAccelDt = 1 - 2.0*(double(rand())/double(RAND_MAX)) - Magnitude;
398 MagnitudeAccel += MagnitudedAccelDt*rate*State->Getdt();
399 Magnitude += MagnitudeAccel*rate*State->Getdt();
401 vDirectiondAccelDt.Normalize();
402 vDirectionAccel += vDirectiondAccelDt*rate*State->Getdt();
403 vDirectionAccel.Normalize();
404 vDirection += vDirectionAccel*rate*State->Getdt();
406 // Diminish z-vector within two wingspans
408 double HOverBMAC = Auxiliary->GetHOverBMAC();
410 vDirection(eZ) *= HOverBMAC / 2.0;
412 vDirection.Normalize();
414 vTurbulence = TurbGain*Magnitude * vDirection;
415 vTurbulenceGrad = TurbGain*MagnitudeAccel * vDirection;
417 vBodyTurbGrad = Propagate->GetTl2b()*vTurbulenceGrad;
418 vTurbPQR(eP) = vBodyTurbGrad(eY)/Aircraft->GetWingSpan();
419 if (Aircraft->GetHTailArm() > 0)
420 vTurbPQR(eQ) = vBodyTurbGrad(eZ)/Aircraft->GetHTailArm();
422 vTurbPQR(eQ) = vBodyTurbGrad(eZ)/10.0;
424 if (Aircraft->GetVTailArm() > 0)
425 vTurbPQR(eR) = vBodyTurbGrad(eX)/Aircraft->GetVTailArm();
427 vTurbPQR(eR) = vBodyTurbGrad(eX)/10.0;
436 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
438 void FGAtmosphere::UseExternal(void)
440 temperature=&exTemperature;
441 pressure=&exPressure;
446 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
448 void FGAtmosphere::UseInternal(void)
450 temperature=&intTemperature;
451 pressure=&intPressure;
456 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
458 void FGAtmosphere::bind(void)
460 typedef double (FGAtmosphere::*PMF)(int) const;
461 PropertyManager->Tie("atmosphere/T-R", this,
462 &FGAtmosphere::GetTemperature);
463 PropertyManager->Tie("atmosphere/rho-slugs_ft3", this,
464 &FGAtmosphere::GetDensity);
465 // PropertyManager->Tie("atmosphere/P-psf", this,
466 // &FGAtmosphere::GetPressure);
467 PropertyManager->Tie("atmosphere/a-fps", this,
468 &FGAtmosphere::GetSoundSpeed);
469 PropertyManager->Tie("atmosphere/T-sl-R", this,
470 &FGAtmosphere::GetTemperatureSL);
471 PropertyManager->Tie("atmosphere/rho-sl-slugs_ft3", this,
472 &FGAtmosphere::GetDensitySL);
473 PropertyManager->Tie("atmosphere/P-sl-psf", this,
474 &FGAtmosphere::GetPressureSL);
475 PropertyManager->Tie("atmosphere/a-sl-fps", this,
476 &FGAtmosphere::GetSoundSpeedSL);
477 PropertyManager->Tie("atmosphere/theta", this,
478 &FGAtmosphere::GetTemperatureRatio);
479 PropertyManager->Tie("atmosphere/sigma", this,
480 &FGAtmosphere::GetDensityRatio);
481 PropertyManager->Tie("atmosphere/delta", this,
482 &FGAtmosphere::GetPressureRatio);
483 PropertyManager->Tie("atmosphere/a-ratio", this,
484 &FGAtmosphere::GetSoundSpeedRatio);
485 PropertyManager->Tie("atmosphere/psiw-rad", this,
486 &FGAtmosphere::GetWindPsi);
487 PropertyManager->Tie("atmosphere/delta-T", this,
488 &FGAtmosphere::GetDeltaT, &FGAtmosphere::SetDeltaT);
489 PropertyManager->Tie("atmosphere/T-sl-dev-F", this,
490 &FGAtmosphere::GetSLTempDev, &FGAtmosphere::SetSLTempDev);
491 PropertyManager->Tie("atmosphere/density-altitude", this,
492 &FGAtmosphere::GetDensityAltitude);
493 PropertyManager->Tie("atmosphere/p-turb-rad_sec", this,1,
494 (PMF)&FGAtmosphere::GetTurbPQR);
495 PropertyManager->Tie("atmosphere/q-turb-rad_sec", this,2,
496 (PMF)&FGAtmosphere::GetTurbPQR);
497 PropertyManager->Tie("atmosphere/r-turb-rad_sec", this,3,
498 (PMF)&FGAtmosphere::GetTurbPQR);
501 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
503 void FGAtmosphere::unbind(void)
505 PropertyManager->Untie("atmosphere/T-R");
506 PropertyManager->Untie("atmosphere/rho-slugs_ft3");
507 // PropertyManager->Untie("atmosphere/P-psf");
508 PropertyManager->Untie("atmosphere/a-fps");
509 PropertyManager->Untie("atmosphere/T-sl-R");
510 PropertyManager->Untie("atmosphere/rho-sl-slugs_ft3");
511 PropertyManager->Untie("atmosphere/P-sl-psf");
512 PropertyManager->Untie("atmosphere/a-sl-fps");
513 PropertyManager->Untie("atmosphere/delta-T");
514 PropertyManager->Untie("atmosphere/T-sl-dev-F");
515 PropertyManager->Untie("atmosphere/density-altitude");
516 PropertyManager->Untie("atmosphere/theta");
517 PropertyManager->Untie("atmosphere/sigma");
518 PropertyManager->Untie("atmosphere/delta");
519 PropertyManager->Untie("atmosphere/a-ratio");
520 PropertyManager->Untie("atmosphere/psiw-rad");
521 PropertyManager->Untie("atmosphere/p-turb-rad_sec");
522 PropertyManager->Untie("atmosphere/q-turb-rad_sec");
523 PropertyManager->Untie("atmosphere/r-turb-rad_sec");
526 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
527 // The bitmasked value choices are as follows:
528 // unset: In this case (the default) JSBSim would only print
529 // out the normally expected messages, essentially echoing
530 // the config files as they are read. If the environment
531 // variable is not set, debug_lvl is set to 1 internally
532 // 0: This requests JSBSim not to output any messages
534 // 1: This value explicity requests the normal JSBSim
536 // 2: This value asks for a message to be printed out when
537 // a class is instantiated
538 // 4: When this value is set, a message is displayed when a
539 // FGModel object executes its Run() method
540 // 8: When this value is set, various runtime state variables
541 // are printed out periodically
542 // 16: When set various parameters are sanity checked and
543 // a message is printed out when they go out of bounds
545 void FGAtmosphere::Debug(int from)
547 if (debug_lvl <= 0) return;
549 if (debug_lvl & 1) { // Standard console startup message output
550 if (from == 0) { // Constructor
553 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
554 if (from == 0) cout << "Instantiated: FGAtmosphere" << endl;
555 if (from == 1) cout << "Destroyed: FGAtmosphere" << endl;
557 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
559 if (debug_lvl & 8 ) { // Runtime state variables
561 if (debug_lvl & 16) { // Sanity checking
563 if (debug_lvl & 128) { // Turbulence
564 if (frame == 0 && from == 2) {
565 cout << "vTurbulence(X), vTurbulence(Y), vTurbulence(Z), "
566 << "vTurbulenceGrad(X), vTurbulenceGrad(Y), vTurbulenceGrad(Z), "
567 << "vDirection(X), vDirection(Y), vDirection(Z), "
569 << "vTurbPQR(P), vTurbPQR(Q), vTurbPQR(R), " << endl;
570 } else if (from == 2) {
571 cout << vTurbulence << ", " << vTurbulenceGrad << ", " << vDirection << ", " << Magnitude << ", " << vTurbPQR << endl;
574 if (debug_lvl & 64) {
575 if (from == 0) { // Constructor
576 cout << IdSrc << endl;
577 cout << IdHdr << endl;
582 } // namespace JSBSim