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;
84 SetTurbType( ttCulp );
88 spike = target_time = strength = 0.0;
89 wind_from_clockwise = 0.0;
91 T_dev_sl = T_dev = delta_T = 0.0;
92 StandardTempOnly = false;
94 vGustNED(1) = vGustNED(2) = vGustNED(3) = 0.0; bgustSet = false;
95 vTurbulence(1) = vTurbulence(2) = vTurbulence(3) = 0.0;
101 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
103 FGAtmosphere::~FGAtmosphere()
108 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
110 bool FGAtmosphere::InitModel(void)
112 if (!FGModel::InitModel()) return false;
114 UseInternal(); // this is the default
117 StdSLtemperature = SLtemperature = 518.67;
118 StdSLpressure = SLpressure = 2116.22;
119 StdSLdensity = SLdensity = 0.00237767;
120 StdSLsoundspeed = SLsoundspeed = sqrt(SHRatio*Reng*StdSLtemperature);
121 rSLtemperature = 1.0/StdSLtemperature;
122 rSLpressure = 1.0/StdSLpressure;
123 rSLdensity = 1.0/StdSLdensity;
124 rSLsoundspeed = 1.0/StdSLsoundspeed;
129 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
131 bool FGAtmosphere::Run(void)
133 if (FGModel::Run()) return true;
134 if (FDMExec->Holding()) return false;
137 h = Propagate->Geth();
150 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
154 void FGAtmosphere::Calculate(double altitude)
156 double slope, reftemp, refpress;
159 if (altitude < htab[lastIndex]) {
165 while (htab[i] > altitude) i--;
167 } else if (altitude > htab[lastIndex+1]) {
168 if (altitude >= htab[7]) {
173 while (htab[i+1] < altitude) i++;
182 //refdens = 0.000706032;
188 //refdens = 0.000171306;
190 case 3: // 104986 ft.
194 //refdens = 1.18422e-05;
196 case 4: // 154199 ft.
200 //refdens = 4.00585e-7;
202 case 5: // 170603 ft.
206 //refdens = 8.17102e-7;
208 case 6: // 200131 ft.
211 refpress = 0.00684986;
212 //refdens = 8.77702e-9;
214 case 7: // 259186 ft.
217 refpress = 0.000122276;
218 //refdens = 2.19541e-10;
221 default: // sea level
222 slope = -0.00356616; // R/ft.
223 reftemp = 518.67; // R
224 refpress = 2116.22; // psf
225 //refdens = 0.00237767; // slugs/cubic ft.
230 // If delta_T is set, then that is our temperature deviation at any altitude.
231 // If not, then we'll estimate a deviation based on the sea level deviation (if set).
233 if(!StandardTempOnly) {
235 if (delta_T != 0.0) {
238 if ((altitude < 36089.239) && (T_dev_sl != 0.0)) {
239 T_dev = T_dev_sl * ( 1.0 - (altitude/36089.239));
246 intTemperature = reftemp;
247 intPressure = refpress*exp(-Inertial->SLgravity()/(reftemp*Reng)*(altitude-htab[i]));
248 intDensity = intPressure/(Reng*intTemperature);
250 intTemperature = reftemp+slope*(altitude-htab[i]);
251 intPressure = refpress*pow(intTemperature/reftemp,-Inertial->SLgravity()/(slope*Reng));
252 intDensity = intPressure/(Reng*intTemperature);
258 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
259 // Calculate parameters derived from T, P and rho
261 void FGAtmosphere::CalculateDerived(void)
263 T_dev = (*temperature) - GetTemperature(h);
264 density_altitude = h + T_dev * 66.7;
266 if (turbType == ttStandard || ttCulp) {
268 vWindNED += vGustNED + vTurbulence;
270 if (vWindNED(1) != 0.0) psiw = atan2( vWindNED(2), vWindNED(1) );
271 if (psiw < 0) psiw += 2*M_PI;
273 soundspeed = sqrt(SHRatio*Reng*(*temperature));
277 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
278 // Get the standard atmospheric properties at a specified altitude
280 void FGAtmosphere::GetStdAtmosphere(double altitude) {
281 StandardTempOnly = true;
283 StandardTempOnly = false;
284 atmosphere.Temperature = intTemperature;
285 atmosphere.Pressure = intPressure;
286 atmosphere.Density = intDensity;
288 // Reset the internal atmospheric state
292 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
293 // Get the standard pressure at a specified altitude
295 double FGAtmosphere::GetPressure(double altitude) {
296 GetStdAtmosphere(altitude);
297 return atmosphere.Pressure;
300 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
301 // Get the standard temperature at a specified altitude
303 double FGAtmosphere::GetTemperature(double altitude) {
304 GetStdAtmosphere(altitude);
305 return atmosphere.Temperature;
308 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
309 // Get the standard density at a specified altitude
311 double FGAtmosphere::GetDensity(double altitude) {
312 GetStdAtmosphere(altitude);
313 return atmosphere.Density;
317 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
318 // square a value, but preserve the original sign
320 static inline double square_signed (double value)
323 return value * value * -1;
325 return value * value;
328 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
330 void FGAtmosphere::Turbulence(void)
334 TurbGain = TurbGain * TurbGain * 100.0;
336 vDirectiondAccelDt(eX) = 1 - 2.0*(double(rand())/double(RAND_MAX));
337 vDirectiondAccelDt(eY) = 1 - 2.0*(double(rand())/double(RAND_MAX));
338 vDirectiondAccelDt(eZ) = 1 - 2.0*(double(rand())/double(RAND_MAX));
340 MagnitudedAccelDt = 1 - 2.0*(double(rand())/double(RAND_MAX)) - Magnitude;
341 // Scale the magnitude so that it moves
342 // away from the peaks
343 MagnitudedAccelDt = ((MagnitudedAccelDt - Magnitude) /
344 (1 + fabs(Magnitude)));
345 MagnitudeAccel += MagnitudedAccelDt*rate*TurbRate*State->Getdt();
346 Magnitude += MagnitudeAccel*rate*State->Getdt();
347 Magnitude = fabs(Magnitude);
349 vDirectiondAccelDt.Normalize();
351 // deemphasise non-vertical forces
352 vDirectiondAccelDt(eX) = square_signed(vDirectiondAccelDt(eX));
353 vDirectiondAccelDt(eY) = square_signed(vDirectiondAccelDt(eY));
355 vDirectionAccel += vDirectiondAccelDt*rate*TurbRate*State->Getdt();
356 vDirectionAccel.Normalize();
357 vDirection += vDirectionAccel*rate*State->Getdt();
359 vDirection.Normalize();
361 // Diminish turbulence within three wingspans
363 vTurbulence = TurbGain * Magnitude * vDirection;
364 double HOverBMAC = Auxiliary->GetHOverBMAC();
366 vTurbulence *= (HOverBMAC / 3.0) * (HOverBMAC / 3.0);
368 // I don't believe these next two statements calculate the proper gradient over
369 // the aircraft body. One reason is because this has no relationship with the
370 // orientation or velocity of the aircraft, which it must have. What is vTurbulenceGrad
371 // supposed to represent? And the direction and magnitude of the turbulence can change,
372 // so both accelerations need to be accounted for, no?
374 // Need to determine the turbulence change in body axes between two time points.
376 vTurbulenceGrad = TurbGain*MagnitudeAccel * vDirection;
377 vBodyTurbGrad = Propagate->GetTl2b()*vTurbulenceGrad;
379 if (Aircraft->GetWingSpan() > 0) {
380 vTurbPQR(eP) = vBodyTurbGrad(eY)/Aircraft->GetWingSpan();
382 vTurbPQR(eP) = vBodyTurbGrad(eY)/30.0;
384 // if (Aircraft->GetHTailArm() != 0.0)
385 // vTurbPQR(eQ) = vBodyTurbGrad(eZ)/Aircraft->GetHTailArm();
387 // vTurbPQR(eQ) = vBodyTurbGrad(eZ)/10.0;
389 if (Aircraft->GetVTailArm() > 0)
390 vTurbPQR(eR) = vBodyTurbGrad(eX)/Aircraft->GetVTailArm();
392 vTurbPQR(eR) = vBodyTurbGrad(eX)/10.0;
394 // Clear the horizontal forces
395 // actually felt by the plane, now
396 // that we've used them to calculate
399 // vTurbulence(eX) = 0.0;
400 // vTurbulence(eY) = 0.0;
404 case ttBerndt: { // This is very experimental and incomplete at the moment.
406 TurbGain = TurbGain * TurbGain * 100.0;
408 vDirectiondAccelDt(eX) = 1 - 2.0*(double(rand())/double(RAND_MAX));
409 vDirectiondAccelDt(eY) = 1 - 2.0*(double(rand())/double(RAND_MAX));
410 vDirectiondAccelDt(eZ) = 1 - 2.0*(double(rand())/double(RAND_MAX));
413 MagnitudedAccelDt = 1 - 2.0*(double(rand())/double(RAND_MAX)) - Magnitude;
414 MagnitudeAccel += MagnitudedAccelDt*rate*State->Getdt();
415 Magnitude += MagnitudeAccel*rate*State->Getdt();
417 vDirectiondAccelDt.Normalize();
418 vDirectionAccel += vDirectiondAccelDt*rate*State->Getdt();
419 vDirectionAccel.Normalize();
420 vDirection += vDirectionAccel*rate*State->Getdt();
422 // Diminish z-vector within two wingspans
424 double HOverBMAC = Auxiliary->GetHOverBMAC();
426 vDirection(eZ) *= HOverBMAC / 2.0;
428 vDirection.Normalize();
430 vTurbulence = TurbGain*Magnitude * vDirection;
431 vTurbulenceGrad = TurbGain*MagnitudeAccel * vDirection;
433 vBodyTurbGrad = Propagate->GetTl2b()*vTurbulenceGrad;
434 vTurbPQR(eP) = vBodyTurbGrad(eY)/Aircraft->GetWingSpan();
435 if (Aircraft->GetHTailArm() > 0)
436 vTurbPQR(eQ) = vBodyTurbGrad(eZ)/Aircraft->GetHTailArm();
438 vTurbPQR(eQ) = vBodyTurbGrad(eZ)/10.0;
440 if (Aircraft->GetVTailArm() > 0)
441 vTurbPQR(eR) = vBodyTurbGrad(eX)/Aircraft->GetVTailArm();
443 vTurbPQR(eR) = vBodyTurbGrad(eX)/10.0;
449 vTurbPQR(eP) = wind_from_clockwise;
450 if (TurbGain == 0.0) return;
452 // keep the inputs within allowable limts for this model
453 if (TurbGain < 0.0) TurbGain = 0.0;
454 if (TurbGain > 1.0) TurbGain = 1.0;
455 if (TurbRate < 0.0) TurbRate = 0.0;
456 if (TurbRate > 30.0) TurbRate = 30.0;
457 if (Rhythmicity < 0.0) Rhythmicity = 0.0;
458 if (Rhythmicity > 1.0) Rhythmicity = 1.0;
460 // generate a sine wave corresponding to turbulence rate in hertz
461 double time = FDMExec->GetSimTime();
462 double sinewave = sin( time * TurbRate * 6.283185307 );
465 if (target_time == 0.0) {
466 strength = random = 1 - 2.0*(double(rand())/double(RAND_MAX));
467 target_time = time + 0.71 + (random * 0.5);
469 if (time > target_time) {
474 // max vertical wind speed in fps, corresponds to TurbGain = 1.0
477 vTurbulence(1) = vTurbulence(2) = vTurbulence(3) = 0.0;
478 double delta = strength * max_vs * TurbGain * (1-Rhythmicity) * spike;
480 // Vertical component of turbulence.
481 vTurbulence(3) = sinewave * max_vs * TurbGain * Rhythmicity;
482 vTurbulence(3)+= delta;
483 double HOverBMAC = Auxiliary->GetHOverBMAC();
485 vTurbulence(3) *= HOverBMAC * 0.3333;
487 // Yaw component of turbulence.
488 vTurbulence(1) = sin( delta * 3.0 );
489 vTurbulence(2) = cos( delta * 3.0 );
491 // Roll component of turbulence. Clockwise vortex causes left roll.
492 vTurbPQR(eP) += delta * 0.04;
502 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
504 void FGAtmosphere::UseExternal(void)
506 temperature=&exTemperature;
507 pressure=&exPressure;
512 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
514 void FGAtmosphere::UseInternal(void)
516 temperature=&intTemperature;
517 pressure=&intPressure;
522 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
524 void FGAtmosphere::bind(void)
526 typedef double (FGAtmosphere::*PMF)(int) const;
527 typedef double (FGAtmosphere::*PMFv)(void) const;
528 typedef void (FGAtmosphere::*PMFd)(int,double);
529 PropertyManager->Tie("atmosphere/T-R", this, (PMFv)&FGAtmosphere::GetTemperature);
530 PropertyManager->Tie("atmosphere/rho-slugs_ft3", this, (PMFv)&FGAtmosphere::GetDensity);
531 PropertyManager->Tie("atmosphere/P-psf", this, (PMFv)&FGAtmosphere::GetPressure);
532 PropertyManager->Tie("atmosphere/a-fps", this, &FGAtmosphere::GetSoundSpeed);
533 PropertyManager->Tie("atmosphere/T-sl-R", this, &FGAtmosphere::GetTemperatureSL);
534 PropertyManager->Tie("atmosphere/rho-sl-slugs_ft3", this, &FGAtmosphere::GetDensitySL);
535 PropertyManager->Tie("atmosphere/P-sl-psf", this, &FGAtmosphere::GetPressureSL);
536 PropertyManager->Tie("atmosphere/a-sl-fps", this, &FGAtmosphere::GetSoundSpeedSL);
537 PropertyManager->Tie("atmosphere/theta", this, &FGAtmosphere::GetTemperatureRatio);
538 PropertyManager->Tie("atmosphere/sigma", this, &FGAtmosphere::GetDensityRatio);
539 PropertyManager->Tie("atmosphere/delta", this, &FGAtmosphere::GetPressureRatio);
540 PropertyManager->Tie("atmosphere/a-ratio", this, &FGAtmosphere::GetSoundSpeedRatio);
541 PropertyManager->Tie("atmosphere/psiw-rad", this, &FGAtmosphere::GetWindPsi);
542 PropertyManager->Tie("atmosphere/delta-T", this, &FGAtmosphere::GetDeltaT, &FGAtmosphere::SetDeltaT);
543 PropertyManager->Tie("atmosphere/T-sl-dev-F", this, &FGAtmosphere::GetSLTempDev, &FGAtmosphere::SetSLTempDev);
544 PropertyManager->Tie("atmosphere/density-altitude", this, &FGAtmosphere::GetDensityAltitude);
545 PropertyManager->Tie("atmosphere/p-turb-rad_sec", this,1, (PMF)&FGAtmosphere::GetTurbPQR);
546 PropertyManager->Tie("atmosphere/q-turb-rad_sec", this,2, (PMF)&FGAtmosphere::GetTurbPQR);
547 PropertyManager->Tie("atmosphere/r-turb-rad_sec", this,3, (PMF)&FGAtmosphere::GetTurbPQR);
548 PropertyManager->Tie("atmosphere/turb-rate", this, &FGAtmosphere::GetTurbRate, &FGAtmosphere::SetTurbRate);
549 PropertyManager->Tie("atmosphere/turb-gain", this, &FGAtmosphere::GetTurbGain, &FGAtmosphere::SetTurbGain);
550 PropertyManager->Tie("atmosphere/turb-rhythmicity", this, &FGAtmosphere::GetRhythmicity,
551 &FGAtmosphere::SetRhythmicity);
552 PropertyManager->Tie("atmosphere/gust-north-fps", this,1, (PMF)&FGAtmosphere::GetGustNED,
553 (PMFd)&FGAtmosphere::SetGustNED);
554 PropertyManager->Tie("atmosphere/gust-east-fps", this,2, (PMF)&FGAtmosphere::GetGustNED,
555 (PMFd)&FGAtmosphere::SetGustNED);
556 PropertyManager->Tie("atmosphere/gust-down-fps", this,3, (PMF)&FGAtmosphere::GetGustNED,
557 (PMFd)&FGAtmosphere::SetGustNED);
558 PropertyManager->Tie("atmosphere/wind-from-cw", this, &FGAtmosphere::GetWindFromClockwise,
559 &FGAtmosphere::SetWindFromClockwise);
562 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
563 // The bitmasked value choices are as follows:
564 // unset: In this case (the default) JSBSim would only print
565 // out the normally expected messages, essentially echoing
566 // the config files as they are read. If the environment
567 // variable is not set, debug_lvl is set to 1 internally
568 // 0: This requests JSBSim not to output any messages
570 // 1: This value explicity requests the normal JSBSim
572 // 2: This value asks for a message to be printed out when
573 // a class is instantiated
574 // 4: When this value is set, a message is displayed when a
575 // FGModel object executes its Run() method
576 // 8: When this value is set, various runtime state variables
577 // are printed out periodically
578 // 16: When set various parameters are sanity checked and
579 // a message is printed out when they go out of bounds
581 void FGAtmosphere::Debug(int from)
583 if (debug_lvl <= 0) return;
585 if (debug_lvl & 1) { // Standard console startup message output
586 if (from == 0) { // Constructor
589 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
590 if (from == 0) cout << "Instantiated: FGAtmosphere" << endl;
591 if (from == 1) cout << "Destroyed: FGAtmosphere" << endl;
593 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
595 if (debug_lvl & 8 ) { // Runtime state variables
597 if (debug_lvl & 16) { // Sanity checking
599 if (debug_lvl & 128) { // Turbulence
600 if (first_pass && from == 2) {
602 cout << "vTurbulence(X), vTurbulence(Y), vTurbulence(Z), "
603 << "vTurbulenceGrad(X), vTurbulenceGrad(Y), vTurbulenceGrad(Z), "
604 << "vDirection(X), vDirection(Y), vDirection(Z), "
606 << "vTurbPQR(P), vTurbPQR(Q), vTurbPQR(R), " << endl;
609 cout << vTurbulence << ", " << vTurbulenceGrad << ", " << vDirection << ", " << Magnitude << ", " << vTurbPQR << endl;
612 if (debug_lvl & 64) {
613 if (from == 0) { // Constructor
614 cout << IdSrc << endl;
615 cout << IdHdr << endl;
620 } // namespace JSBSim