]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/models/propulsion/FGPiston.cpp
Sync w. JSBSim CVS (merge from PRE_OSG_PLIB_20061029 branch)
[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 Lesser General Public License as published by the Free Software
13  Foundation; either version 2 of the License, or (at your option) any later
14  version.
15
16  This program is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18  FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
19  details.
20
21  You should have received a copy of the GNU Lesser General Public License along with
22  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
23  Place - Suite 330, Boston, MA  02111-1307, USA.
24
25  Further information about the GNU Lesser General Public License can also be found on
26  the world wide web at http://www.gnu.org.
27
28 FUNCTIONAL DESCRIPTION
29 --------------------------------------------------------------------------------
30
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 = RankineToKelvin(Atmosphere->GetTemperature());
84   ManifoldPressure_inHg = Atmosphere->GetPressure() * psftoinhg; // psf to in Hg
85   minMAP = 21950;
86   maxMAP = 96250;
87   MAP = Atmosphere->GetPressure() * psftopa;
88   CylinderHeadTemp_degK = RankineToKelvin(Atmosphere->GetTemperature());
89   Magnetos = 0;
90   ExhaustGasTemp_degK = RankineToKelvin(Atmosphere->GetTemperature());
91   EGT_degC = ExhaustGasTemp_degK - 273;
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 * 3386.38;  // inHg to Pa
200   maxMAP = MaxManifoldPressure_inHg * 3386.38;
201   StarterHP = sqrt(MaxHP) * 0.4;
202
203   // Set up and sanity-check the turbo/supercharging configuration based on the input values.
204   if (TakeoffBoost > RatedBoost[0]) bTakeoffBoost = true;
205   for (i=0; i<BoostSpeeds; ++i) {
206     bool bad = false;
207     if (RatedBoost[i] <= 0.0) bad = true;
208     if (RatedPower[i] <= 0.0) bad = true;
209     if (RatedAltitude[i] < 0.0) bad = true;  // 0.0 is deliberately allowed - this corresponds to unregulated supercharging.
210     if (i > 0 && RatedAltitude[i] < RatedAltitude[i - 1]) bad = true;
211     if (bad) {
212       // We can't recover from the above - don't use this supercharger speed.
213       BoostSpeeds--;
214       // TODO - put out a massive error message!
215       break;
216     }
217     // Now sanity-check stuff that is recoverable.
218     if (i < BoostSpeeds - 1) {
219       if (BoostSwitchAltitude[i] < RatedAltitude[i]) {
220         // TODO - put out an error message
221         // But we can also make a reasonable estimate, as below.
222         BoostSwitchAltitude[i] = RatedAltitude[i] + 1000;
223       }
224       BoostSwitchPressure[i] = Atmosphere->GetPressure(BoostSwitchAltitude[i]) * psftopa;
225       //cout << "BoostSwitchAlt = " << BoostSwitchAltitude[i] << ", pressure = " << BoostSwitchPressure[i] << '\n';
226       // Assume there is some hysteresis on the supercharger gear switch, and guess the value for now
227       BoostSwitchHysteresis = 1000;
228     }
229     // Now work out the supercharger pressure multiplier of this speed from the rated boost and altitude.
230     RatedMAP[i] = Atmosphere->GetPressureSL() * psftopa + RatedBoost[i] * 6895;  // psi*6895 = Pa.
231     // Sometimes a separate BCV setting for takeoff or extra power is fitted.
232     if (TakeoffBoost > RatedBoost[0]) {
233       // Assume that the effect on the BCV is the same whichever speed is in use.
234       TakeoffMAP[i] = RatedMAP[i] + ((TakeoffBoost - RatedBoost[0]) * 6895);
235       bTakeoffBoost = true;
236     } else {
237       TakeoffMAP[i] = RatedMAP[i];
238       bTakeoffBoost = false;
239     }
240     BoostMul[i] = RatedMAP[i] / (Atmosphere->GetPressure(RatedAltitude[i]) * psftopa);
241
242   }
243
244   if (BoostSpeeds > 0) {
245     Boosted = true;
246     BoostSpeed = 0;
247   }
248   bBoostOverride = (BoostOverride == 1 ? true : false);
249
250   Debug(0); // Call Debug() routine from constructor if needed
251 }
252
253 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
254
255 FGPiston::~FGPiston()
256 {
257   Debug(1); // Call Debug() routine from constructor if needed
258 }
259
260 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
261
262 double FGPiston::Calculate(void)
263 {
264   if (FuelFlow_gph > 0.0) ConsumeFuel();
265
266   Throttle = FCS->GetThrottlePos(EngineNumber);
267   Mixture = FCS->GetMixturePos(EngineNumber);
268
269   //
270   // Input values.
271   //
272
273   p_amb = Atmosphere->GetPressure() * psftopa;
274   p_amb_sea_level = Atmosphere->GetPressureSL() * psftopa;
275   T_amb = RankineToKelvin(Atmosphere->GetTemperature());
276
277   RPM = Thruster->GetRPM() * Thruster->GetGearRatio();
278
279   IAS = Auxiliary->GetVcalibratedKTS();
280
281   doEngineStartup();
282   if (Boosted) doBoostControl();
283   doMAP();
284   doAirFlow();
285   doFuelFlow();
286
287   //Now that the fuel flow is done check if the mixture is too lean to run the engine
288   //Assume lean limit at 22 AFR for now - thats a thi of 0.668
289   //This might be a bit generous, but since there's currently no audiable warning of impending
290   //cutout in the form of misfiring and/or rough running its probably reasonable for now.
291   if (equivalence_ratio < 0.668)
292     Running = false;
293
294   doEnginePower();
295   doEGT();
296   doCHT();
297   doOilTemperature();
298   doOilPressure();
299
300   if (Thruster->GetType() == FGThruster::ttPropeller) {
301     ((FGPropeller*)Thruster)->SetAdvance(FCS->GetPropAdvance(EngineNumber));
302     ((FGPropeller*)Thruster)->SetFeather(FCS->GetPropFeather(EngineNumber));
303   }
304
305   PowerAvailable = (HP * hptoftlbssec) - Thruster->GetPowerRequired();
306
307   return Thruster->Calculate(PowerAvailable);
308 }
309
310 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
311 /**
312  * Start or stop the engine.
313  */
314
315 void FGPiston::doEngineStartup(void)
316 {
317   // Check parameters that may alter the operating state of the engine.
318   // (spark, fuel, starter motor etc)
319   bool spark;
320   bool fuel;
321
322   // Check for spark
323   Magneto_Left = false;
324   Magneto_Right = false;
325   // Magneto positions:
326   // 0 -> off
327   // 1 -> left only
328   // 2 -> right only
329   // 3 -> both
330   if (Magnetos != 0) {
331     spark = true;
332   } else {
333     spark = false;
334   }  // neglects battery voltage, master on switch, etc for now.
335
336   if ((Magnetos == 1) || (Magnetos > 2)) Magneto_Left = true;
337   if (Magnetos > 1)  Magneto_Right = true;
338
339   // Assume we have fuel for now
340   fuel = !Starved;
341
342   // Check if we are turning the starter motor
343   if (Cranking != Starter) {
344     // This check saves .../cranking from getting updated every loop - they
345     // only update when changed.
346     Cranking = Starter;
347     crank_counter = 0;
348   }
349
350   if (Cranking) crank_counter++;  //Check mode of engine operation
351
352   if (!Running && spark && fuel) {  // start the engine if revs high enough
353     if (Cranking) {
354       if ((RPM > 450) && (crank_counter > 175)) // Add a little delay to startup
355         Running = true;                         // on the starter
356     } else {
357       if (RPM > 450)                            // This allows us to in-air start
358         Running = true;                         // when windmilling
359     }
360   }
361
362   // Cut the engine *power* - Note: the engine may continue to
363   // spin if the prop is in a moving airstream
364
365   if ( Running && (!spark || !fuel) ) Running = false;
366
367   // Check for stalling (RPM = 0).
368   if (Running) {
369     if (RPM == 0) {
370       Running = false;
371     } else if ((RPM <= 480) && (Cranking)) {
372       Running = false;
373     }
374   }
375 }
376
377 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
378
379 /**
380  * Calculate the Current Boost Speed
381  *
382  * This function calculates the current turbo/supercharger boost speed
383  * based on altitude and the (automatic) boost-speed control valve configuration.
384  *
385  * Inputs: p_amb, BoostSwitchPressure, BoostSwitchHysteresis
386  *
387  * Outputs: BoostSpeed
388  */
389
390 void FGPiston::doBoostControl(void)
391 {
392   if(BoostSpeed < BoostSpeeds - 1) {
393     // Check if we need to change to a higher boost speed
394     if(p_amb < BoostSwitchPressure[BoostSpeed] - BoostSwitchHysteresis) {
395       BoostSpeed++;
396     }
397   } else if(BoostSpeed > 0) {
398     // Check if we need to change to a lower boost speed
399     if(p_amb > BoostSwitchPressure[BoostSpeed - 1] + BoostSwitchHysteresis) {
400       BoostSpeed--;
401     }
402   }
403 }
404
405 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
406
407 /**
408  * Calculate the manifold absolute pressure (MAP) in inches hg
409  *
410  * This function calculates manifold absolute pressure (MAP)
411  * from the throttle position, turbo/supercharger boost control
412  * system, engine speed and local ambient air density.
413  *
414  * TODO: changes in MP should not be instantaneous -- introduce
415  * a lag between throttle changes and MP changes, to allow pressure
416  * to build up or disperse.
417  *
418  * Inputs: minMAP, maxMAP, p_amb, Throttle
419  *
420  * Outputs: MAP, ManifoldPressure_inHg
421  */
422
423 void FGPiston::doMAP(void)
424 {
425   if(RPM > 10) {
426     // Naturally aspirated
427     MAP = minMAP + (Throttle * (maxMAP - minMAP));
428     MAP *= p_amb / p_amb_sea_level;
429     if(Boosted) {
430       // If takeoff boost is fitted, we currently assume the following throttle map:
431       // (In throttle % - actual input is 0 -> 1)
432       // 99 / 100 - Takeoff boost
433       // 96 / 97 / 98 - Rated boost
434       // 0 - 95 - Idle to Rated boost (MinManifoldPressure to MaxManifoldPressure)
435       // In real life, most planes would be fitted with a mechanical 'gate' between
436       // the rated boost and takeoff boost positions.
437       double T = Throttle; // processed throttle value.
438       bool bTakeoffPos = false;
439       if(bTakeoffBoost) {
440         if(Throttle > 0.98) {
441           //cout << "Takeoff Boost!!!!\n";
442           bTakeoffPos = true;
443         } else if(Throttle <= 0.95) {
444           bTakeoffPos = false;
445           T *= 1.0 / 0.95;
446         } else {
447           bTakeoffPos = false;
448           //cout << "Rated Boost!!\n";
449           T = 1.0;
450         }
451       }
452       // Boost the manifold pressure.
453       MAP *= BoostMul[BoostSpeed];
454       // Now clip the manifold pressure to BCV or Wastegate setting.
455       if(bTakeoffPos) {
456         if(MAP > TakeoffMAP[BoostSpeed]) {
457           MAP = TakeoffMAP[BoostSpeed];
458         }
459       } else {
460         if(MAP > RatedMAP[BoostSpeed]) {
461           MAP = RatedMAP[BoostSpeed];
462         }
463       }
464     }
465   } else {
466     // rpm < 10 - effectively stopped.
467     // TODO - add a better variation of MAP with engine speed
468     MAP = Atmosphere->GetPressure() * psftopa;
469   }
470
471   // And set the value in American units as well
472   ManifoldPressure_inHg = MAP / 3386.38;
473 }
474
475 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
476 /**
477  * Calculate the air flow through the engine.
478  * Also calculates ambient air density
479  * (used in CHT calculation for air-cooled engines).
480  *
481  * Inputs: p_amb, R_air, T_amb, MAP, Displacement,
482  *   RPM, volumetric_efficiency
483  *
484  * TODO: Model inlet manifold air temperature.
485  *
486  * Outputs: rho_air, m_dot_air
487  */
488
489 void FGPiston::doAirFlow(void)
490 {
491   rho_air = p_amb / (R_air * T_amb);
492   double rho_air_manifold = MAP / (R_air * T_amb);
493   double displacement_SI = Displacement * in3tom3;
494   double swept_volume = (displacement_SI * (RPM/60)) / 2;
495   double v_dot_air = swept_volume * volumetric_efficiency;
496   m_dot_air = v_dot_air * rho_air_manifold;
497 }
498
499 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
500 /**
501  * Calculate the fuel flow into the engine.
502  *
503  * Inputs: Mixture, thi_sea_level, p_amb_sea_level, p_amb, m_dot_air
504  *
505  * Outputs: equivalence_ratio, m_dot_fuel
506  */
507
508 void FGPiston::doFuelFlow(void)
509 {
510   double thi_sea_level = 1.3 * Mixture;
511   equivalence_ratio = thi_sea_level * p_amb_sea_level / p_amb;
512   m_dot_fuel = m_dot_air / 14.7 * equivalence_ratio;
513   FuelFlow_gph = m_dot_fuel
514     * 3600                      // seconds to hours
515     * 2.2046                    // kg to lb
516     / 6.6;                      // lb to gal_us of kerosene
517 }
518
519 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
520 /**
521  * Calculate the power produced by the engine.
522  *
523  * Currently, the JSBSim propellor model does not allow the
524  * engine to produce enough RPMs to get up to a high horsepower.
525  * When tested with sufficient RPM, it has no trouble reaching
526  * 200HP.
527  *
528  * Inputs: ManifoldPressure_inHg, p_amb, p_amb_sea_level, RPM, T_amb,
529  *   equivalence_ratio, Cycles, MaxHP
530  *
531  * Outputs: Percentage_Power, HP
532  */
533
534 void FGPiston::doEnginePower(void)
535 {
536   if (Running) {
537     double T_amb_degF = KelvinToFahrenheit(T_amb);
538     double T_amb_sea_lev_degF = KelvinToFahrenheit(288);
539
540     // FIXME: this needs to be generalized
541     double ManXRPM;  // Convienience term for use in the calculations
542     if(Boosted) {
543       // Currently a simple linear fit.
544       // The zero crossing is moved up the speed-load range to reduce the idling power.
545       // This will change!
546       double zeroOffset = (minMAP / 2.0) * (IdleRPM / 2.0);
547       ManXRPM = MAP * (RPM > RatedRPM[BoostSpeed] ? RatedRPM[BoostSpeed] : RPM);
548       // The speed clip in the line above is deliberate.
549       Percentage_Power = ((ManXRPM - zeroOffset) / ((RatedMAP[BoostSpeed] * RatedRPM[BoostSpeed]) - zeroOffset)) * 107.0;
550       Percentage_Power -= 7.0;  // Another idle power reduction offset - see line above with 107.
551       if (Percentage_Power < 0.0) Percentage_Power = 0.0;
552       // Note that %power is allowed to go over 100 for boosted powerplants
553       // such as for the BCV-override or takeoff power settings.
554       // TODO - currently no altitude effect (temperature & exhaust back-pressure) modelled
555       // for boosted engines.
556     } else {
557       ManXRPM = ManifoldPressure_inHg * RPM; // Note that inHg must be used for the following correlation.
558       Percentage_Power = (6e-9 * ManXRPM * ManXRPM) + (8e-4 * ManXRPM) - 1.0;
559 //      Percentage_Power += ((T_amb_sea_lev_degF - T_amb_degF) * 7 /120);
560       Percentage_Power += ((T_amb_sea_lev_degF - T_amb_degF) * 7 * dt);
561       if (Percentage_Power < 0.0) Percentage_Power = 0.0;
562       else if (Percentage_Power > 100.0) Percentage_Power = 100.0;
563     }
564
565     double Percentage_of_best_power_mixture_power =
566       Power_Mixture_Correlation->GetValue(14.7 / equivalence_ratio);
567
568     Percentage_Power *= Percentage_of_best_power_mixture_power / 100.0;
569
570     if (Boosted) {
571       HP = Percentage_Power * RatedPower[BoostSpeed] / 100.0;
572     } else {
573       HP = Percentage_Power * MaxHP / 100.0;
574     }
575
576   } else {
577
578     // Power output when the engine is not running
579     if (Cranking) {
580       if (RPM < 10) {
581         HP = StarterHP;
582       } else if (RPM < 480) {
583         HP = StarterHP + ((480 - RPM) / 8.0);
584         // This is a guess - would be nice to find a proper starter moter torque curve
585       } else {
586         HP = StarterHP;
587       }
588     } else {
589       // Quick hack until we port the FMEP stuff
590       if (RPM > 0.0)
591         HP = -1.5;
592       else
593         HP = 0.0;
594     }
595   }
596 //  cout << "Power = " << HP << "  RPM = " << RPM << "  Running = " << Running << "  Cranking = " << Cranking << endl;
597 }
598
599 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
600 /**
601  * Calculate the exhaust gas temperature.
602  *
603  * Inputs: equivalence_ratio, m_dot_fuel, calorific_value_fuel,
604  *   Cp_air, m_dot_air, Cp_fuel, m_dot_fuel, T_amb, Percentage_Power
605  *
606  * Outputs: combustion_efficiency, ExhaustGasTemp_degK
607  */
608
609 void FGPiston::doEGT(void)
610 {
611   double delta_T_exhaust;
612   double enthalpy_exhaust;
613   double heat_capacity_exhaust;
614   double dEGTdt;
615
616   if ((Running) && (m_dot_air > 0.0)) {  // do the energy balance
617     combustion_efficiency = Lookup_Combustion_Efficiency->GetValue(equivalence_ratio);
618     enthalpy_exhaust = m_dot_fuel * calorific_value_fuel *
619                               combustion_efficiency * 0.33;
620     heat_capacity_exhaust = (Cp_air * m_dot_air) + (Cp_fuel * m_dot_fuel);
621     delta_T_exhaust = enthalpy_exhaust / heat_capacity_exhaust;
622     ExhaustGasTemp_degK = T_amb + delta_T_exhaust;
623     ExhaustGasTemp_degK *= 0.444 + ((0.544 - 0.444) * Percentage_Power / 100.0);
624   } else {  // Drop towards ambient - guess an appropriate time constant for now
625     combustion_efficiency = 0;
626     dEGTdt = (RankineToKelvin(Atmosphere->GetTemperature()) - ExhaustGasTemp_degK) / 100.0;
627     delta_T_exhaust = dEGTdt * dt;
628     ExhaustGasTemp_degK += delta_T_exhaust;
629   }
630 }
631
632 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
633 /**
634  * Calculate the cylinder head temperature.
635  *
636  * Inputs: T_amb, IAS, rho_air, m_dot_fuel, calorific_value_fuel,
637  *   combustion_efficiency, RPM
638  *
639  * Outputs: CylinderHeadTemp_degK
640  */
641
642 void FGPiston::doCHT(void)
643 {
644   double h1 = -95.0;
645   double h2 = -3.95;
646   double h3 = -0.05;
647
648   double arbitary_area = 1.0;
649   double CpCylinderHead = 800.0;
650   double MassCylinderHead = 8.0;
651
652   double temperature_difference = CylinderHeadTemp_degK - T_amb;
653   double v_apparent = IAS * 0.5144444;
654   double v_dot_cooling_air = arbitary_area * v_apparent;
655   double m_dot_cooling_air = v_dot_cooling_air * rho_air;
656   double dqdt_from_combustion =
657     m_dot_fuel * calorific_value_fuel * combustion_efficiency * 0.33;
658   double dqdt_forced = (h2 * m_dot_cooling_air * temperature_difference) +
659     (h3 * RPM * temperature_difference);
660   double dqdt_free = h1 * temperature_difference;
661   double dqdt_cylinder_head = dqdt_from_combustion + dqdt_forced + dqdt_free;
662
663   double HeatCapacityCylinderHead = CpCylinderHead * MassCylinderHead;
664
665   CylinderHeadTemp_degK +=
666     (dqdt_cylinder_head / HeatCapacityCylinderHead) * dt;
667 }
668
669 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
670 /**
671  * Calculate the oil temperature.
672  *
673  * Inputs: Percentage_Power, running flag.
674  *
675  * Outputs: OilTemp_degK
676  */
677
678 void FGPiston::doOilTemperature(void)
679 {
680   double idle_percentage_power = 2.3;        // approximately
681   double target_oil_temp;        // Steady state oil temp at the current engine conditions
682   double time_constant;          // The time constant for the differential equation
683
684   if (Running) {
685     target_oil_temp = 363;
686     time_constant = 500;        // Time constant for engine-on idling.
687     if (Percentage_Power > idle_percentage_power) {
688       time_constant /= ((Percentage_Power / idle_percentage_power) / 10.0); // adjust for power
689     }
690   } else {
691     target_oil_temp = RankineToKelvin(Atmosphere->GetTemperature());
692     time_constant = 1000;  // Time constant for engine-off; reflects the fact
693                            // that oil is no longer getting circulated
694   }
695
696   double dOilTempdt = (target_oil_temp - OilTemp_degK) / time_constant;
697
698   OilTemp_degK += (dOilTempdt * dt);
699 }
700
701 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
702 /**
703  * Calculate the oil pressure.
704  *
705  * Inputs: RPM
706  *
707  * Outputs: OilPressure_psi
708  */
709
710 void FGPiston::doOilPressure(void)
711 {
712   double Oil_Press_Relief_Valve = 60; // FIXME: may vary by engine
713   double Oil_Press_RPM_Max = 1800;    // FIXME: may vary by engine
714   double Design_Oil_Temp = 358;       // degK; FIXME: may vary by engine
715   double Oil_Viscosity_Index = 0.25;
716
717   OilPressure_psi = (Oil_Press_Relief_Valve / Oil_Press_RPM_Max) * RPM;
718
719   if (OilPressure_psi >= Oil_Press_Relief_Valve) {
720     OilPressure_psi = Oil_Press_Relief_Valve;
721   }
722
723   OilPressure_psi += (Design_Oil_Temp - OilTemp_degK) * Oil_Viscosity_Index;
724 }
725
726 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
727
728 string FGPiston::GetEngineLabels(string delimeter)
729 {
730   std::ostringstream buf;
731
732   buf << Name << " Power Available (engine " << EngineNumber << " in HP)" << delimeter
733       << Name << " HP (engine " << EngineNumber << ")" << delimeter
734       << Name << " equivalent ratio (engine " << EngineNumber << ")" << delimeter
735       << Name << " MAP (engine " << EngineNumber << ")" << delimeter
736       << Thruster->GetThrusterLabels(EngineNumber, delimeter);
737
738   return buf.str();
739 }
740
741 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
742
743 string FGPiston::GetEngineValues(string delimeter)
744 {
745   std::ostringstream buf;
746
747   buf << PowerAvailable << delimeter << HP << delimeter
748       << equivalence_ratio << delimeter << MAP << delimeter
749       << Thruster->GetThrusterValues(EngineNumber, delimeter);
750
751   return buf.str();
752 }
753
754 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
755 //
756 //    The bitmasked value choices are as follows:
757 //    unset: In this case (the default) JSBSim would only print
758 //       out the normally expected messages, essentially echoing
759 //       the config files as they are read. If the environment
760 //       variable is not set, debug_lvl is set to 1 internally
761 //    0: This requests JSBSim not to output any messages
762 //       whatsoever.
763 //    1: This value explicity requests the normal JSBSim
764 //       startup messages
765 //    2: This value asks for a message to be printed out when
766 //       a class is instantiated
767 //    4: When this value is set, a message is displayed when a
768 //       FGModel object executes its Run() method
769 //    8: When this value is set, various runtime state variables
770 //       are printed out periodically
771 //    16: When set various parameters are sanity checked and
772 //       a message is printed out when they go out of bounds
773
774 void FGPiston::Debug(int from)
775 {
776   if (debug_lvl <= 0) return;
777
778   if (debug_lvl & 1) { // Standard console startup message output
779     if (from == 0) { // Constructor
780
781       cout << "\n    Engine Name: "         << Name << endl;
782       cout << "      MinManifoldPressure: " << MinManifoldPressure_inHg << endl;
783       cout << "      MaxManifoldPressure: " << MaxManifoldPressure_inHg << endl;
784       cout << "      MinMaP (Pa):         " << minMAP << endl;
785       cout << "      MaxMaP (Pa): "         << maxMAP << endl;
786       cout << "      Displacement: "        << Displacement             << endl;
787       cout << "      MaxHP: "               << MaxHP                    << endl;
788       cout << "      Cycles: "              << Cycles                   << endl;
789       cout << "      IdleRPM: "             << IdleRPM                  << endl;
790       cout << "      MaxThrottle: "         << MaxThrottle              << endl;
791       cout << "      MinThrottle: "         << MinThrottle              << endl;
792
793       cout << endl;
794       cout << "      Combustion Efficiency table:" << endl;
795       Lookup_Combustion_Efficiency->Print();
796       cout << endl;
797
798       cout << endl;
799       cout << "      Power Mixture Correlation table:" << endl;
800       Power_Mixture_Correlation->Print();
801       cout << endl;
802
803     }
804   }
805   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
806     if (from == 0) cout << "Instantiated: FGPiston" << endl;
807     if (from == 1) cout << "Destroyed:    FGPiston" << endl;
808   }
809   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
810   }
811   if (debug_lvl & 8 ) { // Runtime state variables
812   }
813   if (debug_lvl & 16) { // Sanity checking
814   }
815   if (debug_lvl & 64) {
816     if (from == 0) { // Constructor
817       cout << IdSrc << endl;
818       cout << IdHdr << endl;
819     }
820   }
821 }
822
823 double
824 FGPiston::CalcFuelNeed(void)
825 {
826   return FuelFlow_gph / 3600 * 6 * State->Getdt() * Propulsion->GetRate();
827 }
828
829 } // namespace JSBSim