From f862643dc4dff18f3bc5853b74b881ecd1844390 Mon Sep 17 00:00:00 2001 From: Thomas Albers Raviola Date: Fri, 19 Jul 2024 11:57:33 +0200 Subject: Add test for infinite potential well --- test/__init__.py | 1 + test/infinite.inp | 7 +++++++ test/test_infinite.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+) create mode 100644 test/__init__.py create mode 100644 test/infinite.inp create mode 100644 test/test_infinite.py (limited to 'test') diff --git a/test/__init__.py b/test/__init__.py new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/test/__init__.py @@ -0,0 +1 @@ + diff --git a/test/infinite.inp b/test/infinite.inp new file mode 100644 index 0000000..a61ffbe --- /dev/null +++ b/test/infinite.inp @@ -0,0 +1,7 @@ +2.0 # mass +-2.0 2.0 1999 # xMin xMax nPoint +1 5 # first and last eigenvalue to print +linear # interpolation type +2 # nr. of interpolation points and xy declarations +-2.0 0.0 +2.0 0.0 diff --git a/test/test_infinite.py b/test/test_infinite.py new file mode 100644 index 0000000..aa85ae5 --- /dev/null +++ b/test/test_infinite.py @@ -0,0 +1,45 @@ +import pytest +import numpy as np + +import matplotlib.pyplot as plt + +from schroedinger import ( + Config, potential_interp, build_potential, solve_schroedinger +) + + +def psi(x, n, a): + n += 1 # Index starting from 0 + x = -x + a / 2 # Reflect x and move to the left + return np.sqrt(2 / a) * np.sin(n * np.pi * x / a) + + +def energy(n, a, m): + return (n + 1)**2 * np.pi**2 / m / a**2 / 2.0 + + +def test_infinite(): + conf = Config('test/infinite.inp') + potential, delta = build_potential(conf) + + e, v = solve_schroedinger(conf.mass, potential[:, 1], delta, conf.eig_interval) + # Account for -v also being an eigenvector if v is one + v = np.abs(v) + + a = np.abs(conf.points[1][0] - conf.points[0][0]) + e_theory = np.zeros(e.shape) + v_theory = np.zeros(v.shape) + + + for n in range(e.shape[0]): + e_theory[n] = energy(n, a, conf.mass) + v_theory[:, n] = np.abs(psi(potential[:, 0], n, a)) + + # for n in range(e.shape[0]): + # plt.plot(potential[:, 0], np.abs(v[:, n]), label='Num{}'.format(n)) + # plt.plot(potential[:, 0], np.abs(v_theory[:, n]), ls='--', label='Theory{}'.format(n)) + # plt.legend() + # plt.savefig('test.pdf') + + assert (np.allclose(e, e_theory, rtol=1e-2, atol=1e-2) + and np.allclose(v, v_theory, rtol=1e-2, atol=1e-2)) -- cgit v1.2.3