]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGCoefficient.cpp
Added recent ground effect modeling changes.
[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 #ifndef FGFS
52 #  include <iomanip>
53 #else
54 #  include STL_IOMANIP
55 #endif
56
57 static const char *IdSrc = "$Id$";
58 static const char *IdHdr = ID_COEFFICIENT;
59
60 extern char highint[5];
61 extern char halfint[5];
62 extern char normint[6];
63 extern char reset[5];
64 extern char underon[5];
65 extern char underoff[6];
66 extern char fgblue[6];
67 extern char fgcyan[6];
68 extern char fgred[6];
69 extern char fggreen[6];
70 extern char fgdef[6];
71
72 extern short debug_lvl;
73
74 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
75 CLASS IMPLEMENTATION
76 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
77
78 FGCoefficient::FGCoefficient( FGFDMExec* fdex )
79 {
80
81   FDMExec = fdex;
82   State   = FDMExec->GetState();
83   Table   = 0;
84
85   if (debug_lvl & 2) cout << "Instantiated: FGCoefficient" << endl;
86 }
87
88 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
89
90 FGCoefficient::~FGCoefficient()
91 {
92   if (Table) delete Table;
93   if (debug_lvl & 2) cout << "Destroyed:    FGCoefficient" << endl;
94 }
95
96 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
97
98 bool FGCoefficient::Load(FGConfigFile *AC_cfg) {
99   int start, end, n;
100   string multparms, mult;
101
102   if (AC_cfg) {
103     name = AC_cfg->GetValue("NAME");
104     method = AC_cfg->GetValue("TYPE");
105     AC_cfg->GetNextConfigLine();
106     *AC_cfg >> description;
107     if (debug_lvl > 0) {
108       cout << "\n   " << highint << underon << name << underoff << normint << endl;
109       cout << "   " << description << endl;
110       cout << "   " << method << endl;
111     }
112
113     if      (method == "EQUATION") type = EQUATION;
114     else if (method == "TABLE")    type = TABLE;
115     else if (method == "VECTOR")   type = VECTOR;
116     else if (method == "VALUE")    type = VALUE;
117     else                           type = UNKNOWN;
118
119     if (type == VECTOR || type == TABLE) {
120       *AC_cfg >> rows;
121       if (debug_lvl > 0) cout << "   Rows: " << rows << " ";
122       if (type == TABLE) {
123         *AC_cfg >> columns;
124         if (debug_lvl > 0) cout << "Cols: " << columns;
125         Table = new FGTable(rows, columns);
126       } else {
127         Table = new FGTable(rows);
128       }
129
130       if (debug_lvl > 0) cout << endl;
131
132       *AC_cfg >> multparms;
133       LookupR = State->GetParameterIndex(multparms);
134       if (debug_lvl > 0) cout << "   Row indexing parameter: " << multparms << endl;
135     }
136
137     if (type == TABLE) {
138       *AC_cfg >> multparms;
139       LookupC = State->GetParameterIndex(multparms);
140       if (debug_lvl > 0) cout << "   Column indexing parameter: " << multparms << endl;
141     }
142
143     // Here, read in the line of the form (e.g.) FG_MACH|FG_QBAR|FG_ALPHA
144     // where each non-dimensionalizing parameter for this coefficient is
145     // separated by a | character
146
147     *AC_cfg >> multparms;
148
149     end   = multparms.length();
150     n     = multparms.find("|");
151     start = 0;
152
153     if(multparms != "FG_NONE") {
154       while (n < end && n >= 0) {
155         n -= start;
156         mult = multparms.substr(start,n);
157         multipliers.push_back( State->GetParameterIndex(mult) );
158         start += n+1;
159         n = multparms.find("|",start);
160       }
161       multipliers.push_back(State->GetParameterIndex(multparms.substr(start,n)));
162       // End of non-dimensionalizing parameter read-in
163     }
164
165     switch(type) {
166     case VALUE:
167       *AC_cfg >> StaticValue;
168       if (debug_lvl > 0) cout << "      Value = " << StaticValue << endl;
169       break;
170     case VECTOR:
171     case TABLE:
172       *Table << *AC_cfg;
173       if (debug_lvl > 0) Table->Print();
174       break;
175     case EQUATION:
176     case UNKNOWN:
177       cerr << "Unimplemented coefficient type: " << type << endl;
178       break;
179     }
180     AC_cfg->GetNextConfigLine();
181     if (debug_lvl > 0) DisplayCoeffFactors();
182     return true;
183   } else {
184     return false;
185   }  
186 }
187
188
189
190 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
191
192 float FGCoefficient::Value(float rVal, float cVal)
193 {
194   float Value;
195   unsigned int midx;
196
197   SD = Value = Table->GetValue(rVal, cVal);
198
199   for (midx=0; midx < multipliers.size(); midx++) {
200       Value *= State->GetParameter(multipliers[midx]);
201   }
202   return Value;
203 }
204
205 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
206
207 float FGCoefficient::Value(float Val)
208 {
209   float Value;
210
211   SD = Value = Table->GetValue(Val);
212   
213   for (unsigned int midx=0; midx < multipliers.size(); midx++) 
214       Value *= State->GetParameter(multipliers[midx]);
215   
216   return Value;
217 }
218
219 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
220
221 float FGCoefficient::Value(void)
222 {
223         float Value;
224
225   SD = Value = StaticValue;
226
227   for (unsigned int midx=0; midx < multipliers.size(); midx++)
228     Value *= State->GetParameter(multipliers[midx]);
229
230   return Value;
231 }
232
233 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
234
235 float FGCoefficient::TotalValue()
236 {
237   
238   switch(type) {
239   case 0:
240     return -1;
241   case 1:
242     return (Value());
243   case 2:
244     return (Value(State->GetParameter(LookupR)));
245   case 3:
246     return (Value(State->GetParameter(LookupR),State->GetParameter(LookupC)));
247   case 4:
248     return 0.0;
249   }
250   return 0;
251 }
252
253 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
254
255 void FGCoefficient::DumpSD(void)
256 {
257   cout << "   " << name << ": " << SD << endl;
258 }
259
260 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
261
262 void FGCoefficient::Debug(void)
263 {
264     //TODO: Add your source code here
265 }
266
267 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
268
269 void FGCoefficient::DisplayCoeffFactors(void)
270 {
271   unsigned int i;
272
273   cout << "   Non-Dimensionalized by: ";
274
275   if( multipliers.size() == 0) {
276     cout << "none" << endl;
277   } else {
278     for (i=0; i<multipliers.size();i++) 
279         cout << FDMExec->GetState()->paramdef[multipliers[i]];
280   }
281   cout << endl;
282 }
283
284 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
285
286 string FGCoefficient::GetCoefficientValues(void) {
287   char buffer[10];
288   string value;
289   //value = ", ";
290   snprintf(buffer,10,"%9.6f",SD);
291   value += string(buffer);
292   return value;
293 }
294
295 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%