|
| 1 | +/*! |
| 2 | + * \file CDataDrivenFluid.hpp |
| 3 | + * \brief Defines a template fluid model class using multilayer perceptrons |
| 4 | + * for theromodynamic state definition |
| 5 | + * \author E.C.Bunschoten |
| 6 | + * \version 7.5.1 "Blackbird" |
| 7 | + * |
| 8 | + * SU2 Project Website: https://su2code.github.io |
| 9 | + * |
| 10 | + * The SU2 Project is maintained by the SU2 Foundation |
| 11 | + * (http://su2foundation.org) |
| 12 | + * |
| 13 | + * Copyright 2012-2023, SU2 Contributors (cf. AUTHORS.md) |
| 14 | + * |
| 15 | + * SU2 is free software; you can redistribute it and/or |
| 16 | + * modify it under the terms of the GNU Lesser General Public |
| 17 | + * License as published by the Free Software Foundation; either |
| 18 | + * version 2.1 of the License, or (at your option) any later version. |
| 19 | + * |
| 20 | + * SU2 is distributed in the hope that it will be useful, |
| 21 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 23 | + * Lesser General Public License for more details. |
| 24 | + * |
| 25 | + * You should have received a copy of the GNU Lesser General Public |
| 26 | + * License along with SU2. If not, see <http://www.gnu.org/licenses/>. |
| 27 | + */ |
| 28 | + |
| 29 | +#pragma once |
| 30 | + |
| 31 | +#include <vector> |
| 32 | +#include "../../../Common/include/containers/CLookUpTable.hpp" |
| 33 | +#if defined(HAVE_MLPCPP) |
| 34 | +#define MLP_CUSTOM_TYPE su2double |
| 35 | +#include "../../../subprojects/MLPCpp/include/CLookUp_ANN.hpp" |
| 36 | +#define USE_MLPCPP |
| 37 | +#endif |
| 38 | +#include "CFluidModel.hpp" |
| 39 | + |
| 40 | +/*! |
| 41 | + * \class CDataDrivenFluid |
| 42 | + * \brief Template class for fluid model definition using multi-layer perceptrons for |
| 43 | + * fluid dynamic state definition. |
| 44 | + * \author: E.C.Bunschoten. |
| 45 | + */ |
| 46 | +class CDataDrivenFluid final : public CFluidModel { |
| 47 | + protected: |
| 48 | + int rank{MASTER_NODE}; /*!< \brief MPI Rank. */ |
| 49 | + ENUM_DATADRIVEN_METHOD Kind_DataDriven_Method = |
| 50 | + ENUM_DATADRIVEN_METHOD::LUT; /*!< \brief Interpolation method for data set evaluation. */ |
| 51 | + |
| 52 | + string varname_rho, /*!< \brief Controlling variable name for density. */ |
| 53 | + varname_e; /*!< \brief Controlling variable name for static energy. */ |
| 54 | + |
| 55 | + size_t idx_rho, /*!< \brief Interpolator index for density input. */ |
| 56 | + idx_e; /*!< \brief Interpolator index for energy input. */ |
| 57 | + |
| 58 | + su2double Newton_Relaxation, /*!< \brief Relaxation factor for Newton solvers. */ |
| 59 | + rho_start, /*!< \brief Starting value for the density in Newton solver processes. */ |
| 60 | + e_start, /*!< \brief Starting value for the energy in Newton solver processes. */ |
| 61 | + Newton_Tolerance, /*!< \brief Normalized tolerance for Newton solvers. */ |
| 62 | + rho_min, rho_max, /*!< \brief Minimum and maximum density values in data set. */ |
| 63 | + e_min, e_max; /*!< \brief Minimum and maximum energy values in data set. */ |
| 64 | + |
| 65 | + unsigned long MaxIter_Newton; /*!< \brief Maximum number of iterations for Newton solvers. */ |
| 66 | + |
| 67 | + su2double dsde_rho, /*!< \brief Entropy derivative w.r.t. density. */ |
| 68 | + dsdrho_e, /*!< \brief Entropy derivative w.r.t. static energy. */ |
| 69 | + d2sde2, /*!< \brief Entropy second derivative w.r.t. static energy. */ |
| 70 | + d2sdedrho, /*!< \brief Entropy second derivative w.r.t. density and static energy. */ |
| 71 | + d2sdrho2; /*!< \brief Entropy second derivative w.r.t. static density. */ |
| 72 | + |
| 73 | + su2double R_idealgas, /*!< \brief Approximated ideal gas constant. */ |
| 74 | + Cp_idealgas, /*!< \brief Approximated ideal gas specific heat at constant pressure. */ |
| 75 | + gamma_idealgas, /*!< \brief Approximated ideal gas specific heat ratio. */ |
| 76 | + Cv_idealgas, /*!< \brief Approximated ideal gas specific heat at constant volume. */ |
| 77 | + P_middle, /*!< \brief Pressure computed from the centre of the data set. */ |
| 78 | + T_middle; /*!< \brief Temperature computed from the centre of the data set. */ |
| 79 | + |
| 80 | + su2double Enthalpy, /*!< \brief Fluid enthalpy value [J kg^-1] */ |
| 81 | + dhdrho_e, /*!< \brief Enthalpy derivative w.r.t. density. */ |
| 82 | + dhde_rho; /*!< \brief Enthalpy derivative w.r.t. static energy. */ |
| 83 | + |
| 84 | + vector<string> input_names_rhoe, /*!< \brief Data-driven method input variable names of the independent variables |
| 85 | + (density, energy). */ |
| 86 | + output_names_rhoe; /*!< \brief Output variable names listed in the data-driven method input file name. */ |
| 87 | + |
| 88 | + vector<su2double*> outputs_rhoe; /*!< \brief Pointers to output variables. */ |
| 89 | + |
| 90 | + /*--- Class variables for the multi-layer perceptron method ---*/ |
| 91 | +#ifdef USE_MLPCPP |
| 92 | + MLPToolbox::CLookUp_ANN* lookup_mlp; /*!< \brief Multi-layer perceptron collection. */ |
| 93 | + MLPToolbox::CIOMap* iomap_rhoe; /*!< \brief Input-output map. */ |
| 94 | +#endif |
| 95 | + vector<su2double> MLP_inputs; /*!< \brief Inputs for the multi-layer perceptron look-up operation. */ |
| 96 | + |
| 97 | + CLookUpTable* lookup_table; /*!< \brief Look-up table regression object. */ |
| 98 | + |
| 99 | + unsigned long outside_dataset, /*!< \brief Density-energy combination lies outside data set. */ |
| 100 | + nIter_Newton; /*!< \brief Number of Newton solver iterations. */ |
| 101 | + |
| 102 | + /*! |
| 103 | + * \brief Map dataset variables to specific look-up operations. |
| 104 | + */ |
| 105 | + void MapInputs_to_Outputs(); |
| 106 | + |
| 107 | + /*! |
| 108 | + * \brief Evaluate dataset through multi-layer perceptron. |
| 109 | + * \param[in] rho - Density value. |
| 110 | + * \param[in] e - Static energy value. |
| 111 | + * \return Query point lies outside MLP normalization range (0 is inside, 1 is outside). |
| 112 | + */ |
| 113 | + unsigned long Predict_MLP(su2double rho, su2double e); |
| 114 | + |
| 115 | + /*! |
| 116 | + * \brief Evaluate dataset through look-up table. |
| 117 | + * \param[in] rho - Density value. |
| 118 | + * \param[in] e - Static energy value. |
| 119 | + * \return Query point lies outside table data range (0 is inside, 1 is outside). |
| 120 | + */ |
| 121 | + unsigned long Predict_LUT(su2double rho, su2double e); |
| 122 | + |
| 123 | + /*! |
| 124 | + * \brief Evaluate the data set. |
| 125 | + * \param[in] rho - Density value. |
| 126 | + * \param[in] e - Static energy value. |
| 127 | + */ |
| 128 | + void Evaluate_Dataset(su2double rho, su2double e); |
| 129 | + |
| 130 | + /*! |
| 131 | + * \brief 2D Newton solver for computing the density and energy corresponding to Y1_target and Y2_target. |
| 132 | + * \param[in] Y1_target - Target value for output quantity 1. |
| 133 | + * \param[in] Y2_target - Target value for output quantity 2. |
| 134 | + * \param[in] Y1 - Pointer to output quantity 1. |
| 135 | + * \param[in] Y2 - Pointer to output quantity 2. |
| 136 | + * \param[in] dY1drho - Pointer to the partial derivative of quantity 1 w.r.t. density at constant energy. |
| 137 | + * \param[in] dY1de - Pointer to the partial derivative of quantity 1 w.r.t. energy at constant density. |
| 138 | + * \param[in] dY2drho - Pointer to the partial derivative of quantity 2 w.r.t. density at constant energy. |
| 139 | + * \param[in] dY2de - Pointer to the partial derivative of quantity 2 w.r.t. energy at constant density. |
| 140 | + */ |
| 141 | + void Run_Newton_Solver(su2double Y1_target, su2double Y2_target, su2double* Y1, su2double* Y2, su2double* dY1drho, |
| 142 | + su2double* dY1de, su2double* dY2drho, su2double* dY2de); |
| 143 | + |
| 144 | + /*! |
| 145 | + * \brief 1D Newton solver for computing the density or energy corresponding to Y_target. |
| 146 | + * \param[in] Y_target - Target quantity value. |
| 147 | + * \param[in] Y - Pointer to output quantity. |
| 148 | + * \param[in] X - Pointer to controlling variable (density or energy). |
| 149 | + * \param[in] dYdX - Pointer to the partial derivative of target quantity w.r.t. controlling variable. |
| 150 | + */ |
| 151 | + void Run_Newton_Solver(su2double Y_target, su2double* Y, su2double* X, su2double* dYdX); |
| 152 | + |
| 153 | + void ComputeIdealGasQuantities(); |
| 154 | + public: |
| 155 | + /*! |
| 156 | + * \brief Constructor of the class. |
| 157 | + */ |
| 158 | + CDataDrivenFluid(const CConfig* config, bool display = true); |
| 159 | + |
| 160 | + ~CDataDrivenFluid(); |
| 161 | + /*! |
| 162 | + * \brief Set the Dimensionless State using Density and Internal Energy. |
| 163 | + * \param[in] rho - first thermodynamic variable (density). |
| 164 | + * \param[in] e - second thermodynamic variable (static energy). |
| 165 | + */ |
| 166 | + void SetTDState_rhoe(su2double rho, su2double e) override; |
| 167 | + |
| 168 | + /*! |
| 169 | + * \brief Set the Dimensionless State using Pressure and Temperature. |
| 170 | + * \param[in] P - first thermodynamic variable (pressure). |
| 171 | + * \param[in] T - second thermodynamic variable (temperature). |
| 172 | + */ |
| 173 | + void SetTDState_PT(su2double P, su2double T) override; |
| 174 | + |
| 175 | + /*! |
| 176 | + * \brief Set the Dimensionless State using Pressure and Density. |
| 177 | + * \param[in] P - first thermodynamic variable (pressure). |
| 178 | + * \param[in] rho - second thermodynamic variable (density). |
| 179 | + */ |
| 180 | + void SetTDState_Prho(su2double P, su2double rho) override; |
| 181 | + |
| 182 | + /*! |
| 183 | + * \brief Set the Dimensionless Internal Energy using Pressure and Density. |
| 184 | + * \param[in] P - first thermodynamic variable (pressure). |
| 185 | + * \param[in] rho - second thermodynamic variable (density). |
| 186 | + */ |
| 187 | + void SetEnergy_Prho(su2double P, su2double rho) override; |
| 188 | + |
| 189 | + /*! |
| 190 | + * \brief Set the Dimensionless Internal Energy using Pressure and Density. |
| 191 | + * \param[in] rho - second thermodynamic variable (density). |
| 192 | + */ |
| 193 | + void SetTDState_rhoT(su2double rho, su2double T) override; |
| 194 | + |
| 195 | + /*! |
| 196 | + * \brief Set the Dimensionless State using Enthalpy and Entropy. |
| 197 | + * \param[in] h - first thermodynamic variable (h). |
| 198 | + * \param[in] s - second thermodynamic variable (s). |
| 199 | + */ |
| 200 | + void SetTDState_hs(su2double h, su2double s) override; |
| 201 | + |
| 202 | + /*! |
| 203 | + * \brief Set the Dimensionless State using Pressure and Entropy. |
| 204 | + * \param[in] P - first thermodynamic variable (P). |
| 205 | + * \param[in] s - second thermodynamic variable (s). |
| 206 | + */ |
| 207 | + void SetTDState_Ps(su2double P, su2double s) override; |
| 208 | + |
| 209 | + /*! |
| 210 | + * \brief Get fluid model extrapolation instance. |
| 211 | + * \return Query point lies outside fluid model data range. |
| 212 | + */ |
| 213 | + unsigned long GetExtrapolation() override { return outside_dataset; } |
| 214 | + |
| 215 | + /*! |
| 216 | + * \brief Get number of Newton solver iterations. |
| 217 | + * \return Newton solver iteration count at termination. |
| 218 | + */ |
| 219 | + unsigned long GetnIter_Newton() override { return nIter_Newton; } |
| 220 | +}; |
0 commit comments