]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGPiston.cpp
Frederic Bouvier:
[flightgear.git] / src / FDM / JSBSim / FGPiston.cpp
1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2
3  Module:       FGPiston.cpp
4  Author:       Jon S. Berndt, JSBSim framework
5                Dave Luff, Piston engine model
6  Date started: 09/12/2000
7  Purpose:      This module models a Piston engine
8
9  ------------- Copyright (C) 2000  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 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 General Public License for more
19  details.
20
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.
24
25  Further information about the GNU General Public License can also be found on
26  the world wide web at http://www.gnu.org.
27
28 FUNCTIONAL DESCRIPTION
29 --------------------------------------------------------------------------------
30
31 This class descends from the FGEngine class and models a Piston engine based on
32 parameters given in the engine config file for this class
33
34 HISTORY
35 --------------------------------------------------------------------------------
36 09/12/2000  JSB  Created
37
38 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
39 INCLUDES
40 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
41
42 #include <sstream>
43
44 #include "FGPiston.h"
45 #include "FGPropulsion.h"
46 #include "FGPropeller.h"
47
48 namespace JSBSim {
49
50 static const char *IdSrc = "$Id$";
51 static const char *IdHdr = ID_PISTON;
52
53 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
54 CLASS IMPLEMENTATION
55 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
56
57 FGPiston::FGPiston(FGFDMExec* exec, FGConfigFile* Eng_cfg, int engine_number)
58   : FGEngine(exec, engine_number),
59   R_air(287.3),
60   rho_fuel(800),                 // estimate
61   calorific_value_fuel(47.3e6),
62   Cp_air(1005),
63   Cp_fuel(1700)
64 {
65   string token;
66
67   Type = etPiston;
68   crank_counter = 0;
69   OilTemp_degK = 298;
70   MinManifoldPressure_inHg = 6.5;
71   MaxManifoldPressure_inHg = 28.5;
72   ManifoldPressure_inHg = Atmosphere->GetPressure() * psftoinhg; // psf to in Hg
73   minMAP = 21950;
74   maxMAP = 96250;
75   MAP = Atmosphere->GetPressure() * 47.88;  // psf to Pa
76   CylinderHeadTemp_degK = 0.0;
77   Displacement = 360;
78   MaxHP = 200;
79   Cycles = 2;
80   IdleRPM = 600;
81   Magnetos = 0;
82   ExhaustGasTemp_degK = 0.0;
83   EGT_degC = 0.0;
84
85   dt = State->Getdt();
86
87   // Supercharging
88   BoostSpeeds = 0;  // Default to no supercharging
89   BoostSpeed = 0;
90   Boosted = false;
91   BoostOverride = 0;
92   bBoostOverride = false;
93   bTakeoffBoost = false;
94   TakeoffBoost = 0.0;   // Default to no extra takeoff-boost
95   int i;
96   for(i=0; i<FG_MAX_BOOST_SPEEDS; ++i) {
97     RatedBoost[i] = 0.0;
98     RatedPower[i] = 0.0;
99     RatedAltitude[i] = 0.0;
100     BoostMul[i] = 1.0;
101     RatedMAP[i] = 100000;
102     RatedRPM[i] = 2500;
103     TakeoffMAP[i] = 100000;
104   }
105
106   // Initialisation
107   volumetric_efficiency = 0.8;  // Actually f(speed, load) but this will get us running
108
109   // First column is thi, second is neta (combustion efficiency)
110   Lookup_Combustion_Efficiency = new FGTable(12);
111   *Lookup_Combustion_Efficiency << 0.00 << 0.980;
112   *Lookup_Combustion_Efficiency << 0.90 << 0.980;
113   *Lookup_Combustion_Efficiency << 1.00 << 0.970;
114   *Lookup_Combustion_Efficiency << 1.05 << 0.950;
115   *Lookup_Combustion_Efficiency << 1.10 << 0.900;
116   *Lookup_Combustion_Efficiency << 1.15 << 0.850;
117   *Lookup_Combustion_Efficiency << 1.20 << 0.790;
118   *Lookup_Combustion_Efficiency << 1.30 << 0.700;
119   *Lookup_Combustion_Efficiency << 1.40 << 0.630;
120   *Lookup_Combustion_Efficiency << 1.50 << 0.570;
121   *Lookup_Combustion_Efficiency << 1.60 << 0.525;
122   *Lookup_Combustion_Efficiency << 2.00 << 0.345;
123
124   Power_Mixture_Correlation = new FGTable(13);
125   *Power_Mixture_Correlation << (14.7/1.6) << 78.0;
126   *Power_Mixture_Correlation << 10 <<  86.0;
127   *Power_Mixture_Correlation << 11 <<  93.5;
128   *Power_Mixture_Correlation << 12 <<  98.0;
129   *Power_Mixture_Correlation << 13 << 100.0;
130   *Power_Mixture_Correlation << 14 <<  99.0;
131   *Power_Mixture_Correlation << 15 <<  96.4;
132   *Power_Mixture_Correlation << 16 <<  92.5;
133   *Power_Mixture_Correlation << 17 <<  88.0;
134   *Power_Mixture_Correlation << 18 <<  83.0;
135   *Power_Mixture_Correlation << 19 <<  78.5;
136   *Power_Mixture_Correlation << 20 <<  74.0;
137   *Power_Mixture_Correlation << (14.7/0.6) << 58;
138
139   Name = Eng_cfg->GetValue("NAME");
140   Eng_cfg->GetNextConfigLine();
141   while (Eng_cfg->GetValue() != string("/FG_PISTON")) {
142     *Eng_cfg >> token;
143     if      (token == "MINMP") *Eng_cfg >> MinManifoldPressure_inHg;
144     else if (token == "MAXMP") *Eng_cfg >> MaxManifoldPressure_inHg;
145     else if (token == "DISPLACEMENT") *Eng_cfg >> Displacement;
146     else if (token == "MAXHP") *Eng_cfg >> MaxHP;
147     else if (token == "CYCLES") *Eng_cfg >> Cycles;
148     else if (token == "IDLERPM") *Eng_cfg >> IdleRPM;
149     else if (token == "MAXTHROTTLE") *Eng_cfg >> MaxThrottle;
150     else if (token == "MINTHROTTLE") *Eng_cfg >> MinThrottle;
151     else if (token == "NUMBOOSTSPEEDS") *Eng_cfg >> BoostSpeeds;
152     else if (token == "BOOSTOVERRIDE") *Eng_cfg >> BoostOverride;
153     else if (token == "TAKEOFFBOOST") *Eng_cfg >> TakeoffBoost;
154     else if (token == "RATEDBOOST1") *Eng_cfg >> RatedBoost[0];
155     else if (token == "RATEDBOOST2") *Eng_cfg >> RatedBoost[1];
156     else if (token == "RATEDBOOST3") *Eng_cfg >> RatedBoost[2];
157     else if (token == "RATEDPOWER1") *Eng_cfg >> RatedPower[0];
158     else if (token == "RATEDPOWER2") *Eng_cfg >> RatedPower[1];
159     else if (token == "RATEDPOWER3") *Eng_cfg >> RatedPower[2];
160     else if (token == "RATEDRPM1") *Eng_cfg >> RatedRPM[0];
161     else if (token == "RATEDRPM2") *Eng_cfg >> RatedRPM[1];
162     else if (token == "RATEDRPM3") *Eng_cfg >> RatedRPM[2];
163     else if (token == "RATEDALTITUDE1") *Eng_cfg >> RatedAltitude[0];
164     else if (token == "RATEDALTITUDE2") *Eng_cfg >> RatedAltitude[1];
165     else if (token == "RATEDALTITUDE3") *Eng_cfg >> RatedAltitude[2];
166     else cerr << "Unhandled token in Engine config file: " << token << endl;
167   }
168
169   minMAP = MinManifoldPressure_inHg * 3376.85;  // inHg to Pa
170   maxMAP = MaxManifoldPressure_inHg * 3376.85;
171
172   // Set up and sanity-check the turbo/supercharging configuration based on the input values.
173   if(TakeoffBoost > RatedBoost[0]) bTakeoffBoost = true;
174   for(i=0; i<BoostSpeeds; ++i) {
175     bool bad = false;
176     if(RatedBoost[i] <= 0.0) bad = true;
177     if(RatedPower[i] <= 0.0) bad = true;
178     if(RatedAltitude[i] < 0.0) bad = true;  // 0.0 is deliberately allowed - this corresponds to unregulated supercharging.
179     if(i > 0 && RatedAltitude[i] < RatedAltitude[i - 1]) bad = true;
180     if(bad) {
181       // We can't recover from the above - don't use this supercharger speed.
182       BoostSpeeds--;
183       // TODO - put out a massive error message!
184       break;
185     }
186     // Now sanity-check stuff that is recoverable.
187     if(i < BoostSpeeds - 1) {
188       if(BoostSwitchAltitude[i] < RatedAltitude[i]) {
189         // TODO - put out an error message
190         // But we can also make a reasonable estimate, as below.
191         BoostSwitchAltitude[i] = RatedAltitude[i] + 1000;
192       }
193       BoostSwitchPressure[i] = Atmosphere->GetPressure(BoostSwitchAltitude[i]) * 47.88;
194       //cout << "BoostSwitchAlt = " << BoostSwitchAltitude[i] << ", pressure = " << BoostSwitchPressure[i] << '\n';
195       // Assume there is some hysteresis on the supercharger gear switch, and guess the value for now
196       BoostSwitchHysteresis = 1000;
197     }
198     // Now work out the supercharger pressure multiplier of this speed from the rated boost and altitude.
199     RatedMAP[i] = Atmosphere->GetPressureSL() * 47.88 + RatedBoost[i] * 6895;  // psf*47.88 = Pa, psi*6895 = Pa.
200     // Sometimes a separate BCV setting for takeoff or extra power is fitted.
201     if(TakeoffBoost > RatedBoost[0]) {
202       // Assume that the effect on the BCV is the same whichever speed is in use.
203       TakeoffMAP[i] = RatedMAP[i] + ((TakeoffBoost - RatedBoost[0]) * 6895);
204       bTakeoffBoost = true;
205     } else {
206       TakeoffMAP[i] = RatedMAP[i];
207       bTakeoffBoost = false;
208     }
209     BoostMul[i] = RatedMAP[i] / (Atmosphere->GetPressure(RatedAltitude[i]) * 47.88);
210
211     // TODO - get rid of the debugging output before sending it to Jon
212     //cout << "Speed " << i+1 << '\n';
213     //cout << "BoostMul = " << BoostMul[i] << ", RatedMAP = " << RatedMAP[i] << ", TakeoffMAP = " << TakeoffMAP[i] << '\n';
214   }
215
216   if(BoostSpeeds > 0) {
217     Boosted = true;
218     BoostSpeed = 0;
219   }
220   bBoostOverride = (BoostOverride == 1 ? true : false);
221
222   //cout << "Engine is " << (Boosted ? "supercharged" : "naturally aspirated") << '\n';
223
224   Debug(0); // Call Debug() routine from constructor if needed
225 }
226
227 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
228
229 FGPiston::~FGPiston()
230 {
231   Debug(1); // Call Debug() routine from constructor if needed
232 }
233
234 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
235
236 double FGPiston::Calculate(void)
237 {
238   if (FuelFlow_gph > 0.0) ConsumeFuel();
239
240   Throttle = FCS->GetThrottlePos(EngineNumber);
241   Mixture = FCS->GetMixturePos(EngineNumber);
242
243   //
244   // Input values.
245   //
246
247   p_amb = Atmosphere->GetPressure() * 47.88;              // convert from lbs/ft2 to Pa
248   p_amb_sea_level = Atmosphere->GetPressureSL() * 47.88;
249   T_amb = Atmosphere->GetTemperature() * (5.0 / 9.0);  // convert from Rankine to Kelvin
250
251   RPM = Thruster->GetRPM() * Thruster->GetGearRatio();
252
253   IAS = Auxiliary->GetVcalibratedKTS();
254
255   doEngineStartup();
256   if(Boosted) doBoostControl();
257   doMAP();
258   doAirFlow();
259   doFuelFlow();
260
261   //Now that the fuel flow is done check if the mixture is too lean to run the engine
262   //Assume lean limit at 22 AFR for now - thats a thi of 0.668
263   //This might be a bit generous, but since there's currently no audiable warning of impending
264   //cutout in the form of misfiring and/or rough running its probably reasonable for now.
265   if (equivalence_ratio < 0.668)
266     Running = false;
267
268   doEnginePower();
269   doEGT();
270   doCHT();
271   doOilTemperature();
272   doOilPressure();
273
274   if (Thruster->GetType() == FGThruster::ttPropeller) {
275     ((FGPropeller*)Thruster)->SetAdvance(FCS->GetPropAdvance(EngineNumber));
276   }
277
278   PowerAvailable = (HP * hptoftlbssec) - Thruster->GetPowerRequired();
279
280   return Thruster->Calculate(PowerAvailable);
281 }
282
283 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
284 /**
285  * Start or stop the engine.
286  */
287
288 void FGPiston::doEngineStartup(void)
289 {
290   // Check parameters that may alter the operating state of the engine.
291   // (spark, fuel, starter motor etc)
292   bool spark;
293   bool fuel;
294
295   // Check for spark
296   Magneto_Left = false;
297   Magneto_Right = false;
298   // Magneto positions:
299   // 0 -> off
300   // 1 -> left only
301   // 2 -> right only
302   // 3 -> both
303   if (Magnetos != 0) {
304     spark = true;
305   } else {
306     spark = false;
307   }  // neglects battery voltage, master on switch, etc for now.
308
309   if ((Magnetos == 1) || (Magnetos > 2)) Magneto_Left = true;
310   if (Magnetos > 1)  Magneto_Right = true;
311
312   // Assume we have fuel for now
313   fuel = !Starved;
314
315   // Check if we are turning the starter motor
316   if (Cranking != Starter) {
317     // This check saves .../cranking from getting updated every loop - they
318     // only update when changed.
319     Cranking = Starter;
320     crank_counter = 0;
321   }
322
323   if (Cranking) crank_counter++;  //Check mode of engine operation
324
325   if (!Running && spark && fuel) {  // start the engine if revs high enough
326     if (Cranking) {
327       if ((RPM > 450) && (crank_counter > 175)) // Add a little delay to startup
328         Running = true;                         // on the starter
329     } else {
330       if (RPM > 450)                            // This allows us to in-air start
331         Running = true;                         // when windmilling
332     }
333   }
334
335   // Cut the engine *power* - Note: the engine may continue to
336   // spin if the prop is in a moving airstream
337
338   if ( Running && (!spark || !fuel) ) Running = false;
339
340   // Check for stalling (RPM = 0).
341   if (Running) {
342     if (RPM == 0) {
343       Running = false;
344     } else if ((RPM <= 480) && (Cranking)) {
345       Running = false;
346     }
347   }
348 }
349
350 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
351
352 /**
353  * Calculate the Current Boost Speed
354  *
355  * This function calculates the current turbo/supercharger boost speed
356  * based on altitude and the (automatic) boost-speed control valve configuration.
357  *
358  * Inputs: p_amb, BoostSwitchPressure, BoostSwitchHysteresis
359  *
360  * Outputs: BoostSpeed
361  */
362
363 void FGPiston::doBoostControl(void)
364 {
365   if(BoostSpeed < BoostSpeeds - 1) {
366     // Check if we need to change to a higher boost speed
367     if(p_amb < BoostSwitchPressure[BoostSpeed] - BoostSwitchHysteresis) {
368       BoostSpeed++;
369     }
370   } else if(BoostSpeed > 0) {
371     // Check if we need to change to a lower boost speed
372     if(p_amb > BoostSwitchPressure[BoostSpeed - 1] + BoostSwitchHysteresis) {
373       BoostSpeed--;
374     }
375   }
376 }
377
378 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
379
380 /**
381  * Calculate the manifold absolute pressure (MAP) in inches hg
382  *
383  * This function calculates manifold absolute pressure (MAP)
384  * from the throttle position, turbo/supercharger boost control
385  * system, engine speed and local ambient air density.
386  *
387  * TODO: changes in MP should not be instantaneous -- introduce
388  * a lag between throttle changes and MP changes, to allow pressure
389  * to build up or disperse.
390  *
391  * Inputs: minMAP, maxMAP, p_amb, Throttle
392  *
393  * Outputs: MAP, ManifoldPressure_inHg
394  */
395
396 void FGPiston::doMAP(void)
397 {
398   if(RPM > 10) {
399     // Naturally aspirated
400     MAP = minMAP + (Throttle * (maxMAP - minMAP));
401     MAP *= p_amb / p_amb_sea_level;
402     if(Boosted) {
403       // If takeoff boost is fitted, we currently assume the following throttle map:
404       // (In throttle % - actual input is 0 -> 1)
405       // 99 / 100 - Takeoff boost
406       // 96 / 97 / 98 - Rated boost
407       // 0 - 95 - Idle to Rated boost (MinManifoldPressure to MaxManifoldPressure)
408       // In real life, most planes would be fitted with a mechanical 'gate' between
409       // the rated boost and takeoff boost positions.
410       double T = Throttle; // processed throttle value.
411       bool bTakeoffPos = false;
412       if(bTakeoffBoost) {
413         if(Throttle > 0.98) {
414           //cout << "Takeoff Boost!!!!\n";
415           bTakeoffPos = true;
416         } else if(Throttle <= 0.95) {
417           bTakeoffPos = false;
418           T *= 1.0 / 0.95;
419         } else {
420           bTakeoffPos = false;
421           //cout << "Rated Boost!!\n";
422           T = 1.0;
423         }
424       }
425       // Boost the manifold pressure.
426       MAP *= BoostMul[BoostSpeed];
427       // Now clip the manifold pressure to BCV or Wastegate setting.
428       if(bTakeoffPos) {
429         if(MAP > TakeoffMAP[BoostSpeed]) {
430           MAP = TakeoffMAP[BoostSpeed];
431         }
432       } else {
433         if(MAP > RatedMAP[BoostSpeed]) {
434           MAP = RatedMAP[BoostSpeed];
435         }
436       }
437     }
438   } else {
439     // rpm < 10 - effectively stopped.
440     // TODO - add a better variation of MAP with engine speed
441     MAP = Atmosphere->GetPressure() * 47.88; // psf to Pa
442   }
443
444   // And set the value in American units as well
445   ManifoldPressure_inHg = MAP / 3376.85;
446 }
447
448 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
449 /**
450  * Calculate the air flow through the engine.
451  * Also calculates ambient air density
452  * (used in CHT calculation for air-cooled engines).
453  *
454  * Inputs: p_amb, R_air, T_amb, MAP, Displacement,
455  *   RPM, volumetric_efficiency
456  *
457  * TODO: Model inlet manifold air temperature.
458  *
459  * Outputs: rho_air, m_dot_air
460  */
461
462 void FGPiston::doAirFlow(void)
463 {
464   rho_air = p_amb / (R_air * T_amb);
465   double rho_air_manifold = MAP / (R_air * T_amb);
466   double displacement_SI = Displacement * in3tom3;
467   double swept_volume = (displacement_SI * (RPM/60)) / 2;
468   double v_dot_air = swept_volume * volumetric_efficiency;
469   m_dot_air = v_dot_air * rho_air_manifold;
470 }
471
472 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
473 /**
474  * Calculate the fuel flow into the engine.
475  *
476  * Inputs: Mixture, thi_sea_level, p_amb_sea_level, p_amb, m_dot_air
477  *
478  * Outputs: equivalence_ratio, m_dot_fuel
479  */
480
481 void FGPiston::doFuelFlow(void)
482 {
483   double thi_sea_level = 1.3 * Mixture;
484   equivalence_ratio = thi_sea_level * p_amb_sea_level / p_amb;
485   m_dot_fuel = m_dot_air / 14.7 * equivalence_ratio;
486   FuelFlow_gph = m_dot_fuel
487     * 3600                      // seconds to hours
488     * 2.2046                    // kg to lb
489     / 6.6;                      // lb to gal_us of kerosene
490 }
491
492 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
493 /**
494  * Calculate the power produced by the engine.
495  *
496  * Currently, the JSBSim propellor model does not allow the
497  * engine to produce enough RPMs to get up to a high horsepower.
498  * When tested with sufficient RPM, it has no trouble reaching
499  * 200HP.
500  *
501  * Inputs: ManifoldPressure_inHg, p_amb, p_amb_sea_level, RPM, T_amb,
502  *   equivalence_ratio, Cycles, MaxHP
503  *
504  * Outputs: Percentage_Power, HP
505  */
506
507 void FGPiston::doEnginePower(void)
508 {
509   if (Running) {
510     double T_amb_degF = KelvinToFahrenheit(T_amb);
511     double T_amb_sea_lev_degF = KelvinToFahrenheit(288);
512
513     // FIXME: this needs to be generalized
514     double ManXRPM;  // Convienience term for use in the calculations
515     if(Boosted) {
516       // Currently a simple linear fit.
517       // The zero crossing is moved up the speed-load range to reduce the idling power.
518       // This will change!
519       double zeroOffset = (minMAP / 2.0) * (IdleRPM / 2.0);
520       ManXRPM = MAP * (RPM > RatedRPM[BoostSpeed] ? RatedRPM[BoostSpeed] : RPM);
521       // The speed clip in the line above is deliberate.
522       Percentage_Power = ((ManXRPM - zeroOffset) / ((RatedMAP[BoostSpeed] * RatedRPM[BoostSpeed]) - zeroOffset)) * 107.0;
523       Percentage_Power -= 7.0;  // Another idle power reduction offset - see line above with 107.
524       if (Percentage_Power < 0.0) Percentage_Power = 0.0;
525       // Note that %power is allowed to go over 100 for boosted powerplants
526       // such as for the BCV-override or takeoff power settings.
527       // TODO - currently no altitude effect (temperature & exhaust back-pressure) modelled
528       // for boosted engines.
529     } else {
530       ManXRPM = ManifoldPressure_inHg * RPM; // Note that inHg must be used for the following correlation.
531       Percentage_Power = (6e-9 * ManXRPM * ManXRPM) + (8e-4 * ManXRPM) - 1.0;
532       Percentage_Power += ((T_amb_sea_lev_degF - T_amb_degF) * 7 /120);
533       if (Percentage_Power < 0.0) Percentage_Power = 0.0;
534       else if (Percentage_Power > 100.0) Percentage_Power = 100.0;
535     }
536
537     double Percentage_of_best_power_mixture_power =
538       Power_Mixture_Correlation->GetValue(14.7 / equivalence_ratio);
539
540     Percentage_Power *= Percentage_of_best_power_mixture_power / 100.0;
541
542     if(Boosted) {
543       HP = Percentage_Power * RatedPower[BoostSpeed] / 100.0;
544     } else {
545       HP = Percentage_Power * MaxHP / 100.0;
546     }
547
548   } else {
549
550     // Power output when the engine is not running
551     if (Cranking) {
552       if (RPM < 10) {
553         HP = 3.0;   // This is a hack to prevent overshooting the idle rpm in
554                     // the first time step. It may possibly need to be changed
555                     // if the prop model is changed.
556       } else if (RPM < 480) {
557         HP = 3.0 + ((480 - RPM) / 10.0);
558         // This is a guess - would be nice to find a proper starter moter torque curve
559       } else {
560         HP = 3.0;
561       }
562     } else {
563       // Quick hack until we port the FMEP stuff
564       if (RPM > 0.0)
565         HP = -1.5;
566       else
567         HP = 0.0;
568     }
569   }
570   //cout << "Power = " << HP << '\n';
571 }
572
573 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
574 /**
575  * Calculate the exhaust gas temperature.
576  *
577  * Inputs: equivalence_ratio, m_dot_fuel, calorific_value_fuel,
578  *   Cp_air, m_dot_air, Cp_fuel, m_dot_fuel, T_amb, Percentage_Power
579  *
580  * Outputs: combustion_efficiency, ExhaustGasTemp_degK
581  */
582
583 void FGPiston::doEGT(void)
584 {
585   double delta_T_exhaust;
586   double enthalpy_exhaust;
587   double heat_capacity_exhaust;
588   double dEGTdt;
589
590   if ((Running) && (m_dot_air > 0.0)) {  // do the energy balance
591     combustion_efficiency = Lookup_Combustion_Efficiency->GetValue(equivalence_ratio);
592     enthalpy_exhaust = m_dot_fuel * calorific_value_fuel *
593                               combustion_efficiency * 0.33;
594     heat_capacity_exhaust = (Cp_air * m_dot_air) + (Cp_fuel * m_dot_fuel);
595     delta_T_exhaust = enthalpy_exhaust / heat_capacity_exhaust;
596     ExhaustGasTemp_degK = T_amb + delta_T_exhaust;
597     ExhaustGasTemp_degK *= 0.444 + ((0.544 - 0.444) * Percentage_Power / 100.0);
598   } else {  // Drop towards ambient - guess an appropriate time constant for now
599     dEGTdt = (298.0 - ExhaustGasTemp_degK) / 100.0;
600     delta_T_exhaust = dEGTdt * dt;
601     ExhaustGasTemp_degK += delta_T_exhaust;
602   }
603 }
604
605 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
606 /**
607  * Calculate the cylinder head temperature.
608  *
609  * Inputs: T_amb, IAS, rho_air, m_dot_fuel, calorific_value_fuel,
610  *   combustion_efficiency, RPM
611  *
612  * Outputs: CylinderHeadTemp_degK
613  */
614
615 void FGPiston::doCHT(void)
616 {
617   double h1 = -95.0;
618   double h2 = -3.95;
619   double h3 = -0.05;
620
621   double arbitary_area = 1.0;
622   double CpCylinderHead = 800.0;
623   double MassCylinderHead = 8.0;
624
625   double temperature_difference = CylinderHeadTemp_degK - T_amb;
626   double v_apparent = IAS * 0.5144444;
627   double v_dot_cooling_air = arbitary_area * v_apparent;
628   double m_dot_cooling_air = v_dot_cooling_air * rho_air;
629   double dqdt_from_combustion =
630     m_dot_fuel * calorific_value_fuel * combustion_efficiency * 0.33;
631   double dqdt_forced = (h2 * m_dot_cooling_air * temperature_difference) +
632     (h3 * RPM * temperature_difference);
633   double dqdt_free = h1 * temperature_difference;
634   double dqdt_cylinder_head = dqdt_from_combustion + dqdt_forced + dqdt_free;
635
636   double HeatCapacityCylinderHead = CpCylinderHead * MassCylinderHead;
637
638   CylinderHeadTemp_degK +=
639     (dqdt_cylinder_head / HeatCapacityCylinderHead) * dt;
640 }
641
642 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
643 /**
644  * Calculate the oil temperature.
645  *
646  * Inputs: Percentage_Power, running flag.
647  *
648  * Outputs: OilTemp_degK
649  */
650
651 void FGPiston::doOilTemperature(void)
652 {
653   double idle_percentage_power = 2.3;        // approximately
654   double target_oil_temp;        // Steady state oil temp at the current engine conditions
655   double time_constant;          // The time constant for the differential equation
656
657   if (Running) {
658     target_oil_temp = 363;
659     time_constant = 500;        // Time constant for engine-on idling.
660     if (Percentage_Power > idle_percentage_power) {
661       time_constant /= ((Percentage_Power / idle_percentage_power) / 10.0); // adjust for power
662     }
663   } else {
664     target_oil_temp = 298;
665     time_constant = 1000;  // Time constant for engine-off; reflects the fact
666                            // that oil is no longer getting circulated
667   }
668
669   double dOilTempdt = (target_oil_temp - OilTemp_degK) / time_constant;
670
671   OilTemp_degK += (dOilTempdt * dt);
672 }
673
674 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
675 /**
676  * Calculate the oil pressure.
677  *
678  * Inputs: RPM
679  *
680  * Outputs: OilPressure_psi
681  */
682
683 void FGPiston::doOilPressure(void)
684 {
685   double Oil_Press_Relief_Valve = 60; // FIXME: may vary by engine
686   double Oil_Press_RPM_Max = 1800;    // FIXME: may vary by engine
687   double Design_Oil_Temp = 358;       // degK; FIXME: may vary by engine
688   double Oil_Viscosity_Index = 0.25;
689
690   OilPressure_psi = (Oil_Press_Relief_Valve / Oil_Press_RPM_Max) * RPM;
691
692   if (OilPressure_psi >= Oil_Press_Relief_Valve) {
693     OilPressure_psi = Oil_Press_Relief_Valve;
694   }
695
696   OilPressure_psi += (Design_Oil_Temp - OilTemp_degK) * Oil_Viscosity_Index;
697 }
698
699 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
700
701 string FGPiston::GetEngineLabels(void)
702 {
703   std::ostringstream buf;
704
705   buf << Name << "_PwrAvail[" << EngineNumber << "], "
706       << Thruster->GetThrusterLabels(EngineNumber);
707
708   return buf.str();
709 }
710
711 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
712
713 string FGPiston::GetEngineValues(void)
714 {
715   std::ostringstream buf;
716
717   buf << PowerAvailable << ", " << Thruster->GetThrusterValues(EngineNumber);
718
719   return buf.str();
720 }
721
722 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
723 //
724 //    The bitmasked value choices are as follows:
725 //    unset: In this case (the default) JSBSim would only print
726 //       out the normally expected messages, essentially echoing
727 //       the config files as they are read. If the environment
728 //       variable is not set, debug_lvl is set to 1 internally
729 //    0: This requests JSBSim not to output any messages
730 //       whatsoever.
731 //    1: This value explicity requests the normal JSBSim
732 //       startup messages
733 //    2: This value asks for a message to be printed out when
734 //       a class is instantiated
735 //    4: When this value is set, a message is displayed when a
736 //       FGModel object executes its Run() method
737 //    8: When this value is set, various runtime state variables
738 //       are printed out periodically
739 //    16: When set various parameters are sanity checked and
740 //       a message is printed out when they go out of bounds
741
742 void FGPiston::Debug(int from)
743 {
744   if (debug_lvl <= 0) return;
745
746   if (debug_lvl & 1) { // Standard console startup message output
747     if (from == 0) { // Constructor
748
749       cout << "\n    Engine Name: "         << Name << endl;
750       cout << "      MinManifoldPressure: " << MinManifoldPressure_inHg << endl;
751       cout << "      MaxManifoldPressure: " << MaxManifoldPressure_inHg << endl;
752       cout << "      Displacement: "        << Displacement             << endl;
753       cout << "      MaxHP: "               << MaxHP                    << endl;
754       cout << "      Cycles: "              << Cycles                   << endl;
755       cout << "      IdleRPM: "             << IdleRPM                  << endl;
756       cout << "      MaxThrottle: "         << MaxThrottle              << endl;
757       cout << "      MinThrottle: "         << MinThrottle              << endl;
758
759       cout << endl;
760       cout << "      Combustion Efficiency table:" << endl;
761       Lookup_Combustion_Efficiency->Print();
762       cout << endl;
763
764       cout << endl;
765       cout << "      Power Mixture Correlation table:" << endl;
766       Power_Mixture_Correlation->Print();
767       cout << endl;
768
769     }
770   }
771   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
772     if (from == 0) cout << "Instantiated: FGPiston" << endl;
773     if (from == 1) cout << "Destroyed:    FGPiston" << endl;
774   }
775   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
776   }
777   if (debug_lvl & 8 ) { // Runtime state variables
778   }
779   if (debug_lvl & 16) { // Sanity checking
780   }
781   if (debug_lvl & 64) {
782     if (from == 0) { // Constructor
783       cout << IdSrc << endl;
784       cout << IdHdr << endl;
785     }
786   }
787 }
788
789 double
790 FGPiston::CalcFuelNeed(void)
791 {
792   return FuelFlow_gph / 3600 * 6 * State->Getdt() * Propulsion->GetRate();
793 }
794
795 } // namespace JSBSim