]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGAtmosphere.cpp
Updates from Jon.
[flightgear.git] / src / FDM / JSBSim / FGAtmosphere.cpp
1 /*******************************************************************************
2
3  Module:       FGAtmosphere.cpp
4  Author:       Jon Berndt
5                Implementation of 1959 Standard Atmosphere added by Tony Peden 
6  Date started: 11/24/98
7  Purpose:      Models the atmosphere
8  Called by:    FGSimExec
9
10  ------------- Copyright (C) 1999  Jon S. Berndt (jsb@hal-pc.org) -------------
11
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
15  version.
16
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
20  details.
21
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.
25
26  Further information about the GNU General Public License can also be found on
27  the world wide web at http://www.gnu.org.
28
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.
33
34 HISTORY
35 --------------------------------------------------------------------------------
36 11/24/98   JSB   Created
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
44
45 ********************************************************************************
46 INCLUDES
47 *******************************************************************************/
48
49 #include "FGAtmosphere.h"
50 #include "FGState.h"
51 #include "FGFDMExec.h"
52 #include "FGFCS.h"
53 #include "FGAircraft.h"
54 #include "FGTranslation.h"
55 #include "FGRotation.h"
56 #include "FGPosition.h"
57 #include "FGAuxiliary.h"
58 #include "FGOutput.h"
59 #include "FGDefs.h"
60
61 /*******************************************************************************
62 ************************************ CODE **************************************
63 *******************************************************************************/
64
65
66 FGAtmosphere::FGAtmosphere(FGFDMExec* fdmex) : FGModel(fdmex)
67 {
68   Name = "FGAtmosphere";
69   h=0;
70   Calculate();
71 }
72
73
74 FGAtmosphere::~FGAtmosphere()
75 {
76 }
77
78
79 bool FGAtmosphere::Run(void)
80 {
81   if (!FGModel::Run()) {                 // if false then execute this Run()
82     h = State->Geth();
83     Calculate();
84     State->Seta(soundspeed);
85   } else {                               // skip Run() execution this time
86   }
87   return false;
88 }
89
90
91 float FGAtmosphere::CalcRho(float altitude)
92 {
93   return (0.00237 - 7.0E-08*altitude
94         + 7.0E-13*altitude*altitude
95         - 2.0E-18*altitude*altitude*altitude);
96 }
97
98
99 void FGAtmosphere::Calculate(void)
100 {
101   //see reference [1]
102
103   float slope,reftemp,refpress,refdens;
104   int i=0;
105   float htab[]={0,36089,82020,154198,173882,259183,295272,344484}; //ft.
106
107   if (h <= htab[0]) {
108     h=0;
109   } else if (h >= htab[7]){
110     i = 7;
111     h = htab[7];
112   } else {
113     while (htab[i+1] < h) {
114       i++;
115     }
116   }
117
118   switch(i) {
119   case 0:     // sea level
120     slope     = -0.0035662; // R/ft.
121     reftemp   = 518.688;    // R
122     refpress  = 2116.17;    // psf
123     refdens   = 0.0023765;  // slugs/cubic ft.
124     break;
125   case 1:     // 36089 ft.
126     slope     = 0;
127     reftemp   = 389.988;
128     refpress  = 474.1;
129     refdens   = 0.0007078;
130     break;
131   case 2:     // 82020 ft.
132     slope     = 0.00164594;
133     reftemp   = 389.988;
134     refpress  = 52.7838;
135     refdens   = 7.8849E-5;
136     break;
137   case 3:     // 154198 ft.
138     slope     = 0;
139     reftemp   = 508.788;
140     refpress  = 2.62274;
141     refdens   = 3.01379E-6;
142     break;
143   case 4:     // 173882 ft.
144     slope     = -0.00246891;
145     reftemp   = 508.788;
146     refpress  = 1.28428;
147     refdens   = 1.47035e-06;
148     break;
149   case 5:     // 259183 ft.
150     slope     = 0;
151     reftemp   = 298.188;
152     refpress  = 0.0222008;
153     refdens   = 4.33396e-08;
154     break;
155   case 6:     // 295272 ft.
156     slope     = 0.00219459;
157     reftemp   = 298.188;
158     refpress  = 0.00215742;
159     refdens   = 4.21368e-09;
160     break;
161   case 7:     // 344484 ft.
162     slope     = 0;
163     reftemp   = 406.188;
164     refpress  = 0.000153755;
165     refdens   = 2.20384e-10;
166     break;
167   }
168
169
170   if (slope == 0) {
171     temperature = reftemp;
172     pressure = refpress*exp(-GRAVITY/(reftemp*Reng)*(h-htab[i]));
173     density = refdens*exp(-GRAVITY/(reftemp*Reng)*(h-htab[i]));
174   } else {
175     temperature = reftemp+slope*(h-htab[i]);
176     pressure = refpress*pow(temperature/reftemp,-GRAVITY/(slope*Reng));
177     density = refdens*pow(temperature/reftemp,-(GRAVITY/(slope*Reng)+1));
178   }
179
180   soundspeed = sqrt(SHRATIO*Reng*temperature);
181
182 }
183
184
185 float FGAtmosphere::GetTemperature(float altitude)
186 {
187   if (altitude != h) {
188     h = altitude;
189     Calculate();
190   }
191   return temperature;
192 }
193
194
195 float FGAtmosphere::GetPressure(float altitude)
196 {
197   if (altitude != h) {
198     h = altitude;
199     Calculate();
200   }
201
202   return pressure;
203 }
204
205 float FGAtmosphere::GetDensity(float altitude)
206 {
207   if (altitude != h) {
208     h = altitude;
209     Calculate();
210   }
211
212   return density;
213 }
214
215
216 float FGAtmosphere::GetSoundSpeed(float altitude)
217 {
218   if (altitude != h) {
219     h = altitude;
220     Calculate();
221   }
222
223   return soundspeed;
224 }
225