]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/models/propulsion/FGEngine.cpp
Merge commit 'refs/merge-requests/13' of git://gitorious.org/fg/flightgear into next
[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.40 2010/10/15 11:32:41 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
155   PostLoad(engine_element, PropertyManager, to_string(EngineNumber));
156
157   //cout << "Engine[" << EngineNumber << "] using fuel density: " << FuelDensity << endl;
158
159   Debug(0);
160 }
161
162 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
163
164 FGEngine::~FGEngine()
165 {
166   delete Thruster;
167   Debug(1);
168 }
169
170 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
171
172 void FGEngine::ResetToIC(void)
173 {
174   Throttle = 0.0;
175   Mixture = 1.0;
176   Starter = false;
177   FuelExpended = 0.0;
178   Starved = Running = Cranking = false;
179   PctPower = 0.0;
180   TrimMode = false;
181   FuelFlow_gph = 0.0;
182   FuelFlow_pph = 0.0;
183   FuelFlowRate = 0.0;
184   FuelFreeze = false;
185 }
186
187 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
188 // This base class function should be called from within the
189 // derived class' Calculate() function before any other calculations are done.
190 // This base class method removes fuel from the fuel tanks as appropriate,
191 // and sets the starved flag if necessary.
192 // This version of the fuel consumption code should never see an oxidizer tank.
193
194 void FGEngine::ConsumeFuel(void)
195 {
196   if (FuelFreeze) return;
197   if (TrimMode) return;
198
199   unsigned int i;
200   double Fshortage, FuelNeeded;
201   FGTank* Tank;
202   unsigned int TanksWithFuel = 0;
203   Fshortage = FuelNeeded = 0.0;
204   double FuelToBurn;
205   unsigned int CurrentPriority = 1;
206   vector <int> FeedList;
207   Starved = false;
208
209   FuelToBurn = CalcFuelNeed();
210   if (FuelToBurn == 0.0) return;
211
212   // Count how many fuel tanks with the current priority level have fuel.
213   // If none, then try next lower priority.  Build the feed list.
214   while ((TanksWithFuel == 0) && (CurrentPriority <= Propulsion->GetNumTanks())) {
215     for (i=0; i<Propulsion->GetNumTanks(); i++) {
216       if (SourceTanks[i] != 0) {
217         Tank = Propulsion->GetTank(i);
218         if (Tank->GetType() == FGTank::ttFUEL) {
219           if ((Tank->GetContents() > 0.0) && ((unsigned int)Tank->GetPriority() == CurrentPriority)) {
220              ++TanksWithFuel;
221              FeedList.push_back(i);
222            } 
223         } else {
224            cerr << "No oxidizer tanks should be used for this engine type." << endl;
225         }
226       }
227     }
228     if (TanksWithFuel == 0) CurrentPriority++;
229   }
230
231   // No fuel found at any priority!
232   if (TanksWithFuel == 0) {
233     Starved = true;
234     return;
235   }
236
237   // Remove equal amount of fuel from each feed tank.  
238   FuelNeeded = FuelToBurn/TanksWithFuel;
239   for (i=0; i<FeedList.size(); i++) {
240     Tank = Propulsion->GetTank(FeedList[i]);
241     Tank->Drain(FuelNeeded); 
242   }
243
244 }
245
246 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
247
248 double FGEngine::CalcFuelNeed(void)
249 {
250   double dT = FDMExec->GetDeltaT()*Propulsion->GetRate();
251   FuelFlowRate = SLFuelFlowMax*PctPower;
252   FuelExpended = FuelFlowRate*dT;
253   return FuelExpended;
254 }
255
256 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
257
258 void FGEngine::SetPlacement(FGColumnVector3& location, FGColumnVector3& orientation)
259 {
260   X = location(eX);
261   Y = location(eY);
262   Z = location(eZ);
263   EnginePitch = orientation(ePitch);
264   EngineYaw = orientation (eYaw);
265 }
266
267 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
268
269 void FGEngine::AddFeedTank(int tkID, int priority)
270 {
271   SourceTanks[tkID] = priority;
272 }
273
274 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
275
276 FGColumnVector3& FGEngine::GetBodyForces(void)
277 {
278   return Thruster->GetBodyForces();
279 }
280
281 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
282
283 FGColumnVector3& FGEngine::GetMoments(void)
284 {
285   return Thruster->GetMoments();
286 }
287
288 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
289
290 bool FGEngine::LoadThruster(Element *thruster_element)
291 {
292   string token, fullpath, localpath;
293   string thruster_filename, thruster_fullpathname, thrType;
294   string enginePath = FDMExec->GetEnginePath();
295   string aircraftPath = FDMExec->GetFullAircraftPath();
296   ifstream thruster_file;
297   FGColumnVector3 location, orientation;
298   string separator = "/";
299
300   fullpath = enginePath + separator;
301   localpath = aircraftPath + separator + "Engines" + separator;
302
303   thruster_filename = thruster_element->GetAttributeValue("file");
304   if ( !thruster_filename.empty()) {
305     thruster_fullpathname = fullpath + thruster_filename + ".xml";
306     thruster_file.open(thruster_fullpathname.c_str());
307     if ( !thruster_file.is_open()) {
308       thruster_fullpathname = localpath + thruster_filename + ".xml";
309       thruster_file.open(thruster_fullpathname.c_str());
310       if ( !thruster_file.is_open()) {
311         cerr << "Could not open thruster file: " << thruster_filename << ".xml" << endl;
312         return false;
313       } else {
314         thruster_file.close();
315       }
316     } else {
317       thruster_file.close();
318     }
319   } else {
320     cerr << "No thruster filename given." << endl;
321     return false;
322   }
323
324   document = LoadXMLDocument(thruster_fullpathname);
325   document->SetParent(thruster_element);
326
327   thrType = document->GetName();
328
329   if (thrType == "propeller") {
330     Thruster = new FGPropeller(FDMExec, document, EngineNumber);
331   } else if (thrType == "nozzle") {
332     Thruster = new FGNozzle(FDMExec, document, EngineNumber);
333   } else if (thrType == "rotor") {
334     Thruster = new FGRotor(FDMExec, document, EngineNumber);
335   } else if (thrType == "direct") {
336     Thruster = new FGThruster( FDMExec, document, EngineNumber);
337   }
338
339   Thruster->SetdeltaT(FDMExec->GetDeltaT() * Propulsion->GetRate());
340
341   Debug(2);
342   return true;
343 }
344
345 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
346 //    The bitmasked value choices are as follows:
347 //    unset: In this case (the default) JSBSim would only print
348 //       out the normally expected messages, essentially echoing
349 //       the config files as they are read. If the environment
350 //       variable is not set, debug_lvl is set to 1 internally
351 //    0: This requests JSBSim not to output any messages
352 //       whatsoever.
353 //    1: This value explicity requests the normal JSBSim
354 //       startup messages
355 //    2: This value asks for a message to be printed out when
356 //       a class is instantiated
357 //    4: When this value is set, a message is displayed when a
358 //       FGModel object executes its Run() method
359 //    8: When this value is set, various runtime state variables
360 //       are printed out periodically
361 //    16: When set various parameters are sanity checked and
362 //       a message is printed out when they go out of bounds
363
364 void FGEngine::Debug(int from)
365 {
366   if (debug_lvl <= 0) return;
367
368   if (debug_lvl & 1) { // Standard console startup message output
369     if (from == 0) { // Constructor
370
371     }
372     if (from == 2) { // After thruster loading
373       cout << "      X = " << Thruster->GetLocationX() << endl;
374       cout << "      Y = " << Thruster->GetLocationY() << endl;
375       cout << "      Z = " << Thruster->GetLocationZ() << endl;
376       cout << "      Pitch = " << radtodeg*Thruster->GetAnglesToBody(ePitch) << " degrees" << endl;
377       cout << "      Yaw = " << radtodeg*Thruster->GetAnglesToBody(eYaw) << " degrees" << endl;
378     }
379   }
380   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
381     if (from == 0) cout << "Instantiated: FGEngine" << endl;
382     if (from == 1) cout << "Destroyed:    FGEngine" << endl;
383   }
384   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
385   }
386   if (debug_lvl & 8 ) { // Runtime state variables
387   }
388   if (debug_lvl & 16) { // Sanity checking
389   }
390   if (debug_lvl & 64) {
391     if (from == 0) { // Constructor
392       cout << IdSrc << endl;
393       cout << IdHdr << endl;
394     }
395   }
396 }
397 }