-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.py
369 lines (251 loc) Β· 13 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
364
365
366
367
368
369
import numpy as np
import math as m
import imageio
from scipy import misc
# This is the main class encapsulating all simulation methods
class wave_simulation_AI:
# Constructor of the class. It takes the following parameters:
# dx_in - size of one pixel in the simulation grid
# dt_in - time step
# sz_x_in - height of the simulation grid / pixels
# sz_y_in - width of the simulation grid / pixels
# steps_in - number of steps in the simulation
# broadcast_func_in - function defining the speed of wave propagation and the elements broadcasting the waves
def __init__(self, dx_in, dt_in, sz_x_in, sz_y_in, steps_in, broadcast_func_in):
self.dx = dx_in
self.dt = dt_in
self.sz_x = sz_x_in
self.sz_y = sz_y_in
self.steps = steps_in
self.broadcast_func = broadcast_func_in
# Inplementation of the laplace operator that is used in the wave equation
# It takes the following parameters:
# u_array - grid containing the displacement values
# dx - step size to be used in the aproximation fo the second derivative
def Laplace(self, u_array, dx):
sz_x = u_array.shape[0]
sz_y = u_array.shape[1]
dx2 = np.zeros((sz_x, sz_y), float)
dy2 = np.zeros((sz_x, sz_y), float)
dx2[1:sz_x - 1, 1:sz_y - 1] = ((u_array[0:(sz_x - 2), 1:(sz_y - 1)] - u_array[1:(sz_x - 1),
1:(sz_y - 1)]) / dx - (
u_array[1:(sz_x - 1), 1:(sz_y - 1)] - u_array[2:sz_x, 1:(sz_y - 1)]) / dx) / dx
dy2[1:sz_x - 1, 1:sz_y - 1] = ((u_array[1:(sz_x - 1), 0:(sz_y - 2)] - u_array[1:(sz_x - 1),
1:(sz_y - 1)]) / dx - (
u_array[1:(sz_x - 1), 1:(sz_y - 1)] - u_array[1:(sz_x - 1), 2:sz_y]) / dx) / dx
return (dx2 + dy2)
# A simple edge detector intended for signification of the steps in the refractive index
# It takes the following parameters:
# c_array - The grid contining the wave propagation speed values for every pixel
def Edge_detect(self, c_array):
dx = np.zeros((sz_x, sz_y), float)
contour = np.zeros((sz_x, sz_y), float)
dx[0:(sz_x - 1), 0:(sz_y - 1)] = (np.abs(c_array[0:(sz_x - 1), 1:(sz_y)] - c_array[1:(sz_x), 1:(sz_y)]) + np.abs(
c_array[1:(sz_x), 0:(sz_y - 1)] - c_array[1:(sz_x), 1:(sz_y)]) > 0)
contour[dx>0]=1
return (contour)
# The method that actually executes the simulation
def run(self):
u_array = np.zeros((self.sz_x, self.sz_y), float)
u_array_v = np.zeros((self.sz_x, self.sz_y), float)
arr_new_r = np.zeros((self.sz_x, self.sz_y), float)
arr_new_g = np.zeros((self.sz_x, self.sz_y), float)
arr_new_b = np.zeros((self.sz_x, self.sz_y), float)
outputdata = np.zeros((self.sz_x, self.sz_y, 3), int)
writer = imageio.get_writer('/Your_out_path_goes_here/test.mp4', fps=30)
for t in range(0, self.steps):
b_el, b_el_mask, c = self.broadcast_func(t)
u_array[b_el_mask == 1] = b_el[b_el_mask == 1]
u_array_v = u_array_v + np.multiply(np.square(c), self.Laplace(u_array, self.dx) * self.dt)
u_array = u_array + u_array_v * self.dt
arr_new = u_array
arr_new[b_el_mask == 1] = 0
arr_new_r = np.maximum(arr_new, 0) / np.max(np.maximum(arr_new, 0) + 1e-10)
arr_new_g = np.minimum(arr_new, 0) / np.min(np.minimum(arr_new, 0) + 1e-10)
arr_new_b[b_el_mask == 1] = 1
arr_new_b[self.Edge_detect(c) == 1] = 1
outputdata[0:self.sz_x, 0:self.sz_y, 0] = arr_new_r[0:self.sz_x, 0:self.sz_y] * 255
outputdata[0:self.sz_x, 0:self.sz_y, 1] = arr_new_g[0:self.sz_x, 0:self.sz_y] * 255
outputdata[0:self.sz_x, 0:self.sz_y, 2] = arr_new_b * 255
outputdata[0, 0, 0] = 1
writer.append_data(outputdata)
# This function was used to obtain the video https://youtu.be/uBiQsoDaGkE?t=2m44s
# The function simulates a passage of a plane wave through a piece material with high refractive index
# It is meant to be passed as the last argument of the wave_simulation_AI constructor
def broadcast_func_lin_wave(t):
broadcast_el = np.zeros((sz_x, sz_y), float)
broadcast_el_mask = np.zeros((sz_x, sz_y), int)
broadcast_el[0:5, 0:1024] = 0
broadcast_el_mask[0:5, 0:1024] = 1
broadcast_el[5:15, 5:(1024-5)] = m.sin(2*m.pi*t*0.008)
broadcast_el_mask[5:15, 5:(1024-5)] = 1
c_const = 6
c = np.ones((sz_x, sz_y), float) * c_const
c[502:542, (200):(1024-200)] = c_const * 0.5
return broadcast_el, broadcast_el_mask, c
# This function simulates a passage of a plane wave through a prism
# It is meant to be passed as the last argument of the wave_simulation_AI constructor
def broadcast_func_prism_wave(t):
broadcast_el = np.zeros((sz_x, sz_y), float)
broadcast_el_mask = np.zeros((sz_x, sz_y), int)
broadcast_el[0:5, 0:1024] = 0
broadcast_el_mask[0:5, 0:1024] = 1
broadcast_el[5:15, 5:(1024 - 5)] = m.sin(2 * m.pi * t * 0.024)
broadcast_el_mask[5:15, 5:(1024 - 5)] = 1
c_const = 6
c = np.ones((sz_x, sz_y), float) * c_const
for y in range(200,1024 - 200):
c[(542-round((y-201)/3)):542, y] = c_const * 0.5
return broadcast_el, broadcast_el_mask, c
# This function simulates a passage of a plane wave through a lens
# It is meant to be passed as the last argument of the wave_simulation_AI constructor
def broadcast_func_circlular_lens(t):
broadcast_el = np.zeros((sz_x, sz_y), float)
broadcast_el_mask = np.zeros((sz_x, sz_y), int)
broadcast_el[0:5, 0:1024] = 0
broadcast_el_mask[0:5, 0:1024] = 1
broadcast_el[5:15, 5:(1024 - 5)] = m.sin(2 * m.pi * t * 0.020)
broadcast_el_mask[5:15, 5:(1024 - 5)] = 1
c_const = 6
c = np.ones((sz_x, sz_y), float) * c_const
for y in range(100,1024 - 100):
c[(542-round(m.pow(m.pow((1024-200)/2,2)-m.pow(-(y-100)+((1024-200)/2),2),0.5))):542, y] = c_const * 0.6
return broadcast_el, broadcast_el_mask, c
# This function simulates a passage of a plane wave through a Fresnel lens
# It is meant to be passed as the last argument of the wave_simulation_AI constructor
def broadcast_func_fresnel(t):
broadcast_el = np.zeros((sz_x, sz_y), float)
broadcast_el_mask = np.zeros((sz_x, sz_y), int)
broadcast_el[0:5, 0:1024] = 0
broadcast_el_mask[0:5, 0:1024] = 1
if (t<800):
broadcast_el[5:15, 5:(1024 - 5)] = m.sin(2 * m.pi * t * 0.024)
broadcast_el_mask[5:15, 5:(1024 - 5)] = 1
c_const = 6
c = np.ones((sz_x, sz_y), float) * c_const
for y in range(100,1024 - 100):
y_full=m.pow(m.pow((1024 - 200) / 2, 2) - m.pow(-(y - 100) + ((1024 - 200) / 2), 2), 0.5)
my_lambda = 2*c_const * 0.5 * (1/0.024)*0.01/0.1
c[(542-round(y_full-round(y_full/(my_lambda)-0.5)*(my_lambda))):542, y] = c_const * 0.5
return broadcast_el, broadcast_el_mask, c
# This function simulates a passage of a plane wave through a blazed grating
# It is meant to be passed as the last argument of the wave_simulation_AI constructor
def broadcast_func_blazed_grading(t):
broadcast_el = np.zeros((sz_x, sz_y), float)
broadcast_el_mask = np.zeros((sz_x, sz_y), int)
broadcast_el[0:5, 0:1024] = 0
broadcast_el_mask[0:5, 0:1024] = 1
broadcast_el[5:15, 5:(1024 - 5)] = m.sin(2 * m.pi * t * 0.024)
broadcast_el_mask[5:15, 5:(1024 - 5)] = 1
c_const = 6
c = np.ones((sz_x, sz_y), float) * c_const
for y in range(200,1024 - 200):
y_full = (542 - round((y - 201) / 3))
my_lambda = 2*c_const * 0.5 * (1 / 0.024) * 0.01 / 0.1
c[(542-round(y_full-round(y_full/(my_lambda)-0.5)*(my_lambda))):542, y] = c_const * 0.5
return broadcast_el, broadcast_el_mask, c
def broadcast_func_phased_antenna_streight(t):
broadcast_el = np.zeros((sz_x, sz_y), float)
broadcast_el_mask = np.zeros((sz_x, sz_y), int)
c_const = 6
c = np.ones((sz_x, sz_y), float) * c_const
my_lambda = c_const * (1 / 0.010) * 0.01 / 0.1
broadcast_el[0:5, 0:1024] = 0
broadcast_el_mask[0:5, 0:1024] = 1
n = 10
for a in range(0, n):
broadcast_el_mask[5:15, int(1024/2-5+my_lambda/2*(a-n/2)):int(1024/2+5+my_lambda/2*(a-n/2))] = 1
broadcast_el[5:15, int(1024/2-5+my_lambda/2*(a-n/2)):int(1024/2+5+my_lambda/2*(a-n/2))] = m.sin(2 * m.pi * t * 0.010)
return broadcast_el, broadcast_el_mask, c
def broadcast_func_phased_antenna_angle(t):
broadcast_el = np.zeros((sz_x, sz_y), float)
broadcast_el_mask = np.zeros((sz_x, sz_y), int)
c_const = 3
c = np.ones((sz_x, sz_y), float) * c_const
my_lambda = c_const * (1 / 0.008) * 0.01 / 0.1
#broadcast_el[100:105, 0:1024] = 0
#broadcast_el_mask[100:105, 0:1024] = 1
n = 16
if (t<1000):
for a in range(0, n):
phase=2 * m.pi / 4 * a
phase = 0
broadcast_el_mask[int(1024/2-1):int(1024/2+1), int(1024/2-1+my_lambda/4*(a)-my_lambda/4*n/2):int(1024/2+1+my_lambda/4*(a)-my_lambda/4*n/2)] = 1
broadcast_el[int(1024/2-1):int(1024/2+1), int(1024/2-1+my_lambda/4*(a)-my_lambda/4*n/2):int(1024/2+1+my_lambda/4*(a)-my_lambda/4*n/2)] = m.sin(2 * m.pi * (t) * 0.008 + phase)
return broadcast_el, broadcast_el_mask, c
def broadcast_func_phased_antenna_focus(t):
broadcast_el = np.zeros((sz_x, sz_y), float)
broadcast_el_mask = np.zeros((sz_x, sz_y), int)
c_const = 6
c = np.ones((sz_x, sz_y), float) * c_const
my_lambda = c_const * (1 / 0.008) * 0.01 / 0.1
broadcast_el[0:5, 0:1024] = 0
broadcast_el_mask[0:5, 0:1024] = 1
n = 10
if (t<1000):
for a in range(0, n):
phase=2 * m.pi * m.sqrt(m.pow(500,2)-m.pow(my_lambda/2*(a-n/2),2))/my_lambda
broadcast_el_mask[5:15, int(1024/2-5+my_lambda/2*(a-n/2)):int(1024/2+5+my_lambda/2*(a-n/2))] = 1
broadcast_el[5:15, int(1024/2-5+my_lambda/2*(a-n/2)):int(1024/2+5+my_lambda/2*(a-n/2))] = m.sin(2 * m.pi * (t) * 0.008 + phase)
return broadcast_el, broadcast_el_mask, c
def broadcast_func_phased_antenna_focus2(t):
broadcast_el = np.zeros((sz_x, sz_y), float)
broadcast_el_mask = np.zeros((sz_x, sz_y), int)
c_const = 6
c = np.ones((sz_x, sz_y), float) * c_const
my_lambda = c_const * (1 / 0.008) * 0.01 / 0.1
broadcast_el[100:105, 0:1024] = 0
broadcast_el_mask[100:105, 0:1024] = 1
n = 10
if (t<1000):
for a in range(0, n):
phase=-2 * m.pi * m.sqrt(m.pow(400,2)-m.pow(my_lambda/2*(a-n/2),2))/my_lambda
broadcast_el_mask[105:115, int(1024/2-5+my_lambda/2*(a-n/2)):int(1024/2+5+my_lambda/2*(a-n/2))] = 1
broadcast_el[105:115, int(1024/2-5+my_lambda/2*(a-n/2)):int(1024/2+5+my_lambda/2*(a-n/2))] = m.sin(2 * m.pi * (t) * 0.008 + phase)
return broadcast_el, broadcast_el_mask, c
def broadcast_func_radar(t):
broadcast_el = np.zeros((sz_x, sz_y), float)
broadcast_el_mask = np.zeros((sz_x, sz_y), int)
c_const = 6
c = np.ones((sz_x, sz_y), float) * c_const
for y in range(-20, 20):
c[(400-y):(400+y),int(1024/2+250-m.sqrt(m.pow(40/2,2)-m.pow(y,2))):int(1024/2+250+m.sqrt(m.pow(40/2,2)-m.pow(y,2)))]=c_const/5
my_lambda = c_const * (1 / 0.024) * 0.01 / 0.1
broadcast_el[0:5, 0:1024] = 0
broadcast_el_mask[0:5, 0:1024] = 1
n = 10
if (t<500):
for a in range(0, n):
phase=2 * m.pi / 4 * a
broadcast_el_mask[5:15, int(1024/2-5+my_lambda/2*(a-n/2)):int(1024/2+5+my_lambda/2*(a-n/2))] = 1
broadcast_el[5:15, int(1024/2-5+my_lambda/2*(a-n/2)):int(1024/2+5+my_lambda/2*(a-n/2))] = m.sin(2 * m.pi * (t) * 0.024 + phase)
if ((t>1000)&(t<1500)):
for a in range(0, n):
phase=-2 * m.pi / 4 * a
broadcast_el_mask[5:15, int(1024/2-5+my_lambda/2*(a-n/2)):int(1024/2+5+my_lambda/2*(a-n/2))] = 1
broadcast_el[5:15, int(1024/2-5+my_lambda/2*(a-n/2)):int(1024/2+5+my_lambda/2*(a-n/2))] = m.sin(2 * m.pi * (t) * 0.024 + phase)
return broadcast_el, broadcast_el_mask, c
waveguide_img = misc.imread('/Path_to_your_image_goes_here/kepler_telescope.bmp')
def broadcast_func_image(t):
broadcast_el = np.zeros((sz_x, sz_y), float)
broadcast_el_mask = np.zeros((sz_x, sz_y), int)
broadcast_el[0:5, 0:1024] = 0
broadcast_el_mask[0:5, 0:1024] = 1
broadcast_el[5:15, 5:(1024 - 5)] = m.sin(2 * m.pi * t * 0.024)
broadcast_el_mask[5:15, 5:(1024 - 5)] = 1
c_const = 3
c = np.ones((sz_x, sz_y), float) * c_const
c[(waveguide_img[0:1024,0:1024,2]/255)>0.5] = c_const * 0.6
#my_lambda = c_const * (1 / 0.008) * 0.01 / 0.1
#broadcast_el[112:116, (66-20):(66+20)] = 0
#broadcast_el_mask[112:116, (66-20):(66+20)] = 1
#broadcast_el_mask[116:118, (66-5):(66+5)] = 1
#broadcast_el[116:118, (66-5):(66+5)] = m.sin(2 * m.pi * (t) * 0.008)
return broadcast_el, broadcast_el_mask, c
dx = 0.1
dt = 0.01
sz_x = 1024
sz_y = 1024
steps = 3000
my_sim = wave_simulation_AI(dx, dt, sz_x, sz_y, steps, broadcast_func_image)
my_sim.run()