Repository navigation
Expand file tree
/
Copy pathRandom_R.py
More file actions
50 lines (38 loc) · 1.54 KB
/
Copy pathRandom_R.py
File metadata and controls
50 lines (38 loc) · 1.54 KB
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
50
import numpy as np
from deepxde.backend import tf
import deepxde as dde
def main(P):
NumDomain = 30
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:])
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=NumDomain, 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)
def output_transform(x, y):
return tf.sin(np.pi * x[:, 0:1]) + (1 - x[:, 0:1] ** 2) * (x[:, 1:]) * y
net.apply_output_transform(output_transform)
model = dde.Model(data, net)
resampler = dde.callbacks.PDEResidualResampler(period=P)
model.compile("adam", lr=1e-3, metrics=["l2 relative error"])
losshistory, train_state = model.train(epochs=15000, callbacks=[resampler])
error = np.array(losshistory.metrics_test)[-1]
print("L2 relative error:", error)
dde.saveplot(losshistory, train_state, issave=True, isplot=True)
return error
if __name__ == '__main__':
main(P=100)