INCLUDES
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
+#include <iostream>
+#include <fstream>
+#include <cstdlib>
+
#include "FGEngine.h"
#include "FGTank.h"
#include "FGPropeller.h"
#include "FGNozzle.h"
-#include "FGState.h"
-#include "models/FGPropulsion.h"
+#include "FGRotor.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.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;
X = Y = Z = 0.0;
EnginePitch = EngineYaw = 0.0;
SLFuelFlowMax = 0.0;
+ FuelExpended = 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();
- 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");
// 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;
}
- // Build and initialize the feed tank vector.
- for (i=0; i<(Propulsion->GetNumTanks()); i++) {
- SourceTanks.push_back(0);
- }
-
// Load feed tank[s] references
local_element = engine_element->GetParent()->FindElement("feed");
- if (local_element) {
- while (local_element) {
- int tankID = (int)local_element->GetDataAsNumber();
- FGTank* tank = Propulsion->GetTank(tankID);
- AddFeedTank( tankID , tank->GetPriority());
- FuelDensity = tank->GetDensity();
- 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");
}
string property_name, base_property_name;
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);
- //cout << "Engine[" << EngineNumber << "] using fuel density: " << FuelDensity << endl;
+ PostLoad(engine_element, PropertyManager, ::to_string(EngineNumber));
Debug(0);
}
void FGEngine::ResetToIC(void)
{
- Throttle = 0.0;
- Mixture = 1.0;
Starter = false;
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;
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-// 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.
-// This version of the fuel consumption code should never see an oxidizer tank.
-void FGEngine::ConsumeFuel(void)
+double FGEngine::CalcFuelNeed(void)
{
- if (FuelFreeze) return;
- if (TrimMode) return;
-
- unsigned int i;
- double Fshortage, FuelNeeded;
- FGTank* Tank;
- unsigned int TanksWithFuel = 0;
- Fshortage = FuelNeeded = 0.0;
- double FuelToBurn;
- unsigned int CurrentPriority = 1;
- vector <int> FeedList;
- Starved = false;
-
- FuelToBurn = CalcFuelNeed();
- 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++;
- }
-
- // 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);
- }
-
+ FuelFlowRate = SLFuelFlowMax*PctPower;
+ FuelExpended = FuelFlowRate*in.TotalDeltaT;
+ if (!Starved) FuelUsedLbs += FuelExpended;
+ return FuelExpended;
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-double FGEngine::CalcFuelNeed(void)
+unsigned int FGEngine::GetSourceTank(unsigned int i) const
{
- double dT = State->Getdt()*Propulsion->GetRate();
- FuelFlowRate = SLFuelFlowMax*PctPower;
- FuelExpended = FuelFlowRate*dT;
- return FuelExpended;
+ 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);
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-void FGEngine::AddFeedTank(int tkID, int priority)
+double FGEngine::GetThrust(void) const
{
- SourceTanks[tkID] = priority;
+ 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;
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;
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;