LCOV - code coverage report
Current view: top level - models/propulsion - FGPiston.cpp (source / functions) Hit Total Coverage
Test: JSBSim-Coverage-Statistics Lines: 1 468 0.2 %
Date: 2010-08-24 Functions: 3 25 12.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 3 348 0.9 %

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

Generated by: LCOV version 1.9