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)
139 // FIXME: calculate from actual fuel flow
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();
154 //if (RPM < IdleRPM) RPM = IdleRPM; // kludge
156 IAS = Auxiliary->GetVcalibratedKTS();
158 if (Mixture >= 0.3) {
160 doManifoldPressure();
172 PowerAvailable = (HP * hptoftlbssec) - PowerRequired;
173 return PowerAvailable;
176 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
178 * Start or stop the engine.
181 void FGPiston::doEngineStartup(void)
183 // TODO: check magnetos, spark, starter, etc. and decide whether
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 //Check mode of engine operation
220 // ACK - unfortunately this hack doesn't work in JSBSim since the RPM is reset
221 // each iteration by the propeller :-(
229 // consider making a horrible noise if the starter is engaged with
230 // the engine running
232 // TODO - find a better guess at cranking speed
235 // if ((!Running) && (spark) && (fuel) && (crank_counter > 120)) {
237 if ((!Running) && (spark) && (fuel)) {
238 // start the engine if revs high enough
240 // For now just instantaneously start but later we should maybe crank for
247 if ( (Running) && ((!spark)||(!fuel)) ) {
249 // note that we only cut the power - the engine may continue to
250 // spin if the prop is in a moving airstream
254 // And finally a last check for stalling
256 //Check if we have stalled the engine
259 } else if ((RPM <= 480) && (Cranking)) {
260 // Make sure the engine noise dosn't play if the engine won't
261 // start due to eg mixture lever pulled out.
267 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
270 * Calculate the nominal manifold pressure in inches hg
272 * This function calculates nominal manifold pressure directly
273 * from the throttle position, and does not adjust it for the
274 * difference between the pressure at sea level and the pressure
275 * at the current altitude (that adjustment takes place in
276 * {@link #doEnginePower}).
278 * TODO: changes in MP should not be instantaneous -- introduce
279 * a lag between throttle changes and MP changes, to allow pressure
280 * to build up or disperse.
282 * Inputs: MinManifoldPressure_inHg, MaxManifoldPressure_inHg, Throttle
284 * Outputs: ManifoldPressure_inHg
287 void FGPiston::doManifoldPressure(void)
289 if (Running || Cranking) {
290 ManifoldPressure_inHg = MinManifoldPressure_inHg +
291 (Throttle * (MaxManifoldPressure_inHg - MinManifoldPressure_inHg));
293 ManifoldPressure_inHg = Atmosphere->GetPressure() * 0.014138; // psf to in Hg
297 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
299 * Calculate the air flow through the engine.
301 * At this point, ManifoldPressure_inHg still represents the sea-level
302 * MP, not adjusted for altitude.
304 * Inputs: p_amb, R_air, T_amb, ManifoldPressure_inHg, Displacement,
305 * RPM, volumetric_efficiency
307 * Outputs: rho_air, m_dot_air
310 void FGPiston::doAirFlow(void)
312 rho_air = p_amb / (R_air * T_amb);
313 double rho_air_manifold = rho_air * ManifoldPressure_inHg / 29.6;
314 double displacement_SI = Displacement * CONVERT_CUBIC_INCHES_TO_METERS_CUBED;
315 double swept_volume = (displacement_SI * (RPM/60)) / 2;
316 double v_dot_air = swept_volume * volumetric_efficiency;
317 m_dot_air = v_dot_air * rho_air_manifold;
320 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
322 * Calculate the fuel flow into the engine.
324 * Inputs: Mixture, thi_sea_level, p_amb_sea_level, p_amb, m_dot_air
326 * Outputs: equivalence_ratio, m_dot_fuel
329 void FGPiston::doFuelFlow(void)
331 double thi_sea_level = 1.3 * Mixture;
332 equivalence_ratio = thi_sea_level * p_amb_sea_level / p_amb;
333 m_dot_fuel = m_dot_air / 14.7 * equivalence_ratio;
334 FuelFlow_gph = m_dot_fuel
335 * 3600 // seconds to hours
337 / 6.6; // lb to gal_us of kerosene
340 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
342 * Calculate the power produced by the engine.
344 * Currently, the JSBSim propellor model does not allow the
345 * engine to produce enough RPMs to get up to a high horsepower.
346 * When tested with sufficient RPM, it has no trouble reaching
349 * Inputs: ManifoldPressure_inHg, p_amb, p_amb_sea_level, RPM, T_amb,
350 * equivalence_ratio, Cycles, MaxHP
352 * Outputs: Percentage_Power, HP
355 void FGPiston::doEnginePower(void)
357 ManifoldPressure_inHg *= p_amb / p_amb_sea_level;
358 double ManXRPM = ManifoldPressure_inHg * RPM;
359 // FIXME: this needs to be generalized
360 Percentage_Power = (6e-9 * ManXRPM * ManXRPM) + (8e-4 * ManXRPM) - 1.0;
361 double T_amb_degF = (T_amb * 1.8) - 459.67;
362 double T_amb_sea_lev_degF = (288 * 1.8) - 459.67;
364 Percentage_Power + ((T_amb_sea_lev_degF - T_amb_degF) * 7 /120);
365 double Percentage_of_best_power_mixture_power =
366 Power_Mixture_Correlation->GetValue(14.7 / equivalence_ratio);
368 Percentage_Power * Percentage_of_best_power_mixture_power / 100.0;
369 if (Percentage_Power < 0.0)
370 Percentage_Power = 0.0;
371 else if (Percentage_Power > 100.0)
372 Percentage_Power = 100.0;
373 HP = Percentage_Power * MaxHP / 100.0;
379 HP = 3.0 + ((480 - RPM) / 10.0);
384 // Quick hack until we port the FMEP stuff
393 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
395 * Calculate the exhaust gas temperature.
397 * Inputs: equivalence_ratio, m_dot_fuel, calorific_value_fuel,
398 * Cp_air, m_dot_air, Cp_fuel, m_dot_fuel, T_amb, Percentage_Power
400 * Outputs: combustion_efficiency, ExhaustGasTemp_degK
403 void FGPiston::doEGT(void)
405 double delta_T_exhaust;
406 double enthalpy_exhaust;
407 double heat_capacity_exhaust;
410 if ((Running) && (m_dot_air > 0.0)) { // do the energy balance
411 combustion_efficiency = Lookup_Combustion_Efficiency->GetValue(equivalence_ratio);
412 enthalpy_exhaust = m_dot_fuel * calorific_value_fuel *
413 combustion_efficiency * 0.33;
414 heat_capacity_exhaust = (Cp_air * m_dot_air) + (Cp_fuel * m_dot_fuel);
415 delta_T_exhaust = enthalpy_exhaust / heat_capacity_exhaust;
416 ExhaustGasTemp_degK = T_amb + delta_T_exhaust;
417 ExhaustGasTemp_degK *= 0.444 + ((0.544 - 0.444) * Percentage_Power / 100.0);
418 } else { // Drop towards ambient - guess an appropriate time constant for now
419 dEGTdt = (298.0 - ExhaustGasTemp_degK) / 100.0;
420 delta_T_exhaust = dEGTdt * dt;
421 ExhaustGasTemp_degK += delta_T_exhaust;
425 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
427 * Calculate the cylinder head temperature.
429 * Inputs: T_amb, IAS, rho_air, m_dot_fuel, calorific_value_fuel,
430 * combustion_efficiency, RPM
432 * Outputs: CylinderHeadTemp_degK
435 void FGPiston::doCHT(void)
441 double arbitary_area = 1.0;
442 double CpCylinderHead = 800.0;
443 double MassCylinderHead = 8.0;
445 double temperature_difference = CylinderHeadTemp_degK - T_amb;
446 double v_apparent = IAS * 0.5144444;
447 double v_dot_cooling_air = arbitary_area * v_apparent;
448 double m_dot_cooling_air = v_dot_cooling_air * rho_air;
449 double dqdt_from_combustion =
450 m_dot_fuel * calorific_value_fuel * combustion_efficiency * 0.33;
451 double dqdt_forced = (h2 * m_dot_cooling_air * temperature_difference) +
452 (h3 * RPM * temperature_difference);
453 double dqdt_free = h1 * temperature_difference;
454 double dqdt_cylinder_head = dqdt_from_combustion + dqdt_forced + dqdt_free;
456 double HeatCapacityCylinderHead = CpCylinderHead * MassCylinderHead;
458 CylinderHeadTemp_degK = dqdt_cylinder_head / HeatCapacityCylinderHead;
461 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
463 * Calculate the oil temperature.
465 * Inputs: Percentage_Power, running flag.
467 * Outputs: OilTemp_degK
470 void FGPiston::doOilTemperature(void)
472 double idle_percentage_power = 2.3; // approximately
473 double target_oil_temp; // Steady state oil temp at the current engine conditions
474 double time_constant; // The time constant for the differential equation
477 target_oil_temp = 363;
478 time_constant = 500; // Time constant for engine-on idling.
479 if (Percentage_Power > idle_percentage_power) {
480 time_constant /= ((Percentage_Power / idle_percentage_power) / 10.0); // adjust for power
483 target_oil_temp = 298;
484 time_constant = 1000; // Time constant for engine-off; reflects the fact
485 // that oil is no longer getting circulated
488 double dOilTempdt = (target_oil_temp - OilTemp_degK) / time_constant;
490 OilTemp_degK += (dOilTempdt * dt);
493 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
495 * Calculate the oil pressure.
499 * Outputs: OilPressure_psi
502 void FGPiston::doOilPressure(void)
504 double Oil_Press_Relief_Valve = 60; // FIXME: may vary by engine
505 double Oil_Press_RPM_Max = 1800; // FIXME: may vary by engine
506 double Design_Oil_Temp = 85; // FIXME: may vary by engine
507 // FIXME: WRONG!!! (85 degK???)
508 double Oil_Viscosity_Index = 0.25;
510 OilPressure_psi = (Oil_Press_Relief_Valve / Oil_Press_RPM_Max) * RPM;
512 if (OilPressure_psi >= Oil_Press_Relief_Valve) {
513 OilPressure_psi = Oil_Press_Relief_Valve;
516 OilPressure_psi += (Design_Oil_Temp - OilTemp_degK) * Oil_Viscosity_Index;
519 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
521 // The bitmasked value choices are as follows:
522 // unset: In this case (the default) JSBSim would only print
523 // out the normally expected messages, essentially echoing
524 // the config files as they are read. If the environment
525 // variable is not set, debug_lvl is set to 1 internally
526 // 0: This requests JSBSim not to output any messages
528 // 1: This value explicity requests the normal JSBSim
530 // 2: This value asks for a message to be printed out when
531 // a class is instantiated
532 // 4: When this value is set, a message is displayed when a
533 // FGModel object executes its Run() method
534 // 8: When this value is set, various runtime state variables
535 // are printed out periodically
536 // 16: When set various parameters are sanity checked and
537 // a message is printed out when they go out of bounds
539 void FGPiston::Debug(int from)
541 if (debug_lvl <= 0) return;
543 if (debug_lvl & 1) { // Standard console startup message output
544 if (from == 0) { // Constructor
546 cout << "\n Engine Name: " << Name << endl;
547 cout << " MinManifoldPressure: " << MinManifoldPressure_inHg << endl;
548 cout << " MaxManifoldPressure: " << MaxManifoldPressure_inHg << endl;
549 cout << " Displacement: " << Displacement << endl;
550 cout << " MaxHP: " << MaxHP << endl;
551 cout << " Cycles: " << Cycles << endl;
552 cout << " IdleRPM: " << IdleRPM << endl;
553 cout << " MaxThrottle: " << MaxThrottle << endl;
554 cout << " MinThrottle: " << MinThrottle << endl;
557 cout << " Combustion Efficiency table:" << endl;
558 Lookup_Combustion_Efficiency->Print();
562 cout << " Power Mixture Correlation table:" << endl;
563 Power_Mixture_Correlation->Print();
568 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
569 if (from == 0) cout << "Instantiated: FGPiston" << endl;
570 if (from == 1) cout << "Destroyed: FGPiston" << endl;
572 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
574 if (debug_lvl & 8 ) { // Runtime state variables
576 if (debug_lvl & 16) { // Sanity checking
578 if (debug_lvl & 64) {
579 if (from == 0) { // Constructor
580 cout << IdSrc << endl;
581 cout << IdHdr << endl;
587 FGPiston::CalcFuelNeed(void)
589 // FIXME: is this right?
590 return FuelFlow_gph * State->Getdt() * Propulsion->GetRate();