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.39 2010/08/21 17:13:48 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");
112 if (!LoadThruster(local_element)) exit(-1);
114 cerr << "No thruster definition supplied with engine definition." << endl;
117 // Build and initialize the feed tank vector.
118 for (i=0; i<(Propulsion->GetNumTanks()); i++) {
119 SourceTanks.push_back(0);
122 // Load feed tank[s] references
123 local_element = engine_element->GetParent()->FindElement("feed");
125 while (local_element) {
126 int tankID = (int)local_element->GetDataAsNumber();
127 FGTank* tank = Propulsion->GetTank(tankID);
129 AddFeedTank(tankID, tank->GetPriority());
130 FuelDensity = tank->GetDensity();
132 cerr << "Feed tank " << tankID <<
133 " specified in engine definition does not exist." << endl;
135 local_element = engine_element->GetParent()->FindNextElement("feed");
138 cerr << "No feed tank specified in engine definition." << endl;
141 string property_name, base_property_name;
142 base_property_name = CreateIndexedPropertyName("propulsion/engine", EngineNumber);
144 property_name = base_property_name + "/set-running";
145 PropertyManager->Tie( property_name.c_str(), this, &FGEngine::GetRunning, &FGEngine::SetRunning );
146 property_name = base_property_name + "/thrust-lbs";
147 PropertyManager->Tie( property_name.c_str(), Thruster, &FGThruster::GetThrust);
148 property_name = base_property_name + "/fuel-flow-rate-pps";
149 PropertyManager->Tie( property_name.c_str(), this, &FGEngine::GetFuelFlowRate);
151 PostLoad(engine_element, PropertyManager, to_string(EngineNumber));
153 //cout << "Engine[" << EngineNumber << "] using fuel density: " << FuelDensity << endl;
158 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
160 FGEngine::~FGEngine()
166 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
168 void FGEngine::ResetToIC(void)
174 Starved = Running = Cranking = false;
183 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
184 // This base class function should be called from within the
185 // derived class' Calculate() function before any other calculations are done.
186 // This base class method removes fuel from the fuel tanks as appropriate,
187 // and sets the starved flag if necessary.
188 // This version of the fuel consumption code should never see an oxidizer tank.
190 void FGEngine::ConsumeFuel(void)
192 if (FuelFreeze) return;
193 if (TrimMode) return;
196 double Fshortage, FuelNeeded;
198 unsigned int TanksWithFuel = 0;
199 Fshortage = FuelNeeded = 0.0;
201 unsigned int CurrentPriority = 1;
202 vector <int> FeedList;
205 FuelToBurn = CalcFuelNeed();
206 if (FuelToBurn == 0.0) return;
208 // Count how many fuel tanks with the current priority level have fuel.
209 // If none, then try next lower priority. Build the feed list.
210 while ((TanksWithFuel == 0) && (CurrentPriority <= Propulsion->GetNumTanks())) {
211 for (i=0; i<Propulsion->GetNumTanks(); i++) {
212 if (SourceTanks[i] != 0) {
213 Tank = Propulsion->GetTank(i);
214 if (Tank->GetType() == FGTank::ttFUEL) {
215 if ((Tank->GetContents() > 0.0) && ((unsigned int)Tank->GetPriority() == CurrentPriority)) {
217 FeedList.push_back(i);
220 cerr << "No oxidizer tanks should be used for this engine type." << endl;
224 if (TanksWithFuel == 0) CurrentPriority++;
227 // No fuel found at any priority!
228 if (TanksWithFuel == 0) {
233 // Remove equal amount of fuel from each feed tank.
234 FuelNeeded = FuelToBurn/TanksWithFuel;
235 for (i=0; i<FeedList.size(); i++) {
236 Tank = Propulsion->GetTank(FeedList[i]);
237 Tank->Drain(FuelNeeded);
242 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
244 double FGEngine::CalcFuelNeed(void)
246 double dT = FDMExec->GetDeltaT()*Propulsion->GetRate();
247 FuelFlowRate = SLFuelFlowMax*PctPower;
248 FuelExpended = FuelFlowRate*dT;
252 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
254 void FGEngine::SetPlacement(FGColumnVector3& location, FGColumnVector3& orientation)
259 EnginePitch = orientation(ePitch);
260 EngineYaw = orientation (eYaw);
263 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
265 void FGEngine::AddFeedTank(int tkID, int priority)
267 SourceTanks[tkID] = priority;
270 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
272 FGColumnVector3& FGEngine::GetBodyForces(void)
274 return Thruster->GetBodyForces();
277 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
279 FGColumnVector3& FGEngine::GetMoments(void)
281 return Thruster->GetMoments();
284 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
286 bool FGEngine::LoadThruster(Element *thruster_element)
288 string token, fullpath, localpath;
289 string thruster_filename, thruster_fullpathname, thrType;
290 string enginePath = FDMExec->GetEnginePath();
291 string aircraftPath = FDMExec->GetFullAircraftPath();
292 ifstream thruster_file;
293 FGColumnVector3 location, orientation;
294 string separator = "/";
296 fullpath = enginePath + separator;
297 localpath = aircraftPath + separator + "Engines" + separator;
299 thruster_filename = thruster_element->GetAttributeValue("file");
300 if ( !thruster_filename.empty()) {
301 thruster_fullpathname = fullpath + thruster_filename + ".xml";
302 thruster_file.open(thruster_fullpathname.c_str());
303 if ( !thruster_file.is_open()) {
304 thruster_fullpathname = localpath + thruster_filename + ".xml";
305 thruster_file.open(thruster_fullpathname.c_str());
306 if ( !thruster_file.is_open()) {
307 cerr << "Could not open thruster file: " << thruster_filename << ".xml" << endl;
310 thruster_file.close();
313 thruster_file.close();
316 cerr << "No thruster filename given." << endl;
320 document = LoadXMLDocument(thruster_fullpathname);
321 document->SetParent(thruster_element);
323 thrType = document->GetName();
325 if (thrType == "propeller") {
326 Thruster = new FGPropeller(FDMExec, document, EngineNumber);
327 } else if (thrType == "nozzle") {
328 Thruster = new FGNozzle(FDMExec, document, EngineNumber);
329 } else if (thrType == "rotor") {
330 Thruster = new FGRotor(FDMExec, document, EngineNumber);
331 } else if (thrType == "direct") {
332 Thruster = new FGThruster( FDMExec, document, EngineNumber);
335 Thruster->SetdeltaT(FDMExec->GetDeltaT() * Propulsion->GetRate());
341 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
342 // The bitmasked value choices are as follows:
343 // unset: In this case (the default) JSBSim would only print
344 // out the normally expected messages, essentially echoing
345 // the config files as they are read. If the environment
346 // variable is not set, debug_lvl is set to 1 internally
347 // 0: This requests JSBSim not to output any messages
349 // 1: This value explicity requests the normal JSBSim
351 // 2: This value asks for a message to be printed out when
352 // a class is instantiated
353 // 4: When this value is set, a message is displayed when a
354 // FGModel object executes its Run() method
355 // 8: When this value is set, various runtime state variables
356 // are printed out periodically
357 // 16: When set various parameters are sanity checked and
358 // a message is printed out when they go out of bounds
360 void FGEngine::Debug(int from)
362 if (debug_lvl <= 0) return;
364 if (debug_lvl & 1) { // Standard console startup message output
365 if (from == 0) { // Constructor
368 if (from == 2) { // After thruster loading
369 cout << " X = " << Thruster->GetLocationX() << endl;
370 cout << " Y = " << Thruster->GetLocationY() << endl;
371 cout << " Z = " << Thruster->GetLocationZ() << endl;
372 cout << " Pitch = " << radtodeg*Thruster->GetAnglesToBody(ePitch) << " degrees" << endl;
373 cout << " Yaw = " << radtodeg*Thruster->GetAnglesToBody(eYaw) << " degrees" << endl;
376 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
377 if (from == 0) cout << "Instantiated: FGEngine" << endl;
378 if (from == 1) cout << "Destroyed: FGEngine" << endl;
380 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
382 if (debug_lvl & 8 ) { // Runtime state variables
384 if (debug_lvl & 16) { // Sanity checking
386 if (debug_lvl & 64) {
387 if (from == 0) { // Constructor
388 cout << IdSrc << endl;
389 cout << IdHdr << endl;