]> git.mxchange.org Git - flightgear.git/blobdiff - src/FDM/JSBSim/models/propulsion/FGRocket.cpp
Better fix for a compilation problem with MSVC 2012
[flightgear.git] / src / FDM / JSBSim / models / propulsion / FGRocket.cpp
index ada946f9df24c2c1175518a7a6e10e48e553f75f..c5f3e5017409a24eee34d9d8dfff579875443f1f 100644 (file)
@@ -5,7 +5,7 @@
  Date started: 09/12/2000
  Purpose:      This module models a rocket engine
 
- ------------- Copyright (C) 2000  Jon S. Berndt (jsb@hal-pc.org) --------------
+ ------------- Copyright (C) 2000  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
@@ -38,133 +38,266 @@ HISTORY
 INCLUDES
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
 
+#include <iostream>
 #include <sstream>
-
 #include "FGRocket.h"
+#include "FGThruster.h"
+
+using namespace std;
 
 namespace JSBSim {
 
-static const char *IdSrc = "$Id$";
+static const char *IdSrc = "$Id: FGRocket.cpp,v 1.27 2012/04/08 15:19:08 jberndt Exp $";
 static const char *IdHdr = ID_ROCKET;
 
 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 CLASS IMPLEMENTATION
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
 
-FGRocket::FGRocket(FGFDMExec* exec, Element *el, int engine_number)
-  : FGEngine(exec, el, engine_number)
+FGRocket::FGRocket(FGFDMExec* exec, Element *el, int engine_number, struct Inputs& input)
+  : FGEngine(exec, el, engine_number, input), isp_function(0L)
 {
+  Type = etRocket;
   Element* thrust_table_element = 0;
   ThrustTable = 0L;
   BurnTime = 0.0;
+  previousFuelNeedPerTank = 0.0;
+  previousOxiNeedPerTank = 0.0;
+  PropellantFlowRate = 0.0;
+  TotalPropellantExpended = 0.0;
+  FuelFlowRate = FuelExpended = 0.0;
+  OxidizerFlowRate = OxidizerExpended = 0.0;
+  SLOxiFlowMax = SLFuelFlowMax = PropFlowMax = 0.0;
+  MxR = 0.0;
+  BuildupTime = 0.0;
+  It = 0.0;
+  ThrustVariation = 0.0;
+  TotalIspVariation = 0.0;
+  Flameout = false;
 
   // Defaults
-   Variance = 0.0;
    MinThrottle = 0.0;
    MaxThrottle = 1.0;
 
-  if (el->FindElement("shr"))
-    SHR = el->FindElementValueAsNumber("shr");
-  if (el->FindElement("max_pc"))
-    maxPC = el->FindElementValueAsNumberConvertTo("max_pc", "PSF");
-  if (el->FindElement("prop_eff"))
-    propEff = el->FindElementValueAsNumber("prop_eff");
+  string base_property_name = CreateIndexedPropertyName("propulsion/engine", EngineNumber);
+
+  std::stringstream strEngineNumber;
+  strEngineNumber << EngineNumber;
+
+  Element* isp_el = el->FindElement("isp");
+  Element* isp_func_el=0;
+
+  bindmodel(); // Bind model properties first, since they might be needed in functions.
+
+  // Specific impulse may be specified as a constant value or as a function - perhaps as a function of mixture ratio.
+  if (isp_el) {
+    isp_func_el = isp_el->FindElement("function");
+    if (isp_func_el) {
+      isp_function = new FGFunction(exec->GetPropertyManager(),isp_func_el, strEngineNumber.str());
+    } else {
+    Isp = el->FindElementValueAsNumber("isp");
+    }
+  } else {
+    throw("Specific Impulse <isp> must be specified for a rocket engine");
+  }
+  
+  if (el->FindElement("builduptime"))
+    BuildupTime = el->FindElementValueAsNumber("builduptime");
   if (el->FindElement("maxthrottle"))
     MaxThrottle = el->FindElementValueAsNumber("maxthrottle");
   if (el->FindElement("minthrottle"))
     MinThrottle = el->FindElementValueAsNumber("minthrottle");
-  if (el->FindElement("slfuelflowmax"))
+
+  if (el->FindElement("slfuelflowmax")) {
     SLFuelFlowMax = el->FindElementValueAsNumberConvertTo("slfuelflowmax", "LBS/SEC");
-  if (el->FindElement("sloxiflowmax"))
+    if (el->FindElement("sloxiflowmax")) {
     SLOxiFlowMax = el->FindElementValueAsNumberConvertTo("sloxiflowmax", "LBS/SEC");
-  if (el->FindElement("variance"))
-    Variance = el->FindElementValueAsNumber("variance");
+    }
+    PropFlowMax = SLOxiFlowMax + SLFuelFlowMax;
+    MxR = SLOxiFlowMax/SLFuelFlowMax;
+  } else if (el->FindElement("propflowmax")) {
+    PropFlowMax = el->FindElementValueAsNumberConvertTo("propflowmax", "LBS/SEC");
+    // Mixture ratio may be specified here, but it can also be specified as a function or via property
+    if (el->FindElement("mixtureratio")) {
+      MxR = el->FindElementValueAsNumber("mixtureratio");
+    }
+  }
 
+  if (isp_function) Isp = isp_function->GetValue(); // cause Isp function to be executed if present.
+  // If there is a thrust table element, this is a solid propellant engine.
   thrust_table_element = el->FindElement("thrust_table");
   if (thrust_table_element) {
     ThrustTable = new FGTable(PropertyManager, thrust_table_element);
+    Element* variation_element = el->FindElement("variation");
+    if (variation_element) {
+      if (variation_element->FindElement("thrust")) {
+        ThrustVariation = variation_element->FindElementValueAsNumber("thrust");
+      }
+      if (variation_element->FindElement("total_isp")) {
+        TotalIspVariation = variation_element->FindElementValueAsNumber("total_isp");
+      }
+    }
   }
 
-  Debug(0);
 
-  Type = etRocket;
-  Flameout = false;
-
-  PC = 0.0;
-  kFactor = (2.0*SHR*SHR/(SHR-1.0))*pow(2.0/(SHR+1), (SHR+1)/(SHR-1));
+  Debug(0);
 }
 
 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
 FGRocket::~FGRocket(void)
 {
+  delete ThrustTable;
   Debug(1);
 }
 
 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
-double FGRocket::Calculate(void)
+void FGRocket::Calculate(void)
 {
-  double Cf=0;
+  if (FDMExec->IntegrationSuspended()) return;
+
+  RunPreFunctions();
+
+  PropellantFlowRate = (FuelExpended + OxidizerExpended)/in.TotalDeltaT;
+  TotalPropellantExpended += FuelExpended + OxidizerExpended;
+  // If Isp has been specified as a function, override the value of Isp to that, otherwise
+  // assume a constant value is given.
+  if (isp_function) Isp = isp_function->GetValue();
+
+  // If there is a thrust table, it is a function of propellant burned. The
+  // engine is started when the throttle is advanced to 1.0. After that, it
+  // burns without regard to throttle setting.
+
+  if (ThrustTable != 0L) { // Thrust table given -> Solid fuel used
+
+    if ((in.ThrottlePos[EngineNumber] == 1 || BurnTime > 0.0 ) && !Starved) {
+
+      VacThrust = ThrustTable->GetValue(TotalPropellantExpended)
+                * (ThrustVariation + 1)
+                * (TotalIspVariation + 1);
+      if (BurnTime <= BuildupTime && BuildupTime > 0.0) {
+        VacThrust *= sin((BurnTime/BuildupTime)*M_PI/2.0);
+        // VacThrust *= (1-cos((BurnTime/BuildupTime)*M_PI))/2.0; // 1 - cos approach
+      }
+      BurnTime += in.TotalDeltaT; // Increment burn time
+    } else {
+      VacThrust = 0.0;
+    }
+
+  } else { // liquid fueled rocket assumed
+
+    if (in.ThrottlePos[EngineNumber] < MinThrottle || Starved) { // Combustion not supported
 
-  if (!Flameout && !Starved) ConsumeFuel();
+      PctPower = 0.0; // desired thrust
+      Flameout = true;
+      VacThrust = 0.0;
 
-  Throttle = FCS->GetThrottlePos(EngineNumber);
+    } else { // Calculate thrust
 
-  // If there is a thrust table, it is a function of elapsed burn time. The engine
-  // is started when the throttle is advanced to 1.0. After that, it burns
-  // without regard to throttle setting. The table returns a value between zero
-  // and one, representing the percentage of maximum vacuum thrust being applied.
+      // PctPower = Throttle / MaxThrottle; // Min and MaxThrottle range from 0.0 to 1.0, normally.
+      
+      PctPower = in.ThrottlePos[EngineNumber];
+      Flameout = false;
+      VacThrust = Isp * PropellantFlowRate;
 
-  if (ThrustTable != 0L) {
-    if (Throttle == 1 || BurnTime > 0.0) {
-      BurnTime += State->Getdt();
     }
-    Throttle = ThrustTable->GetValue(BurnTime);
-  }
 
-  if (Throttle < MinThrottle || Starved) {
-    PctPower = Thrust = 0.0; // desired thrust
-    Flameout = true;
-    PC = 0.0;
+  } // End thrust calculations
+
+  LoadThrusterInputs();
+  It += Thruster->Calculate(VacThrust) * in.TotalDeltaT;
+
+  RunPostFunctions();
+}
+
+//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+// 
+// The FuelFlowRate can be affected by the TotalIspVariation value (settable
+// in a config file or via properties). The TotalIspVariation parameter affects
+// thrust, but the thrust determines fuel flow rate, so it must be adjusted
+// for Total Isp Variation.
+
+double FGRocket::CalcFuelNeed(void)
+{
+  if (ThrustTable != 0L) {          // Thrust table given - infers solid fuel
+    FuelFlowRate = VacThrust/Isp;   // This calculates wdot (weight flow rate in lbs/sec)
+    FuelFlowRate /= (1 + TotalIspVariation);
   } else {
-    PctPower = Throttle / MaxThrottle;
-    //todo: remove Variance?
-    PC = maxPC*PctPower * (1.0 + Variance * ((double)rand()/(double)RAND_MAX - 0.5));
-    // The Cf (below) is CF from Eqn. 3-30, "Rocket Propulsion Elements", Fifth Edition,
-    // George P. Sutton. Note that the thruster function GetPowerRequired() might
-    // be better called GetResistance() or something; this function returns the
-    // nozzle exit pressure.
-    Cf = sqrt(kFactor*(1 - pow(Thruster->GetPowerRequired()/(PC), (SHR-1)/SHR)));
-    Flameout = false;
+    SLFuelFlowMax = PropFlowMax / (1 + MxR);
+    FuelFlowRate = SLFuelFlowMax * PctPower;
   }
 
-  return Thruster->Calculate(Cf*maxPC*PctPower*propEff);
+  FuelExpended = FuelFlowRate * in.TotalDeltaT; // For this time step ...
+  return FuelExpended;
 }
 
 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
-string FGRocket::GetEngineLabels(string delimeter)
+double FGRocket::CalcOxidizerNeed(void)
+{
+  SLOxiFlowMax = PropFlowMax * MxR / (1 + MxR);
+  OxidizerFlowRate = SLOxiFlowMax * PctPower;
+  OxidizerExpended = OxidizerFlowRate * in.TotalDeltaT;
+  return OxidizerExpended;
+}
+
+//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+string FGRocket::GetEngineLabels(const string& delimiter)
 {
   std::ostringstream buf;
 
-  buf << Name << " Chamber Pressure (engine " << EngineNumber << " in psf)" << delimeter
-      << Thruster->GetThrusterLabels(EngineNumber, delimeter);
+  buf << Name << " Total Impulse (engine " << EngineNumber << " in psf)" << delimiter
+      << Thruster->GetThrusterLabels(EngineNumber, delimiter);
 
   return buf.str();
 }
 
 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
-string FGRocket::GetEngineValues(string delimeter)
+string FGRocket::GetEngineValues(const string& delimiter)
 {
   std::ostringstream buf;
 
-  buf << PC << delimeter << Thruster->GetThrusterValues(EngineNumber, delimeter);
+  buf << It << delimiter << Thruster->GetThrusterValues(EngineNumber, delimiter);
 
   return buf.str();
 }
 
+//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+// This function should tie properties to rocket engine specific properties
+// that are not bound in the base class (FGEngine) code.
+//
+void FGRocket::bindmodel()
+{
+  string property_name, base_property_name;
+  base_property_name = CreateIndexedPropertyName("propulsion/engine", EngineNumber);
+
+  property_name = base_property_name + "/total-impulse";
+  PropertyManager->Tie( property_name.c_str(), this, &FGRocket::GetTotalImpulse);
+  property_name = base_property_name + "/vacuum-thrust_lbs";
+  PropertyManager->Tie( property_name.c_str(), this, &FGRocket::GetVacThrust);
+
+  if (ThrustTable) { // Solid rocket motor
+    property_name = base_property_name + "/thrust-variation_pct";
+    PropertyManager->Tie( property_name.c_str(), this, &FGRocket::GetThrustVariation,
+                                                       &FGRocket::SetThrustVariation);
+    property_name = base_property_name + "/total-isp-variation_pct";
+    PropertyManager->Tie( property_name.c_str(), this, &FGRocket::GetTotalIspVariation,
+                                                       &FGRocket::SetTotalIspVariation);
+  } else { // Liquid rocket motor
+    property_name = base_property_name + "/oxi-flow-rate-pps";
+    PropertyManager->Tie( property_name.c_str(), this, &FGRocket::GetOxiFlowRate);
+    property_name = base_property_name + "/mixture-ratio";
+    PropertyManager->Tie( property_name.c_str(), this, &FGRocket::GetMixtureRatio,
+                                                       &FGRocket::SetMixtureRatio);
+    property_name = base_property_name + "/isp";
+    PropertyManager->Tie( property_name.c_str(), this, &FGRocket::GetIsp,
+                                                       &FGRocket::SetIsp);
+  }
+}
+
 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 //    The bitmasked value choices are as follows:
 //    unset: In this case (the default) JSBSim would only print
@@ -191,14 +324,13 @@ void FGRocket::Debug(int from)
   if (debug_lvl & 1) { // Standard console startup message output
     if (from == 0) { // Constructor
       cout << "      Engine Name: " << Name << endl;
-      cout << "      Specific Heat Ratio = " << SHR << endl;
-      cout << "      Maximum Chamber Pressure = " << maxPC << endl;
-      cout << "      Propulsive Efficiency = " << propEff << endl;
-      cout << "      MaxThrottle = " << MaxThrottle << endl;
-      cout << "      MinThrottle = " << MinThrottle << endl;
-      cout << "      FuelFlowMax = " << SLFuelFlowMax << endl;
-      cout << "      OxiFlowMax = " << SLOxiFlowMax << endl;
-      cout << "      Variance = " << Variance << endl;
+      cout << "      Vacuum Isp = " << Isp << endl;
+      cout << "      Maximum Throttle = " << MaxThrottle << endl;
+      cout << "      Minimum Throttle = " << MinThrottle << endl;
+      cout << "      Fuel Flow (max) = " << SLFuelFlowMax << endl;
+      cout << "      Oxidizer Flow (max) = " << SLOxiFlowMax << endl;
+      if (SLFuelFlowMax > 0)
+        cout << "      Mixture ratio = " << SLOxiFlowMax/SLFuelFlowMax << endl;
     }
   }
   if (debug_lvl & 2 ) { // Instantiation/Destruction notification