Advertisement
Guest User

Untitled

a guest
Jul 19th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. from scipy.linalg import toeplitz
  4. from scipy.sparse.linalg import spsolve
  5. from scipy import sparse
  6.  
  7. # Spatial discretisation
  8. N = 100
  9. x = np.linspace(0, 1, N)
  10. dx = x[1] - x[0]
  11.  
  12.  
  13. # Time discretisation
  14. K = 10000
  15. t = np.linspace(0, 10, K)
  16. dt = t[1] - t[0]
  17.  
  18. alpha = (1j * dt) / (2 * (dx ** 2))
  19.  
  20. A = sparse.csc_matrix(toeplitz([1 + 2 * alpha, -alpha, *np.zeros(N-4)]), dtype=np.cfloat) # 2 less for both boundaries
  21. B = sparse.csc_matrix(toeplitz([1 - 2 * alpha, alpha, *np.zeros(N-4)]), dtype=np.cfloat)
  22.  
  23. # Initial and boundary conditions (localized gaussian)
  24. psi = np.exp((1j * 50 * x) - (200 * (x - .5) ** 2))
  25. b = B.dot(psi[1:-1])
  26. psi[0], psi[-1] = 0, 0
  27.  
  28. for index, step in enumerate(t):
  29. # Within the domain
  30. psi[1:-1] = spsolve(A, b)
  31.  
  32. # Enforce boundaries
  33. # psi[0], psi[N - 1] = 0, 0
  34.  
  35. b = B.dot(psi[1:-1])
  36. # Square integration to show if it's unitary
  37. print(np.trapz(np.abs(psi) ** 2))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement