[tests] refactor tests

This commit is contained in:
Jan Petykiewicz 2026-04-18 00:52:04 -07:00
commit 8cdcd08ba0
25 changed files with 649 additions and 616 deletions

View file

@ -4,33 +4,8 @@ from numpy.testing import assert_allclose
from types import SimpleNamespace
from ..fdfd import bloch
SHAPE = (2, 2, 2)
G_MATRIX = numpy.eye(3)
EPSILON = numpy.ones((3, *SHAPE), dtype=float)
K0 = numpy.array([0.1, 0.0, 0.0], dtype=float)
H_SIZE = 2 * numpy.prod(SHAPE)
Y0 = (numpy.arange(H_SIZE, dtype=float) + 1j * numpy.linspace(0.1, 0.9, H_SIZE))[None, :]
Y0_TWO_MODE = numpy.vstack(
[
numpy.arange(H_SIZE, dtype=float) + 1j * numpy.linspace(0.1, 0.9, H_SIZE),
numpy.linspace(2.0, 3.5, H_SIZE) - 0.5j * numpy.arange(H_SIZE, dtype=float),
],
)
def build_overlap_fixture() -> tuple[numpy.ndarray, numpy.ndarray, numpy.ndarray, numpy.ndarray]:
e_in = numpy.zeros((3, *SHAPE), dtype=complex)
h_in = numpy.zeros_like(e_in)
e_out = numpy.zeros_like(e_in)
h_out = numpy.zeros_like(e_in)
e_in[1] = 1.0
h_in[2] = 2.0
e_out[1] = 3.0
h_out[2] = 4.0
return e_in, h_in, e_out, h_out
from ._bloch_case import EPSILON, G_MATRIX, H_SIZE, K0_X, SHAPE, Y0, Y0_TWO_MODE, build_overlap_fixture
from .utils import assert_close
def test_rtrace_atb_matches_real_frobenius_inner_product() -> None:
@ -45,8 +20,8 @@ def test_symmetrize_returns_hermitian_average() -> None:
matrix = numpy.array([[1.0 + 2.0j, 3.0 - 1.0j], [2.0j, 4.0]], dtype=complex)
result = bloch._symmetrize(matrix)
assert_allclose(result, 0.5 * (matrix + matrix.conj().T))
assert_allclose(result, result.conj().T)
assert_close(result, 0.5 * (matrix + matrix.conj().T))
assert_close(result, result.conj().T)
def test_inner_product_is_nonmutating_and_obeys_sign_symmetry() -> None:
@ -58,9 +33,9 @@ def test_inner_product_is_nonmutating_and_obeys_sign_symmetry() -> None:
np_term = bloch.inner_product(e_out, -h_out, e_in, h_in)
nn = bloch.inner_product(e_out, -h_out, e_in, -h_in)
assert_allclose(pp, 0.8164965809277263 + 0.0j)
assert_allclose(pp, -nn, atol=1e-12, rtol=1e-12)
assert_allclose(pn, -np_term, atol=1e-12, rtol=1e-12)
assert_close(pp, 0.8164965809277263 + 0.0j)
assert_close(pp, -nn, atol=1e-12, rtol=1e-12)
assert_close(pn, -np_term, atol=1e-12, rtol=1e-12)
assert numpy.array_equal(e_in, originals[0])
assert numpy.array_equal(h_in, originals[1])
assert numpy.array_equal(e_out, originals[2])
@ -72,8 +47,8 @@ def test_trq_returns_expected_transmission_and_reflection() -> None:
transmission, reflection = bloch.trq(e_in, h_in, e_out, h_out)
assert_allclose(transmission, 0.9797958971132713 + 0.0j, atol=1e-12, rtol=1e-12)
assert_allclose(reflection, 0.2 + 0.0j, atol=1e-12, rtol=1e-12)
assert_close(transmission, 0.9797958971132713 + 0.0j, atol=1e-12, rtol=1e-12)
assert_close(reflection, 0.2 + 0.0j, atol=1e-12, rtol=1e-12)
def test_eigsolve_returns_finite_modes_with_small_residual() -> None:
@ -85,7 +60,7 @@ def test_eigsolve_returns_finite_modes_with_small_residual() -> None:
eigvals, eigvecs = bloch.eigsolve(
1,
K0,
K0_X,
G_MATRIX,
EPSILON,
tolerance=1e-6,
@ -94,7 +69,7 @@ def test_eigsolve_returns_finite_modes_with_small_residual() -> None:
callback=callback,
)
operator = bloch.maxwell_operator(K0, G_MATRIX, EPSILON)
operator = bloch.maxwell_operator(K0_X, G_MATRIX, EPSILON)
eigvec = eigvecs[0] / numpy.linalg.norm(eigvecs[0])
residual = numpy.linalg.norm(operator(eigvec).reshape(-1) - eigvals[0] * eigvec) / numpy.linalg.norm(eigvec)
@ -109,7 +84,7 @@ def test_eigsolve_returns_finite_modes_with_small_residual() -> None:
def test_eigsolve_without_initial_guess_returns_finite_modes() -> None:
eigvals, eigvecs = bloch.eigsolve(
1,
K0,
K0_X,
G_MATRIX,
EPSILON,
tolerance=1e-6,
@ -117,7 +92,7 @@ def test_eigsolve_without_initial_guess_returns_finite_modes() -> None:
y0=None,
)
operator = bloch.maxwell_operator(K0, G_MATRIX, EPSILON)
operator = bloch.maxwell_operator(K0_X, G_MATRIX, EPSILON)
eigvec = eigvecs[0] / numpy.linalg.norm(eigvecs[0])
residual = numpy.linalg.norm(operator(eigvec).reshape(-1) - eigvals[0] * eigvec) / numpy.linalg.norm(eigvec)
@ -143,7 +118,7 @@ def test_eigsolve_recovers_from_singular_initial_subspace(monkeypatch: pytest.Mo
eigvals, eigvecs = bloch.eigsolve(
2,
K0,
K0_X,
G_MATRIX,
EPSILON,
tolerance=1e-6,
@ -181,7 +156,7 @@ def test_eigsolve_reconditions_large_trace_initial_subspace(monkeypatch: pytest.
eigvals, eigvecs = bloch.eigsolve(
2,
K0,
K0_X,
G_MATRIX,
EPSILON,
tolerance=1e-6,
@ -208,7 +183,7 @@ def test_eigsolve_qi_memoization_reuses_cached_theta(monkeypatch: pytest.MonkeyP
eigvals, eigvecs = bloch.eigsolve(
1,
K0,
K0_X,
G_MATRIX,
EPSILON,
tolerance=1e-6,
@ -243,7 +218,7 @@ def test_eigsolve_qi_taylor_expansions_return_finite_modes(monkeypatch: pytest.M
eigvals, eigvecs = bloch.eigsolve(
1,
K0,
K0_X,
G_MATRIX,
EPSILON,
tolerance=1e-6,
@ -278,7 +253,7 @@ def test_eigsolve_qi_inexplicable_singularity_raises(monkeypatch: pytest.MonkeyP
with pytest.raises(Exception, match='Inexplicable singularity in trace_func'):
bloch.eigsolve(
1,
K0,
K0_X,
G_MATRIX,
EPSILON,
tolerance=1e-6,
@ -290,7 +265,7 @@ def test_eigsolve_qi_inexplicable_singularity_raises(monkeypatch: pytest.MonkeyP
def test_find_k_returns_vector_frequency_and_callbacks() -> None:
target_eigvals, _target_eigvecs = bloch.eigsolve(
1,
K0,
K0_X,
G_MATRIX,
EPSILON,
tolerance=1e-6,
@ -329,8 +304,8 @@ def test_find_k_returns_vector_frequency_and_callbacks() -> None:
assert found_k.shape == (3,)
assert numpy.isfinite(found_k).all()
assert_allclose(numpy.cross(found_k, [1.0, 0.0, 0.0]), 0.0, atol=1e-12, rtol=1e-12)
assert_allclose(found_k, K0, atol=1e-4, rtol=1e-4)
assert_close(numpy.cross(found_k, [1.0, 0.0, 0.0]), 0.0, atol=1e-12, rtol=1e-12)
assert_close(found_k, K0_X, atol=1e-4, rtol=1e-4)
assert abs(found_frequency - target_frequency) <= 1e-4
assert eigvals.shape == (1,)
assert eigvecs.shape == (1, H_SIZE)