]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGCoefficient.cpp
Sync with latest JSBSim CVS
[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   int start, end, n;
90   string multparms, mult;
91
92   if (AC_cfg) {
93     name = AC_cfg->GetValue("NAME");
94     method = AC_cfg->GetValue("TYPE");
95     AC_cfg->GetNextConfigLine();
96     *AC_cfg >> description;
97     if (debug_lvl > 0) {
98       cout << "\n   " << highint << underon << name << underoff << normint << endl;
99       cout << "   " << description << endl;
100       cout << "   " << method << endl;
101     }
102
103     if      (method == "EQUATION") type = EQUATION;
104     else if (method == "TABLE")    type = TABLE;
105     else if (method == "VECTOR")   type = VECTOR;
106     else if (method == "VALUE")    type = VALUE;
107     else                           type = UNKNOWN;
108
109     if (type == VECTOR || type == TABLE) {
110       *AC_cfg >> rows;
111       if (debug_lvl > 0) cout << "   Rows: " << rows << " ";
112       if (type == TABLE) {
113         *AC_cfg >> columns;
114         if (debug_lvl > 0) cout << "Cols: " << columns;
115         Table = new FGTable(rows, columns);
116       } else {
117         Table = new FGTable(rows);
118       }
119
120       if (debug_lvl > 0) cout << endl;
121
122       *AC_cfg >> multparms;
123       LookupR = State->GetParameterIndex(multparms);
124       if (debug_lvl > 0) cout << "   Row indexing parameter: " << multparms << endl;
125     }
126
127     if (type == TABLE) {
128       *AC_cfg >> multparms;
129       LookupC = State->GetParameterIndex(multparms);
130       if (debug_lvl > 0) cout << "   Column indexing parameter: " << multparms << endl;
131     }
132
133     // Here, read in the line of the form (e.g.) FG_MACH|FG_QBAR|FG_ALPHA
134     // where each non-dimensionalizing parameter for this coefficient is
135     // separated by a | character
136
137     *AC_cfg >> multparms;
138
139     end   = multparms.length();
140     n     = multparms.find("|");
141     start = 0;
142
143     if(multparms != "FG_NONE") {
144       while (n < end && n >= 0) {
145         n -= start;
146         mult = multparms.substr(start,n);
147         multipliers.push_back( State->GetParameterIndex(mult) );
148         start += n+1;
149         n = multparms.find("|",start);
150       }
151       multipliers.push_back(State->GetParameterIndex(multparms.substr(start,n)));
152       // End of non-dimensionalizing parameter read-in
153     }
154
155     switch(type) {
156     case VALUE:
157       *AC_cfg >> StaticValue;
158       if (debug_lvl > 0) cout << "      Value = " << StaticValue << endl;
159       break;
160     case VECTOR:
161     case TABLE:
162       *Table << *AC_cfg;
163       if (debug_lvl > 0) Table->Print();
164       break;
165     case EQUATION:
166     case UNKNOWN:
167       cerr << "Unimplemented coefficient type: " << type << endl;
168       break;
169     }
170     AC_cfg->GetNextConfigLine();
171     if (debug_lvl > 0) DisplayCoeffFactors();
172     return true;
173   } else {
174     return false;
175   }  
176 }
177
178
179
180 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
181
182 float FGCoefficient::Value(float rVal, float cVal)
183 {
184   float Value;
185   unsigned int midx;
186
187   SD = Value = Table->GetValue(rVal, cVal);
188
189   for (midx=0; midx < multipliers.size(); midx++) {
190       Value *= State->GetParameter(multipliers[midx]);
191   }
192   return Value;
193 }
194
195 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
196
197 float FGCoefficient::Value(float Val)
198 {
199   float Value;
200
201   SD = Value = Table->GetValue(Val);
202   
203   for (unsigned int midx=0; midx < multipliers.size(); midx++) 
204       Value *= State->GetParameter(multipliers[midx]);
205   
206   return Value;
207 }
208
209 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
210
211 float FGCoefficient::Value(void)
212 {
213         float Value;
214
215   SD = Value = StaticValue;
216
217   for (unsigned int midx=0; midx < multipliers.size(); midx++)
218     Value *= State->GetParameter(multipliers[midx]);
219
220   return Value;
221 }
222
223 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
224
225 float FGCoefficient::TotalValue()
226 {
227   
228   switch(type) {
229   case 0:
230     return -1;
231   case 1:
232     return (Value());
233   case 2:
234     return (Value(State->GetParameter(LookupR)));
235   case 3:
236     return (Value(State->GetParameter(LookupR),State->GetParameter(LookupC)));
237   case 4:
238     return 0.0;
239   }
240   return 0;
241 }
242
243 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
244
245 void FGCoefficient::DumpSD(void)
246 {
247   cout << "   " << name << ": " << SD << endl;
248 }
249
250 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
251
252 void FGCoefficient::Debug(void)
253 {
254     //TODO: Add your source code here
255 }
256
257 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
258
259 void FGCoefficient::DisplayCoeffFactors(void)
260 {
261   unsigned int i;
262
263   cout << "   Non-Dimensionalized by: ";
264
265   if( multipliers.size() == 0) {
266     cout << "none" << endl;
267   } else {
268     for (i=0; i<multipliers.size();i++) 
269         cout << FDMExec->GetState()->paramdef[multipliers[i]];
270   }
271   cout << endl;
272 }
273
274 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
275
276 string FGCoefficient::GetCoefficientValues(void) {
277   char buffer[10];
278   string value;
279
280   sprintf(buffer,"%9.6f",SD);
281   value = string(buffer);
282   return value;
283 }
284
285 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
286