aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorThomas Albers Raviola <thomas@thomaslabs.org>2024-07-19 11:57:33 +0200
committerThomas Albers Raviola <thomas@thomaslabs.org>2024-07-19 11:57:33 +0200
commitf862643dc4dff18f3bc5853b74b881ecd1844390 (patch)
treee5ad594c893d5808978b8cf0f7292fc8b291716b /test
parent4efcbc29a4faeeb7625922dba78d5d4bc3be5753 (diff)
Add test for infinite potential well
Diffstat (limited to 'test')
-rw-r--r--test/__init__.py1
-rw-r--r--test/infinite.inp7
-rw-r--r--test/test_infinite.py45
3 files changed, 53 insertions, 0 deletions
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))