[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

@ -1,48 +1,21 @@
import numpy
from numpy.testing import assert_allclose
from ..fdmath import vec, unvec
from ..fdmath import unvec, vec
from ..fdfd import functional, operators
from ._fdfd_case import DXES, EPSILON, E_FIELD, H_FIELD, MU, OMEGA, SHAPE, TF_REGION, apply_fdfd_matrix
from .utils import assert_fields_close
OMEGA = 1 / 1500
SHAPE = (2, 3, 2)
ATOL = 1e-9
RTOL = 1e-9
DXES = [
[numpy.array([1.0, 1.5]), numpy.array([0.75, 1.25, 1.5]), numpy.array([1.2, 0.8])],
[numpy.array([0.9, 1.4]), numpy.array([0.8, 1.1, 1.4]), numpy.array([1.0, 0.7])],
]
EPSILON = numpy.stack([
numpy.linspace(1.0, 2.2, numpy.prod(SHAPE)).reshape(SHAPE),
numpy.linspace(1.1, 2.3, numpy.prod(SHAPE)).reshape(SHAPE),
numpy.linspace(1.2, 2.4, numpy.prod(SHAPE)).reshape(SHAPE),
])
MU = numpy.stack([
numpy.linspace(2.0, 3.2, numpy.prod(SHAPE)).reshape(SHAPE),
numpy.linspace(2.1, 3.3, numpy.prod(SHAPE)).reshape(SHAPE),
numpy.linspace(2.2, 3.4, numpy.prod(SHAPE)).reshape(SHAPE),
])
E_FIELD = (numpy.arange(3 * numpy.prod(SHAPE)).reshape((3, *SHAPE)) + 0.5j).astype(complex)
H_FIELD = (numpy.arange(3 * numpy.prod(SHAPE)).reshape((3, *SHAPE)) * 0.25 - 0.75j).astype(complex)
TF_REGION = numpy.zeros((3, *SHAPE), dtype=float)
TF_REGION[:, 0, 1, 0] = 1.0
def apply_matrix(op: operators.sparse.spmatrix, field: numpy.ndarray) -> numpy.ndarray:
return unvec(op @ vec(field), SHAPE)
def assert_fields_match(actual: numpy.ndarray, expected: numpy.ndarray) -> None:
assert_allclose(actual, expected, atol=ATOL, rtol=RTOL)
assert_fields_close(actual, expected, atol=ATOL, rtol=RTOL)
def test_e_full_matches_sparse_operator_without_mu() -> None:
matrix_result = apply_matrix(
matrix_result = apply_fdfd_matrix(
operators.e_full(OMEGA, DXES, vec(EPSILON)),
E_FIELD,
)
@ -52,7 +25,7 @@ def test_e_full_matches_sparse_operator_without_mu() -> None:
def test_e_full_matches_sparse_operator_with_mu() -> None:
matrix_result = apply_matrix(
matrix_result = apply_fdfd_matrix(
operators.e_full(OMEGA, DXES, vec(EPSILON), vec(MU)),
E_FIELD,
)
@ -80,7 +53,7 @@ def test_eh_full_matches_sparse_operator_without_mu() -> None:
def test_e2h_matches_sparse_operator_with_mu() -> None:
matrix_result = apply_matrix(
matrix_result = apply_fdfd_matrix(
operators.e2h(OMEGA, DXES, vec(MU)),
E_FIELD,
)
@ -90,7 +63,7 @@ def test_e2h_matches_sparse_operator_with_mu() -> None:
def test_e2h_matches_sparse_operator_without_mu() -> None:
matrix_result = apply_matrix(
matrix_result = apply_fdfd_matrix(
operators.e2h(OMEGA, DXES),
E_FIELD,
)
@ -100,7 +73,7 @@ def test_e2h_matches_sparse_operator_without_mu() -> None:
def test_m2j_matches_sparse_operator_without_mu() -> None:
matrix_result = apply_matrix(
matrix_result = apply_fdfd_matrix(
operators.m2j(OMEGA, DXES),
H_FIELD,
)
@ -110,7 +83,7 @@ def test_m2j_matches_sparse_operator_without_mu() -> None:
def test_m2j_matches_sparse_operator_with_mu() -> None:
matrix_result = apply_matrix(
matrix_result = apply_fdfd_matrix(
operators.m2j(OMEGA, DXES, vec(MU)),
H_FIELD,
)
@ -120,7 +93,7 @@ def test_m2j_matches_sparse_operator_with_mu() -> None:
def test_e_tfsf_source_matches_sparse_operator_without_mu() -> None:
matrix_result = apply_matrix(
matrix_result = apply_fdfd_matrix(
operators.e_tfsf_source(vec(TF_REGION), OMEGA, DXES, vec(EPSILON)),
E_FIELD,
)
@ -130,7 +103,7 @@ def test_e_tfsf_source_matches_sparse_operator_without_mu() -> None:
def test_e_tfsf_source_matches_sparse_operator_with_mu() -> None:
matrix_result = apply_matrix(
matrix_result = apply_fdfd_matrix(
operators.e_tfsf_source(vec(TF_REGION), OMEGA, DXES, vec(EPSILON), vec(MU)),
E_FIELD,
)
@ -140,7 +113,7 @@ def test_e_tfsf_source_matches_sparse_operator_with_mu() -> None:
def test_poynting_e_cross_h_matches_sparse_operator() -> None:
matrix_result = apply_matrix(
matrix_result = apply_fdfd_matrix(
operators.poynting_e_cross(vec(E_FIELD), DXES),
H_FIELD,
)