Skip to content

Commit 04813b1

Browse files
Added a MLP fluid model template class
1 parent 76b1411 commit 04813b1

11 files changed

Lines changed: 111 additions & 43 deletions

File tree

Common/include/CConfig.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ class CConfig {
223223
string Inlet_Filename; /*!< \brief Filename specifying an inlet profile. */
224224
su2double Inlet_Matching_Tol; /*!< \brief Tolerance used when matching a point to a point from the inlet file. */
225225
string ActDisk_FileName; /*!< \brief Filename specifying an actuator disk. */
226+
string MLP_filename; /*!< \brief Filename specifying an MLP-driven fluid model. */
226227

227228
string *Marker_Euler, /*!< \brief Euler wall markers. */
228229
*Marker_FarField, /*!< \brief Far field markers. */
@@ -4842,6 +4843,12 @@ class CConfig {
48424843
*/
48434844
string GetActDisk_FileName(void) const { return ActDisk_FileName; }
48444845

4846+
/*!
4847+
* \brief Get name of the input file for the fluid model multi-layer perceptron collection.
4848+
* \return Name of the input file for the specified MLP collection file.
4849+
*/
4850+
string GetMLP_FileName(void) const { return MLP_filename; }
4851+
48454852
/*!
48464853
* \brief Get the tolerance used for matching two points on a specified inlet
48474854
* \return Tolerance used for matching a point to a specified inlet

Common/include/option_structure.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,7 @@ enum ENUM_FLUIDMODEL {
569569
MUTATIONPP = 7, /*!< \brief Mutation++ gas model for nonequilibrium flow. */
570570
SU2_NONEQ = 8, /*!< \brief User defined gas model for nonequilibrium flow. */
571571
FLUID_MIXTURE = 9, /*!< \brief Species mixture model. */
572+
MLP_GAS = 10, /*!< \brief multi-layer perceptron driven fluid model. */
572573
};
573574
static const MapType<std::string, ENUM_FLUIDMODEL> FluidModel_Map = {
574575
MakePair("STANDARD_AIR", STANDARD_AIR)
@@ -581,6 +582,7 @@ static const MapType<std::string, ENUM_FLUIDMODEL> FluidModel_Map = {
581582
MakePair("MUTATIONPP", MUTATIONPP)
582583
MakePair("SU2_NONEQ", SU2_NONEQ)
583584
MakePair("FLUID_MIXTURE", FLUID_MIXTURE)
585+
MakePair("MLP_GAS", MLP_GAS)
584586
};
585587

586588
/*!

Common/src/CConfig.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1482,6 +1482,9 @@ void CConfig::SetConfig_Options() {
14821482
/*!\brief ACTDISK_FILENAME \n DESCRIPTION: Input file for a specified actuator disk (w/ extension) \n DEFAULT: actdiskinput.dat \ingroup Config*/
14831483
addStringOption("ACTDISK_FILENAME", ActDisk_FileName, string("actdiskinput.dat"));
14841484

1485+
/*!\brief MLP_FILENAME \n DESCRIPTION: Input file for a multi-layer perceptron input file for fluid model definition. w/ extension) \n DEFAULT: MLP_collection.mlp \ingroup Config*/
1486+
addStringOption("MLP_FILENAME", MLP_filename, string("MLP_collection.mlp"));
1487+
14851488
/*!\brief INLET_TYPE \n DESCRIPTION: Inlet boundary type \n OPTIONS: see \link Inlet_Map \endlink \n DEFAULT: TOTAL_CONDITIONS \ingroup Config*/
14861489
addEnumOption("INLET_TYPE", Kind_Inlet, Inlet_Map, INLET_TYPE::TOTAL_CONDITIONS);
14871490
/*!\brief INC_INLET_TYPE \n DESCRIPTION: List of inlet types for incompressible flows. List length must match number of inlet markers. Options: VELOCITY_INLET, PRESSURE_INLET, INPUT_FILE. \ingroup Config*/

Common/src/toolboxes/multilayer_perceptron/CIOMap.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ MLPToolbox::CIOMap::CIOMap(CLookUp_ANN*MLP_collection, vector<string> &inputs, v
4848
MLP_collection->Check_Use_of_Outputs(outputs, this);
4949

5050
// Check wether there are any duplicates in the MLP outputs
51-
MLP_collection->Check_Duplicate_Outputs(outputs, this);
51+
//MLP_collection->Check_Duplicate_Outputs(outputs, this);
5252
}
5353
}
5454
void MLPToolbox::CIOMap::PairVariableswithMLPs(CLookUp_ANN*MLP_collection, vector<string> &inputs, vector<string> &outputs){

Common/src/toolboxes/multilayer_perceptron/CReadNeuralNetwork.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ using namespace std;
3030

3131
MLPToolbox::CReadNeuralNetwork::CReadNeuralNetwork(string filename_in){
3232
filename = filename_in;
33-
};
33+
}
3434

3535
void MLPToolbox::CReadNeuralNetwork::ReadMLPFile(){
3636
ifstream file_stream;
@@ -109,7 +109,6 @@ void MLPToolbox::CReadNeuralNetwork::ReadMLPFile(){
109109
/* Read MLP input variable names */
110110
if(line.compare("[input names]") == 0){
111111
found_input_names = true;
112-
unsigned short j{1};
113112
getline(file_stream, line);
114113
while(line.compare("") != 0){
115114
input_names.push_back(line);

SU2_CFD/include/fluid/CMLPGas_Template.hpp renamed to SU2_CFD/include/fluid/CMLPGas.hpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*!
2-
* \file CMLPGas_Template.hpp
2+
* \file CMLPGas.hpp
33
* \brief Defines a template fluid model class using multilayer perceptrons
44
* for theromodynamic state definition
55
* \author E.Bunschoten
@@ -31,17 +31,18 @@
3131
#include "CFluidModel.hpp"
3232
#include "../../../Common/include/toolboxes/multilayer_perceptron/CLookUp_ANN.hpp"
3333
/*!
34-
* \class CMLPGas_Template
34+
* \class CMLPGas
3535
* \brief Template class for fluid model definition using multi-layer perceptrons for
3636
* fluid dynamic state definition.
3737
* \author: E.Bunschoten.
3838
*/
39-
class CMLPGas_Template : public CFluidModel {
39+
class CMLPGas : public CFluidModel {
4040
protected:
4141
su2double Gamma{0.0}; /*!< \brief Ratio of Specific Heats. */
4242
su2double Gamma_Minus_One{0.0}; /*!< \brief Ratio of Specific Heats Minus One. */
4343
su2double Gas_Constant{0.0}; /*!< \brief Gas Constant. */
44-
bool ComputeEntropy{true}; /*!< \brief Whether or not to compute entropy. */
44+
45+
su2double R_u = 8.31451;
4546

4647
string ann_input_filename;
4748
MLPToolbox::CLookUp_ANN * lookup_ann;
@@ -69,9 +70,9 @@ class CMLPGas_Template : public CFluidModel {
6970
/*!
7071
* \brief Constructor of the class.
7172
*/
72-
CMLPGas_Template(su2double gamma, su2double R, bool CompEntropy = true);
73+
CMLPGas(const CConfig* config);
7374

74-
~CMLPGas_Template();
75+
~CMLPGas();
7576
/*!
7677
* \brief Set the Dimensionless State using Density and Internal Energy
7778
* \param[in] rho - first thermodynamic variable.

SU2_CFD/obj/Makefile.am

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ libSU2Core_sources = ../src/definition_structure.cpp \
5757
../src/fluid/CNEMOGas.cpp \
5858
../src/fluid/CSU2TCLib.cpp \
5959
../src/fluid/CMutationTCLib.cpp \
60-
../src/fluid/CMLPGas_Template.cpp \
60+
../src/fluid/CMLPGas.cpp \
6161
../src/integration/CIntegration.cpp \
6262
../src/integration/CSingleGridIntegration.cpp \
6363
../src/integration/CMultiGridIntegration.cpp \
Lines changed: 63 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*!
2-
* \file CMLPGas_Template.cpp
2+
* \file CMLPGas.cpp
33
* \brief Source of the data-driven fluid model class using
44
* multilayer perceptrons for data regression
55
* \author E.Bunschoten
@@ -26,35 +26,28 @@
2626
* License along with SU2. If not, see <http://www.gnu.org/licenses/>.
2727
*/
2828

29-
#include "../../include/fluid/CMLPGas_Template.hpp"
29+
#include "../../include/fluid/CMLPGas.hpp"
3030

31-
CMLPGas_Template::CMLPGas_Template(su2double gamma, su2double R, bool CompEntropy) : CFluidModel() {
32-
Gamma = gamma;
33-
Gamma_Minus_One = Gamma - 1.0;
34-
Gas_Constant = R;
35-
Cp = Gamma / Gamma_Minus_One * Gas_Constant;
36-
Cv = Cp - R;
37-
38-
ComputeEntropy = CompEntropy;
31+
CMLPGas::CMLPGas(const CConfig* config) : CFluidModel() {
3932

4033
/* Define a CLookUp_ANN object to allow for data regression */
41-
ann_input_filename = "MLP_collection.mlp";
34+
ann_input_filename = config->GetMLP_FileName();
4235
lookup_ann = new MLPToolbox::CLookUp_ANN(ann_input_filename);
4336

4437
/* Map MLP inputs to outputs for each look-up operation*/
4538
MapInputs_to_Outputs();
4639
}
4740

48-
CMLPGas_Template::~CMLPGas_Template(){
41+
CMLPGas::~CMLPGas(){
4942
delete iomap_rhoe;
5043
delete iomap_PT;
5144
delete iomap_Prho;
5245
delete lookup_ann;
5346
}
54-
void CMLPGas_Template::MapInputs_to_Outputs(){
47+
void CMLPGas::MapInputs_to_Outputs(){
5548

56-
input_names_rhoe.push_back("rho");
57-
input_names_rhoe.push_back("e");
49+
input_names_rhoe.push_back("Density");
50+
input_names_rhoe.push_back("Energy");
5851

5952
output_names_rhoe.push_back("Temperature");
6053
outputs_rhoe.push_back(&Temperature);
@@ -75,49 +68,87 @@ void CMLPGas_Template::MapInputs_to_Outputs(){
7568

7669
iomap_rhoe = new MLPToolbox::CIOMap(lookup_ann, input_names_rhoe, output_names_rhoe);
7770

78-
input_names_PT.push_back("P");
79-
input_names_PT.push_back("T");
71+
input_names_PT.push_back("Pressure");
72+
input_names_PT.push_back("Temperature");
8073

81-
output_names_PT.push_back("rho");
74+
output_names_PT.push_back("Density");
8275
outputs_PT.push_back(&Density);
83-
output_names_PT.push_back("e");
76+
output_names_PT.push_back("Energy");
8477
outputs_PT.push_back(&StaticEnergy);
8578

8679
iomap_PT = new MLPToolbox::CIOMap(lookup_ann, input_names_PT, output_names_PT);
8780

88-
input_names_Prho.push_back("P");
89-
input_names_Prho.push_back("rho");
81+
input_names_Prho.push_back("Pressure");
82+
input_names_Prho.push_back("Density");
9083

91-
output_names_Prho.push_back("e");
84+
output_names_Prho.push_back("Energy");
9285
outputs_Prho.push_back(&StaticEnergy);
9386
iomap_Prho = new MLPToolbox::CIOMap(lookup_ann, input_names_Prho, output_names_Prho);
9487
}
9588

96-
void CMLPGas_Template::SetTDState_rhoe(su2double rho, su2double e) {
89+
void CMLPGas::SetTDState_rhoe(su2double rho, su2double e) {
90+
Density = rho;
91+
StaticEnergy = e;
9792
vector<su2double> ann_inputs;
9893
ann_inputs.push_back(rho);
9994
ann_inputs.push_back(e);
10095
lookup_ann->Predict_ANN(iomap_rhoe, ann_inputs, outputs_rhoe);
101-
}
96+
Cp = (Pressure / (R_u * Density * Temperature)) * 1 / (dTde_rho);
97+
Cv = (1/dTde_rho);
98+
Gamma = Cp / Cv;
99+
Gamma_Minus_One = Gamma - 1;
100+
Gas_Constant = Cp * Gamma_Minus_One;
102101

103-
void CMLPGas_Template::SetTDState_PT(su2double P, su2double T) {
104-
vector<su2double> ann_inputs;
105-
ann_inputs.push_back(P);
106-
ann_inputs.push_back(T);
107-
lookup_ann->Predict_ANN(iomap_PT, ann_inputs, outputs_PT);
102+
}
108103

109-
SetTDState_rhoe(Density, StaticEnergy);
104+
void CMLPGas::SetTDState_PT(su2double P, su2double T) {
105+
// vector<su2double> ann_inputs;
106+
// ann_inputs.push_back(P);
107+
// ann_inputs.push_back(T);
108+
// lookup_ann->Predict_ANN(iomap_PT, ann_inputs, outputs_PT);
109+
110+
su2double rho = 0.5*(6.9682399336300005e-01 + 2.4896548789900002e+00);
111+
su2double e = 0.5*(3.2562734877400001e+05 +4.8566089128400001e+05);
112+
rho = 1.0;
113+
e = 3.2562734877400001e+05;
114+
su2double delta_P, delta_T, delta_rho, delta_e, tolerance_P = 10, tolerance_T = 1.0;
115+
su2double determinant, relaxation = 0.1;
116+
unsigned long iter = 0, iter_max = 1000;
117+
bool converged= false;
118+
cout << "Target PT: " << P << " " << T << endl;
119+
while(!converged && (iter < iter_max)){
120+
SetTDState_rhoe(rho, e);
121+
delta_P = (P - Pressure);
122+
delta_T = (T - Temperature);
123+
cout << delta_P << " " << delta_T << endl;
124+
if((abs(delta_P)<tolerance_P) && (abs(delta_T) < tolerance_T)){
125+
converged = true;
126+
cout << "Converged PT: " << Pressure << " " << Temperature << endl;
127+
return;
128+
}else{
129+
determinant = dPdrho_e * dTde_rho - dPde_rho * dTdrho_e;
130+
delta_rho = (dTde_rho * delta_P - dPde_rho * delta_T) / determinant;
131+
delta_T = (-dTdrho_e * delta_P + dPdrho_e * delta_T) / determinant;
132+
133+
rho -= relaxation * delta_rho;
134+
e -= relaxation * delta_e;
135+
}
136+
iter++;
137+
}
138+
139+
SetTDState_rhoe(rho, e);
140+
cout << "Final(nonconverged) PT: " << Pressure << " " << Temperature << endl;
110141
}
111142

112-
void CMLPGas_Template::SetTDState_Prho(su2double P, su2double rho) {
143+
void CMLPGas::SetTDState_Prho(su2double P, su2double rho) {
113144
vector<su2double> ann_inputs;
114145
ann_inputs.push_back(P);
115146
ann_inputs.push_back(rho);
116147
lookup_ann->Predict_ANN(iomap_Prho, ann_inputs, outputs_Prho);
117148
SetTDState_rhoe(rho, StaticEnergy);
118149
}
119150

120-
void CMLPGas_Template::SetEnergy_Prho(su2double P, su2double rho) {
151+
void CMLPGas::SetEnergy_Prho(su2double P, su2double rho) {
121152
vector<su2double> ann_inputs;
122153
ann_inputs.push_back(P);
123154
ann_inputs.push_back(rho);

SU2_CFD/src/meson.build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ su2_cfd_src += files(['fluid/CFluidModel.cpp',
1111
'fluid/CNEMOGas.cpp',
1212
'fluid/CMutationTCLib.cpp',
1313
'fluid/CSU2TCLib.cpp',
14-
'fluid/CMLPGas_Template.cpp'])
14+
'fluid/CMLPGas.cpp'])
1515

1616
su2_cfd_src += files(['output/COutputFactory.cpp',
1717
'output/CAdjElasticityOutput.cpp',

SU2_CFD/src/solvers/CEulerSolver.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "../../include/fluid/CIdealGas.hpp"
3333
#include "../../include/fluid/CVanDerWaalsGas.hpp"
3434
#include "../../include/fluid/CPengRobinson.hpp"
35+
#include "../../include/fluid/CMLPGas.hpp"
3536
#include "../../include/numerics_simd/CNumericsSIMD.hpp"
3637
#include "../../include/limiters/CLimiterDetails.hpp"
3738

@@ -857,6 +858,12 @@ void CEulerSolver::SetNondimensionalization(CConfig *config, unsigned short iMes
857858
auxFluidModel = new CPengRobinson(Gamma, config->GetGas_Constant(), config->GetPressure_Critical(),
858859
config->GetTemperature_Critical(), config->GetAcentric_Factor());
859860
break;
861+
862+
case MLP_GAS:
863+
864+
auxFluidModel = new CMLPGas(config);
865+
866+
break;
860867

861868
default:
862869
SU2_MPI::Error("Unknown fluid model.", CURRENT_FUNCTION);
@@ -1070,6 +1077,11 @@ void CEulerSolver::SetNondimensionalization(CConfig *config, unsigned short iMes
10701077
config->GetTemperature_Critical() / config->GetTemperature_Ref(),
10711078
config->GetAcentric_Factor());
10721079
break;
1080+
1081+
case MLP_GAS:
1082+
FluidModel[thread] = new CMLPGas(config);
1083+
1084+
break;
10731085
}
10741086

10751087
GetFluidModel()->SetEnergy_Prho(Pressure_FreeStreamND, Density_FreeStreamND);

0 commit comments

Comments
 (0)