1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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
9 ------------- Copyright (C) 2000 Jon S. Berndt (jsb@hal-pc.org) --------------
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
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
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.
25 Further information about the GNU General Public License can also be found on
26 the world wide web at http://www.gnu.org.
28 FUNCTIONAL DESCRIPTION
29 --------------------------------------------------------------------------------
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
35 --------------------------------------------------------------------------------
36 09/12/2000 JSB Created
38 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
40 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
43 #include "FGPropulsion.h"
45 static const char *IdSrc = "$Id$";
46 static const char *IdHdr = ID_PISTON;
48 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
50 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
52 FGPiston::FGPiston(FGFDMExec* exec, FGConfigFile* Eng_cfg) : FGEngine(exec),
54 rho_fuel(800), // estimate
55 calorific_value_fuel(47.3e6),
61 MinManifoldPressure_inHg = 6.5;
62 MaxManifoldPressure_inHg = 28.5;
68 Name = Eng_cfg->GetValue("NAME");
69 Eng_cfg->GetNextConfigLine();
70 while (Eng_cfg->GetValue() != string("/FG_PISTON")) {
72 if (token == "MINMP") *Eng_cfg >> MinManifoldPressure_inHg;
73 else if (token == "MAXMP") *Eng_cfg >> MaxManifoldPressure_inHg;
74 else if (token == "DISPLACEMENT") *Eng_cfg >> Displacement;
75 else if (token == "MAXHP") *Eng_cfg >> MaxHP;
76 else if (token == "CYCLES") *Eng_cfg >> Cycles;
77 else if (token == "IDLERPM") *Eng_cfg >> IdleRPM;
78 else if (token == "MAXTHROTTLE") *Eng_cfg >> MaxThrottle;
79 else if (token == "MINTHROTTLE") *Eng_cfg >> MinThrottle;
80 else cerr << "Unhandled token in Engine config file: " << token << endl;
87 ManifoldPressure_inHg = Atmosphere->GetPressure() * psftoinhg; // psf to in Hg
92 volumetric_efficiency = 0.8; // Actually f(speed, load) but this will get us running
94 // First column is thi, second is neta (combustion efficiency)
95 Lookup_Combustion_Efficiency = new FGTable(12);
96 *Lookup_Combustion_Efficiency << 0.00 << 0.980;
97 *Lookup_Combustion_Efficiency << 0.90 << 0.980;
98 *Lookup_Combustion_Efficiency << 1.00 << 0.970;
99 *Lookup_Combustion_Efficiency << 1.05 << 0.950;
100 *Lookup_Combustion_Efficiency << 1.10 << 0.900;
101 *Lookup_Combustion_Efficiency << 1.15 << 0.850;
102 *Lookup_Combustion_Efficiency << 1.20 << 0.790;
103 *Lookup_Combustion_Efficiency << 1.30 << 0.700;
104 *Lookup_Combustion_Efficiency << 1.40 << 0.630;
105 *Lookup_Combustion_Efficiency << 1.50 << 0.570;
106 *Lookup_Combustion_Efficiency << 1.60 << 0.525;
107 *Lookup_Combustion_Efficiency << 2.00 << 0.345;
109 Power_Mixture_Correlation = new FGTable(13);
110 *Power_Mixture_Correlation << (14.7/1.6) << 78.0;
111 *Power_Mixture_Correlation << 10 << 86.0;
112 *Power_Mixture_Correlation << 11 << 93.5;
113 *Power_Mixture_Correlation << 12 << 98.0;
114 *Power_Mixture_Correlation << 13 << 100.0;
115 *Power_Mixture_Correlation << 14 << 99.0;
116 *Power_Mixture_Correlation << 15 << 96.4;
117 *Power_Mixture_Correlation << 16 << 92.5;
118 *Power_Mixture_Correlation << 17 << 88.0;
119 *Power_Mixture_Correlation << 18 << 83.0;
120 *Power_Mixture_Correlation << 19 << 78.5;
121 *Power_Mixture_Correlation << 20 << 74.0;
122 *Power_Mixture_Correlation << (14.7/0.6) << 58;
124 Debug(0); // Call Debug() routine from constructor if needed
127 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
129 FGPiston::~FGPiston()
131 Debug(1); // Call Debug() routine from constructor if needed
134 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
136 double FGPiston::Calculate(double PowerRequired)
140 Throttle = FCS->GetThrottlePos(EngineNumber);
141 Mixture = FCS->GetMixturePos(EngineNumber);
147 p_amb = Atmosphere->GetPressure() * 48; // convert from lbs/ft2 to Pa
148 p_amb_sea_level = Atmosphere->GetPressureSL() * 48;
149 T_amb = Atmosphere->GetTemperature() * (5.0 / 9.0); // convert from Rankine to Kelvin
151 RPM = Propulsion->GetThruster(EngineNumber)->GetRPM();
153 IAS = Auxiliary->GetVcalibratedKTS();
156 doManifoldPressure();
160 //Now that the fuel flow is done check if the mixture is too lean to run the engine
161 //Assume lean limit at 22 AFR for now - thats a thi of 0.668
162 //This might be a bit generous, but since there's currently no audiable warning of impending
163 //cutout in the form of misfiring and/or rough running its probably reasonable for now.
164 if (equivalence_ratio < 0.668)
173 PowerAvailable = (HP * hptoftlbssec) - PowerRequired;
174 return PowerAvailable;
177 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
179 * Start or stop the engine.
182 void FGPiston::doEngineStartup(void)
184 // Check parameters that may alter the operating state of the engine.
185 // (spark, fuel, starter motor etc)
190 Magneto_Left = false;
191 Magneto_Right = false;
192 // Magneto positions:
201 } // neglects battery voltage, master on switch, etc for now.
203 if ((Magnetos == 1) || (Magnetos > 2)) Magneto_Left = true;
204 if (Magnetos > 1) Magneto_Right = true;
206 // Assume we have fuel for now
209 // Check if we are turning the starter motor
210 if (Cranking != Starter) {
211 // This check saves .../cranking from getting updated every loop - they
212 // only update when changed.
217 if (Cranking) crank_counter++; //Check mode of engine operation
219 if (!Running && spark && fuel) { // start the engine if revs high enough
221 if ((RPM > 450) && (crank_counter > 175)) // Add a little delay to startup
222 Running = true; // on the starter
224 if (RPM > 450) // This allows us to in-air start
225 Running = true; // when windmilling
229 // Cut the engine *power* - Note: the engine may continue to
230 // spin if the prop is in a moving airstream
232 if ( Running && (!spark || !fuel) ) Running = false;
234 // Check for stalling (RPM = 0).
238 } else if ((RPM <= 480) && (Cranking)) {
244 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
247 * Calculate the nominal manifold pressure in inches hg
249 * This function calculates nominal manifold pressure directly
250 * from the throttle position, and does not adjust it for the
251 * difference between the pressure at sea level and the pressure
252 * at the current altitude (that adjustment takes place in
253 * {@link #doEnginePower}).
255 * TODO: changes in MP should not be instantaneous -- introduce
256 * a lag between throttle changes and MP changes, to allow pressure
257 * to build up or disperse.
259 * Inputs: MinManifoldPressure_inHg, MaxManifoldPressure_inHg, Throttle
261 * Outputs: ManifoldPressure_inHg
264 void FGPiston::doManifoldPressure(void)
266 if (Running || Cranking) {
267 ManifoldPressure_inHg = MinManifoldPressure_inHg +
268 (Throttle * (MaxManifoldPressure_inHg - MinManifoldPressure_inHg));
270 ManifoldPressure_inHg = Atmosphere->GetPressure() * psftoinhg; // psf to in Hg
274 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
276 * Calculate the air flow through the engine.
278 * At this point, ManifoldPressure_inHg still represents the sea-level
279 * MP, not adjusted for altitude.
281 * Inputs: p_amb, R_air, T_amb, ManifoldPressure_inHg, Displacement,
282 * RPM, volumetric_efficiency
284 * Outputs: rho_air, m_dot_air
287 void FGPiston::doAirFlow(void)
289 rho_air = p_amb / (R_air * T_amb);
290 double rho_air_manifold = rho_air * ManifoldPressure_inHg / 29.6;
291 double displacement_SI = Displacement * in3tom3;
292 double swept_volume = (displacement_SI * (RPM/60)) / 2;
293 double v_dot_air = swept_volume * volumetric_efficiency;
294 m_dot_air = v_dot_air * rho_air_manifold;
297 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
299 * Calculate the fuel flow into the engine.
301 * Inputs: Mixture, thi_sea_level, p_amb_sea_level, p_amb, m_dot_air
303 * Outputs: equivalence_ratio, m_dot_fuel
306 void FGPiston::doFuelFlow(void)
308 double thi_sea_level = 1.3 * Mixture;
309 equivalence_ratio = thi_sea_level * p_amb_sea_level / p_amb;
310 m_dot_fuel = m_dot_air / 14.7 * equivalence_ratio;
311 FuelFlow_gph = m_dot_fuel
312 * 3600 // seconds to hours
314 / 6.6; // lb to gal_us of kerosene
317 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
319 * Calculate the power produced by the engine.
321 * Currently, the JSBSim propellor model does not allow the
322 * engine to produce enough RPMs to get up to a high horsepower.
323 * When tested with sufficient RPM, it has no trouble reaching
326 * Inputs: ManifoldPressure_inHg, p_amb, p_amb_sea_level, RPM, T_amb,
327 * equivalence_ratio, Cycles, MaxHP
329 * Outputs: Percentage_Power, HP
332 void FGPiston::doEnginePower(void)
334 ManifoldPressure_inHg *= p_amb / p_amb_sea_level;
336 double ManXRPM = ManifoldPressure_inHg * RPM;
337 // FIXME: this needs to be generalized
338 Percentage_Power = (6e-9 * ManXRPM * ManXRPM) + (8e-4 * ManXRPM) - 1.0;
339 double T_amb_degF = (T_amb * 1.8) - 459.67;
340 double T_amb_sea_lev_degF = (288 * 1.8) - 459.67;
342 Percentage_Power + ((T_amb_sea_lev_degF - T_amb_degF) * 7 /120);
343 double Percentage_of_best_power_mixture_power =
344 Power_Mixture_Correlation->GetValue(14.7 / equivalence_ratio);
346 Percentage_Power * Percentage_of_best_power_mixture_power / 100.0;
347 if (Percentage_Power < 0.0)
348 Percentage_Power = 0.0;
349 else if (Percentage_Power > 100.0)
350 Percentage_Power = 100.0;
351 HP = Percentage_Power * MaxHP / 100.0;
353 // Power output when the engine is not running
356 HP = 3.0; // This is a hack to prevent overshooting the idle rpm in the first time step
357 // It may possibly need to be changed if the prop model is changed.
358 } else if (RPM < 480) {
359 HP = 3.0 + ((480 - RPM) / 10.0);
360 // This is a guess - would be nice to find a proper starter moter torque curve
365 // Quick hack until we port the FMEP stuff
374 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
376 * Calculate the exhaust gas temperature.
378 * Inputs: equivalence_ratio, m_dot_fuel, calorific_value_fuel,
379 * Cp_air, m_dot_air, Cp_fuel, m_dot_fuel, T_amb, Percentage_Power
381 * Outputs: combustion_efficiency, ExhaustGasTemp_degK
384 void FGPiston::doEGT(void)
386 double delta_T_exhaust;
387 double enthalpy_exhaust;
388 double heat_capacity_exhaust;
391 if ((Running) && (m_dot_air > 0.0)) { // do the energy balance
392 combustion_efficiency = Lookup_Combustion_Efficiency->GetValue(equivalence_ratio);
393 enthalpy_exhaust = m_dot_fuel * calorific_value_fuel *
394 combustion_efficiency * 0.33;
395 heat_capacity_exhaust = (Cp_air * m_dot_air) + (Cp_fuel * m_dot_fuel);
396 delta_T_exhaust = enthalpy_exhaust / heat_capacity_exhaust;
397 ExhaustGasTemp_degK = T_amb + delta_T_exhaust;
398 ExhaustGasTemp_degK *= 0.444 + ((0.544 - 0.444) * Percentage_Power / 100.0);
399 } else { // Drop towards ambient - guess an appropriate time constant for now
400 dEGTdt = (298.0 - ExhaustGasTemp_degK) / 100.0;
401 delta_T_exhaust = dEGTdt * dt;
402 ExhaustGasTemp_degK += delta_T_exhaust;
406 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
408 * Calculate the cylinder head temperature.
410 * Inputs: T_amb, IAS, rho_air, m_dot_fuel, calorific_value_fuel,
411 * combustion_efficiency, RPM
413 * Outputs: CylinderHeadTemp_degK
416 void FGPiston::doCHT(void)
422 double arbitary_area = 1.0;
423 double CpCylinderHead = 800.0;
424 double MassCylinderHead = 8.0;
426 double temperature_difference = CylinderHeadTemp_degK - T_amb;
427 double v_apparent = IAS * 0.5144444;
428 double v_dot_cooling_air = arbitary_area * v_apparent;
429 double m_dot_cooling_air = v_dot_cooling_air * rho_air;
430 double dqdt_from_combustion =
431 m_dot_fuel * calorific_value_fuel * combustion_efficiency * 0.33;
432 double dqdt_forced = (h2 * m_dot_cooling_air * temperature_difference) +
433 (h3 * RPM * temperature_difference);
434 double dqdt_free = h1 * temperature_difference;
435 double dqdt_cylinder_head = dqdt_from_combustion + dqdt_forced + dqdt_free;
437 double HeatCapacityCylinderHead = CpCylinderHead * MassCylinderHead;
439 CylinderHeadTemp_degK +=
440 (dqdt_cylinder_head / HeatCapacityCylinderHead) * dt;
443 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
445 * Calculate the oil temperature.
447 * Inputs: Percentage_Power, running flag.
449 * Outputs: OilTemp_degK
452 void FGPiston::doOilTemperature(void)
454 double idle_percentage_power = 2.3; // approximately
455 double target_oil_temp; // Steady state oil temp at the current engine conditions
456 double time_constant; // The time constant for the differential equation
459 target_oil_temp = 363;
460 time_constant = 500; // Time constant for engine-on idling.
461 if (Percentage_Power > idle_percentage_power) {
462 time_constant /= ((Percentage_Power / idle_percentage_power) / 10.0); // adjust for power
465 target_oil_temp = 298;
466 time_constant = 1000; // Time constant for engine-off; reflects the fact
467 // that oil is no longer getting circulated
470 double dOilTempdt = (target_oil_temp - OilTemp_degK) / time_constant;
472 OilTemp_degK += (dOilTempdt * dt);
475 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
477 * Calculate the oil pressure.
481 * Outputs: OilPressure_psi
484 void FGPiston::doOilPressure(void)
486 double Oil_Press_Relief_Valve = 60; // FIXME: may vary by engine
487 double Oil_Press_RPM_Max = 1800; // FIXME: may vary by engine
488 double Design_Oil_Temp = 358; // degK; FIXME: may vary by engine
489 double Oil_Viscosity_Index = 0.25;
491 OilPressure_psi = (Oil_Press_Relief_Valve / Oil_Press_RPM_Max) * RPM;
493 if (OilPressure_psi >= Oil_Press_Relief_Valve) {
494 OilPressure_psi = Oil_Press_Relief_Valve;
497 OilPressure_psi += (Design_Oil_Temp - OilTemp_degK) * Oil_Viscosity_Index;
500 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
502 // The bitmasked value choices are as follows:
503 // unset: In this case (the default) JSBSim would only print
504 // out the normally expected messages, essentially echoing
505 // the config files as they are read. If the environment
506 // variable is not set, debug_lvl is set to 1 internally
507 // 0: This requests JSBSim not to output any messages
509 // 1: This value explicity requests the normal JSBSim
511 // 2: This value asks for a message to be printed out when
512 // a class is instantiated
513 // 4: When this value is set, a message is displayed when a
514 // FGModel object executes its Run() method
515 // 8: When this value is set, various runtime state variables
516 // are printed out periodically
517 // 16: When set various parameters are sanity checked and
518 // a message is printed out when they go out of bounds
520 void FGPiston::Debug(int from)
522 if (debug_lvl <= 0) return;
524 if (debug_lvl & 1) { // Standard console startup message output
525 if (from == 0) { // Constructor
527 cout << "\n Engine Name: " << Name << endl;
528 cout << " MinManifoldPressure: " << MinManifoldPressure_inHg << endl;
529 cout << " MaxManifoldPressure: " << MaxManifoldPressure_inHg << endl;
530 cout << " Displacement: " << Displacement << endl;
531 cout << " MaxHP: " << MaxHP << endl;
532 cout << " Cycles: " << Cycles << endl;
533 cout << " IdleRPM: " << IdleRPM << endl;
534 cout << " MaxThrottle: " << MaxThrottle << endl;
535 cout << " MinThrottle: " << MinThrottle << endl;
538 cout << " Combustion Efficiency table:" << endl;
539 Lookup_Combustion_Efficiency->Print();
543 cout << " Power Mixture Correlation table:" << endl;
544 Power_Mixture_Correlation->Print();
549 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
550 if (from == 0) cout << "Instantiated: FGPiston" << endl;
551 if (from == 1) cout << "Destroyed: FGPiston" << endl;
553 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
555 if (debug_lvl & 8 ) { // Runtime state variables
557 if (debug_lvl & 16) { // Sanity checking
559 if (debug_lvl & 64) {
560 if (from == 0) { // Constructor
561 cout << IdSrc << endl;
562 cout << IdHdr << endl;
568 FGPiston::CalcFuelNeed(void)
570 return FuelFlow_gph / 3600 * State->Getdt() * Propulsion->GetRate();