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),
63 MinManifoldPressure_inHg = 6.5;
64 MaxManifoldPressure_inHg = 28.5;
70 Name = Eng_cfg->GetValue("NAME");
71 Eng_cfg->GetNextConfigLine();
72 while (Eng_cfg->GetValue() != string("/FG_PISTON")) {
74 if (token == "MINMP") *Eng_cfg >> MinManifoldPressure_inHg;
75 else if (token == "MAXMP") *Eng_cfg >> MaxManifoldPressure_inHg;
76 else if (token == "DISPLACEMENT") *Eng_cfg >> Displacement;
77 else if (token == "MAXHP") *Eng_cfg >> MaxHP;
78 else if (token == "CYCLES") *Eng_cfg >> Cycles;
79 else if (token == "IDLERPM") *Eng_cfg >> IdleRPM;
80 else if (token == "MAXTHROTTLE") *Eng_cfg >> MaxThrottle;
81 else if (token == "MINTHROTTLE") *Eng_cfg >> MinThrottle;
82 else cerr << "Unhandled token in Engine config file: " << token << endl;
89 ManifoldPressure_inHg = Atmosphere->GetPressure() * psftoinhg; // psf to in Hg
94 volumetric_efficiency = 0.8; // Actually f(speed, load) but this will get us running
96 // First column is thi, second is neta (combustion efficiency)
97 Lookup_Combustion_Efficiency = new FGTable(12);
98 *Lookup_Combustion_Efficiency << 0.00 << 0.980;
99 *Lookup_Combustion_Efficiency << 0.90 << 0.980;
100 *Lookup_Combustion_Efficiency << 1.00 << 0.970;
101 *Lookup_Combustion_Efficiency << 1.05 << 0.950;
102 *Lookup_Combustion_Efficiency << 1.10 << 0.900;
103 *Lookup_Combustion_Efficiency << 1.15 << 0.850;
104 *Lookup_Combustion_Efficiency << 1.20 << 0.790;
105 *Lookup_Combustion_Efficiency << 1.30 << 0.700;
106 *Lookup_Combustion_Efficiency << 1.40 << 0.630;
107 *Lookup_Combustion_Efficiency << 1.50 << 0.570;
108 *Lookup_Combustion_Efficiency << 1.60 << 0.525;
109 *Lookup_Combustion_Efficiency << 2.00 << 0.345;
111 Power_Mixture_Correlation = new FGTable(13);
112 *Power_Mixture_Correlation << (14.7/1.6) << 78.0;
113 *Power_Mixture_Correlation << 10 << 86.0;
114 *Power_Mixture_Correlation << 11 << 93.5;
115 *Power_Mixture_Correlation << 12 << 98.0;
116 *Power_Mixture_Correlation << 13 << 100.0;
117 *Power_Mixture_Correlation << 14 << 99.0;
118 *Power_Mixture_Correlation << 15 << 96.4;
119 *Power_Mixture_Correlation << 16 << 92.5;
120 *Power_Mixture_Correlation << 17 << 88.0;
121 *Power_Mixture_Correlation << 18 << 83.0;
122 *Power_Mixture_Correlation << 19 << 78.5;
123 *Power_Mixture_Correlation << 20 << 74.0;
124 *Power_Mixture_Correlation << (14.7/0.6) << 58;
126 Debug(0); // Call Debug() routine from constructor if needed
129 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
131 FGPiston::~FGPiston()
133 Debug(1); // Call Debug() routine from constructor if needed
136 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
138 double FGPiston::Calculate(double PowerRequired)
142 Throttle = FCS->GetThrottlePos(EngineNumber);
143 Mixture = FCS->GetMixturePos(EngineNumber);
149 p_amb = Atmosphere->GetPressure() * 48; // convert from lbs/ft2 to Pa
150 p_amb_sea_level = Atmosphere->GetPressureSL() * 48;
151 T_amb = Atmosphere->GetTemperature() * (5.0 / 9.0); // convert from Rankine to Kelvin
153 RPM = Propulsion->GetThruster(EngineNumber)->GetRPM();
155 IAS = Auxiliary->GetVcalibratedKTS();
158 doManifoldPressure();
162 //Now that the fuel flow is done check if the mixture is too lean to run the engine
163 //Assume lean limit at 22 AFR for now - thats a thi of 0.668
164 //This might be a bit generous, but since there's currently no audiable warning of impending
165 //cutout in the form of misfiring and/or rough running its probably reasonable for now.
166 if (equivalence_ratio < 0.668)
175 PowerAvailable = (HP * hptoftlbssec) - PowerRequired;
176 return PowerAvailable;
179 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
181 * Start or stop the engine.
184 void FGPiston::doEngineStartup(void)
186 // Check parameters that may alter the operating state of the engine.
187 // (spark, fuel, starter motor etc)
192 Magneto_Left = false;
193 Magneto_Right = false;
194 // Magneto positions:
203 } // neglects battery voltage, master on switch, etc for now.
205 if ((Magnetos == 1) || (Magnetos > 2)) Magneto_Left = true;
206 if (Magnetos > 1) Magneto_Right = true;
208 // Assume we have fuel for now
211 // Check if we are turning the starter motor
212 if (Cranking != Starter) {
213 // This check saves .../cranking from getting updated every loop - they
214 // only update when changed.
219 if (Cranking) crank_counter++; //Check mode of engine operation
221 if (!Running && spark && fuel) { // start the engine if revs high enough
223 if ((RPM > 450) && (crank_counter > 175)) // Add a little delay to startup
224 Running = true; // on the starter
226 if (RPM > 450) // This allows us to in-air start
227 Running = true; // when windmilling
231 // Cut the engine *power* - Note: the engine may continue to
232 // spin if the prop is in a moving airstream
234 if ( Running && (!spark || !fuel) ) Running = false;
236 // Check for stalling (RPM = 0).
240 } else if ((RPM <= 480) && (Cranking)) {
246 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
249 * Calculate the nominal manifold pressure in inches hg
251 * This function calculates nominal manifold pressure directly
252 * from the throttle position, and does not adjust it for the
253 * difference between the pressure at sea level and the pressure
254 * at the current altitude (that adjustment takes place in
255 * {@link #doEnginePower}).
257 * TODO: changes in MP should not be instantaneous -- introduce
258 * a lag between throttle changes and MP changes, to allow pressure
259 * to build up or disperse.
261 * Inputs: MinManifoldPressure_inHg, MaxManifoldPressure_inHg, Throttle
263 * Outputs: ManifoldPressure_inHg
266 void FGPiston::doManifoldPressure(void)
268 if (Running || Cranking) {
269 ManifoldPressure_inHg = MinManifoldPressure_inHg +
270 (Throttle * (MaxManifoldPressure_inHg - MinManifoldPressure_inHg));
272 ManifoldPressure_inHg = Atmosphere->GetPressure() * psftoinhg; // psf to in Hg
276 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
278 * Calculate the air flow through the engine.
280 * At this point, ManifoldPressure_inHg still represents the sea-level
281 * MP, not adjusted for altitude.
283 * Inputs: p_amb, R_air, T_amb, ManifoldPressure_inHg, Displacement,
284 * RPM, volumetric_efficiency
286 * Outputs: rho_air, m_dot_air
289 void FGPiston::doAirFlow(void)
291 rho_air = p_amb / (R_air * T_amb);
292 double rho_air_manifold = rho_air * ManifoldPressure_inHg / 29.6;
293 double displacement_SI = Displacement * in3tom3;
294 double swept_volume = (displacement_SI * (RPM/60)) / 2;
295 double v_dot_air = swept_volume * volumetric_efficiency;
296 m_dot_air = v_dot_air * rho_air_manifold;
299 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
301 * Calculate the fuel flow into the engine.
303 * Inputs: Mixture, thi_sea_level, p_amb_sea_level, p_amb, m_dot_air
305 * Outputs: equivalence_ratio, m_dot_fuel
308 void FGPiston::doFuelFlow(void)
310 double thi_sea_level = 1.3 * Mixture;
311 equivalence_ratio = thi_sea_level * p_amb_sea_level / p_amb;
312 m_dot_fuel = m_dot_air / 14.7 * equivalence_ratio;
313 FuelFlow_gph = m_dot_fuel
314 * 3600 // seconds to hours
316 / 6.6; // lb to gal_us of kerosene
319 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
321 * Calculate the power produced by the engine.
323 * Currently, the JSBSim propellor model does not allow the
324 * engine to produce enough RPMs to get up to a high horsepower.
325 * When tested with sufficient RPM, it has no trouble reaching
328 * Inputs: ManifoldPressure_inHg, p_amb, p_amb_sea_level, RPM, T_amb,
329 * equivalence_ratio, Cycles, MaxHP
331 * Outputs: Percentage_Power, HP
334 void FGPiston::doEnginePower(void)
336 ManifoldPressure_inHg *= p_amb / p_amb_sea_level;
338 double ManXRPM = ManifoldPressure_inHg * RPM;
339 // FIXME: this needs to be generalized
340 Percentage_Power = (6e-9 * ManXRPM * ManXRPM) + (8e-4 * ManXRPM) - 1.0;
341 double T_amb_degF = (T_amb * 1.8) - 459.67;
342 double T_amb_sea_lev_degF = (288 * 1.8) - 459.67;
344 Percentage_Power + ((T_amb_sea_lev_degF - T_amb_degF) * 7 /120);
345 double Percentage_of_best_power_mixture_power =
346 Power_Mixture_Correlation->GetValue(14.7 / equivalence_ratio);
348 Percentage_Power * Percentage_of_best_power_mixture_power / 100.0;
349 if (Percentage_Power < 0.0)
350 Percentage_Power = 0.0;
351 else if (Percentage_Power > 100.0)
352 Percentage_Power = 100.0;
353 HP = Percentage_Power * MaxHP / 100.0;
355 // Power output when the engine is not running
358 HP = 3.0; // This is a hack to prevent overshooting the idle rpm in the first time step
359 // It may possibly need to be changed if the prop model is changed.
360 } else if (RPM < 480) {
361 HP = 3.0 + ((480 - RPM) / 10.0);
362 // This is a guess - would be nice to find a proper starter moter torque curve
367 // Quick hack until we port the FMEP stuff
376 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
378 * Calculate the exhaust gas temperature.
380 * Inputs: equivalence_ratio, m_dot_fuel, calorific_value_fuel,
381 * Cp_air, m_dot_air, Cp_fuel, m_dot_fuel, T_amb, Percentage_Power
383 * Outputs: combustion_efficiency, ExhaustGasTemp_degK
386 void FGPiston::doEGT(void)
388 double delta_T_exhaust;
389 double enthalpy_exhaust;
390 double heat_capacity_exhaust;
393 if ((Running) && (m_dot_air > 0.0)) { // do the energy balance
394 combustion_efficiency = Lookup_Combustion_Efficiency->GetValue(equivalence_ratio);
395 enthalpy_exhaust = m_dot_fuel * calorific_value_fuel *
396 combustion_efficiency * 0.33;
397 heat_capacity_exhaust = (Cp_air * m_dot_air) + (Cp_fuel * m_dot_fuel);
398 delta_T_exhaust = enthalpy_exhaust / heat_capacity_exhaust;
399 ExhaustGasTemp_degK = T_amb + delta_T_exhaust;
400 ExhaustGasTemp_degK *= 0.444 + ((0.544 - 0.444) * Percentage_Power / 100.0);
401 } else { // Drop towards ambient - guess an appropriate time constant for now
402 dEGTdt = (298.0 - ExhaustGasTemp_degK) / 100.0;
403 delta_T_exhaust = dEGTdt * dt;
404 ExhaustGasTemp_degK += delta_T_exhaust;
408 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
410 * Calculate the cylinder head temperature.
412 * Inputs: T_amb, IAS, rho_air, m_dot_fuel, calorific_value_fuel,
413 * combustion_efficiency, RPM
415 * Outputs: CylinderHeadTemp_degK
418 void FGPiston::doCHT(void)
424 double arbitary_area = 1.0;
425 double CpCylinderHead = 800.0;
426 double MassCylinderHead = 8.0;
428 double temperature_difference = CylinderHeadTemp_degK - T_amb;
429 double v_apparent = IAS * 0.5144444;
430 double v_dot_cooling_air = arbitary_area * v_apparent;
431 double m_dot_cooling_air = v_dot_cooling_air * rho_air;
432 double dqdt_from_combustion =
433 m_dot_fuel * calorific_value_fuel * combustion_efficiency * 0.33;
434 double dqdt_forced = (h2 * m_dot_cooling_air * temperature_difference) +
435 (h3 * RPM * temperature_difference);
436 double dqdt_free = h1 * temperature_difference;
437 double dqdt_cylinder_head = dqdt_from_combustion + dqdt_forced + dqdt_free;
439 double HeatCapacityCylinderHead = CpCylinderHead * MassCylinderHead;
441 CylinderHeadTemp_degK +=
442 (dqdt_cylinder_head / HeatCapacityCylinderHead) * dt;
445 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
447 * Calculate the oil temperature.
449 * Inputs: Percentage_Power, running flag.
451 * Outputs: OilTemp_degK
454 void FGPiston::doOilTemperature(void)
456 double idle_percentage_power = 2.3; // approximately
457 double target_oil_temp; // Steady state oil temp at the current engine conditions
458 double time_constant; // The time constant for the differential equation
461 target_oil_temp = 363;
462 time_constant = 500; // Time constant for engine-on idling.
463 if (Percentage_Power > idle_percentage_power) {
464 time_constant /= ((Percentage_Power / idle_percentage_power) / 10.0); // adjust for power
467 target_oil_temp = 298;
468 time_constant = 1000; // Time constant for engine-off; reflects the fact
469 // that oil is no longer getting circulated
472 double dOilTempdt = (target_oil_temp - OilTemp_degK) / time_constant;
474 OilTemp_degK += (dOilTempdt * dt);
477 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
479 * Calculate the oil pressure.
483 * Outputs: OilPressure_psi
486 void FGPiston::doOilPressure(void)
488 double Oil_Press_Relief_Valve = 60; // FIXME: may vary by engine
489 double Oil_Press_RPM_Max = 1800; // FIXME: may vary by engine
490 double Design_Oil_Temp = 358; // degK; FIXME: may vary by engine
491 double Oil_Viscosity_Index = 0.25;
493 OilPressure_psi = (Oil_Press_Relief_Valve / Oil_Press_RPM_Max) * RPM;
495 if (OilPressure_psi >= Oil_Press_Relief_Valve) {
496 OilPressure_psi = Oil_Press_Relief_Valve;
499 OilPressure_psi += (Design_Oil_Temp - OilTemp_degK) * Oil_Viscosity_Index;
502 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
504 // The bitmasked value choices are as follows:
505 // unset: In this case (the default) JSBSim would only print
506 // out the normally expected messages, essentially echoing
507 // the config files as they are read. If the environment
508 // variable is not set, debug_lvl is set to 1 internally
509 // 0: This requests JSBSim not to output any messages
511 // 1: This value explicity requests the normal JSBSim
513 // 2: This value asks for a message to be printed out when
514 // a class is instantiated
515 // 4: When this value is set, a message is displayed when a
516 // FGModel object executes its Run() method
517 // 8: When this value is set, various runtime state variables
518 // are printed out periodically
519 // 16: When set various parameters are sanity checked and
520 // a message is printed out when they go out of bounds
522 void FGPiston::Debug(int from)
524 if (debug_lvl <= 0) return;
526 if (debug_lvl & 1) { // Standard console startup message output
527 if (from == 0) { // Constructor
529 cout << "\n Engine Name: " << Name << endl;
530 cout << " MinManifoldPressure: " << MinManifoldPressure_inHg << endl;
531 cout << " MaxManifoldPressure: " << MaxManifoldPressure_inHg << endl;
532 cout << " Displacement: " << Displacement << endl;
533 cout << " MaxHP: " << MaxHP << endl;
534 cout << " Cycles: " << Cycles << endl;
535 cout << " IdleRPM: " << IdleRPM << endl;
536 cout << " MaxThrottle: " << MaxThrottle << endl;
537 cout << " MinThrottle: " << MinThrottle << endl;
540 cout << " Combustion Efficiency table:" << endl;
541 Lookup_Combustion_Efficiency->Print();
545 cout << " Power Mixture Correlation table:" << endl;
546 Power_Mixture_Correlation->Print();
551 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
552 if (from == 0) cout << "Instantiated: FGPiston" << endl;
553 if (from == 1) cout << "Destroyed: FGPiston" << endl;
555 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
557 if (debug_lvl & 8 ) { // Runtime state variables
559 if (debug_lvl & 16) { // Sanity checking
561 if (debug_lvl & 64) {
562 if (from == 0) { // Constructor
563 cout << IdSrc << endl;
564 cout << IdHdr << endl;
570 FGPiston::CalcFuelNeed(void)
572 return FuelFlow_gph / 3600 * 6 * State->Getdt() * Propulsion->GetRate();
575 } // namespace JSBSim