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