-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolverClass.py
328 lines (219 loc) · 10.2 KB
/
solverClass.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
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as an
from abc import abstractmethod
import scipy
class quatumParticaleSolver:
def __init__(self, dx = 0.1, dt = 1e-4, center = 2, potential = lambda x: 0, numOfPoints = 1024, a = 1, startX=-10 , endX = 30, m = 1.0, h = 1, k = 8) -> None:
self.dt = dt
self.potential = potential
self.k = k
self.startX = startX
self.endX = endX
self.m = m
self.h = h
self.currentTimeStep = 1
# gaussian wave packet parameters
self.a = a
self.center = center
# The x interval array
self.inputX = np.linspace(startX, endX, numOfPoints, dtype=complex)
# Size and spatial step size
self.dx = np.real(self.inputX[1]-self.inputX[0])
self.N = self.inputX.shape[0]
# The wave function variables
self.psiX = np.copy(self.inputX)
self.psiP = np.copy(self.inputX)
self.V = np.array(list(map(self.potential, np.real(self.inputX)))) # The potential energy function array
# Animation matplotlib varaiables
self.fig, self.ax = None, None
self.upperLimit = 1.5
self.lowerLimit = 1.5
def initilizePsi(self):
gaussianPacket = lambda x: ((2/(self.a*self.a*np.pi))**(1/4)*np.exp(1j*self.k*(x-self.center))*np.exp(-(x-self.center)**2/(self.a*self.a)))
gaussianPacketMomentum = lambda x: ((self.a/(2*np.pi))**(1/4))*np.exp((-self.a/4)*(x-self.k)**2)
for i in range(self.N):
self.psiX[i] = gaussianPacket(self.psiX[i])
self.psiP[i] = gaussianPacketMomentum(self.psiP[i])
@abstractmethod
def computeNextTimeStep(self):
pass
def animate(self, i):
self.ax.clear()
self.ax.set_xlim(self.inputX[0],self.inputX[-1])
self.ax.set_ylim(self.lowerLimit, self.upperLimit)
self.computeNextTimeStep() # compute the next time step of the wavefunction and plot it
Psi = self.psiX
line = self.ax.plot(np.real(self.inputX), Psi.real, label = "real(ψ)")
self.ax.plot(np.real(self.inputX), Psi.imag, label = "im(ψ)")
self.ax.plot(np.real(self.inputX), np.absolute(Psi), label = "|ψ|^2")
V = np.array(list(map(self.potential, np.real(self.inputX))))
self.ax.plot(np.real(self.inputX), V, label = "potential function V(x)")
plt.legend()
return line
def draw(self, frames: int = 300, videoName: str = "schrodingerEquationSolution.mp4", upperYLimit: float = 1.5, lowerYLimit: float = -1.5):
'''
Args:
frames: int = 300 -> How many frames to animate
videoName: str = "schrodingerEquationSolution.mp4" -> The name of the output video file
upperYLimit and lowerYLimit -> The lower and upper limit on the plot
Return:
None
'''
plt.rc('lines')
self.fig, self.ax = plt.subplots()
print(f"X step = {self.dx}, time step = {self.dt}")
self.upperLimit = upperYLimit
self.lowerLimit = lowerYLimit
animator = an.FuncAnimation(self.fig, func=self.animate,
frames = frames,
interval = 100,
blit = False)
animator.save(videoName, writer = 'ffmpeg', fps = 30)
class quatumParticaleTridig(quatumParticaleSolver):
def __init__(self, dx = 0.1, dt = 1e-4, center = 2, potential = lambda x: 0, numOfPoints = 1024, a = 1, startX=-10 , endX = 30, m = 1.0, h = 1, k = 8) -> None:
super(quatumParticaleTridig, self).__init__(dx, dt, center, potential, numOfPoints, a, startX, endX, m, h, k)
V = np.array(list(map(self.potential, np.real(self.inputX))))
inital = np.ones((self.N),complex)
alp = (1j)*self.dt/(2*self.dx**2)*inital
xi = inital + 1j*self.dt/2*(2/(self.dx**2)*inital + V)
diags = np.array([-1,0,+1]) # positions of the vectors in the matrix
vecs1 = np.array([-alp,xi,-alp])
self.U1 = scipy.sparse.spdiags(vecs1,diags,self.N,self.N)
self.U1 = self.U1.tocsc()
self.LU = scipy.sparse.linalg.splu(self.U1)
self.U2 = np.zeros((self.N,self.N), complex)
# self.U1 = np.zeros((self.N,self.N), complex)
alpha = 1j*self.dt/(2*self.dx**2)
# U1mid = np.copy(1+(1j*self.dt/2)*((2/self.dx**2)+V))
# U1Up = np.full(self.N, -alpha)
# U1Down = np.full(self.N, -alpha)
for i in range(self.N):
# U1[i,i] = 1+(1j*self.dt/2)*((2/self.dx**2)+V[i])
self.U2[i,i] = 1-(1j*self.dt/2)*((2/self.dx**2)+V[i])
if i != 0 and i != self.N-1:
self.U2[i+1,i] = alpha
self.U2[i-1,i] = alpha
# U1[i+1,i] = -alpha
# U1[i-1,i] = -alpha
self.U2[1,0] = alpha
self.U2[self.N-2, self.N-1] = alpha
# U1[1,0] = -alpha
# U1[self.N-2, self.N-1] = -alpha
def computeNextTimeStep(self):
# V = np.array(list(map(self.potential, np.real(self.inputX))))
# U2 = np.zeros((self.N,self.N), complex)
# U1 = np.zeros((self.N,self.N), complex)
# alpha = 1j*self.dt/(2*self.dx**2)
# U1mid = np.copy(1+(1j*self.dt/2)*((2/self.dx**2)+V))
# U1Up = np.full(self.N, -alpha)
# U1Down = np.full(self.N, -alpha)
# for i in range(self.N):
# U1[i,i] = 1+(1j*self.dt/2)*((2/self.dx**2)+V[i])
# U2[i,i] = 1-(1j*self.dt/2)*((2/self.dx**2)+V[i])
# if i != 0 and i != self.N-1:
# U2[i+1,i] = alpha
# U2[i-1,i] = alpha
# U1[i+1,i] = -alpha
# U1[i-1,i] = -alpha
# U2[1,0] = alpha
# U2[self.N-2, self.N-1] = alpha
# U1[1,0] = -alpha
# U1[self.N-2, self.N-1] = -alpha
psiTemp = [email protected]
# self.psiX = self.TDMAsolver(U1Down, U1mid, U1Up, psiTemp)
self.psiX = self.LU.solve(psiTemp)
print(f"Time step {self.currentTimeStep} finished, Time = {self.currentTimeStep*self.dt}")
self.currentTimeStep += 1
return self.psiX
def TDMAsolver(self, a, b, c, d):
'''
TDMA solver, a b c d can be NumPy array type or Python list type.
refer to http://en.wikipedia.org/wiki/Tridiagonal_matrix_algorithm
and to http://www.cfd-online.com/Wiki/Tridiagonal_matrix_algorithm_-_TDMA_(Thomas_algorithm)
'''
nf = len(d) # number of equations
ac, bc, cc, dc = map(np.array, (a, b, c, d)) # copy arrays
for it in range(1, nf):
mc = ac[it-1]/bc[it-1]
bc[it] = bc[it] - mc*cc[it-1]
dc[it] = dc[it] - mc*dc[it-1]
xc = bc
xc[-1] = dc[-1]/bc[-1]
for il in range(nf-2, -1, -1):
xc[il] = (dc[il]-cc[il]*xc[il+1])/bc[il]
return xc
# None working examples of the solver. Dont use.
# class quatumParticaleTridiagonal(quatumParticaleSolver):
# def computeNextTimeStep(self):
# H = self.computeTridiagonalMatrix()
# # print(np.diagonal(H))
# X = np.linalg.solve(H, self.psiX)
# # print(X)
# temp = np.copy(self.psiX)
# temp = 2*X-self.psiX
# temp = temp/np.linalg.norm(temp)
# self.psiX = temp
# # print(self.psiX)
# print(f"Time step {self.currentTimeStep} finished, Time = {self.currentTimeStep*self.dt}")
# self.currentTimeStep += 1
# return temp
# def computeTridiagonalMatrix(self):
# V = 1j*(self.dt/(2*self.h))*np.diag(np.array(list(map(self.potential, np.real(self.inputX)))))
# K = np.zeros((self.N,self.N))
# for i in range(self.N):
# K[i,i] = 2
# if i != 0 and i != self.N-1:
# K[i+1,i] = -1
# K[i-1,i] = -1
# K[1,0] = -1
# K[self.N-2, self.N-1] = -1
# K = 1j*((self.h*self.dt)/(4*self.m*self.dx**2))*K
# # print(np.diagonal(K))
# H = 1-K+V
# return H
# class quatumParticaleVideo(quatumParticaleSolver):
# def computeNextTimeStep(self):
# Tconj, t= self.computeTridiagonalMatrix()
# Tconj = np.conjugate(np.transpose(Tconj))
# Tpsi = np.matmul(Tconj, self.psiX)
# a = np.zeros(self.N, dtype=complex)
# for i in range(1,self.N):
# a[i] = -(1/(t[i]+a[i-1]))
# for i in range(1, self.N):
# self.prevb[i] = -Tpsi[i] + a[i-1]*self.prevb[i]
# temp = np.copy(self.psiX)
# for i in range(self.N-2, 0, -1):
# temp[i] = a[i]*(temp[i+1]-self.prevb[i])
# self.psiX = np.copy(temp)/np.linalg.norm(temp)
# print(f"Time step {self.currentTimeStep} finished, Time = {self.currentTimeStep*self.dt}")
# self.currentTimeStep += 1
# return self.psiX
# def computeTridiagonalMatrix(self):
# V = np.array(list(map(self.potential, np.real(self.inputX))))
# V[0] = 100000
# V[self.N-1] = 100000
# K = np.zeros((self.N,self.N), dtype=complex)
# for i in range(self.N):
# K[i,i] = 1j*(4*self.m*self.dx**2/(self.h*self.dt))-((2*self.m*self.dx**2)/(self.h**2))*V[i]-2
# if i != 0 and i != self.N-1:
# K[i+1,i] = 1
# K[i-1,i] = 1
# K[1,0] = 1
# K[self.N-2, self.N-1] = 1
# return K, np.diagonal(K)
# class quatumParticaleF(quatumParticaleSolver):
# def computeNextTimeStep(self):
# V = np.array(list(map(self.potential, np.real(self.inputX))))
# temp = np.copy(self.psiX)
# for i in range(self.N):
# psi1 = self.psiX[i-1] if (i-1 > 0) else 0
# psi2 = self.psiX[i]
# psi3 = self.psiX[i+1] if (i+1 < self.N-1) else 0
# # print(f"{psi1} {psi2} {psi3}")
# self.psiX[i] = self.psiXold[i] - 2*((1j*self.dt)/(self.h))*(V[i]*psi2- ((self.h**2)/(2*self.m*self.dx**2))*(psi3+psi1-2*psi2))
# self.psiX = self.psiX/np.linalg.norm(self.psiX)
# self.psiXold = np.copy(temp)
# print(f"Time step {self.currentTimeStep} finished, Time = {self.currentTimeStep*self.dt}")
# self.currentTimeStep += 1
# return self.psiX