Skip to content

Commit

Permalink
Added optika.materials.snells_law_scalar function.
Browse files Browse the repository at this point in the history
  • Loading branch information
byrdie committed Jun 9, 2024
1 parent f3e5163 commit 5a8716d
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
6 changes: 5 additions & 1 deletion optika/materials/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
or reflective materials.
"""

from ._snells_law import snells_law
from ._snells_law import (
snells_law_scalar,
snells_law,
)
from . import profiles
from . import matrices
from ._layers import (
Expand Down Expand Up @@ -36,6 +39,7 @@
)

__all__ = [
"snells_law_scalar",
"snells_law",
"profiles",
"matrices",
Expand Down
27 changes: 27 additions & 0 deletions optika/materials/_snells_law.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,37 @@
import named_arrays as na

__all__ = [
"snells_law_scalar",
"snells_law",
]


def snells_law_scalar(
cos_incidence: float | na.AbstractScalar,
index_refraction: float | na.AbstractScalar,
index_refraction_new: float | na.AbstractScalar,
) -> na.AbstractScalar:
"""
A simple form of Snell's law which computes the cosine of the angle
between the propagation direction inside the new medium and the interface
normal.
Parameters
----------
cos_incidence
The cosine of the incidence angle (the angle between the propagation
direction and the interface normal.)
index_refraction
The index of refraction in the current medium.
index_refraction_new
The index of refraction in the new medium.
"""
sin_incidence = np.sqrt(1 - np.square(cos_incidence))
sin_transmitted = index_refraction * sin_incidence / index_refraction_new
cos_transmitted = np.sqrt(1 - np.square(sin_transmitted))
return cos_transmitted


def snells_law(
wavelength: u.Quantity | na.AbstractScalar,
direction: na.AbstractCartesian3dVectorArray,
Expand Down
38 changes: 38 additions & 0 deletions optika/materials/_tests/test_snells_law.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,44 @@
import optika


@pytest.mark.parametrize(
argnames="cos_incidence",
argvalues=[
1,
na.linspace(-1, 1, axis="angle", num=7),
],
)
@pytest.mark.parametrize(
argnames="index_refraction",
argvalues=[
1,
],
)
@pytest.mark.parametrize(
argnames="index_refraction_new",
argvalues=[
1.5,
na.linspace(1, 2, axis="index_refraction_new", num=4),
],
)
def test_snells_law_scalar(
cos_incidence: float | na.AbstractScalar,
index_refraction: float | na.AbstractScalar,
index_refraction_new: float | na.AbstractScalar,
):
result = optika.materials.snells_law_scalar(
cos_incidence=cos_incidence,
index_refraction=index_refraction,
index_refraction_new=index_refraction_new,
)

n1 = index_refraction
n2 = index_refraction_new
result_expected = np.cos(np.arcsin(n1 * np.sin(np.arccos(cos_incidence)) / n2))

assert np.allclose(result, result_expected)


@pytest.mark.parametrize(
argnames="wavelength",
argvalues=[
Expand Down

0 comments on commit 5a8716d

Please sign in to comment.