]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/models/propulsion/FGRocket.cpp
Sync. with JSBSim CVS
[flightgear.git] / src / FDM / JSBSim / models / propulsion / FGRocket.cpp
1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2
3  Module:       FGRocket.cpp
4  Author:       Jon S. Berndt
5  Date started: 09/12/2000
6  Purpose:      This module models a rocket engine
7
8  ------------- Copyright (C) 2000  Jon S. Berndt (jsb@hal-pc.org) --------------
9
10  This program is free software; you can redistribute it and/or modify it under
11  the terms of the GNU Lesser 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 Lesser General Public License for more
18  details.
19
20  You should have received a copy of the GNU Lesser 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 Lesser 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 rocket engine based on
31 parameters given in the engine config file for this class
32
33 HISTORY
34 --------------------------------------------------------------------------------
35 09/12/2000  JSB  Created
36
37 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
38 INCLUDES
39 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
40
41 #include <sstream>
42
43 #include "FGRocket.h"
44
45 namespace JSBSim {
46
47 static const char *IdSrc = "$Id$";
48 static const char *IdHdr = ID_ROCKET;
49
50 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
51 CLASS IMPLEMENTATION
52 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
53
54 FGRocket::FGRocket(FGFDMExec* exec, Element *el, int engine_number)
55   : FGEngine(exec, el, engine_number)
56 {
57   Element* thrust_table_element = 0;
58   ThrustTable = 0L;
59   BurnTime = 0.0;
60   previousFuelNeedPerTank = 0.0;
61   previousOxiNeedPerTank = 0.0;
62   PropellantFlowRate = 0.0;
63   FuelFlowRate = 0.0;
64   OxidizerFlowRate = 0.0;
65   SLOxiFlowMax = 0.0;
66   It = 0.0;
67
68   // Defaults
69    MinThrottle = 0.0;
70    MaxThrottle = 1.0;
71
72   if (el->FindElement("isp"))
73     Isp = el->FindElementValueAsNumber("isp");
74   if (el->FindElement("maxthrottle"))
75     MaxThrottle = el->FindElementValueAsNumber("maxthrottle");
76   if (el->FindElement("minthrottle"))
77     MinThrottle = el->FindElementValueAsNumber("minthrottle");
78   if (el->FindElement("slfuelflowmax"))
79     SLFuelFlowMax = el->FindElementValueAsNumberConvertTo("slfuelflowmax", "LBS/SEC");
80   if (el->FindElement("sloxiflowmax"))
81     SLOxiFlowMax = el->FindElementValueAsNumberConvertTo("sloxiflowmax", "LBS/SEC");
82
83   thrust_table_element = el->FindElement("thrust_table");
84   if (thrust_table_element) {
85     ThrustTable = new FGTable(PropertyManager, thrust_table_element);
86   }
87
88   bindmodel();
89
90   Debug(0);
91
92   Type = etRocket;
93   Flameout = false;
94
95 }
96
97 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
98
99 FGRocket::~FGRocket(void)
100 {
101   Debug(1);
102 }
103
104 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
105
106 double FGRocket::Calculate(void)
107 {
108   double dT = State->Getdt()*Propulsion->GetRate();
109
110   if (!Flameout && !Starved) ConsumeFuel();
111
112   PropellantFlowRate = (FuelExpended + OxidizerExpended)/dT;
113   Throttle = FCS->GetThrottlePos(EngineNumber);
114
115   // If there is a thrust table, it is a function of propellant remaining. The
116   // engine is started when the throttle is advanced to 1.0. After that, it
117   // burns without regard to throttle setting. The table returns a value between
118   // zero and one, representing the percentage of maximum vacuum thrust being
119   // applied.
120
121   if (ThrustTable != 0L) { // Thrust table given -> Solid fuel used
122
123     if ((Throttle == 1 || BurnTime > 0.0 ) && !Starved) {
124       BurnTime += State->Getdt();
125       double TotalEngineFuelAvailable=0.0;
126       for (int i=0; i<(int)SourceTanks.size(); i++)
127         TotalEngineFuelAvailable += Propulsion->GetTank(SourceTanks[i])->GetContents();
128
129       VacThrust = ThrustTable->GetValue(TotalEngineFuelAvailable);
130     } else {
131       VacThrust = 0.0;
132     }
133
134   } else { // liquid fueled rocket assumed
135
136     if (Throttle < MinThrottle || Starved) { // Combustion not supported
137
138       PctPower = Thrust = 0.0; // desired thrust
139       Flameout = true;
140       VacThrust = 0.0;
141
142     } else { // Calculate thrust
143
144       PctPower = Throttle / MaxThrottle; // Min and MaxThrottle range from 0.0 to 1.0, normally.
145       Flameout = false;
146       VacThrust = Isp * PropellantFlowRate;
147
148     }
149
150   } // End thrust calculations
151
152   Thrust = Thruster->Calculate(VacThrust);
153   It += Thrust * dT;
154
155   return Thrust;
156 }
157
158 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
159 // This overrides the base class ConsumeFuel() function, for special rocket
160 // engine processing.
161
162 void FGRocket::ConsumeFuel(void)
163 {
164   unsigned int i;
165   FGTank* Tank;
166   bool haveOxTanks = false;
167   double Fshortage=0, Oshortage=0, TanksWithFuel=0, TanksWithOxidizer=0;
168
169   if (FuelFreeze) return;
170   if (TrimMode) return;
171
172   // Count how many assigned tanks have fuel for this engine at this time.
173   // If there is/are fuel tanks but no oxidizer tanks, this indicates
174   // a solid rocket is being modeled.
175
176   for (i=0; i<SourceTanks.size(); i++) {
177     Tank = Propulsion->GetTank(SourceTanks[i]);
178     switch(Tank->GetType()) {
179       case FGTank::ttFUEL:
180         if (Tank->GetContents() > 0.0 && Tank->GetSelected()) ++TanksWithFuel;
181         break;
182       case FGTank::ttOXIDIZER:
183         haveOxTanks = true;
184         if (Tank->GetContents() > 0.0 && Tank->GetSelected()) ++TanksWithOxidizer;
185         break;
186     }
187   }
188
189   // If this engine has burned out, it is starved.
190
191   if (TanksWithFuel==0 || (haveOxTanks && TanksWithOxidizer==0)) {
192     Starved = true;
193     return;
194   }
195
196   // Expend fuel from the engine's tanks if the tank is selected as a source
197   // for this engine.
198
199   double fuelNeedPerTank = CalcFuelNeed()/TanksWithFuel;
200   double oxiNeedPerTank = CalcOxidizerNeed()/TanksWithOxidizer;
201
202   for (i=0; i<SourceTanks.size(); i++) {
203     Tank = Propulsion->GetTank(SourceTanks[i]);
204     if ( ! Tank->GetSelected()) continue; // If this tank is not selected as a source, skip it.
205     switch(Tank->GetType()) {
206       case FGTank::ttFUEL:
207         Fshortage += Tank->Drain(2.0*fuelNeedPerTank - previousFuelNeedPerTank);
208         previousFuelNeedPerTank = fuelNeedPerTank;
209         break;
210       case FGTank::ttOXIDIZER:
211         Oshortage += Tank->Drain(2.0*oxiNeedPerTank - previousOxiNeedPerTank);
212         previousOxiNeedPerTank = oxiNeedPerTank;
213         break;
214     }
215   }
216
217   if (Fshortage < 0.00 || (haveOxTanks && Oshortage < 0.00)) Starved = true;
218   else Starved = false;
219 }
220
221 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
222
223 double FGRocket::CalcFuelNeed(void)
224 {
225   double dT = State->Getdt()*Propulsion->GetRate();
226
227   if (ThrustTable != 0L) {          // Thrust table given - infers solid fuel
228     FuelFlowRate = VacThrust/Isp;   // This calculates wdot (weight flow rate in lbs/sec)
229   } else {
230     FuelFlowRate = SLFuelFlowMax*PctPower;
231   }
232
233   FuelExpended = FuelFlowRate*dT; // For this time step ...
234   return FuelExpended;
235 }
236
237 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
238
239 double FGRocket::CalcOxidizerNeed(void)
240 {
241   double dT = State->Getdt()*Propulsion->GetRate();
242   OxidizerFlowRate = SLOxiFlowMax*PctPower;
243   OxidizerExpended = OxidizerFlowRate*dT;
244   return OxidizerExpended;
245 }
246
247 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
248
249 string FGRocket::GetEngineLabels(string delimeter)
250 {
251   std::ostringstream buf;
252
253   buf << Name << " Total Impulse (engine " << EngineNumber << " in psf)" << delimeter
254       << Thruster->GetThrusterLabels(EngineNumber, delimeter);
255
256   return buf.str();
257 }
258
259 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
260
261 string FGRocket::GetEngineValues(string delimeter)
262 {
263   std::ostringstream buf;
264
265   buf << It << delimeter << Thruster->GetThrusterValues(EngineNumber, delimeter);
266
267   return buf.str();
268 }
269
270 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
271 // This funciton should tie properties to rocket engine specific properties
272 // that are not bound in the base class (FGEngine) code.
273 //
274 void FGRocket::bindmodel()
275 {
276   char property_name[80];
277
278   snprintf(property_name, 80, "propulsion/engine[%u]/total-impulse", EngineNumber);
279   PropertyManager->Tie( property_name, this, &FGRocket::GetTotalImpulse);
280   snprintf(property_name, 80, "propulsion/engine[%u]/oxi-flow-rate-pps", EngineNumber);
281   PropertyManager->Tie( property_name, this, &FGRocket::GetOxiFlowRate);
282   snprintf(property_name, 80, "propulsion/engine[%u]/vacuum-thrust_lbs", EngineNumber);
283   PropertyManager->Tie( property_name, this, &FGRocket::GetVacThrust);
284 }
285
286 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
287 //    The bitmasked value choices are as follows:
288 //    unset: In this case (the default) JSBSim would only print
289 //       out the normally expected messages, essentially echoing
290 //       the config files as they are read. If the environment
291 //       variable is not set, debug_lvl is set to 1 internally
292 //    0: This requests JSBSim not to output any messages
293 //       whatsoever.
294 //    1: This value explicity requests the normal JSBSim
295 //       startup messages
296 //    2: This value asks for a message to be printed out when
297 //       a class is instantiated
298 //    4: When this value is set, a message is displayed when a
299 //       FGModel object executes its Run() method
300 //    8: When this value is set, various runtime state variables
301 //       are printed out periodically
302 //    16: When set various parameters are sanity checked and
303 //       a message is printed out when they go out of bounds
304
305 void FGRocket::Debug(int from)
306 {
307   if (debug_lvl <= 0) return;
308
309   if (debug_lvl & 1) { // Standard console startup message output
310     if (from == 0) { // Constructor
311       cout << "      Engine Name: " << Name << endl;
312       cout << "      Vacuum Isp = " << Isp << endl;
313       cout << "      Maximum Throttle = " << MaxThrottle << endl;
314       cout << "      Minimum Throttle = " << MinThrottle << endl;
315       cout << "      Fuel Flow (max) = " << SLFuelFlowMax << endl;
316       cout << "      Oxidizer Flow (max) = " << SLOxiFlowMax << endl;
317       cout << "      Mixture ratio = " << SLOxiFlowMax/SLFuelFlowMax << endl;
318     }
319   }
320   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
321     if (from == 0) cout << "Instantiated: FGRocket" << endl;
322     if (from == 1) cout << "Destroyed:    FGRocket" << endl;
323   }
324   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
325   }
326   if (debug_lvl & 8 ) { // Runtime state variables
327   }
328   if (debug_lvl & 16) { // Sanity checking
329   }
330   if (debug_lvl & 64) {
331     if (from == 0) { // Constructor
332       cout << IdSrc << endl;
333       cout << IdHdr << endl;
334     }
335   }
336 }
337 }