1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 Purpose: Models a lookup table
8 ------------- Copyright (C) 2001 Jon S. Berndt (jon@jsbsim.org) -------------
10 This program is free software; you can redistribute it and/or modify it under
11 the terms of the GNU Lesser General Public License as published by the Free Software
12 Foundation; either version 2 of the License, or (at your option) any later
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 Lesser General Public License for more
20 You should have received a copy of the GNU Lesser 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.
24 Further information about the GNU Lesser General Public License can also be found on
25 the world wide web at http://www.gnu.org.
27 FUNCTIONAL DESCRIPTION
28 --------------------------------------------------------------------------------
32 --------------------------------------------------------------------------------
35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
37 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
40 #include "input_output/FGXMLElement.h"
41 #include "input_output/FGPropertyManager.h"
50 static const char *IdSrc = "$Id: FGTable.cpp,v 1.27 2010/10/21 11:09:56 jberndt Exp $";
51 static const char *IdHdr = ID_TABLE;
53 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
55 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
57 FGTable::FGTable(int NRows) : nRows(NRows), nCols(1), PropertyManager(0)
66 lastRowIndex=lastColumnIndex=2;
69 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
71 FGTable::FGTable(int NRows, int NCols) : nRows(NRows), nCols(NCols), PropertyManager(0)
80 lastRowIndex=lastColumnIndex=2;
83 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
85 FGTable::FGTable(const FGTable& t) : PropertyManager(t.PropertyManager)
88 colCounter = t.colCounter;
89 rowCounter = t.rowCounter;
90 tableCounter = t.tableCounter;
94 dimension = t.dimension;
95 internal = t.internal;
97 lookupProperty[0] = t.lookupProperty[0];
98 lookupProperty[1] = t.lookupProperty[1];
99 lookupProperty[2] = t.lookupProperty[2];
103 for (unsigned int r=0; r<=nRows; r++) {
104 for (unsigned int c=0; c<=nCols; c++) {
105 Data[r][c] = t.Data[r][c];
108 lastRowIndex = t.lastRowIndex;
109 lastColumnIndex = t.lastColumnIndex;
110 lastTableIndex = t.lastTableIndex;
113 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
115 FGTable::FGTable(FGPropertyManager* propMan, Element* el) : PropertyManager(propMan)
120 string property_string;
125 FGPropertyManager* node;
126 Element *tableData=0;
127 Element *parent_element=0;
128 Element *axisElement=0;
129 string operation_types = "function, product, sum, difference, quotient,"
130 "pow, abs, sin, cos, asin, acos, tan, atan, table";
134 // Is this an internal lookup table?
137 Name = el->GetAttributeValue("name"); // Allow this table to be named with a property
138 call_type = el->GetAttributeValue("type");
139 if (call_type == string("internal")) {
140 parent_element = el->GetParent();
141 parent_type = parent_element->GetName();
142 if (operation_types.find(parent_type) == string::npos) {
145 // internal table is a child element of a restricted type
146 throw(" An internal table cannot be nested within another type,"
147 " such as a function. The 'internal' keyword is ignored.");
149 } else if (!call_type.empty()) {
150 throw(" An unknown table type attribute is listed: "
151 ". Execution cannot continue.");
154 // Determine and store the lookup properties for this table unless this table
155 // is part of a 3D table, in which case its independentVar property indexes will
156 // be set by a call from the owning table during creation
160 axisElement = el->FindElement("independentVar");
163 // The 'internal' attribute of the table element cannot be specified
164 // at the same time that independentVars are specified.
166 cerr << endl << fgred << " This table specifies both 'internal' call type" << endl;
167 cerr << " and specific lookup properties via the 'independentVar' element." << endl;
168 cerr << " These are mutually exclusive specifications. The 'internal'" << endl;
169 cerr << " attribute will be ignored." << fgdef << endl << endl;
173 for (i=0; i<3; i++) lookupProperty[i] = 0;
175 while (axisElement) {
176 property_string = axisElement->GetDataLine();
177 // The property string passed into GetNode() must have no spaces or tabs.
178 node = PropertyManager->GetNode(property_string);
181 throw("IndependentVar property, " + property_string + " in Table definition is not defined.");
184 lookup_axis = axisElement->GetAttributeValue("lookup");
185 if (lookup_axis == string("row")) {
186 lookupProperty[eRow] = node;
187 } else if (lookup_axis == string("column")) {
188 lookupProperty[eColumn] = node;
189 } else if (lookup_axis == string("table")) {
190 lookupProperty[eTable] = node;
191 } else if (!lookup_axis.empty()) {
192 throw("Lookup table axis specification not understood: " + lookup_axis);
193 } else { // assumed single dimension table; row lookup
194 lookupProperty[eRow] = node;
197 axisElement = el->FindNextElement("independentVar");
200 } else if (internal) { // This table is an internal table
202 // determine how many rows, columns, and tables in this table (dimension).
204 if (el->GetNumElements("tableData") > 1) {
205 dimension = 3; // this is a 3D table
207 tableData = el->FindElement("tableData");
208 string test_line = tableData->GetDataLine(1); // examine second line in table for dimension
209 if (FindNumColumns(test_line) == 2) dimension = 1; // 1D table
210 else if (FindNumColumns(test_line) > 2) dimension = 2; // 2D table
212 cerr << "Invalid number of columns in table" << endl;
217 brkpt_string = el->GetAttributeValue("breakPoint");
218 if (brkpt_string.empty()) {
219 // no independentVars found, and table is not marked as internal, nor is it a 3D table
220 throw("No independent variable found for table.");
223 // end lookup property code
225 if (brkpt_string.empty()) { // Not a 3D table "table element"
226 tableData = el->FindElement("tableData");
227 } else { // This is a table in a 3D table
229 dimension = 2; // Currently, infers 2D table
232 for (i=0; i<tableData->GetNumDataLines(); i++) {
233 buf << tableData->GetDataLine(i) << string(" ");
237 nRows = tableData->GetNumDataLines();
244 lastRowIndex = lastColumnIndex = 2;
248 nRows = tableData->GetNumDataLines()-1;
251 nCols = FindNumColumns(tableData->GetDataLine(0));
252 if (nCols < 2) throw(string("Not enough columns in table data."));
254 throw(string("Not enough rows in the table data."));
262 lastRowIndex = lastColumnIndex = 2;
266 nTables = el->GetNumElements("tableData");
272 lastRowIndex = lastColumnIndex = 2;
274 Data = Allocate(); // this data array will contain the keys for the associated tables
275 Tables.reserve(nTables); // necessary?
276 tableData = el->FindElement("tableData");
277 for (i=0; i<nTables; i++) {
278 Tables.push_back(new FGTable(PropertyManager, tableData));
279 Data[i+1][1] = tableData->GetAttributeValueAsNumber("breakPoint");
280 Tables[i]->SetRowIndexProperty(lookupProperty[eRow]);
281 Tables[i]->SetColumnIndexProperty(lookupProperty[eColumn]);
282 tableData = el->FindNextElement("tableData");
288 cout << "No dimension given" << endl;
292 // Sanity checks: lookup indices must be increasing monotonically
295 // find next xml element containing a name attribute
296 // to indicate where the error occured
297 Element* nameel = el;
298 while (nameel != 0 && nameel->GetAttributeValue("name") == "")
299 nameel=nameel->GetParent();
301 // check breakpoints, if applicable
303 for (b=2; b<=nTables; ++b) {
304 if (Data[b][1] <= Data[b-1][1]) {
305 stringstream errormsg;
306 errormsg << fgred << highint << endl
307 << " FGTable: breakpoint lookup is not monotonically increasing" << endl
308 << " in breakpoint " << b;
309 if (nameel != 0) errormsg << " of table in " << nameel->GetAttributeValue("name");
310 errormsg << ":" << reset << endl
311 << " " << Data[b][1] << "<=" << Data[b-1][1] << endl;
312 throw(errormsg.str());
317 // check columns, if applicable
319 for (c=2; c<=nCols; ++c) {
320 if (Data[0][c] <= Data[0][c-1]) {
321 stringstream errormsg;
322 errormsg << fgred << highint << endl
323 << " FGTable: column lookup is not monotonically increasing" << endl
324 << " in column " << c;
325 if (nameel != 0) errormsg << " of table in " << nameel->GetAttributeValue("name");
326 errormsg << ":" << reset << endl
327 << " " << Data[0][c] << "<=" << Data[0][c-1] << endl;
328 throw(errormsg.str());
334 if (dimension < 3) { // in 3D tables, check only rows of subtables
335 for (r=2; r<=nRows; ++r) {
336 if (Data[r][0]<=Data[r-1][0]) {
337 stringstream errormsg;
338 errormsg << fgred << highint << endl
339 << " FGTable: row lookup is not monotonically increasing" << endl
341 if (nameel != 0) errormsg << " of table in " << nameel->GetAttributeValue("name");
342 errormsg << ":" << reset << endl
343 << " " << Data[r][0] << "<=" << Data[r-1][0] << endl;
344 throw(errormsg.str());
351 if (debug_lvl & 1) Print();
354 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
356 double** FGTable::Allocate(void)
358 Data = new double*[nRows+1];
359 for (unsigned int r=0; r<=nRows; r++) {
360 Data[r] = new double[nCols+1];
361 for (unsigned int c=0; c<=nCols; c++) {
368 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
373 for (unsigned int i=0; i<nTables; i++) delete Tables[i];
376 for (unsigned int r=0; r<=nRows; r++) delete[] Data[r];
382 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
384 unsigned int FGTable::FindNumColumns(const string& test_line)
386 // determine number of data columns in table (first column is row lookup - don't count)
388 unsigned int nCols=0;
389 while ((position = test_line.find_first_not_of(" \t", position)) != string::npos) {
391 position = test_line.find_first_of(" \t", position);
396 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
398 double FGTable::GetValue(void) const
405 temp = lookupProperty[eRow]->getDoubleValue();
406 temp2 = GetValue(temp);
409 return GetValue(lookupProperty[eRow]->getDoubleValue(),
410 lookupProperty[eColumn]->getDoubleValue());
412 return GetValue(lookupProperty[eRow]->getDoubleValue(),
413 lookupProperty[eColumn]->getDoubleValue(),
414 lookupProperty[eTable]->getDoubleValue());
416 cerr << "Attempted to GetValue() for invalid/unknown table type" << endl;
417 throw(string("Attempted to GetValue() for invalid/unknown table type"));
421 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
423 double FGTable::GetValue(double key) const
425 double Factor, Value, Span;
426 unsigned int r = lastRowIndex;
428 //if the key is off the end of the table, just return the
429 //end-of-table value, do not extrapolate
430 if( key <= Data[1][0] ) {
432 //cout << "Key underneath table: " << key << endl;
434 } else if ( key >= Data[nRows][0] ) {
436 //cout << "Key over table: " << key << endl;
437 return Data[nRows][1];
440 // the key is somewhere in the middle, search for the right breakpoint
441 // The search is particularly efficient if
442 // the correct breakpoint has not changed since last frame or
443 // has only changed very little
445 while (r > 2 && Data[r-1][0] > key) { r--; }
446 while (r < nRows && Data[r][0] < key) { r++; }
449 // make sure denominator below does not go to zero.
451 Span = Data[r][0] - Data[r-1][0];
453 Factor = (key - Data[r-1][0]) / Span;
454 if (Factor > 1.0) Factor = 1.0;
459 Value = Factor*(Data[r][1] - Data[r-1][1]) + Data[r-1][1];
464 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
466 double FGTable::GetValue(double rowKey, double colKey) const
468 double rFactor, cFactor, col1temp, col2temp, Value;
469 unsigned int r = lastRowIndex;
470 unsigned int c = lastColumnIndex;
472 while(r > 2 && Data[r-1][0] > rowKey) { r--; }
473 while(r < nRows && Data[r] [0] < rowKey) { r++; }
475 while(c > 2 && Data[0][c-1] > colKey) { c--; }
476 while(c < nCols && Data[0][c] < colKey) { c++; }
481 rFactor = (rowKey - Data[r-1][0]) / (Data[r][0] - Data[r-1][0]);
482 cFactor = (colKey - Data[0][c-1]) / (Data[0][c] - Data[0][c-1]);
484 if (rFactor > 1.0) rFactor = 1.0;
485 else if (rFactor < 0.0) rFactor = 0.0;
487 if (cFactor > 1.0) cFactor = 1.0;
488 else if (cFactor < 0.0) cFactor = 0.0;
490 col1temp = rFactor*(Data[r][c-1] - Data[r-1][c-1]) + Data[r-1][c-1];
491 col2temp = rFactor*(Data[r][c] - Data[r-1][c]) + Data[r-1][c];
493 Value = col1temp + cFactor*(col2temp - col1temp);
498 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
500 double FGTable::GetValue(double rowKey, double colKey, double tableKey) const
502 double Factor, Value, Span;
503 unsigned int r = lastRowIndex;
505 //if the key is off the end (or before the beginning) of the table,
506 // just return the boundary-table value, do not extrapolate
508 if( tableKey <= Data[1][1] ) {
510 return Tables[0]->GetValue(rowKey, colKey);
511 } else if ( tableKey >= Data[nRows][1] ) {
513 return Tables[nRows-1]->GetValue(rowKey, colKey);
516 // the key is somewhere in the middle, search for the right breakpoint
517 // The search is particularly efficient if
518 // the correct breakpoint has not changed since last frame or
519 // has only changed very little
521 while(r > 2 && Data[r-1][1] > tableKey) { r--; }
522 while(r < nRows && Data[r] [1] < tableKey) { r++; }
525 // make sure denominator below does not go to zero.
527 Span = Data[r][1] - Data[r-1][1];
529 Factor = (tableKey - Data[r-1][1]) / Span;
530 if (Factor > 1.0) Factor = 1.0;
535 Value = Factor*(Tables[r-1]->GetValue(rowKey, colKey) - Tables[r-2]->GetValue(rowKey, colKey))
536 + Tables[r-2]->GetValue(rowKey, colKey);
541 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
543 void FGTable::operator<<(istream& in_stream)
548 // In 1D table, no pseudo-row of column-headers (i.e. keys):
549 if (Type == tt1D) startRow = 1;
551 for (unsigned int r=startRow; r<=nRows; r++) {
552 for (unsigned int c=startCol; c<=nCols; c++) {
553 if (r != 0 || c != 0) {
554 in_stream >> Data[r][c];
560 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
562 FGTable& FGTable::operator<<(const double n)
564 Data[rowCounter][colCounter] = n;
565 if (colCounter == (int)nCols) {
574 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
576 FGTable& FGTable::operator<<(const int n)
582 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
584 void FGTable::Print(void)
589 if (Type == tt1D || Type == tt3D) startRow = 1;
590 if (Type == tt3D) startCol = 1;
592 #if defined (sgi) && !defined(__GNUC__) && (_COMPILER_VERSION < 740)
593 unsigned long flags = cout.setf(ios::fixed);
595 ios::fmtflags flags = cout.setf(ios::fixed); // set up output stream
600 cout << " 1 dimensional table with " << nRows << " rows." << endl;
603 cout << " 2 dimensional table with " << nRows << " rows, " << nCols << " columns." << endl;
606 cout << " 3 dimensional table with " << nRows << " rows, "
607 << nCols << " columns "
608 << nTables << " tables." << endl;
612 for (unsigned int r=startRow; r<=nRows; r++) {
614 for (unsigned int c=startCol; c<=nCols; c++) {
615 if (r == 0 && c == 0) {
618 cout << Data[r][c] << " ";
621 Tables[r-1]->Print();
627 cout.setf(flags); // reset
630 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
632 void FGTable::bind(void)
634 typedef double (FGTable::*PMF)(void) const;
635 if ( !Name.empty() && !internal) {
636 string tmp = PropertyManager->mkPropertyName(Name, false); // Allow upper
637 PropertyManager->Tie( tmp, this, (PMF)&FGTable::GetValue);
640 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
641 // The bitmasked value choices are as follows:
642 // unset: In this case (the default) JSBSim would only print
643 // out the normally expected messages, essentially echoing
644 // the config files as they are read. If the environment
645 // variable is not set, debug_lvl is set to 1 internally
646 // 0: This requests JSBSim not to output any messages
648 // 1: This value explicity requests the normal JSBSim
650 // 2: This value asks for a message to be printed out when
651 // a class is instantiated
652 // 4: When this value is set, a message is displayed when a
653 // FGModel object executes its Run() method
654 // 8: When this value is set, various runtime state variables
655 // are printed out periodically
656 // 16: When set various parameters are sanity checked and
657 // a message is printed out when they go out of bounds
659 void FGTable::Debug(int from)
661 if (debug_lvl <= 0) return;
663 if (debug_lvl & 1) { // Standard console startup message output
664 if (from == 0) { // Constructor
668 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
669 if (from == 0) cout << "Instantiated: FGTable" << endl;
670 if (from == 1) cout << "Destroyed: FGTable" << endl;
672 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
674 if (debug_lvl & 8 ) { // Runtime state variables
676 if (debug_lvl & 16) { // Sanity checking
678 if (debug_lvl & 64) {
679 if (from == 0) { // Constructor
680 cout << IdSrc << endl;
681 cout << IdHdr << endl;