|
| 1 | +/*! |
| 2 | + * \file CLookupTable.hpp |
| 3 | + * \brief tabulation of fluid properties |
| 4 | + * \author D. Mayer, T. Economon |
| 5 | + * \version 7.3.1 "Blackbird" |
| 6 | + * |
| 7 | + * SU2 Project Website: https://su2code.github.io |
| 8 | + * |
| 9 | + * The SU2 Project is maintained by the SU2 Foundation |
| 10 | + * (http://su2foundation.org) |
| 11 | + * |
| 12 | + * Copyright 2012-2022, SU2 Contributors (cf. AUTHORS.md) |
| 13 | + * |
| 14 | + * SU2 is free software; you can redistribute it and/or |
| 15 | + * modify it under the terms of the GNU Lesser General Public |
| 16 | + * License as published by the Free Software Foundation; either |
| 17 | + * version 2.1 of the License, or (at your option) any later version. |
| 18 | + * |
| 19 | + * SU2 is distributed in the hope that it will be useful, |
| 20 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 22 | + * Lesser General Public License for more details. |
| 23 | + * |
| 24 | + * You should have received a copy of the GNU Lesser General Public |
| 25 | + * License along with SU2. If not, see <http://www.gnu.org/licenses/>. |
| 26 | + */ |
| 27 | + |
| 28 | +#pragma once |
| 29 | + |
| 30 | +#include <iomanip> |
| 31 | +#include <string> |
| 32 | +#include <vector> |
| 33 | + |
| 34 | +#include "../../Common/include/option_structure.hpp" |
| 35 | +#include "CFileReaderLUT.hpp" |
| 36 | +#include "CTrapezoidalMap.hpp" |
| 37 | + |
| 38 | +class CLookUpTable { |
| 39 | + protected: |
| 40 | + int rank; /*!< \brief MPI Rank. */ |
| 41 | + |
| 42 | + std::string file_name_lut; |
| 43 | + std::string version_lut; |
| 44 | + std::string version_reader; |
| 45 | + unsigned long n_points; |
| 46 | + unsigned long n_triangles; |
| 47 | + unsigned long n_variables; |
| 48 | + unsigned long n_hull_points; |
| 49 | + |
| 50 | + /*! |
| 51 | + * \brief the lower and upper limits of the enthalpy and progress variable. |
| 52 | + */ |
| 53 | + su2double limits_table_enth[2]; |
| 54 | + su2double limits_table_prog[2]; |
| 55 | + |
| 56 | + /*! \brief Holds the variable names stored in the table file. |
| 57 | + * Order is in sync with data |
| 58 | + */ |
| 59 | + std::vector<std::string> names_var; |
| 60 | + |
| 61 | + /*! \brief |
| 62 | + * Holds all data stored in the table. First index addresses the variable |
| 63 | + * while second index addresses the point. |
| 64 | + */ |
| 65 | + su2activematrix table_data; |
| 66 | + |
| 67 | + su2matrix<unsigned long> triangles; |
| 68 | + |
| 69 | + /* we do not know this size in advance until we go through the entire lookup table */ |
| 70 | + std::vector<std::vector<unsigned long> > edges; |
| 71 | + std::vector<std::vector<unsigned long> > edge_to_triangle; |
| 72 | + |
| 73 | + /*! \brief |
| 74 | + * The hull contains the boundary of the lookup table. |
| 75 | + */ |
| 76 | + std::vector<unsigned long> hull; |
| 77 | + |
| 78 | + CTrapezoidalMap trap_map_prog_enth; |
| 79 | + |
| 80 | + /*! \brief |
| 81 | + * vector of all the weight factors for the interpolation. |
| 82 | + */ |
| 83 | + std::vector<su2activematrix> interp_mat_inv_prog_enth; |
| 84 | + |
| 85 | + /*! \brief |
| 86 | + * returns the index to the variable in the lookup table. |
| 87 | + */ |
| 88 | + inline unsigned int GetIndexOfVar(const std::string& nameVar) const { |
| 89 | + int index = find(names_var.begin(), names_var.end(), nameVar) - names_var.begin(); |
| 90 | + if (index == int(names_var.size())) { |
| 91 | + index = -1; |
| 92 | + std::string error_msg = "Variable '"; |
| 93 | + error_msg.append(nameVar); |
| 94 | + error_msg.append("' is not in the lookup table."); |
| 95 | + SU2_MPI::Error(error_msg, CURRENT_FUNCTION); |
| 96 | + } |
| 97 | + return index; |
| 98 | + } |
| 99 | + |
| 100 | + /*! |
| 101 | + * \brief Get the pointer to the column data of the table (density, temperature, source terms, ...). |
| 102 | + * \returns pointer to the column data. |
| 103 | + */ |
| 104 | + inline const su2double* GetDataP(const std::string& name_var) const { |
| 105 | + return table_data[GetIndexOfVar(name_var)]; |
| 106 | + } |
| 107 | + |
| 108 | + /*! |
| 109 | + * \brief find the table limits, i.e. the minimum and maximum values of the 2 independent. |
| 110 | + * controlling variables (progress variable and enthalpy). We put the values in the variables. |
| 111 | + * limits_table_prog[2] and limit_table_enth[2]. |
| 112 | + * \param[in] name_prog - the string name for the first controlling variable. |
| 113 | + * \param[in] name_enth - the string name of the second controlling variable. |
| 114 | + */ |
| 115 | + void FindTableLimits(const std::string& name_prog, const std::string& name_enth); |
| 116 | + |
| 117 | + /*! |
| 118 | + * \brief construct a list of all the edges and a list of the pair of elements left and right of the edge. |
| 119 | + */ |
| 120 | + void IdentifyUniqueEdges(); |
| 121 | + |
| 122 | + /*! |
| 123 | + * \brief read the lookup table from file and store the data. |
| 124 | + * \param[in] file_name_lut - the filename of the lookup table. |
| 125 | + */ |
| 126 | + void LoadTableRaw(const std::string& file_name_lut); |
| 127 | + |
| 128 | + /*! |
| 129 | + * \brief compute vector of all (inverse) interpolation coefficients "interp_mat_inv_prog_enth" of all triangles. |
| 130 | + * \param[in] name_prog - the string name of the first controlling variable (progress variable). |
| 131 | + * \param[in] name_enth - the string name of the second controlling variable (enthalpy). |
| 132 | + */ |
| 133 | + void ComputeInterpCoeffs(const std::string& name_prog, const std::string& name_enth); |
| 134 | + |
| 135 | + /*! |
| 136 | + * \brief compute the inverse matrix for interpolation. |
| 137 | + * \param[in] vec_x - pointer to first coordinate (progress variable). |
| 138 | + * \param[in] vec_y - pointer to second coordinate (enthalpy). |
| 139 | + * \param[in] point_ids - single triangle data. |
| 140 | + * \param[out] interp_mat_inv - inverse matrix for interpolation. |
| 141 | + */ |
| 142 | + void GetInterpMatInv(const su2double* vec_x, const su2double* vec_y, std::array<unsigned long,3>& point_ids, |
| 143 | + su2activematrix& interp_mat_inv); |
| 144 | + |
| 145 | + /*! |
| 146 | + * \brief compute the interpolation coefficients for the triangular interpolation. |
| 147 | + * \param[in] val_x - value of first coordinate (progress variable). |
| 148 | + * \param[in] val_y - value of second coordinate (enthalpy). |
| 149 | + * \param[in] interp_mat_inv - inverse matrix for interpolation. |
| 150 | + * \param[out] interp_coeffs - interpolation coefficients. |
| 151 | + */ |
| 152 | + void GetInterpCoeffs(su2double val_x, su2double val_y, su2activematrix& interp_mat_inv, |
| 153 | + std::array<su2double,3>& interp_coeffs); |
| 154 | + |
| 155 | + /*! |
| 156 | + * \brief compute interpolated value of a point P in the triangle. |
| 157 | + * \param[in] val_samples - pointer to the variable data. |
| 158 | + * \param[in] val_triangle - ID to the triangle. |
| 159 | + * \param[in] val_interp_coeffs - interpolation coefficients using the point data in P. |
| 160 | + * \returns resulting value of the interpolation. |
| 161 | + */ |
| 162 | + su2double Interpolate(const su2double* val_samples, std::array<unsigned long,3>& val_triangle, |
| 163 | + std::array<su2double,3>& val_interp_coeffs); |
| 164 | + |
| 165 | + /*! |
| 166 | + * \brief find the point on the hull (boundary of the table) that is closest to the point P(val_prog,val_enth). |
| 167 | + * \param[in] val_x - first coordinate of point P(val_x,val_y) to check. |
| 168 | + * \param[in] val_y - second coordinate of point P(val_x,val_y) to check. |
| 169 | + * \param[in] name_prog - string name of the first controlling variable. |
| 170 | + * \param[in] name_enth - string name of the second controlling variable. |
| 171 | + * \returns point id of the nearest neighbor on the hull. |
| 172 | + */ |
| 173 | + unsigned long FindNearestNeighborOnHull(su2double val_prog, su2double val_enth, const std::string& name_prog, const std::string& name_enth); |
| 174 | + |
| 175 | + /*! |
| 176 | + * \brief determine if a point P(val_x,val_y) is inside the triangle val_id_triangle. |
| 177 | + * \param[in] val_x - first coordinate of point P(val_x,val_y) to check. |
| 178 | + * \param[in] val_y - second coordinate of point P(val_x,val_y) to check. |
| 179 | + * \param[in] val_id_triangle - ID of the triangle to check. |
| 180 | + * \param[in] name_prog - string name of the first controlling variable. |
| 181 | + * \param[in] name_enth - string name of the second controlling variable. |
| 182 | + * \returns true if the point is in the triangle, false if it is outside. |
| 183 | + */ |
| 184 | + bool IsInTriangle(su2double val_x, su2double val_y, unsigned long val_id_triangle, const std::string& name_prog, |
| 185 | + const std::string& name_enth); |
| 186 | + |
| 187 | + /*! |
| 188 | + * \brief compute the area of a triangle given the 3 points of the triangle. |
| 189 | + * \param[in] x1 - the coordinates of the points P1(x1,y1), P2(x2,y2) and P3(x3,y3). |
| 190 | + * \param[in] y1 - the coordinates of the points P1(x1,y1), P2(x2,y2) and P3(x3,y3). |
| 191 | + * \param[in] x2 - the coordinates of the points P1(x1,y1), P2(x2,y2) and P3(x3,y3). |
| 192 | + * \param[in] y2 - the coordinates of the points P1(x1,y1), P2(x2,y2) and P3(x3,y3). |
| 193 | + * \param[in] x3 - the coordinates of the points P1(x1,y1), P2(x2,y2) and P3(x3,y3). |
| 194 | + * \param[in] y3 - the coordinates of the points P1(x1,y1), P2(x2,y2) and P3(x3,y3). |
| 195 | + * \returns the absolute value of the area of the triangle. |
| 196 | + */ |
| 197 | + inline su2double TriArea(su2double x1, su2double y1, su2double x2, su2double y2, su2double x3, su2double y3) { |
| 198 | + return abs((x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2)) * 0.5); |
| 199 | + } |
| 200 | + |
| 201 | + public: |
| 202 | + CLookUpTable(const std::string& file_name_lut, const std::string& name_prog, const std::string& name_enth); |
| 203 | + |
| 204 | + |
| 205 | + /*! |
| 206 | + * \brief print information to screen. |
| 207 | + */ |
| 208 | + void PrintTableInfo(); |
| 209 | + |
| 210 | + /*! |
| 211 | + * \brief lookup 1 value of the single variable "val_name_var" using controlling variable values(val_prog,val_enth) |
| 212 | + * whose controlling variable names are "name_prog" and "name_enth". |
| 213 | + * \param[in] val_name_var - string name of the variable to look up. |
| 214 | + * \param[out] val_var - the stored value of the variable to look up. |
| 215 | + * \param[in] val_prog - value of controlling variable 1 (progress variable). |
| 216 | + * \param[in] val_enth - value of controlling variable 2 (enthalpy). |
| 217 | + * \param[in] name_prog - string name of controlling variable 1 (progress variable). |
| 218 | + * \param[in] name_enth - string name of controlling variable 2 (enthalpy). |
| 219 | + * \returns 1 if the lookup and subsequent interpolation was a success, 0 if not. |
| 220 | + */ |
| 221 | + unsigned long LookUp_ProgEnth(const std::string& val_name_var, su2double* val_var, su2double val_prog, su2double val_enth, |
| 222 | + const std::string& name_prog, const std::string& name_enth); |
| 223 | + |
| 224 | + /*! |
| 225 | + * \brief lookup 1 value for each of the variables in "val_name_var" using controlling variable values(val_prog,val_enth) |
| 226 | + * whose controlling variable names are "name_prog" and "name_enth". |
| 227 | + * \param[in] val_names_var - vector of string names of the variables to look up. |
| 228 | + * \param[out] val_vars - pointer to the vector of stored values of the variables to look up. |
| 229 | + * \param[in] val_prog - value of controlling variable 1 (progress variable). |
| 230 | + * \param[in] val_enth - value of controlling variable 2 (enthalpy). |
| 231 | + * \param[in] name_prog - string name of controlling variable 1 (progress variable). |
| 232 | + * \param[in] name_enth - string name of controlling variable 2 (enthalpy). |
| 233 | + * \returns 1 if the lookup and subsequent interpolation was a success, 0 if not. |
| 234 | + */ |
| 235 | + unsigned long LookUp_ProgEnth(const std::vector<std::string>& val_names_var, std::vector<su2double*>& val_vars, su2double val_prog, |
| 236 | + su2double val_enth, const std::string& name_prog, const std::string& name_enth); |
| 237 | + |
| 238 | + /*! |
| 239 | + * \brief lookup the value of the variable "val_name_var" using controlling variable values(val_prog,val_enth) |
| 240 | + * whose controlling variable names are "name_prog" and "name_enth". |
| 241 | + * \param[in] val_name_var - string name of the variable to look up. |
| 242 | + * \param[out] val_var - the stored value of the variable to look up. |
| 243 | + * \param[in] val_prog - value of controlling variable 1 (progress variable). |
| 244 | + * \param[in] val_enth - value of controlling variable 2 (enthalpy). |
| 245 | + * \param[in] name_prog - string name of controlling variable 1 (progress variable). |
| 246 | + * \param[in] name_enth - string name of controlling variable 2 (enthalpy). |
| 247 | + * \returns 1 if the lookup and subsequent interpolation was a success, 0 if not. |
| 248 | + */ |
| 249 | + unsigned long LookUp_ProgEnth(const std::vector<std::string>& val_names_var, std::vector<su2double>& val_vars, su2double val_prog, |
| 250 | + su2double val_enth, const std::string& name_prog, const std::string& name_enth); |
| 251 | + |
| 252 | + /*! |
| 253 | + * \brief determine the minimum and maximum value of the enthalpy (controlling variable 2). |
| 254 | + * \returns pair of minimum and maximum value of controlling variable 2. |
| 255 | + */ |
| 256 | + inline std::pair<su2double, su2double> GetTableLimitsEnth() const { |
| 257 | + return std::make_pair(limits_table_enth[0], limits_table_enth[1]); |
| 258 | + } |
| 259 | + |
| 260 | + /*! |
| 261 | + * \brief determine the minimum and maximum value of the progress variable (controlling variable 1). |
| 262 | + * \returns pair of minimum and maximum value of controlling variable 1. |
| 263 | + */ |
| 264 | + inline std::pair<su2double, su2double> GetTableLimitsProg() const { |
| 265 | + return std::make_pair(limits_table_prog[0], limits_table_prog[1]); |
| 266 | + } |
| 267 | +}; |
0 commit comments