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 "FGThruster.h"
50 static const char *IdSrc = "$Id: FGRocket.cpp,v 1.27 2012/04/08 15:19:08 jberndt Exp $";
51 static const char *IdHdr = ID_ROCKET;
53 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
55 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
57 FGRocket::FGRocket(FGFDMExec* exec, Element *el, int engine_number, struct Inputs& input)
58 : FGEngine(exec, el, engine_number, input), isp_function(0L)
61 Element* thrust_table_element = 0;
64 previousFuelNeedPerTank = 0.0;
65 previousOxiNeedPerTank = 0.0;
66 PropellantFlowRate = 0.0;
67 TotalPropellantExpended = 0.0;
68 FuelFlowRate = FuelExpended = 0.0;
69 OxidizerFlowRate = OxidizerExpended = 0.0;
70 SLOxiFlowMax = SLFuelFlowMax = PropFlowMax = 0.0;
74 ThrustVariation = 0.0;
75 TotalIspVariation = 0.0;
82 string base_property_name = CreateIndexedPropertyName("propulsion/engine", EngineNumber);
84 std::stringstream strEngineNumber;
85 strEngineNumber << EngineNumber;
87 Element* isp_el = el->FindElement("isp");
88 Element* isp_func_el=0;
90 bindmodel(); // Bind model properties first, since they might be needed in functions.
92 // Specific impulse may be specified as a constant value or as a function - perhaps as a function of mixture ratio.
94 isp_func_el = isp_el->FindElement("function");
96 isp_function = new FGFunction(exec->GetPropertyManager(),isp_func_el, strEngineNumber.str());
98 Isp = el->FindElementValueAsNumber("isp");
101 throw("Specific Impulse <isp> must be specified for a rocket engine");
104 if (el->FindElement("builduptime"))
105 BuildupTime = el->FindElementValueAsNumber("builduptime");
106 if (el->FindElement("maxthrottle"))
107 MaxThrottle = el->FindElementValueAsNumber("maxthrottle");
108 if (el->FindElement("minthrottle"))
109 MinThrottle = el->FindElementValueAsNumber("minthrottle");
111 if (el->FindElement("slfuelflowmax")) {
112 SLFuelFlowMax = el->FindElementValueAsNumberConvertTo("slfuelflowmax", "LBS/SEC");
113 if (el->FindElement("sloxiflowmax")) {
114 SLOxiFlowMax = el->FindElementValueAsNumberConvertTo("sloxiflowmax", "LBS/SEC");
116 PropFlowMax = SLOxiFlowMax + SLFuelFlowMax;
117 MxR = SLOxiFlowMax/SLFuelFlowMax;
118 } else if (el->FindElement("propflowmax")) {
119 PropFlowMax = el->FindElementValueAsNumberConvertTo("propflowmax", "LBS/SEC");
120 // Mixture ratio may be specified here, but it can also be specified as a function or via property
121 if (el->FindElement("mixtureratio")) {
122 MxR = el->FindElementValueAsNumber("mixtureratio");
126 if (isp_function) Isp = isp_function->GetValue(); // cause Isp function to be executed if present.
127 // If there is a thrust table element, this is a solid propellant engine.
128 thrust_table_element = el->FindElement("thrust_table");
129 if (thrust_table_element) {
130 ThrustTable = new FGTable(PropertyManager, thrust_table_element);
131 Element* variation_element = el->FindElement("variation");
132 if (variation_element) {
133 if (variation_element->FindElement("thrust")) {
134 ThrustVariation = variation_element->FindElementValueAsNumber("thrust");
136 if (variation_element->FindElement("total_isp")) {
137 TotalIspVariation = variation_element->FindElementValueAsNumber("total_isp");
146 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
148 FGRocket::~FGRocket(void)
154 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
156 void FGRocket::Calculate(void)
158 if (FDMExec->IntegrationSuspended()) return;
162 PropellantFlowRate = (FuelExpended + OxidizerExpended)/in.TotalDeltaT;
163 TotalPropellantExpended += FuelExpended + OxidizerExpended;
164 // If Isp has been specified as a function, override the value of Isp to that, otherwise
165 // assume a constant value is given.
166 if (isp_function) Isp = isp_function->GetValue();
168 // If there is a thrust table, it is a function of propellant burned. The
169 // engine is started when the throttle is advanced to 1.0. After that, it
170 // burns without regard to throttle setting.
172 if (ThrustTable != 0L) { // Thrust table given -> Solid fuel used
174 if ((in.ThrottlePos[EngineNumber] == 1 || BurnTime > 0.0 ) && !Starved) {
176 VacThrust = ThrustTable->GetValue(TotalPropellantExpended)
177 * (ThrustVariation + 1)
178 * (TotalIspVariation + 1);
179 if (BurnTime <= BuildupTime && BuildupTime > 0.0) {
180 VacThrust *= sin((BurnTime/BuildupTime)*M_PI/2.0);
181 // VacThrust *= (1-cos((BurnTime/BuildupTime)*M_PI))/2.0; // 1 - cos approach
183 BurnTime += in.TotalDeltaT; // Increment burn time
188 } else { // liquid fueled rocket assumed
190 if (in.ThrottlePos[EngineNumber] < MinThrottle || Starved) { // Combustion not supported
192 PctPower = 0.0; // desired thrust
196 } else { // Calculate thrust
198 // PctPower = Throttle / MaxThrottle; // Min and MaxThrottle range from 0.0 to 1.0, normally.
200 PctPower = in.ThrottlePos[EngineNumber];
202 VacThrust = Isp * PropellantFlowRate;
206 } // End thrust calculations
208 LoadThrusterInputs();
209 It += Thruster->Calculate(VacThrust) * in.TotalDeltaT;
214 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
216 // The FuelFlowRate can be affected by the TotalIspVariation value (settable
217 // in a config file or via properties). The TotalIspVariation parameter affects
218 // thrust, but the thrust determines fuel flow rate, so it must be adjusted
219 // for Total Isp Variation.
221 double FGRocket::CalcFuelNeed(void)
223 if (ThrustTable != 0L) { // Thrust table given - infers solid fuel
224 FuelFlowRate = VacThrust/Isp; // This calculates wdot (weight flow rate in lbs/sec)
225 FuelFlowRate /= (1 + TotalIspVariation);
227 SLFuelFlowMax = PropFlowMax / (1 + MxR);
228 FuelFlowRate = SLFuelFlowMax * PctPower;
231 FuelExpended = FuelFlowRate * in.TotalDeltaT; // For this time step ...
235 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
237 double FGRocket::CalcOxidizerNeed(void)
239 SLOxiFlowMax = PropFlowMax * MxR / (1 + MxR);
240 OxidizerFlowRate = SLOxiFlowMax * PctPower;
241 OxidizerExpended = OxidizerFlowRate * in.TotalDeltaT;
242 return OxidizerExpended;
245 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
247 string FGRocket::GetEngineLabels(const string& delimiter)
249 std::ostringstream buf;
251 buf << Name << " Total Impulse (engine " << EngineNumber << " in psf)" << delimiter
252 << Thruster->GetThrusterLabels(EngineNumber, delimiter);
257 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
259 string FGRocket::GetEngineValues(const string& delimiter)
261 std::ostringstream buf;
263 buf << It << delimiter << Thruster->GetThrusterValues(EngineNumber, delimiter);
268 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
269 // This function should tie properties to rocket engine specific properties
270 // that are not bound in the base class (FGEngine) code.
272 void FGRocket::bindmodel()
274 string property_name, base_property_name;
275 base_property_name = CreateIndexedPropertyName("propulsion/engine", EngineNumber);
277 property_name = base_property_name + "/total-impulse";
278 PropertyManager->Tie( property_name.c_str(), this, &FGRocket::GetTotalImpulse);
279 property_name = base_property_name + "/vacuum-thrust_lbs";
280 PropertyManager->Tie( property_name.c_str(), this, &FGRocket::GetVacThrust);
282 if (ThrustTable) { // Solid rocket motor
283 property_name = base_property_name + "/thrust-variation_pct";
284 PropertyManager->Tie( property_name.c_str(), this, &FGRocket::GetThrustVariation,
285 &FGRocket::SetThrustVariation);
286 property_name = base_property_name + "/total-isp-variation_pct";
287 PropertyManager->Tie( property_name.c_str(), this, &FGRocket::GetTotalIspVariation,
288 &FGRocket::SetTotalIspVariation);
289 } else { // Liquid rocket motor
290 property_name = base_property_name + "/oxi-flow-rate-pps";
291 PropertyManager->Tie( property_name.c_str(), this, &FGRocket::GetOxiFlowRate);
292 property_name = base_property_name + "/mixture-ratio";
293 PropertyManager->Tie( property_name.c_str(), this, &FGRocket::GetMixtureRatio,
294 &FGRocket::SetMixtureRatio);
295 property_name = base_property_name + "/isp";
296 PropertyManager->Tie( property_name.c_str(), this, &FGRocket::GetIsp,
301 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
302 // The bitmasked value choices are as follows:
303 // unset: In this case (the default) JSBSim would only print
304 // out the normally expected messages, essentially echoing
305 // the config files as they are read. If the environment
306 // variable is not set, debug_lvl is set to 1 internally
307 // 0: This requests JSBSim not to output any messages
309 // 1: This value explicity requests the normal JSBSim
311 // 2: This value asks for a message to be printed out when
312 // a class is instantiated
313 // 4: When this value is set, a message is displayed when a
314 // FGModel object executes its Run() method
315 // 8: When this value is set, various runtime state variables
316 // are printed out periodically
317 // 16: When set various parameters are sanity checked and
318 // a message is printed out when they go out of bounds
320 void FGRocket::Debug(int from)
322 if (debug_lvl <= 0) return;
324 if (debug_lvl & 1) { // Standard console startup message output
325 if (from == 0) { // Constructor
326 cout << " Engine Name: " << Name << endl;
327 cout << " Vacuum Isp = " << Isp << endl;
328 cout << " Maximum Throttle = " << MaxThrottle << endl;
329 cout << " Minimum Throttle = " << MinThrottle << endl;
330 cout << " Fuel Flow (max) = " << SLFuelFlowMax << endl;
331 cout << " Oxidizer Flow (max) = " << SLOxiFlowMax << endl;
332 if (SLFuelFlowMax > 0)
333 cout << " Mixture ratio = " << SLOxiFlowMax/SLFuelFlowMax << endl;
336 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
337 if (from == 0) cout << "Instantiated: FGRocket" << endl;
338 if (from == 1) cout << "Destroyed: FGRocket" << endl;
340 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
342 if (debug_lvl & 8 ) { // Runtime state variables
344 if (debug_lvl & 16) { // Sanity checking
346 if (debug_lvl & 64) {
347 if (from == 0) { // Constructor
348 cout << IdSrc << endl;
349 cout << IdHdr << endl;