]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/models/propulsion/FGEngine.cpp
remove macintosh (note: not MacOS-X) and MWERKS workarounds.
[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 (jsb@hal-pc.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 = SLOxiFlowMax = 0.0;
68   MaxThrottle = 1.0;
69   MinThrottle = 0.0;
70
71   ResetToIC(); // initialize dynamic terms
72
73   FDMExec = exec;
74   State = FDMExec->GetState();
75   Atmosphere = FDMExec->GetAtmosphere();
76   FCS = FDMExec->GetFCS();
77   Propulsion = FDMExec->GetPropulsion();
78   Aircraft = FDMExec->GetAircraft();
79   Propagate = FDMExec->GetPropagate();
80   Auxiliary = FDMExec->GetAuxiliary();
81
82   PropertyManager = FDMExec->GetPropertyManager();
83
84   Name = engine_element->GetAttributeValue("name");
85
86 // Find and set engine location
87
88   local_element = engine_element->GetParent()->FindElement("location");
89   if (local_element)  location = local_element->FindElementTripletConvertTo("IN");
90   else      cerr << "No engine location found for this engine." << endl;
91
92   local_element = engine_element->GetParent()->FindElement("orient");
93   if (local_element)  orientation = local_element->FindElementTripletConvertTo("RAD");
94 //  else          cerr << "No engine orientation found for this engine." << endl;
95 // Jon: The engine orientation has a default and is not normally used.
96
97   SetPlacement(location, orientation);
98
99   // Load thruster
100   local_element = engine_element->GetParent()->FindElement("thruster");
101   if (local_element) {
102     if (!LoadThruster(local_element)) exit(-1);
103   } else {
104     cerr << "No thruster definition supplied with engine definition." << endl;
105   }
106
107   // Load feed tank[s] references
108   local_element = engine_element->GetParent()->FindElement("feed");
109   if (local_element) {
110     while (local_element) {
111       AddFeedTank((int)local_element->GetDataAsNumber());
112       local_element = engine_element->GetParent()->FindNextElement("feed");
113     }
114   } else {
115     cerr << "No feed tank specified in engine definition." << endl;
116   }
117
118   char property_name[80];
119   snprintf(property_name, 80, "propulsion/engine[%d]/set-running", EngineNumber);
120   PropertyManager->Tie( property_name, (FGEngine*)this, &FGEngine::GetRunning,
121                                        &FGEngine::SetRunning );
122
123   Debug(0);
124 }
125
126 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
127
128 FGEngine::~FGEngine()
129 {
130   delete Thruster;
131   Debug(1);
132 }
133
134 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
135
136 void FGEngine::ResetToIC(void)
137 {
138   Thrust = 0.0;
139   Throttle = 0.0;
140   Mixture = 1.0;
141   Starter = false;
142   FuelNeed = OxidizerNeed = 0.0;
143   Starved = Running = Cranking = false;
144   PctPower = 0.0;
145   TrimMode = false;
146   FuelFlow_gph = 0.0;
147   FuelFlow_pph = 0.0;
148   FuelFreeze = false;
149 }
150
151 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
152 // This base class function should be called from within the
153 // derived class' Calculate() function before any other calculations are done.
154 // This base class method removes fuel from the fuel tanks as appropriate,
155 // and sets the starved flag if necessary.
156
157 void FGEngine::ConsumeFuel(void)
158 {
159   if (FuelFreeze) return;
160   if (TrimMode) return;
161
162   unsigned int i;
163   double Fshortage, Oshortage, TanksWithFuel, TanksWithOxidizer;
164   FGTank* Tank;
165   bool haveOxTanks = false;
166   Fshortage = Oshortage = TanksWithFuel = TanksWithOxidizer = 0.0;
167
168   // count how many assigned tanks have fuel
169   for (i=0; i<SourceTanks.size(); i++) {
170     Tank = Propulsion->GetTank(SourceTanks[i]);
171     if (Tank->GetType() == FGTank::ttFUEL){
172       if (Tank->GetContents() > 0.0) ++TanksWithFuel;
173     } else if (Tank->GetType() == FGTank::ttOXIDIZER) {
174       haveOxTanks = true;
175       if (Tank->GetContents() > 0.0) ++TanksWithOxidizer;
176     }
177   }
178   if (TanksWithFuel==0 || (haveOxTanks && TanksWithOxidizer==0)) {
179     Starved = true;
180     return;
181   }
182
183   for (i=0; i<SourceTanks.size(); i++) {
184     Tank = Propulsion->GetTank(SourceTanks[i]);
185     if (Tank->GetType() == FGTank::ttFUEL) {
186        Fshortage += Tank->Drain(CalcFuelNeed()/TanksWithFuel);
187     } else if (Tank->GetType() == FGTank::ttOXIDIZER) {
188        Oshortage += Tank->Drain(CalcOxidizerNeed()/TanksWithOxidizer);
189     }
190   }
191
192   if (Fshortage < 0.00 || Oshortage < 0.00) Starved = true;
193   else Starved = false;
194 }
195
196 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
197
198 double FGEngine::CalcFuelNeed(void)
199 {
200   FuelNeed = SLFuelFlowMax*PctPower*State->Getdt()*Propulsion->GetRate();
201   return FuelNeed;
202 }
203
204 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
205
206 double FGEngine::CalcOxidizerNeed(void)
207 {
208   OxidizerNeed = SLOxiFlowMax*PctPower*State->Getdt()*Propulsion->GetRate();
209   return OxidizerNeed;
210 }
211
212 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
213
214 void FGEngine::SetPlacement(FGColumnVector3& location, FGColumnVector3& orientation)
215 {
216   X = location(eX);
217   Y = location(eY);
218   Z = location(eZ);
219   EnginePitch = orientation(ePitch);
220   EngineYaw = orientation (eYaw);
221 }
222
223 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
224
225 void FGEngine::AddFeedTank(int tkID)
226 {
227   SourceTanks.push_back(tkID);
228 }
229
230 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
231
232 FGColumnVector3& FGEngine::GetBodyForces(void)
233 {
234   return Thruster->GetBodyForces();
235 }
236
237 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
238
239 FGColumnVector3& FGEngine::GetMoments(void)
240 {
241   return Thruster->GetMoments();
242 }
243
244 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
245
246 bool FGEngine::LoadThruster(Element *thruster_element)
247 {
248   string token, fullpath, localpath;
249   string thruster_filename, thruster_fullpathname, thrType;
250   double P_Factor = 0, Sense = 0.0;
251   string enginePath = FDMExec->GetEnginePath();
252   string aircraftPath = FDMExec->GetFullAircraftPath();
253   ifstream thruster_file;
254   FGColumnVector3 location, orientation;
255   string separator = "/";
256
257   fullpath = enginePath + separator;
258   localpath = aircraftPath + separator + "Engines" + separator;
259
260   thruster_filename = thruster_element->GetAttributeValue("file");
261   if ( !thruster_filename.empty()) {
262     thruster_fullpathname = fullpath + thruster_filename + ".xml";
263     thruster_file.open(thruster_fullpathname.c_str());
264     if ( !thruster_file.is_open()) {
265       thruster_fullpathname = localpath + thruster_filename + ".xml";
266       thruster_file.open(thruster_fullpathname.c_str());
267       if ( !thruster_file.is_open()) {
268         cerr << "Could not open thruster file: " << thruster_filename << ".xml" << endl;
269         return false;
270       } else {
271         thruster_file.close();
272       }
273     } else {
274       thruster_file.close();
275     }
276   } else {
277     cerr << "No thruster filename given." << endl;
278     return false;
279   }
280
281   document = LoadXMLDocument(thruster_fullpathname);
282   document->SetParent(thruster_element);
283
284   thrType = document->GetName();
285
286   if (thrType == "propeller") {
287     Thruster = new FGPropeller(FDMExec, document, EngineNumber);
288   } else if (thrType == "nozzle") {
289     Thruster = new FGNozzle(FDMExec, document, EngineNumber);
290   } else if (thrType == "direct") {
291     Thruster = new FGThruster( FDMExec, document, EngineNumber);
292   }
293
294   Thruster->SetdeltaT(State->Getdt() * Propulsion->GetRate());
295
296   Debug(2);
297   return true;
298 }
299
300 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
301 //    The bitmasked value choices are as follows:
302 //    unset: In this case (the default) JSBSim would only print
303 //       out the normally expected messages, essentially echoing
304 //       the config files as they are read. If the environment
305 //       variable is not set, debug_lvl is set to 1 internally
306 //    0: This requests JSBSim not to output any messages
307 //       whatsoever.
308 //    1: This value explicity requests the normal JSBSim
309 //       startup messages
310 //    2: This value asks for a message to be printed out when
311 //       a class is instantiated
312 //    4: When this value is set, a message is displayed when a
313 //       FGModel object executes its Run() method
314 //    8: When this value is set, various runtime state variables
315 //       are printed out periodically
316 //    16: When set various parameters are sanity checked and
317 //       a message is printed out when they go out of bounds
318
319 void FGEngine::Debug(int from)
320 {
321   if (debug_lvl <= 0) return;
322
323   if (debug_lvl & 1) { // Standard console startup message output
324     if (from == 0) { // Constructor
325
326     }
327     if (from == 2) { // After thruster loading
328       cout << "      X = " << Thruster->GetLocationX() << endl;
329       cout << "      Y = " << Thruster->GetLocationY() << endl;
330       cout << "      Z = " << Thruster->GetLocationZ() << endl;
331       cout << "      Pitch = " << radtodeg*Thruster->GetAnglesToBody(ePitch) << " degrees" << endl;
332       cout << "      Yaw = " << radtodeg*Thruster->GetAnglesToBody(eYaw) << " degrees" << endl;
333     }
334   }
335   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
336     if (from == 0) cout << "Instantiated: FGEngine" << endl;
337     if (from == 1) cout << "Destroyed:    FGEngine" << endl;
338   }
339   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
340   }
341   if (debug_lvl & 8 ) { // Runtime state variables
342   }
343   if (debug_lvl & 16) { // Sanity checking
344   }
345   if (debug_lvl & 64) {
346     if (from == 0) { // Constructor
347       cout << IdSrc << endl;
348       cout << IdHdr << endl;
349     }
350   }
351 }
352 }