]> git.mxchange.org Git - flightgear.git/blobdiff - src/FDM/JSBSim/FGCoefficient.cpp
- fixed fuel-need calculations
[flightgear.git] / src / FDM / JSBSim / FGCoefficient.cpp
index d29812e62587a43f56033e1680d98e04986e4282..fa6973a65d9206ad762db94d224c67af38607a2c 100644 (file)
@@ -48,54 +48,56 @@ INCLUDES
 #include "FGState.h"
 #include "FGFDMExec.h"
 
-#include <iomanip.h>
-
-static const char *IdSrc = "$Header$";
-static const char *IdHdr = "ID_COEFFICIENT";
+#ifndef FGFS
+#  if defined(sgi) && !defined(__GNUC__)
+#    include <iomanip.h>
+#  else
+#    include <iomanip>
+#  endif
+#else
+#  include STL_IOMANIP
+#endif
+
+static const char *IdSrc = "$Id$";
+static const char *IdHdr = ID_COEFFICIENT;
 
 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 CLASS IMPLEMENTATION
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
 
-FGCoefficient::FGCoefficient(FGFDMExec* fdex, FGConfigFile* AC_cfg)
+FGCoefficient::FGCoefficient( FGFDMExec* fdex )
 {
-  int r, c, start, end, n;
-  float ftrashcan;
-  string multparms;
-  char nullstring[13] = "            ";
-  
-  bias =0.0;
-  gain = 1.0;
+
+  FDMExec = fdex;
+  State   = FDMExec->GetState();
+  Table   = 0;
   
-  FDMExec     = fdex;
-  State       = FDMExec->GetState();
-  Table = 0;
+  bias=0;
+  gain=1;
 
-  if (AC_cfg) {
-    name = AC_cfg->GetValue("NAME");
-    method = AC_cfg->GetValue("TYPE");
+  if (debug_lvl & 2) cout << "Instantiated: FGCoefficient" << endl;
+}
 
-    AC_cfg->GetNextConfigLine();
-    *AC_cfg >> description;
+//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
-    char bolden[5];
-    char unbolden[5];
+FGCoefficient::~FGCoefficient()
+{
+  if (Table) delete Table;
+  if (debug_lvl & 2) cout << "Destroyed:    FGCoefficient" << endl;
+}
 
-    bolden[0] = 27;
-    bolden[1] = '[';
-    bolden[2] = '1';
-    bolden[3] = 'm';
-    bolden[4] = '\0';
+//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
-    unbolden[0] = 27;
-    unbolden[1] = '[';
-    unbolden[2] = '0';
-    unbolden[3] = 'm';
-    unbolden[4] = '\0';
+bool FGCoefficient::Load(FGConfigFile *AC_cfg)
+{
+  int start, end, n;
+  string mult;
 
-    cout << "   " << bolden << name << unbolden << endl;
-    cout << "   " << description << endl;
-    cout << "   " << method << endl;
+  if (AC_cfg) {
+    name = AC_cfg->GetValue("NAME");
+    method = AC_cfg->GetValue("TYPE");
+    AC_cfg->GetNextConfigLine();
+    *AC_cfg >> description;
 
     if      (method == "EQUATION") type = EQUATION;
     else if (method == "TABLE")    type = TABLE;
@@ -105,23 +107,20 @@ FGCoefficient::FGCoefficient(FGFDMExec* fdex, FGConfigFile* AC_cfg)
 
     if (type == VECTOR || type == TABLE) {
       *AC_cfg >> rows;
-      cout << "   Rows: " << rows << " ";
       if (type == TABLE) {
         *AC_cfg >> columns;
-        cout << "Cols: " << columns;
+        Table = new FGTable(rows, columns);
+      } else {
+        Table = new FGTable(rows);
       }
 
-      cout << endl;
-
-      *AC_cfg >> multparms;
-      LookupR = State->GetParameterIndex(multparms);
-      cout << "   Row indexing parameter: " << multparms << endl;
+      *AC_cfg >> multparmsRow;
+      LookupR = State->GetParameterIndex(multparmsRow);
     }
 
     if (type == TABLE) {
-      *AC_cfg >> multparms;
-      LookupC = State->GetParameterIndex(multparms);
-      cout << "   Column indexing parameter: " << multparms << endl;
+      *AC_cfg >> multparmsCol;
+      LookupC = State->GetParameterIndex(multparmsCol);
     }
 
     // Here, read in the line of the form (e.g.) FG_MACH|FG_QBAR|FG_ALPHA
@@ -134,211 +133,84 @@ FGCoefficient::FGCoefficient(FGFDMExec* fdex, FGConfigFile* AC_cfg)
     n     = multparms.find("|");
     start = 0;
 
-    while (n < end && n >= 0) {
-      n -= start;
+    if (multparms != string("FG_NONE")) {
+      while (n < end && n >= 0) {
+        n -= start;
+        mult = multparms.substr(start,n);
+        multipliers.push_back( State->GetParameterIndex(mult) );
+        start += n+1;
+        n = multparms.find("|",start);
+      }
       multipliers.push_back(State->GetParameterIndex(multparms.substr(start,n)));
-      start += n+1;
-      n = multparms.find("|",start);
+      // End of non-dimensionalizing parameter read-in
     }
 
-    multipliers.push_back(State->GetParameterIndex(multparms.substr(start,n)));
-
-    // End of non-dimensionalizing parameter read-in
-
-    switch(type) {
-    case VALUE:
+    if (type == VALUE) {
       *AC_cfg >> StaticValue;
-      cout << "      Value = " << StaticValue << endl;
-      break;
-    case VECTOR:
-      Allocate(rows,2);
-
-      for (r=1;r<=rows;r++) {
-        *AC_cfg >> Table[r][0];
-        *AC_cfg >> Table[r][1];
-      }
-
-      for (r=1;r<=rows;r++) {
-        cout << "      ";
-        for (c=0;c<columns;c++) {
-          cout << Table[r][c] << "     ";
-        }
-        cout << endl;
-      }
-
-      break;
-    case TABLE:
-      Allocate(rows, columns);
-
-      Table[0][0] = 0.0;
-
-      // Read the table in -- it should be in matrix format with the row
-      // independents as the first column and the column independents in
-      // the first row.  The implication of this layout is that there should
-      // be no value in the upper left corner of the matrix e.g:
-      //      0  10  20 30 ...
-      // -5   1  2   3  4  ...
-      //  ...
-
-      for (r=0; r<=rows; r++) {
-        for (c=0; c <= columns; c++) {
-          if ( !((r == 0) && (c == 0)) ) {
-            *AC_cfg >> Table[r][c];
-          }
-        }
-      }   
-      
-      /* for (c=1;c<=columns;c++) {
-        *AC_cfg >> Table[0][c];
-        for (r=1;r<=rows;r++) {
-          if ( c==1 ) *AC_cfg >> Table[r][0];
-          else *AC_cfg >> ftrashcan;
-          *AC_cfg >> Table[r][c];
-        }
-      } */
-      
-      for (r=0;r<=rows;r++) {
-        cout << "      ";
-        for (c=0;c<=columns;c++) {
-          if( ((r == 0) && (c == 0)) ) {
-             cout << nullstring;
-          } else {
-             cout.flags(ios::left);
-             cout << setw(12) << Table[r][c];
-          }
-        }
-        cout << endl;
-      }
-
-      break;
-    case EQUATION:
-    case UNKNOWN:
+    } else if (type == VECTOR || type == TABLE) {
+      *Table << *AC_cfg;
+    } else {
       cerr << "Unimplemented coefficient type: " << type << endl;
-      break;  
     }
-    AC_cfg->GetNextConfigLine();
-  }
-}
-
-//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-
-FGCoefficient::~FGCoefficient(void) {
-  DeAllocate();
-}
 
-//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+    AC_cfg->GetNextConfigLine();
+    FGCoefficient::Debug(2);
 
-bool FGCoefficient::DeAllocate(void)
-{
-  if (Table != NULL ) {
-    for (unsigned int i=0; i<=rows; i++) delete[] Table[i];
-    
-    delete[] Table;
-  } 
-  
-  return true;
+    return true;
+  } else {
+    return false;
+  }  
 }
 
-//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
-bool FGCoefficient::Allocate(int r, int c)
-{
-  rows = r;
-  columns = c;
-  Table = new float*[r+1];
-  for (int i=0;i<=r;i++) Table[i] = new float[c+1];
-  return true;
-}
 
 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
-float FGCoefficient::Value(float rVal, float cVal)
+double FGCoefficient::Value(double rVal, double cVal)
 {
-  float rFactor, cFactor, col1temp, col2temp, Value;
-  int r, c;
-  unsigned midx;
-
-  if (rows < 2 || columns < 2) return 0.0;
-
-  for (r=1;r<=rows;r++) if (Table[r][0] >= rVal) break;
-  for (c=1;c<=columns;c++) if (Table[0][c] >= cVal) break;
-  //cout << "Value(): rVal: " << rVal << " cVal: " << cVal << endl;
-  //cout << "Value(): r: " << r << " c: " << c << endl;
-  c = c < 2 ? 2 : (c > columns ? columns : c);
-  r = r < 2 ? 2 : (r > rows    ? rows    : r);
-
-  rFactor = (rVal - Table[r-1][0]) / (Table[r][0] - Table[r-1][0]);
-  cFactor = (cVal - Table[0][c-1]) / (Table[0][c] - Table[0][c-1]);
-
-  col1temp = rFactor*(Table[r][c-1] - Table[r-1][c-1]) + Table[r-1][c-1];
-  col2temp = rFactor*(Table[r][c] - Table[r-1][c]) + Table[r-1][c];
+  double Value;
+  unsigned int midx;
 
-  Value = col1temp + cFactor*(col2temp - col1temp);
-  Value = (Value + bias)*gain;
-  SD = Value;
+  SD = Value = gain*Table->GetValue(rVal, cVal) + bias;
   
+
   for (midx=0; midx < multipliers.size(); midx++) {
-    Value *= State->GetParameter(multipliers[midx]);
+      Value *= State->GetParameter(multipliers[midx]);
   }
-
   return Value;
 }
 
 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
-float FGCoefficient::Value(float Val)
+double FGCoefficient::Value(double Val)
 {
+  double Value;
+
+  SD = Value = gain*Table->GetValue(Val) + bias;
   
+  for (unsigned int midx=0; midx < multipliers.size(); midx++) 
+      Value *= State->GetParameter(multipliers[midx]);
   
-  float Factor, Value;
-  int r;
-  unsigned midx;
-
-  if (rows < 2) return 0.0;
-
-  for (r=1;r<=rows;r++) if (Table[r][0] >= Val) break;
-  r = r < 2 ? 2 : (r > rows    ? rows    : r);
-
-  // make sure denominator below does not go to zero.
-  if (Table[r][0] != Table[r-1][0]) {
-    Factor = (Val - Table[r-1][0]) / (Table[r][0] - Table[r-1][0]);
-  } else {
-    Factor = 1.0;
-  }
-
-  Value = Factor*(Table[r][1] - Table[r-1][1]) + Table[r-1][1];
-  Value = (Value + bias)*gain;
-  SD = Value;
-
-  for (midx=0; midx < multipliers.size(); midx++) {
-    Value *= State->GetParameter(multipliers[midx]);
-
-  }
-
   return Value;
 }
 
 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
-float FGCoefficient::Value(void)
+double FGCoefficient::Value(void)
 {
-       float Value;
-       unsigned midx;
+       double Value;
 
-       Value = StaticValue;
-  Value = (Value + bias)*gain;
-  SD = Value;
+  SD = Value = gain*StaticValue + bias;
 
-  for (midx=0; midx < multipliers.size(); midx++) {
+  for (unsigned int midx=0; midx < multipliers.size(); midx++)
     Value *= State->GetParameter(multipliers[midx]);
-  }
 
   return Value;
 }
 
 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
-float FGCoefficient::TotalValue()
+double FGCoefficient::TotalValue()
 {
   switch(type) {
   case 0:
@@ -364,3 +236,99 @@ void FGCoefficient::DumpSD(void)
 
 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
+void FGCoefficient::DisplayCoeffFactors(void)
+{
+  unsigned int i;
+
+  cout << "   Non-Dimensionalized by: ";
+
+  if (multipliers.size() == 0) {
+    cout << "none" << endl;
+  } else {
+    for (i=0; i<multipliers.size(); i++) 
+        cout << FDMExec->GetState()->paramdef[multipliers[i]];
+  }
+  cout << endl;
+}
+
+//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+string FGCoefficient::GetCoefficientValues(void)
+{
+  char buffer[10];
+  string value;
+
+  sprintf(buffer,"%9.6f",SD);
+  value = string(buffer);
+  return value;
+}
+
+//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+//    The bitmasked value choices are as follows:
+//    unset: In this case (the default) JSBSim would only print
+//       out the normally expected messages, essentially echoing
+//       the config files as they are read. If the environment
+//       variable is not set, debug_lvl is set to 1 internally
+//    0: This requests JSBSim not to output any messages
+//       whatsoever.
+//    1: This value explicity requests the normal JSBSim
+//       startup messages
+//    2: This value asks for a message to be printed out when
+//       a class is instantiated
+//    4: When this value is set, a message is displayed when a
+//       FGModel object executes its Run() method
+//    8: When this value is set, various runtime state variables
+//       are printed out periodically
+//    16: When set various parameters are sanity checked and
+//       a message is printed out when they go out of bounds
+
+void FGCoefficient::Debug(int from)
+{
+  if (debug_lvl <= 0) return;
+
+  if (debug_lvl & 1) { // Standard console startup message output
+    
+    if (from == 2) { // Loading
+      cout << "\n   " << highint << underon << name << underoff << normint << endl;
+      cout << "   " << description << endl;
+      cout << "   " << method << endl;
+
+      if (type == VECTOR || type == TABLE) {
+        cout << "   Rows: " << rows << " ";
+        if (type == TABLE) {
+          cout << "Cols: " << columns;
+        }
+        cout << endl << "   Row indexing parameter: " << multparmsRow << endl;
+      }
+
+      if (type == TABLE) {
+        cout << "   Column indexing parameter: " << multparmsCol << endl;
+      }
+
+      if (type == VALUE) {
+        cout << "      Value = " << StaticValue << endl;
+      } else if (type == VECTOR || type == TABLE) {
+        Table->Print();
+      }
+
+      DisplayCoeffFactors();
+    }
+  }
+  if (debug_lvl & 2 ) { // Instantiation/Destruction notification
+    if (from == 0) cout << "Instantiated: FGCoefficient" << endl;
+    if (from == 1) cout << "Destroyed:    FGCoefficient" << endl;
+  }
+  if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
+  }
+  if (debug_lvl & 8 ) { // Runtime state variables
+  }
+  if (debug_lvl & 16) { // Sanity checking
+  }
+  if (debug_lvl & 64) {
+    if (from == 0) { // Constructor
+      cout << IdSrc << endl;
+      cout << IdHdr << endl;
+    }
+  }
+}
+