-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathInverse_problem.py
293 lines (203 loc) · 8.77 KB
/
Inverse_problem.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import deepxde as dde
import numpy as np
# Backend tensorflow.compat.v1 or tensorflow
from deepxde.backend import tf
# Backend pytorch
# import torch
# Backend paddle
# import paddle
import matplotlib.pyplot as plt
Ctrue=4.0
C = dde.Variable(0.05)
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 - C*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.Interval(0, 1)
#timedomain = dde.geometry.TimeDomain(0, 2)
#geomtime = dde.geometry.GeometryXTime(geom, timedomain)
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
observe_x_1 = np.vstack((np.linspace(0, 1, num=200), np.full((200), 2))).T
observe_x_2 = np.vstack((np.linspace(0, 1, num=200), np.full((200), 0))).T
observe_x_3 = np.vstack((np.full((200), 0),np.linspace(0, 2, num=200))).T
observe_x_4 = np.vstack((np.full((200), 1),np.linspace(0, 2, num=200))).T
observe_x_initial = np.random.uniform((0, 0), (1, 2), (1000, 2))
observe_x_5=np.array([a for a in observe_x_initial if 0<a[0]<1 and 0<a[1]<2])
observe_x=np.array(list(observe_x_1)+list(observe_x_2)+list(observe_x_3)+list(observe_x_4)+list(observe_x_5))
observe_y = dde.icbc.PointSetBC(observe_x_5, func(observe_x_5), component=0)
data = dde.data.TimePDE(
geom,
pde,
[bc1, bc2, bc3, bc4, observe_y],
num_domain=0,
num_boundary=0,
train_distribution="uniform", anchors=observe_x,
num_test=1000, solution=func
)
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.train_state.set_data_train( X_train=observe_final, y_train=func(observe_final), train_aux_vars=None)
model.compile("adam", lr=0.001,loss_weights=[1,1,1,1,1,6], external_trainable_variables=C)
filname = "variables.dat"
variable = dde.callbacks.VariableValue(C, period=1, filename=filname)
numepochs=60000
losshistory, train_state=model.train(epochs=numepochs, callbacks=[variable])
#model.compile("L-BFGS-B")
#model.compile("L-BFGS-B", external_trainable_variables=C)
#variable1 = dde.callbacks.VariableValue(C, period=100, filename=filname)
#losshistory, train_state = model.train(epochs=800, callbacks=[variable])
dde.saveplot(
losshistory, train_state, issave=True, isplot=True
)
#observe_x = np.vstack((np.linspace(-1, 1, num=100), np.full((100), 1))).T
#observe_y = dde.icbc.PointSetBC(observe_x, func(observe_x), component=0)
import re
lines = open(filname, "r").readlines()
# read output data in fnamevar
Chat = np.array(
[
np.fromstring(
min(re.findall(re.escape("[") + "(.*?)" + re.escape("]"), line), key=len),
sep=",",
)
for line in lines
]
)
l, c = Chat.shape
plt.plot(range(l), Chat[:, 0], "r-", label="Prediction")
#plt.semilogy(range(0, l * 100, 100), Chat[:, 1], "k-")
plt.plot(range(l), np.ones(Chat[:, 0].shape) * Ctrue, "r--", label="True value")
#plt.semilogy(range(0, l * 100, 100), np.ones(Chat[:, 1].shape) * C2true, "k--")
#plt.legend(["C1hat", "C2hat", "True C1", "True C2"], loc="right")
#plt.xlabel("Epochs")
#plt.title("Variables")
plt.legend()
plt.show()
#print(np.array(losshistory.loss_train).T[0])
np.arange(1,numepochs+1,1)
#plt.plot(np.arange(0,numepochs+1,1),np.array(losshistory.loss_train).T[0])
print(Chat[:, 0][-1])
#predicted_solution_training=np.ravel(model.predict(data.train_x_all))
#print(predicted_solution_training)
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()
import math
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()
print(observe_x.T[0])
print(observe_x.T[1])
print(model.predict(observe_x).T[0])
fig = plt.figure() # plot of the difference between exact and PINN state
ax = plt.axes(projection="3d")
surf2 = ax.scatter(observe_x.T[0], observe_x.T[1], model.predict(observe_x).T[0], marker="o")
ax.set_title("PINN prediction of the training set")
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.scatter(observe_x.T[0], observe_x.T[1], explicit_state(observe_x.T[0], observe_x.T[1]))
ax.set_title("True solution of the training set")
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.scatter(observe_x.T[0], observe_x.T[1], explicit_state(observe_x.T[0], observe_x.T[1])-model.predict(observe_x).T[0])
ax.set_title("Difference of the true and predicted solution of the training set")
ax.set_xlabel("$x$")
ax.set_ylabel("$t$")
#fig.colorbar(surf2, shrink=0.6, aspect=10)
plt.show()
print("Difference of the true and predicted solution of the training set are {}".format(explicit_state(observe_x.T[0], observe_x.T[1])-model.predict(observe_x).T[0]))
fig = plt.figure() # plot of the difference between exact and PINN state
ax = plt.axes(projection="3d")
surf2 = ax.scatter([0,1,2,3,4], [0,1,2,3,4], [3,4,5,6,8])
ax.set_title("Difference of the true and predicted solution of the training set")
ax.set_xlabel("$x$")
ax.set_ylabel("$t$")
#fig.colorbar(surf2, shrink=0.6, aspect=10)
plt.show()
a=np.arange(0,60100,1000)
plt.plot(a,np.array(losshistory.loss_train).T[0], label=r'$L_{PDE}$', linestyle="-" )
plt.plot(a,np.array(losshistory.loss_train).T[1], label=r"$L_{x=0}$", linestyle="-")
plt.plot(a,np.array(losshistory.loss_train).T[2], label=r"$L_{x=1}$", linestyle="-")
plt.plot(a,np.array(losshistory.loss_train).T[3], label=r"${L_{t=0_{2}}}$", linestyle="-")
plt.plot(a,np.array(losshistory.loss_train).T[4], label=r"${L_{t=0_{1}}}$", linestyle="-")
plt.plot(a,np.array(losshistory.loss_train).T[5], label=r"$6·L_{train}$", linestyle="-")
plt.xlabel("Number of epochs")
plt.ylabel("Error")
plt.legend()