1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5 Date started: 09/12/2000
6 Purpose: This module models a rocket engine
8 ------------- Copyright (C) 2000 Jon S. Berndt (jon@jsbsim.org) --------------
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
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
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.
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.
27 FUNCTIONAL DESCRIPTION
28 --------------------------------------------------------------------------------
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
34 --------------------------------------------------------------------------------
35 09/12/2000 JSB Created
37 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
39 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
45 #include "models/FGPropulsion.h"
46 #include "FGThruster.h"
53 static const char *IdSrc = "$Id$";
54 static const char *IdHdr = ID_ROCKET;
56 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
58 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
60 FGRocket::FGRocket(FGFDMExec* exec, Element *el, int engine_number)
61 : FGEngine(exec, el, engine_number)
63 Element* thrust_table_element = 0;
66 previousFuelNeedPerTank = 0.0;
67 previousOxiNeedPerTank = 0.0;
68 PropellantFlowRate = 0.0;
70 OxidizerFlowRate = 0.0;
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");
89 thrust_table_element = el->FindElement("thrust_table");
90 if (thrust_table_element) {
91 ThrustTable = new FGTable(PropertyManager, thrust_table_element);
103 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
105 FGRocket::~FGRocket(void)
111 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
113 double FGRocket::Calculate(void)
115 double dT = State->Getdt()*Propulsion->GetRate();
118 if (!Flameout && !Starved) ConsumeFuel();
120 PropellantFlowRate = (FuelExpended + OxidizerExpended)/dT;
121 Throttle = FCS->GetThrottlePos(EngineNumber);
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.
127 if (ThrustTable != 0L) { // Thrust table given -> Solid fuel used
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();
139 VacThrust = ThrustTable->GetValue(TotalEngineFuelBurned);
144 } else { // liquid fueled rocket assumed
146 if (Throttle < MinThrottle || Starved) { // Combustion not supported
148 PctPower = 0.0; // desired thrust
152 } else { // Calculate thrust
154 PctPower = Throttle / MaxThrottle; // Min and MaxThrottle range from 0.0 to 1.0, normally.
156 VacThrust = Isp * PropellantFlowRate;
160 } // End thrust calculations
162 thrust = Thruster->Calculate(VacThrust);
168 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
169 // This overrides the base class ConsumeFuel() function, for special rocket
170 // engine processing.
172 void FGRocket::ConsumeFuel(void)
176 bool haveOxTanks = false;
177 double Fshortage=0, Oshortage=0, TanksWithFuel=0, TanksWithOxidizer=0;
179 if (FuelFreeze) return;
180 if (TrimMode) return;
182 // Count how many assigned tanks have fuel for this engine at this time.
183 // If there is/are fuel tanks but no oxidizer tanks, this indicates
184 // a solid rocket is being modeled.
186 for (i=0; i<SourceTanks.size(); i++) {
187 Tank = Propulsion->GetTank(SourceTanks[i]);
188 switch(Tank->GetType()) {
190 if (Tank->GetContents() > 0.0 && Tank->GetSelected()) ++TanksWithFuel;
192 case FGTank::ttOXIDIZER:
194 if (Tank->GetContents() > 0.0 && Tank->GetSelected()) ++TanksWithOxidizer;
199 // If this engine has burned out, it is starved.
201 if (TanksWithFuel==0 || (haveOxTanks && TanksWithOxidizer==0)) {
206 // Expend fuel from the engine's tanks if the tank is selected as a source
209 double fuelNeedPerTank = CalcFuelNeed()/TanksWithFuel;
210 double oxiNeedPerTank = CalcOxidizerNeed()/TanksWithOxidizer;
212 for (i=0; i<SourceTanks.size(); i++) {
213 Tank = Propulsion->GetTank(SourceTanks[i]);
214 if ( ! Tank->GetSelected()) continue; // If this tank is not selected as a source, skip it.
215 switch(Tank->GetType()) {
217 Fshortage += Tank->Drain(2.0*fuelNeedPerTank - previousFuelNeedPerTank);
218 previousFuelNeedPerTank = fuelNeedPerTank;
220 case FGTank::ttOXIDIZER:
221 Oshortage += Tank->Drain(2.0*oxiNeedPerTank - previousOxiNeedPerTank);
222 previousOxiNeedPerTank = oxiNeedPerTank;
227 if (Fshortage < 0.00 || (haveOxTanks && Oshortage < 0.00)) Starved = true;
228 else Starved = false;
231 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
233 double FGRocket::CalcFuelNeed(void)
235 double dT = State->Getdt()*Propulsion->GetRate();
237 if (ThrustTable != 0L) { // Thrust table given - infers solid fuel
238 FuelFlowRate = VacThrust/Isp; // This calculates wdot (weight flow rate in lbs/sec)
240 FuelFlowRate = SLFuelFlowMax*PctPower;
243 FuelExpended = FuelFlowRate*dT; // For this time step ...
247 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
249 double FGRocket::CalcOxidizerNeed(void)
251 double dT = State->Getdt()*Propulsion->GetRate();
252 OxidizerFlowRate = SLOxiFlowMax*PctPower;
253 OxidizerExpended = OxidizerFlowRate*dT;
254 return OxidizerExpended;
257 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
259 string FGRocket::GetEngineLabels(const string& delimiter)
261 std::ostringstream buf;
263 buf << Name << " Total Impulse (engine " << EngineNumber << " in psf)" << delimiter
264 << Thruster->GetThrusterLabels(EngineNumber, delimiter);
269 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
271 string FGRocket::GetEngineValues(const string& delimiter)
273 std::ostringstream buf;
275 buf << It << delimiter << Thruster->GetThrusterValues(EngineNumber, delimiter);
280 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
281 // This funciton should tie properties to rocket engine specific properties
282 // that are not bound in the base class (FGEngine) code.
284 void FGRocket::bindmodel()
286 string property_name, base_property_name;
287 base_property_name = CreateIndexedPropertyName("propulsion/engine", EngineNumber);
289 property_name = base_property_name + "/total-impulse";
290 PropertyManager->Tie( property_name.c_str(), this, &FGRocket::GetTotalImpulse);
291 property_name = base_property_name + "/oxi-flow-rate-pps";
292 PropertyManager->Tie( property_name.c_str(), this, &FGRocket::GetOxiFlowRate);
293 property_name = base_property_name + "/vacuum-thrust_lbs";
294 PropertyManager->Tie( property_name.c_str(), this, &FGRocket::GetVacThrust);
297 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
298 // The bitmasked value choices are as follows:
299 // unset: In this case (the default) JSBSim would only print
300 // out the normally expected messages, essentially echoing
301 // the config files as they are read. If the environment
302 // variable is not set, debug_lvl is set to 1 internally
303 // 0: This requests JSBSim not to output any messages
305 // 1: This value explicity requests the normal JSBSim
307 // 2: This value asks for a message to be printed out when
308 // a class is instantiated
309 // 4: When this value is set, a message is displayed when a
310 // FGModel object executes its Run() method
311 // 8: When this value is set, various runtime state variables
312 // are printed out periodically
313 // 16: When set various parameters are sanity checked and
314 // a message is printed out when they go out of bounds
316 void FGRocket::Debug(int from)
318 if (debug_lvl <= 0) return;
320 if (debug_lvl & 1) { // Standard console startup message output
321 if (from == 0) { // Constructor
322 cout << " Engine Name: " << Name << endl;
323 cout << " Vacuum Isp = " << Isp << endl;
324 cout << " Maximum Throttle = " << MaxThrottle << endl;
325 cout << " Minimum Throttle = " << MinThrottle << endl;
326 cout << " Fuel Flow (max) = " << SLFuelFlowMax << endl;
327 cout << " Oxidizer Flow (max) = " << SLOxiFlowMax << endl;
328 cout << " Mixture ratio = " << SLOxiFlowMax/SLFuelFlowMax << endl;
331 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
332 if (from == 0) cout << "Instantiated: FGRocket" << endl;
333 if (from == 1) cout << "Destroyed: FGRocket" << endl;
335 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
337 if (debug_lvl & 8 ) { // Runtime state variables
339 if (debug_lvl & 16) { // Sanity checking
341 if (debug_lvl & 64) {
342 if (from == 0) { // Constructor
343 cout << IdSrc << endl;
344 cout << IdHdr << endl;