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