def feature_transform(t): t = 0.01 * t return tf.concat( (t, tf.sin(t), tf.sin(2 * t), tf.sin(3 * t), tf.sin( 4 * t), tf.sin(5 * t)), axis=1, )
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 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 pde(x, y): J = dde.grad.Jacobian(y, x) dy_t = J(j=1) H = dde.grad.Hessian(y, x, grad_y=J()) dy_xx = H(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 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 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) # Backend tensorflow.compat.v1 or tensorflow 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 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 pde(x, y): f = 2 * (np.pi**2) * tf.sin(np.pi * x[:, 0:1]) \ * tf.sin(np.pi * x[:, 1:2]) # Definition of the spatial derivatives dy_x = tf.gradients(y, x)[0] dy_x, dy_y = dy_x[:, 0:1], dy_x[:, 1:] dy_xx = tf.gradients(dy_x, x)[0][:, 0:1] dy_yy = tf.gradients(dy_y, x)[0][:, 1:] # Definition of the Poisson equation return dy_xx + dy_yy + f
def main(): def pde(x, y): dy_r = dde.grad.jacobian(y, x, i=0, j=0) dy_rr = dde.grad.hessian(y, x, i=0, j=0) dy_thetatheta = dde.grad.hessian(y, x, i=1, j=1) 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 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 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 feature_transform(t): return tf.concat( ( t, tf.sin(t), tf.sin(2 * t), tf.sin(3 * t), tf.sin(4 * t), tf.sin(5 * t), tf.sin(6 * t), ), axis=1, )
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 pde(x, y): dy_xx = dde.grad.hessian(y, x) return -dy_xx - np.pi ** 2 * tf.sin(np.pi * 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( # Backend tensorflow.compat.v1 or tensorflow lambda x, y: x[:, 1:2] * (1 - x[:, 0:1]**2) * y + tf.sin(np.pi * x[:, 0:1]) # Backend pytorch # lambda x, y: x[:, 1:2] * (1 - x[:, 0:1] ** 2) * y + torch.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 pde(x, y): dy_x = tf.gradients(y, x)[0] dy_xx = tf.gradients(dy_x, x)[0] return -dy_xx - np.pi**2 * tf.sin(np.pi * x)
def pde(x, y): dy_xx = dde.grad.hessian(y, x) # Use tf.sin for backend tensorflow.compat.v1 or tensorflow return -dy_xx - np.pi**2 * tf.sin(np.pi * x)
def feature_transform(x): return tf.concat( [x[:, 0:1] * tf.sin(x[:, 1:2]), x[:, 0:1] * tf.cos(x[:, 1:2])], axis=1)