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),
53 CONVERT_CUBIC_INCHES_TO_METERS_CUBED(1.638706e-5),
55 rho_fuel(800), // estimate
56 calorific_value_fuel(47.3e6),
62 MinManifoldPressure_inHg = 6.5;
63 MaxManifoldPressure_inHg = 28.5;
69 Name = Eng_cfg->GetValue("NAME");
70 Eng_cfg->GetNextConfigLine();
71 while (Eng_cfg->GetValue() != string("/FG_PISTON")) {
73 if (token == "MINMP") *Eng_cfg >> MinManifoldPressure_inHg;
74 else if (token == "MAXMP") *Eng_cfg >> MaxManifoldPressure_inHg;
75 else if (token == "DISPLACEMENT") *Eng_cfg >> Displacement;
76 else if (token == "MAXHP") *Eng_cfg >> MaxHP;
77 else if (token == "CYCLES") *Eng_cfg >> Cycles;
78 else if (token == "IDLERPM") *Eng_cfg >> IdleRPM;
79 else if (token == "MAXTHROTTLE") *Eng_cfg >> MaxThrottle;
80 else if (token == "MINTHROTTLE") *Eng_cfg >> MinThrottle;
81 else cerr << "Unhandled token in Engine config file: " << token << endl;
88 ManifoldPressure_inHg = Atmosphere->GetPressure() * 0.014138; // psf to in Hg
93 volumetric_efficiency = 0.8; // Actually f(speed, load) but this will get us running
95 // First column is thi, second is neta (combustion efficiency)
96 Lookup_Combustion_Efficiency = new FGTable(12);
97 *Lookup_Combustion_Efficiency << 0.00 << 0.980;
98 *Lookup_Combustion_Efficiency << 0.90 << 0.980;
99 *Lookup_Combustion_Efficiency << 1.00 << 0.970;
100 *Lookup_Combustion_Efficiency << 1.05 << 0.950;
101 *Lookup_Combustion_Efficiency << 1.10 << 0.900;
102 *Lookup_Combustion_Efficiency << 1.15 << 0.850;
103 *Lookup_Combustion_Efficiency << 1.20 << 0.790;
104 *Lookup_Combustion_Efficiency << 1.30 << 0.700;
105 *Lookup_Combustion_Efficiency << 1.40 << 0.630;
106 *Lookup_Combustion_Efficiency << 1.50 << 0.570;
107 *Lookup_Combustion_Efficiency << 1.60 << 0.525;
108 *Lookup_Combustion_Efficiency << 2.00 << 0.345;
110 Power_Mixture_Correlation = new FGTable(13);
111 *Power_Mixture_Correlation << (14.7/1.6) << 78.0;
112 *Power_Mixture_Correlation << 10 << 86.0;
113 *Power_Mixture_Correlation << 11 << 93.5;
114 *Power_Mixture_Correlation << 12 << 98.0;
115 *Power_Mixture_Correlation << 13 << 100.0;
116 *Power_Mixture_Correlation << 14 << 99.0;
117 *Power_Mixture_Correlation << 15 << 96.4;
118 *Power_Mixture_Correlation << 16 << 92.5;
119 *Power_Mixture_Correlation << 17 << 88.0;
120 *Power_Mixture_Correlation << 18 << 83.0;
121 *Power_Mixture_Correlation << 19 << 78.5;
122 *Power_Mixture_Correlation << 20 << 74.0;
123 *Power_Mixture_Correlation << (14.7/0.6) << 58;
125 Debug(0); // Call Debug() routine from constructor if needed
128 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
130 FGPiston::~FGPiston()
132 Debug(1); // Call Debug() routine from constructor if needed
135 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
137 double FGPiston::Calculate(double PowerRequired)
141 Throttle = FCS->GetThrottlePos(EngineNumber);
142 Mixture = FCS->GetMixturePos(EngineNumber);
148 p_amb = Atmosphere->GetPressure() * 48; // convert from lbs/ft2 to Pa
149 p_amb_sea_level = Atmosphere->GetPressureSL() * 48;
150 T_amb = Atmosphere->GetTemperature() * (5.0 / 9.0); // convert from Rankine to Kelvin
152 RPM = Propulsion->GetThruster(EngineNumber)->GetRPM();
153 //if (RPM < IdleRPM) RPM = IdleRPM; // kludge
155 IAS = Auxiliary->GetVcalibratedKTS();
157 if (Mixture >= 0.3) {
159 doManifoldPressure();
171 PowerAvailable = (HP * hptoftlbssec) - PowerRequired;
172 return PowerAvailable;
175 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
177 * Start or stop the engine.
180 void FGPiston::doEngineStartup(void)
182 // Check parameters that may alter the operating state of the engine.
183 // (spark, fuel, starter motor etc)
188 Magneto_Left = false;
189 Magneto_Right = false;
190 // Magneto positions:
199 } // neglects battery voltage, master on switch, etc for now.
201 if ((Magnetos == 1) || (Magnetos > 2)) Magneto_Left = true;
202 if (Magnetos > 1) Magneto_Right = true;
204 // Assume we have fuel for now
207 // Check if we are turning the starter motor
208 if (Cranking != Starter) {
209 // This check saves .../cranking from getting updated every loop - they
210 // only update when changed.
215 //Check mode of engine operation
219 // Do nothing !! - cranking power output is now handled in the doPower section
221 // consider making a horrible noise if the starter is engaged with
222 // the engine running
226 // if ((!Running) && (spark) && (fuel) && (crank_counter > 120)) {
228 if ((!Running) && (spark) && (fuel)) {
229 // start the engine if revs high enough
230 if ((RPM > 450) && (crank_counter > 175)) {
231 // For now just instantaneously start but later we should maybe crank for
238 if ( (Running) && ((!spark)||(!fuel)) ) {
240 // note that we only cut the power - the engine may continue to
241 // spin if the prop is in a moving airstream
245 // And finally a last check for stalling
247 //Check if we have stalled the engine
250 } else if ((RPM <= 480) && (Cranking)) {
251 // Make sure the engine noise dosn't play if the engine won't
252 // start due to eg mixture lever pulled out.
258 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
261 * Calculate the nominal manifold pressure in inches hg
263 * This function calculates nominal manifold pressure directly
264 * from the throttle position, and does not adjust it for the
265 * difference between the pressure at sea level and the pressure
266 * at the current altitude (that adjustment takes place in
267 * {@link #doEnginePower}).
269 * TODO: changes in MP should not be instantaneous -- introduce
270 * a lag between throttle changes and MP changes, to allow pressure
271 * to build up or disperse.
273 * Inputs: MinManifoldPressure_inHg, MaxManifoldPressure_inHg, Throttle
275 * Outputs: ManifoldPressure_inHg
278 void FGPiston::doManifoldPressure(void)
280 if (Running || Cranking) {
281 ManifoldPressure_inHg = MinManifoldPressure_inHg +
282 (Throttle * (MaxManifoldPressure_inHg - MinManifoldPressure_inHg));
284 ManifoldPressure_inHg = Atmosphere->GetPressure() * 0.014138; // psf to in Hg
288 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
290 * Calculate the air flow through the engine.
292 * At this point, ManifoldPressure_inHg still represents the sea-level
293 * MP, not adjusted for altitude.
295 * Inputs: p_amb, R_air, T_amb, ManifoldPressure_inHg, Displacement,
296 * RPM, volumetric_efficiency
298 * Outputs: rho_air, m_dot_air
301 void FGPiston::doAirFlow(void)
303 rho_air = p_amb / (R_air * T_amb);
304 double rho_air_manifold = rho_air * ManifoldPressure_inHg / 29.6;
305 double displacement_SI = Displacement * CONVERT_CUBIC_INCHES_TO_METERS_CUBED;
306 double swept_volume = (displacement_SI * (RPM/60)) / 2;
307 double v_dot_air = swept_volume * volumetric_efficiency;
308 m_dot_air = v_dot_air * rho_air_manifold;
311 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
313 * Calculate the fuel flow into the engine.
315 * Inputs: Mixture, thi_sea_level, p_amb_sea_level, p_amb, m_dot_air
317 * Outputs: equivalence_ratio, m_dot_fuel
320 void FGPiston::doFuelFlow(void)
322 double thi_sea_level = 1.3 * Mixture;
323 equivalence_ratio = thi_sea_level * p_amb_sea_level / p_amb;
324 m_dot_fuel = m_dot_air / 14.7 * equivalence_ratio;
325 FuelFlow_gph = m_dot_fuel
326 * 3600 // seconds to hours
328 / 6.6; // lb to gal_us of kerosene
331 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
333 * Calculate the power produced by the engine.
335 * Currently, the JSBSim propellor model does not allow the
336 * engine to produce enough RPMs to get up to a high horsepower.
337 * When tested with sufficient RPM, it has no trouble reaching
340 * Inputs: ManifoldPressure_inHg, p_amb, p_amb_sea_level, RPM, T_amb,
341 * equivalence_ratio, Cycles, MaxHP
343 * Outputs: Percentage_Power, HP
346 void FGPiston::doEnginePower(void)
348 ManifoldPressure_inHg *= p_amb / p_amb_sea_level;
350 double ManXRPM = ManifoldPressure_inHg * RPM;
351 // FIXME: this needs to be generalized
352 Percentage_Power = (6e-9 * ManXRPM * ManXRPM) + (8e-4 * ManXRPM) - 1.0;
353 double T_amb_degF = (T_amb * 1.8) - 459.67;
354 double T_amb_sea_lev_degF = (288 * 1.8) - 459.67;
356 Percentage_Power + ((T_amb_sea_lev_degF - T_amb_degF) * 7 /120);
357 double Percentage_of_best_power_mixture_power =
358 Power_Mixture_Correlation->GetValue(14.7 / equivalence_ratio);
360 Percentage_Power * Percentage_of_best_power_mixture_power / 100.0;
361 if (Percentage_Power < 0.0)
362 Percentage_Power = 0.0;
363 else if (Percentage_Power > 100.0)
364 Percentage_Power = 100.0;
365 HP = Percentage_Power * MaxHP / 100.0;
367 // Power output when the engine is not running
370 HP = 3.0; // This is a hack to prevent overshooting the idle rpm in the first time step
371 // It may possibly need to be changed if the prop model is changed.
372 } else if (RPM < 480) {
373 HP = 3.0 + ((480 - RPM) / 10.0);
374 // This is a guess - would be nice to find a proper starter moter torque curve
379 // Quick hack until we port the FMEP stuff
388 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
390 * Calculate the exhaust gas temperature.
392 * Inputs: equivalence_ratio, m_dot_fuel, calorific_value_fuel,
393 * Cp_air, m_dot_air, Cp_fuel, m_dot_fuel, T_amb, Percentage_Power
395 * Outputs: combustion_efficiency, ExhaustGasTemp_degK
398 void FGPiston::doEGT(void)
400 double delta_T_exhaust;
401 double enthalpy_exhaust;
402 double heat_capacity_exhaust;
405 if ((Running) && (m_dot_air > 0.0)) { // do the energy balance
406 combustion_efficiency = Lookup_Combustion_Efficiency->GetValue(equivalence_ratio);
407 enthalpy_exhaust = m_dot_fuel * calorific_value_fuel *
408 combustion_efficiency * 0.33;
409 heat_capacity_exhaust = (Cp_air * m_dot_air) + (Cp_fuel * m_dot_fuel);
410 delta_T_exhaust = enthalpy_exhaust / heat_capacity_exhaust;
411 ExhaustGasTemp_degK = T_amb + delta_T_exhaust;
412 ExhaustGasTemp_degK *= 0.444 + ((0.544 - 0.444) * Percentage_Power / 100.0);
413 } else { // Drop towards ambient - guess an appropriate time constant for now
414 dEGTdt = (298.0 - ExhaustGasTemp_degK) / 100.0;
415 delta_T_exhaust = dEGTdt * dt;
416 ExhaustGasTemp_degK += delta_T_exhaust;
420 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
422 * Calculate the cylinder head temperature.
424 * Inputs: T_amb, IAS, rho_air, m_dot_fuel, calorific_value_fuel,
425 * combustion_efficiency, RPM
427 * Outputs: CylinderHeadTemp_degK
430 void FGPiston::doCHT(void)
436 double arbitary_area = 1.0;
437 double CpCylinderHead = 800.0;
438 double MassCylinderHead = 8.0;
440 double temperature_difference = CylinderHeadTemp_degK - T_amb;
441 double v_apparent = IAS * 0.5144444;
442 double v_dot_cooling_air = arbitary_area * v_apparent;
443 double m_dot_cooling_air = v_dot_cooling_air * rho_air;
444 double dqdt_from_combustion =
445 m_dot_fuel * calorific_value_fuel * combustion_efficiency * 0.33;
446 double dqdt_forced = (h2 * m_dot_cooling_air * temperature_difference) +
447 (h3 * RPM * temperature_difference);
448 double dqdt_free = h1 * temperature_difference;
449 double dqdt_cylinder_head = dqdt_from_combustion + dqdt_forced + dqdt_free;
451 double HeatCapacityCylinderHead = CpCylinderHead * MassCylinderHead;
453 CylinderHeadTemp_degK = dqdt_cylinder_head / HeatCapacityCylinderHead;
456 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
458 * Calculate the oil temperature.
460 * Inputs: Percentage_Power, running flag.
462 * Outputs: OilTemp_degK
465 void FGPiston::doOilTemperature(void)
467 double idle_percentage_power = 2.3; // approximately
468 double target_oil_temp; // Steady state oil temp at the current engine conditions
469 double time_constant; // The time constant for the differential equation
472 target_oil_temp = 363;
473 time_constant = 500; // Time constant for engine-on idling.
474 if (Percentage_Power > idle_percentage_power) {
475 time_constant /= ((Percentage_Power / idle_percentage_power) / 10.0); // adjust for power
478 target_oil_temp = 298;
479 time_constant = 1000; // Time constant for engine-off; reflects the fact
480 // that oil is no longer getting circulated
483 double dOilTempdt = (target_oil_temp - OilTemp_degK) / time_constant;
485 OilTemp_degK += (dOilTempdt * dt);
488 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
490 * Calculate the oil pressure.
494 * Outputs: OilPressure_psi
497 void FGPiston::doOilPressure(void)
499 double Oil_Press_Relief_Valve = 60; // FIXME: may vary by engine
500 double Oil_Press_RPM_Max = 1800; // FIXME: may vary by engine
501 double Design_Oil_Temp = 85; // FIXME: may vary by engine
502 // FIXME: WRONG!!! (85 degK???)
503 double Oil_Viscosity_Index = 0.25;
505 OilPressure_psi = (Oil_Press_Relief_Valve / Oil_Press_RPM_Max) * RPM;
507 if (OilPressure_psi >= Oil_Press_Relief_Valve) {
508 OilPressure_psi = Oil_Press_Relief_Valve;
511 OilPressure_psi += (Design_Oil_Temp - OilTemp_degK) * Oil_Viscosity_Index;
514 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
516 // The bitmasked value choices are as follows:
517 // unset: In this case (the default) JSBSim would only print
518 // out the normally expected messages, essentially echoing
519 // the config files as they are read. If the environment
520 // variable is not set, debug_lvl is set to 1 internally
521 // 0: This requests JSBSim not to output any messages
523 // 1: This value explicity requests the normal JSBSim
525 // 2: This value asks for a message to be printed out when
526 // a class is instantiated
527 // 4: When this value is set, a message is displayed when a
528 // FGModel object executes its Run() method
529 // 8: When this value is set, various runtime state variables
530 // are printed out periodically
531 // 16: When set various parameters are sanity checked and
532 // a message is printed out when they go out of bounds
534 void FGPiston::Debug(int from)
536 if (debug_lvl <= 0) return;
538 if (debug_lvl & 1) { // Standard console startup message output
539 if (from == 0) { // Constructor
541 cout << "\n Engine Name: " << Name << endl;
542 cout << " MinManifoldPressure: " << MinManifoldPressure_inHg << endl;
543 cout << " MaxManifoldPressure: " << MaxManifoldPressure_inHg << endl;
544 cout << " Displacement: " << Displacement << endl;
545 cout << " MaxHP: " << MaxHP << endl;
546 cout << " Cycles: " << Cycles << endl;
547 cout << " IdleRPM: " << IdleRPM << endl;
548 cout << " MaxThrottle: " << MaxThrottle << endl;
549 cout << " MinThrottle: " << MinThrottle << endl;
552 cout << " Combustion Efficiency table:" << endl;
553 Lookup_Combustion_Efficiency->Print();
557 cout << " Power Mixture Correlation table:" << endl;
558 Power_Mixture_Correlation->Print();
563 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
564 if (from == 0) cout << "Instantiated: FGPiston" << endl;
565 if (from == 1) cout << "Destroyed: FGPiston" << endl;
567 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
569 if (debug_lvl & 8 ) { // Runtime state variables
571 if (debug_lvl & 16) { // Sanity checking
573 if (debug_lvl & 64) {
574 if (from == 0) { // Constructor
575 cout << IdSrc << endl;
576 cout << IdHdr << endl;
582 FGPiston::CalcFuelNeed(void)
584 return FuelFlow_gph / 3600 * State->Getdt() * Propulsion->GetRate();