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