1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 Purpose: Models the Martian atmosphere very simply
9 ------------- Copyright (C) 2004 Jon S. Berndt (jsb@hal-pc.org) -------------
11 This program is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free Software
13 Foundation; either version 2 of the License, or (at your option) any later
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 General Public License for more
21 You should have received a copy of the GNU 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.
25 Further information about the GNU General Public License can also be found on
26 the world wide web at http://www.gnu.org.
28 FUNCTIONAL DESCRIPTION
29 --------------------------------------------------------------------------------
30 Models the Martian atmosphere.
33 --------------------------------------------------------------------------------
36 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
37 COMMENTS, REFERENCES, and NOTES
38 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
40 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
49 static const char *IdSrc = "$Id$";
50 static const char *IdHdr = ID_MARS;
52 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
54 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
57 FGMars::FGMars(FGFDMExec* fdmex) : FGAtmosphere(fdmex)
67 MagnitudedAccelDt = MagnitudeAccel = Magnitude = 0.0;
69 turbType = ttStandard;
70 // turbType = ttBerndt;
79 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
87 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
89 bool FGMars::InitModel(void)
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;
111 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
113 bool FGMars::Run(void)
115 if (FGModel::Run()) return true;
116 if (FDMExec->Holding()) return false;
118 //do temp, pressure, and density first
120 h = Propagate->Geth();
124 if (turbType != ttNone) {
126 vWindNED += vTurbulence;
129 if (vWindNED(1) != 0.0) psiw = atan2( vWindNED(2), vWindNED(1) );
131 if (psiw < 0) psiw += 2*M_PI;
133 soundspeed = sqrt(SHRatio*Reng*(*temperature));
140 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
142 void FGMars::Calculate(double altitude)
144 //Calculate reftemp, refpress, and density
146 // LIMIT the temperatures so they do not descend below absolute zero.
148 if (altitude < 22960.0) {
149 intTemperature = -25.68 - 0.000548*altitude; // Deg Fahrenheit
151 intTemperature = -10.34 - 0.001217*altitude; // Deg Fahrenheit
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
156 //cout << "Atmosphere: h=" << altitude << " rho= " << intDensity << endl;
159 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
161 // square a value, but preserve the original sign
164 square_signed (double value)
167 return value * value * -1;
169 return value * value;
172 void FGMars::Turbulence(void)
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));
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();
188 vDirectiondAccelDt.Normalize();
190 // deemphasise non-vertical forces
191 vDirectiondAccelDt(eX) = square_signed(vDirectiondAccelDt(eX));
192 vDirectiondAccelDt(eY) = square_signed(vDirectiondAccelDt(eY));
194 vDirectionAccel += vDirectiondAccelDt*rate*TurbRate*State->Getdt();
195 vDirectionAccel.Normalize();
196 vDirection += vDirectionAccel*rate*State->Getdt();
198 vDirection.Normalize();
200 // Diminish turbulence within three wingspans
202 vTurbulence = TurbGain * Magnitude * vDirection;
203 double HOverBMAC = Auxiliary->GetHOverBMAC();
205 vTurbulence *= (HOverBMAC / 3.0) * (HOverBMAC / 3.0);
207 vTurbulenceGrad = TurbGain*MagnitudeAccel * vDirection;
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();
214 // vTurbPQR(eQ) = vBodyTurbGrad(eZ)/10.0;
216 if (Aircraft->GetVTailArm())
217 vTurbPQR(eR) = vBodyTurbGrad(eX)/Aircraft->GetVTailArm();
219 vTurbPQR(eR) = vBodyTurbGrad(eX)/10.0;
221 // Clear the horizontal forces
222 // actually felt by the plane, now
223 // that we've used them to calculate
225 vTurbulence(eX) = 0.0;
226 vTurbulence(eY) = 0.0;
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));
236 MagnitudedAccelDt = 1 - 2.0*(double(rand())/double(RAND_MAX)) - Magnitude;
237 MagnitudeAccel += MagnitudedAccelDt*rate*State->Getdt();
238 Magnitude += MagnitudeAccel*rate*State->Getdt();
240 vDirectiondAccelDt.Normalize();
241 vDirectionAccel += vDirectiondAccelDt*rate*State->Getdt();
242 vDirectionAccel.Normalize();
243 vDirection += vDirectionAccel*rate*State->Getdt();
245 // Diminish z-vector within two wingspans
247 double HOverBMAC = Auxiliary->GetHOverBMAC();
249 vDirection(eZ) *= HOverBMAC / 2.0;
251 vDirection.Normalize();
253 vTurbulence = TurbGain*Magnitude * vDirection;
254 vTurbulenceGrad = TurbGain*MagnitudeAccel * vDirection;
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();
261 vTurbPQR(eQ) = vBodyTurbGrad(eZ)/10.0;
263 if (Aircraft->GetVTailArm())
264 vTurbPQR(eR) = vBodyTurbGrad(eX)/Aircraft->GetVTailArm();
266 vTurbPQR(eR) = vBodyTurbGrad(eX)/10.0;
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
283 // 1: This value explicity requests the normal JSBSim
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
294 void FGMars::Debug(int from)
296 if (debug_lvl <= 0) return;
298 if (debug_lvl & 1) { // Standard console startup message output
299 if (from == 0) { // Constructor
302 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
303 if (from == 0) cout << "Instantiated: FGMars" << endl;
304 if (from == 1) cout << "Destroyed: FGMars" << endl;
306 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
308 if (debug_lvl & 8 ) { // Runtime state variables
310 if (debug_lvl & 16) { // Sanity checking
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), "
318 << "vTurbPQR(P), vTurbPQR(Q), vTurbPQR(R), " << endl;
319 } else if (from == 2) {
320 cout << vTurbulence << ", " << vTurbulenceGrad << ", " << vDirection << ", " << Magnitude << ", " << vTurbPQR << endl;
323 if (debug_lvl & 64) {
324 if (from == 0) { // Constructor
325 cout << IdSrc << endl;
326 cout << IdHdr << endl;
331 } // namespace JSBSim