]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/models/propulsion/FGRocket.cpp
New version of JSBSim, a big rewrite.
[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.26 2011/08/04 13:45:42 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)
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 = 0.0;
71   BuildupTime = 0.0;
72   It = 0.0;
73   ThrustVariation = 0.0;
74   TotalIspVariation = 0.0;
75   Flameout = false;
76
77   // Defaults
78    MinThrottle = 0.0;
79    MaxThrottle = 1.0;
80
81   if (el->FindElement("isp"))
82     Isp = el->FindElementValueAsNumber("isp");
83   if (el->FindElement("builduptime"))
84     BuildupTime = el->FindElementValueAsNumber("builduptime");
85   if (el->FindElement("maxthrottle"))
86     MaxThrottle = el->FindElementValueAsNumber("maxthrottle");
87   if (el->FindElement("minthrottle"))
88     MinThrottle = el->FindElementValueAsNumber("minthrottle");
89   if (el->FindElement("slfuelflowmax"))
90     SLFuelFlowMax = el->FindElementValueAsNumberConvertTo("slfuelflowmax", "LBS/SEC");
91   if (el->FindElement("sloxiflowmax"))
92     SLOxiFlowMax = el->FindElementValueAsNumberConvertTo("sloxiflowmax", "LBS/SEC");
93
94   // If there is a thrust table element, this is a solid propellant engine.
95   thrust_table_element = el->FindElement("thrust_table");
96   if (thrust_table_element) {
97     ThrustTable = new FGTable(PropertyManager, thrust_table_element);
98     Element* variation_element = el->FindElement("variation");
99     if (variation_element) {
100       if (variation_element->FindElement("thrust")) {
101         ThrustVariation = variation_element->FindElementValueAsNumber("thrust");
102       }
103       if (variation_element->FindElement("total_isp")) {
104         TotalIspVariation = variation_element->FindElementValueAsNumber("total_isp");
105       }
106     }
107   }
108
109   bindmodel();
110
111   Debug(0);
112 }
113
114 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
115
116 FGRocket::~FGRocket(void)
117 {
118   delete ThrustTable;
119   Debug(1);
120 }
121
122 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
123
124 void FGRocket::Calculate(void)
125 {
126   if (FDMExec->IntegrationSuspended()) return;
127
128   RunPreFunctions();
129
130   PropellantFlowRate = (FuelExpended + OxidizerExpended)/in.TotalDeltaT;
131   TotalPropellantExpended += FuelExpended + OxidizerExpended;
132
133   // If there is a thrust table, it is a function of propellant burned. The
134   // engine is started when the throttle is advanced to 1.0. After that, it
135   // burns without regard to throttle setting.
136
137   if (ThrustTable != 0L) { // Thrust table given -> Solid fuel used
138
139     if ((in.ThrottlePos[EngineNumber] == 1 || BurnTime > 0.0 ) && !Starved) {
140
141       VacThrust = ThrustTable->GetValue(TotalPropellantExpended)
142                 * (ThrustVariation + 1)
143                 * (TotalIspVariation + 1);
144       if (BurnTime <= BuildupTime && BuildupTime > 0.0) {
145         VacThrust *= sin((BurnTime/BuildupTime)*M_PI/2.0);
146         // VacThrust *= (1-cos((BurnTime/BuildupTime)*M_PI))/2.0; // 1 - cos approach
147       }
148       BurnTime += in.TotalDeltaT; // Increment burn time
149     } else {
150       VacThrust = 0.0;
151     }
152
153   } else { // liquid fueled rocket assumed
154
155     if (in.ThrottlePos[EngineNumber] < MinThrottle || Starved) { // Combustion not supported
156
157       PctPower = 0.0; // desired thrust
158       Flameout = true;
159       VacThrust = 0.0;
160
161     } else { // Calculate thrust
162
163       // PctPower = Throttle / MaxThrottle; // Min and MaxThrottle range from 0.0 to 1.0, normally.
164       
165       PctPower = in.ThrottlePos[EngineNumber];
166       Flameout = false;
167       VacThrust = Isp * PropellantFlowRate;
168
169     }
170
171   } // End thrust calculations
172
173   LoadThrusterInputs();
174   It += Thruster->Calculate(VacThrust) * in.TotalDeltaT;
175
176   RunPostFunctions();
177 }
178
179 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
180 // 
181 // The FuelFlowRate can be affected by the TotalIspVariation value (settable
182 // in a config file or via properties). The TotalIspVariation parameter affects
183 // thrust, but the thrust determines fuel flow rate, so it must be adjusted
184 // for Total Isp Variation.
185
186 double FGRocket::CalcFuelNeed(void)
187 {
188   if (ThrustTable != 0L) {          // Thrust table given - infers solid fuel
189     FuelFlowRate = VacThrust/Isp;   // This calculates wdot (weight flow rate in lbs/sec)
190     FuelFlowRate /= (1 + TotalIspVariation);
191   } else {
192     FuelFlowRate = SLFuelFlowMax*PctPower;
193   }
194
195   FuelExpended = FuelFlowRate * in.TotalDeltaT; // For this time step ...
196   return FuelExpended;
197 }
198
199 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
200
201 double FGRocket::CalcOxidizerNeed(void)
202 {
203   OxidizerFlowRate = SLOxiFlowMax * PctPower;
204   OxidizerExpended = OxidizerFlowRate * in.TotalDeltaT;
205   return OxidizerExpended;
206 }
207
208 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
209
210 string FGRocket::GetEngineLabels(const string& delimiter)
211 {
212   std::ostringstream buf;
213
214   buf << Name << " Total Impulse (engine " << EngineNumber << " in psf)" << delimiter
215       << Thruster->GetThrusterLabels(EngineNumber, delimiter);
216
217   return buf.str();
218 }
219
220 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
221
222 string FGRocket::GetEngineValues(const string& delimiter)
223 {
224   std::ostringstream buf;
225
226   buf << It << delimiter << Thruster->GetThrusterValues(EngineNumber, delimiter);
227
228   return buf.str();
229 }
230
231 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
232 // This function should tie properties to rocket engine specific properties
233 // that are not bound in the base class (FGEngine) code.
234 //
235 void FGRocket::bindmodel()
236 {
237   string property_name, base_property_name;
238   base_property_name = CreateIndexedPropertyName("propulsion/engine", EngineNumber);
239
240   property_name = base_property_name + "/total-impulse";
241   PropertyManager->Tie( property_name.c_str(), this, &FGRocket::GetTotalImpulse);
242   property_name = base_property_name + "/vacuum-thrust_lbs";
243   PropertyManager->Tie( property_name.c_str(), this, &FGRocket::GetVacThrust);
244
245   if (ThrustTable) { // Solid rocket motor
246     property_name = base_property_name + "/thrust-variation_pct";
247     PropertyManager->Tie( property_name.c_str(), this, &FGRocket::GetThrustVariation,
248                                                        &FGRocket::SetThrustVariation);
249     property_name = base_property_name + "/total-isp-variation_pct";
250     PropertyManager->Tie( property_name.c_str(), this, &FGRocket::GetTotalIspVariation,
251                                                        &FGRocket::SetTotalIspVariation);
252   } else { // Liquid rocket motor
253     property_name = base_property_name + "/oxi-flow-rate-pps";
254     PropertyManager->Tie( property_name.c_str(), this, &FGRocket::GetOxiFlowRate);
255   }
256 }
257
258 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
259 //    The bitmasked value choices are as follows:
260 //    unset: In this case (the default) JSBSim would only print
261 //       out the normally expected messages, essentially echoing
262 //       the config files as they are read. If the environment
263 //       variable is not set, debug_lvl is set to 1 internally
264 //    0: This requests JSBSim not to output any messages
265 //       whatsoever.
266 //    1: This value explicity requests the normal JSBSim
267 //       startup messages
268 //    2: This value asks for a message to be printed out when
269 //       a class is instantiated
270 //    4: When this value is set, a message is displayed when a
271 //       FGModel object executes its Run() method
272 //    8: When this value is set, various runtime state variables
273 //       are printed out periodically
274 //    16: When set various parameters are sanity checked and
275 //       a message is printed out when they go out of bounds
276
277 void FGRocket::Debug(int from)
278 {
279   if (debug_lvl <= 0) return;
280
281   if (debug_lvl & 1) { // Standard console startup message output
282     if (from == 0) { // Constructor
283       cout << "      Engine Name: " << Name << endl;
284       cout << "      Vacuum Isp = " << Isp << endl;
285       cout << "      Maximum Throttle = " << MaxThrottle << endl;
286       cout << "      Minimum Throttle = " << MinThrottle << endl;
287       cout << "      Fuel Flow (max) = " << SLFuelFlowMax << endl;
288       cout << "      Oxidizer Flow (max) = " << SLOxiFlowMax << endl;
289       if (SLFuelFlowMax > 0)
290         cout << "      Mixture ratio = " << SLOxiFlowMax/SLFuelFlowMax << endl;
291     }
292   }
293   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
294     if (from == 0) cout << "Instantiated: FGRocket" << endl;
295     if (from == 1) cout << "Destroyed:    FGRocket" << endl;
296   }
297   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
298   }
299   if (debug_lvl & 8 ) { // Runtime state variables
300   }
301   if (debug_lvl & 16) { // Sanity checking
302   }
303   if (debug_lvl & 64) {
304     if (from == 0) { // Constructor
305       cout << IdSrc << endl;
306       cout << IdHdr << endl;
307     }
308   }
309 }
310 }