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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
40 COMMENTS, REFERENCES, and NOTES
41 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 [1] Anderson, John D. "Introduction to Flight, Third Edition", McGraw-Hill,
43 1989, ISBN 0-07-001641-0
45 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
47 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
49 #include "FGAtmosphere.h"
51 #include "FGFDMExec.h"
53 #include "FGAircraft.h"
54 #include "FGTranslation.h"
55 #include "FGRotation.h"
56 #include "FGPosition.h"
57 #include "FGAuxiliary.h"
59 #include "FGMatrix33.h"
60 #include "FGColumnVector3.h"
61 #include "FGColumnVector4.h"
62 #include "FGPropertyManager.h"
64 static const char *IdSrc = "$Id$";
65 static const char *IdHdr = ID_ATMOSPHERE;
67 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
69 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
72 FGAtmosphere::FGAtmosphere(FGFDMExec* fdmex) : FGModel(fdmex)
74 Name = "FGAtmosphere";
85 htab[7]=259186.352; //ft.
87 MagnitudedAccelDt = MagnitudeAccel = Magnitude = 0.0;
89 // turbType = ttBerndt; // temporarily disable turbulence until fully tested
96 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
98 FGAtmosphere::~FGAtmosphere()
104 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
106 bool FGAtmosphere::InitModel(void)
108 FGModel::InitModel();
111 SLtemperature = temperature;
112 SLpressure = pressure;
114 SLsoundspeed = sqrt(SHRatio*Reng*temperature);
115 rSLtemperature = 1.0/temperature;
116 rSLpressure = 1.0/pressure;
117 rSLdensity = 1.0/density;
118 rSLsoundspeed = 1.0/SLsoundspeed;
124 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
126 bool FGAtmosphere::Run(void)
128 if (!FGModel::Run()) { // if false then execute this Run()
129 //do temp, pressure, and density first
131 h = Position->Geth();
135 pressure = exPressure;
136 temperature = exTemperature;
139 if (turbType != ttNone) {
141 vWindNED += vTurbulence;
144 if (vWindNED(1) != 0.0) psiw = atan2( vWindNED(2), vWindNED(1) );
146 if (psiw < 0) psiw += 2*M_PI;
148 soundspeed = sqrt(SHRatio*Reng*temperature);
150 State->Seta(soundspeed);
154 } else { // skip Run() execution this time
160 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
164 void FGAtmosphere::Calculate(double altitude)
166 double slope, reftemp, refpress;
170 if (altitude < htab[lastIndex]) {
176 while (htab[i] > altitude) i--;
178 } else if (altitude > htab[lastIndex+1]) {
179 if (altitude >= htab[7]) {
184 while (htab[i+1] < altitude) i++;
193 //refdens = 0.000706032;
199 //refdens = 0.000171306;
201 case 3: // 104986 ft.
205 //refdens = 1.18422e-05;
207 case 4: // 154199 ft.
211 //refdens = 4.00585e-7;
213 case 5: // 170603 ft.
217 //refdens = 8.17102e-7;
219 case 6: // 200131 ft.
222 refpress = 0.00684986;
223 //refdens = 8.77702e-9;
225 case 7: // 259186 ft.
228 refpress = 0.000122276;
229 //refdens = 2.19541e-10;
232 default: // sea level
233 slope = -0.00356616; // R/ft.
234 reftemp = 518.67; // R
235 refpress = 2116.22; // psf
236 //refdens = 0.00237767; // slugs/cubic ft.
242 temperature = reftemp;
243 pressure = refpress*exp(-Inertial->SLgravity()/(reftemp*Reng)*(altitude-htab[i]));
244 //density = refdens*exp(-Inertial->SLgravity()/(reftemp*Reng)*(altitude-htab[i]));
245 density = pressure/(Reng*temperature);
247 temperature = reftemp+slope*(altitude-htab[i]);
248 pressure = refpress*pow(temperature/reftemp,-Inertial->SLgravity()/(slope*Reng));
249 //density = refdens*pow(temperature/reftemp,-(Inertial->SLgravity()/(slope*Reng)+1));
250 density = pressure/(Reng*temperature);
253 //cout << "Atmosphere: h=" << altitude << " rho= " << density << endl;
256 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
258 void FGAtmosphere::Turbulence(void)
262 vDirectiondAccelDt(eX) = 1 - 2.0*(((double)(rand()))/RAND_MAX);
263 vDirectiondAccelDt(eY) = 1 - 2.0*(((double)(rand()))/RAND_MAX);
264 vDirectiondAccelDt(eZ) = 1 - 2.0*(((double)(rand()))/RAND_MAX);
266 MagnitudedAccelDt = 1 - 2.0*(((double)(rand()))/RAND_MAX);
267 MagnitudeAccel += MagnitudedAccelDt*rate*State->Getdt();
268 Magnitude += MagnitudeAccel*rate*State->Getdt();
270 vDirectiondAccelDt.Normalize();
271 vDirectionAccel += vDirectiondAccelDt*rate*State->Getdt();
272 vDirectionAccel.Normalize();
273 vDirection += vDirectionAccel*rate*State->Getdt();
274 vDirection.Normalize();
276 vTurbulence = TurbGain*Magnitude * vDirection;
277 vTurbulenceGrad = TurbGain*MagnitudeAccel * vDirection;
279 vBodyTurbGrad = State->GetTl2b()*vTurbulenceGrad;
280 vTurbPQR(eP) = vBodyTurbGrad(eY)/Aircraft->GetWingSpan();
281 if (Aircraft->GetHTailArm() != 0.0)
282 vTurbPQR(eQ) = vBodyTurbGrad(eZ)/Aircraft->GetHTailArm();
284 vTurbPQR(eQ) = vBodyTurbGrad(eZ)/10.0;
286 if (Aircraft->GetVTailArm())
287 vTurbPQR(eR) = vBodyTurbGrad(eX)/Aircraft->GetVTailArm();
289 vTurbPQR(eR) = vBodyTurbGrad(eX)/10.0;
297 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
299 void FGAtmosphere::bind(void)
301 PropertyManager->Tie("atmosphere/T-R", this,
302 &FGAtmosphere::GetTemperature);
303 PropertyManager->Tie("atmosphere/rho-slugs_ft3", this,
304 &FGAtmosphere::GetDensity);
305 PropertyManager->Tie("atmosphere/P-psf", this,
306 &FGAtmosphere::GetPressure);
307 PropertyManager->Tie("atmosphere/a-fps", this,
308 &FGAtmosphere::GetSoundSpeed);
309 PropertyManager->Tie("atmosphere/T-sl-R", this,
310 &FGAtmosphere::GetTemperatureSL);
311 PropertyManager->Tie("atmosphere/rho-sl-slugs_ft3", this,
312 &FGAtmosphere::GetDensitySL);
313 PropertyManager->Tie("atmosphere/P-sl-psf", this,
314 &FGAtmosphere::GetPressureSL);
315 PropertyManager->Tie("atmosphere/a-sl-fps", this,
316 &FGAtmosphere::GetSoundSpeedSL);
317 PropertyManager->Tie("atmosphere/theta-norm", this,
318 &FGAtmosphere::GetTemperatureRatio);
319 PropertyManager->Tie("atmosphere/sigma-norm", this,
320 &FGAtmosphere::GetDensityRatio);
321 PropertyManager->Tie("atmosphere/delta-norm", this,
322 &FGAtmosphere::GetPressureRatio);
323 PropertyManager->Tie("atmosphere/a-norm", this,
324 &FGAtmosphere::GetSoundSpeedRatio);
325 PropertyManager->Tie("atmosphere/psiw-rad", this,
326 &FGAtmosphere::GetWindPsi);
327 PropertyManager->Tie("atmosphere/p-turb-rad_sec", this,1,
328 &FGAtmosphere::GetTurbPQR);
329 PropertyManager->Tie("atmosphere/q-turb-rad_sec", this,2,
330 &FGAtmosphere::GetTurbPQR);
331 PropertyManager->Tie("atmosphere/r-turb-rad_sec", this,3,
332 &FGAtmosphere::GetTurbPQR);
335 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
337 void FGAtmosphere::unbind(void)
339 PropertyManager->Untie("atmosphere/T-R");
340 PropertyManager->Untie("atmosphere/rho-slugs_ft3");
341 PropertyManager->Untie("atmosphere/P-psf");
342 PropertyManager->Untie("atmosphere/a-fps");
343 PropertyManager->Untie("atmosphere/T-sl-R");
344 PropertyManager->Untie("atmosphere/rho-sl-slugs_ft3");
345 PropertyManager->Untie("atmosphere/P-sl-psf");
346 PropertyManager->Untie("atmosphere/a-sl-fps");
347 PropertyManager->Untie("atmosphere/theta-norm");
348 PropertyManager->Untie("atmosphere/sigma-norm");
349 PropertyManager->Untie("atmosphere/delta-norm");
350 PropertyManager->Untie("atmosphere/a-norm");
351 PropertyManager->Untie("atmosphere/psiw-rad");
352 PropertyManager->Untie("atmosphere/p-turb-rad_sec");
353 PropertyManager->Untie("atmosphere/q-turb-rad_sec");
354 PropertyManager->Untie("atmosphere/r-turb-rad_sec");
357 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
358 // The bitmasked value choices are as follows:
359 // unset: In this case (the default) JSBSim would only print
360 // out the normally expected messages, essentially echoing
361 // the config files as they are read. If the environment
362 // variable is not set, debug_lvl is set to 1 internally
363 // 0: This requests JSBSim not to output any messages
365 // 1: This value explicity requests the normal JSBSim
367 // 2: This value asks for a message to be printed out when
368 // a class is instantiated
369 // 4: When this value is set, a message is displayed when a
370 // FGModel object executes its Run() method
371 // 8: When this value is set, various runtime state variables
372 // are printed out periodically
373 // 16: When set various parameters are sanity checked and
374 // a message is printed out when they go out of bounds
376 void FGAtmosphere::Debug(int from)
378 if (debug_lvl <= 0) return;
380 if (debug_lvl & 1) { // Standard console startup message output
381 if (from == 0) { // Constructor
384 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
385 if (from == 0) cout << "Instantiated: FGAtmosphere" << endl;
386 if (from == 1) cout << "Destroyed: FGAtmosphere" << endl;
388 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
390 if (debug_lvl & 8 ) { // Runtime state variables
392 if (debug_lvl & 16) { // Sanity checking
394 if (debug_lvl & 32) { // Turbulence
395 if (frame == 0 && from == 2) {
396 cout << "vTurbulence(X), vTurbulence(Y), vTurbulence(Z), "
397 << "vTurbulenceGrad(X), vTurbulenceGrad(Y), vTurbulenceGrad(Z), "
398 << "vDirection(X), vDirection(Y), vDirection(Z), "
400 << "vTurbPQR(P), vTurbPQR(Q), vTurbPQR(R), " << endl;
401 } else if (from == 2) {
402 cout << vTurbulence << ", " << vTurbulenceGrad << ", " << vDirection << ", " << Magnitude << ", " << vTurbPQR << endl;
405 if (debug_lvl & 64) {
406 if (from == 0) { // Constructor
407 cout << IdSrc << endl;
408 cout << IdHdr << endl;