Skip to content

Commit e497514

Browse files
Added AD unit tests for fluid model classes.
1 parent 880b1a3 commit e497514

File tree

2 files changed

+163
-1
lines changed

2 files changed

+163
-1
lines changed
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
/*!
2+
* \file CFluidModel_tests.cpp
3+
* \brief AD unit tests for the fluid model classes.
4+
* \author E.Bunschoten
5+
* \version 8.1.0 "Harrier"
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-2024, 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+
#include "catch.hpp"
29+
#include <sstream>
30+
#include "../../../SU2_CFD/include/fluid/CFluidModel.hpp"
31+
#include "../../../SU2_CFD/include/fluid/CIdealGas.hpp"
32+
#include "../../../SU2_CFD/include/fluid/CDataDrivenFluid.hpp"
33+
#include "../../../SU2_CFD/include/fluid/CPengRobinson.hpp"
34+
35+
void FluidModelADChecks(CFluidModel * fluid_model, su2double val_p, su2double val_T) {
36+
/*--- Consistency tests for the fluid model secondary variables and AD integration. ---*/
37+
38+
/*--- Extract fluid density and static energy. ---*/
39+
fluid_model->SetTDState_PT(val_p, val_T);
40+
su2double val_rho = fluid_model->GetDensity();
41+
su2double val_e = fluid_model->GetStaticEnergy();
42+
43+
/*--- Test 1: check consistency of pressure derivatives. ---*/
44+
AD::Reset();
45+
AD::StartRecording();
46+
AD::RegisterInput(val_rho);
47+
AD::RegisterInput(val_e);
48+
49+
fluid_model->SetTDState_rhoe(val_rho, val_e);
50+
su2double val_p_pred = fluid_model->GetPressure();
51+
AD::RegisterOutput(val_p_pred);
52+
AD::StopRecording();
53+
54+
SU2_TYPE::SetDerivative(val_p_pred, 1.0);
55+
AD::ComputeAdjoint();
56+
57+
/*--- Extract pressure derivatives from fluid model and AD. ---*/
58+
const su2double dpdrho_e_AD = SU2_TYPE::GetDerivative(val_rho);
59+
const su2double dpde_rho_AD = SU2_TYPE::GetDerivative(val_e);
60+
const su2double dpdrho_e_pred = fluid_model->GetdPdrho_e();
61+
const su2double dpde_rho_pred = fluid_model->GetdPde_rho();
62+
63+
CHECK(SU2_TYPE::GetValue(dpdrho_e_AD) == Approx(SU2_TYPE::GetValue(dpdrho_e_pred)));
64+
CHECK(SU2_TYPE::GetValue(dpde_rho_AD) == Approx(SU2_TYPE::GetValue(dpde_rho_pred)));
65+
66+
/*--- Test 2: check consistency of temperature derivatives. ---*/
67+
AD::Reset();
68+
AD::StartRecording();
69+
AD::RegisterInput(val_rho);
70+
AD::RegisterInput(val_e);
71+
72+
fluid_model->SetTDState_rhoe(val_rho, val_e);
73+
su2double val_T_pred = fluid_model->GetTemperature();
74+
75+
AD::RegisterOutput(val_T_pred);
76+
AD::StopRecording();
77+
78+
SU2_TYPE::SetDerivative(val_T_pred, 1.0);
79+
AD::ComputeAdjoint();
80+
81+
/*--- Extract temperature derivatives from fluid model and AD. ---*/
82+
const su2double dTdrho_e_AD = SU2_TYPE::GetDerivative(val_rho);
83+
const su2double dTde_rho_AD = SU2_TYPE::GetDerivative(val_e);
84+
const su2double dTdrho_e_pred = fluid_model->GetdTdrho_e();
85+
const su2double dTde_rho_pred = fluid_model->GetdTde_rho();
86+
87+
CHECK(SU2_TYPE::GetValue(dTdrho_e_AD) == Approx(SU2_TYPE::GetValue(dTdrho_e_pred)));
88+
CHECK(SU2_TYPE::GetValue(dTde_rho_AD) == Approx(SU2_TYPE::GetValue(dTde_rho_pred)));
89+
90+
/*--- Test 3: check consistency of specific heat at constant pressure. ---*/
91+
const su2double drhode_p = -dpde_rho_AD/dpdrho_e_AD;
92+
const su2double dTde_p = dTde_rho_AD + dTdrho_e_AD*drhode_p;
93+
const su2double dhde_rho = 1 + dpde_rho_AD / val_rho;
94+
const su2double dhdrho_e = -val_p * (1 / pow(val_rho, 2)) + dpdrho_e_AD / val_rho;
95+
const su2double dhde_p = dhde_rho + drhode_p*dhdrho_e;
96+
const su2double Cp_AD = dhde_p / dTde_p;
97+
const su2double Cp_pred = fluid_model->GetCp();
98+
99+
CHECK(SU2_TYPE::GetValue(Cp_AD) == Approx(SU2_TYPE::GetValue(Cp_pred)));
100+
101+
/*--- Test 4: check consistency of secondary variables for non-reflecting boundary conditions.---*/
102+
AD::Reset();
103+
AD::StartRecording();
104+
AD::RegisterInput(val_p);
105+
AD::RegisterInput(val_rho);
106+
107+
fluid_model->ComputeDerivativeNRBC_Prho(val_p, val_rho);
108+
su2double val_s_pred = fluid_model->GetEntropy();
109+
110+
AD::RegisterOutput(val_s_pred);
111+
AD::StopRecording();
112+
113+
SU2_TYPE::SetDerivative(val_s_pred, 1.0);
114+
AD::ComputeAdjoint();
115+
const su2double dsdp_rho_AD = SU2_TYPE::GetDerivative(val_p);
116+
const su2double dsdrho_p_AD = SU2_TYPE::GetDerivative(val_rho);
117+
const su2double dsdp_rho_pred = fluid_model->GetdsdP_rho();
118+
const su2double dsdrho_p_pred = fluid_model->Getdsdrho_P();
119+
120+
CHECK(SU2_TYPE::GetValue(dsdp_rho_pred) == Approx(SU2_TYPE::GetValue(dsdp_rho_AD)));
121+
CHECK(SU2_TYPE::GetValue(dsdrho_p_pred) == Approx(SU2_TYPE::GetValue(dsdrho_p_AD)));
122+
123+
}
124+
125+
TEST_CASE("AD test case for ideal gas fluid model","[AD tests]") {
126+
127+
CIdealGas * fluid_model = new CIdealGas(1.4, 287.0,true);
128+
FluidModelADChecks(fluid_model, 101325, 300.0);
129+
130+
delete fluid_model;
131+
}
132+
133+
134+
TEST_CASE("AD test case for data-driven fluid model","[AD tests]") {
135+
std::stringstream config_options;
136+
137+
config_options << "SOLVER=RANS" << std::endl;
138+
config_options<<"KIND_TURB_MODEL=SA"<<std::endl;
139+
config_options<<"SA_OPTIONS= NONE"<<std::endl;
140+
config_options<<"REYNOLDS_NUMBER=1e6"<<std::endl;
141+
config_options << "FLUID_MODEL=DATADRIVEN_FLUID" << std::endl;
142+
config_options << "USE_PINN=YES" << std::endl;
143+
config_options << "INTERPOLATION_METHOD=MLP" << std::endl;
144+
config_options << "FILENAMES_INTERPOLATOR=(/home/ecbunschoten/SU2_NICFD/UnitTests/SU2_CFD/fluid/MLP_PINN.mlp)"<<std::endl;
145+
config_options << "CONV_NUM_METHOD_FLOW=JST"<<std::endl;
146+
147+
/*--- Setup ---*/
148+
149+
CConfig* config = new CConfig(config_options, SU2_COMPONENT::SU2_CFD, false);
150+
151+
/*--- Define fluid model ---*/
152+
CDataDrivenFluid *fluid_model = new CDataDrivenFluid(config, false);
153+
154+
FluidModelADChecks(fluid_model, 3e5, 520.0);
155+
FluidModelADChecks(fluid_model, 1.83e6, 523.0);
156+
157+
delete config;
158+
delete fluid_model;
159+
}
160+

UnitTests/meson.build

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@ su2_cfd_tests = files(['Common/geometry/primal_grid/CPrimalGrid_tests.cpp',
1414
'Common/containers/CLookupTable_tests.cpp',
1515
'Common/toolboxes/multilayer_perceptron/CLookUp_ANN_tests.cpp',
1616
'SU2_CFD/numerics/CNumerics_tests.cpp',
17+
'SU2_CFD/fluid/CFluidModel_tests.cpp',
1718
'SU2_CFD/gradients.cpp',
1819
'SU2_CFD/windowing.cpp'])
1920

2021
# Reverse-mode (algorithmic differentiation) tests:
21-
su2_cfd_tests_ad = files(['Common/simple_ad_test.cpp'])
22+
su2_cfd_tests_ad = files(['Common/simple_ad_test.cpp',
23+
'SU2_CFD/fluid/CFluidModel_tests_AD.cpp'])
2224

2325
# Forward-mode (direct differentiation) tests:
2426
su2_cfd_tests_dd = files(['Common/simple_directdiff_test.cpp'])

0 commit comments

Comments
 (0)