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