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;
99 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
101 FGAtmosphere::~FGAtmosphere()
107 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
109 bool FGAtmosphere::InitModel(void)
111 FGModel::InitModel();
114 SLtemperature = intTemperature;
115 SLpressure = intPressure;
116 SLdensity = intDensity;
117 SLsoundspeed = sqrt(SHRatio*Reng*intTemperature);
118 rSLtemperature = 1.0/intTemperature;
119 rSLpressure = 1.0/intPressure;
120 rSLdensity = 1.0/intDensity;
121 rSLsoundspeed = 1.0/SLsoundspeed;
122 temperature=&intTemperature;
123 pressure=&intPressure;
131 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
133 bool FGAtmosphere::Run(void)
135 if (!FGModel::Run()) { // if false then execute this Run()
136 //do temp, pressure, and density first
138 h = Position->Geth();
142 if (turbType != ttNone) {
144 vWindNED += vTurbulence;
147 if (vWindNED(1) != 0.0) psiw = atan2( vWindNED(2), vWindNED(1) );
149 if (psiw < 0) psiw += 2*M_PI;
151 soundspeed = sqrt(SHRatio*Reng*(*temperature));
153 State->Seta(soundspeed);
158 } else { // skip Run() execution this time
163 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
167 void FGAtmosphere::Calculate(double altitude)
169 double slope, reftemp, refpress;
173 if (altitude < htab[lastIndex]) {
179 while (htab[i] > altitude) i--;
181 } else if (altitude > htab[lastIndex+1]) {
182 if (altitude >= htab[7]) {
187 while (htab[i+1] < altitude) i++;
196 //refdens = 0.000706032;
202 //refdens = 0.000171306;
204 case 3: // 104986 ft.
208 //refdens = 1.18422e-05;
210 case 4: // 154199 ft.
214 //refdens = 4.00585e-7;
216 case 5: // 170603 ft.
220 //refdens = 8.17102e-7;
222 case 6: // 200131 ft.
225 refpress = 0.00684986;
226 //refdens = 8.77702e-9;
228 case 7: // 259186 ft.
231 refpress = 0.000122276;
232 //refdens = 2.19541e-10;
235 default: // sea level
236 slope = -0.00356616; // R/ft.
237 reftemp = 518.67; // R
238 refpress = 2116.22; // psf
239 //refdens = 0.00237767; // slugs/cubic ft.
245 intTemperature = reftemp;
246 intPressure = refpress*exp(-Inertial->SLgravity()/(reftemp*Reng)*(altitude-htab[i]));
247 //intDensity = refdens*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 = refdens*pow(intTemperature/reftemp,-(Inertial->SLgravity()/(slope*Reng)+1));
253 intDensity = intPressure/(Reng*intTemperature);
256 //cout << "Atmosphere: h=" << altitude << " rho= " << intDensity << endl;
259 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
261 void FGAtmosphere::Turbulence(void)
265 vDirectiondAccelDt(eX) = 1 - 2.0*(((double)(rand()))/RAND_MAX);
266 vDirectiondAccelDt(eY) = 1 - 2.0*(((double)(rand()))/RAND_MAX);
267 vDirectiondAccelDt(eZ) = 1 - 2.0*(((double)(rand()))/RAND_MAX);
270 MagnitudedAccelDt = 1 - 2.0*(((double)(rand()))/RAND_MAX) - Magnitude;
271 MagnitudeAccel += MagnitudedAccelDt*rate*State->Getdt();
272 Magnitude += MagnitudeAccel*rate*State->Getdt();
274 vDirectiondAccelDt.Normalize();
275 vDirectionAccel += vDirectiondAccelDt*rate*State->Getdt();
276 vDirectionAccel.Normalize();
277 vDirection += vDirectionAccel*rate*State->Getdt();
279 // Diminish z-vector within two wingspans
281 double HOverBMAC = Position->GetHOverBMAC();
283 vDirection(eZ) *= HOverBMAC / 2.0;
285 vDirection.Normalize();
287 vTurbulence = TurbGain*Magnitude * vDirection;
288 vTurbulenceGrad = TurbGain*MagnitudeAccel * vDirection;
290 vBodyTurbGrad = State->GetTl2b()*vTurbulenceGrad;
291 vTurbPQR(eP) = vBodyTurbGrad(eY)/Aircraft->GetWingSpan();
292 if (Aircraft->GetHTailArm() != 0.0)
293 vTurbPQR(eQ) = vBodyTurbGrad(eZ)/Aircraft->GetHTailArm();
295 vTurbPQR(eQ) = vBodyTurbGrad(eZ)/10.0;
297 if (Aircraft->GetVTailArm())
298 vTurbPQR(eR) = vBodyTurbGrad(eX)/Aircraft->GetVTailArm();
300 vTurbPQR(eR) = vBodyTurbGrad(eX)/10.0;
309 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
311 void FGAtmosphere::UseExternal(void) {
312 temperature=&exTemperature;
313 pressure=&exPressure;
318 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
320 void FGAtmosphere::UseInternal(void) {
321 temperature=&intTemperature;
322 pressure=&intPressure;
328 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
330 void FGAtmosphere::bind(void)
332 typedef double (FGAtmosphere::*PMF)(int) const;
333 PropertyManager->Tie("atmosphere/T-R", this,
334 &FGAtmosphere::GetTemperature);
335 PropertyManager->Tie("atmosphere/rho-slugs_ft3", this,
336 &FGAtmosphere::GetDensity);
337 PropertyManager->Tie("atmosphere/P-psf", this,
338 &FGAtmosphere::GetPressure);
339 PropertyManager->Tie("atmosphere/a-fps", this,
340 &FGAtmosphere::GetSoundSpeed);
341 PropertyManager->Tie("atmosphere/T-sl-R", this,
342 &FGAtmosphere::GetTemperatureSL);
343 PropertyManager->Tie("atmosphere/rho-sl-slugs_ft3", this,
344 &FGAtmosphere::GetDensitySL);
345 PropertyManager->Tie("atmosphere/P-sl-psf", this,
346 &FGAtmosphere::GetPressureSL);
347 PropertyManager->Tie("atmosphere/a-sl-fps", this,
348 &FGAtmosphere::GetSoundSpeedSL);
349 PropertyManager->Tie("atmosphere/theta-norm", this,
350 &FGAtmosphere::GetTemperatureRatio);
351 PropertyManager->Tie("atmosphere/sigma-norm", this,
352 &FGAtmosphere::GetDensityRatio);
353 PropertyManager->Tie("atmosphere/delta-norm", this,
354 &FGAtmosphere::GetPressureRatio);
355 PropertyManager->Tie("atmosphere/a-norm", this,
356 &FGAtmosphere::GetSoundSpeedRatio);
357 PropertyManager->Tie("atmosphere/psiw-rad", this,
358 &FGAtmosphere::GetWindPsi);
359 PropertyManager->Tie("atmosphere/p-turb-rad_sec", this,1,
360 (PMF)&FGAtmosphere::GetTurbPQR);
361 PropertyManager->Tie("atmosphere/q-turb-rad_sec", this,2,
362 (PMF)&FGAtmosphere::GetTurbPQR);
363 PropertyManager->Tie("atmosphere/r-turb-rad_sec", this,3,
364 (PMF)&FGAtmosphere::GetTurbPQR);
367 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
369 void FGAtmosphere::unbind(void)
371 PropertyManager->Untie("atmosphere/T-R");
372 PropertyManager->Untie("atmosphere/rho-slugs_ft3");
373 PropertyManager->Untie("atmosphere/P-psf");
374 PropertyManager->Untie("atmosphere/a-fps");
375 PropertyManager->Untie("atmosphere/T-sl-R");
376 PropertyManager->Untie("atmosphere/rho-sl-slugs_ft3");
377 PropertyManager->Untie("atmosphere/P-sl-psf");
378 PropertyManager->Untie("atmosphere/a-sl-fps");
379 PropertyManager->Untie("atmosphere/theta-norm");
380 PropertyManager->Untie("atmosphere/sigma-norm");
381 PropertyManager->Untie("atmosphere/delta-norm");
382 PropertyManager->Untie("atmosphere/a-norm");
383 PropertyManager->Untie("atmosphere/psiw-rad");
384 PropertyManager->Untie("atmosphere/p-turb-rad_sec");
385 PropertyManager->Untie("atmosphere/q-turb-rad_sec");
386 PropertyManager->Untie("atmosphere/r-turb-rad_sec");
389 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
390 // The bitmasked value choices are as follows:
391 // unset: In this case (the default) JSBSim would only print
392 // out the normally expected messages, essentially echoing
393 // the config files as they are read. If the environment
394 // variable is not set, debug_lvl is set to 1 internally
395 // 0: This requests JSBSim not to output any messages
397 // 1: This value explicity requests the normal JSBSim
399 // 2: This value asks for a message to be printed out when
400 // a class is instantiated
401 // 4: When this value is set, a message is displayed when a
402 // FGModel object executes its Run() method
403 // 8: When this value is set, various runtime state variables
404 // are printed out periodically
405 // 16: When set various parameters are sanity checked and
406 // a message is printed out when they go out of bounds
408 void FGAtmosphere::Debug(int from)
410 if (debug_lvl <= 0) return;
412 if (debug_lvl & 1) { // Standard console startup message output
413 if (from == 0) { // Constructor
416 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
417 if (from == 0) cout << "Instantiated: FGAtmosphere" << endl;
418 if (from == 1) cout << "Destroyed: FGAtmosphere" << endl;
420 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
422 if (debug_lvl & 8 ) { // Runtime state variables
424 if (debug_lvl & 16) { // Sanity checking
426 if (debug_lvl & 32) { // Turbulence
427 if (frame == 0 && from == 2) {
428 cout << "vTurbulence(X), vTurbulence(Y), vTurbulence(Z), "
429 << "vTurbulenceGrad(X), vTurbulenceGrad(Y), vTurbulenceGrad(Z), "
430 << "vDirection(X), vDirection(Y), vDirection(Z), "
432 << "vTurbPQR(P), vTurbPQR(Q), vTurbPQR(R), " << endl;
433 } else if (from == 2) {
434 cout << vTurbulence << ", " << vTurbulenceGrad << ", " << vDirection << ", " << Magnitude << ", " << vTurbPQR << endl;
437 if (debug_lvl & 64) {
438 if (from == 0) { // Constructor
439 cout << IdSrc << endl;
440 cout << IdHdr << endl;
445 } // namespace JSBSim