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