-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
143 lines (110 loc) · 4.81 KB
/
utils.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
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import numpy as np
from scipy.integrate import simps
def animRealImag(schrodinger):
Nt = schrodinger.psi.shape[1]
fig, axs = plt.subplots(2, 2, figsize=(10, 6), tight_layout=True)
fig.suptitle(r"Time evolution of $Re(\Psi)$ and $Im(\Psi)$")
for ax in axs[-1]:
ax.set_xlabel(r'Position $x$ [a.u.]')
for ax in axs[:, 0]:
ax.set_ylabel('Amplitude')
axs = axs.flatten()
for ax in axs:
ax.set_xlim((schrodinger.x[0], schrodinger.x[-1]))
ax.set_ylim((-1.1 * np.max(abs(schrodinger.psi)), 1.1 * np.max(abs(schrodinger.psi))))
ax.grid()
ax.plot(schrodinger.x, schrodinger.V, c='gray', alpha=0.7)
line_real = axs[1].plot([], [], c='#1f77b4', label=r'Re$(\Psi(x, t))$')[0]
line_imag = axs[3].plot([], [], c='#ff7f0e', label=r'Im$(\Psi(x, t))$')[0]
line_mod_1 = axs[1].plot([], [], 'g-', label=r'$|\Psi(x, t)|^2$')[0]
line_mod_2 = axs[3].plot([], [], 'g-', label=r'$|\Psi(x, t)|^2$')[0]
def update(i):
line_real.set_data(schrodinger.x, np.real(schrodinger.psi[:, i]))
line_imag.set_data(schrodinger.x, np.imag(schrodinger.psi[:, i]))
line_mod_1.set_data(schrodinger.x, np.abs(schrodinger.psi[:, i]) ** 2)
line_mod_2.set_data(schrodinger.x, np.abs(schrodinger.psi[:, i]) ** 2)
return line_real, line_imag, line_mod_1, line_mod_2
anim = FuncAnimation(fig, update, frames=range(Nt), blit=True, interval=10)
# Plot initial wavefunction at t=0
axs[0].plot(schrodinger.x, np.real(schrodinger.psi[:, 0]), c='#1f77b4', label=r'Re$(\Psi(x, t=0))$')
axs[2].plot(schrodinger.x, np.imag(schrodinger.psi[:, 0]), c='#ff7f0e', label=r'Im$(\Psi(x, t=0))$')
for ax in axs:
ax.legend()
return anim
def animModulus(schrodinger):
Nt = schrodinger.psi.shape[1]
fig, ax = plt.subplots()
ax.set_xlabel("Position")
ax.set_ylabel(r"$|\Psi|^2$")
ax.set_xlim(min(schrodinger.x), max(schrodinger.x))
ax.set_ylim(np.min(np.abs(schrodinger.psi)**2)/2, np.max(np.abs(schrodinger.psi)**2)/2)
ax.plot(schrodinger.x, schrodinger.V, "r", label=r"$V$")
lines, = ax.plot([], [], lw=1, color="black", label=r"$|\Psi|^2$")
def update(frame):
lines.set_data(schrodinger.x, np.abs(schrodinger.psi[:, frame])**2)
return lines,
anim = FuncAnimation(fig, update, frames=range(Nt), blit=True, interval=1)
txt = r"$\kappa=$" + str(schrodinger.kappa) + "\n" + r"$\sigma=$" + str(schrodinger.sigma)
ax.legend(title=txt)
return anim
def plotUncertainty(schrodinger):
mean_x = np.zeros(schrodinger.Nt)
mean_x_2 = np.zeros(schrodinger.Nt)
for i in range(schrodinger.Nt):
x = schrodinger.x
y = schrodinger.psi[:, i]
mean_x[i] = simps(x * np.abs(y) ** 2, x=x)
mean_x_2[i] = simps(x**2 * np.abs(y) ** 2, x=x)
plt.plot(schrodinger.t, mean_x_2 - mean_x**2, lw=1, c='black')
plt.xlabel(r"Time $t$ [u.a.]")
plt.ylabel(r"$\langle x^2 \rangle - \langle x \rangle^2$")
plt.title("Uncertainty")
plt.grid(True)
def plotNormalization(schrodinger):
norm = np.zeros(schrodinger.Nt)
for i in range(schrodinger.Nt):
norm[i] = np.trapz(np.abs(schrodinger.psi[:, i]) ** 2, x=schrodinger.x)
plt.plot(schrodinger.t, norm, c="black", lw=1)
plt.grid()
plt.xlabel("Time [a.u.]")
plt.ylabel(r"$\int |\Psi|^2 dx$")
def plotExpectedPosition(schrodinger, ret=False):
plt.title("Expected Position [u.a.]")
plt.xlabel("Time [u.a.]")
plt.ylabel(r"$\langle x \rangle$")
expected = np.zeros(schrodinger.Nt)
for i in range(schrodinger.Nt):
y = schrodinger.psi[:, i]
expected[i] = np.trapz(schrodinger.x * np.abs(y) ** 2, x=schrodinger.x)
if ret == False:
plt.plot(schrodinger.t, expected, c="black", lw=1)
else:
return expected
plt.grid(True)
def plotTimeEvolution(schrodinger):
fig, ax = plt.subplots()
mesh = ax.pcolormesh(schrodinger.x, schrodinger.t, np.abs(schrodinger.psi).T, cmap='gray')
fig.colorbar(
mesh,
ax=ax,
orientation='vertical',
label=r'$|\psi(x)|$',
fraction=0.06, pad=0.02,
)
plt.ylabel("Time [u.a.]")
plt.xlabel("Position [u.a.]")
plt.title("Time Evolution of the packet")
def plotSome(schrodinger):
fig, axs = plt.subplots(ncols=1, nrows=5, sharex=True, sharey=True, figsize=(9, 7))
t_ = 0
dt_ = schrodinger.Nt // axs.size - 1
for ax in axs.flatten():
ax.set_title(f'Time: {t_/schrodinger.Nt} [u.a.]')
ax.plot(schrodinger.x, np.real(schrodinger.psi[:, t_]), label=r"Re($\Psi$)")
ax.plot(schrodinger.x, np.imag(schrodinger.psi[:, t_]), label=r"Im($\Psi$)")
ax.grid(True)
t_ += dt_
ax.legend()
plt.tight_layout()