]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/models/propulsion/FGRocket.cpp
Merge branch 'next' of gitorious.org:fg/flightgear into next
[flightgear.git] / src / FDM / JSBSim / models / propulsion / 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 #include "FGRocket.h"
44 #include "FGThruster.h"
45
46 using namespace std;
47
48 namespace JSBSim {
49
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;
52
53 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
54 CLASS IMPLEMENTATION
55 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
56
57 FGRocket::FGRocket(FGFDMExec* exec, Element *el, int engine_number, struct Inputs& input)
58   : FGEngine(exec, el, engine_number, input), isp_function(0L)
59 {
60   Type = etRocket;
61   Element* thrust_table_element = 0;
62   ThrustTable = 0L;
63   BurnTime = 0.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;
71   MxR = 0.0;
72   BuildupTime = 0.0;
73   It = 0.0;
74   ThrustVariation = 0.0;
75   TotalIspVariation = 0.0;
76   Flameout = false;
77
78   // Defaults
79    MinThrottle = 0.0;
80    MaxThrottle = 1.0;
81
82   string base_property_name = CreateIndexedPropertyName("propulsion/engine", EngineNumber);
83
84   std::stringstream strEngineNumber;
85   strEngineNumber << EngineNumber;
86
87   Element* isp_el = el->FindElement("isp");
88   Element* isp_func_el=0;
89
90   bindmodel(); // Bind model properties first, since they might be needed in functions.
91
92   // Specific impulse may be specified as a constant value or as a function - perhaps as a function of mixture ratio.
93   if (isp_el) {
94     isp_func_el = isp_el->FindElement("function");
95     if (isp_func_el) {
96       isp_function = new FGFunction(exec->GetPropertyManager(),isp_func_el, strEngineNumber.str());
97     } else {
98     Isp = el->FindElementValueAsNumber("isp");
99     }
100   } else {
101     throw("Specific Impulse <isp> must be specified for a rocket engine");
102   }
103   
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");
110
111   if (el->FindElement("slfuelflowmax")) {
112     SLFuelFlowMax = el->FindElementValueAsNumberConvertTo("slfuelflowmax", "LBS/SEC");
113     if (el->FindElement("sloxiflowmax")) {
114     SLOxiFlowMax = el->FindElementValueAsNumberConvertTo("sloxiflowmax", "LBS/SEC");
115     }
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");
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
148 FGRocket::~FGRocket(void)
149 {
150   delete ThrustTable;
151   Debug(1);
152 }
153
154 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
155
156 void FGRocket::Calculate(void)
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, otherwise
165   // 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
211   RunPostFunctions();
212 }
213
214 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
215 // 
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.
220
221 double FGRocket::CalcFuelNeed(void)
222 {
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);
226   } else {
227     SLFuelFlowMax = PropFlowMax / (1 + MxR);
228     FuelFlowRate = SLFuelFlowMax * PctPower;
229   }
230
231   FuelExpended = FuelFlowRate * in.TotalDeltaT; // For this time step ...
232   return FuelExpended;
233 }
234
235 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
236
237 double FGRocket::CalcOxidizerNeed(void)
238 {
239   SLOxiFlowMax = PropFlowMax * MxR / (1 + MxR);
240   OxidizerFlowRate = SLOxiFlowMax * PctPower;
241   OxidizerExpended = OxidizerFlowRate * in.TotalDeltaT;
242   return OxidizerExpended;
243 }
244
245 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
246
247 string FGRocket::GetEngineLabels(const string& delimiter)
248 {
249   std::ostringstream buf;
250
251   buf << Name << " Total Impulse (engine " << EngineNumber << " in psf)" << delimiter
252       << Thruster->GetThrusterLabels(EngineNumber, delimiter);
253
254   return buf.str();
255 }
256
257 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
258
259 string FGRocket::GetEngineValues(const string& delimiter)
260 {
261   std::ostringstream buf;
262
263   buf << It << delimiter << Thruster->GetThrusterValues(EngineNumber, delimiter);
264
265   return buf.str();
266 }
267
268 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
269 // This function should tie properties to rocket engine specific properties
270 // that are not bound in the base class (FGEngine) code.
271 //
272 void FGRocket::bindmodel()
273 {
274   string property_name, base_property_name;
275   base_property_name = CreateIndexedPropertyName("propulsion/engine", EngineNumber);
276
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);
281
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,
297                                                        &FGRocket::SetIsp);
298   }
299 }
300
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
308 //       whatsoever.
309 //    1: This value explicity requests the normal JSBSim
310 //       startup messages
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
319
320 void FGRocket::Debug(int from)
321 {
322   if (debug_lvl <= 0) return;
323
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;
334     }
335   }
336   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
337     if (from == 0) cout << "Instantiated: FGRocket" << endl;
338     if (from == 1) cout << "Destroyed:    FGRocket" << endl;
339   }
340   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
341   }
342   if (debug_lvl & 8 ) { // Runtime state variables
343   }
344   if (debug_lvl & 16) { // Sanity checking
345   }
346   if (debug_lvl & 64) {
347     if (from == 0) { // Constructor
348       cout << IdSrc << endl;
349       cout << IdHdr << endl;
350     }
351   }
352 }
353 }