def VisuError(Error, testcases, Savetofile=False): name = testcases.name Vals = testcases.Vals symbols = ('^','o', 's','D','p','H',) lines = ('-', '--',':', '-.','.',) newfig(width=1.3) for i in range(len(Vals)): namestr = ', ' + '$N_s$=' + str(Vals[i]) if name != 'SampleNum' and i ==0: plt.semilogy(M_Vec,Error[i,:,4], 'k-.',label='Projection' ) plt.semilogy(M_Vec,Error[i,:,0], 'r-.',label='POD-G ' ) elif name == 'SampleNum': plt.semilogy(M_Vec,Error[i,:,4], 'k-.'+symbols[i] ,label='Projection'+namestr ) plt.semilogy(M_Vec,Error[i,:,0], 'r-.'+symbols[i] ,label='POD-G' +namestr ) if not( name == 'NResi' ): plt.semilogy(M_Vec,Error[i,:,1], 'y:'+symbols[i] ,label='PDNN' +namestr ) plt.semilogy(M_Vec,Error[i,:,2], 'g--'+symbols[i] ,label='PINN'+namestr ) plt.semilogy(M_Vec,Error[i,:,3], 'g-'+symbols[i] ,label='PRNN'+namestr ) plt.xlabel('$m$') plt.ylabel('Error') #plt.title(name) plt.legend(loc="lower left", ncol=1, handlelength=3) plt.show() if Savetofile: savefig("fig/ErrorComparsion_"+name)
def VisuError(Error, testcases, Savetofile=False): name = testcases.name Vals = testcases.Vals symbols = ('^','o', 's','D','p','H',) lines = ('-', '--',':', '-.','.',) #plt.close('all')i if name == 'SampleNum': tmp = '$N_s$' width = 1.3 elif name =='NetSize': tmp = '$n_H$' width = 1 elif name == 'NResi': tmp = '$N_{Resi}$' width = 1 newfig(width=width) for i in range(len(Vals)): namestr = ', ' + tmp+'=' + str(Vals[i]) if name != 'SampleNum' and i ==0: plt.semilogy(M_Vec,Error[i,:,4], 'k-.',label='Projection' ) plt.semilogy(M_Vec,Error[i,:,0], 'r-.',label='POD-G ' ) elif name == 'SampleNum': plt.semilogy(M_Vec,Error[i,:,4], 'k-.'+symbols[i] ,label='Projection'+namestr ) plt.semilogy(M_Vec,Error[i,:,0], 'r-.'+symbols[i] ,label='POD-G' +namestr ) if not( name == 'NResi' ): plt.semilogy(M_Vec,Error[i,:,1], 'y:'+symbols[i] ,label='PDNN' +namestr ) plt.semilogy(M_Vec,Error[i,:,2], 'g--'+symbols[i] ,label='PINN'+namestr ) plt.semilogy(M_Vec,Error[i,:,3], 'g-'+symbols[i] ,label='PRNN'+namestr ) if name == 'NResi': plt.ylim(bottom=9E-5) elif name == 'NetSize': plt.ylim(bottom=7E-5) plt.xlabel('$m$') plt.ylabel(r'Error $\varepsilon$') #plt.title(name) plt.legend(loc="lower left", ncol=1, handlelength=3) if name == 'NResi': plt.legend(loc="best", ncol=1, handlelength=3) plt.show() if Savetofile: savefig("fig/ErrorComparsion_"+name)
np.random.choice(np.arange(1, N - 1), size=N_eqns - 2, replace=False), np.array([N - 1]) ]) model = HiddenPathways(t_train[idx_data], S_train[idx_data, :], t_train[idx_eqns], layers) model.train(num_epochs=20000, batch_size=N_eqns, learning_rate=1e-3) model.train(num_epochs=40000, batch_size=N_eqns, learning_rate=1e-4) model.train(num_epochs=20000, batch_size=N_eqns, learning_rate=1e-5) S_pred, S_pred_std = model.predict(t_star[:, None]) ####### Plotting ################## fig, ax = newfig(3.0, 0.3) gs0 = gridspec.GridSpec(1, 2) gs0.update(top=0.95, bottom=0.1, left=0.1, right=0.95, hspace=0.5, wspace=0.3) ax = plt.subplot(gs0[0:1, 0:1]) ax.plot(t_star, S_star[:, 4], 'C1', linewidth=2, label='input data') ax.scatter(t_star[idx_data], S_star[idx_data, 4], marker='o', s=50, label='sampled input') ax.set_xlabel('$t\ (min)$', fontsize=18)
M = 1 scheme = 'AM' model = Multistep_NN(dt, X_train, layers, M, scheme) N_Iter = 50000 model.train(N_Iter) def learned_f(x, t): f = model.predict_f(x[None, :]) return f.flatten() learned_X_star = odeint(learned_f, x0, t_star) ####### Plotting ################## fig, ax = newfig(1.0, 0.8) ax.axis('off') gs0 = gridspec.GridSpec(1, 2) gs0.update(top=0.95, bottom=0.1, left=0.0, right=0.90, wspace=0.15) ax = plt.subplot(gs0[:, 0:1], projection='3d') ax.w_xaxis.set_pane_color((1.0, 1.0, 1.0, 1.0)) ax.w_yaxis.set_pane_color((1.0, 1.0, 1.0, 1.0)) ax.w_zaxis.set_pane_color((1.0, 1.0, 1.0, 1.0)) colorline3d(ax, X_star[:, 0], X_star[:, 1], X_star[:, 2], cmap=plt.cm.ocean) ax.grid(False)
def plot_pressure(p_pred, p_star, X_star, epoch, learning_rate): # Predict for plotting lb = X_star.min(0) ub = X_star.max(0) nn = 200 x = np.linspace(lb[0], ub[0], nn) y = np.linspace(lb[1], ub[1], nn) X, Y = np.meshgrid(x, y) PP_star = griddata(X_star, np.ndarray.flatten(p_pred.numpy()), (X, Y), method='cubic') P_exact = griddata(X_star, np.ndarray.flatten(p_star), (X, Y), method='cubic') x_star = X_star[:, 0:1] y_star = X_star[:, 1:2] fig, ax = newfig(1.015, 0.8) ax.axis('off') ######## Predicted p(t,x,y) ########### gs2 = gridspec.GridSpec(1, 2) gs2.update(top=1, bottom=1 - 1 / 2, left=0.1, right=0.9, wspace=0.5) ax = plt.subplot(gs2[:, 0]) h = ax.imshow( PP_star, interpolation='nearest', cmap='rainbow', extent=[x_star.min(), x_star.max(), y_star.min(), y_star.max()], origin='lower', aspect='auto') divider = make_axes_locatable(ax) cax = divider.append_axes("right", size="5%", pad=0.05) fig.colorbar(h, cax=cax) ax.set_xlabel('$x$') ax.set_ylabel('$y$') ax.set_aspect('equal', 'box') ax.set_title('Predicted pressure', fontsize=10) ######## Exact p(t,x,y) ########### ax = plt.subplot(gs2[:, 1]) h = ax.imshow( P_exact, interpolation='nearest', cmap='rainbow', extent=[x_star.min(), x_star.max(), y_star.min(), y_star.max()], origin='lower', aspect='auto') divider = make_axes_locatable(ax) cax = divider.append_axes("right", size="5%", pad=0.05) fig.colorbar(h, cax=cax) ax.set_xlabel('$x$') ax.set_ylabel('$y$') ax.set_aspect('equal', 'box') ax.set_title('Exact pressure', fontsize=10) savefig('./figures/NavierStokes_prediction ' + str(learning_rate) + ' ' + str(epoch)) plt.close() pass
def plot_inf_disc_results(x_star, idx_t_0, idx_t_1, x_0, u_0, ub, lb, u_1_pred, Exact_u, x, t, save_path=None, save_hp=None): fig, ax = newfig(1.0, 1.2) ax.axis('off') ####### Row 0: h(t,x) ################## gs0 = gridspec.GridSpec(1, 2) gs0.update(top=1 - 0.06, bottom=1 - 1 / 2 + 0.1, left=0.15, right=0.85, wspace=0) ax = plt.subplot(gs0[:, :]) h = ax.imshow(Exact_u.T, interpolation='nearest', cmap='rainbow', extent=[t.min(), t.max(), x_star.min(), x_star.max()], origin='lower', aspect='auto') divider = make_axes_locatable(ax) cax = divider.append_axes("right", size="5%", pad=0.05) fig.colorbar(h, cax=cax) line = np.linspace(x.min(), x.max(), 2)[:, None] ax.plot(t[idx_t_0] * np.ones((2, 1)), line, 'w-', linewidth=1) ax.plot(t[idx_t_1] * np.ones((2, 1)), line, 'w-', linewidth=1) ax.set_xlabel('$t$') ax.set_ylabel('$x$') leg = ax.legend(frameon=False, loc='best') ax.set_title('$u(t,x)$', fontsize=10) ####### Row 1: h(t,x) slices ################## gs1 = gridspec.GridSpec(1, 2) gs1.update(top=1 - 1 / 2 - 0.05, bottom=0.15, left=0.15, right=0.85, wspace=0.5) ax = plt.subplot(gs1[0, 0]) ax.plot(x, Exact_u[idx_t_0, :], 'b-', linewidth=2) ax.plot(x_0, u_0, 'rx', linewidth=2, label='Data') ax.set_xlabel('$x$') ax.set_ylabel('$u(t,x)$') ax.set_title('$t = %.2f$' % (t[idx_t_0]), fontsize=10) ax.set_xlim([lb - 0.1, ub + 0.1]) ax.legend(loc='upper center', bbox_to_anchor=(0.8, -0.3), ncol=2, frameon=False) ax = plt.subplot(gs1[0, 1]) ax.plot(x, Exact_u[idx_t_1, :], 'b-', linewidth=2, label='Exact') ax.plot(x_star, u_1_pred, 'r--', linewidth=2, label='Prediction') ax.set_xlabel('$x$') ax.set_ylabel('$u(t,x)$') ax.set_title('$t = %.2f$' % (t[idx_t_1]), fontsize=10) ax.set_xlim([lb - 0.1, ub + 0.1]) ax.legend(loc='upper center', bbox_to_anchor=(0.1, -0.3), ncol=2, frameon=False) if save_path != None and save_hp != None: saveResultDir(save_path, save_hp) else: plt.show()
return self.loss_Eqs(x, self.u_net(x), source, weight) def loss_Eqs(self, x, lamda, source, weight=1): # def loss_PINN(self,x,source): #lamda = self.u_net(x); fx = torch.matmul(lamda[:, None, None, :], self.A[None, :, :, :]) fx = torch.matmul(fx, lamda[:, None, :, None]) fx = fx.view(lamda.shape) fx = fx + torch.matmul(lamda, self.B.T) - source return self.lossfun(weight * fx, torch.zeros_like(fx)) if __name__ == '__main__': NumSolsdir = 'NumSols' Nsample = 80 matfile = NumSolsdir + '/' + 'Burges1D_SampleNum=' + str(Nsample) + '.mat' M = 2 roeqs = CustomedEqs(matfile, M) # Net = CustomedNet(roeqs=roeqs,layers=[2,20,20,20,M]) # print(Net.labeledLoss) from plotting import newfig, savefig import matplotlib.pyplot as plt newfig(width=0.8) plt.semilogy(np.arange(roeqs.sigma.shape[0]) + 1, roeqs.sigma, '-ko') plt.xlabel('$m$') plt.ylabel('Singular value') plt.show() savefig('fig/SingularValues_%d' % (Nsample))
lines = ( '-', '--', ':', '-.', '.', ) colors = ( 'r', 'g', 'b', 'g', 'y', ) plt.close('all') newfig(width=1) for k in range(2): for i in range(alpha.shape[0]): alphai = alpha[i:i + 1, :] lamdai = Net(torch.tensor(alphai).float().to(DEVICE)) phi_proj = np.matmul(lamdai, roeqs.Modes.T) phi_Exact = roeqs.phix(roeqs.xgrid.T, alphai[:, 0:1], alphai[:, 1:2]) #plot name = '$\\boldsymbol{\\mu}=(%0.1f,%0.1f)$' % (alphai[0, 0], alphai[0, 1]) if k == 0: plt.plot(roeqs.xgrid, phi_Exact.T, colors[i] + lines[0], label='PS',
data_vort = scipy.io.loadmat('../Data/cylinder_nektar_t0_vorticity.mat') x_vort = data_vort['x'] y_vort = data_vort['y'] w_vort = data_vort['w'] modes = np.asscalar(data_vort['modes']) nel = np.asscalar(data_vort['nel']) xx_vort = np.reshape(x_vort, (modes + 1, modes + 1, nel), order='F') yy_vort = np.reshape(y_vort, (modes + 1, modes + 1, nel), order='F') ww_vort = np.reshape(w_vort, (modes + 1, modes + 1, nel), order='F') box_lb = np.array([1.0, -2.0]) box_ub = np.array([8.0, 2.0]) fig, ax = newfig(1.0, 1.2) ax.axis('off') ####### Row 0: Vorticity ################## gs0 = gridspec.GridSpec(1, 2) gs0.update(top=1 - 0.06, bottom=1 - 2 / 4 + 0.12, left=0.0, right=1.0, wspace=0) ax = plt.subplot(gs0[:, :]) for i in range(0, nel): h = ax.pcolormesh(xx_vort[:, :, i], yy_vort[:, :, i], ww_vort[:, :, i],
def plot_inf_cont_results(X_star, u_pred, v_pred, h_pred, Exact_h, X, T, x, t, ub, lb, x0, tb, save_path=None, save_hp=None): # Interpolating the results on the whole (x,t) domain. # griddata(points, values, points at which to interpolate, method) U_pred = griddata(X_star, u_pred.flatten(), (X, T), method='cubic') V_pred = griddata(X_star, v_pred.flatten(), (X, T), method='cubic') H_pred = griddata(X_star, h_pred.flatten(), (X, T), method='cubic') # FU_pred = griddata(X_star, f_u_pred.flatten(), (X, T), method='cubic') # FV_pred = griddata(X_star, f_v_pred.flatten(), (X, T), method='cubic') X0 = np.concatenate((x0, 0*x0), 1) # (x0, 0) X_lb = np.concatenate((0*tb + lb[0], tb), 1) # (lb[0], tb) X_ub = np.concatenate((0*tb + ub[0], tb), 1) # (ub[0], tb) X_u_train = np.vstack([X0, X_lb, X_ub]) fig, ax = newfig(1.0, 0.9) ax.axis('off') ####### Row 0: h(t,x) ################## gs0 = gridspec.GridSpec(1, 2) gs0.update(top=1-0.06, bottom=1-1/3, left=0.15, right=0.85, wspace=0) ax = plt.subplot(gs0[:, :]) h = ax.imshow(H_pred.T, interpolation='nearest', cmap='YlGnBu', extent=[lb[1], ub[1], lb[0], ub[0]], origin='lower', aspect='auto') divider = make_axes_locatable(ax) cax = divider.append_axes("right", size="5%", pad=0.05) fig.colorbar(h, cax=cax) ax.plot(X_u_train[:,1], X_u_train[:,0], 'kx', label = 'Data (%d points)' % (X_u_train.shape[0]), markersize = 4, clip_on = False) line = np.linspace(x.min(), x.max(), 2)[:,None] ax.plot(t[75]*np.ones((2,1)), line, 'k--', linewidth = 1) ax.plot(t[100]*np.ones((2,1)), line, 'k--', linewidth = 1) ax.plot(t[125]*np.ones((2,1)), line, 'k--', linewidth = 1) ax.set_xlabel('$t$') ax.set_ylabel('$x$') leg = ax.legend(frameon=False, loc = 'best') # plt.setp(leg.get_texts(), color='w') ax.set_title('$|h(t,x)|$', fontsize = 10) ####### Row 1: h(t,x) slices ################## gs1 = gridspec.GridSpec(1, 3) gs1.update(top=1-1/3, bottom=0, left=0.1, right=0.9, wspace=0.5) ax = plt.subplot(gs1[0, 0]) ax.plot(x,Exact_h[:,75], 'b-', linewidth = 2, label = 'Exact') ax.plot(x,H_pred[75,:], 'r--', linewidth = 2, label = 'Prediction') ax.set_xlabel('$x$') ax.set_ylabel('$|h(t,x)|$') ax.set_title('$t = %.2f$' % (t[75]), fontsize = 10) ax.axis('square') ax.set_xlim([-5.1,5.1]) ax.set_ylim([-0.1,5.1]) ax = plt.subplot(gs1[0, 1]) ax.plot(x,Exact_h[:,100], 'b-', linewidth = 2, label = 'Exact') ax.plot(x,H_pred[100,:], 'r--', linewidth = 2, label = 'Prediction') ax.set_xlabel('$x$') ax.set_ylabel('$|h(t,x)|$') ax.axis('square') ax.set_xlim([-5.1,5.1]) ax.set_ylim([-0.1,5.1]) ax.set_title('$t = %.2f$' % (t[100]), fontsize = 10) ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.8), ncol=5, frameon=False) ax = plt.subplot(gs1[0, 2]) ax.plot(x,Exact_h[:,125], 'b-', linewidth = 2, label = 'Exact') ax.plot(x,H_pred[125,:], 'r--', linewidth = 2, label = 'Prediction') ax.set_xlabel('$x$') ax.set_ylabel('$|h(t,x)|$') ax.axis('square') ax.set_xlim([-5.1,5.1]) ax.set_ylim([-0.1,5.1]) ax.set_title('$t = %.2f$' % (t[125]), fontsize = 10) if save_path != None and save_hp != None: saveResultDir(save_path, save_hp) else: plt.show()
def plot_inf_cont_results(X_star, U_pred, Sigma_pred, X_u_train, u_train, Exact_u, X, T, x, t, save_path=None, save_hp=None): # Interpolating the results on the whole (x,t) domain. # griddata(points, values, points at which to interpolate, method) # U_pred = griddata(X_star, u_pred, (X, T), method='cubic') # Creating the figures fig, ax = newfig(1.0, 1.1) ax.axis('off') X_u = X_u_train[:, 0:1] T_u = X_u_train[:, 1:2] Y_u = u_train Exact = Exact_u ####### Row 0: u(t,x) ################## gs0 = gridspec.GridSpec(1, 2) gs0.update(top=1 - 0.06, bottom=1 - 1 / 3, left=0.15, right=0.85, wspace=0) ax = plt.subplot(gs0[:, :]) h = ax.imshow(U_pred.T, interpolation='nearest', cmap='rainbow', extent=[t.min(), t.max(), x.min(), x.max()], origin='lower', aspect='auto') divider = make_axes_locatable(ax) cax = divider.append_axes("right", size="5%", pad=0.05) fig.colorbar(h, cax=cax) ax.plot(T_u, X_u, 'kx', label='Data (%d points)' % (Y_u.shape[0]), markersize=4, clip_on=False) line = np.linspace(x.min(), x.max(), 2)[:, None] ax.plot(t[25] * np.ones((2, 1)), line, 'w-', linewidth=1) ax.plot(t[50] * np.ones((2, 1)), line, 'w-', linewidth=1) ax.plot(t[75] * np.ones((2, 1)), line, 'w-', linewidth=1) ax.set_xlabel('$t$') ax.set_ylabel('$x$') ax.legend(frameon=False, loc='best') ax.set_title('$u(t,x)$', fontsize=10) ####### Row 1: u(t,x) slices ################## gs1 = gridspec.GridSpec(1, 3) gs1.update(top=1 - 1 / 3, bottom=0, left=0.1, right=0.9, wspace=0.5) ax = plt.subplot(gs1[0, 0]) ax.plot(x, Exact[25, :], 'b-', linewidth=2, label='Exact') ax.plot(x, U_pred[25, :], 'r--', linewidth=2, label='Prediction') lower = U_pred[25, :] - 2.0 * np.sqrt(Sigma_pred[25, :]) upper = U_pred[25, :] + 2.0 * np.sqrt(Sigma_pred[25, :]) plt.fill_between(x.flatten(), lower.flatten(), upper.flatten(), facecolor='orange', alpha=0.5, label="Two std band") ax.set_xlabel('$x$') ax.set_ylabel('$u(t,x)$') ax.set_title('$t = 0.25$', fontsize=10) ax.axis('square') ax.set_xlim([-1.1, 1.1]) ax.set_ylim([-1.1, 1.1]) ax = plt.subplot(gs1[0, 1]) ax.plot(x, Exact[50, :], 'b-', linewidth=2, label='Exact') ax.plot(x, U_pred[50, :], 'r--', linewidth=2, label='Prediction') lower = U_pred[50, :] - 2.0 * np.sqrt(Sigma_pred[50, :]) upper = U_pred[50, :] + 2.0 * np.sqrt(Sigma_pred[50, :]) plt.fill_between(x.flatten(), lower.flatten(), upper.flatten(), facecolor='orange', alpha=0.5, label="Two std band") ax.set_xlabel('$x$') ax.set_ylabel('$u(t,x)$') ax.axis('square') ax.set_xlim([-1.1, 1.1]) ax.set_ylim([-1.1, 1.1]) ax.set_title('$t = 0.50$', fontsize=10) ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.35), ncol=5, frameon=False) ax = plt.subplot(gs1[0, 2]) ax.plot(x, Exact[75, :], 'b-', linewidth=2, label='Exact') ax.plot(x, U_pred[75, :], 'r--', linewidth=2, label='Prediction') lower = U_pred[75, :] - 2.0 * np.sqrt(Sigma_pred[75, :]) upper = U_pred[75, :] + 2.0 * np.sqrt(Sigma_pred[75, :]) plt.fill_between(x.flatten(), lower.flatten(), upper.flatten(), facecolor='orange', alpha=0.5, label="Two std band") ax.set_xlabel('$x$') ax.set_ylabel('$u(t,x)$') ax.axis('square') ax.set_xlim([-1.1, 1.1]) ax.set_ylim([-1.1, 1.1]) ax.set_title('$t = 0.75$', fontsize=10) # savefig('./Prediction') # fig, ax = newfig(1.0) # ax.axis('off') # ############# Uncertainty ################## # gs2 = gridspec.GridSpec(1, 2) # gs2.update(top=1-0.06, bottom=1-1/3, left=0.15, right=0.85, wspace=0) # ax = plt.subplot(gs2[:, :]) # h = ax.imshow(Sigma_pred.T, interpolation='nearest', cmap='rainbow', # extent=[t.min(), t.max(), x.min(), x.max()], # origin='lower', aspect='auto') # divider = make_axes_locatable(ax) # cax = divider.append_axes("right", size="5%", pad=0.05) # fig.colorbar(h, cax=cax) # ax.set_xlabel('$t$') # ax.set_ylabel('$x$') # ax.legend(frameon=False, loc = 'best') # ax.set_title('Variance of $u(t,x)$', fontsize = 10) if save_path != None and save_hp != None: saveResultDir(save_path, save_hp) else: plt.show()
def main(): ## parameters device = torch.device(f"cpu") epochs = 5000 lr = 0.001 num_x = 100 num_y = 100 num_t = 10 num_b_train = 10 # boundary sampling points num_f_train = 100 # inner sampling points num_i_train = 100 # initial sampling points x = np.linspace(0, 1, num=num_x) y = np.linspace(0, 1, num=num_y) t = np.linspace(0, 1, num=num_t) x_grid, y_grid = np.meshgrid(x, y) # x_test = np.concatenate((t_grid.flatten()[:,None], x_grid.flatten()[:,None], y_grid.flatten()[:,None]), axis=1) x_2d = np.concatenate( (x_grid.flatten()[:, None], y_grid.flatten()[:, None]), axis=1) xt_init = np.concatenate((np.zeros((num_x * num_y, 1)), x_2d), axis=1) u_init = AC_2D_init(xt_init)[:, None] x_2d_ext = np.tile(x_2d, [num_t, 1]) t_ext = np.tile(t[:, None], [num_x * num_y, 1]) xt_2d_ext = np.concatenate((t_ext, x_2d_ext), axis=1) ## find a smart way to take boundary point x_up = np.vstack((x_grid[-1, :], y_grid[-1, :])).T x_dw = np.vstack((x_grid[0, :], y_grid[0, :])).T x_l = np.vstack((x_grid[:, 0], y_grid[:, 0])).T x_r = np.vstack((x_grid[:, -1], y_grid[:, -1])).T x_bound = np.vstack((x_up, x_dw, x_l, x_r)) x_bound_ext = np.tile(x_bound, [num_t, 1]) t_bound_ext = np.tile(t[:, None], [num_x * 4, 1]) xt_bound_ext = np.concatenate((t_bound_ext, x_bound_ext), axis=1) u_bound_ext = -1 * np.ones((num_x * 4 * num_t))[:, None] ## sampling id_f = np.random.choice(num_x * num_y * num_t, num_f_train) id_b = np.random.choice(num_x * 4, num_b_train) ## Dirichlet id_i = np.random.choice(num_x * num_y, num_i_train) x_i = xt_init[id_i, :] u_i = u_init[id_i, :] x_f = xt_2d_ext[id_f, :] x_b = xt_bound_ext[id_b, :] u_b = u_bound_ext[id_b, :] ## set data as tensor and send to device x_f_train = torch.tensor(x_f, requires_grad=True, dtype=torch.float32).to(device) x_b_train = torch.tensor(x_b, requires_grad=True, dtype=torch.float32).to(device) x_test = torch.tensor(xt_2d_ext, requires_grad=True, dtype=torch.float32).to(device) x_i_train = torch.tensor(x_i, dtype=torch.float32).to(device) u_i_train = torch.tensor(u_i, dtype=torch.float32).to(device) u_b_train = torch.tensor(u_b, dtype=torch.float32).to(device) ## instantiate model model = Model().to(device) # Loss and optimizer optimizer = torch.optim.Adam(model.parameters(), lr=lr) # training def train(epoch): model.train() def closure(): optimizer.zero_grad() loss_pde = model.loss_pde(x_f_train) loss_bc = model.loss_bc(x_b_train, u_b_train) loss_ic = model.loss_ic(x_i_train, u_i_train) loss = loss_pde + loss_bc + loss_ic loss.backward() return loss loss = optimizer.step(closure) loss_value = loss.item() if not isinstance(loss, float) else loss print(f'epoch {epoch}: loss {loss_value:.6f}') print('start training...') tic = time.time() for epoch in range(1, epochs + 1): train(epoch) toc = time.time() print(f'total training time: {toc-tic}') ## test u_test = np.zeros((num_t, num_x, num_y)) for i in range(0, 6): xt = np.concatenate((t[i] * np.ones((num_x * num_y, 1)), x_2d), axis=1)[:, None] xt_tensor = torch.tensor(xt, dtype=torch.float32).to(device) u_test[i, :, :] = to_numpy(model(xt_tensor)).reshape(num_x, num_y) x_test = to_numpy(x_test) fig, ax = newfig(2.0, 1.1) ax.axis('off') gs0 = gridspec.GridSpec(1, 2) gs0.update(top=1 - 0.06, bottom=1 - 1 / 3, left=0.15, right=0.85, wspace=0) h = ax.imshow(u_test[i, :, :].T, interpolation='nearest', cmap='rainbow', origin='lower', aspect='auto') fig.colorbar(h) ax.plot(x_test[:, 1], x_test[:, 2], 'kx', label='Data (%d points)' % (x_test.shape[0]), markersize=4, clip_on=False) line = np.linspace(x_test.min(), x_test.max(), 2)[:, None] savefig('./u_test_' + str(i))
def plot_ide_cont_results(X_star, u_pred, X_u_train, u_train, Exact_u, X, T, x, t, lambda_1_value, lambda_1_value_noisy, lambda_2_value, lambda_2_value_noisy): fig, ax = newfig(1.0, 1.4) ax.axis('off') U_pred = griddata(X_star, u_pred.flatten(), (X, T), method='cubic') ####### Row 0: u(t,x) ################## gs0 = gridspec.GridSpec(1, 2) gs0.update(top=1 - 0.06, bottom=1 - 1.0 / 3.0 + 0.06, left=0.15, right=0.85, wspace=0) ax = plt.subplot(gs0[:, :]) h = ax.imshow(U_pred.T, interpolation='nearest', cmap='rainbow', extent=[t.min(), t.max(), x.min(), x.max()], origin='lower', aspect='auto') divider = make_axes_locatable(ax) cax = divider.append_axes("right", size="5%", pad=0.05) fig.colorbar(h, cax=cax) ax.plot(X_u_train[:, 1], X_u_train[:, 0], 'kx', label='Data (%d points)' % (u_train.shape[0]), markersize=2, clip_on=False) line = np.linspace(x.min(), x.max(), 2)[:, None] ax.plot(t[25] * np.ones((2, 1)), line, 'w-', linewidth=1) ax.plot(t[50] * np.ones((2, 1)), line, 'w-', linewidth=1) ax.plot(t[75] * np.ones((2, 1)), line, 'w-', linewidth=1) ax.set_xlabel('$t$') ax.set_ylabel('$x$') ax.legend(loc='upper center', bbox_to_anchor=(1.0, -0.125), ncol=5, frameon=False) ax.set_title('$u(t,x)$', fontsize=10) ####### Row 1: u(t,x) slices ################## gs1 = gridspec.GridSpec(1, 3) gs1.update(top=1 - 1.0 / 3.0 - 0.1, bottom=1.0 - 2.0 / 3.0, left=0.1, right=0.9, wspace=0.5) ax = plt.subplot(gs1[0, 0]) ax.plot(x, Exact_u[25, :], 'b-', linewidth=2, label='Exact') ax.plot(x, U_pred[25, :], 'r--', linewidth=2, label='Prediction') ax.set_xlabel('$x$') ax.set_ylabel('$u(t,x)$') ax.set_title('$t = 0.25$', fontsize=10) ax.axis('square') ax.set_xlim([-1.1, 1.1]) ax.set_ylim([-1.1, 1.1]) ax = plt.subplot(gs1[0, 1]) ax.plot(x, Exact_u[50, :], 'b-', linewidth=2, label='Exact') ax.plot(x, U_pred[50, :], 'r--', linewidth=2, label='Prediction') ax.set_xlabel('$x$') ax.set_ylabel('$u(t,x)$') ax.axis('square') ax.set_xlim([-1.1, 1.1]) ax.set_ylim([-1.1, 1.1]) ax.set_title('$t = 0.50$', fontsize=10) ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.35), ncol=5, frameon=False) ax = plt.subplot(gs1[0, 2]) ax.plot(x, Exact_u[75, :], 'b-', linewidth=2, label='Exact') ax.plot(x, U_pred[75, :], 'r--', linewidth=2, label='Prediction') ax.set_xlabel('$x$') ax.set_ylabel('$u(t,x)$') ax.axis('square') ax.set_xlim([-1.1, 1.1]) ax.set_ylim([-1.1, 1.1]) ax.set_title('$t = 0.75$', fontsize=10) ####### Row 3: Identified PDE ################## gs2 = gridspec.GridSpec(1, 3) gs2.update(top=1.0 - 2.0 / 3.0, bottom=0, left=0.0, right=1.0, wspace=0.0) ax = plt.subplot(gs2[:, :]) ax.axis('off') s1 = r'$\begin{tabular}{ |c|c| } \hline Correct PDE & $u_t + u u_x - 0.0031831 u_{xx} = 0$ \\ \hline Identified PDE (clean data) & ' s2 = r'$u_t + %.5f u u_x - %.7f u_{xx} = 0$ \\ \hline ' % (lambda_1_value, lambda_2_value) s3 = r'Identified PDE (1\% noise) & ' s4 = r'$u_t + %.5f u u_x - %.7f u_{xx} = 0$ \\ \hline ' % ( lambda_1_value_noisy, lambda_2_value_noisy) s5 = r'\end{tabular}$' s = s1 + s2 + s3 + s4 + s5 ax.text(0.1, 0.1, s) plt.show()
def plot_ide_disc_results(x_star, t_star, idx_t_0, idx_t_1, x_0, u_0, x_1, u_1, ub, lb, u_1_pred, Exact, lambda_1_value, lambda_1_value_noisy, lambda_2_value, lambda_2_value_noisy, x, t, save_path=None, save_hp=None): fig, ax = newfig(1.0, 1.5) ax.axis('off') gs0 = gridspec.GridSpec(1, 2) gs0.update(top=1 - 0.06, bottom=1 - 1 / 3 + 0.05, left=0.15, right=0.85, wspace=0) ax = plt.subplot(gs0[:, :]) h = ax.imshow(Exact, interpolation='nearest', cmap='rainbow', extent=[t_star.min(), t_star.max(), lb[0], ub[0]], origin='lower', aspect='auto') divider = make_axes_locatable(ax) cax = divider.append_axes("right", size="5%", pad=0.05) fig.colorbar(h, cax=cax) line = np.linspace(x_star.min(), x_star.max(), 2)[:, None] ax.plot(t_star[idx_t_0] * np.ones((2, 1)), line, 'w-', linewidth=1.0) ax.plot(t_star[idx_t_1] * np.ones((2, 1)), line, 'w-', linewidth=1.0) ax.set_xlabel('$t$') ax.set_ylabel('$x$') ax.set_title('$u(t,x)$', fontsize=10) gs1 = gridspec.GridSpec(1, 2) gs1.update(top=1 - 1 / 3 - 0.1, bottom=1 - 2 / 3, left=0.15, right=0.85, wspace=0.5) ax = plt.subplot(gs1[0, 0]) ax.plot(x_star, Exact[:, idx_t_0][:, None], 'b', linewidth=2, label='Exact') ax.plot(x_0, u_0, 'rx', linewidth=2, label='Data') ax.set_xlabel('$x$') ax.set_ylabel('$u(t,x)$') ax.set_title('$t = %.2f$\n%d trainng data' % (t_star[idx_t_0], u_0.shape[0]), fontsize=10) ax = plt.subplot(gs1[0, 1]) ax.plot(x_star, Exact[:, idx_t_1][:, None], 'b', linewidth=2, label='Exact') ax.plot(x_1, u_1, 'rx', linewidth=2, label='Data') ax.set_xlabel('$x$') ax.set_ylabel('$u(t,x)$') ax.set_title('$t = %.2f$\n%d trainng data' % (t_star[idx_t_1], u_1.shape[0]), fontsize=10) ax.legend(loc='upper center', bbox_to_anchor=(-0.3, -0.3), ncol=2, frameon=False) gs2 = gridspec.GridSpec(1, 2) gs2.update(top=1 - 2 / 3 - 0.05, bottom=0, left=0.15, right=0.85, wspace=0.0) ax = plt.subplot(gs2[0, 0]) ax.axis('off') nu = 0.01 / np.pi s1 = r'$\begin{tabular}{ |c|c| } \hline Correct PDE & $u_t + u u_x + %.6f u_{xx} = 0$ \\ \hline Identified PDE (clean data) & ' % ( nu) s2 = r'$u_t + %.3f u u_x + %.6f u_{xx} = 0$ \\ \hline ' % (lambda_1_value, lambda_2_value) s3 = r'Identified PDE (1\% noise) & ' s4 = r'$u_t + %.3f u u_x + %.6f u_{xx} = 0$ \\ \hline ' % ( lambda_1_value_noisy, lambda_2_value_noisy) s5 = r'\end{tabular}$' s = s1 + s2 + s3 + s4 + s5 ax.text(-0.1, 0.2, s) if save_path != None and save_hp != None: saveResultDir(save_path, save_hp) else: plt.show()
pmin, pmax = Fields_Num[0::6, :].min(), Fields_Num[0::6, :].max() Tmin, Tmax = Fields_Num[3::6, :].min(), Fields_Num[3::6, :].max() def contourplot(ax, x, y, phi1, vmin, vmax, phi2, levels, title): sc1 = ax.contourf(x, y, phi1, vmin=vmin, vmax=vmax, cmap='jet') sc2 = ax.contour(x, y, phi2, levels=levels, colors=['k']) s2 = np.sqrt(2) / 2 ax.axis([-s2, s2, -s2, s2]) ax.set_title(title) ax.axis('off') return sc1, sc2 plt.close('all') fig, _ = newfig(width=2, nplots=2 / len(ValidationInd)) gs = gridspec.GridSpec(2, len(ValidationInd)) for i in range(len(ValidationInd)): alphai = alpha[i:i + 1, :] Fieldi = Fields[i] x, y, p, u, v, T, omega, psi = Fieldi[0], Fieldi[1], Fieldi[2], Fieldi[ 3], Fieldi[4], Fieldi[5], Fieldi[6], Fieldi[7] pNum = np.reshape(Fields_Num[0::6, i], x.shape) uNum = np.reshape(Fields_Num[1::6, i], x.shape) vNum = np.reshape(Fields_Num[2::6, i], x.shape) TNum = np.reshape(Fields_Num[3::6, i], x.shape) omegaNum = np.reshape(Fields_Num[4::6, i], x.shape) psiNum = np.reshape(Fields_Num[5::6, i], x.shape) psi_max = psiNum.max()
# Prediction u_pred, e_pred = model.predict(t, x) # Error error_u = np.linalg.norm(u - u_pred, 2) / np.linalg.norm(u, 2) error_e = np.linalg.norm(e - e_pred, 2) / np.linalg.norm(e, 2) print('Error p: %e' % (error_u)) print('Error e: %e' % (error_e)) ###################################################################### ############################# Plotting ############################### ###################################################################### fig, ax = newfig(1.0, 1.2) ax.axis('off') gs = gridspec.GridSpec(2, 2) gs.update(top=0.9, bottom=0.1, left=0.1, right=0.9, wspace=0.5, hspace=0.7) ######## Exact p(t,x) ########### ax = plt.subplot(gs[0:1, 0]) h = plot_solution(t, x, u, ax) divider = make_axes_locatable(ax) cax = divider.append_axes("right", size="5%", pad=0.05) fig.colorbar(h, cax=cax) ax.set_xlabel('$t$') ax.set_ylabel('$x$') ax.set_title('Exact $P(t,x)$', fontsize=10)
def plotting(t_star, S_star, S_pred, perm, forecast=False): sns.set() fig, ax = newfig(2.0, 0.7) gs0 = gridspec.GridSpec(1, 1) gs0.update(top=0.9, bottom=0.15, left=0.1, right=0.95, hspace=0.3, wspace=0.3) ax = plt.subplot(gs0[0:1, 0:1]) ax.plot(t_star, S_star[:, 2], 'C1', linewidth=2, label='input data') ax.scatter(t_star[perm], S_star[perm, 2], marker='o', s=40, label='sampled input') ax.set_xlabel('$t\ (min)$', fontsize=18) ax.set_ylabel('$G\ (mg/dl) $', fontsize=18) ax.legend(fontsize='large') #################################### fig, ax = newfig(1.8, 0.75) gs1 = gridspec.GridSpec(1, 2) gs1.update(top=0.85, bottom=0.15, left=0.1, right=0.95, hspace=0.3, wspace=0.3) ax = plt.subplot(gs1[0:1, 0:1]) ax.plot(t_star, S_star[:, 0] * Vg2, 'C1', linewidth=2, label='exact') ax.plot(t_star, S_pred[:, 0] * Vg2, 'g-.', linewidth=3, label='learned') ax.set_xlabel('$t\ (min)$', fontsize=18) ax.set_ylabel('$I_p\ (\mu U/ml)$', fontsize=18) ax.legend(fontsize='large') ax = plt.subplot(gs1[0:1, 1:2]) ax.plot(t_star, S_star[:, 1] * Vg2, 'C1', linewidth=2) ax.plot(t_star, S_pred[:, 1] * Vg2, 'g-.', linewidth=3) ax.set_xlabel('$t\ (min)$', fontsize=18) ax.set_ylabel('$I_i\ (\mu U/ml)$', fontsize=18) fig, ax = newfig(1.8, 0.75) gs2 = gridspec.GridSpec(1, 2) gs2.update(top=0.85, bottom=0.15, left=0.1, right=0.95, hspace=0.3, wspace=0.3) ax = plt.subplot(gs2[0:1, 0:1]) if not forecast: ax.scatter(t_star[perm], S_star[perm, 2], marker='o', c='C1', s=30) else: ax.plot(t_star, S_star[:, 2], 'C1', linewidth=2) ax.plot(t_star, S_pred[:, 2], 'g-.', linewidth=3) ax.set_xlabel('$t\ (min)$', fontsize=18) ax.set_ylabel('$G\ (mg/dl)$', fontsize=18) ax = plt.subplot(gs2[0:1, 1:2]) ax.plot(t_star, S_star[:, 6] * Vg2, 'C1', linewidth=2) ax.plot(t_star, S_pred[:, 6] * Vg2, 'g-.', linewidth=3) ax.set_xlabel('$t\ (min)$', fontsize=18) ax.set_ylabel('$I_G\ (mg/min)$', fontsize=18)
print('Error u: %e' % (error_u)) U_pred = griddata(X_star, u_pred.flatten(), (X, T), method='cubic') FU_pred = griddata(X_star, f_u_pred.flatten(), (X, T), method='cubic') ###################################################################### ############################# Plotting ############################### ###################################################################### X0 = np.concatenate((x0, 0 * x0), 1) # (x0, 0) X_lb = np.concatenate((0 * tb + lb[0], tb), 1) # (lb[0], tb) X_ub = np.concatenate((0 * tb + ub[0], tb), 1) # (ub[0], tb) X_u_train = np.vstack([X0, X_lb, X_ub]) fig, ax = newfig(1.3, 1.0) ax.axis('off') ####### Row 0: h(t,x) ################## gs0 = gridspec.GridSpec(1, 2) gs0.update(top=1 - 0.06, bottom=1 - 1 / 3, left=0.15, right=0.85, wspace=0) ax = plt.subplot(gs0[:, :]) h = ax.imshow(U_pred.T, interpolation='nearest', cmap='YlGnBu', extent=[lb[1], ub[1], lb[0], ub[0]], origin='lower', aspect='auto') divider = make_axes_locatable(ax) cax = divider.append_axes("right", size="5%", pad=0.05)
Exact_idn = np.loadtxt("Results/ReLU/exact_sol.csv", delimiter=',') U_relu = np.loadtxt("Results/ReLU/idn_relu.csv", delimiter=',') U_sine = np.loadtxt("Results/Sine/idn_sine.csv", delimiter=',') U_rat = np.loadtxt("Results/Rational_3_2/idn_rat.csv", delimiter=',') lb_idn = np.array([0.0, -20.0]) ub_idn = np.array([40.0, 20.0]) # Print mean square errors E_relu = np.mean((Exact_idn-U_relu)**2) E_sine = np.mean((Exact_idn-U_sine)**2) E_rat = np.mean((Exact_idn-U_rat)**2) print("Error ReLU = %e, Error Sine = %e, Error Rat = %e" % (E_relu, E_sine, E_rat)) # Plot the different solutions fig, ax = newfig(0.6, 1.2) ax.axis('off') gs = gridspec.GridSpec(1, 1) gs.update(top=0.9, bottom=0.1, left=0.1, right=0.9, wspace=0.7, hspace=0.5) # ######## Exact solution ####################### ax = plt.subplot(gs[0, 0]) h = ax.imshow(Exact_idn, interpolation='nearest', cmap='jet', extent=[lb_idn[0], ub_idn[0], lb_idn[1], ub_idn[1]], origin='lower', aspect='auto') divider = make_axes_locatable(ax) cax = divider.append_axes("right", size="5%", pad=0.05) fig.colorbar(h, cax=cax) savefig('Results/solution')
u_pred, f_pred = model.idn_predict(t_sol_star, x_sol_star) u_pred_idn, f_pred_idn = model.sol_predict(t_idn_star, x_idn_star) error_u = np.linalg.norm(u_sol_star-u_pred,2)/np.linalg.norm(u_sol_star,2) error_u_idn = np.linalg.norm(u_idn_star-u_pred_idn,2)/np.linalg.norm(u_idn_star,2) print('Error u: %e' % (error_u)) print('Error u (idn): %e' % (error_u_idn)) U_pred = griddata(X_sol_star, u_pred.flatten(), (T_sol, X_sol), method='cubic') ###################################################################### ############################# Plotting ############################### ###################################################################### fig, ax = newfig(1.0, 0.6) ax.axis('off') ######## Row 2: Pressure ####################### ######## Predicted p(t,x,y) ########### gs = gridspec.GridSpec(1, 2) gs.update(top=0.8, bottom=0.2, left=0.1, right=0.9, wspace=0.5) ax = plt.subplot(gs[:, 0]) h = ax.imshow(Exact_sol, interpolation='nearest', cmap='jet', extent=[lb_sol[0], ub_sol[0], lb_sol[1], ub_sol[1]], origin='lower', aspect='auto') divider = make_axes_locatable(ax) cax = divider.append_axes("right", size="5%", pad=0.05) fig.colorbar(h, cax=cax) ax.set_xlabel('$t$')
M = 1 scheme = 'AM' model = Multistep_NN(dt, X_train, layers, M, scheme) N_Iter = 50000 model.train(N_Iter) def learned_f(x, t): f = model.predict_f(x[None, :]) return f.flatten() learned_X_star = odeint(learned_f, x0, t_star) ####### Plotting ################## fig, ax = newfig(1.0, 0.8) ax.axis('off') gs0 = gridspec.GridSpec(1, 2) gs0.update(top=0.95, bottom=0.1, left=0.0, right=0.90, wspace=0.15) ax = plt.subplot(gs0[:, 0:1], projection='3d') ax.w_xaxis.set_pane_color((1.0, 1.0, 1.0, 1.0)) ax.w_yaxis.set_pane_color((1.0, 1.0, 1.0, 1.0)) ax.w_zaxis.set_pane_color((1.0, 1.0, 1.0, 1.0)) colorline3d(ax, X_star[:, 0], X_star[:, 1], X_star[:, 2], cmap=plt.cm.jet) ax.grid(False) ax.set_xlim([-200, 200]) ax.set_ylim([-200, 200]) ax.set_zlim([-150, 0]) ax.set_xticks([-200, 0, 200])
U2_star = griddata(X_star2, u2_star.flatten(), (X2, T2), method='cubic') U3_star = griddata(X_star3, u3_star.flatten(), (X3, T3), method='cubic') U4_star = griddata(X_star4, u4_star.flatten(), (X4, T4), method='cubic') u1_err = abs(u1_pred - u1_star) u2_err = abs(u2_pred - u2_star) u3_err = abs(u3_pred - u3_star) u4_err = abs(u4_pred - u4_star) U1_err = griddata(X_star1, u1_err.flatten(), (X1, T1), method='cubic') U2_err = griddata(X_star2, u2_err.flatten(), (X2, T2), method='cubic') U3_err = griddata(X_star3, u3_err.flatten(), (X3, T3), method='cubic') U4_err = griddata(X_star4, u4_err.flatten(), (X4, T4), method='cubic') ############################ Plotting #################>>>>>>>>>>>>>>>>>> fig, ax = newfig(1.0, 1.1) # fig = plt.figure() gridspec.GridSpec(1,1) ax = plt.subplot2grid((1,1), (0,0)) maxLevel = max(max(u1_star),max(max(u2_star),max(max(u3_star),max(u4_star))))[0] minLevel = min(min(u1_star),min(min(u2_star), min(min(u3_star), min(u4_star)) ))[0] levels = np.linspace(minLevel-0.01, maxLevel+0.01, 200) CS_ext1 = ax.contourf(T, X, U_star, levels=levels, cmap='jet', origin='lower') cbar = fig.colorbar(CS_ext1) # , ticks=[-1, -0.5, 0, 0.5, 1]) # cbar.ax.set_yticklabels(['-1', '-0.5', '0', '0.5', '1']) cbar.ax.tick_params(labelsize=20) ax.set_xlim(-0.01, 1) ax.set_ylim(-1.01, 1.02) #ax_pred.locator_params(nbins=5)
M = 1 scheme = 'AM' model = Multistep_NN(dt, X_train, layers, M, scheme) N_Iter = 50000 model.train(N_Iter) def learned_f(x, t): f = model.predict_f(x[None, :]) return f.flatten() learned_X_star = odeint(learned_f, x0, t_star) ####### Plotting ################## fig, ax = newfig(1.0, 1.55) ax.axis('off') gs0 = gridspec.GridSpec(3, 2) gs0.update(top=0.95, bottom=0.35, left=0.1, right=0.95, hspace=0.5, wspace=0.3) ax = plt.subplot(gs0[0:1, 0:1]) ax.plot(t_star, X_star[:, 0], 'r-') ax.plot(t_star, learned_X_star[:, 0], 'k--') ax.set_xlabel('$t$') ax.set_ylabel('$S_1$')