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