1 /*******************************************************************************
3 Module: FGCoefficient.cpp
6 Purpose: Encapsulates the stability derivative class FGCoefficient;
9 ------------- Copyright (C) 1999 Jon S. Berndt (jsb@hal-pc.org) -------------
11 This program is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free Software
13 Foundation; either version 2 of the License, or (at your option) any later
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
21 You should have received a copy of the GNU General Public License along with
22 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
23 Place - Suite 330, Boston, MA 02111-1307, USA.
25 Further information about the GNU General Public License can also be found on
26 the world wide web at http://www.gnu.org.
28 FUNCTIONAL DESCRIPTION
29 --------------------------------------------------------------------------------
30 This class models the stability derivative coefficient lookup tables or
31 equations. Note that the coefficients need not be calculated each delta-t.
33 Note that the values in a row which index into the table must be the same value
34 for each column of data, so the first column of numbers for each altitude are
35 seen to be equal, and there are the same number of values for each altitude.
37 See the header file FGCoefficient.h for the values of the identifiers.
40 --------------------------------------------------------------------------------
43 ********************************************************************************
45 *******************************************************************************/
47 #include "FGCoefficient.h"
49 #include "FGFDMExec.h"
51 static const char *IdSrc = "$Header$";
52 static const char *IdHdr = "ID_COEFFICIENT";
54 /*******************************************************************************
55 ************************************ CODE **************************************
56 *******************************************************************************/
58 FGCoefficient::FGCoefficient(FGFDMExec* fdex, FGConfigFile* AC_cfg)
60 int r, c, start, end, n;
65 State = FDMExec->GetState();
69 name = AC_cfg->GetValue("NAME");
70 method = AC_cfg->GetValue("TYPE");
72 AC_cfg->GetNextConfigLine();
73 *AC_cfg >> description;
75 cout << " " << name << endl;
76 cout << " " << description << endl;
77 cout << " " << method << endl;
79 if (method == "EQUATION") type = EQUATION;
80 else if (method == "TABLE") type = TABLE;
81 else if (method == "VECTOR") type = VECTOR;
82 else if (method == "VALUE") type = VALUE;
85 if (type == VECTOR || type == TABLE) {
87 cout << " Rows: " << rows << " ";
90 cout << "Cols: " << columns;
96 LookupR = State->GetParameterIndex(multparms);
97 cout << " Row indexing parameter: " << multparms << endl;
101 *AC_cfg >> multparms;
102 LookupC = State->GetParameterIndex(multparms);
103 cout << " Column indexing parameter: " << multparms << endl;
106 // Here, read in the line of the form (e.g.) FG_MACH|FG_QBAR|FG_ALPHA
107 // where each non-dimensionalizing parameter for this coefficient is
108 // separated by a | character
110 *AC_cfg >> multparms;
112 end = multparms.length();
113 n = multparms.find("|");
116 while (n < end && n >= 0) {
118 multipliers.push_back(State->GetParameterIndex(multparms.substr(start,n)));
120 n = multparms.find("|",start);
123 multipliers.push_back(State->GetParameterIndex(multparms.substr(start,n)));
125 // End of non-dimensionalizing parameter read-in
129 *AC_cfg >> StaticValue;
130 cout << " Value = " << StaticValue << endl;
135 for (r=1;r<=rows;r++) {
136 *AC_cfg >> Table[r][0];
137 *AC_cfg >> Table[r][1];
140 for (r=1;r<=rows;r++) {
142 for (c=0;c<columns;c++) {
143 cout << Table[r][c] << " ";
150 Allocate(rows, columns);
153 for (c=1;c<=columns;c++) {
154 *AC_cfg >> Table[0][c];
155 for (r=1;r<=rows;r++) {
156 if ( c==1 ) *AC_cfg >> Table[r][0];
157 else *AC_cfg >> ftrashcan;
158 *AC_cfg >> Table[r][c];
162 for (r=0;r<=rows;r++) {
164 for (c=0;c<=columns;c++) {
165 cout << Table[r][c] << " ";
173 cerr << "Unimplemented coefficient type: " << type << endl;
176 AC_cfg->GetNextConfigLine();
180 /******************************************************************************/
182 FGCoefficient::~FGCoefficient(void) {
186 /******************************************************************************/
188 bool FGCoefficient::DeAllocate(void)
190 if (Table != NULL ) {
191 for (unsigned int i=0; i<=rows; i++) delete[] Table[i];
199 /******************************************************************************/
201 bool FGCoefficient::Allocate(int r, int c)
205 Table = new float*[r+1];
206 for (int i=0;i<=r;i++) Table[i] = new float[c+1];
210 /******************************************************************************/
212 float FGCoefficient::Value(float rVal, float cVal)
214 float rFactor, cFactor, col1temp, col2temp, Value;
218 if (rows < 2 || columns < 2) return 0.0;
220 for (r=1;r<=rows;r++) if (Table[r][0] >= rVal) break;
221 for (c=1;c<=columns;c++) if (Table[0][c] >= cVal) break;
223 c = c < 2 ? 2 : (c > columns ? columns : c);
224 r = r < 2 ? 2 : (r > rows ? rows : r);
226 rFactor = (rVal - Table[r-1][0]) / (Table[r][0] - Table[r-1][0]);
227 cFactor = (cVal - Table[0][c-1]) / (Table[0][c] - Table[0][c-1]);
229 col1temp = rFactor*(Table[r][c-1] - Table[r-1][c-1]) + Table[r-1][c-1];
230 col2temp = rFactor*(Table[r][c] - Table[r-1][c]) + Table[r-1][c];
232 SD = Value = col1temp + cFactor*(col2temp - col1temp);
234 for (midx=0; midx < multipliers.size(); midx++) {
235 Value *= State->GetParameter(multipliers[midx]);
241 /******************************************************************************/
243 float FGCoefficient::Value(float Val)
251 if (rows < 2) return 0.0;
253 for (r=1;r<=rows;r++) if (Table[r][0] >= Val) break;
254 r = r < 2 ? 2 : (r > rows ? rows : r);
256 // make sure denominator below does not go to zero.
257 if (Table[r][0] != Table[r-1][0]) {
258 Factor = (Val - Table[r-1][0]) / (Table[r][0] - Table[r-1][0]);
263 SD = Value = Factor*(Table[r][1] - Table[r-1][1]) + Table[r-1][1];
264 for (midx=0; midx < multipliers.size(); midx++) {
265 Value *= State->GetParameter(multipliers[midx]);
272 /******************************************************************************/
274 float FGCoefficient::Value(void)
279 SD = Value = StaticValue;
281 for (midx=0; midx < multipliers.size(); midx++) {
282 Value *= State->GetParameter(multipliers[midx]);
288 /******************************************************************************/
290 float FGCoefficient::TotalValue()
298 return (Value(State->GetParameter(LookupR)));
300 return (Value(State->GetParameter(LookupR),State->GetParameter(LookupC)));
307 /******************************************************************************/
309 void FGCoefficient::DumpSD(void)
311 cout << " " << name << ": " << SD << endl;
314 /******************************************************************************/