Skip to content

Commit

Permalink
add dipole field pytests
Browse files Browse the repository at this point in the history
  • Loading branch information
rjmoerland committed Jan 27, 2025
1 parent cce039a commit cab0c75
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 13 deletions.
18 changes: 5 additions & 13 deletions src/lumicks/pyoptics/field_distributions/dipole.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def field_dipole_x(px, n_medium, lambda_vac, x, y, z):
Refractive index of the medium in which the dipole is embedded
lambda_vac : float
Wavelength in vacuum of the radiation
x, y, z : Union[float, mp.ndarray]
x, y, z : Union[float, np.ndarray]
(Array of) coordinates at which the electromagnetic field is to be evaluated
Returns
Expand All @@ -39,9 +39,7 @@ def field_dipole_x(px, n_medium, lambda_vac, x, y, z):
.. [1] Principles of Nano-optics, 2nd Ed., Ch. 2
"""
x = np.atleast_1d(x)
y = np.atleast_1d(y)
z = np.atleast_1d(z)
x, y, z = [np.atleast_1d(ax) for ax in (x, y, z)]
k = 2 * np.pi * n_medium / lambda_vac
R = np.hypot(np.hypot(x, y), z)

Expand Down Expand Up @@ -239,20 +237,14 @@ def field_dipole(p, n_medium, lambda_vac, x, y, z, farfield=False):
.. [1] Classical Electrodynamics, Ch. 9, 3rd Edition, J.D. Jackson
"""
x = np.atleast_1d(x)
y = np.atleast_1d(y)
z = np.atleast_1d(z)
x, y, z = [np.atleast_1d(ax) for ax in (x, y, z)]

r = np.hypot(np.hypot(x, y), z)
k = 2 * np.pi * n_medium / lambda_vac
fix = r == 0
r[fix] = 1
nx = x / r
ny = y / r
nz = z / r
px = p[0]
py = p[1]
pz = p[2]
nx, ny, nz = [ax / r for ax in (x, y, z)]
px, py, pz = p

nxp_x = ny * pz - nz * py
nxp_y = nz * px - nx * pz
Expand Down
66 changes: 66 additions & 0 deletions tests/field_distributions/test_dipole_fields.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import numpy as np
import pytest

from lumicks.pyoptics.field_distributions.dipole import (
farfield_dipole_position,
field_dipole,
field_dipole_y,
field_dipole_z,
)
from lumicks.pyoptics.mathutils.integration import get_integration_locations, get_nearest_order


def scale_coords(coords_list, radius: float):
return [ax * radius for ax in coords_list]


@pytest.mark.parametrize("order", [15, 28])
@pytest.mark.parametrize("radius", np.linspace(100e-9, 4e-6, 5))
def test_dipole_z(order: int, radius: float):
n_medium = 1.33
lambda_vac = 1064e-9
pz = np.pi
x, y, z, _ = get_integration_locations(get_nearest_order(order), "lebedev-laikov")
x, y, z = scale_coords((x, y, z), radius)
reference_fields = field_dipole([0.0, 0.0, pz], n_medium, lambda_vac, x, y, z)
dipole_fields = field_dipole_z(pz, n_medium, lambda_vac, x, y, z)
np.testing.assert_allclose(reference_fields, dipole_fields)


@pytest.mark.parametrize("order", [15, 28])
@pytest.mark.parametrize("radius", np.linspace(100e-9, 4e-6, 5))
def test_dipole_y(order: int, radius: float):
n_medium = 1.33
lambda_vac = 1064e-9
py = np.exp(1)
x, y, z, _ = get_integration_locations(get_nearest_order(order), "lebedev-laikov")
x, y, z = scale_coords((x, y, z), radius)
reference_fields = field_dipole([0.0, py, 0.0], n_medium, lambda_vac, x, y, z)
dipole_fields = field_dipole_y(py, n_medium, lambda_vac, x, y, z)
np.testing.assert_allclose(reference_fields, dipole_fields)


@pytest.mark.parametrize("order", [15, 28])
@pytest.mark.parametrize("radius", np.linspace(100e-9, 4e-6, 5))
def test_dipole_x(order: int, radius: float):
n_medium = 1.33
lambda_vac = 1064e-9
py = np.exp(1)
x, y, z, _ = get_integration_locations(get_nearest_order(order), "lebedev-laikov")
x, y, z = scale_coords((x, y, z), radius)
reference_fields = field_dipole([0.0, py, 0.0], n_medium, lambda_vac, x, y, z)
dipole_fields = field_dipole_y(py, n_medium, lambda_vac, x, y, z)
np.testing.assert_allclose(reference_fields, dipole_fields)


@pytest.mark.parametrize("order", [15, 28])
@pytest.mark.parametrize("radius", np.linspace(1e-2, 1.0, 5))
def test_dipole_ff(order: int, radius: float):
n_medium = 1.33
lambda_vac = 1064e-9
p = np.random.standard_normal(3)
x, y, z, _ = get_integration_locations(get_nearest_order(order), "lebedev-laikov")
x, y, z = scale_coords((x, y, z), radius)
reference_fields = field_dipole(p, n_medium, lambda_vac, x, y, z, farfield=True)
dipole_fields = farfield_dipole_position(p, n_medium, lambda_vac, x, y, z)
np.testing.assert_allclose(reference_fields[:3], dipole_fields)

0 comments on commit cab0c75

Please sign in to comment.