-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTRAIN_NN.py
90 lines (78 loc) · 3.12 KB
/
TRAIN_NN.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#%%
import numpy as np
from NN_airfoil import NeuralAirfoil
from sklearn.preprocessing import MinMaxScaler
def read_data():
"""
xy0, xy1, xy2 represent files corresponding to three outputs (CL, CM and CD)
xy0:
x: (x_geo (7 thickness, 7 cmaber) Ma, alpha),
y: (CL), CD, CM,
pCL/px, pCM/px, pCD/px (??? not sure about the order)
"""
np.random.seed(0)
xy0 = np.loadtxt('M0CFDdata.txt')
xy1 = np.loadtxt('M1CFDdata.txt')
xy2 = np.loadtxt('M2CFDdata.txt')
xy = np.concatenate((np.concatenate((xy0, xy1)), xy2))
#shuffle the data, because it has some pattern
np.random.shuffle(xy)
dim = 16
x = xy[:, :dim]
y = xy[:, dim:dim+3]
N_train = np.int(len(y[:,0])*0.8) #Number of train data
y_train_ok = y[:N_train,:]
y_test_ok = y[N_train:,:]
scaler = MinMaxScaler()
x_train = x[:N_train,:]
np.savetxt('x_train.txt',x_train)
np.savetxt('y_train.txt', y_train_ok)
x_train = scaler.fit_transform(x_train) #normalize the X_train
x_test = scaler.transform(x[N_train:,:]) #normalize the X_test
y_train_n = scaler.fit_transform(y[:N_train,:]) #normalized data
y_test_n = scaler.transform(y[N_train:,:]) #normalized data
return x_train, y_train_n, x_test, y_test_n, y_train_ok, y_test_ok
X_train, y_train, X_test, y_test, y_train_ok, y_test_ok = read_data() #Read de data
Cl_train = np.reshape(y_train[:,0],[-1,1])
Cl_test = np.reshape(y_test[:,0],[-1,1])
Cl_ok = np.reshape(y_train_ok[:,0],[-1,1])
Cl_test_ok = np.reshape(y_test_ok[:,0],[-1,1])
Cm_train = np.reshape(y_train[:,2],[-1,1])
Cm_test = np.reshape(y_test[:,2],[-1,1])
Cm_ok = np.reshape(y_train_ok[:,2],[-1,1])
Cm_test_ok = np.reshape(y_test_ok[:,2],[-1,1])
Cd_train = np.reshape(y_train[:,1],[-1,1])
Cd_test = np.reshape(y_test[:,1],[-1,1])
Cd_ok = np.reshape(y_train_ok[:,1],[-1,1])
Cd_test_ok = np.reshape(y_test_ok[:,1],[-1,1])
#%%
alpha = 5.9e-4
n_neur = 158
n_layers = 4
x_dim = 16#281+2
epc = 2000
save = True
show = False
tol=2E-6
#%%
#create NN for Cd
model_Cd = NeuralAirfoil(x_dim = x_dim, N_hlayers=n_layers, n_neur=n_neur, learning_rate=alpha, num_epochs=epc)
#Train the NN for Cd
model_Cd.train_NN(X_train, Cd_train, X_test, Cd_test, Cd_ok, Cd_test_ok,tolerance=tol)
#generate the plots for Cd
model_Cd.generate_plot('Cd', show=show, save=save)
print('Relative error test %',model_Cd.R_error_test[-1])
print('Relative error train %',model_Cd.R_error_train[-1])
#%%
model_Cm = NeuralAirfoil(x_dim = x_dim, N_hlayers=n_layers, n_neur=n_neur, learning_rate=alpha, num_epochs=epc)
model_Cm.train_NN(X_train, Cm_train, X_test, Cm_test, Cm_ok, Cm_test_ok,tolerance=tol)
model_Cm.generate_plot('Cm', show=show, save=save)
print('Relative error test %',model_Cm.R_error_test[-1])
print('Relative error train %',model_Cm.R_error_train[-1])
#%%
alpha = .01
model_Cl = NeuralAirfoil(x_dim = x_dim, N_hlayers=n_layers, n_neur=n_neur, learning_rate=alpha, num_epochs=epc)
model_Cl.train_NN(X_train, Cl_train, X_test, Cl_test, Cl_ok, Cl_test_ok,tolerance=2*tol)
model_Cl.generate_plot('Cl', show=show, save=save)
print('Relative error test %',model_Cl.R_error_test[-1])
print('Relative error train %',model_Cl.R_error_train[-1])