]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGAtmosphere.cpp
Updated to latest JSBSim, including preliminary support for
[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 {
73   Name = "FGAtmosphere";
74   lastIndex=0;
75   h = 0;
76   htab[0]=0;
77   htab[1]=36089.239;
78   htab[2]=65616.798;
79   htab[3]=104986.878;
80   htab[4]=154199.475;
81   htab[5]=170603.675;
82   htab[6]=200131.234;
83   htab[7]=259186.352; //ft.
84
85   MagnitudedAccelDt = MagnitudeAccel = Magnitude = 0.0;
86   turbType = ttNone;
87 //  turbType = ttBerndt; // temporarily disable turbulence until fully tested
88   TurbGain = 100.0;
89
90   Debug(0);
91 }
92
93 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
94
95 FGAtmosphere::~FGAtmosphere()
96 {
97   Debug(1);
98 }
99
100 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
101
102 bool FGAtmosphere::InitModel(void)
103 {
104   FGModel::InitModel();
105
106   Calculate(h);
107   SLtemperature = temperature;
108   SLpressure    = pressure;
109   SLdensity     = density;
110   SLsoundspeed  = sqrt(SHRatio*Reng*temperature);
111   rSLtemperature = 1.0/temperature;
112   rSLpressure    = 1.0/pressure;
113   rSLdensity     = 1.0/density;
114   rSLsoundspeed  = 1.0/SLsoundspeed;
115   useExternal=false;
116   
117   return true;
118 }
119
120 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
121
122 bool FGAtmosphere::Run(void)
123 {
124   if (!FGModel::Run()) {                 // if false then execute this Run()
125     //do temp, pressure, and density first
126     if (!useExternal) {
127       h = Position->Geth();
128       Calculate(h);
129     } else {
130       density = exDensity;
131       pressure = exPressure;
132       temperature = exTemperature;
133     }
134
135     if (turbType != ttNone) {
136       Turbulence();
137       vWindNED += vTurbulence;
138     }
139
140     if (vWindNED(1) != 0.0) psiw = atan2( vWindNED(2), vWindNED(1) );
141
142     if (psiw < 0) psiw += 2*M_PI;
143
144     soundspeed = sqrt(SHRatio*Reng*temperature);
145
146     State->Seta(soundspeed);
147
148     Debug(2);
149
150   } else {                               // skip Run() execution this time
151   }
152
153   return false;
154 }
155
156 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
157 //
158 // See reference 1
159
160 void FGAtmosphere::Calculate(double altitude)
161 {
162   double slope, reftemp, refpress;
163   int i = 0;
164
165   i = lastIndex;
166   if (altitude < htab[lastIndex]) {
167     if (altitude <= 0) { 
168       i = 0;
169       altitude=0;
170     } else {
171        i = lastIndex-1;
172        while (htab[i] > altitude) i--;
173     }   
174   } else if (altitude > htab[lastIndex+1]){
175     if (altitude >= htab[7]){
176       i = 7;
177       altitude = htab[7];
178     } else {
179       i = lastIndex+1;
180       while(htab[i+1] < altitude) i++;
181     }  
182   } 
183
184   switch(i) {
185   case 1:     // 36089 ft.
186     slope     = 0;
187     reftemp   = 389.97;
188     refpress  = 472.452;
189     //refdens   = 0.000706032;
190     break;
191   case 2:     // 65616 ft.
192     slope     = 0.00054864;
193     reftemp   = 389.97;
194     refpress  = 114.636;
195     //refdens   = 0.000171306;
196     break;
197   case 3:     // 104986 ft.
198     slope     = 0.00153619;
199     reftemp   = 411.57;
200     refpress  = 8.36364;
201     //refdens   = 1.18422e-05;
202     break;
203   case 4:     // 154199 ft.
204     slope     = 0;
205     reftemp   = 487.17;
206     refpress  = 0.334882;
207     //refdens   = 4.00585e-7;
208     break;
209   case 5:     // 170603 ft.
210     slope     = -0.00109728;
211     reftemp   = 487.17;
212     refpress  = 0.683084;
213     //refdens   = 8.17102e-7;
214     break;
215   case 6:     // 200131 ft.
216     slope     = -0.00219456;
217     reftemp   = 454.17;
218     refpress  = 0.00684986;
219     //refdens   = 8.77702e-9;
220     break;
221   case 7:     // 259186 ft.
222     slope     = 0;
223     reftemp   = 325.17;
224     refpress  = 0.000122276;
225     //refdens   = 2.19541e-10;
226     break;
227   case 0:
228   default:     // sea level
229     slope     = -0.00356616; // R/ft.
230     reftemp   = 518.67;    // R
231     refpress  = 2116.22;    // psf
232     //refdens   = 0.00237767;  // slugs/cubic ft.
233     break;
234   
235   }
236  
237   if (slope == 0) {
238     temperature = reftemp;
239     pressure = refpress*exp(-Inertial->SLgravity()/(reftemp*Reng)*(altitude-htab[i]));
240     //density = refdens*exp(-Inertial->SLgravity()/(reftemp*Reng)*(altitude-htab[i]));
241     density = pressure/(Reng*temperature);
242   } else {
243     temperature = reftemp+slope*(altitude-htab[i]);
244     pressure = refpress*pow(temperature/reftemp,-Inertial->SLgravity()/(slope*Reng));
245     //density = refdens*pow(temperature/reftemp,-(Inertial->SLgravity()/(slope*Reng)+1));
246     density = pressure/(Reng*temperature);
247   }
248   lastIndex=i;
249   //cout << "Atmosphere:  h=" << altitude << " rho= " << density << endl;
250 }
251
252 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
253
254 void FGAtmosphere::Turbulence(void)
255 {
256   switch (turbType) {
257   case ttBerndt:
258     vDirectiondAccelDt(eX) = 1 - 2.0*(((double)(rand()))/RAND_MAX);
259     vDirectiondAccelDt(eY) = 1 - 2.0*(((double)(rand()))/RAND_MAX);
260     vDirectiondAccelDt(eZ) = 1 - 2.0*(((double)(rand()))/RAND_MAX);
261
262     MagnitudedAccelDt = 1 - 2.0*(((double)(rand()))/RAND_MAX);
263     MagnitudeAccel    += MagnitudedAccelDt*rate*State->Getdt();
264     Magnitude         += MagnitudeAccel*rate*State->Getdt();
265
266     vDirectiondAccelDt.Normalize();
267     vDirectionAccel += vDirectiondAccelDt*rate*State->Getdt();
268     vDirectionAccel.Normalize();
269     vDirection      += vDirectionAccel*rate*State->Getdt();
270     vDirection.Normalize();
271     
272     vTurbulence = TurbGain*Magnitude * vDirection;
273     vTurbulenceGrad = TurbGain*MagnitudeAccel * vDirection;
274
275     vBodyTurbGrad = State->GetTl2b()*vTurbulenceGrad;
276     vTurbPQR(eP) = vBodyTurbGrad(eY)/Aircraft->GetWingSpan();
277     if (Aircraft->GetHTailArm() != 0.0)
278       vTurbPQR(eQ) = vBodyTurbGrad(eZ)/Aircraft->GetHTailArm();
279     else
280       vTurbPQR(eQ) = vBodyTurbGrad(eZ)/10.0;
281
282     if (Aircraft->GetVTailArm())
283       vTurbPQR(eR) = vBodyTurbGrad(eX)/Aircraft->GetVTailArm();
284     else
285       vTurbPQR(eR) = vBodyTurbGrad(eX)/10.0;
286
287     break;
288   default:
289     break;
290   }
291 }
292
293 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
294 //    The bitmasked value choices are as follows:
295 //    unset: In this case (the default) JSBSim would only print
296 //       out the normally expected messages, essentially echoing
297 //       the config files as they are read. If the environment
298 //       variable is not set, debug_lvl is set to 1 internally
299 //    0: This requests JSBSim not to output any messages
300 //       whatsoever.
301 //    1: This value explicity requests the normal JSBSim
302 //       startup messages
303 //    2: This value asks for a message to be printed out when
304 //       a class is instantiated
305 //    4: When this value is set, a message is displayed when a
306 //       FGModel object executes its Run() method
307 //    8: When this value is set, various runtime state variables
308 //       are printed out periodically
309 //    16: When set various parameters are sanity checked and
310 //       a message is printed out when they go out of bounds
311
312 void FGAtmosphere::Debug(int from)
313 {
314   if (debug_lvl <= 0) return;
315
316   if (debug_lvl & 1) { // Standard console startup message output
317     if (from == 0) { // Constructor
318     }
319   }
320   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
321     if (from == 0) cout << "Instantiated: FGAtmosphere" << endl;
322     if (from == 1) cout << "Destroyed:    FGAtmosphere" << endl;
323   }
324   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
325   }
326   if (debug_lvl & 8 ) { // Runtime state variables
327   }
328   if (debug_lvl & 16) { // Sanity checking
329   }
330   if (debug_lvl & 32) { // Turbulence
331     if (frame == 0 && from == 2) {
332       cout << "vTurbulence(X), vTurbulence(Y), vTurbulence(Z), "
333            << "vTurbulenceGrad(X), vTurbulenceGrad(Y), vTurbulenceGrad(Z), "
334            << "vDirection(X), vDirection(Y), vDirection(Z), "
335            << "Magnitude, "
336            << "vTurbPQR(P), vTurbPQR(Q), vTurbPQR(R), " << endl;
337     } else if (from == 2) {
338       cout << vTurbulence << ", " << vTurbulenceGrad << ", " << vDirection << ", " << Magnitude << ", " << vTurbPQR << endl;
339     }
340   }
341   if (debug_lvl & 64) {
342     if (from == 0) { // Constructor
343       cout << IdSrc << endl;
344       cout << IdHdr << endl;
345     }
346   }
347 }
348