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