]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/models/atmosphere/FGMars.cpp
Sync. w. JSB CVS as of 15/01/2007
[flightgear.git] / src / FDM / JSBSim / models / atmosphere / FGMars.cpp
1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2
3  Module:       FGMars.cpp
4  Author:       Jon Berndt
5  Date started: 1/4/04
6  Purpose:      Models the Martian atmosphere very simply
7  Called by:    FGFDMExec
8
9  ------------- Copyright (C) 2004  Jon S. Berndt (jsb@hal-pc.org) -------------
10
11  This program is free software; you can redistribute it and/or modify it under
12  the terms of the GNU Lesser General Public License as published by the Free Software
13  Foundation; either version 2 of the License, or (at your option) any later
14  version.
15
16  This program is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18  FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
19  details.
20
21  You should have received a copy of the GNU Lesser General Public License along with
22  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
23  Place - Suite 330, Boston, MA  02111-1307, USA.
24
25  Further information about the GNU Lesser General Public License can also be found on
26  the world wide web at http://www.gnu.org.
27
28 FUNCTIONAL DESCRIPTION
29 --------------------------------------------------------------------------------
30 Models the Martian atmosphere.
31
32 HISTORY
33 --------------------------------------------------------------------------------
34 1/04/2004   JSB   Created
35
36 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
37 COMMENTS, REFERENCES,  and NOTES
38 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
39
40 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
41 INCLUDES
42 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
43
44 #include "FGMars.h"
45 #include "FGState.h"
46
47 namespace JSBSim {
48
49 static const char *IdSrc = "$Id$";
50 static const char *IdHdr = ID_MARS;
51
52 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
53 CLASS IMPLEMENTATION
54 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
55
56
57 FGMars::FGMars(FGFDMExec* fdmex) : FGAtmosphere(fdmex)
58 {
59   Name = "FGMars";
60   Reng = 53.5 * 44.01;
61
62 /*
63   lastIndex = 0;
64   h = 0.0;
65   psiw = 0.0;
66
67   MagnitudedAccelDt = MagnitudeAccel = Magnitude = 0.0;
68 //   turbType = ttNone;
69   turbType = ttStandard;
70 //   turbType = ttBerndt;
71   TurbGain = 0.0;
72   TurbRate = 1.0;
73 */
74
75   bind();
76   Debug(0);
77 }
78
79 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
80 /*
81 FGMars::~FGMars()
82 {
83   unbind();
84   Debug(1);
85 }
86 */
87 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
88
89 bool FGMars::InitModel(void)
90 {
91   FGModel::InitModel();
92
93   Calculate(h);
94   SLtemperature = intTemperature;
95   SLpressure    = intPressure;
96   SLdensity     = intDensity;
97   SLsoundspeed  = sqrt(SHRatio*Reng*intTemperature);
98   rSLtemperature = 1.0/intTemperature;
99   rSLpressure    = 1.0/intPressure;
100   rSLdensity     = 1.0/intDensity;
101   rSLsoundspeed  = 1.0/SLsoundspeed;
102   temperature = &intTemperature;
103   pressure = &intPressure;
104   density = &intDensity;
105
106   useExternal=false;
107
108   return true;
109 }
110
111 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
112
113 bool FGMars::Run(void)
114 {
115   if (FGModel::Run()) return true;
116   if (FDMExec->Holding()) return false;
117
118   //do temp, pressure, and density first
119   if (!useExternal) {
120     h = Propagate->Geth();
121     Calculate(h);
122   }
123
124   if (turbType != ttNone) {
125     Turbulence();
126     vWindNED += vTurbulence;
127   }
128
129   if (vWindNED(1) != 0.0) psiw = atan2( vWindNED(2), vWindNED(1) );
130
131   if (psiw < 0) psiw += 2*M_PI;
132
133   soundspeed = sqrt(SHRatio*Reng*(*temperature));
134
135   Debug(2);
136
137   return false;
138 }
139
140 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
141
142 void FGMars::Calculate(double altitude)
143 {
144   //Calculate reftemp, refpress, and density
145
146   // LIMIT the temperatures so they do not descend below absolute zero.
147
148   if (altitude < 22960.0) {
149     intTemperature = -25.68 - 0.000548*altitude; // Deg Fahrenheit
150   } else {
151     intTemperature = -10.34 - 0.001217*altitude; // Deg Fahrenheit
152   }
153   intPressure = 14.62*exp(-0.00003*altitude); // psf - 14.62 psf =~ 7 millibars
154   intDensity = intPressure/(Reng*intTemperature); // slugs/ft^3 (needs deg R. as input
155
156   //cout << "Atmosphere:  h=" << altitude << " rho= " << intDensity << endl;
157 }
158
159 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
160
161 // square a value, but preserve the original sign
162
163 static inline double
164 square_signed (double value)
165 {
166   if (value < 0)
167     return value * value * -1;
168   else
169     return value * value;
170 }
171
172 void FGMars::Turbulence(void)
173 {
174   switch (turbType) {
175   case ttStandard: {
176     vDirectiondAccelDt(eX) = 1 - 2.0*(double(rand())/double(RAND_MAX));
177     vDirectiondAccelDt(eY) = 1 - 2.0*(double(rand())/double(RAND_MAX));
178     vDirectiondAccelDt(eZ) = 1 - 2.0*(double(rand())/double(RAND_MAX));
179
180     MagnitudedAccelDt = 1 - 2.0*(double(rand())/double(RAND_MAX)) - Magnitude;
181                                 // Scale the magnitude so that it moves
182                                 // away from the peaks
183     MagnitudedAccelDt = ((MagnitudedAccelDt - Magnitude) /
184                          (1 + fabs(Magnitude)));
185     MagnitudeAccel    += MagnitudedAccelDt*rate*TurbRate*State->Getdt();
186     Magnitude         += MagnitudeAccel*rate*State->Getdt();
187
188     vDirectiondAccelDt.Normalize();
189
190                                 // deemphasise non-vertical forces
191     vDirectiondAccelDt(eX) = square_signed(vDirectiondAccelDt(eX));
192     vDirectiondAccelDt(eY) = square_signed(vDirectiondAccelDt(eY));
193
194     vDirectionAccel += vDirectiondAccelDt*rate*TurbRate*State->Getdt();
195     vDirectionAccel.Normalize();
196     vDirection      += vDirectionAccel*rate*State->Getdt();
197
198     vDirection.Normalize();
199
200                                 // Diminish turbulence within three wingspans
201                                 // of the ground
202     vTurbulence = TurbGain * Magnitude * vDirection;
203     double HOverBMAC = Auxiliary->GetHOverBMAC();
204     if (HOverBMAC < 3.0)
205         vTurbulence *= (HOverBMAC / 3.0) * (HOverBMAC / 3.0);
206
207     vTurbulenceGrad = TurbGain*MagnitudeAccel * vDirection;
208
209     vBodyTurbGrad = Propagate->GetTl2b()*vTurbulenceGrad;
210     vTurbPQR(eP) = vBodyTurbGrad(eY)/Aircraft->GetWingSpan();
211 //     if (Aircraft->GetHTailArm() != 0.0)
212 //       vTurbPQR(eQ) = vBodyTurbGrad(eZ)/Aircraft->GetHTailArm();
213 //     else
214 //       vTurbPQR(eQ) = vBodyTurbGrad(eZ)/10.0;
215
216     if (Aircraft->GetVTailArm())
217       vTurbPQR(eR) = vBodyTurbGrad(eX)/Aircraft->GetVTailArm();
218     else
219       vTurbPQR(eR) = vBodyTurbGrad(eX)/10.0;
220
221                                 // Clear the horizontal forces
222                                 // actually felt by the plane, now
223                                 // that we've used them to calculate
224                                 // moments.
225     vTurbulence(eX) = 0.0;
226     vTurbulence(eY) = 0.0;
227
228     break;
229   }
230   case ttBerndt: {
231     vDirectiondAccelDt(eX) = 1 - 2.0*(double(rand())/double(RAND_MAX));
232     vDirectiondAccelDt(eY) = 1 - 2.0*(double(rand())/double(RAND_MAX));
233     vDirectiondAccelDt(eZ) = 1 - 2.0*(double(rand())/double(RAND_MAX));
234
235
236     MagnitudedAccelDt = 1 - 2.0*(double(rand())/double(RAND_MAX)) - Magnitude;
237     MagnitudeAccel    += MagnitudedAccelDt*rate*State->Getdt();
238     Magnitude         += MagnitudeAccel*rate*State->Getdt();
239
240     vDirectiondAccelDt.Normalize();
241     vDirectionAccel += vDirectiondAccelDt*rate*State->Getdt();
242     vDirectionAccel.Normalize();
243     vDirection      += vDirectionAccel*rate*State->Getdt();
244
245                                 // Diminish z-vector within two wingspans
246                                 // of the ground
247     double HOverBMAC = Auxiliary->GetHOverBMAC();
248     if (HOverBMAC < 2.0)
249         vDirection(eZ) *= HOverBMAC / 2.0;
250
251     vDirection.Normalize();
252
253     vTurbulence = TurbGain*Magnitude * vDirection;
254     vTurbulenceGrad = TurbGain*MagnitudeAccel * vDirection;
255
256     vBodyTurbGrad = Propagate->GetTl2b()*vTurbulenceGrad;
257     vTurbPQR(eP) = vBodyTurbGrad(eY)/Aircraft->GetWingSpan();
258     if (Aircraft->GetHTailArm() != 0.0)
259       vTurbPQR(eQ) = vBodyTurbGrad(eZ)/Aircraft->GetHTailArm();
260     else
261       vTurbPQR(eQ) = vBodyTurbGrad(eZ)/10.0;
262
263     if (Aircraft->GetVTailArm())
264       vTurbPQR(eR) = vBodyTurbGrad(eX)/Aircraft->GetVTailArm();
265     else
266       vTurbPQR(eR) = vBodyTurbGrad(eX)/10.0;
267
268     break;
269   }
270   default:
271     break;
272   }
273 }
274
275 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
276 //    The bitmasked value choices are as follows:
277 //    unset: In this case (the default) JSBSim would only print
278 //       out the normally expected messages, essentially echoing
279 //       the config files as they are read. If the environment
280 //       variable is not set, debug_lvl is set to 1 internally
281 //    0: This requests JSBSim not to output any messages
282 //       whatsoever.
283 //    1: This value explicity requests the normal JSBSim
284 //       startup messages
285 //    2: This value asks for a message to be printed out when
286 //       a class is instantiated
287 //    4: When this value is set, a message is displayed when a
288 //       FGModel object executes its Run() method
289 //    8: When this value is set, various runtime state variables
290 //       are printed out periodically
291 //    16: When set various parameters are sanity checked and
292 //       a message is printed out when they go out of bounds
293
294 void FGMars::Debug(int from)
295 {
296   if (debug_lvl <= 0) return;
297
298   if (debug_lvl & 1) { // Standard console startup message output
299     if (from == 0) { // Constructor
300     }
301   }
302   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
303     if (from == 0) cout << "Instantiated: FGMars" << endl;
304     if (from == 1) cout << "Destroyed:    FGMars" << endl;
305   }
306   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
307   }
308   if (debug_lvl & 8 ) { // Runtime state variables
309   }
310   if (debug_lvl & 16) { // Sanity checking
311   }
312   if (debug_lvl & 32) { // Turbulence
313     if (frame == 0 && from == 2) {
314       cout << "vTurbulence(X), vTurbulence(Y), vTurbulence(Z), "
315            << "vTurbulenceGrad(X), vTurbulenceGrad(Y), vTurbulenceGrad(Z), "
316            << "vDirection(X), vDirection(Y), vDirection(Z), "
317            << "Magnitude, "
318            << "vTurbPQR(P), vTurbPQR(Q), vTurbPQR(R), " << endl;
319     } else if (from == 2) {
320       cout << vTurbulence << ", " << vTurbulenceGrad << ", " << vDirection << ", " << Magnitude << ", " << vTurbPQR << endl;
321     }
322   }
323   if (debug_lvl & 64) {
324     if (from == 0) { // Constructor
325       cout << IdSrc << endl;
326       cout << IdHdr << endl;
327     }
328   }
329 }
330
331 } // namespace JSBSim