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"
54 #include "FGAircraft.h"
55 #include "FGTranslation.h"
56 #include "FGRotation.h"
57 #include "FGPosition.h"
58 #include "FGAuxiliary.h"
60 #include "FGMatrix33.h"
61 #include "FGColumnVector3.h"
62 #include "FGColumnVector4.h"
63 #include "FGPropertyManager.h"
67 static const char *IdSrc = "$Id$";
68 static const char *IdHdr = ID_ATMOSPHERE;
70 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
72 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
75 FGAtmosphere::FGAtmosphere(FGFDMExec* fdmex) : FGModel(fdmex)
77 Name = "FGAtmosphere";
88 htab[7]=259186.352; //ft.
90 MagnitudedAccelDt = MagnitudeAccel = Magnitude = 0.0;
92 turbType = ttStandard;
93 // turbType = ttBerndt;
101 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
103 FGAtmosphere::~FGAtmosphere()
109 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
111 bool FGAtmosphere::InitModel(void)
113 FGModel::InitModel();
116 SLtemperature = intTemperature;
117 SLpressure = intPressure;
118 SLdensity = intDensity;
119 SLsoundspeed = sqrt(SHRatio*Reng*intTemperature);
120 rSLtemperature = 1.0/intTemperature;
121 rSLpressure = 1.0/intPressure;
122 rSLdensity = 1.0/intDensity;
123 rSLsoundspeed = 1.0/SLsoundspeed;
124 temperature=&intTemperature;
125 pressure=&intPressure;
133 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
135 bool FGAtmosphere::Run(void)
137 if (!FGModel::Run()) { // if false then execute this Run()
138 //do temp, pressure, and density first
140 h = Position->Geth();
144 if (turbType != ttNone) {
146 vWindNED += vTurbulence;
149 if (vWindNED(1) != 0.0) psiw = atan2( vWindNED(2), vWindNED(1) );
151 if (psiw < 0) psiw += 2*M_PI;
153 soundspeed = sqrt(SHRatio*Reng*(*temperature));
155 State->Seta(soundspeed);
160 } else { // skip Run() execution this time
165 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
169 void FGAtmosphere::Calculate(double altitude)
171 double slope, reftemp, refpress;
175 if (altitude < htab[lastIndex]) {
181 while (htab[i] > altitude) i--;
183 } else if (altitude > htab[lastIndex+1]) {
184 if (altitude >= htab[7]) {
189 while (htab[i+1] < altitude) i++;
198 //refdens = 0.000706032;
204 //refdens = 0.000171306;
206 case 3: // 104986 ft.
210 //refdens = 1.18422e-05;
212 case 4: // 154199 ft.
216 //refdens = 4.00585e-7;
218 case 5: // 170603 ft.
222 //refdens = 8.17102e-7;
224 case 6: // 200131 ft.
227 refpress = 0.00684986;
228 //refdens = 8.77702e-9;
230 case 7: // 259186 ft.
233 refpress = 0.000122276;
234 //refdens = 2.19541e-10;
237 default: // sea level
238 slope = -0.00356616; // R/ft.
239 reftemp = 518.67; // R
240 refpress = 2116.22; // psf
241 //refdens = 0.00237767; // slugs/cubic ft.
247 intTemperature = reftemp;
248 intPressure = refpress*exp(-Inertial->SLgravity()/(reftemp*Reng)*(altitude-htab[i]));
249 //intDensity = refdens*exp(-Inertial->SLgravity()/(reftemp*Reng)*(altitude-htab[i]));
250 intDensity = intPressure/(Reng*intTemperature);
252 intTemperature = reftemp+slope*(altitude-htab[i]);
253 intPressure = refpress*pow(intTemperature/reftemp,-Inertial->SLgravity()/(slope*Reng));
254 //intDensity = refdens*pow(intTemperature/reftemp,-(Inertial->SLgravity()/(slope*Reng)+1));
255 intDensity = intPressure/(Reng*intTemperature);
258 //cout << "Atmosphere: h=" << altitude << " rho= " << intDensity << endl;
261 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
263 void FGAtmosphere::Turbulence(void)
267 vDirectiondAccelDt(eX) = 1 - 2.0*(double(rand())/double(RAND_MAX));
268 vDirectiondAccelDt(eY) = 1 - 2.0*(double(rand())/double(RAND_MAX));
269 vDirectiondAccelDt(eZ) = 1 - 2.0*(double(rand())/double(RAND_MAX));
271 MagnitudedAccelDt = 1 - 2.0*(double(rand())/double(RAND_MAX)) - Magnitude;
272 // Scale the magnitude so that it moves
273 // away from the peaks
274 MagnitudedAccelDt = ((MagnitudedAccelDt - Magnitude) /
275 (1 + fabs(Magnitude)));
276 MagnitudeAccel += MagnitudedAccelDt*rate*TurbRate*State->Getdt();
277 Magnitude += MagnitudeAccel*rate*State->Getdt();
279 vDirectiondAccelDt.Normalize();
280 vDirectiondAccelDt(eY) *= vDirectiondAccelDt(eY);
281 vDirectiondAccelDt(eZ) *= vDirectiondAccelDt(eZ);
282 vDirectionAccel += vDirectiondAccelDt*rate*TurbRate*State->Getdt();
283 vDirectionAccel.Normalize();
284 vDirection += vDirectionAccel*rate*State->Getdt();
286 vDirection.Normalize();
288 // Diminish turbulence within three wingspans
290 vTurbulence = TurbGain * Magnitude * vDirection;
291 double HOverBMAC = Position->GetHOverBMAC();
293 vTurbulence *= (HOverBMAC / 3.0) * (HOverBMAC / 3.0);
295 vTurbulenceGrad = TurbGain*MagnitudeAccel * vDirection;
297 vBodyTurbGrad = State->GetTl2b()*vTurbulenceGrad;
298 vTurbPQR(eP) = vBodyTurbGrad(eY)/Aircraft->GetWingSpan();
299 // if (Aircraft->GetHTailArm() != 0.0)
300 // vTurbPQR(eQ) = vBodyTurbGrad(eZ)/Aircraft->GetHTailArm();
302 // vTurbPQR(eQ) = vBodyTurbGrad(eZ)/10.0;
304 if (Aircraft->GetVTailArm())
305 vTurbPQR(eR) = vBodyTurbGrad(eX)/Aircraft->GetVTailArm();
307 vTurbPQR(eR) = vBodyTurbGrad(eX)/10.0;
309 // Clear the horizontal forces
310 // actually felt by the plane, now
311 // that we've used them to calculate
313 vTurbulence(eX) = 0.0;
314 vTurbulence(eY) = 0.0;
319 vDirectiondAccelDt(eX) = 1 - 2.0*(double(rand())/double(RAND_MAX));
320 vDirectiondAccelDt(eY) = 1 - 2.0*(double(rand())/double(RAND_MAX));
321 vDirectiondAccelDt(eZ) = 1 - 2.0*(double(rand())/double(RAND_MAX));
324 MagnitudedAccelDt = 1 - 2.0*(double(rand())/double(RAND_MAX)) - Magnitude;
325 MagnitudeAccel += MagnitudedAccelDt*rate*State->Getdt();
326 Magnitude += MagnitudeAccel*rate*State->Getdt();
328 vDirectiondAccelDt.Normalize();
329 vDirectionAccel += vDirectiondAccelDt*rate*State->Getdt();
330 vDirectionAccel.Normalize();
331 vDirection += vDirectionAccel*rate*State->Getdt();
333 // Diminish z-vector within two wingspans
335 double HOverBMAC = Position->GetHOverBMAC();
337 vDirection(eZ) *= HOverBMAC / 2.0;
339 vDirection.Normalize();
341 vTurbulence = TurbGain*Magnitude * vDirection;
342 vTurbulenceGrad = TurbGain*MagnitudeAccel * vDirection;
344 vBodyTurbGrad = State->GetTl2b()*vTurbulenceGrad;
345 vTurbPQR(eP) = vBodyTurbGrad(eY)/Aircraft->GetWingSpan();
346 if (Aircraft->GetHTailArm() != 0.0)
347 vTurbPQR(eQ) = vBodyTurbGrad(eZ)/Aircraft->GetHTailArm();
349 vTurbPQR(eQ) = vBodyTurbGrad(eZ)/10.0;
351 if (Aircraft->GetVTailArm())
352 vTurbPQR(eR) = vBodyTurbGrad(eX)/Aircraft->GetVTailArm();
354 vTurbPQR(eR) = vBodyTurbGrad(eX)/10.0;
363 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
365 void FGAtmosphere::UseExternal(void) {
366 temperature=&exTemperature;
367 pressure=&exPressure;
372 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
374 void FGAtmosphere::UseInternal(void) {
375 temperature=&intTemperature;
376 pressure=&intPressure;
382 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
384 void FGAtmosphere::bind(void)
386 typedef double (FGAtmosphere::*PMF)(int) const;
387 PropertyManager->Tie("atmosphere/T-R", this,
388 &FGAtmosphere::GetTemperature);
389 PropertyManager->Tie("atmosphere/rho-slugs_ft3", this,
390 &FGAtmosphere::GetDensity);
391 PropertyManager->Tie("atmosphere/P-psf", this,
392 &FGAtmosphere::GetPressure);
393 PropertyManager->Tie("atmosphere/a-fps", this,
394 &FGAtmosphere::GetSoundSpeed);
395 PropertyManager->Tie("atmosphere/T-sl-R", this,
396 &FGAtmosphere::GetTemperatureSL);
397 PropertyManager->Tie("atmosphere/rho-sl-slugs_ft3", this,
398 &FGAtmosphere::GetDensitySL);
399 PropertyManager->Tie("atmosphere/P-sl-psf", this,
400 &FGAtmosphere::GetPressureSL);
401 PropertyManager->Tie("atmosphere/a-sl-fps", this,
402 &FGAtmosphere::GetSoundSpeedSL);
403 PropertyManager->Tie("atmosphere/theta-norm", this,
404 &FGAtmosphere::GetTemperatureRatio);
405 PropertyManager->Tie("atmosphere/sigma-norm", this,
406 &FGAtmosphere::GetDensityRatio);
407 PropertyManager->Tie("atmosphere/delta-norm", this,
408 &FGAtmosphere::GetPressureRatio);
409 PropertyManager->Tie("atmosphere/a-norm", this,
410 &FGAtmosphere::GetSoundSpeedRatio);
411 PropertyManager->Tie("atmosphere/psiw-rad", this,
412 &FGAtmosphere::GetWindPsi);
413 PropertyManager->Tie("atmosphere/p-turb-rad_sec", this,1,
414 (PMF)&FGAtmosphere::GetTurbPQR);
415 PropertyManager->Tie("atmosphere/q-turb-rad_sec", this,2,
416 (PMF)&FGAtmosphere::GetTurbPQR);
417 PropertyManager->Tie("atmosphere/r-turb-rad_sec", this,3,
418 (PMF)&FGAtmosphere::GetTurbPQR);
421 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
423 void FGAtmosphere::unbind(void)
425 PropertyManager->Untie("atmosphere/T-R");
426 PropertyManager->Untie("atmosphere/rho-slugs_ft3");
427 PropertyManager->Untie("atmosphere/P-psf");
428 PropertyManager->Untie("atmosphere/a-fps");
429 PropertyManager->Untie("atmosphere/T-sl-R");
430 PropertyManager->Untie("atmosphere/rho-sl-slugs_ft3");
431 PropertyManager->Untie("atmosphere/P-sl-psf");
432 PropertyManager->Untie("atmosphere/a-sl-fps");
433 PropertyManager->Untie("atmosphere/theta-norm");
434 PropertyManager->Untie("atmosphere/sigma-norm");
435 PropertyManager->Untie("atmosphere/delta-norm");
436 PropertyManager->Untie("atmosphere/a-norm");
437 PropertyManager->Untie("atmosphere/psiw-rad");
438 PropertyManager->Untie("atmosphere/p-turb-rad_sec");
439 PropertyManager->Untie("atmosphere/q-turb-rad_sec");
440 PropertyManager->Untie("atmosphere/r-turb-rad_sec");
443 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
444 // The bitmasked value choices are as follows:
445 // unset: In this case (the default) JSBSim would only print
446 // out the normally expected messages, essentially echoing
447 // the config files as they are read. If the environment
448 // variable is not set, debug_lvl is set to 1 internally
449 // 0: This requests JSBSim not to output any messages
451 // 1: This value explicity requests the normal JSBSim
453 // 2: This value asks for a message to be printed out when
454 // a class is instantiated
455 // 4: When this value is set, a message is displayed when a
456 // FGModel object executes its Run() method
457 // 8: When this value is set, various runtime state variables
458 // are printed out periodically
459 // 16: When set various parameters are sanity checked and
460 // a message is printed out when they go out of bounds
462 void FGAtmosphere::Debug(int from)
464 if (debug_lvl <= 0) return;
466 if (debug_lvl & 1) { // Standard console startup message output
467 if (from == 0) { // Constructor
470 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
471 if (from == 0) cout << "Instantiated: FGAtmosphere" << endl;
472 if (from == 1) cout << "Destroyed: FGAtmosphere" << endl;
474 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
476 if (debug_lvl & 8 ) { // Runtime state variables
478 if (debug_lvl & 16) { // Sanity checking
480 if (debug_lvl & 32) { // Turbulence
481 if (frame == 0 && from == 2) {
482 cout << "vTurbulence(X), vTurbulence(Y), vTurbulence(Z), "
483 << "vTurbulenceGrad(X), vTurbulenceGrad(Y), vTurbulenceGrad(Z), "
484 << "vDirection(X), vDirection(Y), vDirection(Z), "
486 << "vTurbPQR(P), vTurbPQR(Q), vTurbPQR(R), " << endl;
487 } else if (from == 2) {
488 cout << vTurbulence << ", " << vTurbulenceGrad << ", " << vDirection << ", " << Magnitude << ", " << vTurbPQR << endl;
491 if (debug_lvl & 64) {
492 if (from == 0) { // Constructor
493 cout << IdSrc << endl;
494 cout << IdHdr << endl;
499 } // namespace JSBSim