]> git.mxchange.org Git - flightgear.git/blobdiff - src/FDM/JSBSim/models/propulsion/FGEngine.cpp
Merge branch 'next' into durk-atc
[flightgear.git] / src / FDM / JSBSim / models / propulsion / FGEngine.cpp
index fd7290029c2eac9ca5cead79a6a542bd9dcc7301..707d425e4c7433aa72a51a4f71d8f2a0057629e1 100644 (file)
@@ -41,13 +41,20 @@ INCLUDES
 #include "FGTank.h"
 #include "FGPropeller.h"
 #include "FGNozzle.h"
+#include "FGRotor.h"
+#include "models/FGPropulsion.h"
 #include "input_output/FGXMLParse.h"
 #include "math/FGColumnVector3.h"
+
+#include <iostream>
 #include <fstream>
+#include <cstdlib>
+
+using namespace std;
 
 namespace JSBSim {
 
-static const char *IdSrc = "$Id$";
+static const char *IdSrc = "$Id: FGEngine.cpp,v 1.42 2011/03/03 12:16:26 jberndt Exp $";
 static const char *IdHdr = ID_ENGINE;
 
 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -67,12 +74,12 @@ FGEngine::FGEngine(FGFDMExec* exec, Element* engine_element, int engine_number)
   SLFuelFlowMax = 0.0;
   MaxThrottle = 1.0;
   MinThrottle = 0.0;
+  FuelDensity = 6.0;
   unsigned int i;
 
   ResetToIC(); // initialize dynamic terms
 
   FDMExec = exec;
-  State = FDMExec->GetState();
   Atmosphere = FDMExec->GetAtmosphere();
   FCS = FDMExec->GetFCS();
   Propulsion = FDMExec->GetPropulsion();
@@ -84,6 +91,8 @@ FGEngine::FGEngine(FGFDMExec* exec, Element* engine_element, int engine_number)
 
   Name = engine_element->GetAttributeValue("name");
 
+  Load(engine_element, PropertyManager, to_string(EngineNumber)); // Call ModelFunctions loader
+
 // Find and set engine location
 
   local_element = engine_element->GetParent()->FindElement("location");
@@ -100,7 +109,11 @@ FGEngine::FGEngine(FGFDMExec* exec, Element* engine_element, int engine_number)
   // Load thruster
   local_element = engine_element->GetParent()->FindElement("thruster");
   if (local_element) {
-    if (!LoadThruster(local_element)) exit(-1);
+    try {
+      if (!LoadThruster(local_element)) exit(-1);
+    } catch (std::string str) {
+      throw("Error loading engine " + Name + ". " + str);
+    }
   } else {
     cerr << "No thruster definition supplied with engine definition." << endl;
   }
@@ -115,7 +128,14 @@ FGEngine::FGEngine(FGFDMExec* exec, Element* engine_element, int engine_number)
   if (local_element) {
     while (local_element) {
       int tankID = (int)local_element->GetDataAsNumber();
-      AddFeedTank( tankID , Propulsion->GetTank(tankID)->GetPriority());
+      FGTank* tank = Propulsion->GetTank(tankID); 
+      if (tank) {
+        AddFeedTank(tankID, tank->GetPriority());
+        FuelDensity = tank->GetDensity();
+      } else {
+        cerr << "Feed tank " << tankID <<
+          " specified in engine definition does not exist." << endl;
+      }
       local_element = engine_element->GetParent()->FindNextElement("feed");
     }
   } else {
@@ -131,6 +151,12 @@ FGEngine::FGEngine(FGFDMExec* exec, Element* engine_element, int engine_number)
   PropertyManager->Tie( property_name.c_str(), Thruster, &FGThruster::GetThrust);
   property_name = base_property_name + "/fuel-flow-rate-pps";
   PropertyManager->Tie( property_name.c_str(), this, &FGEngine::GetFuelFlowRate);
+  property_name = base_property_name + "/fuel-used-lbs";
+  PropertyManager->Tie( property_name.c_str(), this, &FGEngine::GetFuelUsedLbs);
+
+  PostLoad(engine_element, PropertyManager, to_string(EngineNumber));
+
+  //cout << "Engine[" << EngineNumber << "] using fuel density: " << FuelDensity << endl;
 
   Debug(0);
 }
@@ -153,10 +179,11 @@ void FGEngine::ResetToIC(void)
   FuelExpended = 0.0;
   Starved = Running = Cranking = false;
   PctPower = 0.0;
-  TrimMode = false;
   FuelFlow_gph = 0.0;
   FuelFlow_pph = 0.0;
+  FuelFlowRate = 0.0;
   FuelFreeze = false;
+  FuelUsedLbs = 0.0;
 }
 
 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -169,7 +196,7 @@ void FGEngine::ResetToIC(void)
 void FGEngine::ConsumeFuel(void)
 {
   if (FuelFreeze) return;
-  if (TrimMode) return;
+  if (FDMExec->GetTrimStatus()) return;
 
   unsigned int i;
   double Fshortage, FuelNeeded;
@@ -182,57 +209,48 @@ void FGEngine::ConsumeFuel(void)
   Starved = false;
 
   FuelToBurn = CalcFuelNeed();
-
-  while (FuelToBurn > 0.0) {
-
-    // Count how many fuel tanks with the current priority level have fuel.
-    // If none, then try next lower priority.  Build the feed list.
-    while ((TanksWithFuel == 0.0) && (CurrentPriority <= Propulsion->GetNumTanks())) {
-      for (i=0; i<Propulsion->GetNumTanks(); i++) {
-        if (SourceTanks[i] != 0) {
-          Tank = Propulsion->GetTank(i);
-          if (Tank->GetType() == FGTank::ttFUEL) {
-            if ((Tank->GetContents() > 0.0) && ((unsigned int)Tank->GetPriority() == CurrentPriority)) {
-               ++TanksWithFuel;
-               FeedList.push_back(i);
-             } 
-          } else {
-             cerr << "No oxidizer tanks should be used for this engine type." << endl;
-          }
+  if (FuelToBurn == 0.0) return;
+
+  // Count how many fuel tanks with the current priority level have fuel.
+  // If none, then try next lower priority.  Build the feed list.
+  while ((TanksWithFuel == 0) && (CurrentPriority <= Propulsion->GetNumTanks())) {
+    for (i=0; i<Propulsion->GetNumTanks(); i++) {
+      if (SourceTanks[i] != 0) {
+        Tank = Propulsion->GetTank(i);
+        if (Tank->GetType() == FGTank::ttFUEL) {
+          if ((Tank->GetContents() > 0.0) && ((unsigned int)Tank->GetPriority() == CurrentPriority)) {
+             ++TanksWithFuel;
+             FeedList.push_back(i);
+           } 
+        } else {
+           cerr << "No oxidizer tanks should be used for this engine type." << endl;
         }
       }
-      if (TanksWithFuel == 0) CurrentPriority++;
     }
+    if (TanksWithFuel == 0) CurrentPriority++;
+  }
 
-    // No fuel found at any priority!
-    if (TanksWithFuel == 0) {
-      Starved = true;
-      return;
-    }
+  // No fuel found at any priority!
+  if (TanksWithFuel == 0) {
+    Starved = true;
+    return;
+  }
 
-    // Remove equal amount of fuel from each feed tank.  
-    FuelNeeded = FuelToBurn/TanksWithFuel;
-    for (i=0; i<FeedList.size(); i++) {
-      Tank = Propulsion->GetTank(FeedList[i]);
-      Tank->Drain(FuelNeeded); 
-      FuelToBurn -= FuelNeeded;
-    }
+  // Remove equal amount of fuel from each feed tank.  
+  FuelNeeded = FuelToBurn/TanksWithFuel;
+  for (i=0; i<FeedList.size(); i++) {
+    Tank = Propulsion->GetTank(FeedList[i]);
+    Tank->Drain(FuelNeeded); 
+  }
+  FuelUsedLbs += FuelToBurn;
 
-    // check if we were not able to burn all the fuel we needed to at this priority level
-    if (FuelToBurn > 0.0) {
-      CurrentPriority++;
-      TanksWithFuel = 0;
-      FeedList.clear();
-    }
-  }  // while
 }
 
 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
 double FGEngine::CalcFuelNeed(void)
 {
-  double dT = State->Getdt()*Propulsion->GetRate();
+  double dT = FDMExec->GetDeltaT()*Propulsion->GetRate();
   FuelFlowRate = SLFuelFlowMax*PctPower;
   FuelExpended = FuelFlowRate*dT;
   return FuelExpended;
@@ -315,11 +333,13 @@ bool FGEngine::LoadThruster(Element *thruster_element)
     Thruster = new FGPropeller(FDMExec, document, EngineNumber);
   } else if (thrType == "nozzle") {
     Thruster = new FGNozzle(FDMExec, document, EngineNumber);
+  } else if (thrType == "rotor") {
+    Thruster = new FGRotor(FDMExec, document, EngineNumber);
   } else if (thrType == "direct") {
     Thruster = new FGThruster( FDMExec, document, EngineNumber);
   }
 
-  Thruster->SetdeltaT(State->Getdt() * Propulsion->GetRate());
+  Thruster->SetdeltaT(FDMExec->GetDeltaT() * Propulsion->GetRate());
 
   Debug(2);
   return true;