''' Test solutions to the infinite potential well by comparing with theoretical values ''' import numpy as np from numpy.typing import NDArray from schroedinger import ( Config, build_potential, solve_schroedinger ) def psi(x: NDArray[np.float64], level: int, resolution: float) -> NDArray[np.float64]: '''Generate the n=level wave function''' level += 1 # Index starting from 0 x = -x + resolution / 2 # Reflect x and move to the left return np.sqrt(2 / resolution) * np.sin(level * np.pi * x / resolution) def energy(level: int, resolution: float, mass: float) -> float: '''Energy eigenvalue for the n=level wave function''' return (level + 1)**2 * np.pi**2 / mass / resolution**2 / 2.0 def test_infinite() -> None: '''Test infinite potential well''' conf = Config('test/infinite.inp') potential, delta = build_potential(conf) energies, wavefuncs = solve_schroedinger(conf.mass, potential[:, 1], delta, conf.eig_interval) # Account for -v also being an eigenvector if v is one wavefuncs = np.abs(wavefuncs) resolution = np.abs(conf.points[1][0] - conf.points[0][0]) energies_theory = np.zeros(energies.shape) wavefuncs_theory = np.zeros(wavefuncs.shape) for level in range(energies.shape[0]): energies_theory[level] = energy(level, resolution, conf.mass) wavefuncs_theory[:, level] = np.abs(psi(potential[:, 0], level, resolution)) assert (np.allclose(energies, energies_theory, rtol=1e-2, atol=1e-2) and np.allclose(wavefuncs, wavefuncs_theory, rtol=1e-2, atol=1e-2))