]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/models/propulsion/FGPiston.cpp
Synchronized with JSBSim/CVS
[flightgear.git] / src / FDM / JSBSim / models / propulsion / FGPiston.cpp
1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2
3  Module:       FGPiston.cpp
4  Author:       Jon S. Berndt, JSBSim framework
5                Dave Luff, Piston engine model
6                Ronald Jensen, Piston engine model
7  Date started: 09/12/2000
8  Purpose:      This module models a Piston engine
9
10  ------------- Copyright (C) 2000  Jon S. Berndt (jon@jsbsim.org) --------------
11
12  This program is free software; you can redistribute it and/or modify it under
13  the terms of the GNU Lesser General Public License as published by the Free Software
14  Foundation; either version 2 of the License, or (at your option) any later
15  version.
16
17  This program is distributed in the hope that it will be useful, but WITHOUT
18  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19  FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
20  details.
21
22  You should have received a copy of the GNU Lesser General Public License along with
23  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
24  Place - Suite 330, Boston, MA  02111-1307, USA.
25
26  Further information about the GNU Lesser General Public License can also be found on
27  the world wide web at http://www.gnu.org.
28
29 FUNCTIONAL DESCRIPTION
30 --------------------------------------------------------------------------------
31
32 This class descends from the FGEngine class and models a Piston engine based on
33 parameters given in the engine config file for this class
34
35 HISTORY
36 --------------------------------------------------------------------------------
37 09/12/2000  JSB  Created
38
39 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
40 INCLUDES
41 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
42
43 #include <iostream>
44 #include <sstream>
45
46 #include "FGPiston.h"
47 #include "FGPropeller.h"
48 #include "input_output/FGXMLElement.h"
49
50 using namespace std;
51
52 namespace JSBSim {
53
54 static const char *IdSrc = "$Id: FGPiston.cpp,v 1.72 2013/11/24 11:40:57 bcoconni Exp $";
55 static const char *IdHdr = ID_PISTON;
56
57 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
58 CLASS IMPLEMENTATION
59 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
60
61 FGPiston::FGPiston(FGFDMExec* exec, Element* el, int engine_number, struct Inputs& input)
62   : FGEngine(exec, el, engine_number, input),
63   R_air(287.3),                  // Gas constant for air J/Kg/K
64   rho_fuel(800),                 // estimate
65   calorific_value_fuel(47.3e6),  // J/Kg
66   Cp_air(1005),                  // Specific heat (constant pressure) J/Kg/K
67   Cp_fuel(1700),
68   standard_pressure(101320.73)
69 {
70   Element *table_element;
71   string token;
72   string name="";
73
74   // Defaults and initializations
75
76   Type = etPiston;
77
78   // These items are read from the configuration file
79   // Defaults are from a Lycoming O-360, more or less
80
81   Cycles = 4;
82   IdleRPM = 600;
83   MaxRPM = 2800;
84   Displacement = 360;
85   SparkFailDrop = 1.0;
86   MaxHP = 200;
87   MinManifoldPressure_inHg = 6.5;
88   MaxManifoldPressure_inHg = 28.5;
89   ManifoldPressureLag=1.0;
90   ISFC = -1;
91   volumetric_efficiency = 0.85;
92   Bore = 5.125;
93   Stroke = 4.375;
94   Cylinders = 4;
95   CylinderHeadMass = 2; //kg
96   CompressionRatio = 8.5;
97   Z_airbox = -999;
98   Ram_Air_Factor = 1;
99   PeakMeanPistonSpeed_fps = 100;
100   FMEPDynamic= 18400;
101   FMEPStatic = 46500;
102   Cooling_Factor = 0.5144444;
103   StaticFriction_HP = 1.5;
104   StarterGain = 1.;
105   StarterTorque = -1.;
106   StarterRPM = -1.;
107
108   // These are internal program variables
109
110   Lookup_Combustion_Efficiency = 0;
111   Mixture_Efficiency_Correlation = 0;
112   crank_counter = 0;
113   Magnetos = 0;
114   minMAP = 21950;
115   maxMAP = 96250;
116
117   ResetToIC();
118
119   // Supercharging
120   BoostSpeeds = 0;  // Default to no supercharging
121   BoostSpeed = 0;
122   Boosted = false;
123   BoostOverride = 0;
124   BoostManual = 0;
125   bBoostOverride = false;
126   bTakeoffBoost = false;
127   TakeoffBoost = 0.0;   // Default to no extra takeoff-boost
128   BoostLossFactor = 0.0;   // Default to free boost
129   
130   int i;
131   for (i=0; i<FG_MAX_BOOST_SPEEDS; i++) {
132     RatedBoost[i] = 0.0;
133     RatedPower[i] = 0.0;
134     RatedAltitude[i] = 0.0;
135     BoostMul[i] = 1.0;
136     RatedMAP[i] = 100000;
137     RatedRPM[i] = 2500;
138     TakeoffMAP[i] = 100000;
139   }
140   for (i=0; i<FG_MAX_BOOST_SPEEDS-1; i++) {
141     BoostSwitchAltitude[i] = 0.0;
142     BoostSwitchPressure[i] = 0.0;
143   }
144
145   // Read inputs from engine data file where present.
146
147   if (el->FindElement("minmp")) 
148     MinManifoldPressure_inHg = el->FindElementValueAsNumberConvertTo("minmp","INHG");
149   if (el->FindElement("maxmp"))
150     MaxManifoldPressure_inHg = el->FindElementValueAsNumberConvertTo("maxmp","INHG");
151   if (el->FindElement("man-press-lag"))
152     ManifoldPressureLag = el->FindElementValueAsNumber("man-press-lag");
153   if (el->FindElement("displacement"))
154     Displacement = el->FindElementValueAsNumberConvertTo("displacement","IN3");
155   if (el->FindElement("maxhp"))
156     MaxHP = el->FindElementValueAsNumberConvertTo("maxhp","HP");
157   if (el->FindElement("static-friction"))
158     StaticFriction_HP = el->FindElementValueAsNumberConvertTo("static-friction","HP");
159   if (el->FindElement("sparkfaildrop"))
160     SparkFailDrop = Constrain(0, 1 - el->FindElementValueAsNumber("sparkfaildrop"), 1);
161   if (el->FindElement("cycles"))
162     Cycles = el->FindElementValueAsNumber("cycles");
163   if (el->FindElement("idlerpm"))
164     IdleRPM = el->FindElementValueAsNumber("idlerpm");
165   if (el->FindElement("maxrpm"))
166     MaxRPM = el->FindElementValueAsNumber("maxrpm");
167   if (el->FindElement("maxthrottle"))
168     MaxThrottle = el->FindElementValueAsNumber("maxthrottle");
169   if (el->FindElement("minthrottle"))
170     MinThrottle = el->FindElementValueAsNumber("minthrottle");
171   if (el->FindElement("bsfc"))
172     ISFC = el->FindElementValueAsNumberConvertTo("bsfc", "LBS/HP*HR");
173   if (el->FindElement("volumetric-efficiency"))
174     volumetric_efficiency = el->FindElementValueAsNumber("volumetric-efficiency");
175   if (el->FindElement("compression-ratio"))
176     CompressionRatio = el->FindElementValueAsNumber("compression-ratio");
177   if (el->FindElement("bore"))
178     Bore = el->FindElementValueAsNumberConvertTo("bore","IN");
179   if (el->FindElement("stroke"))
180     Stroke = el->FindElementValueAsNumberConvertTo("stroke","IN");
181   if (el->FindElement("cylinders"))
182     Cylinders = el->FindElementValueAsNumber("cylinders");
183   if (el->FindElement("cylinder-head-mass"))
184     CylinderHeadMass = el->FindElementValueAsNumberConvertTo("cylinder-head-mass","KG");
185   if (el->FindElement("air-intake-impedance-factor"))
186     Z_airbox = el->FindElementValueAsNumber("air-intake-impedance-factor");
187   if (el->FindElement("ram-air-factor"))
188     Ram_Air_Factor  = el->FindElementValueAsNumber("ram-air-factor");
189   if (el->FindElement("cooling-factor"))
190     Cooling_Factor  = el->FindElementValueAsNumber("cooling-factor");
191   if (el->FindElement("starter-rpm"))
192     StarterRPM  = el->FindElementValueAsNumber("starter-rpm");
193   if (el->FindElement("starter-torque"))
194     StarterTorque  = el->FindElementValueAsNumber("starter-torque");
195   if (el->FindElement("dynamic-fmep"))
196     FMEPDynamic= el->FindElementValueAsNumberConvertTo("dynamic-fmep","PA");
197   if (el->FindElement("static-fmep"))
198     FMEPStatic = el->FindElementValueAsNumberConvertTo("static-fmep","PA");
199   if (el->FindElement("peak-piston-speed"))
200     PeakMeanPistonSpeed_fps  = el->FindElementValueAsNumber("peak-piston-speed");
201   if (el->FindElement("numboostspeeds")) { // Turbo- and super-charging parameters
202     BoostSpeeds = (int)el->FindElementValueAsNumber("numboostspeeds");
203     if (el->FindElement("boostoverride"))
204       BoostOverride = (int)el->FindElementValueAsNumber("boostoverride");
205     if (el->FindElement("boostmanual"))
206       BoostManual = (int)el->FindElementValueAsNumber("boostmanual");
207     if (el->FindElement("takeoffboost"))
208       TakeoffBoost = el->FindElementValueAsNumberConvertTo("takeoffboost", "PSI");
209     if (el->FindElement("boost-loss-factor"))
210       BoostLossFactor = el->FindElementValueAsNumber("boost-loss-factor");
211     if (el->FindElement("ratedboost1"))
212       RatedBoost[0] = el->FindElementValueAsNumberConvertTo("ratedboost1", "PSI");
213     if (el->FindElement("ratedboost2"))
214       RatedBoost[1] = el->FindElementValueAsNumberConvertTo("ratedboost2", "PSI");
215     if (el->FindElement("ratedboost3"))
216       RatedBoost[2] = el->FindElementValueAsNumberConvertTo("ratedboost3", "PSI");
217     if (el->FindElement("ratedpower1"))
218       RatedPower[0] = el->FindElementValueAsNumberConvertTo("ratedpower1", "HP");
219     if (el->FindElement("ratedpower2"))
220       RatedPower[1] = el->FindElementValueAsNumberConvertTo("ratedpower2", "HP");
221     if (el->FindElement("ratedpower3"))
222       RatedPower[2] = el->FindElementValueAsNumberConvertTo("ratedpower3", "HP");
223     if (el->FindElement("ratedrpm1"))
224       RatedRPM[0] = el->FindElementValueAsNumber("ratedrpm1");
225     if (el->FindElement("ratedrpm2"))
226       RatedRPM[1] = el->FindElementValueAsNumber("ratedrpm2");
227     if (el->FindElement("ratedrpm3"))
228       RatedRPM[2] = el->FindElementValueAsNumber("ratedrpm3");
229     if (el->FindElement("ratedaltitude1"))
230       RatedAltitude[0] = el->FindElementValueAsNumberConvertTo("ratedaltitude1", "FT");
231     if (el->FindElement("ratedaltitude2"))
232       RatedAltitude[1] = el->FindElementValueAsNumberConvertTo("ratedaltitude2", "FT");
233     if (el->FindElement("ratedaltitude3"))
234       RatedAltitude[2] = el->FindElementValueAsNumberConvertTo("ratedaltitude3", "FT");
235   }
236
237   while((table_element = el->FindNextElement("table")) != 0) {
238     name = table_element->GetAttributeValue("name");
239     try {
240       if (name == "COMBUSTION") {
241         Lookup_Combustion_Efficiency = new FGTable(PropertyManager, table_element);
242       } else if (name == "MIXTURE") {
243         Mixture_Efficiency_Correlation = new FGTable(PropertyManager, table_element);
244       } else {
245         cerr << "Unknown table type: " << name << " in piston engine definition." << endl;
246       }
247     } catch (std::string str) {
248       throw("Error loading piston engine table:" + name + ". " + str);
249     }
250   }
251
252
253   volumetric_efficiency_reduced = volumetric_efficiency;
254
255   if(StarterRPM < 0.) StarterRPM = 2*IdleRPM;
256   if(StarterTorque < 0)
257     StarterTorque = (MaxHP)*0.4; //just a wag.
258
259   displacement_SI = Displacement * in3tom3;
260   RatedMeanPistonSpeed_fps =  ( MaxRPM * Stroke) / (360); // AKA 2 * (RPM/60) * ( Stroke / 12) or 2NS
261
262   // Create IFSC to match the engine if not provided
263   if (ISFC < 0) {
264       double pmep = 29.92 - MaxManifoldPressure_inHg;
265       pmep *= inhgtopa  * volumetric_efficiency;
266       double fmep = (FMEPDynamic * RatedMeanPistonSpeed_fps * fttom + FMEPStatic);
267       double hp_loss = ((pmep + fmep) * displacement_SI * MaxRPM)/(Cycles*22371);
268       ISFC = ( 1.1*Displacement * MaxRPM * volumetric_efficiency *(MaxManifoldPressure_inHg / 29.92) ) / (9411 * (MaxHP+hp_loss-StaticFriction_HP));
269 // cout <<"FMEP: "<< fmep <<" PMEP: "<< pmep << " hp_loss: " <<hp_loss <<endl;
270   }
271   if ( MaxManifoldPressure_inHg > 29.9 ) {   // Don't allow boosting with a bogus number
272       MaxManifoldPressure_inHg = 29.9;
273   }
274   minMAP = MinManifoldPressure_inHg * inhgtopa;  // inHg to Pa
275   maxMAP = MaxManifoldPressure_inHg * inhgtopa;
276
277 // For throttle
278 /*
279  * Pm = ( Ze / ( Ze + Zi + Zt ) ) * Pa
280  * Where:
281  * Pm = Manifold Pressure
282  * Pa = Ambient Pressre
283  * Ze = engine impedance, Ze is effectively 1 / Mean Piston Speed
284  * Zi = airbox impedance
285  * Zt = throttle impedance
286  *
287  * For the calculation below throttle is fully open or Zt = 0
288  *
289  *
290  *
291  */
292   if(Z_airbox < 0.0){
293     double Ze=PeakMeanPistonSpeed_fps/RatedMeanPistonSpeed_fps; // engine impedence
294     Z_airbox = (standard_pressure *Ze / maxMAP) - Ze; // impedence of airbox
295   }
296   // Constant for Throttle impedence
297   Z_throttle=(PeakMeanPistonSpeed_fps/((IdleRPM * Stroke) / 360))*(standard_pressure/minMAP - 1) - Z_airbox; 
298   //  Z_throttle=(MaxRPM/IdleRPM )*(standard_pressure/minMAP+2); // Constant for Throttle impedence
299
300 // Default tables if not provided in the configuration file
301   if(Lookup_Combustion_Efficiency == 0) {
302     // First column is thi, second is neta (combustion efficiency)
303     Lookup_Combustion_Efficiency = new FGTable(12);
304     *Lookup_Combustion_Efficiency << 0.00 << 0.980;
305     *Lookup_Combustion_Efficiency << 0.90 << 0.980;
306     *Lookup_Combustion_Efficiency << 1.00 << 0.970;
307     *Lookup_Combustion_Efficiency << 1.05 << 0.950;
308     *Lookup_Combustion_Efficiency << 1.10 << 0.900;
309     *Lookup_Combustion_Efficiency << 1.15 << 0.850;
310     *Lookup_Combustion_Efficiency << 1.20 << 0.790;
311     *Lookup_Combustion_Efficiency << 1.30 << 0.700;
312     *Lookup_Combustion_Efficiency << 1.40 << 0.630;
313     *Lookup_Combustion_Efficiency << 1.50 << 0.570;
314     *Lookup_Combustion_Efficiency << 1.60 << 0.525;
315     *Lookup_Combustion_Efficiency << 2.00 << 0.345;
316   }
317
318     // First column is Fuel/Air Ratio, second is neta (mixture efficiency)
319   if( Mixture_Efficiency_Correlation == 0) {
320     Mixture_Efficiency_Correlation = new FGTable(15);
321     *Mixture_Efficiency_Correlation << 0.05000 << 0.00000;
322     *Mixture_Efficiency_Correlation << 0.05137 << 0.00862;
323     *Mixture_Efficiency_Correlation << 0.05179 << 0.21552;
324     *Mixture_Efficiency_Correlation << 0.05430 << 0.48276;
325     *Mixture_Efficiency_Correlation << 0.05842 << 0.70690;
326     *Mixture_Efficiency_Correlation << 0.06312 << 0.83621;
327     *Mixture_Efficiency_Correlation << 0.06942 << 0.93103;
328     *Mixture_Efficiency_Correlation << 0.07786 << 1.00000;
329     *Mixture_Efficiency_Correlation << 0.08845 << 1.00000;
330     *Mixture_Efficiency_Correlation << 0.09270 << 0.98276;
331     *Mixture_Efficiency_Correlation << 0.10120 << 0.93103;
332     *Mixture_Efficiency_Correlation << 0.11455 << 0.72414;
333     *Mixture_Efficiency_Correlation << 0.12158 << 0.45690;
334     *Mixture_Efficiency_Correlation << 0.12435 << 0.23276;
335     *Mixture_Efficiency_Correlation << 0.12500 << 0.00000;
336   }
337
338   string property_name, base_property_name;
339   base_property_name = CreateIndexedPropertyName("propulsion/engine", EngineNumber);
340   property_name = base_property_name + "/power-hp";
341   PropertyManager->Tie(property_name, &HP);
342   property_name = base_property_name + "/friction-hp";
343   PropertyManager->Tie(property_name, &StaticFriction_HP);
344   property_name = base_property_name + "/bsfc-lbs_hphr";
345   PropertyManager->Tie(property_name, &ISFC);
346   property_name = base_property_name + "/starter-norm";
347   PropertyManager->Tie(property_name, &StarterGain);
348   property_name = base_property_name + "/volumetric-efficiency";
349   PropertyManager->Tie(property_name, &volumetric_efficiency);
350   property_name = base_property_name + "/map-pa";
351   PropertyManager->Tie(property_name, &MAP);
352   property_name = base_property_name + "/map-inhg";
353   PropertyManager->Tie(property_name, &ManifoldPressure_inHg);
354   property_name = base_property_name + "/air-intake-impedance-factor";
355   PropertyManager->Tie(property_name, &Z_airbox);
356   property_name = base_property_name + "/ram-air-factor";
357   PropertyManager->Tie(property_name, &Ram_Air_Factor);
358   property_name = base_property_name + "/cooling-factor";
359   PropertyManager->Tie(property_name, &Cooling_Factor);
360   property_name = base_property_name + "/boost-speed";
361   PropertyManager->Tie(property_name, &BoostSpeed);
362   property_name = base_property_name + "/cht-degF";
363   PropertyManager->Tie(property_name, this, &FGPiston::getCylinderHeadTemp_degF);
364   property_name = base_property_name + "/oil-temperature-degF";
365   PropertyManager->Tie(property_name, this, &FGPiston::getOilTemp_degF);
366   property_name = base_property_name + "/oil-pressure-psi";
367   PropertyManager->Tie(property_name, this, &FGPiston::getOilPressure_psi);
368   property_name = base_property_name + "/egt-degF";
369   PropertyManager->Tie(property_name, this, &FGPiston::getExhaustGasTemp_degF);
370   if(BoostLossFactor > 0.0) {
371     property_name = base_property_name + "/boostloss-factor";
372     PropertyManager->Tie(property_name, &BoostLossFactor);
373     property_name = base_property_name + "/boostloss-hp";
374     PropertyManager->Tie(property_name, &BoostLossHP);
375   }
376
377   // Set up and sanity-check the turbo/supercharging configuration based on the input values.
378   if (TakeoffBoost > RatedBoost[0]) bTakeoffBoost = true;
379   for (i=0; i<BoostSpeeds; ++i) {
380     bool bad = false;
381     if (RatedBoost[i] <= 0.0) bad = true;
382     if (RatedPower[i] <= 0.0) bad = true;
383     if (RatedAltitude[i] < 0.0) bad = true;  // 0.0 is deliberately allowed - this corresponds to unregulated supercharging.
384     if (i > 0 && RatedAltitude[i] < RatedAltitude[i - 1]) bad = true;
385     if (bad) {
386       // We can't recover from the above - don't use this supercharger speed.
387       BoostSpeeds--;
388       // TODO - put out a massive error message!
389       break;
390     }
391     // Now sanity-check stuff that is recoverable.
392     if (i < BoostSpeeds - 1) {
393       if (BoostSwitchAltitude[i] < RatedAltitude[i]) {
394         // TODO - put out an error message
395         // But we can also make a reasonable estimate, as below.
396         BoostSwitchAltitude[i] = RatedAltitude[i] + 1000;
397       }
398       BoostSwitchPressure[i] = GetStdPressure100K(BoostSwitchAltitude[i]) * psftopa;
399       //cout << "BoostSwitchAlt = " << BoostSwitchAltitude[i] << ", pressure = " << BoostSwitchPressure[i] << '\n';
400       // Assume there is some hysteresis on the supercharger gear switch, and guess the value for now
401       BoostSwitchHysteresis = 1000;
402     }
403     // Now work out the supercharger pressure multiplier of this speed from the rated boost and altitude.
404     RatedMAP[i] = standard_pressure + RatedBoost[i] * 6895;  // psi*6895 = Pa.
405     // Sometimes a separate BCV setting for takeoff or extra power is fitted.
406     if (TakeoffBoost > RatedBoost[0]) {
407       // Assume that the effect on the BCV is the same whichever speed is in use.
408       TakeoffMAP[i] = RatedMAP[i] + ((TakeoffBoost - RatedBoost[0]) * 6895);
409       bTakeoffBoost = true;
410     } else {
411       TakeoffMAP[i] = RatedMAP[i];
412       bTakeoffBoost = false;
413     }
414     BoostMul[i] = RatedMAP[i] / (GetStdPressure100K(RatedAltitude[i]) * psftopa);
415
416   }
417
418   if (BoostSpeeds > 0) {
419     Boosted = true;
420     BoostSpeed = 0;
421   }
422   bBoostOverride = (BoostOverride == 1 ? true : false);
423   bBoostManual   = (BoostManual   == 1 ? true : false);
424   Debug(0); // Call Debug() routine from constructor if needed
425 }
426
427 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
428
429 FGPiston::~FGPiston()
430 {
431   delete Lookup_Combustion_Efficiency;
432   delete Mixture_Efficiency_Correlation;
433   Debug(1); // Call Debug() routine from constructor if needed
434 }
435
436 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
437
438 void FGPiston::ResetToIC(void)
439 {
440   FGEngine::ResetToIC();
441
442   ManifoldPressure_inHg = in.Pressure * psftoinhg; // psf to in Hg
443   MAP = in.Pressure * psftopa;
444   TMAP = MAP;
445   double airTemperature_degK = RankineToKelvin(in.Temperature);
446   OilTemp_degK = airTemperature_degK;
447   CylinderHeadTemp_degK = airTemperature_degK;
448   ExhaustGasTemp_degK = airTemperature_degK;
449   EGT_degC = ExhaustGasTemp_degK - 273;
450   Thruster->SetRPM(0.0);
451   RPM = 0.0;
452   OilPressure_psi = 0.0;
453   BoostLossHP = 0.;
454 }
455
456 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
457
458 void FGPiston::Calculate(void)
459 {
460   // Input values.
461
462   p_amb = in.Pressure * psftopa;
463   double p = in.TotalPressure * psftopa;
464   p_ram = (p - p_amb) * Ram_Air_Factor + p_amb;
465   T_amb = RankineToKelvin(in.Temperature);
466
467   RunPreFunctions();
468
469   TotalDeltaT = ( in.TotalDeltaT < 1e-9 ) ? 1.0 : in.TotalDeltaT;
470
471 /* The thruster controls the engine RPM because it encapsulates the gear ratio and other transmission variables */
472   RPM = Thruster->GetEngineRPM();
473
474   MeanPistonSpeed_fps =  ( RPM * Stroke) / (360); // AKA 2 * (RPM/60) * ( Stroke / 12) or 2NS
475
476   IAS = in.Vc;
477
478   doEngineStartup();
479   if (Boosted) doBoostControl();
480   doMAP();
481   doAirFlow();
482   doFuelFlow();
483
484   //Now that the fuel flow is done check if the mixture is too lean to run the engine
485   //Assume lean limit at 22 AFR for now - thats a thi of 0.668
486   //This might be a bit generous, but since there's currently no audiable warning of impending
487   //cutout in the form of misfiring and/or rough running its probably reasonable for now.
488
489   //  if (equivalence_ratio < 0.668)
490   //    Running = false;
491
492   doEnginePower();
493   if (IndicatedHorsePower < 0.1250) Running = false;
494
495   doEGT();
496   doCHT();
497   doOilTemperature();
498   doOilPressure();
499
500   if (Thruster->GetType() == FGThruster::ttPropeller) {
501     ((FGPropeller*)Thruster)->SetAdvance(in.PropAdvance[EngineNumber]);
502     ((FGPropeller*)Thruster)->SetFeather(in.PropFeather[EngineNumber]);
503   }
504
505   LoadThrusterInputs();
506   Thruster->Calculate(HP * hptoftlbssec);
507
508   RunPostFunctions();
509 }
510
511 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
512
513 double FGPiston::CalcFuelNeed(void)
514 {
515   FuelExpended = FuelFlowRate * in.TotalDeltaT;
516   if (!Starved) FuelUsedLbs += FuelExpended; 
517   return FuelExpended;
518 }
519
520 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
521
522 int FGPiston::InitRunning(void)
523 {
524   Magnetos=3;
525   in.MixtureCmd[EngineNumber] = in.PressureRatio*1.3;
526   in.MixturePos[EngineNumber] = in.PressureRatio*1.3;
527   Thruster->SetRPM( 2.0*IdleRPM/Thruster->GetGearRatio() );
528   Running = true;
529   return 1;
530 }
531
532 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
533 /**
534  * Start or stop the engine.
535  */
536
537 void FGPiston::doEngineStartup(void)
538 {
539   // Check parameters that may alter the operating state of the engine.
540   // (spark, fuel, starter motor etc)
541   bool spark;
542   bool fuel;
543   // Check for spark
544   Magneto_Left = false;
545   Magneto_Right = false;
546   // Magneto positions:
547   // 0 -> off
548   // 1 -> left only
549   // 2 -> right only
550   // 3 -> both
551   if (Magnetos != 0) {
552     spark = true;
553   } else {
554     spark = false;
555   }  // neglects battery voltage, master on switch, etc for now.
556
557   if ((Magnetos == 1) || (Magnetos > 2)) Magneto_Left = true;
558   if (Magnetos > 1)  Magneto_Right = true;
559
560 // We will 'run' with any fuel flow. If there is not enough fuel to make power it will show in doEnginePower
561   fuel = FuelFlowRate > 0.0 ? 1 : 0;
562
563   // Check if we are turning the starter motor
564   if (Cranking != Starter) {
565     // This check saves .../cranking from getting updated every loop - they
566     // only update when changed.
567     Cranking = Starter;
568   }
569
570
571   // Cut the engine *power* - Note: the engine will continue to
572   // spin depending on prop Ixx and freestream velocity
573
574   if ( Running ) {
575     if (!spark || !fuel)    Running = false;
576     if (RPM < IdleRPM*0.8 ) Running = false;
577   } else { // !Running  
578     if ( spark && fuel) {     // start the engine if revs high enough
579       if (RPM > IdleRPM*0.8)  // This allows us to in-air start
580         Running = true;       // when windmilling
581     }
582   }
583
584 }
585
586 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
587
588 /**
589  * Calculate the Current Boost Speed
590  *
591  * This function calculates the current turbo/supercharger boost speed
592  * based on altitude and the (automatic) boost-speed control valve configuration.
593  *
594  * Inputs: p_amb, BoostSwitchPressure, BoostSwitchHysteresis
595  *
596  * Outputs: BoostSpeed
597  */
598
599 void FGPiston::doBoostControl(void)
600 {
601   if(bBoostManual) {
602     if(BoostSpeed > BoostSpeeds-1) BoostSpeed = BoostSpeeds-1;
603     if(BoostSpeed < 0) BoostSpeed = 0;
604   } else {
605     if(BoostSpeed < BoostSpeeds - 1) {
606       // Check if we need to change to a higher boost speed
607       if(p_amb < BoostSwitchPressure[BoostSpeed] - BoostSwitchHysteresis) {
608         BoostSpeed++;
609       }
610     } if(BoostSpeed > 0) {
611       // Check if we need to change to a lower boost speed
612       if(p_amb > BoostSwitchPressure[BoostSpeed - 1] + BoostSwitchHysteresis) {
613         BoostSpeed--;
614       }
615     }
616   }
617 }
618
619 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
620
621 /**
622  * Calculate the manifold absolute pressure (MAP) in inches hg
623  *
624  * This function calculates manifold absolute pressure (MAP)
625  * from the throttle position, turbo/supercharger boost control
626  * system, engine speed and local ambient air density.
627  *
628  * Inputs: p_amb, Throttle,
629  *         MeanPistonSpeed_fps, dt
630  *
631  * Outputs: MAP, ManifoldPressure_inHg, TMAP, BoostLossHP
632  */
633
634 void FGPiston::doMAP(void)
635 {
636   double Zt = (1 - in.ThrottlePos[EngineNumber])*(1 - in.ThrottlePos[EngineNumber])*Z_throttle; // throttle impedence
637   double Ze= MeanPistonSpeed_fps > 0 ? PeakMeanPistonSpeed_fps/MeanPistonSpeed_fps : 999999; // engine impedence
638
639   double map_coefficient = Ze/(Ze+Z_airbox+Zt);
640
641   // Add a variable lag to manifold pressure changes
642   double dMAP=(TMAP - p_ram * map_coefficient);
643   if (ManifoldPressureLag > TotalDeltaT) dMAP *= TotalDeltaT/ManifoldPressureLag;
644
645   TMAP -=dMAP;
646
647   // Find the mean effective pressure required to achieve this manifold pressure
648   // Fixme: determine the HP consumed by the supercharger
649
650   PMEP = (TMAP - p_amb) * volumetric_efficiency; // Fixme: p_amb should be exhaust manifold pressure
651
652   if (Boosted) {
653     // If takeoff boost is fitted, we currently assume the following throttle map:
654     // (In throttle % - actual input is 0 -> 1)
655     // 99 / 100 - Takeoff boost
656     // In real life, most planes would be fitted with a mechanical 'gate' between
657     // the rated boost and takeoff boost positions.
658
659     bool bTakeoffPos = false;
660     if (bTakeoffBoost) {
661       if (in.ThrottlePos[EngineNumber] > 0.98) {
662         bTakeoffPos = true;
663       }
664     }
665     // Boost the manifold pressure.
666     double boost_factor = (( BoostMul[BoostSpeed] - 1 ) / RatedRPM[BoostSpeed] ) * RPM + 1;
667     MAP = TMAP * boost_factor;
668     // Now clip the manifold pressure to BCV or Wastegate setting.
669     if(!bBoostOverride) {
670       if (bTakeoffPos) {
671         if (MAP > TakeoffMAP[BoostSpeed]) MAP = TakeoffMAP[BoostSpeed];
672       } else {
673         if (MAP > RatedMAP[BoostSpeed]) MAP = RatedMAP[BoostSpeed];
674       }
675     }
676   } else {
677       MAP = TMAP;
678   }
679
680   if( BoostLossFactor > 0.0 )
681   {
682       double gamma = 1.414; // specific heat constants
683       double Nstage = 1; // Nstage is the number of boost stages.
684       BoostLossHP = ((Nstage * TMAP * v_dot_air * gamma) / (gamma - 1)) * (pow((MAP/TMAP),((gamma-1)/(Nstage * gamma))) - 1) * BoostLossFactor / 745.7 ; // 745.7 convert watt to hp
685   } else {
686       BoostLossHP = 0;
687   }
688   
689   // And set the value in American units as well
690   ManifoldPressure_inHg = MAP / inhgtopa;
691 }
692
693 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
694 /**
695  * Calculate the air flow through the engine.
696  * Also calculates ambient air density
697  * (used in CHT calculation for air-cooled engines).
698  *
699  * Inputs: p_amb, R_air, T_amb, MAP, Displacement,
700  *   RPM, volumetric_efficiency,
701  *
702  * TODO: Model inlet manifold air temperature.
703  *
704  * Outputs: rho_air, m_dot_air, volumetric_efficiency_reduced
705  */
706
707 void FGPiston::doAirFlow(void)
708 {
709   double gamma = 1.3; // specific heat constants
710 // loss of volumentric efficiency due to difference between MAP and exhaust pressure
711 // Eq 6-10 from The Internal Combustion Engine - Charles Taylor Vol 1
712   double mratio = MAP < 1. ? CompressionRatio : p_amb/MAP;
713   if (mratio > CompressionRatio) mratio = CompressionRatio;
714   double ve =((gamma-1)/gamma) +( CompressionRatio -(mratio))/(gamma*( CompressionRatio - 1));
715
716   rho_air = p_amb / (R_air * T_amb);
717   double swept_volume = (displacement_SI * (RPM/60)) / 2;
718   volumetric_efficiency_reduced = volumetric_efficiency *ve;
719   v_dot_air = swept_volume * volumetric_efficiency_reduced;
720
721   double rho_air_manifold = MAP / (R_air * T_amb);
722   m_dot_air = v_dot_air * rho_air_manifold;
723
724 }
725
726 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
727 /**
728  * Calculate the fuel flow into the engine.
729  *
730  * Inputs: Mixture, thi_sea_level, p_amb, m_dot_air
731  *
732  * Outputs: equivalence_ratio, m_dot_fuel
733  */
734
735 void FGPiston::doFuelFlow(void)
736 {
737   double thi_sea_level = 1.3 * in.MixturePos[EngineNumber]; // Allows an AFR of infinity:1 to 11.3075:1
738   equivalence_ratio = thi_sea_level * 101325.0 / p_amb;
739 //  double AFR = 10+(12*(1-in.Mixture[EngineNumber]));// mixture 10:1 to 22:1
740 //  m_dot_fuel = m_dot_air / AFR;
741   m_dot_fuel = (m_dot_air * equivalence_ratio) / 14.7;
742   FuelFlowRate =  m_dot_fuel * 2.2046;  // kg to lb
743   if(Starved) // There is no fuel, so zero out the flows we've calculated so far
744   {
745     equivalence_ratio = 0.0;
746     FuelFlowRate = 0.0;
747     m_dot_fuel = 0.0;
748   }
749   FuelFlow_pph = FuelFlowRate  * 3600;  // seconds to hours
750   FuelFlow_gph = FuelFlow_pph / 6.0;    // Assumes 6 lbs / gallon
751 }
752
753 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
754 /**
755  * Calculate the power produced by the engine.
756  *
757  * Inputs: ManifoldPressure_inHg, p_amb, RPM, T_amb, ISFC,
758  *   Mixture_Efficiency_Correlation, Cycles, MaxHP, PMEP,
759  *   MeanPistonSpeed_fps
760  *
761  * Outputs: PctPower, HP, FMEP, IndicatedHorsePower
762  */
763
764 void FGPiston::doEnginePower(void)
765 {
766   IndicatedHorsePower = -StaticFriction_HP;
767   FMEP = 0;
768   if (Running) {
769     double ME, power;  // Convienience term for use in the calculations
770     ME = Mixture_Efficiency_Correlation->GetValue(m_dot_fuel/m_dot_air);
771
772 // Guestimate engine friction losses from Figure 4.4 of "Engines: An Introduction", John Lumley
773     FMEP = (-FMEPDynamic * MeanPistonSpeed_fps * fttom - FMEPStatic);
774
775     power = 1;
776
777     if ( Magnetos != 3 ) power *= SparkFailDrop;
778
779
780     IndicatedHorsePower = (FuelFlow_pph / ISFC )* ME * power - StaticFriction_HP; //FIXME static friction should depend on oil temp and configuration;
781
782   } else {
783     // Power output when the engine is not running
784     double torque, k_torque, rpm;  // Convienience term for use in the calculations
785     
786     rpm = RPM < 1.0 ? 1.0 : RPM;
787     if (Cranking) {
788       if(RPM<StarterRPM) k_torque = 1.0-RPM/(StarterRPM);
789       else k_torque = 0;
790       torque = StarterTorque*k_torque*StarterGain;
791       IndicatedHorsePower = torque * rpm / 5252;
792      } 
793   }
794
795   // Constant is (1/2) * 60 * 745.7
796   // (1/2) convert cycles, 60 minutes to seconds, 745.7 watts to hp.
797   double pumping_hp = ((PMEP + FMEP) * displacement_SI * RPM)/(Cycles*22371);
798
799 HP = IndicatedHorsePower + pumping_hp - BoostLossHP;
800 //  cout << "pumping_hp " <<pumping_hp << FMEP << PMEP <<endl;
801   PctPower = HP / MaxHP ;
802 //  cout << "Power = " << HP << "  RPM = " << RPM << "  Running = " << Running << "  Cranking = " << Cranking << endl;
803 }
804
805 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
806 /**
807  * Calculate the exhaust gas temperature.
808  *
809  * Inputs: equivalence_ratio, m_dot_fuel, calorific_value_fuel,
810  *   Cp_air, m_dot_air, Cp_fuel, m_dot_fuel, T_amb, PctPower
811  *
812  * Outputs: combustion_efficiency, ExhaustGasTemp_degK
813  */
814
815 void FGPiston::doEGT(void)
816 {
817   double delta_T_exhaust;
818   double enthalpy_exhaust;
819   double heat_capacity_exhaust;
820   double dEGTdt;
821
822   if ((Running) && (m_dot_air > 0.0)) {  // do the energy balance
823     combustion_efficiency = Lookup_Combustion_Efficiency->GetValue(equivalence_ratio);
824     enthalpy_exhaust = m_dot_fuel * calorific_value_fuel *
825                               combustion_efficiency * 0.30;
826     heat_capacity_exhaust = (Cp_air * m_dot_air) + (Cp_fuel * m_dot_fuel);
827     delta_T_exhaust = enthalpy_exhaust / heat_capacity_exhaust;
828     ExhaustGasTemp_degK = T_amb + delta_T_exhaust;
829   } else {  // Drop towards ambient - guess an appropriate time constant for now
830     combustion_efficiency = 0;
831     dEGTdt = (RankineToKelvin(in.Temperature) - ExhaustGasTemp_degK) / 100.0;
832     delta_T_exhaust = dEGTdt * TotalDeltaT;
833
834     ExhaustGasTemp_degK += delta_T_exhaust;
835   }
836 }
837
838 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
839 /**
840  * Calculate the cylinder head temperature.
841  *
842  * Inputs: T_amb, IAS, rho_air, m_dot_fuel, calorific_value_fuel,
843  *   combustion_efficiency, RPM, MaxRPM, Displacement, Cylinders
844  *
845  * Outputs: CylinderHeadTemp_degK
846  */
847
848 void FGPiston::doCHT(void)
849 {
850   double h1 = -95.0;
851   double h2 = -3.95;
852   double h3 = -140.0; // -0.05 * 2800 (default maxrpm)
853
854   double arbitary_area = Displacement/360.0;
855   double CpCylinderHead = 800.0;
856   double MassCylinderHead = CylinderHeadMass * Cylinders;
857
858   double temperature_difference = CylinderHeadTemp_degK - T_amb;
859   double v_apparent = IAS * Cooling_Factor;
860   double v_dot_cooling_air = arbitary_area * v_apparent;
861   double m_dot_cooling_air = v_dot_cooling_air * rho_air;
862   double dqdt_from_combustion =
863     m_dot_fuel * calorific_value_fuel * combustion_efficiency * 0.33;
864   double dqdt_forced = (h2 * m_dot_cooling_air * temperature_difference) +
865     (h3 * RPM * temperature_difference / MaxRPM);
866   double dqdt_free = h1 * temperature_difference * arbitary_area;
867   double dqdt_cylinder_head = dqdt_from_combustion + dqdt_forced + dqdt_free;
868
869   double HeatCapacityCylinderHead = CpCylinderHead * MassCylinderHead;
870
871   CylinderHeadTemp_degK +=
872     (dqdt_cylinder_head / HeatCapacityCylinderHead) * TotalDeltaT;
873
874 }
875
876 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
877 /**
878  * Calculate the oil temperature.
879  *
880  * Inputs: CylinderHeadTemp_degK, T_amb, OilPressure_psi.
881  *
882  * Outputs: OilTemp_degK
883  */
884
885 void FGPiston::doOilTemperature(void)
886 {
887   double target_oil_temp;        // Steady state oil temp at the current engine conditions
888   double time_constant;          // The time constant for the differential equation
889   double efficiency = 0.667;     // The aproximate oil cooling system efficiency // FIXME: may vary by engine
890
891 //  Target oil temp is interpolated between ambient temperature and Cylinder Head Tempurature
892 //  target_oil_temp = ( T_amb * efficiency ) + (CylinderHeadTemp_degK *(1-efficiency)) ;
893   target_oil_temp = CylinderHeadTemp_degK + efficiency * (T_amb - CylinderHeadTemp_degK) ;
894
895   if (OilPressure_psi > 5.0 ) {
896     time_constant = 5000 / OilPressure_psi; // Guess at a time constant for circulated oil.
897                                             // The higher the pressure the faster it reaches
898                                             // target temperature.  Oil pressure should be about
899                                             // 60 PSI yielding a TC of about 80.
900   } else {
901     time_constant = 1000;  // Time constant for engine-off; reflects the fact
902                            // that oil is no longer getting circulated
903   }
904
905   double dOilTempdt = (target_oil_temp - OilTemp_degK) / time_constant;
906
907   OilTemp_degK += (dOilTempdt * TotalDeltaT);
908 }
909
910 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
911 /**
912  * Calculate the oil pressure.
913  *
914  * Inputs: RPM, MaxRPM, OilTemp_degK
915  *
916  * Outputs: OilPressure_psi
917  */
918
919 void FGPiston::doOilPressure(void)
920 {
921   double Oil_Press_Relief_Valve = 60; // FIXME: may vary by engine
922   double Oil_Press_RPM_Max = MaxRPM * 0.75;    // 75% of max rpm FIXME: may vary by engine
923   double Design_Oil_Temp = 358;          // degK; FIXME: may vary by engine
924   double Oil_Viscosity_Index = 0.25;
925
926   OilPressure_psi = (Oil_Press_Relief_Valve / Oil_Press_RPM_Max) * RPM;
927
928   if (OilPressure_psi >= Oil_Press_Relief_Valve) {
929     OilPressure_psi = Oil_Press_Relief_Valve;
930   }
931
932   OilPressure_psi += (Design_Oil_Temp - OilTemp_degK) * Oil_Viscosity_Index * OilPressure_psi / Oil_Press_Relief_Valve;
933 }
934
935 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
936 //
937 // This is a local copy of the same function in FGStandardAtmosphere.
938
939 double FGPiston::GetStdPressure100K(double altitude) const
940 {
941   // Limit this equation to input altitudes of 100000 ft.
942   if (altitude > 100000.0) altitude = 100000.0;
943
944   double alt[5];
945   const double coef[5] = {  2116.217,
946                           -7.648932746E-2,
947                            1.0925498604E-6,
948                           -7.1135726027E-12,
949                            1.7470331356E-17 };
950
951   alt[0] = 1;
952   for (int pwr=1; pwr<=4; pwr++) alt[pwr] = alt[pwr-1]*altitude;
953
954   double press = 0.0;
955   for (int ctr=0; ctr<=4; ctr++) press += coef[ctr]*alt[ctr];
956   return press;
957 }
958
959 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
960
961 string FGPiston::GetEngineLabels(const string& delimiter)
962 {
963   std::ostringstream buf;
964
965   buf << Name << " Power Available (engine " << EngineNumber << " in ft-lbs/sec)" << delimiter
966       << Name << " HP (engine " << EngineNumber << ")" << delimiter
967       << Name << " equivalent ratio (engine " << EngineNumber << ")" << delimiter
968       << Name << " MAP (engine " << EngineNumber << " in inHg)" << delimiter
969       << Thruster->GetThrusterLabels(EngineNumber, delimiter);
970
971   return buf.str();
972 }
973
974 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
975
976 string FGPiston::GetEngineValues(const string& delimiter)
977 {
978   std::ostringstream buf;
979
980   buf << (HP * hptoftlbssec) << delimiter << HP << delimiter
981       << equivalence_ratio << delimiter << ManifoldPressure_inHg << delimiter
982       << Thruster->GetThrusterValues(EngineNumber, delimiter);
983
984   return buf.str();
985 }
986
987 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
988 //
989 //    The bitmasked value choices are as follows:
990 //    unset: In this case (the default) JSBSim would only print
991 //       out the normally expected messages, essentially echoing
992 //       the config files as they are read. If the environment
993 //       variable is not set, debug_lvl is set to 1 internally
994 //    0: This requests JSBSim not to output any messages
995 //       whatsoever.
996 //    1: This value explicity requests the normal JSBSim
997 //       startup messages
998 //    2: This value asks for a message to be printed out when
999 //       a class is instantiated
1000 //    4: When this value is set, a message is displayed when a
1001 //       FGModel object executes its Run() method
1002 //    8: When this value is set, various runtime state variables
1003 //       are printed out periodically
1004 //    16: When set various parameters are sanity checked and
1005 //       a message is printed out when they go out of bounds
1006
1007 void FGPiston::Debug(int from)
1008 {
1009   if (debug_lvl <= 0) return;
1010
1011   if (debug_lvl & 1) { // Standard console startup message output
1012     if (from == 0) { // Constructor
1013
1014       cout << "\n    Engine Name: "         << Name << endl;
1015       cout << "      MinManifoldPressure: " << MinManifoldPressure_inHg << endl;
1016       cout << "      MaxManifoldPressure: " << MaxManifoldPressure_inHg << endl;
1017       cout << "      MinMaP (Pa):         " << minMAP << endl;
1018       cout << "      MaxMaP (Pa):         " << maxMAP << endl;
1019       cout << "      Displacement: "        << Displacement             << endl;
1020       cout << "      Bore: "                << Bore                     << endl;
1021       cout << "      Stroke: "              << Stroke                   << endl;
1022       cout << "      Cylinders: "           << Cylinders                << endl;
1023       cout << "      Cylinders Head Mass: " << CylinderHeadMass         << endl;
1024       cout << "      Compression Ratio: "   << CompressionRatio         << endl;
1025       cout << "      MaxHP: "               << MaxHP                    << endl;
1026       cout << "      Cycles: "              << Cycles                   << endl;
1027       cout << "      IdleRPM: "             << IdleRPM                  << endl;
1028       cout << "      MaxRPM: "              << MaxRPM                   << endl;
1029       cout << "      Throttle Constant: "   << Z_throttle               << endl;
1030       cout << "      ISFC: "                << ISFC                     << endl;
1031       cout << "      Volumetric Efficiency: " << volumetric_efficiency    << endl;
1032       cout << "      PeakMeanPistonSpeed_fps: " << PeakMeanPistonSpeed_fps << endl;
1033       cout << "      Intake Impedance Factor: " << Z_airbox << endl;
1034       cout << "      Dynamic FMEP Factor: " << FMEPDynamic << endl;
1035       cout << "      Static FMEP Factor: " << FMEPStatic << endl;
1036
1037       cout << "      Starter Motor Torque: " << StarterTorque << endl;
1038       cout << "      Starter Motor RPM:    " << StarterRPM << endl;
1039
1040       cout << endl;
1041       cout << "      Combustion Efficiency table:" << endl;
1042       Lookup_Combustion_Efficiency->Print();
1043       cout << endl;
1044
1045       cout << endl;
1046       cout << "      Mixture Efficiency Correlation table:" << endl;
1047       Mixture_Efficiency_Correlation->Print();
1048       cout << endl;
1049
1050     }
1051   }
1052   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
1053     if (from == 0) cout << "Instantiated: FGPiston" << endl;
1054     if (from == 1) cout << "Destroyed:    FGPiston" << endl;
1055   }
1056   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
1057   }
1058   if (debug_lvl & 8 ) { // Runtime state variables
1059   }
1060   if (debug_lvl & 16) { // Sanity checking
1061   }
1062   if (debug_lvl & 64) {
1063     if (from == 0) { // Constructor
1064       cout << IdSrc << endl;
1065       cout << IdHdr << endl;
1066     }
1067   }
1068 }
1069 } // namespace JSBSim