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