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"
63 static const char *IdSrc = "$Id$";
64 static const char *IdHdr = ID_ATMOSPHERE;
66 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
68 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
71 FGAtmosphere::FGAtmosphere(FGFDMExec* fdmex) : FGModel(fdmex),
73 vDirectiondAccelDt(3),
81 Name = "FGAtmosphere";
91 htab[7]=259186.352; //ft.
93 MagnitudedAccelDt = MagnitudeAccel = Magnitude = 0.0;
95 turbType = ttBerndt; // temporarily disable turbulence until fully tested
98 if (debug_lvl & 2) cout << "Instantiated: " << Name << endl;
101 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
103 FGAtmosphere::~FGAtmosphere()
105 if (debug_lvl & 2) cout << "Destroyed: FGAtmosphere" << endl;
108 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
110 bool FGAtmosphere::InitModel(void)
112 FGModel::InitModel();
115 SLtemperature = temperature;
116 SLpressure = pressure;
118 SLsoundspeed = sqrt(SHRatio*Reng*temperature);
119 rSLtemperature = 1.0/temperature;
120 rSLpressure = 1.0/pressure;
121 rSLdensity = 1.0/density;
122 rSLsoundspeed = 1.0/SLsoundspeed;
128 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
130 bool FGAtmosphere::Run(void)
132 if (!FGModel::Run()) { // if false then execute this Run()
133 //do temp, pressure, and density first
135 h = Position->Geth();
139 pressure = exPressure;
140 temperature = exTemperature;
143 if (turbType != ttNone) {
145 vWindNED += vTurbulence;
148 if (vWindNED(1) != 0.0) psiw = atan2( vWindNED(2), vWindNED(1) );
150 if (psiw < 0) psiw += 2*M_PI;
152 soundspeed = sqrt(SHRatio*Reng*temperature);
154 State->Seta(soundspeed);
156 if (debug_lvl > 1) Debug();
158 } else { // skip Run() execution this time
164 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
168 void FGAtmosphere::Calculate(double altitude)
170 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++;
195 slope = -0.00356616; // R/ft.
196 reftemp = 518.67; // R
197 refpress = 2116.22; // psf
198 //refdens = 0.00237767; // slugs/cubic ft.
204 //refdens = 0.000706032;
210 //refdens = 0.000171306;
212 case 3: // 104986 ft.
216 //refdens = 1.18422e-05;
218 case 4: // 154199 ft.
222 //refdens = 4.00585e-7;
224 case 5: // 170603 ft.
228 //refdens = 8.17102e-7;
230 case 6: // 200131 ft.
233 refpress = 0.00684986;
234 //refdens = 8.77702e-9;
236 case 7: // 259186 ft.
239 refpress = 0.000122276;
240 //refdens = 2.19541e-10;
245 temperature = reftemp;
246 pressure = refpress*exp(-Inertial->SLgravity()/(reftemp*Reng)*(altitude-htab[i]));
247 //density = refdens*exp(-Inertial->SLgravity()/(reftemp*Reng)*(altitude-htab[i]));
248 density = pressure/(Reng*temperature);
250 temperature = reftemp+slope*(altitude-htab[i]);
251 pressure = refpress*pow(temperature/reftemp,-Inertial->SLgravity()/(slope*Reng));
252 //density = refdens*pow(temperature/reftemp,-(Inertial->SLgravity()/(slope*Reng)+1));
253 density = pressure/(Reng*temperature);
256 //cout << "Atmosphere: h=" << altitude << " rho= " << density << 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);
269 MagnitudedAccelDt = 1 - 2.0*(((double)(rand()))/RAND_MAX);
270 MagnitudeAccel += MagnitudedAccelDt*rate*State->Getdt();
271 Magnitude += MagnitudeAccel*rate*State->Getdt();
273 vDirectiondAccelDt.Normalize();
274 vDirectionAccel += vDirectiondAccelDt*rate*State->Getdt();
275 vDirectionAccel.Normalize();
276 vDirection += vDirectionAccel*rate*State->Getdt();
277 vDirection.Normalize();
279 vTurbulence = TurbGain*Magnitude * vDirection;
280 vTurbulenceGrad = TurbGain*MagnitudeAccel * vDirection;
282 vBodyTurbGrad = State->GetTl2b()*vTurbulenceGrad;
283 vTurbPQR(eP) = vBodyTurbGrad(eY)/Aircraft->GetWingSpan();
284 if (Aircraft->GetHTailArm() != 0.0)
285 vTurbPQR(eQ) = vBodyTurbGrad(eZ)/Aircraft->GetHTailArm();
287 vTurbPQR(eQ) = vBodyTurbGrad(eZ)/10.0;
289 if (Aircraft->GetVTailArm())
290 vTurbPQR(eR) = vBodyTurbGrad(eX)/Aircraft->GetVTailArm();
292 vTurbPQR(eR) = vBodyTurbGrad(eX)/10.0;
300 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
302 void FGAtmosphere::Debug(void)
305 cout << "vTurbulence(X), vTurbulence(Y), vTurbulence(Z), "
306 << "vTurbulenceGrad(X), vTurbulenceGrad(Y), vTurbulenceGrad(Z), "
307 << "vDirection(X), vDirection(Y), vDirection(Z), "
309 << "vTurbPQR(P), vTurbPQR(Q), vTurbPQR(R), " << endl;
311 cout << vTurbulence << ", " << vTurbulenceGrad << ", " << vDirection << ", " << Magnitude << ", " << vTurbPQR << endl;