]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGTurbine.cpp
Frederic Bouvier:
[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, int engine_number)
58   : FGEngine(exec, engine_number)
59 {
60   SetDefaults();
61
62   Load(cfg);
63   Debug(0);
64 }
65
66 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
67
68 FGTurbine::~FGTurbine()
69 {
70   unbind();
71   Debug(1);
72 }
73
74 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
75 // The main purpose of Calculate() is to determine what phase the engine should
76 // be in, then call the corresponding function.
77
78 double FGTurbine::Calculate(void)
79 {
80   TAT = (Auxiliary->GetTotalTemperature() - 491.69) * 0.5555556;
81   dt = State->Getdt() * Propulsion->GetRate();
82   ThrottlePos = FCS->GetThrottlePos(EngineNumber);
83   if (ThrottlePos > 1.0) {
84     AugmentCmd = ThrottlePos - 1.0;
85     ThrottlePos -= AugmentCmd;
86   } else {
87     AugmentCmd = 0.0;
88   }
89
90   // When trimming is finished check if user wants engine OFF or RUNNING
91   if ((phase == tpTrim) && (dt > 0)) {
92     if (Running && !Starved) {
93       phase = tpRun;
94       N2 = IdleN2 + ThrottlePos * N2_factor;
95       N1 = IdleN1 + ThrottlePos * N1_factor;
96       OilTemp_degK = 366.0;
97       Cutoff = false;
98       }
99     else {
100       phase = tpOff;
101       Cutoff = true;
102       EGT_degC = TAT;
103       }
104     }
105
106   if (!Running && Cutoff && Starter) {
107      if (phase == tpOff) phase = tpSpinUp;
108      }
109   if (!Running && !Cutoff && (N2 > 15.0)) phase = tpStart;
110   if (Cutoff && (phase != tpSpinUp)) phase = tpOff;
111   if (dt == 0) phase = tpTrim;
112   if (Starved) phase = tpOff;
113   if (Stalled) phase = tpStall;
114   if (Seized) phase = tpSeize;
115
116   switch (phase) {
117     case tpOff:    Thrust = Off(); break;
118     case tpRun:    Thrust = Run(); break;
119     case tpSpinUp: Thrust = SpinUp(); break;
120     case tpStart:  Thrust = Start(); break;
121     case tpStall:  Thrust = Stall(); break;
122     case tpSeize:  Thrust = Seize(); break;
123     case tpTrim:   Thrust = Trim(); break;
124     default: Thrust = Off();
125   }
126
127   return Thruster->Calculate(Thrust);
128 }
129
130 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
131
132 double FGTurbine::Off(void)
133 {
134   double qbar = Auxiliary->Getqbar();
135   Running = false;
136   FuelFlow_pph = Seek(&FuelFlow_pph, 0, 1000.0, 10000.0);
137   N1 = Seek(&N1, qbar/10.0, N1/2.0, N1/2.0);
138   N2 = Seek(&N2, qbar/15.0, N2/2.0, N2/2.0);
139   EGT_degC = Seek(&EGT_degC, TAT, 11.7, 7.3);
140   OilTemp_degK = Seek(&OilTemp_degK, TAT + 273.0, 0.2, 0.2);
141   OilPressure_psi = N2 * 0.62;
142   NozzlePosition = Seek(&NozzlePosition, 1.0, 0.8, 0.8);
143   EPR = Seek(&EPR, 1.0, 0.2, 0.2);
144   Augmentation = false;
145   return 0.0;
146 }
147
148 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
149
150 double FGTurbine::Run(void)
151 {
152   double idlethrust, milthrust, thrust;
153   double N2norm;   // 0.0 = idle N2, 1.0 = maximum N2
154   idlethrust = MilThrust * ThrustTables[0]->TotalValue();
155   milthrust = (MilThrust - idlethrust) * ThrustTables[1]->TotalValue();
156
157   Running = true;
158   Starter = false;
159
160   N2 = Seek(&N2, IdleN2 + ThrottlePos * N2_factor, delay, delay * 3.0);
161   N1 = Seek(&N1, IdleN1 + ThrottlePos * N1_factor, delay, delay * 2.4);
162   N2norm = (N2 - IdleN2) / N2_factor;
163   thrust = idlethrust + (milthrust * N2norm * N2norm);
164   EGT_degC = TAT + 363.1 + ThrottlePos * 357.1;
165   OilPressure_psi = N2 * 0.62;
166   OilTemp_degK = Seek(&OilTemp_degK, 366.0, 1.2, 0.1);
167
168   if (!Augmentation) {
169     double correctedTSFC = TSFC + TSFC - (N2norm * TSFC);
170     FuelFlow_pph = Seek(&FuelFlow_pph, thrust * correctedTSFC, 1000.0, 100000);
171     if (FuelFlow_pph < IdleFF) FuelFlow_pph = IdleFF;
172     NozzlePosition = Seek(&NozzlePosition, 1.0 - N2norm, 0.8, 0.8);
173     thrust = thrust * (1.0 - BleedDemand);
174     EPR = 1.0 + thrust/MilThrust;
175   }
176
177   if (AugMethod == 1) {
178     if ((ThrottlePos > 0.99) && (N2 > 97.0)) {Augmentation = true;}
179     else {Augmentation = false;}
180   }
181
182   if ((Augmented == 1) && Augmentation && (AugMethod < 2)) {
183     thrust = MaxThrust * ThrustTables[2]->TotalValue();
184     FuelFlow_pph = Seek(&FuelFlow_pph, thrust * ATSFC, 5000.0, 10000.0);
185     NozzlePosition = Seek(&NozzlePosition, 1.0, 0.8, 0.8);
186   }
187
188   if (AugMethod == 2) {
189     if (AugmentCmd > 0.0) {
190       Augmentation = true;
191       double tdiff = (MaxThrust * ThrustTables[2]->TotalValue()) - thrust;
192       thrust += (tdiff * AugmentCmd);
193       FuelFlow_pph = Seek(&FuelFlow_pph, thrust * ATSFC, 5000.0, 10000.0);
194       NozzlePosition = Seek(&NozzlePosition, 1.0, 0.8, 0.8);
195     } else {
196       Augmentation = false;
197     }
198   }
199
200   if ((Injected == 1) && Injection)
201     thrust = thrust * ThrustTables[3]->TotalValue();
202
203   ConsumeFuel();
204   if (Cutoff) phase = tpOff;
205   if (Starved) phase = tpOff;
206
207   return thrust;
208 }
209
210 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
211
212 double FGTurbine::SpinUp(void)
213 {
214   Running = false;
215   FuelFlow_pph = 0.0;
216   N2 = Seek(&N2, 25.18, 3.0, N2/2.0);
217   N1 = Seek(&N1, 5.21, 1.0, N1/2.0);
218   EGT_degC = Seek(&EGT_degC, TAT, 11.7, 7.3);
219   OilPressure_psi = N2 * 0.62;
220   OilTemp_degK = Seek(&OilTemp_degK, TAT + 273.0, 0.2, 0.2);
221   EPR = 1.0;
222   NozzlePosition = 1.0;
223   return 0.0;
224 }
225
226 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
227
228 double FGTurbine::Start(void)
229 {
230   if ((N2 > 15.0) && !Starved) {       // minimum 15% N2 needed for start
231     Cranking = true;                   // provided for sound effects signal
232     if (N2 < IdleN2) {
233       N2 = Seek(&N2, IdleN2, 2.0, N2/2.0);
234       N1 = Seek(&N1, IdleN1, 1.4, N1/2.0);
235       EGT_degC = Seek(&EGT_degC, TAT + 363.1, 21.3, 7.3);
236       FuelFlow_pph = Seek(&FuelFlow_pph, IdleFF, 103.7, 103.7);
237       OilPressure_psi = N2 * 0.62;
238       }
239     else {
240       phase = tpRun;
241       Running = true;
242       Starter = false;
243       Cranking = false;
244       }
245     }
246   else {                 // no start if N2 < 15%
247     phase = tpOff;
248     Starter = false;
249     }
250
251   return 0.0;
252 }
253
254 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
255
256 double FGTurbine::Stall(void)
257 {
258   double qbar = Auxiliary->Getqbar();
259   EGT_degC = TAT + 903.14;
260   FuelFlow_pph = IdleFF;
261   N1 = Seek(&N1, qbar/10.0, 0, N1/10.0);
262   N2 = Seek(&N2, qbar/15.0, 0, N2/10.0);
263   if (ThrottlePos < 0.01) phase = tpRun;        // clear the stall with throttle
264
265   return 0.0;
266 }
267
268 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
269
270 double FGTurbine::Seize(void)
271 {
272     double qbar = Auxiliary->Getqbar();
273     N2 = 0.0;
274     N1 = Seek(&N1, qbar/20.0, 0, N1/15.0);
275     FuelFlow_pph = IdleFF;
276     OilPressure_psi = 0.0;
277     OilTemp_degK = Seek(&OilTemp_degK, TAT + 273.0, 0, 0.2);
278     Running = false;
279     return 0.0;
280 }
281
282 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
283
284 double FGTurbine::Trim(void)
285 {
286     double idlethrust, milthrust, thrust, tdiff;
287     idlethrust = MilThrust * ThrustTables[0]->TotalValue();
288     milthrust = (MilThrust - idlethrust) * ThrustTables[1]->TotalValue();
289     thrust = (idlethrust + (milthrust * ThrottlePos * ThrottlePos)) * (1.0 - BleedDemand);
290     if (AugmentCmd > 0.0) {
291       tdiff = (MaxThrust * ThrustTables[2]->TotalValue()) - thrust;
292       thrust += (tdiff * AugmentCmd);
293       }
294     return thrust;
295 }
296
297 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
298
299 double FGTurbine::CalcFuelNeed(void)
300 {
301   return FuelFlow_pph /3600 * State->Getdt() * Propulsion->GetRate();
302 }
303
304 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
305
306 double FGTurbine::GetPowerAvailable(void) {
307   if( ThrottlePos <= 0.77 )
308     return 64.94*ThrottlePos;
309   else
310     return 217.38*ThrottlePos - 117.38;
311 }
312
313 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
314
315 double FGTurbine::Seek(double *var, double target, double accel, double decel) {
316   double v = *var;
317   if (v > target) {
318     v -= dt * decel;
319     if (v < target) v = target;
320   } else if (v < target) {
321     v += dt * accel;
322     if (v > target) v = target;
323   }
324   return v;
325 }
326
327 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
328
329 void FGTurbine::SetDefaults(void)
330 {
331   Name = "Not defined";
332   N1 = N2 = 0.0;
333   Type = etTurbine;
334   MilThrust = 10000.0;
335   MaxThrust = 10000.0;
336   BypassRatio = 0.0;
337   TSFC = 0.8;
338   ATSFC = 1.7;
339   IdleN1 = 30.0;
340   IdleN2 = 60.0;
341   MaxN1 = 100.0;
342   MaxN2 = 100.0;
343   Augmented = 0;
344   AugMethod = 0;
345   Injected = 0;
346   BleedDemand = 0.0;
347   ThrottlePos = 0.0;
348   AugmentCmd = 0.0;
349   InletPosition = 1.0;
350   NozzlePosition = 1.0;
351   Augmentation = false;
352   Injection = false;
353   Reversed = false;
354   Cutoff = true;
355   phase = tpOff;
356   Stalled = false;
357   Seized = false;
358   Overtemp = false;
359   Fire = false;
360   EGT_degC = 0.0;
361 }
362
363 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
364
365 bool FGTurbine::Load(FGConfigFile *Eng_cfg)
366 {
367   string token;
368
369   Name = Eng_cfg->GetValue("NAME");
370   Eng_cfg->GetNextConfigLine();
371   int counter=0;
372
373   while ( ((token = Eng_cfg->GetValue()) != string("/FG_TURBINE")) &&
374           (token != string("/FG_SIMTURBINE")) ) {
375     *Eng_cfg >> token;
376
377     if (token[0] == '<') token.erase(0,1); // Tables are read "<TABLE"
378
379     if      (token == "MILTHRUST") *Eng_cfg >> MilThrust;
380     else if (token == "MAXTHRUST") *Eng_cfg >> MaxThrust;
381     else if (token == "BYPASSRATIO") *Eng_cfg >> BypassRatio;
382     else if (token == "BLEED") *Eng_cfg >> BleedDemand;
383     else if (token == "TSFC") *Eng_cfg >> TSFC;
384     else if (token == "ATSFC") *Eng_cfg >> ATSFC;
385     else if (token == "IDLEN1") *Eng_cfg >> IdleN1;
386     else if (token == "IDLEN2") *Eng_cfg >> IdleN2;
387     else if (token == "MAXN1") *Eng_cfg >> MaxN1;
388     else if (token == "MAXN2") *Eng_cfg >> MaxN2;
389     else if (token == "AUGMENTED") *Eng_cfg >> Augmented;
390     else if (token == "AUGMETHOD") *Eng_cfg >> AugMethod;
391     else if (token == "INJECTED") *Eng_cfg >> Injected;
392     else if (token == "MINTHROTTLE") *Eng_cfg >> MinThrottle;
393     else if (token == "TABLE") {
394       if (counter++ == 0) Debug(2); // print engine specs prior to table read
395       ThrustTables.push_back( new FGCoefficient(FDMExec) );
396       ThrustTables.back()->Load(Eng_cfg);
397     }
398     else cerr << "Unhandled token in Engine config file: " << token << endl;
399   }
400
401   // Pre-calculations and initializations
402
403   delay = 60.0 / (BypassRatio + 3.0);
404   N1_factor = MaxN1 - IdleN1;
405   N2_factor = MaxN2 - IdleN2;
406   OilTemp_degK = (Auxiliary->GetTotalTemperature() - 491.69) * 0.5555556 + 273.0;
407   IdleFF = pow(MilThrust, 0.2) * 107.0;  // just an estimate
408
409   bindmodel();
410   return true;
411 }
412
413 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
414
415 string FGTurbine::GetEngineLabels(void)
416 {
417   std::ostringstream buf;
418
419   buf << Name << "_N1[" << EngineNumber << "], "
420       << Name << "_N2[" << EngineNumber << "], "
421       << Thruster->GetThrusterLabels(EngineNumber);
422
423   return buf.str();
424 }
425
426 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
427
428 string FGTurbine::GetEngineValues(void)
429 {
430   std::ostringstream buf;
431
432   buf << N1 << ", "
433       << N2 << ", "
434       << Thruster->GetThrusterValues(EngineNumber);
435
436   return buf.str();
437 }
438
439 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
440
441 void FGTurbine::bindmodel()
442 {
443   char property_name[80];
444
445   snprintf(property_name, 80, "propulsion/n1[%u]", EngineNumber);
446   PropertyManager->Tie( property_name, &N1);
447   snprintf(property_name, 80, "propulsion/n2[%u]", EngineNumber);
448   PropertyManager->Tie( property_name, &N2);
449 }
450
451 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
452
453 void FGTurbine::unbind()
454 {
455   char property_name[80];
456
457   snprintf(property_name, 80, "propulsion/n1[%u]", EngineNumber);
458   PropertyManager->Untie(property_name);
459   snprintf(property_name, 80, "propulsion/n2[%u]", EngineNumber);
460   PropertyManager->Untie(property_name);
461 }
462
463 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
464 //    The bitmasked value choices are as follows:
465 //    unset: In this case (the default) JSBSim would only print
466 //       out the normally expected messages, essentially echoing
467 //       the config files as they are read. If the environment
468 //       variable is not set, debug_lvl is set to 1 internally
469 //    0: This requests JSBSim not to output any messages
470 //       whatsoever.
471 //    1: This value explicity requests the normal JSBSim
472 //       startup messages
473 //    2: This value asks for a message to be printed out when
474 //       a class is instantiated
475 //    4: When this value is set, a message is displayed when a
476 //       FGModel object executes its Run() method
477 //    8: When this value is set, various runtime state variables
478 //       are printed out periodically
479 //    16: When set various parameters are sanity checked and
480 //       a message is printed out when they go out of bounds
481
482 void FGTurbine::Debug(int from)
483 {
484   if (debug_lvl <= 0) return;
485
486   if (debug_lvl & 1) { // Standard console startup message output
487     if (from == 0) { // Constructor
488
489     }
490     if (from == 2) { // called from Load()
491       cout << "\n    Engine Name: "         << Name << endl;
492       cout << "      MilThrust:   "         << MilThrust << endl;
493       cout << "      MaxThrust:   "         << MaxThrust << endl;
494       cout << "      BypassRatio: "         << BypassRatio << endl;
495       cout << "      TSFC:        "         << TSFC << endl;
496       cout << "      ATSFC:       "         << ATSFC << endl;
497       cout << "      IdleN1:      "         << IdleN1 << endl;
498       cout << "      IdleN2:      "         << IdleN2 << endl;
499       cout << "      MaxN1:       "         << MaxN1 << endl;
500       cout << "      MaxN2:       "         << MaxN2 << endl;
501       cout << "      Augmented:   "         << Augmented << endl;
502       cout << "      AugMethod:   "         << AugMethod << endl;
503       cout << "      Injected:    "         << Injected << endl;
504       cout << "      MinThrottle: "         << MinThrottle << endl;
505
506       cout << endl;
507     }
508   }
509   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
510     if (from == 0) cout << "Instantiated: FGTurbine" << endl;
511     if (from == 1) cout << "Destroyed:    FGTurbine" << endl;
512   }
513   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
514   }
515   if (debug_lvl & 8 ) { // Runtime state variables
516   }
517   if (debug_lvl & 16) { // Sanity checking
518   }
519   if (debug_lvl & 64) {
520     if (from == 0) { // Constructor
521       cout << IdSrc << endl;
522       cout << IdHdr << endl;
523     }
524   }
525 }
526 }