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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
44 #include "models/FGPropulsion.h"
45 #include "FGThruster.h"
52 static const char *IdSrc = "$Id: FGRocket.cpp,v 1.20 2010/08/21 17:13:48 jberndt Exp $";
53 static const char *IdHdr = ID_ROCKET;
55 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
57 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
59 FGRocket::FGRocket(FGFDMExec* exec, Element *el, int engine_number)
60 : FGEngine(exec, el, engine_number)
63 Element* thrust_table_element = 0;
66 previousFuelNeedPerTank = 0.0;
67 previousOxiNeedPerTank = 0.0;
68 PropellantFlowRate = 0.0;
69 FuelFlowRate = FuelExpended = 0.0;
70 OxidizerFlowRate = OxidizerExpended = 0.0;
71 SLOxiFlowMax = SLFuelFlowMax = 0.0;
74 ThrustVariation = 0.0;
75 TotalIspVariation = 0.0;
82 if (el->FindElement("isp"))
83 Isp = el->FindElementValueAsNumber("isp");
84 if (el->FindElement("builduptime"))
85 BuildupTime = el->FindElementValueAsNumber("builduptime");
86 if (el->FindElement("maxthrottle"))
87 MaxThrottle = el->FindElementValueAsNumber("maxthrottle");
88 if (el->FindElement("minthrottle"))
89 MinThrottle = el->FindElementValueAsNumber("minthrottle");
90 if (el->FindElement("slfuelflowmax"))
91 SLFuelFlowMax = el->FindElementValueAsNumberConvertTo("slfuelflowmax", "LBS/SEC");
92 if (el->FindElement("sloxiflowmax"))
93 SLOxiFlowMax = el->FindElementValueAsNumberConvertTo("sloxiflowmax", "LBS/SEC");
95 // If there is a thrust table element, this is a solid propellant engine.
96 thrust_table_element = el->FindElement("thrust_table");
97 if (thrust_table_element) {
98 ThrustTable = new FGTable(PropertyManager, thrust_table_element);
99 Element* variation_element = el->FindElement("variation");
100 if (variation_element) {
101 if (variation_element->FindElement("thrust")) {
102 ThrustVariation = variation_element->FindElementValueAsNumber("thrust");
104 if (variation_element->FindElement("total_isp")) {
105 TotalIspVariation = variation_element->FindElementValueAsNumber("total_isp");
115 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
117 FGRocket::~FGRocket(void)
123 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
125 void FGRocket::Calculate(void)
127 double dT = FDMExec->GetDeltaT()*Propulsion->GetRate();
131 if (!Flameout && !Starved) ConsumeFuel();
133 PropellantFlowRate = (FuelExpended + OxidizerExpended)/dT;
134 Throttle = FCS->GetThrottlePos(EngineNumber);
136 // If there is a thrust table, it is a function of propellant burned. The
137 // engine is started when the throttle is advanced to 1.0. After that, it
138 // burns without regard to throttle setting.
140 if (ThrustTable != 0L) { // Thrust table given -> Solid fuel used
142 if ((Throttle == 1 || BurnTime > 0.0 ) && !Starved) {
143 double TotalEngineFuelBurned=0.0;
144 for (int i=0; i<(int)SourceTanks.size(); i++) {
145 FGTank* tank = Propulsion->GetTank(i);
146 if (SourceTanks[i] == 1) {
147 TotalEngineFuelBurned += tank->GetCapacity() - tank->GetContents();
151 VacThrust = ThrustTable->GetValue(TotalEngineFuelBurned)
152 * (ThrustVariation + 1)
153 * (TotalIspVariation + 1);
154 if (BurnTime <= BuildupTime && BuildupTime > 0.0) {
155 VacThrust *= sin((BurnTime/BuildupTime)*M_PI/2.0);
156 // VacThrust *= (1-cos((BurnTime/BuildupTime)*M_PI))/2.0; // 1 - cos approach
158 BurnTime += FDMExec->GetDeltaT(); // Increment burn time
163 } else { // liquid fueled rocket assumed
165 if (Throttle < MinThrottle || Starved) { // Combustion not supported
167 PctPower = 0.0; // desired thrust
171 } else { // Calculate thrust
173 // This is nonsensical. Max throttle should be assumed to be 1.0. One might
174 // conceivably have a throttle setting > 1.0 for some rocket engines. But, 1.0
175 // should always be the default.
176 // PctPower = Throttle / MaxThrottle; // Min and MaxThrottle range from 0.0 to 1.0, normally.
180 VacThrust = Isp * PropellantFlowRate;
184 } // End thrust calculations
186 It += Thruster->Calculate(VacThrust) * dT;
191 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
192 // This overrides the base class ConsumeFuel() function, for special rocket
193 // engine processing.
195 void FGRocket::ConsumeFuel(void)
199 bool haveOxTanks = false;
200 double Fshortage=0, Oshortage=0, TanksWithFuel=0, TanksWithOxidizer=0;
202 if (FuelFreeze) return;
203 if (TrimMode) return;
205 // Count how many assigned tanks have fuel for this engine at this time.
206 // If there is/are fuel tanks but no oxidizer tanks, this indicates
207 // a solid rocket is being modeled.
209 for (i=0; i<SourceTanks.size(); i++) {
210 Tank = Propulsion->GetTank(i);
211 switch(Tank->GetType()) {
213 if (Tank->GetContents() > 0.0 && Tank->GetSelected() && SourceTanks[i] > 0) ++TanksWithFuel;
215 case FGTank::ttOXIDIZER:
216 if (Tank->GetContents() > 0.0 && Tank->GetSelected() && SourceTanks[i] > 0) {
224 // If this engine has burned out, it is starved.
226 if (TanksWithFuel==0 || (haveOxTanks && TanksWithOxidizer==0)) {
231 // Expend fuel from the engine's tanks if the tank is selected as a source
234 double fuelNeedPerTank = 0;
235 double oxiNeedPerTank = 0;
237 if (TanksWithFuel > 0) fuelNeedPerTank = CalcFuelNeed()/TanksWithFuel;
238 if (TanksWithOxidizer > 0) oxiNeedPerTank = CalcOxidizerNeed()/TanksWithOxidizer;
240 for (i=0; i<SourceTanks.size(); i++) {
241 Tank = Propulsion->GetTank(i);
242 if ( ! Tank->GetSelected() || SourceTanks[i] == 0) continue; // If this tank is not selected as a source, skip it.
243 switch(Tank->GetType()) {
245 Fshortage += Tank->Drain(2.0*fuelNeedPerTank - previousFuelNeedPerTank);
246 previousFuelNeedPerTank = fuelNeedPerTank;
248 case FGTank::ttOXIDIZER:
249 Oshortage += Tank->Drain(2.0*oxiNeedPerTank - previousOxiNeedPerTank);
250 previousOxiNeedPerTank = oxiNeedPerTank;
255 if (Fshortage < 0.00 || (haveOxTanks && Oshortage < 0.00)) Starved = true;
256 else Starved = false;
259 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
261 // The FuelFlowRate can be affected by the TotalIspVariation value (settable
262 // in a config file or via properties). The TotalIspVariation parameter affects
263 // thrust, but the thrust determines fuel flow rate, so it must be adjusted
264 // for Total Isp Variation.
266 double FGRocket::CalcFuelNeed(void)
268 double dT = FDMExec->GetDeltaT()*Propulsion->GetRate();
270 if (ThrustTable != 0L) { // Thrust table given - infers solid fuel
271 FuelFlowRate = VacThrust/Isp; // This calculates wdot (weight flow rate in lbs/sec)
272 FuelFlowRate /= (1 + TotalIspVariation);
274 FuelFlowRate = SLFuelFlowMax*PctPower;
277 FuelExpended = FuelFlowRate*dT; // For this time step ...
281 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
283 double FGRocket::CalcOxidizerNeed(void)
285 double dT = FDMExec->GetDeltaT()*Propulsion->GetRate();
286 OxidizerFlowRate = SLOxiFlowMax*PctPower;
287 OxidizerExpended = OxidizerFlowRate*dT;
288 return OxidizerExpended;
291 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
293 string FGRocket::GetEngineLabels(const string& delimiter)
295 std::ostringstream buf;
297 buf << Name << " Total Impulse (engine " << EngineNumber << " in psf)" << delimiter
298 << Thruster->GetThrusterLabels(EngineNumber, delimiter);
303 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
305 string FGRocket::GetEngineValues(const string& delimiter)
307 std::ostringstream buf;
309 buf << It << delimiter << Thruster->GetThrusterValues(EngineNumber, delimiter);
314 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
315 // This function should tie properties to rocket engine specific properties
316 // that are not bound in the base class (FGEngine) code.
318 void FGRocket::bindmodel()
320 string property_name, base_property_name;
321 base_property_name = CreateIndexedPropertyName("propulsion/engine", EngineNumber);
323 property_name = base_property_name + "/total-impulse";
324 PropertyManager->Tie( property_name.c_str(), this, &FGRocket::GetTotalImpulse);
325 property_name = base_property_name + "/vacuum-thrust_lbs";
326 PropertyManager->Tie( property_name.c_str(), this, &FGRocket::GetVacThrust);
328 if (ThrustTable) { // Solid rocket motor
329 property_name = base_property_name + "/thrust-variation_pct";
330 PropertyManager->Tie( property_name.c_str(), this, &FGRocket::GetThrustVariation,
331 &FGRocket::SetThrustVariation);
332 property_name = base_property_name + "/total-isp-variation_pct";
333 PropertyManager->Tie( property_name.c_str(), this, &FGRocket::GetTotalIspVariation,
334 &FGRocket::SetTotalIspVariation);
335 } else { // Liquid rocket motor
336 property_name = base_property_name + "/oxi-flow-rate-pps";
337 PropertyManager->Tie( property_name.c_str(), this, &FGRocket::GetOxiFlowRate);
341 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
342 // The bitmasked value choices are as follows:
343 // unset: In this case (the default) JSBSim would only print
344 // out the normally expected messages, essentially echoing
345 // the config files as they are read. If the environment
346 // variable is not set, debug_lvl is set to 1 internally
347 // 0: This requests JSBSim not to output any messages
349 // 1: This value explicity requests the normal JSBSim
351 // 2: This value asks for a message to be printed out when
352 // a class is instantiated
353 // 4: When this value is set, a message is displayed when a
354 // FGModel object executes its Run() method
355 // 8: When this value is set, various runtime state variables
356 // are printed out periodically
357 // 16: When set various parameters are sanity checked and
358 // a message is printed out when they go out of bounds
360 void FGRocket::Debug(int from)
362 if (debug_lvl <= 0) return;
364 if (debug_lvl & 1) { // Standard console startup message output
365 if (from == 0) { // Constructor
366 cout << " Engine Name: " << Name << endl;
367 cout << " Vacuum Isp = " << Isp << endl;
368 cout << " Maximum Throttle = " << MaxThrottle << endl;
369 cout << " Minimum Throttle = " << MinThrottle << endl;
370 cout << " Fuel Flow (max) = " << SLFuelFlowMax << endl;
371 cout << " Oxidizer Flow (max) = " << SLOxiFlowMax << endl;
372 if (SLFuelFlowMax > 0)
373 cout << " Mixture ratio = " << SLOxiFlowMax/SLFuelFlowMax << endl;
376 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
377 if (from == 0) cout << "Instantiated: FGRocket" << endl;
378 if (from == 1) cout << "Destroyed: FGRocket" << endl;
380 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
382 if (debug_lvl & 8 ) { // Runtime state variables
384 if (debug_lvl & 16) { // Sanity checking
386 if (debug_lvl & 64) {
387 if (from == 0) { // Constructor
388 cout << IdSrc << endl;
389 cout << IdHdr << endl;