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"
47 static const char *IdSrc = "$Id$";
48 static const char *IdHdr = ID_PISTON;
50 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
52 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
54 FGPiston::FGPiston(FGFDMExec* exec, FGConfigFile* Eng_cfg) : FGEngine(exec),
56 rho_fuel(800), // estimate
57 calorific_value_fuel(47.3e6),
67 MinManifoldPressure_inHg = 6.5;
68 MaxManifoldPressure_inHg = 28.5;
69 ManifoldPressure_inHg = Atmosphere->GetPressure() * psftoinhg; // psf to in Hg
70 CylinderHeadTemp_degK = 0.0;
76 ExhaustGasTemp_degK = 0.0;
82 volumetric_efficiency = 0.8; // Actually f(speed, load) but this will get us running
84 // First column is thi, second is neta (combustion efficiency)
85 Lookup_Combustion_Efficiency = new FGTable(12);
86 *Lookup_Combustion_Efficiency << 0.00 << 0.980;
87 *Lookup_Combustion_Efficiency << 0.90 << 0.980;
88 *Lookup_Combustion_Efficiency << 1.00 << 0.970;
89 *Lookup_Combustion_Efficiency << 1.05 << 0.950;
90 *Lookup_Combustion_Efficiency << 1.10 << 0.900;
91 *Lookup_Combustion_Efficiency << 1.15 << 0.850;
92 *Lookup_Combustion_Efficiency << 1.20 << 0.790;
93 *Lookup_Combustion_Efficiency << 1.30 << 0.700;
94 *Lookup_Combustion_Efficiency << 1.40 << 0.630;
95 *Lookup_Combustion_Efficiency << 1.50 << 0.570;
96 *Lookup_Combustion_Efficiency << 1.60 << 0.525;
97 *Lookup_Combustion_Efficiency << 2.00 << 0.345;
99 Power_Mixture_Correlation = new FGTable(13);
100 *Power_Mixture_Correlation << (14.7/1.6) << 78.0;
101 *Power_Mixture_Correlation << 10 << 86.0;
102 *Power_Mixture_Correlation << 11 << 93.5;
103 *Power_Mixture_Correlation << 12 << 98.0;
104 *Power_Mixture_Correlation << 13 << 100.0;
105 *Power_Mixture_Correlation << 14 << 99.0;
106 *Power_Mixture_Correlation << 15 << 96.4;
107 *Power_Mixture_Correlation << 16 << 92.5;
108 *Power_Mixture_Correlation << 17 << 88.0;
109 *Power_Mixture_Correlation << 18 << 83.0;
110 *Power_Mixture_Correlation << 19 << 78.5;
111 *Power_Mixture_Correlation << 20 << 74.0;
112 *Power_Mixture_Correlation << (14.7/0.6) << 58;
114 Name = Eng_cfg->GetValue("NAME");
115 Eng_cfg->GetNextConfigLine();
116 while (Eng_cfg->GetValue() != string("/FG_PISTON")) {
118 if (token == "MINMP") *Eng_cfg >> MinManifoldPressure_inHg;
119 else if (token == "MAXMP") *Eng_cfg >> MaxManifoldPressure_inHg;
120 else if (token == "DISPLACEMENT") *Eng_cfg >> Displacement;
121 else if (token == "MAXHP") *Eng_cfg >> MaxHP;
122 else if (token == "CYCLES") *Eng_cfg >> Cycles;
123 else if (token == "IDLERPM") *Eng_cfg >> IdleRPM;
124 else if (token == "MAXTHROTTLE") *Eng_cfg >> MaxThrottle;
125 else if (token == "MINTHROTTLE") *Eng_cfg >> MinThrottle;
126 else cerr << "Unhandled token in Engine config file: " << token << endl;
129 Debug(0); // Call Debug() routine from constructor if needed
132 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
134 FGPiston::~FGPiston()
136 Debug(1); // Call Debug() routine from constructor if needed
139 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
141 double FGPiston::Calculate(double PowerRequired)
143 if (FuelFlow_gph > 0.0) ConsumeFuel();
145 Throttle = FCS->GetThrottlePos(EngineNumber);
146 Mixture = FCS->GetMixturePos(EngineNumber);
152 p_amb = Atmosphere->GetPressure() * 48; // convert from lbs/ft2 to Pa
153 p_amb_sea_level = Atmosphere->GetPressureSL() * 48;
154 T_amb = Atmosphere->GetTemperature() * (5.0 / 9.0); // convert from Rankine to Kelvin
156 RPM = Propulsion->GetThruster(EngineNumber)->GetRPM() *
157 Propulsion->GetThruster(EngineNumber)->GetGearRatio();
159 IAS = Auxiliary->GetVcalibratedKTS();
162 doManifoldPressure();
166 //Now that the fuel flow is done check if the mixture is too lean to run the engine
167 //Assume lean limit at 22 AFR for now - thats a thi of 0.668
168 //This might be a bit generous, but since there's currently no audiable warning of impending
169 //cutout in the form of misfiring and/or rough running its probably reasonable for now.
170 if (equivalence_ratio < 0.668)
179 PowerAvailable = (HP * hptoftlbssec) - PowerRequired;
180 return PowerAvailable;
183 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
185 * Start or stop the engine.
188 void FGPiston::doEngineStartup(void)
190 // Check parameters that may alter the operating state of the engine.
191 // (spark, fuel, starter motor etc)
196 Magneto_Left = false;
197 Magneto_Right = false;
198 // Magneto positions:
207 } // neglects battery voltage, master on switch, etc for now.
209 if ((Magnetos == 1) || (Magnetos > 2)) Magneto_Left = true;
210 if (Magnetos > 1) Magneto_Right = true;
212 // Assume we have fuel for now
215 // Check if we are turning the starter motor
216 if (Cranking != Starter) {
217 // This check saves .../cranking from getting updated every loop - they
218 // only update when changed.
223 if (Cranking) crank_counter++; //Check mode of engine operation
225 if (!Running && spark && fuel) { // start the engine if revs high enough
227 if ((RPM > 450) && (crank_counter > 175)) // Add a little delay to startup
228 Running = true; // on the starter
230 if (RPM > 450) // This allows us to in-air start
231 Running = true; // when windmilling
235 // Cut the engine *power* - Note: the engine may continue to
236 // spin if the prop is in a moving airstream
238 if ( Running && (!spark || !fuel) ) Running = false;
240 // Check for stalling (RPM = 0).
244 } else if ((RPM <= 480) && (Cranking)) {
250 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
253 * Calculate the nominal manifold pressure in inches hg
255 * This function calculates nominal manifold pressure directly
256 * from the throttle position, and does not adjust it for the
257 * difference between the pressure at sea level and the pressure
258 * at the current altitude (that adjustment takes place in
259 * {@link #doEnginePower}).
261 * TODO: changes in MP should not be instantaneous -- introduce
262 * a lag between throttle changes and MP changes, to allow pressure
263 * to build up or disperse.
265 * Inputs: MinManifoldPressure_inHg, MaxManifoldPressure_inHg, Throttle
267 * Outputs: ManifoldPressure_inHg
270 void FGPiston::doManifoldPressure(void)
272 if (Running || Cranking) {
273 ManifoldPressure_inHg = MinManifoldPressure_inHg +
274 (Throttle * (MaxManifoldPressure_inHg - MinManifoldPressure_inHg));
276 ManifoldPressure_inHg = Atmosphere->GetPressure() * psftoinhg; // psf to in Hg
280 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
282 * Calculate the air flow through the engine.
284 * At this point, ManifoldPressure_inHg still represents the sea-level
285 * MP, not adjusted for altitude.
287 * Inputs: p_amb, R_air, T_amb, ManifoldPressure_inHg, Displacement,
288 * RPM, volumetric_efficiency
290 * Outputs: rho_air, m_dot_air
293 void FGPiston::doAirFlow(void)
295 rho_air = p_amb / (R_air * T_amb);
296 double rho_air_manifold = rho_air * ManifoldPressure_inHg / 29.6;
297 double displacement_SI = Displacement * in3tom3;
298 double swept_volume = (displacement_SI * (RPM/60)) / 2;
299 double v_dot_air = swept_volume * volumetric_efficiency;
300 m_dot_air = v_dot_air * rho_air_manifold;
303 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
305 * Calculate the fuel flow into the engine.
307 * Inputs: Mixture, thi_sea_level, p_amb_sea_level, p_amb, m_dot_air
309 * Outputs: equivalence_ratio, m_dot_fuel
312 void FGPiston::doFuelFlow(void)
314 double thi_sea_level = 1.3 * Mixture;
315 equivalence_ratio = thi_sea_level * p_amb_sea_level / p_amb;
316 m_dot_fuel = m_dot_air / 14.7 * equivalence_ratio;
317 FuelFlow_gph = m_dot_fuel
318 * 3600 // seconds to hours
320 / 6.6; // lb to gal_us of kerosene
323 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
325 * Calculate the power produced by the engine.
327 * Currently, the JSBSim propellor model does not allow the
328 * engine to produce enough RPMs to get up to a high horsepower.
329 * When tested with sufficient RPM, it has no trouble reaching
332 * Inputs: ManifoldPressure_inHg, p_amb, p_amb_sea_level, RPM, T_amb,
333 * equivalence_ratio, Cycles, MaxHP
335 * Outputs: Percentage_Power, HP
338 void FGPiston::doEnginePower(void)
340 ManifoldPressure_inHg *= p_amb / p_amb_sea_level;
343 double ManXRPM = ManifoldPressure_inHg * RPM;
344 double T_amb_degF = KelvinToFahrenheit(T_amb);
345 double T_amb_sea_lev_degF = KelvinToFahrenheit(288);
347 // FIXME: this needs to be generalized
348 Percentage_Power = (6e-9 * ManXRPM * ManXRPM) + (8e-4 * ManXRPM) - 1.0;
349 Percentage_Power += ((T_amb_sea_lev_degF - T_amb_degF) * 7 /120);
351 double Percentage_of_best_power_mixture_power =
352 Power_Mixture_Correlation->GetValue(14.7 / equivalence_ratio);
354 Percentage_Power *= Percentage_of_best_power_mixture_power / 100.0;
356 if (Percentage_Power < 0.0) Percentage_Power = 0.0;
357 else if (Percentage_Power > 100.0) Percentage_Power = 100.0;
359 HP = Percentage_Power * MaxHP / 100.0;
363 // Power output when the engine is not running
366 HP = 3.0; // This is a hack to prevent overshooting the idle rpm in
367 // the first time step. It may possibly need to be changed
368 // if the prop model is changed.
369 } else if (RPM < 480) {
370 HP = 3.0 + ((480 - RPM) / 10.0);
371 // This is a guess - would be nice to find a proper starter moter torque curve
376 // Quick hack until we port the FMEP stuff
385 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
387 * Calculate the exhaust gas temperature.
389 * Inputs: equivalence_ratio, m_dot_fuel, calorific_value_fuel,
390 * Cp_air, m_dot_air, Cp_fuel, m_dot_fuel, T_amb, Percentage_Power
392 * Outputs: combustion_efficiency, ExhaustGasTemp_degK
395 void FGPiston::doEGT(void)
397 double delta_T_exhaust;
398 double enthalpy_exhaust;
399 double heat_capacity_exhaust;
402 if ((Running) && (m_dot_air > 0.0)) { // do the energy balance
403 combustion_efficiency = Lookup_Combustion_Efficiency->GetValue(equivalence_ratio);
404 enthalpy_exhaust = m_dot_fuel * calorific_value_fuel *
405 combustion_efficiency * 0.33;
406 heat_capacity_exhaust = (Cp_air * m_dot_air) + (Cp_fuel * m_dot_fuel);
407 delta_T_exhaust = enthalpy_exhaust / heat_capacity_exhaust;
408 ExhaustGasTemp_degK = T_amb + delta_T_exhaust;
409 ExhaustGasTemp_degK *= 0.444 + ((0.544 - 0.444) * Percentage_Power / 100.0);
410 } else { // Drop towards ambient - guess an appropriate time constant for now
411 dEGTdt = (298.0 - ExhaustGasTemp_degK) / 100.0;
412 delta_T_exhaust = dEGTdt * dt;
413 ExhaustGasTemp_degK += delta_T_exhaust;
417 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
419 * Calculate the cylinder head temperature.
421 * Inputs: T_amb, IAS, rho_air, m_dot_fuel, calorific_value_fuel,
422 * combustion_efficiency, RPM
424 * Outputs: CylinderHeadTemp_degK
427 void FGPiston::doCHT(void)
433 double arbitary_area = 1.0;
434 double CpCylinderHead = 800.0;
435 double MassCylinderHead = 8.0;
437 double temperature_difference = CylinderHeadTemp_degK - T_amb;
438 double v_apparent = IAS * 0.5144444;
439 double v_dot_cooling_air = arbitary_area * v_apparent;
440 double m_dot_cooling_air = v_dot_cooling_air * rho_air;
441 double dqdt_from_combustion =
442 m_dot_fuel * calorific_value_fuel * combustion_efficiency * 0.33;
443 double dqdt_forced = (h2 * m_dot_cooling_air * temperature_difference) +
444 (h3 * RPM * temperature_difference);
445 double dqdt_free = h1 * temperature_difference;
446 double dqdt_cylinder_head = dqdt_from_combustion + dqdt_forced + dqdt_free;
448 double HeatCapacityCylinderHead = CpCylinderHead * MassCylinderHead;
450 CylinderHeadTemp_degK +=
451 (dqdt_cylinder_head / HeatCapacityCylinderHead) * dt;
454 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
456 * Calculate the oil temperature.
458 * Inputs: Percentage_Power, running flag.
460 * Outputs: OilTemp_degK
463 void FGPiston::doOilTemperature(void)
465 double idle_percentage_power = 2.3; // approximately
466 double target_oil_temp; // Steady state oil temp at the current engine conditions
467 double time_constant; // The time constant for the differential equation
470 target_oil_temp = 363;
471 time_constant = 500; // Time constant for engine-on idling.
472 if (Percentage_Power > idle_percentage_power) {
473 time_constant /= ((Percentage_Power / idle_percentage_power) / 10.0); // adjust for power
476 target_oil_temp = 298;
477 time_constant = 1000; // Time constant for engine-off; reflects the fact
478 // that oil is no longer getting circulated
481 double dOilTempdt = (target_oil_temp - OilTemp_degK) / time_constant;
483 OilTemp_degK += (dOilTempdt * dt);
486 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
488 * Calculate the oil pressure.
492 * Outputs: OilPressure_psi
495 void FGPiston::doOilPressure(void)
497 double Oil_Press_Relief_Valve = 60; // FIXME: may vary by engine
498 double Oil_Press_RPM_Max = 1800; // FIXME: may vary by engine
499 double Design_Oil_Temp = 358; // degK; FIXME: may vary by engine
500 double Oil_Viscosity_Index = 0.25;
502 OilPressure_psi = (Oil_Press_Relief_Valve / Oil_Press_RPM_Max) * RPM;
504 if (OilPressure_psi >= Oil_Press_Relief_Valve) {
505 OilPressure_psi = Oil_Press_Relief_Valve;
508 OilPressure_psi += (Design_Oil_Temp - OilTemp_degK) * Oil_Viscosity_Index;
511 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
513 // The bitmasked value choices are as follows:
514 // unset: In this case (the default) JSBSim would only print
515 // out the normally expected messages, essentially echoing
516 // the config files as they are read. If the environment
517 // variable is not set, debug_lvl is set to 1 internally
518 // 0: This requests JSBSim not to output any messages
520 // 1: This value explicity requests the normal JSBSim
522 // 2: This value asks for a message to be printed out when
523 // a class is instantiated
524 // 4: When this value is set, a message is displayed when a
525 // FGModel object executes its Run() method
526 // 8: When this value is set, various runtime state variables
527 // are printed out periodically
528 // 16: When set various parameters are sanity checked and
529 // a message is printed out when they go out of bounds
531 void FGPiston::Debug(int from)
533 if (debug_lvl <= 0) return;
535 if (debug_lvl & 1) { // Standard console startup message output
536 if (from == 0) { // Constructor
538 cout << "\n Engine Name: " << Name << endl;
539 cout << " MinManifoldPressure: " << MinManifoldPressure_inHg << endl;
540 cout << " MaxManifoldPressure: " << MaxManifoldPressure_inHg << endl;
541 cout << " Displacement: " << Displacement << endl;
542 cout << " MaxHP: " << MaxHP << endl;
543 cout << " Cycles: " << Cycles << endl;
544 cout << " IdleRPM: " << IdleRPM << endl;
545 cout << " MaxThrottle: " << MaxThrottle << endl;
546 cout << " MinThrottle: " << MinThrottle << endl;
549 cout << " Combustion Efficiency table:" << endl;
550 Lookup_Combustion_Efficiency->Print();
554 cout << " Power Mixture Correlation table:" << endl;
555 Power_Mixture_Correlation->Print();
560 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
561 if (from == 0) cout << "Instantiated: FGPiston" << endl;
562 if (from == 1) cout << "Destroyed: FGPiston" << endl;
564 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
566 if (debug_lvl & 8 ) { // Runtime state variables
568 if (debug_lvl & 16) { // Sanity checking
570 if (debug_lvl & 64) {
571 if (from == 0) { // Constructor
572 cout << IdSrc << endl;
573 cout << IdHdr << endl;
579 FGPiston::CalcFuelNeed(void)
581 return FuelFlow_gph / 3600 * 6 * State->Getdt() * Propulsion->GetRate();
584 } // namespace JSBSim