1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5 Date started: 09/12/2000
6 Purpose: This module models a rocket engine
8 ------------- Copyright (C) 2000 Jon S. Berndt (jsb@hal-pc.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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
47 static const char *IdSrc = "$Id$";
48 static const char *IdHdr = ID_ROCKET;
50 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
52 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
54 FGRocket::FGRocket(FGFDMExec* exec, Element *el, int engine_number)
55 : FGEngine(exec, el, engine_number)
57 Element* thrust_table_element = 0;
60 previousFuelNeedPerTank = 0.0;
61 previousOxiNeedPerTank = 0.0;
62 PropellantFlowRate = 0.0;
64 OxidizerFlowRate = 0.0;
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");
83 thrust_table_element = el->FindElement("thrust_table");
84 if (thrust_table_element) {
85 ThrustTable = new FGTable(PropertyManager, thrust_table_element);
97 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
99 FGRocket::~FGRocket(void)
104 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
106 double FGRocket::Calculate(void)
108 double dT = State->Getdt()*Propulsion->GetRate();
111 if (!Flameout && !Starved) ConsumeFuel();
113 PropellantFlowRate = (FuelExpended + OxidizerExpended)/dT;
114 Throttle = FCS->GetThrottlePos(EngineNumber);
116 // If there is a thrust table, it is a function of propellant remaining. The
117 // engine is started when the throttle is advanced to 1.0. After that, it
118 // burns without regard to throttle setting. The table returns a value between
119 // zero and one, representing the percentage of maximum vacuum thrust being
122 if (ThrustTable != 0L) { // Thrust table given -> Solid fuel used
124 if ((Throttle == 1 || BurnTime > 0.0 ) && !Starved) {
125 BurnTime += State->Getdt();
126 double TotalEngineFuelAvailable=0.0;
127 for (int i=0; i<(int)SourceTanks.size(); i++)
128 TotalEngineFuelAvailable += Propulsion->GetTank(SourceTanks[i])->GetContents();
130 VacThrust = ThrustTable->GetValue(TotalEngineFuelAvailable);
135 } else { // liquid fueled rocket assumed
137 if (Throttle < MinThrottle || Starved) { // Combustion not supported
139 PctPower = 0.0; // desired thrust
143 } else { // Calculate thrust
145 PctPower = Throttle / MaxThrottle; // Min and MaxThrottle range from 0.0 to 1.0, normally.
147 VacThrust = Isp * PropellantFlowRate;
151 } // End thrust calculations
153 thrust = Thruster->Calculate(VacThrust);
159 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
160 // This overrides the base class ConsumeFuel() function, for special rocket
161 // engine processing.
163 void FGRocket::ConsumeFuel(void)
167 bool haveOxTanks = false;
168 double Fshortage=0, Oshortage=0, TanksWithFuel=0, TanksWithOxidizer=0;
170 if (FuelFreeze) return;
171 if (TrimMode) return;
173 // Count how many assigned tanks have fuel for this engine at this time.
174 // If there is/are fuel tanks but no oxidizer tanks, this indicates
175 // a solid rocket is being modeled.
177 for (i=0; i<SourceTanks.size(); i++) {
178 Tank = Propulsion->GetTank(SourceTanks[i]);
179 switch(Tank->GetType()) {
181 if (Tank->GetContents() > 0.0 && Tank->GetSelected()) ++TanksWithFuel;
183 case FGTank::ttOXIDIZER:
185 if (Tank->GetContents() > 0.0 && Tank->GetSelected()) ++TanksWithOxidizer;
190 // If this engine has burned out, it is starved.
192 if (TanksWithFuel==0 || (haveOxTanks && TanksWithOxidizer==0)) {
197 // Expend fuel from the engine's tanks if the tank is selected as a source
200 double fuelNeedPerTank = CalcFuelNeed()/TanksWithFuel;
201 double oxiNeedPerTank = CalcOxidizerNeed()/TanksWithOxidizer;
203 for (i=0; i<SourceTanks.size(); i++) {
204 Tank = Propulsion->GetTank(SourceTanks[i]);
205 if ( ! Tank->GetSelected()) continue; // If this tank is not selected as a source, skip it.
206 switch(Tank->GetType()) {
208 Fshortage += Tank->Drain(2.0*fuelNeedPerTank - previousFuelNeedPerTank);
209 previousFuelNeedPerTank = fuelNeedPerTank;
211 case FGTank::ttOXIDIZER:
212 Oshortage += Tank->Drain(2.0*oxiNeedPerTank - previousOxiNeedPerTank);
213 previousOxiNeedPerTank = oxiNeedPerTank;
218 if (Fshortage < 0.00 || (haveOxTanks && Oshortage < 0.00)) Starved = true;
219 else Starved = false;
222 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
224 double FGRocket::CalcFuelNeed(void)
226 double dT = State->Getdt()*Propulsion->GetRate();
228 if (ThrustTable != 0L) { // Thrust table given - infers solid fuel
229 FuelFlowRate = VacThrust/Isp; // This calculates wdot (weight flow rate in lbs/sec)
231 FuelFlowRate = SLFuelFlowMax*PctPower;
234 FuelExpended = FuelFlowRate*dT; // For this time step ...
238 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
240 double FGRocket::CalcOxidizerNeed(void)
242 double dT = State->Getdt()*Propulsion->GetRate();
243 OxidizerFlowRate = SLOxiFlowMax*PctPower;
244 OxidizerExpended = OxidizerFlowRate*dT;
245 return OxidizerExpended;
248 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
250 string FGRocket::GetEngineLabels(string delimeter)
252 std::ostringstream buf;
254 buf << Name << " Total Impulse (engine " << EngineNumber << " in psf)" << delimeter
255 << Thruster->GetThrusterLabels(EngineNumber, delimeter);
260 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
262 string FGRocket::GetEngineValues(string delimeter)
264 std::ostringstream buf;
266 buf << It << delimeter << Thruster->GetThrusterValues(EngineNumber, delimeter);
271 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
272 // This funciton should tie properties to rocket engine specific properties
273 // that are not bound in the base class (FGEngine) code.
275 void FGRocket::bindmodel()
277 string property_name, base_property_name;
278 base_property_name = CreateIndexedPropertyName("propulsion/engine", EngineNumber);
280 property_name = base_property_name + "/total-impulse";
281 PropertyManager->Tie( property_name.c_str(), this, &FGRocket::GetTotalImpulse);
282 property_name = base_property_name + "/oxi-flow-rate-pps";
283 PropertyManager->Tie( property_name.c_str(), this, &FGRocket::GetOxiFlowRate);
284 property_name = base_property_name + "/vacuum-thrust_lbs";
285 PropertyManager->Tie( property_name.c_str(), this, &FGRocket::GetVacThrust);
288 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
289 // The bitmasked value choices are as follows:
290 // unset: In this case (the default) JSBSim would only print
291 // out the normally expected messages, essentially echoing
292 // the config files as they are read. If the environment
293 // variable is not set, debug_lvl is set to 1 internally
294 // 0: This requests JSBSim not to output any messages
296 // 1: This value explicity requests the normal JSBSim
298 // 2: This value asks for a message to be printed out when
299 // a class is instantiated
300 // 4: When this value is set, a message is displayed when a
301 // FGModel object executes its Run() method
302 // 8: When this value is set, various runtime state variables
303 // are printed out periodically
304 // 16: When set various parameters are sanity checked and
305 // a message is printed out when they go out of bounds
307 void FGRocket::Debug(int from)
309 if (debug_lvl <= 0) return;
311 if (debug_lvl & 1) { // Standard console startup message output
312 if (from == 0) { // Constructor
313 cout << " Engine Name: " << Name << endl;
314 cout << " Vacuum Isp = " << Isp << endl;
315 cout << " Maximum Throttle = " << MaxThrottle << endl;
316 cout << " Minimum Throttle = " << MinThrottle << endl;
317 cout << " Fuel Flow (max) = " << SLFuelFlowMax << endl;
318 cout << " Oxidizer Flow (max) = " << SLOxiFlowMax << endl;
319 cout << " Mixture ratio = " << SLOxiFlowMax/SLFuelFlowMax << endl;
322 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
323 if (from == 0) cout << "Instantiated: FGRocket" << endl;
324 if (from == 1) cout << "Destroyed: FGRocket" << endl;
326 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
328 if (debug_lvl & 8 ) { // Runtime state variables
330 if (debug_lvl & 16) { // Sanity checking
332 if (debug_lvl & 64) {
333 if (from == 0) { // Constructor
334 cout << IdSrc << endl;
335 cout << IdHdr << endl;