def test_cn_delta(): depths = [0.2, 1.0, 5.0] for d in depths: v = potential.DeltaPotential1D(d) e0 = v.get_eigenenergy() tmax = -2 * np.pi / e0 dt = tmax / 500 s = solver.CrankNicolsonSolver(20 / d, 0.1 / d, dt, v) psi0 = v.get_eigenfunction() psi0_value = psi0(s.x) np.testing.assert_almost_equal(wavefunction.norm(s.x, psi0_value), 1, decimal=2) times = [tmax, tmax / 2, tmax / 4] psi_expected = [ psi0_value, -psi0_value, psi0_value * np.exp(0.5j * np.pi) ] for t, psi1 in zip(times, psi_expected): psi = s.execute(t, psi0=psi0) np.testing.assert_almost_equal( wavefunction.norm(s.x, psi), wavefunction.norm(s.x, psi0_value), decimal=7, err_msg=f"Norm not conserved for depth {d}") np.testing.assert_allclose(np.abs(psi), np.abs(psi0_value), atol=0.02) np.testing.assert_allclose(psi, psi1, atol=0.05)
def test_euler_delta(): depths = [0.2, 1.0, 5.0] for d in depths: v = potential.DeltaPotential1D(d) e0 = v.get_eigenenergy() tmax = -2 * np.pi / e0 dt = tmax / 500000 s = solver.EulerSolver(20 / d, 0.1 / d, dt, v) psi0 = v.get_eigenfunction() psi0_value = psi0(s.x) np.testing.assert_almost_equal(wavefunction.norm(s.x, psi0_value), 1, decimal=2) psi = s.execute(tmax, psi0=psi0) np.testing.assert_almost_equal( wavefunction.norm(s.x, psi), wavefunction.norm(s.x, psi0_value), decimal=3, err_msg=f"Norm not conserved for depth {d}") np.testing.assert_allclose(np.abs(psi), np.abs(psi0_value), atol=0.06)
def test_delta_potential(): x = np.linspace(-50, 50, 40000) depths = np.linspace(0.1, 10, 10) for d in depths: v = potential.DeltaPotential1D(d) assert (v.get_delta_depth() == d) assert (v.get_number_of_levels() == 1) with pytest.raises(ValueError): v.get_eigenenergy(random.randint(1, 100)) with pytest.raises(ValueError): v.get_eigenfunction(random.randint(1, 100)) psi = v.get_eigenfunction()(x) np.testing.assert_almost_equal(wavefunction.norm(x, psi), 1.0, decimal=4, err_msg=f"Norm is not 1 for depth {d}")
def test_uniform_field(): amps = [1.0, -2.0, lambda t: 0.5 * t] t = 3.0 x = np.linspace(-10, 10, 1000) for amp in amps: v = potential.UniformField1D(amp) assert (v.get_number_of_levels() == 0) with pytest.raises(ValueError): v.get_eigenfunction() with pytest.raises(ValueError): v.get_eigenenergy() if callable(amp): np.testing.assert_allclose(-amp(t) * x, v.get_potential()(t, x)) else: np.testing.assert_allclose(-amp * x, v.get_potential()(x)) assert (v.get_delta_depth() == 0.0) v = potential.UniformField1D( amp, potential=potential.QuadraticPotential1D(1.0)) if callable(amp): value1 = -amp(t) * x + 0.5 * x**2 value2 = v.get_potential()(t, x) else: value1 = -amp * x + 0.5 * x**2 value2 = v.get_potential()(x) np.testing.assert_allclose(value1, value2) v = potential.UniformField1D(amp, potential=potential.DeltaPotential1D(1.0)) if callable(amp): np.testing.assert_allclose(-amp(t) * x, v.get_potential()(t, x)) else: np.testing.assert_allclose(-amp * x, v.get_potential()(x)) assert (v.get_delta_depth() == 1.0)
def test_sohs_non_stationary_delta(): _test_non_stationary(solver.SplitOperatorHalfSpectralSolver, potential.DeltaPotential1D(1.0))
def test_cn_non_stationary_delta(): _test_non_stationary(solver.CrankNicolsonSolver, potential.DeltaPotential1D(1.0))