]> git.mxchange.org Git - flightgear.git/blob - src/FDM/YASim/PistonEngine.cpp
Somewhere along the line in the recent changes some std::cout were
[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.7;
7 const static float CIN2CM = 1.6387064e-5;
8 const static float RPM2RADPS = 0.1047198;
9
10 PistonEngine::PistonEngine(float power, float speed)
11 {
12     _boost = 1;
13     _running = false;
14     _cranking = false;
15
16     // Presume a BSFC (in lb/hour per HP) of 0.45.  In SI that becomes
17     // (2.2 lb/kg, 745.7 W/hp, 3600 sec/hour) 7.62e-08 kg/Ws.
18     _f0 = power * 7.62e-08;
19
20     _power0 = power;
21     _omega0 = speed;
22
23     // We must be at sea level under standard conditions
24     _rho0 = Atmosphere::getStdDensity(0);
25
26     // Further presume that takeoff is (duh) full throttle and
27     // peak-power, that means that by our efficiency function, we are
28     // at 11/8 of "ideal" fuel flow.
29     float realFlow = _f0 * (11.0/8.0);
30     _mixCoeff = realFlow * 1.1 / _omega0;
31
32     _turbo = 1;
33     _maxMP = 1e6; // No waste gate on non-turbo engines.
34
35     // Guess at reasonable values for these guys.  Displacements run
36     // at about 2 cubic inches per horsepower or so, at least for
37     // non-turbocharged engines.
38     _compression = 8;
39     _displacement = power * (2*CIN2CM/HP2W);
40 }
41
42 void PistonEngine::setTurboParams(float turbo, float maxMP)
43 {
44     _turbo = turbo;
45     _maxMP = maxMP;
46
47     // This changes the "sea level" manifold air density
48     float P0 = Atmosphere::getStdPressure(0);
49     float P = P0 * (1 + _boost * (_turbo - 1));
50     if(P > _maxMP) P = _maxMP;
51     float T = Atmosphere::getStdTemperature(0) * Math::pow(P/P0, 2./7.);
52     _rho0 = P / (287.1 * T);
53 }
54
55 void PistonEngine::setDisplacement(float d)
56 {
57     _displacement = d;
58 }
59
60 void PistonEngine::setCompression(float c)
61 {
62     _compression = c;
63 }
64
65 float PistonEngine::getMaxPower()
66 {
67     return _power0;
68 }
69
70 void PistonEngine::setThrottle(float t)
71 {
72     _throttle = t;
73 }
74
75 void PistonEngine::setRunning(bool r)
76 {
77     _running = r;
78 }
79
80 void PistonEngine::setStarter(bool s)
81 {
82     _cranking = s;
83 }
84
85 void PistonEngine::setMagnetos(int m)
86 {
87     _magnetos = m;
88 }
89
90 void PistonEngine::setMixture(float m)
91 {
92     _mixture = m;
93 }
94
95 void PistonEngine::setBoost(float boost)
96 {
97     _boost = boost;
98 }
99
100 bool PistonEngine::isRunning()
101 {
102     return _running;
103 }
104
105 bool PistonEngine::isCranking()
106 {
107     return _cranking;
108 }
109
110 float PistonEngine::getTorque()
111 {
112     return _torque;
113 }
114
115 float PistonEngine::getFuelFlow()
116 {
117     return _fuelFlow;
118 }
119
120 float PistonEngine::getMP()
121 {
122     return _mp;
123 }
124
125 float PistonEngine::getEGT()
126 {
127     return _egt;
128 }
129
130 void PistonEngine::calc(float pressure, float temp, float speed)
131 {
132     if(_magnetos == 0 || speed < 200*RPM2RADPS)
133         _running = false;
134     else
135         _running = true;
136
137     // Calculate manifold pressure as ambient pressure modified for
138     // turbocharging and reduced by the throttle setting.  According
139     // to Dave Luff, minimum throttle at sea level corresponds to 6"
140     // manifold pressure.  Assume that this means that minimum MP is
141     // always 20% of ambient pressure.  But we need to produce _zero_
142     // thrust at that setting, so hold onto the "output" value
143     // separately.  Ick.
144     _mp = pressure * (1 + _boost*(_turbo-1)); // turbocharger
145     float mp = _mp * (0.2 + 0.8 * _throttle); // throttle
146     _mp *= _throttle;
147     if(mp > _maxMP) mp = _maxMP;              // wastegate
148
149     // Air entering the manifold does so rapidly, and thus the
150     // pressure change can be assumed to be adiabatic.  Calculate a
151     // temperature change, and use that to get the density.
152     float T = temp * Math::pow(mp/pressure, 2.0/7.0);
153     float rho = mp / (287.1 * T);
154
155     // The actual fuel flow is determined only by engine RPM and the
156     // mixture setting.  Not all of this will burn with the same
157     // efficiency.
158     _fuelFlow = _mixture * speed * _mixCoeff;
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-.625)*(4.0/3.0);
177
178     // Correct for engine control state
179     if(!_running)
180         burned = 0;
181     if(_magnetos < 3)
182         burned *= 0.9;
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 20% of the engine's
190     // cruise torque.
191     if(_cranking && !_running)
192         _torque += 0.20 * _power0/_omega0;
193
194     // Also, add a negative torque of 10% of cruise, to represent
195     // internal friction.  Propeller aerodynamic friction is too low
196     // at low RPMs to provide a good deceleration.  Interpolate it
197     // away as we approach cruise RPMs, though, to prevent interaction
198     // with the power computations.  Ugly.
199     if(speed > 0 && speed < _omega0)
200         _torque -= 0.05 * (_power0/_omega0) * (1 - speed/_omega0);
201
202     // Now EGT.  This one gets a little goofy.  We can calculate the
203     // work done by an isentropically expanding exhaust gas as the
204     // mass of the gas times the specific heat times the change in
205     // temperature.  The mass is just the engine displacement times
206     // the manifold density, plus the mass of the fuel, which we know.
207     // The change in temperature can be calculated adiabatically as a
208     // function of the exhaust gas temperature and the compression
209     // ratio (which we know).  So just rearrange the equation to get
210     // EGT as a function of engine power.  Cool.  I'm using a value of
211     // 1300 J/(kg*K) for the exhaust gas specific heat.  I found this
212     // on a web page somewhere; no idea if it's accurate.  Also,
213     // remember that four stroke engines do one combustion cycle every
214     // TWO revolutions, so the displacement per revolution is half of
215     // what we'd expect.  And diddle the work done by the gas a bit to
216     // account for non-thermodynamic losses like internal friction;
217     // 10% should do it.
218
219     float massFlow = _fuelFlow + (rho * 0.5 * _displacement * speed);
220     float specHeat = 1300;
221     float corr = 1.0/(Math::pow(_compression, 0.4) - 1);
222     _egt = corr * (power * 1.1) / (massFlow * specHeat);
223     if(_egt < temp) _egt = temp;
224 }
225
226 }; // namespace yasim