-
Notifications
You must be signed in to change notification settings - Fork 2
/
Helm_pinn_ps_sine_tti.py
292 lines (211 loc) · 8.92 KB
/
Helm_pinn_ps_sine_tti.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
"""
@author: Chao Song
"""
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import scipy.io
from scipy.interpolate import griddata
import time
import cmath
import math
np.random.seed(1234)
tf.set_random_seed(1234)
fre = 6.0
PI = 3.1415926
omega = 2.0*PI*fre
niter = 50000
theta0 = 0.0/180.0*PI
misfit = []
misfit1 = []
w0_adam = []
def fwd_gradients(Y, x):
dummy = tf.ones_like(Y)
G = tf.gradients(Y, x, grad_ys=dummy, colocate_gradients_with_ops=True)[0]
Y_x = tf.gradients(G, dummy, colocate_gradients_with_ops=True)[0]
return Y_x
class PhysicsInformedNN:
# Initialize the class
def __init__(self, x, z, A, B, C, ps, m, epsilon, delta, layers, omega):
X = np.concatenate([x, z], 1)
self.iter=0
self.start_time=0
self.iter=0
self.start_time=0
self.lb = X.min(0)
self.ub = X.max(0)
self.X = X
self.x = X[:,0:1]
self.z = X[:,1:2]
self.ps = ps
self.m = m
self.epsilon = epsilon
self.delta = delta
self.A = A
self.B = B
self.C = C
self.layers = layers
self.omega = omega
# Initialize NN
self.weights, self.biases, self.w0 = self.initialize_NN(layers)
# tf placeholders
self.sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True,
log_device_placement=True))
self.x_tf = tf.placeholder(tf.float32, shape=[None, self.x.shape[1]])
self.z_tf = tf.placeholder(tf.float32, shape=[None, self.z.shape[1]])
self.u_real_pred, self.u_imag_pred, self.u_loss, self.q_loss = self.net_NS(self.x_tf, self.z_tf)
# loss function we define
self.loss = tf.reduce_sum(tf.square(tf.abs(self.u_loss))) + tf.reduce_sum(tf.square(tf.abs(self.q_loss)))
# optimizer used by default (in original paper)
self.optimizer = tf.contrib.opt.ScipyOptimizerInterface(self.loss,
method = 'L-BFGS-B',
options = {'maxiter': 50000,
'maxfun': 50000,
'maxcor': 50,
'maxls': 50,
'ftol' : 1.0 * np.finfo(float).eps})
self.optimizer_Adam = tf.train.AdamOptimizer()
self.train_op_Adam = self.optimizer_Adam.minimize(self.loss)
init = tf.global_variables_initializer()
self.sess.run(init)
def initialize_NN(self, layers):
weights = []
biases = []
num_layers = len(layers)
w0 = tf.Variable(tf.zeros([1,1], dtype=tf.float32)+3.0, dtype=tf.float32)
for l in range(0,num_layers-1):
if (l == 0):
W = self.xavier_init_first(size=[layers[l], layers[l+1]])
else:
W = self.xavier_init(w0,size=[layers[l], layers[l+1]])
b = tf.Variable(tf.zeros([1,layers[l+1]], dtype=tf.float32)+0.0, dtype=tf.float32)
weights.append(W)
biases.append(b)
return weights, biases, w0
def xavier_init(self, w0, size):
in_dim = size[0]
out_dim = size[1]
w_std = np.sqrt(1./in_dim)/w0
return tf.Variable(tf.random_uniform([in_dim, out_dim], -w_std, w_std), dtype=tf.float32)
def xavier_init_first(self, size):
in_dim = size[0]
out_dim = size[1]
w_std = (1./np.sqrt(in_dim))
return tf.Variable(tf.random_uniform([in_dim, out_dim], -w_std, w_std), dtype=tf.float32)
def neural_net(self, X, weights, biases, w0):
num_layers = len(weights) + 1
H = 2.0*(X - self.lb)/(self.ub - self.lb) - 1.0
H = w0*H
for l in range(0,num_layers-2):
W = weights[l]
b = biases[l]
H = tf.sin(tf.add(tf.matmul(H, W), b))
W = weights[-1]
b = biases[-1]
Y = tf.add(tf.matmul(H, W), b)
return Y
def net_NS(self, x, z):
omega = self.omega
m = self.m
epsilon = self.epsilon
delta = self.delta
ps = self.ps
A = self.A
B = self.B
C = self.C
u_and_q = self.neural_net(tf.concat([x,z], 1), self.weights, self.biases, self.w0)
u_real = u_and_q[:,0:1]
u_imag = u_and_q[:,1:2]
q_real = u_and_q[:,2:3]
q_imag = u_and_q[:,3:4]
u = tf.complex(u_real, u_imag)
q = tf.complex(q_real, q_imag)
dudx = fwd_gradients(u, x)
dudz = fwd_gradients(u, z)
dudxx = fwd_gradients((A*dudx), x)
dudzz = fwd_gradients((B*dudz), z)
dudxz = fwd_gradients((B*dudx), z)
dqdx = fwd_gradients(q, x)
dqdz = fwd_gradients(q, z)
dqdxx = fwd_gradients((A*dqdx), x)
dqdzz = fwd_gradients((B*dqdz), z)
dqdxz = fwd_gradients((B*dqdx), z)
u_loss = C*omega*omega*m*u + (1+2*delta)*(math.cos(theta0)**2*dudxx + math.sin(theta0)**2*dudzz - math.sin(2*theta0)*dudxz) +\
(math.sin(theta0)**2*dudxx + math.cos(theta0)**2*dudzz + math.sin(2*theta0)*dudxz) +\
(1+2*delta)*(math.cos(theta0)**2*dqdxx + math.sin(theta0)**2*dqdzz - math.sin(2*theta0)*dqdxz) - ps
q_loss = C*omega*omega*m*q + 2*(epsilon - delta)*(math.cos(theta0)**2*dqdxx + math.sin(theta0)**2*dqdzz - math.sin(2*theta0)*dqdxz) +\
2*(epsilon - delta)*(math.cos(theta0)**2*dudxx + math.sin(theta0)**2*dudzz - math.sin(2*theta0)*dudxz)
return u_real, u_imag, u_loss, q_loss
def callback(self, loss):
#print('Loss: %.3e' % (loss))
misfit1.append(loss)
self.iter=self.iter+1
if self.iter % 10 == 0:
elapsed = time.time() - self.start_time
print('It: %d, LBFGS Loss: %.3e,Time: %.2f' %
(self.iter, loss, elapsed))
self.start_time = time.time()
def train(self, nIter):
tf_dict = {self.x_tf: self.x, self.z_tf: self.z}
start_time = time.time()
for it in range(nIter):
self.sess.run(self.train_op_Adam, tf_dict)
[loss_value, w0_value] = self.sess.run([self.loss,self.w0], tf_dict)
misfit.append(loss_value)
w0_adam.append(w0_value)
# Print
if it % 10 == 0:
elapsed = time.time() - start_time
[loss_value, w0_value] = self.sess.run([self.loss,self.w0], tf_dict)
#misfit.append(loss_value)
print('It: %d, Loss: %.3e,Time: %.2f,w0:%.2f' %
(it, loss_value, elapsed,w0_value))
start_time = time.time()
self.optimizer.minimize(self.sess,
feed_dict = tf_dict,
fetches = [self.loss],
loss_callback = self.callback)
def predict(self, x_star, z_star):
tf_dict = {self.x_tf: x_star, self.z_tf: z_star}
u_real_star = self.sess.run(self.u_real_pred, tf_dict)
u_imag_star = self.sess.run(self.u_imag_pred, tf_dict)
return u_real_star, u_imag_star
if __name__ == "__main__":
layers = [2, 40, 40, 40, 40, 40, 40,40, 40, 4]
# Load Data
data = scipy.io.loadmat('Tti_homo_6Hz_sine.mat')
ps = data['Ps']
x = data['x']
z = data['z']
m = data['m']
epsilon = data['epsilon']
delta = data['delta']
A = data['A']
B = data['B']
C = data['C']
N = x.shape[0]
N_train = N
# Training Data
idx = np.random.choice(N, N_train, replace=False)
x_train = x[idx,:]
z_train = z[idx,:]
ps_train = ps[idx,:]
A_train = A[idx,:]
B_train = B[idx,:]
C_train = C[idx,:]
m_train = m[idx,:]
epsilon_train = epsilon[idx,:]
delta_train = delta[idx,:]
# Training
model = PhysicsInformedNN(x_train, z_train, A_train, B_train, C_train, ps_train, m_train, epsilon_train, delta_train, layers, omega)
model.train(niter)
scipy.io.savemat('w0_adam_tti.mat',{'w0':w0_adam})
scipy.io.savemat('loss_adam_const_tti.mat',{'misfit':misfit})
scipy.io.savemat('loss_lbfgs_const_tti.mat',{'misfit1':misfit1})
# Test Data
x_star = x
z_star = z
# Prediction
u_real_pred, u_imag_pred = model.predict(x_star, z_star)
scipy.io.savemat('u_real_pred_sine_tti.mat',{'u_real_pred':u_real_pred})
scipy.io.savemat('u_imag_pred_sine_tti.mat',{'u_imag_pred':u_imag_pred})