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