]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/models/propulsion/FGEngine.cpp
Removed code from ConsumeFuel() which failed due to rounding error.
[flightgear.git] / src / FDM / JSBSim / models / propulsion / FGEngine.cpp
1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2
3  Module:       FGEngine.cpp
4  Author:       Jon Berndt
5  Date started: 01/21/99
6  Called by:    FGAircraft
7
8  ------------- Copyright (C) 1999  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 details.
18
19  You should have received a copy of the GNU Lesser General Public License along with
20  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
21  Place - Suite 330, Boston, MA  02111-1307, USA.
22
23  Further information about the GNU Lesser General Public License can also be found on
24  the world wide web at http://www.gnu.org.
25
26 FUNCTIONAL DESCRIPTION
27 --------------------------------------------------------------------------------
28 See header file.
29
30 HISTORY
31 --------------------------------------------------------------------------------
32 01/21/99   JSB   Created
33 09/03/99   JSB   Changed Rocket thrust equation to correct -= Thrust instead of
34                  += Thrust (thanks to Tony Peden)
35
36 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
37 INCLUDES
38 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
39
40 #include "FGEngine.h"
41 #include "FGTank.h"
42 #include "FGPropeller.h"
43 #include "FGNozzle.h"
44 #include "input_output/FGXMLParse.h"
45 #include "math/FGColumnVector3.h"
46 #include <fstream>
47
48 namespace JSBSim {
49
50 static const char *IdSrc = "$Id$";
51 static const char *IdHdr = ID_ENGINE;
52
53 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
54 CLASS IMPLEMENTATION
55 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
56
57 FGEngine::FGEngine(FGFDMExec* exec, Element* engine_element, int engine_number)
58                       : EngineNumber(engine_number)
59 {
60   Element* local_element;
61   FGColumnVector3 location, orientation;
62
63   Name = "";
64   Type = etUnknown;
65   X = Y = Z = 0.0;
66   EnginePitch = EngineYaw = 0.0;
67   SLFuelFlowMax = 0.0;
68   MaxThrottle = 1.0;
69   MinThrottle = 0.0;
70   unsigned int i;
71
72   ResetToIC(); // initialize dynamic terms
73
74   FDMExec = exec;
75   State = FDMExec->GetState();
76   Atmosphere = FDMExec->GetAtmosphere();
77   FCS = FDMExec->GetFCS();
78   Propulsion = FDMExec->GetPropulsion();
79   Aircraft = FDMExec->GetAircraft();
80   Propagate = FDMExec->GetPropagate();
81   Auxiliary = FDMExec->GetAuxiliary();
82
83   PropertyManager = FDMExec->GetPropertyManager();
84
85   Name = engine_element->GetAttributeValue("name");
86
87 // Find and set engine location
88
89   local_element = engine_element->GetParent()->FindElement("location");
90   if (local_element)  location = local_element->FindElementTripletConvertTo("IN");
91   else      cerr << "No engine location found for this engine." << endl;
92
93   local_element = engine_element->GetParent()->FindElement("orient");
94   if (local_element)  orientation = local_element->FindElementTripletConvertTo("RAD");
95 //  else          cerr << "No engine orientation found for this engine." << endl;
96 // Jon: The engine orientation has a default and is not normally used.
97
98   SetPlacement(location, orientation);
99
100   // Load thruster
101   local_element = engine_element->GetParent()->FindElement("thruster");
102   if (local_element) {
103     if (!LoadThruster(local_element)) exit(-1);
104   } else {
105     cerr << "No thruster definition supplied with engine definition." << endl;
106   }
107
108   // Build and initialize the feed tank vector.
109   for (i=0; i<(Propulsion->GetNumTanks()); i++) {
110     SourceTanks.push_back(0);
111   }
112
113   // Load feed tank[s] references
114   local_element = engine_element->GetParent()->FindElement("feed");
115   if (local_element) {
116     while (local_element) {
117       int tankID = (int)local_element->GetDataAsNumber();
118       AddFeedTank( tankID , Propulsion->GetTank(tankID)->GetPriority());
119       local_element = engine_element->GetParent()->FindNextElement("feed");
120     }
121   } else {
122     cerr << "No feed tank specified in engine definition." << endl;
123   }
124
125   string property_name, base_property_name;
126   base_property_name = CreateIndexedPropertyName("propulsion/engine", EngineNumber);
127
128   property_name = base_property_name + "/set-running";
129   PropertyManager->Tie( property_name.c_str(), this, &FGEngine::GetRunning, &FGEngine::SetRunning );
130   property_name = base_property_name + "/thrust-lbs";
131   PropertyManager->Tie( property_name.c_str(), Thruster, &FGThruster::GetThrust);
132   property_name = base_property_name + "/fuel-flow-rate-pps";
133   PropertyManager->Tie( property_name.c_str(), this, &FGEngine::GetFuelFlowRate);
134
135   Debug(0);
136 }
137
138 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
139
140 FGEngine::~FGEngine()
141 {
142   delete Thruster;
143   Debug(1);
144 }
145
146 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
147
148 void FGEngine::ResetToIC(void)
149 {
150   Throttle = 0.0;
151   Mixture = 1.0;
152   Starter = false;
153   FuelExpended = 0.0;
154   Starved = Running = Cranking = false;
155   PctPower = 0.0;
156   TrimMode = false;
157   FuelFlow_gph = 0.0;
158   FuelFlow_pph = 0.0;
159   FuelFreeze = false;
160 }
161
162 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
163 // This base class function should be called from within the
164 // derived class' Calculate() function before any other calculations are done.
165 // This base class method removes fuel from the fuel tanks as appropriate,
166 // and sets the starved flag if necessary.
167 // This version of the fuel consumption code should never see an oxidizer tank.
168
169 void FGEngine::ConsumeFuel(void)
170 {
171   if (FuelFreeze) return;
172   if (TrimMode) return;
173
174   unsigned int i;
175   double Fshortage, FuelNeeded;
176   FGTank* Tank;
177   unsigned int TanksWithFuel = 0;
178   Fshortage = FuelNeeded = 0.0;
179   double FuelToBurn;
180   unsigned int CurrentPriority = 1;
181   vector <int> FeedList;
182   Starved = false;
183
184   FuelToBurn = CalcFuelNeed();
185   if (FuelToBurn == 0.0) return;
186
187   while (FuelToBurn > 0.0) {
188
189     // Count how many fuel tanks with the current priority level have fuel.
190     // If none, then try next lower priority.  Build the feed list.
191     while ((TanksWithFuel == 0) && (CurrentPriority <= Propulsion->GetNumTanks())) {
192       for (i=0; i<Propulsion->GetNumTanks(); i++) {
193         if (SourceTanks[i] != 0) {
194           Tank = Propulsion->GetTank(i);
195           if (Tank->GetType() == FGTank::ttFUEL) {
196             if ((Tank->GetContents() > 0.0) && ((unsigned int)Tank->GetPriority() == CurrentPriority)) {
197                ++TanksWithFuel;
198                FeedList.push_back(i);
199              } 
200           } else {
201              cerr << "No oxidizer tanks should be used for this engine type." << endl;
202           }
203         }
204       }
205       if (TanksWithFuel == 0) CurrentPriority++;
206     }
207
208     // No fuel found at any priority!
209     if (TanksWithFuel == 0) {
210       Starved = true;
211       return;
212     }
213
214     // Remove equal amount of fuel from each feed tank.  
215     FuelNeeded = FuelToBurn/TanksWithFuel;
216     for (i=0; i<FeedList.size(); i++) {
217       Tank = Propulsion->GetTank(FeedList[i]);
218       Tank->Drain(FuelNeeded); 
219       FuelToBurn -= FuelNeeded;
220     }
221  
222   }  // while
223 }
224
225 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
226
227 double FGEngine::CalcFuelNeed(void)
228 {
229   double dT = State->Getdt()*Propulsion->GetRate();
230   FuelFlowRate = SLFuelFlowMax*PctPower;
231   FuelExpended = FuelFlowRate*dT;
232   return FuelExpended;
233 }
234
235 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
236
237 void FGEngine::SetPlacement(FGColumnVector3& location, FGColumnVector3& orientation)
238 {
239   X = location(eX);
240   Y = location(eY);
241   Z = location(eZ);
242   EnginePitch = orientation(ePitch);
243   EngineYaw = orientation (eYaw);
244 }
245
246 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
247
248 void FGEngine::AddFeedTank(int tkID, int priority)
249 {
250   SourceTanks[tkID] = priority;
251 }
252
253 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
254
255 FGColumnVector3& FGEngine::GetBodyForces(void)
256 {
257   return Thruster->GetBodyForces();
258 }
259
260 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
261
262 FGColumnVector3& FGEngine::GetMoments(void)
263 {
264   return Thruster->GetMoments();
265 }
266
267 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
268
269 bool FGEngine::LoadThruster(Element *thruster_element)
270 {
271   string token, fullpath, localpath;
272   string thruster_filename, thruster_fullpathname, thrType;
273   string enginePath = FDMExec->GetEnginePath();
274   string aircraftPath = FDMExec->GetFullAircraftPath();
275   ifstream thruster_file;
276   FGColumnVector3 location, orientation;
277   string separator = "/";
278
279   fullpath = enginePath + separator;
280   localpath = aircraftPath + separator + "Engines" + separator;
281
282   thruster_filename = thruster_element->GetAttributeValue("file");
283   if ( !thruster_filename.empty()) {
284     thruster_fullpathname = fullpath + thruster_filename + ".xml";
285     thruster_file.open(thruster_fullpathname.c_str());
286     if ( !thruster_file.is_open()) {
287       thruster_fullpathname = localpath + thruster_filename + ".xml";
288       thruster_file.open(thruster_fullpathname.c_str());
289       if ( !thruster_file.is_open()) {
290         cerr << "Could not open thruster file: " << thruster_filename << ".xml" << endl;
291         return false;
292       } else {
293         thruster_file.close();
294       }
295     } else {
296       thruster_file.close();
297     }
298   } else {
299     cerr << "No thruster filename given." << endl;
300     return false;
301   }
302
303   document = LoadXMLDocument(thruster_fullpathname);
304   document->SetParent(thruster_element);
305
306   thrType = document->GetName();
307
308   if (thrType == "propeller") {
309     Thruster = new FGPropeller(FDMExec, document, EngineNumber);
310   } else if (thrType == "nozzle") {
311     Thruster = new FGNozzle(FDMExec, document, EngineNumber);
312   } else if (thrType == "direct") {
313     Thruster = new FGThruster( FDMExec, document, EngineNumber);
314   }
315
316   Thruster->SetdeltaT(State->Getdt() * Propulsion->GetRate());
317
318   Debug(2);
319   return true;
320 }
321
322 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
323 //    The bitmasked value choices are as follows:
324 //    unset: In this case (the default) JSBSim would only print
325 //       out the normally expected messages, essentially echoing
326 //       the config files as they are read. If the environment
327 //       variable is not set, debug_lvl is set to 1 internally
328 //    0: This requests JSBSim not to output any messages
329 //       whatsoever.
330 //    1: This value explicity requests the normal JSBSim
331 //       startup messages
332 //    2: This value asks for a message to be printed out when
333 //       a class is instantiated
334 //    4: When this value is set, a message is displayed when a
335 //       FGModel object executes its Run() method
336 //    8: When this value is set, various runtime state variables
337 //       are printed out periodically
338 //    16: When set various parameters are sanity checked and
339 //       a message is printed out when they go out of bounds
340
341 void FGEngine::Debug(int from)
342 {
343   if (debug_lvl <= 0) return;
344
345   if (debug_lvl & 1) { // Standard console startup message output
346     if (from == 0) { // Constructor
347
348     }
349     if (from == 2) { // After thruster loading
350       cout << "      X = " << Thruster->GetLocationX() << endl;
351       cout << "      Y = " << Thruster->GetLocationY() << endl;
352       cout << "      Z = " << Thruster->GetLocationZ() << endl;
353       cout << "      Pitch = " << radtodeg*Thruster->GetAnglesToBody(ePitch) << " degrees" << endl;
354       cout << "      Yaw = " << radtodeg*Thruster->GetAnglesToBody(eYaw) << " degrees" << endl;
355     }
356   }
357   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
358     if (from == 0) cout << "Instantiated: FGEngine" << endl;
359     if (from == 1) cout << "Destroyed:    FGEngine" << endl;
360   }
361   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
362   }
363   if (debug_lvl & 8 ) { // Runtime state variables
364   }
365   if (debug_lvl & 16) { // Sanity checking
366   }
367   if (debug_lvl & 64) {
368     if (from == 0) { // Constructor
369       cout << IdSrc << endl;
370       cout << IdHdr << endl;
371     }
372   }
373 }
374 }