Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 39 additions & 16 deletions NN_airofil.py → NN_airfoil.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,28 @@ def __init__(self, x_dim = 16, y_dim = 1, learning_rate = 0.001, N_hlayers = 1

def model(self):

self.X = tf.placeholder(dtype=tf.float32, shape=(None,self.x_dim), name='X')
self.X = tf.placeholder(dtype=tf.float32, shape=[None,280], name='X')
self.extra = tf.placeholder(dtype=tf.float32, shape=(None,2), name='extra')
self.y = tf.placeholder(dtype=tf.float32, shape=(None, self.y_dim), name='y')

x_1 = self.X[:,0:140]
x_2 = tf.reverse(self.X[:,140:], [1])
print(x_2.get_shape())
x_input = tf.concat([x_1,x_2], 1)

conv1 = tf.layers.conv2d(inputs=tf.reshape(x_input,[-1,2,140,1]), filters=4, kernel_size=[2,2],
padding='same', activation=self.activation_function)
print(conv1.get_shape())
pool1 = tf.layers.max_pooling2d(inputs=conv1, pool_size = [1,4], strides=[1,4])
print(pool1.get_shape())
conv2 = tf.layers.conv2d(inputs= pool1, filters=4, kernel_size=[2,2],
padding='same', activation=self.activation_function)
print(conv2.get_shape())
pool2 = tf.layers.max_pooling2d(inputs=conv2, pool_size = [2,7], strides=[2,7])
print(pool2.get_shape())
pool2_flat = tf.reshape(pool2,[-1,5*4] )
input_NN = tf.concat([pool2_flat, self.extra],1)
#input layer
layer_1 = tf.layers.dense(inputs=self.X, units=self.n_neur, activation = self.activation_function,
layer_1 = tf.layers.dense(inputs=input_NN, units=self.n_neur, activation = self.activation_function,
name = 'layer1')

d = {'layer_1':layer_1} #dictionary for the layer
Expand All @@ -54,7 +71,7 @@ def model(self):
self.optimizer = tf.train.AdamOptimizer(self.learning_rate).minimize(self.cost)


def train_NN(self, X_train, y_train, X_test, y_test, y_train_ok, y_test_ok, shuffle=False):
def train_NN(self, X_train, y_train, X_test, y_test, y_train_ok, y_test_ok, verbosity = True, tolerance=1E-6,shuffle=False):
"""
X_train: input X to train the NN
y_train: normalized y_train
Expand All @@ -72,40 +89,46 @@ def train_NN(self, X_train, y_train, X_test, y_test, y_train_ok, y_test_ok, shuf
self.y_test = y_test
self.y_train_ok = y_train_ok
self.y_test_ok = y_test_ok
self.shuffle = shuffle
self.shuffle = shuffle
self.tolerance = tolerance
self.training_cost = [] #list to save the cost
self.R_error_train = [] #list to save the relative error of the train set
self.R_error_test = [] #lsit to save the relative error of the test set
start = time.time()
#batch size used during the optimization
self.batch_size = int(self.X_train.shape[0]*0.05)
self.x_train_points = np.concatenate((self.X_train[:,0:140],self.X_train[:,141:281]),axis=1)
self.feat_train_points = self.X_train[:,281:]
self.x_test_points = np.concatenate((self.X_test[:,0:140],self.X_test[:,141:281]),axis=1)
self.feat_test_points = self.X_test[:,281:]
# The optimization:
for i in range(self.num_epochs):
batch_generator = self.generate_batches()
for batch_x, batch_y in batch_generator:
feed = {self.X: batch_x, self.y: batch_y}
_, cost = self.sess.run([self.optimizer, self.cost], feed_dict=feed)

for batch_x, batch_e, batch_y in batch_generator:
feed = {self.X: batch_x, self.extra: batch_e, self.y: batch_y}
_ = self.sess.run([self.optimizer], feed_dict=feed)
cost = self.sess.run([self.cost], feed_dict={self.X: self.x_train_points, self.extra: self.feat_train_points, self.y: self.y_train})
self.training_cost.append(cost)

#evaluate the NN using x_test
self.y_pred = self.sess.run(self.network, feed_dict={self.X: self.X_test})
self.y_pred = self.sess.run(self.network, feed_dict={self.X: self.x_test_points, self.extra: self.feat_test_points})
#denormalize the prediction
self.y_pred_ok = self.denormalize(self.y_pred)
#compute the relative error of the test data
error_test = np.sqrt(np.sum((self.y_test_ok-self.y_pred_ok)**2))/np.sqrt(np.sum(self.y_test_ok**2))*100
self.R_error_test.append(error_test)

#evaluate the NN using x_train
y_pred_train = self.sess.run(self.network, feed_dict={self.X: self.X_train})
y_pred_train = self.sess.run(self.network, feed_dict={self.X: self.x_train_points, self.extra: self.feat_train_points})
#denormalize the prediction
y_pred_train_ok = self.denormalize(y_pred_train)
#compute the relative error of the training data
error_train = np.sqrt(np.sum((self.y_train_ok-y_pred_train_ok)**2))/np.sqrt(np.sum(self.y_train_ok**2))*100
self.R_error_train.append(error_train)

print('Epoch: ', i+1, 'Training cost: ', self.training_cost[-1])

if verbosity:
print('Epoch: ', i+1, 'Training cost: ', self.training_cost[-1])
if np.asarray(self.training_cost[-1])<self.tolerance:
break
end = time.time()
self.total_time = end-start
print('====== NN trained, ',self.total_time/60, '[min] ======')
Expand All @@ -122,7 +145,7 @@ def generate_batches(self):
X_copy = data[:,:-1]
y_copy = data[:,-1].reshape((-1,1))
for i in range(0, self.X_train.shape[0], self.batch_size):
yield (X_copy[i:i+self.batch_size,:], y_copy[i:i+self.batch_size])
yield (np.concatenate((X_copy[i:i+self.batch_size,:140],X_copy[i:i+self.batch_size,141:281]),axis=1),X_copy[i:i+self.batch_size,281:], y_copy[i:i+self.batch_size])

def generate_plot(self, name, show=False, save=False):
"""
Expand All @@ -131,7 +154,7 @@ def generate_plot(self, name, show=False, save=False):
show: show plots if True
save: save NN, plots and a .txt file if True, using the 'name'
"""
self.test_cost = self.sess.run(self.cost, feed_dict= {self.X:self.X_test, self.y: self.y_test})
self.test_cost = self.sess.run(self.cost, feed_dict= {self.X: self.x_test_points, self.extra: self.feat_test_points, self.y: self.y_test})

fig1 = plt.figure(figsize=(9,4))
plt.subplot(1,2,1)
Expand Down
77 changes: 77 additions & 0 deletions Train_NN.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#%%
import numpy as np
from NN_airfoil import NeuralAirfoil
from sklearn.preprocessing import MinMaxScaler

def read_newdata():
np.random.seed(0)
xy0 = np.loadtxt('new_data1.txt')
xy1 = np.loadtxt('new_data2.txt')
xy2 = np.loadtxt('new_data3.txt')
xy = np.concatenate((np.concatenate((xy0, xy1)), xy2))
np.random.shuffle(xy)
dim = 281+2 #281 points + Mach number + angle of attack
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 = scaler.fit_transform(x[:N_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_newdata() #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 = .001
n_neur = 60
n_layers = 2
x_dim = 281 #y points
epc = 10
save = False
show = False

#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)
#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)
# 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])


# 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)
# 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])
Binary file added __pycache__/NN_airfoil.cpython-37.pyc
Binary file not shown.
Binary file modified __pycache__/NN_airofil.cpython-37.pyc
Binary file not shown.
10 changes: 5 additions & 5 deletions data/Cd

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions data/Cl

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions data/Cm

Large diffs are not rendered by default.

Binary file modified data/NN_Cd.ckpt.data-00000-of-00001
Binary file not shown.
Binary file modified data/NN_Cd.ckpt.index
Binary file not shown.
Binary file modified data/NN_Cd.ckpt.meta
Binary file not shown.
Binary file modified data/NN_Cl.ckpt.data-00000-of-00001
Binary file not shown.
Binary file modified data/NN_Cl.ckpt.index
Binary file not shown.
Binary file modified data/NN_Cl.ckpt.meta
Binary file not shown.
Binary file modified data/NN_Cm.ckpt.data-00000-of-00001
Binary file not shown.
Binary file modified data/NN_Cm.ckpt.index
Binary file not shown.
Binary file modified data/NN_Cm.ckpt.meta
Binary file not shown.
4 changes: 2 additions & 2 deletions data/checkpoint
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
model_checkpoint_path: "NN_Cl.ckpt"
all_model_checkpoint_paths: "NN_Cl.ckpt"
model_checkpoint_path: "NN_Cd.ckpt"
all_model_checkpoint_paths: "NN_Cd.ckpt"
Binary file modified data/compareCd.pdf
Binary file not shown.
Binary file modified data/compareCl.pdf
Binary file not shown.
Binary file modified data/compareCm.pdf
Binary file not shown.
Binary file modified data/errorCd.pdf
Binary file not shown.
Binary file modified data/errorCl.pdf
Binary file not shown.
Binary file modified data/errorCm.pdf
Binary file not shown.
Binary file modified data/relativeerrorCd.pdf
Binary file not shown.
Binary file modified data/relativeerrorCl.pdf
Binary file not shown.
Binary file modified data/relativeerrorCm.pdf
Binary file not shown.
Binary file modified data/training_costCd.pdf
Binary file not shown.
Binary file modified data/training_costCl.pdf
Binary file not shown.
Binary file modified data/training_costCm.pdf
Binary file not shown.
103 changes: 0 additions & 103 deletions main_code.py

This file was deleted.

16 changes: 14 additions & 2 deletions modes_to_points.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
xy1 = np.loadtxt('M1CFDdata.txt')
xy2 = np.loadtxt('M2CFDdata.txt')
xy = np.concatenate((np.concatenate((xy0, xy1)), xy2))

n1 = xy0.shape[0]
n2 = xy1.shape[0]
basedata = np.loadtxt('basis.txt')
x = basedata[0,:].copy()
U = basedata[1:,:].copy()
Expand All @@ -17,5 +18,16 @@
for i in range(N_data):
new_data[i,0:N_points] = np.dot(xy[i,0:14],U)
new_data[:,N_points:N_points+5] = xy[:,14:19]
np.savetxt('new_data.txt',new_data)
np.savetxt('new_data1.txt',new_data[:n1])
np.savetxt('new_data2.txt',new_data[n1:n1+n2])
np.savetxt('new_data3.txt',new_data[n1+n2:])
np.savetxt('x_geom.txt',x)

#%%
a = np.array([1,2,3,4,5,6])
c = np.array([[1,2,3],[4,5,6]])
c1 = np.array([[7,8,9],[10,11,12]])
b = np.concatenate((c,c1),axis=1)
print(b)


Loading