]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGCoefficient.cpp
builddir -> srcdir so builds can be done outside the master source directory.
[flightgear.git] / src / FDM / JSBSim / FGCoefficient.cpp
1 /*******************************************************************************
2
3  Module:       FGCoefficient.cpp
4  Author:       Jon S. Berndt
5  Date started: 12/28/98
6  Purpose:      Encapsulates the stability derivative class FGCoefficient;
7  Called by:    FGAircraft
8
9  ------------- Copyright (C) 1999  Jon S. Berndt (jsb@hal-pc.org) -------------
10
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
14  version.
15
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
19  details.
20
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.
24
25  Further information about the GNU General Public License can also be found on
26  the world wide web at http://www.gnu.org.
27
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.
32
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.
36
37 See the header file FGCoefficient.h for the values of the identifiers.
38
39 HISTORY
40 --------------------------------------------------------------------------------
41 12/28/98   JSB   Created
42
43 ********************************************************************************
44 INCLUDES
45 *******************************************************************************/
46
47 #include "FGCoefficient.h"
48 #include "FGState.h"
49 #include "FGFDMExec.h"
50
51 /*******************************************************************************
52 ************************************ CODE **************************************
53 *******************************************************************************/
54
55 FGCoefficient::FGCoefficient(FGFDMExec* fdex, FGConfigFile* AC_cfg)
56 {
57   int r, c, start, end, n;
58   float ftrashcan;
59   string multparms;
60
61   FDMExec     = fdex;
62   State       = FDMExec->GetState();
63
64   if (AC_cfg) {
65     name = AC_cfg->GetValue("NAME");
66     method = AC_cfg->GetValue("TYPE");
67
68     AC_cfg->GetNextConfigLine();
69     *AC_cfg >> description;
70
71     cout << "   " << name << endl;
72     cout << "   " << description << endl;
73     cout << "   " << method << endl;
74
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;
79     else                           type = UNKNOWN;
80
81     if (type == VECTOR || type == TABLE) {
82       *AC_cfg >> rows;
83       cout << "   Rows: " << rows << " ";
84       if (type == TABLE) {
85         *AC_cfg >> columns;
86         cout << "Cols: " << columns;
87       }
88
89       cout << endl;
90
91       *AC_cfg >> multparms;
92       if (multparms.substr(0,1) == "F") {
93         LookupR = State->GetParameterIndex(multparms);
94         cout << "   Row indexing parameter: " << multparms << endl;
95       } else {
96         LookupR = atoi(multparms.c_str());
97         cout << "   Row indexing parameter: " << LookupR << endl;
98       }
99
100     }
101
102     if (type == TABLE) {
103       *AC_cfg >> multparms;
104       if (multparms.substr(0,1) == "F") {
105         LookupC = State->GetParameterIndex(multparms);
106         cout << "   Column indexing parameter: " << multparms << endl;
107       } else {
108         LookupC = atoi(multparms.c_str());
109         cout << "   Column indexing parameter: " << LookupC << endl;
110       }
111     }
112
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
116
117     *AC_cfg >> multparms;
118
119     end   = multparms.length();
120     n     = multparms.find("|");
121     start = mult_count = multipliers = 0;
122
123     while (n < end && n >= 0) {
124       n -= start;
125       mult_idx[mult_count] = State->GetParameterIndex(multparms.substr(start,n));
126       multipliers += mult_idx[mult_count];
127       mult_count++;
128       start += n+1;
129       n = multparms.find("|",start);
130     }
131     mult_idx[mult_count] = State->GetParameterIndex(multparms.substr(start,n));
132     multipliers += mult_idx[mult_count];
133     mult_count++;
134
135     // End of non-dimensionalizing parameter read-in
136
137     switch(type) {
138     case VALUE:
139       *AC_cfg >> StaticValue;
140       cout << "      Value = " << StaticValue << endl;
141       break;
142     case VECTOR:
143       Allocate(rows,2);
144
145       for (r=1;r<=rows;r++) {
146         *AC_cfg >> Table3D[r][0];
147         *AC_cfg >> Table3D[r][1];
148       }
149
150       for (r=1;r<=rows;r++) {
151         cout << "       ";
152         for (c=0;c<columns;c++) {
153           cout << Table3D[r][c] << "    ";
154         }
155         cout << endl;
156       }
157
158       break;
159     case TABLE:
160       Allocate(rows, columns);
161
162       Table3D[0][0] = 0.0;
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];
169         }
170       }
171
172       for (r=0;r<=rows;r++) {
173         cout << "       ";
174         for (c=0;c<=columns;c++) {
175           cout << Table3D[r][c] << "    ";
176         }
177         cout << endl;
178       }
179
180       break;
181     }
182     AC_cfg->GetNextConfigLine();
183   }
184 }
185
186 /******************************************************************************/
187
188 FGCoefficient::~FGCoefficient(void)
189 {
190 }
191
192 /******************************************************************************/
193
194 bool FGCoefficient::Allocate(int r, int c)
195 {
196   rows = r;
197   columns = c;
198   Table3D = new float*[r+1];
199   for (int i=0;i<=r;i++) Table3D[i] = new float[c+1];
200   return true;
201 }
202
203 /******************************************************************************/
204
205 bool FGCoefficient::Allocate(int n)
206 {
207   rows = n;
208   columns = 0;
209   Table2D = new float[n+1];
210   return true;
211 }
212
213 /******************************************************************************/
214
215 float FGCoefficient::Value(float rVal, float cVal)
216 {
217   float rFactor, cFactor, col1temp, col2temp, Value;
218   int r, c, midx;
219
220   if (rows < 2 || columns < 2) return 0.0;
221
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;
224
225   c = c < 2 ? 2 : (c > columns ? columns : c);
226   r = r < 2 ? 2 : (r > rows    ? rows    : r);
227
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]);
230
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];
233
234   SD = Value = col1temp + cFactor*(col2temp - col1temp);
235
236   for (midx=0;midx<mult_count;midx++) {
237     Value *= State->GetParameter(mult_idx[midx]);
238   }
239
240   return Value;
241 }
242
243 /******************************************************************************/
244
245 float FGCoefficient::Value(float Val)
246 {
247   float Factor, Value;
248   int r, midx;
249
250   if (rows < 2) return 0.0;
251
252   for (r=1;r<=rows;r++) if (Table3D[r][0] >= Val) break;
253   r = r < 2 ? 2 : (r > rows    ? rows    : r);
254
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]);
258   } else {
259     Factor = 1.0;
260   }
261
262   SD = Value = Factor*(Table3D[r][1] - Table3D[r-1][1]) + Table3D[r-1][1];
263
264   for (midx=0;midx<mult_count;midx++) {
265     Value *= State->GetParameter(mult_idx[midx]);
266   }
267
268   return Value;
269 }
270
271 /******************************************************************************/
272
273 float FGCoefficient::Value(void)
274 {
275         float Value;
276         int midx;
277
278         SD = Value = StaticValue;
279
280   for (midx=0;midx<mult_count;midx++) {
281     Value *= State->GetParameter(mult_idx[midx]);
282   }
283
284   return Value;
285 }
286
287 /******************************************************************************/
288
289 float FGCoefficient::TotalValue()
290 {
291   switch(type) {
292   case 0:
293     return -1;
294   case 1:
295     return (Value());
296   case 2:
297     return (Value(State->GetParameter(LookupR)));
298   case 3:
299     return (Value(State->GetParameter(LookupR),State->GetParameter(LookupC)));
300   case 4:
301     return 0.0;
302   }
303   return 0;
304 }
305
306 /******************************************************************************/
307
308 void FGCoefficient::DumpSD(void)
309 {
310   cout << "   " << name << ": " << SD << endl;
311 }
312
313 /******************************************************************************/
314