This repository has been archived by the owner on Jun 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·363 lines (272 loc) · 9.61 KB
/
main.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#!/usr/bin/env python3
import numpy as np
import scipy.special as sp
import matplotlib.pyplot as plt
import sys
import scipy.io as sio
import shutil
from scipy.integrate import odeint
import numpy as np
import random
from scipy import integrate
# Bash Parameters
# Integer Values
seed = int(sys.argv[1])
samples = int(sys.argv[2])
exponent_truth = int(sys.argv[3])
exponent_approx = int(sys.argv[4])
epochs = int(sys.argv[5])
b_layers = int(sys.argv[6])
neurons = int(sys.argv[7])
# String Values
func_str = sys.argv[8]
save_dir = sys.argv[9]
itr = "mid"
# itr = 'trapz'
save_str = func_str + itr + "_Seed_" + str(seed) + "_Samples_" + str(samples) + "_X_" + str(exponent_truth) + "_" + str(exponent_approx) + "_epochs_" + str(epochs) + "_blayers_" + str(b_layers) + "_neurons_" + str(neurons)
# Set seed and calculate number of points
np.random.random(seed)
points = 2**exponent_truth + 1
# I believe the functions we want to structure are I(x) = int_a^b g(x) S(rx) dx
if func_str == "Levin1":
def oscil_func(x, nu, r): # Levin paper Bessel function
y = 1 / (x**2 + 1) * sp.jv(nu, r * x)
return y
# Create Bounds for integegration for the Levin paper
a = 1
b = 2
x = np.linspace(a, b, points)
# Calculate the function
y = np.zeros((points, samples))
for i in range(0, samples):
nu = np.random.random() * 0
r = np.random.random() * 50 + 125
y[:, i] = oscil_func(x, nu, r)
elif func_str == "Levin2":
def oscil_func(x, nu, r, k1): # Levin paper Bessel function
y = 1 / (x**2 + 1) * np.cos(k1 * x) * sp.jv(nu, r * x)
return y
# Create Bounds for integegration for the Levin paper
a = 1
b = 2
x = np.linspace(a, b, points)
# Calculate the function
y = np.zeros((points, samples))
for i in range(0, samples):
nu = np.random.random() * 0
r = np.random.random() * 50 + 125
k1 = np.random.random() * 50 + 75
y[:, i] = oscil_func(x, nu, r, k1)
elif func_str == "EvansWebster1":
def oscil_func(x, k1, k2): # EvansWebster Functions
y = np.cos(k1 * x**2) * np.sin(k2 * x)
return y
a = 0
b = 1
x = np.linspace(a, b, points)
# Calculate the function
y = np.zeros((points, samples))
for i in range(0, samples):
k1 = np.random.random() * 10 + 5
k2 = np.random.random() * 50 + 25
y[:, i] = oscil_func(x, k1, k2)
elif func_str == "EvansWebster2":
def oscil_func(x, k1): # EvansWebster Functions
y = np.cos(x) * np.cos(k1 * np.cos(x))
return y
a = 0
b = 1
x = np.linspace(a, b, points)
# Calculate the function
y = np.zeros((points, samples))
for i in range(0, samples):
k1 = np.random.random() * 40 + 20
y[:, i] = oscil_func(x, k1)
elif func_str == "EvansWebster3":
def oscil_func(x, k1): # EvansWebster Functions
y = np.sin(x) * np.cos(k1 * (x**2 + x))
return y
a = 0
b = 1
x = np.linspace(a, b, points)
# Calculate the function
y = np.zeros((points, samples))
for i in range(0, samples):
k1 = np.random.random() * 50 + 475
y[:, i] = oscil_func(x, k1)
elif func_str == "EvansWebster6":
def oscil_func(x, k1): # EvansWebster Functions
y = np.exp(x) * np.sin(k1 * np.cosh(x))
return y
a = 0
b = 2
x = np.linspace(a, b, points)
# Calculate the function
y = np.zeros((points, samples))
for i in range(0, samples):
k1 = np.random.random() * 50 + 25
y[:, i] = oscil_func(x, k1)
# 4 &\int_0^\pi \cos(30x)\cos(30\cos(x)) \dd x \\
# 5 &\int_0^{\pi/2} \sin(x)\cos(\cos(x)) \cos(100 \cos(x)) \dd x \\
# 7 &\int_{-1}^1 \cos(47 \pi x^2/4)\cos(41\pi x /4)\dd x
elif func_str == "RP":
# Define RP
def equation(y0, t):
R, u = y0
return u, (P_g - P_0 - 1317000 * np.cos(2 * np.pi * 26500 * t) - 2 * sigma / R - 4 * miu * u / R + (2 * sigma / R_0 + P_0 - P_g) * (R_0 / R) ** (3 * k)) / (R * rho) - 3 * u**2 / (2 * R)
# parameters
a = 0
b = 2
time = np.linspace(0, b / 1000000, points)
sigma = 0.0725
miu = 8.9 * 10 ** (-4)
P_g = 2330
P_0 = 10000
k = 1.33
# initial conditions
R_0 = 0.0000026
u_0 = 0
x = time * 10**6
def oscil_func(time): # EvansWebster Functions
R_1 = odeint(equation, [R_0, u_0], time)
y = R_1[:, 0] * 10**6
return y
# Calculate the function
y = np.zeros((points, samples))
for i in range(0, samples):
rho = np.random.random(1) * 500 + 500
y[:, i] = oscil_func(time).reshape(
points,
)
elif func_str == "sinx":
def oscil_func(x, k): # Levin paper Bessel function
y = np.sin(k * x)
return y
# Create Bounds for integegration for the Levin paper
a = 1
b = 2
x = np.linspace(a, b, points)
# Calculate the function
y = np.zeros((points, samples))
for i in range(0, samples):
k = np.random.random() * 10 + 25
y[:, i] = oscil_func(x, k)
else:
print("Functions not defined for integration")
# Define the Trapezoidal Integrating Function
# def Integrate_funcs_trapz(x,y,samples):
# I = np.zeros((samples,))
# for i in range(0,samples):
# I[i] = np.trapz(y[:,i], x)
# return I
# Define the Trapezoidal Integrating Function
# Define the Simpson Integrating Function
# def Integrate_funcs_simp(x,y,samples):
# I = np.zeros((samples,))
# for i in range(0,samples):
# I[i] = integrate.simpson(y[:,i], x)
# return I
# Define the Midpoint Integrating Function
def Integrate_funcs_mid(x, y, samples):
I = np.zeros((samples,))
len(x)
dx = (x[len(x) - 1] - x[0]) / (len(x) - 2)
for i in range(0, samples):
I_i = 0
for m in range(1, len(x) - 1):
I_i += y[m, i] * dx
I[i] = I_i
return I
# Integrate the functions
I = Integrate_funcs_mid(x, y, samples)
# Center and Normalize the data Determine
I_max = np.max(np.abs(I))
I_mean = np.mean(I)
I = (I - I_mean) / I_max
# Integrate with smaller bin sizes
Is = np.zeros((samples,))
# Idiff = np.zeros((samples,))
approx_points = 2 ** (exponent_approx) + 1
xs = np.linspace(a, b, approx_points)
# xs = a+ (b-a)*np.random.random(approx_points)
inds = ((xs - 1) * (points - 1)).astype(int)
Is = Integrate_funcs_mid(xs, y[inds, :], samples)
Is = (Is - I_mean) / I_max
Idiff = I - Is
# Error Metric
normalized_MSE = np.mean(Idiff**2, axis=0) / np.mean(I**2)
def Xs(a, b, col_points, total_points):
xs = np.linspace(a, b, col_points)
inds = ((xs - 1) * (total_points - 1)).astype(int)
return xs, inds
xs, inds = Xs(a, b, approx_points, points)
# %% DeepONet implementation
def DeepONet(samples, split, points, approx_points, y, I, inds, neurons, epochs, b_layers):
import deepxde as dde
# define error metrics
def mean_squared_error(y_true, y_pred):
error = np.ravel((y_true - y_pred) ** 2)
return np.mean(error)
def mean_relative_error(y_true, y_pred):
error = np.ravel((((y_true - y_pred) / y_true) ** 2) ** (1 / 2))
return np.mean(error)
X_train0 = np.transpose(y[inds, 0:split])
y_train = I[0:split,].reshape(split, 1)
X_train1 = np.ones(np.size(y_train)).reshape(split, 1)
X_test0 = np.transpose(y[inds, split:samples])
y_test = I[split:samples,].reshape(samples - split, 1)
X_test1 = np.ones(np.size(y_test)).reshape(samples - split, 1)
X_train = (X_train0, X_train1)
X_test = (X_test0, X_test1)
data = dde.data.Triple(X_train=X_train, y_train=y_train, X_test=X_test, y_test=y_test)
m = np.size(y[inds, 0]) # 604*2
print(m)
dim_x = 1
lr = 0.0001
t_layers = 1
activation = "relu"
branch = [neurons] * (b_layers + 1)
branch[0] = m
trunk = [neurons] * (t_layers + 1)
trunk[0] = dim_x
net = dde.maps.DeepONet(
branch,
trunk,
"relu",
"Glorot normal",
use_bias=True,
stacked=False,
) # "relu","Glorot normal", # batch_size =
model = dde.Model(data, net)
model.compile("adam", lr=lr, metrics=[mean_squared_error])
checker = dde.callbacks.ModelCheckpoint("/Users/anshumansinha/Desktop/Project/model/" + save_str + "model.ckpt", save_better_only=False, period=100)
# Training for different input points from 2^4 to 2^11.
# Training will be done for 10,000 epochs.
# isplot = True will generate 10 plots for each simulation.
losshistory, train_state = model.train(epochs=epochs, callbacks=[checker]) # Training Model batch_size = 10000
# For plotting the residuals and the training history: isplot=True will plot
if exponent_approx == 10 or exponent_approx == 6:
dde.saveplot(losshistory, train_state, issave=False, isplot=False)
NN_obs = model.predict(X_train)
NN_test = model.predict(X_test)
NN_obs_Idiff = (
NN_obs.reshape(
split,
)
- I[0:split,]
)
NN_test_Idiff = (
NN_test.reshape(
samples - split,
)
- I[split:samples,]
)
normalized_MSE_NN = np.mean(NN_test_Idiff**2) / np.mean(I[split:samples] ** 2)
normalized_MSE_NN_obs = np.mean(NN_obs_Idiff**2) / np.mean(I[0:split] ** 2)
print("Neuron", neurons)
print("Exponent_approx.", exponent_approx)
print(normalized_MSE_NN)
return normalized_MSE_NN
split = int(samples * 0.75)
NN_MSEs_test = DeepONet(samples, split, points, approx_points, y / np.max(np.abs(y)), I, inds, neurons, epochs, b_layers)
sio.savemat(save_dir + func_str + itr + "_" + str(exponent_approx) + "_blayers_" + str(b_layers) + "_neurons_" + str(neurons) + ".mat", {"NN_MSEs_test": NN_MSEs_test, "normalized_MSE": normalized_MSE})