Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
* (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,
)
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 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 np.sin(np.pi * x)
geom = dde.geometry.Interval(-1, 1)
bc1 = dde.DirichletBC(geom, func, boundary_l)
bc2 = dde.PeriodicBC(geom, 0, boundary_r)
data = dde.data.PDE(geom, 1, pde, [bc1, bc2], 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 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)
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,
1,
pde,
[bc, ic],
num_domain=40,
num_boundary=20,
num_initial=10,
func=func,
num_test=10000,
)
layer_size = [2] + [32] * 3 + [1]
activation = "tanh"
initializer = "Glorot uniform"
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 boundary(x, on_boundary):
return on_boundary and np.isclose(x[0], 0)
def func(x):
return np.exp(-x) * np.cosh(x)
geom = dde.geometry.Interval(0, 5)
bc = dde.DirichletBC(geom, func, boundary)
quad_deg = 20
data = dde.data.IDE(
geom,
ide,
bc,
quad_deg,
kernel=kernel,
num_domain=10,
num_boundary=2,
train_distribution="uniform",
)
layer_size = [1] + [20] * 3 + [1]
activation = "tanh"
initializer = "Glorot uniform"
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 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.RobinBC(geom, lambda X, y: y, 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)
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 boundary(x, on_boundary):
return on_boundary and np.isclose(x[0], 0)
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.Interval(0, 1)
bc = dde.DirichletBC(geom, func, boundary)
quad_deg = 16
data = dde.data.IDE(geom, ide, bc, 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)
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,
def main():
def pde(x, y):
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:]
return -dy_xx - dy_yy - 1
def boundary(x, on_boundary):
return on_boundary
def func(x):
return np.zeros([len(x), 1])
geom = dde.geometry.Polygon([[0, 0], [1, 0], [1, -1], [-1, -1], [-1, 1], [0, 1]])
bc = dde.DirichletBC(geom, func, boundary)
data = dde.data.PDE(
geom, 1, 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)