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