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, pde, [bc, ic, observe_y], num_domain=40, num_boundary=20, num_initial=10, anchors=observe_x, 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) 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(): alpha = 1.5 def fpde(x, y, int_mat): """(D_{0+}^alpha + D_{1-}^alpha) u(x) = f(x) """ if isinstance(int_mat, (list, tuple)) and len(int_mat) == 3: int_mat = tf.SparseTensor(*int_mat) lhs = tf.sparse_tensor_dense_matmul(int_mat, y) else: lhs = tf.matmul(int_mat, y) rhs = (gamma(4) / gamma(4 - alpha) * (x**(3 - alpha) + (1 - x)**(3 - alpha)) - 3 * gamma(5) / gamma(5 - alpha) * (x**(4 - alpha) + (1 - x)**(4 - alpha)) + 3 * gamma(6) / gamma(6 - alpha) * (x**(5 - alpha) + (1 - x)**(5 - alpha)) - gamma(7) / gamma(7 - alpha) * (x**(6 - alpha) + (1 - x)**(6 - alpha))) # lhs /= 2 * np.cos(alpha * np.pi / 2) # rhs = gamma(alpha + 2) * x return lhs - rhs[:tf.size(lhs)] def func(x): # return x * (np.abs(1 - x**2)) ** (alpha / 2) return x**3 * (1 - x)**3 geom = dde.geometry.Interval(0, 1) bc = dde.DirichletBC(geom, func, lambda _, on_boundary: on_boundary) # Static auxiliary points data = dde.data.FPDE(geom, fpde, alpha, bc, [101], meshtype="static", solution=func) # Dynamic auxiliary points # data = dde.data.FPDE( # geom, fpde, alpha, bc, [100], meshtype="dynamic", num_domain=20, num_boundary=2, solution=func, num_test=100 # ) net = dde.maps.FNN([1] + [20] * 4 + [1], "tanh", "Glorot normal") net.apply_output_transform(lambda x, y: x * (1 - x) * y) model = dde.Model(data, net) model.compile("adam", lr=1e-3) losshistory, train_state = model.train(epochs=10000) 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_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(): alpha0 = 1.8 alpha = tf.Variable(1.5) def fpde(x, y, int_mat): """\int_theta D_theta^alpha u(x) """ if isinstance(int_mat, (list, tuple)) and len(int_mat) == 3: int_mat = tf.SparseTensor(*int_mat) lhs = tf.sparse_tensor_dense_matmul(int_mat, y) else: lhs = tf.matmul(int_mat, y) lhs = lhs[:, 0] lhs *= -tf.exp( tf.lgamma((1 - alpha) / 2) + tf.lgamma( (2 + alpha) / 2)) / (2 * np.pi**1.5) x = x[:tf.size(lhs)] rhs = (2**alpha0 * gamma(2 + alpha0 / 2) * gamma(1 + alpha0 / 2) * (1 - (1 + alpha0 / 2) * tf.reduce_sum(x**2, axis=1))) return lhs - rhs def func(x): return (1 - np.linalg.norm(x, axis=1, keepdims=True)**2)**(1 + alpha0 / 2) geom = dde.geometry.Disk([0, 0], 1) observe_x = geom.random_points(30) observe_y = dde.PointSetBC(observe_x, func(observe_x)) data = dde.data.FPDE( geom, fpde, alpha, observe_y, [8, 100], num_domain=64, anchors=observe_x, solution=func, ) net = dde.maps.FNN([2] + [20] * 4 + [1], "tanh", "Glorot normal") net.apply_output_transform( lambda x, y: (1 - tf.reduce_sum(x**2, axis=1, keepdims=True)) * y) model = dde.Model(data, net) model.compile("adam", lr=1e-3, loss_weights=[1, 100]) variable = dde.callbacks.VariableValue(alpha, period=1000) losshistory, train_state = model.train(epochs=10000, callbacks=[variable]) dde.saveplot(losshistory, train_state, issave=True, isplot=True)
def main(): def ddy(x, y): dy_x = tf.gradients(y, x)[0] return tf.gradients(dy_x, x)[0] def dddy(x, y): return tf.gradients(ddy(x, y), x)[0] def pde(x, y): dy_xxxx = tf.gradients(dddy(x, y), x)[0] return dy_xxxx + 1 def boundary_l(x, on_boundary): return on_boundary and np.isclose(x[0], 0) def boundary_r(x, on_boundary): return on_boundary and np.isclose(x[0], 1) def func(x): return -x ** 4 / 24 + x ** 3 / 6 - x ** 2 / 4 geom = dde.geometry.Interval(0, 1) zero_func = lambda x: np.zeros((len(x), 1)) bc1 = dde.DirichletBC(geom, zero_func, boundary_l) bc2 = dde.NeumannBC(geom, zero_func, boundary_l) bc3 = dde.OperatorBC(geom, lambda x, y, _: ddy(x, y), boundary_r) bc4 = dde.OperatorBC(geom, lambda x, y, _: dddy(x, y), boundary_r) data = dde.data.PDE( geom, 1, pde, [bc1, bc2, bc3, bc4], num_domain=10, num_boundary=2, func=func, num_test=100, ) 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, metrics=["l2 relative error"]) losshistory, train_state = model.train(epochs=10000) 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(): geom = dde.geometry.Interval(-1, 1) num_train = 16 num_test = 100 data = dde.data.Function(geom, func, num_train, num_test) activation = "tanh" initializer = "Glorot uniform" net = dde.maps.FNN([1] + [20] * 3 + [1], 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(): alpha = 1.8 def fpde(x, y, int_mat): """\int_theta D_theta^alpha u(x) """ if isinstance(int_mat, (list, tuple)) and len(int_mat) == 3: int_mat = tf.SparseTensor(*int_mat) lhs = tf.sparse_tensor_dense_matmul(int_mat, y) else: lhs = tf.matmul(int_mat, y) lhs = lhs[:, 0] lhs *= gamma((1 - alpha) / 2) * gamma( (2 + alpha) / 2) / (2 * np.pi**1.5) x = x[:tf.size(lhs)] rhs = (2**alpha * gamma(2 + alpha / 2) * gamma(1 + alpha / 2) * (1 - (1 + alpha / 2) * tf.reduce_sum(x**2, axis=1))) return lhs - rhs def func(x): return (np.abs(1 - np.linalg.norm(x, axis=1, keepdims=True)**2))**( 1 + alpha / 2) geom = dde.geometry.Disk([0, 0], 1) bc = dde.DirichletBC(geom, func, lambda _, on_boundary: on_boundary) data = dde.data.FPDE(geom, fpde, alpha, bc, [8, 100], num_domain=100, num_boundary=1, solution=func) net = dde.maps.FNN([2] + [20] * 4 + [1], "tanh", "Glorot normal") net.apply_output_transform( lambda x, y: (1 - tf.reduce_sum(x**2, axis=1, keepdims=True)) * y) model = dde.Model(data, net) model.compile("adam", lr=1e-3) losshistory, train_state = model.train(epochs=20000) dde.saveplot(losshistory, train_state, issave=True, isplot=True) X = geom.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(): 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=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 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(): fname_train = "dataset/dataset.train" fname_test = "dataset/dataset.test" data = dde.data.DataSet( fname_train=fname_train, fname_test=fname_test, col_x=(0,), col_y=(1,) ) layer_size = [1] + [50] * 3 + [1] activation = "tanh" initializer = "Glorot normal" 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=50000) dde.saveplot(losshistory, train_state, issave=True, isplot=True)
def main(): def pde(x, y): dy_xx = dde.grad.hessian(y, x) return -dy_xx - np.pi ** 2 * tf.sin(np.pi * x) def boundary(x, on_boundary): return on_boundary def func(x): return np.sin(np.pi * x) geom = dde.geometry.Interval(-1, 1) bc = dde.DirichletBC(geom, func, boundary) data = dde.data.PDE(geom, pde, bc, 16, 2, solution=func, 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, metrics=["l2 relative error"]) checkpointer = dde.callbacks.ModelCheckpoint( "model/model.ckpt", verbose=1, save_better_only=True ) # ImageMagick (https://imagemagick.org/) is required to generate the movie. movie = dde.callbacks.MovieDumper( "model/movie", [-1], [1], period=100, save_spectrum=True, y_reference=func ) losshistory, train_state = model.train( epochs=10000, callbacks=[checkpointer, movie] ) dde.saveplot(losshistory, train_state, issave=True, isplot=True) # Plot PDE residual model.restore("model/model.ckpt-" + str(train_state.best_step), verbose=1) x = geom.uniform_points(1000, True) y = model.predict(x, operator=pde) plt.figure() plt.plot(x, y) plt.xlabel("x") plt.ylabel("PDE residual") plt.show()
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(): geom = dde.geometry.Polygon([[0, 0], [1, 0], [1, -1], [-1, -1], [-1, 1], [0, 1]]) bc = dde.DirichletBC(geom, lambda x: 0, boundary) data = dde.data.PDE(geom, pde, bc, num_domain=1200, num_boundary=120, num_test=1500) net = dde.maps.FNN([2] + [50] * 4 + [1], "tanh", "Glorot uniform") model = dde.Model(data, net) model.compile("adam", lr=0.001) model.train(epochs=50000) model.compile("L-BFGS-B") losshistory, train_state = model.train() dde.saveplot(losshistory, train_state, issave=True, isplot=True)
def main(): def pde(x, y): dy_xx = dde.grad.hessian(y, x) return (dy_xx + (np.pi * A)**2 * tf.sin(np.pi * A * x) + 0.1 * (np.pi * B)**2 * tf.sin(np.pi * B * x)) def func(x): return np.sin(np.pi * A * x) + 0.1 * np.sin(np.pi * B * x) geom = dde.geometry.Interval(0, 1) bc = dde.DirichletBC(geom, func, lambda _, on_boundary: on_boundary) data = dde.data.PDE( geom, pde, bc, 1280, 2, train_distribution="pseudo", solution=func, num_test=10000, ) layer_size = [1] + [100] * 3 + [1] activation = "tanh" initializer = "Glorot uniform" net = dde.maps.MsFFN(layer_size, activation, initializer, sigmas=[1, 10]) model = dde.Model(data, net) model.compile( "adam", lr=0.001, metrics=["l2 relative error"], decay=("inverse time", 2000, 0.9), ) pde_residual_resampler = dde.callbacks.PDEResidualResampler(period=1) model.train(epochs=20000, callbacks=[pde_residual_resampler]) dde.saveplot(model.losshistory, model.train_state, issave=True, isplot=True)
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(x, on_boundary): return on_boundary and np.isclose(x[0], 0) def func(x): """ y1 = sin(x) y2 = cos(x) """ return np.hstack((np.sin(x), np.cos(x))) geom = dde.geometry.Interval(0, 10) bc1 = dde.DirichletBC(geom, np.sin, boundary, component=0) bc2 = dde.DirichletBC(geom, np.cos, boundary, component=1) data = dde.data.PDE(geom, 2, ode_system, [bc1, bc2], 35, 2, func=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(): def ode_system(x, y): """ODE system. dy1/dx = y2 dy2/dx = -y1 """ y1, y2 = y[:, 0:1], y[:, 1:] dy1_x = dde.grad.jacobian(y, x, i=0) dy2_x = dde.grad.jacobian(y, x, i=1) 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(): geom = dde.geometry.Interval(0, 1) num_test = 1000 data = dde.data.MfFunc(geom, func_lo, func_hi, 100, 6, num_test) activation = "tanh" initializer = "Glorot uniform" regularization = ["l2", 0.01] net = dde.maps.MfNN( [1] + [20] * 4 + [1], [10] * 2 + [1], activation, initializer, regularization=regularization, ) model = dde.Model(data, net) model.compile("adam", lr=0.001, metrics=["l2 relative error"]) losshistory, train_state = model.train(epochs=80000) dde.saveplot(losshistory, train_state, issave=True, isplot=True)
def main(): geom = dde.geometry.Interval(-1, 1) observe_x = np.linspace(-1, 1, num=20)[:, None] observe_y = dde.PointSetBC(observe_x, func(observe_x)) # Static auxiliary points # data = dde.data.FPDE( # geom, # fpde, # alpha, # observe_y, # [101], # meshtype="static", # anchors=observe_x, # solution=func, # ) # Dynamic auxiliary points data = dde.data.FPDE( geom, fpde, alpha, observe_y, [100], meshtype="dynamic", num_domain=20, anchors=observe_x, solution=func, num_test=100, ) net = dde.maps.FNN([1] + [20] * 4 + [1], "tanh", "Glorot normal") net.apply_output_transform(lambda x, y: (1 - x**2) * y) model = dde.Model(data, net) model.compile("adam", lr=1e-3, loss_weights=[1, 100]) variable = dde.callbacks.VariableValue(alpha, period=1000) losshistory, train_state = model.train(epochs=10000, callbacks=[variable]) dde.saveplot(losshistory, train_state, issave=True, isplot=True)
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 nn(data): layer_size = [data.train_x.shape[1]] + [32] * 2 + [1] activation = "selu" initializer = "LeCun normal" regularization = ["l2", 0.01] loss = "MAPE" optimizer = "adam" if data.train_x.shape[1] == 3: lr = 0.0001 else: lr = 0.001 epochs = 30000 net = dde.maps.FNN(layer_size, activation, initializer, regularization=regularization) model = dde.Model(data, net) model.compile(optimizer, lr=lr, loss=loss, metrics=["MAPE"]) losshistory, train_state = model.train(epochs=epochs) dde.saveplot(losshistory, train_state, issave=True, isplot=False) return train_state.best_metrics[0]
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) data = dde.data.TimePDE( geomtime, pde, [], num_domain=40, 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.apply_output_transform(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 pde(x, y): dy_x = tf.gradients(y, x)[0] dy_xx = tf.gradients(dy_x, x)[0] return dy_xx - 2 def boundary_l(x, on_boundary): return on_boundary and np.isclose(x[0], -1) def boundary_r(x, on_boundary): return on_boundary and np.isclose(x[0], 1) def func(x): return (x + 1)**2 geom = dde.geometry.Interval(-1, 1) bc_l = dde.DirichletBC(geom, func, boundary_l) bc_r = dde.NeumannBC(geom, lambda X: 2 * (X + 1), boundary_r) data = dde.data.PDE(geom, 1, pde, [bc_l, bc_r], 16, 2, func=func, 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, metrics=["l2 relative error"]) losshistory, train_state = model.train(epochs=10000) dde.saveplot(losshistory, train_state, issave=True, isplot=True)
def main(): def pde(x, y): dy_x = dde.grad.jacobian(y, x, i=0, j=0) 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 + 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: 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 main(): def pde(x, y): dy_x = tf.gradients(y, x)[0] dy_r, dy_theta = dy_x[:, 0:1], dy_x[:, 1:2] dy_rr = tf.gradients(dy_r, x)[0][:, 0:1] dy_thetatheta = tf.gradients(dy_theta, x)[0][:, 1:2] return x[:, 0:1] * dy_r + x[:, 0:1]**2 * dy_rr + dy_thetatheta def solution(x): r, theta = x[:, 0:1], x[:, 1:] return r * np.cos(theta) geom = dde.geometry.Rectangle(xmin=[0, 0], xmax=[1, 2 * np.pi]) bc_rad = dde.DirichletBC( geom, lambda x: np.cos(x[:, 1:2]), lambda x, on_boundary: on_boundary and np.isclose(x[0], 1), ) data = dde.data.PDE(geom, pde, bc_rad, num_domain=2540, num_boundary=80, solution=solution) net = dde.maps.FNN([2] + [20] * 3 + [1], "tanh", "Glorot normal") # Use [r*sin(theta), r*cos(theta)] as features, # so that the network is automatically periodic along the theta coordinate. net.apply_feature_transform(lambda x: tf.concat( [x[:, 0:1] * tf.sin(x[:, 1:2]), x[:, 0:1] * tf.cos(x[:, 1:2])], axis=1) ) model = dde.Model(data, net) model.compile("adam", lr=1e-3, metrics=["l2 relative error"]) losshistory, train_state = model.train(epochs=15000) dde.saveplot(losshistory, train_state, issave=True, isplot=True)
def main(): geom = dde.geometry.Interval(0, 1) bc = dde.DirichletBC(geom, func, lambda _, on_boundary: on_boundary) # Static auxiliary points data = dde.data.FPDE(geom, fpde, alpha, bc, [101], meshtype="static", solution=func) # Dynamic auxiliary points # data = dde.data.FPDE( # geom, fpde, alpha, bc, [100], meshtype="dynamic", num_domain=20, num_boundary=2, solution=func, num_test=100 # ) net = dde.maps.FNN([1] + [20] * 4 + [1], "tanh", "Glorot normal") net.apply_output_transform(lambda x, y: x * (1 - x) * y) model = dde.Model(data, net) model.compile("adam", lr=1e-3) losshistory, train_state = model.train(epochs=10000) dde.saveplot(losshistory, train_state, issave=True, isplot=True)
def boundary_r(x, on_boundary): return on_boundary and np.isclose(x[0], 1) def func(x): return (x + 1)**2 geom = dde.geometry.Interval(-1, 1) bc_l = dde.DirichletBC(geom, func, boundary_l) bc_r = dde.RobinBC(geom, lambda X, y: y, boundary_r) data = dde.data.PDE(geom, pde, [bc_l, bc_r], 16, 2, solution=func, 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, 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:3] dy = [] 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) dy.append(dy1_x - int(1) * C1 * (y2 - y1)) dy.append(dy2_x - y1 * (C2 - y3) + y2) dy.append(dy3_x - y1 * y2 + C3 * y3) dy[1] = dy[1] + dy2_x - dy2_x - y2 + y2 # return [ # dy1_x - C1 * (y2 - y1), # dy2_x - y1 * (C2 - y3) + y2, # dy3_x - y1 * y2 + C3 * y3, # ] return dy 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() ptset = dde.bc.PointSet(observe_t) inside = lambda x, _: ptset.inside(x) observe_y0 = dde.DirichletBC(geom, ptset.values_to_func(ob_y[:, 0:1]), inside, component=0) observe_y1 = dde.DirichletBC(geom, ptset.values_to_func(ob_y[:, 1:2]), inside, component=1) observe_y2 = dde.DirichletBC(geom, ptset.values_to_func(ob_y[:, 2:3]), inside, 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)