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 Lesser 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 Lesser General Public License for more
22 You should have received a copy of the GNU Lesser 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 Lesser 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;
98 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
100 FGAtmosphere::~FGAtmosphere()
106 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
108 bool FGAtmosphere::InitModel(void)
110 FGModel::InitModel();
112 UseInternal(); // this is the default
115 StdSLtemperature = SLtemperature = 518.67;
116 StdSLpressure = SLpressure = 2116.22;
117 StdSLdensity = SLdensity = 0.00237767;
118 StdSLsoundspeed = SLsoundspeed = sqrt(SHRatio*Reng*StdSLtemperature);
119 rSLtemperature = 1.0/StdSLtemperature;
120 rSLpressure = 1.0/StdSLpressure;
121 rSLdensity = 1.0/StdSLdensity;
122 rSLsoundspeed = 1.0/StdSLsoundspeed;
127 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
129 bool FGAtmosphere::Run(void)
131 if (FGModel::Run()) return true;
132 if (FDMExec->Holding()) return false;
135 h = Propagate->Geth();
148 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
152 void FGAtmosphere::Calculate(double altitude)
154 double slope, reftemp, refpress;
158 if (altitude < htab[lastIndex]) {
164 while (htab[i] > altitude) i--;
166 } else if (altitude > htab[lastIndex+1]) {
167 if (altitude >= htab[7]) {
172 while (htab[i+1] < altitude) i++;
181 //refdens = 0.000706032;
187 //refdens = 0.000171306;
189 case 3: // 104986 ft.
193 //refdens = 1.18422e-05;
195 case 4: // 154199 ft.
199 //refdens = 4.00585e-7;
201 case 5: // 170603 ft.
205 //refdens = 8.17102e-7;
207 case 6: // 200131 ft.
210 refpress = 0.00684986;
211 //refdens = 8.77702e-9;
213 case 7: // 259186 ft.
216 refpress = 0.000122276;
217 //refdens = 2.19541e-10;
220 default: // sea level
221 slope = -0.00356616; // R/ft.
222 reftemp = 518.67; // R
223 refpress = 2116.22; // psf
224 //refdens = 0.00237767; // slugs/cubic ft.
229 // If delta_T is set, then that is our temperature deviation at any altitude.
230 // If not, then we'll estimate a deviation based on the sea level deviation (if set).
232 if(!StandardTempOnly) {
234 if (delta_T != 0.0) {
237 if ((altitude < 36089.239) && (T_dev_sl != 0.0)) {
238 T_dev = T_dev_sl * ( 1.0 - (altitude/36089.239));
245 intTemperature = reftemp;
246 intPressure = refpress*exp(-Inertial->SLgravity()/(reftemp*Reng)*(altitude-htab[i]));
247 intDensity = intPressure/(Reng*intTemperature);
249 intTemperature = reftemp+slope*(altitude-htab[i]);
250 intPressure = refpress*pow(intTemperature/reftemp,-Inertial->SLgravity()/(slope*Reng));
251 intDensity = intPressure/(Reng*intTemperature);
257 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
258 // Calculate parameters derived from T, P and rho
260 void FGAtmosphere::CalculateDerived(void)
262 T_dev = (*temperature) - GetTemperature(h);
263 density_altitude = h + T_dev * 66.7;
265 if (turbType == ttStandard) {
267 vWindNED += vTurbulence;
269 if (vWindNED(1) != 0.0) psiw = atan2( vWindNED(2), vWindNED(1) );
270 if (psiw < 0) psiw += 2*M_PI;
272 soundspeed = sqrt(SHRatio*Reng*(*temperature));
276 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
277 // Get the standard atmospheric properties at a specified altitude
279 void FGAtmosphere::GetStdAtmosphere(double altitude) {
280 StandardTempOnly = true;
282 StandardTempOnly = false;
283 atmosphere.Temperature = intTemperature;
284 atmosphere.Pressure = intPressure;
285 atmosphere.Density = intDensity;
287 // Reset the internal atmospheric state
291 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
292 // Get the standard pressure at a specified altitude
294 double FGAtmosphere::GetPressure(double altitude) {
295 GetStdAtmosphere(altitude);
296 return atmosphere.Pressure;
299 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
300 // Get the standard temperature at a specified altitude
302 double FGAtmosphere::GetTemperature(double altitude) {
303 GetStdAtmosphere(altitude);
304 return atmosphere.Temperature;
307 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
308 // Get the standard density at a specified altitude
310 double FGAtmosphere::GetDensity(double altitude) {
311 GetStdAtmosphere(altitude);
312 return atmosphere.Density;
316 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
317 // square a value, but preserve the original sign
319 static inline double square_signed (double value)
322 return value * value * -1;
324 return value * value;
327 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
329 void FGAtmosphere::Turbulence(void)
333 vDirectiondAccelDt(eX) = 1 - 2.0*(double(rand())/double(RAND_MAX));
334 vDirectiondAccelDt(eY) = 1 - 2.0*(double(rand())/double(RAND_MAX));
335 vDirectiondAccelDt(eZ) = 1 - 2.0*(double(rand())/double(RAND_MAX));
337 MagnitudedAccelDt = 1 - 2.0*(double(rand())/double(RAND_MAX)) - Magnitude;
338 // Scale the magnitude so that it moves
339 // away from the peaks
340 MagnitudedAccelDt = ((MagnitudedAccelDt - Magnitude) /
341 (1 + fabs(Magnitude)));
342 MagnitudeAccel += MagnitudedAccelDt*rate*TurbRate*State->Getdt();
343 Magnitude += MagnitudeAccel*rate*State->Getdt();
345 vDirectiondAccelDt.Normalize();
347 // deemphasise non-vertical forces
348 vDirectiondAccelDt(eX) = square_signed(vDirectiondAccelDt(eX));
349 vDirectiondAccelDt(eY) = square_signed(vDirectiondAccelDt(eY));
351 vDirectionAccel += vDirectiondAccelDt*rate*TurbRate*State->Getdt();
352 vDirectionAccel.Normalize();
353 vDirection += vDirectionAccel*rate*State->Getdt();
355 vDirection.Normalize();
357 // Diminish turbulence within three wingspans
359 vTurbulence = TurbGain * Magnitude * vDirection;
360 double HOverBMAC = Auxiliary->GetHOverBMAC();
362 vTurbulence *= (HOverBMAC / 3.0) * (HOverBMAC / 3.0);
364 vTurbulenceGrad = TurbGain*MagnitudeAccel * vDirection;
366 vBodyTurbGrad = Propagate->GetTl2b()*vTurbulenceGrad;
368 if (Aircraft->GetWingSpan() > 0) {
369 vTurbPQR(eP) = vBodyTurbGrad(eY)/Aircraft->GetWingSpan();
371 vTurbPQR(eP) = vBodyTurbGrad(eY)/30.0;
373 // if (Aircraft->GetHTailArm() != 0.0)
374 // vTurbPQR(eQ) = vBodyTurbGrad(eZ)/Aircraft->GetHTailArm();
376 // vTurbPQR(eQ) = vBodyTurbGrad(eZ)/10.0;
378 if (Aircraft->GetVTailArm() > 0)
379 vTurbPQR(eR) = vBodyTurbGrad(eX)/Aircraft->GetVTailArm();
381 vTurbPQR(eR) = vBodyTurbGrad(eX)/10.0;
383 // Clear the horizontal forces
384 // actually felt by the plane, now
385 // that we've used them to calculate
387 vTurbulence(eX) = 0.0;
388 vTurbulence(eY) = 0.0;
393 vDirectiondAccelDt(eX) = 1 - 2.0*(double(rand())/double(RAND_MAX));
394 vDirectiondAccelDt(eY) = 1 - 2.0*(double(rand())/double(RAND_MAX));
395 vDirectiondAccelDt(eZ) = 1 - 2.0*(double(rand())/double(RAND_MAX));
398 MagnitudedAccelDt = 1 - 2.0*(double(rand())/double(RAND_MAX)) - Magnitude;
399 MagnitudeAccel += MagnitudedAccelDt*rate*State->Getdt();
400 Magnitude += MagnitudeAccel*rate*State->Getdt();
402 vDirectiondAccelDt.Normalize();
403 vDirectionAccel += vDirectiondAccelDt*rate*State->Getdt();
404 vDirectionAccel.Normalize();
405 vDirection += vDirectionAccel*rate*State->Getdt();
407 // Diminish z-vector within two wingspans
409 double HOverBMAC = Auxiliary->GetHOverBMAC();
411 vDirection(eZ) *= HOverBMAC / 2.0;
413 vDirection.Normalize();
415 vTurbulence = TurbGain*Magnitude * vDirection;
416 vTurbulenceGrad = TurbGain*MagnitudeAccel * vDirection;
418 vBodyTurbGrad = Propagate->GetTl2b()*vTurbulenceGrad;
419 vTurbPQR(eP) = vBodyTurbGrad(eY)/Aircraft->GetWingSpan();
420 if (Aircraft->GetHTailArm() > 0)
421 vTurbPQR(eQ) = vBodyTurbGrad(eZ)/Aircraft->GetHTailArm();
423 vTurbPQR(eQ) = vBodyTurbGrad(eZ)/10.0;
425 if (Aircraft->GetVTailArm() > 0)
426 vTurbPQR(eR) = vBodyTurbGrad(eX)/Aircraft->GetVTailArm();
428 vTurbPQR(eR) = vBodyTurbGrad(eX)/10.0;
437 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
439 void FGAtmosphere::UseExternal(void)
441 temperature=&exTemperature;
442 pressure=&exPressure;
447 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
449 void FGAtmosphere::UseInternal(void)
451 temperature=&intTemperature;
452 pressure=&intPressure;
457 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
459 void FGAtmosphere::bind(void)
461 typedef double (FGAtmosphere::*PMF)(int) const;
462 typedef double (FGAtmosphere::*PMFv)(void) const;
463 PropertyManager->Tie("atmosphere/T-R", this, (PMFv)&FGAtmosphere::GetTemperature);
464 PropertyManager->Tie("atmosphere/rho-slugs_ft3", this, (PMFv)&FGAtmosphere::GetDensity);
465 PropertyManager->Tie("atmosphere/P-psf", this, (PMFv)&FGAtmosphere::GetPressure);
466 PropertyManager->Tie("atmosphere/a-fps", this, &FGAtmosphere::GetSoundSpeed);
467 PropertyManager->Tie("atmosphere/T-sl-R", this, &FGAtmosphere::GetTemperatureSL);
468 PropertyManager->Tie("atmosphere/rho-sl-slugs_ft3", this, &FGAtmosphere::GetDensitySL);
469 PropertyManager->Tie("atmosphere/P-sl-psf", this, &FGAtmosphere::GetPressureSL);
470 PropertyManager->Tie("atmosphere/a-sl-fps", this, &FGAtmosphere::GetSoundSpeedSL);
471 PropertyManager->Tie("atmosphere/theta", this, &FGAtmosphere::GetTemperatureRatio);
472 PropertyManager->Tie("atmosphere/sigma", this, &FGAtmosphere::GetDensityRatio);
473 PropertyManager->Tie("atmosphere/delta", this, &FGAtmosphere::GetPressureRatio);
474 PropertyManager->Tie("atmosphere/a-ratio", this, &FGAtmosphere::GetSoundSpeedRatio);
475 PropertyManager->Tie("atmosphere/psiw-rad", this, &FGAtmosphere::GetWindPsi);
476 PropertyManager->Tie("atmosphere/delta-T", this, &FGAtmosphere::GetDeltaT, &FGAtmosphere::SetDeltaT);
477 PropertyManager->Tie("atmosphere/T-sl-dev-F", this, &FGAtmosphere::GetSLTempDev, &FGAtmosphere::SetSLTempDev);
478 PropertyManager->Tie("atmosphere/density-altitude", this, &FGAtmosphere::GetDensityAltitude);
479 PropertyManager->Tie("atmosphere/p-turb-rad_sec", this,1, (PMF)&FGAtmosphere::GetTurbPQR);
480 PropertyManager->Tie("atmosphere/q-turb-rad_sec", this,2, (PMF)&FGAtmosphere::GetTurbPQR);
481 PropertyManager->Tie("atmosphere/r-turb-rad_sec", this,3, (PMF)&FGAtmosphere::GetTurbPQR);
484 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
486 void FGAtmosphere::unbind(void)
488 PropertyManager->Untie("atmosphere/T-R");
489 PropertyManager->Untie("atmosphere/rho-slugs_ft3");
490 PropertyManager->Untie("atmosphere/P-psf");
491 PropertyManager->Untie("atmosphere/a-fps");
492 PropertyManager->Untie("atmosphere/T-sl-R");
493 PropertyManager->Untie("atmosphere/rho-sl-slugs_ft3");
494 PropertyManager->Untie("atmosphere/P-sl-psf");
495 PropertyManager->Untie("atmosphere/a-sl-fps");
496 PropertyManager->Untie("atmosphere/delta-T");
497 PropertyManager->Untie("atmosphere/T-sl-dev-F");
498 PropertyManager->Untie("atmosphere/density-altitude");
499 PropertyManager->Untie("atmosphere/theta");
500 PropertyManager->Untie("atmosphere/sigma");
501 PropertyManager->Untie("atmosphere/delta");
502 PropertyManager->Untie("atmosphere/a-ratio");
503 PropertyManager->Untie("atmosphere/psiw-rad");
504 PropertyManager->Untie("atmosphere/p-turb-rad_sec");
505 PropertyManager->Untie("atmosphere/q-turb-rad_sec");
506 PropertyManager->Untie("atmosphere/r-turb-rad_sec");
509 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
510 // The bitmasked value choices are as follows:
511 // unset: In this case (the default) JSBSim would only print
512 // out the normally expected messages, essentially echoing
513 // the config files as they are read. If the environment
514 // variable is not set, debug_lvl is set to 1 internally
515 // 0: This requests JSBSim not to output any messages
517 // 1: This value explicity requests the normal JSBSim
519 // 2: This value asks for a message to be printed out when
520 // a class is instantiated
521 // 4: When this value is set, a message is displayed when a
522 // FGModel object executes its Run() method
523 // 8: When this value is set, various runtime state variables
524 // are printed out periodically
525 // 16: When set various parameters are sanity checked and
526 // a message is printed out when they go out of bounds
528 void FGAtmosphere::Debug(int from)
530 if (debug_lvl <= 0) return;
532 if (debug_lvl & 1) { // Standard console startup message output
533 if (from == 0) { // Constructor
536 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
537 if (from == 0) cout << "Instantiated: FGAtmosphere" << endl;
538 if (from == 1) cout << "Destroyed: FGAtmosphere" << endl;
540 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
542 if (debug_lvl & 8 ) { // Runtime state variables
544 if (debug_lvl & 16) { // Sanity checking
546 if (debug_lvl & 128) { // Turbulence
547 if (first_pass && from == 2) {
549 cout << "vTurbulence(X), vTurbulence(Y), vTurbulence(Z), "
550 << "vTurbulenceGrad(X), vTurbulenceGrad(Y), vTurbulenceGrad(Z), "
551 << "vDirection(X), vDirection(Y), vDirection(Z), "
553 << "vTurbPQR(P), vTurbPQR(Q), vTurbPQR(R), " << endl;
556 cout << vTurbulence << ", " << vTurbulenceGrad << ", " << vDirection << ", " << Magnitude << ", " << vTurbPQR << endl;
559 if (debug_lvl & 64) {
560 if (from == 0) { // Constructor
561 cout << IdSrc << endl;
562 cout << IdHdr << endl;
567 } // namespace JSBSim