]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGAtmosphere.cpp
Updated JSBsim code.
[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   SLtemperature = temperature;
72   SLpressure    = pressure;
73   SLdensity     = density;
74   SLsoundspeed  = sqrt(SHRATIO*Reng*temperature);
75   useExternal=false;
76 }
77
78
79 FGAtmosphere::~FGAtmosphere()
80 {
81 }
82
83
84 bool FGAtmosphere::Run(void)
85 {
86   if (!FGModel::Run()) {                 // if false then execute this Run()
87     if (!useExternal) {
88       h = State->Geth();
89       Calculate(h);
90     } else {
91       density = exDensity;
92       pressure = exPressure;
93       temperature = exTemperature;
94     }
95     soundspeed = sqrt(SHRATIO*Reng*temperature);
96     State->Seta(soundspeed);
97   } else {                               // skip Run() execution this time
98   }
99   return false;
100 }
101
102 void FGAtmosphere::Calculate(float altitude)
103 {
104   //see reference [1]
105
106
107     float slope,reftemp,refpress,refdens;
108     int i=0;
109     float htab[]={0,36089,82020,154198,173882,259183,295272,344484}; //ft.
110 //      cout << "Atmosphere:  h=" << altitude << " rho= " << density << endl;
111     if (altitude <= htab[0]) {
112       altitude=0;
113     } else if (altitude >= htab[7]){
114       i = 7;
115       altitude = htab[7];
116     } else {
117       while (htab[i+1] < altitude) {
118         i++;
119       }
120     }
121
122     switch(i) {
123     case 0:     // sea level
124       slope     = -0.0035662; // R/ft.
125       reftemp   = 518.688;    // R
126       refpress  = 2116.17;    // psf
127       refdens   = 0.0023765;  // slugs/cubic ft.
128       break;
129     case 1:     // 36089 ft.
130       slope     = 0;
131       reftemp   = 389.988;
132       refpress  = 474.1;
133       refdens   = 0.0007078;
134       break;
135     case 2:     // 82020 ft.
136       slope     = 0.00164594;
137       reftemp   = 389.988;
138       refpress  = 52.7838;
139       refdens   = 7.8849E-5;
140       break;
141     case 3:     // 154198 ft.
142       slope     = 0;
143       reftemp   = 508.788;
144       refpress  = 2.62274;
145       refdens   = 3.01379E-6;
146       break;
147     case 4:     // 173882 ft.
148       slope     = -0.00246891;
149       reftemp   = 508.788;
150       refpress  = 1.28428;
151       refdens   = 1.47035e-06;
152       break;
153     case 5:     // 259183 ft.
154       slope     = 0;
155       reftemp   = 298.188;
156       refpress  = 0.0222008;
157       refdens   = 4.33396e-08;
158       break;
159     case 6:     // 295272 ft.
160       slope     = 0.00219459;
161       reftemp   = 298.188;
162       refpress  = 0.00215742;
163       refdens   = 4.21368e-09;
164       break;
165     case 7:     // 344484 ft.
166       slope     = 0;
167       reftemp   = 406.188;
168       refpress  = 0.000153755;
169       refdens   = 2.20384e-10;
170       break;
171     }
172
173
174     if (slope == 0) {
175       temperature = reftemp;
176       pressure = refpress*exp(-GRAVITY/(reftemp*Reng)*(altitude-htab[i]));
177       density = refdens*exp(-GRAVITY/(reftemp*Reng)*(altitude-htab[i]));
178     } else {
179       temperature = reftemp+slope*(altitude-htab[i]);
180       pressure = refpress*pow(temperature/reftemp,-GRAVITY/(slope*Reng));
181       density = refdens*pow(temperature/reftemp,-(GRAVITY/(slope*Reng)+1));
182     }
183
184    //cout << "Atmosphere:  h=" << altitude << " rho= " << density << endl;
185
186 }
187