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 The coefficient files are located in the axis subdirectory for each aircraft.
34 For instance, for the X-15, you would find subdirectories under the
35 aircraft/X-15/ directory named CLIFT, CDRAG, CSIDE, CROLL, CPITCH, CYAW. Under
36 each of these directories would be files named a, a0, q, and so on. The file
37 named "a" under the CLIFT directory would contain data for the stability
38 derivative modeling lift due to a change in alpha. See the FGAircraft.cpp file
39 for additional information. The coefficient files have the following format:
42 <short description of coefficient with no embedded spaces>
43 <method used in calculating the coefficient: TABLE | EQUATION | VECTOR | VALUE>
44 <parameter identifier for table row (if required)>
45 <parameter identifier for table column (if required)>
46 <OR'ed list of parameter identifiers needed to turn this coefficient into a force>
47 <number of rows in table (if required)>
48 <number of columns in table (if required)>
50 <value of parameter indexing into the column of a table or vector - or value
51 itself for a VALUE coefficient>
52 <values of parameter indexing into row of a table if TABLE type> <Value of
53 coefficient at this row and column>
55 <... repeat above for each column of data in table ...>
57 As an example for the X-15, for the lift due to mach:
96 Note that the values in a row which index into the table must be the same value
97 for each column of data, so the first column of numbers for each altitude are
98 seen to be equal, and there are the same number of values for each altitude.
100 See the header file FGCoefficient.h for the values of the identifiers.
103 --------------------------------------------------------------------------------
106 ********************************************************************************
108 *******************************************************************************/
112 #include "FGCoefficient.h"
114 #include "FGAtmosphere.h"
116 #include "FGFDMExec.h"
118 #include "FGAircraft.h"
119 #include "FGTranslation.h"
120 #include "FGRotation.h"
121 #include "FGPosition.h"
122 #include "FGAuxiliary.h"
123 #include "FGOutput.h"
125 /*******************************************************************************
126 ************************************ CODE **************************************
127 *******************************************************************************/
129 FGCoefficient::FGCoefficient(FGFDMExec* fdex)
132 State = FDMExec->GetState();
133 Atmosphere = FDMExec->GetAtmosphere();
134 FCS = FDMExec->GetFCS();
135 Aircraft = FDMExec->GetAircraft();
136 Translation = FDMExec->GetTranslation();
137 Rotation = FDMExec->GetRotation();
138 Position = FDMExec->GetPosition();
139 Auxiliary = FDMExec->GetAuxiliary();
140 Output = FDMExec->GetOutput();
146 FGCoefficient::FGCoefficient(FGFDMExec* fdex, char* fname)
152 State = FDMExec->GetState();
153 Atmosphere = FDMExec->GetAtmosphere();
154 FCS = FDMExec->GetFCS();
155 Aircraft = FDMExec->GetAircraft();
156 Translation = FDMExec->GetTranslation();
157 Rotation = FDMExec->GetRotation();
158 Position = FDMExec->GetPosition();
159 Auxiliary = FDMExec->GetAuxiliary();
160 Output = FDMExec->GetOutput();
162 ifstream coeffDefFile(fname);
165 if (!coeffDefFile.fail()) {
166 coeffDefFile >> name;
167 coeffDefFile >> description;
168 coeffDefFile >> method;
170 if (strcmp(method,"EQUATION") == 0) type = 4;
171 else if (strcmp(method,"TABLE") == 0) type = 3;
172 else if (strcmp(method,"VECTOR") == 0) type = 2;
173 else if (strcmp(method,"VALUE") == 0) type = 1;
176 if (type == 2 || type == 3) {
177 coeffDefFile >> rows;
179 coeffDefFile >> columns;
181 coeffDefFile >> LookupR;
185 coeffDefFile >> LookupC;
188 coeffDefFile >> multipliers;
191 if (multipliers & FG_QBAR) {
192 mult_idx[mult_count] = FG_QBAR;
195 if (multipliers & FG_WINGAREA) {
196 mult_idx[mult_count] = FG_WINGAREA;
199 if (multipliers & FG_WINGSPAN) {
200 mult_idx[mult_count] = FG_WINGSPAN;
203 if (multipliers & FG_CBAR) {
204 mult_idx[mult_count] = FG_CBAR;
207 if (multipliers & FG_ALPHA) {
208 mult_idx[mult_count] = FG_ALPHA;
211 if (multipliers & FG_ALPHADOT) {
212 mult_idx[mult_count] = FG_ALPHADOT;
215 if (multipliers & FG_BETA) {
216 mult_idx[mult_count] = FG_BETA;
219 if (multipliers & FG_BETADOT) {
220 mult_idx[mult_count] = FG_BETADOT;
223 if (multipliers & FG_PITCHRATE) {
224 mult_idx[mult_count] = FG_PITCHRATE;
227 if (multipliers & FG_ROLLRATE) {
228 mult_idx[mult_count] = FG_ROLLRATE;
231 if (multipliers & FG_YAWRATE) {
232 mult_idx[mult_count] = FG_YAWRATE;
235 if (multipliers & FG_ELEVATOR) {
236 mult_idx[mult_count] = FG_ELEVATOR;
239 if (multipliers & FG_AILERON) {
240 mult_idx[mult_count] = FG_AILERON;
243 if (multipliers & FG_RUDDER) {
244 mult_idx[mult_count] = FG_RUDDER;
247 if (multipliers & FG_MACH) {
248 mult_idx[mult_count] = FG_MACH;
251 if (multipliers & FG_ALTITUDE) {
252 mult_idx[mult_count] = FG_ALTITUDE;
258 coeffDefFile >> StaticValue;
263 for (r=1;r<=rows;r++) {
264 coeffDefFile >> Table3D[r][0];
265 coeffDefFile >> Table3D[r][1];
269 Allocate(rows, columns);
271 for (c=1;c<=columns;c++) {
272 coeffDefFile >> Table3D[0][c];
273 for (r=1;r<=rows;r++) {
274 if ( c==1 ) coeffDefFile >> Table3D[r][0];
275 else coeffDefFile >> ftrashcan;
276 coeffDefFile >> Table3D[r][c];
282 cerr << "Empty file" << endl;
284 coeffDefFile.close();
289 FGCoefficient::FGCoefficient(FGFDMExec* fdex, int r, int c)
292 State = FDMExec->GetState();
293 Atmosphere = FDMExec->GetAtmosphere();
294 FCS = FDMExec->GetFCS();
295 Aircraft = FDMExec->GetAircraft();
296 Translation = FDMExec->GetTranslation();
297 Rotation = FDMExec->GetRotation();
298 Position = FDMExec->GetPosition();
299 Auxiliary = FDMExec->GetAuxiliary();
300 Output = FDMExec->GetOutput();
308 FGCoefficient::FGCoefficient(FGFDMExec* fdex, int n)
311 State = FDMExec->GetState();
312 Atmosphere = FDMExec->GetAtmosphere();
313 FCS = FDMExec->GetFCS();
314 Aircraft = FDMExec->GetAircraft();
315 Translation = FDMExec->GetTranslation();
316 Rotation = FDMExec->GetRotation();
317 Position = FDMExec->GetPosition();
318 Auxiliary = FDMExec->GetAuxiliary();
319 Output = FDMExec->GetOutput();
327 FGCoefficient::~FGCoefficient(void)
332 bool FGCoefficient::Allocate(int r, int c)
336 Table3D = new float*[r+1];
337 for (int i=0;i<=r;i++) Table3D[i] = new float[c+1];
342 bool FGCoefficient::Allocate(int n)
346 Table2D = new float[n+1];
351 float FGCoefficient::Value(float rVal, float cVal)
353 float rFactor, cFactor, col1temp, col2temp, Value;
356 if (rows < 2 || columns < 2) return 0.0;
358 for (r=1;r<=rows;r++) if (Table3D[r][0] >= rVal) break;
359 for (c=1;c<=columns;c++) if (Table3D[0][c] >= cVal) break;
361 c = c < 2 ? 2 : (c > columns ? columns : c);
362 r = r < 2 ? 2 : (r > rows ? rows : r);
364 rFactor = (rVal - Table3D[r-1][0]) / (Table3D[r][0] - Table3D[r-1][0]);
365 cFactor = (cVal - Table3D[0][c-1]) / (Table3D[0][c] - Table3D[0][c-1]);
367 col1temp = rFactor*(Table3D[r][c-1] - Table3D[r-1][c-1]) + Table3D[r-1][c-1];
368 col2temp = rFactor*(Table3D[r][c] - Table3D[r-1][c]) + Table3D[r-1][c];
370 Value = col1temp + cFactor*(col2temp - col1temp);
372 for (midx=0;midx<mult_count;midx++) {
373 Value *= GetCoeffVal(mult_idx[midx]);
380 float FGCoefficient::Value(float Val)
385 if (rows < 2) return 0.0;
387 for (r=1;r<=rows;r++) if (Table3D[r][0] >= Val) break;
388 r = r < 2 ? 2 : (r > rows ? rows : r);
390 // make sure denominator below does not go to zero.
391 if (Table3D[r][0] != Table3D[r-1][0]) {
392 Factor = (Val - Table3D[r-1][0]) / (Table3D[r][0] - Table3D[r-1][0]);
396 Value = Factor*(Table3D[r][1] - Table3D[r-1][1]) + Table3D[r-1][1];
398 for (midx=0;midx<mult_count;midx++) {
399 Value *= GetCoeffVal(mult_idx[midx]);
406 float FGCoefficient::Value()
412 return (StaticValue);
414 return (Value(GetCoeffVal(LookupR)));
416 return (Value(GetCoeffVal(LookupR),GetCoeffVal(LookupC)));
423 float FGCoefficient::GetCoeffVal(int val_idx)
427 return State->Getqbar();
429 return Aircraft->GetWingArea();
431 return Aircraft->GetWingSpan();
433 return Aircraft->Getcbar();
435 return Translation->Getalpha();
437 return State->Getadot();
439 return Translation->Getbeta();
441 return State->Getbdot();
443 return Rotation->GetQ();
445 return Rotation->GetP();
447 return Rotation->GetR();
455 return State->GetMach();
457 return State->Geth();