]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGAtmosphere.cpp
Syncing with the very latest JSBSim development 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 #include "FGMatrix33.h"
61 #include "FGColumnVector3.h"
62 #include "FGColumnVector4.h"
63
64 static const char *IdSrc = "$Id$";
65 static const char *IdHdr = ID_ATMOSPHERE;
66
67 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
68 CLASS IMPLEMENTATION
69 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
70
71
72 FGAtmosphere::FGAtmosphere(FGFDMExec* fdmex) : FGModel(fdmex),
73                                                vWindNED(3)
74 {
75   Name = "FGAtmosphere";
76   lastIndex=0;
77   h = 0;
78   htab[0]=0;
79   htab[1]=36089.239;
80   htab[2]=65616.798;
81   htab[3]=104986.878;
82   htab[4]=154199.475;
83   htab[5]=170603.675;
84   htab[6]=200131.234;
85   htab[7]=259186.352; //ft.
86
87   Calculate(h);
88   SLtemperature = temperature;
89   SLpressure    = pressure;
90   SLdensity     = density;
91   SLsoundspeed  = sqrt(SHRATIO*Reng*temperature);
92   rSLtemperature = 1.0/temperature;
93   rSLpressure    = 1.0/pressure;
94   rSLdensity     = 1.0/density;
95   rSLsoundspeed  = 1.0/SLsoundspeed;
96   useExternal=false;
97
98   if (debug_lvl & 2) cout << "Instantiated: " << Name << endl;
99 }
100
101 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
102
103 FGAtmosphere::~FGAtmosphere()
104 {
105   if (debug_lvl & 2) cout << "Destroyed:    FGAtmosphere" << endl;
106 }
107
108 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
109
110 bool FGAtmosphere::Run(void)
111 {
112   if (!FGModel::Run()) {                 // if false then execute this Run()
113     //do temp, pressure, and density first
114     if (!useExternal) {
115       h = Position->Geth();
116       Calculate(h);
117     } else {
118       density = exDensity;
119       pressure = exPressure;
120       temperature = exTemperature;
121     }
122
123     if (vWindNED(1) != 0.0) psiw = atan2( vWindNED(2), vWindNED(1) );
124
125     if (psiw < 0) psiw += 2*M_PI;
126
127     soundspeed = sqrt(SHRATIO*Reng*temperature);
128
129     State->Seta(soundspeed);
130
131   } else {                               // skip Run() execution this time
132   }
133   return false;
134 }
135
136 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
137
138 void FGAtmosphere::Calculate(float altitude)
139 {
140   //see reference [1]
141
142   float slope,reftemp,refpress;
143   int i=0; bool lookup = false;
144   // cout << "Atmosphere:  h=" << altitude << " rho= " << density << endl;
145   i=lastIndex;
146   if(altitude < htab[lastIndex]) {
147     if (altitude <= 0) { 
148       i=0; altitude=0;
149     } else {
150        i=lastIndex-1;
151        while (htab[i] > altitude) { i--; }
152     }   
153   } else if (altitude > htab[lastIndex+1]){
154     if (altitude >= htab[7]){
155       i = 7; altitude = htab[7];
156     } else {
157       i=lastIndex+1;
158       while(htab[i+1] < altitude) { i++; } 
159     }  
160   } 
161
162   switch(i) {
163   case 0:     // sea level
164     slope     = -0.00356616; // R/ft.
165     reftemp   = 518.67;    // R
166     refpress  = 2116.22;    // psf
167     //refdens   = 0.00237767;  // slugs/cubic ft.
168     break;
169   case 1:     // 36089 ft.
170     slope     = 0;
171     reftemp   = 389.97;
172     refpress  = 472.452;
173     //refdens   = 0.000706032;
174     break;
175   case 2:     // 65616 ft.
176     slope     = 0.00054864;
177     reftemp   = 389.97;
178     refpress  = 114.636;
179     //refdens   = 0.000171306;
180     break;
181   case 3:     // 104986 ft.
182     slope     = 0.00153619;
183     reftemp   = 411.57;
184     refpress  = 8.36364;
185     //refdens   = 1.18422e-05;
186     break;
187   case 4:     // 154199 ft.
188     slope     = 0;
189     reftemp   = 487.17;
190     refpress  = 0.334882;
191     //refdens   = 4.00585e-7;
192     break;
193   case 5:     // 170603 ft.
194     slope     = -0.00109728;
195     reftemp   = 487.17;
196     refpress  = 0.683084;
197     //refdens   = 8.17102e-7;
198     break;
199   case 6:     // 200131 ft.
200     slope     = -0.00219456;
201     reftemp   = 454.17;
202     refpress  = 0.00684986;
203     //refdens   = 8.77702e-9;
204     break;
205   case 7:     // 259186 ft.
206     slope     = 0;
207     reftemp   = 325.17;
208     refpress  = 0.000122276;
209     //refdens   = 2.19541e-10;
210     break;
211   }
212  
213   if (slope == 0) {
214     temperature = reftemp;
215     pressure = refpress*exp(-GRAVITY/(reftemp*Reng)*(altitude-htab[i]));
216     //density = refdens*exp(-GRAVITY/(reftemp*Reng)*(altitude-htab[i]));
217     density = pressure/(Reng*temperature);
218   } else {
219     temperature = reftemp+slope*(altitude-htab[i]);
220     pressure = refpress*pow(temperature/reftemp,-GRAVITY/(slope*Reng));
221     //density = refdens*pow(temperature/reftemp,-(GRAVITY/(slope*Reng)+1));
222     density = pressure/(Reng*temperature);
223   }
224   lastIndex=i;
225   //cout << "Atmosphere:  h=" << altitude << " rho= " << density << endl;
226 }
227
228 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
229
230 void FGAtmosphere::Debug(void)
231 {
232     //TODO: Add your source code here
233 }
234