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