Skip to content

Commit 6d79df7

Browse files
authored
Merge pull request #46 from su2code/develop
Develop
2 parents 688e6bb + 25d54da commit 6d79df7

File tree

35 files changed

+1850
-84
lines changed

35 files changed

+1850
-84
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
#!/usr/bin/env python
2+
3+
## \file Generate_Dataset.py
4+
# \brief Example python script for generating training data for
5+
# data-driven fluid model in SU2
6+
# \author E.C.Bunschoten
7+
# \version 7.5.1 "Blackbird"
8+
#
9+
# SU2 Project Website: https://su2code.github.io
10+
#
11+
# The SU2 Project is maintained by the SU2 Foundation
12+
# (http://su2foundation.org)
13+
#
14+
# Copyright 2012-2023, SU2 Contributors (cf. AUTHORS.md)
15+
#
16+
# SU2 is free software; you can redistribute it and/or
17+
# modify it under the terms of the GNU Lesser General Public
18+
# License as published by the Free Software Foundation; either
19+
# version 2.1 of the License, or (at your option) any later version.
20+
#
21+
# SU2 is distributed in the hope that it will be useful,
22+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
23+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24+
# Lesser General Public License for more details.
25+
#
26+
# You should have received a copy of the GNU Lesser General Public
27+
# License along with SU2. If not, see <http://www.gnu.org/licenses/>.
28+
29+
# make print(*args) function available in PY2.6+, does'nt work on PY < 2.6
30+
31+
import CoolProp
32+
import numpy as np
33+
from tqdm import tqdm
34+
import csv
35+
36+
# Name of the fluid in the CoolProp library.
37+
fluidName = 'Air'
38+
39+
# Type of equation of state to be used by CoolProp.
40+
CP_eos = "HEOS"
41+
42+
# Minimum and maximum dataset temperatures [K].
43+
T_min = 280
44+
T_max = 1000
45+
46+
# Minimum and maximum dataset pressures [Pa].
47+
P_min = 5e4
48+
P_max = 2e6
49+
50+
# Number of data points along each axis.
51+
Np_grid = 500
52+
53+
# Fraction of data points to be used as training data for MLP training (0-1).
54+
f_train = 0.8
55+
56+
# Fraction of data poins to be used as test data for MLP validation (0-1).
57+
f_test = 0.1
58+
59+
60+
# Prepare data grid
61+
T_range = np.linspace(T_min, T_max, Np_grid)
62+
P_range = np.linspace(P_min, P_max, Np_grid)
63+
64+
T_grid, P_grid = np.meshgrid(T_range, P_range)
65+
66+
T_dataset = T_grid.flatten()
67+
P_dataset = P_grid.flatten()
68+
69+
density_dataset = np.zeros(np.shape(T_dataset))
70+
energy_dataset = np.zeros(np.shape(T_dataset))
71+
s_dataset = np.zeros(np.shape(T_dataset))
72+
dsde_dataset = np.zeros(np.shape(T_dataset))
73+
dsdrho_dataset = np.zeros(np.shape(T_dataset))
74+
d2sde2_dataset = np.zeros(np.shape(T_dataset))
75+
d2sdedrho_dataset = np.zeros(np.shape(T_dataset))
76+
d2sdrho2_dataset = np.zeros(np.shape(T_dataset))
77+
78+
# Evaluate CoolProp on data grid.
79+
fluid = CoolProp.AbstractState(CP_eos, fluidName)
80+
idx_failed_below = []
81+
idx_failed_above = []
82+
print("Generating CoolProp data set...")
83+
for i in tqdm(range(len(T_dataset))):
84+
try:
85+
fluid.update(CoolProp.PT_INPUTS, P_dataset[i], T_dataset[i])
86+
87+
density_dataset[i] = fluid.rhomass()
88+
energy_dataset[i] = fluid.umass()
89+
s_dataset[i] = fluid.smass()
90+
dsde_dataset[i] = fluid.first_partial_deriv(CoolProp.iSmass, CoolProp.iUmass, CoolProp.iDmass)
91+
dsdrho_dataset[i] = fluid.first_partial_deriv(CoolProp.iSmass, CoolProp.iDmass, CoolProp.iUmass)
92+
d2sde2_dataset[i] = fluid.second_partial_deriv(CoolProp.iSmass, CoolProp.iUmass, CoolProp.iDmass, CoolProp.iUmass, CoolProp.iDmass)
93+
d2sdedrho_dataset[i] = fluid.second_partial_deriv(CoolProp.iSmass, CoolProp.iUmass, CoolProp.iDmass, CoolProp.iDmass, CoolProp.iUmass)
94+
d2sdrho2_dataset[i] = fluid.second_partial_deriv(CoolProp.iSmass, CoolProp.iDmass, CoolProp.iUmass, CoolProp.iDmass, CoolProp.iUmass)
95+
except:
96+
idx_failed_below.append(i)
97+
print("CoolProp failed at temperature "+str(T_dataset[i]) + ", pressure "+str(P_dataset[i]))
98+
print("Done!")
99+
100+
# Collect all data arrays and fill in failed data points.
101+
collected_data = np.vstack([density_dataset,
102+
energy_dataset,
103+
s_dataset,
104+
dsde_dataset,
105+
dsdrho_dataset,
106+
d2sde2_dataset,
107+
d2sdedrho_dataset,
108+
d2sdrho2_dataset]).T
109+
for i_failed in idx_failed_below:
110+
collected_data[i_failed, :] = 0.5*(collected_data[i_failed+1, :] + collected_data[i_failed-1, :])
111+
112+
# Shuffle data set and extract training, validation, and test data.
113+
np.random.shuffle(collected_data)
114+
np_train = int(f_train*len(density_dataset))
115+
np_val = int(f_test*len(density_dataset))
116+
np_test = len(density_dataset) - np_train - np_val
117+
118+
train_data = collected_data[:np_train, :]
119+
dev_data = collected_data[np_train:(np_train+np_val), :]
120+
test_data = collected_data[(np_train+np_val):, :]
121+
122+
# Write output files.
123+
with open(fluidName + "_dataset_full.csv", "w+") as fid:
124+
fid.write("Density,Energy,s,dsde_rho,dsdrho_e,d2sde2,d2sdedrho,d2sdrho2\n")
125+
csvWriter = csv.writer(fid,delimiter=',')
126+
csvWriter.writerows(collected_data)
127+
128+
with open(fluidName + "_dataset_train.csv", "w+") as fid:
129+
fid.write("Density,Energy,s,dsde_rho,dsdrho_e,d2sde2,d2sdedrho,d2sdrho2\n")
130+
csvWriter = csv.writer(fid,delimiter=',')
131+
csvWriter.writerows(train_data)
132+
133+
with open(fluidName + "_dataset_dev.csv", "w+") as fid:
134+
fid.write("Density,Energy,s,dsde_rho,dsdrho_e,d2sde2,d2sdedrho,d2sdrho2\n")
135+
csvWriter = csv.writer(fid,delimiter=',')
136+
csvWriter.writerows(dev_data)
137+
138+
with open(fluidName + "_dataset_test.csv", "w+") as fid:
139+
fid.write("Density,Energy,s,dsde_rho,dsdrho_e,d2sde2,d2sdedrho,d2sdrho2\n")
140+
csvWriter = csv.writer(fid,delimiter=',')
141+
csvWriter.writerows(test_data)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
%!/usr/bin/env matlab
2+
3+
%% \file LUTWriter.m
4+
% \brief Example MATLAB script for generating a look-up table file
5+
% compatible with the CDataDriven_Fluid class in SU2.
6+
% \author E.C.Bunschoten
7+
% \version 7.5.1 "Blackbird"
8+
%
9+
% SU2 Project Website: https://su2code.github.io
10+
%
11+
% The SU2 Project is maintained by the SU2 Foundation
12+
% (http://su2foundation.org)
13+
%
14+
% Copyright 2012-2023, SU2 Contributors (cf. AUTHORS.md)
15+
%
16+
% SU2 is free software; you can redistribute it and/or
17+
% modify it under the terms of the GNU Lesser General Public
18+
% License as published by the Free Software Foundation; either
19+
% version 2.1 of the License, or (at your option) any later version.
20+
%
21+
% SU2 is distributed in the hope that it will be useful,
22+
% but WITHOUT ANY WARRANTY; without even the implied warranty of
23+
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24+
% Lesser General Public License for more details.
25+
%
26+
% You should have received a copy of the GNU Lesser General Public
27+
% License along with SU2. If not, see <http://www.gnu.org/licenses/>.
28+
29+
% make print(*args) function available in PY2.6+, does'nt work on PY < 2.6
30+
31+
% CoolProp input data file.
32+
input_datafile = "Air_dataset_full.csv";
33+
34+
% Data point frequency (the larger, the coarser the table).
35+
data_freq = 2;
36+
37+
% LUT output file name.
38+
output_LUTfile = "reftable.drg";
39+
40+
%% provide import data file
41+
% space delimited
42+
data_ref = importdata(input_datafile);
43+
44+
% Identify data entries
45+
rho = data_ref.data(1:data_freq:end, 1);
46+
e = data_ref.data(1:data_freq:end, 2);
47+
s = data_ref.data(1:data_freq:end, 3);
48+
ds_de = data_ref.data(1:data_freq:end, 4);
49+
ds_drho = data_ref.data(1:data_freq:end, 5);
50+
d2s_de2 = data_ref.data(1:data_freq:end, 6);
51+
d2s_dedrho = data_ref.data(1:data_freq:end, 7);
52+
d2s_drho2 = data_ref.data(1:data_freq:end, 8);
53+
54+
rho_min = min(rho);
55+
rho_max = max(rho);
56+
e_min = min(e);
57+
e_max = max(e);
58+
59+
% Normalize density and energy
60+
rho_norm = (rho - rho_min)/(rho_max - rho_min);
61+
e_norm = (e - e_min)/(e_max - e_min);
62+
63+
%% Define table connectivity
64+
T = delaunayTriangulation(rho_norm, e_norm);
65+
66+
data_LUT = [rho, e, s, ds_de, ds_drho, d2s_de2, d2s_dedrho, d2s_drho2];
67+
68+
[~, boundNodes] = boundaryFacets(alphaShape(rho_norm, e_norm, 0.05));
69+
hullIDs = find(ismember([rho_norm, e_norm], boundNodes, "rows"));
70+
71+
%% Write table data to output
72+
fid = fopen(output_LUTfile, 'w+');
73+
74+
header = ['Dragon library' newline newline];
75+
76+
header = [header '<Header>' newline];
77+
78+
header = [header '[Version]' newline '1.0.1' newline newline];
79+
80+
header = [header '[Number of points]' newline];
81+
header = [header sprintf('%3d',length(rho)) newline newline];
82+
83+
header = [header '[Number of triangles]' newline];
84+
header = [header sprintf('%3d',length(T.ConnectivityList)) newline newline];
85+
86+
header = [header '[Number of hull points]' newline];
87+
header = [header sprintf('%3d',length(hullIDs)) newline newline];
88+
89+
header = [header '[Number of variables]' newline];
90+
header = [header sprintf('%3d',8) newline newline];
91+
92+
header = [header '[Variable names]' newline];
93+
header = [header sprintf('1:Density\n2:Energy\n3:s\n4:dsde_rho\n5:dsdrho_e\n6:d2sde2\n7:d2sdedrho\n8:d2sdrho2\n')];
94+
95+
header = [header newline '</Header>' newline newline];
96+
header = [header '<Data>'];
97+
98+
fprintf(fid,'%s', header);
99+
printformat = '\n';
100+
for iTabVar=1:8
101+
printformat = [printformat '%.14e\t'];
102+
end
103+
fprintf(fid,printformat,data_LUT');
104+
fprintf(fid,'%s', newline);
105+
fprintf(fid,'%s', '</Data>');
106+
107+
fprintf(fid,'%s', newline);
108+
fprintf(fid,'%s', newline);
109+
fprintf(fid,'%s', '<Connectivity>');
110+
111+
printformat = ['\n' '%5i\t' '%5i\t' '%5i\t'];
112+
113+
fprintf(fid,printformat,T.ConnectivityList');
114+
115+
fprintf(fid,'%s', newline);
116+
fprintf(fid,'%s', '</Connectivity>');
117+
118+
%% print hull block
119+
fprintf(fid,'%s', newline);
120+
fprintf(fid,'%s', newline);
121+
fprintf(fid,'%s', '<Hull>');
122+
123+
printformat = ['\n' '%5i\t'];
124+
125+
fprintf(fid,printformat,hullIDs);
126+
127+
fprintf(fid,'%s', newline);
128+
fprintf(fid,'%s', '</Hull>');
129+
130+
%% close .dat file
131+
fclose(fid);

0 commit comments

Comments
 (0)