Skip to content

Latest commit

 

History

1 Commit

Folders and files

Repository files navigation

Particle in a Box

An interactive simulation of a quantum particle in an infinitely deep box, in 1D, 2D or 3D, drawn in an $x$-$y$-$z$ Cartesian coordinate system that you can rotate with the mouse. Sliders set the box lengths $L_x$, $L_y$, $L_z$ and the quantum numbers $n_x$, $n_y$, $n_z$. A length of $0$ removes that dimension, so the same program covers a line, a rectangle and a cuboid without ever dividing by zero.

A Python backend (Flask and NumPy) builds and checks the quantum state; the browser animates it.

Screenshot

Run it

pip install -r requirements.txt
python server.py

Your browser opens at http://127.0.0.1:8004. On Windows you can double-click launch.pyw instead (no console window). The server quits by itself when you close the tab.

Controls

  • Sliders and number boxes for $L_x$, $L_y$, $L_z$ (0 to 6 on the slider, up to 50 in the box) and $n_x$, $n_y$, $n_z$ (1 to 20 on the slider, up to 60 in the box). Everything updates as you drag.
  • State: eigenstate, superposition of two eigenstates, or Gaussian wave packet.
  • View: drag to rotate, wheel to zoom, double-click to reset. The buttons Iso, x-y, x-z and y-z jump to standard viewpoints, and Auto-rotate spins the scene.
  • Playback: pause, restart and speed.

A length of $0$ greys out that axis and its quantum number. If all three lengths are $0$ the page says there is no box instead of drawing anything.

Lengths Box
$L_x = 2$, $L_y = 0$, $L_z = 0$ 1D, length 2 along $x$
$L_x = 2$, $L_y = 1$, $L_z = 0$ 2D rectangle $2 \times 1$
$L_x = 0$, $L_y = 1$, $L_z = 3$ 2D rectangle in the $y$-$z$ plane
$L_x = 2$, $L_y = 1$, $L_z = 3$ 3D cuboid
all three $0$ no box, so nothing is drawn

How the wave is plotted

The axes are always $x$, $y$ and $z$, with tick marks in box units, and the box sits in them at its true shape.

  • 1D: the box is a segment of one axis. The wave is drawn as a curve in the space around it: $\mathrm{Re},\psi$ on one perpendicular axis and $\mathrm{Im},\psi$ on the other, so $\psi$ itself is a helix. A second view plots $|\psi|^2$ along one perpendicular axis.
  • 2D: the box is a rectangle in a coordinate plane and the remaining axis is the height of a surface: $\mathrm{Re},\psi$, $\mathrm{Im},\psi$ or $|\psi|^2$ (orange positive, blue negative).
  • 3D: a cloud of points inside the wireframe box. Brightness follows $|\psi|^2$, or you can show the sign of $\mathrm{Re},\psi$ or $\mathrm{Im},\psi$. The cut-off slider hides the faintest points.

Heights and brightness use one fixed scale for the whole animation (the largest value the state ever reaches) for 1D and 2D, so you can see a packet spread and fade. The 3D cloud is scaled to the peak of each frame, because a spread-out packet would otherwise vanish.

Three kinds of state are available:

  1. Eigenstate: a stationary state. The density never changes but the phase of $\psi$ rotates.
  2. Superposition: the eigenstates $\mathbf{n}$ and $\mathbf{n}+\hat{x}_1$ added together (one level up along the first existing axis), so the density sloshes back and forth.
  3. Gaussian wave packet: the closest thing to a "particle". It travels, bounces off the walls, spreads out, interferes with itself and eventually reforms.

The physics

Units are $\hbar = m = 1$. For a 1D box of length $L$ the eigenstates and energies are

$$ \phi_n(x) = \sqrt{\frac{2}{L}},\sin\frac{n\pi x}{L} \qquad E_n = \frac{\pi^2 n^2}{2L^2} \qquad n = 1, 2, 3, \dots $$

The box is separable, so in $d$ dimensions the states are products and the energies add:

$$ \psi_{\mathbf{n}}(\mathbf{r}) = \prod_{i=1}^{d} \phi_{n_i}(x_i) \qquad E_{\mathbf{n}} = \frac{\pi^2}{2}\sum_{i=1}^{d} \frac{n_i^2}{L_i^2} $$

The wave packet is a Gaussian expanded in box eigenstates, which makes the wall reflections exact:

$$ \psi(x, t) = \sum_{n} c_n,\phi_n(x),e^{-iE_n t} \qquad c_n = \int_0^L \phi_n(x),\psi(x, 0),dx $$

Because $E_n \propto n^2$ every phase returns to 1 at the revival time $T_{\mathrm{rev}} = 4L^2/\pi$, and the packet then reappears exactly as it started.

Each second of animation corresponds to a chosen amount of physical time, so a full cycle of the motion takes about 6 seconds on screen. For a packet that cycle is one round trip across the longest side.

Project structure

.
├── server.py                 # Flask app: validates input, serves the page, quits with the tab
├── box_physics.py            # all the physics, pure NumPy
├── static/index.html         # the page: sliders, 3D canvas renderer, animation
├── launch.pyw                # double-click launcher (Windows)
├── requirements.txt          # web app: flask, numpy
├── assets/screenshot.png
└── docs/
    └── Particle in a box.pdf  # every formula used in the program

The server sends the page the eigenfunction expansion of the state (quantum numbers, coefficients, energies). The page then evaluates $\psi(x, y, z, t)$ itself on every frame, using the fact that the box is separable, so sliders and animation stay smooth with no round trip.

Formulas

All the mathematics used in the program (eigenstates, superposition, wave packet, the plotted quantities and the 3D projection) is collected in docs/Particle in a box.pdf.

License

MIT, see LICENSE.

Releases

Packages

Contributors

Languages