]> git.mxchange.org Git - flightgear.git/blobdiff - src/FDM/JSBSim/FGCoefficient.cpp
Encapsulate the interpolstion version of FGEnvironment and fix some bugs
[flightgear.git] / src / FDM / JSBSim / FGCoefficient.cpp
index 7714e7eaf4c3f5d775857ecfdb20a97991ff8903..66111bc401448f6a9ce0162334710e1b52027f7a 100644 (file)
@@ -44,13 +44,15 @@ HISTORY
 INCLUDES
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
 
+#include <stdio.h>
+
 #include "FGCoefficient.h"
 #include "FGState.h"
 #include "FGFDMExec.h"
 #include "FGPropertyManager.h"
 
 #ifndef FGFS
-#  if defined(sgi) && !defined(__GNUC__)
+#  if defined(sgi) && !defined(__GNUC__) && (_COMPILER_VERSION < 740)
 #    include <iomanip.h>
 #  else
 #    include <iomanip>
@@ -59,6 +61,8 @@ INCLUDES
 #  include STL_IOMANIP
 #endif
 
+namespace JSBSim {
+
 static const char *IdSrc = "$Id$";
 static const char *IdHdr = ID_COEFFICIENT;
 
@@ -68,21 +72,32 @@ CLASS IMPLEMENTATION
 
 FGCoefficient::FGCoefficient( FGFDMExec* fdex )
 {
-
   FDMExec = fdex;
   State   = FDMExec->GetState();
   Table   = 0;
   
   PropertyManager = FDMExec->GetPropertyManager();
   
-  bias=0;
-  gain=1;
-  
+  Table = (FGTable*)0L;
   LookupR = LookupC = 0;
-  
-  totalValue = 0;
-
-  if (debug_lvl & 2) cout << "Instantiated: FGCoefficient" << endl;
+  numInstances = 0;
+  rows = columns = 0;
+
+  StaticValue  = 0.0;
+  totalValue   = 0.0;
+  bias = 0.0;
+  gain = 1.0;
+  SD = 0.0;
+
+  filename.erase();
+  description.erase();
+  name.erase();
+  method.erase();
+  multparms.erase();
+  multparmsRow.erase();
+  multparmsCol.erase();
+
+  Debug(0);
 }
 
 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -90,7 +105,7 @@ FGCoefficient::FGCoefficient( FGFDMExec* fdex )
 FGCoefficient::~FGCoefficient()
 {
   if (Table) delete Table;
-  if (debug_lvl & 2) cout << "Destroyed:    FGCoefficient" << endl;
+  Debug(1);
 }
 
 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -98,14 +113,13 @@ FGCoefficient::~FGCoefficient()
 bool FGCoefficient::Load(FGConfigFile *AC_cfg)
 {
   int start, end, n;
-  string mult,prop;
+  string mult;
 
   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;
     else if (method == "VECTOR")   type = VECTOR;
@@ -122,43 +136,46 @@ bool FGCoefficient::Load(FGConfigFile *AC_cfg)
       }
 
       *AC_cfg >> multparmsRow;
-      prop = State->GetPropertyName( State->GetParameterIndex(multparmsRow) );
-      LookupR = PropertyManager->GetNode( prop );
+      LookupR = PropertyManager->GetNode( multparmsRow );
     }
 
     if (type == TABLE) {
       *AC_cfg >> multparmsCol;
-      prop = State->GetPropertyName( State->GetParameterIndex(multparmsCol) );
 
-      LookupC = PropertyManager->GetNode( prop );
+      LookupC = PropertyManager->GetNode( multparmsCol );
     }
 
     // Here, read in the line of the form (e.g.) FG_MACH|FG_QBAR|FG_ALPHA
     // where each non-dimensionalizing parameter for this coefficient is
     // separated by a | character
 
-    *AC_cfg >> multparms;
+    string line=AC_cfg->GetCurrentLine();
+    unsigned j=0;
+    char tmp[255];
+    for(unsigned i=0;i<line.length(); i++ ) {
+      if( !isspace(line[i]) ) {
+        tmp[j]=line[i];
+        j++;
+      }
+    } 
+    tmp[j]='\0'; multparms=tmp;  
+    end  = multparms.length();
 
-    end   = multparms.length();
     n     = multparms.find("|");
     start = 0;
