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