def main(): kf = tf.Variable(0.05) D = tf.Variable(1.0) def pde(x, y): ca, cb = y[:, 0:1], y[:, 1:2] dca_t = dde.grad.jacobian(y, x, i=0, j=1) dca_xx = dde.grad.hessian(y, x, component=0, i=0, j=0) dcb_t = dde.grad.jacobian(y, x, i=1, j=1) dcb_xx = dde.grad.hessian(y, x, component=1, i=0, j=0) eq_a = dca_t - 1e-3 * D * dca_xx + kf * ca * cb ** 2 eq_b = dcb_t - 1e-3 * D * dcb_xx + 2 * kf * ca * cb ** 2 return [eq_a, eq_b] def fun_bc(x): return 1 - x[:, 0:1] def fun_init(x): return np.exp(-20 * x[:, 0:1]) geom = dde.geometry.Interval(0, 1) timedomain = dde.geometry.TimeDomain(0, 10) geomtime = dde.geometry.GeometryXTime(geom, timedomain) bc_a = dde.DirichletBC( geomtime, fun_bc, lambda _, on_boundary: on_boundary, component=0 ) bc_b = dde.DirichletBC( geomtime, fun_bc, lambda _, on_boundary: on_boundary, component=1 ) ic1 = dde.IC(geomtime, fun_init, lambda _, on_initial: on_initial, component=0) ic2 = dde.IC(geomtime, fun_init, lambda _, on_initial: on_initial, component=1) observe_x, Ca, Cb = gen_traindata() ptset = dde.bc.PointSet(observe_x) observe_y1 = dde.DirichletBC( geomtime, ptset.values_to_func(Ca), lambda x, _: ptset.inside(x), component=0 ) observe_y2 = dde.DirichletBC( geomtime, ptset.values_to_func(Cb), lambda x, _: ptset.inside(x), component=1 ) data = dde.data.TimePDE( geomtime, pde, [bc_a, bc_b, ic1, ic2, observe_y1, observe_y2], num_domain=2000, num_boundary=100, num_initial=100, anchors=observe_x, num_test=50000, ) net = dde.maps.FNN([2] + [20] * 3 + [2], "tanh", "Glorot uniform") model = dde.Model(data, net) model.compile("adam", lr=0.001) variable = dde.callbacks.VariableValue( [kf, D], period=1000, filename="variables.dat" ) losshistory, train_state = model.train(epochs=80000, callbacks=[variable]) dde.saveplot(losshistory, train_state, issave=True, isplot=True)
def main(): def pde(x, y): dy_t = dde.grad.jacobian(y, x,i=0, j=4) dy_xx = dde.grad.hessian(y, x,component=0 , j=0) # dy_xx = dde.grad.hessian(y, x , j=0) return ( dy_t - dy_xx + tf.exp(-x[:, 4:]) * (tf.sin(np.pi * x[:, 0:1]) - np.pi ** 2 * tf.sin(np.pi * x[:, 0:1])), x[:, 0:1] * 0, ) def func(x): return np.sin(np.pi * x[:, 0:1]) * np.exp(-x[:, 4:]) def func2(x): return np.sin(np.pi * x[:, 0:1]) * np.exp(-x[:, 4:]),0 # geom = dde.geometry.Interval(-1, 1) geom = dde.geometry.Rectangle geom = dde.geometry.Hypercube([-1]*4,[1]*4) timedomain = dde.geometry.TimeDomain(0, 1) geomtime = dde.geometry.GeometryXTime(geom, timedomain) bc = dde.DirichletBC(geomtime, func, lambda _, on_boundary: on_boundary,component=0) ic = dde.IC(geomtime, func, lambda _, on_initial: on_initial,component=0) ic2 = dde.IC(geomtime,lambda shit: 1, lambda _, on_initial: on_initial,component=1) # observe_x = np.vstack((np.linspace(-1, 1, num=10), np.full((10), 1))).T # ptset = dde.bc.PointSet(observe_x) # observe_y = dde.DirichletBC( # geomtime, ptset.values_to_func(func(observe_x)), lambda x, _: ptset.inside(x) # ) data = dde.data.TimePDE( geomtime, pde, [bc, ic,ic2], num_domain=4000, num_boundary=1, num_initial=100, ) layer_size = [5] + [32] * 3 + [2] activation = "tanh" initializer = "Glorot uniform" net = dde.maps.FNN(layer_size, activation, initializer) model = dde.Model(data, net) model.compile("adam", lr=0.001, metrics=["l2 relative error"]) losshistory, train_state = model.train(epochs=10000) dde.saveplot(losshistory, train_state, issave=True, isplot=True)
def main(): C1 = tf.Variable(1.0) C2 = tf.Variable(1.0) C3 = tf.Variable(1.0) def Lorenz_system(x, y): """Lorenz system. dy1/dx = 10 * (y2 - y1) dy2/dx = y1 * (28 - y3) - y2 dy3/dx = y1 * y2 - 8/3 * y3 """ y1, y2, y3 = y[:, 0:1], y[:, 1:2], y[:, 2:] dy1_x = dde.grad.jacobian(y, x, i=0) dy2_x = dde.grad.jacobian(y, x, i=1) dy3_x = dde.grad.jacobian(y, x, i=2) return [ dy1_x - C1 * (y2 - y1), dy2_x - y1 * (C2 - y3) + y2, dy3_x - y1 * y2 + C3 * y3, ] def boundary(_, on_initial): return on_initial geom = dde.geometry.TimeDomain(0, 3) # Initial conditions ic1 = dde.IC(geom, lambda X: -8, boundary, component=0) ic2 = dde.IC(geom, lambda X: 7, boundary, component=1) ic3 = dde.IC(geom, lambda X: 27, boundary, component=2) # Get the train data observe_t, ob_y = gen_traindata() observe_y0 = dde.PointSetBC(observe_t, ob_y[:, 0:1], component=0) observe_y1 = dde.PointSetBC(observe_t, ob_y[:, 1:2], component=1) observe_y2 = dde.PointSetBC(observe_t, ob_y[:, 2:3], component=2) data = dde.data.PDE( geom, Lorenz_system, [ic1, ic2, ic3, observe_y0, observe_y1, observe_y2], num_domain=400, num_boundary=2, anchors=observe_t, ) net = dde.maps.FNN([1] + [40] * 3 + [3], "tanh", "Glorot uniform") model = dde.Model(data, net) model.compile("adam", lr=0.001) variable = dde.callbacks.VariableValue([C1, C2, C3], period=600, filename="variables.dat") losshistory, train_state = model.train(epochs=60000, callbacks=[variable]) dde.saveplot(losshistory, train_state, issave=True, isplot=True)
def main(): #sir params beta = 1 #infection gamma = .2 #removal N = 150 #population initial_infected = 20 #x is time, y is odes def odes(x, y): s, i, r = y[:, 0:1], y[:, 1:2], y[:, 2:3] ds_x = tf.gradients(s, x)[0] di_x = tf.gradients(i, x)[0] dr_x = tf.gradients(r, x)[0] de1 = ds_x + beta * i * s / N de2 = di_x - (beta * i * s / N) + (gamma * i) de3 = dr_x - gamma * i # de1 = tf.gradients(ds_x, x)[0] - s # de2 = tf.gradients(di_x, x)[0] - i # de3 = tf.gradients(dr_x, x)[0] - r return [de1, de2, de3] def boundary(_, on_initial): return on_initial def func(x): return np.hstack((0 * np.cos(x), 0 * np.cos(x), 0 * np.cos(x))) #return np.hstack(((N-initial_infected)*np.cos(x), initial_infected*np.cos(x), 0*np.cos(x))) geom = dde.geometry.TimeDomain(0, 15) ic1 = dde.IC(geom, lambda x: (N - initial_infected) * np.ones(x.shape), boundary, component=0) ic2 = dde.IC(geom, lambda x: initial_infected * np.ones(x.shape), boundary, component=1) ic3 = dde.IC(geom, lambda x: 0 * np.ones(x.shape), boundary, component=2) data = dde.data.PDE(geom, odes, [ic1, ic2, ic3], 1000, 3) layer_size = [1] + [50] * 3 + [3] activation = "tanh" initializer = "Glorot uniform" net = dde.maps.FNN(layer_size, activation, initializer) model = dde.Model(data, net) model.compile("adam", lr=0.001) losshistory, train_state = model.train(epochs=20000) #dde.saveplot(losshistory, train_state, issave=True, isplot=True) plot_best_state(train_state) plot_loss_history(losshistory) plt.show()
def main(): geom = dde.geometry.TimeDomain(0, 5) ic = dde.IC(geom, func, lambda _, on_initial: on_initial) quad_deg = 20 data = dde.data.IDE( geom, ide, ic, quad_deg, kernel=kernel, num_domain=10, num_boundary=2, train_distribution="uniform", ) layer_size = [1] + [20] * 3 + [1] activation = "tanh" initializer = "Glorot uniform" net = dde.maps.FNN(layer_size, activation, initializer) model = dde.Model(data, net) model.compile("L-BFGS-B") model.train() X = geom.uniform_points(100) y_true = func(X) y_pred = model.predict(X) print("L2 relative error:", dde.metrics.l2_relative_error(y_true, y_pred)) plt.figure() plt.plot(X, y_true, "-") plt.plot(X, y_pred, "o") plt.show() np.savetxt("test.dat", np.hstack((X, y_true, y_pred)))
def main(): def pde(x, y): dy_tt = dde.grad.hessian(y, x, i=1, j=1) dy_xx = dde.grad.hessian(y, x, i=0, j=0) return dy_tt - C**2 * dy_xx def func(x): x, t = np.split(x, 2, axis=1) return np.sin(np.pi * x) * np.cos(C * np.pi * t) + np.sin( A * np.pi * x) * np.cos(A * C * np.pi * t) geom = dde.geometry.Interval(0, 1) timedomain = dde.geometry.TimeDomain(0, 1) geomtime = dde.geometry.GeometryXTime(geom, timedomain) bc = dde.DirichletBC(geomtime, func, lambda _, on_boundary: on_boundary) ic_1 = dde.IC(geomtime, func, lambda _, on_initial: on_initial) # do not use dde.NeumannBC here, since `normal_derivative` does not work with temporal coordinate. ic_2 = dde.OperatorBC( geomtime, lambda x, y, _: dde.grad.jacobian(y, x, i=0, j=1), lambda x, _: np.isclose(x[1], 0), ) data = dde.data.TimePDE( geomtime, pde, [bc, ic_1, ic_2], num_domain=360, num_boundary=360, num_initial=360, solution=func, num_test=10000, ) layer_size = [2] + [100] * 3 + [1] activation = "tanh" initializer = "Glorot uniform" net = dde.maps.STMsFFN(layer_size, activation, initializer, sigmas_x=[1], sigmas_t=[1, 10]) net.apply_feature_transform(lambda x: (x - 0.5) * 2 * np.sqrt(3)) model = dde.Model(data, net) initial_losses = get_initial_loss(model) loss_weights = 5 / initial_losses model.compile( "adam", lr=0.001, metrics=["l2 relative error"], loss_weights=loss_weights, decay=("inverse time", 2000, 0.9), ) pde_residual_resampler = dde.callbacks.PDEResidualResampler(period=1) losshistory, train_state = model.train(epochs=10000, callbacks=[pde_residual_resampler], display_every=500) dde.saveplot(losshistory, train_state, issave=True, isplot=True)
def main(): """ main program haotian song 2020/12/23 """ geom = dde.geometry.TimeDomain(0, 3) y0 = InitialCondition() a = dde.config.real.set_complex128() ic = [] for j in range(0, len(y0)): ic.append(dde.IC(geom, lambda X: y0, boundary, component=j)) # BoundaryCondition = dde.OperatorBC(geom,boundaryFunction,on_boundary=on_boundary) data = dde.data.PDE(geom, SLE_DL, ic, num_domain=400, num_boundary=2, num_test=100) layer_size = [1] + [50] * 3 + [1] activation = "tanh" initializer = "Glorot uniform" net = dde.maps.FNN(layer_size, activation, initializer) model = dde.Model(data, net) model.compile("adam", lr=0.001) variable = dde.callbacks.VariableValue([C1, C2, C3], period=600, filename="variables.dat") losshistory, train_state = model.train(epochs=60000, callbacks=[variable]) dde.saveplot(losshistory, train_state, issave=True, isplot=True)
def main(): geom = dde.geometry.Interval(-1, 1) timedomain = dde.geometry.TimeDomain(0, 0.99) geomtime = dde.geometry.GeometryXTime(geom, timedomain) bc = dde.DirichletBC(geomtime, lambda x: 0, lambda _, on_boundary: on_boundary) ic = dde.IC( geomtime, lambda x: -np.sin(np.pi * x[:, 0:1]), lambda _, on_initial: on_initial ) data = dde.data.TimePDE( geomtime, pde, [bc, ic], num_domain=2540, num_boundary=80, num_initial=160 ) net = dde.maps.FNN([2] + [20] * 3 + [1], "tanh", "Glorot normal") model = dde.Model(data, net) model.compile("adam", lr=1e-3) model.train(epochs=15000) model.compile("L-BFGS-B") losshistory, train_state = model.train() dde.saveplot(losshistory, train_state, issave=True, isplot=True) X, y_true = gen_testdata() y_pred = model.predict(X) f = model.predict(X, operator=pde) print("Mean residual:", np.mean(np.absolute(f))) print("L2 relative error:", dde.metrics.l2_relative_error(y_true, y_pred)) np.savetxt("test.dat", np.hstack((X, y_true, y_pred)))
def visualize_solution(nu): # Build pseudo model def pde(x, u): u_x = dde.grad.jacobian(u, x, i=0, j=0) u_t = dde.grad.jacobian(u, x, i=0, j=1) u_xx = dde.grad.hessian(u, x, i=0, j=0) return u_t + u * u_x - nu * u_xx spatial_domain = dde.geometry.Interval(x_min, x_max) temporal_domain = dde.geometry.TimeDomain(t_min, t_max) spatio_temporal_domain = dde.geometry.GeometryXTime( spatial_domain, temporal_domain) boundary_condition = dde.DirichletBC(spatio_temporal_domain, lambda x: 0, lambda _, on_boundary: on_boundary) initial_condition = dde.IC(spatio_temporal_domain, lambda x: -np.sin(np.pi * x[:, 0:1]), lambda _, on_initial: on_initial) data = dde.data.TimePDE(spatio_temporal_domain, pde, [boundary_condition, initial_condition], num_domain=domain_points, num_boundary=boundary_points, num_initial=initial_points, num_test=test_points) net = dde.maps.FNN([2] + hidden_layers * [hidden_units] + [1], "tanh", "Glorot normal") model = dde.Model(data, net) # Load reference solution file_name = 'Reference_Solutions/u_exact_nu_{}.mat'.format(nus[i]) data = scipy.io.loadmat(file_name) u_exact = data['usol'].T x_test, t_test = np.meshgrid(np.linspace(x_min, x_max, test_points_x), np.linspace(t_min, t_max, test_points_t)) X = np.vstack((np.ravel(x_test), np.ravel(t_test))).T # Reload model and make predictions model_name = 'Neural_Networks/nu_{}/Burger_Equation_Source_Model_nu_{}-{}'.format( nus[i], nus[i], epochs_source[i]) model.compile("adam", lr=learning_rate) model.train(model_restore_path=model_name, epochs=0) u_pred = model.predict(X).reshape(test_points_t, test_points_x) f = model.predict(X, operator=pde) ax = plt.axes(projection="3d") ax.plot_wireframe(x_test, t_test, u_pred) ax.set_xlabel('location x') ax.set_ylabel('time t') ax.set_zlabel('u') plt.tight_layout() plt.savefig('Predictions/Predicted_solution_nu_{}.png'.format(nus[i]), dpi=300) return
def main(): def pde(x, y): dy_x = tf.gradients(y, x)[0] dy_x, dy_t = dy_x[:, 0:1], dy_x[:, 1:2] dy_xx = tf.gradients(dy_x, x)[0][:, 0:1] return dy_t + y * dy_x - 0.01 / np.pi * dy_xx geom = dde.geometry.Interval(-1, 1) timedomain = dde.geometry.TimeDomain(0, 0.99) geomtime = dde.geometry.GeometryXTime(geom, timedomain) bc = dde.DirichletBC(geomtime, lambda x: np.zeros((len(x), 1)), lambda _, on_boundary: on_boundary) ic = dde.IC(geomtime, lambda x: -np.sin(np.pi * x[:, 0:1]), lambda _, on_initial: on_initial) data = dde.data.TimePDE(geomtime, 1, pde, [bc, ic], num_domain=2500, num_boundary=100, num_initial=160) net = dde.maps.FNN([2] + [20] * 3 + [1], "tanh", "Glorot normal") model = dde.Model(data, net) model.compile("adam", lr=1.0e-3) model.train(epochs=10000) model.compile("L-BFGS-B") model.train() X = geomtime.random_points(100000) err = 1 while err > 0.005: f = model.predict(X, operator=pde) err_eq = np.absolute(f) err = np.mean(err_eq) print("Mean residual: %.3e" % (err)) x_id = np.argmax(err_eq) print("Adding new point:", X[x_id], "\n") data.add_anchors(X[x_id]) early_stopping = dde.callbacks.EarlyStopping(min_delta=1e-4, patience=2000) model.compile("adam", lr=1e-3) model.train(epochs=10000, disregard_previous_best=True, callbacks=[early_stopping]) model.compile("L-BFGS-B") losshistory, train_state = model.train() dde.saveplot(losshistory, train_state, issave=True, isplot=True) X, y_true = gen_testdata() y_pred = model.predict(X) print("L2 relative error:", dde.metrics.l2_relative_error(y_true, y_pred)) np.savetxt("test.dat", np.hstack((X, y_true, y_pred)))
def main(): def ode_system(x, y): """ODE system. dy1/dx = y2 dy2/dx = -y1 """ y1, y2 = y[:, 0:1], y[:, 1:] dy1_x = tf.gradients(y1, x)[0] dy2_x = tf.gradients(y2, x)[0] return [dy1_x - y2, dy2_x + y1] def boundary(_, on_initial): return on_initial def func(x): """ y1 = sin(x) y2 = cos(x) """ return np.hstack((np.sin(x), np.cos(x))) geom = dde.geometry.TimeDomain(0, 10) ic1 = dde.IC(geom, np.sin, boundary, component=0) ic2 = dde.IC(geom, np.cos, boundary, component=1) data = dde.data.PDE(geom, ode_system, [ic1, ic2], 35, 2, solution=func, num_test=100) layer_size = [1] + [50] * 3 + [2] activation = "tanh" initializer = "Glorot uniform" net = dde.maps.FNN(layer_size, activation, initializer) model = dde.Model(data, net) model.compile("adam", lr=0.001, metrics=["l2 relative error"]) losshistory, train_state = model.train(epochs=20000) dde.saveplot(losshistory, train_state, issave=True, isplot=True)
def main(): C = tf.Variable(2.0) def pde(x, y): dy_x = tf.gradients(y, x)[0] dy_x, dy_t = dy_x[:, 0:1], dy_x[:, 1:] dy_xx = tf.gradients(dy_x, x)[0][:, 0:1] return ( dy_t - C * dy_xx + tf.exp(-x[:, 1:]) * (tf.sin(np.pi * x[:, 0:1]) - np.pi**2 * tf.sin(np.pi * x[:, 0:1]))) def func(x): return np.sin(np.pi * x[:, 0:1]) * np.exp(-x[:, 1:]) geom = dde.geometry.Interval(-1, 1) timedomain = dde.geometry.TimeDomain(0, 1) geomtime = dde.geometry.GeometryXTime(geom, timedomain) bc = dde.DirichletBC(geomtime, func, lambda _, on_boundary: on_boundary) ic = dde.IC(geomtime, func, lambda _, on_initial: on_initial) observe_x = np.vstack((np.linspace(-1, 1, num=10), np.full((10), 1))).T ptset = dde.bc.PointSet(observe_x) observe_y = dde.DirichletBC(geomtime, ptset.values_to_func(func(observe_x)), lambda x, _: ptset.inside(x)) data = dde.data.TimePDE( geomtime, 1, pde, [bc, ic, observe_y], num_domain=40, num_boundary=20, num_initial=10, anchors=observe_x, func=func, num_test=10000, ) layer_size = [2] + [32] * 3 + [1] activation = "tanh" initializer = "Glorot uniform" net = dde.maps.FNN(layer_size, activation, initializer) model = dde.Model(data, net) model.compile("adam", lr=0.001, metrics=["l2 relative error"]) variable = dde.callbacks.VariableValue(C, period=1000) losshistory, train_state = model.train(epochs=50000, callbacks=[variable]) dde.saveplot(losshistory, train_state, issave=True, isplot=True)
def main(): """ main function for differential function with q """ dde.config.real.set_float64() # geometry part tmax = 1 Qmax = 10 / C.sigma Xmin = [] Xmax = [] for i in range(1, C.N + 1): Xmin.append(-Qmax) Xmax.append(Qmax) geom = dde.geometry.Hypercube(Xmin, Xmax) # geom = dde.geometry.Interval(-1, 1) timedomain = dde.geometry.TimeDomain(0, tmax) geom = dde.geometry.GeometryXTime(geom, timedomain) x_initial = np.random.rand(13, C.N + 1) xtest = tf.convert_to_tensor(x_initial) ytest = np.random.rand(13, C.sizeRho) ytest = tf.convert_to_tensor(ytest) # Initial conditions ic = [] for j in range(0, (C.N + 1)**2): ic.append( dde.IC(geom, lambda X: initialState(X, j), boundary, component=j)) # test # print(initialState(x_initial,j)) # print(SLE_q(xtest,ytest)) bc = dde.DirichletBC(geom, lambda _: 0, lambda _, on_boundary: on_boundary) ic.append(bc) # data data = dde.data.TimePDE(geom, lambda x, y: SLE_q(x, y, C), ic, num_domain=4000, num_boundary=0, num_initial=100, num_test=None) layer_size = [C.N + 1] + [50] * 3 + [(C.N + 1)**2] activation = "tanh" initializer = "Glorot uniform" net = dde.maps.FNN(layer_size, activation, initializer) model = dde.Model(data, net) model.compile("adam", lr=0.001) model.compile("L-BFGS-B") losshistory, train_state = model.train(epochs=600000, callbacks=ModelCheckpoint) dde.saveplot(losshistory, train_state, issave=True, isplot=True)
def main(): geom = dde.geometry.Interval(0, 1) timedomain = dde.geometry.TimeDomain(0, 1) geomtime = dde.geometry.GeometryXTime(geom, timedomain) bc = dde.DirichletBC(geomtime, func, lambda _, on_boundary: on_boundary) ic = dde.IC(geomtime, func, lambda _, on_initial: on_initial) # Static auxiliary points data = dde.data.TimeFPDE( geomtime, fpde, alpha, [bc, ic], [52], meshtype="static", num_domain=400, solution=func, ) # Dynamic auxiliary points # data = dde.data.TimeFPDE( # geomtime, # fpde, # alpha, # [bc, ic], # [100], # num_domain=20, # num_boundary=1, # num_initial=1, # solution=func, # num_test=50, # ) net = dde.maps.FNN([2] + [20] * 4 + [1], "tanh", "Glorot normal") net.apply_output_transform( lambda x, y: x[:, 0:1] * (1 - x[:, 0:1]) * x[:, 1:2] * y + x[:, 0:1] ** 3 * (1 - x[:, 0:1]) ** 3 ) model = dde.Model(data, net) model.compile("adam", lr=1e-3) losshistory, train_state = model.train(epochs=10000) dde.saveplot(losshistory, train_state, issave=False, isplot=True) X = geomtime.random_points(1000) y_true = func(X) y_pred = model.predict(X) print("L2 relative error:", dde.metrics.l2_relative_error(y_true, y_pred)) np.savetxt("test.dat", np.hstack((X, y_true, y_pred)))
def main(): def pde(x, y): dy_t = dde.grad.jacobian(y, x, i=0, j=1) dy_xx = dde.grad.hessian(y, x, i=0, j=0) return ( dy_t - dy_xx + tf.exp(-x[:, 1:]) * (tf.sin(np.pi * x[:, 0:1]) - np.pi ** 2 * tf.sin(np.pi * x[:, 0:1])) ) def func(x): return np.sin(np.pi * x[:, 0:1]) * np.exp(-x[:, 1:]) geom = dde.geometry.Interval(-1, 1) timedomain = dde.geometry.TimeDomain(0, 1) geomtime = dde.geometry.GeometryXTime(geom, timedomain) bc = dde.DirichletBC(geomtime, func, lambda _, on_boundary: on_boundary) ic = dde.IC(geomtime, func, lambda _, on_initial: on_initial) data = dde.data.TimePDE( geomtime, pde, [bc, ic], num_domain=40, num_boundary=20, num_initial=10, train_distribution="pseudo", solution=func, num_test=10000, ) layer_size = [2] + [32] * 3 + [1] activation = "tanh" initializer = "Glorot uniform" net = dde.maps.FNN(layer_size, activation, initializer) model = dde.Model(data, net) for _ in range(5): model.compile("adam", lr=0.001, metrics=["l2 relative error"]) model.train(epochs=2000) print("epoch = {}, resample train points...".format(model.train_state.epoch)) data.resample_train_points() model.compile("adam", lr=0.001, metrics=["l2 relative error"]) losshistory, train_state = model.train(epochs=2000) dde.saveplot(losshistory, train_state, issave=True, isplot=True)
def main(): def pde(x, y): dy_x = tf.gradients(y, x)[0] dy_x, dy_t = dy_x[:, 0:1], dy_x[:, 1:] dy_xx = tf.gradients(dy_x, x)[0][:, 0:1] return ( dy_t - dy_xx + tf.exp(-x[:, 1:]) * (tf.sin(np.pi * x[:, 0:1]) - np.pi ** 2 * tf.sin(np.pi * x[:, 0:1])) ) def func(x): return np.sin(np.pi * x[:, 0:1]) * np.exp(-x[:, 1:]) geom = dde.geometry.Interval(-1, 1) timedomain = dde.geometry.TimeDomain(0, 1) geomtime = dde.geometry.GeometryXTime(geom, timedomain) bc = dde.DirichletBC(geomtime, func, lambda _, on_boundary: on_boundary) ic = dde.IC(geomtime, func, lambda _, on_initial: on_initial) data = dde.data.TimePDE( geomtime, pde, [bc, ic], num_domain=40, num_boundary=1, num_initial=1, solution=func, num_test=10000, ) layer_size = [2] + [32] * 3 + [1] activation = "tanh" initializer = "Glorot uniform" net = dde.maps.FNN(layer_size, activation, initializer) net.outputs_modify( lambda x, y: x[:, 1:2] * (1 - x[:, 0:1] ** 2) * y + tf.sin(np.pi * x[:, 0:1]) ) model = dde.Model(data, net) model.compile("adam", lr=0.001, metrics=["l2 relative error"]) losshistory, train_state = model.train(epochs=10000) dde.saveplot(losshistory, train_state, issave=True, isplot=True)
def main(): def ide(x, y, int_mat): rhs = tf.matmul(int_mat, y) lhs1 = tf.gradients(y, x)[0] return (lhs1 + y)[:tf.size(rhs)] - rhs def kernel(x, s): return np.exp(s - x) def func(x): return np.exp(-x) * np.cosh(x) geom = dde.geometry.TimeDomain(0, 5) ic = dde.IC(geom, func, lambda _, on_initial: on_initial) quad_deg = 20 data = dde.data.IDE( geom, ide, ic, quad_deg, kernel=kernel, num_domain=10, num_boundary=2, train_distribution="uniform", ) layer_size = [1] + [20] * 3 + [1] activation = "tanh" initializer = "Glorot uniform" net = dde.maps.FNN(layer_size, activation, initializer) model = dde.Model(data, net) model.compile("L-BFGS-B") model.train() X = geom.uniform_points(100) y_true = func(X) y_pred = model.predict(X) print("L2 relative error:", dde.metrics.l2_relative_error(y_true, y_pred)) plt.figure() plt.plot(X, y_true, "-") plt.plot(X, y_pred, "o") plt.show() np.savetxt("test.dat", np.hstack((X, y_true, y_pred)))
def main(): def pde(x, y): dy_t = dde.grad.jacobian(y, x, j=1) dy_xx = dde.grad.hessian(y, x, j=0) return ( dy_t - dy_xx + tf.exp(-x[:, 1:]) * (tf.sin(np.pi * x[:, 0:1]) - np.pi**2 * tf.sin(np.pi * x[:, 0:1]))) def func(x): return np.sin(np.pi * x[:, 0:1]) * np.exp(-x[:, 1:]) def func2(x): return np.exp(-x[:, 1:]) geom = dde.geometry.Interval(-1, 1) timedomain = dde.geometry.TimeDomain(0, 1) geomtime = dde.geometry.GeometryXTime(geom, timedomain) bc = dde.DirichletBC(geomtime, func, lambda _, on_boundary: on_boundary) ic = dde.IC(geomtime, func, lambda _, on_initial: on_initial) data = dde.data.TimePDE( geomtime, pde, [bc, ic], num_domain=4000, num_boundary=20, num_initial=10, solution=func, num_test=None, ) layer_size = [2] + [32] * 3 + [1] activation = "tanh" initializer = "Glorot uniform" net = dde.maps.FNN(layer_size, activation, initializer) model = dde.Model(data, net) model.compile("adam", lr=0.001, metrics=["l2 relative error"]) losshistory, train_state = model.train(epochs=10000) dde.saveplot(losshistory, train_state, issave=True, isplot=True) model.save('./diffusion_1d_model', verbose=1)
def main(): def ide(x, y, int_mat): """int_0^x y(t)dt """ lhs1 = tf.matmul(int_mat, y) lhs2 = tf.gradients(y, x)[0] rhs = 2 * np.pi * tf.cos(2 * np.pi * x) + tf.sin(np.pi * x) ** 2 / np.pi return lhs1 + (lhs2 - rhs)[: tf.size(lhs1)] def func(x): """ x: array_like, N x D_in y: array_like, N x D_out """ return np.sin(2 * np.pi * x) geom = dde.geometry.TimeDomain(0, 1) ic = dde.IC(geom, func, lambda _, on_initial: on_initial) quad_deg = 16 data = dde.data.IDE(geom, ide, ic, quad_deg, num_domain=16, num_boundary=2) layer_size = [1] + [20] * 3 + [1] activation = "tanh" initializer = "Glorot uniform" net = dde.maps.FNN(layer_size, activation, initializer) model = dde.Model(data, net) model.compile("adam", lr=0.001) model.train(epochs=10000) X = geom.uniform_points(100, True) y_true = func(X) y_pred = model.predict(X) print("L2 relative error:", dde.metrics.l2_relative_error(y_true, y_pred)) plt.figure() plt.plot(X, y_true, "-") plt.plot(X, y_pred, "o") plt.show() np.savetxt("test.dat", np.hstack((X, y_true, y_pred)))
def solve_heat_with_dl(lightweight=False): geom = dde.geometry.Rectangle(xmin=[0, 0], xmax=[1, 1]) timedomain = dde.geometry.TimeDomain(0, 2) geomtime = dde.geometry.GeometryXTime(geom, timedomain) bc = dde.DirichletBC(geomtime, _func, lambda _, on_boundary: on_boundary) ic = dde.IC(geomtime, _func, lambda _, on_initial: on_initial) data = dde.data.TimePDE( geomtime, _pde, ic_bcs=[ic, bc], num_domain=500, num_boundary=300, num_initial=200, num_test=10000, solution=_func, ) # inputs (2D Heat Eqn) - t, x, y # output (solution) - u(t, x, y) layer_size = [3] + [128] + [256] + [521] + [256] + [128] + [1] activation = 'tanh' initializer = 'Glorot uniform' net = dde.maps.FNN(layer_size, activation, initializer) model = dde.Model(data, net) # train model model.compile('adam', lr=0.001, metrics=['l2 relative error']) losshistory, train_state = model.train(epochs=10000) if not lightweight: dde.saveplot(losshistory, train_state, issave=True, isplot=True) save_dynamic_contours_from_model(model, 1, 1, 2, 100, 'heat2d') return model
def train_source_model(d): path = Path('Neural_Networks', 'd_{}'.format(d)) if path.exists() and path.is_dir(): shutil.rmtree(path) os.mkdir(path) def pde(x, u): u_vel, v_vel, w_vel, p = u[:, 0:1], u[:, 1:2], u[:, 2:3], u[:, 3:4] u_vel_x = dde.grad.jacobian(u, x, i=0, j=0) u_vel_y = dde.grad.jacobian(u, x, i=0, j=1) u_vel_z = dde.grad.jacobian(u, x, i=0, j=2) u_vel_t = dde.grad.jacobian(u, x, i=0, j=3) u_vel_xx = dde.grad.hessian(u, x, component=0, i=0, j=0) u_vel_yy = dde.grad.hessian(u, x, component=0, i=1, j=1) u_vel_zz = dde.grad.hessian(u, x, component=0, i=2, j=2) v_vel_x = dde.grad.jacobian(u, x, i=1, j=0) v_vel_y = dde.grad.jacobian(u, x, i=1, j=1) v_vel_z = dde.grad.jacobian(u, x, i=1, j=2) v_vel_t = dde.grad.jacobian(u, x, i=1, j=3) v_vel_xx = dde.grad.hessian(u, x, component=1, i=0, j=0) v_vel_yy = dde.grad.hessian(u, x, component=1, i=1, j=1) v_vel_zz = dde.grad.hessian(u, x, component=1, i=2, j=2) w_vel_x = dde.grad.jacobian(u, x, i=2, j=0) w_vel_y = dde.grad.jacobian(u, x, i=2, j=1) w_vel_z = dde.grad.jacobian(u, x, i=2, j=2) w_vel_t = dde.grad.jacobian(u, x, i=2, j=3) w_vel_xx = dde.grad.hessian(u, x, component=2, i=0, j=0) w_vel_yy = dde.grad.hessian(u, x, component=2, i=1, j=1) w_vel_zz = dde.grad.hessian(u, x, component=2, i=2, j=2) p_x = dde.grad.jacobian(u, x, i=3, j=0) p_y = dde.grad.jacobian(u, x, i=3, j=1) p_z = dde.grad.jacobian(u, x, i=3, j=2) momentum_x = u_vel_t + ( u_vel * u_vel_x + v_vel * u_vel_y + w_vel * u_vel_z) + p_x - 1 / Re * (u_vel_xx + u_vel_yy + u_vel_zz) momentum_y = v_vel_t + ( u_vel * v_vel_x + v_vel * v_vel_y + w_vel * v_vel_z) + p_y - 1 / Re * (v_vel_xx + v_vel_yy + v_vel_zz) momentum_z = w_vel_t + ( u_vel * w_vel_x + v_vel * w_vel_y + w_vel * w_vel_z) + p_z - 1 / Re * (w_vel_xx + w_vel_yy + w_vel_zz) continuity = u_vel_x + v_vel_y + w_vel_z return [momentum_x, momentum_y, momentum_z, continuity] def u_func(x): return -a * ( np.exp(a * x[:, 0:1]) * np.sin(a * x[:, 1:2] + d * x[:, 2:3]) + np.exp(a * x[:, 2:3]) * np.cos(a * x[:, 0:1] + d * x[:, 1:2])) * np.exp(-d**2 * x[:, 3:4]) def v_func(x): return -a * ( np.exp(a * x[:, 1:2]) * np.sin(a * x[:, 2:3] + d * x[:, 0:1]) + np.exp(a * x[:, 0:1]) * np.cos(a * x[:, 1:2] + d * x[:, 2:3])) * np.exp(-d**2 * x[:, 3:4]) def w_func(x): return -a * ( np.exp(a * x[:, 2:3]) * np.sin(a * x[:, 0:1] + d * x[:, 1:2]) + np.exp(a * x[:, 1:2]) * np.cos(a * x[:, 2:3] + d * x[:, 0:1])) * np.exp(-d**2 * x[:, 3:4]) def p_func(x): return -1 / 2 * a**2 * (np.exp(2 * a * x[:, 0:1]) + np.exp( 2 * a * x[:, 0:1]) + np.exp(2 * a * x[:, 2:3]) + 2 * np.exp(a * x[:, 0:1] + d * x[:, 1:2]) * np.cos(a * x[:, 2:3] + d * x[:, 0:1]) * np.exp(a * (x[:, 1:2] + x[:, 2:3])) + 2 * np.exp(a * x[:, 1:2] + d * x[:, 2:3]) * np.cos(a * x[:, 0:1] + d * x[:, 1:2]) * np.exp(a * (x[:, 2:3] + x[:, 0:1])) + 2 * np.exp(a * x[:, 2:3] + d * x[:, 0:1]) * np.cos(a * x[:, 1:2] + d * x[:, 2:3]) * np.exp(a * (x[:, 0:1] + x[:, 1:2]))) * np.exp( -2 * d**2 * x[:, 3:4]) spatial_domain = dde.geometry.geometry_3d.Cuboid( xmin=[x_min, y_min, z_min], xmax=[x_max, y_max, z_max]) temporal_domain = dde.geometry.TimeDomain(t_min, t_max) spatio_temporal_domain = dde.geometry.GeometryXTime( spatial_domain, temporal_domain) boundary_condition_u = dde.DirichletBC(spatio_temporal_domain, u_func, lambda _, on_boundary: on_boundary, component=0) boundary_condition_v = dde.DirichletBC(spatio_temporal_domain, v_func, lambda _, on_boundary: on_boundary, component=1) boundary_condition_w = dde.DirichletBC(spatio_temporal_domain, w_func, lambda _, on_boundary: on_boundary, component=2) #boundary_condition_p = dde.DirichletBC(spatio_temporal_domain, p_func, lambda _, on_boundary: on_boundary, component=3) initial_condition_u = dde.IC(spatio_temporal_domain, u_func, lambda _, on_initial: on_initial, component=0) initial_condition_v = dde.IC(spatio_temporal_domain, v_func, lambda _, on_initial: on_initial, component=1) initial_condition_w = dde.IC(spatio_temporal_domain, w_func, lambda _, on_initial: on_initial, component=2) #initial_condition_p = dde.IC(spatio_temporal_domain, p_func, lambda _, on_initial: on_initial, component = 3) data = dde.data.TimePDE( spatio_temporal_domain, pde, [ boundary_condition_u, boundary_condition_v, boundary_condition_w, initial_condition_u, initial_condition_v, initial_condition_w ], num_domain=domain_points, num_boundary=boundary_points, num_initial=initial_points, num_test=test_points) model_name = 'Neural_Networks/d_{}/Beltrami_Flow_Source_Model_d_{}'.format( d, d) net = dde.maps.FNN([4] + hidden_layers * [hidden_units] + [4], "tanh", "Glorot normal") model = dde.Model(data, net) start = time.time() model.compile("adam", lr=learning_rate, loss_weights=[1, 1, 1, 1, 100, 100, 100, 100, 100, 100]) model.train(epochs=number_of_epochs) model.compile("L-BFGS-B", loss_weights=[1, 1, 1, 1, 100, 100, 100, 100, 100, 100]) losshistory, train_state = model.train(model_save_path=model_name) end = time.time() length = end - start x, y, z = np.meshgrid( np.linspace(x_min, x_max, test_points_per_dimension), np.linspace(y_min, y_max, test_points_per_dimension), np.linspace(z_min, z_max, test_points_per_dimension), ) X = np.vstack((np.ravel(x), np.ravel(y), np.ravel(z))).T t_0 = t_min * np.ones(test_points).reshape(test_points, 1) t_1 = t_max * np.ones(test_points).reshape(test_points, 1) X_0 = np.hstack((X, t_0)) X_1 = np.hstack((X, t_1)) output_0 = model.predict(X_0) output_1 = model.predict(X_1) u_pred_0 = output_0[:, 0].reshape(-1) v_pred_0 = output_0[:, 1].reshape(-1) w_pred_0 = output_0[:, 2].reshape(-1) p_pred_0 = output_0[:, 3].reshape(-1) u_exact_0 = u_func(X_0).reshape(-1) v_exact_0 = v_func(X_0).reshape(-1) w_exact_0 = w_func(X_0).reshape(-1) p_exact_0 = p_func(X_0).reshape(-1) u_pred_1 = output_1[:, 0].reshape(-1) v_pred_1 = output_1[:, 1].reshape(-1) w_pred_1 = output_1[:, 2].reshape(-1) p_pred_1 = output_1[:, 3].reshape(-1) u_exact_1 = u_func(X_1).reshape(-1) v_exact_1 = v_func(X_1).reshape(-1) w_exact_1 = w_func(X_1).reshape(-1) p_exact_1 = p_func(X_1).reshape(-1) f_0 = model.predict(X_0, operator=pde) f_1 = model.predict(X_1, operator=pde) l2_difference_u_0 = dde.metrics.l2_relative_error(u_exact_0, u_pred_0) l2_difference_v_0 = dde.metrics.l2_relative_error(v_exact_0, v_pred_0) l2_difference_w_0 = dde.metrics.l2_relative_error(w_exact_0, w_pred_0) l2_difference_p_0 = dde.metrics.l2_relative_error(p_exact_0, p_pred_0) residual_0 = np.mean(np.absolute(f_0)) l2_difference_u_1 = dde.metrics.l2_relative_error(u_exact_1, u_pred_1) l2_difference_v_1 = dde.metrics.l2_relative_error(v_exact_1, v_pred_1) l2_difference_w_1 = dde.metrics.l2_relative_error(w_exact_1, w_pred_1) l2_difference_p_1 = dde.metrics.l2_relative_error(p_exact_1, p_pred_1) residual_1 = np.mean(np.absolute(f_1)) final_epochs = train_state.epoch return l2_difference_u_0, l2_difference_v_0, l2_difference_w_0, l2_difference_p_0, residual_0, l2_difference_u_1, l2_difference_v_1, l2_difference_w_1, l2_difference_p_1, residual_1, length, final_epochs
def main(): """ main function for differential function with q """ # dde.config.real.set_float64() # geometry part tmax = 1 Qmax = 10 / C.sigma Xmin = [] Xmax = [] for i in range(1, C.N + 1): Xmin.append(-Qmax) Xmax.append(Qmax) geom = dde.geometry.Hypercube(Xmin, Xmax) # geom = dde.geometry.Interval(-1, 1) ''' check rho0 for i in range(0,len(ob_y[1,:])): for j in range(0,len(x0[1,:])): plt.plot(x0[:,j],ob_y[:,i],'.') plt.show() ''' timedomain = dde.geometry.TimeDomain(0, tmax) geom = dde.geometry.GeometryXTime(geom, timedomain) # test points setting # x0 = np.random.random([1000,C.N+1]) # x0[:,0:C.N] = 2 * Qmax*(x0[:,0:C.N]-0.5) # ob_y = initialState(x0) # ptset = dde.bc.PointSet(x0) # inside = lambda x, _: ptset.inside(x) # x_initial = np.random.rand(13, C.N+1) # xtest = tf.convert_to_tensor(x_initial) # ytest = np.random.rand(13, C.sizeRho) # ytest = tf.convert_to_tensor(ytest) # Initial conditions ic = [] for j in range(0, (C.N + 1)**2): ic.append( dde.IC(geom, lambda X: initialState(X, j), boundary, component=j)) # test # print(initialState(x_initial,j)) # print(SLE_q(xtest,ytest)) # bc = dde.DirichletBC(geom,lambda _: 0, lambda _, on_boundary: on_boundary) # ic.append(bc) # data data = dde.data.TimePDE(geom, lambda x, y: SLE_q(x, y, C), ic, num_domain=2000, num_boundary=0, num_initial=500, num_test=None) # settings for model layer_size = [C.N + 1] + [100] * 5 + [(C.N + 1)**2] activation = "tanh" initializer = "Glorot uniform" save_model_dir = os.path.join(os.getcwd(), 'Model_save', 'test_rho0_0102') if not os.path.isdir(save_model_dir): os.mkdir(save_model_dir) save_model_name = os.path.join(save_model_dir, 'model_save') load_epoch = '15000' load_model_name = save_model_name + '-' + load_epoch Callfcn = dde.callbacks.ModelCheckpoint(save_model_name, verbose=1, save_better_only=True, period=10000) # initialize model net = dde.maps.FNN(layer_size, activation, initializer) net.apply_output_transform(boundary_condition) model = dde.Model(data, net) model.compile("adam", lr=0.001) model.restore(load_model_name) losshistory, train_state = model.train(epochs=60000, callbacks=[Callfcn], model_save_path=save_model_name) dde.saveplot(losshistory, train_state, issave=True, isplot=True)
# Definition of the Dirichlet BCs bc_u_no_slip = dde.DirichletBC(geomtime, zero_velocity, zero_boundary, component=0) bc_v_no_slip = dde.DirichletBC(geomtime, zero_velocity, zero_boundary, component=1) bc_u_cylinder = dde.DirichletBC(geomtime, zero_velocity, cylinder, component=0) bc_v_cylinder = dde.DirichletBC(geomtime, zero_velocity, cylinder, component=1) bc_u_inlet = dde.DirichletBC(geomtime, inlet_velocity, inlet, component=0) bc_v_inlet = dde.DirichletBC(geomtime, inlet_velocity, inlet, component=1) bc_p = dde.DirichletBC(geomtime, zero_pressure, outlet, component=2) ic = dde.IC(geomtime, zero_velocity, on_initial) bc = [ bc_u_no_slip, bc_v_no_slip, bc_u_cylinder, bc_v_cylinder, bc_u_inlet, bc_v_inlet, bc_p, ic ] # Domain sampling and data generation data = dde.data.TimePDE(geomtime, pde, bc, num_domain=Nd, num_boundary=Nb, num_initial=Nt) # Definition of the FFNN architecture
def main(): data = get_covid_data() #sir params beta = tf.Variable(.0001) gamma = tf.Variable(.0002) N = data[1][0][0] + data[1][0][1] #population initial_infected = data[1][0][1] initial_removed = data[1][0][2] #x is time, y is odes def odes(x, y): s, i, r = y[:, 0:1], y[:, 1:2], y[:, 2:3] ds_x = tf.gradients(s, x)[0] di_x = tf.gradients(i, x)[0] dr_x = tf.gradients(r, x)[0] de1 = ds_x + abs(beta) * i * s / N de2 = di_x - abs(beta) * i * s / N + (abs(gamma) * i) de3 = dr_x - abs(gamma) * i return [de1, de2, de3] def boundary(_, on_initial): return on_initial def func(x): return np.hstack( (np.zeros(x.shape), np.zeros(x.shape), np.zeros(x.shape))) geom = dde.geometry.TimeDomain(0, len(data[0])) ic1 = dde.IC(geom, lambda x: (N - initial_infected - initial_removed) * np.ones(x.shape), boundary, component=0) ic2 = dde.IC(geom, lambda x: initial_infected * np.ones(x.shape), boundary, component=1) ic3 = dde.IC(geom, lambda x: initial_removed * np.ones(x.shape), boundary, component=2) observe_t, observe_y = data ptset = dde.bc.PointSet(observe_t) inside = lambda x, _: ptset.inside(x) observe_y0 = dde.DirichletBC(geom, ptset.values_to_func(observe_y[:, 0:1]), inside, component=0) observe_y1 = dde.DirichletBC(geom, ptset.values_to_func(observe_y[:, 1:2]), inside, component=1) observe_y2 = dde.DirichletBC(geom, ptset.values_to_func(observe_y[:, 2:3]), inside, component=2) data = dde.data.PDE(geom, odes, [ic2, ic3, observe_y1, observe_y2], num_domain=300, num_boundary=1, anchors=observe_t) net = dde.maps.FNN([1] + [40] * 4 + [3], 'tanh', 'Glorot uniform') model = dde.Model(data, net) model.compile("adam", lr=.01, loss='mse') # try 'log cosh' variable = dde.callbacks.VariableValue([beta, gamma], period=1000, filename="variables.dat") losshistory, train_state = model.train(epochs=10000, callbacks=[variable]) plot_best_state(train_state) plot_loss_history(losshistory) plt.show()
def visualize_solution(d, epochs_source): def pde(x, u): u_vel, v_vel, w_vel, p = u[:, 0:1], u[:, 1:2], u[:, 2:3], u[:, 3:4] u_vel_x = dde.grad.jacobian(u, x, i=0, j=0) u_vel_y = dde.grad.jacobian(u, x, i=0, j=1) u_vel_z = dde.grad.jacobian(u, x, i=0, j=2) u_vel_t = dde.grad.jacobian(u, x, i=0, j=3) u_vel_xx = dde.grad.hessian(u, x, component=0, i=0, j=0) u_vel_yy = dde.grad.hessian(u, x, component=0, i=1, j=1) u_vel_zz = dde.grad.hessian(u, x, component=0, i=2, j=2) v_vel_x = dde.grad.jacobian(u, x, i=1, j=0) v_vel_y = dde.grad.jacobian(u, x, i=1, j=1) v_vel_z = dde.grad.jacobian(u, x, i=1, j=2) v_vel_t = dde.grad.jacobian(u, x, i=1, j=3) v_vel_xx = dde.grad.hessian(u, x, component=1, i=0, j=0) v_vel_yy = dde.grad.hessian(u, x, component=1, i=1, j=1) v_vel_zz = dde.grad.hessian(u, x, component=1, i=2, j=2) w_vel_x = dde.grad.jacobian(u, x, i=2, j=0) w_vel_y = dde.grad.jacobian(u, x, i=2, j=1) w_vel_z = dde.grad.jacobian(u, x, i=2, j=2) w_vel_t = dde.grad.jacobian(u, x, i=2, j=3) w_vel_xx = dde.grad.hessian(u, x, component=2, i=0, j=0) w_vel_yy = dde.grad.hessian(u, x, component=2, i=1, j=1) w_vel_zz = dde.grad.hessian(u, x, component=2, i=2, j=2) p_x = dde.grad.jacobian(u, x, i=3, j=0) p_y = dde.grad.jacobian(u, x, i=3, j=1) p_z = dde.grad.jacobian(u, x, i=3, j=2) momentum_x = u_vel_t + ( u_vel * u_vel_x + v_vel * u_vel_y + w_vel * u_vel_z) + p_x - 1 / Re * (u_vel_xx + u_vel_yy + u_vel_zz) momentum_y = v_vel_t + ( u_vel * v_vel_x + v_vel * v_vel_y + w_vel * v_vel_z) + p_y - 1 / Re * (v_vel_xx + v_vel_yy + v_vel_zz) momentum_z = w_vel_t + ( u_vel * w_vel_x + v_vel * w_vel_y + w_vel * w_vel_z) + p_z - 1 / Re * (w_vel_xx + w_vel_yy + w_vel_zz) continuity = u_vel_x + v_vel_y + w_vel_z return [momentum_x, momentum_y, momentum_z, continuity] def u_func(x): return -a * ( np.exp(a * x[:, 0:1]) * np.sin(a * x[:, 1:2] + d * x[:, 2:3]) + np.exp(a * x[:, 2:3]) * np.cos(a * x[:, 0:1] + d * x[:, 1:2])) * np.exp(-d**2 * x[:, 3:4]) def v_func(x): return -a * ( np.exp(a * x[:, 1:2]) * np.sin(a * x[:, 2:3] + d * x[:, 0:1]) + np.exp(a * x[:, 0:1]) * np.cos(a * x[:, 1:2] + d * x[:, 2:3])) * np.exp(-d**2 * x[:, 3:4]) def w_func(x): return -a * ( np.exp(a * x[:, 2:3]) * np.sin(a * x[:, 0:1] + d * x[:, 1:2]) + np.exp(a * x[:, 1:2]) * np.cos(a * x[:, 2:3] + d * x[:, 0:1])) * np.exp(-d**2 * x[:, 3:4]) def p_func(x): return -1 / 2 * a**2 * (np.exp(2 * a * x[:, 0:1]) + np.exp( 2 * a * x[:, 0:1]) + np.exp(2 * a * x[:, 2:3]) + 2 * np.exp(a * x[:, 0:1] + d * x[:, 1:2]) * np.cos(a * x[:, 2:3] + d * x[:, 0:1]) * np.exp(a * (x[:, 1:2] + x[:, 2:3])) + 2 * np.exp(a * x[:, 1:2] + d * x[:, 2:3]) * np.cos(a * x[:, 0:1] + d * x[:, 1:2]) * np.exp(a * (x[:, 2:3] + x[:, 0:1])) + 2 * np.exp(a * x[:, 2:3] + d * x[:, 0:1]) * np.cos(a * x[:, 1:2] + d * x[:, 2:3]) * np.exp(a * (x[:, 0:1] + x[:, 1:2]))) * np.exp( -2 * d**2 * x[:, 3:4]) spatial_domain = dde.geometry.geometry_3d.Cuboid( xmin=[x_min, y_min, z_min], xmax=[x_max, y_max, z_max]) temporal_domain = dde.geometry.TimeDomain(t_min, t_max) spatio_temporal_domain = dde.geometry.GeometryXTime( spatial_domain, temporal_domain) boundary_condition_u = dde.DirichletBC(spatio_temporal_domain, u_func, lambda _, on_boundary: on_boundary, component=0) boundary_condition_v = dde.DirichletBC(spatio_temporal_domain, v_func, lambda _, on_boundary: on_boundary, component=1) boundary_condition_w = dde.DirichletBC(spatio_temporal_domain, w_func, lambda _, on_boundary: on_boundary, component=2) #boundary_condition_p = dde.DirichletBC(spatio_temporal_domain, p_func, lambda _, on_boundary: on_boundary, component=3) initial_condition_u = dde.IC(spatio_temporal_domain, u_func, lambda _, on_initial: on_initial, component=0) initial_condition_v = dde.IC(spatio_temporal_domain, v_func, lambda _, on_initial: on_initial, component=1) initial_condition_w = dde.IC(spatio_temporal_domain, w_func, lambda _, on_initial: on_initial, component=2) #initial_condition_p = dde.IC(spatio_temporal_domain, p_func, lambda _, on_initial: on_initial, component = 3) data = dde.data.TimePDE( spatio_temporal_domain, pde, [ boundary_condition_u, boundary_condition_v, boundary_condition_w, initial_condition_u, initial_condition_v, initial_condition_w ], num_domain=domain_points, num_boundary=boundary_points, num_initial=initial_points, num_test=test_points) net = dde.maps.FNN([4] + hidden_layers * [hidden_units] + [4], "tanh", "Glorot normal") model = dde.Model(data, net) # Compute reference solution x, y, z = np.meshgrid( np.linspace(x_min, x_max, test_points_per_dimension), np.linspace(y_min, y_max, test_points_per_dimension), np.linspace(z_min, z_max, test_points_per_dimension), ) X = np.vstack((np.ravel(x), np.ravel(y), np.ravel(z))).T t_0 = t_min * np.ones(test_points).reshape(test_points, 1) t_1 = t_max * np.ones(test_points).reshape(test_points, 1) X_0 = np.hstack((X, t_0)) X_1 = np.hstack((X, t_1)) # Reload model and make predictions model_name = 'Neural_Networks/d_{}/Beltrami_Flow_Source_Model_d_{}-{}'.format( d, d, epochs_source) model.compile("adam", lr=learning_rate) model.train(model_restore_path=model_name, epochs=0) output_0 = model.predict(X_0) output_1 = model.predict(X_1) u_pred_0 = output_0[:, 0].reshape(test_points_per_dimension, test_points_per_dimension, test_points_per_dimension) v_pred_0 = output_0[:, 1].reshape(test_points_per_dimension, test_points_per_dimension, test_points_per_dimension) w_pred_0 = output_0[:, 2].reshape(test_points_per_dimension, test_points_per_dimension, test_points_per_dimension) p_pred_0 = output_0[:, 3].reshape(test_points_per_dimension, test_points_per_dimension, test_points_per_dimension) u_pred_1 = output_1[:, 0].reshape(test_points_per_dimension, test_points_per_dimension, test_points_per_dimension) v_pred_1 = output_1[:, 1].reshape(test_points_per_dimension, test_points_per_dimension, test_points_per_dimension) w_pred_1 = output_1[:, 2].reshape(test_points_per_dimension, test_points_per_dimension, test_points_per_dimension) p_pred_1 = output_1[:, 3].reshape(test_points_per_dimension, test_points_per_dimension, test_points_per_dimension) plt.figure(0) ax = plt.axes(projection="3d") ax.plot_wireframe(x[:, :, z_index], y[:, :, z_index], u_pred_0[:, :, z_index]) ax.set_xlabel('location x') ax.set_ylabel('location y') ax.set_zlabel('u') plt.tight_layout() plt.savefig('Predictions/Predicted_solution_u_d_{}_t_0.png'.format(d), dpi=300) plt.figure(1) ax = plt.axes(projection="3d") ax.plot_wireframe(x[:, :, z_index], y[:, :, z_index], u_pred_1[:, :, z_index]) ax.set_xlabel('location x') ax.set_ylabel('location y') ax.set_zlabel('u') plt.tight_layout() plt.savefig('Predictions/Predicted_solution_u_d_{}_t_1.png'.format(d), dpi=300) plt.figure(2) ax = plt.axes(projection="3d") ax.plot_wireframe(x[:, :, z_index], y[:, :, z_index], v_pred_0[:, :, z_index]) ax.set_xlabel('location x') ax.set_ylabel('location y') ax.set_zlabel('v') plt.tight_layout() plt.savefig('Predictions/Predicted_solution_v_d_{}_t_0.png'.format(d), dpi=300) plt.figure(3) ax = plt.axes(projection="3d") ax.plot_wireframe(x[:, :, z_index], y[:, :, z_index], v_pred_1[:, :, z_index]) ax.set_xlabel('location x') ax.set_ylabel('location y') ax.set_zlabel('v') plt.tight_layout() plt.savefig('Predictions/Predicted_solution_v_d_{}_t_1.png'.format(d), dpi=300) plt.figure(4) ax = plt.axes(projection="3d") ax.plot_wireframe(x[:, :, z_index], y[:, :, z_index], w_pred_0[:, :, z_index]) ax.set_xlabel('location x') ax.set_ylabel('location y') ax.set_zlabel('w') plt.tight_layout() plt.savefig('Predictions/Predicted_solution_w_d_{}_t_0.png'.format(d), dpi=300) plt.figure(5) ax = plt.axes(projection="3d") ax.plot_wireframe(x[:, :, z_index], y[:, :, z_index], w_pred_1[:, :, z_index]) ax.set_xlabel('location x') ax.set_ylabel('location y') ax.set_zlabel('w') plt.tight_layout() plt.savefig('Predictions/Predicted_solution_w_d_{}_t_1.png'.format(d), dpi=300) plt.figure(6) ax = plt.axes(projection="3d") ax.plot_wireframe(x[:, :, z_index], y[:, :, z_index], p_pred_0[:, :, z_index]) ax.set_xlabel('location x') ax.set_ylabel('location y') ax.set_zlabel('p') plt.tight_layout() plt.savefig('Predictions/Predicted_solution_p_d_{}_t_0.png'.format(d), dpi=300) plt.figure(7) ax = plt.axes(projection="3d") ax.plot_wireframe(x[:, :, z_index], y[:, :, z_index], p_pred_1[:, :, z_index]) ax.set_xlabel('location x') ax.set_ylabel('location y') ax.set_zlabel('p') plt.tight_layout() plt.savefig('Predictions/Predicted_solution_p_d_{}_t_1.png'.format(d), dpi=300) return
def train_target_model(nus_source, epochs_source, nu_target, optimization_strategy): individual_similarities = np.zeros(len(nus_source)) for k in range(len(nus_source)): individual_similarities[k] = np.linalg.norm( nu_target - nus_source[k]) / np.linalg.norm(nu_target) index = np.unravel_index(np.argmin(individual_similarities, axis=None), individual_similarities.shape)[0] similarity = individual_similarities[index] closest_nu = nus_source[index] file_name = 'Reference_Solutions/u_exact_nu_{}.mat'.format(nu_target) data = scipy.io.loadmat(file_name) u_exact = data['usol'].T x_test, t_test = np.meshgrid(np.linspace(x_min, x_max, test_points_x), np.linspace(t_min, t_max, test_points_t)) X = np.vstack((np.ravel(x_test), np.ravel(t_test))).T def pde(x, u): u_x = dde.grad.jacobian(u, x, i=0, j=0) u_t = dde.grad.jacobian(u, x, i=0, j=1) u_xx = dde.grad.hessian(u, x, i=0, j=0) return u_t + u * u_x - nu_target * u_xx spatial_domain = dde.geometry.Interval(x_min, x_max) temporal_domain = dde.geometry.TimeDomain(t_min, t_max) spatio_temporal_domain = dde.geometry.GeometryXTime( spatial_domain, temporal_domain) boundary_condition = dde.DirichletBC(spatio_temporal_domain, lambda x: 0, lambda _, on_boundary: on_boundary) initial_condition = dde.IC(spatio_temporal_domain, lambda x: -np.sin(np.pi * x[:, 0:1]), lambda _, on_initial: on_initial) data = dde.data.TimePDE(spatio_temporal_domain, pde, [boundary_condition, initial_condition], num_domain=domain_points, num_boundary=boundary_points, num_initial=initial_points, num_test=test_points) net = dde.maps.FNN([2] + hidden_layers * [hidden_units] + [1], "tanh", "Glorot normal") model = dde.Model(data, net) start = time.time() if optimization_strategy == 0: print("L-BFGS (random)") model.compile("L-BFGS-B") model.train() elif optimization_strategy == 1: print("L-BFGS (smart)") model_name_source = '../Source/Neural_Networks/nu_{}/Burger_Equation_Source_Model_nu_{}-{}'.format( closest_nu, closest_nu, epochs_source[index]) model.compile("L-BFGS-B") model.train(model_restore_path=model_name_source) else: print("Adam + L-BFGS (random)") model.compile("adam", lr=learning_rate) model.train(epochs=number_of_epochs) model.compile("L-BFGS-B") model.train() end = time.time() length = end - start u_pred = model.predict(X).reshape(test_points_t, test_points_x) f = model.predict(X, operator=pde) residual = np.mean(np.absolute(f)) l2_difference = dde.metrics.l2_relative_error(u_exact, u_pred) return l2_difference, residual, similarity, length
def check_x0(): """ main function for differential function with q """ # dde.config.real.set_float64() # geometry part tmax = 1 Qmax = 10 / C.sigma Xmin = [] Xmax = [] for i in range(1, C.N + 1): Xmin.append(-Qmax) Xmax.append(Qmax) # x0 = np.random.random([1000,C.N+1]) x0 = np.load('./rand_x0.npy') xtest = np.random.random([10000, C.N + 1]) ytest = initialState(xtest) ob_y = initialState(x0) geom = dde.geometry.Hypercube(Xmin, Xmax) # geom = dde.geometry.Interval(-1, 1) ''' check rho0 for i in range(0,len(ob_y[1,:])): for j in range(0,len(x0[1,:])): plt.plot(x0[:,j],ob_y[:,i],'.') plt.show() ''' timedomain = dde.geometry.TimeDomain(0, tmax) geom = dde.geometry.GeometryXTime(geom, timedomain) # Initial conditions ic = [] ic_nb = [] ptset = dde.bc.PointSet(x0) inside = lambda x, _: ptset.inside(x) for j in range(0, (C.N + 1)**2): ic.append( dde.IC(geom, lambda X: initialState(X, j), boundary, component=j)) ic.append( dde.DirichletBC(geom, ptset.values_to_func(ob_y[:, j:j + 1]), inside, component=j)) ic_nb.append( dde.IC(geom, lambda X: initialState(X, j), boundary, component=j)) # test # print(initialState(x_initial,j)) # print(SLE_q(xtest,ytest)) # bc = dde.DirichletBC(geom,lambda _: 0, lambda _, on_boundary: on_boundary) # ic.append(bc) # data data_sle = dde.data.TimePDE(geom, lambda x, y: SLE_q(x, y, C), ic_nb, num_domain=1000, num_boundary=0, num_initial=100, num_test=None) data = dde.data.TimePDE(geom, lambda x, y: y * 0, ic, num_domain=0, num_boundary=0, num_initial=100, anchors=x0, num_test=None) # lambda x,y: SLE_q(x,y,C), # settings for model # layer_size = [C.N+1] + [100] * 5 + [(C.N+1) **2] layer_size = [C.N + 1] + [50] * 3 + [(C.N + 1)**2] activation = "tanh" initializer = "Glorot uniform" save_model_dir = os.path.join(os.getcwd(), 'Model_save', 'test_rho0_0101') if not os.path.isdir(save_model_dir): os.mkdir(save_model_dir) save_model_name = os.path.join(save_model_dir, 'model_test') load_epoch = '65000' load_model_name = save_model_name + '-' + load_epoch Callfcn = dde.callbacks.ModelCheckpoint(save_model_name, verbose=1, save_better_only=True, period=10000) # initialize model net = dde.maps.FNN(layer_size, activation, initializer) net.apply_output_transform(boundary_condition) model = dde.Model(data, net) # load model model.compile("adam", lr=0.001) model.restore(load_model_name) y_pre = model.predict(x0) x1 = x0 x1[:, 1] = x1[:, 1] * 0 # x1[:,2]=x1[:,2]*0 y_pre2 = model.predict(x1) # load_model_name = save_model_name + '-' + '5000' # model.restore(load_model_name) # y_pre = model.predict(x0) for i in range(0, 9): plt.subplot(3, 3, i + 1) plt.plot(x0[:, 0], ob_y[:, i], '.b') plt.plot(x0[:, 0], y_pre[:, i], '.r') plt.plot(x1[:, 0], y_pre2[:, i], '.g') plt.show()
def train_target_model(ds_source, epochs_source, d_target, optimization_strategy): individual_similarities = np.zeros(len(ds_source)) for k in range(len(ds_source)): individual_similarities[k] = 1 - np.linalg.norm( d_target - ds_source[k]) / np.linalg.norm(d_target) index = np.unravel_index(np.argmax(individual_similarities, axis=None), individual_similarities.shape)[0] similarity = individual_similarities[index] closest_d = ds_source[index] def pde(x, u): u_vel, v_vel, w_vel, p = u[:, 0:1], u[:, 1:2], u[:, 2:3], u[:, 3:4] u_vel_x = dde.grad.jacobian(u, x, i=0, j=0) u_vel_y = dde.grad.jacobian(u, x, i=0, j=1) u_vel_z = dde.grad.jacobian(u, x, i=0, j=2) u_vel_t = dde.grad.jacobian(u, x, i=0, j=3) u_vel_xx = dde.grad.hessian(u, x, component=0, i=0, j=0) u_vel_yy = dde.grad.hessian(u, x, component=0, i=1, j=1) u_vel_zz = dde.grad.hessian(u, x, component=0, i=2, j=2) v_vel_x = dde.grad.jacobian(u, x, i=1, j=0) v_vel_y = dde.grad.jacobian(u, x, i=1, j=1) v_vel_z = dde.grad.jacobian(u, x, i=1, j=2) v_vel_t = dde.grad.jacobian(u, x, i=1, j=3) v_vel_xx = dde.grad.hessian(u, x, component=1, i=0, j=0) v_vel_yy = dde.grad.hessian(u, x, component=1, i=1, j=1) v_vel_zz = dde.grad.hessian(u, x, component=1, i=2, j=2) w_vel_x = dde.grad.jacobian(u, x, i=2, j=0) w_vel_y = dde.grad.jacobian(u, x, i=2, j=1) w_vel_z = dde.grad.jacobian(u, x, i=2, j=2) w_vel_t = dde.grad.jacobian(u, x, i=2, j=3) w_vel_xx = dde.grad.hessian(u, x, component=2, i=0, j=0) w_vel_yy = dde.grad.hessian(u, x, component=2, i=1, j=1) w_vel_zz = dde.grad.hessian(u, x, component=2, i=2, j=2) p_x = dde.grad.jacobian(u, x, i=3, j=0) p_y = dde.grad.jacobian(u, x, i=3, j=1) p_z = dde.grad.jacobian(u, x, i=3, j=2) momentum_x = u_vel_t + ( u_vel * u_vel_x + v_vel * u_vel_y + w_vel * u_vel_z) + p_x - 1 / Re * (u_vel_xx + u_vel_yy + u_vel_zz) momentum_y = v_vel_t + ( u_vel * v_vel_x + v_vel * v_vel_y + w_vel * v_vel_z) + p_y - 1 / Re * (v_vel_xx + v_vel_yy + v_vel_zz) momentum_z = w_vel_t + ( u_vel * w_vel_x + v_vel * w_vel_y + w_vel * w_vel_z) + p_z - 1 / Re * (w_vel_xx + w_vel_yy + w_vel_zz) continuity = u_vel_x + v_vel_y + w_vel_z return [momentum_x, momentum_y, momentum_z, continuity] def u_func(x): return -a * (np.exp(a * x[:, 0:1]) * np.sin(a * x[:, 1:2] + d_target * x[:, 2:3]) + np.exp(a * x[:, 2:3]) * np.cos(a * x[:, 0:1] + d_target * x[:, 1:2])) * np.exp( -d_target**2 * x[:, 3:4]) def v_func(x): return -a * (np.exp(a * x[:, 1:2]) * np.sin(a * x[:, 2:3] + d_target * x[:, 0:1]) + np.exp(a * x[:, 0:1]) * np.cos(a * x[:, 1:2] + d_target * x[:, 2:3])) * np.exp( -d_target**2 * x[:, 3:4]) def w_func(x): return -a * (np.exp(a * x[:, 2:3]) * np.sin(a * x[:, 0:1] + d_target * x[:, 1:2]) + np.exp(a * x[:, 1:2]) * np.cos(a * x[:, 2:3] + d_target * x[:, 0:1])) * np.exp( -d_target**2 * x[:, 3:4]) def p_func(x): return -1 / 2 * a**2 * ( np.exp(2 * a * x[:, 0:1]) + np.exp(2 * a * x[:, 0:1]) + np.exp(2 * a * x[:, 2:3]) + 2 * np.exp(a * x[:, 0:1] + d_target * x[:, 1:2]) * np.cos(a * x[:, 2:3] + d_target * x[:, 0:1]) * np.exp(a * (x[:, 1:2] + x[:, 2:3])) + 2 * np.exp(a * x[:, 1:2] + d_target * x[:, 2:3]) * np.cos(a * x[:, 0:1] + d_target * x[:, 1:2]) * np.exp(a * (x[:, 2:3] + x[:, 0:1])) + 2 * np.exp(a * x[:, 2:3] + d_target * x[:, 0:1]) * np.cos(a * x[:, 1:2] + d_target * x[:, 2:3]) * np.exp(a * (x[:, 0:1] + x[:, 1:2]))) * np.exp( -2 * d_target**2 * x[:, 3:4]) spatial_domain = dde.geometry.geometry_3d.Cuboid( xmin=[x_min, y_min, z_min], xmax=[x_max, y_max, z_max]) temporal_domain = dde.geometry.TimeDomain(t_min, t_max) spatio_temporal_domain = dde.geometry.GeometryXTime( spatial_domain, temporal_domain) boundary_condition_u = dde.DirichletBC(spatio_temporal_domain, u_func, lambda _, on_boundary: on_boundary, component=0) boundary_condition_v = dde.DirichletBC(spatio_temporal_domain, v_func, lambda _, on_boundary: on_boundary, component=1) boundary_condition_w = dde.DirichletBC(spatio_temporal_domain, w_func, lambda _, on_boundary: on_boundary, component=2) #boundary_condition_p = dde.DirichletBC(spatio_temporal_domain, p_func, lambda _, on_boundary: on_boundary, component=3) initial_condition_u = dde.IC(spatio_temporal_domain, u_func, lambda _, on_initial: on_initial, component=0) initial_condition_v = dde.IC(spatio_temporal_domain, v_func, lambda _, on_initial: on_initial, component=1) initial_condition_w = dde.IC(spatio_temporal_domain, w_func, lambda _, on_initial: on_initial, component=2) #initial_condition_p = dde.IC(spatio_temporal_domain, p_func, lambda _, on_initial: on_initial, component = 3) data = dde.data.TimePDE( spatio_temporal_domain, pde, [ boundary_condition_u, boundary_condition_v, boundary_condition_w, initial_condition_u, initial_condition_v, initial_condition_w ], num_domain=domain_points, num_boundary=boundary_points, num_initial=initial_points, num_test=test_points) net = dde.maps.FNN([4] + hidden_layers * [hidden_units] + [4], "tanh", "Glorot normal") model = dde.Model(data, net) start = time.time() if optimization_strategy == 0: print("L-BFGS (random)") model.compile("L-BFGS-B", loss_weights=[1, 1, 1, 1, 100, 100, 100, 100, 100, 100]) model.train() elif optimization_strategy == 1: print("L-BFGS (smart)") model_name_source = '../Source/Neural_Networks/d_{}/Beltrami_Flow_Source_Model_d_{}-{}'.format( closest_d, closest_d, epochs_source[index]) model.compile("L-BFGS-B", loss_weights=[1, 1, 1, 1, 100, 100, 100, 100, 100, 100]) model.train(model_restore_path=model_name_source) else: print("Adam + L-BFGS (random)") model.compile("adam", lr=learning_rate, loss_weights=[1, 1, 1, 1, 100, 100, 100, 100, 100, 100]) model.train(epochs=number_of_epochs) model.compile("L-BFGS-B", loss_weights=[1, 1, 1, 1, 100, 100, 100, 100, 100, 100]) model.train() end = time.time() length = end - start x, y, z = np.meshgrid( np.linspace(x_min, x_max, test_points_per_dimension), np.linspace(y_min, y_max, test_points_per_dimension), np.linspace(z_min, z_max, test_points_per_dimension), ) X = np.vstack((np.ravel(x), np.ravel(y), np.ravel(z))).T t_0 = t_min * np.ones(test_points).reshape(test_points, 1) t_1 = t_max * np.ones(test_points).reshape(test_points, 1) X_0 = np.hstack((X, t_0)) X_1 = np.hstack((X, t_1)) output_0 = model.predict(X_0) output_1 = model.predict(X_1) u_pred_0 = output_0[:, 0].reshape(-1) v_pred_0 = output_0[:, 1].reshape(-1) w_pred_0 = output_0[:, 2].reshape(-1) p_pred_0 = output_0[:, 3].reshape(-1) u_exact_0 = u_func(X_0).reshape(-1) v_exact_0 = v_func(X_0).reshape(-1) w_exact_0 = w_func(X_0).reshape(-1) p_exact_0 = p_func(X_0).reshape(-1) u_pred_1 = output_1[:, 0].reshape(-1) v_pred_1 = output_1[:, 1].reshape(-1) w_pred_1 = output_1[:, 2].reshape(-1) p_pred_1 = output_1[:, 3].reshape(-1) u_exact_1 = u_func(X_1).reshape(-1) v_exact_1 = v_func(X_1).reshape(-1) w_exact_1 = w_func(X_1).reshape(-1) p_exact_1 = p_func(X_1).reshape(-1) f_0 = model.predict(X_0, operator=pde) f_1 = model.predict(X_1, operator=pde) l2_difference_u_0 = dde.metrics.l2_relative_error(u_exact_0, u_pred_0) l2_difference_v_0 = dde.metrics.l2_relative_error(v_exact_0, v_pred_0) l2_difference_w_0 = dde.metrics.l2_relative_error(w_exact_0, w_pred_0) l2_difference_p_0 = dde.metrics.l2_relative_error(p_exact_0, p_pred_0) residual_0 = np.mean(np.absolute(f_0)) l2_difference_u_1 = dde.metrics.l2_relative_error(u_exact_1, u_pred_1) l2_difference_v_1 = dde.metrics.l2_relative_error(v_exact_1, v_pred_1) l2_difference_w_1 = dde.metrics.l2_relative_error(w_exact_1, w_pred_1) l2_difference_p_1 = dde.metrics.l2_relative_error(p_exact_1, p_pred_1) residual_1 = np.mean(np.absolute(f_1)) return l2_difference_u_0, l2_difference_v_0, l2_difference_w_0, l2_difference_p_0, residual_0, l2_difference_u_1, l2_difference_v_1, l2_difference_w_1, l2_difference_p_1, residual_1, similarity, length
return [dy1_x - y2, dy2_x + y1] def boundary(_, on_initial): return on_initial def func(x): """ y1 = sin(x) y2 = cos(x) """ return np.hstack((np.sin(x), np.cos(x))) geom = dde.geometry.TimeDomain(0, 10) ic1 = dde.IC(geom, np.sin, boundary, component=0) ic2 = dde.IC(geom, np.cos, boundary, component=1) data = dde.data.PDE(geom, ode_system, [ic1, ic2], 35, 2, solution=func, num_test=100) layer_size = [1] + [50] * 3 + [2] activation = "tanh" initializer = "Glorot uniform" net = dde.maps.FNN(layer_size, activation, initializer) model = dde.Model(data, net) model.compile("adam", lr=0.001, metrics=["l2 relative error"]) losshistory, train_state = model.train(epochs=20000) dde.saveplot(losshistory, train_state, issave=True, isplot=True)
def main(): # For starters, define some (dimensionless) parameters a = 1 d = 1 Re = 1 # Introduce the span of the geometric domain dx = 2 dy = 0.3 dz = 0.5 dt = 2e-3 def pde(x, u): u_vel, v_vel, w_vel, p = u[:, 0:1], u[:, 1:2], u[:, 2:3], u[:, 3:4] # Define the (partial) derivatives of the variables in a way that tensorflow understands # We query corresponding entries of the jacobian and hessian and map them to new variables # u # First derivatives u_vel_x = dde.grad.jacobian(u, x, i=0, j=0) u_vel_y = dde.grad.jacobian(u, x, i=0, j=1) u_vel_z = dde.grad.jacobian(u, x, i=0, j=2) u_vel_t = dde.grad.jacobian(u, x, i=0, j=3) # Second derivatives u_vel_xx = dde.grad.hessian(u, x, component=0, i=0, j=0) u_vel_yy = dde.grad.hessian(u, x, component=0, i=1, j=1) u_vel_zz = dde.grad.hessian(u, x, component=0, i=2, j=2) # v # First derivatives v_vel_x = dde.grad.jacobian(u, x, i=1, j=0) v_vel_y = dde.grad.jacobian(u, x, i=1, j=1) v_vel_z = dde.grad.jacobian(u, x, i=1, j=2) v_vel_t = dde.grad.jacobian(u, x, i=1, j=3) # Second derivatives v_vel_xx = dde.grad.hessian(u, x, component=1, i=0, j=0) v_vel_yy = dde.grad.hessian(u, x, component=1, i=1, j=1) v_vel_zz = dde.grad.hessian(u, x, component=1, i=2, j=2) # w # First derivatives w_vel_x = dde.grad.jacobian(u, x, i=2, j=0) w_vel_y = dde.grad.jacobian(u, x, i=2, j=1) w_vel_z = dde.grad.jacobian(u, x, i=2, j=2) w_vel_t = dde.grad.jacobian(u, x, i=2, j=3) # Second derivatives w_vel_xx = dde.grad.hessian(u, x, component=2, i=0, j=0) w_vel_yy = dde.grad.hessian(u, x, component=2, i=1, j=1) w_vel_zz = dde.grad.hessian(u, x, component=2, i=2, j=2) # pressure p_x = dde.grad.jacobian(u, x, i=3, j=0) p_y = dde.grad.jacobian(u, x, i=3, j=1) p_z = dde.grad.jacobian(u, x, i=3, j=2) # Assemble the momentum equations momentum_x = (u_vel_t + (u_vel * u_vel_x + v_vel * u_vel_y + w_vel * u_vel_z) + p_x - 1 / Re * (u_vel_xx + u_vel_yy + u_vel_zz)) momentum_y = (v_vel_t + (u_vel * v_vel_x + v_vel * v_vel_y + w_vel * v_vel_z) + p_y - 1 / Re * (v_vel_xx + v_vel_yy + v_vel_zz)) momentum_z = (w_vel_t + (u_vel * w_vel_x + v_vel * w_vel_y + w_vel * w_vel_z) + p_z - 1 / Re * (w_vel_xx + w_vel_yy + w_vel_zz)) # Assemble continuity equation continuity = u_vel_x + v_vel_y + w_vel_z return [momentum_x, momentum_y, momentum_z, continuity] # Make a simple cubic domain spatial_domain = dde.geometry.Cuboid(xmin=[0, 0, 0], xmax=[dx, dy, dz]) # Specify the time domain with end time: 2 mm / (1000 mm/s) = 0.002 s temporal_domain = dde.geometry.TimeDomain(0, dt) spatio_temporal_domain = dde.geometry.GeometryXTime( spatial_domain, temporal_domain) # Define functions for the boundary conditions def u_func(x): return ( -a * (np.exp(a * x[:, 0:1]) * np.sin(a * x[:, 1:2] + d * x[:, 2:3]) + np.exp(a * x[:, 2:3]) * np.cos(a * x[:, 0:1] + d * x[:, 1:2])) * np.exp(-(d**2) * x[:, 3:4])) def v_func(x): return ( -a * (np.exp(a * x[:, 1:2]) * np.sin(a * x[:, 2:3] + d * x[:, 0:1]) + np.exp(a * x[:, 0:1]) * np.cos(a * x[:, 1:2] + d * x[:, 2:3])) * np.exp(-(d**2) * x[:, 3:4])) def w_func(x): return ( -a * (np.exp(a * x[:, 2:3]) * np.sin(a * x[:, 0:1] + d * x[:, 1:2]) + np.exp(a * x[:, 1:2]) * np.cos(a * x[:, 2:3] + d * x[:, 0:1])) * np.exp(-(d**2) * x[:, 3:4])) def p_func(x): return (-0.5 * a**2 * (np.exp(2 * a * x[:, 0:1]) + np.exp(2 * a * x[:, 0:1]) + np.exp(2 * a * x[:, 2:3]) + 2 * np.exp(a * x[:, 0:1] + d * x[:, 1:2]) * np.cos(a * x[:, 2:3] + d * x[:, 0:1]) * np.exp(a * (x[:, 1:2] + x[:, 2:3])) + 2 * np.exp(a * x[:, 1:2] + d * x[:, 2:3]) * np.cos(a * x[:, 0:1] + d * x[:, 1:2]) * np.exp(a * (x[:, 2:3] + x[:, 0:1])) + 2 * np.exp(a * x[:, 2:3] + d * x[:, 0:1]) * np.cos(a * x[:, 1:2] + d * x[:, 2:3]) * np.exp(a * (x[:, 0:1] + x[:, 1:2]))) * np.exp(-2 * d**2 * x[:, 3:4])) # Define the boundary conditions boundary_condition_u = dde.DirichletBC(spatio_temporal_domain, u_func, lambda _, on_boundary: on_boundary, component=0) boundary_condition_v = dde.DirichletBC(spatio_temporal_domain, v_func, lambda _, on_boundary: on_boundary, component=1) boundary_condition_w = dde.DirichletBC(spatio_temporal_domain, w_func, lambda _, on_boundary: on_boundary, component=2) # Define the initial conditions initial_condition_u = dde.IC(spatio_temporal_domain, u_func, lambda _, on_initial: on_initial, component=0) initial_condition_v = dde.IC(spatio_temporal_domain, v_func, lambda _, on_initial: on_initial, component=1) initial_condition_w = dde.IC(spatio_temporal_domain, w_func, lambda _, on_initial: on_initial, component=2) # Define the data that is fed into the NN data = dde.data.TimePDE( spatio_temporal_domain, pde, [ boundary_condition_u, boundary_condition_v, boundary_condition_w, initial_condition_u, initial_condition_v, initial_condition_w, ], num_domain=50000, num_boundary=5000, num_initial=5000, num_test=10000, ) # Set up the NN architecture # Using a Feedforward network (FNN) with 4 input and output layers and 4 hidden layers # with 50 neurons each net = dde.maps.FNN([4] + 4 * [50] + [4], "tanh", "Glorot normal") model = dde.Model(data, net) # Compile the model # Use a learning rate of 1e-3 with an adam optimizer model.compile("adam", lr=1e-3, loss_weights=[1, 1, 1, 1, 100, 100, 100, 100, 100, 100]) # Train the model in 30000 epochs model.train(epochs=30000) model.compile("L-BFGS-B", loss_weights=[1, 1, 1, 1, 100, 100, 100, 100, 100, 100]) losshistory, train_state = model.train() # Generate the inference data # Sample the domain stepsize = 1e-3 x, y, z = np.meshgrid( np.arange(0, dx, stepsize), np.arange(0, dy, stepsize), np.arange(0, dz, stepsize), ) # Transform into a concatenated row vector X = np.vstack((np.ravel(x), np.ravel(y), np.ravel(z))).T # Create the time vectors dim = (dx * dy * dz) / stepsize t_0 = np.zeros(dim).reshape(dim, 1) t_1 = np.zeros(dim).reshape(dim, 1) t_1[:, 1] = dt # Concatenate spatial and temporal vectors to spatiotemporal input arrays X_0 = np.hstack(X, t_0) X_1 = np.hstack(X, t_1) # Calculate the inferred function values at the given points output_0 = model.predict(X_0) output_1 = model.predict(X_1) # Get the separate fields from the output # Remember, we gain four separate outputs u_pred_0 = output_0[:, 0].reshape(-1) v_pred_0 = output_0[:, 1].reshape(-1) w_pred_0 = output_0[:, 2].reshape(-1) p_pred_0 = output_0[:, 3].reshape(-1) u_exact_0 = u_func(X_0).reshape(-1) v_exact_0 = v_func(X_0).reshape(-1) w_exact_0 = w_func(X_0).reshape(-1) p_exact_0 = p_func(X_0).reshape(-1) u_pred_1 = output_1[:, 0].reshape(-1) v_pred_1 = output_1[:, 1].reshape(-1) w_pred_1 = output_1[:, 2].reshape(-1) p_pred_1 = output_1[:, 3].reshape(-1) u_exact_1 = u_func(X_1).reshape(-1) v_exact_1 = v_func(X_1).reshape(-1) w_exact_1 = w_func(X_1).reshape(-1) p_exact_1 = p_func(X_1).reshape(-1) # Now, infer the function fitting the PDE f_0 = model.predict(X_0, operator=pde) f_1 = model.predict(X_1, operator=pde) # Define the field-wise error metrics l2_difference_u_0 = dde.metrics.l2_relative_error(u_exact_0, u_pred_0) l2_difference_v_0 = dde.metrics.l2_relative_error(v_exact_0, v_pred_0) l2_difference_w_0 = dde.metrics.l2_relative_error(w_exact_0, w_pred_0) l2_difference_p_0 = dde.metrics.l2_realtive_error(p_exact_0, p_pred_0) residual_0 = np.mean(np.absolute(f_0)) l2_difference_u_1 = dde.metrics.l2_relative_error(u_exact_1, u_pred_1) l2_difference_v_1 = dde.metrics.l2_relative_error(v_exact_1, v_pred_1) l2_difference_w_1 = dde.metrics.l2_relative_error(w_exact_1, w_pred_1) l2_difference_p_1 = dde.metrics.l2_realtive_error(p_exact_1, p_pred_1) residual_1 = np.mean(np.absolute(f_1)) print("Accuracy at t = 0:") print("Mean residual: ", residual_0) print("L2 realtive error in u: ", l2_difference_u_0) print("L2 relative error in v: ", l2_difference_v_0) print("L2 relative error in w: ", l2_difference_w_0) print("\n") print("Accuracy at t = 1:") print("Mean residual: ", residual_1) print("L2 realtive error in u: ", l2_difference_u_1) print("L2 relative error in v: ", l2_difference_v_1) print("L2 relative error in w: ", l2_difference_w_1)