]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/models/atmosphere/FGMars.cpp
Update to the latest version of JSBSim which supports Lighter Than Air craft
[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   Debug(1);
84 }
85 */
86 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
87
88 bool FGMars::InitModel(void)
89 {
90   FGModel::InitModel();
91
92   Calculate(h);
93   SLtemperature = intTemperature;
94   SLpressure    = intPressure;
95   SLdensity     = intDensity;
96   SLsoundspeed  = sqrt(SHRatio*Reng*intTemperature);
97   rSLtemperature = 1.0/intTemperature;
98   rSLpressure    = 1.0/intPressure;
99   rSLdensity     = 1.0/intDensity;
100   rSLsoundspeed  = 1.0/SLsoundspeed;
101   temperature = &intTemperature;
102   pressure = &intPressure;
103   density = &intDensity;
104
105   useExternal=false;
106
107   return true;
108 }
109
110 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
111
112 bool FGMars::Run(void)
113 {
114   if (FGModel::Run()) return true;
115   if (FDMExec->Holding()) return false;
116
117   //do temp, pressure, and density first
118   if (!useExternal) {
119     h = Propagate->Geth();
120     Calculate(h);
121   }
122
123   if (turbType != ttNone) {
124     Turbulence();
125     vWindNED += vTurbulence;
126   }
127
128   if (vWindNED(1) != 0.0) psiw = atan2( vWindNED(2), vWindNED(1) );
129
130   if (psiw < 0) psiw += 2*M_PI;
131
132   soundspeed = sqrt(SHRatio*Reng*(*temperature));
133
134   Debug(2);
135
136   return false;
137 }
138
139 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
140
141 void FGMars::Calculate(double altitude)
142 {
143   //Calculate reftemp, refpress, and density
144
145   // LIMIT the temperatures so they do not descend below absolute zero.
146
147   if (altitude < 22960.0) {
148     intTemperature = -25.68 - 0.000548*altitude; // Deg Fahrenheit
149   } else {
150     intTemperature = -10.34 - 0.001217*altitude; // Deg Fahrenheit
151   }
152   intPressure = 14.62*exp(-0.00003*altitude); // psf - 14.62 psf =~ 7 millibars
153   intDensity = intPressure/(Reng*intTemperature); // slugs/ft^3 (needs deg R. as input
154
155   //cout << "Atmosphere:  h=" << altitude << " rho= " << intDensity << endl;
156 }
157
158 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
159
160 // square a value, but preserve the original sign
161
162 static inline double
163 square_signed (double value)
164 {
165   if (value < 0)
166     return value * value * -1;
167   else
168     return value * value;
169 }
170
171 void FGMars::Turbulence(void)
172 {
173   switch (turbType) {
174   case ttStandard: {
175     vDirectiondAccelDt(eX) = 1 - 2.0*(double(rand())/double(RAND_MAX));
176     vDirectiondAccelDt(eY) = 1 - 2.0*(double(rand())/double(RAND_MAX));
177     vDirectiondAccelDt(eZ) = 1 - 2.0*(double(rand())/double(RAND_MAX));
178
179     MagnitudedAccelDt = 1 - 2.0*(double(rand())/double(RAND_MAX)) - Magnitude;
180                                 // Scale the magnitude so that it moves
181                                 // away from the peaks
182     MagnitudedAccelDt = ((MagnitudedAccelDt - Magnitude) /
183                          (1 + fabs(Magnitude)));
184     MagnitudeAccel    += MagnitudedAccelDt*rate*TurbRate*State->Getdt();
185     Magnitude         += MagnitudeAccel*rate*State->Getdt();
186
187     vDirectiondAccelDt.Normalize();
188
189                                 // deemphasise non-vertical forces
190     vDirectiondAccelDt(eX) = square_signed(vDirectiondAccelDt(eX));
191     vDirectiondAccelDt(eY) = square_signed(vDirectiondAccelDt(eY));
192
193     vDirectionAccel += vDirectiondAccelDt*rate*TurbRate*State->Getdt();
194     vDirectionAccel.Normalize();
195     vDirection      += vDirectionAccel*rate*State->Getdt();
196
197     vDirection.Normalize();
198
199                                 // Diminish turbulence within three wingspans
200                                 // of the ground
201     vTurbulence = TurbGain * Magnitude * vDirection;
202     double HOverBMAC = Auxiliary->GetHOverBMAC();
203     if (HOverBMAC < 3.0)
204         vTurbulence *= (HOverBMAC / 3.0) * (HOverBMAC / 3.0);
205
206     vTurbulenceGrad = TurbGain*MagnitudeAccel * vDirection;
207
208     vBodyTurbGrad = Propagate->GetTl2b()*vTurbulenceGrad;
209     vTurbPQR(eP) = vBodyTurbGrad(eY)/Aircraft->GetWingSpan();
210 //     if (Aircraft->GetHTailArm() != 0.0)
211 //       vTurbPQR(eQ) = vBodyTurbGrad(eZ)/Aircraft->GetHTailArm();
212 //     else
213 //       vTurbPQR(eQ) = vBodyTurbGrad(eZ)/10.0;
214
215     if (Aircraft->GetVTailArm())
216       vTurbPQR(eR) = vBodyTurbGrad(eX)/Aircraft->GetVTailArm();
217     else
218       vTurbPQR(eR) = vBodyTurbGrad(eX)/10.0;
219
220                                 // Clear the horizontal forces
221                                 // actually felt by the plane, now
222                                 // that we've used them to calculate
223                                 // moments.
224     vTurbulence(eX) = 0.0;
225     vTurbulence(eY) = 0.0;
226
227     break;
228   }
229   case ttBerndt: {
230     vDirectiondAccelDt(eX) = 1 - 2.0*(double(rand())/double(RAND_MAX));
231     vDirectiondAccelDt(eY) = 1 - 2.0*(double(rand())/double(RAND_MAX));
232     vDirectiondAccelDt(eZ) = 1 - 2.0*(double(rand())/double(RAND_MAX));
233
234
235     MagnitudedAccelDt = 1 - 2.0*(double(rand())/double(RAND_MAX)) - Magnitude;
236     MagnitudeAccel    += MagnitudedAccelDt*rate*State->Getdt();
237     Magnitude         += MagnitudeAccel*rate*State->Getdt();
238
239     vDirectiondAccelDt.Normalize();
240     vDirectionAccel += vDirectiondAccelDt*rate*State->Getdt();
241     vDirectionAccel.Normalize();
242     vDirection      += vDirectionAccel*rate*State->Getdt();
243
244                                 // Diminish z-vector within two wingspans
245                                 // of the ground
246     double HOverBMAC = Auxiliary->GetHOverBMAC();
247     if (HOverBMAC < 2.0)
248         vDirection(eZ) *= HOverBMAC / 2.0;
249
250     vDirection.Normalize();
251
252     vTurbulence = TurbGain*Magnitude * vDirection;
253     vTurbulenceGrad = TurbGain*MagnitudeAccel * vDirection;
254
255     vBodyTurbGrad = Propagate->GetTl2b()*vTurbulenceGrad;
256     vTurbPQR(eP) = vBodyTurbGrad(eY)/Aircraft->GetWingSpan();
257     if (Aircraft->GetHTailArm() != 0.0)
258       vTurbPQR(eQ) = vBodyTurbGrad(eZ)/Aircraft->GetHTailArm();
259     else
260       vTurbPQR(eQ) = vBodyTurbGrad(eZ)/10.0;
261
262     if (Aircraft->GetVTailArm())
263       vTurbPQR(eR) = vBodyTurbGrad(eX)/Aircraft->GetVTailArm();
264     else
265       vTurbPQR(eR) = vBodyTurbGrad(eX)/10.0;
266
267     break;
268   }
269   default:
270     break;
271   }
272 }
273
274 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
275 //    The bitmasked value choices are as follows:
276 //    unset: In this case (the default) JSBSim would only print
277 //       out the normally expected messages, essentially echoing
278 //       the config files as they are read. If the environment
279 //       variable is not set, debug_lvl is set to 1 internally
280 //    0: This requests JSBSim not to output any messages
281 //       whatsoever.
282 //    1: This value explicity requests the normal JSBSim
283 //       startup messages
284 //    2: This value asks for a message to be printed out when
285 //       a class is instantiated
286 //    4: When this value is set, a message is displayed when a
287 //       FGModel object executes its Run() method
288 //    8: When this value is set, various runtime state variables
289 //       are printed out periodically
290 //    16: When set various parameters are sanity checked and
291 //       a message is printed out when they go out of bounds
292
293 void FGMars::Debug(int from)
294 {
295   if (debug_lvl <= 0) return;
296
297   if (debug_lvl & 1) { // Standard console startup message output
298     if (from == 0) { // Constructor
299     }
300   }
301   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
302     if (from == 0) cout << "Instantiated: FGMars" << endl;
303     if (from == 1) cout << "Destroyed:    FGMars" << endl;
304   }
305   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
306   }
307   if (debug_lvl & 8 ) { // Runtime state variables
308   }
309   if (debug_lvl & 16) { // Sanity checking
310   }
311   if (debug_lvl & 32) { // Turbulence
312     if (first_pass && from == 2) {
313       cout << "vTurbulence(X), vTurbulence(Y), vTurbulence(Z), "
314            << "vTurbulenceGrad(X), vTurbulenceGrad(Y), vTurbulenceGrad(Z), "
315            << "vDirection(X), vDirection(Y), vDirection(Z), "
316            << "Magnitude, "
317            << "vTurbPQR(P), vTurbPQR(Q), vTurbPQR(R), " << endl;
318     } else if (from == 2) {
319       cout << vTurbulence << ", " << vTurbulenceGrad << ", " << vDirection << ", " << Magnitude << ", " << vTurbPQR << endl;
320     }
321   }
322   if (debug_lvl & 64) {
323     if (from == 0) { // Constructor
324       cout << IdSrc << endl;
325       cout << IdHdr << endl;
326     }
327   }
328 }
329
330 } // namespace JSBSim