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