]> git.mxchange.org Git - flightgear.git/blobdiff - src/FDM/JSBSim/models/propulsion/FGEngine.cpp
Better fix for a compilation problem with MSVC 2012
[flightgear.git] / src / FDM / JSBSim / models / propulsion / FGEngine.cpp
index cededea8bfbb3db90e0951d8e6efa716cb91814d..7dbec50a4cc5bd20bd747108e1d64b917ffcffab 100644 (file)
@@ -5,7 +5,7 @@
  Date started: 01/21/99
  Called by:    FGAircraft
 
- ------------- Copyright (C) 1999  Jon S. Berndt (jsb@hal-pc.org) -------------
+ ------------- Copyright (C) 1999  Jon S. Berndt (jon@jsbsim.org) -------------
 
  This program is free software; you can redistribute it and/or modify it under
  the terms of the GNU Lesser General Public License as published by the Free Software
@@ -37,25 +37,31 @@ HISTORY
 INCLUDES
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
 
+#include <iostream>
+#include <fstream>
+#include <cstdlib>
+
 #include "FGEngine.h"
 #include "FGTank.h"
 #include "FGPropeller.h"
 #include "FGNozzle.h"
-#include <input_output/FGXMLParse.h>
-#include <math/FGColumnVector3.h>
-#include <fstream>
+#include "FGRotor.h"
+#include "input_output/FGXMLParse.h"
+#include "math/FGColumnVector3.h"
+
+using namespace std;
 
 namespace JSBSim {
 
-static const char *IdSrc = "$Id$";
+static const char *IdSrc = "$Id: FGEngine.cpp,v 1.50 2012/03/17 20:46:29 jentron Exp $";
 static const char *IdHdr = ID_ENGINE;
 
 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 CLASS IMPLEMENTATION
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
 
-FGEngine::FGEngine(FGFDMExec* exec, Element* engine_element, int engine_number)
-                      : EngineNumber(engine_number)
+FGEngine::FGEngine(FGFDMExec* exec, Element* engine_element, int engine_number, struct Inputs& input)
+                      : in(input), EngineNumber(engine_number)
 {
   Element* local_element;
   FGColumnVector3 location, orientation;
@@ -64,25 +70,21 @@ FGEngine::FGEngine(FGFDMExec* exec, Element* engine_element, int engine_number)
   Type = etUnknown;
   X = Y = Z = 0.0;
   EnginePitch = EngineYaw = 0.0;
-  SLFuelFlowMax = SLOxiFlowMax = 0.0;
+  SLFuelFlowMax = 0.0;
+  FuelExpended = 0.0;
   MaxThrottle = 1.0;
   MinThrottle = 0.0;
 
   ResetToIC(); // initialize dynamic terms
 
   FDMExec = exec;
-  State = FDMExec->GetState();
-  Atmosphere = FDMExec->GetAtmosphere();
-  FCS = FDMExec->GetFCS();
-  Propulsion = FDMExec->GetPropulsion();
-  Aircraft = FDMExec->GetAircraft();
-  Propagate = FDMExec->GetPropagate();
-  Auxiliary = FDMExec->GetAuxiliary();
 
   PropertyManager = FDMExec->GetPropertyManager();
 
   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");
@@ -99,26 +101,38 @@ 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;
   }
 
   // Load feed tank[s] references
   local_element = engine_element->GetParent()->FindElement("feed");
-  if (local_element) {
-    while (local_element) {
-      AddFeedTank((int)local_element->GetDataAsNumber());
-      local_element = engine_element->GetParent()->FindNextElement("feed");
-    }
-  } else {
-    cerr << "No feed tank specified in engine definition." << endl;
+  while (local_element) {
+    int tankID = (int)local_element->GetDataAsNumber();
+    SourceTanks.push_back(tankID);
+    local_element = engine_element->GetParent()->FindNextElement("feed");
   }
 
-  char property_name[80];
-  snprintf(property_name, 80, "propulsion/engine[%d]/set-running", EngineNumber);
-  PropertyManager->Tie( property_name, (FGEngine*)this, &FGEngine::GetRunning,
-                                       &FGEngine::SetRunning );
+  string property_name, base_property_name;
+  base_property_name = CreateIndexedPropertyName("propulsion/engine", EngineNumber);
+
+  property_name = base_property_name + "/set-running";
+  PropertyManager->Tie( property_name.c_str(), this, &FGEngine::GetRunning, &FGEngine::SetRunning );
+  property_name = base_property_name + "/thrust-lbs";
+  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-flow-rate-gph";
+  PropertyManager->Tie( property_name.c_str(), this, &FGEngine::GetFuelFlowRateGPH);
+  property_name = base_property_name + "/fuel-used-lbs";
+  PropertyManager->Tie( property_name.c_str(), this, &FGEngine::GetFuelUsedLbs);
+
+  PostLoad(engine_element, PropertyManager, ::to_string(EngineNumber));
 
   Debug(0);
 }
@@ -135,83 +149,42 @@ FGEngine::~FGEngine()
 
 void FGEngine::ResetToIC(void)
 {
-  Thrust = 0.0;
-  Throttle = 0.0;
-  Mixture = 1.0;
   Starter = false;
-  FuelNeed = OxidizerNeed = 0.0;
+  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;
-}
-
-//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-// This base class function should be called from within the
-// derived class' Calculate() function before any other calculations are done.
-// This base class method removes fuel from the fuel tanks as appropriate,
-// and sets the starved flag if necessary.
-
-void FGEngine::ConsumeFuel(void)
-{
-  if (FuelFreeze) return;
-  if (TrimMode) return;
-
-  unsigned int i;
-  double Fshortage, Oshortage, TanksWithFuel, TanksWithOxidizer;
-  FGTank* Tank;
-  bool haveOxTanks = false;
-  Fshortage = Oshortage = TanksWithFuel = TanksWithOxidizer = 0.0;
-
-  // count how many assigned tanks have fuel
-  for (i=0; i<SourceTanks.size(); i++) {
-    Tank = Propulsion->GetTank(SourceTanks[i]);
-    if (Tank->GetType() == FGTank::ttFUEL){
-      if (Tank->GetContents() > 0.0) ++TanksWithFuel;
-    } else if (Tank->GetType() == FGTank::ttOXIDIZER) {
-      haveOxTanks = true;
-      if (Tank->GetContents() > 0.0) ++TanksWithOxidizer;
-    }
-  }
-  if (TanksWithFuel==0 || (haveOxTanks && TanksWithOxidizer==0)) {
-    Starved = true;
-    return;
-  }
-
-  for (i=0; i<SourceTanks.size(); i++) {
-    Tank = Propulsion->GetTank(SourceTanks[i]);
-    if (Tank->GetType() == FGTank::ttFUEL) {
-       Fshortage += Tank->Drain(CalcFuelNeed()/TanksWithFuel);
-    } else if (Tank->GetType() == FGTank::ttOXIDIZER) {
-       Oshortage += Tank->Drain(CalcOxidizerNeed()/TanksWithOxidizer);
-    }
-  }
-
-  if (Fshortage < 0.00 || Oshortage < 0.00) Starved = true;
-  else Starved = false;
+  FuelUsedLbs = 0.0;
 }
 
 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
 double FGEngine::CalcFuelNeed(void)
 {
-  FuelNeed = SLFuelFlowMax*PctPower*State->Getdt()*Propulsion->GetRate();
-  return FuelNeed;
+  FuelFlowRate = SLFuelFlowMax*PctPower;
+  FuelExpended = FuelFlowRate*in.TotalDeltaT;
+  if (!Starved) FuelUsedLbs += FuelExpended;
+  return FuelExpended;
 }
 
 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
-double FGEngine::CalcOxidizerNeed(void)
+unsigned int FGEngine::GetSourceTank(unsigned int i) const
 {
-  OxidizerNeed = SLOxiFlowMax*PctPower*State->Getdt()*Propulsion->GetRate();
-  return OxidizerNeed;
+  if (i >= 0 && i < SourceTanks.size()) {
+    return SourceTanks[i];
+  } else {
+    throw("No such source tank is available for this engine");
+  }
 }
 
 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
-void FGEngine::SetPlacement(FGColumnVector3& location, FGColumnVector3& orientation)
+void FGEngine::SetPlacement(const FGColumnVector3& location,
+                            const FGColumnVector3& orientation)
 {
   X = location(eX);
   Y = location(eY);
@@ -222,32 +195,48 @@ void FGEngine::SetPlacement(FGColumnVector3& location, FGColumnVector3& orientat
 
 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
-void FGEngine::AddFeedTank(int tkID)
+double FGEngine::GetThrust(void) const 
 {
-  SourceTanks.push_back(tkID);
+  return Thruster->GetThrust();
 }
 
 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
-FGColumnVector3& FGEngine::GetBodyForces(void)
+const FGColumnVector3& FGEngine::GetBodyForces(void)
 {
   return Thruster->GetBodyForces();
 }
 
 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
-FGColumnVector3& FGEngine::GetMoments(void)
+const FGColumnVector3& FGEngine::GetMoments(void)
 {
   return Thruster->GetMoments();
 }
 
 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
+void FGEngine::LoadThrusterInputs()
+{
+  Thruster->in.TotalDeltaT     = in.TotalDeltaT;
+  Thruster->in.H_agl           = in.H_agl;
+  Thruster->in.PQR             = in.PQR;
+  Thruster->in.AeroPQR         = in.AeroPQR;
+  Thruster->in.AeroUVW         = in.AeroUVW;
+  Thruster->in.Density         = in.Density;
+  Thruster->in.Pressure        = in.Pressure;
+  Thruster->in.Soundspeed      = in.Soundspeed;
+  Thruster->in.Alpha           = in.alpha;
+  Thruster->in.Beta            = in.beta;
+  Thruster->in.Vt              = in.Vt;
+}
+
+//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
 bool FGEngine::LoadThruster(Element *thruster_element)
 {
   string token, fullpath, localpath;
   string thruster_filename, thruster_fullpathname, thrType;
-  double P_Factor = 0, Sense = 0.0;
   string enginePath = FDMExec->GetEnginePath();
   string aircraftPath = FDMExec->GetFullAircraftPath();
   ifstream thruster_file;
@@ -259,10 +248,10 @@ bool FGEngine::LoadThruster(Element *thruster_element)
 
   thruster_filename = thruster_element->GetAttributeValue("file");
   if ( !thruster_filename.empty()) {
-    thruster_fullpathname = fullpath + thruster_filename + ".xml";
+    thruster_fullpathname = localpath + thruster_filename + ".xml";
     thruster_file.open(thruster_fullpathname.c_str());
     if ( !thruster_file.is_open()) {
-      thruster_fullpathname = localpath + thruster_filename + ".xml";
+      thruster_fullpathname = fullpath + thruster_filename + ".xml";
       thruster_file.open(thruster_fullpathname.c_str());
       if ( !thruster_file.is_open()) {
         cerr << "Could not open thruster file: " << thruster_filename << ".xml" << endl;
@@ -287,11 +276,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(in.TotalDeltaT);
 
   Debug(2);
   return true;