*******************************************************************************/
#include "FGCoefficient.h"
-#include "FGAtmosphere.h"
#include "FGState.h"
#include "FGFDMExec.h"
-#include "FGFCS.h"
-#include "FGAircraft.h"
-#include "FGTranslation.h"
-#include "FGRotation.h"
-#include "FGPosition.h"
-#include "FGAuxiliary.h"
-#include "FGOutput.h"
+
+static const char *IdSrc = "$Header$";
+static const char *IdHdr = "ID_COEFFICIENT";
/*******************************************************************************
************************************ CODE **************************************
*******************************************************************************/
-FGCoefficient::FGCoefficient(FGFDMExec* fdex, ifstream& coeffDefFile)
+FGCoefficient::FGCoefficient(FGFDMExec* fdex, FGConfigFile* AC_cfg)
{
int r, c, start, end, n;
float ftrashcan;
- string strashcan;
-
- coeffdef["FG_QBAR"] = 1;
- coeffdef["FG_WINGAREA"] = 2;
- coeffdef["FG_WINGSPAN"] = 4;
- coeffdef["FG_CBAR"] = 8;
- coeffdef["FG_ALPHA"] = 16;
- coeffdef["FG_ALPHADOT"] = 32;
- coeffdef["FG_BETA"] = 64;
- coeffdef["FG_BETADOT"] = 128;
- coeffdef["FG_PITCHRATE"] = 256;
- coeffdef["FG_ROLLRATE"] = 512;
- coeffdef["FG_YAWRATE"] = 1024;
- coeffdef["FG_ELEVATOR"] = 2048;
- coeffdef["FG_AILERON"] = 4096;
- coeffdef["FG_RUDDER"] = 8192;
- coeffdef["FG_MACH"] = 16384;
- coeffdef["FG_ALTITUDE"] = 32768L;
- coeffdef["FG_BI2VEL"] = 65536L;
- coeffdef["FG_CI2VEL"] = 131072L;
+ string multparms;
FDMExec = fdex;
State = FDMExec->GetState();
- Atmosphere = FDMExec->GetAtmosphere();
- FCS = FDMExec->GetFCS();
- Aircraft = FDMExec->GetAircraft();
- Translation = FDMExec->GetTranslation();
- Rotation = FDMExec->GetRotation();
- Position = FDMExec->GetPosition();
- Auxiliary = FDMExec->GetAuxiliary();
- Output = FDMExec->GetOutput();
-
- if (coeffDefFile) {
- if (!coeffDefFile.fail()) {
- coeffDefFile >> name;
- cout << " " << name << endl;
- coeffDefFile >> strashcan;
- coeffDefFile >> description;
- cout << " " << description << endl;
- coeffDefFile >> method;
- cout << " " << method << endl;
-
- if (method == "EQUATION") type = EQUATION;
- else if (method == "TABLE") type = TABLE;
- else if (method == "VECTOR") type = VECTOR;
- else if (method == "VALUE") type = VALUE;
- else type = UNKNOWN;
-
- if (type == VECTOR || type == TABLE) {
- coeffDefFile >> rows;
- cout << " Rows: " << rows << " ";
- if (type == TABLE) {
- coeffDefFile >> columns;
- cout << "Cols: " << columns;
- }
+ Table = 0;
- cout << endl;
+ if (AC_cfg) {
+ name = AC_cfg->GetValue("NAME");
+ method = AC_cfg->GetValue("TYPE");
- coeffDefFile >> strashcan;
- if (strashcan.substr(0,1) == "F") {
- LookupR = coeffdef[strashcan.c_str()];
- cout << " Row indexing parameter: " << strashcan << endl;
- } else {
- LookupR = atoi(strashcan.c_str());
- cout << " Row indexing parameter: " << LookupR << endl;
- }
+ AC_cfg->GetNextConfigLine();
+ *AC_cfg >> description;
- }
+ cout << " " << name << endl;
+ cout << " " << description << endl;
+ cout << " " << method << endl;
+
+ if (method == "EQUATION") type = EQUATION;
+ else if (method == "TABLE") type = TABLE;
+ else if (method == "VECTOR") type = VECTOR;
+ else if (method == "VALUE") type = VALUE;
+ else type = UNKNOWN;
+ if (type == VECTOR || type == TABLE) {
+ *AC_cfg >> rows;
+ cout << " Rows: " << rows << " ";
if (type == TABLE) {
- coeffDefFile >> strashcan;
- if (strashcan.substr(0,1) == "F") {
- LookupC = coeffdef[strashcan.c_str()];
- cout << " Column indexing parameter: " << strashcan << endl;
- } else {
- LookupC = atoi(strashcan.c_str());
- cout << " Column indexing parameter: " << LookupC << endl;
- }
+ *AC_cfg >> columns;
+ cout << "Cols: " << columns;
}
- coeffDefFile >> strashcan;
-
- end = strashcan.length();
- n = strashcan.find("|");
- start = 0;
- multipliers = 0;
- if (strashcan.substr(0,1) == "F") {
- while(n < end && n >= 0) {
- n -= start;
- multipliers += coeffdef[strashcan.substr(start,n).c_str()];
- start += n+1;
- n = strashcan.find("|",start);
- }
- multipliers += coeffdef[strashcan.substr(start,end).c_str()];
- } else {
- multipliers = atoi(strashcan.c_str());
- }
+ cout << endl;
- cout << " Non-Dimensionalized by: ";
+ *AC_cfg >> multparms;
+ LookupR = State->GetParameterIndex(multparms);
+ cout << " Row indexing parameter: " << multparms << endl;
+ }
- mult_count = 0;
- if (multipliers & FG_QBAR) {
- mult_idx[mult_count] = FG_QBAR;
- mult_count++;
- cout << "qbar ";
- }
- if (multipliers & FG_WINGAREA) {
- mult_idx[mult_count] = FG_WINGAREA;
- mult_count++;
- cout << "S ";
- }
- if (multipliers & FG_WINGSPAN) {
- mult_idx[mult_count] = FG_WINGSPAN;
- mult_count++;
- cout << "b ";
- }
- if (multipliers & FG_CBAR) {
- mult_idx[mult_count] = FG_CBAR;
- mult_count++;
- cout << "c ";
- }
- if (multipliers & FG_ALPHA) {
- mult_idx[mult_count] = FG_ALPHA;
- mult_count++;
- cout << "alpha ";
- }
- if (multipliers & FG_ALPHADOT) {
- mult_idx[mult_count] = FG_ALPHADOT;
- mult_count++;
- cout << "alphadot ";
- }
- if (multipliers & FG_BETA) {
- mult_idx[mult_count] = FG_BETA;
- mult_count++;
- cout << "beta ";
- }
- if (multipliers & FG_BETADOT) {
- mult_idx[mult_count] = FG_BETADOT;
- mult_count++;
- cout << "betadot ";
- }
- if (multipliers & FG_PITCHRATE) {
- mult_idx[mult_count] = FG_PITCHRATE;
- mult_count++;
- cout << "q ";
- }
- if (multipliers & FG_ROLLRATE) {
- mult_idx[mult_count] = FG_ROLLRATE;
- mult_count++;
- cout << "p ";
- }
- if (multipliers & FG_YAWRATE) {
- mult_idx[mult_count] = FG_YAWRATE;
- mult_count++;
- cout << "r ";
- }
- if (multipliers & FG_ELEVATOR) {
- mult_idx[mult_count] = FG_ELEVATOR;
- mult_count++;
- cout << "De ";
- }
- if (multipliers & FG_AILERON) {
- mult_idx[mult_count] = FG_AILERON;
- mult_count++;
- cout << "Da ";
- }
- if (multipliers & FG_RUDDER) {
- mult_idx[mult_count] = FG_RUDDER;
- mult_count++;
- cout << "Dr ";
- }
- if (multipliers & FG_MACH) {
- mult_idx[mult_count] = FG_MACH;
- mult_count++;
- cout << "Mach ";
- }
- if (multipliers & FG_ALTITUDE) {
- mult_idx[mult_count] = FG_ALTITUDE;
- mult_count++;
- cout << "h ";
- }
- if (multipliers & FG_BI2VEL) {
- mult_idx[mult_count] = FG_BI2VEL;
- mult_count++;
- cout << "b /(2*Vt) ";
- }
- if (multipliers & FG_CI2VEL) {
- mult_idx[mult_count] = FG_CI2VEL;
- mult_count++;
- cout << "c /(2*Vt) ";
- }
- cout << endl;
+ if (type == TABLE) {
+ *AC_cfg >> multparms;
+ LookupC = State->GetParameterIndex(multparms);
+ cout << " Column indexing parameter: " << multparms << endl;
+ }
- switch(type) {
- case VALUE:
- coeffDefFile >> StaticValue;
- cout << " Value = " << StaticValue << endl;
- break;
- case VECTOR:
- Allocate(rows,2);
+ // 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
- for (r=1;r<=rows;r++) {
- coeffDefFile >> Table3D[r][0];
- coeffDefFile >> Table3D[r][1];
- }
+ *AC_cfg >> multparms;
- for (r=0;r<=rows;r++) {
- cout << " ";
- for (c=0;c<=columns;c++) {
- cout << Table3D[r][c] << " ";
- }
- cout << endl;
- }
+ end = multparms.length();
+ n = multparms.find("|");
+ start = 0;
+
+ while (n < end && n >= 0) {
+ n -= start;
+ multipliers.push_back(State->GetParameterIndex(multparms.substr(start,n)));
+ start += n+1;
+ n = multparms.find("|",start);
+ }
- break;
- case TABLE:
- Allocate(rows, columns);
-
- Table3D[0][0] = 0.0;
- for (c=1;c<=columns;c++) {
- coeffDefFile >> Table3D[0][c];
- for (r=1;r<=rows;r++) {
- if ( c==1 ) coeffDefFile >> Table3D[r][0];
- else coeffDefFile >> ftrashcan;
- coeffDefFile >> Table3D[r][c];
- }
+ multipliers.push_back(State->GetParameterIndex(multparms.substr(start,n)));
+
+ // End of non-dimensionalizing parameter read-in
+
+ switch(type) {
+ case 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);
- for (r=0;r<=rows;r++) {
- cout << " ";
- for (c=0;c<=columns;c++) {
- cout << Table3D[r][c] << " ";
- }
- cout << endl;
+ Table[0][0] = 0.0;
+ 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];
}
+ }
- break;
+ for (r=0;r<=rows;r++) {
+ cout << " ";
+ for (c=0;c<=columns;c++) {
+ cout << Table[r][c] << " ";
+ }
+ cout << endl;
}
- } else {
- cerr << "Empty file" << endl;
+
+ break;
+ case EQUATION:
+ case UNKNOWN:
+ cerr << "Unimplemented coefficient type: " << type << endl;
+ break;
}
+ AC_cfg->GetNextConfigLine();
}
}
+/******************************************************************************/
-FGCoefficient::~FGCoefficient(void)
-{
+FGCoefficient::~FGCoefficient(void) {
+ DeAllocate();
}
+/******************************************************************************/
-bool FGCoefficient::Allocate(int r, int c)
+bool FGCoefficient::DeAllocate(void)
{
- rows = r;
- columns = c;
- Table3D = new float*[r+1];
- for (int i=0;i<=r;i++) Table3D[i] = new float[c+1];
+ if (Table != NULL ) {
+ for (unsigned int i=0; i<=rows; i++) delete[] Table[i];
+
+ delete[] Table;
+ }
+
return true;
}
+/******************************************************************************/
-bool FGCoefficient::Allocate(int n)
+bool FGCoefficient::Allocate(int r, int c)
{
- rows = n;
- columns = 0;
- Table2D = new float[n+1];
+ 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)
{
float rFactor, cFactor, col1temp, col2temp, Value;
- int r, c, midx;
+ int r, c;
+ unsigned midx;
if (rows < 2 || columns < 2) return 0.0;
- for (r=1;r<=rows;r++) if (Table3D[r][0] >= rVal) break;
- for (c=1;c<=columns;c++) if (Table3D[0][c] >= cVal) break;
+ for (r=1;r<=rows;r++) if (Table[r][0] >= rVal) break;
+ for (c=1;c<=columns;c++) if (Table[0][c] >= cVal) break;
c = c < 2 ? 2 : (c > columns ? columns : c);
r = r < 2 ? 2 : (r > rows ? rows : r);
- rFactor = (rVal - Table3D[r-1][0]) / (Table3D[r][0] - Table3D[r-1][0]);
- cFactor = (cVal - Table3D[0][c-1]) / (Table3D[0][c] - Table3D[0][c-1]);
+ 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*(Table3D[r][c-1] - Table3D[r-1][c-1]) + Table3D[r-1][c-1];
- col2temp = rFactor*(Table3D[r][c] - Table3D[r-1][c]) + Table3D[r-1][c];
+ 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];
- Value = col1temp + cFactor*(col2temp - col1temp);
+ SD = Value = col1temp + cFactor*(col2temp - col1temp);
- for (midx=0;midx<mult_count;midx++) {
- Value *= GetCoeffVal(mult_idx[midx]);
+ for (midx=0; midx < multipliers.size(); midx++) {
+ Value *= State->GetParameter(multipliers[midx]);
}
return Value;
}
+/******************************************************************************/
float FGCoefficient::Value(float Val)
{
+
+
float Factor, Value;
- int r, midx;
+ int r;
+ unsigned midx;
if (rows < 2) return 0.0;
- for (r=1;r<=rows;r++) if (Table3D[r][0] >= Val) break;
+ 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 (Table3D[r][0] != Table3D[r-1][0]) {
- Factor = (Val - Table3D[r-1][0]) / (Table3D[r][0] - Table3D[r-1][0]);
+ 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*(Table3D[r][1] - Table3D[r-1][1]) + Table3D[r-1][1];
+ SD = Value = Factor*(Table[r][1] - Table[r-1][1]) + Table[r-1][1];
+ for (midx=0; midx < multipliers.size(); midx++) {
+ Value *= State->GetParameter(multipliers[midx]);
- for (midx=0;midx<mult_count;midx++) {
- Value *= GetCoeffVal(mult_idx[midx]);
}
return Value;
}
+/******************************************************************************/
float FGCoefficient::Value(void)
{
float Value;
- int midx;
-
- Value = StaticValue;
+ unsigned midx;
+
+ SD = Value = StaticValue;
- for (midx=0;midx<mult_count;midx++) {
- Value *= GetCoeffVal(mult_idx[midx]);
+ for (midx=0; midx < multipliers.size(); midx++) {
+ Value *= State->GetParameter(multipliers[midx]);
}
return Value;
}
+/******************************************************************************/
+
float FGCoefficient::TotalValue()
{
switch(type) {
case 1:
return (Value());
case 2:
- return (Value(GetCoeffVal(LookupR)));
+ return (Value(State->GetParameter(LookupR)));
case 3:
- return (Value(GetCoeffVal(LookupR),GetCoeffVal(LookupC)));
+ return (Value(State->GetParameter(LookupR),State->GetParameter(LookupC)));
case 4:
return 0.0;
}
return 0;
}
-float FGCoefficient::GetCoeffVal(int val_idx)
+/******************************************************************************/
+
+void FGCoefficient::DumpSD(void)
{
- switch(val_idx) {
- case FG_QBAR:
- return State->Getqbar();
- case FG_WINGAREA:
- return Aircraft->GetWingArea();
- case FG_WINGSPAN:
- return Aircraft->GetWingSpan();
- case FG_CBAR:
- return Aircraft->Getcbar();
- case FG_ALPHA:
- return Translation->Getalpha();
- case FG_ALPHADOT:
- return State->Getadot();
- case FG_BETA:
- return Translation->Getbeta();
- case FG_BETADOT:
- return State->Getbdot();
- case FG_PITCHRATE:
- return Rotation->GetQ();
- case FG_ROLLRATE:
- return Rotation->GetP();
- case FG_YAWRATE:
- return Rotation->GetR();
- case FG_ELEVATOR:
- return FCS->GetDe();
- case FG_AILERON:
- return FCS->GetDa();
- case FG_RUDDER:
- return FCS->GetDr();
- case FG_MACH:
- return State->GetMach();
- case FG_ALTITUDE:
- return State->Geth();
- case FG_BI2VEL:
- return Aircraft->GetWingSpan()/(2.0 * State->GetVt());
- case FG_CI2VEL:
- return Aircraft->Getcbar()/(2.0 * State->GetVt());
- }
- return 0;
+ cout << " " << name << ": " << SD << endl;
}
+/******************************************************************************/
+