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