meanas/meanas/test/test_fdfd_functional.py

122 lines
4 KiB
Python
Raw Normal View History

import numpy
2026-04-18 00:52:04 -07:00
from ..fdmath import unvec, vec
from ..fdfd import functional, operators
2026-04-18 00:52:04 -07:00
from ._fdfd_case import DXES, EPSILON, E_FIELD, H_FIELD, MU, OMEGA, SHAPE, TF_REGION, apply_fdfd_matrix
from .utils import assert_fields_close
ATOL = 1e-9
RTOL = 1e-9
def assert_fields_match(actual: numpy.ndarray, expected: numpy.ndarray) -> None:
2026-04-18 00:52:04 -07:00
assert_fields_close(actual, expected, atol=ATOL, rtol=RTOL)
def test_e_full_matches_sparse_operator_without_mu() -> None:
2026-04-18 00:52:04 -07:00
matrix_result = apply_fdfd_matrix(
operators.e_full(OMEGA, DXES, vec(EPSILON)),
E_FIELD,
)
functional_result = functional.e_full(OMEGA, DXES, EPSILON)(E_FIELD)
assert_fields_match(functional_result, matrix_result)
def test_e_full_matches_sparse_operator_with_mu() -> None:
2026-04-18 00:52:04 -07:00
matrix_result = apply_fdfd_matrix(
operators.e_full(OMEGA, DXES, vec(EPSILON), vec(MU)),
E_FIELD,
)
functional_result = functional.e_full(OMEGA, DXES, EPSILON, MU)(E_FIELD)
assert_fields_match(functional_result, matrix_result)
def test_eh_full_matches_sparse_operator_with_mu() -> None:
matrix_result = operators.eh_full(OMEGA, DXES, vec(EPSILON), vec(MU)) @ numpy.concatenate([vec(E_FIELD), vec(H_FIELD)])
matrix_e, matrix_h = (unvec(part, SHAPE) for part in numpy.split(matrix_result, 2))
functional_e, functional_h = functional.eh_full(OMEGA, DXES, EPSILON, MU)(E_FIELD, H_FIELD)
assert_fields_match(functional_e, matrix_e)
assert_fields_match(functional_h, matrix_h)
def test_eh_full_matches_sparse_operator_without_mu() -> None:
matrix_result = operators.eh_full(OMEGA, DXES, vec(EPSILON)) @ numpy.concatenate([vec(E_FIELD), vec(H_FIELD)])
matrix_e, matrix_h = (unvec(part, SHAPE) for part in numpy.split(matrix_result, 2))
functional_e, functional_h = functional.eh_full(OMEGA, DXES, EPSILON)(E_FIELD, H_FIELD)
assert_fields_match(functional_e, matrix_e)
assert_fields_match(functional_h, matrix_h)
def test_e2h_matches_sparse_operator_with_mu() -> None:
2026-04-18 00:52:04 -07:00
matrix_result = apply_fdfd_matrix(
operators.e2h(OMEGA, DXES, vec(MU)),
E_FIELD,
)
functional_result = functional.e2h(OMEGA, DXES, MU)(E_FIELD)
assert_fields_match(functional_result, matrix_result)
def test_e2h_matches_sparse_operator_without_mu() -> None:
2026-04-18 00:52:04 -07:00
matrix_result = apply_fdfd_matrix(
operators.e2h(OMEGA, DXES),
E_FIELD,
)
functional_result = functional.e2h(OMEGA, DXES)(E_FIELD)
assert_fields_match(functional_result, matrix_result)
def test_m2j_matches_sparse_operator_without_mu() -> None:
2026-04-18 00:52:04 -07:00
matrix_result = apply_fdfd_matrix(
operators.m2j(OMEGA, DXES),
H_FIELD,
)
functional_result = functional.m2j(OMEGA, DXES)(H_FIELD)
assert_fields_match(functional_result, matrix_result)
def test_m2j_matches_sparse_operator_with_mu() -> None:
2026-04-18 00:52:04 -07:00
matrix_result = apply_fdfd_matrix(
operators.m2j(OMEGA, DXES, vec(MU)),
H_FIELD,
)
functional_result = functional.m2j(OMEGA, DXES, MU)(H_FIELD)
assert_fields_match(functional_result, matrix_result)
def test_e_tfsf_source_matches_sparse_operator_without_mu() -> None:
2026-04-18 00:52:04 -07:00
matrix_result = apply_fdfd_matrix(
operators.e_tfsf_source(vec(TF_REGION), OMEGA, DXES, vec(EPSILON)),
E_FIELD,
)
functional_result = functional.e_tfsf_source(TF_REGION, OMEGA, DXES, EPSILON)(E_FIELD)
assert_fields_match(functional_result, matrix_result)
def test_e_tfsf_source_matches_sparse_operator_with_mu() -> None:
2026-04-18 00:52:04 -07:00
matrix_result = apply_fdfd_matrix(
operators.e_tfsf_source(vec(TF_REGION), OMEGA, DXES, vec(EPSILON), vec(MU)),
E_FIELD,
)
functional_result = functional.e_tfsf_source(TF_REGION, OMEGA, DXES, EPSILON, MU)(E_FIELD)
assert_fields_match(functional_result, matrix_result)
def test_poynting_e_cross_h_matches_sparse_operator() -> None:
2026-04-18 00:52:04 -07:00
matrix_result = apply_fdfd_matrix(
operators.poynting_e_cross(vec(E_FIELD), DXES),
H_FIELD,
)
functional_result = functional.poynting_e_cross_h(DXES)(E_FIELD, H_FIELD)
assert_fields_match(functional_result, matrix_result)