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 /*******************************************************************************
52 ************************************ CODE **************************************
53 *******************************************************************************/
55 FGCoefficient::FGCoefficient(FGFDMExec* fdex, FGConfigFile* AC_cfg)
57 int r, c, start, end, n;
62 State = FDMExec->GetState();
65 name = AC_cfg->GetValue("NAME");
66 method = AC_cfg->GetValue("TYPE");
68 AC_cfg->GetNextConfigLine();
69 *AC_cfg >> description;
71 cout << " " << name << endl;
72 cout << " " << description << endl;
73 cout << " " << method << endl;
75 if (method == "EQUATION") type = EQUATION;
76 else if (method == "TABLE") type = TABLE;
77 else if (method == "VECTOR") type = VECTOR;
78 else if (method == "VALUE") type = VALUE;
81 if (type == VECTOR || type == TABLE) {
83 cout << " Rows: " << rows << " ";
86 cout << "Cols: " << columns;
92 if (multparms.substr(0,1) == "F") {
93 LookupR = State->GetParameterIndex(multparms);
94 cout << " Row indexing parameter: " << multparms << endl;
96 LookupR = atoi(multparms.c_str());
97 cout << " Row indexing parameter: " << LookupR << endl;
103 *AC_cfg >> multparms;
104 if (multparms.substr(0,1) == "F") {
105 LookupR = State->GetParameterIndex(multparms);
106 cout << " Column indexing parameter: " << multparms << endl;
108 LookupC = atoi(multparms.c_str());
109 cout << " Column indexing parameter: " << LookupC << endl;
113 // Here, read in the line of the form (e.g.) FG_MACH|FG_QBAR|FG_ALPHA
114 // where each non-dimensionalizing parameter for this coefficient is
115 // separated by a | character
117 *AC_cfg >> multparms;
119 end = multparms.length();
120 n = multparms.find("|");
121 start = mult_count = multipliers = 0;
123 while(n < end && n >= 0) {
125 mult_idx[mult_count] = State->GetParameterIndex(multparms.substr(start,n));
126 multipliers += mult_idx[mult_count];
129 n = multparms.find("|",start);
131 mult_idx[mult_count] = State->GetParameterIndex(multparms.substr(start,n));
132 multipliers += mult_idx[mult_count];
135 // End of non-dimensionalizing parameter read-in
139 *AC_cfg >> StaticValue;
140 cout << " Value = " << StaticValue << endl;
145 for (r=1;r<=rows;r++) {
146 *AC_cfg >> Table3D[r][0];
147 *AC_cfg >> Table3D[r][1];
150 for (r=1;r<=rows;r++) {
152 for (c=0;c<columns;c++) {
153 cout << Table3D[r][c] << " ";
160 Allocate(rows, columns);
163 for (c=1;c<=columns;c++) {
164 *AC_cfg >> Table3D[0][c];
165 for (r=1;r<=rows;r++) {
166 if ( c==1 ) *AC_cfg >> Table3D[r][0];
167 else *AC_cfg >> ftrashcan;
168 *AC_cfg >> Table3D[r][c];
172 for (r=0;r<=rows;r++) {
174 for (c=0;c<=columns;c++) {
175 cout << Table3D[r][c] << " ";
182 AC_cfg->GetNextConfigLine();
186 /******************************************************************************/
188 FGCoefficient::~FGCoefficient(void)
192 /******************************************************************************/
194 bool FGCoefficient::Allocate(int r, int c)
198 Table3D = new float*[r+1];
199 for (int i=0;i<=r;i++) Table3D[i] = new float[c+1];
203 /******************************************************************************/
205 bool FGCoefficient::Allocate(int n)
209 Table2D = new float[n+1];
213 /******************************************************************************/
215 float FGCoefficient::Value(float rVal, float cVal)
217 float rFactor, cFactor, col1temp, col2temp, Value;
220 if (rows < 2 || columns < 2) return 0.0;
222 for (r=1;r<=rows;r++) if (Table3D[r][0] >= rVal) break;
223 for (c=1;c<=columns;c++) if (Table3D[0][c] >= cVal) break;
225 c = c < 2 ? 2 : (c > columns ? columns : c);
226 r = r < 2 ? 2 : (r > rows ? rows : r);
228 rFactor = (rVal - Table3D[r-1][0]) / (Table3D[r][0] - Table3D[r-1][0]);
229 cFactor = (cVal - Table3D[0][c-1]) / (Table3D[0][c] - Table3D[0][c-1]);
231 col1temp = rFactor*(Table3D[r][c-1] - Table3D[r-1][c-1]) + Table3D[r-1][c-1];
232 col2temp = rFactor*(Table3D[r][c] - Table3D[r-1][c]) + Table3D[r-1][c];
234 SD = Value = col1temp + cFactor*(col2temp - col1temp);
236 for (midx=0;midx<mult_count;midx++) {
237 Value *= State->GetParameter(mult_idx[midx]);
243 /******************************************************************************/
245 float FGCoefficient::Value(float Val)
250 if (rows < 2) return 0.0;
252 for (r=1;r<=rows;r++) if (Table3D[r][0] >= Val) break;
253 r = r < 2 ? 2 : (r > rows ? rows : r);
255 // make sure denominator below does not go to zero.
256 if (Table3D[r][0] != Table3D[r-1][0]) {
257 Factor = (Val - Table3D[r-1][0]) / (Table3D[r][0] - Table3D[r-1][0]);
262 SD = Value = Factor*(Table3D[r][1] - Table3D[r-1][1]) + Table3D[r-1][1];
264 for (midx=0;midx<mult_count;midx++) {
265 Value *= State->GetParameter(mult_idx[midx]);
271 /******************************************************************************/
273 float FGCoefficient::Value(void)
278 SD = Value = StaticValue;
280 for (midx=0;midx<mult_count;midx++) {
281 Value *= State->GetParameter(mult_idx[midx]);
287 /******************************************************************************/
289 float FGCoefficient::TotalValue()
297 return (Value(State->GetParameter(LookupR)));
299 return (Value(State->GetParameter(LookupR),State->GetParameter(LookupC)));
306 /******************************************************************************/
308 void FGCoefficient::DumpSD(void)
310 cout << " " << name << ": " << SD << endl;
313 /******************************************************************************/