-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWave_handler.py
364 lines (209 loc) · 8.09 KB
/
Wave_handler.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
import matplotlib.pyplot as plt
import numpy as np
import matplotlib
import math as ma
import pylab
class Wave_function_handler:
def __init__(self, k_0, x, x_s, m, L, w, sigma, hbar, E, dt, del_x, N_x, rho, v_g):
self.__k_0 = k_0
self.__x = x
self.__x_s = x_s
self.__m = m
self.__L = L
self.__w = w
self.__sigma = sigma
self.__hbar = hbar
self.__E = E
self.__dt = dt
self.__del_x = del_x
self.__N_x = N_x
self.__rho = rho
self.__v_g = v_g
def get_del_x(self):
return self.__del_x
def get_k_0(self):
return self.__k_0
def get_E(self):
return self.__E
def get_x(self):
return self.__x
def get_hbar(self):
return self.__hbar
def get_x_s(self):
return self.__x_s
def get_m(self):
return self.__m
def get_L(self):
return self.__L
def get_w(self):
return self.__w
def get_sigma(self):
return self.__sigma
def get_dt(self):
return self.__dt
def get_N_x(self):
return self.__N_x
def get_rho(self):
return self.__rho
def get_v_g(self):
return self.__v_g
def set_sigma(self, sigma):
self.__sigma = sigma
def set_del_x(self, del_x):
self.__del_x = del_x
def set_k_o(self, k_o):
self.__k_o = k_o
def set_w(self, w):
self.__w = w
def set_x_s(self, x_s):
self.__x_s = x_s
def set_hbar(self, hbar):
self.__hbar = hbar
def set_x(self, x):
self.__x = x
def set_e(self, E):
self.__E = E
def set_m(self, m):
self.__m = m
def set_N_x(self, N_x):
self.__N_x = N_x
def set_rho(self, rho):
self.__rho = rho
def set_v_g(self, v_g):
self.__v_g = v_g
#Constant C for norm
def c_factor(self):
return 1 / ((np.pi * self.get_sigma() ** 2) ** (1 / 4))
# Funksjoner ============================================================================================================#
#Potensiale
def V_func(self, x, c, b):
l = int(b / self.get_del_x())
V_list = [0.0] * int(self.get_N_x() / 2 + self.get_N_x() % 2 - l / 2 - l % 2)
V_list += [c * self.get_E()] * (l)
V_list += [0.0] * int(self.get_N_x() / 2 - l / 2)
return np.array(V_list)
#gauss
def gaussian(self):
return self.c_factor() * np.exp(-(self.get_x() - self.get_x_s()) ** (2) / (2*self.get_sigma() ** (2)))
#Real and Imag
def calc_psi_r(self):
return self.gaussian() * np.cos(self.get_k_0() * self.get_x())
def calc_psi_i(self):
return self.gaussian() * np.sin(self.get_k_0() * self.get_x() - self.get_w() * self.get_dt() / 2)
def calc_matrix(self):
new_psi_r = self.calc_psi_r()
new_psi_i = self.calc_psi_i()
new_psi_r[0] = 0
new_psi_r[-1] = 0
new_psi_i[0] = 0
new_psi_i[-1] = 0
return new_psi_r + 1j * new_psi_i
def Psi_plot(self, x, Psi_R, Psi_I, plot_squared=False):
matplotlib.rcParams.update({'font.size': 22})
plt.figure(figsize=(15, 10))
plt.plot(x, Psi_R,'m', label='$\Psi_R$')
plt.plot(x, Psi_I,'lightgreen', label='$\Psi_I$')
plt.legend()
plt.show()
if plot_squared:
Psi = Psi_R ** 2 + Psi_I ** 2
plt.figure(figsize=(15, 10))
plt.plot(x, Psi, label='$|\Psi(x,t=T)|^2$')
plt.legend()
plt.show()
#Udef
def Psi_propagate(self, c, b, plot=False, plot_squared=False):
# Define the last remaining variables
T = self.get_L() / (2 * self.get_v_g())
Nt = int(T / self.get_dt())
# Defining arrays
V = self.V_func(self.get_x(), c, b)
Psi = self.calc_matrix()
PsiR0 = Psi.real
PsiI0 = Psi.imag
#To be iterated over
PsiR = Psi.real
PsiI = Psi.imag
# Defining matrices
A = -2 * np.diagflat([0.0] + [1.0] * (self.get_N_x() - 2) + [0.0])
A += np.diagflat([0.0] + [1.0] * (self.get_N_x() - 2), 1)
A += np.diagflat([1.0] * (self.get_N_x() - 2) + [0.0], -1)
A = A * (self.get_hbar() * self.get_dt() / (2 * self.get_m() * self.get_del_x() ** 2))
P = np.diagflat(V) # I will assume that V is always zero at the endpoints.
P = P * (self.get_dt() / self.get_hbar())
Matrix = - A + P
# Propagating wave function
for t in range(Nt):
PsiR += Matrix.dot(PsiI)
PsiI += - Matrix.dot(PsiR)
if t % 100 == 0:
C = np.sqrt(sum(PsiR ** 2 + PsiI ** 2) * self.get_del_x())
PsiR = PsiR/C #normalization preservation
PsiI = PsiI/C
if plot:
self.Psi_plot(self.get_x(), PsiR0, PsiI0, plot_squared=plot_squared)
self.Psi_plot(self.get_x(), PsiR, PsiI, plot_squared=plot_squared)
return PsiR, PsiI
def probabilities_ref_tra(self, PsiR, PsiI):
Psi_squared = PsiR ** 2 + PsiI ** 2
if self.get_N_x() % 2 == 1:
p_ref = (sum(Psi_squared[:int(self.get_N_x() / 2)]) + 0.5 * Psi_squared[int(self.get_N_x() / 2)]) * self.get_del_x()
p_tra = (sum(Psi_squared[int(self.get_N_x() / 2 + 1):]) + 0.5 * Psi_squared[int(self.get_N_x() / 2)]) * self.get_del_x()
else:
p_ref = sum(Psi_squared[:self.get_N_x() / 2]) * self.get_del_x()
p_tra = sum(Psi_squared[self.get_N_x() / 2:]) * self.get_del_x()
return p_ref, p_tra
def problem_1(self):
#sigma = 1.0
self.Psi_propagate(0, 0, plot=True, plot_squared=True)
def problem_2(self):
self.Psi_propagate(0, 0, plot=True, plot_squared=False)
def problem_3(self):
c = 0.5
width = self.get_L() / 50
PsiR, PsiI = self.Psi_propagate(c, width, plot=False, plot_squared=False)
x = np.linspace(0.0, self.get_L(), self.get_N_x())
V = self.V_func(x, c, width)
plt.figure(figsize=(15, 10))
plt.plot(x, PsiR, label='$\Psi_R$')
plt.plot(x, PsiI, label='$\Psi_I$')
plt.plot(x, V / self.get_E(), label='$V$', linewidth=2)
plt.legend()
plt.show()
p_ref, p_tra = self.probabilities_ref_tra(PsiR, PsiI)
print("Probabilities of ")
print("reflection: \t", p_ref)
print("transmission: \t", p_tra)
print("total: \t\t", p_ref + p_tra)
def problem_4(self):
p_ref_list = []
p_tra_list = []
b = self.get_L() / 50
c_list = np.linspace(0.0, 1.5, 50)
for c in c_list:
PsiR, PsiI = self.Psi_propagate(c, b, plot=False, plot_squared=False)
p_ref, p_tra = self.probabilities_ref_tra(PsiR, PsiI)
p_ref_list.append(p_ref)
p_tra_list.append(p_tra)
plt.figure(figsize=(15, 10))
plt.plot(c_list, p_ref_list, 'm', label='$p_r$')
plt.plot(c_list, p_tra_list, 'lightgreen', label='$p_t$')
plt.xlabel("$V_{max} / E$")
plt.legend()
plt.show()
def problem_5(self):
p_ref_list = []
p_tra_list = []
c = 0.9
b_list = np.linspace(0.0, self.get_L() / 20, 50)
for b in b_list:
PsiR, PsiI = self.Psi_propagate(c, b, plot=False, plot_squared=False)
p_ref, p_tra = self.probabilities_ref_tra(PsiR, PsiI)
p_ref_list.append(p_ref)
p_tra_list.append(p_tra)
plt.figure(figsize=(15, 10))
plt.plot(b_list, p_ref_list, 'm', label='$p_r$')
plt.plot(b_list, p_tra_list, 'lightgreen', label='$p_t$')
plt.xlabel("potential width $b$")
plt.legend()
plt.show()