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