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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
67 FGAtmosphere::FGAtmosphere(FGFDMExec* fdmex) : FGModel(fdmex)
69 Name = "FGAtmosphere";
80 htab[7]=259186.352; //ft.
82 MagnitudedAccelDt = MagnitudeAccel = Magnitude = 0.0;
83 SetTurbType( ttCulp );
87 spike = target_time = strength = 0.0;
88 wind_from_clockwise = 0.0;
90 T_dev_sl = T_dev = delta_T = 0.0;
91 StandardTempOnly = false;
93 vGustNED(1) = vGustNED(2) = vGustNED(3) = 0.0; bgustSet = false;
94 vTurbulence(1) = vTurbulence(2) = vTurbulence(3) = 0.0;
100 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
102 FGAtmosphere::~FGAtmosphere()
107 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
109 bool FGAtmosphere::InitModel(void)
111 if (!FGModel::InitModel()) return false;
113 UseInternal(); // this is the default
116 StdSLtemperature = SLtemperature = 518.67;
117 StdSLpressure = SLpressure = 2116.22;
118 StdSLdensity = SLdensity = 0.00237767;
119 StdSLsoundspeed = SLsoundspeed = sqrt(SHRatio*Reng*StdSLtemperature);
120 rSLtemperature = 1.0/StdSLtemperature;
121 rSLpressure = 1.0/StdSLpressure;
122 rSLdensity = 1.0/StdSLdensity;
123 rSLsoundspeed = 1.0/StdSLsoundspeed;
128 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
130 bool FGAtmosphere::Run(void)
132 if (FGModel::Run()) return true;
133 if (FDMExec->Holding()) return false;
136 h = Propagate->Geth();
149 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
153 void FGAtmosphere::Calculate(double altitude)
155 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 || ttCulp) {
267 vWindNED += vGustNED + 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 TurbGain = TurbGain * TurbGain * 100.0;
335 vDirectiondAccelDt(eX) = 1 - 2.0*(double(rand())/double(RAND_MAX));
336 vDirectiondAccelDt(eY) = 1 - 2.0*(double(rand())/double(RAND_MAX));
337 vDirectiondAccelDt(eZ) = 1 - 2.0*(double(rand())/double(RAND_MAX));
339 MagnitudedAccelDt = 1 - 2.0*(double(rand())/double(RAND_MAX)) - Magnitude;
340 // Scale the magnitude so that it moves
341 // away from the peaks
342 MagnitudedAccelDt = ((MagnitudedAccelDt - Magnitude) /
343 (1 + fabs(Magnitude)));
344 MagnitudeAccel += MagnitudedAccelDt*rate*TurbRate*State->Getdt();
345 Magnitude += MagnitudeAccel*rate*State->Getdt();
346 Magnitude = fabs(Magnitude);
348 vDirectiondAccelDt.Normalize();
350 // deemphasise non-vertical forces
351 vDirectiondAccelDt(eX) = square_signed(vDirectiondAccelDt(eX));
352 vDirectiondAccelDt(eY) = square_signed(vDirectiondAccelDt(eY));
354 vDirectionAccel += vDirectiondAccelDt*rate*TurbRate*State->Getdt();
355 vDirectionAccel.Normalize();
356 vDirection += vDirectionAccel*rate*State->Getdt();
358 vDirection.Normalize();
360 // Diminish turbulence within three wingspans
362 vTurbulence = TurbGain * Magnitude * vDirection;
363 double HOverBMAC = Auxiliary->GetHOverBMAC();
365 vTurbulence *= (HOverBMAC / 3.0) * (HOverBMAC / 3.0);
367 // I don't believe these next two statements calculate the proper gradient over
368 // the aircraft body. One reason is because this has no relationship with the
369 // orientation or velocity of the aircraft, which it must have. What is vTurbulenceGrad
370 // supposed to represent? And the direction and magnitude of the turbulence can change,
371 // so both accelerations need to be accounted for, no?
373 // Need to determine the turbulence change in body axes between two time points.
375 vTurbulenceGrad = TurbGain*MagnitudeAccel * vDirection;
376 vBodyTurbGrad = Propagate->GetTl2b()*vTurbulenceGrad;
378 if (Aircraft->GetWingSpan() > 0) {
379 vTurbPQR(eP) = vBodyTurbGrad(eY)/Aircraft->GetWingSpan();
381 vTurbPQR(eP) = vBodyTurbGrad(eY)/30.0;
383 // if (Aircraft->GetHTailArm() != 0.0)
384 // vTurbPQR(eQ) = vBodyTurbGrad(eZ)/Aircraft->GetHTailArm();
386 // vTurbPQR(eQ) = vBodyTurbGrad(eZ)/10.0;
388 if (Aircraft->GetVTailArm() > 0)
389 vTurbPQR(eR) = vBodyTurbGrad(eX)/Aircraft->GetVTailArm();
391 vTurbPQR(eR) = vBodyTurbGrad(eX)/10.0;
393 // Clear the horizontal forces
394 // actually felt by the plane, now
395 // that we've used them to calculate
398 // vTurbulence(eX) = 0.0;
399 // vTurbulence(eY) = 0.0;
403 case ttBerndt: { // This is very experimental and incomplete at the moment.
405 TurbGain = TurbGain * TurbGain * 100.0;
407 vDirectiondAccelDt(eX) = 1 - 2.0*(double(rand())/double(RAND_MAX));
408 vDirectiondAccelDt(eY) = 1 - 2.0*(double(rand())/double(RAND_MAX));
409 vDirectiondAccelDt(eZ) = 1 - 2.0*(double(rand())/double(RAND_MAX));
412 MagnitudedAccelDt = 1 - 2.0*(double(rand())/double(RAND_MAX)) - Magnitude;
413 MagnitudeAccel += MagnitudedAccelDt*rate*State->Getdt();
414 Magnitude += MagnitudeAccel*rate*State->Getdt();
416 vDirectiondAccelDt.Normalize();
417 vDirectionAccel += vDirectiondAccelDt*rate*State->Getdt();
418 vDirectionAccel.Normalize();
419 vDirection += vDirectionAccel*rate*State->Getdt();
421 // Diminish z-vector within two wingspans
423 double HOverBMAC = Auxiliary->GetHOverBMAC();
425 vDirection(eZ) *= HOverBMAC / 2.0;
427 vDirection.Normalize();
429 vTurbulence = TurbGain*Magnitude * vDirection;
430 vTurbulenceGrad = TurbGain*MagnitudeAccel * vDirection;
432 vBodyTurbGrad = Propagate->GetTl2b()*vTurbulenceGrad;
433 vTurbPQR(eP) = vBodyTurbGrad(eY)/Aircraft->GetWingSpan();
434 if (Aircraft->GetHTailArm() > 0)
435 vTurbPQR(eQ) = vBodyTurbGrad(eZ)/Aircraft->GetHTailArm();
437 vTurbPQR(eQ) = vBodyTurbGrad(eZ)/10.0;
439 if (Aircraft->GetVTailArm() > 0)
440 vTurbPQR(eR) = vBodyTurbGrad(eX)/Aircraft->GetVTailArm();
442 vTurbPQR(eR) = vBodyTurbGrad(eX)/10.0;
448 vTurbPQR(eP) = wind_from_clockwise;
449 if (TurbGain == 0.0) return;
451 // keep the inputs within allowable limts for this model
452 if (TurbGain < 0.0) TurbGain = 0.0;
453 if (TurbGain > 1.0) TurbGain = 1.0;
454 if (TurbRate < 0.0) TurbRate = 0.0;
455 if (TurbRate > 30.0) TurbRate = 30.0;
456 if (Rhythmicity < 0.0) Rhythmicity = 0.0;
457 if (Rhythmicity > 1.0) Rhythmicity = 1.0;
459 // generate a sine wave corresponding to turbulence rate in hertz
460 double time = FDMExec->GetSimTime();
461 double sinewave = sin( time * TurbRate * 6.283185307 );
464 if (target_time == 0.0) {
465 strength = random = 1 - 2.0*(double(rand())/double(RAND_MAX));
466 target_time = time + 0.71 + (random * 0.5);
468 if (time > target_time) {
473 // max vertical wind speed in fps, corresponds to TurbGain = 1.0
476 vTurbulence(1) = vTurbulence(2) = vTurbulence(3) = 0.0;
477 double delta = strength * max_vs * TurbGain * (1-Rhythmicity) * spike;
479 // Vertical component of turbulence.
480 vTurbulence(3) = sinewave * max_vs * TurbGain * Rhythmicity;
481 vTurbulence(3)+= delta;
482 double HOverBMAC = Auxiliary->GetHOverBMAC();
484 vTurbulence(3) *= HOverBMAC * 0.3333;
486 // Yaw component of turbulence.
487 vTurbulence(1) = sin( delta * 3.0 );
488 vTurbulence(2) = cos( delta * 3.0 );
490 // Roll component of turbulence. Clockwise vortex causes left roll.
491 vTurbPQR(eP) += delta * 0.04;
501 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
503 void FGAtmosphere::UseExternal(void)
505 temperature=&exTemperature;
506 pressure=&exPressure;
511 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
513 void FGAtmosphere::UseInternal(void)
515 temperature=&intTemperature;
516 pressure=&intPressure;
521 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
523 void FGAtmosphere::bind(void)
525 typedef double (FGAtmosphere::*PMF)(int) const;
526 typedef double (FGAtmosphere::*PMFv)(void) const;
527 typedef void (FGAtmosphere::*PMFd)(int,double);
528 PropertyManager->Tie("atmosphere/T-R", this, (PMFv)&FGAtmosphere::GetTemperature);
529 PropertyManager->Tie("atmosphere/rho-slugs_ft3", this, (PMFv)&FGAtmosphere::GetDensity);
530 PropertyManager->Tie("atmosphere/P-psf", this, (PMFv)&FGAtmosphere::GetPressure);
531 PropertyManager->Tie("atmosphere/a-fps", this, &FGAtmosphere::GetSoundSpeed);
532 PropertyManager->Tie("atmosphere/T-sl-R", this, &FGAtmosphere::GetTemperatureSL);
533 PropertyManager->Tie("atmosphere/rho-sl-slugs_ft3", this, &FGAtmosphere::GetDensitySL);
534 PropertyManager->Tie("atmosphere/P-sl-psf", this, &FGAtmosphere::GetPressureSL);
535 PropertyManager->Tie("atmosphere/a-sl-fps", this, &FGAtmosphere::GetSoundSpeedSL);
536 PropertyManager->Tie("atmosphere/theta", this, &FGAtmosphere::GetTemperatureRatio);
537 PropertyManager->Tie("atmosphere/sigma", this, &FGAtmosphere::GetDensityRatio);
538 PropertyManager->Tie("atmosphere/delta", this, &FGAtmosphere::GetPressureRatio);
539 PropertyManager->Tie("atmosphere/a-ratio", this, &FGAtmosphere::GetSoundSpeedRatio);
540 PropertyManager->Tie("atmosphere/psiw-rad", this, &FGAtmosphere::GetWindPsi);
541 PropertyManager->Tie("atmosphere/delta-T", this, &FGAtmosphere::GetDeltaT, &FGAtmosphere::SetDeltaT);
542 PropertyManager->Tie("atmosphere/T-sl-dev-F", this, &FGAtmosphere::GetSLTempDev, &FGAtmosphere::SetSLTempDev);
543 PropertyManager->Tie("atmosphere/density-altitude", this, &FGAtmosphere::GetDensityAltitude);
544 PropertyManager->Tie("atmosphere/p-turb-rad_sec", this,1, (PMF)&FGAtmosphere::GetTurbPQR);
545 PropertyManager->Tie("atmosphere/q-turb-rad_sec", this,2, (PMF)&FGAtmosphere::GetTurbPQR);
546 PropertyManager->Tie("atmosphere/r-turb-rad_sec", this,3, (PMF)&FGAtmosphere::GetTurbPQR);
547 PropertyManager->Tie("atmosphere/turb-rate", this, &FGAtmosphere::GetTurbRate, &FGAtmosphere::SetTurbRate);
548 PropertyManager->Tie("atmosphere/turb-gain", this, &FGAtmosphere::GetTurbGain, &FGAtmosphere::SetTurbGain);
549 PropertyManager->Tie("atmosphere/turb-rhythmicity", this, &FGAtmosphere::GetRhythmicity,
550 &FGAtmosphere::SetRhythmicity);
551 PropertyManager->Tie("atmosphere/gust-north-fps", this,1, (PMF)&FGAtmosphere::GetGustNED,
552 (PMFd)&FGAtmosphere::SetGustNED);
553 PropertyManager->Tie("atmosphere/gust-east-fps", this,2, (PMF)&FGAtmosphere::GetGustNED,
554 (PMFd)&FGAtmosphere::SetGustNED);
555 PropertyManager->Tie("atmosphere/gust-down-fps", this,3, (PMF)&FGAtmosphere::GetGustNED,
556 (PMFd)&FGAtmosphere::SetGustNED);
557 PropertyManager->Tie("atmosphere/wind-from-cw", this, &FGAtmosphere::GetWindFromClockwise,
558 &FGAtmosphere::SetWindFromClockwise);
561 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
562 // The bitmasked value choices are as follows:
563 // unset: In this case (the default) JSBSim would only print
564 // out the normally expected messages, essentially echoing
565 // the config files as they are read. If the environment
566 // variable is not set, debug_lvl is set to 1 internally
567 // 0: This requests JSBSim not to output any messages
569 // 1: This value explicity requests the normal JSBSim
571 // 2: This value asks for a message to be printed out when
572 // a class is instantiated
573 // 4: When this value is set, a message is displayed when a
574 // FGModel object executes its Run() method
575 // 8: When this value is set, various runtime state variables
576 // are printed out periodically
577 // 16: When set various parameters are sanity checked and
578 // a message is printed out when they go out of bounds
580 void FGAtmosphere::Debug(int from)
582 if (debug_lvl <= 0) return;
584 if (debug_lvl & 1) { // Standard console startup message output
585 if (from == 0) { // Constructor
588 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
589 if (from == 0) cout << "Instantiated: FGAtmosphere" << endl;
590 if (from == 1) cout << "Destroyed: FGAtmosphere" << endl;
592 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
594 if (debug_lvl & 8 ) { // Runtime state variables
596 if (debug_lvl & 16) { // Sanity checking
598 if (debug_lvl & 128) { // Turbulence
599 if (first_pass && from == 2) {
601 cout << "vTurbulence(X), vTurbulence(Y), vTurbulence(Z), "
602 << "vTurbulenceGrad(X), vTurbulenceGrad(Y), vTurbulenceGrad(Z), "
603 << "vDirection(X), vDirection(Y), vDirection(Z), "
605 << "vTurbPQR(P), vTurbPQR(Q), vTurbPQR(R), " << endl;
608 cout << vTurbulence << ", " << vTurbulenceGrad << ", " << vDirection << ", " << Magnitude << ", " << vTurbPQR << endl;
611 if (debug_lvl & 64) {
612 if (from == 0) { // Constructor
613 cout << IdSrc << endl;
614 cout << IdHdr << endl;
619 } // namespace JSBSim