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