Boy! What a long delay. I am going to post the entire piece of code. Not too long. You could probably browse over it.
"""waveFn Class to solve the Schrödinger equation
Example:
>>> psi = waveFn()
>>> psi.setup()
>>> psi.solve()
"""
def waveFn():
def __init__(self):
self.timeStep = #the smaller the better
self.spaceStep =
self.timeSize =
self.spaceSize =
self.currentState = None
self.nextState =
self.solution =
self.bndCndns =
self.oneRunNo =
#Diagonal rows in tridiagonal matrix equation. Refer Wiki for details.
self.a =
self.b =
self.c =
self.v =
def potential(self, x):
#potential constant in time so generate this just once
return(np.cos(x))
def setup(self, x):
self.currentState = np.exp(x)
return
def oneRun(self, a, b, c, v):
#Crank-Nicolson algorithm. Refer Wiki for details.
spaceSize = self.spaceSize
currentState = self.currentState
nextState = self.nextState
for t in range(oneRunNo):
for i in range(spaceSize):
m = a[i]/b[i-1]
b[i] = b[i] - m*c[i-1]
v[i] = v[i] - m*v[i-1]
nextState[spaceSize] = v[spaceSize-1]/b[spaceSize-1]
for i in range(spaceSize-2,-1,-1):
nextState[i]=(v[i]-c[i]*nextState[i+1])/b[i]
currentState = nextState
def solve(self):
timeSize = self.timeSize
currentState = self.currentState
nextState = self.nextState
for t in range():
oneRun()
np.save(solution, nextState)