aboutsummaryrefslogtreecommitdiff
path: root/test/test_infinite.py
blob: 066604cb404834ec213357210e9a19c0b17d517c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
'''
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))