-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathap50.py
More file actions
294 lines (247 loc) · 10.8 KB
/
ap50.py
File metadata and controls
294 lines (247 loc) · 10.8 KB
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
#required libraries
import math
import numpy as np
import matplotlib.pyplot as plt
import sys
from PyQt5 import uic
import os
from PyQt5.QtWidgets import QApplication, QMainWindow
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt5agg \
import FigureCanvasQTAgg as FigureCanvas
import psutil as p
Form = uic.loadUiType(os.path.join(os.getcwd(), 'gui.ui'))[0]
# constants:
MaxTime = 100 # Duration of simulation
# Required constants and variables
fp = 20.0E9 # Electromagnetic wave Frequency
A = 2.0 # Sine source magnitude
c = 3E8 # speed of EM wave
landa = c / fp # EM wavelength
d = 1.0 # distance in exercise (in m)
dx = landa / 2 # Spatial discretisation step, at least 20 samples per wavelength
nd = math.ceil(d / dx) # number of grids in d
# Room Dimention
SizeX = nd + 1 # X position of the room
SizeY = nd + 1 # Y position of the room
#print(SizeY)
# Source Position
xsrc = math.floor(SizeX / 3)
ysrc = math.floor(SizeY / 3)
# Wall position
wx1 = 50
wx2 = 100
wy1 = 65
wy2 = 70
# remaining useful constants and variables
Cdtds = 1.0 / math.sqrt(2.0) # Courant number %Courant stability factor
delta = 1e-3
deltat = Cdtds * delta / c
cn = 1 / math.sqrt(2)
dt = cn * dx / c / math.sqrt(2) # Time
mu_0 = 4.0 * np.pi * 1.0e-7; # Permeability of free space
eps_0 = 8.8542e-12;
imp0 = np.sqrt(mu_0 / eps_0) # 377.0
#print("we are after consts")
class main(FigureCanvas):
def __init__(self):
# Initializing the values of Electronic and magnetic wave
self.Ez = [[0.0 for mm in range(0, SizeX + 1)] for nn in range(0, SizeY + 1)]
self.Hx = [[0.0 for mm in range(0, SizeX + 1)] for nn in range(0, SizeY + 1)]
self.Hy = [[0.0 for mm in range(0, SizeX + 1)] for nn in range(0, SizeY + 1)]
self.zarib(2)
self.PML_init()
# first image setup
self.fig = Figure()
self.ax = self.fig.add_subplot(111)
FigureCanvas.__init__(self, self.fig)
self.ax.set_xlim(0, 200)
self.ax.set_ylim(0, 100)
# and disable figure-wide autoscale
self.ax.set_autoscale_on(False)
# generates first "empty" plots
self.user = []
self.l_user, = self.ax.plot([], self.user, label='User %')
self.ax.legend()
self.y, self.x = np.mgrid[range(SizeX), range(SizeY)]
print("sizey",type(self.Ez[:-1, :-1]))
self.mesh = self.ax.pcolormesh(self.x, self.y, self.Ez[:-1, :-1],
cmap='RdBu', vmin=-0.01, vmax=0.01)
# self.mesh.set_array(np.zeros((SizeX, SizeY)).ravel())
self.fig.canvas.draw()
# initialize the iteration counter
self.cnt = 0
# call the update method (to speed-up visualization)
print("before timer")
self.timerEvent(None)
# start timer, trigger event every 200 millisecs
self.timer = self.startTimer(200)
def zarib(self, condition):
if condition == 0:
pass
else:
self.Cezh = [[Cdtds * imp0 for mm in range(0, SizeX + 1)] for nn in range(0, SizeY + 1)]
self.Ceze = [[1.0 for mm in range(0, SizeX + 1)] for nn in range(0, SizeY + 1)]
self.Chxh = [[1.0 for mm in range(0, SizeX + 1)] for nn in range(0, SizeY + 1)]
self.Chxe = [[Cdtds / imp0 for mm in range(0, SizeX + 1)] for nn in range(0, SizeY + 1)]
self.Chyh = [[1.0 for mm in range(0, SizeX + 1)] for nn in range(0, SizeY + 1)]
self.Chye = [[Cdtds / imp0 for mm in range(0, SizeX + 1)] for nn in range(0, SizeY + 1)]
# ezInc Fn Defining the value of a sine wave generator
def ezInc(self, t):
return A * np.sin(2.0 * np.pi * fp * t)
def PML_init(self):
########################################
# PML CHANGES
npmls = 10 # Depth of PML region in # of cells
ip = SizeX - npmls
jp = SizeY - npmls
# ***********************************************************************
# Set up the Berenger's PML material constants
# ***********************************************************************
sigmax = -3.0 * eps_0 * c * np.log(1.0e-5) / (2.0 * dx * npmls)
rhomax = sigmax * (imp0 ** 2)
sig = [sigmax * ((m - 0.5) / (npmls + 0.5)) ** 2 for m in range(1, npmls + 1)]
rho = [rhomax * (m / (npmls + 0.5)) ** 2 for m in range(1, npmls + 1)]
# print(len(rho), 'after sig')
# ***********************************************************************
# Set up constants for Berenger's PML
# ***********************************************************************
re = [sig[m] * dt / eps_0 for m in range(0, npmls)]
rm = [rho[m] * dt / mu_0 for m in range(0, npmls)]
ca = [np.exp(-re[m]) for m in range(0, npmls)]
cb = [-(np.exp(-re[m]) - 1.0) / sig[m] / dx for m in range(0, npmls)]
da = [np.exp(-rm[m]) for m in range(0, npmls)]
db = [-(np.exp(-rm[m]) - 1.0) / rho[m] / dx for m in range(0, npmls)]
# ***********************************************************************
# Initialise all matrices for the Berenger's PML
# ***********************************************************************
# <<<<<<<<<<<<<<<<<<<<<<<<<<<<< Ez Fields >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
# ..... Left and Right PML Regions .....
for i in range(1, nd + 1): # =2:nd
for j in range(3, npmls + 2): # =2:npmls+1
m = npmls + 2 - j
self.Ceze[i][j] = ca[m]
self.Cezh[i][j] = cb[m]
for j in range(jp, nd - 1): # =jp+1:nd
m = j - jp
self.Ceze[i][j] = ca[m]
self.Cezh[i][j] = cb[m]
# ..... Front and Back PML Regions .....
for j in range(1, nd + 1): # =2:nd
for i in range(3, npmls + 2): # =2:npmls+1
m = npmls + 2 - i
self.Ceze[i][j] = ca[m]
self.Cezh[i][j] = cb[m]
for i in range(ip, nd - 1): # =ip+1:nd
m = i - ip
self.Ceze[i][j] = ca[m]
self.Cezh[i][j] = cb[m]
# <<<<<<<<<<<<<<<<<<<<<<<<<<<<< Hx Fields >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
# ..... Left and Right PML Regions .....
for i in range(1, nd + 1): # =2:nd
for j in range(2, npmls + 1): # =1:npmls
m = npmls + 1 - j
self.Chxh[i][j] = da[m]
self.Chxe[i][j] = db[m]
for j in range(jp, nd - 1): # =jp+1:nd
m = j - jp
self.Chxh[i][j] = da[m]
self.Chxe[i][j] = db[m]
# <<<<<<<<<<<<<<<<<<<<<<<<<<<<< Hy Fields >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
# ..... Front and Back PML Regions .....
for j in range(1, nd + 1): # =2:nd
for i in range(2, npmls + 1): # =1:npmls
m = npmls + 1 - i
self.Chyh[i][j] = da[m]
self.Chye[i][j] = db[m]
for i in range(ip, nd - 1): # =ip+1:nd
m = i - ip
self.Chyh[i][j] = da[m]
self.Chye[i][j] = db[m]
########################################
# Initializing existance of wave in the room (0)
self.room = [[0.0 for mm in range(0, SizeX + 1)] for nn in range(0, SizeY + 1)]
# Initializing existance of wall in the room (0)
self.wall = [[0.0 for mm in range(0, SizeX + 1)] for nn in range(0, SizeY + 1)]
#print('{:<4}'.format('Size X , Y'), len(self.Hy) - 1, len(self.Hy[0]) - 1)
def update(self):
# plt.ion()
# Update magnetic field
for mm in range(0, SizeX + 1):
# Periodic Boundary condition for X axis
if mm == SizeX:
for nn in range(0, SizeY + 1):
self.Hy[mm][nn] = self.Chyh[mm][nn] * self.Hy[mm][nn] + \
self.Chye[mm][nn] * (self.Ez[0][nn] - self.Ez[mm][nn])
else:
for nn in range(0, SizeY + 1):
self.Hy[mm][nn] = self.Chyh[mm][nn] * self.Hy[mm][nn] + \
self.Chye[mm][nn] * (self.Ez[mm + 1][nn] - self.Ez[mm][nn])
for mm in range(0, SizeX + 1):
for nn in range(0, SizeY + 1):
# Periodic Boundary condition for Y axis
if nn == SizeY:
self.Hx[mm][nn] = self.Chxh[mm][nn] * self.Hx[mm][nn] - \
self.Chxe[mm][nn] * (self.Ez[mm][0] - self.Ez[mm][nn])
else:
self.Hx[mm][nn] = self.Chxh[mm][nn] * self.Hx[mm][nn] - \
self.Chxe[mm][nn] * (self.Ez[mm][nn + 1] - self.Ez[mm][nn])
# Update electrical field
for mm in range(0, SizeX + 1):
for nn in range(0, SizeY + 1):
self.Ez[mm][nn] = self.Ceze[mm][nn] * self.Ez[mm][nn] + \
self.Cezh[mm][nn] * (
(self.Hy[mm][nn] - self.Hy[mm - 1][nn]) - (self.Hx[mm][nn] - self.Hx[mm][nn - 1]))
# Defining wall
# for mm in range(wx1, wx2):
# for nn in range(wy1, wy2):
# self.wall[mm][nn] = 2
# self.Ez[mm][nn] = 0
# Finilize the room value = self.wall + self.Ez
# self.room = [[self.wall[mm][nn] + self.Ez[mm][nn] for mm in range(0, SizeX + 1)] for nn in range(0, SizeY + 1)]
# Plotting the room
# plt.ion()
# if Time % 2 == 0:
# img = plt.imshow(self.room, vmax=A, vmin=-A)
# plt.colorbar(img)
# plt.pause(0.0002)
# plt.clf()
return self.Ez
def timerEvent(self, evt):
self.Ez[math.ceil(xsrc)][math.ceil(ysrc)] = self.ezInc(deltat * self.cnt)
result = self.update()
print(type(self.Ez), self.cnt)
# # append new data to the data sets
# self.user.append(result)
# # update lines data using the lists with new data
# self.l_user.set_data(range(len(self.user)), self.user)
# # force a redraw of the Figure
# self.mesh.set_array(self.Ez[:-1, :-1].ravel())
self.fig.canvas.draw()
print("hello")
if self.cnt == MaxTime:
# stop the timer
self.killTimer(self.timer)
else:
self.cnt += 1
print("plus")
class PlotWindow(QMainWindow, Form):
def __init__(self):
QMainWindow.__init__(self)
Form.__init__(self)
self.setupUi(self)
def start_clicked(self):
print("after click")
widget = main()
widget.setWindowTitle("start")
widget.show()
# # create the GUI application
# app = QApplication(sys.argv)
# app.setStyle("Fusion")
# w = PlotWindow()
#
# w.start_Button.clicked.connect(PlotWindow.start_clicked)
#
# w.show()
# sys.exit(app.exec_())
main()