1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4 Author: Jon S. Berndt, JSBSim framework
5 Dave Luff, Piston engine model
6 Ronald Jensen, Piston engine model
7 Date started: 09/12/2000
8 Purpose: This module models a Piston engine
10 ------------- Copyright (C) 2000 Jon S. Berndt (jon@jsbsim.org) --------------
12 This program is free software; you can redistribute it and/or modify it under
13 the terms of the GNU Lesser General Public License as published by the Free Software
14 Foundation; either version 2 of the License, or (at your option) any later
17 This program is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
22 You should have received a copy of the GNU Lesser General Public License along with
23 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
24 Place - Suite 330, Boston, MA 02111-1307, USA.
26 Further information about the GNU Lesser General Public License can also be found on
27 the world wide web at http://www.gnu.org.
29 FUNCTIONAL DESCRIPTION
30 --------------------------------------------------------------------------------
32 This class descends from the FGEngine class and models a Piston engine based on
33 parameters given in the engine config file for this class
36 --------------------------------------------------------------------------------
37 09/12/2000 JSB Created
39 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
41 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
47 #include "FGPropeller.h"
53 static const char *IdSrc = "$Id: FGPiston.cpp,v 1.67 2011/09/25 23:56:11 jentron Exp $";
54 static const char *IdHdr = ID_PISTON;
56 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
58 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
60 FGPiston::FGPiston(FGFDMExec* exec, Element* el, int engine_number, struct Inputs& input)
61 : FGEngine(exec, el, engine_number, input),
62 R_air(287.3), // Gas constant for air J/Kg/K
63 rho_fuel(800), // estimate
64 calorific_value_fuel(47.3e6), // J/Kg
65 Cp_air(1005), // Specific heat (constant pressure) J/Kg/K
67 standard_pressure(101320.73)
69 Element *table_element;
73 // Defaults and initializations
77 // These items are read from the configuration file
78 // Defaults are from a Lycoming O-360, more or less
86 MinManifoldPressure_inHg = 6.5;
87 MaxManifoldPressure_inHg = 28.5;
89 volumetric_efficiency = 0.85;
93 CylinderHeadMass = 2; //kg
94 CompressionRatio = 8.5;
97 PeakMeanPistonSpeed_fps = 100;
100 Cooling_Factor = 0.5144444;
101 StaticFriction_HP = 1.5;
103 // These are internal program variables
105 Lookup_Combustion_Efficiency = 0;
106 Mixture_Efficiency_Correlation = 0;
115 BoostSpeeds = 0; // Default to no supercharging
120 bBoostOverride = false;
121 bTakeoffBoost = false;
122 TakeoffBoost = 0.0; // Default to no extra takeoff-boost
124 for (i=0; i<FG_MAX_BOOST_SPEEDS; i++) {
127 RatedAltitude[i] = 0.0;
129 RatedMAP[i] = 100000;
131 TakeoffMAP[i] = 100000;
133 for (i=0; i<FG_MAX_BOOST_SPEEDS-1; i++) {
134 BoostSwitchAltitude[i] = 0.0;
135 BoostSwitchPressure[i] = 0.0;
138 // Read inputs from engine data file where present.
140 if (el->FindElement("minmp")) // Should have ELSE statement telling default value used?
141 MinManifoldPressure_inHg = el->FindElementValueAsNumberConvertTo("minmp","INHG");
142 if (el->FindElement("maxmp"))
143 MaxManifoldPressure_inHg = el->FindElementValueAsNumberConvertTo("maxmp","INHG");
144 if (el->FindElement("displacement"))
145 Displacement = el->FindElementValueAsNumberConvertTo("displacement","IN3");
146 if (el->FindElement("maxhp"))
147 MaxHP = el->FindElementValueAsNumberConvertTo("maxhp","HP");
148 if (el->FindElement("static-friction"))
149 StaticFriction_HP = el->FindElementValueAsNumberConvertTo("static-friction","HP");
150 if (el->FindElement("sparkfaildrop"))
151 SparkFailDrop = Constrain(0, 1 - el->FindElementValueAsNumber("sparkfaildrop"), 1);
152 if (el->FindElement("cycles"))
153 Cycles = el->FindElementValueAsNumber("cycles");
154 if (el->FindElement("idlerpm"))
155 IdleRPM = el->FindElementValueAsNumber("idlerpm");
156 if (el->FindElement("maxrpm"))
157 MaxRPM = el->FindElementValueAsNumber("maxrpm");
158 if (el->FindElement("maxthrottle"))
159 MaxThrottle = el->FindElementValueAsNumber("maxthrottle");
160 if (el->FindElement("minthrottle"))
161 MinThrottle = el->FindElementValueAsNumber("minthrottle");
162 if (el->FindElement("bsfc"))
163 ISFC = el->FindElementValueAsNumberConvertTo("bsfc", "LBS/HP*HR");
164 if (el->FindElement("volumetric-efficiency"))
165 volumetric_efficiency = el->FindElementValueAsNumber("volumetric-efficiency");
166 if (el->FindElement("compression-ratio"))
167 CompressionRatio = el->FindElementValueAsNumber("compression-ratio");
168 if (el->FindElement("bore"))
169 Bore = el->FindElementValueAsNumberConvertTo("bore","IN");
170 if (el->FindElement("stroke"))
171 Stroke = el->FindElementValueAsNumberConvertTo("stroke","IN");
172 if (el->FindElement("cylinders"))
173 Cylinders = el->FindElementValueAsNumber("cylinders");
174 if (el->FindElement("cylinder-head-mass"))
175 CylinderHeadMass = el->FindElementValueAsNumberConvertTo("cylinder-head-mass","KG");
176 if (el->FindElement("air-intake-impedance-factor"))
177 Z_airbox = el->FindElementValueAsNumber("air-intake-impedance-factor");
178 if (el->FindElement("ram-air-factor"))
179 Ram_Air_Factor = el->FindElementValueAsNumber("ram-air-factor");
180 if (el->FindElement("cooling-factor"))
181 Cooling_Factor = el->FindElementValueAsNumber("cooling-factor");
182 if (el->FindElement("dynamic-fmep"))
183 FMEPDynamic= el->FindElementValueAsNumberConvertTo("dynamic-fmep","PA");
184 if (el->FindElement("static-fmep"))
185 FMEPStatic = el->FindElementValueAsNumberConvertTo("static-fmep","PA");
186 if (el->FindElement("peak-piston-speed"))
187 PeakMeanPistonSpeed_fps = el->FindElementValueAsNumber("peak-piston-speed");
188 if (el->FindElement("numboostspeeds")) { // Turbo- and super-charging parameters
189 BoostSpeeds = (int)el->FindElementValueAsNumber("numboostspeeds");
190 if (el->FindElement("boostoverride"))
191 BoostOverride = (int)el->FindElementValueAsNumber("boostoverride");
192 if (el->FindElement("boostmanual"))
193 BoostManual = (int)el->FindElementValueAsNumber("boostmanual");
194 if (el->FindElement("takeoffboost"))
195 TakeoffBoost = el->FindElementValueAsNumberConvertTo("takeoffboost", "PSI");
196 if (el->FindElement("ratedboost1"))
197 RatedBoost[0] = el->FindElementValueAsNumberConvertTo("ratedboost1", "PSI");
198 if (el->FindElement("ratedboost2"))
199 RatedBoost[1] = el->FindElementValueAsNumberConvertTo("ratedboost2", "PSI");
200 if (el->FindElement("ratedboost3"))
201 RatedBoost[2] = el->FindElementValueAsNumberConvertTo("ratedboost3", "PSI");
202 if (el->FindElement("ratedpower1"))
203 RatedPower[0] = el->FindElementValueAsNumberConvertTo("ratedpower1", "HP");
204 if (el->FindElement("ratedpower2"))
205 RatedPower[1] = el->FindElementValueAsNumberConvertTo("ratedpower2", "HP");
206 if (el->FindElement("ratedpower3"))
207 RatedPower[2] = el->FindElementValueAsNumberConvertTo("ratedpower3", "HP");
208 if (el->FindElement("ratedrpm1"))
209 RatedRPM[0] = el->FindElementValueAsNumber("ratedrpm1");
210 if (el->FindElement("ratedrpm2"))
211 RatedRPM[1] = el->FindElementValueAsNumber("ratedrpm2");
212 if (el->FindElement("ratedrpm3"))
213 RatedRPM[2] = el->FindElementValueAsNumber("ratedrpm3");
214 if (el->FindElement("ratedaltitude1"))
215 RatedAltitude[0] = el->FindElementValueAsNumberConvertTo("ratedaltitude1", "FT");
216 if (el->FindElement("ratedaltitude2"))
217 RatedAltitude[1] = el->FindElementValueAsNumberConvertTo("ratedaltitude2", "FT");
218 if (el->FindElement("ratedaltitude3"))
219 RatedAltitude[2] = el->FindElementValueAsNumberConvertTo("ratedaltitude3", "FT");
222 while((table_element = el->FindNextElement("table")) != 0) {
223 name = table_element->GetAttributeValue("name");
225 if (name == "COMBUSTION") {
226 Lookup_Combustion_Efficiency = new FGTable(PropertyManager, table_element);
227 } else if (name == "MIXTURE") {
228 Mixture_Efficiency_Correlation = new FGTable(PropertyManager, table_element);
230 cerr << "Unknown table type: " << name << " in piston engine definition." << endl;
232 } catch (std::string str) {
233 throw("Error loading piston engine table:" + name + ". " + str);
237 StarterHP = sqrt(MaxHP) * 0.4;
238 displacement_SI = Displacement * in3tom3;
239 RatedMeanPistonSpeed_fps = ( MaxRPM * Stroke) / (360); // AKA 2 * (RPM/60) * ( Stroke / 12) or 2NS
241 // Create IFSC to match the engine if not provided
243 double pmep = 29.92 - MaxManifoldPressure_inHg;
244 pmep *= inhgtopa * volumetric_efficiency;
245 double fmep = (FMEPDynamic * RatedMeanPistonSpeed_fps * fttom + FMEPStatic);
246 double hp_loss = ((pmep + fmep) * displacement_SI * MaxRPM)/(Cycles*22371);
247 ISFC = ( 1.1*Displacement * MaxRPM * volumetric_efficiency *(MaxManifoldPressure_inHg / 29.92) ) / (9411 * (MaxHP+hp_loss-StaticFriction_HP));
248 // cout <<"FMEP: "<< fmep <<" PMEP: "<< pmep << " hp_loss: " <<hp_loss <<endl;
250 if ( MaxManifoldPressure_inHg > 29.9 ) { // Don't allow boosting with a bogus number
251 MaxManifoldPressure_inHg = 29.9;
253 minMAP = MinManifoldPressure_inHg * inhgtopa; // inHg to Pa
254 maxMAP = MaxManifoldPressure_inHg * inhgtopa;
258 * Pm = ( Ze / ( Ze + Zi + Zt ) ) * Pa
260 * Pm = Manifold Pressure
261 * Pa = Ambient Pressre
262 * Ze = engine impedance, Ze is effectively 1 / Mean Piston Speed
263 * Zi = airbox impedance
264 * Zt = throttle impedance
266 * For the calculation below throttle is fully open or Zt = 0
272 double Ze=PeakMeanPistonSpeed_fps/RatedMeanPistonSpeed_fps; // engine impedence
273 Z_airbox = (standard_pressure *Ze / maxMAP) - Ze; // impedence of airbox
275 // Constant for Throttle impedence
276 Z_throttle=(PeakMeanPistonSpeed_fps/((IdleRPM * Stroke) / 360))*(standard_pressure/minMAP - 1) - Z_airbox;
277 // Z_throttle=(MaxRPM/IdleRPM )*(standard_pressure/minMAP+2); // Constant for Throttle impedence
279 // Default tables if not provided in the configuration file
280 if(Lookup_Combustion_Efficiency == 0) {
281 // First column is thi, second is neta (combustion efficiency)
282 Lookup_Combustion_Efficiency = new FGTable(12);
283 *Lookup_Combustion_Efficiency << 0.00 << 0.980;
284 *Lookup_Combustion_Efficiency << 0.90 << 0.980;
285 *Lookup_Combustion_Efficiency << 1.00 << 0.970;
286 *Lookup_Combustion_Efficiency << 1.05 << 0.950;
287 *Lookup_Combustion_Efficiency << 1.10 << 0.900;
288 *Lookup_Combustion_Efficiency << 1.15 << 0.850;
289 *Lookup_Combustion_Efficiency << 1.20 << 0.790;
290 *Lookup_Combustion_Efficiency << 1.30 << 0.700;
291 *Lookup_Combustion_Efficiency << 1.40 << 0.630;
292 *Lookup_Combustion_Efficiency << 1.50 << 0.570;
293 *Lookup_Combustion_Efficiency << 1.60 << 0.525;
294 *Lookup_Combustion_Efficiency << 2.00 << 0.345;
297 // First column is Fuel/Air Ratio, second is neta (mixture efficiency)
298 if( Mixture_Efficiency_Correlation == 0) {
299 Mixture_Efficiency_Correlation = new FGTable(15);
300 *Mixture_Efficiency_Correlation << 0.05000 << 0.00000;
301 *Mixture_Efficiency_Correlation << 0.05137 << 0.00862;
302 *Mixture_Efficiency_Correlation << 0.05179 << 0.21552;
303 *Mixture_Efficiency_Correlation << 0.05430 << 0.48276;
304 *Mixture_Efficiency_Correlation << 0.05842 << 0.70690;
305 *Mixture_Efficiency_Correlation << 0.06312 << 0.83621;
306 *Mixture_Efficiency_Correlation << 0.06942 << 0.93103;
307 *Mixture_Efficiency_Correlation << 0.07786 << 1.00000;
308 *Mixture_Efficiency_Correlation << 0.08845 << 1.00000;
309 *Mixture_Efficiency_Correlation << 0.09270 << 0.98276;
310 *Mixture_Efficiency_Correlation << 0.10120 << 0.93103;
311 *Mixture_Efficiency_Correlation << 0.11455 << 0.72414;
312 *Mixture_Efficiency_Correlation << 0.12158 << 0.45690;
313 *Mixture_Efficiency_Correlation << 0.12435 << 0.23276;
314 *Mixture_Efficiency_Correlation << 0.12500 << 0.00000;
317 string property_name, base_property_name;
318 base_property_name = CreateIndexedPropertyName("propulsion/engine", EngineNumber);
319 property_name = base_property_name + "/power-hp";
320 PropertyManager->Tie(property_name, &HP);
321 property_name = base_property_name + "/bsfc-lbs_hphr";
322 PropertyManager->Tie(property_name, &ISFC);
323 property_name = base_property_name + "/volumetric-efficiency";
324 PropertyManager->Tie(property_name, &volumetric_efficiency);
325 property_name = base_property_name + "/map-pa";
326 PropertyManager->Tie(property_name, &MAP);
327 property_name = base_property_name + "/map-inhg";
328 PropertyManager->Tie(property_name, &ManifoldPressure_inHg);
329 property_name = base_property_name + "/air-intake-impedance-factor";
330 PropertyManager->Tie(property_name, &Z_airbox);
331 property_name = base_property_name + "/ram-air-factor";
332 PropertyManager->Tie(property_name, &Ram_Air_Factor);
333 property_name = base_property_name + "/cooling-factor";
334 PropertyManager->Tie(property_name, &Cooling_Factor);
335 property_name = base_property_name + "/boost-speed";
336 PropertyManager->Tie(property_name, &BoostSpeed);
337 property_name = base_property_name + "/cht-degF";
338 PropertyManager->Tie(property_name, this, &FGPiston::getCylinderHeadTemp_degF);
339 property_name = base_property_name + "/oil-temperature-degF";
340 PropertyManager->Tie(property_name, this, &FGPiston::getOilTemp_degF);
341 property_name = base_property_name + "/oil-pressure-psi";
342 PropertyManager->Tie(property_name, this, &FGPiston::getOilPressure_psi);
343 property_name = base_property_name + "/egt-degF";
344 PropertyManager->Tie(property_name, this, &FGPiston::getExhaustGasTemp_degF);
346 // Set up and sanity-check the turbo/supercharging configuration based on the input values.
347 if (TakeoffBoost > RatedBoost[0]) bTakeoffBoost = true;
348 for (i=0; i<BoostSpeeds; ++i) {
350 if (RatedBoost[i] <= 0.0) bad = true;
351 if (RatedPower[i] <= 0.0) bad = true;
352 if (RatedAltitude[i] < 0.0) bad = true; // 0.0 is deliberately allowed - this corresponds to unregulated supercharging.
353 if (i > 0 && RatedAltitude[i] < RatedAltitude[i - 1]) bad = true;
355 // We can't recover from the above - don't use this supercharger speed.
357 // TODO - put out a massive error message!
360 // Now sanity-check stuff that is recoverable.
361 if (i < BoostSpeeds - 1) {
362 if (BoostSwitchAltitude[i] < RatedAltitude[i]) {
363 // TODO - put out an error message
364 // But we can also make a reasonable estimate, as below.
365 BoostSwitchAltitude[i] = RatedAltitude[i] + 1000;
367 BoostSwitchPressure[i] = GetStdPressure100K(BoostSwitchAltitude[i]) * psftopa;
368 //cout << "BoostSwitchAlt = " << BoostSwitchAltitude[i] << ", pressure = " << BoostSwitchPressure[i] << '\n';
369 // Assume there is some hysteresis on the supercharger gear switch, and guess the value for now
370 BoostSwitchHysteresis = 1000;
372 // Now work out the supercharger pressure multiplier of this speed from the rated boost and altitude.
373 RatedMAP[i] = standard_pressure + RatedBoost[i] * 6895; // psi*6895 = Pa.
374 // Sometimes a separate BCV setting for takeoff or extra power is fitted.
375 if (TakeoffBoost > RatedBoost[0]) {
376 // Assume that the effect on the BCV is the same whichever speed is in use.
377 TakeoffMAP[i] = RatedMAP[i] + ((TakeoffBoost - RatedBoost[0]) * 6895);
378 bTakeoffBoost = true;
380 TakeoffMAP[i] = RatedMAP[i];
381 bTakeoffBoost = false;
383 BoostMul[i] = RatedMAP[i] / (GetStdPressure100K(RatedAltitude[i]) * psftopa);
387 if (BoostSpeeds > 0) {
391 bBoostOverride = (BoostOverride == 1 ? true : false);
392 bBoostManual = (BoostManual == 1 ? true : false);
393 Debug(0); // Call Debug() routine from constructor if needed
396 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
398 FGPiston::~FGPiston()
400 delete Lookup_Combustion_Efficiency;
401 delete Mixture_Efficiency_Correlation;
402 Debug(1); // Call Debug() routine from constructor if needed
405 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
407 void FGPiston::ResetToIC(void)
409 FGEngine::ResetToIC();
411 ManifoldPressure_inHg = in.Pressure * psftoinhg; // psf to in Hg
412 MAP = in.Pressure * psftopa;
414 double airTemperature_degK = RankineToKelvin(in.Temperature);
415 OilTemp_degK = airTemperature_degK;
416 CylinderHeadTemp_degK = airTemperature_degK;
417 ExhaustGasTemp_degK = airTemperature_degK;
418 EGT_degC = ExhaustGasTemp_degK - 273;
419 Thruster->SetRPM(0.0);
421 OilPressure_psi = 0.0;
424 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
426 void FGPiston::Calculate(void)
430 p_amb = in.Pressure * psftopa;
431 double p = in.TotalPressure * psftopa;
432 p_ram = (p - p_amb) * Ram_Air_Factor + p_amb;
433 T_amb = RankineToKelvin(in.Temperature);
437 /* The thruster controls the engine RPM because it encapsulates the gear ratio and other transmission variables */
438 RPM = Thruster->GetEngineRPM();
440 MeanPistonSpeed_fps = ( RPM * Stroke) / (360); // AKA 2 * (RPM/60) * ( Stroke / 12) or 2NS
445 if (Boosted) doBoostControl();
450 //Now that the fuel flow is done check if the mixture is too lean to run the engine
451 //Assume lean limit at 22 AFR for now - thats a thi of 0.668
452 //This might be a bit generous, but since there's currently no audiable warning of impending
453 //cutout in the form of misfiring and/or rough running its probably reasonable for now.
455 // if (equivalence_ratio < 0.668)
459 if (IndicatedHorsePower < 0.1250) Running = false;
466 if (Thruster->GetType() == FGThruster::ttPropeller) {
467 ((FGPropeller*)Thruster)->SetAdvance(in.PropAdvance[EngineNumber]);
468 ((FGPropeller*)Thruster)->SetFeather(in.PropFeather[EngineNumber]);
471 LoadThrusterInputs();
472 Thruster->Calculate(HP * hptoftlbssec);
477 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
479 double FGPiston::CalcFuelNeed(void)
481 FuelExpended = FuelFlowRate * in.TotalDeltaT;
482 if (!Starved) FuelUsedLbs += FuelExpended;
486 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
488 int FGPiston::InitRunning(void)
491 in.MixtureCmd[EngineNumber] = in.PressureRatio/1.3;
492 in.MixturePos[EngineNumber] = in.PressureRatio/1.3;
493 Thruster->SetRPM( 2.0*IdleRPM/Thruster->GetGearRatio() );
498 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
500 * Start or stop the engine.
503 void FGPiston::doEngineStartup(void)
505 // Check parameters that may alter the operating state of the engine.
506 // (spark, fuel, starter motor etc)
510 Magneto_Left = false;
511 Magneto_Right = false;
512 // Magneto positions:
521 } // neglects battery voltage, master on switch, etc for now.
523 if ((Magnetos == 1) || (Magnetos > 2)) Magneto_Left = true;
524 if (Magnetos > 1) Magneto_Right = true;
526 // Assume we have fuel for now
529 // Check if we are turning the starter motor
530 if (Cranking != Starter) {
531 // This check saves .../cranking from getting updated every loop - they
532 // only update when changed.
537 if (Cranking) crank_counter++; //Check mode of engine operation
539 if (!Running && spark && fuel) { // start the engine if revs high enough
541 if ((RPM > IdleRPM*0.8) && (crank_counter > 175)) // Add a little delay to startup
542 Running = true; // on the starter
544 if (RPM > IdleRPM*0.8) // This allows us to in-air start
545 Running = true; // when windmilling
549 // Cut the engine *power* - Note: the engine may continue to
550 // spin if the prop is in a moving airstream
552 if ( Running && (!spark || !fuel) ) Running = false;
554 // Check for stalling (RPM = 0).
558 } else if ((RPM <= IdleRPM *0.8 ) && (Cranking)) {
564 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
567 * Calculate the Current Boost Speed
569 * This function calculates the current turbo/supercharger boost speed
570 * based on altitude and the (automatic) boost-speed control valve configuration.
572 * Inputs: p_amb, BoostSwitchPressure, BoostSwitchHysteresis
574 * Outputs: BoostSpeed
577 void FGPiston::doBoostControl(void)
580 if(BoostSpeed > BoostSpeeds-1) BoostSpeed = BoostSpeeds-1;
581 if(BoostSpeed < 0) BoostSpeed = 0;
583 if(BoostSpeed < BoostSpeeds - 1) {
584 // Check if we need to change to a higher boost speed
585 if(p_amb < BoostSwitchPressure[BoostSpeed] - BoostSwitchHysteresis) {
588 } if(BoostSpeed > 0) {
589 // Check if we need to change to a lower boost speed
590 if(p_amb > BoostSwitchPressure[BoostSpeed - 1] + BoostSwitchHysteresis) {
597 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
600 * Calculate the manifold absolute pressure (MAP) in inches hg
602 * This function calculates manifold absolute pressure (MAP)
603 * from the throttle position, turbo/supercharger boost control
604 * system, engine speed and local ambient air density.
606 * Inputs: p_amb, Throttle,
607 * MeanPistonSpeed_fps, dt
609 * Outputs: MAP, ManifoldPressure_inHg, TMAP
612 void FGPiston::doMAP(void)
614 double Zt = (1 - in.ThrottlePos[EngineNumber])*(1 - in.ThrottlePos[EngineNumber])*Z_throttle; // throttle impedence
615 double Ze= MeanPistonSpeed_fps > 0 ? PeakMeanPistonSpeed_fps/MeanPistonSpeed_fps : 999999; // engine impedence
617 double map_coefficient = Ze/(Ze+Z_airbox+Zt);
619 // Add a one second lag to manifold pressure changes
621 if (in.TotalDeltaT > 0.0)
622 dMAP = (TMAP - p_ram * map_coefficient) * in.TotalDeltaT;
624 dMAP = (TMAP - p_ram * map_coefficient) / 120;
628 // Find the mean effective pressure required to achieve this manifold pressure
629 // Fixme: determine the HP consumed by the supercharger
631 PMEP = (TMAP - p_amb) * volumetric_efficiency; // Fixme: p_amb should be exhaust manifold pressure
634 // If takeoff boost is fitted, we currently assume the following throttle map:
635 // (In throttle % - actual input is 0 -> 1)
636 // 99 / 100 - Takeoff boost
637 // In real life, most planes would be fitted with a mechanical 'gate' between
638 // the rated boost and takeoff boost positions.
640 bool bTakeoffPos = false;
642 if (in.ThrottlePos[EngineNumber] > 0.98) {
646 // Boost the manifold pressure.
647 double boost_factor = (( BoostMul[BoostSpeed] - 1 ) / RatedRPM[BoostSpeed] ) * RPM + 1;
648 MAP = TMAP * boost_factor;
649 // Now clip the manifold pressure to BCV or Wastegate setting.
651 if (MAP > TakeoffMAP[BoostSpeed]) MAP = TakeoffMAP[BoostSpeed];
653 if (MAP > RatedMAP[BoostSpeed]) MAP = RatedMAP[BoostSpeed];
659 // And set the value in American units as well
660 ManifoldPressure_inHg = MAP / inhgtopa;
663 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
665 * Calculate the air flow through the engine.
666 * Also calculates ambient air density
667 * (used in CHT calculation for air-cooled engines).
669 * Inputs: p_amb, R_air, T_amb, MAP, Displacement,
670 * RPM, volumetric_efficiency,
672 * TODO: Model inlet manifold air temperature.
674 * Outputs: rho_air, m_dot_air
677 void FGPiston::doAirFlow(void)
679 double gamma = 1.3; // specific heat constants
680 // loss of volumentric efficiency due to difference between MAP and exhaust pressure
681 // Eq 6-10 from The Internal Combustion Engine - Charles Taylor Vol 1
682 double ve =((gamma-1)/gamma) +( CompressionRatio -(p_amb/MAP))/(gamma*( CompressionRatio - 1));
684 rho_air = p_amb / (R_air * T_amb);
685 double swept_volume = (displacement_SI * (RPM/60)) / 2;
686 double v_dot_air = swept_volume * volumetric_efficiency *ve;
688 double rho_air_manifold = MAP / (R_air * T_amb);
689 m_dot_air = v_dot_air * rho_air_manifold;
693 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
695 * Calculate the fuel flow into the engine.
697 * Inputs: Mixture, thi_sea_level, p_amb, m_dot_air
699 * Outputs: equivalence_ratio, m_dot_fuel
702 void FGPiston::doFuelFlow(void)
704 double thi_sea_level = 1.3 * in.MixturePos[EngineNumber]; // Allows an AFR of infinity:1 to 11.3075:1
705 equivalence_ratio = thi_sea_level * 101325.0 / p_amb;
706 // double AFR = 10+(12*(1-in.Mixture[EngineNumber]));// mixture 10:1 to 22:1
707 // m_dot_fuel = m_dot_air / AFR;
708 m_dot_fuel = (m_dot_air * equivalence_ratio) / 14.7;
709 FuelFlowRate = m_dot_fuel * 2.2046; // kg to lb
710 if(Starved) // There is no fuel, so zero out the flows we've calculated so far
712 equivalence_ratio = 0.0;
716 FuelFlow_pph = FuelFlowRate * 3600; // seconds to hours
717 FuelFlow_gph = FuelFlow_pph / 6.0; // Assumes 6 lbs / gallon
720 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
722 * Calculate the power produced by the engine.
724 * Inputs: ManifoldPressure_inHg, p_amb, RPM, T_amb, ISFC,
725 * Mixture_Efficiency_Correlation, Cycles, MaxHP, PMEP,
726 * MeanPistonSpeed_fps
728 * Outputs: PctPower, HP, FMEP, IndicatedHorsePower
731 void FGPiston::doEnginePower(void)
733 IndicatedHorsePower = 0;
736 // FIXME: this needs to be generalized
737 double ME, percent_RPM, power; // Convienience term for use in the calculations
738 ME = Mixture_Efficiency_Correlation->GetValue(m_dot_fuel/m_dot_air);
740 percent_RPM = RPM/MaxRPM;
741 // Guestimate engine friction losses from Figure 4.4 of "Engines: An Introduction", John Lumley
742 FMEP = (-FMEPDynamic * MeanPistonSpeed_fps * fttom - FMEPStatic);
746 if ( Magnetos != 3 ) power *= SparkFailDrop;
749 IndicatedHorsePower = (FuelFlow_pph / ISFC )* ME * power;
752 // Power output when the engine is not running
755 IndicatedHorsePower = StarterHP;
756 } else if (RPM < IdleRPM*0.8) {
757 IndicatedHorsePower = StarterHP + ((IdleRPM*0.8 - RPM) / 8.0);
758 // This is a guess - would be nice to find a proper starter moter torque curve
760 IndicatedHorsePower = StarterHP;
765 // Constant is (1/2) * 60 * 745.7
766 // (1/2) convert cycles, 60 minutes to seconds, 745.7 watts to hp.
767 double pumping_hp = ((PMEP + FMEP) * displacement_SI * RPM)/(Cycles*22371);
769 HP = IndicatedHorsePower + pumping_hp - StaticFriction_HP; //FIXME static friction should depend on oil temp and configuration
770 // cout << "pumping_hp " <<pumping_hp << FMEP << PMEP <<endl;
771 PctPower = HP / MaxHP ;
772 // cout << "Power = " << HP << " RPM = " << RPM << " Running = " << Running << " Cranking = " << Cranking << endl;
775 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
777 * Calculate the exhaust gas temperature.
779 * Inputs: equivalence_ratio, m_dot_fuel, calorific_value_fuel,
780 * Cp_air, m_dot_air, Cp_fuel, m_dot_fuel, T_amb, PctPower
782 * Outputs: combustion_efficiency, ExhaustGasTemp_degK
785 void FGPiston::doEGT(void)
787 double delta_T_exhaust;
788 double enthalpy_exhaust;
789 double heat_capacity_exhaust;
792 if ((Running) && (m_dot_air > 0.0)) { // do the energy balance
793 combustion_efficiency = Lookup_Combustion_Efficiency->GetValue(equivalence_ratio);
794 enthalpy_exhaust = m_dot_fuel * calorific_value_fuel *
795 combustion_efficiency * 0.30;
796 heat_capacity_exhaust = (Cp_air * m_dot_air) + (Cp_fuel * m_dot_fuel);
797 delta_T_exhaust = enthalpy_exhaust / heat_capacity_exhaust;
798 ExhaustGasTemp_degK = T_amb + delta_T_exhaust;
799 } else { // Drop towards ambient - guess an appropriate time constant for now
800 combustion_efficiency = 0;
801 dEGTdt = (RankineToKelvin(in.Temperature) - ExhaustGasTemp_degK) / 100.0;
802 if (in.TotalDeltaT > 0.0)
803 delta_T_exhaust = dEGTdt * in.TotalDeltaT;
805 delta_T_exhaust = dEGTdt / 120;
807 ExhaustGasTemp_degK += delta_T_exhaust;
811 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
813 * Calculate the cylinder head temperature.
815 * Inputs: T_amb, IAS, rho_air, m_dot_fuel, calorific_value_fuel,
816 * combustion_efficiency, RPM, MaxRPM, Displacement, Cylinders
818 * Outputs: CylinderHeadTemp_degK
821 void FGPiston::doCHT(void)
825 double h3 = -140.0; // -0.05 * 2800 (default maxrpm)
827 double arbitary_area = Displacement/360.0;
828 double CpCylinderHead = 800.0;
829 double MassCylinderHead = CylinderHeadMass * Cylinders;
831 double temperature_difference = CylinderHeadTemp_degK - T_amb;
832 double v_apparent = IAS * Cooling_Factor;
833 double v_dot_cooling_air = arbitary_area * v_apparent;
834 double m_dot_cooling_air = v_dot_cooling_air * rho_air;
835 double dqdt_from_combustion =
836 m_dot_fuel * calorific_value_fuel * combustion_efficiency * 0.33;
837 double dqdt_forced = (h2 * m_dot_cooling_air * temperature_difference) +
838 (h3 * RPM * temperature_difference / MaxRPM);
839 double dqdt_free = h1 * temperature_difference * arbitary_area;
840 double dqdt_cylinder_head = dqdt_from_combustion + dqdt_forced + dqdt_free;
842 double HeatCapacityCylinderHead = CpCylinderHead * MassCylinderHead;
844 if (in.TotalDeltaT > 0.0)
845 CylinderHeadTemp_degK +=
846 (dqdt_cylinder_head / HeatCapacityCylinderHead) * in.TotalDeltaT;
848 CylinderHeadTemp_degK +=
849 (dqdt_cylinder_head / HeatCapacityCylinderHead) / 120.0;
852 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
854 * Calculate the oil temperature.
856 * Inputs: CylinderHeadTemp_degK, T_amb, OilPressure_psi.
858 * Outputs: OilTemp_degK
861 void FGPiston::doOilTemperature(void)
863 double target_oil_temp; // Steady state oil temp at the current engine conditions
864 double time_constant; // The time constant for the differential equation
865 double efficiency = 0.667; // The aproximate oil cooling system efficiency // FIXME: may vary by engine
867 // Target oil temp is interpolated between ambient temperature and Cylinder Head Tempurature
868 // target_oil_temp = ( T_amb * efficiency ) + (CylinderHeadTemp_degK *(1-efficiency)) ;
869 target_oil_temp = CylinderHeadTemp_degK + efficiency * (T_amb - CylinderHeadTemp_degK) ;
871 if (OilPressure_psi > 5.0 ) {
872 time_constant = 5000 / OilPressure_psi; // Guess at a time constant for circulated oil.
873 // The higher the pressure the faster it reaches
874 // target temperature. Oil pressure should be about
875 // 60 PSI yielding a TC of about 80.
877 time_constant = 1000; // Time constant for engine-off; reflects the fact
878 // that oil is no longer getting circulated
881 double dOilTempdt = (target_oil_temp - OilTemp_degK) / time_constant;
883 if (in.TotalDeltaT > 0.0)
884 OilTemp_degK += (dOilTempdt * in.TotalDeltaT);
886 OilTemp_degK += (dOilTempdt / 120.0);
889 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
891 * Calculate the oil pressure.
893 * Inputs: RPM, MaxRPM, OilTemp_degK
895 * Outputs: OilPressure_psi
898 void FGPiston::doOilPressure(void)
900 double Oil_Press_Relief_Valve = 60; // FIXME: may vary by engine
901 double Oil_Press_RPM_Max = MaxRPM * 0.75; // 75% of max rpm FIXME: may vary by engine
902 double Design_Oil_Temp = 358; // degK; FIXME: may vary by engine
903 double Oil_Viscosity_Index = 0.25;
905 OilPressure_psi = (Oil_Press_Relief_Valve / Oil_Press_RPM_Max) * RPM;
907 if (OilPressure_psi >= Oil_Press_Relief_Valve) {
908 OilPressure_psi = Oil_Press_Relief_Valve;
911 OilPressure_psi += (Design_Oil_Temp - OilTemp_degK) * Oil_Viscosity_Index * OilPressure_psi / Oil_Press_Relief_Valve;
914 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
916 // This is a local copy of the same function in FGStandardAtmosphere.
918 double FGPiston::GetStdPressure100K(double altitude) const
920 // Limit this equation to input altitudes of 100000 ft.
921 if (altitude > 100000.0) altitude = 100000.0;
924 const double coef[5] = { 2116.217,
931 for (int pwr=1; pwr<=4; pwr++) alt[pwr] = alt[pwr-1]*altitude;
934 for (int ctr=0; ctr<=4; ctr++) press += coef[ctr]*alt[ctr];
938 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
940 string FGPiston::GetEngineLabels(const string& delimiter)
942 std::ostringstream buf;
944 buf << Name << " Power Available (engine " << EngineNumber << " in ft-lbs/sec)" << delimiter
945 << Name << " HP (engine " << EngineNumber << ")" << delimiter
946 << Name << " equivalent ratio (engine " << EngineNumber << ")" << delimiter
947 << Name << " MAP (engine " << EngineNumber << " in inHg)" << delimiter
948 << Thruster->GetThrusterLabels(EngineNumber, delimiter);
953 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
955 string FGPiston::GetEngineValues(const string& delimiter)
957 std::ostringstream buf;
959 buf << (HP * hptoftlbssec) << delimiter << HP << delimiter
960 << equivalence_ratio << delimiter << ManifoldPressure_inHg << delimiter
961 << Thruster->GetThrusterValues(EngineNumber, delimiter);
966 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
968 // The bitmasked value choices are as follows:
969 // unset: In this case (the default) JSBSim would only print
970 // out the normally expected messages, essentially echoing
971 // the config files as they are read. If the environment
972 // variable is not set, debug_lvl is set to 1 internally
973 // 0: This requests JSBSim not to output any messages
975 // 1: This value explicity requests the normal JSBSim
977 // 2: This value asks for a message to be printed out when
978 // a class is instantiated
979 // 4: When this value is set, a message is displayed when a
980 // FGModel object executes its Run() method
981 // 8: When this value is set, various runtime state variables
982 // are printed out periodically
983 // 16: When set various parameters are sanity checked and
984 // a message is printed out when they go out of bounds
986 void FGPiston::Debug(int from)
988 if (debug_lvl <= 0) return;
990 if (debug_lvl & 1) { // Standard console startup message output
991 if (from == 0) { // Constructor
993 cout << "\n Engine Name: " << Name << endl;
994 cout << " MinManifoldPressure: " << MinManifoldPressure_inHg << endl;
995 cout << " MaxManifoldPressure: " << MaxManifoldPressure_inHg << endl;
996 cout << " MinMaP (Pa): " << minMAP << endl;
997 cout << " MaxMaP (Pa): " << maxMAP << endl;
998 cout << " Displacement: " << Displacement << endl;
999 cout << " Bore: " << Bore << endl;
1000 cout << " Stroke: " << Stroke << endl;
1001 cout << " Cylinders: " << Cylinders << endl;
1002 cout << " Cylinders Head Mass: " <<CylinderHeadMass << endl;
1003 cout << " Compression Ratio: " << CompressionRatio << endl;
1004 cout << " MaxHP: " << MaxHP << endl;
1005 cout << " Cycles: " << Cycles << endl;
1006 cout << " IdleRPM: " << IdleRPM << endl;
1007 cout << " MaxRPM: " << MaxRPM << endl;
1008 cout << " Throttle Constant: " << Z_throttle << endl;
1009 cout << " ISFC: " << ISFC << endl;
1010 cout << " Volumetric Efficiency: " << volumetric_efficiency << endl;
1011 cout << " PeakMeanPistonSpeed_fps: " << PeakMeanPistonSpeed_fps << endl;
1012 cout << " Intake Impedance Factor: " << Z_airbox << endl;
1013 cout << " Dynamic FMEP Factor: " << FMEPDynamic << endl;
1014 cout << " Static FMEP Factor: " << FMEPStatic << endl;
1017 cout << " Combustion Efficiency table:" << endl;
1018 Lookup_Combustion_Efficiency->Print();
1022 cout << " Mixture Efficiency Correlation table:" << endl;
1023 Mixture_Efficiency_Correlation->Print();
1028 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
1029 if (from == 0) cout << "Instantiated: FGPiston" << endl;
1030 if (from == 1) cout << "Destroyed: FGPiston" << endl;
1032 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
1034 if (debug_lvl & 8 ) { // Runtime state variables
1036 if (debug_lvl & 16) { // Sanity checking
1038 if (debug_lvl & 64) {
1039 if (from == 0) { // Constructor
1040 cout << IdSrc << endl;
1041 cout << IdHdr << endl;
1045 } // namespace JSBSim