This repository was archived by the owner on Mar 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaths.py
More file actions
171 lines (145 loc) · 4.65 KB
/
Copy pathmaths.py
File metadata and controls
171 lines (145 loc) · 4.65 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
if __name__ == "__main__":
print("main.py should be started instead")
exit()
import numpy as np
# Calculation range (nm)
x_min = -8
x_max = 10
calculations = 1000
# Potential
default_V_0 = 0 # (eV)
V_0 = default_V_0
default_V_barrier = 1
V_barrier = default_V_barrier
default_V_1 = 0
V_1 = default_V_1
default_barrier_start = 0 # (nm)
barrier_start = default_barrier_start
default_barrier_end = 2
barrier_end = default_barrier_end
potential = [[], []] # Potential plot data
# Energy
default_E = 1
E = default_E
E_min = -2
E_max = 2
E_limit = 0.0
energy = [[x_min, x_max], []] # Potential plot data
# Constants
k_s = 0
k_b = 0
K_b = 0
k_e = 0
A = 0
B = 0
R = 0
T = 0
# Wave function
psi_min = -4
psi_max = 4
psi = [[], []]
# Wave
wave_packet = False
default_k_0 = 6
k_0 = default_k_0
k_0_min = 1
k_0_max = 15
alpha = 1
omega = k_0**2 * alpha / 2
default_a = 1
a = default_a
a_min = 0.5
a_max = 5
delta_start = -4
# Time
default_t = 0
t = default_t
t_min = 0
t_max = 5
def calculate_potential():
""" Calculates the value of the potential """
global potential
potential[0] = [x_min, barrier_start, barrier_start, barrier_end, barrier_end, x_max]
potential[1] = [V_0, V_0, V_barrier, V_barrier, V_1, V_1]
def calculate_energy():
""" Calculates the value of the potential """
global energy
energy[1] = [E, E]
def calculate_wave_function():
""" Calculates the wave function """
global psi
calculate_constants()
psi[0] = np.linspace(x_min, x_max, calculations)
psi[1] = []
time_factor = np.exp(-1j * omega * t)
for x in psi[0]:
psi[1].append(wave_function_value(x) * time_factor)
def calculate_constants():
""" Calculate needed constants for the wave function """
global k_s, k_b, K_b, k_e, A, B, R, T, E
if E - V_0 <= 0:
return
k_s = np.sqrt((E - V_0) / E) * k_0
k_b = np.sqrt((E - V_barrier) / E) * k_0
K_b = np.sqrt((V_barrier - E) / E) * k_0
k_e = np.sqrt((E - V_1) / E + 0j) * k_0 # + Oj added to allow square root calculation for negative numbers
x_s = barrier_start
x_e = barrier_end
# Preventing division by 0 (good enough for plotting)
if k_e == 0:
k_e = 0.000001
if k_s == k_b:
k_s = k_b + 0.000001
if E - V_barrier > 0: # Case 1
A = 1 / \
((k_b / k_e - 1) / (k_b / k_e + 1) * np.exp(2 * 1j * k_b * x_e) + (1 + k_b / k_s) / (1 - k_b / k_s) * np.exp(2 * 1j * k_b * x_s)) \
* (2 * np.exp(1j * k_s * k_b * x_s)) / (1 - k_b / k_s)
B = (k_b / k_e - 1) / (k_b / k_e + 1) * A * np.exp(2 * 1j * k_b * x_e)
R = np.exp(1j * k_s * x_s) * (A * np.exp(1j * k_b * x_s) + B * np.exp(-1j * k_b * x_s) - np.exp(1j * k_s * x_s))
T = np.exp(-1j * k_e * x_e) * (A * np.exp(1j * k_b * x_e) + B * np.exp(-1j * k_b * x_e))
elif E - V_barrier < 0: # Case 2
A = 1 / \
(
(1j * k_e * np.sinh(K_b * x_e) - K_b * np.cosh(K_b * x_e)) / (
K_b * np.sinh(K_b * x_e) - 1j * k_e * np.cosh(K_b * x_e))
+ (1j * k_s * np.sinh(K_b * x_s) + K_b * np.cosh(K_b * x_s)) / (
1j * k_s * np.cosh(K_b * x_s) + K_b * np.sinh(K_b * x_s))
) \
* 2 * 1j * k_s * np.exp(1j * k_s * x_s) / \
(1j * k_s * np.cosh(K_b * x_s) + K_b * np.sinh(K_b * x_s))
B = A * \
(1j * k_e * np.sinh(K_b * x_e) - K_b * np.cosh(K_b * x_e)) / \
(K_b * np.sinh(K_b * x_e) - 1j * k_e * np.cosh(K_b * x_e))
R = np.exp(1j * k_s * x_s) * (A * np.sinh(K_b * x_s) + B * np.cosh(K_b * x_s) - np.exp(1j * k_s * x_s))
T = np.exp(-1j * k_e * x_e) * (A * np.sinh(K_b * x_e) + B * np.cosh(K_b * x_e))
else: # E - V_barrier == 0 # Case 3
A = 2 * np.exp(1j * k_s * x_s) / \
(x_s - x_e + 1 / (1j * k_s) + 1 / (1j * k_e))
B = A * (1 / (1j * k_e) - x_e)
R = np.exp(1j * k_s * x_s) * (np.exp(1j * k_s * x_s) - A / (1j * k_s))
T = A / (1j * k_e * np.exp(1j * k_e * x_e))
def gaussian(x, x_start, direction=1):
if wave_packet:
k = k_0
v = alpha * k
w = a ** 2 + 1j * alpha * t / 2
x = x - x_start
return np.exp(-((direction * x - v*t)**2)/(4*w))
else:
return 1
def wave_function_value(x):
""" Returns value of the wave function at a x value """
if E - V_0 <= 0:
return 0
else: # E - V_0 > 0
if x < barrier_start:
return gaussian(x, barrier_start + delta_start, 1) * np.exp(1j * k_s * x) + gaussian(x, barrier_start - delta_start, -1) * R * np.exp(-1j * k_s * x)
elif barrier_start <= x <= barrier_end:
if E - V_barrier > 0:
return gaussian(x, barrier_start + delta_start, 1) * (A * np.exp(1j * k_b * x) + B * np.exp(-1j * k_b * x))
elif E - V_barrier < 0:
return gaussian(x, barrier_start + delta_start, 1) * (A * np.sinh(K_b * x) + B * np.cosh(K_b * x))
else: # E - V_barrier = 0
return gaussian(x, barrier_start + delta_start, 1) * (A * x + B)
else: # x > barrier_end
return gaussian(x, barrier_start + delta_start, 1) * T * np.exp(1j * k_e * x)