meanas/meanas/test/test_bloch_foundations.py

73 lines
2.6 KiB
Python
Raw Normal View History

import numpy
from numpy.linalg import norm
from ..fdfd import bloch
2026-04-18 00:52:04 -07:00
from ._bloch_case import EPSILON, G_MATRIX, H_MN, K0_AXIAL, K0_GENERAL, MU, SHAPE, ZERO_H_MN
from .utils import assert_close
def test_generate_kmn_general_case_returns_orthonormal_basis() -> None:
2026-04-18 00:52:04 -07:00
k_mag, m_vecs, n_vecs = bloch.generate_kmn(K0_GENERAL, G_MATRIX, SHAPE)
assert k_mag.shape == SHAPE + (1,)
assert m_vecs.shape == SHAPE + (3,)
assert n_vecs.shape == SHAPE + (3,)
assert numpy.isfinite(k_mag).all()
assert numpy.isfinite(m_vecs).all()
assert numpy.isfinite(n_vecs).all()
2026-04-18 00:52:04 -07:00
assert_close(norm(m_vecs.reshape(-1, 3), axis=1), 1.0)
assert_close(norm(n_vecs.reshape(-1, 3), axis=1), 1.0)
assert_close(numpy.sum(m_vecs * n_vecs, axis=3), 0.0, atol=1e-12)
def test_generate_kmn_z_aligned_uses_default_transverse_basis() -> None:
2026-04-18 00:52:04 -07:00
k_mag, m_vecs, n_vecs = bloch.generate_kmn(K0_AXIAL, G_MATRIX, (1, 1, 1))
assert numpy.isfinite(k_mag).all()
2026-04-18 00:52:04 -07:00
assert_close(m_vecs[0, 0, 0], [0.0, 1.0, 0.0])
assert_close(numpy.sum(m_vecs * n_vecs, axis=3), 0.0, atol=1e-12)
assert_close(norm(n_vecs.reshape(-1, 3), axis=1), 1.0)
def test_maxwell_operator_returns_finite_column_vector_without_mu() -> None:
2026-04-18 00:52:04 -07:00
operator = bloch.maxwell_operator(K0_GENERAL, G_MATRIX, EPSILON)
result = operator(H_MN.copy())
zero_result = operator(ZERO_H_MN.copy())
assert result.shape == (2 * numpy.prod(SHAPE), 1)
assert numpy.isfinite(result).all()
2026-04-18 00:52:04 -07:00
assert_close(zero_result, 0.0)
def test_maxwell_operator_returns_finite_column_vector_with_mu() -> None:
2026-04-18 00:52:04 -07:00
operator = bloch.maxwell_operator(K0_GENERAL, G_MATRIX, EPSILON, MU)
result = operator(H_MN.copy())
zero_result = operator(ZERO_H_MN.copy())
assert result.shape == (2 * numpy.prod(SHAPE), 1)
assert numpy.isfinite(result).all()
2026-04-18 00:52:04 -07:00
assert_close(zero_result, 0.0)
def test_inverse_maxwell_operator_returns_finite_column_vector_for_both_mu_branches() -> None:
for mu in (None, MU):
2026-04-18 00:52:04 -07:00
operator = bloch.inverse_maxwell_operator_approx(K0_GENERAL, G_MATRIX, EPSILON, mu)
result = operator(H_MN.copy())
zero_result = operator(ZERO_H_MN.copy())
assert result.shape == (2 * numpy.prod(SHAPE), 1)
assert numpy.isfinite(result).all()
2026-04-18 00:52:04 -07:00
assert_close(zero_result, 0.0)
def test_bloch_field_converters_return_finite_fields() -> None:
2026-04-18 00:52:04 -07:00
e_field = bloch.hmn_2_exyz(K0_GENERAL, G_MATRIX, EPSILON)(H_MN.copy())
h_field = bloch.hmn_2_hxyz(K0_GENERAL, G_MATRIX, EPSILON)(H_MN.copy())
assert e_field.shape == (3, *SHAPE)
assert h_field.shape == (3, *SHAPE)
assert numpy.isfinite(e_field).all()
assert numpy.isfinite(h_field).all()