Skip to content

Commit 25961ea

Browse files
committed
fix e and h cartesian calculations
1 parent 193cd1f commit 25961ea

4 files changed

Lines changed: 76 additions & 4 deletions

File tree

CHANGELOG.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
Changelog
22
=========
33

4+
3.2.0 (03/06/2026)
5+
-------------------
6+
* fix error in E & H calculations in the near field when y≠0 (thanks @dorianherle)
7+
* add regression test
8+
49
3.1.0 (02/07/2026)
510
-------------------
611
* add near-field E and H field APIs, fix boundary continuity/medium handling, and expand validation tests

miepython/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ def _missing(*_args, **_kwargs):
147147
"_D_downwards",
148148
)
149149

150-
__version__ = "3.1.0"
150+
__version__ = "3.2.0"
151151
__author__ = "Scott Prahl"
152152
__email__ = "scott.prahl@oit.edu"
153153
__copyright__ = "2017-2026, Scott Prahl"

miepython/field.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ def _e_near_abcd(abcd, lambda0, d_sphere, m_sphere, n_env, r, theta, phi, includ
403403
E_the += Ei_the
404404
E_phi += Ei_phi
405405

406-
return np.array([E_rad, E_the, -E_phi])
406+
return np.array([E_rad, E_the, E_phi])
407407

408408

409409
def _h_near_abcd(abcd, lambda0, d_sphere, m_sphere, n_env, r, theta, phi, include_incident):
@@ -460,7 +460,7 @@ def _h_near_abcd(abcd, lambda0, d_sphere, m_sphere, n_env, r, theta, phi, includ
460460
H_the += Hi_the
461461
H_phi += Hi_phi
462462

463-
return np.array([H_rad, H_the, -H_phi])
463+
return np.array([H_rad, H_the, H_phi])
464464

465465

466466
def _eh_near_abcd(abcd, lambda0, d_sphere, m_sphere, n_env, r, theta, phi, include_incident):
@@ -520,7 +520,7 @@ def _eh_near_abcd(abcd, lambda0, d_sphere, m_sphere, n_env, r, theta, phi, inclu
520520
h_the += h_i[1]
521521
h_phi += h_i[2]
522522

523-
return np.array([e_rad, e_the, -e_phi]), np.array([h_rad, h_the, -h_phi])
523+
return np.array([e_rad, e_the, e_phi]), np.array([h_rad, h_the, h_phi])
524524

525525

526526
def e_near(lambda0, d_sphere, m_sphere, n_env, r, theta, phi, include_incident=True, n_pole=0, abcd=None):

tests/test_field.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,3 +291,70 @@ def test_cartesian_wrappers_match_spherical_transform(m_sphere, n_env):
291291
np.testing.assert_allclose(h_xyz, h_ref, rtol=1e-12, atol=1e-12)
292292
np.testing.assert_allclose(e_xyz2, e_ref, rtol=1e-12, atol=1e-12)
293293
np.testing.assert_allclose(h_xyz2, h_ref, rtol=1e-12, atol=1e-12)
294+
295+
296+
def test_no_contrast_cartesian_fields_match_incident_plane_wave():
297+
"""No-contrast sphere should reduce to the incident plane wave everywhere."""
298+
lambda0 = 1.0
299+
n_env = 1.0
300+
d_sphere = 0.002
301+
m_sphere = 1.0 + 0.0j
302+
303+
x = np.array([3.0, 0.0, 0.0, 2.0, 2.0])
304+
y = np.array([0.0, 0.0, 3.0, 2.0, 2.0])
305+
z = np.array([0.0, 3.0, 0.0, 0.0, 0.5])
306+
307+
k = 2 * np.pi * n_env / lambda0
308+
phase = np.exp(1j * k * z)
309+
zeros = np.zeros_like(phase)
310+
e_expected = np.array([phase, zeros, zeros])
311+
h_expected = np.array([zeros, phase, zeros])
312+
313+
e_xyz = e_near_cartesian(lambda0, d_sphere, m_sphere, n_env, x, y, z, include_incident=True)
314+
h_xyz = h_near_cartesian(lambda0, d_sphere, m_sphere, n_env, x, y, z, include_incident=True)
315+
e_xyz2, h_xyz2 = eh_near_cartesian(lambda0, d_sphere, m_sphere, n_env, x, y, z, include_incident=True)
316+
317+
np.testing.assert_allclose(e_xyz, e_expected, rtol=1e-12, atol=1e-12)
318+
np.testing.assert_allclose(h_xyz, h_expected, rtol=1e-12, atol=1e-12)
319+
np.testing.assert_allclose(e_xyz2, e_expected, rtol=1e-12, atol=1e-12)
320+
np.testing.assert_allclose(h_xyz2, h_expected, rtol=1e-12, atol=1e-12)
321+
322+
323+
def test_no_contrast_spherical_fields_match_incident_components():
324+
"""No-contrast spherical fields should follow standard spherical components."""
325+
lambda0 = 1.0
326+
n_env = 1.0
327+
d_sphere = 0.002
328+
m_sphere = 1.0 + 0.0j
329+
x, y, z = 2.0, 2.0, 0.5
330+
331+
r = np.sqrt(x**2 + y**2 + z**2)
332+
theta = np.arccos(z / r)
333+
phi = np.arctan2(y, x)
334+
335+
k = 2 * np.pi * n_env / lambda0
336+
phase = np.exp(1j * k * z)
337+
338+
e_expected = np.array(
339+
[
340+
phase * np.sin(theta) * np.cos(phi),
341+
phase * np.cos(theta) * np.cos(phi),
342+
-phase * np.sin(phi),
343+
]
344+
)
345+
h_expected = np.array(
346+
[
347+
phase * np.sin(theta) * np.sin(phi),
348+
phase * np.cos(theta) * np.sin(phi),
349+
phase * np.cos(phi),
350+
]
351+
)
352+
353+
e_sph = e_near(lambda0, d_sphere, m_sphere, n_env, r, theta, phi, include_incident=True)
354+
h_sph = h_near(lambda0, d_sphere, m_sphere, n_env, r, theta, phi, include_incident=True)
355+
e_sph2, h_sph2 = eh_near(lambda0, d_sphere, m_sphere, n_env, r, theta, phi, include_incident=True)
356+
357+
np.testing.assert_allclose(e_sph, e_expected, rtol=1e-12, atol=1e-12)
358+
np.testing.assert_allclose(h_sph, h_expected, rtol=1e-12, atol=1e-12)
359+
np.testing.assert_allclose(e_sph2, e_expected, rtol=1e-12, atol=1e-12)
360+
np.testing.assert_allclose(h_sph2, h_expected, rtol=1e-12, atol=1e-12)

0 commit comments

Comments
 (0)