]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/math/FGTable.cpp
f285117dee747431b2cf4b01bee3757bcc408b81
[flightgear.git] / src / FDM / JSBSim / math / 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__ ) && (_COMPILER_VERSION < 740)
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 FGTable::FGTable(int NRows) : nRows(NRows), nCols(1), PropertyManager(0)
59 {
60   Type = tt1D;
61   colCounter = 0;
62   rowCounter = 1;
63   nTables = 0;
64
65   Data = Allocate();
66   Debug(0);
67   lastRowIndex=lastColumnIndex=2;
68 }
69
70 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
71
72 FGTable::FGTable(const FGTable& t) : PropertyManager(t.PropertyManager)
73 {
74   Type = t.Type;
75   colCounter = t.colCounter;
76   rowCounter = t.rowCounter;
77   tableCounter = t.tableCounter;
78   nRows = t.nRows;
79   nCols = t.nCols;
80   nTables = t.nTables;
81   dimension = t.dimension;
82   internal = t.internal;
83
84   Tables = t.Tables;
85   Data = Allocate();
86   for (int r=0; r<=nRows; r++) {
87     for (int c=0; c<=nCols; c++) {
88       Data[r][c] = t.Data[r][c];
89     }
90   }
91   lastRowIndex = t.lastRowIndex;
92   lastColumnIndex = t.lastColumnIndex;
93   lastTableIndex = t.lastTableIndex;
94 }
95
96 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
97
98 FGTable::FGTable(FGPropertyManager* propMan, Element* el) : PropertyManager(propMan)
99 {
100   int i;
101
102   stringstream buf;
103   string property_string;
104   string lookup_axis;
105   string call_type;
106   string parent_type;
107   FGPropertyManager* node;
108   Element *tableData;
109   Element *parent_element;
110   Element *axisElement;
111   string operation_types = "function, product, sum, difference, quotient,"
112                            "pow, abs, sin, cos, asin, acos, tan, atan, table";
113
114   nTables = 0;
115
116   // Is this an internal lookup table?
117
118   internal = false;
119   call_type = el->GetAttributeValue("type");
120   if (call_type == string("internal")) {
121     parent_element = el->GetParent();
122     parent_type = parent_element->GetName();
123     if (operation_types.find(parent_type) == string::npos) {
124       internal = true;
125     } else {
126       // internal table is a child element of a restricted type
127       cerr << endl << fgred << "  An internal table cannot be nested within another type," << endl;
128       cerr << "  such as a function. The 'internal' keyword is ignored." << fgdef << endl << endl;
129     }
130   } else if (!call_type.empty()) {
131     cerr << endl << fgred << "  An unknown table type attribute is listed: " << call_type
132                  << ". Execution cannot continue." << fgdef << endl << endl;
133     abort();
134   }
135
136   // Determine and store the lookup properties for this table unless this table
137   // is part of a 3D table, in which case its independentVar property indexes will
138   // be set by a call from the owning table during creation
139
140   dimension = 0;
141
142   axisElement = el->FindElement("independentVar");
143   if (axisElement) {
144
145     // The 'internal' attribute of the table element cannot be specified
146     // at the same time that independentVars are specified.
147     if (internal) {
148       cerr << endl << fgred << "  This table specifies both 'internal' call type" << endl;
149       cerr << "  and specific lookup properties via the 'independentVar' element." << endl;
150       cerr << "  These are mutually exclusive specifications. The 'internal'" << endl;
151       cerr << "  attribute will be ignored." << fgdef << endl << endl;
152       internal = false;
153     }
154
155     for (i=0; i<3; i++) lookupProperty[i] = 0;
156
157     while (axisElement) {
158       property_string = axisElement->GetDataLine();
159       node = PropertyManager->GetNode(property_string);
160
161       if (node == 0) {
162         cerr << "IndependenVar property, " << property_string
163              << " in Table definition is not defined." << endl;
164         abort();
165       }
166
167       lookup_axis = axisElement->GetAttributeValue("lookup");
168       if (lookup_axis == string("row")) {
169         lookupProperty[eRow] = node;
170       } else if (lookup_axis == string("column")) {
171         lookupProperty[eColumn] = node;
172       } else if (lookup_axis == string("table")) {
173         lookupProperty[eTable] = node;
174       } else { // assumed single dimension table; row lookup
175         lookupProperty[eRow] = node;
176       }
177       dimension++;
178       axisElement = el->FindNextElement("independentVar");
179     }
180
181   } else if (internal) { // This table is an internal table
182
183   // determine how many rows, columns, and tables in this table (dimension).
184
185     if (el->GetNumElements("tableData") > 1) {
186       dimension = 3; // this is a 3D table
187     } else {
188       tableData = el->FindElement("tableData");
189       string test_line = tableData->GetDataLine(1);  // examine second line in table for dimension
190       if (FindNumColumns(test_line) == 2) dimension = 1;    // 1D table
191       else if (FindNumColumns(test_line) > 2) dimension = 2; // 2D table
192       else {
193         cerr << "Invalid number of columns in table" << endl;
194       }
195     }
196
197   } else { // no independentVars found, and table is not marked as internal
198     cerr << endl << fgred << "No independent variable found for table." << fgdef << endl << endl;
199     abort();
200   }
201   // end lookup property code
202
203   tableData = el->FindElement("tableData");
204   for (i=0; i<tableData->GetNumDataLines(); i++) {
205     buf << tableData->GetDataLine(i) << string(" ");
206   }
207   switch (dimension) {
208   case 1:
209     nRows = tableData->GetNumDataLines();
210     nCols = 1;
211     Type = tt1D;
212     colCounter = 0;
213     rowCounter = 1;
214     Data = Allocate();
215     Debug(0);
216     lastRowIndex = lastColumnIndex = 2;
217     *this << buf;
218     break;
219   case 2:
220     nRows = tableData->GetNumDataLines()-1;
221
222     if (nRows >= 2) nCols = FindNumColumns(tableData->GetDataLine(0));
223     else {
224       cerr << endl << fgred << "Not enough rows in this table." << fgdef << endl;
225       abort();
226     }
227
228     Type = tt2D;
229     colCounter = 1;
230     rowCounter = 0;
231
232     Data = Allocate();
233     lastRowIndex = lastColumnIndex = 2;
234     *this << buf;
235     break;
236   case 3:
237     nTables = el->GetNumElements("tableData");
238     nRows = nTables;
239     nCols = 1;
240     Type = tt3D;
241     colCounter = 1;
242     rowCounter = 1;
243
244     Data = Allocate(); // this data array will contain the keys for the associated tables
245     Tables.reserve(nTables); // necessary?
246     tableData = el->FindElement("tableData");
247     for (i=0; i<nTables; i++) {
248       Tables.push_back(new FGTable(PropertyManager, tableData));
249       Data[i+1][1] = tableData->GetAttributeValueAsNumber("breakPoint");
250       Tables[i]->SetRowIndexProperty(lookupProperty[eRow]);
251       Tables[i]->SetColumnIndexProperty(lookupProperty[eColumn]);
252       tableData = el->FindNextElement("tableData");
253     }
254
255     Debug(0);
256     break;
257   default:
258     cout << "No dimension given" << endl;
259     break;
260   }
261   if (debug_lvl & 1) Print();
262 }
263
264 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
265
266 double** FGTable::Allocate(void)
267 {
268   Data = new double*[nRows+1];
269   for (int r=0; r<=nRows; r++) {
270     Data[r] = new double[nCols+1];
271     for (int c=0; c<=nCols; c++) {
272       Data[r][c] = 0.0;
273     }
274   }
275   return Data;
276 }
277
278 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
279
280 FGTable::~FGTable()
281 {
282   if (nTables > 0) {
283 cout << "nTables = " << nTables << endl;
284     for (int i=0; i<nTables; i++) delete Tables[i];
285     Tables.clear();
286   }
287   for (int r=0; r<=nRows; r++) if (Data[r]) delete[] Data[r];
288   if (Data) delete[] Data;
289   Debug(1);
290 }
291
292 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
293
294 int FGTable::FindNumColumns(string test_line)
295 {
296   // determine number of data columns in table (first column is row lookup - don't count)
297   int position=0;
298   int nCols=0;
299   while ((position = test_line.find_first_not_of(" \t", position)) != string::npos) {
300     nCols++;
301     position = test_line.find_first_of(" \t", position);
302   }
303   return nCols;
304 }
305
306 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
307
308 double FGTable::GetValue(void) const
309 {
310   double temp = 0;
311   double temp2 = 0;
312
313   switch (Type) {
314   case tt1D:
315     temp = lookupProperty[eRow]->getDoubleValue();
316     temp2 = GetValue(temp);
317     return temp2;
318   case tt2D:
319     return GetValue(lookupProperty[eRow]->getDoubleValue(),
320                     lookupProperty[eColumn]->getDoubleValue());
321   case tt3D:
322     return GetValue(lookupProperty[eRow]->getDoubleValue(),
323                     lookupProperty[eColumn]->getDoubleValue(),
324                     lookupProperty[eTable]->getDoubleValue());
325   default:
326     cerr << "Attempted to GetValue() for invalid/unknown table type" << endl;
327     throw(string("Attempted to GetValue() for invalid/unknown table type"));
328   }
329 }
330
331 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
332
333 double FGTable::GetValue(double key) const
334 {
335   double Factor, Value, Span;
336   int r=lastRowIndex;
337
338   //if the key is off the end of the table, just return the
339   //end-of-table value, do not extrapolate
340   if( key <= Data[1][0] ) {
341     lastRowIndex=2;
342     //cout << "Key underneath table: " << key << endl;
343     return Data[1][1];
344   } else if ( key >= Data[nRows][0] ) {
345     lastRowIndex=nRows;
346     //cout << "Key over table: " << key << endl;
347     return Data[nRows][1];
348   }
349
350   // the key is somewhere in the middle, search for the right breakpoint
351   // assume the correct breakpoint has not changed since last frame or
352   // has only changed very little
353
354   if ( r > 2 && Data[r-1][0] > key ) {
355     while( Data[r-1][0] > key && r > 2) { r--; }
356   } else if ( Data[r][0] < key ) {
357     while( Data[r][0] <= key && r <= nRows) { r++; }
358   }
359
360   lastRowIndex=r;
361   // make sure denominator below does not go to zero.
362
363   Span = Data[r][0] - Data[r-1][0];
364   if (Span != 0.0) {
365     Factor = (key - Data[r-1][0]) / Span;
366     if (Factor > 1.0) Factor = 1.0;
367   } else {
368     Factor = 1.0;
369   }
370
371   Value = Factor*(Data[r][1] - Data[r-1][1]) + Data[r-1][1];
372
373   return Value;
374 }
375
376 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
377
378 double FGTable::GetValue(double rowKey, double colKey) const
379 {
380   double rFactor, cFactor, col1temp, col2temp, Value;
381   int r=lastRowIndex;
382   int c=lastColumnIndex;
383
384   if ( r > 2 && Data[r-1][0] > rowKey ) {
385     while ( Data[r-1][0] > rowKey && r > 2) { r--; }
386   } else if ( Data[r][0] < rowKey ) {
387     while ( r <= nRows && Data[r][0] <= rowKey ) { r++; }
388     if ( r > nRows ) r = nRows;
389   }
390
391   if ( c > 2 && Data[0][c-1] > colKey ) {
392     while( Data[0][c-1] > colKey && c > 2) { c--; }
393   } else if ( Data[0][c] < colKey ) {
394     while( Data[0][c] <= colKey && c <= nCols) { c++; }
395     if ( c > nCols ) c = nCols;
396   }
397
398   lastRowIndex=r;
399   lastColumnIndex=c;
400
401   rFactor = (rowKey - Data[r-1][0]) / (Data[r][0] - Data[r-1][0]);
402   cFactor = (colKey - Data[0][c-1]) / (Data[0][c] - Data[0][c-1]);
403
404   if (rFactor > 1.0) rFactor = 1.0;
405   else if (rFactor < 0.0) rFactor = 0.0;
406
407   if (cFactor > 1.0) cFactor = 1.0;
408   else if (cFactor < 0.0) cFactor = 0.0;
409
410   col1temp = rFactor*(Data[r][c-1] - Data[r-1][c-1]) + Data[r-1][c-1];
411   col2temp = rFactor*(Data[r][c] - Data[r-1][c]) + Data[r-1][c];
412
413   Value = col1temp + cFactor*(col2temp - col1temp);
414
415   return Value;
416 }
417
418 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
419
420 double FGTable::GetValue(double rowKey, double colKey, double tableKey) const
421 {
422   double Factor, Value, Span;
423   int r=lastRowIndex;
424
425   //if the key is off the end  (or before the beginning) of the table,
426   // just return the boundary-table value, do not extrapolate
427
428   if( tableKey <= Data[1][1] ) {
429     lastRowIndex=2;
430     return Tables[0]->GetValue(rowKey, colKey);
431   } else if ( tableKey >= Data[nRows][1] ) {
432     lastRowIndex=nRows;
433     return Tables[nRows-1]->GetValue(rowKey, colKey);
434   }
435
436   // the key is somewhere in the middle, search for the right breakpoint
437   // assume the correct breakpoint has not changed since last frame or
438   // has only changed very little
439
440   if ( r > 2 && Data[r-1][1] > tableKey ) {
441     while( Data[r-1][1] > tableKey && r > 2) { r--; }
442   } else if ( Data[r][1] < tableKey ) {
443     while( Data[r][1] <= tableKey && r <= nRows) { r++; }
444   }
445
446   lastRowIndex=r;
447   // make sure denominator below does not go to zero.
448
449   Span = Data[r][1] - Data[r-1][1];
450   if (Span != 0.0) {
451     Factor = (tableKey - Data[r-1][1]) / Span;
452     if (Factor > 1.0) Factor = 1.0;
453   } else {
454     Factor = 1.0;
455   }
456
457   Value = Factor*(Tables[r-1]->GetValue(rowKey, colKey) - Tables[r-2]->GetValue(rowKey, colKey))
458                               + Tables[r-2]->GetValue(rowKey, colKey);
459
460   return Value;
461 }
462
463 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
464
465 void FGTable::operator<<(stringstream& in_stream)
466 {
467   int startRow=0;
468   int startCol=0;
469
470   if (Type == tt1D || Type == tt3D) startRow = 1;
471   if (Type == tt3D) startCol = 1;
472
473   for (int r=startRow; r<=nRows; r++) {
474     for (int c=startCol; c<=nCols; c++) {
475       if (r != 0 || c != 0) {
476         in_stream >> Data[r][c];
477       }
478     }
479   }
480 }
481
482 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
483
484 FGTable& FGTable::operator<<(const double n)
485 {
486   Data[rowCounter][colCounter] = n;
487   if (colCounter == nCols) {
488     colCounter = 0;
489     rowCounter++;
490   } else {
491     colCounter++;
492   }
493   return *this;
494 }
495
496 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
497
498 FGTable& FGTable::operator<<(const int n)
499 {
500   *this << (double)n;
501   return *this;
502 }
503
504 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
505
506 void FGTable::Print(void)
507 {
508   int startRow=0;
509   int startCol=0;
510
511   if (Type == tt1D || Type == tt3D) startRow = 1;
512   if (Type == tt3D) startCol = 1;
513
514 #if defined (sgi) && !defined(__GNUC__) && (_COMPILER_VERSION < 740)
515   unsigned long flags = cout.setf(ios::fixed);
516 #else
517   ios::fmtflags flags = cout.setf(ios::fixed); // set up output stream
518 #endif
519
520   switch(Type) {
521     case tt1D:
522       cout << "    1 dimensional table with " << nRows << " rows." << endl;
523       break;
524     case tt2D:
525       cout << "    2 dimensional table with " << nRows << " rows, " << nCols << " columns." << endl;
526       break;
527     case tt3D:
528       cout << "    3 dimensional table with " << nRows << " rows, "
529                                           << nCols << " columns "
530                                           << nTables << " tables." << endl;
531       break;
532   }
533   cout.precision(4);
534   for (int r=startRow; r<=nRows; r++) {
535     cout << "   ";
536     for (int c=startCol; c<=nCols; c++) {
537       if (r == 0 && c == 0) {
538         cout << "       ";
539       } else {
540         cout << Data[r][c] << " ";
541         if (Type == tt3D) {
542           cout << endl;
543           Tables[r-1]->Print();
544         }
545       }
546     }
547     cout << endl;
548   }
549   cout.setf(flags); // reset
550 }
551
552 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
553 //    The bitmasked value choices are as follows:
554 //    unset: In this case (the default) JSBSim would only print
555 //       out the normally expected messages, essentially echoing
556 //       the config files as they are read. If the environment
557 //       variable is not set, debug_lvl is set to 1 internally
558 //    0: This requests JSBSim not to output any messages
559 //       whatsoever.
560 //    1: This value explicity requests the normal JSBSim
561 //       startup messages
562 //    2: This value asks for a message to be printed out when
563 //       a class is instantiated
564 //    4: When this value is set, a message is displayed when a
565 //       FGModel object executes its Run() method
566 //    8: When this value is set, various runtime state variables
567 //       are printed out periodically
568 //    16: When set various parameters are sanity checked and
569 //       a message is printed out when they go out of bounds
570
571 void FGTable::Debug(int from)
572 {
573   if (debug_lvl <= 0) return;
574
575   if (debug_lvl & 1) { // Standard console startup message output
576     if (from == 0) { // Constructor
577
578     }
579   }
580   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
581     if (from == 0) cout << "Instantiated: FGTable" << endl;
582     if (from == 1) cout << "Destroyed:    FGTable" << endl;
583   }
584   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
585   }
586   if (debug_lvl & 8 ) { // Runtime state variables
587   }
588   if (debug_lvl & 16) { // Sanity checking
589   }
590   if (debug_lvl & 64) {
591     if (from == 0) { // Constructor
592       cout << IdSrc << endl;
593       cout << IdHdr << endl;
594     }
595   }
596 }
597 }