andybuckley

Time evolution of QM infinite square-well amplitudes & probs

Feb 13th, 2020
609
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.01 KB | None | 0 0
  1. #! /usr/bin/env python
  2.  
  3. from __future__ import print_function, division
  4.  
  5. ## Parse args
  6. import argparse
  7. ap = argparse.ArgumentParser()
  8. ap.add_argument("COEFFS", help="list of coefficients (generally complex, e.g. 1+2j) for the inf-well eigenstates",
  9.                 nargs="*", type=complex, default=[1., 3., 0., 8., 2., 1.])
  10. ap.add_argument("-o", "--out", dest="MPGOUT", help="write to this MPEG file rather than showing interactively", default=None)
  11. args = ap.parse_args()
  12.  
  13.  
  14. ## Normalize coeffs and make separate u^+- coeff arrays
  15. CS = args.COEFFS
  16. #CS = [10., 0., 5., 0., 2.]
  17. #CS = [0., 3., 0., 8., 0., 1.]
  18. #CS = [1., 3., 0., 8., 2., 1.]
  19. import numpy as np
  20. Z = np.sqrt((np.abs(CS)**2).sum())
  21. cpluses = np.array(CS[::2]).reshape((1,1,-1))
  22. cminuses = np.array(CS[1::2]).reshape((1,1,-1))
  23.  
  24.  
  25. ## Make arrays of t and x values (could move to command-line args, ...)
  26. A, NT, NX = 5, 500, 200
  27. ts = np.linspace(0, 10, NT).reshape((-1,1,1))
  28. xs = np.linspace(-A, A, NX).reshape((1,-1,1))
  29. #print(ts,ks,sep="\n")
  30.  
  31.  
  32. ## Compute all terms
  33. M = 1; HBAR = 1
  34. npluses = np.array(range(1, cpluses.size+1)).reshape((1,1,-1))
  35. nminuses = np.array(range(1, cminuses.size+1)).reshape((1,1,-1))
  36. #print(npluses, npluses.shape)
  37. #print(nminuses, nminuses.shape)
  38. cupluses = cpluses * np.cos((2*npluses-1) * np.pi * xs / 2 / A) / np.sqrt(A) * \
  39.            np.exp(1j / 2 / M * ((2*npluses-1) * np.pi * HBAR / 2 / A)**2 * ts / HBAR)
  40. cuminuses = cminuses * np.sin((2*nminuses) * np.pi * xs / 2 / A) / np.sqrt(A) * \
  41.             np.exp(1j / 2 / M * ((2*nminuses) * np.pi * HBAR / 2 / A)**2 * ts / HBAR)
  42.  
  43.  
  44. ## Sum over eigenstates
  45. psis = cupluses.sum(axis=2) + cuminuses.sum(axis=2)
  46. #print(psis.shape)
  47.  
  48.  
  49. ## Render the first frame
  50. import matplotlib.pyplot as plt
  51. import matplotlib.animation as animation
  52. fig, ax = plt.subplots(figsize=(14,8))
  53. line, = ax.plot(xs.reshape(-1), np.absolute(psis[0,:]), linewidth=2)
  54. line_r, = ax.plot(xs.reshape(-1), np.real(psis[0,:]), linestyle="--", color="lightgray")
  55. line_i, = ax.plot(xs.reshape(-1), np.imag(psis[0,:]), linestyle=":", color="lightgray")
  56. ax.set_xlim(-A,A)
  57. #ax.set_ylim(-8,12)
  58.  
  59.  
  60. ## Set up the animation callbacks for updating the lines
  61. def init():  # only required for blitting to give a clean slate.
  62.     line.set_ydata([np.nan] * psis.shape[1])
  63.     line_r.set_ydata([np.nan] * psis.shape[1])
  64.     line_i.set_ydata([np.nan] * psis.shape[1])
  65.     return line, line_r, line_i
  66. def animate(i):
  67.     #print(np.absolute(psis[i,:]), np.real(psis[i,:]), np.imag(psis[i,:]), sep="\n")
  68.     line.set_ydata(np.absolute(psis[i,:]))
  69.     line_r.set_ydata(np.real(psis[i,:]))
  70.     line_i.set_ydata(np.imag(psis[i,:]))
  71.     return line, line_r, line_i
  72. anim = animation.FuncAnimation(fig, animate, frames=NT, init_func=init, interval=20, blit=True, save_count=50)
  73.  
  74.  
  75. ## Display/save
  76. if not args.MPGOUT:
  77.     plt.show()
  78.     #HTML(anim.to_html5_video())
  79. else:
  80.     Writer = animation.writers['ffmpeg']
  81.     writer = Writer(fps=24, metadata=dict(artist='Andy Buckley'), bitrate=1800)
  82.     anim.save(args.MPGOUT, writer=writer)
Add Comment
Please, Sign In to add comment