]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGTable.cpp
Updates from JSBSim, including new turbine engine model from David Culp
[flightgear.git] / src / FDM / JSBSim / FGTable.cpp
1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2
3  Module:       FGTable.cpp
4  Author:       Jon S. Berndt
5  Date started: 1/9/2001
6  Purpose:      Models a lookup table
7
8  ------------- Copyright (C) 2001  Jon S. Berndt (jsb@hal-pc.org) -------------
9
10  This program is free software; you can redistribute it and/or modify it under
11  the terms of the GNU General Public License as published by the Free Software
12  Foundation; either version 2 of the License, or (at your option) any later
13  version.
14
15  This program is distributed in the hope that it will be useful, but WITHOUT
16  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17  FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
18  details.
19
20  You should have received a copy of the GNU General Public License along with
21  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
22  Place - Suite 330, Boston, MA  02111-1307, USA.
23
24  Further information about the GNU General Public License can also be found on
25  the world wide web at http://www.gnu.org.
26
27 FUNCTIONAL DESCRIPTION
28 --------------------------------------------------------------------------------
29 Models a lookup table
30
31 HISTORY
32 --------------------------------------------------------------------------------
33 JSB  1/9/00          Created
34
35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
36 INCLUDES
37 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
38
39 #include "FGTable.h"
40
41 #if defined ( sgi ) && !defined( __GNUC__ )
42 #include <iomanip.h>
43 #else
44 #include <iomanip>
45 #endif
46
47 using namespace std;
48
49 namespace JSBSim {
50
51 static const char *IdSrc = "$Id$";
52 static const char *IdHdr = ID_TABLE;
53
54 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
55 CLASS IMPLEMENTATION
56 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
57
58
59 FGTable::FGTable(int NRows, int NCols) : nRows(NRows), nCols(NCols)
60 {
61   if (NCols > 1) {
62     Type = tt2D;
63     colCounter = 1;
64     rowCounter = 0;
65   } else if (NCols == 1) {
66     Type = tt1D;
67     colCounter = 0;
68     rowCounter = 1;
69   } else {
70     cerr << "FGTable cannot accept 'Rows=0'" << endl;
71   }
72
73   Data = Allocate();
74   lastRowIndex=lastColumnIndex=2;
75   Debug(0);
76 }
77
78 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
79
80 FGTable::FGTable(int NRows) : nRows(NRows), nCols(1)
81 {
82   Type = tt1D;
83   colCounter = 0;
84   rowCounter = 1;
85
86   Data = Allocate();
87   Debug(0);
88   lastRowIndex=lastColumnIndex=2;
89 }
90
91 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
92
93 double** FGTable::Allocate(void)
94 {
95   Data = new double*[nRows+1];
96   for (int r=0; r<=nRows; r++) {
97     Data[r] = new double[nCols+1];
98     for (int c=0; c<=nCols; c++) {
99       Data[r][c] = 0.0;
100     }
101   }
102   return Data;
103 }
104
105 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
106
107 FGTable::~FGTable()
108 {
109   for (int r=0; r<=nRows; r++) if (Data[r]) delete[] Data[r];
110   if (Data) delete[] Data;
111   Debug(1);
112 }
113
114 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
115
116 double FGTable::GetValue(double key)
117 {
118   double Factor, Value, Span;
119   int r=lastRowIndex;
120   
121   //if the key is off the end of the table, just return the 
122   //end-of-table value, do not extrapolate
123   if( key <= Data[1][0] ) {
124     lastRowIndex=2;
125     //cout << "Key underneath table: " << key << endl;
126     return Data[1][1];
127   } else if ( key >= Data[nRows][0] ) {
128     lastRowIndex=nRows;
129     //cout << "Key over table: " << key << endl;
130     return Data[nRows][1];
131   }    
132
133   // the key is somewhere in the middle, search for the right breakpoint
134   // assume the correct breakpoint has not changed since last frame or
135   // has only changed very little
136   
137   if ( r > 2 && Data[r-1][0] > key ) {
138     while( Data[r-1][0] > key && r > 2) { r--; }
139   } else if ( Data[r][0] < key ) { 
140     while( Data[r][0] <= key && r <= nRows) { r++; }  
141   }  
142   
143   lastRowIndex=r;  
144   // make sure denominator below does not go to zero.
145   
146   Span = Data[r][0] - Data[r-1][0];
147   if (Span != 0.0) {
148     Factor = (key - Data[r-1][0]) / Span;
149     if (Factor > 1.0) Factor = 1.0;
150   } else {
151     Factor = 1.0;
152   }
153
154   Value = Factor*(Data[r][1] - Data[r-1][1]) + Data[r-1][1];
155
156   return Value;
157 }
158
159 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
160
161
162 double FGTable::GetValue(double rowKey, double colKey)
163 {
164   double rFactor, cFactor, col1temp, col2temp, Value;
165   int r=lastRowIndex;
166   int c=lastColumnIndex;
167   
168   if ( r > 2 && Data[r-1][0] > rowKey ) {
169     while ( Data[r-1][0] > rowKey && r > 2) { r--; }
170   } else if ( Data[r][0] < rowKey ) { 
171 //    cout << Data[r][0] << endl;
172     while ( r <= nRows && Data[r][0] <= rowKey ) { r++; }
173     if ( r > nRows ) r = nRows;  
174   }  
175   
176   if ( c > 2 && Data[0][c-1] > colKey ) {
177     while( Data[0][c-1] > colKey && c > 2) { c--; }
178   } else if ( Data[0][c] < colKey ) { 
179     while( Data[0][c] <= colKey && c <= nCols) { c++; } 
180     if ( c > nCols ) c = nCols;  
181   }  
182
183   lastRowIndex=r;
184   lastColumnIndex=c;
185   
186   rFactor = (rowKey - Data[r-1][0]) / (Data[r][0] - Data[r-1][0]);
187   cFactor = (colKey - Data[0][c-1]) / (Data[0][c] - Data[0][c-1]);
188
189   if (rFactor > 1.0) rFactor = 1.0;
190   else if (rFactor < 0.0) rFactor = 0.0;
191
192   if (cFactor > 1.0) cFactor = 1.0;
193   else if (cFactor < 0.0) cFactor = 0.0;
194
195   col1temp = rFactor*(Data[r][c-1] - Data[r-1][c-1]) + Data[r-1][c-1];
196   col2temp = rFactor*(Data[r][c] - Data[r-1][c]) + Data[r-1][c];
197
198   Value = col1temp + cFactor*(col2temp - col1temp);
199
200   return Value;
201 }
202
203 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
204
205 void FGTable::operator<<(FGConfigFile& infile)
206 {
207   int startRow;
208
209   if (Type == tt1D) startRow = 1;
210   else startRow = 0;
211
212   for (int r=startRow; r<=nRows; r++) {
213     for (int c=0; c<=nCols; c++) {
214       if (r != 0 || c != 0) {
215         infile >> Data[r][c];
216       }
217     }
218   }
219 }
220
221 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
222
223 FGTable& FGTable::operator<<(const double n)
224 {
225   Data[rowCounter][colCounter] = n;
226   if (colCounter == nCols) {
227     colCounter = 0;
228     rowCounter++;
229   } else {
230     colCounter++;
231   }
232   return *this;
233 }
234
235 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
236
237 FGTable& FGTable::operator<<(const int n)
238 {
239   *this << (double)n;
240   return *this;
241 }
242
243 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
244
245 void FGTable::Print(void)
246 {
247   int startRow;
248
249   if (Type == tt1D) startRow = 1;
250   else startRow = 0;
251
252 #if defined (sgi) && !defined(__GNUC__)
253   unsigned long flags = cout.setf(ios::fixed);
254 #else
255   ios::fmtflags flags = cout.setf(ios::fixed); // set up output stream
256 #endif
257
258   cout.precision(4);
259
260   for (int r=startRow; r<=nRows; r++) {
261     cout << "   ";
262     for (int c=0; c<=nCols; c++) {
263       if (r == 0 && c == 0) {
264       cout << " ";
265       } else {
266       cout << Data[r][c] << "   ";
267       }
268     }
269     cout << endl;
270   }
271   cout.setf(flags); // reset
272 }
273
274 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
275 //    The bitmasked value choices are as follows:
276 //    unset: In this case (the default) JSBSim would only print
277 //       out the normally expected messages, essentially echoing
278 //       the config files as they are read. If the environment
279 //       variable is not set, debug_lvl is set to 1 internally
280 //    0: This requests JSBSim not to output any messages
281 //       whatsoever.
282 //    1: This value explicity requests the normal JSBSim
283 //       startup messages
284 //    2: This value asks for a message to be printed out when
285 //       a class is instantiated
286 //    4: When this value is set, a message is displayed when a
287 //       FGModel object executes its Run() method
288 //    8: When this value is set, various runtime state variables
289 //       are printed out periodically
290 //    16: When set various parameters are sanity checked and
291 //       a message is printed out when they go out of bounds
292
293 void FGTable::Debug(int from)
294 {
295   if (debug_lvl <= 0) return;
296
297   if (debug_lvl & 1) { // Standard console startup message output
298     if (from == 0) { // Constructor
299
300     }
301   }
302   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
303     if (from == 0) cout << "Instantiated: FGTable" << endl;
304     if (from == 1) cout << "Destroyed:    FGTable" << endl;
305   }
306   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
307   }
308   if (debug_lvl & 8 ) { // Runtime state variables
309   }
310   if (debug_lvl & 16) { // Sanity checking
311   }
312   if (debug_lvl & 64) {
313     if (from == 0) { // Constructor
314       cout << IdSrc << endl;
315       cout << IdHdr << endl;
316     }
317   }
318 }
319 }