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