]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGAtmosphere.cpp
Jan 11, 2000 changes 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(h);
71   temperature = T;
72   pressure    = p;
73   density     = rho;
74   soundspeed  = a;
75 }
76
77
78 FGAtmosphere::~FGAtmosphere()
79 {
80 }
81
82
83 bool FGAtmosphere::Run(void)
84 {
85   if (!FGModel::Run()) {                 // if false then execute this Run()
86     h = State->Geth();
87
88     Calculate(h);
89
90     temperature = T;
91     pressure    = p;
92     density     = rhos;
93     soundspeed  = a;
94     State->Seta(soundspeed);
95   } else {                               // skip Run() execution this time
96   }
97   return false;
98 }
99
100
101 float FGAtmosphere::CalcRho(float altitude)
102 {
103   //return (0.00237 - 7.0E-08*altitude
104   //      + 7.0E-13*altitude*altitude
105   //      - 2.0E-18*altitude*altitude*altitude);
106   return GetDensity(altitude);
107 }
108
109
110 void FGAtmosphere::Calculate(float altitude)
111 {
112   //see reference [1]
113
114   float slope,reftemp,refpress,refdens;
115   int i=0;
116   float htab[]={0,36089,82020,154198,173882,259183,295272,344484}; //ft.
117
118   if (altitude <= htab[0]) {
119     altitude=0;
120   } else if (altitude >= htab[7]){
121     i = 7;
122     altitude = htab[7];
123   } else {
124     while (htab[i+1] < altitude) {
125       i++;
126     }
127   }
128
129   switch(i) {
130   case 0:     // sea level
131     slope     = -0.0035662; // R/ft.
132     reftemp   = 518.688;    // R
133     refpress  = 2116.17;    // psf
134     refdens   = 0.0023765;  // slugs/cubic ft.
135     break;
136   case 1:     // 36089 ft.
137     slope     = 0;
138     reftemp   = 389.988;
139     refpress  = 474.1;
140     refdens   = 0.0007078;
141     break;
142   case 2:     // 82020 ft.
143     slope     = 0.00164594;
144     reftemp   = 389.988;
145     refpress  = 52.7838;
146     refdens   = 7.8849E-5;
147     break;
148   case 3:     // 154198 ft.
149     slope     = 0;
150     reftemp   = 508.788;
151     refpress  = 2.62274;
152     refdens   = 3.01379E-6;
153     break;
154   case 4:     // 173882 ft.
155     slope     = -0.00246891;
156     reftemp   = 508.788;
157     refpress  = 1.28428;
158     refdens   = 1.47035e-06;
159     break;
160   case 5:     // 259183 ft.
161     slope     = 0;
162     reftemp   = 298.188;
163     refpress  = 0.0222008;
164     refdens   = 4.33396e-08;
165     break;
166   case 6:     // 295272 ft.
167     slope     = 0.00219459;
168     reftemp   = 298.188;
169     refpress  = 0.00215742;
170     refdens   = 4.21368e-09;
171     break;
172   case 7:     // 344484 ft.
173     slope     = 0;
174     reftemp   = 406.188;
175     refpress  = 0.000153755;
176     refdens   = 2.20384e-10;
177     break;
178   }
179
180
181   if (slope == 0) {
182     T = reftemp;
183     p = refpress*exp(-GRAVITY/(reftemp*Reng)*(altitude-htab[i]));
184     rhos = refdens*exp(-GRAVITY/(reftemp*Reng)*(altitude-htab[i]));
185   } else {
186     T = reftemp+slope*(altitude-htab[i]);
187     p = refpress*pow(T/reftemp,-GRAVITY/(slope*Reng));
188     rhos = refdens*pow(T/reftemp,-(GRAVITY/(slope*Reng)+1));
189   }
190
191   a = sqrt(SHRATIO*Reng*T);
192
193 }
194
195
196 float FGAtmosphere::GetTemperature(float altitude)
197 {
198     Calculate(altitude);
199     return T;
200 }
201
202
203 float FGAtmosphere::GetPressure(float altitude)
204 {
205     Calculate(altitude);
206     return p;
207 }
208
209 float FGAtmosphere::GetDensity(float altitude)
210 {
211     Calculate(altitude);
212     return rhos;
213 }
214
215
216 float FGAtmosphere::GetSoundSpeed(float altitude)
217 {
218     Calculate(altitude);
219     return a;
220 }
221