]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGTurbine.cpp
Sync w. JSBSim CVS
[flightgear.git] / src / FDM / JSBSim / FGTurbine.cpp
1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2
3  Module:       FGTurbine.cpp
4  Author:       David Culp
5  Date started: 03/11/2003
6  Purpose:      This module models a turbine engine.
7
8  ------------- Copyright (C) 2003  David Culp (davidculp2@comcast.net) ---------
9
10  This program is free software; you can redistribute it and/or modify it under
11  the terms of the GNU General Public License as published by the Free Software
12  Foundation; either version 2 of the License, or (at your option) any later
13  version.
14
15  This program is distributed in the hope that it will be useful, but WITHOUT
16  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17  FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
18  details.
19
20  You should have received a copy of the GNU General Public License along with
21  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
22  Place - Suite 330, Boston, MA  02111-1307, USA.
23
24  Further information about the GNU General Public License can also be found on
25  the world wide web at http://www.gnu.org.
26
27 FUNCTIONAL DESCRIPTION
28 --------------------------------------------------------------------------------
29
30 This class descends from the FGEngine class and models a turbine engine based
31 on parameters given in the engine config file for this class
32
33 HISTORY
34 --------------------------------------------------------------------------------
35 03/11/2003  DPC  Created
36 09/08/2003  DPC  Changed Calculate() and added engine phases
37
38 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
39 INCLUDES
40 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
41
42 #include <vector>
43 #include <sstream>
44
45 #include "FGTurbine.h"
46
47 namespace JSBSim {
48
49 static const char *IdSrc = "$Id$";
50 static const char *IdHdr = ID_TURBINE;
51
52 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
53 CLASS IMPLEMENTATION
54 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
55
56
57 FGTurbine::FGTurbine(FGFDMExec* exec, FGConfigFile* cfg) : FGEngine(exec)
58 {
59   SetDefaults();
60
61   Load(cfg);
62   Debug(0);
63 }
64
65 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
66
67 FGTurbine::~FGTurbine()
68 {
69   Debug(1);
70 }
71
72 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
73 // The main purpose of Calculate() is to determine what phase the engine should
74 // be in, then call the corresponding function.
75
76 double FGTurbine::Calculate(void)
77 {
78   TAT = (Auxiliary->GetTotalTemperature() - 491.69) * 0.5555556;
79   dt = State->Getdt() * Propulsion->GetRate();
80   ThrottlePos = FCS->GetThrottlePos(EngineNumber);
81   if (ThrottlePos > 1.0) {
82     AugmentCmd = ThrottlePos - 1.0;
83     ThrottlePos -= AugmentCmd;
84   } else {
85     AugmentCmd = 0.0;
86   }
87
88   // When trimming is finished check if user wants engine OFF or RUNNING
89   if ((phase == tpTrim) && (dt > 0)) {
90     if (Running && !Starved) {
91       phase = tpRun;
92       N2 = IdleN2 + ThrottlePos * N2_factor;
93       N1 = IdleN1 + ThrottlePos * N1_factor;
94       OilTemp_degK = 366.0;
95       Cutoff = false;
96       }
97     else {
98       phase = tpOff;
99       Cutoff = true;
100       EGT_degC = TAT;
101       }
102     }
103
104   if (!Running && Cutoff && Starter) {
105      if (phase == tpOff) phase = tpSpinUp;
106      }
107   if (!Running && !Cutoff && (N2 > 15.0)) phase = tpStart;
108   if (Cutoff && (phase != tpSpinUp)) phase = tpOff;
109   if (dt == 0) phase = tpTrim;
110   if (Starved) phase = tpOff;
111   if (Stalled) phase = tpStall;
112   if (Seized) phase = tpSeize;
113
114   switch (phase) {
115     case tpOff:    Thrust = Off(); break;
116     case tpRun:    Thrust = Run(); break;
117     case tpSpinUp: Thrust = SpinUp(); break;
118     case tpStart:  Thrust = Start(); break;
119     case tpStall:  Thrust = Stall(); break;
120     case tpSeize:  Thrust = Seize(); break;
121     case tpTrim:   Thrust = Trim(); break;
122     default: Thrust = Off();
123   }
124
125   return Thruster->Calculate(Thrust);
126 }
127
128 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
129
130 double FGTurbine::Off(void)
131 {
132   double qbar = Auxiliary->Getqbar();
133   Running = false;
134   FuelFlow_pph = Seek(&FuelFlow_pph, 0, 1000.0, 10000.0);
135   N1 = Seek(&N1, qbar/10.0, N1/2.0, N1/2.0);
136   N2 = Seek(&N2, qbar/15.0, N2/2.0, N2/2.0);
137   EGT_degC = Seek(&EGT_degC, TAT, 11.7, 7.3);
138   OilTemp_degK = Seek(&OilTemp_degK, TAT + 273.0, 0.2, 0.2);
139   OilPressure_psi = N2 * 0.62;
140   NozzlePosition = Seek(&NozzlePosition, 1.0, 0.8, 0.8);
141   EPR = Seek(&EPR, 1.0, 0.2, 0.2);
142   Augmentation = false;
143   return 0.0;
144 }
145
146 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
147
148 double FGTurbine::Run(void)
149 {
150   double idlethrust, milthrust, thrust;
151   double N2norm;   // 0.0 = idle N2, 1.0 = maximum N2
152   idlethrust = MilThrust * ThrustTables[0]->TotalValue();
153   milthrust = (MilThrust - idlethrust) * ThrustTables[1]->TotalValue();
154
155   Running = true;
156   Starter = false;
157
158   N2 = Seek(&N2, IdleN2 + ThrottlePos * N2_factor, delay, delay * 3.0);
159   N1 = Seek(&N1, IdleN1 + ThrottlePos * N1_factor, delay, delay * 2.4);
160   N2norm = (N2 - IdleN2) / N2_factor;
161   thrust = idlethrust + (milthrust * N2norm * N2norm);
162   EGT_degC = TAT + 363.1 + ThrottlePos * 357.1;
163   OilPressure_psi = N2 * 0.62;
164   OilTemp_degK = Seek(&OilTemp_degK, 366.0, 1.2, 0.1);
165
166   if (!Augmentation) {
167     double correctedTSFC = TSFC + TSFC - (N2norm * TSFC);
168     FuelFlow_pph = Seek(&FuelFlow_pph, thrust * correctedTSFC, 1000.0, 100000);
169     if (FuelFlow_pph < IdleFF) FuelFlow_pph = IdleFF;
170     NozzlePosition = Seek(&NozzlePosition, 1.0 - N2norm, 0.8, 0.8);
171     thrust = thrust * (1.0 - BleedDemand);
172     EPR = 1.0 + thrust/MilThrust;
173   }
174
175   if (AugMethod == 1) {
176     if ((ThrottlePos > 0.99) && (N2 > 97.0)) {Augmentation = true;}
177     else {Augmentation = false;}
178   }
179
180   if ((Augmented == 1) && Augmentation && (AugMethod < 2)) {
181     thrust = MaxThrust * ThrustTables[2]->TotalValue();
182     FuelFlow_pph = Seek(&FuelFlow_pph, thrust * ATSFC, 5000.0, 10000.0);
183     NozzlePosition = Seek(&NozzlePosition, 1.0, 0.8, 0.8);
184   }
185
186   if ((AugmentCmd > 0.0) && (AugMethod == 2)) {
187     Augmentation = true;
188     double tdiff = (MaxThrust * ThrustTables[2]->TotalValue()) - thrust;
189     thrust += (tdiff * AugmentCmd);
190     FuelFlow_pph = Seek(&FuelFlow_pph, thrust * ATSFC, 5000.0, 10000.0);
191     NozzlePosition = Seek(&NozzlePosition, 1.0, 0.8, 0.8);
192   } else {
193     Augmentation = false;
194   }
195
196   if ((Injected == 1) && Injection)
197     thrust = thrust * ThrustTables[3]->TotalValue();
198
199   ConsumeFuel();
200   if (Cutoff) phase = tpOff;
201   if (Starved) phase = tpOff;
202
203   return thrust;
204 }
205
206 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
207
208 double FGTurbine::SpinUp(void)
209 {
210   Running = false;
211   FuelFlow_pph = 0.0;
212   N2 = Seek(&N2, 25.18, 3.0, N2/2.0);
213   N1 = Seek(&N1, 5.21, 1.0, N1/2.0);
214   EGT_degC = Seek(&EGT_degC, TAT, 11.7, 7.3);
215   OilPressure_psi = N2 * 0.62;
216   OilTemp_degK = Seek(&OilTemp_degK, TAT + 273.0, 0.2, 0.2);
217   EPR = 1.0;
218   NozzlePosition = 1.0;
219   return 0.0;
220 }
221
222 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
223
224 double FGTurbine::Start(void)
225 {
226   if ((N2 > 15.0) && !Starved) {       // minimum 15% N2 needed for start
227     Cranking = true;                   // provided for sound effects signal
228     if (N2 < IdleN2) {
229       N2 = Seek(&N2, IdleN2, 2.0, N2/2.0);
230       N1 = Seek(&N1, IdleN1, 1.4, N1/2.0);
231       EGT_degC = Seek(&EGT_degC, TAT + 363.1, 21.3, 7.3);
232       FuelFlow_pph = Seek(&FuelFlow_pph, IdleFF, 103.7, 103.7);
233       OilPressure_psi = N2 * 0.62;
234       }
235     else {
236       phase = tpRun;
237       Running = true;
238       Starter = false;
239       Cranking = false;
240       }
241     }
242   else {                 // no start if N2 < 15%
243     phase = tpOff;
244     Starter = false;
245     }
246
247   return 0.0;
248 }
249
250 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
251
252 double FGTurbine::Stall(void)
253 {
254   double qbar = Auxiliary->Getqbar();
255   EGT_degC = TAT + 903.14;
256   FuelFlow_pph = IdleFF;
257   N1 = Seek(&N1, qbar/10.0, 0, N1/10.0);
258   N2 = Seek(&N2, qbar/15.0, 0, N2/10.0);
259   if (ThrottlePos < 0.01) phase = tpRun;        // clear the stall with throttle
260
261   return 0.0;
262 }
263
264 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
265
266 double FGTurbine::Seize(void)
267 {
268     double qbar = Auxiliary->Getqbar();
269     N2 = 0.0;
270     N1 = Seek(&N1, qbar/20.0, 0, N1/15.0);
271     FuelFlow_pph = IdleFF;
272     OilPressure_psi = 0.0;
273     OilTemp_degK = Seek(&OilTemp_degK, TAT + 273.0, 0, 0.2);
274     Running = false;
275     return 0.0;
276 }
277
278 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
279
280 double FGTurbine::Trim(void)
281 {
282     double idlethrust, milthrust, thrust, tdiff;
283     idlethrust = MilThrust * ThrustTables[0]->TotalValue();
284     milthrust = (MilThrust - idlethrust) * ThrustTables[1]->TotalValue();
285     thrust = (idlethrust + (milthrust * ThrottlePos * ThrottlePos)) * (1.0 - BleedDemand);
286     if (AugmentCmd > 0.0) {
287       tdiff = (MaxThrust * ThrustTables[2]->TotalValue()) - thrust;
288       thrust += (tdiff * AugmentCmd);
289       }
290     return thrust;
291 }
292
293 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
294
295 double FGTurbine::CalcFuelNeed(void)
296 {
297   return FuelFlow_pph /3600 * State->Getdt() * Propulsion->GetRate();
298 }
299
300 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
301
302 double FGTurbine::GetPowerAvailable(void) {
303   if( ThrottlePos <= 0.77 )
304     return 64.94*ThrottlePos;
305   else
306     return 217.38*ThrottlePos - 117.38;
307 }
308
309 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
310
311 double FGTurbine::Seek(double *var, double target, double accel, double decel) {
312   double v = *var;
313   if (v > target) {
314     v -= dt * decel;
315     if (v < target) v = target;
316   } else if (v < target) {
317     v += dt * accel;
318     if (v > target) v = target;
319   }
320   return v;
321 }
322
323 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
324
325 void FGTurbine::SetDefaults(void)
326 {
327   Name = "Not defined";
328   N1 = N2 = 0.0;
329   Type = etTurbine;
330   MilThrust = 10000.0;
331   MaxThrust = 10000.0;
332   BypassRatio = 0.0;
333   TSFC = 0.8;
334   ATSFC = 1.7;
335   IdleN1 = 30.0;
336   IdleN2 = 60.0;
337   MaxN1 = 100.0;
338   MaxN2 = 100.0;
339   Augmented = 0;
340   AugMethod = 0;
341   Injected = 0;
342   BleedDemand = 0.0;
343   ThrottlePos = 0.0;
344   AugmentCmd = 0.0;
345   InletPosition = 1.0;
346   NozzlePosition = 1.0;
347   Augmentation = false;
348   Injection = false;
349   Reversed = false;
350   Cutoff = true;
351   phase = tpOff;
352   Stalled = false;
353   Seized = false;
354   Overtemp = false;
355   Fire = false;
356   EGT_degC = 0.0;
357 }
358
359 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
360
361 bool FGTurbine::Load(FGConfigFile *Eng_cfg)
362 {
363   string token;
364
365   Name = Eng_cfg->GetValue("NAME");
366   Eng_cfg->GetNextConfigLine();
367   int counter=0;
368
369   while ( ((token = Eng_cfg->GetValue()) != string("/FG_TURBINE")) &&
370           (token != string("/FG_SIMTURBINE")) ) {
371     *Eng_cfg >> token;
372
373     if (token[0] == '<') token.erase(0,1); // Tables are read "<TABLE"
374
375     if      (token == "MILTHRUST") *Eng_cfg >> MilThrust;
376     else if (token == "MAXTHRUST") *Eng_cfg >> MaxThrust;
377     else if (token == "BYPASSRATIO") *Eng_cfg >> BypassRatio;
378     else if (token == "BLEED") *Eng_cfg >> BleedDemand;
379     else if (token == "TSFC") *Eng_cfg >> TSFC;
380     else if (token == "ATSFC") *Eng_cfg >> ATSFC;
381     else if (token == "IDLEN1") *Eng_cfg >> IdleN1;
382     else if (token == "IDLEN2") *Eng_cfg >> IdleN2;
383     else if (token == "MAXN1") *Eng_cfg >> MaxN1;
384     else if (token == "MAXN2") *Eng_cfg >> MaxN2;
385     else if (token == "AUGMENTED") *Eng_cfg >> Augmented;
386     else if (token == "AUGMETHOD") *Eng_cfg >> AugMethod;
387     else if (token == "INJECTED") *Eng_cfg >> Injected;
388     else if (token == "MINTHROTTLE") *Eng_cfg >> MinThrottle;
389     else if (token == "TABLE") {
390       if (counter++ == 0) Debug(2); // print engine specs prior to table read
391       ThrustTables.push_back( new FGCoefficient(FDMExec) );
392       ThrustTables.back()->Load(Eng_cfg);
393     }
394     else cerr << "Unhandled token in Engine config file: " << token << endl;
395   }
396
397   // Pre-calculations and initializations
398
399   delay = 60.0 / (BypassRatio + 3.0);
400   N1_factor = MaxN1 - IdleN1;
401   N2_factor = MaxN2 - IdleN2;
402   OilTemp_degK = (Auxiliary->GetTotalTemperature() - 491.69) * 0.5555556 + 273.0;
403   IdleFF = pow(MilThrust, 0.2) * 107.0;  // just an estimate
404
405   return true;
406 }
407
408 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
409
410 string FGTurbine::GetEngineLabels(void)
411 {
412   std::ostringstream buf;
413
414   buf << Name << "_N1[" << EngineNumber << "], "
415       << Name << "_N2[" << EngineNumber << "], "
416       << Thruster->GetThrusterLabels(EngineNumber);
417
418   return buf.str();
419 }
420
421 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
422
423 string FGTurbine::GetEngineValues(void)
424 {
425   std::ostringstream buf;
426
427   buf << N1 << ", "
428       << N2 << ", "
429       << Thruster->GetThrusterValues(EngineNumber);
430
431   return buf.str();
432 }
433
434 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
435 //    The bitmasked value choices are as follows:
436 //    unset: In this case (the default) JSBSim would only print
437 //       out the normally expected messages, essentially echoing
438 //       the config files as they are read. If the environment
439 //       variable is not set, debug_lvl is set to 1 internally
440 //    0: This requests JSBSim not to output any messages
441 //       whatsoever.
442 //    1: This value explicity requests the normal JSBSim
443 //       startup messages
444 //    2: This value asks for a message to be printed out when
445 //       a class is instantiated
446 //    4: When this value is set, a message is displayed when a
447 //       FGModel object executes its Run() method
448 //    8: When this value is set, various runtime state variables
449 //       are printed out periodically
450 //    16: When set various parameters are sanity checked and
451 //       a message is printed out when they go out of bounds
452
453 void FGTurbine::Debug(int from)
454 {
455   if (debug_lvl <= 0) return;
456
457   if (debug_lvl & 1) { // Standard console startup message output
458     if (from == 0) { // Constructor
459
460     }
461     if (from == 2) { // called from Load()
462       cout << "\n    Engine Name: "         << Name << endl;
463       cout << "      MilThrust:   "         << MilThrust << endl;
464       cout << "      MaxThrust:   "         << MaxThrust << endl;
465       cout << "      BypassRatio: "         << BypassRatio << endl;
466       cout << "      TSFC:        "         << TSFC << endl;
467       cout << "      ATSFC:       "         << ATSFC << endl;
468       cout << "      IdleN1:      "         << IdleN1 << endl;
469       cout << "      IdleN2:      "         << IdleN2 << endl;
470       cout << "      MaxN1:       "         << MaxN1 << endl;
471       cout << "      MaxN2:       "         << MaxN2 << endl;
472       cout << "      Augmented:   "         << Augmented << endl;
473       cout << "      AugMethod:   "         << AugMethod << endl;
474       cout << "      Injected:    "         << Injected << endl;
475       cout << "      MinThrottle: "         << MinThrottle << endl;
476
477       cout << endl;
478     }
479   }
480   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
481     if (from == 0) cout << "Instantiated: FGTurbine" << endl;
482     if (from == 1) cout << "Destroyed:    FGTurbine" << endl;
483   }
484   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
485   }
486   if (debug_lvl & 8 ) { // Runtime state variables
487   }
488   if (debug_lvl & 16) { // Sanity checking
489   }
490   if (debug_lvl & 64) {
491     if (from == 0) { // Constructor
492       cout << IdSrc << endl;
493       cout << IdHdr << endl;
494     }
495   }
496 }
497 }