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