|
32 | 32 | #include <limits> |
33 | 33 | #include <cstdlib> |
34 | 34 | #include <vector> |
35 | | -#include "../../CConfig.hpp" |
36 | 35 | #include "../../linear_algebra/blas_structure.hpp" |
37 | 36 | #include "CLookUp_ANN.hpp" |
38 | 37 |
|
39 | | -using namespace std; |
40 | 38 | namespace MLPToolbox |
41 | 39 | { |
42 | 40 |
|
43 | 41 | class CLookUp_ANN; |
44 | 42 |
|
45 | 43 | class CIOMap |
| 44 | + /*! |
| 45 | + *\class CIOMap |
| 46 | + *\brief This class is used by the CLookUp_ANN class to assign user-defined inputs |
| 47 | + * and outputs to loaded multi-layer perceptrons. When a look-up operation is called |
| 48 | + * with a specific CIOMap, the multi-layer perceptrons are evaluated with input and |
| 49 | + * output variables coinciding with the desired input and output variable names. |
| 50 | + * |
| 51 | + * |
| 52 | + * For example, in a custom, data-driven fluid model, MLP's are used for thermodynamic state |
| 53 | + * definition. There are three MLP's loaded. MLP_1 predicts temperature and specific heat |
| 54 | + * based on density and energy. MLP_2 predicts pressure and speed of sound based on density and |
| 55 | + * energy as well. MLP_3 predicts density and energy based on pressure and temperature. |
| 56 | + * During a certain look-up operation in the CFluidModel, temperature, speed of sound and pressure |
| 57 | + * are needed for a given density and energy. What the CIOMap does is to point to MLP_1 for |
| 58 | + * temperature evalutation, and to MLP_2 for pressure and speed of sound evaluation. MLP_3 is |
| 59 | + * not considered, as the respective inputs and outputs don't match with the function call |
| 60 | + * inputs and outputs. |
| 61 | + * |
| 62 | + * call variables: MLP inputs: MLP outputs: call outputs: |
| 63 | + * |
| 64 | + * 2--> energy --| |--> temperature --> 1 |
| 65 | + * |--> MLP_1 --| |
| 66 | + * 1:density 1--> density --| |--> c_p 1:temperature |
| 67 | + * 2:energy 2:speed of sound |
| 68 | + * 1--> density --| |--> pressure --> 3 3:pressure |
| 69 | + * |--> MLP_2 --| |
| 70 | + * 2--> energy --| |--> speed of sound --> 2 |
| 71 | + * |
| 72 | + * pressure --| |--> density |
| 73 | + * |--> MLP_3 --| |
| 74 | + * temperature --| |--> energy |
| 75 | + * |
| 76 | + * |
| 77 | + * \author E.Bunschoten |
| 78 | + */ |
| 79 | + |
46 | 80 | { |
47 | 81 | private: |
48 | | - vector<size_t> ANN_indices; |
49 | | - vector<vector<pair<size_t, size_t>>> Input_Map; |
50 | | - vector<vector<pair<size_t, size_t>>> Output_Map; |
51 | | - vector<vector<su2double>> input_indices; |
52 | | - public: |
53 | | - CIOMap(){}; |
54 | | - CIOMap(CLookUp_ANN*ANN_collection, vector<string> &inputs, vector<string> &outputs); |
55 | | - ~CIOMap(){}; |
56 | | - void Push_ANN_index(size_t i_ANN){ANN_indices.push_back(i_ANN);}; |
57 | | - void PairVariableswithANNs(CLookUp_ANN * ANN_collection, vector<string> &inputs, vector<string> &outputs); |
58 | | - size_t GetNANNs(){return ANN_indices.size();} |
59 | | - size_t GetANNIndex(size_t i_Map){return ANN_indices[i_Map];} |
60 | | - size_t GetInputIndex(size_t i_Map, size_t iInput){return Input_Map[i_Map][iInput].first;} |
61 | | - size_t GetOutputIndex(size_t i_Map, size_t iOutput){return Output_Map[i_Map][iOutput].first;} |
62 | | - size_t GetANNOutputIndex(size_t i_Map, size_t iOutput){return Output_Map[i_Map][iOutput].second;} |
63 | | - size_t GetNMappedOutputs(size_t i_Map){return Output_Map[i_Map].size();} |
64 | | - |
65 | | - vector<pair<size_t, size_t>> GetOutputMapping(size_t i_map){return Output_Map[i_map];} |
66 | | - vector<pair<size_t, size_t>> GetInputMapping(size_t i_map){return Input_Map[i_map];} |
| 82 | + std::vector<std::size_t> MLP_indices; /*!< Loaded MLP index */ |
| 83 | + |
| 84 | + std::vector<std::vector<std::pair<std::size_t, std::size_t>>> |
| 85 | + Input_Map, /*!< Mapping of call variable inputs to matching MLP inputs */ |
| 86 | + Output_Map; /*!< Mapping of call variable outputs to matching MLP outputs */ |
| 87 | + |
| 88 | + std::vector<std::vector<su2double>> input_indices; /*!< */ |
67 | 89 |
|
68 | | - vector<su2double> GetANN_Inputs(size_t i_Map, vector<su2double>&inputs){ |
69 | | - vector<su2double> ANN_input; |
70 | | - ANN_input.resize(Input_Map[i_Map].size()); |
| 90 | + public: |
71 | 91 |
|
72 | | - for(size_t iInput=0; iInput<Input_Map[i_Map].size(); iInput++){ |
73 | | - ANN_input.at(iInput) = inputs[GetInputIndex(i_Map, iInput)]; |
74 | | - } |
75 | | - return ANN_input; |
76 | | - } |
77 | | - vector<su2double*> GetANN_Outputs(size_t i_Map, vector<su2double*>&outputs){ |
78 | | - vector<su2double*> ANN_output; |
79 | | - ANN_output.resize(Output_Map[i_Map].size()); |
| 92 | + /*! |
| 93 | + * \brief CIOMap class constructor; |
| 94 | + * \param[in] MLP_collection - Pointer to CLookUp_ANN class for which the input-output amap is to be created |
| 95 | + * \param[in] inputs - Input names for the call function. These should match with at least one of the MLP inputs. |
| 96 | + * \param[in] outputs - Output names for the call function. These should match with at least one of the MLP outputs. |
| 97 | + */ |
| 98 | + CIOMap(CLookUp_ANN*MLP_collection, std::vector<std::string> &inputs, std::vector<std::string> &outputs); |
| 99 | + |
| 100 | + /*! |
| 101 | + * \brief Set MLP index in IO map |
| 102 | + * \param[in] i_MLP - loaded MLP index |
| 103 | + */ |
| 104 | + void Push_MLP_index(std::size_t i_MLP){MLP_indices.push_back(i_MLP);}; |
| 105 | + |
| 106 | + /*! |
| 107 | + * \brief Pair call inputs and outputs with the inputs and outputs of |
| 108 | + the loaded MLPs |
| 109 | + * \param[in] MLP_collection - pointer to MLP collection class |
| 110 | + * \param[in] inputs - vector with call input variable names |
| 111 | + * \param[in] outputs - vector with call output variable names |
| 112 | + */ |
| 113 | + void PairVariableswithMLPs(CLookUp_ANN * MLP_collection, std::vector<std::string> &inputs, std::vector<std::string> &outputs); |
| 114 | + |
| 115 | + /*! |
| 116 | + * \brief Get the number of MLPs in the current IO map |
| 117 | + * \return number of MLPs with matching inputs and output(s) |
| 118 | + */ |
| 119 | + std::size_t GetNMLPs() const {return MLP_indices.size();} |
| 120 | + |
| 121 | + /*! |
| 122 | + * \brief Get the loaded MLP index |
| 123 | + * \return MLP index |
| 124 | + */ |
| 125 | + std::size_t GetMLPIndex(std::size_t i_Map) const {return MLP_indices[i_Map];} |
| 126 | + |
| 127 | + /*! |
| 128 | + * \brief Get the call input variable index |
| 129 | + * \param[in] i_Map - input-output mapping index of the IO map |
| 130 | + * \param[in] i_Input - input index of the call input variable |
| 131 | + * \return MLP input variable index |
| 132 | + */ |
| 133 | + std::size_t GetInputIndex(std::size_t i_Map, std::size_t i_Input) const {return Input_Map[i_Map][i_Input].first;} |
| 134 | + |
| 135 | + /*! |
| 136 | + * \brief Get the call output variable index |
| 137 | + * \param[in] i_Map - input-output mapping index of the IO map |
| 138 | + * \param[in] i_Output - output index of the call input variable |
| 139 | + * \return call variable output index |
| 140 | + */ |
| 141 | + std::size_t GetOutputIndex(std::size_t i_Map, std::size_t i_Output) const {return Output_Map[i_Map][i_Output].first;} |
| 142 | + |
| 143 | + /*! |
| 144 | + * \brief Get the MLP output variable index |
| 145 | + * \param[in] i_Map - input-output mapping index of the IO map |
| 146 | + * \param[in] i_Output - output index of the call input variable |
| 147 | + * \return MLP output variable index |
| 148 | + */ |
| 149 | + std::size_t GetMLPOutputIndex(std::size_t i_Map, std::size_t i_Output) const {return Output_Map[i_Map][i_Output].second;} |
| 150 | + |
| 151 | + /*! |
| 152 | + * \brief Get the number of matching output variables between the call and MLP outputs |
| 153 | + * \param[in] i_Map - input-output mapping index of the IO map |
| 154 | + * \return Number of matching variables between the loaded MLP and call variables |
| 155 | + */ |
| 156 | + std::size_t GetNMappedOutputs(std::size_t i_Map) const {return Output_Map[i_Map].size();} |
| 157 | + |
| 158 | + /*! |
| 159 | + * \brief Get the mapping of MLP outputs matching to call outputs |
| 160 | + * \param[in] i_Map - input-output mapping index of the IO map |
| 161 | + * \return Mapping of MLP output variables to call variables |
| 162 | + */ |
| 163 | + std::vector<std::pair<std::size_t, std::size_t>> GetOutputMapping(std::size_t i_map) const {return Output_Map[i_map];} |
| 164 | + |
| 165 | + /*! |
| 166 | + * \brief Get the mapping of MLP inputs to call inputs |
| 167 | + * \param[in] i_Map - input-output mapping index of the IO map |
| 168 | + * \return Mapping of MLP input variables to call inputs |
| 169 | + */ |
| 170 | + std::vector<std::pair<std::size_t, std::size_t>> GetInputMapping(std::size_t i_map) const{return Input_Map[i_map];} |
| 171 | + |
| 172 | + /*! |
| 173 | + * \brief Get the mapped inputs for the MLP at i_Map |
| 174 | + * \param[in] i_Map - input-output mapping index of the IO map |
| 175 | + * \param[in] inputs - call inputs |
| 176 | + * \return Vector with call inputs in the correct order of the loaded MLP |
| 177 | + */ |
| 178 | + std::vector<su2double> GetMLP_Inputs(std::size_t i_Map, std::vector<su2double>&inputs) const { |
| 179 | + std::vector<su2double> MLP_input; |
| 180 | + MLP_input.resize(Input_Map[i_Map].size()); |
80 | 181 |
|
81 | | - for(size_t iOutput=0; iOutput<Output_Map[i_Map].size(); iOutput++){ |
82 | | - ANN_output.at(iOutput) = outputs[GetOutputIndex(i_Map, iOutput)]; |
| 182 | + for(std::size_t i_Input=0; i_Input<Input_Map[i_Map].size(); i_Input++){ |
| 183 | + MLP_input[i_Input] = inputs[GetInputIndex(i_Map, i_Input)]; |
83 | 184 | } |
84 | | - return ANN_output; |
| 185 | + return MLP_input; |
85 | 186 | } |
86 | 187 | }; |
87 | 188 | } |
0 commit comments