Solving Schrodinger Eq. w/ Crank Nicolson Method

  • Thread starter Thread starter WiFO215
  • Start date Start date
  • Tags Tags
    Crank Method
Click For Summary
SUMMARY

The discussion focuses on solving the Schrödinger equation using the Crank-Nicolson method and the tridiagonal matrix algorithm. The user is encountering issues with their implementation in Python, specifically with boundary conditions, indexing, and convergence of the solution. Suggestions provided include verifying boundary conditions, checking array indexing, considering alternative algorithms like LU decomposition, and debugging the code to track variable values.

PREREQUISITES
  • Understanding of the Schrödinger equation and its numerical solutions
  • Familiarity with the Crank-Nicolson method for time-dependent problems
  • Knowledge of Python programming, particularly with NumPy for array manipulation
  • Experience with solving tridiagonal matrices using algorithms like the tridiagonal matrix algorithm
NEXT STEPS
  • Review and validate boundary conditions in numerical simulations
  • Learn about LU decomposition as an alternative to the tridiagonal matrix algorithm
  • Explore debugging techniques in Python to track variable states
  • Investigate convergence criteria for numerical solutions of differential equations
USEFUL FOR

Researchers, physicists, and computational scientists working on quantum mechanics simulations, particularly those implementing numerical methods for solving differential equations.

WiFO215
Messages
417
Reaction score
1
I have tried discretizing the Schrödinger equation using the crank nicolson method and then have tried to solve the resulting tri-diagonal matrix using the algorithm on Wikipedia.

http://en.wikipedia.org/wiki/Tridiagonal_matrix_algorithm

However, I am not getting the required result and cannot put my finger on it. Please help! I have attached the code below.

h is my space step and deltat is my time step. J = total number of space steps and K = total number of time steps

Code:
import numpy as np

#----------------------------------------------------#

#GIVEN DATA

h = 0.01
deltat = 0.001

J = 500
K = 500

phi_a = np.zeros(shape=(K,J), dtype = np.complex)

#BOUNDARY CONDITIONS#

for p in range(1,J-1):
   phi_a[0][p] = np.sin(np.pi*(p+1)/J)

#----------------------------------------------------#

a = np.ones(J, dtype=np.complex)/(4*h**2)
a[0], a[J-1] = 0,0

c = np.ones(J, dtype=np.complex)/(4*h**2)
c[0], c[J-1] = 0,0

#----------------------------------------------------#

def b(k):
   b = np.ones(J, dtype=np.complex)
   for p in range(1,J-1):
      b[p] = (1j/(deltat)) - (1/(2*h**2))

   return(b)

def d(k):
   d = np.ones(J, dtype=np.complex)

   for p in range(1,J-2):
      d[p] = -(phi_a[k][p+1]/(4*h**2)) + (phi_a[k][p]*((1j/deltat) + 1/(2*h**2))) - (phi_a[k][p-1]/(4*h**2))

   d[0] = phi_a[k+1][0]
   d[J-1] = phi_a[k+1][J-1]
   
   return(d)

#----------------------------------------------------#

def tdms(a,c,k,s,b,d):
   b,d = b(k),d(k)

   for p in range(1,J-1):
      c[p] = c[p]/(b[p] - c[p-1]*a[p])

   for p in range(J):
      d[p] = (d[p]-d[p-1]*a[p])/(b[p] - c[p-1]*a[p])

   for p in range(J-2,0,-1):
      s[k+1][p] = d[p] - c[p]*s[k+1][p+1]

   return

#----------------------------------------------------#

for k in range(0,K-1):
   tdms(a,c,k,phi_a,b,d)

#----------------------------------------------------#

anphi_a = open('test.txt','w')
for W in range(K):
   for Q in range(J-1):
      anphi_a.write(str(phi_a[W][Q]) + ', ')
   anphi_a.write(str(phi_a[W][J-1]) + '\n')
anphi_a.close()

This is written in Python, which is nearly pseudo-code, so most of you should be able to understand it. Please help!
 
Technology news on Phys.org


Hi there,

It's difficult to pinpoint the exact issue without more information, but here are a few suggestions that may help:

1. Double check your boundary conditions: Make sure they are correctly implemented and are consistent with the initial conditions.

2. Check your indexing: Make sure that you are using the correct indices when accessing and assigning values to arrays. A simple mistake in indexing can lead to unexpected results.

3. Consider using a different algorithm: The tridiagonal matrix algorithm is known to have stability issues for certain types of matrices. You may want to try using a different algorithm, such as the LU decomposition method, to solve the resulting matrix.

4. Check for convergence: Make sure that your solution is converging to a steady state. If it is not, then there may be a problem with your discretization or algorithm.

5. Debug your code: Use print statements or a debugger to track the values of your variables and make sure they are behaving as expected.

I hope these suggestions help. Good luck with your research!
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
Replies
1
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 97 ·
4
Replies
97
Views
10K
Replies
55
Views
7K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 17 ·
Replies
17
Views
3K