]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGAtmosphere.cpp
Latest round of JSBSim updates.
[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 static const char *IdSrc = "$Id$";
63 static const char *IdHdr = ID_ATMOSPHERE;
64
65 extern short debug_lvl;
66
67 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
68 CLASS IMPLEMENTATION
69 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
70
71
72 FGAtmosphere::FGAtmosphere(FGFDMExec* fdmex) : FGModel(fdmex),
73                                                     vWindNED(3)
74 {
75   Name = "FGAtmosphere";
76   h = 0;
77   Calculate(h);
78   SLtemperature = temperature;
79   SLpressure    = pressure;
80   SLdensity     = density;
81   SLsoundspeed  = sqrt(SHRATIO*Reng*temperature);
82   useExternal=false;
83   vWindNED(1)=0;vWindNED(2)=0;vWindNED(3)=0;
84
85   if (debug_lvl & 2) cout << "Instantiated: " << Name << endl;
86 }
87
88 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
89
90 FGAtmosphere::~FGAtmosphere()
91 {
92   if (debug_lvl & 2) cout << "Destroyed:    FGAtmosphere" << endl;
93 }
94
95 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
96
97 bool FGAtmosphere::Run(void)
98 {
99   //cout << "In FGAtmosphere::Run(void)" << endl;
100   if (!FGModel::Run()) {                 // if false then execute this Run()
101     //do temp, pressure, and density first
102     if (!useExternal) {
103       //cout << "Atmosphere: Using internal model, altitude= ";
104       h = Position->Geth();
105
106       Calculate(h);
107     } else {
108       density = exDensity;
109       pressure = exPressure;
110       temperature = exTemperature;
111     }
112
113     psiw = atan2( vWindNED(2), vWindNED(1) );
114
115     if (psiw < 0) psiw += 2*M_PI;
116
117     soundspeed = sqrt(SHRATIO*Reng*temperature);
118     //cout << "Atmosphere: soundspeed: " << soundspeed << endl;
119     State->Seta(soundspeed);
120
121   } else {                               // skip Run() execution this time
122   }
123   return false;
124 }
125
126 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
127
128 void FGAtmosphere::Calculate(float altitude)
129 {
130   //see reference [1]
131
132   float slope,reftemp,refpress,refdens;
133   int i=0;
134   float htab[]={0,36089,82020,154198,173882,259183,295272,344484}; //ft.
135   // cout << "Atmosphere:  h=" << altitude << " rho= " << density << endl;
136   if (altitude <= htab[0]) {
137     altitude=0;
138   } else if (altitude >= htab[7]){
139     i = 7;
140     altitude = htab[7];
141   } else {
142     while (htab[i+1] < altitude) {
143       i++;
144     }
145   }
146
147   switch(i) {
148   case 0:     // sea level
149     slope     = -0.0035662; // R/ft.
150     reftemp   = 518.688;    // R
151     refpress  = 2116.17;    // psf
152     refdens   = 0.0023765;  // slugs/cubic ft.
153     break;
154   case 1:     // 36089 ft.
155     slope     = 0;
156     reftemp   = 389.988;
157     refpress  = 474.1;
158     refdens   = 0.0007078;
159     break;
160   case 2:     // 82020 ft.
161     slope     = 0.00164594;
162     reftemp   = 389.988;
163     refpress  = 52.7838;
164     refdens   = 7.8849E-5;
165     break;
166   case 3:     // 154198 ft.
167     slope     = 0;
168     reftemp   = 508.788;
169     refpress  = 2.62274;
170     refdens   = 3.01379E-6;
171     break;
172   case 4:     // 173882 ft.
173     slope     = -0.00246891;
174     reftemp   = 508.788;
175     refpress  = 1.28428;
176     refdens   = 1.47035e-06;
177     break;
178   case 5:     // 259183 ft.
179     slope     = 0;
180     reftemp   = 298.188;
181     refpress  = 0.0222008;
182     refdens   = 4.33396e-08;
183     break;
184   case 6:     // 295272 ft.
185     slope     = 0.00219459;
186     reftemp   = 298.188;
187     refpress  = 0.00215742;
188     refdens   = 4.21368e-09;
189     break;
190   case 7:     // 344484 ft.
191     slope     = 0;
192     reftemp   = 406.188;
193     refpress  = 0.000153755;
194     refdens   = 2.20384e-10;
195     break;
196   }
197
198   if (slope == 0) {
199     temperature = reftemp;
200     pressure = refpress*exp(-GRAVITY/(reftemp*Reng)*(altitude-htab[i]));
201     density = refdens*exp(-GRAVITY/(reftemp*Reng)*(altitude-htab[i]));
202   } else {
203     temperature = reftemp+slope*(altitude-htab[i]);
204     pressure = refpress*pow(temperature/reftemp,-GRAVITY/(slope*Reng));
205     density = refdens*pow(temperature/reftemp,-(GRAVITY/(slope*Reng)+1));
206   }
207
208   //cout << "Atmosphere:  h=" << altitude << " rho= " << density << endl;
209
210 }
211
212 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
213
214 void FGAtmosphere::Debug(void)
215 {
216     //TODO: Add your source code here
217 }
218