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