-
Notifications
You must be signed in to change notification settings - Fork 774
/
Copy pathode_system.py
49 lines (38 loc) · 1.28 KB
/
ode_system.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
"""Backend supported: tensorflow.compat.v1, tensorflow, pytorch, jax, paddle"""
import deepxde as dde
import numpy as np
def ode_system(x, y):
"""ODE system.
dy1/dx = y2
dy2/dx = -y1
"""
# Most backends
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)
# Backend jax
# y_val, y_fn = y
# y1, y2 = y_val[:, 0:1], y_val[:, 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.icbc.IC(geom, lambda x: 0, boundary, component=0)
ic2 = dde.icbc.IC(geom, lambda x: 1, 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.nn.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(iterations=20000)
dde.saveplot(losshistory, train_state, issave=True, isplot=True)