JSBSim Flight Dynamics Model  1.0 (02 March 2017)
An Open Source Flight Dynamics and Control Software Library in C++
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 
44 #include "FGRocket.h"
45 #include "FGThruster.h"
46 #include "input_output/FGXMLElement.h"
47 
48 using namespace std;
49 
50 namespace JSBSim {
51 
52 IDENT(IdSrc,"$Id: FGRocket.cpp,v 1.39 2015/09/27 09:54:21 bcoconni Exp $");
53 IDENT(IdHdr,ID_ROCKET);
54 
55 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
56 CLASS IMPLEMENTATION
57 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
58 
59 FGRocket::FGRocket(FGFDMExec* exec, Element *el, int engine_number, struct Inputs& input)
60  : FGEngine(engine_number, input), isp_function(0L), FDMExec(exec)
61 {
62  Load(exec, el);
63 
64  Type = etRocket;
65  Element* thrust_table_element = 0;
66  ThrustTable = 0L;
67  BurnTime = 0.0;
68  previousFuelNeedPerTank = 0.0;
69  previousOxiNeedPerTank = 0.0;
70  PropellantFlowRate = 0.0;
71  TotalPropellantExpended = 0.0;
72  FuelFlowRate = FuelExpended = 0.0;
73  OxidizerFlowRate = OxidizerExpended = 0.0;
74  SLOxiFlowMax = SLFuelFlowMax = PropFlowMax = 0.0;
75  MxR = 0.0;
76  BuildupTime = 0.0;
77  It = ItVac = 0.0;
78  ThrustVariation = 0.0;
79  TotalIspVariation = 0.0;
80  VacThrust = 0.0;
81  Flameout = false;
82 
83  // Defaults
84  MinThrottle = 0.0;
85  MaxThrottle = 1.0;
86 
87  std::stringstream strEngineNumber;
88  strEngineNumber << EngineNumber;
89 
90  FGPropertyManager* PropertyManager = exec->GetPropertyManager();
91  bindmodel(PropertyManager); // Bind model properties first, since they might be needed in functions.
92 
93  Element* isp_el = el->FindElement("isp");
94 
95  // Specific impulse may be specified as a constant value or as a function - perhaps as a function of mixture ratio.
96  if (isp_el) {
97  Element* isp_func_el = isp_el->FindElement("function");
98  if (isp_func_el) {
99  isp_function = new FGFunction(exec->GetPropertyManager(),isp_func_el, strEngineNumber.str());
100  } else {
101  Isp = el->FindElementValueAsNumber("isp");
102  }
103  } else {
104  throw("Specific Impulse <isp> must be specified for a rocket engine");
105  }
106 
107  if (el->FindElement("builduptime"))
108  BuildupTime = el->FindElementValueAsNumber("builduptime");
109  if (el->FindElement("maxthrottle"))
110  MaxThrottle = el->FindElementValueAsNumber("maxthrottle");
111  if (el->FindElement("minthrottle"))
112  MinThrottle = el->FindElementValueAsNumber("minthrottle");
113 
114  if (el->FindElement("slfuelflowmax")) {
115  SLFuelFlowMax = el->FindElementValueAsNumberConvertTo("slfuelflowmax", "LBS/SEC");
116  if (el->FindElement("sloxiflowmax")) {
117  SLOxiFlowMax = el->FindElementValueAsNumberConvertTo("sloxiflowmax", "LBS/SEC");
118  }
119  PropFlowMax = SLOxiFlowMax + SLFuelFlowMax;
120  MxR = SLOxiFlowMax/SLFuelFlowMax;
121  } else if (el->FindElement("propflowmax")) {
122  PropFlowMax = el->FindElementValueAsNumberConvertTo("propflowmax", "LBS/SEC");
123  // Mixture ratio may be specified here, but it can also be specified as a function or via property
124  if (el->FindElement("mixtureratio")) {
125  MxR = el->FindElementValueAsNumber("mixtureratio");
126  }
127  }
128 
129  if (isp_function) Isp = isp_function->GetValue(); // cause Isp function to be executed if present.
130  // If there is a thrust table element, this is a solid propellant engine.
131  thrust_table_element = el->FindElement("thrust_table");
132  if (thrust_table_element) {
133  ThrustTable = new FGTable(PropertyManager, thrust_table_element);
134  Element* variation_element = el->FindElement("variation");
135  if (variation_element) {
136  if (variation_element->FindElement("thrust")) {
137  ThrustVariation = variation_element->FindElementValueAsNumber("thrust");
138  }
139  if (variation_element->FindElement("total_isp")) {
140  TotalIspVariation = variation_element->FindElementValueAsNumber("total_isp");
141  }
142  }
143  }
144 
145 
146  Debug(0);
147 }
148 
149 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
150 
152 {
153  delete ThrustTable;
154  Debug(1);
155 }
156 
157 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
158 
160 {
161  if (FDMExec->IntegrationSuspended()) return;
162 
163  RunPreFunctions();
164 
165  PropellantFlowRate = (FuelExpended + OxidizerExpended)/in.TotalDeltaT;
166  TotalPropellantExpended += FuelExpended + OxidizerExpended;
167  // If Isp has been specified as a function, override the value of Isp to that, otherwise
168  // assume a constant value is given.
169  if (isp_function) Isp = isp_function->GetValue();
170 
171  // If there is a thrust table, it is a function of propellant burned. The
172  // engine is started when the throttle is advanced to 1.0. After that, it
173  // burns without regard to throttle setting.
174 
175  if (ThrustTable != 0L) { // Thrust table given -> Solid fuel used
176 
177  if ((in.ThrottlePos[EngineNumber] == 1 || BurnTime > 0.0 ) && !Starved) {
178 
179  VacThrust = ThrustTable->GetValue(TotalPropellantExpended)
180  * (ThrustVariation + 1)
181  * (TotalIspVariation + 1);
182  if (BurnTime <= BuildupTime && BuildupTime > 0.0) {
183  VacThrust *= sin((BurnTime/BuildupTime)*M_PI/2.0);
184  // VacThrust *= (1-cos((BurnTime/BuildupTime)*M_PI))/2.0; // 1 - cos approach
185  }
186  BurnTime += in.TotalDeltaT; // Increment burn time
187  } else {
188  VacThrust = 0.0;
189  }
190 
191  } else { // liquid fueled rocket assumed
192 
193  if (in.ThrottlePos[EngineNumber] < MinThrottle || Starved) { // Combustion not supported
194 
195  PctPower = 0.0; // desired thrust
196  Flameout = true;
197  VacThrust = 0.0;
198 
199  } else { // Calculate thrust
200 
201  // PctPower = Throttle / MaxThrottle; // Min and MaxThrottle range from 0.0 to 1.0, normally.
202 
203  PctPower = in.ThrottlePos[EngineNumber];
204  Flameout = false;
205  VacThrust = Isp * PropellantFlowRate;
206 
207  }
208 
209  } // End thrust calculations
210 
211  LoadThrusterInputs();
212  It += Thruster->Calculate(VacThrust) * in.TotalDeltaT;
213  ItVac += VacThrust * in.TotalDeltaT;
214 
215  RunPostFunctions();
216 }
217 
218 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
219 //
220 // The FuelFlowRate can be affected by the TotalIspVariation value (settable
221 // in a config file or via properties). The TotalIspVariation parameter affects
222 // thrust, but the thrust determines fuel flow rate, so it must be adjusted
223 // for Total Isp Variation.
224 
226 {
227  if (ThrustTable != 0L) { // Thrust table given - infers solid fuel
228  FuelFlowRate = VacThrust/Isp; // This calculates wdot (weight flow rate in lbs/sec)
229  FuelFlowRate /= (1 + TotalIspVariation);
230  } else {
231  SLFuelFlowMax = PropFlowMax / (1 + MxR);
232  FuelFlowRate = SLFuelFlowMax * PctPower;
233  }
234 
235  FuelExpended = FuelFlowRate * in.TotalDeltaT; // For this time step ...
236  return FuelExpended;
237 }
238 
239 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
240 
242 {
243  SLOxiFlowMax = PropFlowMax * MxR / (1 + MxR);
244  OxidizerFlowRate = SLOxiFlowMax * PctPower;
245  OxidizerExpended = OxidizerFlowRate * in.TotalDeltaT;
246  return OxidizerExpended;
247 }
248 
249 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
250 
251 string FGRocket::GetEngineLabels(const string& delimiter)
252 {
253  std::ostringstream buf;
254 
255  buf << Name << " Total Impulse (engine " << EngineNumber << " in lbf)" << delimiter
256  << Name << " Total Vacuum Impulse (engine " << EngineNumber << " in lbf)" << delimiter
257  << Name << " Roll Moment (engine " << EngineNumber << " in ft-lbf)" << delimiter
258  << Name << " Pitch Moment (engine " << EngineNumber << " in ft-lbf)" << delimiter
259  << Name << " Yaw Moment (engine " << EngineNumber << " in ft-lbf)" << delimiter
260  << Name << " X Force (engine " << EngineNumber << " in lbf)" << delimiter
261  << Name << " Y Force (engine " << EngineNumber << " in lbf)" << delimiter
262  << Name << " Z Force (engine " << EngineNumber << " in lbf)" << delimiter
263  << Thruster->GetThrusterLabels(EngineNumber, delimiter);
264 
265  return buf.str();
266 }
267 
268 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
269 
270 string FGRocket::GetEngineValues(const string& delimiter)
271 {
272  std::ostringstream buf;
273 
274  buf << It << delimiter
275  << ItVac << delimiter
276  << GetMoments().Dump(delimiter) << delimiter
277  << Thruster->GetBodyForces().Dump(delimiter) << delimiter
278  << Thruster->GetThrusterValues(EngineNumber, delimiter);
279 
280  return buf.str();
281 }
282 
283 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
284 // This function should tie properties to rocket engine specific properties
285 // that are not bound in the base class (FGEngine) code.
286 //
287 void FGRocket::bindmodel(FGPropertyManager* PropertyManager)
288 {
289  string property_name, base_property_name;
290  base_property_name = CreateIndexedPropertyName("propulsion/engine", EngineNumber);
291 
292  property_name = base_property_name + "/total-impulse";
293  PropertyManager->Tie( property_name.c_str(), this, &FGRocket::GetTotalImpulse);
294  property_name = base_property_name + "/total-vac-impulse";
295  PropertyManager->Tie( property_name.c_str(), this, &FGRocket::GetVacTotalImpulse);
296  property_name = base_property_name + "/vacuum-thrust_lbs";
297  PropertyManager->Tie( property_name.c_str(), this, &FGRocket::GetVacThrust);
298 
299  if (ThrustTable) { // Solid rocket motor
300  property_name = base_property_name + "/thrust-variation_pct";
301  PropertyManager->Tie( property_name.c_str(), this, &FGRocket::GetThrustVariation,
303  property_name = base_property_name + "/total-isp-variation_pct";
304  PropertyManager->Tie( property_name.c_str(), this, &FGRocket::GetTotalIspVariation,
306  } else { // Liquid rocket motor
307  property_name = base_property_name + "/oxi-flow-rate-pps";
308  PropertyManager->Tie( property_name.c_str(), this, &FGRocket::GetOxiFlowRate);
309  property_name = base_property_name + "/mixture-ratio";
310  PropertyManager->Tie( property_name.c_str(), this, &FGRocket::GetMixtureRatio,
311  &FGRocket::SetMixtureRatio);
312  property_name = base_property_name + "/isp";
313  PropertyManager->Tie( property_name.c_str(), this, &FGRocket::GetIsp,
314  &FGRocket::SetIsp);
315  }
316 }
317 
318 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
319 // The bitmasked value choices are as follows:
320 // unset: In this case (the default) JSBSim would only print
321 // out the normally expected messages, essentially echoing
322 // the config files as they are read. If the environment
323 // variable is not set, debug_lvl is set to 1 internally
324 // 0: This requests JSBSim not to output any messages
325 // whatsoever.
326 // 1: This value explicity requests the normal JSBSim
327 // startup messages
328 // 2: This value asks for a message to be printed out when
329 // a class is instantiated
330 // 4: When this value is set, a message is displayed when a
331 // FGModel object executes its Run() method
332 // 8: When this value is set, various runtime state variables
333 // are printed out periodically
334 // 16: When set various parameters are sanity checked and
335 // a message is printed out when they go out of bounds
336 
337 void FGRocket::Debug(int from)
338 {
339  if (debug_lvl <= 0) return;
340 
341  if (debug_lvl & 1) { // Standard console startup message output
342  if (from == 0) { // Constructor
343  cout << " Engine Name: " << Name << endl;
344  cout << " Vacuum Isp = " << Isp << endl;
345  cout << " Maximum Throttle = " << MaxThrottle << endl;
346  cout << " Minimum Throttle = " << MinThrottle << endl;
347  cout << " Fuel Flow (max) = " << SLFuelFlowMax << endl;
348  cout << " Oxidizer Flow (max) = " << SLOxiFlowMax << endl;
349  if (SLFuelFlowMax > 0)
350  cout << " Mixture ratio = " << SLOxiFlowMax/SLFuelFlowMax << endl;
351  }
352  }
353  if (debug_lvl & 2 ) { // Instantiation/Destruction notification
354  if (from == 0) cout << "Instantiated: FGRocket" << endl;
355  if (from == 1) cout << "Destroyed: FGRocket" << endl;
356  }
357  if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
358  }
359  if (debug_lvl & 8 ) { // Runtime state variables
360  }
361  if (debug_lvl & 16) { // Sanity checking
362  }
363  if (debug_lvl & 64) {
364  if (from == 0) { // Constructor
365  cout << IdSrc << endl;
366  cout << IdHdr << endl;
367  }
368  }
369 }
370 }
~FGRocket(void)
Destructor.
Definition: FGRocket.cpp:151
double FindElementValueAsNumberConvertTo(const std::string &el, const std::string &target_units)
Searches for the named element and converts and returns the data belonging to it. ...
double GetTotalIspVariation(void) const
Returns the Total Isp variation, if any.
Definition: FGRocket.h:210
STL namespace.
double CalcFuelNeed(void)
The fuel need is calculated based on power levels and flow rate for that power level.
Definition: FGRocket.cpp:225
Element * FindElement(const std::string &el="")
Searches for a specified element.
double FindElementValueAsNumber(const std::string &el="")
Searches for the named element and returns the data belonging to it as a number.
void Calculate(void)
Determines the thrust.
Definition: FGRocket.cpp:159
double GetValue(void) const
Retrieves the value of the function object.
Definition: FGFunction.cpp:364
FGPropertyManager * GetPropertyManager(void)
Returns a pointer to the property manager object.
Definition: FGFDMExec.cpp:1099
std::string Dump(const std::string &delimeter) const
Prints the contents of the vector.
void Tie(const std::string &name, bool *pointer, bool useDefault=true)
Tie a property to an external bool variable.
double GetTotalImpulse(void) const
Gets the total impulse of the rocket.
Definition: FGRocket.h:161
void SetThrustVariation(double var)
Sets the thrust variation for a solid rocket engine.
Definition: FGRocket.h:195
Represents a mathematical function.
Definition: FGFunction.h:699
double GetThrustVariation(void) const
Returns the thrust variation, if any.
Definition: FGRocket.h:207
double CalcOxidizerNeed(void)
The oxidizer need is calculated based on power levels and flow rate for that power level...
Definition: FGRocket.cpp:241
bool IntegrationSuspended(void) const
Returns the simulation suspension state.
Definition: FGFDMExec.h:546
Base class for all engines.
Definition: FGEngine.h:121
void SetTotalIspVariation(double var)
Sets the variation in total motor energy.
Definition: FGRocket.h:204
Encapsulates the JSBSim simulation executive.
Definition: FGFDMExec.h:189
Lookup table class.
Definition: FGTable.h:243
double GetVacTotalImpulse(void) const
Gets the total impulse of the rocket.
Definition: FGRocket.h:165