-
Notifications
You must be signed in to change notification settings - Fork 11
/
Wave_equation.py
201 lines (123 loc) · 4.73 KB
/
Wave_equation.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import matplotlib.pyplot as plt
from matplotlib import cm
import numpy as np
from itertools import product
import deepxde as dde
import math
import pathlib
import os
OUTPUT_DIRECTORY = pathlib.Path.cwd() / "results" / "wave_equation"
if not OUTPUT_DIRECTORY.exists():
os.makedirs(OUTPUT_DIRECTORY, exist_ok=True)
def pde(x, y): # wave equation
dy_tt = dde.grad.hessian(y, x, i=1, j=1)
dy_xx = dde.grad.hessian(y, x, i=0, j=0)
return dy_tt - 4*dy_xx
def initial_pos(x): # initial position
return np.sin(np.pi * x[:, 0:1])
def initial_velo(x): # initial velocity
return 0.0
def boundary_left(x, on_boundary): # boundary x=0
is_on_boundary_left = on_boundary and np.isclose(x[0], 0)
return is_on_boundary_left
def boundary_right(x, on_boundary): # boundary x=1
is_on_boundary_right = on_boundary and np.isclose(x[0], 1)
return is_on_boundary_right
def boundary_bottom(x, on_boundary): # boundary t=0
is_on_boundary_bottom = (
on_boundary
and np.isclose(x[1], 0)
and not np.isclose(x[0], 0)
and not np.isclose(x[0], 1)
)
return is_on_boundary_bottom
def boundary_upper(x, on_boundary): # boundary t=2
is_on_boundary_upper = (
on_boundary
and np.isclose(x[1], 2)
and not np.isclose(x[0], 0)
and not np.isclose(x[0], 1)
)
return is_on_boundary_upper
def func(x):
return np.sin(np.pi * x[:, 0:1]) * np.cos(2*np.pi*x[:, 1:])
geom = dde.geometry.Rectangle(xmin=[0, 0], xmax=[1, 2])
bc1 = dde.DirichletBC(geom, lambda x: 0, boundary_left) #correct
bc2 = dde.DirichletBC(geom, lambda x: 0, boundary_right) #correct
bc3 = dde.DirichletBC(geom, initial_pos, boundary_bottom) #correct
bc4 = dde.NeumannBC(geom, initial_velo, boundary_bottom) #correct
data = dde.data.TimePDE(
geom,
pde,
[bc1, bc2, bc3, bc4],
num_domain=100,
num_boundary=50, solution=func
)
print("The training set is {}".format(data.train_x_all.T))
net = dde.maps.FNN([2] + [50] * 4 + [1], "tanh", "Glorot uniform") #the input layer has size 2, there are 4 hidden layers of size 50 and one output layer of size 1
#Activation function is tanh; the weights are initially chosen to be uniformly distributed according to Glorat distribution
model = dde.Model(data, net)
model.compile("adam", lr=0.001)
model.train(epochs=8000)
model.compile("L-BFGS-B")
losshistory, train_state = model.train()
dde.saveplot(
losshistory, train_state, issave=True, isplot=True, output_dir=OUTPUT_DIRECTORY
)
#Post-processing: error analysis and figures
xx=np.linspace(0,1,100)
tt=np.linspace(0,2,200)
list_all_data=list(product(*[list(xx), list(tt)], repeat=1))
training_set=[(data.train_x_all.T[0][k], data.train_x_all.T[1][k]) for k in range(len(data.train_x_all.T[0]))]
def exact_function(x):
return math.sin(math.pi*x[0])*math.cos(2*math.pi*x[1])
X = np.linspace(0, 1, 100)
t = np.linspace(0, 2, 200)
X_repeated = np.repeat(X, t.shape[0])
t_tiled = np.tile(t, X.shape[0])
XX = np.vstack((X_repeated, t_tiled)).T
state_predict = model.predict(XX).T
state_predict_M = state_predict.reshape((100, 200)).T
Xx, Tt = np.meshgrid(np.linspace(0, 1, 100), np.linspace(0, 2, 200))
fig = plt.figure() # plot of predicted state
ax = plt.axes(projection="3d")
surf = ax.plot_surface(
Xx, Tt, state_predict_M, cmap="hsv_r", linewidth=0, antialiased=False
)
ax.set_title("PINN state ")
ax.set_xlabel("$x$")
ax.set_ylabel("$t$")
fig.colorbar(surf, shrink=0.6, aspect=10)
plt.show()
def explicit_state(x,t):
return np.sin(math.pi*x)*np.cos(2*math.pi*t)
state_exact = explicit_state(Xx, Tt) # computation of exact state
fig = plt.figure() # plot of exact state
ax = plt.axes(projection="3d")
surf2 = ax.plot_surface(
Xx, Tt, state_exact, cmap="hsv_r", linewidth=0, antialiased=False
)
ax.set_title("Exact state ")
ax.set_xlabel("$x$")
ax.set_ylabel("$t$")
fig.colorbar(surf2, shrink=0.6, aspect=10)
plt.show()
fig = plt.figure() # plot of the difference between exact and PINN state
ax = plt.axes(projection="3d")
surf2 = ax.plot_surface(
Xx, Tt, state_exact-state_predict_M, cmap="hsv_r",
linewidth=0, antialiased=False
)
ax.set_title("Difference of the exact and PINN state")
ax.set_xlabel("$x$")
ax.set_ylabel("$t$")
fig.colorbar(surf2, shrink=0.6, aspect=10)
plt.show()
#Plot the training set and validation set
fig = plt.figure()
plt.plot(data.train_x_all.T[0], data.train_x_all.T[1],"o")
#plt.plot(validating_set.T[0], validating_set.T[1], "r", label="Validation set")
plt.xlabel("x")
plt.ylabel("t")
plt.title("Training set")
#plt.legend()