]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGCoefficient.cpp
Latest JSBSim 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 #  if defined(sgi) && !defined(__GNUC__)
53 #    include <iomanip.h>
54 #  else
55 #    include <iomanip>
56 #  endif
57 #else
58 #  include STL_IOMANIP
59 #endif
60
61 static const char *IdSrc = "$Id$";
62 static const char *IdHdr = ID_COEFFICIENT;
63
64 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
65 CLASS IMPLEMENTATION
66 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
67
68 FGCoefficient::FGCoefficient( FGFDMExec* fdex )
69 {
70
71   FDMExec = fdex;
72   State   = FDMExec->GetState();
73   Table   = 0;
74
75   if (debug_lvl & 2) cout << "Instantiated: FGCoefficient" << endl;
76 }
77
78 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
79
80 FGCoefficient::~FGCoefficient()
81 {
82   if (Table) delete Table;
83   if (debug_lvl & 2) cout << "Destroyed:    FGCoefficient" << endl;
84 }
85
86 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
87
88 bool FGCoefficient::Load(FGConfigFile *AC_cfg)
89 {
90   int start, end, n;
91   string mult;
92
93   if (AC_cfg) {
94     name = AC_cfg->GetValue("NAME");
95     method = AC_cfg->GetValue("TYPE");
96     AC_cfg->GetNextConfigLine();
97     *AC_cfg >> description;
98
99     if      (method == "EQUATION") type = EQUATION;
100     else if (method == "TABLE")    type = TABLE;
101     else if (method == "VECTOR")   type = VECTOR;
102     else if (method == "VALUE")    type = VALUE;
103     else                           type = UNKNOWN;
104
105     if (type == VECTOR || type == TABLE) {
106       *AC_cfg >> rows;
107       if (type == TABLE) {
108         *AC_cfg >> columns;
109         Table = new FGTable(rows, columns);
110       } else {
111         Table = new FGTable(rows);
112       }
113
114       *AC_cfg >> multparmsRow;
115       LookupR = State->GetParameterIndex(multparmsRow);
116     }
117
118     if (type == TABLE) {
119       *AC_cfg >> multparmsCol;
120       LookupC = State->GetParameterIndex(multparmsCol);
121     }
122
123     // Here, read in the line of the form (e.g.) FG_MACH|FG_QBAR|FG_ALPHA
124     // where each non-dimensionalizing parameter for this coefficient is
125     // separated by a | character
126
127     *AC_cfg >> multparms;
128
129     end   = multparms.length();
130     n     = multparms.find("|");
131     start = 0;
132
133     if (multparms != string("FG_NONE")) {
134       while (n < end && n >= 0) {
135         n -= start;
136         mult = multparms.substr(start,n);
137         multipliers.push_back( State->GetParameterIndex(mult) );
138         start += n+1;
139         n = multparms.find("|",start);
140       }
141       multipliers.push_back(State->GetParameterIndex(multparms.substr(start,n)));
142       // End of non-dimensionalizing parameter read-in
143     }
144
145     if (type == VALUE) {
146       *AC_cfg >> StaticValue;
147     } else if (type == VECTOR || type == TABLE) {
148       *Table << *AC_cfg;
149     } else {
150       cerr << "Unimplemented coefficient type: " << type << endl;
151     }
152
153     AC_cfg->GetNextConfigLine();
154     Debug(2);
155
156     return true;
157   } else {
158     return false;
159   }  
160 }
161
162
163
164 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
165
166 double FGCoefficient::Value(double rVal, double cVal)
167 {
168   double Value;
169   unsigned int midx;
170
171   SD = Value = Table->GetValue(rVal, cVal);
172
173   for (midx=0; midx < multipliers.size(); midx++) {
174       Value *= State->GetParameter(multipliers[midx]);
175   }
176   return Value;
177 }
178
179 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
180
181 double FGCoefficient::Value(double Val)
182 {
183   double Value;
184
185   SD = Value = Table->GetValue(Val);
186   
187   for (unsigned int midx=0; midx < multipliers.size(); midx++) 
188       Value *= State->GetParameter(multipliers[midx]);
189   
190   return Value;
191 }
192
193 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
194
195 double FGCoefficient::Value(void)
196 {
197         double Value;
198
199   SD = Value = StaticValue;
200
201   for (unsigned int midx=0; midx < multipliers.size(); midx++)
202     Value *= State->GetParameter(multipliers[midx]);
203
204   return Value;
205 }
206
207 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
208
209 double FGCoefficient::TotalValue()
210 {
211   switch(type) {
212   case 0:
213     return -1;
214   case 1:
215     return (Value());
216   case 2:
217     return (Value(State->GetParameter(LookupR)));
218   case 3:
219     return (Value(State->GetParameter(LookupR),State->GetParameter(LookupC)));
220   case 4:
221     return 0.0;
222   }
223   return 0;
224 }
225
226 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
227
228 void FGCoefficient::DumpSD(void)
229 {
230   cout << "   " << name << ": " << SD << endl;
231 }
232
233 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
234
235 void FGCoefficient::DisplayCoeffFactors(void)
236 {
237   unsigned int i;
238
239   cout << "   Non-Dimensionalized by: ";
240
241   if (multipliers.size() == 0) {
242     cout << "none" << endl;
243   } else {
244     for (i=0; i<multipliers.size(); i++) 
245         cout << FDMExec->GetState()->paramdef[multipliers[i]];
246   }
247   cout << endl;
248 }
249
250 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
251
252 string FGCoefficient::GetCoefficientValues(void)
253 {
254   char buffer[10];
255   string value;
256
257   sprintf(buffer,"%9.6f",SD);
258   value = string(buffer);
259   return value;
260 }
261
262 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
263 //    The bitmasked value choices are as follows:
264 //    unset: In this case (the default) JSBSim would only print
265 //       out the normally expected messages, essentially echoing
266 //       the config files as they are read. If the environment
267 //       variable is not set, debug_lvl is set to 1 internally
268 //    0: This requests JSBSim not to output any messages
269 //       whatsoever.
270 //    1: This value explicity requests the normal JSBSim
271 //       startup messages
272 //    2: This value asks for a message to be printed out when
273 //       a class is instantiated
274 //    4: When this value is set, a message is displayed when a
275 //       FGModel object executes its Run() method
276 //    8: When this value is set, various runtime state variables
277 //       are printed out periodically
278 //    16: When set various parameters are sanity checked and
279 //       a message is printed out when they go out of bounds
280
281 void FGCoefficient::Debug(int from)
282 {
283   if (debug_lvl <= 0) return;
284
285   if (debug_lvl & 1) { // Standard console startup message output
286     if (from == 2) { // Loading
287       cout << "\n   " << highint << underon << name << underoff << normint << endl;
288       cout << "   " << description << endl;
289       cout << "   " << method << endl;
290
291       if (type == VECTOR || type == TABLE) {
292         cout << "   Rows: " << rows << " ";
293         if (type == TABLE) {
294           cout << "Cols: " << columns;
295         }
296         cout << endl << "   Row indexing parameter: " << multparmsRow << endl;
297       }
298
299       if (type == TABLE) {
300         cout << "   Column indexing parameter: " << multparmsCol << endl;
301       }
302
303       if (type == VALUE) {
304         cout << "      Value = " << StaticValue << endl;
305       } else if (type == VECTOR || type == TABLE) {
306         Table->Print();
307       }
308
309       DisplayCoeffFactors();
310     }
311   }
312   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
313     if (from == 0) cout << "Instantiated: FGCoefficient" << endl;
314     if (from == 1) cout << "Destroyed:    FGCoefficient" << endl;
315   }
316   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
317   }
318   if (debug_lvl & 8 ) { // Runtime state variables
319   }
320   if (debug_lvl & 16) { // Sanity checking
321   }
322 }
323