| 1 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2 | |
| 3 | Header: FGTable.h |
| 4 | Author: Jon S. Berndt |
| 5 | Date started: 1/9/2001 |
| 6 | |
| 7 | ------------- Copyright (C) 2001 Jon S. Berndt ([email protected]) -------------- |
| 8 | |
| 9 | This program is free software; you can redistribute it and/or modify it under |
| 10 | the terms of the GNU Lesser General Public License as published by the Free |
| 11 | Software Foundation; either version 2 of the License, or (at your option) any |
| 12 | later version. |
| 13 | |
| 14 | This program is distributed in the hope that it will be useful, but WITHOUT |
| 15 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 16 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
| 17 | details. |
| 18 | |
| 19 | You should have received a copy of the GNU Lesser General Public License along |
| 20 | with this program; if not, write to the Free Software Foundation, Inc., 59 |
| 21 | Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 22 | |
| 23 | Further information about the GNU Lesser General Public License can also be |
| 24 | found on the world wide web at http://www.gnu.org. |
| 25 | |
| 26 | HISTORY |
| 27 | -------------------------------------------------------------------------------- |
| 28 | JSB 1/9/00 Created |
| 29 | |
| 30 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 31 | SENTRY |
| 32 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 33 | |
| 34 | #ifndef FGTABLE_H |
| 35 | #define FGTABLE_H |
| 36 | |
| 37 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 38 | INCLUDES |
| 39 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 40 | |
| 41 | #include <memory> |
| 42 | |
| 43 | #include "FGParameter.h" |
| 44 | #include "math/FGPropertyValue.h" |
| 45 | |
| 46 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 47 | FORWARD DECLARATIONS |
| 48 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 49 | |
| 50 | namespace JSBSim { |
| 51 | |
| 52 | class Element; |
| 53 | |
| 54 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 55 | CLASS DOCUMENTATION |
| 56 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 57 | |
| 58 | /** Lookup table class. |
| 59 | Models a one, two, or three dimensional lookup table for use in aerodynamics |
| 60 | and function definitions. |
| 61 | |
| 62 | For a single "vector" lookup table, the format is as follows: |
| 63 | |
| 64 | @code |
| 65 | <table name="property_name"> |
| 66 | <independentVar lookup="row"> property_name </independentVar> |
| 67 | <tableData> |
| 68 | key_1 value_1 |
| 69 | key_2 value_2 |
| 70 | ... ... |
| 71 | key_n value_n |
| 72 | </tableData> |
| 73 | </table> |
| 74 | @endcode |
| 75 | |
| 76 | The lookup="row" attribute in the independentVar element is option in this case; |
| 77 | it is assumed that the independentVar is a row variable. |
| 78 | |
| 79 | A "real life" example is as shown here: |
| 80 | |
| 81 | @code |
| 82 | <table> |
| 83 | <independentVar lookup="row"> aero/alpha-rad </independentVar> |
| 84 | <tableData> |
| 85 | -1.57 1.500 |
| 86 | -0.26 0.033 |
| 87 | 0.00 0.025 |
| 88 | 0.26 0.033 |
| 89 | 1.57 1.500 |
| 90 | </tableData> |
| 91 | </table> |
| 92 | @endcode |
| 93 | |
| 94 | The first column in the data table represents the lookup index (or "key"). In |
| 95 | this case, the lookup index is aero/alpha-rad (angle of attack in radians). |
| 96 | If alpha is 0.26 radians, the value returned from the lookup table |
| 97 | would be 0.033. |
| 98 | |
| 99 | The definition for a 2D table, is as follows: |
| 100 | |
| 101 | @code |
| 102 | <table name="property_name"> |
| 103 | <independentVar lookup="row"> property_name </independentVar> |
| 104 | <independentVar lookup="column"> property_name </independentVar> |
| 105 | <tableData> |
| 106 | {col_1_key col_2_key ... col_n_key } |
| 107 | {row_1_key} {col_1_data col_2_data ... col_n_data} |
| 108 | {row_2_key} {... ... ... ... } |
| 109 | { ... } {... ... ... ... } |
| 110 | {row_n_key} {... ... ... ... } |
| 111 | </tableData> |
| 112 | </table> |
| 113 | @endcode |
| 114 | |
| 115 | The data is in a gridded format. |
| 116 | |
| 117 | A "real life" example is as shown below. Alpha in radians is the row lookup (alpha |
| 118 | breakpoints are arranged in the first column) and flap position in degrees is |
| 119 | |
| 120 | @code |
| 121 | <table> |
| 122 | <independentVar lookup="row">aero/alpha-rad</independentVar> |
| 123 | <independentVar lookup="column">fcs/flap-pos-deg</independentVar> |
| 124 | <tableData> |
| 125 | 0.0 10.0 20.0 30.0 |
| 126 | -0.0523599 8.96747e-05 0.00231942 0.0059252 0.00835082 |
| 127 | -0.0349066 0.000313268 0.00567451 0.0108461 0.0140545 |
| 128 | -0.0174533 0.00201318 0.0105059 0.0172432 0.0212346 |
| 129 | 0.0 0.0051894 0.0168137 0.0251167 0.0298909 |
| 130 | 0.0174533 0.00993967 0.0247521 0.0346492 0.0402205 |
| 131 | 0.0349066 0.0162201 0.0342207 0.0457119 0.0520802 |
| 132 | 0.0523599 0.0240308 0.0452195 0.0583047 0.0654701 |
| 133 | 0.0698132 0.0333717 0.0577485 0.0724278 0.0803902 |
| 134 | 0.0872664 0.0442427 0.0718077 0.088081 0.0968405 |
| 135 | </tableData> |
| 136 | </table> |
| 137 | @endcode |
| 138 | |
| 139 | The definition for a 3D table in a coefficient would be (for example): |
| 140 | |
| 141 | @code |
| 142 | <table name="property_name"> |
| 143 | <independentVar lookup="row"> property_name </independentVar> |
| 144 | <independentVar lookup="column"> property_name </independentVar> |
| 145 | <tableData breakpoint="table_1_key"> |
| 146 | {col_1_key col_2_key ... col_n_key } |
| 147 | {row_1_key} {col_1_data col_2_data ... col_n_data} |
| 148 | {row_2_key} {... ... ... ... } |
| 149 | { ... } {... ... ... ... } |
| 150 | {row_n_key} {... ... ... ... } |
| 151 | </tableData> |
| 152 | <tableData breakpoint="table_2_key"> |
| 153 | {col_1_key col_2_key ... col_n_key } |
| 154 | {row_1_key} {col_1_data col_2_data ... col_n_data} |
| 155 | {row_2_key} {... ... ... ... } |
| 156 | { ... } {... ... ... ... } |
| 157 | {row_n_key} {... ... ... ... } |
| 158 | </tableData> |
| 159 | ... |
| 160 | <tableData breakpoint="table_n_key"> |
| 161 | {col_1_key col_2_key ... col_n_key } |
| 162 | {row_1_key} {col_1_data col_2_data ... col_n_data} |
| 163 | {row_2_key} {... ... ... ... } |
| 164 | { ... } {... ... ... ... } |
| 165 | {row_n_key} {... ... ... ... } |
| 166 | </tableData> |
| 167 | </table> |
| 168 | @endcode |
| 169 | |
| 170 | [Note the "breakpoint" attribute in the tableData element, above.] |
| 171 | |
| 172 | Here's an example: |
| 173 | |
| 174 | @code |
| 175 | <table> |
| 176 | <independentVar lookup="row">fcs/row-value</independentVar> |
| 177 | <independentVar lookup="column">fcs/column-value</independentVar> |
| 178 | <independentVar lookup="table">fcs/table-value</independentVar> |
| 179 | <tableData breakPoint="-1.0"> |
| 180 | -1.0 1.0 |
| 181 | 0.0 1.0000 2.0000 |
| 182 | 1.0 3.0000 4.0000 |
| 183 | </tableData> |
| 184 | <tableData breakPoint="0.0000"> |
| 185 | 0.0 10.0 |
| 186 | 2.0 1.0000 2.0000 |
| 187 | 3.0 3.0000 4.0000 |
| 188 | </tableData> |
| 189 | <tableData breakPoint="1.0"> |
| 190 | 0.0 10.0 20.0 |
| 191 | 2.0 1.0000 2.0000 3.0000 |
| 192 | 3.0 4.0000 5.0000 6.0000 |
| 193 | 10.0 7.0000 8.0000 9.0000 |
| 194 | </tableData> |
| 195 | </table> |
| 196 | @endcode |
| 197 | |
| 198 | In addition to using a Table for something like a coefficient, where all the |
| 199 | row and column elements are read in from a file, a Table could be created |
| 200 | and populated completely within program code: |
| 201 | |
| 202 | @code |
| 203 | // First column is thi, second is neta (combustion efficiency) |
| 204 | Lookup_Combustion_Efficiency = new FGTable(12); |
| 205 | |
| 206 | *Lookup_Combustion_Efficiency << 0.00 << 0.980; |
| 207 | *Lookup_Combustion_Efficiency << 0.90 << 0.980; |
| 208 | *Lookup_Combustion_Efficiency << 1.00 << 0.970; |
| 209 | *Lookup_Combustion_Efficiency << 1.05 << 0.950; |
| 210 | *Lookup_Combustion_Efficiency << 1.10 << 0.900; |
| 211 | *Lookup_Combustion_Efficiency << 1.15 << 0.850; |
| 212 | *Lookup_Combustion_Efficiency << 1.20 << 0.790; |
| 213 | *Lookup_Combustion_Efficiency << 1.30 << 0.700; |
| 214 | *Lookup_Combustion_Efficiency << 1.40 << 0.630; |
| 215 | *Lookup_Combustion_Efficiency << 1.50 << 0.570; |
| 216 | *Lookup_Combustion_Efficiency << 1.60 << 0.525; |
| 217 | *Lookup_Combustion_Efficiency << 2.00 << 0.345; |
| 218 | @endcode |
| 219 | |
| 220 | The first column in the table, above, is thi (the lookup index, or key). The |
| 221 | second column is the output data - in this case, "neta" (the Greek letter |
| 222 | referring to combustion efficiency). Later on, the table is used like this: |
| 223 | |
| 224 | @code |
| 225 | combustion_efficiency = Lookup_Combustion_Efficiency->GetValue(equivalence_ratio); |
| 226 | @endcode |
| 227 | |
| 228 | @author Jon S. Berndt |
| 229 | */ |
| 230 | |
| 231 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 232 | CLASS DECLARATION |
| 233 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 234 | |
| 235 | class JSBSIM_API FGTable : public FGParameter, public FGJSBBase |
| 236 | { |
| 237 | public: |
| 238 | /// Destructor |
| 239 | ~FGTable(); |
| 240 | |
| 241 | /** This is the very important copy constructor. |
| 242 | @param table a const reference to a table.*/ |
| 243 | FGTable(const FGTable& table); |
| 244 | /// Copy assignment constructor. |
| 245 | /* MSVC issues an error C2280 if not defined : it is needed by |
| 246 | std::unique_ptr<FGTable>. |
| 247 | See StackOverflow: https://stackoverflow.com/questions/31264984/c-compiler-error-c2280-attempting-to-reference-a-deleted-function-in-visual */ |
| 248 | FGTable& operator=(const FGTable&); |
| 249 | |
| 250 | /// The constructor for a table |
| 251 | FGTable (FGPropertyManager* propMan, Element* el, const std::string& prefix=""); |
| 252 | FGTable (int); |
| 253 | FGTable (int, int); |
| 254 | |
| 255 | /// Get the current table value |
| 256 | double GetValue(void) const; |
| 257 | /// @brief Get a value from a 1D internal table |
| 258 | /// @param key Row coordinate at which the value must be interpolated |
| 259 | /// @return The interpolated value |
| 260 | double GetValue(double key) const; |
| 261 | /// @brief Get a value from a 2D internal table |
| 262 | /// @param rowKey Row coordinate at which the value must be interpolated |
| 263 | /// @param colKey Column coordinate at which the value must be interpolated |
| 264 | /// @return The interpolated value |
| 265 | double GetValue(double rowKey, double colKey) const; |
| 266 | /// @brief Get a value from a 3D internal table |
| 267 | /// @param rowKey Row coordinate at which the value must be interpolated |
| 268 | /// @param colKey Column coordinate at which the value must be interpolated |
| 269 | /// @param TableKey Table coordinate at which the value must be interpolated |
| 270 | /// @return The interpolated value |
| 271 | double GetValue(double rowKey, double colKey, double TableKey) const; |
| 272 | |
| 273 | double GetMinValue(void) const; |
| 274 | double GetMinValue(double colKey) const; |
| 275 | double GetMinValue(double colKey, double TableKey) const; |
| 276 | |
| 277 | /** Read the table in. |
| 278 | Data in the config file should be in matrix format with the row |
| 279 | independents as the first column and the column independents in |
| 280 | the first row. The implication of this layout is that there should |
| 281 | be no value in the upper left corner of the matrix e.g: |
| 282 | <pre> |
| 283 | 0 10 20 30 ... |
| 284 | -5 1 2 3 4 ... |
| 285 | ... |
| 286 | </pre> |
| 287 | |
| 288 | For multiple-table (i.e. 3D) data sets there is an additional number |
| 289 | key in the table definition. For example: |
| 290 | |
| 291 | <pre> |
| 292 | 0.0 |
| 293 | 0 10 20 30 ... |
| 294 | -5 1 2 3 4 ... |
| 295 | ... |
| 296 | </pre> |
| 297 | */ |
| 298 | |
| 299 | void operator<<(std::istream&); |
| 300 | FGTable& operator<<(const double x); |
| 301 | |
| 302 | double GetElement(unsigned int r, unsigned int c) const; |
| 303 | double operator()(unsigned int r, unsigned int c) const |
| 304 | { return GetElement(r, c); } |
| 305 | |
| 306 | void SetRowIndexProperty(FGPropertyNode *node) |
| 307 | { lookupProperty[eRow] = new FGPropertyValue(node); } |
| 308 | void SetColumnIndexProperty(FGPropertyNode *node) |
| 309 | { lookupProperty[eColumn] = new FGPropertyValue(node); } |
| 310 | |
| 311 | unsigned int GetNumRows() const {return nRows;} |
| 312 | |
| 313 | void Print(void); |
| 314 | |
| 315 | std::string GetName(void) const {return Name;} |
| 316 | |
| 317 | private: |
| 318 | enum type {tt1D, tt2D, tt3D} Type; |
| 319 | enum axis {eRow=0, eColumn, eTable}; |
| 320 | bool internal = false; |
| 321 | FGPropertyValue_ptr lookupProperty[3]; |
| 322 | std::vector<double> Data; |
| 323 | std::vector<std::unique_ptr<FGTable>> Tables; |
| 324 | unsigned int nRows, nCols; |
| 325 | FGPropertyManager* const PropertyManager; |
| 326 | std::string Name; |
| 327 | void bind(Element* el, const std::string& Prefix); |
| 328 | void missingData(Element *el, unsigned int expected_size, size_t actual_size); |
| 329 | void Debug(int from); |
| 330 | }; |
| 331 | } |
| 332 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 333 | |
| 334 | #endif |