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