]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/models/propulsion/FGRocket.cpp
Merge branch 'jmt/gps'
[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 "FGState.h"
45 #include "models/FGPropulsion.h"
46 #include "FGThruster.h"
47 #include "FGTank.h"
48
49 using namespace std;
50
51 namespace JSBSim {
52
53 static const char *IdSrc = "$Id$";
54 static const char *IdHdr = ID_ROCKET;
55
56 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
57 CLASS IMPLEMENTATION
58 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
59
60 FGRocket::FGRocket(FGFDMExec* exec, Element *el, int engine_number)
61   : FGEngine(exec, el, engine_number)
62 {
63   Element* thrust_table_element = 0;
64   ThrustTable = 0L;
65   BurnTime = 0.0;
66   previousFuelNeedPerTank = 0.0;
67   previousOxiNeedPerTank = 0.0;
68   PropellantFlowRate = 0.0;
69   FuelFlowRate = 0.0;
70   OxidizerFlowRate = 0.0;
71   SLOxiFlowMax = 0.0;
72   BuildupTime = 0.0;
73   It = 0.0;
74
75   // Defaults
76    MinThrottle = 0.0;
77    MaxThrottle = 1.0;
78
79   if (el->FindElement("isp"))
80     Isp = el->FindElementValueAsNumber("isp");
81   if (el->FindElement("builduptime"))
82     BuildupTime = el->FindElementValueAsNumber("builduptime");
83   if (el->FindElement("maxthrottle"))
84     MaxThrottle = el->FindElementValueAsNumber("maxthrottle");
85   if (el->FindElement("minthrottle"))
86     MinThrottle = el->FindElementValueAsNumber("minthrottle");
87   if (el->FindElement("slfuelflowmax"))
88     SLFuelFlowMax = el->FindElementValueAsNumberConvertTo("slfuelflowmax", "LBS/SEC");
89   if (el->FindElement("sloxiflowmax"))
90     SLOxiFlowMax = el->FindElementValueAsNumberConvertTo("sloxiflowmax", "LBS/SEC");
91
92   thrust_table_element = el->FindElement("thrust_table");
93   if (thrust_table_element) {
94     ThrustTable = new FGTable(PropertyManager, thrust_table_element);
95   }
96
97   bindmodel();
98
99   Debug(0);
100
101   Type = etRocket;
102   Flameout = false;
103
104 }
105
106 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
107
108 FGRocket::~FGRocket(void)
109 {
110   delete ThrustTable;
111   Debug(1);
112 }
113
114 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
115
116 double FGRocket::Calculate(void)
117 {
118   double dT = State->Getdt()*Propulsion->GetRate();
119   double thrust;
120
121   if (!Flameout && !Starved) ConsumeFuel();
122
123   PropellantFlowRate = (FuelExpended + OxidizerExpended)/dT;
124   Throttle = FCS->GetThrottlePos(EngineNumber);
125
126   // If there is a thrust table, it is a function of propellant burned. The
127   // engine is started when the throttle is advanced to 1.0. After that, it
128   // burns without regard to throttle setting.
129
130   if (ThrustTable != 0L) { // Thrust table given -> Solid fuel used
131
132     if ((Throttle == 1 || BurnTime > 0.0 ) && !Starved) {
133       double TotalEngineFuelBurned=0.0;
134       for (int i=0; i<(int)SourceTanks.size(); i++) {
135         FGTank* tank = Propulsion->GetTank(i);
136         if (SourceTanks[i] == 1) {
137           TotalEngineFuelBurned += tank->GetCapacity() - tank->GetContents();
138         }
139       }
140
141       VacThrust = ThrustTable->GetValue(TotalEngineFuelBurned);
142       if (BurnTime <= BuildupTime && BuildupTime > 0.0) {
143         VacThrust *= sin((BurnTime/BuildupTime)*M_PI/2.0);
144         // VacThrust *= (1-cos((BurnTime/BuildupTime)*M_PI))/2.0; // 1 - cos approach
145       }
146       BurnTime += State->Getdt(); // Increment burn time
147     } else {
148       VacThrust = 0.0;
149     }
150
151   } else { // liquid fueled rocket assumed
152
153     if (Throttle < MinThrottle || Starved) { // Combustion not supported
154
155       PctPower = 0.0; // desired thrust
156       Flameout = true;
157       VacThrust = 0.0;
158
159     } else { // Calculate thrust
160
161       // This is nonsensical. Max throttle should be assumed to be 1.0. One might
162       // conceivably have a throttle setting > 1.0 for some rocket engines. But, 1.0
163       // should always be the default.
164       // PctPower = Throttle / MaxThrottle; // Min and MaxThrottle range from 0.0 to 1.0, normally.
165       
166       PctPower = Throttle;
167       Flameout = false;
168       VacThrust = Isp * PropellantFlowRate;
169
170     }
171
172   } // End thrust calculations
173
174   thrust = Thruster->Calculate(VacThrust);
175   It += thrust * dT;
176
177   return thrust;
178 }
179
180 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
181 // This overrides the base class ConsumeFuel() function, for special rocket
182 // engine processing.
183
184 void FGRocket::ConsumeFuel(void)
185 {
186   unsigned int i;
187   FGTank* Tank;
188   bool haveOxTanks = false;
189   double Fshortage=0, Oshortage=0, TanksWithFuel=0, TanksWithOxidizer=0;
190
191   if (FuelFreeze) return;
192   if (TrimMode) return;
193
194   // Count how many assigned tanks have fuel for this engine at this time.
195   // If there is/are fuel tanks but no oxidizer tanks, this indicates
196   // a solid rocket is being modeled.
197
198   for (i=0; i<SourceTanks.size(); i++) {
199     Tank = Propulsion->GetTank(SourceTanks[i]);
200     switch(Tank->GetType()) {
201       case FGTank::ttFUEL:
202         if (Tank->GetContents() > 0.0 && Tank->GetSelected()) ++TanksWithFuel;
203         break;
204       case FGTank::ttOXIDIZER:
205         haveOxTanks = true;
206         if (Tank->GetContents() > 0.0 && Tank->GetSelected()) ++TanksWithOxidizer;
207         break;
208     }
209   }
210
211   // If this engine has burned out, it is starved.
212
213   if (TanksWithFuel==0 || (haveOxTanks && TanksWithOxidizer==0)) {
214     Starved = true;
215     return;
216   }
217
218   // Expend fuel from the engine's tanks if the tank is selected as a source
219   // for this engine.
220
221   double fuelNeedPerTank = CalcFuelNeed()/TanksWithFuel;
222   double oxiNeedPerTank = CalcOxidizerNeed()/TanksWithOxidizer;
223
224   for (i=0; i<SourceTanks.size(); i++) {
225     Tank = Propulsion->GetTank(SourceTanks[i]);
226     if ( ! Tank->GetSelected()) continue; // If this tank is not selected as a source, skip it.
227     switch(Tank->GetType()) {
228       case FGTank::ttFUEL:
229         Fshortage += Tank->Drain(2.0*fuelNeedPerTank - previousFuelNeedPerTank);
230         previousFuelNeedPerTank = fuelNeedPerTank;
231         break;
232       case FGTank::ttOXIDIZER:
233         Oshortage += Tank->Drain(2.0*oxiNeedPerTank - previousOxiNeedPerTank);
234         previousOxiNeedPerTank = oxiNeedPerTank;
235         break;
236     }
237   }
238
239   if (Fshortage < 0.00 || (haveOxTanks && Oshortage < 0.00)) Starved = true;
240   else Starved = false;
241 }
242
243 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
244
245 double FGRocket::CalcFuelNeed(void)
246 {
247   double dT = State->Getdt()*Propulsion->GetRate();
248
249   if (ThrustTable != 0L) {          // Thrust table given - infers solid fuel
250     FuelFlowRate = VacThrust/Isp;   // This calculates wdot (weight flow rate in lbs/sec)
251   } else {
252     FuelFlowRate = SLFuelFlowMax*PctPower;
253   }
254
255   FuelExpended = FuelFlowRate*dT; // For this time step ...
256   return FuelExpended;
257 }
258
259 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
260
261 double FGRocket::CalcOxidizerNeed(void)
262 {
263   double dT = State->Getdt()*Propulsion->GetRate();
264   OxidizerFlowRate = SLOxiFlowMax*PctPower;
265   OxidizerExpended = OxidizerFlowRate*dT;
266   return OxidizerExpended;
267 }
268
269 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
270
271 string FGRocket::GetEngineLabels(const string& delimiter)
272 {
273   std::ostringstream buf;
274
275   buf << Name << " Total Impulse (engine " << EngineNumber << " in psf)" << delimiter
276       << Thruster->GetThrusterLabels(EngineNumber, delimiter);
277
278   return buf.str();
279 }
280
281 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
282
283 string FGRocket::GetEngineValues(const string& delimiter)
284 {
285   std::ostringstream buf;
286
287   buf << It << delimiter << Thruster->GetThrusterValues(EngineNumber, delimiter);
288
289   return buf.str();
290 }
291
292 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
293 // This funciton should tie properties to rocket engine specific properties
294 // that are not bound in the base class (FGEngine) code.
295 //
296 void FGRocket::bindmodel()
297 {
298   string property_name, base_property_name;
299   base_property_name = CreateIndexedPropertyName("propulsion/engine", EngineNumber);
300
301   property_name = base_property_name + "/total-impulse";
302   PropertyManager->Tie( property_name.c_str(), this, &FGRocket::GetTotalImpulse);
303   property_name = base_property_name + "/oxi-flow-rate-pps";
304   PropertyManager->Tie( property_name.c_str(), this, &FGRocket::GetOxiFlowRate);
305   property_name = base_property_name + "/vacuum-thrust_lbs";
306   PropertyManager->Tie( property_name.c_str(), this, &FGRocket::GetVacThrust);
307 }
308
309 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
310 //    The bitmasked value choices are as follows:
311 //    unset: In this case (the default) JSBSim would only print
312 //       out the normally expected messages, essentially echoing
313 //       the config files as they are read. If the environment
314 //       variable is not set, debug_lvl is set to 1 internally
315 //    0: This requests JSBSim not to output any messages
316 //       whatsoever.
317 //    1: This value explicity requests the normal JSBSim
318 //       startup messages
319 //    2: This value asks for a message to be printed out when
320 //       a class is instantiated
321 //    4: When this value is set, a message is displayed when a
322 //       FGModel object executes its Run() method
323 //    8: When this value is set, various runtime state variables
324 //       are printed out periodically
325 //    16: When set various parameters are sanity checked and
326 //       a message is printed out when they go out of bounds
327
328 void FGRocket::Debug(int from)
329 {
330   if (debug_lvl <= 0) return;
331
332   if (debug_lvl & 1) { // Standard console startup message output
333     if (from == 0) { // Constructor
334       cout << "      Engine Name: " << Name << endl;
335       cout << "      Vacuum Isp = " << Isp << endl;
336       cout << "      Maximum Throttle = " << MaxThrottle << endl;
337       cout << "      Minimum Throttle = " << MinThrottle << endl;
338       cout << "      Fuel Flow (max) = " << SLFuelFlowMax << endl;
339       cout << "      Oxidizer Flow (max) = " << SLOxiFlowMax << endl;
340       cout << "      Mixture ratio = " << SLOxiFlowMax/SLFuelFlowMax << endl;
341     }
342   }
343   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
344     if (from == 0) cout << "Instantiated: FGRocket" << endl;
345     if (from == 1) cout << "Destroyed:    FGRocket" << endl;
346   }
347   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
348   }
349   if (debug_lvl & 8 ) { // Runtime state variables
350   }
351   if (debug_lvl & 16) { // Sanity checking
352   }
353   if (debug_lvl & 64) {
354     if (from == 0) { // Constructor
355       cout << IdSrc << endl;
356       cout << IdHdr << endl;
357     }
358   }
359 }
360 }