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