-
-    if (multparms != string("FG_NONE")) {
+    if (multparms != string("none")) {
       while (n < end && n >= 0) {
         n -= start;
         mult = multparms.substr(start,n);
-        prop= State->GetPropertyName( State->GetParameterIndex(mult) );
-        multipliers.push_back( PropertyManager->GetNode(prop) );
+        multipliers.push_back( resolveSymbol( mult ) );
         start += n+1;
         n = multparms.find("|",start);
       }
-      prop=State->GetPropertyName( 
-            State->GetParameterIndex( multparms.substr(start,n) ) );
       mult = multparms.substr(start,n);
-      multipliers.push_back( PropertyManager->GetNode(prop) );
+      multipliers.push_back( resolveSymbol( mult ) );
       // End of non-dimensionalizing parameter read-in
     }
-
+    AC_cfg->GetNextConfigLine();
     if (type == VALUE) {
       *AC_cfg >> StaticValue;
     } else if (type == VECTOR || type == TABLE) {
@@ -186,7 +203,6 @@ double FGCoefficient::Value(double rVal, double cVal)
   unsigned int midx;
 
   SD = Value = gain*Table->GetValue(rVal, cVal) + bias;
-  
 
   for (midx=0; midx < multipliers.size(); midx++) {
       Value *= multipliers[midx]->getDoubleValue();
@@ -199,7 +215,7 @@ double FGCoefficient::Value(double rVal, double cVal)
 double FGCoefficient::Value(double Val)
 {
   double Value;
-
+  
   SD = Value = gain*Table->GetValue(Val) + bias;
   
   for (unsigned int midx=0; midx < multipliers.size(); midx++) 
@@ -227,31 +243,29 @@ double FGCoefficient::Value(void)
 double FGCoefficient::TotalValue(void)
 {
   switch(type) {
-  case 0:
-    totalValue=-1;
-    return totalValue;
-  case 1:
-    totalValue=Value();
-    return totalValue;
-  case 2:
-    totalValue=Value( LookupR->getDoubleValue() );
-    return totalValue;
-  case 3:
-    totalValue=Value( LookupR->getDoubleValue(),
-                        LookupC->getDoubleValue() );
-    return totalValue;
-  case 4:
-    totalValue=0.0;
-    return totalValue;
-  }
-  return totalValue;
-}
 
-//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+  case UNKNOWN:
+    totalValue = -1;
+    break;
 
-void FGCoefficient::DumpSD(void)
-{
-  cout << "   " << name << ": " << SD << endl;
+  case VALUE:
+    totalValue = Value();
+    break;
+
+  case VECTOR:
+    totalValue = Value( LookupR->getDoubleValue() );
+    break;
+
+  case TABLE:
+    totalValue = Value( LookupR->getDoubleValue(),
+                      LookupC->getDoubleValue() );
+    break;
+
+  case EQUATION:
+    totalValue = 0.0;
+    break;
+  }
+  return totalValue;
 }
 
 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -273,9 +287,9 @@ void FGCoefficient::DisplayCoeffFactors(void)
 
 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
-string FGCoefficient::GetCoefficientValues(void)
+string FGCoefficient::GetSDstring(void)
 {
-  char buffer[10];
+  char buffer[16];
   string value;
 
   sprintf(buffer,"%9.6f",SD);
@@ -285,23 +299,24 @@ string FGCoefficient::GetCoefficientValues(void)
 
 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
-void FGCoefficient::bind(FGPropertyManager *parent) {
+void FGCoefficient::bind(FGPropertyManager *parent)
+{
   string mult;
   unsigned i;
   
-  node=parent->GetNode(name,true);
+  node = parent->GetNode(name,true);
   
   node->SetString("description",description);
-  if(LookupR) node->SetString("row-parm",LookupR->getName() );
-  if(LookupC) node->SetString("column-parm",LookupC->getName() );
+  if (LookupR) node->SetString("row-parm",LookupR->getName() );
+  if (LookupC) node->SetString("column-parm",LookupC->getName() );
   
   mult="";
-  if(multipliers.size() == 0) 
+  if (multipliers.size() == 0) 
     mult="none";
     
   for (i=0; i<multipliers.size(); i++) {
       mult += multipliers[i]->getName();
-      if( i < multipliers.size()-1 ) mult += " "; 
+      if ( i < multipliers.size()-1 ) mult += " "; 
   }
   node->SetString("multipliers",mult);
   
@@ -318,12 +333,28 @@ void FGCoefficient::bind(FGPropertyManager *parent) {
 
 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
-void FGCoefficient::unbind(void) {
+void FGCoefficient::unbind(void)
+{
   node->Untie("SD-norm");
   node->Untie("value-lbs"); 
   node->Untie("bias");  
   node->Untie("gain");
-}  
+}
+
+//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+FGPropertyManager* FGCoefficient::resolveSymbol(string name)
+{
+  FGPropertyManager* tmpn;
+
+  tmpn = PropertyManager->GetNode(name,false);
+  if ( !tmpn ) {
+    cerr << "Coefficient multipliers cannot create properties, check spelling?" << endl;
+    exit(1);
+  } 
+  return tmpn; 
+}
+
 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 //    The bitmasked value choices are as follows:
 //    unset: In this case (the default) JSBSim would only print
@@ -393,3 +424,4 @@ void FGCoefficient::Debug(int from)
   }
 }
 
+} // namespace JSBSim