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 FGTranslation
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 "FGPosition.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;
94 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
96 FGAtmosphere::~FGAtmosphere()
102 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
104 bool FGAtmosphere::InitModel(void)
106 FGModel::InitModel();
109 SLtemperature = intTemperature;
110 SLpressure = intPressure;
111 SLdensity = intDensity;
112 SLsoundspeed = sqrt(SHRatio*Reng*intTemperature);
113 rSLtemperature = 1.0/intTemperature;
114 rSLpressure = 1.0/intPressure;
115 rSLdensity = 1.0/intDensity;
116 rSLsoundspeed = 1.0/SLsoundspeed;
117 temperature=&intTemperature;
118 pressure=&intPressure;
126 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
128 bool FGAtmosphere::Run(void)
130 if (!FGModel::Run()) { // if false then execute this Run()
131 //do temp, pressure, and density first
133 h = Position->Geth();
137 if (turbType != ttNone) {
139 vWindNED += vTurbulence;
142 if (vWindNED(1) != 0.0) psiw = atan2( vWindNED(2), vWindNED(1) );
144 if (psiw < 0) psiw += 2*M_PI;
146 soundspeed = sqrt(SHRatio*Reng*(*temperature));
151 } else { // skip Run() execution this time
156 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
160 void FGAtmosphere::Calculate(double altitude)
162 double slope, reftemp, refpress;
166 if (altitude < htab[lastIndex]) {
172 while (htab[i] > altitude) i--;
174 } else if (altitude > htab[lastIndex+1]) {
175 if (altitude >= htab[7]) {
180 while (htab[i+1] < altitude) i++;
189 //refdens = 0.000706032;
195 //refdens = 0.000171306;
197 case 3: // 104986 ft.
201 //refdens = 1.18422e-05;
203 case 4: // 154199 ft.
207 //refdens = 4.00585e-7;
209 case 5: // 170603 ft.
213 //refdens = 8.17102e-7;
215 case 6: // 200131 ft.
218 refpress = 0.00684986;
219 //refdens = 8.77702e-9;
221 case 7: // 259186 ft.
224 refpress = 0.000122276;
225 //refdens = 2.19541e-10;
228 default: // sea level
229 slope = -0.00356616; // R/ft.
230 reftemp = 518.67; // R
231 refpress = 2116.22; // psf
232 //refdens = 0.00237767; // slugs/cubic ft.
238 intTemperature = reftemp;
239 intPressure = refpress*exp(-Inertial->SLgravity()/(reftemp*Reng)*(altitude-htab[i]));
240 //intDensity = refdens*exp(-Inertial->SLgravity()/(reftemp*Reng)*(altitude-htab[i]));
241 intDensity = intPressure/(Reng*intTemperature);
243 intTemperature = reftemp+slope*(altitude-htab[i]);
244 intPressure = refpress*pow(intTemperature/reftemp,-Inertial->SLgravity()/(slope*Reng));
245 //intDensity = refdens*pow(intTemperature/reftemp,-(Inertial->SLgravity()/(slope*Reng)+1));
246 intDensity = intPressure/(Reng*intTemperature);
249 //cout << "Atmosphere: h=" << altitude << " rho= " << intDensity << endl;
252 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
254 // square a value, but preserve the original sign
256 square_signed (double value)
259 return value * value * -1;
261 return value * value;
264 void FGAtmosphere::Turbulence(void)
268 vDirectiondAccelDt(eX) = 1 - 2.0*(double(rand())/double(RAND_MAX));
269 vDirectiondAccelDt(eY) = 1 - 2.0*(double(rand())/double(RAND_MAX));
270 vDirectiondAccelDt(eZ) = 1 - 2.0*(double(rand())/double(RAND_MAX));
272 MagnitudedAccelDt = 1 - 2.0*(double(rand())/double(RAND_MAX)) - Magnitude;
273 // Scale the magnitude so that it moves
274 // away from the peaks
275 MagnitudedAccelDt = ((MagnitudedAccelDt - Magnitude) /
276 (1 + fabs(Magnitude)));
277 MagnitudeAccel += MagnitudedAccelDt*rate*TurbRate*State->Getdt();
278 Magnitude += MagnitudeAccel*rate*State->Getdt();
280 vDirectiondAccelDt.Normalize();
282 // deemphasise non-vertical forces
283 vDirectiondAccelDt(eX) = square_signed(vDirectiondAccelDt(eX));
284 vDirectiondAccelDt(eY) = square_signed(vDirectiondAccelDt(eY));
286 vDirectionAccel += vDirectiondAccelDt*rate*TurbRate*State->Getdt();
287 vDirectionAccel.Normalize();
288 vDirection += vDirectionAccel*rate*State->Getdt();
290 vDirection.Normalize();
292 // Diminish turbulence within three wingspans
294 vTurbulence = TurbGain * Magnitude * vDirection;
295 double HOverBMAC = Position->GetHOverBMAC();
297 vTurbulence *= (HOverBMAC / 3.0) * (HOverBMAC / 3.0);
299 vTurbulenceGrad = TurbGain*MagnitudeAccel * vDirection;
301 vBodyTurbGrad = State->GetTl2b()*vTurbulenceGrad;
303 if (Aircraft->GetWingSpan() > 0) {
304 vTurbPQR(eP) = vBodyTurbGrad(eY)/Aircraft->GetWingSpan();
306 vTurbPQR(eP) = vBodyTurbGrad(eY)/30.0;
308 // if (Aircraft->GetHTailArm() != 0.0)
309 // vTurbPQR(eQ) = vBodyTurbGrad(eZ)/Aircraft->GetHTailArm();
311 // vTurbPQR(eQ) = vBodyTurbGrad(eZ)/10.0;
313 if (Aircraft->GetVTailArm() > 0)
314 vTurbPQR(eR) = vBodyTurbGrad(eX)/Aircraft->GetVTailArm();
316 vTurbPQR(eR) = vBodyTurbGrad(eX)/10.0;
318 // Clear the horizontal forces
319 // actually felt by the plane, now
320 // that we've used them to calculate
322 vTurbulence(eX) = 0.0;
323 vTurbulence(eY) = 0.0;
328 vDirectiondAccelDt(eX) = 1 - 2.0*(double(rand())/double(RAND_MAX));
329 vDirectiondAccelDt(eY) = 1 - 2.0*(double(rand())/double(RAND_MAX));
330 vDirectiondAccelDt(eZ) = 1 - 2.0*(double(rand())/double(RAND_MAX));
333 MagnitudedAccelDt = 1 - 2.0*(double(rand())/double(RAND_MAX)) - Magnitude;
334 MagnitudeAccel += MagnitudedAccelDt*rate*State->Getdt();
335 Magnitude += MagnitudeAccel*rate*State->Getdt();
337 vDirectiondAccelDt.Normalize();
338 vDirectionAccel += vDirectiondAccelDt*rate*State->Getdt();
339 vDirectionAccel.Normalize();
340 vDirection += vDirectionAccel*rate*State->Getdt();
342 // Diminish z-vector within two wingspans
344 double HOverBMAC = Position->GetHOverBMAC();
346 vDirection(eZ) *= HOverBMAC / 2.0;
348 vDirection.Normalize();
350 vTurbulence = TurbGain*Magnitude * vDirection;
351 vTurbulenceGrad = TurbGain*MagnitudeAccel * vDirection;
353 vBodyTurbGrad = State->GetTl2b()*vTurbulenceGrad;
354 vTurbPQR(eP) = vBodyTurbGrad(eY)/Aircraft->GetWingSpan();
355 if (Aircraft->GetHTailArm() > 0)
356 vTurbPQR(eQ) = vBodyTurbGrad(eZ)/Aircraft->GetHTailArm();
358 vTurbPQR(eQ) = vBodyTurbGrad(eZ)/10.0;
360 if (Aircraft->GetVTailArm() > 0)
361 vTurbPQR(eR) = vBodyTurbGrad(eX)/Aircraft->GetVTailArm();
363 vTurbPQR(eR) = vBodyTurbGrad(eX)/10.0;
372 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
374 void FGAtmosphere::UseExternal(void) {
375 temperature=&exTemperature;
376 pressure=&exPressure;
381 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
383 void FGAtmosphere::UseInternal(void) {
384 temperature=&intTemperature;
385 pressure=&intPressure;
391 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
393 void FGAtmosphere::bind(void)
395 typedef double (FGAtmosphere::*PMF)(int) const;
396 PropertyManager->Tie("atmosphere/T-R", this,
397 &FGAtmosphere::GetTemperature);
398 PropertyManager->Tie("atmosphere/rho-slugs_ft3", this,
399 &FGAtmosphere::GetDensity);
400 PropertyManager->Tie("atmosphere/P-psf", this,
401 &FGAtmosphere::GetPressure);
402 PropertyManager->Tie("atmosphere/a-fps", this,
403 &FGAtmosphere::GetSoundSpeed);
404 PropertyManager->Tie("atmosphere/T-sl-R", this,
405 &FGAtmosphere::GetTemperatureSL);
406 PropertyManager->Tie("atmosphere/rho-sl-slugs_ft3", this,
407 &FGAtmosphere::GetDensitySL);
408 PropertyManager->Tie("atmosphere/P-sl-psf", this,
409 &FGAtmosphere::GetPressureSL);
410 PropertyManager->Tie("atmosphere/a-sl-fps", this,
411 &FGAtmosphere::GetSoundSpeedSL);
412 PropertyManager->Tie("atmosphere/theta-norm", this,
413 &FGAtmosphere::GetTemperatureRatio);
414 PropertyManager->Tie("atmosphere/sigma-norm", this,
415 &FGAtmosphere::GetDensityRatio);
416 PropertyManager->Tie("atmosphere/delta-norm", this,
417 &FGAtmosphere::GetPressureRatio);
418 PropertyManager->Tie("atmosphere/a-norm", this,
419 &FGAtmosphere::GetSoundSpeedRatio);
420 PropertyManager->Tie("atmosphere/psiw-rad", this,
421 &FGAtmosphere::GetWindPsi);
422 PropertyManager->Tie("atmosphere/p-turb-rad_sec", this,1,
423 (PMF)&FGAtmosphere::GetTurbPQR);
424 PropertyManager->Tie("atmosphere/q-turb-rad_sec", this,2,
425 (PMF)&FGAtmosphere::GetTurbPQR);
426 PropertyManager->Tie("atmosphere/r-turb-rad_sec", this,3,
427 (PMF)&FGAtmosphere::GetTurbPQR);
430 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
432 void FGAtmosphere::unbind(void)
434 PropertyManager->Untie("atmosphere/T-R");
435 PropertyManager->Untie("atmosphere/rho-slugs_ft3");
436 PropertyManager->Untie("atmosphere/P-psf");
437 PropertyManager->Untie("atmosphere/a-fps");
438 PropertyManager->Untie("atmosphere/T-sl-R");
439 PropertyManager->Untie("atmosphere/rho-sl-slugs_ft3");
440 PropertyManager->Untie("atmosphere/P-sl-psf");
441 PropertyManager->Untie("atmosphere/a-sl-fps");
442 PropertyManager->Untie("atmosphere/theta-norm");
443 PropertyManager->Untie("atmosphere/sigma-norm");
444 PropertyManager->Untie("atmosphere/delta-norm");
445 PropertyManager->Untie("atmosphere/a-norm");
446 PropertyManager->Untie("atmosphere/psiw-rad");
447 PropertyManager->Untie("atmosphere/p-turb-rad_sec");
448 PropertyManager->Untie("atmosphere/q-turb-rad_sec");
449 PropertyManager->Untie("atmosphere/r-turb-rad_sec");
452 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
453 // The bitmasked value choices are as follows:
454 // unset: In this case (the default) JSBSim would only print
455 // out the normally expected messages, essentially echoing
456 // the config files as they are read. If the environment
457 // variable is not set, debug_lvl is set to 1 internally
458 // 0: This requests JSBSim not to output any messages
460 // 1: This value explicity requests the normal JSBSim
462 // 2: This value asks for a message to be printed out when
463 // a class is instantiated
464 // 4: When this value is set, a message is displayed when a
465 // FGModel object executes its Run() method
466 // 8: When this value is set, various runtime state variables
467 // are printed out periodically
468 // 16: When set various parameters are sanity checked and
469 // a message is printed out when they go out of bounds
471 void FGAtmosphere::Debug(int from)
473 if (debug_lvl <= 0) return;
475 if (debug_lvl & 1) { // Standard console startup message output
476 if (from == 0) { // Constructor
479 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
480 if (from == 0) cout << "Instantiated: FGAtmosphere" << endl;
481 if (from == 1) cout << "Destroyed: FGAtmosphere" << endl;
483 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
485 if (debug_lvl & 8 ) { // Runtime state variables
487 if (debug_lvl & 16) { // Sanity checking
489 if (debug_lvl & 32) { // Turbulence
490 if (frame == 0 && from == 2) {
491 cout << "vTurbulence(X), vTurbulence(Y), vTurbulence(Z), "
492 << "vTurbulenceGrad(X), vTurbulenceGrad(Y), vTurbulenceGrad(Z), "
493 << "vDirection(X), vDirection(Y), vDirection(Z), "
495 << "vTurbPQR(P), vTurbPQR(Q), vTurbPQR(R), " << endl;
496 } else if (from == 2) {
497 cout << vTurbulence << ", " << vTurbulenceGrad << ", " << vDirection << ", " << Magnitude << ", " << vTurbPQR << endl;
500 if (debug_lvl & 64) {
501 if (from == 0) { // Constructor
502 cout << IdSrc << endl;
503 cout << IdHdr << endl;
508 } // namespace JSBSim