]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/models/propulsion/FGRocket.cpp
sync. with JSBSim CVS again
[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 (jon@jsbsim.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   delete ThrustTable;
102   Debug(1);
103 }
104
105 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
106
107 double FGRocket::Calculate(void)
108 {
109   double dT = State->Getdt()*Propulsion->GetRate();
110   double thrust;
111
112   if (!Flameout && !Starved) ConsumeFuel();
113
114   PropellantFlowRate = (FuelExpended + OxidizerExpended)/dT;
115   Throttle = FCS->GetThrottlePos(EngineNumber);
116
117   // If there is a thrust table, it is a function of propellant remaining. The
118   // engine is started when the throttle is advanced to 1.0. After that, it
119   // burns without regard to throttle setting. The table returns a value between
120   // zero and one, representing the percentage of maximum vacuum thrust being
121   // applied.
122
123   if (ThrustTable != 0L) { // Thrust table given -> Solid fuel used
124
125     if ((Throttle == 1 || BurnTime > 0.0 ) && !Starved) {
126       BurnTime += State->Getdt();
127       double TotalEngineFuelAvailable=0.0;
128       for (int i=0; i<(int)SourceTanks.size(); i++) {
129         FGTank* tank = Propulsion->GetTank(i);
130         if (SourceTanks[i] == 1) {
131           TotalEngineFuelAvailable += tank->GetContents();
132         }
133       }
134
135       VacThrust = ThrustTable->GetValue(TotalEngineFuelAvailable);
136     } else {
137       VacThrust = 0.0;
138     }
139
140   } else { // liquid fueled rocket assumed
141
142     if (Throttle < MinThrottle || Starved) { // Combustion not supported
143
144       PctPower = 0.0; // desired thrust
145       Flameout = true;
146       VacThrust = 0.0;
147
148     } else { // Calculate thrust
149
150       PctPower = Throttle / MaxThrottle; // Min and MaxThrottle range from 0.0 to 1.0, normally.
151       Flameout = false;
152       VacThrust = Isp * PropellantFlowRate;
153
154     }
155
156   } // End thrust calculations
157
158   thrust = Thruster->Calculate(VacThrust);
159   It += thrust * dT;
160
161   return thrust;
162 }
163
164 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
165 // This overrides the base class ConsumeFuel() function, for special rocket
166 // engine processing.
167
168 void FGRocket::ConsumeFuel(void)
169 {
170   unsigned int i;
171   FGTank* Tank;
172   bool haveOxTanks = false;
173   double Fshortage=0, Oshortage=0, TanksWithFuel=0, TanksWithOxidizer=0;
174
175   if (FuelFreeze) return;
176   if (TrimMode) return;
177
178   // Count how many assigned tanks have fuel for this engine at this time.
179   // If there is/are fuel tanks but no oxidizer tanks, this indicates
180   // a solid rocket is being modeled.
181
182   for (i=0; i<SourceTanks.size(); i++) {
183     Tank = Propulsion->GetTank(SourceTanks[i]);
184     switch(Tank->GetType()) {
185       case FGTank::ttFUEL:
186         if (Tank->GetContents() > 0.0 && Tank->GetSelected()) ++TanksWithFuel;
187         break;
188       case FGTank::ttOXIDIZER:
189         haveOxTanks = true;
190         if (Tank->GetContents() > 0.0 && Tank->GetSelected()) ++TanksWithOxidizer;
191         break;
192     }
193   }
194
195   // If this engine has burned out, it is starved.
196
197   if (TanksWithFuel==0 || (haveOxTanks && TanksWithOxidizer==0)) {
198     Starved = true;
199     return;
200   }
201
202   // Expend fuel from the engine's tanks if the tank is selected as a source
203   // for this engine.
204
205   double fuelNeedPerTank = CalcFuelNeed()/TanksWithFuel;
206   double oxiNeedPerTank = CalcOxidizerNeed()/TanksWithOxidizer;
207
208   for (i=0; i<SourceTanks.size(); i++) {
209     Tank = Propulsion->GetTank(SourceTanks[i]);
210     if ( ! Tank->GetSelected()) continue; // If this tank is not selected as a source, skip it.
211     switch(Tank->GetType()) {
212       case FGTank::ttFUEL:
213         Fshortage += Tank->Drain(2.0*fuelNeedPerTank - previousFuelNeedPerTank);
214         previousFuelNeedPerTank = fuelNeedPerTank;
215         break;
216       case FGTank::ttOXIDIZER:
217         Oshortage += Tank->Drain(2.0*oxiNeedPerTank - previousOxiNeedPerTank);
218         previousOxiNeedPerTank = oxiNeedPerTank;
219         break;
220     }
221   }
222
223   if (Fshortage < 0.00 || (haveOxTanks && Oshortage < 0.00)) Starved = true;
224   else Starved = false;
225 }
226
227 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
228
229 double FGRocket::CalcFuelNeed(void)
230 {
231   double dT = State->Getdt()*Propulsion->GetRate();
232
233   if (ThrustTable != 0L) {          // Thrust table given - infers solid fuel
234     FuelFlowRate = VacThrust/Isp;   // This calculates wdot (weight flow rate in lbs/sec)
235   } else {
236     FuelFlowRate = SLFuelFlowMax*PctPower;
237   }
238
239   FuelExpended = FuelFlowRate*dT; // For this time step ...
240   return FuelExpended;
241 }
242
243 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
244
245 double FGRocket::CalcOxidizerNeed(void)
246 {
247   double dT = State->Getdt()*Propulsion->GetRate();
248   OxidizerFlowRate = SLOxiFlowMax*PctPower;
249   OxidizerExpended = OxidizerFlowRate*dT;
250   return OxidizerExpended;
251 }
252
253 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
254
255 string FGRocket::GetEngineLabels(string delimeter)
256 {
257   std::ostringstream buf;
258
259   buf << Name << " Total Impulse (engine " << EngineNumber << " in psf)" << delimeter
260       << Thruster->GetThrusterLabels(EngineNumber, delimeter);
261
262   return buf.str();
263 }
264
265 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
266
267 string FGRocket::GetEngineValues(string delimeter)
268 {
269   std::ostringstream buf;
270
271   buf << It << delimeter << Thruster->GetThrusterValues(EngineNumber, delimeter);
272
273   return buf.str();
274 }
275
276 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
277 // This funciton should tie properties to rocket engine specific properties
278 // that are not bound in the base class (FGEngine) code.
279 //
280 void FGRocket::bindmodel()
281 {
282   string property_name, base_property_name;
283   base_property_name = CreateIndexedPropertyName("propulsion/engine", EngineNumber);
284
285   property_name = base_property_name + "/total-impulse";
286   PropertyManager->Tie( property_name.c_str(), this, &FGRocket::GetTotalImpulse);
287   property_name = base_property_name + "/oxi-flow-rate-pps";
288   PropertyManager->Tie( property_name.c_str(), this, &FGRocket::GetOxiFlowRate);
289   property_name = base_property_name + "/vacuum-thrust_lbs";
290   PropertyManager->Tie( property_name.c_str(), this, &FGRocket::GetVacThrust);
291 }
292
293 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
294 //    The bitmasked value choices are as follows:
295 //    unset: In this case (the default) JSBSim would only print
296 //       out the normally expected messages, essentially echoing
297 //       the config files as they are read. If the environment
298 //       variable is not set, debug_lvl is set to 1 internally
299 //    0: This requests JSBSim not to output any messages
300 //       whatsoever.
301 //    1: This value explicity requests the normal JSBSim
302 //       startup messages
303 //    2: This value asks for a message to be printed out when
304 //       a class is instantiated
305 //    4: When this value is set, a message is displayed when a
306 //       FGModel object executes its Run() method
307 //    8: When this value is set, various runtime state variables
308 //       are printed out periodically
309 //    16: When set various parameters are sanity checked and
310 //       a message is printed out when they go out of bounds
311
312 void FGRocket::Debug(int from)
313 {
314   if (debug_lvl <= 0) return;
315
316   if (debug_lvl & 1) { // Standard console startup message output
317     if (from == 0) { // Constructor
318       cout << "      Engine Name: " << Name << endl;
319       cout << "      Vacuum Isp = " << Isp << endl;
320       cout << "      Maximum Throttle = " << MaxThrottle << endl;
321       cout << "      Minimum Throttle = " << MinThrottle << endl;
322       cout << "      Fuel Flow (max) = " << SLFuelFlowMax << endl;
323       cout << "      Oxidizer Flow (max) = " << SLOxiFlowMax << endl;
324       cout << "      Mixture ratio = " << SLOxiFlowMax/SLFuelFlowMax << endl;
325     }
326   }
327   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
328     if (from == 0) cout << "Instantiated: FGRocket" << endl;
329     if (from == 1) cout << "Destroyed:    FGRocket" << endl;
330   }
331   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
332   }
333   if (debug_lvl & 8 ) { // Runtime state variables
334   }
335   if (debug_lvl & 16) { // Sanity checking
336   }
337   if (debug_lvl & 64) {
338     if (from == 0) { // Constructor
339       cout << IdSrc << endl;
340       cout << IdHdr << endl;
341     }
342   }
343 }
344 }