1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
8 ------------- Copyright (C) 1999 Jon S. Berndt (jon@jsbsim.org) -------------
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
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.
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.
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.
26 FUNCTIONAL DESCRIPTION
27 --------------------------------------------------------------------------------
31 --------------------------------------------------------------------------------
33 09/03/99 JSB Changed Rocket thrust equation to correct -= Thrust instead of
34 += Thrust (thanks to Tony Peden)
36 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
38 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
42 #include "FGPropeller.h"
45 #include "models/FGPropulsion.h"
46 #include "input_output/FGXMLParse.h"
47 #include "math/FGColumnVector3.h"
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;
60 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
62 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
64 FGEngine::FGEngine(FGFDMExec* exec, Element* engine_element, int engine_number)
65 : EngineNumber(engine_number)
67 Element* local_element;
68 FGColumnVector3 location, orientation;
73 EnginePitch = EngineYaw = 0.0;
80 ResetToIC(); // initialize dynamic terms
83 Atmosphere = FDMExec->GetAtmosphere();
84 FCS = FDMExec->GetFCS();
85 Propulsion = FDMExec->GetPropulsion();
86 Aircraft = FDMExec->GetAircraft();
87 Propagate = FDMExec->GetPropagate();
88 Auxiliary = FDMExec->GetAuxiliary();
90 PropertyManager = FDMExec->GetPropertyManager();
92 Name = engine_element->GetAttributeValue("name");
94 Load(engine_element, PropertyManager, to_string(EngineNumber)); // Call ModelFunctions loader
96 // Find and set engine location
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;
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.
107 SetPlacement(location, orientation);
110 local_element = engine_element->GetParent()->FindElement("thruster");
113 if (!LoadThruster(local_element)) exit(-1);
114 } catch (std::string str) {
115 throw("Error loading engine " + Name + ". " + str);
118 cerr << "No thruster definition supplied with engine definition." << endl;
121 // Build and initialize the feed tank vector.
122 for (i=0; i<(Propulsion->GetNumTanks()); i++) {
123 SourceTanks.push_back(0);
126 // Load feed tank[s] references
127 local_element = engine_element->GetParent()->FindElement("feed");
129 while (local_element) {
130 int tankID = (int)local_element->GetDataAsNumber();
131 FGTank* tank = Propulsion->GetTank(tankID);
133 AddFeedTank(tankID, tank->GetPriority());
134 FuelDensity = tank->GetDensity();
136 cerr << "Feed tank " << tankID <<
137 " specified in engine definition does not exist." << endl;
139 local_element = engine_element->GetParent()->FindNextElement("feed");
142 cerr << "No feed tank specified in engine definition." << endl;
145 string property_name, base_property_name;
146 base_property_name = CreateIndexedPropertyName("propulsion/engine", EngineNumber);
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);
155 PostLoad(engine_element, PropertyManager, to_string(EngineNumber));
157 //cout << "Engine[" << EngineNumber << "] using fuel density: " << FuelDensity << endl;
162 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
164 FGEngine::~FGEngine()
170 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
172 void FGEngine::ResetToIC(void)
178 Starved = Running = Cranking = false;
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.
194 void FGEngine::ConsumeFuel(void)
196 if (FuelFreeze) return;
197 if (TrimMode) return;
200 double Fshortage, FuelNeeded;
202 unsigned int TanksWithFuel = 0;
203 Fshortage = FuelNeeded = 0.0;
205 unsigned int CurrentPriority = 1;
206 vector <int> FeedList;
209 FuelToBurn = CalcFuelNeed();
210 if (FuelToBurn == 0.0) return;
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)) {
221 FeedList.push_back(i);
224 cerr << "No oxidizer tanks should be used for this engine type." << endl;
228 if (TanksWithFuel == 0) CurrentPriority++;
231 // No fuel found at any priority!
232 if (TanksWithFuel == 0) {
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);
246 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
248 double FGEngine::CalcFuelNeed(void)
250 double dT = FDMExec->GetDeltaT()*Propulsion->GetRate();
251 FuelFlowRate = SLFuelFlowMax*PctPower;
252 FuelExpended = FuelFlowRate*dT;
256 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
258 void FGEngine::SetPlacement(FGColumnVector3& location, FGColumnVector3& orientation)
263 EnginePitch = orientation(ePitch);
264 EngineYaw = orientation (eYaw);
267 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
269 void FGEngine::AddFeedTank(int tkID, int priority)
271 SourceTanks[tkID] = priority;
274 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
276 FGColumnVector3& FGEngine::GetBodyForces(void)
278 return Thruster->GetBodyForces();
281 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
283 FGColumnVector3& FGEngine::GetMoments(void)
285 return Thruster->GetMoments();
288 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
290 bool FGEngine::LoadThruster(Element *thruster_element)
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 = "/";
300 fullpath = enginePath + separator;
301 localpath = aircraftPath + separator + "Engines" + separator;
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;
314 thruster_file.close();
317 thruster_file.close();
320 cerr << "No thruster filename given." << endl;
324 document = LoadXMLDocument(thruster_fullpathname);
325 document->SetParent(thruster_element);
327 thrType = document->GetName();
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);
339 Thruster->SetdeltaT(FDMExec->GetDeltaT() * Propulsion->GetRate());
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
353 // 1: This value explicity requests the normal JSBSim
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
364 void FGEngine::Debug(int from)
366 if (debug_lvl <= 0) return;
368 if (debug_lvl & 1) { // Standard console startup message output
369 if (from == 0) { // Constructor
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;
380 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
381 if (from == 0) cout << "Instantiated: FGEngine" << endl;
382 if (from == 1) cout << "Destroyed: FGEngine" << endl;
384 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
386 if (debug_lvl & 8 ) { // Runtime state variables
388 if (debug_lvl & 16) { // Sanity checking
390 if (debug_lvl & 64) {
391 if (from == 0) { // Constructor
392 cout << IdSrc << endl;
393 cout << IdHdr << endl;