]> git.mxchange.org Git - flightgear.git/blob - src/FDM/YASim/PistonEngine.cpp
latest updates from JSBSim
[flightgear.git] / src / FDM / YASim / PistonEngine.cpp
1 #include "Atmosphere.hpp"
2 #include "Math.hpp"
3 #include "PistonEngine.hpp"
4 namespace yasim {
5
6 const static float HP2W = 745.7f;
7 const static float CIN2CM = 1.6387064e-5f;
8 const static float RPM2RADPS = 0.1047198f;
9
10 PistonEngine::PistonEngine(float power, float speed)
11 {
12     _boost = 1;
13     _running = false;
14     _fuel = true;
15     _boostPressure = 0;
16     _hasSuper = false;
17     
18     _oilTemp = Atmosphere::getStdTemperature(0);
19     _oilTempTarget = _oilTemp;
20     _dOilTempdt = 0;
21
22     // Presume a BSFC (in lb/hour per HP) of 0.45.  In SI that becomes
23     // (2.2 lb/kg, 745.7 W/hp, 3600 sec/hour) 7.62e-08 kg/Ws.
24     _f0 = power * 7.62e-08f;
25
26     _power0 = power;
27     _omega0 = speed;
28
29     // We must be at sea level under standard conditions
30     _rho0 = Atmosphere::getStdDensity(0);
31
32     // Further presume that takeoff is (duh) full throttle and
33     // peak-power, that means that by our efficiency function, we are
34     // at 11/8 of "ideal" fuel flow.
35     float realFlow = _f0 * (11.0f/8.0f);
36     _mixCoeff = realFlow * 1.1f / _omega0;
37
38     _turbo = 1;
39     _minthrottle = 0.1;
40     _maxMP = 1e6; // No waste gate on non-turbo engines.
41     _wastegate = 1;
42     _charge = 1;
43     _chargeTarget = 1;
44     _turboLag = 2;
45
46     // Guess at reasonable values for these guys.  Displacements run
47     // at about 2 cubic inches per horsepower or so, at least for
48     // non-turbocharged engines.
49     _compression = 8;
50     _displacement = power * (2*CIN2CM/HP2W);
51 }
52
53 void PistonEngine::setTurboParams(float turbo, float maxMP)
54 {
55     _turbo = turbo;
56     _maxMP = maxMP;
57
58     // This changes the "sea level" manifold air density
59     float P0 = Atmosphere::getStdPressure(0);
60     float P = P0 * (1 + _boost * (_turbo - 1));
61     if(P > _maxMP) P = _maxMP;
62     float T = Atmosphere::getStdTemperature(0) * Math::pow(P/P0, 2./7.);
63     _rho0 = P / (287.1f * T);
64 }
65
66 void PistonEngine::setDisplacement(float d)
67 {
68     _displacement = d;
69 }
70
71 void PistonEngine::setCompression(float c)
72 {
73     _compression = c;
74 }
75
76 void PistonEngine::setMinThrottle(float m)
77 {
78     _minthrottle = m;
79 }
80
81 float PistonEngine::getMaxPower()
82 {
83     return _power0;
84 }
85
86 bool PistonEngine::isCranking()
87 {
88     return _starter;
89 }
90
91 float PistonEngine::getTorque()
92 {
93     return _torque;
94 }
95
96 float PistonEngine::getFuelFlow()
97 {
98     return _fuelFlow;
99 }
100
101 float PistonEngine::getMP()
102 {
103     return _mp;
104 }
105
106 float PistonEngine::getEGT()
107 {
108     return _egt;
109 }
110
111 void PistonEngine::stabilize()
112 {
113     _oilTemp = _oilTempTarget;
114     _charge = _chargeTarget;
115 }
116
117 void PistonEngine::integrate(float dt) 
118 {
119     _oilTemp += (_dOilTempdt * dt);
120
121     // See comments in Jet.cpp for how this decay constant works
122     float decay = 2.3f / _turboLag;
123     _charge = (_charge + dt*decay * _chargeTarget) / (1 + dt*decay);
124 }
125
126 void PistonEngine::calc(float pressure, float temp, float speed)
127 {
128     _running = _magnetos && _fuel && (speed > 60*RPM2RADPS);
129
130     // Calculate the factor required to modify supercharger output for
131     // rpm. Assume that the normalized supercharger output ~= 1 when
132     // the engine is at the nominal peak-power rpm.  A power equation
133     // of the form (A * B^x * x^C) has been derived empirically from
134     // some representative supercharger data.  This provides
135     // near-linear output over the normal operating range, with
136     // fall-off in the over-speed situation.
137     float rpm_norm = (speed / _omega0);
138     float A = 1.795206541;
139     float B = 0.55620178;
140     float C = 1.246708471;
141     float rpm_factor = A * Math::pow(B, rpm_norm) * Math::pow(rpm_norm, C);
142     _chargeTarget = 1 + (_boost * (_turbo-1) * rpm_factor);
143
144     if(_hasSuper) {
145         // Superchargers have no lag
146         _charge = _chargeTarget;
147     } else if(!_running) {
148         // Turbochargers only work well when the engine is actually
149         // running.  The 25% number is a guesstimate from Vivian.
150         _chargeTarget = 1 + (_chargeTarget - 1) * 0.25;
151     }
152
153     // We need to adjust the minimum manifold pressure to get a
154     // reasonable idle speed (a "closed" throttle doesn't suck a total
155     // vacuum in real manifolds).  This is a hack.
156     float _minMP = (-0.008 * _turbo ) + _minthrottle;
157
158     _mp = pressure * _charge;
159
160     // Scale to throttle setting, clamp to wastegate
161     if(_running)
162         _mp *= _minMP + (1 -_minMP) * _throttle;
163
164     // Scale the max MP according to the WASTEGATE control input.  Use
165     // the un-supercharged MP as the bottom limit.
166     float max = _wastegate * _maxMP;
167     if(max < _mp/_charge) max = _mp/_charge;
168     if(_mp > max) _mp = max;
169     
170
171     // The "boost" is the delta above ambient
172     _boostPressure = _mp - pressure;
173
174     // Air entering the manifold does so rapidly, and thus the
175     // pressure change can be assumed to be adiabatic.  Calculate a
176     // temperature change, and use that to get the density.
177     // Note: need to model intercoolers here...
178     float T = temp * Math::pow((_mp*_mp)/(pressure*pressure), 1.0/7.0);
179     float rho = _mp / (287.1f * T);
180
181     // The actual fuel flow is determined only by engine RPM and the
182     // mixture setting.  Not all of this will burn with the same
183     // efficiency.
184     _fuelFlow = _mixture * speed * _mixCoeff;
185     if(_fuel == false) _fuelFlow = 0;
186
187     // How much fuel could be burned with ideal (i.e. uncorrected!)
188     // combustion.
189     float burnable = _f0 * (rho/_rho0) * (speed/_omega0);
190
191     // Calculate the fuel that actually burns to produce work.  The
192     // idea is that less than 5/8 of ideal, we get complete
193     // combustion.  We use up all the oxygen at 1 3/8 of ideal (that
194     // is, you need to waste fuel to use all your O2).  In between,
195     // interpolate.  This vaguely matches a curve I copied out of a
196     // book for a single engine.  Shrug.
197     float burned;
198     float r = _fuelFlow/burnable;
199     if     (burnable == 0) burned = 0;
200     else if(r < .625)      burned = _fuelFlow;
201     else if(r > 1.375)     burned = burnable;
202     else
203         burned = _fuelFlow + (burnable-_fuelFlow)*(r-0.625f)*(4.0f/3.0f);
204
205     // Correct for engine control state
206     if(!_running)
207         burned = 0;
208     if(_magnetos < 3)
209         burned *= 0.9f;
210
211     // And finally the power is just the reference power scaled by the
212     // amount of fuel burned, and torque is that divided by RPM.
213     float power = _power0 * burned/_f0;
214     _torque = power/speed;
215
216     // Figure that the starter motor produces 15% of the engine's
217     // cruise torque.  Assuming 60RPM starter speed vs. 1800RPM cruise
218     // speed on a 160HP engine, that comes out to about 160*.15/30 ==
219     // 0.8 HP starter motor.  Which sounds about right to me.  I think
220     // I've finally got this tuned. :)
221     if(_starter && !_running)
222         _torque += 0.15f * _power0/_omega0;
223
224     // Also, add a negative torque of 8% of cruise, to represent
225     // internal friction.  Propeller aerodynamic friction is too low
226     // at low RPMs to provide a good deceleration.  Interpolate it
227     // away as we approach cruise RPMs (full at 50%, zero at 100%),
228     // though, to prevent interaction with the power computations.
229     // Ugly.
230     if(speed > 0 && speed < _omega0) {
231         float interp = 2 - 2*speed/_omega0;
232         interp = (interp > 1) ? 1 : interp;
233         _torque -= 0.08f * (_power0/_omega0) * interp;
234     }
235
236     // Now EGT.  This one gets a little goofy.  We can calculate the
237     // work done by an isentropically expanding exhaust gas as the
238     // mass of the gas times the specific heat times the change in
239     // temperature.  The mass is just the engine displacement times
240     // the manifold density, plus the mass of the fuel, which we know.
241     // The change in temperature can be calculated adiabatically as a
242     // function of the exhaust gas temperature and the compression
243     // ratio (which we know).  So just rearrange the equation to get
244     // EGT as a function of engine power.  Cool.  I'm using a value of
245     // 1300 J/(kg*K) for the exhaust gas specific heat.  I found this
246     // on a web page somewhere; no idea if it's accurate.  Also,
247     // remember that four stroke engines do one combustion cycle every
248     // TWO revolutions, so the displacement per revolution is half of
249     // what we'd expect.  And diddle the work done by the gas a bit to
250     // account for non-thermodynamic losses like internal friction;
251     // 10% should do it.
252     float massFlow = _fuelFlow + (rho * 0.5f * _displacement * speed);
253     float specHeat = 1300;
254     float corr = 1.0f/(Math::pow(_compression, 0.4f) - 1.0f);
255     _egt = corr * (power * 1.1f) / (massFlow * specHeat);
256     if(_egt < temp) _egt = temp;
257     
258     
259     // Oil temperature.
260     // Assume a linear variation between ~90degC at idle and ~120degC
261     // at full power.  No attempt to correct for airflow over the
262     // engine is made.  Make the time constant to attain target steady-
263     // state oil temp greater at engine off than on to reflect no
264     // circulation.  Nothing fancy, but populates the guage with a
265     // plausible value.
266     float tau;  // secs 
267     if(_running) {
268         _oilTempTarget = 363.0f + (30.0f * (power/_power0));
269         tau = 600;
270         // Reduce tau linearly to 300 at max power
271         tau -= (power/_power0) * 300.0f;
272     } else {
273         _oilTempTarget = temp;
274         tau = 1500;             
275     }
276     _dOilTempdt = (_oilTempTarget - _oilTemp) / tau;
277 }
278
279 }; // namespace yasim