]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGCoefficient.cpp
Check return value of FDM::init().
[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 static const char *IdSrc = "$Header$";
52 static const char *IdHdr = "ID_COEFFICIENT";
53
54 /*******************************************************************************
55 ************************************ CODE **************************************
56 *******************************************************************************/
57
58 FGCoefficient::FGCoefficient(FGFDMExec* fdex, FGConfigFile* AC_cfg)
59 {
60   int r, c, start, end, n;
61   float ftrashcan;
62   string multparms;
63
64   FDMExec     = fdex;
65   State       = FDMExec->GetState();
66   Table = 0;
67
68   if (AC_cfg) {
69     name = AC_cfg->GetValue("NAME");
70     method = AC_cfg->GetValue("TYPE");
71
72     AC_cfg->GetNextConfigLine();
73     *AC_cfg >> description;
74
75     cout << "   " << name << endl;
76     cout << "   " << description << endl;
77     cout << "   " << method << endl;
78
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;
83     else                           type = UNKNOWN;
84
85     if (type == VECTOR || type == TABLE) {
86       *AC_cfg >> rows;
87       cout << "   Rows: " << rows << " ";
88       if (type == TABLE) {
89         *AC_cfg >> columns;
90         cout << "Cols: " << columns;
91       }
92
93       cout << endl;
94
95       *AC_cfg >> multparms;
96       LookupR = State->GetParameterIndex(multparms);
97       cout << "   Row indexing parameter: " << multparms << endl;
98     }
99
100     if (type == TABLE) {
101       *AC_cfg >> multparms;
102       LookupC = State->GetParameterIndex(multparms);
103       cout << "   Column indexing parameter: " << multparms << endl;
104     }
105
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
109
110     *AC_cfg >> multparms;
111
112     end   = multparms.length();
113     n     = multparms.find("|");
114     start = 0;
115
116     while (n < end && n >= 0) {
117       n -= start;
118       multipliers.push_back(State->GetParameterIndex(multparms.substr(start,n)));
119       start += n+1;
120       n = multparms.find("|",start);
121     }
122
123     multipliers.push_back(State->GetParameterIndex(multparms.substr(start,n)));
124
125     // End of non-dimensionalizing parameter read-in
126
127     switch(type) {
128     case VALUE:
129       *AC_cfg >> StaticValue;
130       cout << "      Value = " << StaticValue << endl;
131       break;
132     case VECTOR:
133       Allocate(rows,2);
134
135       for (r=1;r<=rows;r++) {
136         *AC_cfg >> Table[r][0];
137         *AC_cfg >> Table[r][1];
138       }
139
140       for (r=1;r<=rows;r++) {
141         cout << "       ";
142         for (c=0;c<columns;c++) {
143           cout << Table[r][c] << "      ";
144         }
145         cout << endl;
146       }
147
148       break;
149     case TABLE:
150       Allocate(rows, columns);
151
152       Table[0][0] = 0.0;
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];
159         }
160       }
161
162       for (r=0;r<=rows;r++) {
163         cout << "       ";
164         for (c=0;c<=columns;c++) {
165           cout << Table[r][c] << "      ";
166         }
167         cout << endl;
168       }
169
170       break;
171     case EQUATION:
172     case UNKNOWN:
173       cerr << "Unimplemented coefficient type: " << type << endl;
174       break;  
175     }
176     AC_cfg->GetNextConfigLine();
177   }
178 }
179
180 /******************************************************************************/
181
182 FGCoefficient::~FGCoefficient(void) {
183   DeAllocate();
184 }
185
186 /******************************************************************************/
187
188 bool FGCoefficient::DeAllocate(void)
189 {
190   if (Table != NULL ) {
191     for (unsigned int i=0; i<=rows; i++) delete[] Table[i];
192     
193     delete[] Table;
194   } 
195   
196   return true;
197 }
198
199 /******************************************************************************/
200
201 bool FGCoefficient::Allocate(int r, int c)
202 {
203   rows = r;
204   columns = c;
205   Table = new float*[r+1];
206   for (int i=0;i<=r;i++) Table[i] = new float[c+1];
207   return true;
208 }
209
210 /******************************************************************************/
211
212 float FGCoefficient::Value(float rVal, float cVal)
213 {
214   float rFactor, cFactor, col1temp, col2temp, Value;
215   int r, c;
216   unsigned midx;
217
218   if (rows < 2 || columns < 2) return 0.0;
219
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;
222
223   c = c < 2 ? 2 : (c > columns ? columns : c);
224   r = r < 2 ? 2 : (r > rows    ? rows    : r);
225
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]);
228
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];
231
232   SD = Value = col1temp + cFactor*(col2temp - col1temp);
233
234   for (midx=0; midx < multipliers.size(); midx++) {
235     Value *= State->GetParameter(multipliers[midx]);
236   }
237
238   return Value;
239 }
240
241 /******************************************************************************/
242
243 float FGCoefficient::Value(float Val)
244 {
245   
246   
247   float Factor, Value;
248   int r;
249   unsigned midx;
250
251   if (rows < 2) return 0.0;
252
253   for (r=1;r<=rows;r++) if (Table[r][0] >= Val) break;
254   r = r < 2 ? 2 : (r > rows    ? rows    : r);
255
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]);
259   } else {
260     Factor = 1.0;
261   }
262
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]);
266
267   }
268
269   return Value;
270 }
271
272 /******************************************************************************/
273
274 float FGCoefficient::Value(void)
275 {
276         float Value;
277         unsigned midx;
278
279         SD = Value = StaticValue;
280
281   for (midx=0; midx < multipliers.size(); midx++) {
282     Value *= State->GetParameter(multipliers[midx]);
283   }
284
285   return Value;
286 }
287
288 /******************************************************************************/
289
290 float FGCoefficient::TotalValue()
291 {
292   switch(type) {
293   case 0:
294     return -1;
295   case 1:
296     return (Value());
297   case 2:
298     return (Value(State->GetParameter(LookupR)));
299   case 3:
300     return (Value(State->GetParameter(LookupR),State->GetParameter(LookupC)));
301   case 4:
302     return 0.0;
303   }
304   return 0;
305 }
306
307 /******************************************************************************/
308
309 void FGCoefficient::DumpSD(void)
310 {
311   cout << "   " << name << ": " << SD << endl;
312 }
313
314 /******************************************************************************/
315