]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGAtmosphere.cpp
Fixes to jsbsim.
[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 #include "FGMatrix.h"
61
62 /*******************************************************************************
63 ************************************ CODE **************************************
64 *******************************************************************************/
65
66
67 FGAtmosphere::FGAtmosphere(FGFDMExec* fdmex) : FGModel(fdmex),vWindUVW(3),vWindNED(3)
68 {
69     Name = "FGAtmosphere";
70     h = 0;
71     Calculate(h);
72     SLtemperature = temperature;
73     SLpressure    = pressure;
74     SLdensity     = density;
75     SLsoundspeed  = sqrt(SHRATIO*Reng*temperature);
76     useExternal=false;
77     vWindUVW(1)=0;vWindUVW(2)=0;vWindUVW(3)=0;
78     vWindNED(1)=0;vWindNED(2)=0;vWindNED(3)=0;
79 }
80
81
82 FGAtmosphere::~FGAtmosphere()
83 {
84 }
85
86
87 bool FGAtmosphere::Run(void)
88 {
89     //cout << "In FGAtmosphere::Run(void)" << endl;
90     if (!FGModel::Run()) {                 // if false then execute this Run()
91         //do temp, pressure, and density first
92         if (!useExternal) {
93             //cout << "Atmosphere: Using internal model, altitude= ";
94             h = Position->Geth();
95
96             Calculate(h);
97         } else {
98             density = exDensity;
99             pressure = exPressure;
100             temperature = exTemperature;
101             //switch sign of wind components so that they are
102             //in aircraft reference frame.  The classic example is
103             //takeoff or landing where you always want to fly
104             //into the wind.  Suppose that an aircraft is
105             //taking off into the wind on the runway heading
106             //of pure north.  Into the wind means the wind is
107             //flowing to the south (or negative) direction,
108             //and we know that headwinds increase the relative
109             //velocity, so to make a positive delta U from the
110             //southerly wind the sign must be switched.
111             vWindNED *= -1;
112             vWindUVW  = State->GetTl2b()*vWindNED;
113         }
114         soundspeed = sqrt(SHRATIO*Reng*temperature);
115         //cout << "Atmosphere: soundspeed: " << soundspeed << endl;
116         State->Seta(soundspeed);
117
118
119     } else {                               // skip Run() execution this time
120     }
121     return false;
122 }
123
124
125 void FGAtmosphere::Calculate(float altitude)
126 {
127     //see reference [1]
128
129     float slope,reftemp,refpress,refdens;
130     int i=0;
131     float htab[]={0,36089,82020,154198,173882,259183,295272,344484}; //ft.
132     // cout << "Atmosphere:  h=" << altitude << " rho= " << density << endl;
133     if (altitude <= htab[0]) {
134         altitude=0;
135     } else if (altitude >= htab[7]){
136         i = 7;
137         altitude = htab[7];
138     } else {
139         while (htab[i+1] < altitude) {
140             i++;
141         }
142     }
143
144     switch(i) {
145     case 0:     // sea level
146         slope     = -0.0035662; // R/ft.
147         reftemp   = 518.688;    // R
148         refpress  = 2116.17;    // psf
149         refdens   = 0.0023765;  // slugs/cubic ft.
150         break;
151     case 1:     // 36089 ft.
152         slope     = 0;
153         reftemp   = 389.988;
154         refpress  = 474.1;
155         refdens   = 0.0007078;
156         break;
157     case 2:     // 82020 ft.
158         slope     = 0.00164594;
159         reftemp   = 389.988;
160         refpress  = 52.7838;
161         refdens   = 7.8849E-5;
162         break;
163     case 3:     // 154198 ft.
164         slope     = 0;
165         reftemp   = 508.788;
166         refpress  = 2.62274;
167         refdens   = 3.01379E-6;
168         break;
169     case 4:     // 173882 ft.
170         slope     = -0.00246891;
171         reftemp   = 508.788;
172         refpress  = 1.28428;
173         refdens   = 1.47035e-06;
174         break;
175     case 5:     // 259183 ft.
176         slope     = 0;
177         reftemp   = 298.188;
178         refpress  = 0.0222008;
179         refdens   = 4.33396e-08;
180         break;
181     case 6:     // 295272 ft.
182         slope     = 0.00219459;
183         reftemp   = 298.188;
184         refpress  = 0.00215742;
185         refdens   = 4.21368e-09;
186         break;
187     case 7:     // 344484 ft.
188         slope     = 0;
189         reftemp   = 406.188;
190         refpress  = 0.000153755;
191         refdens   = 2.20384e-10;
192         break;
193     }
194
195
196     if (slope == 0) {
197         temperature = reftemp;
198         pressure = refpress*exp(-GRAVITY/(reftemp*Reng)*(altitude-htab[i]));
199         density = refdens*exp(-GRAVITY/(reftemp*Reng)*(altitude-htab[i]));
200     } else {
201         temperature = reftemp+slope*(altitude-htab[i]);
202         pressure = refpress*pow(temperature/reftemp,-GRAVITY/(slope*Reng));
203         density = refdens*pow(temperature/reftemp,-(GRAVITY/(slope*Reng)+1));
204     }
205
206     //cout << "Atmosphere:  h=" << altitude << " rho= " << density << endl;
207
208 }
209
210
211
212