aboutsummaryrefslogtreecommitdiff
'''
Test solutions by comparing with results known to be correct
'''

import pytest
import numpy as np

from schroedinger import (
    Config, build_potential, solve_schroedinger
)

POTENTIAL = {}
ENERGIES = {}
WAVEFUNCS = {}

FORMS = ['asymmetric', 'double_linear', 'double_cubic', 'harmonic', 'finite']

@pytest.mark.parametrize('form', FORMS)
def test_potential(form: str) -> None:
    '''Compare potential with stored result'''
    potential_prec = np.loadtxt(f'test/{form}/potential.dat')
    assert np.allclose(POTENTIAL[form], potential_prec, rtol=1e-2, atol=1e-2)


@pytest.mark.parametrize('form', FORMS)
def test_energy(form: str) -> None:
    '''Compare energy with stored result'''
    e_prec = np.loadtxt(f'test/{form}/energies.dat')
    assert np.allclose(ENERGIES[form], e_prec, rtol=1e-2, atol=1e-2)


@pytest.mark.parametrize('form', FORMS)
def test_wavefunc(form: str) -> None:
    '''Compare wave functions with stored result'''
    v_prec = np.loadtxt(f'test/{form}/wavefuncs.dat')[:, 1:]
    assert np.allclose(WAVEFUNCS[form], v_prec, rtol=1e-2, atol=1e-2)


def setup_module():
    '''Set up global variables for tests to run'''

    for form in FORMS:
        conf = Config(f'test/{form}.inp')
        POTENTIAL[form], delta = build_potential(conf)
        ENERGIES[form], WAVEFUNCS[form] = solve_schroedinger(
            conf.mass, POTENTIAL[form][:, 1], delta, conf.eig_interval)