From 73e7b234bad478ce4e928b31f06c00ef56573e93 Mon Sep 17 00:00:00 2001 From: Hugo Uittenbosch Date: Mon, 2 Nov 2026 10:32:01 +0100 Subject: [PATCH 1/4] Feature: ConstantRefractiveIndex, numbers as n in every constructor (#184) Every constructor that takes a refractive index accepts a plain number and stores it as ConstantRefractiveIndex instead of a closure. Numbers are normalized once in Lens(shape, n::Real) and Prism(shape, n::Real); the factories pass n on, and the 8 per-constructor number methods are removed. The agent skill now states that numbers are constant indices. Co-Authored-By: Claude Opus 5.5 --- docs/src/basics/components/beamsplitters.md | 6 ++--- docs/src/basics/components/lenses.md | 6 +++-- docs/src/basics/components/prisms.md | 8 +++--- skills/beamletoptics/API.md | 2 +- skills/beamletoptics/CHECKLIST.md | 2 +- skills/beamletoptics/SKILL.md | 5 ++-- skills/beamletoptics/components/lenses.md | 4 +-- skills/beamletoptics/components/materials.md | 9 ++++--- src/Exports.jl | 2 +- .../Beamsplitters/Compensators.jl | 5 ++-- .../Beamsplitters/CubeBeamsplitter.jl | 3 +-- .../Beamsplitters/PlateBeamsplitter.jl | 8 +++--- src/OpticalComponents/DoubletLenses.jl | 12 ++++----- src/OpticalComponents/Lenses.jl | 22 +++++++++------- .../Polarizers/LinearPolarizer.jl | 4 +-- src/OpticalComponents/Prisms.jl | 14 +++++----- src/OpticalComponents/SphericalLenses.jl | 14 +++++----- src/OpticalComponents/TripletLenses.jl | 18 ++++++------- src/Utils/RefractiveIndexUtils.jl | 19 ++++++++++++-- test/Components/TestBeamsplitters.jl | 6 +++++ test/Components/TestPolarizers.jl | 1 + test/Lenses/TestSphericalLenses.jl | 26 +++++++++++++++++++ test/TestUtils.jl | 8 ++++++ 23 files changed, 131 insertions(+), 73 deletions(-) diff --git a/docs/src/basics/components/beamsplitters.md b/docs/src/basics/components/beamsplitters.md index 1efaf04d7..023366584 100644 --- a/docs/src/basics/components/beamsplitters.md +++ b/docs/src/basics/components/beamsplitters.md @@ -35,7 +35,7 @@ Plate beamsplitters provide a more sophisticated model by incorporating a substr The [`RectangularPlateBeamsplitter`](@ref) represents a planar, rectangular substrate with a partially reflective coating. ```@docs; canonical=false -RectangularPlateBeamsplitter(::Real, ::Real, ::Real, ::BeamletOptics.RefractiveIndex) +RectangularPlateBeamsplitter(::Real, ::Real, ::Real, ::Any) ``` A common application involves that this type of beamsplitter is paired with a compensator plate. Below an exemplary illustration of such a setup is shown, where the beamsplitter reflects part of the incoming beam perpendiculary. A [`RectangularCompensatorPlate`](@ref) ensures that the parallel path offset is corrected. @@ -49,7 +49,7 @@ The depicted system consist of a rectangular beamsplitter (e.g. [BSW26R](https:/ This variant uses a circular substrate. While components of this type often feature a wedge angle to avoid "ghost beams" in practice, e.g. the [BSW26](https://www.thorlabs.com/thorproduct.cfm?partnumber=BSW26), this is not modeled here. ```@docs; canonical=false -RoundPlateBeamsplitter(::Real, ::Real, ::BeamletOptics.RefractiveIndex) +RoundPlateBeamsplitter(::Real, ::Real, ::Any) ``` ## Cube beamsplitter @@ -57,7 +57,7 @@ RoundPlateBeamsplitter(::Real, ::Real, ::BeamletOptics.RefractiveIndex) The [`CubeBeamsplitter`](@ref) is composed of two [`RightAnglePrism`](@ref)s with a partially reflective interface at their internal joint. The splitting interface is represented by a [`ThinBeamsplitter`](@ref). As with other beamsplitters in this package, the cube beamsplitter uses scalar reflection and transmission coefficients. ```@docs; canonical=false -CubeBeamsplitter(::Real, ::BeamletOptics.RefractiveIndex) +CubeBeamsplitter(::Real, ::Any) ``` A classic application of cube beamsplitters is in Mach–Zehnder interferometers, where two beamsplitters combine with additional mirrors to form two optical paths that later recombine. The figure below shows a rudimentary Mach–Zehnder arrangement using two cube beamsplitters and two [`RightAnglePrismMirror`](@ref)s: diff --git a/docs/src/basics/components/lenses.md b/docs/src/basics/components/lenses.md index ae94c7eb9..a5d1384c0 100644 --- a/docs/src/basics/components/lenses.md +++ b/docs/src/basics/components/lenses.md @@ -50,7 +50,7 @@ A [`Lens`](@ref) can be then constructed with the following function call: ```@docs; canonical=false -Lens(::BeamletOptics.AbstractRotationallySymmetricSurface, ::BeamletOptics.AbstractRotationallySymmetricSurface, ::Real, ::BeamletOptics.RefractiveIndex) +Lens(::BeamletOptics.AbstractRotationallySymmetricSurface, ::BeamletOptics.AbstractRotationallySymmetricSurface, ::Real, ::Any) ``` ### Lens constructor example @@ -76,6 +76,8 @@ LB1811 = Lens( ) ``` +Every constructor that takes a refractive index also accepts a plain number, e.g. `1.5`, for a material without dispersion. It is stored as a [`ConstantRefractiveIndex`](@ref). Use a [`DiscreteRefractiveIndex`](@ref) or a [`SellmeierEquation`](@ref) whenever the result depends on the wavelength. + ![Lens constructor](lens_constructor.png) ### SDF-based spherical lenses @@ -216,7 +218,7 @@ The following image shows the [AC254-150-AB](https://www.thorlabs.com/thorproduc Doublet lenses with aspherical surfaces, or with a different clear aperture per surface, are specified via the surface types introduced in the [Surface based lens construction](@ref) section. ```@docs; canonical=false -DoubletLens(::BeamletOptics.AbstractRotationallySymmetricSurface, ::BeamletOptics.AbstractRotationallySymmetricSurface, ::BeamletOptics.AbstractRotationallySymmetricSurface, ::Real, ::Real, ::BeamletOptics.RefractiveIndex, ::BeamletOptics.RefractiveIndex) +DoubletLens(::BeamletOptics.AbstractRotationallySymmetricSurface, ::BeamletOptics.AbstractRotationallySymmetricSurface, ::BeamletOptics.AbstractRotationallySymmetricSurface, ::Real, ::Real, ::Any, ::Any) ``` ## Triplet lenses diff --git a/docs/src/basics/components/prisms.md b/docs/src/basics/components/prisms.md index 3116b8c51..a8be121d1 100644 --- a/docs/src/basics/components/prisms.md +++ b/docs/src/basics/components/prisms.md @@ -15,7 +15,7 @@ prism = Prism([(0.0, 0.0), (30e-3, 0.0), (0.0, 17.32e-3)], 20e-3, NBK7) ``` ```@docs; canonical=false -Prism(::AbstractVector, ::Real, ::BeamletOptics.RefractiveIndex) +Prism(::AbstractVector, ::Real, ::Any) BeamletOptics.PolygonPrismSDF ``` @@ -24,7 +24,7 @@ BeamletOptics.PolygonPrismSDF The [`EquilateralPrism`](@ref) is centered at the centroid of its triangle with the apex along +y and is the classic dispersing prism. A ray traversing it symmetrically at minimum deviation is deflected by ``\delta = 2\arcsin(n \sin(\alpha/2)) - \alpha`` with the apex angle ``\alpha = 60^\circ``. The [`DovePrism`](@ref) is a trapezoid with 45° end faces whose long axis is the y-axis. The [`RightAnglePrism`](@ref) has its legs along x and y. ```@docs; canonical=false -EquilateralPrism(::Real, ::Real, ::BeamletOptics.RefractiveIndex) -DovePrism(::Real, ::Real, ::Real, ::BeamletOptics.RefractiveIndex) -RightAnglePrism(::Real, ::Real, ::BeamletOptics.RefractiveIndex) +EquilateralPrism(::Real, ::Real, ::Any) +DovePrism(::Real, ::Real, ::Real, ::Any) +RightAnglePrism(::Real, ::Real, ::Any) ``` diff --git a/skills/beamletoptics/API.md b/skills/beamletoptics/API.md index 7d238e195..9e4446bc7 100644 --- a/skills/beamletoptics/API.md +++ b/skills/beamletoptics/API.md @@ -36,7 +36,7 @@ Use `scripts/api_lookup.jl NAME` to print the docstring and signatures of any na | Rays & beams | `Ray`, `PolarizedRay`, `Beam`, `GaussianBeamlet`, `AstigmaticGaussianBeamlet`, `rays`, `point_on_beam`, `path_segments`, `rayleigh_range`, `normal3d` | | Sources | `CollimatedSource`, `UniformDiscSource`, `PointSource`, `UniformPointSource`, `UniformLineSource`, `UniformFanSource`, `num_rays!`, `CollimatedGaussianBeamletSource`, `SphericalGaussianBeamletSource`, `EllipticalGaussianBeamletSource`, `GaussianBeamletDecomposition`, `WavefrontBeamletDecomposition`, `GaussianModeDecomposition`, `AstigmaticBeamGroup` | | System | `System`, `StaticSystem`, `solve_system!`, `initialize!`, `ObjectGroup` | -| Materials | `DiscreteRefractiveIndex`, `SellmeierEquation` | +| Materials | `DiscreteRefractiveIndex`, `SellmeierEquation`, `ConstantRefractiveIndex` | | Lenses | `Lens`, `ThinLens`, `SphericalLens`, `DoubletLens`, `SphericalDoubletLens`, `TripletLens`, `SphericalTripletLens`, `thickness` | | Surfaces | `SphericalSurface`, `CircularFlatSurface`, `RectangularFlatSurface`, `EvenAsphericalSurface`, `CylindricalSurface`, `AcylindricalSurface` | | Mirrors | `Mirror`, `RoundPlanoMirror`, `SquarePlanoMirror`, `SquarePlanoMirror2D`, `RectangularPlanoMirror`, `RightAnglePrismMirror`, `SphericalMirror`, `ConicMirror`, `OffAxisConicMirror`, `ParabolicMirror`, `OffAxisParabolicMirror`, `EllipsoidalMirror`, `OffAxisEllipsoidalMirror`, `HyperbolicMirror`, `OffAxisHyperbolicMirror`, `Retroreflector` | diff --git a/skills/beamletoptics/CHECKLIST.md b/skills/beamletoptics/CHECKLIST.md index bb4618acc..bdc4bc091 100644 --- a/skills/beamletoptics/CHECKLIST.md +++ b/skills/beamletoptics/CHECKLIST.md @@ -3,7 +3,7 @@ ## Correctness - [ ] All lengths and wavelengths in **meters** (`532e-9`, not `532`); angles in **radians** (`deg2rad`). -- [ ] Refractive indices are callables of λ (`λ -> 1.5`, `SellmeierEquation`, `DiscreteRefractiveIndex`). +- [ ] Refractive indices are `SellmeierEquation`, `DiscreteRefractiveIndex`, a function of λ, or a number (constant, no dispersion); a number only where dispersion does not matter. - [ ] `DiscreteRefractiveIndex` contains the exact wavelength of every source (no interpolation). - [ ] Lens radius signs follow ISO 10110 (biconvex: `r1 > 0`, `r2 < 0`). - [ ] Geometry assumes +y as the optical axis; rotations are cumulative and relative. diff --git a/skills/beamletoptics/SKILL.md b/skills/beamletoptics/SKILL.md index 550135132..6e416de87 100644 --- a/skills/beamletoptics/SKILL.md +++ b/skills/beamletoptics/SKILL.md @@ -24,8 +24,9 @@ When this Skill is active: - Read `src/` of the installed package (`julia -e 'using BeamletOptics; println(pkgdir(BeamletOptics))'`). - Everything is **SI**: meters, radians, watts, V/m. **Wavelengths are in meters** (`532e-9`). - The **optical axis is +y**, not z. Components spawn at the origin aligned with +y. -- **Refractive indices are callables** `n(λ)` (λ in meters), never plain numbers: - `λ -> 1.5`, `DiscreteRefractiveIndex(...)` or `SellmeierEquation(...)`. +- **Refractive indices are callables** `n(λ)` (λ in meters): `DiscreteRefractiveIndex(...)`, + `SellmeierEquation(...)` or a function. A plain number (`1.5`) is accepted everywhere and stored as + `ConstantRefractiveIndex(1.5)`, i.e. without dispersion. - **Detectors accumulate.** Solve with `solve_system!(system, beam; initialize = true)`, or call `initialize!(system)` (all detectors) or `empty!(detector)` (one) before every `solve_system!` that reuses the detectors; never reset between sources that should superpose. Field functions superpose all stored hits coherently; moving the detector empties it. diff --git a/skills/beamletoptics/components/lenses.md b/skills/beamletoptics/components/lenses.md index 0c5080fec..b06ef5043 100644 --- a/skills/beamletoptics/components/lenses.md +++ b/skills/beamletoptics/components/lenses.md @@ -6,7 +6,7 @@ ## Spherical catalog lenses ```julia -SphericalLens(r1, r2, l, d = BeamletOptics.inch, n = λ -> 1.5) # l: center thickness, d: diameter +SphericalLens(r1, r2, l, d = BeamletOptics.inch, n = 1.5) # l: center thickness, d: diameter ThinLens(R1, R2, d, n) # thin-lens Lens; SphericalLens uses it for l = 0 SphericalDoubletLens(r1, r2, r3, l1, l2, d, n1, n2) # cemented doublet SphericalTripletLens(r1, r2, r3, r4, l1, l2, l3, d, n1, n2, n3) # cemented triplet @@ -64,7 +64,7 @@ TripletLens(s1, s2, s3, s4, l1, l2, l3, n1, n2, n3) # s2, s3: cemented surfaces ``` Surfaces are the rotationally symmetric surfaces listed above, in order along +y; `l` are center -thicknesses, `n` are `RefractiveIndex` functions (not plain numbers). Element `i` is +thicknesses, `n` are refractive indices (see `materials.md`; numbers are constant indices). Element `i` is `Lens(s[i], s[i+1], l[i], n[i])`, placed flush behind the previous one, so the limits of the general `Lens` constructor apply to each element. Use these for aspherical surfaces or a different clear aperture per surface. Cylindrical surfaces are not supported (`MethodError`). diff --git a/skills/beamletoptics/components/materials.md b/skills/beamletoptics/components/materials.md index 4b7540164..1c42b572c 100644 --- a/skills/beamletoptics/components/materials.md +++ b/skills/beamletoptics/components/materials.md @@ -1,11 +1,12 @@ # Refractive index / materials A refractive index is anything of type -`RefractiveIndex = Union{Function, DiscreteRefractiveIndex, SellmeierEquation}`. It must be callable as -`n(λ)` with **λ in meters** and return a real number. Plain numbers are **not** accepted. +`RefractiveIndex = Union{Function, DiscreteRefractiveIndex, SellmeierEquation, ConstantRefractiveIndex}`. +It must be callable as `n(λ)` with **λ in meters** and return a real number. Every constructor that +takes `n` also accepts a plain number and stores it as `ConstantRefractiveIndex(n)` (no dispersion). ```julia -n_const = λ -> 1.5 # constant index +n_const = 1.5 # constant index, same as ConstantRefractiveIndex(1.5) NBK7 = DiscreteRefractiveIndex([532e-9, 1064e-9], # exact lookup table, [1.5195, 1.5066]) # NO interpolation: any other λ -> KeyError @@ -17,5 +18,7 @@ N_BK7(532e-9) # still called with meters - Coefficients from refractiveindex.info or glass catalogs (µm-based) go straight into `SellmeierEquation`. - The surrounding medium is always n = 1. +- A constant index ignores dispersion. Use `SellmeierEquation` or `DiscreteRefractiveIndex` whenever the + result depends on λ (several wavelengths, chromatic focal shift, prisms for dispersion). - Constructors validate the index by calling it with `1`, `1f0` and `1.0`, so closures must accept any `Real`. - Materials are used by lenses, prisms, plate/cube beamsplitters, compensator plates and linear polarizers. diff --git a/src/Exports.jl b/src/Exports.jl index 1262140e0..9f63d42de 100644 --- a/src/Exports.jl +++ b/src/Exports.jl @@ -19,7 +19,7 @@ export System, StaticSystem, solve_system!, initialize! export ObjectGroup # additional -export DiscreteRefractiveIndex, SellmeierEquation +export DiscreteRefractiveIndex, SellmeierEquation, ConstantRefractiveIndex #= components diff --git a/src/OpticalComponents/Beamsplitters/Compensators.jl b/src/OpticalComponents/Beamsplitters/Compensators.jl index 8ed84237f..1388aa55a 100644 --- a/src/OpticalComponents/Beamsplitters/Compensators.jl +++ b/src/OpticalComponents/Beamsplitters/Compensators.jl @@ -10,9 +10,9 @@ The compensator is aligned with the positive y-axis. The first surface lies at t - `width`: compensator width along the x-axis in [m] - `height`: compensator height along the z-axis in [m] - `thickness`: compensator thickness along the y-axis in [m] -- `n`: the [`RefractiveIndex`](@ref) of the substrate +- `n`: the [`RefractiveIndex`](@ref) of the substrate, or a number for a constant refractive index """ -function RectangularCompensatorPlate(width::W, height::H, thickness::T, n::RefractiveIndex) where {W<:Real,H<:Real,T<:Real} +function RectangularCompensatorPlate(width::W, height::H, thickness::T, n) where {W<:Real,H<:Real,T<:Real} shape = CuboidMesh(width, thickness, height) translate3d!(shape, [ -width/2, @@ -23,4 +23,3 @@ function RectangularCompensatorPlate(width::W, height::H, thickness::T, n::Refra return Prism(shape, n) end -RectangularCompensatorPlate(w::Real, h::Real, t::Real, n::Real) = RectangularCompensatorPlate(w, h, t, λ->n) \ No newline at end of file diff --git a/src/OpticalComponents/Beamsplitters/CubeBeamsplitter.jl b/src/OpticalComponents/Beamsplitters/CubeBeamsplitter.jl index 5b523d482..3d8eaf56b 100644 --- a/src/OpticalComponents/Beamsplitters/CubeBeamsplitter.jl +++ b/src/OpticalComponents/Beamsplitters/CubeBeamsplitter.jl @@ -48,7 +48,7 @@ coating is orientated at a 45° angle with respect to the y-axis. """ function CubeBeamsplitter( leg_length::Real, - n::RefractiveIndex; + n; reflectance::Real=0.5 ) front = RightAnglePrism(leg_length, leg_length, n) @@ -59,7 +59,6 @@ function CubeBeamsplitter( set_new_origin3d!(shape(bs)) return CubeBeamsplitter(front, back, bs) end -CubeBeamsplitter(leg_length::Real, n::Real; reflectance::Real=0.5) = CubeBeamsplitter(leg_length, λ -> n; reflectance) function interact3d( system::AbstractSystem, diff --git a/src/OpticalComponents/Beamsplitters/PlateBeamsplitter.jl b/src/OpticalComponents/Beamsplitters/PlateBeamsplitter.jl index d490104c7..b41f10165 100644 --- a/src/OpticalComponents/Beamsplitters/PlateBeamsplitter.jl +++ b/src/OpticalComponents/Beamsplitters/PlateBeamsplitter.jl @@ -77,7 +77,7 @@ the substrate lies on the positive y-side of it, from y = 0 to `thickness`. See - `width`: substrate width along the x-axis in [m] - `height`: substrate height along the z-axis in [m] - `thickness`: substrate thickness along the y-axis in [m] -- `n`: the [`RefractiveIndex`](@ref) of the substrate +- `n`: the [`RefractiveIndex`](@ref) of the substrate, or a number for a constant refractive index # Keywords @@ -87,7 +87,7 @@ function RectangularPlateBeamsplitter( width::Real, height::Real, thickness::Real, - n::RefractiveIndex; + n; reflectance::Real=0.5 ) # create substrate prism and move into pos @@ -131,7 +131,7 @@ lies on the positive y-side of it, from y = 0 to `thickness`. See also [`Rectang - `diameter`: x-z-plane substrate diameter in [m] - `thickness`: substrate thickness along the y-axis in [m] -- `n`: the [`RefractiveIndex`](@ref) of the substrate +- `n`: the [`RefractiveIndex`](@ref) of the substrate, or a number for a constant refractive index # Keywords @@ -140,7 +140,7 @@ lies on the positive y-side of it, from y = 0 to `thickness`. See also [`Rectang function RoundPlateBeamsplitter( diameter::Real, thickness::Real, - n::RefractiveIndex; + n; reflectance::Real=0.5 ) # create substrate cylinder prism diff --git a/src/OpticalComponents/DoubletLenses.jl b/src/OpticalComponents/DoubletLenses.jl index 5a8bf0a99..90601ece0 100644 --- a/src/OpticalComponents/DoubletLenses.jl +++ b/src/OpticalComponents/DoubletLenses.jl @@ -47,8 +47,8 @@ both elements share the cemented surface `s2`. - `s3`: third surface - `l1`: first lens center thickness in m - `l2`: second lens center thickness in m -- `n1`: first lens [`RefractiveIndex`](@ref) -- `n2`: second lens [`RefractiveIndex`](@ref) +- `n1`: first lens [`RefractiveIndex`](@ref), or a number for a constant refractive index +- `n2`: second lens [`RefractiveIndex`](@ref), or a number for a constant refractive index # Additional information @@ -72,8 +72,8 @@ function DoubletLens( s3::AbstractRotationallySymmetricSurface, l1::Real, l2::Real, - n1::RefractiveIndex, - n2::RefractiveIndex) + n1, + n2) # Generate "cemented" front and back lenses that share the surface s2 front = Lens(s1, s2, l1, n1) back = Lens(s2, s3, l2, n2) @@ -96,8 +96,8 @@ For radii sign definition, refer to the [`SphericalLens`](@ref) constructor. - `l1`: first lens thickness - `l2`: second lens thickness - `d`: lens diameter -- `n1`: first lens [`RefractiveIndex`](@ref) -- `n1`: second lens [`RefractiveIndex`](@ref) +- `n1`: first lens [`RefractiveIndex`](@ref), or a number for a constant refractive index +- `n2`: second lens [`RefractiveIndex`](@ref), or a number for a constant refractive index """ function SphericalDoubletLens(r1, r2, r3, l1, l2, d, n1, n2) # Generate "cemented" front and back spherical lenses diff --git a/src/OpticalComponents/Lenses.jl b/src/OpticalComponents/Lenses.jl index 960792b66..c25679cb7 100644 --- a/src/OpticalComponents/Lenses.jl +++ b/src/OpticalComponents/Lenses.jl @@ -142,8 +142,8 @@ Refer to the [`Lens`](@ref) and [`SphericalLens`](@ref) constructors for more in !!! info "Refractive index" The chromatic dispersion of the lens is represented by a λ-dependent function for `n` - and must be provided by the user. For testing purposes, an anonymous function, e.g. λ -> 1.5 - can be passed such that the lens has the same refractive index for all wavelengths. + and must be provided by the user. A number, e.g. `1.5`, is stored as a + [`ConstantRefractiveIndex`](@ref), i.e. the lens has the same refractive index for all wavelengths. """ struct Lens{T, S <: AbstractShape{T}, N <: RefractiveIndex} <: AbstractRefractiveOptic{T, N} shape::S @@ -155,6 +155,8 @@ struct Lens{T, S <: AbstractShape{T}, N <: RefractiveIndex} <: AbstractRefractiv end end +Lens(shape::AbstractShape, n::Real) = Lens(shape, ConstantRefractiveIndex(n)) + thickness(l::Lens) = thickness(shape(l)) """ @@ -176,7 +178,7 @@ Constructs a new [`Lens`](@ref) object using the surface specifications `front_s `back_surface` and the `center_thickness`. These inputs are used to construct a [`UnionSDF`](@ref) that consists of the appropriate sub-SDFs to represent the shape of the lens. -The material properties are supplied via the `n` parameter. +The material is supplied via the [`RefractiveIndex`](@ref) `n`, or a number for a constant refractive index. # Additional information @@ -191,7 +193,7 @@ function Lens( front_surface::AbstractRotationallySymmetricSurface, back_surface::AbstractRotationallySymmetricSurface, center_thickness::Real, - n::RefractiveIndex) + n) # Define effective (optical and mechanical) diameters: d_mid = min(diameter(front_surface), diameter(back_surface)) md_mid = max(mechanical_diameter(front_surface), mechanical_diameter(back_surface)) @@ -300,7 +302,7 @@ function Lens( end function Lens(front_surface::AbstractRotationallySymmetricSurface, - center_thickness::Real, n::RefractiveIndex) + center_thickness::Real, n) Lens( front_surface, CircularFlatSurface(diameter(front_surface)), @@ -310,7 +312,7 @@ function Lens(front_surface::AbstractRotationallySymmetricSurface, end function Lens(front_surface::CircularFlatSurface, back_surface::CircularFlatSurface, - center_thickness::Real, n::RefractiveIndex) + center_thickness::Real, n) d_mid = min(diameter(front_surface), diameter(back_surface)) return Lens( @@ -330,7 +332,7 @@ This method of `Lens` is specific for cylindric lenses and has some limitations: - The cylinder height of both surfaces has to be identical - No mixture with non-cylindric surfaces is supported at the moment -The material properties are supplied via the `n` parameter. +The material is supplied via the [`RefractiveIndex`](@ref) `n`, or a number for a constant refractive index. # Additional information @@ -341,7 +343,7 @@ function Lens( front_surface::AbstractCylindricalSurface, back_surface::AbstractCylindricalSurface, center_thickness::Real, - n::RefractiveIndex) + n) # Initialize remaining box section length. l0 = center_thickness @@ -399,7 +401,7 @@ function Lens( end function Lens(front_surface::AbstractCylindricalSurface, - center_thickness::Real, n::RefractiveIndex) + center_thickness::Real, n) Lens( front_surface, RectangularFlatSurface(diameter(front_surface)), @@ -443,7 +445,7 @@ function cylindric_lens_outer_parameters( end function Lens(front_surface::RectangularFlatSurface, back_surface::RectangularFlatSurface, - center_thickness::Real, n::RefractiveIndex) + center_thickness::Real, n) d_mid = min(diameter(front_surface), diameter(back_surface)) mid = BoxSDF(d_mid, center_thickness, d_mid) translate3d!(mid, [0, center_thickness / 2, 0]) diff --git a/src/OpticalComponents/Polarizers/LinearPolarizer.jl b/src/OpticalComponents/Polarizers/LinearPolarizer.jl index a8a40bca9..a6fb0cade 100644 --- a/src/OpticalComponents/Polarizers/LinearPolarizer.jl +++ b/src/OpticalComponents/Polarizers/LinearPolarizer.jl @@ -57,7 +57,7 @@ of the given `diameter`, flush against the film on both sides. - `diameter`: outer diameter of the film and substrates in [m] - `front_thickness`: thickness of the front glass substrate in [m] - `back_thickness`: thickness of the back glass substrate in [m] -- `n`: [`RefractiveIndex`](@ref) of both glass substrates +- `n`: [`RefractiveIndex`](@ref) of both glass substrates, or a number for a constant refractive index # Keywords @@ -70,7 +70,7 @@ The component is centered on the film (local origin); the front substrate extend along local +y. The film transmits along local x and blocks local z. """ function RoundLinearPolarizer(diameter::Real, front_thickness::Real, back_thickness::Real, - n::RefractiveIndex; cutoff_strength = eps()) + n; cutoff_strength = eps()) front_thickness > 0 || throw(ArgumentError("front_thickness must be positive")) back_thickness > 0 || throw(ArgumentError("back_thickness must be positive")) diff --git a/src/OpticalComponents/Prisms.jl b/src/OpticalComponents/Prisms.jl index 18f75599c..47a5c1898 100644 --- a/src/OpticalComponents/Prisms.jl +++ b/src/OpticalComponents/Prisms.jl @@ -13,6 +13,8 @@ struct Prism{T, S <: AbstractShape{T}, N <: RefractiveIndex} <: AbstractRefracti end end +Prism(shape::AbstractShape, n::Real) = Prism(shape, ConstantRefractiveIndex(n)) + thickness(p::Prism) = thickness(shape(p)) """ @@ -28,11 +30,10 @@ and the hypotenuse passes through the local origin, see [`RightAnglePrismSDF`](@ - `height`: extent along the local z-axis in [m] - `n`: [`RefractiveIndex`](@ref) of the prism, or a number for a constant refractive index """ -function RightAnglePrism(leg_length::Real, height::Real, n::RefractiveIndex) +function RightAnglePrism(leg_length::Real, height::Real, n) shape = RightAnglePrismSDF(leg_length, height) return Prism(shape, n) end -RightAnglePrism(leg_length::Real, height::Real, n::Real) = RightAnglePrism(leg_length, height, λ -> n) """ Prism(vertices, height, n) @@ -50,10 +51,9 @@ for the geometry. See also [`EquilateralPrism`](@ref) and [`DovePrism`](@ref). - `height`: extent along the local z-axis in [m], the prism is centered at `z = 0` - `n`: [`RefractiveIndex`](@ref) of the prism, or a number for a constant refractive index """ -function Prism(vertices::AbstractVector, height::Real, n::RefractiveIndex) +function Prism(vertices::AbstractVector, height::Real, n) return Prism(PolygonPrismSDF(vertices, height), n) end -Prism(vertices::AbstractVector, height::Real, n::Real) = Prism(vertices, height, λ -> n) """ EquilateralPrism(side, height, n) @@ -69,12 +69,11 @@ minimum deviation is deflected towards the base. - `height`: extent along the local z-axis in [m] - `n`: [`RefractiveIndex`](@ref) of the prism, or a number for a constant refractive index """ -function EquilateralPrism(side::Real, height::Real, n::RefractiveIndex) +function EquilateralPrism(side::Real, height::Real, n) side > 0 || throw(ArgumentError("the side length must be positive")) r = side * sqrt(oftype(float(side), 3)) / 6 # inradius return Prism([(-side / 2, -r), (side / 2, -r), (zero(r), 2r)], height, n) end -EquilateralPrism(side::Real, height::Real, n::Real) = EquilateralPrism(side, height, λ -> n) """ DovePrism(length, aperture, height, n) @@ -92,10 +91,9 @@ cross-section's bounding box. - `height`: extent along the local z-axis in [m] - `n`: [`RefractiveIndex`](@ref) of the prism, or a number for a constant refractive index """ -function DovePrism(length::Real, aperture::Real, height::Real, n::RefractiveIndex) +function DovePrism(length::Real, aperture::Real, height::Real, n) aperture > 0 || throw(ArgumentError("the aperture must be positive")) length > 2aperture || throw(ArgumentError("the length must be larger than twice the aperture")) a, l = aperture / 2, length / 2 return Prism([(-a, -l), (a, -(l - aperture)), (a, l - aperture), (-a, l)], height, n) end -DovePrism(length::Real, aperture::Real, height::Real, n::Real) = DovePrism(length, aperture, height, λ -> n) diff --git a/src/OpticalComponents/SphericalLenses.jl b/src/OpticalComponents/SphericalLenses.jl index f58c7217f..7d9af0a7f 100644 --- a/src/OpticalComponents/SphericalLenses.jl +++ b/src/OpticalComponents/SphericalLenses.jl @@ -1,5 +1,5 @@ """ - SphericalLens(r1, r2, l, d=1inch, n=λ->1.5) + SphericalLens(r1, r2, l, d=1inch, n=1.5) Creates a spherical [`Lens`](@ref) based on: @@ -7,7 +7,7 @@ Creates a spherical [`Lens`](@ref) based on: - `r2`: back radius - `l`: lens thickness - `d`: lens diameter, default is one inch -- `n`: [`RefractiveIndex`](@ref) as a function of λ, i.e. `n = n(λ)` +- `n`: [`RefractiveIndex`](@ref) as a function of λ, i.e. `n = n(λ)`, or a number for a constant refractive index # Notes @@ -17,7 +17,7 @@ Creates a spherical [`Lens`](@ref) based on: !!! info "Thin lenses" If `l` is set to zero, a [`ThinLens`](@ref) will be created. However, note that the actual lens thickness will be different from zero. """ -function SphericalLens(r1::Real, r2::Real, l::Real, d::Real = 1inch, n::RefractiveIndex = λ -> 1.5) +function SphericalLens(r1::Real, r2::Real, l::Real, d::Real = 1inch, n = 1.5) # Test for thin lens if iszero(l) return ThinLens(r1, r2, d, n) @@ -31,16 +31,14 @@ function SphericalLens(r1::Real, r2::Real, l::Real, d::Real = 1inch, n::Refracti ) end -SphericalLens(r1, r2, l, d, n::Real) = SphericalLens(r1, r2, l, d, λ -> n) """ - ThinLens(R1::Real, R2::Real, d::Real, n::Function) + ThinLens(R1::Real, R2::Real, d::Real, n) Directly creates an ideal spherical thin [`Lens`](@ref) with radii of curvature `R1` and `R2` and diameter `d` -and [`RefractiveIndex`](@ref) `n`. +and [`RefractiveIndex`](@ref) `n`, or a number for a constant refractive index. """ -function ThinLens(R1::Real, R2::Real, d::Real, n::RefractiveIndex) +function ThinLens(R1::Real, R2::Real, d::Real, n) shape = ThinLensSDF(R1, R2, d) return Lens(shape, n) end -ThinLens(R1::Real, R2::Real, d::Real, n::Real) = ThinLens(R1, R2, d, x -> n) \ No newline at end of file diff --git a/src/OpticalComponents/TripletLenses.jl b/src/OpticalComponents/TripletLenses.jl index e585478fe..54ef1ca7f 100644 --- a/src/OpticalComponents/TripletLenses.jl +++ b/src/OpticalComponents/TripletLenses.jl @@ -60,9 +60,9 @@ along +y by `l1` and `l1 + l2`, so that neighbouring elements share the cemented - `l1`: first lens center thickness in m - `l2`: second lens center thickness in m - `l3`: third lens center thickness in m -- `n1`: first lens [`RefractiveIndex`](@ref) -- `n2`: second lens [`RefractiveIndex`](@ref) -- `n3`: third lens [`RefractiveIndex`](@ref) +- `n1`: first lens [`RefractiveIndex`](@ref), or a number for a constant refractive index +- `n2`: second lens [`RefractiveIndex`](@ref), or a number for a constant refractive index +- `n3`: third lens [`RefractiveIndex`](@ref), or a number for a constant refractive index # Additional information @@ -88,9 +88,9 @@ function TripletLens( l1::Real, l2::Real, l3::Real, - n1::RefractiveIndex, - n2::RefractiveIndex, - n3::RefractiveIndex) + n1, + n2, + n3) # Generate "cemented" front, middle and back lenses that share the surfaces s2 and s3 front = Lens(s1, s2, l1, n1) middle = Lens(s2, s3, l2, n2) @@ -118,9 +118,9 @@ For radii sign definition, refer to the [`SphericalLens`](@ref) constructor. - `l2`: second lens thickness - `l3`: third lens thickness - `d`: lens diameter -- `n1`: first lens [`RefractiveIndex`](@ref) -- `n2`: second lens [`RefractiveIndex`](@ref) -- `n3`: third lens [`RefractiveIndex`](@ref) +- `n1`: first lens [`RefractiveIndex`](@ref), or a number for a constant refractive index +- `n2`: second lens [`RefractiveIndex`](@ref), or a number for a constant refractive index +- `n3`: third lens [`RefractiveIndex`](@ref), or a number for a constant refractive index # Additional information diff --git a/src/Utils/RefractiveIndexUtils.jl b/src/Utils/RefractiveIndexUtils.jl index ce71d3bad..79899071e 100644 --- a/src/Utils/RefractiveIndexUtils.jl +++ b/src/Utils/RefractiveIndexUtils.jl @@ -58,6 +58,19 @@ function test_refractive_index_function(input) return nothing end +""" + ConstantRefractiveIndex{T} + +Represents a refractive index `n` that is the same for all wavelengths, i.e. without dispersion. +Can be called like a function `n = n(λ)`. Constructors that take a [`RefractiveIndex`](@ref) store +a plain number `n` as `ConstantRefractiveIndex(n)`. +""" +struct ConstantRefractiveIndex{T <: Real} + n::T +end + +(cri::ConstantRefractiveIndex)(λ) = cri.n + """ SellmeierEquation @@ -106,6 +119,8 @@ The core assumption is that: 1. the refractive index is callable with a **single** `Number` argument `λ` to represent the wavelength in [m] 2. the return value is a **single** `Number` value for the refractive index -Refer to e.g. [`DiscreteRefractiveIndex`](@ref). +Refer to e.g. [`DiscreteRefractiveIndex`](@ref), [`SellmeierEquation`](@ref) and +[`ConstantRefractiveIndex`](@ref). Constructors of optical components also accept a plain number +`n` and store it as a [`ConstantRefractiveIndex`](@ref). """ -const RefractiveIndex = Union{Function, DiscreteRefractiveIndex, SellmeierEquation} \ No newline at end of file +const RefractiveIndex = Union{Function, DiscreteRefractiveIndex, SellmeierEquation, ConstantRefractiveIndex} \ No newline at end of file diff --git a/test/Components/TestBeamsplitters.jl b/test/Components/TestBeamsplitters.jl index 0ef0c496c..3e00dac8e 100644 --- a/test/Components/TestBeamsplitters.jl +++ b/test/Components/TestBeamsplitters.jl @@ -131,6 +131,12 @@ const mm = 1e-3 @test prism isa Prism @test BMO.refractive_index(prism, 1e-6) == N0 @test BMO.refractive_index(prism, 500e-9) == N0 + @test BMO.refractive_index(prism) isa ConstantRefractiveIndex + for substrate in (RectangularPlateBeamsplitter(25e-3, 25e-3, 5e-3, N0).substrate, + RoundPlateBeamsplitter(25e-3, 5e-3, N0).substrate, + RectangularCompensatorPlate(25e-3, 25e-3, 5e-3, N0)) + @test BMO.refractive_index(substrate) == ConstantRefractiveIndex(N0) + end cbs = CubeBeamsplitter(25e-3, N0; reflectance = 0.3) @test BMO.refractive_index(cbs, 1e-6) == N0 diff --git a/test/Components/TestPolarizers.jl b/test/Components/TestPolarizers.jl index 4c496d57b..e221e25e9 100644 --- a/test/Components/TestPolarizers.jl +++ b/test/Components/TestPolarizers.jl @@ -193,6 +193,7 @@ const mm = 1e-3 t = transmission_axis(lp) @test norm(t) ≈ 1 @test abs(dot(t, [1, 0, 0])) ≈ 1 atol = 1e-12 + @test BMO.refractive_index(RoundLinearPolarizer(25.4mm, 1.6mm, 1.6mm, 1.5), 1e-6) == 1.5 for θ in 0:15:180 lp2 = RoundLinearPolarizer(25.4mm, 1.6mm, 1.6mm, n) diff --git a/test/Lenses/TestSphericalLenses.jl b/test/Lenses/TestSphericalLenses.jl index 18de1a0df..52b3220c6 100644 --- a/test/Lenses/TestSphericalLenses.jl +++ b/test/Lenses/TestSphericalLenses.jl @@ -76,6 +76,32 @@ const mm = 1e-3 end end + @testset "Number as refractive index" begin + sphere(r) = SphericalSurface(r, BMO.inch) + @test BMO.refractive_index(SphericalLens(50mm, -50mm, 5mm)) isa ConstantRefractiveIndex + @test BMO.refractive_index(ThinLens(50mm, -50mm, BMO.inch, 1.5)) isa ConstantRefractiveIndex + @test BMO.refractive_index(Lens(sphere(50mm), 5mm, 1.5)) isa ConstantRefractiveIndex + dl = DoubletLens(sphere(50mm), sphere(-40mm), sphere(100mm), 6mm, 3mm, 1.5, λ -> 1.7) + @test BMO.refractive_index(dl.front) isa ConstantRefractiveIndex + tl = TripletLens(sphere(50mm), sphere(-40mm), sphere(100mm), sphere(-60mm), 8mm, 3mm, 6mm, + 1.5, 1.6, 1.5) + @test all(BMO.refractive_index(l) isa ConstantRefractiveIndex for l in BMO.shape(tl)) + # a number traces exactly like the equivalent closure, also on a moved lens + function trace_moved_lens(n) + lens = SphericalLens(50mm, -50mm, 5mm, BMO.inch, n) + zrotate3d!(lens, deg2rad(10)) + translate3d!(lens, [1mm, 20mm, -2mm]) + beam = Beam(Ray([0, -0.05, 5mm], [0, 1, 0], 1e-6)) + solve_system!(System(lens), beam) + return last(rays(beam)), length(rays(beam)) + end + r_number, num_rays = trace_moved_lens(1.5) + r_closure, _ = trace_moved_lens(λ -> 1.5) + @test num_rays == 3 + @test position(r_number) == position(r_closure) + @test direction(r_number) == direction(r_closure) + end + @testset "Testing lens constructor" begin # Test against Thorlab spherical lenses r1 = 34.9mm diff --git a/test/TestUtils.jl b/test/TestUtils.jl index cf62eeb46..657b82b2f 100644 --- a/test/TestUtils.jl +++ b/test/TestUtils.jl @@ -280,6 +280,14 @@ const BMO = BeamletOptics @test_throws ArgumentError DiscreteRefractiveIndex([1], [1, 2]) end + @testset "ConstantRefractiveIndex" begin + cri = ConstantRefractiveIndex(1.5) + @test cri isa BMO.RefractiveIndex + @test all(cri(λ) == 1.5 for λ in (1, 1f0, 532e-9)) + @test ConstantRefractiveIndex(1.5f0)(532e-9) isa Float32 + @test isnothing(BMO.test_refractive_index_function(cri)) + end + @testset "SellmeierEquation" begin # Known reference index for fused silica at 500 nm @test isapprox(ref_sellm(500e-9), 1.4623, atol=3e-5) From 5100dd42f2ae2f447d6038f256a8e4dd588fcb6c Mon Sep 17 00:00:00 2001 From: Hugo Uittenbosch Date: Mon, 2 Nov 2026 11:45:11 +0100 Subject: [PATCH 2/4] Address review round 1 of #213 - Catalog links point at the new constructor signatures (F1) - Tests for numbers in the cylindrical and flat-surface Lens methods (F2) and for the properties label of a number (F3) - Lens docstring signatures show an untyped n (F4) - Skill examples and templates use numbers for constant indices (F5) - Number paragraph follows the LB1811 figure (F6) Co-Authored-By: Claude Opus 5.5 --- docs/src/basics/components/lenses.md | 4 ++-- docs/src/components/catalog.json | 18 +++++++++--------- skills/beamletoptics/API.md | 2 +- skills/beamletoptics/components/lenses.md | 4 ++-- .../templates/02_gaussian_telescope.jl | 2 +- skills/beamletoptics/templates/03_psf_airy.jl | 2 +- .../templates/07_render_system.jl | 2 +- src/OpticalComponents/Lenses.jl | 4 ++-- test/Lenses/TestCylindricalLenses.jl | 13 +++++++++++++ test/Lenses/TestSphericalLenses.jl | 2 ++ test/TestProperties.jl | 3 +++ 11 files changed, 37 insertions(+), 19 deletions(-) diff --git a/docs/src/basics/components/lenses.md b/docs/src/basics/components/lenses.md index a5d1384c0..b405d2a30 100644 --- a/docs/src/basics/components/lenses.md +++ b/docs/src/basics/components/lenses.md @@ -76,10 +76,10 @@ LB1811 = Lens( ) ``` -Every constructor that takes a refractive index also accepts a plain number, e.g. `1.5`, for a material without dispersion. It is stored as a [`ConstantRefractiveIndex`](@ref). Use a [`DiscreteRefractiveIndex`](@ref) or a [`SellmeierEquation`](@ref) whenever the result depends on the wavelength. - ![Lens constructor](lens_constructor.png) +Every constructor that takes a refractive index also accepts a plain number, e.g. `1.5`, for a material without dispersion. It is stored as a [`ConstantRefractiveIndex`](@ref). Use a [`DiscreteRefractiveIndex`](@ref) or a [`SellmeierEquation`](@ref) whenever the result depends on the wavelength. + ### SDF-based spherical lenses In order to model the lens surfaces shown above, the following SDF-based spherical lens shapes have been implemented: diff --git a/docs/src/components/catalog.json b/docs/src/components/catalog.json index 9d9298885..e1a70e2a4 100644 --- a/docs/src/components/catalog.json +++ b/docs/src/components/catalog.json @@ -129,7 +129,7 @@ "name": "ThinLens", "category": "Lenses", "image": "catalog_ThinLens.png", - "href": "/reference#BeamletOptics.ThinLens-Tuple{Real,%20Real,%20Real,%20Union{Function,%20DiscreteRefractiveIndex,%20SellmeierEquation}}", + "href": "/reference#BeamletOptics.ThinLens-Tuple{Real,%20Real,%20Real,%20Any}", "teaser": "Idealized thin spherical lens." }, { @@ -171,21 +171,21 @@ "name": "RightAnglePrism", "category": "Prisms", "image": "catalog_RightAnglePrism.png", - "href": "/reference#BeamletOptics.RightAnglePrism-Tuple{Real,%20Real,%20Union{Function,%20DiscreteRefractiveIndex,%20SellmeierEquation}}", + "href": "/reference#BeamletOptics.RightAnglePrism-Tuple{Real,%20Real,%20Any}", "teaser": "Right-angle symmetric prism, e.g. for cube beamsplitters." }, { "name": "EquilateralPrism", "category": "Prisms", "image": "catalog_EquilateralPrism.png", - "href": "/reference#BeamletOptics.EquilateralPrism-Tuple{Real,%20Real,%20Union{Function,%20DiscreteRefractiveIndex,%20SellmeierEquation}}", + "href": "/reference#BeamletOptics.EquilateralPrism-Tuple{Real,%20Real,%20Any}", "teaser": "Equilateral triangular prism for dispersion." }, { "name": "DovePrism", "category": "Prisms", "image": "catalog_DovePrism.png", - "href": "/reference#BeamletOptics.DovePrism-Tuple{Real,%20Real,%20Real,%20Union{Function,%20DiscreteRefractiveIndex,%20SellmeierEquation}}", + "href": "/reference#BeamletOptics.DovePrism-Tuple{Real,%20Real,%20Real,%20Any}", "teaser": "Trapezoidal Dove prism with 45 degree end faces." }, { @@ -206,28 +206,28 @@ "name": "RectangularPlateBeamsplitter", "category": "Beamsplitters", "image": "catalog_RectangularPlateBeamsplitter.png", - "href": "/reference#BeamletOptics.RectangularPlateBeamsplitter-Tuple{Real,%20Real,%20Real,%20Union{Function,%20DiscreteRefractiveIndex,%20SellmeierEquation}}", + "href": "/reference#BeamletOptics.RectangularPlateBeamsplitter-Tuple{Real,%20Real,%20Real,%20Any}", "teaser": "Rectangular plate beamsplitter with a finite substrate." }, { "name": "RoundPlateBeamsplitter", "category": "Beamsplitters", "image": "catalog_RoundPlateBeamsplitter.png", - "href": "/reference#BeamletOptics.RoundPlateBeamsplitter-Tuple{Real,%20Real,%20Union{Function,%20DiscreteRefractiveIndex,%20SellmeierEquation}}", + "href": "/reference#BeamletOptics.RoundPlateBeamsplitter-Tuple{Real,%20Real,%20Any}", "teaser": "Round plate beamsplitter with a finite substrate." }, { "name": "CubeBeamsplitter", "category": "Beamsplitters", "image": "catalog_CubeBeamsplitter.png", - "href": "/reference#BeamletOptics.CubeBeamsplitter-Tuple{Real,%20Union{Function,%20DiscreteRefractiveIndex,%20SellmeierEquation}}", + "href": "/reference#BeamletOptics.CubeBeamsplitter-Tuple{Real,%20Any}", "teaser": "Two cemented right-angle prisms forming a cube splitter." }, { "name": "RectangularCompensatorPlate", "category": "Beamsplitters", "image": "catalog_RectangularCompensatorPlate.png", - "href": "/reference#BeamletOptics.RectangularCompensatorPlate-Union{Tuple{T},%20Tuple{H},%20Tuple{W},%20Tuple{W,%20H,%20T,%20Union{Function,%20DiscreteRefractiveIndex,%20SellmeierEquation}}}%20where%20{W<:Real,%20H<:Real,%20T<:Real}", + "href": "/reference#BeamletOptics.RectangularCompensatorPlate-Union{Tuple{T},%20Tuple{H},%20Tuple{W},%20Tuple{W,%20H,%20T,%20Any}}%20where%20{W<:Real,%20H<:Real,%20T<:Real}", "teaser": "Corrects the parallel beam offset of a plate beamsplitter." }, { @@ -262,7 +262,7 @@ "name": "RoundLinearPolarizer", "category": "Polarizers", "image": "catalog_RoundLinearPolarizer.png", - "href": "/reference#BeamletOptics.RoundLinearPolarizer-Tuple{Real,%20Real,%20Real,%20Union{Function,%20DiscreteRefractiveIndex,%20SellmeierEquation}}", + "href": "/reference#BeamletOptics.RoundLinearPolarizer-Tuple{Real,%20Real,%20Real,%20Any}", "teaser": "Builds a round film polarizer with glass substrates." }, { diff --git a/skills/beamletoptics/API.md b/skills/beamletoptics/API.md index 9e4446bc7..507f07ed7 100644 --- a/skills/beamletoptics/API.md +++ b/skills/beamletoptics/API.md @@ -7,7 +7,7 @@ using BeamletOptics const mm = 1e-3 # 1. build components (at the origin, aligned with +y) -lens = SphericalLens(50mm, -50mm, 5mm, 25.4mm, λ -> 1.5) +lens = SphericalLens(50mm, -50mm, 5mm, 25.4mm, 1.5) det = Detector(20mm) # 2. place them (relative moves, radians) diff --git a/skills/beamletoptics/components/lenses.md b/skills/beamletoptics/components/lenses.md index b06ef5043..7334be586 100644 --- a/skills/beamletoptics/components/lenses.md +++ b/skills/beamletoptics/components/lenses.md @@ -50,7 +50,7 @@ Cylindrical surfaces (curved in one direction, extruded over `height`): ```julia A = [0, A4 * 1e3^3, A6 * 1e3^5, A8 * 1e3^7, A10 * 1e3^9] -AL75150 = Lens(EvenAsphericalSurface(76.68mm, 75mm, -0.675, A), 15mm, λ -> 1.5006520430) +AL75150 = Lens(EvenAsphericalSurface(76.68mm, 75mm, -0.675, A), 15mm, 1.5006520430) ``` Aspheres are experimental. A meniscus whose center thickness does not exceed the sagitta of its convex @@ -72,7 +72,7 @@ surface. Cylindrical surfaces are not supported (`MethodError`). ```julia # doublet with an aspherical front surface, A as above dl = DoubletLens(EvenAsphericalSurface(50mm, 25.4mm, -0.8, A), SphericalSurface(-40mm, 25.4mm), - SphericalSurface(-200mm, 25.4mm), 8mm, 3mm, λ -> 1.6456, λ -> 1.7168) + SphericalSurface(-200mm, 25.4mm), 8mm, 3mm, 1.6456, 1.7168) ``` ## Multi-element lenses from parts diff --git a/skills/beamletoptics/templates/02_gaussian_telescope.jl b/skills/beamletoptics/templates/02_gaussian_telescope.jl index 7781e0459..e7313e64d 100644 --- a/skills/beamletoptics/templates/02_gaussian_telescope.jl +++ b/skills/beamletoptics/templates/02_gaussian_telescope.jl @@ -5,7 +5,7 @@ const mm = 1e-3 const nm = 1e-9 λ = 1064nm -n_glass = λ -> 1.5066 # constant-index callable (λ in meters) +n_glass = 1.5066 # constant index, no dispersion f1, f2 = 50mm, 150mm # thin symmetric biconvex lenses: R = 2(n-1)f diff --git a/skills/beamletoptics/templates/03_psf_airy.jl b/skills/beamletoptics/templates/03_psf_airy.jl index c2bbb0921..fe58ab3f4 100644 --- a/skills/beamletoptics/templates/03_psf_airy.jl +++ b/skills/beamletoptics/templates/03_psf_airy.jl @@ -5,7 +5,7 @@ const mm = 1e-3 const µm = 1e-6 λ = 1000e-9 -lens = SphericalLens(100mm, Inf, 1mm, 25.4mm, λ -> 1.5) +lens = SphericalLens(100mm, Inf, 1mm, 25.4mm, 1.5) det = Detector(10mm) translate3d!(det, [0, 200mm + 0.13mm, 0]) # near best focus diff --git a/skills/beamletoptics/templates/07_render_system.jl b/skills/beamletoptics/templates/07_render_system.jl index 47440dda9..137a52765 100644 --- a/skills/beamletoptics/templates/07_render_system.jl +++ b/skills/beamletoptics/templates/07_render_system.jl @@ -4,7 +4,7 @@ using GLMakie, BeamletOptics const mm = 1e-3 -lens = SphericalLens(50mm, -50mm, 6mm, 25.4mm, λ -> 1.5) +lens = SphericalLens(50mm, -50mm, 6mm, 25.4mm, 1.5) mirror = RoundPlanoMirror(25.4mm, 5mm) zrotate3d!(mirror, deg2rad(45)) # fold +y -> +x translate3d!(mirror, [0, 80mm, 0]) diff --git a/src/OpticalComponents/Lenses.jl b/src/OpticalComponents/Lenses.jl index c25679cb7..0613cf81e 100644 --- a/src/OpticalComponents/Lenses.jl +++ b/src/OpticalComponents/Lenses.jl @@ -172,7 +172,7 @@ _signed_edge_sag(s::SphericalSurface, sd) = sign(radius(s)) * edge_sag(s, sd) _signed_edge_sag(s::CylindricalSurface, sd) = sign(radius(s)) * edge_sag(s, sd) """ - Lens(front_surface::AbstractRotationallySymmetricSurface, back_surface::AbstractRotationallySymmetricSurface, center_thickness::Real, n::RefractiveIndex) + Lens(front_surface::AbstractRotationallySymmetricSurface, back_surface::AbstractRotationallySymmetricSurface, center_thickness::Real, n) Constructs a new [`Lens`](@ref) object using the surface specifications `front_surface` and `back_surface` and the `center_thickness`. These inputs are used to construct a [`UnionSDF`](@ref) @@ -322,7 +322,7 @@ function Lens(front_surface::CircularFlatSurface, back_surface::CircularFlatSurf end """ - Lens(front_surface::AbstractCylindricalSurface, back_surface::AbstractCylindricalSurface, center_thickness::Real, n::RefractiveIndex) + Lens(front_surface::AbstractCylindricalSurface, back_surface::AbstractCylindricalSurface, center_thickness::Real, n) Constructs a new [`Lens`](@ref) object using the cylindric surface specifications `front_surface` and `back_surface` and the `center_thickness`. These inputs are used to construct a [`UnionSDF`](@ref) diff --git a/test/Lenses/TestCylindricalLenses.jl b/test/Lenses/TestCylindricalLenses.jl index 3a30e6b90..b47347f64 100644 --- a/test/Lenses/TestCylindricalLenses.jl +++ b/test/Lenses/TestCylindricalLenses.jl @@ -100,6 +100,19 @@ const mm = 1e-3 # test lens thickness @test thickness(lens) ≈ ct end + + @testset "Number as refractive index" begin + cyl = CylindricalSurface(5.2mm, 10mm, 20mm) + for lens in ( + Lens(CylindricalSurface(50mm, 10mm, 20mm), CylindricalSurface(-50mm, 10mm, 20mm), + 2mm, 1.517), + Lens(cyl, 5.9mm, 1.517), + Lens(RectangularFlatSurface(10mm), RectangularFlatSurface(10mm), 2mm, 1.517)) + @test BMO.refractive_index(lens) == ConstantRefractiveIndex(1.517) + end + # same back focal length as the closure above + @test working_distance(Lens(cyl, 5.9mm, 1.517), 0.05 * 10mm / 2) ≈ 6.1mm atol=1e-4 + end end end # MODULE \ No newline at end of file diff --git a/test/Lenses/TestSphericalLenses.jl b/test/Lenses/TestSphericalLenses.jl index 52b3220c6..2863ac080 100644 --- a/test/Lenses/TestSphericalLenses.jl +++ b/test/Lenses/TestSphericalLenses.jl @@ -81,6 +81,8 @@ const mm = 1e-3 @test BMO.refractive_index(SphericalLens(50mm, -50mm, 5mm)) isa ConstantRefractiveIndex @test BMO.refractive_index(ThinLens(50mm, -50mm, BMO.inch, 1.5)) isa ConstantRefractiveIndex @test BMO.refractive_index(Lens(sphere(50mm), 5mm, 1.5)) isa ConstantRefractiveIndex + flat = CircularFlatSurface(BMO.inch) + @test BMO.refractive_index(Lens(flat, flat, 5mm, 1.5)) isa ConstantRefractiveIndex dl = DoubletLens(sphere(50mm), sphere(-40mm), sphere(100mm), 6mm, 3mm, 1.5, λ -> 1.7) @test BMO.refractive_index(dl.front) isa ConstantRefractiveIndex tl = TripletLens(sphere(50mm), sphere(-40mm), sphere(100mm), sphere(-60mm), 8mm, 3mm, 6mm, diff --git a/test/TestProperties.jl b/test/TestProperties.jl index 19ca66638..137f4022d 100644 --- a/test/TestProperties.jl +++ b/test/TestProperties.jl @@ -70,6 +70,9 @@ prop(props, name) = (i = findfirst(p -> p.first == name, props); isnothing(i) ? @test prop(props, "n(λ₀)") ≈ 1.5 @test prop(props, "λ₀ [m]") == get_default_wavelength() @test prop(props, "Index model") == "function of λ" + props = BMO.properties(SphericalLens(50e-3, -50e-3, 5e-3, 25.4e-3, 1.5)) + @test prop(props, "Index model") == "ConstantRefractiveIndex" + @test prop(props, "n(λ₀)") == 1.5 glass = DiscreteRefractiveIndex([1e-6, 2e-6], [1.5, 1.4]) @test prop(BMO.properties(SphericalLens(50e-3, -50e-3, 5e-3, 25.4e-3, glass)), "Index model") == "DiscreteRefractiveIndex" From e434f0e1da177a15086e92772bf659c649c1581d Mon Sep 17 00:00:00 2001 From: uitt_hu Date: Mon, 2 Nov 2026 22:38:38 +0100 Subject: [PATCH 3/4] AbstractRefractiveIndex replaces the RefractiveIndex union (#213) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Refractive indices are subtypes of AbstractRefractiveIndex, whose docstring is the contract n(λ) -> Real; ConstantRefractiveIndex, DiscreteRefractiveIndex and SellmeierEquation subtype it. Constructors take n::AbstractRefractiveIndex: numbers and functions throw a MethodError, a constant index is ConstantRefractiveIndex(c). The RefractiveIndex union, the number methods of Lens/Prism and test_refractive_index_function are removed; a testset checks the contract for every subtype in the package. Breaking: λ -> 1.5 and 1.5 as n no longer work. Co-Authored-By: Claude Opus 5.5 --- docs/src/assets/beam_renders/agb_showcase.jl | 6 +- .../beam_renders/beam_catalog_showcase.jl | 2 +- docs/src/assets/beam_renders/beam_showcase.jl | 2 +- docs/src/assets/beam_renders/gb_showcase.jl | 2 +- docs/src/assets/bs_assets/cbs_showcase.jl | 4 +- docs/src/assets/bs_assets/pbs_showcase.jl | 4 +- .../assets/catalog_assets/catalog_showcase.jl | 6 +- .../detector_assets/spotdetector_showcase.jl | 6 +- docs/src/assets/examples/double_gauss.jl | 14 +- docs/src/assets/examples/lens_groups.jl | 32 +- .../assets/examples/psfdetector_showcase.jl | 4 +- .../lens_assets/cylindrical_lens_showcase.jl | 4 +- .../lens_assets/lens_constructor_showcase.jl | 2 +- .../assets/raman_assets/OpenRaman/longpass.jl | 6 +- docs/src/basics/autodiff.md | 4 +- docs/src/basics/components/beamsplitters.md | 8 +- docs/src/basics/components/lenses.md | 10 +- docs/src/basics/components/prisms.md | 8 +- docs/src/basics/rays.md | 2 +- docs/src/components/catalog.json | 18 +- docs/src/examples/aspherical_lenses.md | 12 +- docs/src/examples/double_gauss.md | 14 +- docs/src/examples/lens_groups.md | 32 +- docs/src/examples/psf.md | 4 +- docs/src/index.md | 5 +- docs/src/tutorials/openraman.md | 2 +- skills/beamletoptics/API.md | 2 +- skills/beamletoptics/CHECKLIST.md | 2 +- skills/beamletoptics/SKILL.md | 6 +- .../beamletoptics/components/beamsplitters.md | 2 +- skills/beamletoptics/components/lenses.md | 8 +- skills/beamletoptics/components/materials.md | 14 +- .../components/prisms-and-dummies.md | 2 +- .../templates/02_gaussian_telescope.jl | 2 +- skills/beamletoptics/templates/03_psf_airy.jl | 2 +- .../templates/07_render_system.jl | 2 +- src/AbstractTypes/AbstractSystem.jl | 4 +- src/Exports.jl | 1 + .../Beamsplitters/Compensators.jl | 4 +- .../Beamsplitters/CubeBeamsplitter.jl | 4 +- .../Beamsplitters/PlateBeamsplitter.jl | 8 +- src/OpticalComponents/DoubletLenses.jl | 14 +- src/OpticalComponents/Lenses.jl | 43 +- .../Polarizers/LinearPolarizer.jl | 8 +- src/OpticalComponents/Prisms.jl | 25 +- src/OpticalComponents/SphericalLenses.jl | 12 +- src/OpticalComponents/TripletLenses.jl | 20 +- src/Properties.jl | 10 +- src/Utils/RefractiveIndexUtils.jl | 102 ++--- src/Workloads/asphere_wl.jl | 2 +- test/Components/TestBeamsplitters.jl | 407 +++++++++--------- test/Components/TestDetector.jl | 10 +- test/Components/TestPolarizers.jl | 12 +- test/Components/TestPrisms.jl | 18 +- test/E2E/TestAnalyticReferences.jl | 28 +- test/E2E/TestDoubleGaussLens.jl | 8 +- test/E2E/TestSonnarLens.jl | 6 +- test/Geometry/SDFs/TestAbstractSDF.jl | 2 +- test/Geometry/SDFs/TestSDFBoundingSpheres.jl | 8 +- test/Geometry/TestBoundingSphere.jl | 6 +- test/Lenses/TestAsphericalLenses.jl | 14 +- test/Lenses/TestCylindricalLenses.jl | 25 +- test/Lenses/TestSphericalLenses.jl | 69 ++- test/Lenses/TestSurfaces.jl | 22 +- test/Rendering/TestLiveBeams.jl | 4 +- test/Rendering/TestRenderBoundingSphere.jl | 16 +- test/Rendering/TestRenderLive.jl | 4 +- test/Rendering/TestRenderLook.jl | 24 +- test/Rendering/TestRenderPolarization.jl | 2 +- test/Rendering/TestTessellation.jl | 20 +- test/Rendering/TestWavelengthColor.jl | 6 +- test/TestAstigmaticGaussianBeamlet.jl | 18 +- test/TestAstigmaticGaussianPhysical.jl | 10 +- test/TestAutoDiff.jl | 20 +- test/TestBeamGroups.jl | 4 +- test/TestBeamletFieldInMedia.jl | 4 +- test/TestBugFixes.jl | 34 +- test/TestGaussianBeamlet.jl | 8 +- test/TestKinematicTrait.jl | 6 +- test/TestObjectGroups.jl | 4 +- test/TestOpticsBaseExt.jl | 2 +- test/TestPathSegments.jl | 2 +- test/TestPolarizedRays.jl | 14 +- test/TestProgress.jl | 2 +- test/TestProperties.jl | 25 +- test/TestSharedParts.jl | 8 +- test/TestSourceKinematics.jl | 6 +- test/TestUtils.jl | 37 +- 88 files changed, 698 insertions(+), 729 deletions(-) diff --git a/docs/src/assets/beam_renders/agb_showcase.jl b/docs/src/assets/beam_renders/agb_showcase.jl index fd1c7859e..03abae156 100644 --- a/docs/src/assets/beam_renders/agb_showcase.jl +++ b/docs/src/assets/beam_renders/agb_showcase.jl @@ -16,7 +16,7 @@ c_view = [ agb = AstigmaticGaussianBeamlet([0,0,0], [0,1,0], 9e-4, 2e-3; support=[0,0,1]) -l = ThinLens(20e-3, 20e-3, BMO.inch, 1.5) +l = ThinLens(20e-3, 20e-3, BMO.inch, ConstantRefractiveIndex(1.5)) s = System([l]) translate3d!(l, [0, 30e-3, 0]) @@ -56,8 +56,8 @@ save("agbtest2.png", fig; px_per_unit=8, update = false) ## s1 = BMO.CylinderSDF(BMO.inch/2, BMO.inch) s2 = BMO.CylinderSDF(BMO.inch/2, BMO.inch) -l1 = BMO.Lens(s1, n->1.5) -l2 = BMO.Lens(s2, n->1.5) +l1 = BMO.Lens(s1, ConstantRefractiveIndex(1.5)) +l2 = BMO.Lens(s2, ConstantRefractiveIndex(1.5)) translate3d!(l1, [0, 50mm, 0]) translate3d!(l2, [0, 100mm, 0]) xrotate3d!(l1, deg2rad(90)) diff --git a/docs/src/assets/beam_renders/beam_catalog_showcase.jl b/docs/src/assets/beam_renders/beam_catalog_showcase.jl index 213cbf7e1..1d04a3157 100644 --- a/docs/src/assets/beam_renders/beam_catalog_showcase.jl +++ b/docs/src/assets/beam_renders/beam_catalog_showcase.jl @@ -14,7 +14,7 @@ const λ = 1e-6 "LB1811-like bi-convex lens and a detector near its focus." function test_system() - lens = SphericalLens(34.9mm, -34.9mm, 6.8mm, inch, λ -> 1.5168) + lens = SphericalLens(34.9mm, -34.9mm, 6.8mm, inch, ConstantRefractiveIndex(1.5168)) translate3d!(lens, [0, 50mm, 0]) detector = Detector(inch) translate3d!(detector, [0, 85mm, 0]) diff --git a/docs/src/assets/beam_renders/beam_showcase.jl b/docs/src/assets/beam_renders/beam_showcase.jl index 9803d4468..85196bc41 100644 --- a/docs/src/assets/beam_renders/beam_showcase.jl +++ b/docs/src/assets/beam_renders/beam_showcase.jl @@ -13,7 +13,7 @@ m1 = RoundPlanoMirror(20mm, 5mm) m2 = RoundPlanoMirror(20mm, 5mm) m3 = RoundPlanoMirror(20mm, 5mm) -prism = RightAnglePrism(20mm, 20mm, n->1.2) +prism = RightAnglePrism(20mm, 20mm, ConstantRefractiveIndex(1.2)) translate_to3d!(m1, [0, 100mm, 0]) zrotate3d!(m1, deg2rad(-30)) diff --git a/docs/src/assets/beam_renders/gb_showcase.jl b/docs/src/assets/beam_renders/gb_showcase.jl index 97e229c12..d87aeb5d8 100644 --- a/docs/src/assets/beam_renders/gb_showcase.jl +++ b/docs/src/assets/beam_renders/gb_showcase.jl @@ -14,7 +14,7 @@ c_view = [ gb = GaussianBeamlet([0,0,0], [0,1,0], 9e-4, 2e-3; support=[0,0,1]) -l = ThinLens(20e-3, 20e-3, BMO.inch, 1.5) +l = ThinLens(20e-3, 20e-3, BMO.inch, ConstantRefractiveIndex(1.5)) s = System([l]) translate3d!(l, [0, 30e-3, 0]) diff --git a/docs/src/assets/bs_assets/cbs_showcase.jl b/docs/src/assets/bs_assets/cbs_showcase.jl index e5286db4e..19dadcb6d 100644 --- a/docs/src/assets/bs_assets/cbs_showcase.jl +++ b/docs/src/assets/bs_assets/cbs_showcase.jl @@ -8,10 +8,10 @@ const mm = 1e-3 ## n = 1.5 -cbs1 = CubeBeamsplitter(BeamletOptics.inch, _->n) +cbs1 = CubeBeamsplitter(BeamletOptics.inch, ConstantRefractiveIndex(n)) cbs1_mount = NonInteractableObject(joinpath(@__DIR__, "CBS Mount.stl")) -cbs2 = CubeBeamsplitter(BeamletOptics.inch, _->n) +cbs2 = CubeBeamsplitter(BeamletOptics.inch, ConstantRefractiveIndex(n)) cbs2_mount = NonInteractableObject(joinpath(@__DIR__, "CBS Mount.stl")) zrotate3d!(cbs2_mount, π) diff --git a/docs/src/assets/bs_assets/pbs_showcase.jl b/docs/src/assets/bs_assets/pbs_showcase.jl index 9391c5654..2cd70fa38 100644 --- a/docs/src/assets/bs_assets/pbs_showcase.jl +++ b/docs/src/assets/bs_assets/pbs_showcase.jl @@ -7,12 +7,12 @@ const mm = 1e-3 ## n = 1.5 -cbs = RectangularPlateBeamsplitter(36mm, 25mm, 5mm, _->n) +cbs = RectangularPlateBeamsplitter(36mm, 25mm, 5mm, ConstantRefractiveIndex(n)) cbs_mount = NonInteractableObject(joinpath(@__DIR__, "PBS Mount.stl")) cbs_assembly = ObjectGroup([cbs, cbs_mount]) -cmp = RectangularCompensatorPlate(36mm, 25mm, 5mm, _->n) +cmp = RectangularCompensatorPlate(36mm, 25mm, 5mm, ConstantRefractiveIndex(n)) cmp_mount = NonInteractableObject(joinpath(@__DIR__, "PBS Mount.stl")) cmp_assembly = ObjectGroup([cmp, cmp_mount]) diff --git a/docs/src/assets/catalog_assets/catalog_showcase.jl b/docs/src/assets/catalog_assets/catalog_showcase.jl index f911d4800..bac36d78a 100644 --- a/docs/src/assets/catalog_assets/catalog_showcase.jl +++ b/docs/src/assets/catalog_assets/catalog_showcase.jl @@ -11,8 +11,8 @@ const mm = 1e-3 const inch = BMO.inch # refractive indices, the exact values do not matter for a catalog tile -const n_crown = λ -> 1.5168 -const n_flint = λ -> 1.7 +const n_crown = ConstantRefractiveIndex(1.5168) +const n_flint = ConstantRefractiveIndex(1.7) "Two cemented spherical lenses (AC254-100-like), assembled via the `DoubletLens` type directly." function manual_doublet() @@ -60,7 +60,7 @@ const TILES = [ # lenses "Lens" => object_tile(() -> Lens(SphericalSurface(25.8mm, inch), CircularFlatSurface(inch), 5.3mm, n_crown)), "SphericalLens" => object_tile(() -> SphericalLens(34.9mm, -34.9mm, 6.8mm, inch, n_crown)), - "ThinLens" => object_tile(() -> ThinLens(34.9mm, -34.9mm, inch, 1.5)), + "ThinLens" => object_tile(() -> ThinLens(34.9mm, -34.9mm, inch, ConstantRefractiveIndex(1.5))), "DoubletLens" => object_tile(manual_doublet), "SphericalDoubletLens" => object_tile(() -> SphericalDoubletLens(87.9mm, -105.6mm, -1000, 6mm, 3mm, inch, n_crown, n_flint)), "TripletLens" => object_tile(manual_triplet), diff --git a/docs/src/assets/detector_assets/spotdetector_showcase.jl b/docs/src/assets/detector_assets/spotdetector_showcase.jl index 150a5a013..50de58bd1 100644 --- a/docs/src/assets/detector_assets/spotdetector_showcase.jl +++ b/docs/src/assets/detector_assets/spotdetector_showcase.jl @@ -7,14 +7,14 @@ const mm = 1e-3 ## # define spherical lenses -l1 = SphericalLens(69.21e-3, 433.84e-3, 9.33e-3, 70e-3, λ -> 1.671) +l1 = SphericalLens(69.21e-3, 433.84e-3, 9.33e-3, 70e-3, ConstantRefractiveIndex(1.671)) # front triplet: last surface only 40 mm clear aperture -> constructed from surfaces l234 = TripletLens( SphericalSurface(35.86e-3, 60e-3), SphericalSurface(85.87e-3, 60e-3), SphericalSurface(-646.31e-3, 60e-3), SphericalSurface(23.51e-3, 40e-3), - 11.81e-3, 7.05e-3, 1.9e-3, λ -> 1.671, λ -> 1.4892, λ -> 1.7394) + 11.81e-3, 7.05e-3, 1.9e-3, ConstantRefractiveIndex(1.671), ConstantRefractiveIndex(1.4892), ConstantRefractiveIndex(1.7394)) l567 = SphericalTripletLens(Inf, 51.09e-3, -22.12e-3, -103.13e-3, 2.48e-3, 19.81e-3, 4.57e-3, 42e-3, - λ -> 1.5232, λ -> 1.6578, λ -> 1.5894) + ConstantRefractiveIndex(1.5232), ConstantRefractiveIndex(1.6578), ConstantRefractiveIndex(1.5894)) # Calculate translation distances l_234 = thickness(l1) + 0.38e-3 diff --git a/docs/src/assets/examples/double_gauss.jl b/docs/src/assets/examples/double_gauss.jl index 0aef2b0c3..c259f951c 100644 --- a/docs/src/assets/examples/double_gauss.jl +++ b/docs/src/assets/examples/double_gauss.jl @@ -7,10 +7,10 @@ const mm = 1e-3 ## # define spherical lenses -l1 = SphericalLens(48.88e-3, 182.96e-3, 8.89e-3, 52.3e-3, λ -> 1.62286) -l23 = SphericalDoubletLens(36.92e-3, Inf, 23.06e-3, 15.11e-3, 2.31e-3, 45.11e-3, λ -> 1.58565, λ -> 1.67764) -l45 = SphericalDoubletLens(-23.91e-3, Inf, -36.92e-3, 1.92e-3, 7.77e-3, 40.01e-3, λ -> 1.57046, λ -> 1.64128) -l6 = SphericalLens(1063.24e-3, -48.88e-3, 6.73e-3, 45.11e-3, λ -> 1.62286) +l1 = SphericalLens(48.88e-3, 182.96e-3, 8.89e-3, 52.3e-3, ConstantRefractiveIndex(1.62286)) +l23 = SphericalDoubletLens(36.92e-3, Inf, 23.06e-3, 15.11e-3, 2.31e-3, 45.11e-3, ConstantRefractiveIndex(1.58565), ConstantRefractiveIndex(1.67764)) +l45 = SphericalDoubletLens(-23.91e-3, Inf, -36.92e-3, 1.92e-3, 7.77e-3, 40.01e-3, ConstantRefractiveIndex(1.57046), ConstantRefractiveIndex(1.64128)) +l6 = SphericalLens(1063.24e-3, -48.88e-3, 6.73e-3, 45.11e-3, ConstantRefractiveIndex(1.62286)) # Calculate translation distances l_23 = thickness(l1) + 0.38e-3 @@ -51,14 +51,14 @@ set_view(ax, cview) save("double_gauss.png", fig; px_per_unit=8, update = false) ## Sonnar comparison -s1 = SphericalLens(69.21e-3, 433.84e-3, 9.33e-3, 70e-3, λ -> 1.671) +s1 = SphericalLens(69.21e-3, 433.84e-3, 9.33e-3, 70e-3, ConstantRefractiveIndex(1.671)) # front triplet: last surface only has a clear aperture of 40 mm -> constructed from surfaces s234 = TripletLens( SphericalSurface(35.86e-3, 60e-3), SphericalSurface(85.87e-3, 60e-3), SphericalSurface(-646.31e-3, 60e-3), SphericalSurface(23.51e-3, 40e-3), - 11.81e-3, 7.05e-3, 1.9e-3, λ -> 1.671, λ -> 1.4892, λ -> 1.7394) + 11.81e-3, 7.05e-3, 1.9e-3, ConstantRefractiveIndex(1.671), ConstantRefractiveIndex(1.4892), ConstantRefractiveIndex(1.7394)) s567 = SphericalTripletLens(Inf, 51.09e-3, -22.12e-3, -103.13e-3, 2.48e-3, 19.81e-3, 4.57e-3, 42e-3, - λ -> 1.5232, λ -> 1.6578, λ -> 1.5894) + ConstantRefractiveIndex(1.5232), ConstantRefractiveIndex(1.6578), ConstantRefractiveIndex(1.5894)) # Calculate translation distances s_234 = thickness(s1) + 0.38e-3 diff --git a/docs/src/assets/examples/lens_groups.jl b/docs/src/assets/examples/lens_groups.jl index b6581c285..7225c2808 100644 --- a/docs/src/assets/examples/lens_groups.jl +++ b/docs/src/assets/examples/lens_groups.jl @@ -7,8 +7,8 @@ const mm = 1e-3 const cm = 10mm ## focus group -l1 = SphericalDoubletLens(103.4371e-3, 61.14925e-3, -603.2959e-3, 1.5e-3, 10.03975e-3, 55e-3, 1.963000, 1.603112) -l2 = SphericalLens(49.97282e-3, 168.2416e-3, 8.622723e-3, 52e-3, 1.603001) +l1 = SphericalDoubletLens(103.4371e-3, 61.14925e-3, -603.2959e-3, 1.5e-3, 10.03975e-3, 55e-3, ConstantRefractiveIndex(1.963000), ConstantRefractiveIndex(1.603112)) +l2 = SphericalLens(49.97282e-3, 168.2416e-3, 8.622723e-3, 52e-3, ConstantRefractiveIndex(1.603001)) l_2 = BeamletOptics.thickness(l1) + 1e-4 translate3d!(l2, [0, l_2, 0]) @@ -16,9 +16,9 @@ translate3d!(l2, [0, l_2, 0]) focus_group = ObjectGroup([l1, l2]) ## variator -l3 = SphericalLens(100.3834e-3, 16.72327e-3, 1e-3, 28.00995e-3, λ -> 1.603112) -l4 = SphericalLens(-30.28003e-3, 21.29033e-3, 0.5e-3, 22.31297e-3, λ -> 1.764500) -l5 = SphericalLens(23.06904e-3, 243.5999e-3, 3.2e-3, 22.12162e-3, λ -> 1.963000) +l3 = SphericalLens(100.3834e-3, 16.72327e-3, 1e-3, 28.00995e-3, ConstantRefractiveIndex(1.603112)) +l4 = SphericalLens(-30.28003e-3, 21.29033e-3, 0.5e-3, 22.31297e-3, ConstantRefractiveIndex(1.764500)) +l5 = SphericalLens(23.06904e-3, 243.5999e-3, 3.2e-3, 22.12162e-3, ConstantRefractiveIndex(1.963000)) l_3 = l_2 + BeamletOptics.thickness(l2) + 1.1e-3 l_4 = l_3 + BeamletOptics.thickness(l3) + 7.25496e-3 @@ -30,19 +30,19 @@ translate3d!(l5, [0, l_5, 0]) variator_group = ObjectGroup([l3, l4, l5]) ## compensator -l6 = SphericalLens(-24.31747e-3, -255.5571e-3, 1e-3, 17.70449e-3, λ -> 1.638539) +l6 = SphericalLens(-24.31747e-3, -255.5571e-3, 1e-3, 17.70449e-3, ConstantRefractiveIndex(1.638539)) compensator = l6 l_6 = l_5 + BeamletOptics.thickness(l5) + 40.889e-3 translate3d!(l6, [0, l_6, 0]) # master I -l7 = SphericalLens(Inf, Inf, 1.2e-3, 19.896e-3, λ -> 1.516330) -l8 = SphericalLens(1207.65e-3, -28.39078e-3, 2.4e-3, 21.05932e-3, λ -> 1.583126) -l9 = SphericalLens(74.34233e-3, -62.52319e-3, 2.4e-3, 21.61478e-3, λ -> 1.570989) -l10 = SphericalLens(42.10459e-3, -128.0055e-3, 2.4e-3, 20.88128e-3, λ -> 1.583126) -l11 = SphericalLens(28.62223e-3, 378.3789e-3, 2.4e-3, 19.26452e-3, λ -> 1.651000) -l12 = SphericalLens(-40.92189e-3, 41.10496e-3, 1e-3, 18.17719e-3, λ -> 1.854779) +l7 = SphericalLens(Inf, Inf, 1.2e-3, 19.896e-3, ConstantRefractiveIndex(1.516330)) +l8 = SphericalLens(1207.65e-3, -28.39078e-3, 2.4e-3, 21.05932e-3, ConstantRefractiveIndex(1.583126)) +l9 = SphericalLens(74.34233e-3, -62.52319e-3, 2.4e-3, 21.61478e-3, ConstantRefractiveIndex(1.570989)) +l10 = SphericalLens(42.10459e-3, -128.0055e-3, 2.4e-3, 20.88128e-3, ConstantRefractiveIndex(1.583126)) +l11 = SphericalLens(28.62223e-3, 378.3789e-3, 2.4e-3, 19.26452e-3, ConstantRefractiveIndex(1.651000)) +l12 = SphericalLens(-40.92189e-3, 41.10496e-3, 1e-3, 18.17719e-3, ConstantRefractiveIndex(1.854779)) l_7 = l_6 + BeamletOptics.thickness(l6) + 4.765141e-3 l_8 = l_7 + BeamletOptics.thickness(l7) + 1e-3 @@ -60,10 +60,10 @@ translate3d!(l12, [0, l_12, 0]) master_group_I = ObjectGroup([l7, l8, l9, l10, l11, l12]) ## master II -l13 = SphericalLens(34.65626e-3, -45.71147e-3, 2.4e-3, 15.6e-3, λ -> 1.762001) -l14 = SphericalLens(27.08863e-3, 11.38107e-3, 1e-3, 14.40932e-3, λ -> 1.761821) -l15 = SphericalLens(20.49712e-3, -203.9304e-3, 2.4e-3, 14.40932e-3, λ -> 1.693501) -l16 = SphericalLens(Inf, Inf, 4e-3, 12.92286e-3, λ -> 1.522494) +l13 = SphericalLens(34.65626e-3, -45.71147e-3, 2.4e-3, 15.6e-3, ConstantRefractiveIndex(1.762001)) +l14 = SphericalLens(27.08863e-3, 11.38107e-3, 1e-3, 14.40932e-3, ConstantRefractiveIndex(1.761821)) +l15 = SphericalLens(20.49712e-3, -203.9304e-3, 2.4e-3, 14.40932e-3, ConstantRefractiveIndex(1.693501)) +l16 = SphericalLens(Inf, Inf, 4e-3, 12.92286e-3, ConstantRefractiveIndex(1.522494)) l_13 = l_12 + BeamletOptics.thickness(l12) + 11.97864e-3 l_14 = l_13 + BeamletOptics.thickness(l13) + 0.1e-3 diff --git a/docs/src/assets/examples/psfdetector_showcase.jl b/docs/src/assets/examples/psfdetector_showcase.jl index b1bc8a7d2..cc858fdaa 100644 --- a/docs/src/assets/examples/psfdetector_showcase.jl +++ b/docs/src/assets/examples/psfdetector_showcase.jl @@ -13,7 +13,7 @@ n = 1.5 λ = 1e-6 cs = UniformDiscSource([0, -10mm, 0], [0, 1, 0], 15e-3, λ) -lens = SphericalLens(R1, R2, l, d, x -> n) +lens = SphericalLens(R1, R2, l, d, ConstantRefractiveIndex(n)) psfd = Detector(10e-3) translate3d!(psfd, [0, 200mm + 0.13mm, 0]) @@ -41,7 +41,7 @@ A = [0*(1e3)^1, 2.7709219e-8*(1e3)^3, 6.418186e-13*(1e3)^5, -1.5724014e-17*(1e3) AL75150 = Lens( EvenAsphericalSurface(radius, d, k, A), l, - n -> 1.5006520430 + ConstantRefractiveIndex(1.5006520430) ) xrotate3d!(AL75150, deg2rad(-0.5)) diff --git a/docs/src/assets/lens_assets/cylindrical_lens_showcase.jl b/docs/src/assets/lens_assets/cylindrical_lens_showcase.jl index 9597aad11..25b8e30b6 100644 --- a/docs/src/assets/lens_assets/cylindrical_lens_showcase.jl +++ b/docs/src/assets/lens_assets/cylindrical_lens_showcase.jl @@ -8,7 +8,7 @@ ct = 5.9e-3 # center thickness cylindrical_lens = Lens( CylindricalSurface(r, d, h), ct, - n -> 1.517 + ConstantRefractiveIndex(1.517) ) fig_cylindrical = Figure(size=(600, 450)) @@ -34,7 +34,7 @@ acylindrical_lens = Lens( [0, 1.1926075e-5*(1e3)^3, -2.9323497e-9*(1e3)^5, -1.8718889e-11*(1e3)^7, -1.7009961e-14*(1e3)^9, 3.5481542e-17*(1e3)^11, 6.5241296e-20*(1e3)^13] ), 7.5e-3, - n -> 1.777 + ConstantRefractiveIndex(1.777) ) fig_acylindrical = Figure(size=(600, 450)) diff --git a/docs/src/assets/lens_assets/lens_constructor_showcase.jl b/docs/src/assets/lens_assets/lens_constructor_showcase.jl index d0773f912..cd47033b9 100644 --- a/docs/src/assets/lens_assets/lens_constructor_showcase.jl +++ b/docs/src/assets/lens_assets/lens_constructor_showcase.jl @@ -14,7 +14,7 @@ LB1811 = Lens( SphericalSurface(r1, inch), SphericalSurface(r2, inch), l, - n -> 1.5 + ConstantRefractiveIndex(1.5) ) translate3d!(LB1811, [0, 10mm, 0]) diff --git a/docs/src/assets/raman_assets/OpenRaman/longpass.jl b/docs/src/assets/raman_assets/OpenRaman/longpass.jl index a52ab8e60..efb714a51 100644 --- a/docs/src/assets/raman_assets/OpenRaman/longpass.jl +++ b/docs/src/assets/raman_assets/OpenRaman/longpass.jl @@ -8,10 +8,10 @@ refractive index `n`, i.e. the beam is refracted at both surfaces. # Fields - `shape`: a `PlanoSurfaceSDF` representing the substrate -- `n`: the substrate [`BeamletOptics.RefractiveIndex`](@ref) +- `n`: the substrate [`BeamletOptics.AbstractRefractiveIndex`](@ref) - `cuton`: the cut-on wavelength in [m] """ -struct LongpassDichroicMirror{T, N <: BMO.RefractiveIndex} <: BMO.AbstractObject{T} +struct LongpassDichroicMirror{T, N <: BMO.AbstractRefractiveIndex} <: BMO.AbstractObject{T} shape::BMO.PlanoSurfaceSDF{T} n::N cuton::T @@ -26,7 +26,7 @@ BMO.refractive_index(lpm::LongpassDichroicMirror, λ::Real)::Float64 = lpm.n(λ) Spawns a round [`LongpassDichroicMirror`](@ref) at the global origin. The reflective face is normal to the local y-axis. """ -function LongpassDichroicMirror(diameter::D, thickness::T, n::BMO.RefractiveIndex, cuton::Real) where {D, T} +function LongpassDichroicMirror(diameter::D, thickness::T, n::BMO.AbstractRefractiveIndex, cuton::Real) where {D, T} TT = promote_type(D, T) shape = BMO.PlanoSurfaceSDF(TT(thickness), TT(diameter)) return LongpassDichroicMirror(shape, n, TT(cuton)) diff --git a/docs/src/basics/autodiff.md b/docs/src/basics/autodiff.md index 5b1f88258..71146f1d7 100644 --- a/docs/src/basics/autodiff.md +++ b/docs/src/basics/autodiff.md @@ -18,7 +18,7 @@ using BeamletOptics, ForwardDiff # x coordinate of the spot behind a lens that is shifted laterally by `dx` function spot_x(dx) T = typeof(dx) - lens = SphericalLens(T(50e-3), T(-50e-3), T(5e-3), T(25e-3), _ -> T(1.5)) + lens = SphericalLens(T(50e-3), T(-50e-3), T(5e-3), T(25e-3), ConstantRefractiveIndex(T(1.5))) translate3d!(lens, [dx, zero(T), zero(T)]) pd = Detector(T(40e-3)) translate3d!(pd, T[0, 0.1, 0]) @@ -31,7 +31,7 @@ ForwardDiff.derivative(spot_x, 0.0) # ≈ 1.90 ``` The parameter can enter anywhere: the pose of a component (`translate3d!`, `rotate3d!`), its -geometry (radii, thicknesses), a refractive index (e.g. `_ -> n0` for the parameter `n0`) or a +geometry (radii, thicknesses), a refractive index (e.g. `ConstantRefractiveIndex(n0)` for the parameter `n0`) or a source. Many parameters at once are differentiated with `ForwardDiff.gradient` or `ForwardDiff.jacobian`. diff --git a/docs/src/basics/components/beamsplitters.md b/docs/src/basics/components/beamsplitters.md index 023366584..991d198bb 100644 --- a/docs/src/basics/components/beamsplitters.md +++ b/docs/src/basics/components/beamsplitters.md @@ -28,14 +28,14 @@ This model acts as a quasi-coating, representing a very thin (zero-thickness) la ## Plate beamsplitters -Plate beamsplitters provide a more sophisticated model by incorporating a substrate with finite thickness and [`BeamletOptics.RefractiveIndex`](@ref). The splitter coating is modeled via a [`ThinBeamsplitter`](@ref) placed flush onto a single face of the substrate. This allows for more realistic simulation of refractive effects such as e.g. beam path displacement and optical path differences caused by the substrate's geometry. Below two concrete implementations are showcased. +Plate beamsplitters provide a more sophisticated model by incorporating a substrate with finite thickness and [`BeamletOptics.AbstractRefractiveIndex`](@ref). The splitter coating is modeled via a [`ThinBeamsplitter`](@ref) placed flush onto a single face of the substrate. This allows for more realistic simulation of refractive effects such as e.g. beam path displacement and optical path differences caused by the substrate's geometry. Below two concrete implementations are showcased. ### Rectangular plate beamsplitter The [`RectangularPlateBeamsplitter`](@ref) represents a planar, rectangular substrate with a partially reflective coating. ```@docs; canonical=false -RectangularPlateBeamsplitter(::Real, ::Real, ::Real, ::Any) +RectangularPlateBeamsplitter(::Real, ::Real, ::Real, ::BeamletOptics.AbstractRefractiveIndex) ``` A common application involves that this type of beamsplitter is paired with a compensator plate. Below an exemplary illustration of such a setup is shown, where the beamsplitter reflects part of the incoming beam perpendiculary. A [`RectangularCompensatorPlate`](@ref) ensures that the parallel path offset is corrected. @@ -49,7 +49,7 @@ The depicted system consist of a rectangular beamsplitter (e.g. [BSW26R](https:/ This variant uses a circular substrate. While components of this type often feature a wedge angle to avoid "ghost beams" in practice, e.g. the [BSW26](https://www.thorlabs.com/thorproduct.cfm?partnumber=BSW26), this is not modeled here. ```@docs; canonical=false -RoundPlateBeamsplitter(::Real, ::Real, ::Any) +RoundPlateBeamsplitter(::Real, ::Real, ::BeamletOptics.AbstractRefractiveIndex) ``` ## Cube beamsplitter @@ -57,7 +57,7 @@ RoundPlateBeamsplitter(::Real, ::Real, ::Any) The [`CubeBeamsplitter`](@ref) is composed of two [`RightAnglePrism`](@ref)s with a partially reflective interface at their internal joint. The splitting interface is represented by a [`ThinBeamsplitter`](@ref). As with other beamsplitters in this package, the cube beamsplitter uses scalar reflection and transmission coefficients. ```@docs; canonical=false -CubeBeamsplitter(::Real, ::Any) +CubeBeamsplitter(::Real, ::BeamletOptics.AbstractRefractiveIndex) ``` A classic application of cube beamsplitters is in Mach–Zehnder interferometers, where two beamsplitters combine with additional mirrors to form two optical paths that later recombine. The figure below shows a rudimentary Mach–Zehnder arrangement using two cube beamsplitters and two [`RightAnglePrismMirror`](@ref)s: diff --git a/docs/src/basics/components/lenses.md b/docs/src/basics/components/lenses.md index b405d2a30..5c179184e 100644 --- a/docs/src/basics/components/lenses.md +++ b/docs/src/basics/components/lenses.md @@ -50,7 +50,7 @@ A [`Lens`](@ref) can be then constructed with the following function call: ```@docs; canonical=false -Lens(::BeamletOptics.AbstractRotationallySymmetricSurface, ::BeamletOptics.AbstractRotationallySymmetricSurface, ::Real, ::Any) +Lens(::BeamletOptics.AbstractRotationallySymmetricSurface, ::BeamletOptics.AbstractRotationallySymmetricSurface, ::Real, ::BeamletOptics.AbstractRefractiveIndex) ``` ### Lens constructor example @@ -78,7 +78,7 @@ LB1811 = Lens( ![Lens constructor](lens_constructor.png) -Every constructor that takes a refractive index also accepts a plain number, e.g. `1.5`, for a material without dispersion. It is stored as a [`ConstantRefractiveIndex`](@ref). Use a [`DiscreteRefractiveIndex`](@ref) or a [`SellmeierEquation`](@ref) whenever the result depends on the wavelength. +The refractive index `n` is a [`BeamletOptics.AbstractRefractiveIndex`](@ref). For a material without dispersion pass a [`ConstantRefractiveIndex`](@ref), e.g. `ConstantRefractiveIndex(1.5)`; use a [`DiscreteRefractiveIndex`](@ref) or a [`SellmeierEquation`](@ref) whenever the result depends on the wavelength. ### SDF-based spherical lenses @@ -171,7 +171,7 @@ ct = 5.9e-3 # center thickness lens = Lens( CylindricalSurface(r, d, h), ct, - n -> 1.517 + ConstantRefractiveIndex(1.517) ) ``` @@ -194,7 +194,7 @@ lens = Lens( [0, 1.1926075e-5*(1e3)^3, -2.9323497e-9*(1e3)^5, -1.8718889e-11*(1e3)^7, -1.7009961e-14*(1e3)^9, 3.5481542e-17*(1e3)^11, 6.5241296e-20*(1e3)^13] ), 7.5e-3, - n -> 1.777 + ConstantRefractiveIndex(1.777) ) ``` @@ -218,7 +218,7 @@ The following image shows the [AC254-150-AB](https://www.thorlabs.com/thorproduc Doublet lenses with aspherical surfaces, or with a different clear aperture per surface, are specified via the surface types introduced in the [Surface based lens construction](@ref) section. ```@docs; canonical=false -DoubletLens(::BeamletOptics.AbstractRotationallySymmetricSurface, ::BeamletOptics.AbstractRotationallySymmetricSurface, ::BeamletOptics.AbstractRotationallySymmetricSurface, ::Real, ::Real, ::Any, ::Any) +DoubletLens(::BeamletOptics.AbstractRotationallySymmetricSurface, ::BeamletOptics.AbstractRotationallySymmetricSurface, ::BeamletOptics.AbstractRotationallySymmetricSurface, ::Real, ::Real, ::BeamletOptics.AbstractRefractiveIndex, ::BeamletOptics.AbstractRefractiveIndex) ``` ## Triplet lenses diff --git a/docs/src/basics/components/prisms.md b/docs/src/basics/components/prisms.md index a8be121d1..d995f055e 100644 --- a/docs/src/basics/components/prisms.md +++ b/docs/src/basics/components/prisms.md @@ -15,7 +15,7 @@ prism = Prism([(0.0, 0.0), (30e-3, 0.0), (0.0, 17.32e-3)], 20e-3, NBK7) ``` ```@docs; canonical=false -Prism(::AbstractVector, ::Real, ::Any) +Prism(::AbstractVector, ::Real, ::BeamletOptics.AbstractRefractiveIndex) BeamletOptics.PolygonPrismSDF ``` @@ -24,7 +24,7 @@ BeamletOptics.PolygonPrismSDF The [`EquilateralPrism`](@ref) is centered at the centroid of its triangle with the apex along +y and is the classic dispersing prism. A ray traversing it symmetrically at minimum deviation is deflected by ``\delta = 2\arcsin(n \sin(\alpha/2)) - \alpha`` with the apex angle ``\alpha = 60^\circ``. The [`DovePrism`](@ref) is a trapezoid with 45° end faces whose long axis is the y-axis. The [`RightAnglePrism`](@ref) has its legs along x and y. ```@docs; canonical=false -EquilateralPrism(::Real, ::Real, ::Any) -DovePrism(::Real, ::Real, ::Real, ::Any) -RightAnglePrism(::Real, ::Real, ::Any) +EquilateralPrism(::Real, ::Real, ::BeamletOptics.AbstractRefractiveIndex) +DovePrism(::Real, ::Real, ::Real, ::BeamletOptics.AbstractRefractiveIndex) +RightAnglePrism(::Real, ::Real, ::BeamletOptics.AbstractRefractiveIndex) ``` diff --git a/docs/src/basics/rays.md b/docs/src/basics/rays.md index 88bc0d0ac..634c9dde3 100644 --- a/docs/src/basics/rays.md +++ b/docs/src/basics/rays.md @@ -14,7 +14,7 @@ Individual monochromatic rays form the basic building blocks to describe the pro \vec{x}(t) = \vec{p} + t \cdot \vec{d} ``` -where ``\vec{p}`` and ``\vec{d}`` are the position and direction ``\mathbb{R}^3``-vectors, respectively. The ray length ``t`` is used to describe the geometrical length of the ray. This assumes that the [`BeamletOptics.RefractiveIndex`](@ref) along the ray path is constant. If after solving an optical system a ray intersection is determined, a new ray must be spawned to model an arbitrary light path. This data is stored, e.g., in a [`Beam`](@ref). More on this can be found in the [Beams](@ref) chapter. +where ``\vec{p}`` and ``\vec{d}`` are the position and direction ``\mathbb{R}^3``-vectors, respectively. The ray length ``t`` is used to describe the geometrical length of the ray. This assumes that the [`BeamletOptics.AbstractRefractiveIndex`](@ref) along the ray path is constant. If after solving an optical system a ray intersection is determined, a new ray must be spawned to model an arbitrary light path. This data is stored, e.g., in a [`Beam`](@ref). More on this can be found in the [Beams](@ref) chapter. !!! tip "Moving rays" A ray can be translated and rotated directly via [`translate3d!`](@ref)/[`rotate3d!`](@ref) and friends, but is more commonly moved indirectly as part of a [`Beam`](@ref) or [`BeamletOptics.AbstractBeamGroup`](@ref). Refer to [Moving sources](@ref) for the full kinematic API and its reset semantics. diff --git a/docs/src/components/catalog.json b/docs/src/components/catalog.json index e1a70e2a4..da84ab478 100644 --- a/docs/src/components/catalog.json +++ b/docs/src/components/catalog.json @@ -129,7 +129,7 @@ "name": "ThinLens", "category": "Lenses", "image": "catalog_ThinLens.png", - "href": "/reference#BeamletOptics.ThinLens-Tuple{Real,%20Real,%20Real,%20Any}", + "href": "/reference#BeamletOptics.ThinLens-Tuple{Real,%20Real,%20Real,%20BeamletOptics.AbstractRefractiveIndex}", "teaser": "Idealized thin spherical lens." }, { @@ -171,21 +171,21 @@ "name": "RightAnglePrism", "category": "Prisms", "image": "catalog_RightAnglePrism.png", - "href": "/reference#BeamletOptics.RightAnglePrism-Tuple{Real,%20Real,%20Any}", + "href": "/reference#BeamletOptics.RightAnglePrism-Tuple{Real,%20Real,%20BeamletOptics.AbstractRefractiveIndex}", "teaser": "Right-angle symmetric prism, e.g. for cube beamsplitters." }, { "name": "EquilateralPrism", "category": "Prisms", "image": "catalog_EquilateralPrism.png", - "href": "/reference#BeamletOptics.EquilateralPrism-Tuple{Real,%20Real,%20Any}", + "href": "/reference#BeamletOptics.EquilateralPrism-Tuple{Real,%20Real,%20BeamletOptics.AbstractRefractiveIndex}", "teaser": "Equilateral triangular prism for dispersion." }, { "name": "DovePrism", "category": "Prisms", "image": "catalog_DovePrism.png", - "href": "/reference#BeamletOptics.DovePrism-Tuple{Real,%20Real,%20Real,%20Any}", + "href": "/reference#BeamletOptics.DovePrism-Tuple{Real,%20Real,%20Real,%20BeamletOptics.AbstractRefractiveIndex}", "teaser": "Trapezoidal Dove prism with 45 degree end faces." }, { @@ -206,28 +206,28 @@ "name": "RectangularPlateBeamsplitter", "category": "Beamsplitters", "image": "catalog_RectangularPlateBeamsplitter.png", - "href": "/reference#BeamletOptics.RectangularPlateBeamsplitter-Tuple{Real,%20Real,%20Real,%20Any}", + "href": "/reference#BeamletOptics.RectangularPlateBeamsplitter-Tuple{Real,%20Real,%20Real,%20BeamletOptics.AbstractRefractiveIndex}", "teaser": "Rectangular plate beamsplitter with a finite substrate." }, { "name": "RoundPlateBeamsplitter", "category": "Beamsplitters", "image": "catalog_RoundPlateBeamsplitter.png", - "href": "/reference#BeamletOptics.RoundPlateBeamsplitter-Tuple{Real,%20Real,%20Any}", + "href": "/reference#BeamletOptics.RoundPlateBeamsplitter-Tuple{Real,%20Real,%20BeamletOptics.AbstractRefractiveIndex}", "teaser": "Round plate beamsplitter with a finite substrate." }, { "name": "CubeBeamsplitter", "category": "Beamsplitters", "image": "catalog_CubeBeamsplitter.png", - "href": "/reference#BeamletOptics.CubeBeamsplitter-Tuple{Real,%20Any}", + "href": "/reference#BeamletOptics.CubeBeamsplitter-Tuple{Real,%20BeamletOptics.AbstractRefractiveIndex}", "teaser": "Two cemented right-angle prisms forming a cube splitter." }, { "name": "RectangularCompensatorPlate", "category": "Beamsplitters", "image": "catalog_RectangularCompensatorPlate.png", - "href": "/reference#BeamletOptics.RectangularCompensatorPlate-Union{Tuple{T},%20Tuple{H},%20Tuple{W},%20Tuple{W,%20H,%20T,%20Any}}%20where%20{W<:Real,%20H<:Real,%20T<:Real}", + "href": "/reference#BeamletOptics.RectangularCompensatorPlate-Union{Tuple{T},%20Tuple{H},%20Tuple{W},%20Tuple{W,%20H,%20T,%20BeamletOptics.AbstractRefractiveIndex}}%20where%20{W<:Real,%20H<:Real,%20T<:Real}", "teaser": "Corrects the parallel beam offset of a plate beamsplitter." }, { @@ -262,7 +262,7 @@ "name": "RoundLinearPolarizer", "category": "Polarizers", "image": "catalog_RoundLinearPolarizer.png", - "href": "/reference#BeamletOptics.RoundLinearPolarizer-Tuple{Real,%20Real,%20Real,%20Any}", + "href": "/reference#BeamletOptics.RoundLinearPolarizer-Tuple{Real,%20Real,%20Real,%20BeamletOptics.AbstractRefractiveIndex}", "teaser": "Builds a round film polarizer with glass substrates." }, { diff --git a/docs/src/examples/aspherical_lenses.md b/docs/src/examples/aspherical_lenses.md index d061b04b8..a00f64c54 100644 --- a/docs/src/examples/aspherical_lenses.md +++ b/docs/src/examples/aspherical_lenses.md @@ -22,7 +22,7 @@ lens = Lens( EvenAsphericalSurface(R, d, k, A), SphericalSurface(Inf, d), ct, - x -> n + ConstantRefractiveIndex(n) ) system = System(lens) @@ -80,7 +80,7 @@ L1 = Lens( -0.34529*(1e3)^11, 5.9938*(1e3)^13] # coeffs ), 0.72e-3, # center thickness - n -> 1.580200 + ConstantRefractiveIndex(1.580200) ) # construct the second lens @@ -101,7 +101,7 @@ L2 = Lens( -0.080329*(1e3)^15] # coeffs ), 0.55e-3, # center_thickness - n -> 1.804700 + ConstantRefractiveIndex(1.804700) ) # shift the second lens to its position, assuming L1 is at the origin (0,0,0) @@ -126,7 +126,7 @@ L3 = Lens( 1.5264e-5*(1e3)^15] # coeffs ), 0.7e-3, # center_thickness - n -> 1.580200 + ConstantRefractiveIndex(1.580200) ) # first translate the lens to the L2 position, then shift it by the relative offset with respect to L2 @@ -138,7 +138,7 @@ Filt = Lens( SphericalSurface(Inf, 4.2e-3), SphericalSurface(Inf, 4.2e-3), 0.15e-3, - n -> 1.516800 + ConstantRefractiveIndex(1.516800) ) # same principle as for L3 @@ -150,7 +150,7 @@ Cover = Lens( SphericalSurface(Inf, 4.9e-3), SphericalSurface(Inf, 4.9e-3), 0.5e-3, - n -> 1.469200 + ConstantRefractiveIndex(1.469200) ) translate_to3d!(Cover, position(Filt)) translate3d!(Cover, [0, BeamletOptics.thickness(Filt) + 0.18e-3,0]) diff --git a/docs/src/examples/double_gauss.md b/docs/src/examples/double_gauss.md index 890adbf4b..7a0a2dd9b 100644 --- a/docs/src/examples/double_gauss.md +++ b/docs/src/examples/double_gauss.md @@ -12,10 +12,10 @@ This showcase is taken from the [pencilofrays.com](https://www.pencilofrays.com/ using GLMakie, BeamletOptics # define spherical lenses -l1 = SphericalLens(48.88e-3, 182.96e-3, 8.89e-3, 52.3e-3, λ -> 1.62286) -l23 = SphericalDoubletLens(36.92e-3, Inf, 23.06e-3, 15.11e-3, 2.31e-3, 45.11e-3, λ -> 1.58565, λ -> 1.67764) -l45 = SphericalDoubletLens(-23.91e-3, Inf, -36.92e-3, 1.92e-3, 7.77e-3, 40.01e-3, λ -> 1.57046, λ -> 1.64128) -l6 = SphericalLens(1063.24e-3, -48.88e-3, 6.73e-3, 45.11e-3, λ -> 1.62286) +l1 = SphericalLens(48.88e-3, 182.96e-3, 8.89e-3, 52.3e-3, ConstantRefractiveIndex(1.62286)) +l23 = SphericalDoubletLens(36.92e-3, Inf, 23.06e-3, 15.11e-3, 2.31e-3, 45.11e-3, ConstantRefractiveIndex(1.58565), ConstantRefractiveIndex(1.67764)) +l45 = SphericalDoubletLens(-23.91e-3, Inf, -36.92e-3, 1.92e-3, 7.77e-3, 40.01e-3, ConstantRefractiveIndex(1.57046), ConstantRefractiveIndex(1.64128)) +l6 = SphericalLens(1063.24e-3, -48.88e-3, 6.73e-3, 45.11e-3, ConstantRefractiveIndex(1.62286)) # Calculate translation distances l_23 = thickness(l1) + 0.38e-3 @@ -63,14 +63,14 @@ The reference above compares the Double Gauss lens to a [Sonnar lens](https://ww The cemented triplets are modeled with the [`TripletLens`](@ref) type. The steep last surface of the front triplet only has a clear aperture of 40 mm, hence this triplet is constructed from [`SphericalSurface`](@ref)s with different diameters. The rear triplet can be created directly with the [`SphericalTripletLens`](@ref) constructor. ```julia -s1 = SphericalLens(69.21e-3, 433.84e-3, 9.33e-3, 70e-3, λ -> 1.671) +s1 = SphericalLens(69.21e-3, 433.84e-3, 9.33e-3, 70e-3, ConstantRefractiveIndex(1.671)) # front triplet: last surface only has a clear aperture of 40 mm -> constructed from surfaces s234 = TripletLens( SphericalSurface(35.86e-3, 60e-3), SphericalSurface(85.87e-3, 60e-3), SphericalSurface(-646.31e-3, 60e-3), SphericalSurface(23.51e-3, 40e-3), - 11.81e-3, 7.05e-3, 1.9e-3, λ -> 1.671, λ -> 1.4892, λ -> 1.7394) + 11.81e-3, 7.05e-3, 1.9e-3, ConstantRefractiveIndex(1.671), ConstantRefractiveIndex(1.4892), ConstantRefractiveIndex(1.7394)) s567 = SphericalTripletLens(Inf, 51.09e-3, -22.12e-3, -103.13e-3, 2.48e-3, 19.81e-3, 4.57e-3, 42e-3, - λ -> 1.5232, λ -> 1.6578, λ -> 1.5894) + ConstantRefractiveIndex(1.5232), ConstantRefractiveIndex(1.6578), ConstantRefractiveIndex(1.5894)) # Calculate translation distances s_234 = thickness(s1) + 0.38e-3 diff --git a/docs/src/examples/lens_groups.md b/docs/src/examples/lens_groups.md index 333cf53d0..9ba7f1f73 100644 --- a/docs/src/examples/lens_groups.md +++ b/docs/src/examples/lens_groups.md @@ -12,8 +12,8 @@ This example highlights the capabilities of [`ObjectGroup`](@ref)s. Several grou using GLMakie, BeamletOptics # focus group -l1 = SphericalDoubletLens(103.4371e-3, 61.14925e-3, -603.2959e-3, 1.5e-3, 10.03975e-3, 55e-3, 1.963000, 1.603112) -l2 = SphericalLens(49.97282e-3, 168.2416e-3, 8.622723e-3, 52e-3, 1.603001) +l1 = SphericalDoubletLens(103.4371e-3, 61.14925e-3, -603.2959e-3, 1.5e-3, 10.03975e-3, 55e-3, ConstantRefractiveIndex(1.963000), ConstantRefractiveIndex(1.603112)) +l2 = SphericalLens(49.97282e-3, 168.2416e-3, 8.622723e-3, 52e-3, ConstantRefractiveIndex(1.603001)) l_2 = BeamletOptics.thickness(l1) + 1e-4 translate3d!(l2, [0, l_2, 0]) @@ -21,9 +21,9 @@ translate3d!(l2, [0, l_2, 0]) focus_group = ObjectGroup([l1, l2]) # variator -l3 = SphericalLens(100.3834e-3, 16.72327e-3, 1e-3, 28.00995e-3, λ -> 1.603112) -l4 = SphericalLens(-30.28003e-3, 21.29033e-3, 0.5e-3, 22.31297e-3, λ -> 1.764500) -l5 = SphericalLens(23.06904e-3, 243.5999e-3, 3.2e-3, 22.12162e-3, λ -> 1.963000) +l3 = SphericalLens(100.3834e-3, 16.72327e-3, 1e-3, 28.00995e-3, ConstantRefractiveIndex(1.603112)) +l4 = SphericalLens(-30.28003e-3, 21.29033e-3, 0.5e-3, 22.31297e-3, ConstantRefractiveIndex(1.764500)) +l5 = SphericalLens(23.06904e-3, 243.5999e-3, 3.2e-3, 22.12162e-3, ConstantRefractiveIndex(1.963000)) l_3 = l_2 + BeamletOptics.thickness(l2) + 1.1e-3 l_4 = l_3 + BeamletOptics.thickness(l3) + 7.25496e-3 @@ -35,19 +35,19 @@ translate3d!(l5, [0, l_5, 0]) variator_group = ObjectGroup([l3, l4, l5]) # compensator -l6 = SphericalLens(-24.31747e-3, -255.5571e-3, 1e-3, 17.70449e-3, λ -> 1.638539) +l6 = SphericalLens(-24.31747e-3, -255.5571e-3, 1e-3, 17.70449e-3, ConstantRefractiveIndex(1.638539)) compensator = l6 l_6 = l_5 + BeamletOptics.thickness(l5) + 40.889e-3 translate3d!(l6, [0, l_6, 0]) # master I -l7 = SphericalLens(Inf, Inf, 1.2e-3, 19.896e-3, λ -> 1.516330) -l8 = SphericalLens(1207.65e-3, -28.39078e-3, 2.4e-3, 21.05932e-3, λ -> 1.583126) -l9 = SphericalLens(74.34233e-3, -62.52319e-3, 2.4e-3, 21.61478e-3, λ -> 1.570989) -l10 = SphericalLens(42.10459e-3, -128.0055e-3, 2.4e-3, 20.88128e-3, λ -> 1.583126) -l11 = SphericalLens(28.62223e-3, 378.3789e-3, 2.4e-3, 19.26452e-3, λ -> 1.651000) -l12 = SphericalLens(-40.92189e-3, 41.10496e-3, 1e-3, 18.17719e-3, λ -> 1.854779) +l7 = SphericalLens(Inf, Inf, 1.2e-3, 19.896e-3, ConstantRefractiveIndex(1.516330)) +l8 = SphericalLens(1207.65e-3, -28.39078e-3, 2.4e-3, 21.05932e-3, ConstantRefractiveIndex(1.583126)) +l9 = SphericalLens(74.34233e-3, -62.52319e-3, 2.4e-3, 21.61478e-3, ConstantRefractiveIndex(1.570989)) +l10 = SphericalLens(42.10459e-3, -128.0055e-3, 2.4e-3, 20.88128e-3, ConstantRefractiveIndex(1.583126)) +l11 = SphericalLens(28.62223e-3, 378.3789e-3, 2.4e-3, 19.26452e-3, ConstantRefractiveIndex(1.651000)) +l12 = SphericalLens(-40.92189e-3, 41.10496e-3, 1e-3, 18.17719e-3, ConstantRefractiveIndex(1.854779)) l_7 = l_6 + BeamletOptics.thickness(l6) + 4.765141e-3 l_8 = l_7 + BeamletOptics.thickness(l7) + 1e-3 @@ -65,10 +65,10 @@ translate3d!(l12, [0, l_12, 0]) master_group_I = ObjectGroup([l7, l8, l9, l10, l11, l12]) # master II -l13 = SphericalLens(34.65626e-3, -45.71147e-3, 2.4e-3, 15.6e-3, λ -> 1.762001) -l14 = SphericalLens(27.08863e-3, 11.38107e-3, 1e-3, 14.40932e-3, λ -> 1.761821) -l15 = SphericalLens(20.49712e-3, -203.9304e-3, 2.4e-3, 14.40932e-3, λ -> 1.693501) -l16 = SphericalLens(Inf, Inf, 4e-3, 12.92286e-3, λ -> 1.522494) +l13 = SphericalLens(34.65626e-3, -45.71147e-3, 2.4e-3, 15.6e-3, ConstantRefractiveIndex(1.762001)) +l14 = SphericalLens(27.08863e-3, 11.38107e-3, 1e-3, 14.40932e-3, ConstantRefractiveIndex(1.761821)) +l15 = SphericalLens(20.49712e-3, -203.9304e-3, 2.4e-3, 14.40932e-3, ConstantRefractiveIndex(1.693501)) +l16 = SphericalLens(Inf, Inf, 4e-3, 12.92286e-3, ConstantRefractiveIndex(1.522494)) l_13 = l_12 + BeamletOptics.thickness(l12) + 11.97864e-3 l_14 = l_13 + BeamletOptics.thickness(l13) + 0.1e-3 diff --git a/docs/src/examples/psf.md b/docs/src/examples/psf.md index 28d856d93..097aa3dce 100644 --- a/docs/src/examples/psf.md +++ b/docs/src/examples/psf.md @@ -35,7 +35,7 @@ n = 1.5 # generate uniform source, lens and detector cs = UniformDiscSource([0, -10mm, 0], [0, 1, 0], 15e-3, λ) -lens = SphericalLens(R1, R2, l, d, x -> n) +lens = SphericalLens(R1, R2, l, d, ConstantRefractiveIndex(n)) detector = Detector(10e-3) # shift detector into focus @@ -68,7 +68,7 @@ A = [0*(1e3)^1, 2.7709219e-8*(1e3)^3, 6.418186e-13*(1e3)^5, -1.5724014e-17*(1e3) AL75150 = Lens( EvenAsphericalSurface(radius, d, k, A), l, - n -> 1.5006520430 + ConstantRefractiveIndex(1.5006520430) ) xrotate3d!(AL75150, deg2rad(-0.5)) diff --git a/docs/src/index.md b/docs/src/index.md index effe5544a..9d5dd50cb 100644 --- a/docs/src/index.md +++ b/docs/src/index.md @@ -82,8 +82,9 @@ using GLMakie, BeamletOptics const mm = 1e-3 # Keplerian beam expander: f₁ = 15 mm, f₂ = 45 mm → 3× magnification -lens1 = ThinLens(15mm, 15mm, 12mm, 1.5) # biconvex, f = R for n = 1.5 -lens2 = ThinLens(45mm, 45mm, 25mm, 1.5) +glass = ConstantRefractiveIndex(1.5) +lens1 = ThinLens(15mm, 15mm, 12mm, glass) # biconvex, f = R for n = 1.5 +lens2 = ThinLens(45mm, 45mm, 25mm, glass) translate3d!(lens1, [0, 20mm, 0]) translate3d!(lens2, [0, 80mm, 0]) # spacing f₁ + f₂ system = System([lens1, lens2]) diff --git a/docs/src/tutorials/openraman.md b/docs/src/tutorials/openraman.md index 09ef69af1..1f50f46ec 100644 --- a/docs/src/tutorials/openraman.md +++ b/docs/src/tutorials/openraman.md @@ -193,7 +193,7 @@ A custom component is a subtype of [`BeamletOptics.AbstractObject`](@ref). With API (`translate3d!`, `zrotate3d!`, `position`, ...) then works for free: ```julia -struct LongpassDichroicMirror{T, N <: BMO.RefractiveIndex} <: BMO.AbstractObject{T} +struct LongpassDichroicMirror{T, N <: BMO.AbstractRefractiveIndex} <: BMO.AbstractObject{T} shape::BMO.PlanoSurfaceSDF{T} n::N cuton::T diff --git a/skills/beamletoptics/API.md b/skills/beamletoptics/API.md index 507f07ed7..b1cc03890 100644 --- a/skills/beamletoptics/API.md +++ b/skills/beamletoptics/API.md @@ -7,7 +7,7 @@ using BeamletOptics const mm = 1e-3 # 1. build components (at the origin, aligned with +y) -lens = SphericalLens(50mm, -50mm, 5mm, 25.4mm, 1.5) +lens = SphericalLens(50mm, -50mm, 5mm, 25.4mm, ConstantRefractiveIndex(1.5)) det = Detector(20mm) # 2. place them (relative moves, radians) diff --git a/skills/beamletoptics/CHECKLIST.md b/skills/beamletoptics/CHECKLIST.md index bdc4bc091..47e18ed6a 100644 --- a/skills/beamletoptics/CHECKLIST.md +++ b/skills/beamletoptics/CHECKLIST.md @@ -3,7 +3,7 @@ ## Correctness - [ ] All lengths and wavelengths in **meters** (`532e-9`, not `532`); angles in **radians** (`deg2rad`). -- [ ] Refractive indices are `SellmeierEquation`, `DiscreteRefractiveIndex`, a function of λ, or a number (constant, no dispersion); a number only where dispersion does not matter. +- [ ] Refractive indices are `SellmeierEquation`, `DiscreteRefractiveIndex` or `ConstantRefractiveIndex` (no numbers, no functions); a constant index only where dispersion does not matter. - [ ] `DiscreteRefractiveIndex` contains the exact wavelength of every source (no interpolation). - [ ] Lens radius signs follow ISO 10110 (biconvex: `r1 > 0`, `r2 < 0`). - [ ] Geometry assumes +y as the optical axis; rotations are cumulative and relative. diff --git a/skills/beamletoptics/SKILL.md b/skills/beamletoptics/SKILL.md index 6e416de87..8b75c5177 100644 --- a/skills/beamletoptics/SKILL.md +++ b/skills/beamletoptics/SKILL.md @@ -24,9 +24,9 @@ When this Skill is active: - Read `src/` of the installed package (`julia -e 'using BeamletOptics; println(pkgdir(BeamletOptics))'`). - Everything is **SI**: meters, radians, watts, V/m. **Wavelengths are in meters** (`532e-9`). - The **optical axis is +y**, not z. Components spawn at the origin aligned with +y. -- **Refractive indices are callables** `n(λ)` (λ in meters): `DiscreteRefractiveIndex(...)`, - `SellmeierEquation(...)` or a function. A plain number (`1.5`) is accepted everywhere and stored as - `ConstantRefractiveIndex(1.5)`, i.e. without dispersion. +- **Refractive indices are models** `n(λ)` (λ in meters): `SellmeierEquation(...)`, + `DiscreteRefractiveIndex(...)` or `ConstantRefractiveIndex(1.5)` (no dispersion). A plain number or a + function as `n` throws a `MethodError`. - **Detectors accumulate.** Solve with `solve_system!(system, beam; initialize = true)`, or call `initialize!(system)` (all detectors) or `empty!(detector)` (one) before every `solve_system!` that reuses the detectors; never reset between sources that should superpose. Field functions superpose all stored hits coherently; moving the detector empties it. diff --git a/skills/beamletoptics/components/beamsplitters.md b/skills/beamletoptics/components/beamsplitters.md index 546c9863c..c5c9d6ad0 100644 --- a/skills/beamletoptics/components/beamsplitters.md +++ b/skills/beamletoptics/components/beamsplitters.md @@ -23,7 +23,7 @@ Splitting creates a beam tree: `beam.children == [transmitted, reflected]`. | Constructor | Geometry at spawn | |-------------|-------------------| -| `CubeBeamsplitter(leg_length, n; reflectance = 0.5)` | centered at the origin, coating at 45° to the y-axis; `n` may be a number | +| `CubeBeamsplitter(leg_length, n; reflectance = 0.5)` | centered at the origin, coating at 45° to the y-axis | | `RectangularPlateBeamsplitter(width, height, thickness, n; reflectance = 0.5)` | coating centered at the origin, substrate towards +y (from y = 0 to `thickness`) | | `RoundPlateBeamsplitter(diameter, thickness, n; reflectance = 0.5)` | as above, round | | `ThinBeamsplitter(width[, height]; reflectance = 0.5)` | zero-thickness coating (testing, composites) | diff --git a/skills/beamletoptics/components/lenses.md b/skills/beamletoptics/components/lenses.md index 7334be586..186fbe43b 100644 --- a/skills/beamletoptics/components/lenses.md +++ b/skills/beamletoptics/components/lenses.md @@ -6,7 +6,7 @@ ## Spherical catalog lenses ```julia -SphericalLens(r1, r2, l, d = BeamletOptics.inch, n = 1.5) # l: center thickness, d: diameter +SphericalLens(r1, r2, l, d = BeamletOptics.inch, n = ConstantRefractiveIndex(1.5)) # l: center thickness, d: diameter ThinLens(R1, R2, d, n) # thin-lens Lens; SphericalLens uses it for l = 0 SphericalDoubletLens(r1, r2, r3, l1, l2, d, n1, n2) # cemented doublet SphericalTripletLens(r1, r2, r3, r4, l1, l2, l3, d, n1, n2, n3) # cemented triplet @@ -50,7 +50,7 @@ Cylindrical surfaces (curved in one direction, extruded over `height`): ```julia A = [0, A4 * 1e3^3, A6 * 1e3^5, A8 * 1e3^7, A10 * 1e3^9] -AL75150 = Lens(EvenAsphericalSurface(76.68mm, 75mm, -0.675, A), 15mm, 1.5006520430) +AL75150 = Lens(EvenAsphericalSurface(76.68mm, 75mm, -0.675, A), 15mm, ConstantRefractiveIndex(1.5006520430)) ``` Aspheres are experimental. A meniscus whose center thickness does not exceed the sagitta of its convex @@ -64,7 +64,7 @@ TripletLens(s1, s2, s3, s4, l1, l2, l3, n1, n2, n3) # s2, s3: cemented surfaces ``` Surfaces are the rotationally symmetric surfaces listed above, in order along +y; `l` are center -thicknesses, `n` are refractive indices (see `materials.md`; numbers are constant indices). Element `i` is +thicknesses, `n` are refractive indices (see `materials.md`). Element `i` is `Lens(s[i], s[i+1], l[i], n[i])`, placed flush behind the previous one, so the limits of the general `Lens` constructor apply to each element. Use these for aspherical surfaces or a different clear aperture per surface. Cylindrical surfaces are not supported (`MethodError`). @@ -72,7 +72,7 @@ surface. Cylindrical surfaces are not supported (`MethodError`). ```julia # doublet with an aspherical front surface, A as above dl = DoubletLens(EvenAsphericalSurface(50mm, 25.4mm, -0.8, A), SphericalSurface(-40mm, 25.4mm), - SphericalSurface(-200mm, 25.4mm), 8mm, 3mm, 1.6456, 1.7168) + SphericalSurface(-200mm, 25.4mm), 8mm, 3mm, ConstantRefractiveIndex(1.6456), ConstantRefractiveIndex(1.7168)) ``` ## Multi-element lenses from parts diff --git a/skills/beamletoptics/components/materials.md b/skills/beamletoptics/components/materials.md index 1c42b572c..820149ddc 100644 --- a/skills/beamletoptics/components/materials.md +++ b/skills/beamletoptics/components/materials.md @@ -1,12 +1,12 @@ # Refractive index / materials -A refractive index is anything of type -`RefractiveIndex = Union{Function, DiscreteRefractiveIndex, SellmeierEquation, ConstantRefractiveIndex}`. -It must be callable as `n(λ)` with **λ in meters** and return a real number. Every constructor that -takes `n` also accepts a plain number and stores it as `ConstantRefractiveIndex(n)` (no dispersion). +A refractive index is a subtype of `BeamletOptics.AbstractRefractiveIndex`: `ConstantRefractiveIndex`, +`DiscreteRefractiveIndex` or `SellmeierEquation`. It is callable as `n(λ)` with **λ in meters** and returns +a real number. Constructors take only these types: a plain number (`1.5`) or a function (`λ -> 1.5`) +throws a `MethodError`. ```julia -n_const = 1.5 # constant index, same as ConstantRefractiveIndex(1.5) +n_const = ConstantRefractiveIndex(1.5) # constant index, no dispersion NBK7 = DiscreteRefractiveIndex([532e-9, 1064e-9], # exact lookup table, [1.5195, 1.5066]) # NO interpolation: any other λ -> KeyError @@ -20,5 +20,7 @@ N_BK7(532e-9) # still called with meters - The surrounding medium is always n = 1. - A constant index ignores dispersion. Use `SellmeierEquation` or `DiscreteRefractiveIndex` whenever the result depends on λ (several wavelengths, chromatic focal shift, prisms for dispersion). -- Constructors validate the index by calling it with `1`, `1f0` and `1.0`, so closures must accept any `Real`. +- Another dispersion formula is a new subtype with a functor: + `struct Cauchy <: BeamletOptics.AbstractRefractiveIndex; A::Float64; B::Float64 end` and + `(c::Cauchy)(λ) = c.A + c.B / (λ * 1e6)^2`. It must accept any `Real` λ and return a `Real`. - Materials are used by lenses, prisms, plate/cube beamsplitters, compensator plates and linear polarizers. diff --git a/skills/beamletoptics/components/prisms-and-dummies.md b/skills/beamletoptics/components/prisms-and-dummies.md index e08d2b929..c3f66b7d1 100644 --- a/skills/beamletoptics/components/prisms-and-dummies.md +++ b/skills/beamletoptics/components/prisms-and-dummies.md @@ -3,7 +3,7 @@ ## Prisms - `RightAnglePrism(leg_length, height, n)`: refractive right-angle prism with legs in x/y and height in z. - `n` may be a number for a constant refractive index. It is not aligned with the y-axis at spawn, so render it once to check its orientation. + It is not aligned with the y-axis at spawn, so render it once to check its orientation. - `Prism(vertices, height, n)`: right prism over any strictly convex polygon. `vertices` are `(x, y)` points in [m] in the local frame (either orientation, validated), extruded by `height` along local z, origin at the polygon coordinates' origin. Not aligned with the y-axis; orient it with the kinematic functions. diff --git a/skills/beamletoptics/templates/02_gaussian_telescope.jl b/skills/beamletoptics/templates/02_gaussian_telescope.jl index e7313e64d..6170bd2d9 100644 --- a/skills/beamletoptics/templates/02_gaussian_telescope.jl +++ b/skills/beamletoptics/templates/02_gaussian_telescope.jl @@ -5,7 +5,7 @@ const mm = 1e-3 const nm = 1e-9 λ = 1064nm -n_glass = 1.5066 # constant index, no dispersion +n_glass = ConstantRefractiveIndex(1.5066) # constant index, no dispersion f1, f2 = 50mm, 150mm # thin symmetric biconvex lenses: R = 2(n-1)f diff --git a/skills/beamletoptics/templates/03_psf_airy.jl b/skills/beamletoptics/templates/03_psf_airy.jl index fe58ab3f4..04c0f14a9 100644 --- a/skills/beamletoptics/templates/03_psf_airy.jl +++ b/skills/beamletoptics/templates/03_psf_airy.jl @@ -5,7 +5,7 @@ const mm = 1e-3 const µm = 1e-6 λ = 1000e-9 -lens = SphericalLens(100mm, Inf, 1mm, 25.4mm, 1.5) +lens = SphericalLens(100mm, Inf, 1mm, 25.4mm, ConstantRefractiveIndex(1.5)) det = Detector(10mm) translate3d!(det, [0, 200mm + 0.13mm, 0]) # near best focus diff --git a/skills/beamletoptics/templates/07_render_system.jl b/skills/beamletoptics/templates/07_render_system.jl index 137a52765..e8e6c72a7 100644 --- a/skills/beamletoptics/templates/07_render_system.jl +++ b/skills/beamletoptics/templates/07_render_system.jl @@ -4,7 +4,7 @@ using GLMakie, BeamletOptics const mm = 1e-3 -lens = SphericalLens(50mm, -50mm, 6mm, 25.4mm, 1.5) +lens = SphericalLens(50mm, -50mm, 6mm, 25.4mm, ConstantRefractiveIndex(1.5)) mirror = RoundPlanoMirror(25.4mm, 5mm) zrotate3d!(mirror, deg2rad(45)) # fold +y -> +x translate3d!(mirror, [0, 80mm, 0]) diff --git a/src/AbstractTypes/AbstractSystem.jl b/src/AbstractTypes/AbstractSystem.jl index 33131a17c..624f3c952 100644 --- a/src/AbstractTypes/AbstractSystem.jl +++ b/src/AbstractTypes/AbstractSystem.jl @@ -10,11 +10,11 @@ Subtypes of `AbstractSystem` must implement the following: ## Fields: - `objects`: a vector or tuple of [`AbstractObject`](@ref)s that make up the system -- `n`: (optional) [`RefractiveIndex`](@ref) of the surrounding medium, default value is 1.0 +- `n`: (optional) [`AbstractRefractiveIndex`](@ref) of the surrounding medium, default value is 1.0 ## Functions: -- `refractive_index`: returns the [`RefractiveIndex`](@ref) `n` of the system medium, see above +- `refractive_index`: returns the [`AbstractRefractiveIndex`](@ref) `n` of the system medium, see above """ abstract type AbstractSystem end diff --git a/src/Exports.jl b/src/Exports.jl index 9f63d42de..50f3a4147 100644 --- a/src/Exports.jl +++ b/src/Exports.jl @@ -20,6 +20,7 @@ export ObjectGroup # additional export DiscreteRefractiveIndex, SellmeierEquation, ConstantRefractiveIndex +public AbstractRefractiveIndex #= components diff --git a/src/OpticalComponents/Beamsplitters/Compensators.jl b/src/OpticalComponents/Beamsplitters/Compensators.jl index 1388aa55a..30f0b4de0 100644 --- a/src/OpticalComponents/Beamsplitters/Compensators.jl +++ b/src/OpticalComponents/Beamsplitters/Compensators.jl @@ -10,9 +10,9 @@ The compensator is aligned with the positive y-axis. The first surface lies at t - `width`: compensator width along the x-axis in [m] - `height`: compensator height along the z-axis in [m] - `thickness`: compensator thickness along the y-axis in [m] -- `n`: the [`RefractiveIndex`](@ref) of the substrate, or a number for a constant refractive index +- `n`: the [`AbstractRefractiveIndex`](@ref) of the substrate """ -function RectangularCompensatorPlate(width::W, height::H, thickness::T, n) where {W<:Real,H<:Real,T<:Real} +function RectangularCompensatorPlate(width::W, height::H, thickness::T, n::AbstractRefractiveIndex) where {W<:Real,H<:Real,T<:Real} shape = CuboidMesh(width, thickness, height) translate3d!(shape, [ -width/2, diff --git a/src/OpticalComponents/Beamsplitters/CubeBeamsplitter.jl b/src/OpticalComponents/Beamsplitters/CubeBeamsplitter.jl index 3d8eaf56b..5c4d7df26 100644 --- a/src/OpticalComponents/Beamsplitters/CubeBeamsplitter.jl +++ b/src/OpticalComponents/Beamsplitters/CubeBeamsplitter.jl @@ -40,7 +40,7 @@ coating is orientated at a 45° angle with respect to the y-axis. # Inputs - `leg_length`: the x-, y- and z-edge length in [m] -- `n`: the [`RefractiveIndex`](@ref) of the front and back prism, or a number for a constant refractive index +- `n`: the [`AbstractRefractiveIndex`](@ref) of the front and back prism # Keywords @@ -48,7 +48,7 @@ coating is orientated at a 45° angle with respect to the y-axis. """ function CubeBeamsplitter( leg_length::Real, - n; + n::AbstractRefractiveIndex; reflectance::Real=0.5 ) front = RightAnglePrism(leg_length, leg_length, n) diff --git a/src/OpticalComponents/Beamsplitters/PlateBeamsplitter.jl b/src/OpticalComponents/Beamsplitters/PlateBeamsplitter.jl index b41f10165..bdcc6b4e4 100644 --- a/src/OpticalComponents/Beamsplitters/PlateBeamsplitter.jl +++ b/src/OpticalComponents/Beamsplitters/PlateBeamsplitter.jl @@ -77,7 +77,7 @@ the substrate lies on the positive y-side of it, from y = 0 to `thickness`. See - `width`: substrate width along the x-axis in [m] - `height`: substrate height along the z-axis in [m] - `thickness`: substrate thickness along the y-axis in [m] -- `n`: the [`RefractiveIndex`](@ref) of the substrate, or a number for a constant refractive index +- `n`: the [`AbstractRefractiveIndex`](@ref) of the substrate # Keywords @@ -87,7 +87,7 @@ function RectangularPlateBeamsplitter( width::Real, height::Real, thickness::Real, - n; + n::AbstractRefractiveIndex; reflectance::Real=0.5 ) # create substrate prism and move into pos @@ -131,7 +131,7 @@ lies on the positive y-side of it, from y = 0 to `thickness`. See also [`Rectang - `diameter`: x-z-plane substrate diameter in [m] - `thickness`: substrate thickness along the y-axis in [m] -- `n`: the [`RefractiveIndex`](@ref) of the substrate, or a number for a constant refractive index +- `n`: the [`AbstractRefractiveIndex`](@ref) of the substrate # Keywords @@ -140,7 +140,7 @@ lies on the positive y-side of it, from y = 0 to `thickness`. See also [`Rectang function RoundPlateBeamsplitter( diameter::Real, thickness::Real, - n; + n::AbstractRefractiveIndex; reflectance::Real=0.5 ) # create substrate cylinder prism diff --git a/src/OpticalComponents/DoubletLenses.jl b/src/OpticalComponents/DoubletLenses.jl index 90601ece0..be03cfb43 100644 --- a/src/OpticalComponents/DoubletLenses.jl +++ b/src/OpticalComponents/DoubletLenses.jl @@ -16,7 +16,7 @@ See also [`SphericalDoubletLens`](@ref) and the surface-based constructor This component type strongly assumes that both lenses are mounted fully flush with respect to each other. Gaps between the components might lead to incorrect results. """ -struct DoubletLens{T, F<:AbstractShape{T}, B<:AbstractShape{T}, N1<:RefractiveIndex, N2<:RefractiveIndex} <: AbstractObject{T} +struct DoubletLens{T, F<:AbstractShape{T}, B<:AbstractShape{T}, N1<:AbstractRefractiveIndex, N2<:AbstractRefractiveIndex} <: AbstractObject{T} front::Lens{T, F, N1} back::Lens{T, B, N2} end @@ -47,8 +47,8 @@ both elements share the cemented surface `s2`. - `s3`: third surface - `l1`: first lens center thickness in m - `l2`: second lens center thickness in m -- `n1`: first lens [`RefractiveIndex`](@ref), or a number for a constant refractive index -- `n2`: second lens [`RefractiveIndex`](@ref), or a number for a constant refractive index +- `n1`: first lens [`AbstractRefractiveIndex`](@ref) +- `n2`: second lens [`AbstractRefractiveIndex`](@ref) # Additional information @@ -72,8 +72,8 @@ function DoubletLens( s3::AbstractRotationallySymmetricSurface, l1::Real, l2::Real, - n1, - n2) + n1::AbstractRefractiveIndex, + n2::AbstractRefractiveIndex) # Generate "cemented" front and back lenses that share the surface s2 front = Lens(s1, s2, l1, n1) back = Lens(s2, s3, l2, n2) @@ -96,8 +96,8 @@ For radii sign definition, refer to the [`SphericalLens`](@ref) constructor. - `l1`: first lens thickness - `l2`: second lens thickness - `d`: lens diameter -- `n1`: first lens [`RefractiveIndex`](@ref), or a number for a constant refractive index -- `n2`: second lens [`RefractiveIndex`](@ref), or a number for a constant refractive index +- `n1`: first lens [`AbstractRefractiveIndex`](@ref) +- `n2`: second lens [`AbstractRefractiveIndex`](@ref) """ function SphericalDoubletLens(r1, r2, r3, l1, l2, d, n1, n2) # Generate "cemented" front and back spherical lenses diff --git a/src/OpticalComponents/Lenses.jl b/src/OpticalComponents/Lenses.jl index 0613cf81e..f9be6c209 100644 --- a/src/OpticalComponents/Lenses.jl +++ b/src/OpticalComponents/Lenses.jl @@ -9,7 +9,7 @@ Subtypes of `AbstractRefractiveOptic` should implement all supertype reqs. as we ## Fields -- `n`: a callable field which returns the [`RefractiveIndex`](@ref) for a wavelength `λ` +- `n`: the [`AbstractRefractiveIndex`](@ref) of the material, called as `n(λ)` ## Getters/setters @@ -37,7 +37,7 @@ abstract type AbstractRefractiveOptic{T, F} <: AbstractObject{T} end refractive_index(object::AbstractRefractiveOptic) = object.n # The result is converted to the number type of the optic and the wavelength, since `n` may be any # function: e.g. a `ForwardDiff.Dual` within automatic differentiation, else `Float64`. -function refractive_index(object::AbstractRefractiveOptic{T, <:RefractiveIndex}, λ::L) where {T, L <: Real} +function refractive_index(object::AbstractRefractiveOptic{T, <:AbstractRefractiveIndex}, λ::L) where {T, L <: Real} return convert(promote_type(T, L), object.n(λ))::promote_type(T, L) end @@ -128,35 +128,32 @@ function interact3d(system::AbstractSystem, optic::AbstractRefractiveOptic, end """ - Lens{T, S <: AbstractShape{T}, N <: RefractiveIndex} <: AbstractRefractiveOptic{T, N} + Lens{T, S <: AbstractShape{T}, N <: AbstractRefractiveIndex} <: AbstractRefractiveOptic{T, N} -Represents an uncoated `Lens` with a homogeneous [`RefractiveIndex`](@ref) `n = n(λ)`. +Represents an uncoated `Lens` with a homogeneous [`AbstractRefractiveIndex`](@ref) `n = n(λ)`. Refer to the [`Lens`](@ref) and [`SphericalLens`](@ref) constructors for more information on how to generate lenses. # Fields - `shape`: geometry of the lens, refer to [`AbstractShape`](@ref) for more information -- `n`: [`RefractiveIndex`](@ref) function that returns n(λ) +- `n`: [`AbstractRefractiveIndex`](@ref) of the lens material # Additional information !!! info "Refractive index" - The chromatic dispersion of the lens is represented by a λ-dependent function for `n` - and must be provided by the user. A number, e.g. `1.5`, is stored as a - [`ConstantRefractiveIndex`](@ref), i.e. the lens has the same refractive index for all wavelengths. + The chromatic dispersion of the lens is represented by the [`AbstractRefractiveIndex`](@ref) `n`, + e.g. a [`SellmeierEquation`](@ref). A [`ConstantRefractiveIndex`](@ref) gives the lens the same + refractive index for all wavelengths. """ -struct Lens{T, S <: AbstractShape{T}, N <: RefractiveIndex} <: AbstractRefractiveOptic{T, N} +struct Lens{T, S <: AbstractShape{T}, N <: AbstractRefractiveIndex} <: AbstractRefractiveOptic{T, N} shape::S n::N function Lens( - shape::S, n::N) where {T <: Real, S <: AbstractShape{T}, N <: RefractiveIndex} - test_refractive_index_function(n) + shape::S, n::N) where {T <: Real, S <: AbstractShape{T}, N <: AbstractRefractiveIndex} return new{T, S, N}(shape, n) end end -Lens(shape::AbstractShape, n::Real) = Lens(shape, ConstantRefractiveIndex(n)) - thickness(l::Lens) = thickness(shape(l)) """ @@ -172,13 +169,13 @@ _signed_edge_sag(s::SphericalSurface, sd) = sign(radius(s)) * edge_sag(s, sd) _signed_edge_sag(s::CylindricalSurface, sd) = sign(radius(s)) * edge_sag(s, sd) """ - Lens(front_surface::AbstractRotationallySymmetricSurface, back_surface::AbstractRotationallySymmetricSurface, center_thickness::Real, n) + Lens(front_surface::AbstractRotationallySymmetricSurface, back_surface::AbstractRotationallySymmetricSurface, center_thickness::Real, n::AbstractRefractiveIndex) Constructs a new [`Lens`](@ref) object using the surface specifications `front_surface` and `back_surface` and the `center_thickness`. These inputs are used to construct a [`UnionSDF`](@ref) that consists of the appropriate sub-SDFs to represent the shape of the lens. -The material is supplied via the [`RefractiveIndex`](@ref) `n`, or a number for a constant refractive index. +The material is supplied via the [`AbstractRefractiveIndex`](@ref) `n`. # Additional information @@ -193,7 +190,7 @@ function Lens( front_surface::AbstractRotationallySymmetricSurface, back_surface::AbstractRotationallySymmetricSurface, center_thickness::Real, - n) + n::AbstractRefractiveIndex) # Define effective (optical and mechanical) diameters: d_mid = min(diameter(front_surface), diameter(back_surface)) md_mid = max(mechanical_diameter(front_surface), mechanical_diameter(back_surface)) @@ -302,7 +299,7 @@ function Lens( end function Lens(front_surface::AbstractRotationallySymmetricSurface, - center_thickness::Real, n) + center_thickness::Real, n::AbstractRefractiveIndex) Lens( front_surface, CircularFlatSurface(diameter(front_surface)), @@ -312,7 +309,7 @@ function Lens(front_surface::AbstractRotationallySymmetricSurface, end function Lens(front_surface::CircularFlatSurface, back_surface::CircularFlatSurface, - center_thickness::Real, n) + center_thickness::Real, n::AbstractRefractiveIndex) d_mid = min(diameter(front_surface), diameter(back_surface)) return Lens( @@ -322,7 +319,7 @@ function Lens(front_surface::CircularFlatSurface, back_surface::CircularFlatSurf end """ - Lens(front_surface::AbstractCylindricalSurface, back_surface::AbstractCylindricalSurface, center_thickness::Real, n) + Lens(front_surface::AbstractCylindricalSurface, back_surface::AbstractCylindricalSurface, center_thickness::Real, n::AbstractRefractiveIndex) Constructs a new [`Lens`](@ref) object using the cylindric surface specifications `front_surface` and `back_surface` and the `center_thickness`. These inputs are used to construct a [`UnionSDF`](@ref) @@ -332,7 +329,7 @@ This method of `Lens` is specific for cylindric lenses and has some limitations: - The cylinder height of both surfaces has to be identical - No mixture with non-cylindric surfaces is supported at the moment -The material is supplied via the [`RefractiveIndex`](@ref) `n`, or a number for a constant refractive index. +The material is supplied via the [`AbstractRefractiveIndex`](@ref) `n`. # Additional information @@ -343,7 +340,7 @@ function Lens( front_surface::AbstractCylindricalSurface, back_surface::AbstractCylindricalSurface, center_thickness::Real, - n) + n::AbstractRefractiveIndex) # Initialize remaining box section length. l0 = center_thickness @@ -401,7 +398,7 @@ function Lens( end function Lens(front_surface::AbstractCylindricalSurface, - center_thickness::Real, n) + center_thickness::Real, n::AbstractRefractiveIndex) Lens( front_surface, RectangularFlatSurface(diameter(front_surface)), @@ -445,7 +442,7 @@ function cylindric_lens_outer_parameters( end function Lens(front_surface::RectangularFlatSurface, back_surface::RectangularFlatSurface, - center_thickness::Real, n) + center_thickness::Real, n::AbstractRefractiveIndex) d_mid = min(diameter(front_surface), diameter(back_surface)) mid = BoxSDF(d_mid, center_thickness, d_mid) translate3d!(mid, [0, center_thickness / 2, 0]) diff --git a/src/OpticalComponents/Polarizers/LinearPolarizer.jl b/src/OpticalComponents/Polarizers/LinearPolarizer.jl index a6fb0cade..4dfebe869 100644 --- a/src/OpticalComponents/Polarizers/LinearPolarizer.jl +++ b/src/OpticalComponents/Polarizers/LinearPolarizer.jl @@ -1,5 +1,5 @@ """ - LinearPolarizer{T, N <: RefractiveIndex} <: AbstractObject{T} + LinearPolarizer{T, N <: AbstractRefractiveIndex} <: AbstractObject{T} Represents a real, round linear polarizing film laminated between two uncoated glass plates (e.g. Thorlabs LPNIRE100-B), modeled as a thin [`PolarizationFilter`](@ref) cemented between two @@ -26,7 +26,7 @@ for the constructor. the beam is terminated at the cemented film interface once the norm of the transmitted electric field falls to or below the `cutoff_strength` of the underlying [`PolarizationFilter`](@ref). """ -struct LinearPolarizer{T, N <: RefractiveIndex} <: AbstractObject{T} +struct LinearPolarizer{T, N <: AbstractRefractiveIndex} <: AbstractObject{T} filter::PolarizationFilter{T, Mesh{T}} front::Prism{T, PlanoSurfaceSDF{T}, N} back::Prism{T, PlanoSurfaceSDF{T}, N} @@ -57,7 +57,7 @@ of the given `diameter`, flush against the film on both sides. - `diameter`: outer diameter of the film and substrates in [m] - `front_thickness`: thickness of the front glass substrate in [m] - `back_thickness`: thickness of the back glass substrate in [m] -- `n`: [`RefractiveIndex`](@ref) of both glass substrates, or a number for a constant refractive index +- `n`: [`AbstractRefractiveIndex`](@ref) of both glass substrates # Keywords @@ -70,7 +70,7 @@ The component is centered on the film (local origin); the front substrate extend along local +y. The film transmits along local x and blocks local z. """ function RoundLinearPolarizer(diameter::Real, front_thickness::Real, back_thickness::Real, - n; cutoff_strength = eps()) + n::AbstractRefractiveIndex; cutoff_strength = eps()) front_thickness > 0 || throw(ArgumentError("front_thickness must be positive")) back_thickness > 0 || throw(ArgumentError("back_thickness must be positive")) diff --git a/src/OpticalComponents/Prisms.jl b/src/OpticalComponents/Prisms.jl index 47a5c1898..ec5223551 100644 --- a/src/OpticalComponents/Prisms.jl +++ b/src/OpticalComponents/Prisms.jl @@ -1,20 +1,17 @@ """ - Prism{T, S <: AbstractShape{T}, N <: RefractiveIndex} <: AbstractRefractiveOptic{T, N} + Prism{T, S <: AbstractShape{T}, N <: AbstractRefractiveIndex} <: AbstractRefractiveOptic{T, N} Essentially represents the same functionality as [`Lens`](@ref). Refer to its documentation. """ -struct Prism{T, S <: AbstractShape{T}, N <: RefractiveIndex} <: AbstractRefractiveOptic{T, N} +struct Prism{T, S <: AbstractShape{T}, N <: AbstractRefractiveIndex} <: AbstractRefractiveOptic{T, N} shape::S n::N - function Prism(shape::S, n::N) where {T<:Real, S<:AbstractShape{T}, N<:RefractiveIndex} - test_refractive_index_function(n) + function Prism(shape::S, n::N) where {T<:Real, S<:AbstractShape{T}, N<:AbstractRefractiveIndex} return new{T, S, N}(shape, n) end end -Prism(shape::AbstractShape, n::Real) = Prism(shape, ConstantRefractiveIndex(n)) - thickness(p::Prism) = thickness(shape(p)) """ @@ -28,9 +25,9 @@ and the hypotenuse passes through the local origin, see [`RightAnglePrismSDF`](@ - `leg_length`: dimension in x- and y-direction in [m] - `height`: extent along the local z-axis in [m] -- `n`: [`RefractiveIndex`](@ref) of the prism, or a number for a constant refractive index +- `n`: [`AbstractRefractiveIndex`](@ref) of the prism """ -function RightAnglePrism(leg_length::Real, height::Real, n) +function RightAnglePrism(leg_length::Real, height::Real, n::AbstractRefractiveIndex) shape = RightAnglePrismSDF(leg_length, height) return Prism(shape, n) end @@ -49,9 +46,9 @@ for the geometry. See also [`EquilateralPrism`](@ref) and [`DovePrism`](@ref). - `vertices`: corners `(x, y)` of the cross-section in the local x-y-plane in [m], as a vector of 2D points or tuples. Both vertex orders are accepted. The polygon must be strictly convex. - `height`: extent along the local z-axis in [m], the prism is centered at `z = 0` -- `n`: [`RefractiveIndex`](@ref) of the prism, or a number for a constant refractive index +- `n`: [`AbstractRefractiveIndex`](@ref) of the prism """ -function Prism(vertices::AbstractVector, height::Real, n) +function Prism(vertices::AbstractVector, height::Real, n::AbstractRefractiveIndex) return Prism(PolygonPrismSDF(vertices, height), n) end @@ -67,9 +64,9 @@ minimum deviation is deflected towards the base. - `side`: edge length of the triangular cross-section in [m], must be positive - `height`: extent along the local z-axis in [m] -- `n`: [`RefractiveIndex`](@ref) of the prism, or a number for a constant refractive index +- `n`: [`AbstractRefractiveIndex`](@ref) of the prism """ -function EquilateralPrism(side::Real, height::Real, n) +function EquilateralPrism(side::Real, height::Real, n::AbstractRefractiveIndex) side > 0 || throw(ArgumentError("the side length must be positive")) r = side * sqrt(oftype(float(side), 3)) / 6 # inradius return Prism([(-side / 2, -r), (side / 2, -r), (zero(r), 2r)], height, n) @@ -89,9 +86,9 @@ cross-section's bounding box. - `length`: length of the long base face in [m], must be larger than `2 * aperture` - `aperture`: distance between the base face and the parallel short face in [m], must be positive - `height`: extent along the local z-axis in [m] -- `n`: [`RefractiveIndex`](@ref) of the prism, or a number for a constant refractive index +- `n`: [`AbstractRefractiveIndex`](@ref) of the prism """ -function DovePrism(length::Real, aperture::Real, height::Real, n) +function DovePrism(length::Real, aperture::Real, height::Real, n::AbstractRefractiveIndex) aperture > 0 || throw(ArgumentError("the aperture must be positive")) length > 2aperture || throw(ArgumentError("the length must be larger than twice the aperture")) a, l = aperture / 2, length / 2 diff --git a/src/OpticalComponents/SphericalLenses.jl b/src/OpticalComponents/SphericalLenses.jl index 7d9af0a7f..0f669e760 100644 --- a/src/OpticalComponents/SphericalLenses.jl +++ b/src/OpticalComponents/SphericalLenses.jl @@ -1,5 +1,5 @@ """ - SphericalLens(r1, r2, l, d=1inch, n=1.5) + SphericalLens(r1, r2, l, d=1inch, n=ConstantRefractiveIndex(1.5)) Creates a spherical [`Lens`](@ref) based on: @@ -7,7 +7,7 @@ Creates a spherical [`Lens`](@ref) based on: - `r2`: back radius - `l`: lens thickness - `d`: lens diameter, default is one inch -- `n`: [`RefractiveIndex`](@ref) as a function of λ, i.e. `n = n(λ)`, or a number for a constant refractive index +- `n`: [`AbstractRefractiveIndex`](@ref) of the lens material # Notes @@ -17,7 +17,7 @@ Creates a spherical [`Lens`](@ref) based on: !!! info "Thin lenses" If `l` is set to zero, a [`ThinLens`](@ref) will be created. However, note that the actual lens thickness will be different from zero. """ -function SphericalLens(r1::Real, r2::Real, l::Real, d::Real = 1inch, n = 1.5) +function SphericalLens(r1::Real, r2::Real, l::Real, d::Real = 1inch, n::AbstractRefractiveIndex = ConstantRefractiveIndex(1.5)) # Test for thin lens if iszero(l) return ThinLens(r1, r2, d, n) @@ -33,12 +33,12 @@ end """ - ThinLens(R1::Real, R2::Real, d::Real, n) + ThinLens(R1::Real, R2::Real, d::Real, n::AbstractRefractiveIndex) Directly creates an ideal spherical thin [`Lens`](@ref) with radii of curvature `R1` and `R2` and diameter `d` -and [`RefractiveIndex`](@ref) `n`, or a number for a constant refractive index. +and [`AbstractRefractiveIndex`](@ref) `n`. """ -function ThinLens(R1::Real, R2::Real, d::Real, n) +function ThinLens(R1::Real, R2::Real, d::Real, n::AbstractRefractiveIndex) shape = ThinLensSDF(R1, R2, d) return Lens(shape, n) end diff --git a/src/OpticalComponents/TripletLenses.jl b/src/OpticalComponents/TripletLenses.jl index 54ef1ca7f..f03364946 100644 --- a/src/OpticalComponents/TripletLenses.jl +++ b/src/OpticalComponents/TripletLenses.jl @@ -26,7 +26,7 @@ See also [`SphericalTripletLens`](@ref) and the surface-based constructor Total internal reflection at a cemented interface between two elements is not modeled correctly. """ struct TripletLens{T, F<:AbstractShape{T}, M<:AbstractShape{T}, B<:AbstractShape{T}, - N1<:RefractiveIndex, N2<:RefractiveIndex, N3<:RefractiveIndex} <: AbstractObject{T} + N1<:AbstractRefractiveIndex, N2<:AbstractRefractiveIndex, N3<:AbstractRefractiveIndex} <: AbstractObject{T} front::Lens{T, F, N1} middle::Lens{T, M, N2} back::Lens{T, B, N3} @@ -60,9 +60,9 @@ along +y by `l1` and `l1 + l2`, so that neighbouring elements share the cemented - `l1`: first lens center thickness in m - `l2`: second lens center thickness in m - `l3`: third lens center thickness in m -- `n1`: first lens [`RefractiveIndex`](@ref), or a number for a constant refractive index -- `n2`: second lens [`RefractiveIndex`](@ref), or a number for a constant refractive index -- `n3`: third lens [`RefractiveIndex`](@ref), or a number for a constant refractive index +- `n1`: first lens [`AbstractRefractiveIndex`](@ref) +- `n2`: second lens [`AbstractRefractiveIndex`](@ref) +- `n3`: third lens [`AbstractRefractiveIndex`](@ref) # Additional information @@ -88,9 +88,9 @@ function TripletLens( l1::Real, l2::Real, l3::Real, - n1, - n2, - n3) + n1::AbstractRefractiveIndex, + n2::AbstractRefractiveIndex, + n3::AbstractRefractiveIndex) # Generate "cemented" front, middle and back lenses that share the surfaces s2 and s3 front = Lens(s1, s2, l1, n1) middle = Lens(s2, s3, l2, n2) @@ -118,9 +118,9 @@ For radii sign definition, refer to the [`SphericalLens`](@ref) constructor. - `l2`: second lens thickness - `l3`: third lens thickness - `d`: lens diameter -- `n1`: first lens [`RefractiveIndex`](@ref), or a number for a constant refractive index -- `n2`: second lens [`RefractiveIndex`](@ref), or a number for a constant refractive index -- `n3`: third lens [`RefractiveIndex`](@ref), or a number for a constant refractive index +- `n1`: first lens [`AbstractRefractiveIndex`](@ref) +- `n2`: second lens [`AbstractRefractiveIndex`](@ref) +- `n3`: third lens [`AbstractRefractiveIndex`](@ref) # Additional information diff --git a/src/Properties.jl b/src/Properties.jl index f7cac812d..3bf6037b9 100644 --- a/src/Properties.jl +++ b/src/Properties.jl @@ -126,21 +126,19 @@ Components =# """ -Returns the refractive index model `n` (see [`RefractiveIndex`](@ref)) and its value at the +Returns the refractive index model `n` (see [`AbstractRefractiveIndex`](@ref)) and its value at the default wavelength λ₀, see [`get_default_wavelength`](@ref). """ -function _index_properties(n) +function _index_properties(n::AbstractRefractiveIndex) λ0 = get_default_wavelength() - return Pair{String, Any}["Index model" => _index_model(n), "n(λ₀)" => n(λ0), "λ₀ [m]" => λ0] + return Pair{String, Any}["Index model" => _type_name(n), "n(λ₀)" => n(λ0), "λ₀ [m]" => λ0] end # Only defined at its wavelengths, which are listed instead of a value at λ₀ function _index_properties(n::DiscreteRefractiveIndex) λs = sort!(collect(keys(n.data))) - return Pair{String, Any}["Index model" => _index_model(n), "Wavelengths [m]" => λs, + return Pair{String, Any}["Index model" => _type_name(n), "Wavelengths [m]" => λs, "Indices" => [n.data[λ] for λ in λs]] end -_index_model(::Function) = "function of λ" -_index_model(n) = _type_name(n) properties(x::AbstractRefractiveOptic) = [default_properties(x); _index_properties(refractive_index(x))] diff --git a/src/Utils/RefractiveIndexUtils.jl b/src/Utils/RefractiveIndexUtils.jl index 79899071e..8ad910173 100644 --- a/src/Utils/RefractiveIndexUtils.jl +++ b/src/Utils/RefractiveIndexUtils.jl @@ -1,11 +1,49 @@ +""" + AbstractRefractiveIndex + +A generic representation of the refractive index `n(λ)` of a material, passed as `n` to e.g. +[`Lens`](@ref), [`Prism`](@ref) and the beamsplitters. See [`ConstantRefractiveIndex`](@ref), +[`DiscreteRefractiveIndex`](@ref) and [`SellmeierEquation`](@ref). + +# Implementation reqs. + +Subtypes of `AbstractRefractiveIndex` must implement the following: + +## Functions + +- `(n::AbstractRefractiveIndex)(λ)`: returns the refractive index as a `Real` for the wavelength + `λ` in \\[m\\], for any `Real` type of `λ` within the domain of the model + +# Additional information + +!!! info "Domain" + A model may be defined for some wavelengths only, e.g. a [`DiscreteRefractiveIndex`](@ref) + only at its stored wavelengths. +""" +abstract type AbstractRefractiveIndex end + +(n::AbstractRefractiveIndex)(λ) = error(lazy"refractive index n(λ) not defined for $(typeof(n))") + +""" + ConstantRefractiveIndex{T} + +Represents a refractive index `n` that is the same for all wavelengths, i.e. without dispersion. +Can be called like a function `n = n(λ)`. +""" +struct ConstantRefractiveIndex{T <: Real} <: AbstractRefractiveIndex + n::T +end + +(cri::ConstantRefractiveIndex)(λ) = cri.n + """ DiscreteRefractiveIndex{T} Represents a incomplete set of dispersion data where for each exact wavelength one refractive index value is stored in the `data` field. Can be called like a function `n = n(λ)`. Does not interpolate between data points. -Refer to [`RefractiveIndex`](@ref) for more information. +Refer to [`AbstractRefractiveIndex`](@ref) for more information. """ -struct DiscreteRefractiveIndex{T} +struct DiscreteRefractiveIndex{T} <: AbstractRefractiveIndex data::Dict{T, T} end @@ -30,47 +68,6 @@ end (dri::DiscreteRefractiveIndex)(λ) = dri.data[λ] -"[`DiscreteRefractiveIndex`](@ref) passes test by default" -test_refractive_index_function(::DiscreteRefractiveIndex) = nothing - -""" - test_refractive_index_function(input) - -Tests if `input` is callable with a single `Real` argument for the wavelength `λ` and -returns a single `Real` value for the refractive index `n`. -""" -function test_refractive_index_function(input) - # Test function compat for the following types of λ - Ts = [Int, Float32, Float64] - try - for T in Ts - # Test if input accepts types - answer = input(one(T)) - # Test if input returns single real value - if !isa(answer, Real) - error() - end - end - catch - error_msg = "Ref. index must be callable with a single Real argument and return a single real result." - throw(ArgumentError(error_msg)) - end - return nothing -end - -""" - ConstantRefractiveIndex{T} - -Represents a refractive index `n` that is the same for all wavelengths, i.e. without dispersion. -Can be called like a function `n = n(λ)`. Constructors that take a [`RefractiveIndex`](@ref) store -a plain number `n` as `ConstantRefractiveIndex(n)`. -""" -struct ConstantRefractiveIndex{T <: Real} - n::T -end - -(cri::ConstantRefractiveIndex)(λ) = cri.n - """ SellmeierEquation @@ -85,14 +82,14 @@ n_532 = NBK7(532e-9) !!! info When initializing this type, the data should be provided with the classic **μm-based coefficients**. - However, when calling the function, use SI-units, e.g. `532e-9` for 532 nm. + However, when calling the function, use SI-units, e.g. `532e-9` for 532 nm. # Fields - `B1`, `B2`, `B3` : dimensionless Sellmeier coefficients. - `C1`, `C2`, `C3` : squared resonance wavelengths in μm². """ -struct SellmeierEquation{T<:Real} +struct SellmeierEquation{T<:Real} <: AbstractRefractiveIndex B1::T B2::T B3::T @@ -109,18 +106,3 @@ function (SE::SellmeierEquation)(λ) (SE.B3*λ^2)/(λ^2 - SE.C3) return sqrt(n²) end - -""" - RefractiveIndex - -Union type that represents valid means to pass a refractive index `n` to e.g. [`AbstractObject`](@ref)s. -The core assumption is that: - -1. the refractive index is callable with a **single** `Number` argument `λ` to represent the wavelength in [m] -2. the return value is a **single** `Number` value for the refractive index - -Refer to e.g. [`DiscreteRefractiveIndex`](@ref), [`SellmeierEquation`](@ref) and -[`ConstantRefractiveIndex`](@ref). Constructors of optical components also accept a plain number -`n` and store it as a [`ConstantRefractiveIndex`](@ref). -""" -const RefractiveIndex = Union{Function, DiscreteRefractiveIndex, SellmeierEquation, ConstantRefractiveIndex} \ No newline at end of file diff --git a/src/Workloads/asphere_wl.jl b/src/Workloads/asphere_wl.jl index bb7c47681..55bd19789 100644 --- a/src/Workloads/asphere_wl.jl +++ b/src/Workloads/asphere_wl.jl @@ -18,7 +18,7 @@ EvenAsphericalSurface(R, d, k, A), SphericalSurface(Inf, d), ct, - _n -> n + ConstantRefractiveIndex(n) ) system = System(lens) diff --git a/test/Components/TestBeamsplitters.jl b/test/Components/TestBeamsplitters.jl index 3e00dac8e..0cb2a91f8 100644 --- a/test/Components/TestBeamsplitters.jl +++ b/test/Components/TestBeamsplitters.jl @@ -1,204 +1,203 @@ -module TestBeamsplitters - -using BeamletOptics -using LinearAlgebra -using Test - -const BMO = BeamletOptics - -const mm = 1e-3 - -@testset "Beamsplitters" begin - N0 = 1.5 - @testset "Testing RectangularPlateBeamsplitter with Beam" begin - # Init splitter - N0 = 1.5 - pbs = RectangularPlateBeamsplitter(36mm, 25mm, 1mm, n -> N0) - system = System([pbs]) - beam = Beam([0, -50mm, 0], [0, 1, 0], 1e-6) - # Trace normally - zrotate3d!(pbs, deg2rad(45)) - # Keep geometry regressions from growing an unbounded beam tree in CI. - solve_system!(system, beam; depth_max=4) - - @testset "Test pos/dir" begin - @test position(pbs) == zeros(3) - @test orientation(pbs) ≈ orientation(pbs.substrate) - end - - @testset "Test children after tracing" begin - p = beam.rays - t = beam.children[1].rays - r = beam.children[2].rays - # no of rays - @test length(p) == 1 - @test length(t) == 2 - @test length(r) == 1 - # correct ref. index - @test all(BMO.refractive_index.(p) .== 1) - @test all(BMO.refractive_index.(t) .== [N0, 1]) - @test all(BMO.refractive_index.(r) .== 1) - # correct dir - @test BMO.direction(first(p)) ≈ BMO.direction(last(t)) - @test BMO.direction(first(r)) ≈ [1, 0, 0] - end - - # Solve again with the backside - zrotate3d!(pbs, π) - solve_system!(system, beam; depth_max=4) - - @testset "Test children after solving again" begin - p = beam.rays - t = beam.children[1].rays - r = beam.children[2].rays - # no of rays - @test length(p) == 2 - @test length(t) == 1 - @test length(r) == 2 - # correct ref. index - @test all(BMO.refractive_index.(p) .== [1, N0]) - @test all(BMO.refractive_index.(t) .== 1) - @test all(BMO.refractive_index.(r) .== [N0, 1]) - # correct dir - @test BMO.direction(first(p)) ≈ BMO.direction(last(t)) - @test BMO.direction(last(r)) ≈ [1, 0, 0] - end - end - - @testset "Testing CubeBeamsplitter with Beam" begin - # Init splitter - cbs = CubeBeamsplitter(25e-3, n -> N0) - translate3d!(cbs, [0, 50mm, 0]) - system = System([cbs]) - beam = Beam([0, 0, 0], [0, 1, 0], 1e-6) - - @testset "Initial CBS tracing" begin - # Trace normally - solve_system!(system, beam) - # Test correct ray length, ref. indices, dirs - p = BMO.rays(beam) - t = BMO.rays(beam.children[1]) - r = BMO.rays(beam.children[2]) - - @test length(p) == 2 - @test length(r) == 2 - @test length(t) == 2 - @test BMO.refractive_index.(p) == [1, N0] - @test BMO.refractive_index.(t) == [N0, 1] - @test BMO.refractive_index.(r) == [N0, 1] - @test BMO.direction(last(t)) ≈ BMO.direction(first(p)) - @test BMO.direction(last(r)) ≈ [-1, 0, 0] - end - - @testset "Solve again after 45° CBS rotation" begin - # Solve again - zrotate3d!(cbs, π / 2) - solve_system!(system, beam) - - # Test correct ray dirs - p = BMO.rays(beam) - t = BMO.rays(beam.children[1]) - r = BMO.rays(beam.children[2]) - - @test BMO.direction(last(t)) ≈ BMO.direction(first(p)) - @test BMO.direction(last(t)) ≈ [0, 1, 0] - @test BMO.position(p[2]) ≈ [0, 50mm - 12.5mm, 0] - end - - @testset "Solve again, CBS backside" begin - # Solve again, backside - zrotate3d!(cbs, π / 2) - solve_system!(system, beam) - - # Test correct ray length, ref. indices, dirs - p = BMO.rays(beam) - t = BMO.rays(beam.children[1]) - r = BMO.rays(beam.children[2]) - - @test length(p) == 2 - @test length(r) == 2 - @test length(t) == 2 - @test BMO.refractive_index.(p) == [1, N0] - @test BMO.refractive_index.(t) == [N0, 1] - @test BMO.refractive_index.(r) == [N0, 1] - @test BMO.direction(last(t)) ≈ BMO.direction(first(p)) - @test BMO.direction(last(r)) ≈ [-1, 0, 0] - end - end - - @testset "CubeBeamsplitter and RightAnglePrism with a constant refractive index" begin - prism = RightAnglePrism(25e-3, 20e-3, N0) - @test prism isa Prism - @test BMO.refractive_index(prism, 1e-6) == N0 - @test BMO.refractive_index(prism, 500e-9) == N0 - @test BMO.refractive_index(prism) isa ConstantRefractiveIndex - for substrate in (RectangularPlateBeamsplitter(25e-3, 25e-3, 5e-3, N0).substrate, - RoundPlateBeamsplitter(25e-3, 5e-3, N0).substrate, - RectangularCompensatorPlate(25e-3, 25e-3, 5e-3, N0)) - @test BMO.refractive_index(substrate) == ConstantRefractiveIndex(N0) - end - - cbs = CubeBeamsplitter(25e-3, N0; reflectance = 0.3) - @test BMO.refractive_index(cbs, 1e-6) == N0 - @test BMO.refractive_index(cbs, 500e-9) == N0 - @test cbs.coating.reflectance ≈ CubeBeamsplitter(25e-3, n -> N0; reflectance = 0.3).coating.reflectance - translate3d!(cbs, [0, 50mm, 0]) - beam = Beam([0, 0, 0], [0, 1, 0], 1e-6) - solve_system!(System([cbs]), beam) - @test BMO.refractive_index.(BMO.rays(beam)) == [1, N0] - @test length(beam.children) == 2 - end - - @testset "depth_max branch limiting" begin - beamsplitter = CubeBeamsplitter(25e-3, n -> N0) - translate3d!(beamsplitter, [0, 50mm, 0]) - system = System([beamsplitter]) - - beam = Beam([0, 0, 0], [0, 1, 0], 1e-6) - solve_system!(system, beam; depth_max=0) - @test isempty(beam.children) - - beam = Beam([0, 0, 0], [0, 1, 0], 1e-6) - solve_system!(system, beam; depth_max=1) - @test length(beam.children) == 2 - end - - @testset "depth_max default" begin - beamsplitter = CubeBeamsplitter(25e-3, n -> N0) - translate3d!(beamsplitter, [0, 50mm, 0]) - system = System([beamsplitter]) - beam = Beam([0, 0, 0], [0, 1, 0], 1e-6) - - @test BMO.get_default_depth_max() == 100 - solve_system!(system, beam) - @test length(beam.children) == 2 - end - - @testset "Field of a polarized ray behind the plate" begin - # the coating transmits half of the power into the glass, the uncoated back takes its - # Fresnel loss: the field of the transmitted ray is transverse and has this amplitude - n = 1.5 - θ_i = π / 4 - θ_t = asin(sin(θ_i) / n) - r_s = (cos(θ_i) - n * cos(θ_t)) / (cos(θ_i) + n * cos(θ_t)) - r_p = (n * cos(θ_i) - cos(θ_t)) / (n * cos(θ_i) + cos(θ_t)) - for (e, R_back) in (([0.0, 0, 1], r_s^2), ([1.0, 0, 0], r_p^2)) - pbs = RectangularPlateBeamsplitter(25e-3, 25e-3, 5e-3, λ -> n) - zrotate3d!(pbs, π / 4) - beam = Beam(PolarizedRay([0.0, -0.1, 0], [0.0, 1, 0], 1e-6, e)) - solve_system!(System([pbs]), beam) - transmitted, reflected = BMO.children(beam) - inside, outside = BMO.rays(transmitted) - @test BMO.refractive_index(inside) == n - for ray in (inside, outside, first(BMO.rays(reflected))) - @test abs(dot(BMO.direction(ray), BMO.polarization(ray))) < 1e-12 - end - @test sum(abs2, BMO.polarization(first(BMO.rays(reflected)))) ≈ 1 / 2 - @test sum(abs2, BMO.polarization(outside)) ≈ (1 - R_back) / 2 - # in the glass the power is n cos(θ) |E|² per area of the surface - @test n * cos(θ_t) * sum(abs2, BMO.polarization(inside)) ≈ cos(θ_i) / 2 - end - end -end - -end +module TestBeamsplitters + +using BeamletOptics +using LinearAlgebra +using Test + +const BMO = BeamletOptics + +const mm = 1e-3 + +@testset "Beamsplitters" begin + N0 = 1.5 + @testset "Testing RectangularPlateBeamsplitter with Beam" begin + # Init splitter + N0 = 1.5 + pbs = RectangularPlateBeamsplitter(36mm, 25mm, 1mm, ConstantRefractiveIndex(N0)) + system = System([pbs]) + beam = Beam([0, -50mm, 0], [0, 1, 0], 1e-6) + # Trace normally + zrotate3d!(pbs, deg2rad(45)) + # Keep geometry regressions from growing an unbounded beam tree in CI. + solve_system!(system, beam; depth_max=4) + + @testset "Test pos/dir" begin + @test position(pbs) == zeros(3) + @test orientation(pbs) ≈ orientation(pbs.substrate) + end + + @testset "Test children after tracing" begin + p = beam.rays + t = beam.children[1].rays + r = beam.children[2].rays + # no of rays + @test length(p) == 1 + @test length(t) == 2 + @test length(r) == 1 + # correct ref. index + @test all(BMO.refractive_index.(p) .== 1) + @test all(BMO.refractive_index.(t) .== [N0, 1]) + @test all(BMO.refractive_index.(r) .== 1) + # correct dir + @test BMO.direction(first(p)) ≈ BMO.direction(last(t)) + @test BMO.direction(first(r)) ≈ [1, 0, 0] + end + + # Solve again with the backside + zrotate3d!(pbs, π) + solve_system!(system, beam; depth_max=4) + + @testset "Test children after solving again" begin + p = beam.rays + t = beam.children[1].rays + r = beam.children[2].rays + # no of rays + @test length(p) == 2 + @test length(t) == 1 + @test length(r) == 2 + # correct ref. index + @test all(BMO.refractive_index.(p) .== [1, N0]) + @test all(BMO.refractive_index.(t) .== 1) + @test all(BMO.refractive_index.(r) .== [N0, 1]) + # correct dir + @test BMO.direction(first(p)) ≈ BMO.direction(last(t)) + @test BMO.direction(last(r)) ≈ [1, 0, 0] + end + end + + @testset "Testing CubeBeamsplitter with Beam" begin + # Init splitter + cbs = CubeBeamsplitter(25e-3, ConstantRefractiveIndex(N0)) + translate3d!(cbs, [0, 50mm, 0]) + system = System([cbs]) + beam = Beam([0, 0, 0], [0, 1, 0], 1e-6) + + @testset "Initial CBS tracing" begin + # Trace normally + solve_system!(system, beam) + # Test correct ray length, ref. indices, dirs + p = BMO.rays(beam) + t = BMO.rays(beam.children[1]) + r = BMO.rays(beam.children[2]) + + @test length(p) == 2 + @test length(r) == 2 + @test length(t) == 2 + @test BMO.refractive_index.(p) == [1, N0] + @test BMO.refractive_index.(t) == [N0, 1] + @test BMO.refractive_index.(r) == [N0, 1] + @test BMO.direction(last(t)) ≈ BMO.direction(first(p)) + @test BMO.direction(last(r)) ≈ [-1, 0, 0] + end + + @testset "Solve again after 45° CBS rotation" begin + # Solve again + zrotate3d!(cbs, π / 2) + solve_system!(system, beam) + + # Test correct ray dirs + p = BMO.rays(beam) + t = BMO.rays(beam.children[1]) + r = BMO.rays(beam.children[2]) + + @test BMO.direction(last(t)) ≈ BMO.direction(first(p)) + @test BMO.direction(last(t)) ≈ [0, 1, 0] + @test BMO.position(p[2]) ≈ [0, 50mm - 12.5mm, 0] + end + + @testset "Solve again, CBS backside" begin + # Solve again, backside + zrotate3d!(cbs, π / 2) + solve_system!(system, beam) + + # Test correct ray length, ref. indices, dirs + p = BMO.rays(beam) + t = BMO.rays(beam.children[1]) + r = BMO.rays(beam.children[2]) + + @test length(p) == 2 + @test length(r) == 2 + @test length(t) == 2 + @test BMO.refractive_index.(p) == [1, N0] + @test BMO.refractive_index.(t) == [N0, 1] + @test BMO.refractive_index.(r) == [N0, 1] + @test BMO.direction(last(t)) ≈ BMO.direction(first(p)) + @test BMO.direction(last(r)) ≈ [-1, 0, 0] + end + end + + @testset "Splitters and prisms with a constant refractive index" begin + prism = RightAnglePrism(25e-3, 20e-3, ConstantRefractiveIndex(N0)) + @test prism isa Prism + @test BMO.refractive_index(prism, 1e-6) == N0 + @test BMO.refractive_index(prism, 500e-9) == N0 + @test BMO.refractive_index(prism) == ConstantRefractiveIndex(N0) + for substrate in (RectangularPlateBeamsplitter(25e-3, 25e-3, 5e-3, ConstantRefractiveIndex(N0)).substrate, + RoundPlateBeamsplitter(25e-3, 5e-3, ConstantRefractiveIndex(N0)).substrate, + RectangularCompensatorPlate(25e-3, 25e-3, 5e-3, ConstantRefractiveIndex(N0))) + @test BMO.refractive_index(substrate) == ConstantRefractiveIndex(N0) + end + + cbs = CubeBeamsplitter(25e-3, ConstantRefractiveIndex(N0); reflectance = 0.3) + @test BMO.refractive_index(cbs, 1e-6) == N0 + @test BMO.refractive_index(cbs, 500e-9) == N0 + translate3d!(cbs, [0, 50mm, 0]) + beam = Beam([0, 0, 0], [0, 1, 0], 1e-6) + solve_system!(System([cbs]), beam) + @test BMO.refractive_index.(BMO.rays(beam)) == [1, N0] + @test length(beam.children) == 2 + end + + @testset "depth_max branch limiting" begin + beamsplitter = CubeBeamsplitter(25e-3, ConstantRefractiveIndex(N0)) + translate3d!(beamsplitter, [0, 50mm, 0]) + system = System([beamsplitter]) + + beam = Beam([0, 0, 0], [0, 1, 0], 1e-6) + solve_system!(system, beam; depth_max=0) + @test isempty(beam.children) + + beam = Beam([0, 0, 0], [0, 1, 0], 1e-6) + solve_system!(system, beam; depth_max=1) + @test length(beam.children) == 2 + end + + @testset "depth_max default" begin + beamsplitter = CubeBeamsplitter(25e-3, ConstantRefractiveIndex(N0)) + translate3d!(beamsplitter, [0, 50mm, 0]) + system = System([beamsplitter]) + beam = Beam([0, 0, 0], [0, 1, 0], 1e-6) + + @test BMO.get_default_depth_max() == 100 + solve_system!(system, beam) + @test length(beam.children) == 2 + end + + @testset "Field of a polarized ray behind the plate" begin + # the coating transmits half of the power into the glass, the uncoated back takes its + # Fresnel loss: the field of the transmitted ray is transverse and has this amplitude + n = 1.5 + θ_i = π / 4 + θ_t = asin(sin(θ_i) / n) + r_s = (cos(θ_i) - n * cos(θ_t)) / (cos(θ_i) + n * cos(θ_t)) + r_p = (n * cos(θ_i) - cos(θ_t)) / (n * cos(θ_i) + cos(θ_t)) + for (e, R_back) in (([0.0, 0, 1], r_s^2), ([1.0, 0, 0], r_p^2)) + pbs = RectangularPlateBeamsplitter(25e-3, 25e-3, 5e-3, ConstantRefractiveIndex(n)) + zrotate3d!(pbs, π / 4) + beam = Beam(PolarizedRay([0.0, -0.1, 0], [0.0, 1, 0], 1e-6, e)) + solve_system!(System([pbs]), beam) + transmitted, reflected = BMO.children(beam) + inside, outside = BMO.rays(transmitted) + @test BMO.refractive_index(inside) == n + for ray in (inside, outside, first(BMO.rays(reflected))) + @test abs(dot(BMO.direction(ray), BMO.polarization(ray))) < 1e-12 + end + @test sum(abs2, BMO.polarization(first(BMO.rays(reflected)))) ≈ 1 / 2 + @test sum(abs2, BMO.polarization(outside)) ≈ (1 - R_back) / 2 + # in the glass the power is n cos(θ) |E|² per area of the surface + @test n * cos(θ_t) * sum(abs2, BMO.polarization(inside)) ≈ cos(θ_i) / 2 + end + end +end + +end diff --git a/test/Components/TestDetector.jl b/test/Components/TestDetector.jl index 958991206..5cdfe76d4 100644 --- a/test/Components/TestDetector.jl +++ b/test/Components/TestDetector.jl @@ -134,7 +134,7 @@ end cs = UniformDiscSource([0, -10e-3, 0], [0, 1, 0], D, λ; num_rays) # test lens - lens = SphericalLens(R1, R2, l, d, x -> n) + lens = SphericalLens(R1, R2, l, d, ConstantRefractiveIndex(n)) # PSF detector x_shift = y_shift = -2mm @@ -192,7 +192,7 @@ end # Raytracing system (for all tests) pd_l = Detector(l) # n pd_s = Detector(l / 10) # n ÷ 10 - ln = ThinLens(R1, R2, d, nl) + ln = ThinLens(R1, R2, d, ConstantRefractiveIndex(nl)) translate3d!(pd_l, [0, z, 0]) translate3d!(pd_s, [0, z, 0]) translate3d!(ln, [0, z - f - thickness(ln) / 2, 0]) @@ -565,7 +565,7 @@ end @testset "Moving other objects keeps the hits" begin pd = Detector(50mm) translate3d!(pd, [0, 100mm, 0]) - lens = ThinLens(80mm, 80mm, 4mm, 1.5) + lens = ThinLens(80mm, 80mm, 4mm, ConstantRefractiveIndex(1.5)) translate3d!(lens, [0, 50mm, 0]) beam = Beam([0.0, 0, 0], [0.0, 1, 0]) solve_system!(System([lens, pd]), beam) @@ -662,12 +662,12 @@ end (GaussianBeamlet([0.0, 0, 0], [0.0, 1, 0], λ, w0), b -> b.chief), (AstigmaticGaussianBeamlet([0.0, 0, 0], [0.0, 1, 0], λ, w0), b -> b.c)) # lens in front of and behind the splitter - lens = ThinLens(200mm, 200mm, 5mm, 1.5) + lens = ThinLens(200mm, 200mm, 5mm, ConstantRefractiveIndex(1.5)) translate3d!(lens, [0, 30mm, 0]) bs = ThinBeamsplitter(25mm) zrotate3d!(bs, π / 4) translate3d!(bs, [0, 80mm, 0]) - lens2 = ThinLens(200mm, 200mm, 5mm, 1.5) + lens2 = ThinLens(200mm, 200mm, 5mm, ConstantRefractiveIndex(1.5)) translate3d!(lens2, [0, 130mm, 0]) solve_system!(System([lens, bs, lens2]), beam) @test length(BMO.children(beam)) == 2 diff --git a/test/Components/TestPolarizers.jl b/test/Components/TestPolarizers.jl index e221e25e9..ed87d16a2 100644 --- a/test/Components/TestPolarizers.jl +++ b/test/Components/TestPolarizers.jl @@ -188,12 +188,12 @@ const mm = 1e-3 end @testset "LinearPolarizer" begin - n = λ -> 1.5 + n = ConstantRefractiveIndex(1.5) lp = RoundLinearPolarizer(25.4mm, 1.6mm, 1.6mm, n) t = transmission_axis(lp) @test norm(t) ≈ 1 @test abs(dot(t, [1, 0, 0])) ≈ 1 atol = 1e-12 - @test BMO.refractive_index(RoundLinearPolarizer(25.4mm, 1.6mm, 1.6mm, 1.5), 1e-6) == 1.5 + @test BMO.refractive_index(RoundLinearPolarizer(25.4mm, 1.6mm, 1.6mm, ConstantRefractiveIndex(1.5)), 1e-6) == 1.5 for θ in 0:15:180 lp2 = RoundLinearPolarizer(25.4mm, 1.6mm, 1.6mm, n) @@ -225,7 +225,7 @@ const mm = 1e-3 end @testset "Linear polarizer - geometry and kinematics" begin - n = λ -> 1.5 + n = ConstantRefractiveIndex(1.5) tf = 1.6mm tb = 1.0mm @@ -246,7 +246,7 @@ const mm = 1e-3 end @testset "Linear polarizer - Malus law (forward and reverse)" begin - n = λ -> 1.5 + n = ConstantRefractiveIndex(1.5) tf = 1.6mm tb = 1.0mm D = 25.4mm @@ -323,7 +323,7 @@ const mm = 1e-3 end @testset "Linear polarizer - tilted plate offset" begin - n = λ -> 1.5 + n = ConstantRefractiveIndex(1.5) tf = 1.6mm tb = 1.0mm D = 25.4mm @@ -355,7 +355,7 @@ const mm = 1e-3 end @testset "Linear polarizer - beamlets" begin - n = λ -> 1.5 + n = ConstantRefractiveIndex(1.5) tf = 1.6mm tb = 1.0mm D = 25.4mm diff --git a/test/Components/TestPrisms.jl b/test/Components/TestPrisms.jl index 7507d531f..07dad69eb 100644 --- a/test/Components/TestPrisms.jl +++ b/test/Components/TestPrisms.jl @@ -89,22 +89,22 @@ const mm = 1e-3 end @testset "Constructors" begin - @test Prism([(0, 0), (1, 0), (0, 1)], 1, 1.5) isa Prism - eq = EquilateralPrism(20mm, 10mm, 1.5) + @test Prism([(0, 0), (1, 0), (0, 1)], 1, ConstantRefractiveIndex(1.5)) isa Prism + eq = EquilateralPrism(20mm, 10mm, ConstantRefractiveIndex(1.5)) @test eq isa Prism V = eq.shape.vertices @test sum(V) / 3 ≈ zeros(2) atol = 1e-15 # centroid at the origin @test V[3][2] > 0 && V[3][1] == 0 # apex along +y @test all(norm(V[i] - V[mod1(i + 1, 3)]) ≈ 20mm for i in 1:3) - @test EquilateralPrism(20f-3, 10f-3, 1.5).shape isa BMO.PolygonPrismSDF{Float32} - @test_throws ArgumentError EquilateralPrism(-20mm, 10mm, 1.5) - dove = DovePrism(50mm, 10mm, 10mm, 1.5) + @test EquilateralPrism(20f-3, 10f-3, ConstantRefractiveIndex(1.5)).shape isa BMO.PolygonPrismSDF{Float32} + @test_throws ArgumentError EquilateralPrism(-20mm, 10mm, ConstantRefractiveIndex(1.5)) + dove = DovePrism(50mm, 10mm, 10mm, ConstantRefractiveIndex(1.5)) @test dove isa Prism @test BMO.thickness(dove) ≈ 50mm - @test DovePrism(50f-3, 10mm, 10mm, 1.5) isa Prism # mixed element types - @test_throws ArgumentError DovePrism(10mm, 10mm, 10mm, 1.5) - @test_throws ArgumentError DovePrism(50mm, -10mm, 10mm, 1.5) - @test BMO.thickness(RightAnglePrism(20mm, 10mm, 1.5)) ≈ 20mm + @test DovePrism(50f-3, 10mm, 10mm, ConstantRefractiveIndex(1.5)) isa Prism # mixed element types + @test_throws ArgumentError DovePrism(10mm, 10mm, 10mm, ConstantRefractiveIndex(1.5)) + @test_throws ArgumentError DovePrism(50mm, -10mm, 10mm, ConstantRefractiveIndex(1.5)) + @test BMO.thickness(RightAnglePrism(20mm, 10mm, ConstantRefractiveIndex(1.5))) ≈ 20mm # a Dove prism does not deviate a beam on its axis (n = 1.5, parallel to the base) system = System([dove]) beam = Beam([-1mm, -40mm, 0], [0, 1, 0], 1e-6) diff --git a/test/E2E/TestAnalyticReferences.jl b/test/E2E/TestAnalyticReferences.jl index 3991fe99f..781dd076a 100644 --- a/test/E2E/TestAnalyticReferences.jl +++ b/test/E2E/TestAnalyticReferences.jl @@ -75,12 +75,12 @@ end n = 1.5 hs = [0.01mm, 1mm, 4mm, 8mm, 11mm] lenses = [ - "bi-convex" => (SphericalLens(50mm, -50mm, 5mm, 25mm, _ -> n), [(0.0, 50mm, n), (5mm, -50mm, 1.0)]), - "plano-convex" => (SphericalLens(50mm, Inf, 5mm, 25mm, _ -> n), [(0.0, 50mm, n), (5mm, Inf, 1.0)]), - "convex-plano" => (SphericalLens(Inf, -50mm, 5mm, 25mm, _ -> n), [(0.0, Inf, n), (5mm, -50mm, 1.0)]), - "bi-concave" => (SphericalLens(-50mm, 50mm, 3mm, 25mm, _ -> n), [(0.0, -50mm, n), (3mm, 50mm, 1.0)]), - "meniscus" => (SphericalLens(30mm, 60mm, 6mm, 25mm, _ -> n), [(0.0, 30mm, n), (6mm, 60mm, 1.0)]), - "cemented doublet" => (SphericalDoubletLens(40mm, -30mm, -120mm, 6mm, 2.5mm, 25mm, _ -> 1.62, _ -> 1.72), + "bi-convex" => (SphericalLens(50mm, -50mm, 5mm, 25mm, ConstantRefractiveIndex(n)), [(0.0, 50mm, n), (5mm, -50mm, 1.0)]), + "plano-convex" => (SphericalLens(50mm, Inf, 5mm, 25mm, ConstantRefractiveIndex(n)), [(0.0, 50mm, n), (5mm, Inf, 1.0)]), + "convex-plano" => (SphericalLens(Inf, -50mm, 5mm, 25mm, ConstantRefractiveIndex(n)), [(0.0, Inf, n), (5mm, -50mm, 1.0)]), + "bi-concave" => (SphericalLens(-50mm, 50mm, 3mm, 25mm, ConstantRefractiveIndex(n)), [(0.0, -50mm, n), (3mm, 50mm, 1.0)]), + "meniscus" => (SphericalLens(30mm, 60mm, 6mm, 25mm, ConstantRefractiveIndex(n)), [(0.0, 30mm, n), (6mm, 60mm, 1.0)]), + "cemented doublet" => (SphericalDoubletLens(40mm, -30mm, -120mm, 6mm, 2.5mm, 25mm, ConstantRefractiveIndex(1.62), ConstantRefractiveIndex(1.72)), [(0.0, 40mm, 1.62), (6mm, -30mm, 1.72), (8.5mm, -120mm, 1.0)]), ] @testset "$label" for (label, (lens, surfaces)) in lenses @@ -90,12 +90,12 @@ end end @testset "Sonnar 50 mm F1.5, 10 surfaces" begin # as in TestSonnarLens.jl - l1 = SphericalLens(69.21mm, 433.84mm, 9.33mm, 70mm, λ -> 1.671) + l1 = SphericalLens(69.21mm, 433.84mm, 9.33mm, 70mm, ConstantRefractiveIndex(1.671)) l234 = TripletLens(SphericalSurface(35.86mm, 60mm), SphericalSurface(85.87mm, 60mm), SphericalSurface(-646.31mm, 60mm), SphericalSurface(23.51mm, 40mm), - 11.81mm, 7.05mm, 1.9mm, λ -> 1.671, λ -> 1.4892, λ -> 1.7394) + 11.81mm, 7.05mm, 1.9mm, ConstantRefractiveIndex(1.671), ConstantRefractiveIndex(1.4892), ConstantRefractiveIndex(1.7394)) l567 = SphericalTripletLens(Inf, 51.09mm, -22.12mm, -103.13mm, 2.48mm, 19.81mm, 4.57mm, 42mm, - λ -> 1.5232, λ -> 1.6578, λ -> 1.5894) + ConstantRefractiveIndex(1.5232), ConstantRefractiveIndex(1.6578), ConstantRefractiveIndex(1.5894)) y2 = thickness(l1) + 0.38mm y3 = y2 + thickness(l234) + 13.0mm + 2.24mm translate3d!(l234, [0, y2, 0]) @@ -132,7 +132,7 @@ end # n sin(A/2) = sin((A + D_min)/2) for the apex angle A n, A = 1.5, π / 3 function deviation(θ) - prism = EquilateralPrism(20mm, 10mm, n) + prism = EquilateralPrism(20mm, 10mm, ConstantRefractiveIndex(n)) zrotate3d!(prism, θ) translate3d!(prism, [50mm, 0, 0]) beam = Beam([0.0, 2mm, 0], [1.0, 0, 0], 1e-6) @@ -161,7 +161,7 @@ end return pol === :s ? (c - n * ct) / (c + n * ct) : (n * c - ct) / (n * c + ct) end for θ in deg2rad.((0.0, 20.0, 45.0, 70.0)), pol in (:s, :p) - window = Lens(RectangularFlatSurface(30mm), RectangularFlatSurface(30mm), 5mm, _ -> n) + window = Lens(RectangularFlatSurface(30mm), RectangularFlatSurface(30mm), 5mm, ConstantRefractiveIndex(n)) zrotate3d!(window, θ) translate3d!(window, [0, 50mm, 0]) pd = Detector(40mm) @@ -182,7 +182,7 @@ end @testset "GaussianBeamlet behind a thick lens" begin # a narrow beam, such that the spherical aberration of the parabasal rays is negligible w0 = 0.15mm - lens = SphericalLens(50mm, -50mm, t, 25mm, _ -> n) + lens = SphericalLens(50mm, -50mm, t, 25mm, ConstantRefractiveIndex(n)) translate3d!(lens, [0, y_lens, 0]) g = GaussianBeamlet([0.0, 0, 0], [0.0, 1, 0], λ, w0) solve_system!(System([lens]), g) @@ -196,7 +196,7 @@ end @testset "AstigmaticGaussianBeamlet behind a cylindrical lens" begin # the curved axis focuses, the other one propagates as in a plate w0 = 0.5mm - lens = Lens(CylindricalSurface(30mm, 20mm, 25mm), t, _ -> n) + lens = Lens(CylindricalSurface(30mm, 20mm, 25mm), t, ConstantRefractiveIndex(n)) translate3d!(lens, [0, y_lens, 0]) g = AstigmaticGaussianBeamlet([0.0, 0, 0], [0.0, 1, 0], λ, w0) solve_system!(System([lens]), g) @@ -215,7 +215,7 @@ end λ = 632.8e-9 function power(dx) cm = 1e-2 - cbs = CubeBeamsplitter(25.4mm, _ -> 1.51509) + cbs = CubeBeamsplitter(25.4mm, ConstantRefractiveIndex(1.51509)) zrotate3d!(cbs, -π / 2) translate3d!(cbs, [0, 23.5cm, 0]) m1 = RoundPlanoMirror(25.4mm, 5mm) diff --git a/test/E2E/TestDoubleGaussLens.jl b/test/E2E/TestDoubleGaussLens.jl index 1c7be75da..9639b19f5 100644 --- a/test/E2E/TestDoubleGaussLens.jl +++ b/test/E2E/TestDoubleGaussLens.jl @@ -21,12 +21,12 @@ const mm = 1e-3 end # Based on https://www.pencilofrays.com/double-gauss-sonnar-comparison/ - l1 = SphericalLens(48.88mm, 182.96mm, 8.89mm, 52.3mm, λ -> 1.62286) + l1 = SphericalLens(48.88mm, 182.96mm, 8.89mm, 52.3mm, ConstantRefractiveIndex(1.62286)) l23 = SphericalDoubletLens(36.92mm, Inf, 23.06mm, 15.11mm, 2.31mm, - 45.11mm, λ -> 1.58565, λ -> 1.67764) + 45.11mm, ConstantRefractiveIndex(1.58565), ConstantRefractiveIndex(1.67764)) l45 = SphericalDoubletLens(-23.91mm, Inf, -36.92mm, 1.92mm, 7.77mm, - 40.01mm, λ -> 1.57046, λ -> 1.64128) - l6 = SphericalLens(1063.24mm, -48.88mm, 6.73mm, 45.11mm, λ -> 1.62286) + 40.01mm, ConstantRefractiveIndex(1.57046), ConstantRefractiveIndex(1.64128)) + l6 = SphericalLens(1063.24mm, -48.88mm, 6.73mm, 45.11mm, ConstantRefractiveIndex(1.62286)) # Calculate translation distances l_23 = thickness(l1) + 0.38mm l_45 = l_23 + thickness(l23) + 9.14mm + 13.36mm diff --git a/test/E2E/TestSonnarLens.jl b/test/E2E/TestSonnarLens.jl index 5b00e89a2..d004eb8b4 100644 --- a/test/E2E/TestSonnarLens.jl +++ b/test/E2E/TestSonnarLens.jl @@ -11,14 +11,14 @@ const mm = 1e-3 @testset "Sonnar lens" begin # Sonnar 50 mm F1.5, source: pencilofrays.com, Sonnar_50mmF1p5_FR837616.zmx, # scaled to f = 100 mm - l1 = SphericalLens(69.21mm, 433.84mm, 9.33mm, 70mm, λ -> 1.671) + l1 = SphericalLens(69.21mm, 433.84mm, 9.33mm, 70mm, ConstantRefractiveIndex(1.671)) # front triplet: last surface only 40 mm clear aperture -> constructed from surfaces l234 = TripletLens( SphericalSurface(35.86mm, 60mm), SphericalSurface(85.87mm, 60mm), SphericalSurface(-646.31mm, 60mm), SphericalSurface(23.51mm, 40mm), - 11.81mm, 7.05mm, 1.9mm, λ -> 1.671, λ -> 1.4892, λ -> 1.7394) + 11.81mm, 7.05mm, 1.9mm, ConstantRefractiveIndex(1.671), ConstantRefractiveIndex(1.4892), ConstantRefractiveIndex(1.7394)) l567 = SphericalTripletLens(Inf, 51.09mm, -22.12mm, -103.13mm, 2.48mm, 19.81mm, 4.57mm, 42mm, - λ -> 1.5232, λ -> 1.6578, λ -> 1.5894) + ConstantRefractiveIndex(1.5232), ConstantRefractiveIndex(1.6578), ConstantRefractiveIndex(1.5894)) l_234 = thickness(l1) + 0.38mm l_567 = l_234 + thickness(l234) + 13.0mm + 2.24mm translate3d!(l234, [0, l_234, 0]) diff --git a/test/Geometry/SDFs/TestAbstractSDF.jl b/test/Geometry/SDFs/TestAbstractSDF.jl index 7a1fd529a..b3dbfe956 100644 --- a/test/Geometry/SDFs/TestAbstractSDF.jl +++ b/test/Geometry/SDFs/TestAbstractSDF.jl @@ -145,7 +145,7 @@ BMO.sdf(c::CountingSDF, point) = (c.count += 1; BMO.sdf(c.shape, point)) i = BMO.intersect3d(ring, Ray([-10mm, 0, 0], [1.0, 0, 0])) @test i.t ≈ 20mm atol = 1e-9 # a lens is a union of parts: the faces between the parts within the lens are no exit - lens = BMO.shape(SphericalLens(50mm, -50mm, 5mm, 25.4mm, λ -> 1.5)) + lens = BMO.shape(SphericalLens(50mm, -50mm, 5mm, 25.4mm, ConstantRefractiveIndex(1.5))) front = BMO.intersect3d(lens, Ray([2mm, -50mm, 1mm], [0.0, 1, 0])) p = Point3(2mm, -50mm, 1mm) + front.t * Point3(0.0, 1, 0) d = normalize(Point3(0.1, 1, 0.05)) diff --git a/test/Geometry/SDFs/TestSDFBoundingSpheres.jl b/test/Geometry/SDFs/TestSDFBoundingSpheres.jl index 9415ed0a4..7eb71f393 100644 --- a/test/Geometry/SDFs/TestSDFBoundingSpheres.jl +++ b/test/Geometry/SDFs/TestSDFBoundingSpheres.jl @@ -26,7 +26,7 @@ Returns `label => shape` pairs that cover the 20 concrete SDF types, both signs a type has them, and the shapes of the public lens, prism and mirror constructors. """ function instances() - n = λ -> 1.5 + n = ConstantRefractiveIndex(1.5) asph = [0, 1e3] acyl = [0, 1.1926075e-5 * (1e3)^3, -2.9323497e-9 * (1e3)^5] # operands that are moved one by one, such that their frames differ from that of the composite @@ -52,9 +52,9 @@ function instances() [(0.0, 0.0), (20e-3, 0.0), (25e-3, 10e-3), (8e-3, 18e-3), (-4e-3, 9e-3)], 7e-3), "PolygonPrismSDF, right triangle" => BMO.PolygonPrismSDF( [(0.0, 0.0), (20e-3, 0.0), (0.0, 20e-3)], 30e-3), - "EquilateralPrism" => BMO.shape(EquilateralPrism(20e-3, 10e-3, 1.5)), - "DovePrism" => BMO.shape(DovePrism(50e-3, 10e-3, 10e-3, 1.5)), - "RightAnglePrism" => BMO.shape(RightAnglePrism(25e-3, 20e-3, 1.5)), + "EquilateralPrism" => BMO.shape(EquilateralPrism(20e-3, 10e-3, ConstantRefractiveIndex(1.5))), + "DovePrism" => BMO.shape(DovePrism(50e-3, 10e-3, 10e-3, ConstantRefractiveIndex(1.5))), + "RightAnglePrism" => BMO.shape(RightAnglePrism(25e-3, 20e-3, ConstantRefractiveIndex(1.5))), # SphericalLensSDF.jl "PlanoSurfaceSDF" => BMO.PlanoSurfaceSDF(5e-3, 25e-3), "SphereSDF" => BMO.SphereSDF(8e-3), diff --git a/test/Geometry/TestBoundingSphere.jl b/test/Geometry/TestBoundingSphere.jl index 928528277..8b578aca6 100644 --- a/test/Geometry/TestBoundingSphere.jl +++ b/test/Geometry/TestBoundingSphere.jl @@ -50,11 +50,11 @@ same(a, b) = !isnothing(a) && !isnothing(b) && length(a) == length(b) && BMO.obj # Whether the sphere `outer` encloses the sphere `inner` encloses(outer, inner) = norm(inner.pos - outer.pos) + inner.radius ≤ outer.radius + 1e-12 -sphere_prism(radius, bound = radius) = BMO.Prism(WrappedSDF(BMO.SphereSDF(radius), bound), λ -> 1.5) +sphere_prism(radius, bound = radius) = BMO.Prism(WrappedSDF(BMO.SphereSDF(radius), bound), ConstantRefractiveIndex(1.5)) @testset "Bounding sphere" begin radius = 10e-3 - n = λ -> 1.5 + n = ConstantRefractiveIndex(1.5) @testset "Shapes" begin plain = WrappedSDF(BMO.SphereSDF(radius)) @@ -455,7 +455,7 @@ sphere_prism(radius, bound = radius) = BMO.Prism(WrappedSDF(BMO.SphereSDF(radius r = 10.0f-3 shape = WrappedSDF(BMO.SphereSDF(r), r) translate3d!(shape.shape, Float32[1, 2, 3]) - prism = BMO.Prism(shape, λ -> 1.5f0) + prism = BMO.Prism(shape, ConstantRefractiveIndex(1.5f0)) system = System([prism]) c = Float64.(position(shape)) @test BMO.bounding_sphere_of(shape) isa BMO.SingleBoundingSphere{Float32} diff --git a/test/Lenses/TestAsphericalLenses.jl b/test/Lenses/TestAsphericalLenses.jl index 054d8ef3c..03a5652b0 100644 --- a/test/Lenses/TestAsphericalLenses.jl +++ b/test/Lenses/TestAsphericalLenses.jl @@ -35,7 +35,7 @@ const mm = 1e-3 lens = Lens( EvenAsphericalSurface(R, d, k, A), ct, - x -> n + ConstantRefractiveIndex(n) ) system = System(lens) @@ -92,7 +92,7 @@ const mm = 1e-3 # A meniscus, whose center thickness does not exceed the sagitta of the convex surface, # is only supported for spherical surfaces d = 25.4mm - n = λ -> 1.5 + n = ConstantRefractiveIndex(1.5) asphere(R) = EvenAsphericalSurface(R, d, 0.0, [0, 1e-7 * (1e3)^3]) @test Lens(SphericalSurface(20mm, d), SphericalSurface(21mm, d), 1mm, n) isa Lens @test_throws ArgumentError Lens(SphericalSurface(20mm, d), asphere(21mm), 1mm, n) @@ -120,7 +120,7 @@ const mm = 1e-3 -0.34529 * (1e3)^11, 5.9938 * (1e3)^13] # coeffs ), 0.72mm, # center thickness - n -> 1.580200 + ConstantRefractiveIndex(1.580200) ) L2 = Lens( @@ -141,7 +141,7 @@ const mm = 1e-3 -0.080329 * (1e3)^15] # coeffs ), 0.55mm, # center_thickness - n -> 1.804700 + ConstantRefractiveIndex(1.804700) ) translate3d!(L2, [0, thickness(L1) + 0.39mm, 0]) @@ -164,7 +164,7 @@ const mm = 1e-3 1.5264e-5 * (1e3)^15] # coeffs ), 0.7mm, # center_thickness - n -> 1.580200 + ConstantRefractiveIndex(1.580200) ) translate_to3d!(L3, position(L2)) @@ -173,7 +173,7 @@ const mm = 1e-3 Filt = Lens( CircularFlatSurface(4.2mm), 0.15mm, - n -> 1.516800 + ConstantRefractiveIndex(1.516800) ) translate_to3d!(Filt, position(L3)) @@ -182,7 +182,7 @@ const mm = 1e-3 Cover = Lens( CircularFlatSurface(4.9mm), 0.5mm, - n -> 1.469200 + ConstantRefractiveIndex(1.469200) ) translate_to3d!(Cover, position(Filt)) translate3d!(Cover, [0, thickness(Filt) + 0.18mm, 0]) diff --git a/test/Lenses/TestCylindricalLenses.jl b/test/Lenses/TestCylindricalLenses.jl index b47347f64..495705842 100644 --- a/test/Lenses/TestCylindricalLenses.jl +++ b/test/Lenses/TestCylindricalLenses.jl @@ -29,7 +29,7 @@ const mm = 1e-3 lens = Lens( CylindricalSurface(r, d, h), ct, - n -> 1.517 + ConstantRefractiveIndex(1.517) ) # test lens thickness @@ -47,7 +47,7 @@ const mm = 1e-3 lens = Lens( CylindricalSurface(r, d, h), ct, - n -> 1.517 + ConstantRefractiveIndex(1.517) ) # test lens thickness @@ -70,7 +70,7 @@ const mm = 1e-3 [0, 1.1926075e-5*(1e3)^3, -2.9323497e-9*(1e3)^5, -1.8718889e-11*(1e3)^7, -1.7009961e-14*(1e3)^9, 3.5481542e-17*(1e3)^11, 6.5241296e-20*(1e3)^13] ), ct, - n -> 1.777 + ConstantRefractiveIndex(1.777) ) # test lens thickness @@ -94,24 +94,25 @@ const mm = 1e-3 [0, 1.1926075e-5*(1e3)^3, -2.9323497e-9*(1e3)^5, -1.8718889e-11*(1e3)^7, -1.7009961e-14*(1e3)^9, 3.5481542e-17*(1e3)^11, 6.5241296e-20*(1e3)^13] ), ct, - n -> 1.777 + ConstantRefractiveIndex(1.777) ) # test lens thickness @test thickness(lens) ≈ ct end - @testset "Number as refractive index" begin + @testset "Refractive index argument" begin + n = ConstantRefractiveIndex(1.517) cyl = CylindricalSurface(5.2mm, 10mm, 20mm) + rect = RectangularFlatSurface(10mm) for lens in ( - Lens(CylindricalSurface(50mm, 10mm, 20mm), CylindricalSurface(-50mm, 10mm, 20mm), - 2mm, 1.517), - Lens(cyl, 5.9mm, 1.517), - Lens(RectangularFlatSurface(10mm), RectangularFlatSurface(10mm), 2mm, 1.517)) - @test BMO.refractive_index(lens) == ConstantRefractiveIndex(1.517) + Lens(CylindricalSurface(50mm, 10mm, 20mm), CylindricalSurface(-50mm, 10mm, 20mm), 2mm, n), + Lens(cyl, 5.9mm, n), + Lens(rect, rect, 2mm, n)) + @test BMO.refractive_index(lens) === n end - # same back focal length as the closure above - @test working_distance(Lens(cyl, 5.9mm, 1.517), 0.05 * 10mm / 2) ≈ 6.1mm atol=1e-4 + @test_throws MethodError Lens(cyl, 5.9mm, 1.517) + @test_throws MethodError Lens(rect, rect, 2mm, λ -> 1.517) end end diff --git a/test/Lenses/TestSphericalLenses.jl b/test/Lenses/TestSphericalLenses.jl index 2863ac080..cbf616f57 100644 --- a/test/Lenses/TestSphericalLenses.jl +++ b/test/Lenses/TestSphericalLenses.jl @@ -19,7 +19,7 @@ const mm = 1e-3 r = only(rings) return BMO.position(r)[2] - r.hthickness, BMO.position(r)[2] + r.hthickness end - n = λ -> 1.5 + n = ConstantRefractiveIndex(1.5) # biconvex: edge from s to T - s lens = Lens(SphericalSurface(R, d, md), SphericalSurface(-R, d), 5mm, n) lo, hi = ring_extent(lens) @@ -52,7 +52,7 @@ const mm = 1e-3 nl = 1.5 tl = BMO.ThinLensSDF(R1, R2, 0.1) translate3d!(tl, [0, -thickness(tl) / 2, 0]) - p = Lens(tl, x -> 1.5) + p = Lens(tl, ConstantRefractiveIndex(1.5)) system = System(p) # compare numerical and analytical focal length @@ -76,32 +76,25 @@ const mm = 1e-3 end end - @testset "Number as refractive index" begin + @testset "Refractive index argument" begin sphere(r) = SphericalSurface(r, BMO.inch) - @test BMO.refractive_index(SphericalLens(50mm, -50mm, 5mm)) isa ConstantRefractiveIndex - @test BMO.refractive_index(ThinLens(50mm, -50mm, BMO.inch, 1.5)) isa ConstantRefractiveIndex - @test BMO.refractive_index(Lens(sphere(50mm), 5mm, 1.5)) isa ConstantRefractiveIndex + n = ConstantRefractiveIndex(1.5) + @test BMO.refractive_index(SphericalLens(50mm, -50mm, 5mm)) == n + @test BMO.refractive_index(ThinLens(50mm, -50mm, BMO.inch, n)) === n + @test BMO.refractive_index(Lens(sphere(50mm), 5mm, n)) === n flat = CircularFlatSurface(BMO.inch) - @test BMO.refractive_index(Lens(flat, flat, 5mm, 1.5)) isa ConstantRefractiveIndex - dl = DoubletLens(sphere(50mm), sphere(-40mm), sphere(100mm), 6mm, 3mm, 1.5, λ -> 1.7) - @test BMO.refractive_index(dl.front) isa ConstantRefractiveIndex - tl = TripletLens(sphere(50mm), sphere(-40mm), sphere(100mm), sphere(-60mm), 8mm, 3mm, 6mm, - 1.5, 1.6, 1.5) - @test all(BMO.refractive_index(l) isa ConstantRefractiveIndex for l in BMO.shape(tl)) - # a number traces exactly like the equivalent closure, also on a moved lens - function trace_moved_lens(n) - lens = SphericalLens(50mm, -50mm, 5mm, BMO.inch, n) - zrotate3d!(lens, deg2rad(10)) - translate3d!(lens, [1mm, 20mm, -2mm]) - beam = Beam(Ray([0, -0.05, 5mm], [0, 1, 0], 1e-6)) - solve_system!(System(lens), beam) - return last(rays(beam)), length(rays(beam)) + @test BMO.refractive_index(Lens(flat, flat, 5mm, n)) === n + dl = DoubletLens(sphere(50mm), sphere(-40mm), sphere(100mm), 6mm, 3mm, n, ConstantRefractiveIndex(1.7)) + @test BMO.refractive_index(dl.front) === n + # anything else than an AbstractRefractiveIndex is rejected by the constructor called + for wrong in (1.5, λ -> 1.5, "N-BK7") + err = try + SphericalLens(50mm, -50mm, 5mm, BMO.inch, wrong) + catch e + e + end + @test err isa MethodError && err.f === SphericalLens end - r_number, num_rays = trace_moved_lens(1.5) - r_closure, _ = trace_moved_lens(λ -> 1.5) - @test num_rays == 3 - @test position(r_number) == position(r_closure) - @test direction(r_number) == direction(r_closure) end @testset "Testing lens constructor" begin @@ -205,8 +198,8 @@ const mm = 1e-3 # Every ray/beam type the solver can produce must trace through a DoubletLens. # Regression: the chief ray of an AstigmaticGaussianBeamlet is a PolarizedRay, # which is not a subtype of Ray, so the doublet interact3d must accept AbstractRay. - n1 = λ -> 1.5 - n2 = λ -> 1.7 + n1 = ConstantRefractiveIndex(1.5) + n2 = ConstantRefractiveIndex(1.7) λ = 1e-6 w0 = 1mm pos = [0.0, 0, 0] @@ -261,8 +254,8 @@ const mm = 1e-3 @testset "Equal-index invariant" begin tl = SphericalTripletLens(50mm, -40mm, 100mm, -60mm, 8mm, 3mm, 6mm, BMO.inch, - λ -> 1.5, λ -> 1.5, λ -> 1.5) - sl = SphericalLens(50mm, -60mm, 17mm, BMO.inch, λ -> 1.5) + ConstantRefractiveIndex(1.5), ConstantRefractiveIndex(1.5), ConstantRefractiveIndex(1.5)) + sl = SphericalLens(50mm, -60mm, 17mm, BMO.inch, ConstantRefractiveIndex(1.5)) for h in (0.0, 1e-6, 1mm, 5mm, -7mm, 10mm) sys_tl = System([tl]) sys_sl = System([sl]) @@ -278,8 +271,8 @@ const mm = 1e-3 end @testset "Doublet equivalence" begin - n1 = λ -> 1.5 - n2 = λ -> 1.7 + n1 = ConstantRefractiveIndex(1.5) + n2 = ConstantRefractiveIndex(1.7) tl = SphericalTripletLens(33.3mm, -22.3mm, -100mm, -291.1mm, 9mm, 1mm, 1.5mm, 25.4mm, n1, n2, n2) dl = SphericalDoubletLens(33.3mm, -22.3mm, -291.1mm, 9mm, 2.5mm, 25.4mm, n1, n2) @@ -299,7 +292,7 @@ const mm = 1e-3 @testset "Kinematics" begin tl = SphericalTripletLens(50mm, -40mm, 100mm, -60mm, 8mm, 3mm, 6mm, BMO.inch, - λ -> 1.5, λ -> 1.7, λ -> 1.6) + ConstantRefractiveIndex(1.5), ConstantRefractiveIndex(1.7), ConstantRefractiveIndex(1.6)) translate3d!(tl, [0.05, 0.05, 0.05]) xrotate3d!(tl, deg2rad(-60)) zrotate3d!(tl, deg2rad(45)) @@ -314,10 +307,10 @@ const mm = 1e-3 end @testset "Composition from individual lenses" begin - front = SphericalLens(35.86mm, 85.87mm, 11.81mm, 60mm, 1.671) - middle = SphericalLens(85.87mm, -646.31mm, 7.05mm, 60mm, 1.4892) + front = SphericalLens(35.86mm, 85.87mm, 11.81mm, 60mm, ConstantRefractiveIndex(1.671)) + middle = SphericalLens(85.87mm, -646.31mm, 7.05mm, 60mm, ConstantRefractiveIndex(1.4892)) back = Lens(SphericalSurface(-646.31mm, 60mm), SphericalSurface(23.51mm, 40mm), - 1.9mm, λ -> 1.7394) + 1.9mm, ConstantRefractiveIndex(1.7394)) translate3d!(middle, [0, thickness(front), 0]) translate3d!(back, [0, thickness(front) + thickness(middle), 0]) tl = TripletLens(front, middle, back) @@ -329,9 +322,9 @@ const mm = 1e-3 # Every ray/beam type the solver can produce must trace through a TripletLens. # Regression: the chief ray of an AstigmaticGaussianBeamlet is a PolarizedRay, # which is not a subtype of Ray, so the triplet interact3d must accept AbstractRay. - n1 = λ -> 1.5 - n2 = λ -> 1.7 - n3 = λ -> 1.6 + n1 = ConstantRefractiveIndex(1.5) + n2 = ConstantRefractiveIndex(1.7) + n3 = ConstantRefractiveIndex(1.6) λ = 1e-6 w0 = 1mm pos = [0.0, 0, 0] diff --git a/test/Lenses/TestSurfaces.jl b/test/Lenses/TestSurfaces.jl index eccd74411..6f98e7728 100644 --- a/test/Lenses/TestSurfaces.jl +++ b/test/Lenses/TestSurfaces.jl @@ -31,7 +31,7 @@ const mm = 1e-3 lens = Lens( SphericalSurface(r1, d), l, - n -> 1.458 + ConstantRefractiveIndex(1.458) ) # test lens thickness @@ -50,7 +50,7 @@ const mm = 1e-3 SphericalSurface(r1, d), SphericalSurface(r2, d), l, - n -> 1.517 + ConstantRefractiveIndex(1.517) ) shape = lens.shape @@ -70,7 +70,7 @@ const mm = 1e-3 SphericalSurface(r1, d), SphericalSurface(r2, d), l, - n -> 1.517 + ConstantRefractiveIndex(1.517) ) shape = lens.shape @@ -89,7 +89,7 @@ const mm = 1e-3 SphericalSurface(r1, d), SphericalSurface(r2, d), l, - n -> 1.517 + ConstantRefractiveIndex(1.517) ) shape = lens.shape @@ -108,7 +108,7 @@ const mm = 1e-3 SphericalSurface(r1, d), SphericalSurface(r2, d), l, - n -> 1.517 + ConstantRefractiveIndex(1.517) ) shape = lens.shape @@ -127,7 +127,7 @@ const mm = 1e-3 SphericalSurface(r1, d), SphericalSurface(r2, d), l, - n -> 1.517 + ConstantRefractiveIndex(1.517) ) shape = lens.shape @@ -146,7 +146,7 @@ const mm = 1e-3 SphericalSurface(r1, d), SphericalSurface(r2, d), l, - n -> 1.517 + ConstantRefractiveIndex(1.517) ) shape = lens.shape @@ -166,7 +166,7 @@ const mm = 1e-3 SphericalSurface(r1, d), SphericalSurface(r2, d), l, - n -> 1.517 + ConstantRefractiveIndex(1.517) ) shape = lens.shape @@ -209,9 +209,9 @@ const mm = 1e-3 @testset "Doublet and triplet construction from surfaces" begin d = 25.4mm hs = (0.0, 1e-6, 1mm, 5mm, -7mm, 10mm) - n1 = λ -> 1.5 - n2 = λ -> 1.7 - n3 = λ -> 1.6 + n1 = ConstantRefractiveIndex(1.5) + n2 = ConstantRefractiveIndex(1.7) + n3 = ConstantRefractiveIndex(1.6) # Even aspheric coefficients for r², r⁴ and r⁶ in SI units A = [0, 2e-7 * (1e3)^3, 1e-11 * (1e3)^5] diff --git a/test/Rendering/TestLiveBeams.jl b/test/Rendering/TestLiveBeams.jl index ad91e784d..27cbd7eae 100644 --- a/test/Rendering/TestLiveBeams.jl +++ b/test/Rendering/TestLiveBeams.jl @@ -66,7 +66,7 @@ end # Lens + mirror system function _lens_mirror_fixture() - lens = SphericalLens(50e-3, -50e-3, 10e-3, 25e-3, 1.5168) + lens = SphericalLens(50e-3, -50e-3, 10e-3, 25e-3, ConstantRefractiveIndex(1.5168)) mir = RoundPlanoMirror(25.4e-3, 5e-3) translate3d!(mir, [0, 60e-3, 0]) zrotate3d!(mir, deg2rad(45)) @@ -107,7 +107,7 @@ end # AGB through a thin lens, see test/Rendering/TestRenderPolarization.jl function _agb_lens_fixture() agb = AstigmaticGaussianBeamlet([0, 0, 0], [0, 1, 0], 1000e-9, 5e-3; support = [0, 0, 1]) - l = ThinLens(50e-3, 50e-3, BMO.inch, 1.5) + l = ThinLens(50e-3, 50e-3, BMO.inch, ConstantRefractiveIndex(1.5)) translate3d!(l, [0, 50e-3, 0]) sys = System([l]) solve_system!(sys, agb; check_invariant = false) diff --git a/test/Rendering/TestRenderBoundingSphere.jl b/test/Rendering/TestRenderBoundingSphere.jl index b1102441e..dbaa6e545 100644 --- a/test/Rendering/TestRenderBoundingSphere.jl +++ b/test/Rendering/TestRenderBoundingSphere.jl @@ -127,18 +127,18 @@ types(plots) = map(Makie.plotfunc, plots) # A lens whose shape has a bounding sphere on the base branch already, such that the tests do not # depend on which other shape types have one -meniscus_lens() = Lens(BMO.MeniscusLensSDF(50mm, 80mm, 5mm, 25.4mm), λ -> 1.5) +meniscus_lens() = Lens(BMO.MeniscusLensSDF(50mm, 80mm, 5mm, 25.4mm), ConstantRefractiveIndex(1.5)) function test_system() - lens = SphericalLens(50mm, -50mm, 10mm, 25.4mm, 1.5) + lens = SphericalLens(50mm, -50mm, 10mm, 25.4mm, ConstantRefractiveIndex(1.5)) meniscus = meniscus_lens() - doublet = SphericalDoubletLens(33.3mm, -22.3mm, -291.1mm, 9mm, 2.5mm, 25.4mm, 1.5, 1.6) + doublet = SphericalDoubletLens(33.3mm, -22.3mm, -291.1mm, 9mm, 2.5mm, 25.4mm, ConstantRefractiveIndex(1.5), ConstantRefractiveIndex(1.6)) mirror = RoundPlanoMirror(25.4mm, 5mm) - cube = CubeBeamsplitter(25mm, λ -> 1.5) - plate = RectangularPlateBeamsplitter(36mm, 25mm, 1mm, λ -> 1.5) + cube = CubeBeamsplitter(25mm, ConstantRefractiveIndex(1.5)) + plate = RectangularPlateBeamsplitter(36mm, 25mm, 1mm, ConstantRefractiveIndex(1.5)) filter = RoundPolarizationFilter(10mm) - polarizer = RoundLinearPolarizer(25.4mm, 1.6mm, 1.6mm, λ -> 1.5) - prism = RightAnglePrism(20mm, 10mm, 1.5) + polarizer = RoundLinearPolarizer(25.4mm, 1.6mm, 1.6mm, ConstantRefractiveIndex(1.5)) + prism = RightAnglePrism(20mm, 10mm, ConstantRefractiveIndex(1.5)) detector = Detector(10mm) group = ObjectGroup([RoundPlanoMirror(20mm, 4mm), meniscus_lens()]) objs = [lens, meniscus, doublet, mirror, cube, plate, filter, polarizer, prism, detector, group] @@ -323,7 +323,7 @@ end @test length(sphere_plots(plots)) == 1 # polarizers with their own keywords - for pol in (RoundPolarizationFilter(10mm), RoundLinearPolarizer(25.4mm, 1.6mm, 1.6mm, λ -> 1.5)) + for pol in (RoundPolarizationFilter(10mm), RoundLinearPolarizer(25.4mm, 1.6mm, 1.6mm, ConstantRefractiveIndex(1.5))) plain = rendered_plots(pol; show_transmission_axis = false) plots = rendered_plots(pol; show_transmission_axis = false, show_bounding_sphere = true) @test length(plots) == length(plain) + length(spheres(pol)) diff --git a/test/Rendering/TestRenderLive.jl b/test/Rendering/TestRenderLive.jl index 5b11dc34c..de3949eb8 100644 --- a/test/Rendering/TestRenderLive.jl +++ b/test/Rendering/TestRenderLive.jl @@ -147,7 +147,7 @@ Base.delete!(h::_Combined, oh::BMO.AbstractObjectRenderHandle) = (filter!(c -> c end @testset "MultiShape (CubeBeamsplitter): rigid update + non-rigid fallback" begin - cbs = CubeBeamsplitter(0.02, λ -> 1.5) + cbs = CubeBeamsplitter(0.02, ConstantRefractiveIndex(1.5)) h = live_render!(ax, cbs) n = length(h.plots) @test count(p -> p isa Makie.Mesh, h.plots) == 2 # merged front and back, coating @@ -337,7 +337,7 @@ Base.delete!(h::_Combined, oh::BMO.AbstractObjectRenderHandle) = (filter!(c -> c end @testset "pick_object after a fallback rerender" begin - cbs = CubeBeamsplitter(0.02, λ -> 1.5) + cbs = CubeBeamsplitter(0.02, ConstantRefractiveIndex(1.5)) h = live_render!(ax, System([cbs])) old_plots = copy(h.handles[1].plots) translate3d!(cbs.front, [0.0, 0.0, 0.01]) diff --git a/test/Rendering/TestRenderLook.jl b/test/Rendering/TestRenderLook.jl index e363d674b..0c5a9efce 100644 --- a/test/Rendering/TestRenderLook.jl +++ b/test/Rendering/TestRenderLook.jl @@ -131,7 +131,7 @@ end @test length(edge_plots(rendered_plots(RoundPlanoMirror(25e-3, 5e-3); edges = true))) == 1 @test isempty(edge_plots(rendered_plots(lens; edges = false))) # coating and prisms of a beamsplitter - @test length(edge_plots(rendered_plots(CubeBeamsplitter(20e-3, λ -> 1.5)))) == 1 + @test length(edge_plots(rendered_plots(CubeBeamsplitter(20e-3, ConstantRefractiveIndex(1.5))))) == 1 @test length(edge_plots(rendered_plots(ThinBeamsplitter(10e-3, 10e-3)))) == 1 # MultiShape: only the parts with edges contribute, mechanics parts never by default @@ -176,7 +176,7 @@ end # component classes for (obj, class) in (RoundPlanoMirror(25e-3, 5e-3) => :reflective, Retroreflector(25e-3) => :reflective, - Prism(BMO.BoxSDF(10e-3, 10e-3, 10e-3), λ -> 1.5) => :refractive, + Prism(BMO.BoxSDF(10e-3, 10e-3, 10e-3), ConstantRefractiveIndex(1.5)) => :refractive, ThinBeamsplitter(10e-3, 10e-3) => :coating, RoundPolarizationFilter(25e-3) => :polarizer, Detector(10e-3) => :detector, @@ -187,7 +187,7 @@ end end # LinearPolarizer: substrates refractive, film with the polarizer material, edges switchable - lipo = RoundLinearPolarizer(25e-3, 2e-3, 2e-3, λ -> 1.5) + lipo = RoundLinearPolarizer(25e-3, 2e-3, 2e-3, ConstantRefractiveIndex(1.5)) meshes = mesh_plots(rendered_plots(lipo)) @test count(p -> p.color[] == Makie.to_color(mats[:polarizer].color), meshes) == 1 @test count(p -> p.color[] == Makie.to_color(mats[:refractive].color), meshes) == 2 @@ -196,7 +196,7 @@ end @test n_lines - n_lines_noedges == 3 # one edge plot per part, the axis lines stay # MultiShape: materials per part, explicit kwargs for all parts - cbs = CubeBeamsplitter(20e-3, λ -> 1.5) + cbs = CubeBeamsplitter(20e-3, ConstantRefractiveIndex(1.5)) prisms, coating = mesh_plots(rendered_plots(cbs)) check_material(prisms, mats[:refractive]) check_material(coating, mats[:coating]) @@ -209,7 +209,7 @@ end end @testset "feature edges" begin - box = Prism(BMO.BoxSDF(20e-3, 10e-3, 30e-3), λ -> 1.5) + box = Prism(BMO.BoxSDF(20e-3, 10e-3, 30e-3), ConstantRefractiveIndex(1.5)) zrotate3d!(box, deg2rad(35)) xrotate3d!(box, deg2rad(-20)) plots = rendered_plots(box) @@ -225,7 +225,7 @@ end # two circles, no edges along the smooth side wall s = BMO.CylinderSDF(5e-3, 2e-3) - cyl = Prism(s, λ -> 1.5) + cyl = Prism(s, ConstantRefractiveIndex(1.5)) # mechanics: no edges by default, a mesh (e.g. loaded from a file) keeps its smooth shading mech = NonInteractableObject(BMO.CylinderSDF(5e-3, 2e-3)) @@ -267,20 +267,20 @@ end @test length(segments(only(edge_plots(rendered_plots(Detector(10e-3)))))) == 4 # coincident flat faces (the bases of the caps of a thin lens) have no edges, only the rim - tl = ThinLens(34.9e-3, -34.9e-3, 25.4e-3, 1.5) + tl = ThinLens(34.9e-3, -34.9e-3, 25.4e-3, ConstantRefractiveIndex(1.5)) segs = segments(only(edge_plots(rendered_plots(tl)))) @test !isempty(segs) @test all(abs(norm(p[[1, 3]]) - 12.7e-3) < 1e-6 for seg in segs for p in seg) # edges = false - for obj in (box, cyl, CubeBeamsplitter(20e-3, λ -> 1.5), System([box, cyl])) + for obj in (box, cyl, CubeBeamsplitter(20e-3, ConstantRefractiveIndex(1.5)), System([box, cyl])) plots = rendered_plots(obj; edges = false) @test isempty(edge_plots(plots)) @test all(p -> p isa Makie.Mesh, plots) end # MultiShape: one edge plot per object, after the meshes - plots = rendered_plots(CubeBeamsplitter(20e-3, λ -> 1.5)) + plots = rendered_plots(CubeBeamsplitter(20e-3, ConstantRefractiveIndex(1.5))) @test length(edge_plots(plots)) == 1 @test plots[end] isa Makie.Lines @@ -305,7 +305,7 @@ end end @testset "cemented interfaces" begin - dl = SphericalDoubletLens(87.9e-3, -105.6e-3, -1000, 6e-3, 3e-3, 25.4e-3, 1.5, 1.6) + dl = SphericalDoubletLens(87.9e-3, -105.6e-3, -1000, 6e-3, 3e-3, 25.4e-3, ConstantRefractiveIndex(1.5), ConstantRefractiveIndex(1.6)) zrotate3d!(dl, deg2rad(35)) translate3d!(dl, [0.01, -0.02, 0.015]) plots = rendered_plots(dl) @@ -336,7 +336,7 @@ end @test isempty(edge_plots(rendered_plots(dl; edges = false))) # triplet: both interfaces in one mesh - tl = SphericalTripletLens(60e-3, 25e-3, -25e-3, -60e-3, 3e-3, 8e-3, 3e-3, 25.4e-3, 1.5, 1.6, 1.5) + tl = SphericalTripletLens(60e-3, 25e-3, -25e-3, -60e-3, 3e-3, 8e-3, 3e-3, 25.4e-3, ConstantRefractiveIndex(1.5), ConstantRefractiveIndex(1.6), ConstantRefractiveIndex(1.5)) outer, interface = mesh_plots(rendered_plots(tl)) s1, s2, s3 = BMO.shape(tl.front), BMO.shape(tl.middle), BMO.shape(tl.back) pts = points(interface) @@ -348,7 +348,7 @@ end @test is_closed(points(outer), faces(outer), tol) # other composite objects have no interface mesh - @test length(mesh_plots(rendered_plots(CubeBeamsplitter(20e-3, λ -> 1.5)))) == 2 + @test length(mesh_plots(rendered_plots(CubeBeamsplitter(20e-3, ConstantRefractiveIndex(1.5))))) == 2 @test length(mesh_plots(rendered_plots(SphericalLens(34.9e-3, -34.9e-3, 6.8e-3, 25.4e-3)))) == 1 end diff --git a/test/Rendering/TestRenderPolarization.jl b/test/Rendering/TestRenderPolarization.jl index b74276d78..cd12c157a 100644 --- a/test/Rendering/TestRenderPolarization.jl +++ b/test/Rendering/TestRenderPolarization.jl @@ -14,7 +14,7 @@ const BMO = BeamletOptics # Shared fixture: 5 mm collimated AGB through an f = 50 mm ThinLens -- the # case that exposes the unbounded focus gain (afb6e18). agb = AstigmaticGaussianBeamlet([0, 0, 0], [0, 1, 0], 1000e-9, 5e-3; support = [0, 0, 1]) - l = ThinLens(50e-3, 50e-3, BMO.inch, 1.5) + l = ThinLens(50e-3, 50e-3, BMO.inch, ConstantRefractiveIndex(1.5)) translate3d!(l, [0, 50e-3, 0]) solve_system!(System([l]), agb; check_invariant = false) flen = 0.12 diff --git a/test/Rendering/TestTessellation.jl b/test/Rendering/TestTessellation.jl index 4ebd6144d..8fa058c09 100644 --- a/test/Rendering/TestTessellation.jl +++ b/test/Rendering/TestTessellation.jl @@ -181,12 +181,12 @@ BMO.sdf(s::BlobSDF, p) = norm(BMO._world_to_sdf(s, p)) - 5e-3 lenses = [ "bi-convex" => SphericalLens(34.9e-3, -34.9e-3, 6.8e-3, 25.4e-3), "bi-concave" => SphericalLens(-50e-3, 50e-3, 3e-3, 25.4e-3), - "plano-convex" => Lens(SphericalSurface(25.8e-3, 25.4e-3), CircularFlatSurface(25.4e-3), 5.3e-3, λ -> 1.5), - "thin" => ThinLens(34.9e-3, 34.9e-3, 25.4e-3, 1.5), + "plano-convex" => Lens(SphericalSurface(25.8e-3, 25.4e-3), CircularFlatSurface(25.4e-3), 5.3e-3, ConstantRefractiveIndex(1.5)), + "thin" => ThinLens(34.9e-3, 34.9e-3, 25.4e-3, ConstantRefractiveIndex(1.5)), "meniscus (left)" => SphericalLens(20e-3, 30e-3, 2e-3, 25.4e-3), "meniscus (right)" => SphericalLens(-30e-3, -20e-3, 2e-3, 25.4e-3), - "bi-concave with ring" => Lens(BMO.BiConcaveLensSDF(50e-3, 50e-3, 3e-3, 20e-3, 25.4e-3), λ -> 1.5), - "meniscus with ring" => Lens(BMO.MeniscusLensSDF(20e-3, 30e-3, 2e-3, 20e-3, 25.4e-3), λ -> 1.5), + "bi-concave with ring" => Lens(BMO.BiConcaveLensSDF(50e-3, 50e-3, 3e-3, 20e-3, 25.4e-3), ConstantRefractiveIndex(1.5)), + "meniscus with ring" => Lens(BMO.MeniscusLensSDF(20e-3, 30e-3, 2e-3, 20e-3, 25.4e-3), ConstantRefractiveIndex(1.5)), ] for (name, lens) in lenses @testset "$name" begin @@ -201,7 +201,7 @@ BMO.sdf(s::BlobSDF, p) = norm(BMO._world_to_sdf(s, p)) - 5e-3 @test BMO.shape(lenses[6][2]) isa BMO.MeniscusLensSDF @testset "doublet" begin - dl = SphericalDoubletLens(87.9e-3, -105.6e-3, -1000, 6e-3, 3e-3, 25.4e-3, 1.5, 1.6) + dl = SphericalDoubletLens(87.9e-3, -105.6e-3, -1000, 6e-3, 3e-3, 25.4e-3, ConstantRefractiveIndex(1.5), ConstantRefractiveIndex(1.6)) posed!(dl) plots = rendered_plots(dl; edges = false) @test length(plots) == 2 # outer mesh and cemented interface, see TestRenderLook.jl @@ -214,11 +214,11 @@ BMO.sdf(s::BlobSDF, p) = norm(BMO._world_to_sdf(s, p)) - 5e-3 @testset "other SDF shapes" begin # the solids of the cylindric and aspheric lens surfaces acyl = Lens(AcylindricalSurface(-15.538e-3, 25e-3, 50e-3, -1.0, - [0, 1.1926075e-5 * (1e3)^3, -2.9323497e-9 * (1e3)^5]), 7.5e-3, λ -> 1.5) - for lens in (Lens(CylindricalSurface(5.2e-3, 10e-3, 20e-3), 5.9e-3, λ -> 1.5), - Lens(CylindricalSurface(-20e-3, 10e-3, 20e-3), CylindricalSurface(20e-3, 10e-3, 20e-3), 3e-3, λ -> 1.5), + [0, 1.1926075e-5 * (1e3)^3, -2.9323497e-9 * (1e3)^5]), 7.5e-3, ConstantRefractiveIndex(1.5)) + for lens in (Lens(CylindricalSurface(5.2e-3, 10e-3, 20e-3), 5.9e-3, ConstantRefractiveIndex(1.5)), + Lens(CylindricalSurface(-20e-3, 10e-3, 20e-3), CylindricalSurface(20e-3, 10e-3, 20e-3), 3e-3, ConstantRefractiveIndex(1.5)), acyl, - Lens(EvenAsphericalSurface(20e-3, 10e-3, -1.0, [0, 1e3]), 4e-3, λ -> 1.5)) + Lens(EvenAsphericalSurface(20e-3, 10e-3, -1.0, [0, 1e3]), 4e-3, ConstantRefractiveIndex(1.5))) s = BMO.shape(posed!(lens)) @test Ext._has_mesh(s) pts, _, fcs = mesh_data(s) @@ -248,7 +248,7 @@ BMO.sdf(s::BlobSDF, p) = norm(BMO._world_to_sdf(s, p)) - 5e-3 @testset "objects" begin # one mesh plot per color: prisms merged, coating separate - cbs = CubeBeamsplitter(20e-3, λ -> 1.5) + cbs = CubeBeamsplitter(20e-3, ConstantRefractiveIndex(1.5)) plots = rendered_plots(cbs; edges = false) @test length(plots) == 2 @test all(p -> p isa Makie.Mesh, plots) diff --git a/test/Rendering/TestWavelengthColor.jl b/test/Rendering/TestWavelengthColor.jl index 73b4ebb0b..7d775bab8 100644 --- a/test/Rendering/TestWavelengthColor.jl +++ b/test/Rendering/TestWavelengthColor.jl @@ -43,7 +43,7 @@ end rgba(λ, α = 1) = Makie.RGBAf(wavelength_color(λ)..., α) # a dispersive prism that splits the beams, a group of beams of different wavelengths - prism = RightAnglePrism(30e-3, 20e-3, λ -> 1.5 + 4e-15 / λ^2) + prism = RightAnglePrism(30e-3, 20e-3, DiscreteRefractiveIndex(λs, 1.5 .+ 4e-15 ./ λs .^ 2)) translate3d!(prism, [0, 20e-3, 0]) # a beamsplitter whose child beams must keep the color of their wavelength bs = RoundThinBeamsplitter(25e-3; reflectance = 0.5) @@ -153,7 +153,7 @@ end @testset "color = :wavelength: positions and colors are updated together" begin # a mirror that is moved in and out of the beam changes the number of segments on every solve - lens = SphericalLens(50e-3, -50e-3, 10e-3, 25e-3, 1.5168) + lens = SphericalLens(50e-3, -50e-3, 10e-3, 25e-3, ConstantRefractiveIndex(1.5168)) ax = LScene(Figure()[1, 1]) @testset "$name" for (name, make, kwargs) in ( @@ -189,7 +189,7 @@ end @testset "render_settings!: color" begin rgba(λ, α = 1) = Makie.RGBAf(wavelength_color(λ)..., α) red = Makie.RGBAf(Makie.to_color(:red)) - lens = SphericalLens(50e-3, -50e-3, 10e-3, 25e-3, 1.5168) + lens = SphericalLens(50e-3, -50e-3, 10e-3, 25e-3, ConstantRefractiveIndex(1.5168)) sys = System([lens]) ax = LScene(Figure()[1, 1]) # positions and colors of every colored plot of `h` have the same length diff --git a/test/TestAstigmaticGaussianBeamlet.jl b/test/TestAstigmaticGaussianBeamlet.jl index cafbef671..882ff8f7c 100644 --- a/test/TestAstigmaticGaussianBeamlet.jl +++ b/test/TestAstigmaticGaussianBeamlet.jl @@ -127,7 +127,7 @@ const BMO = BeamletOptics @testset "Lens interaction" begin s1 = BMO.CylinderSDF(BMO.inch / 2, BMO.inch) - l1 = BMO.Lens(s1, n -> 1.5) + l1 = BMO.Lens(s1, ConstantRefractiveIndex(1.5)) BMO.translate3d!(l1, [0, 0.1, 0.0]) BMO.zrotate3d!(l1, deg2rad(90)) @@ -188,8 +188,8 @@ const BMO = BeamletOptics @testset "Two-lens astigmatic system" begin s1 = BMO.CylinderSDF(BMO.inch / 2, BMO.inch) s2 = BMO.CylinderSDF(BMO.inch / 2, BMO.inch) - l1 = BMO.Lens(s1, n -> 1.5) - l2 = BMO.Lens(s2, n -> 1.5) + l1 = BMO.Lens(s1, ConstantRefractiveIndex(1.5)) + l2 = BMO.Lens(s2, ConstantRefractiveIndex(1.5)) BMO.translate3d!(l1, [0, 0.1, 0.0]) BMO.translate3d!(l2, [0, 0.15, 0.0]) BMO.zrotate3d!(l1, deg2rad(90)) @@ -209,7 +209,7 @@ const BMO = BeamletOptics @testset "Solving again" begin s1 = BMO.CylinderSDF(BMO.inch / 2, BMO.inch) - l1 = BMO.Lens(s1, n -> 1.5) + l1 = BMO.Lens(s1, ConstantRefractiveIndex(1.5)) BMO.translate3d!(l1, [0, 0.1, 0.0]) BMO.zrotate3d!(l1, deg2rad(90)) @@ -346,7 +346,7 @@ end return optical_power(pd) end function tilted_lens() - lens = SphericalLens(0.5, -0.5, 8e-3, BMO.inch, x -> 1.5) + lens = SphericalLens(0.5, -0.5, 8e-3, BMO.inch, ConstantRefractiveIndex(1.5)) translate3d!(lens, [0, 0.1, 0]) BMO.zrotate3d!(lens, deg2rad(8)) BMO.xrotate3d!(lens, deg2rad(5)) @@ -377,7 +377,7 @@ end # A cylinder lens rotated against the axes of the beam twists it: for unequal M2, # H H† is complex, and det S exceeds |h1 × h2|² by 1.86 for M2 = (1, 20) function behind_cylinder_lens(M2_x, M2_y) - lens = Lens(CylindricalSurface(30e-3, 20e-3, 25e-3), 4e-3, _ -> 1.5) + lens = Lens(CylindricalSurface(30e-3, 20e-3, 25e-3), 4e-3, ConstantRefractiveIndex(1.5)) BMO.yrotate3d!(lens, deg2rad(45)) translate3d!(lens, [0, 50e-3, 0]) pd = Detector(0.1) @@ -401,7 +401,7 @@ end @testset "The limit does not depend on M2 and λ" begin # H deviates by 8.5 % in this focus, within the limit of 50 % function reaches_detector(; kw...) - lens = SphericalLens(50e-3, -50e-3, 8e-3, 30e-3, x -> 1.5) + lens = SphericalLens(50e-3, -50e-3, 8e-3, 30e-3, ConstantRefractiveIndex(1.5)) translate3d!(lens, [0, 0.1, 0]) pd = Detector(0.2) translate3d!(pd, [0, 0.15, 0]) @@ -424,7 +424,7 @@ end # waists the Lagrange invariant of one axis drops and changes its sign: Im Q has no # Gaussian decay left, the field would hold 1e6 times the power of the source. function detector_behind_lens(f; check_invariant = true) - lens = SphericalLens(f, -f, 8e-3, BMO.inch, x -> 1.5) + lens = SphericalLens(f, -f, 8e-3, BMO.inch, ConstantRefractiveIndex(1.5)) translate3d!(lens, [0, 0.1, 0]) pd = Detector(0.02) translate3d!(pd, [0, 0.1 + f, 0]) @@ -458,7 +458,7 @@ end λ, w0, n, d = 1e-6, 100e-6, 1.5, 0.05 zR = π * w0^2 / λ beam = AstigmaticGaussianBeamlet([0.0, -d, 0], [0.0, 1, 0], λ, w0; support = [1.0, 0, 0]) - solve_system!(System([SphericalLens(Inf, Inf, 20e-3, BMO.inch, λ -> n)]), beam) + solve_system!(System([SphericalLens(Inf, Inf, 20e-3, BMO.inch, ConstantRefractiveIndex(n))]), beam) for l in (0.0, 5e-3, 15e-3) # depth in the glass w = w0 * sqrt(1 + ((d + l / n) / zR)^2) field(r) = BMO.parabasal_field(beam, [r, 0.0, 0], d + l + 1e-9) diff --git a/test/TestAstigmaticGaussianPhysical.jl b/test/TestAstigmaticGaussianPhysical.jl index a6392a1e9..8adfb0860 100644 --- a/test/TestAstigmaticGaussianPhysical.jl +++ b/test/TestAstigmaticGaussianPhysical.jl @@ -466,7 +466,7 @@ dominant(E) = E[argmax(abs.(E))] surf2 = BMO.CircularFlatSurface(10.0mm) # n=2.0 (high index) - lens = BMO.Lens(surf1, surf2, 1.0mm, λ -> 2.0) + lens = BMO.Lens(surf1, surf2, 1.0mm, ConstantRefractiveIndex(2.0)) # Tilt the lens by 80 degrees BMO.zrotate3d!(lens, deg2rad(80)) @@ -487,7 +487,7 @@ dominant(E) = E[argmax(abs.(E))] w0 = 1mm P0 = 1e-3 n = 1.5 - lens() = (l = ThinLens(200mm, 200mm, 5mm, n); translate3d!(l, [0, 30mm, 0]); l) # f = 200 mm + lens() = (l = ThinLens(200mm, 200mm, 5mm, ConstantRefractiveIndex(n)); translate3d!(l, [0, 30mm, 0]); l) # f = 200 mm function splitter() bs = ThinBeamsplitter(25mm, reflectance = 0.5) translate3d!(bs, [0, 80mm, 0]) @@ -593,7 +593,7 @@ dominant(E) = E[argmax(abs.(E))] T_normal = (4n / (n + 1)^2)^2 @testset "Uncoated lens" begin - lens = ThinLens(200mm, 200mm, 5mm, n) + lens = ThinLens(200mm, 200mm, 5mm, ConstantRefractiveIndex(n)) translate3d!(lens, [0, 30mm, 0]) pd = detector([0, 140mm, 0], [0.0, 1, 0]) b = beam() @@ -606,7 +606,7 @@ dominant(E) = E[argmax(abs.(E))] @testset "Cube beamsplitter" begin # coating at 135°: +y is reflected to -x - cbs = CubeBeamsplitter(25mm, n) + cbs = CubeBeamsplitter(25mm, ConstantRefractiveIndex(n)) pd_t = detector([0, 100mm, 0], [0.0, 1, 0]) pd_r = detector([-100mm, 0, 0], [-1.0, 0, 0]) b = AstigmaticGaussianBeamlet([0.0, -100mm, 0], [0.0, 1, 0], λ, w0; P0, support = [1.0, 0, 0]) @@ -626,7 +626,7 @@ dominant(E) = E[argmax(abs.(E))] r_p = (n * cos(θ_i) - cos(θ_t)) / (n * cos(θ_i) + cos(θ_t)) # (polarization, power reflection of the back surface): s is normal to the plane of incidence for (e, R_back) in (([0.0, 0, 1], r_s^2), ([1.0, 0, 0], r_p^2)) - pbs = RectangularPlateBeamsplitter(25mm, 25mm, 5mm, λ -> n) + pbs = RectangularPlateBeamsplitter(25mm, 25mm, 5mm, ConstantRefractiveIndex(n)) zrotate3d!(pbs, π / 4) pd_t = detector([0, 100mm, 0], [0.0, 1, 0]) pd_r = detector([100mm, 0, 0], [1.0, 0, 0]) diff --git a/test/TestAutoDiff.jl b/test/TestAutoDiff.jl index 8c2321cc3..a54160a16 100644 --- a/test/TestAutoDiff.jl +++ b/test/TestAutoDiff.jl @@ -45,7 +45,7 @@ function check_derivative(component; h, mirror = false) return ForwardDiff.derivative(f, 0.0), (f(δ) - f(-δ)) / 2δ end -n(T) = _ -> T(1.5) +n(T) = ConstantRefractiveIndex(T(1.5)) lenses = [ "bi-convex SphericalLens" => T -> SphericalLens(T(50mm), T(-50mm), T(5mm), T(25mm), n(T)), "bi-concave SphericalLens" => T -> SphericalLens(T(-50mm), T(50mm), T(3mm), T(25mm), n(T)), @@ -65,7 +65,7 @@ mirrors = [ pd_at(T, y) = (d = Detector(T(40mm)); translate3d!(d, T[0, y, 0]); d) hit_x(pd) = BMO.hit_point(only(BMO.hits(pd)))[1] -bi_convex(T) = SphericalLens(T(50mm), T(-50mm), T(5mm), T(25mm), _ -> T(1.5)) +bi_convex(T) = SphericalLens(T(50mm), T(-50mm), T(5mm), T(25mm), ConstantRefractiveIndex(T(1.5))) # paraxial ABCD matrices in reduced angles: a translation by the reduced distance `d` (length / n), # and a refraction or reflection with the power `P` translation(d) = [1 d; 0 1] @@ -79,7 +79,7 @@ arm mirror `m1` shifted by `dx` along its axis. function michelson_power(dx) T = typeof(dx) cm = T(1e-2) - cbs = CubeBeamsplitter(T(25.4mm), _ -> T(1.51509)) + cbs = CubeBeamsplitter(T(25.4mm), ConstantRefractiveIndex(T(1.51509))) zrotate3d!(cbs, T(-π / 2)) translate3d!(cbs, T[0, 23.5cm, 0]) m1 = RoundPlanoMirror(T(25.4mm), T(5mm)) @@ -97,9 +97,9 @@ end # label, function of the parameter, value of the parameter parametric_cases = [ - ("radius of curvature", R -> (T = typeof(R); l = SphericalLens(R, T(-50mm), T(5mm), T(25mm), _ -> T(1.5)); + ("radius of curvature", R -> (T = typeof(R); l = SphericalLens(R, T(-50mm), T(5mm), T(25mm), ConstantRefractiveIndex(T(1.5))); d = pd_at(T, 100mm); solve_system!(System([l, d]), Beam(T[2mm, -50mm, 0], T[0, 1, 0], T(1e-6))); hit_x(d)), 50mm), - ("center thickness", t -> (T = typeof(t); l = SphericalLens(T(50mm), T(-50mm), t, T(25mm), _ -> T(1.5)); + ("center thickness", t -> (T = typeof(t); l = SphericalLens(T(50mm), T(-50mm), t, T(25mm), ConstantRefractiveIndex(T(1.5))); d = pd_at(T, 100mm); solve_system!(System([l, d]), Beam(T[2mm, -50mm, 0], T[0, 1, 0], T(1e-6))); hit_x(d)), 5mm), ("lens tilt", θ -> (T = typeof(θ); l = bi_convex(T); zrotate3d!(l, θ); d = pd_at(T, 100mm); solve_system!(System([l, d]), Beam(T[0, -50mm, 0], T[0, 1, 0], T(1e-6))); hit_x(d)), 0.0), @@ -113,13 +113,13 @@ parametric_cases = [ ("ThinBeamsplitter tilt, reflected beam", θ -> (T = typeof(θ); bs = ThinBeamsplitter(T(30mm)); zrotate3d!(bs, T(π / 4) + θ); translate3d!(bs, T[0, 50mm, 0]); d = Detector(T(40mm)); zrotate3d!(d, T(π / 2)); translate3d!(d, T[100mm, 50mm, 0]); solve_system!(System([bs, d]), Beam(T[0, 0, 0], T[0, 1, 0], T(1e-6))); BMO.hit_point(only(BMO.hits(d)))[2]), 0.0), - ("CubeBeamsplitter tilt", θ -> (T = typeof(θ); c = CubeBeamsplitter(T(25mm), _ -> T(1.5)); zrotate3d!(c, θ); + ("CubeBeamsplitter tilt", θ -> (T = typeof(θ); c = CubeBeamsplitter(T(25mm), ConstantRefractiveIndex(T(1.5))); zrotate3d!(c, θ); translate3d!(c, T[0, 50mm, 0]); d = pd_at(T, 150mm); solve_system!(System([c, d]), Beam(T[1mm, 0, 0], T[0, 1, 0], T(1e-6))); hit_x(d)), 0.05), - ("RoundPlateBeamsplitter tilt", θ -> (T = typeof(θ); p = RoundPlateBeamsplitter(T(25mm), T(5mm), _ -> T(1.5)); + ("RoundPlateBeamsplitter tilt", θ -> (T = typeof(θ); p = RoundPlateBeamsplitter(T(25mm), T(5mm), ConstantRefractiveIndex(T(1.5))); zrotate3d!(p, T(π / 4) + θ); translate3d!(p, T[0, 50mm, 0]); d = pd_at(T, 150mm); solve_system!(System([p, d]), Beam(T[0, 0, 0], T[0, 1, 0], T(1e-6))); hit_x(d)), 0.0), - ("total internal reflection in a RightAnglePrism", θ -> (T = typeof(θ); p = RightAnglePrism(T(25mm), T(25mm), _ -> T(1.5)); + ("total internal reflection in a RightAnglePrism", θ -> (T = typeof(θ); p = RightAnglePrism(T(25mm), T(25mm), ConstantRefractiveIndex(T(1.5))); zrotate3d!(p, θ); translate3d!(p, T[0, 50mm, 0]); b = Beam(T[4mm, 0, 0], T[0, 1, 0], T(1e-6)); solve_system!(System([p]), b); BMO.direction(last(BMO.rays(b)))[2]), 0.0), ("RoundPolarizationFilter angle (Malus)", θ -> (T = typeof(θ); f = RoundPolarizationFilter(T(25mm)); yrotate3d!(f, θ); @@ -160,7 +160,7 @@ parametric_cases = [ # the spot of a ray at h = 2 mm behind a bi-convex lens with the refractive index `n0` function spot(n0) T = typeof(n0) - lens = SphericalLens(T(50mm), T(-50mm), T(5mm), T(25mm), _ -> n0) + lens = SphericalLens(T(50mm), T(-50mm), T(5mm), T(25mm), ConstantRefractiveIndex(n0)) pd = Detector(T(40mm)) translate3d!(pd, T[0, 100mm, 0]) beam = Beam(T[2mm, -50mm, 0], T[0, 1, 0], T(1e-6)) @@ -182,7 +182,7 @@ parametric_cases = [ # the hit point of a beamlet on the axis behind a lens that is shifted laterally function spot(dx) T = typeof(dx) - lens = SphericalLens(T(50mm), T(-50mm), T(5mm), T(25mm), _ -> T(1.5)) + lens = SphericalLens(T(50mm), T(-50mm), T(5mm), T(25mm), ConstantRefractiveIndex(T(1.5))) translate3d!(lens, [dx, zero(T), zero(T)]) pd = Detector(T(40mm)) translate3d!(pd, T[0, 100mm, 0]) diff --git a/test/TestBeamGroups.jl b/test/TestBeamGroups.jl index 1fe4d1e0c..df65a00c1 100644 --- a/test/TestBeamGroups.jl +++ b/test/TestBeamGroups.jl @@ -394,7 +394,7 @@ const BMO = BeamletOptics @testset "Testing plane of the rays" begin # a line source in a plane that contains the axis of a lens stays in this plane - lens = SphericalLens(50e-3, -50e-3, 5e-3, BMO.inch, λ -> 1.5) + lens = SphericalLens(50e-3, -50e-3, 5e-3, BMO.inch, ConstantRefractiveIndex(1.5)) system = System([lens]) sheet = UniformLineSource([0, -0.1, 0], [0, 1, 0], 20e-3, lambda; num_rays = 21, basis = [1, 0, 0]) solve_system!(system, sheet) @@ -745,7 +745,7 @@ end # the RMS spot behind a plano-convex lens, which weights every ray equally: the rings agree # with the equal-area sunflower sampling, in contrast to rays per ring in proportion to the # circumference, which weight the edge twice and gave +4 % here - lens = SphericalLens(50mm, Inf, 5mm, 25mm, _ -> 1.5) + lens = SphericalLens(50mm, Inf, 5mm, 25mm, ConstantRefractiveIndex(1.5)) function rms_spot(source) pd = Detector(30mm) translate3d!(pd, [0, 95mm, 0]) diff --git a/test/TestBeamletFieldInMedia.jl b/test/TestBeamletFieldInMedia.jl index 6353baf54..6bdf4e17d 100644 --- a/test/TestBeamletFieldInMedia.jl +++ b/test/TestBeamletFieldInMedia.jl @@ -46,13 +46,13 @@ end power(pd) = optical_power(pd, RegularGrid(num_points = 301, crop_factor = 4)) function plate_beamsplitter() - pbs = RectangularPlateBeamsplitter(30mm, 30mm, 5mm, _ -> n_glass) + pbs = RectangularPlateBeamsplitter(30mm, 30mm, 5mm, ConstantRefractiveIndex(n_glass)) translate3d!(pbs, [0, 50mm, 0]) return pbs end function glass_plate() - plate = Lens(RectangularFlatSurface(30mm), RectangularFlatSurface(30mm), 5mm, _ -> n_glass) + plate = Lens(RectangularFlatSurface(30mm), RectangularFlatSurface(30mm), 5mm, ConstantRefractiveIndex(n_glass)) translate3d!(plate, [0, 50mm, 0]) return plate end diff --git a/test/TestBugFixes.jl b/test/TestBugFixes.jl index 48d2a32e4..cea5b1b1b 100644 --- a/test/TestBugFixes.jl +++ b/test/TestBugFixes.jl @@ -15,12 +15,12 @@ const mm = 1e-3 # been moved and rotated before (suspected rounding errors of `_world_to_sdf`). @testset "Narrow point source through a double Gauss lens" begin # Based on https://www.pencilofrays.com/double-gauss-sonnar-comparison/ - l1 = SphericalLens(48.88mm, 182.96mm, 8.89mm, 52.3mm, λ -> 1.62286) + l1 = SphericalLens(48.88mm, 182.96mm, 8.89mm, 52.3mm, ConstantRefractiveIndex(1.62286)) l23 = SphericalDoubletLens(36.92mm, Inf, 23.06mm, 15.11mm, 2.31mm, - 45.11mm, λ -> 1.58565, λ -> 1.67764) + 45.11mm, ConstantRefractiveIndex(1.58565), ConstantRefractiveIndex(1.67764)) l45 = SphericalDoubletLens(-23.91mm, Inf, -36.92mm, 1.92mm, 7.77mm, - 40.01mm, λ -> 1.57046, λ -> 1.64128) - l6 = SphericalLens(1063.24mm, -48.88mm, 6.73mm, 45.11mm, λ -> 1.62286) + 40.01mm, ConstantRefractiveIndex(1.57046), ConstantRefractiveIndex(1.64128)) + l6 = SphericalLens(1063.24mm, -48.88mm, 6.73mm, 45.11mm, ConstantRefractiveIndex(1.62286)) l_23 = thickness(l1) + 0.38mm l_45 = l_23 + thickness(l23) + 9.14mm + 13.36mm l_6 = l_45 + thickness(l45) + 0.38mm @@ -48,7 +48,7 @@ const mm = 1e-3 # a rotationally symmetric lens: every ray must stay in its meridional plane. n = 1.458 lens = Lens(CircularFlatSurface(30mm), - EvenAsphericalSurface((n - 1) * 50mm, 30mm, -n^2, [0.0]), 4mm, x -> n) + EvenAsphericalSurface((n - 1) * 50mm, 30mm, -n^2, [0.0]), 4mm, ConstantRefractiveIndex(n)) xrotate3d!(lens, deg2rad(0.5)) zrotate3d!(lens, deg2rad(0.2)) axis = BMO.orientation(lens)[:, 2] @@ -117,7 +117,7 @@ end ref_signal(ϕ, A) = (cos(ϕ) + 1) / 2 * A # setup system for tests below - splitter = CubeBeamsplitter(10mm, n -> 1) + splitter = CubeBeamsplitter(10mm, ConstantRefractiveIndex(1)) substrate_length = 10mm substrate = TestSubstrate(BMO.CylinderSDF(5mm, substrate_length / 2), 1.5) @@ -234,7 +234,7 @@ end @testset "Meniscus lens on-axis normal" begin # Issue: AD normal of MeniscusLensSDF flipped on the optical axis, on-axis rays missed the lens. for (r1, r2) in ((69.21mm, 433.84mm), (-433.84mm, -69.21mm)) - lens = SphericalLens(r1, r2, 9.33mm, 70mm, λ -> 1.671) + lens = SphericalLens(r1, r2, 9.33mm, 70mm, ConstantRefractiveIndex(1.671)) @test BMO.shape(lens) isa BMO.MeniscusLensSDF # rotate to test transformation of the normal zrotate3d!(lens, deg2rad(30)) @@ -260,9 +260,9 @@ end ct = 4mm d = 30mm concave = Lens(CircularFlatSurface(d), - EvenAsphericalSurface((n - 1) * f, d, -n^2, [0.0]), ct, x -> n) + EvenAsphericalSurface((n - 1) * f, d, -n^2, [0.0]), ct, ConstantRefractiveIndex(n)) convex = Lens(CircularFlatSurface(d), - EvenAsphericalSurface(-(n - 1) * f, d, -n^2, [0.0]), 2ct, x -> n) + EvenAsphericalSurface(-(n - 1) * f, d, -n^2, [0.0]), 2ct, ConstantRefractiveIndex(n)) # The conic vertex lies on the back face, `thickness` along the lens axis for (lens, thickness, f_signed) in ((concave, ct, -f), (convex, 2ct, f)), tilt in (0.0, deg2rad(0.5)) @@ -292,7 +292,7 @@ end n = 1.458 spurious = 0 for _ in 1:2000 - lens = Lens(CircularFlatSurface(30mm), SphericalSurface(31.85mm, 30mm), 4mm, λ -> n) + lens = Lens(CircularFlatSurface(30mm), SphericalSurface(31.85mm, 30mm), 4mm, ConstantRefractiveIndex(n)) rotate3d!(lens, BMO.rotate3d(normalize(randn(rng, 3)), 2π * rand(rng))) translate3d!(lens, 12 .* randn(rng, 3)) dir = orientation(lens)[:, 2] @@ -308,10 +308,10 @@ end # where id > 1 throws a BoundsError in interact3d for AstigmaticGaussianBeamlet. # 1 lens followed by 1 plate beamsplitter - lens = ThinLens(Inf, 50mm, 25mm, 1.5) + lens = ThinLens(Inf, 50mm, 25mm, ConstantRefractiveIndex(1.5)) BMO.translate3d!(lens, [0, 50mm, 0]) - pbs = RoundPlateBeamsplitter(25mm, 5mm, λ -> 1.5) + pbs = RoundPlateBeamsplitter(25mm, 5mm, ConstantRefractiveIndex(1.5)) BMO.translate3d!(pbs, [0, 100mm, 0]) system = System([lens, pbs]) @@ -331,7 +331,7 @@ end # A large beamlet with separate waists leaves its model in this focus, see # "Limit of the beamlet model" in TestAstigmaticGaussianBeamlet f = 0.05 - lens = SphericalLens(f, -f, 8e-3, BMO.inch, x -> 1.5) + lens = SphericalLens(f, -f, 8e-3, BMO.inch, ConstantRefractiveIndex(1.5)) translate3d!(lens, [0.1, 0.1, 0]) # Beside the beam pd = Detector(0.02) translate3d!(pd, [0, 0.1 + f, 0]) @@ -377,7 +377,7 @@ end end @testset "Plate beamsplitter under 45°" begin - pbs = RoundPlateBeamsplitter(25mm, 3mm, λ -> 1.5; reflectance = 0.1) + pbs = RoundPlateBeamsplitter(25mm, 3mm, ConstantRefractiveIndex(1.5); reflectance = 0.1) zrotate3d!(pbs, deg2rad(45)) for E0 in ([0, 0, 1.0], [1.0, 0, 0]) beam = Beam(PolarizedRay([0, -20mm, 0], [0, 1.0, 0], 780e-9, E0)) @@ -394,7 +394,7 @@ end @test_nowarn PolarizedRay(zeros(3), [0, 1, 0], 1e-6, [1e3, 1e-8, 0]) @test_throws ErrorException PolarizedRay(zeros(3), [0, 1, 0], 1e-6, [1e3, 1, 0]) # Oblique rays with large field amplitudes through a lens - lens = SphericalLens(8mm, Inf, 3mm, 12mm, λ -> 1.51) + lens = SphericalLens(8mm, Inf, 3mm, 12mm, ConstantRefractiveIndex(1.51)) for h in (0.2mm, 1mm, 3mm), θ in (0.0, 5.0, 15.0) dir = [sind(θ), cosd(θ), 0] E0 = 1e4 .* [0, 0, 1.0] @@ -424,7 +424,7 @@ end @testset "Waist behind a lens does not depend on placement" begin function waists(d) - lens = SphericalLens(50mm, -50mm, 5mm, 25.4mm, λ -> 1.5) + lens = SphericalLens(50mm, -50mm, 5mm, 25.4mm, ConstantRefractiveIndex(1.5)) align3d!(lens, d) translate3d!(lens, 100mm .* normalize(d)) g = GaussianBeamlet(zeros(3), d, λ, 1mm; support = BMO.normal3d(d)) @@ -454,7 +454,7 @@ end @testset "Same model limit as an AstigmaticGaussianBeamlet" begin function detector_behind_lens(beam, f; kwargs...) - lens = SphericalLens(f, -f, 8mm, 25.4mm, λ -> 1.5) + lens = SphericalLens(f, -f, 8mm, 25.4mm, ConstantRefractiveIndex(1.5)) translate3d!(lens, [0, 0.1, 0]) pd = Detector(20mm) translate3d!(pd, [0, 0.1 + 2f, 0]) diff --git a/test/TestGaussianBeamlet.jl b/test/TestGaussianBeamlet.jl index 7bad5d46f..e30b864d2 100644 --- a/test/TestGaussianBeamlet.jl +++ b/test/TestGaussianBeamlet.jl @@ -166,7 +166,7 @@ const mm = 1e-3 # Numerical result tl = BMO.ThinLensSDF(R1, R2, 0.025) - lens = Lens(tl, x -> nl) + lens = Lens(tl, ConstantRefractiveIndex(nl)) system = System(lens) translate3d!(lens, [0, lens_y_location, 0]) # Create and solve beam, calculate beam parameters @@ -210,7 +210,7 @@ end λ = 1e-6 n = 1.5 # glass from y = 50.0 mm to about 50.5 mm - lens = ThinLens(50mm, 50mm, 10mm, n) + lens = ThinLens(50mm, 50mm, 10mm, ConstantRefractiveIndex(n)) translate3d!(lens, [0, 50mm, 0]) system = System([lens]) Δφ(a, b) = rad2deg(angle(a / b)) @@ -247,7 +247,7 @@ end w0 = 1mm R = 50mm k = 2π / λ - lens(y) = (l = ThinLens(R, R, 10mm, n); translate3d!(l, [0, y, 0]); l) # f = 50 mm + lens(y) = (l = ThinLens(R, R, 10mm, ConstantRefractiveIndex(n)); translate3d!(l, [0, y, 0]); l) # f = 50 mm gb() = GaussianBeamlet([0.0, 0, 0], [0.0, 1, 0], λ, w0; support = [1.0, 0, 0]) # ABCD law for the traced thick lenses: Gouy phase [deg] at the distance z along the beam, # -arg(A + B/q) with q = -i z_R at the waist. The segments alternate between air and glass. @@ -359,7 +359,7 @@ end end @testset "GaussianSegment matches gauss_parameters" begin - lens(y) = (l = ThinLens(50mm, 50mm, 10mm, 1.5); translate3d!(l, [0, y, 0]); l) + lens(y) = (l = ThinLens(50mm, 50mm, 10mm, ConstantRefractiveIndex(1.5)); translate3d!(l, [0, y, 0]); l) gauss = GaussianBeamlet([0.0, 0, 0], [0.0, 1, 0], 1e-6, 1mm; support = [1.0, 0, 0]) solve_system!(System([lens(50mm), lens(150mm)]), gauss) chief_rays = BMO.rays(gauss.chief) diff --git a/test/TestKinematicTrait.jl b/test/TestKinematicTrait.jl index c07adfd84..1aaa8509f 100644 --- a/test/TestKinematicTrait.jl +++ b/test/TestKinematicTrait.jl @@ -96,7 +96,7 @@ function oriented_subjects() BMO.CubeMesh(1mm), BMO.SphereSDF(1mm) + BMO.CylinderSDF(1mm, 2mm), mirror(), - CubeBeamsplitter(10mm, n -> 1.5), + CubeBeamsplitter(10mm, ConstantRefractiveIndex(1.5)), ObjectGroup([mirror(), lens()]), CollimatedSource([0, -50mm, 0], [0, 1, 0], 5mm, 1e-6; num_rings = 2, num_rays = 40), ] @@ -132,7 +132,7 @@ end BMO.SphereSDF(1mm) + BMO.CylinderSDF(1mm, 2mm), BMO.SphereSDF(1mm) - BMO.CylinderSDF(1mm, 2mm), mirror(), - CubeBeamsplitter(10mm, n -> 1.5), + CubeBeamsplitter(10mm, ConstantRefractiveIndex(1.5)), ObjectGroup([mirror(), lens()]), CollimatedSource([0, -50mm, 0], [0, 1, 0], 5mm, 1e-6; num_rings = 2, num_rays = 40), ] @@ -193,7 +193,7 @@ end m = mirror() zrotate3d!(m, deg2rad(45)) translate3d!(m, [0, 60mm, 0]) - bs = CubeBeamsplitter(10mm, n -> 1.5) + bs = CubeBeamsplitter(10mm, ConstantRefractiveIndex(1.5)) system = System([bs, m]) b = Beam([1mm, -50mm, 0.5mm], [0, 1, 0]) solve_system!(system, b) diff --git a/test/TestObjectGroups.jl b/test/TestObjectGroups.jl index cafe98060..507867ffa 100644 --- a/test/TestObjectGroups.jl +++ b/test/TestObjectGroups.jl @@ -315,8 +315,8 @@ end BMO.CylinderSDF(1e-3, 2e-3) + BMO.CylinderSDF(2e-3, 1e-3), # composite SDF BMO.CubeMesh(1e-3), # mesh RoundPlanoMirror(10e-3, 2e-3), # object (SingleShape) - CubeBeamsplitter(5e-3, λ -> 1.5), # object (MultiShape) - ObjectGroup([RoundPlanoMirror(10e-3, 2e-3), CubeBeamsplitter(5e-3, λ -> 1.5)]), + CubeBeamsplitter(5e-3, ConstantRefractiveIndex(1.5)), # object (MultiShape) + ObjectGroup([RoundPlanoMirror(10e-3, 2e-3), CubeBeamsplitter(5e-3, ConstantRefractiveIndex(1.5))]), ] for x in subjects translate3d!(x, [1e-3, 2e-3, 3e-3]) diff --git a/test/TestOpticsBaseExt.jl b/test/TestOpticsBaseExt.jl index 64502006b..b91f47fa7 100644 --- a/test/TestOpticsBaseExt.jl +++ b/test/TestOpticsBaseExt.jl @@ -577,7 +577,7 @@ end source() = BMO.AstigmaticGaussianBeamlet([0.0, ys, 0], [0.0, 1, 0], λ, w0; P0, support = [1.0, 0, 0]) beam, det = source(), detector_at(0.1) - BMO.solve_system!(BMO.System([BMO.SphericalLens(Inf, Inf, t, BMO.inch, λ -> n_glass), det]), + BMO.solve_system!(BMO.System([BMO.SphericalLens(Inf, Inf, t, BMO.inch, ConstantRefractiveIndex(n_glass)), det]), beam) @test length(BMO.rays(beam.c)) == 3 kw = (size = (64, 64), spacing = (w0 / 8, w0 / 8)) diff --git a/test/TestPathSegments.jl b/test/TestPathSegments.jl index 745de2e32..6e489ef80 100644 --- a/test/TestPathSegments.jl +++ b/test/TestPathSegments.jl @@ -121,7 +121,7 @@ end s = only(path_segments(b)) @test s.final && s.leaf && s.s_stop == 1.0 @test s.ray_index == 1 && s.parent == 0 - lens = SphericalLens(0.1, -0.1, 5e-3, BMO.inch, 1.5) + lens = SphericalLens(0.1, -0.1, 5e-3, BMO.inch, ConstantRefractiveIndex(1.5)) sys = System([lens]) b2 = Beam([0, -0.1, 0], [0, 1.0, 0]) solve_system!(sys, b2) diff --git a/test/TestPolarizedRays.jl b/test/TestPolarizedRays.jl index e3a6c086c..dad1bb3b6 100644 --- a/test/TestPolarizedRays.jl +++ b/test/TestPolarizedRays.jl @@ -109,11 +109,11 @@ const BMO = BeamletOptics s3 = BMO.CuboidMesh(1.0, d, 1.0) s4 = BMO.CuboidMesh(1.0, d, 1.0) s5 = BMO.CuboidMesh(1.0, d, 1.0) - l1 = Lens(s1, x -> n) - l2 = Lens(s2, x -> n) - l3 = Lens(s3, x -> n) - l4 = Lens(s4, x -> n) - l5 = Lens(s5, x -> n) + l1 = Lens(s1, ConstantRefractiveIndex(n)) + l2 = Lens(s2, ConstantRefractiveIndex(n)) + l3 = Lens(s3, ConstantRefractiveIndex(n)) + l4 = Lens(s4, ConstantRefractiveIndex(n)) + l5 = Lens(s5, ConstantRefractiveIndex(n)) translate3d!.([l1, l2, l3, l4, l5], Ref([-0.5, -d / 2, -0.5])) BMO.set_new_origin3d!.(BMO.shape.([l1, l2, l3, l4, l5])) translate3d!(l2, [0, 0.5, -1d / 2]) @@ -146,7 +146,7 @@ const BMO = BeamletOptics # Create Fresnel rhomb with n=1.5 and θ=53.3° for quarter-wave plate effect n = 1.5 s1 = BMO.CuboidMesh(0.5, 1.25, 0.5, deg2rad(53.3)) - l1 = Lens(s1, x -> n) + l1 = Lens(s1, ConstantRefractiveIndex(n)) translate3d!(l1, [-0.25, 0, -0.25]) BMO.set_new_origin3d!(s1) # Rotate prism to obtain 45° beam input polarization @@ -325,7 +325,7 @@ const BMO = BeamletOptics D = 15mm num_rays = 1000 - lens = SphericalLens(R1, R2, l, d, x -> n) + lens = SphericalLens(R1, R2, l, d, ConstantRefractiveIndex(n)) x_shift = y_shift = -2mm psfd = Detector(10e-3) diff --git a/test/TestProgress.jl b/test/TestProgress.jl index a2cf153e4..3b01b23da 100644 --- a/test/TestProgress.jl +++ b/test/TestProgress.jl @@ -10,7 +10,7 @@ const BMO = BeamletOptics const mm = 1e-3 function lens_system() - lens = SphericalLens(100mm, Inf, 5mm, 25.4mm, x -> 1.5) + lens = SphericalLens(100mm, Inf, 5mm, 25.4mm, ConstantRefractiveIndex(1.5)) pd = Detector(20mm) translate3d!(pd, [0, 100mm, 0]) return System([lens, pd]), pd diff --git a/test/TestProperties.jl b/test/TestProperties.jl index 137f4022d..573958b69 100644 --- a/test/TestProperties.jl +++ b/test/TestProperties.jl @@ -13,19 +13,19 @@ prop(props, name) = (i = findfirst(p -> p.first == name, props); isnothing(i) ? objs = Any[ SphericalLens(50e-3, -50e-3, 5e-3, 25.4e-3), SphericalLens(50e-3, 50e-3, 0, 25.4e-3), - Lens(EvenAsphericalSurface(20e-3, 25e-3, -1.0, [0, 1e-5]), 8e-3, λ -> 1.5), - Lens(CylindricalSurface(5.2e-3, 10e-3, 20e-3), 5.9e-3, λ -> 1.517), - SphericalDoubletLens(50e-3, -40e-3, -100e-3, 4e-3, 2e-3, 25.4e-3, λ -> 1.5, λ -> 1.6), + Lens(EvenAsphericalSurface(20e-3, 25e-3, -1.0, [0, 1e-5]), 8e-3, ConstantRefractiveIndex(1.5)), + Lens(CylindricalSurface(5.2e-3, 10e-3, 20e-3), 5.9e-3, ConstantRefractiveIndex(1.517)), + SphericalDoubletLens(50e-3, -40e-3, -100e-3, 4e-3, 2e-3, 25.4e-3, ConstantRefractiveIndex(1.5), ConstantRefractiveIndex(1.6)), SquarePlanoMirror2D(25e-3), RectangularPlanoMirror(25e-3, 20e-3, 5e-3), RoundPlanoMirror(25e-3, 5e-3), SphericalMirror(100e-3, 25e-3, 5e-3), ParabolicMirror(25e-3, 50e-3), OffAxisParabolicMirror(50e-3, 25e-3; angle = 90), RightAnglePrismMirror(10e-3, 10e-3), Retroreflector(1.0), Detector(10e-3), ThinBeamsplitter(10e-3, 10e-3), RoundThinBeamsplitter(10e-3), - RoundPlateBeamsplitter(25e-3, 3e-3, λ -> 1.5), - RectangularPlateBeamsplitter(25e-3, 25e-3, 3e-3, λ -> 1.5), - CubeBeamsplitter(10e-3, λ -> 1.5), RectangularCompensatorPlate(25e-3, 25e-3, 3e-3, λ -> 1.5), + RoundPlateBeamsplitter(25e-3, 3e-3, ConstantRefractiveIndex(1.5)), + RectangularPlateBeamsplitter(25e-3, 25e-3, 3e-3, ConstantRefractiveIndex(1.5)), + CubeBeamsplitter(10e-3, ConstantRefractiveIndex(1.5)), RectangularCompensatorPlate(25e-3, 25e-3, 3e-3, ConstantRefractiveIndex(1.5)), PolarizationFilter(10e-3), RoundPolarizationFilter(10e-3), - RoundLinearPolarizer(25e-3, 1e-3, 1e-3, λ -> 1.5), RightAnglePrism(10e-3, 10e-3, λ -> 1.5), + RoundLinearPolarizer(25e-3, 1e-3, 1e-3, ConstantRefractiveIndex(1.5)), RightAnglePrism(10e-3, 10e-3, ConstantRefractiveIndex(1.5)), ObjectGroup([RoundPlanoMirror(25e-3, 5e-3), Detector(10e-3)]), Beam([0, 0, 0.0], [0, 1.0, 0], 1e-6), GaussianBeamlet([0, 0, 0.0], [0, 1.0, 0], 1e-6, 1e-3), CollimatedSource([0, 0, 0.0], [0, 1.0, 0], 5e-3; num_rings = 2), @@ -63,16 +63,13 @@ prop(props, name) = (i = findfirst(p -> p.first == name, props); isnothing(i) ? end @testset "Component values" begin - lens = SphericalLens(50e-3, -50e-3, 5e-3, 25.4e-3, λ -> 1.5) + lens = SphericalLens(50e-3, -50e-3, 5e-3, 25.4e-3, ConstantRefractiveIndex(1.5)) props = BMO.properties(lens) @test prop(props, "Shape") == "UnionSDF" @test prop(props, "Thickness [m]") ≈ 5e-3 @test prop(props, "n(λ₀)") ≈ 1.5 @test prop(props, "λ₀ [m]") == get_default_wavelength() - @test prop(props, "Index model") == "function of λ" - props = BMO.properties(SphericalLens(50e-3, -50e-3, 5e-3, 25.4e-3, 1.5)) @test prop(props, "Index model") == "ConstantRefractiveIndex" - @test prop(props, "n(λ₀)") == 1.5 glass = DiscreteRefractiveIndex([1e-6, 2e-6], [1.5, 1.4]) @test prop(BMO.properties(SphericalLens(50e-3, -50e-3, 5e-3, 25.4e-3, glass)), "Index model") == "DiscreteRefractiveIndex" @@ -95,15 +92,15 @@ prop(props, name) = (i = findfirst(p -> p.first == name, props); isnothing(i) ? @test prop(BMO.properties(bs), "Reflectance") ≈ 0.3 @test prop(BMO.properties(bs), "Transmittance") ≈ 0.7 @test prop(BMO.properties(bs), "Size [m]") ≈ [10e-3, 20e-3] - pbs = RoundPlateBeamsplitter(25e-3, 3e-3, λ -> 1.5; reflectance = 0.4) + pbs = RoundPlateBeamsplitter(25e-3, 3e-3, ConstantRefractiveIndex(1.5); reflectance = 0.4) @test prop(BMO.properties(pbs), "Reflectance") ≈ 0.4 @test prop(BMO.properties(pbs), "Thickness [m]") ≈ 3e-3 @test prop(BMO.properties(pbs), "Parts") == 2 - @test prop(BMO.properties(CubeBeamsplitter(10e-3, λ -> 1.5; reflectance = 0.2)), "Reflectance") ≈ 0.2 + @test prop(BMO.properties(CubeBeamsplitter(10e-3, ConstantRefractiveIndex(1.5); reflectance = 0.2)), "Reflectance") ≈ 0.2 pf = PolarizationFilter(10e-3) @test prop(BMO.properties(pf), "Transmission axis") ≈ collect(transmission_axis(pf)) - lp = RoundLinearPolarizer(25e-3, 1e-3, 1e-3, λ -> 1.5) + lp = RoundLinearPolarizer(25e-3, 1e-3, 1e-3, ConstantRefractiveIndex(1.5)) @test prop(BMO.properties(lp), "Transmission axis") ≈ collect(transmission_axis(lp)) group = ObjectGroup([RoundPlanoMirror(25e-3, 5e-3), Detector(10e-3), Detector(5e-3)]) diff --git a/test/TestSharedParts.jl b/test/TestSharedParts.jl index c3216c385..b71c91d0e 100644 --- a/test/TestSharedParts.jl +++ b/test/TestSharedParts.jl @@ -207,7 +207,7 @@ Rz(θ) = BMO.rotate3d([0, 0, 1], θ) @test_throws ArgumentError translate3d!(Parts(Single(s), s), [0, 0, 1.0]) @test isapprox(position(s), [0, 0, 0]; atol = 1e-12) # Doublet and its own front lens - dl = SphericalDoubletLens(40mm, -30mm, -120mm, 6mm, 2.5mm, 25mm, _ -> 1.62, _ -> 1.72) + dl = SphericalDoubletLens(40mm, -30mm, -120mm, 6mm, 2.5mm, 25mm, ConstantRefractiveIndex(1.62), ConstantRefractiveIndex(1.72)) p_front, p_back = position(dl.front), position(dl.back) g = ObjectGroup([dl, dl.front]) @test_throws ArgumentError translate3d!(g, [0, 0, 1.0]) @@ -222,9 +222,9 @@ Rz(θ) = BMO.rotate3d([0, 0, 1], θ) end @testset "Built-in MultiShape objects in a group" begin - dl = SphericalDoubletLens(40mm, -30mm, -120mm, 6mm, 2.5mm, 25mm, _ -> 1.62, _ -> 1.72) - cbs = CubeBeamsplitter(10mm, n -> 1.5) - pbs = RectangularPlateBeamsplitter(36mm, 25mm, 1mm, n -> 1.5) + dl = SphericalDoubletLens(40mm, -30mm, -120mm, 6mm, 2.5mm, 25mm, ConstantRefractiveIndex(1.62), ConstantRefractiveIndex(1.72)) + cbs = CubeBeamsplitter(10mm, ConstantRefractiveIndex(1.5)) + pbs = RectangularPlateBeamsplitter(36mm, 25mm, 1mm, ConstantRefractiveIndex(1.5)) p = [position(x) for x in (dl.front, dl.back, cbs, pbs)] g = ObjectGroup([ObjectGroup([dl, cbs]), ObjectGroup([pbs, dl])]) translate3d!(g, [0, 0, 1.0]) diff --git a/test/TestSourceKinematics.jl b/test/TestSourceKinematics.jl index ccbf72f3c..8f156b612 100644 --- a/test/TestSourceKinematics.jl +++ b/test/TestSourceKinematics.jl @@ -492,7 +492,7 @@ start_rays(b) = map(BMO.first_ray, BMO._component_beams(b)) end @testset "Moving a child beam throws" begin - pbs = RectangularPlateBeamsplitter(36mm, 25mm, 1mm, n -> 1.5) + pbs = RectangularPlateBeamsplitter(36mm, 25mm, 1mm, ConstantRefractiveIndex(1.5)) zrotate3d!(pbs, deg2rad(45)) bs_system = System([pbs]) beam = Beam([0, -50mm, 0], [0, 1, 0], 1e-6) @@ -550,8 +550,8 @@ start_rays(b) = map(BMO.first_ray, BMO._component_beams(b)) BMO.CylinderSDF(1mm, 2mm) + BMO.CylinderSDF(2mm, 1mm), # composite SDF BMO.CubeMesh(1mm), # mesh ref, # object (SingleShape) - CubeBeamsplitter(5mm, λ -> 1.5), # object (MultiShape) - ObjectGroup([RoundPlanoMirror(10mm, 2mm), CubeBeamsplitter(5mm, λ -> 1.5)]), + CubeBeamsplitter(5mm, ConstantRefractiveIndex(1.5)), # object (MultiShape) + ObjectGroup([RoundPlanoMirror(10mm, 2mm), CubeBeamsplitter(5mm, ConstantRefractiveIndex(1.5))]), Ray([1.0, 0, 0], [0, 1.0, 0]), PolarizedRay([1.0, 0, 0], [0, 1.0, 0], 1e-6, [0, 0, 1.0]), Beam([1.0, 0, 0], [0, 1.0, 0]), diff --git a/test/TestUtils.jl b/test/TestUtils.jl index 657b82b2f..718488112 100644 --- a/test/TestUtils.jl +++ b/test/TestUtils.jl @@ -7,6 +7,9 @@ using GeometryBasics const BMO = BeamletOptics +# Subtype without a functor, for the fallback of the refractive index contract +struct DummyRefractiveIndex <: BMO.AbstractRefractiveIndex end + @testset "Utilities" begin @testset "Testing normal3d" begin @testset "normal3d(v) with Array" begin @@ -281,11 +284,8 @@ const BMO = BeamletOptics end @testset "ConstantRefractiveIndex" begin - cri = ConstantRefractiveIndex(1.5) - @test cri isa BMO.RefractiveIndex - @test all(cri(λ) == 1.5 for λ in (1, 1f0, 532e-9)) + @test ConstantRefractiveIndex(1.5)(532e-9) == 1.5 @test ConstantRefractiveIndex(1.5f0)(532e-9) isa Float32 - @test isnothing(BMO.test_refractive_index_function(cri)) end @testset "SellmeierEquation" begin @@ -300,20 +300,21 @@ const BMO = BeamletOptics @test all(diff(n_vis) .< 0) end - @testset "Test ref. helper function" begin - f1(x::Float64) = x # fail - f2(x::Union{Int, Float64}) = x # fail - f3(x::Real) = "a" # fail - f4(x) = x, 1 # fail - f5(x) = x # pass - # Test if illegal functions are detected - @test_throws ArgumentError BMO.test_refractive_index_function(f1) - @test_throws ArgumentError BMO.test_refractive_index_function(f2) - @test_throws ArgumentError BMO.test_refractive_index_function(f3) - @test_throws ArgumentError BMO.test_refractive_index_function(f4) - @test isnothing(BMO.test_refractive_index_function(f5)) - @test isnothing(BMO.test_refractive_index_function(ref_index)) - @test isnothing(BMO.test_refractive_index_function(ref_sellm)) + @testset "AbstractRefractiveIndex contract" begin + # One example per concrete subtype in BMO, with wavelengths of each Real type in its domain + examples = Dict( + ConstantRefractiveIndex => (ConstantRefractiveIndex(1.5), (1, 1f-6, 532e-9)), + DiscreteRefractiveIndex => (ref_index, (lambdas[1], T2(lambdas[2]))), + SellmeierEquation => (ref_sellm, (1, 1f-6, 532e-9))) + models = [getfield(BMO, s) for s in names(BMO; all = true) if isdefined(BMO, s)] + filter!(x -> x isa Type && x <: BMO.AbstractRefractiveIndex && !isabstracttype(x), models) + @test Set(models) == Set(keys(examples)) + for (model, λs) in values(examples), λ in λs + @test model(λ) isa Real + end + + @test_throws ErrorException DummyRefractiveIndex()(532e-9) + @test_throws "DummyRefractiveIndex" DummyRefractiveIndex()(532e-9) end end From 0a0f9b928ebe1bc5b5cad603f8886bbe32b2385c Mon Sep 17 00:00:00 2001 From: uitt_hu Date: Mon, 2 Nov 2026 23:56:24 +0100 Subject: [PATCH 4/4] Address review round 3 of #213 - Asphere example and its printed copy use ConstantRefractiveIndex (F9) - Prose and the properties docstring describe constant indices (F11) - Comment on the conversion of the index type (F12) - TestBeamsplitters.jl back to LF line endings (F13) Co-Authored-By: Claude Opus 5.5 --- .../lens_assets/aspherical_lens_showcase.jl | 2 +- docs/src/basics/components/lenses.md | 2 +- docs/src/examples/double_gauss.md | 2 +- docs/src/examples/lens_groups.md | 2 +- src/OpticalComponents/Lenses.jl | 4 +- src/Properties.jl | 2 +- test/Components/TestBeamsplitters.jl | 406 +++++++++--------- 7 files changed, 210 insertions(+), 210 deletions(-) diff --git a/docs/src/assets/lens_assets/aspherical_lens_showcase.jl b/docs/src/assets/lens_assets/aspherical_lens_showcase.jl index 359ef1dd6..d6bc2defd 100644 --- a/docs/src/assets/lens_assets/aspherical_lens_showcase.jl +++ b/docs/src/assets/lens_assets/aspherical_lens_showcase.jl @@ -24,7 +24,7 @@ L3 = Lens( 1.5264e-5*(1e3)^15] # coeffs ), 0.7e-3, # center_thickness - n -> 1.580200 # refractive index + ConstantRefractiveIndex(1.580200) # refractive index ) xrotate3d!(L3, π/2) diff --git a/docs/src/basics/components/lenses.md b/docs/src/basics/components/lenses.md index 5c179184e..68be1a502 100644 --- a/docs/src/basics/components/lenses.md +++ b/docs/src/basics/components/lenses.md @@ -147,7 +147,7 @@ L3 = Lens( 1.5264e-5*(1e3)^15] # coeffs ), 0.7e-3, # center_thickness - n -> 1.580200 # refractive index + ConstantRefractiveIndex(1.580200) # refractive index ) ``` diff --git a/docs/src/examples/double_gauss.md b/docs/src/examples/double_gauss.md index 7a0a2dd9b..b79d3cb9f 100644 --- a/docs/src/examples/double_gauss.md +++ b/docs/src/examples/double_gauss.md @@ -30,7 +30,7 @@ translate3d!(l6, [0, l_6, 0]) system = StaticSystem([l1, l23, l45, l6]) ``` -Defining a [`StaticSystem`](@ref) will allow the compiler to generate more efficient code to solve this simulation. Note that the refractive indices above are given as anonymous functions. This is because no lens material is specified. Rather, these indices are unique to ``\lambda = 486.0~\text{nm}``. +Defining a [`StaticSystem`](@ref) will allow the compiler to generate more efficient code to solve this simulation. Note that the refractive indices above are given as [`ConstantRefractiveIndex`](@ref)s. This is because no lens material is specified. Rather, these indices are unique to ``\lambda = 486.0~\text{nm}``. In the next step, we will define a `Figure` and `Axis3` environment in which the ray-tracing results will be visualized. diff --git a/docs/src/examples/lens_groups.md b/docs/src/examples/lens_groups.md index 9ba7f1f73..85ef16d2d 100644 --- a/docs/src/examples/lens_groups.md +++ b/docs/src/examples/lens_groups.md @@ -98,7 +98,7 @@ source = UniformDiscSource([0,-10cm,0], [0,1,0], 6mm, 1e-6; num_rays=100) solve_system!(system, source) ``` -Note that the wavelength `1e-6` does not change the result of the tracing solution, since the refractive indices have not been specified as a function of the wavelength (e.g. via an anonymous function, [`DiscreteRefractiveIndex`](@ref) or [`SellmeierEquation`](@ref)). The ray paths can be visualized via the following code +Note that the wavelength `1e-6` does not change the result of the tracing solution, since the refractive indices are constant ([`ConstantRefractiveIndex`](@ref)) and not a dispersion model such as a [`DiscreteRefractiveIndex`](@ref) or a [`SellmeierEquation`](@ref). The ray paths can be visualized via the following code ```julia fig = Figure() diff --git a/src/OpticalComponents/Lenses.jl b/src/OpticalComponents/Lenses.jl index f9be6c209..a588f31a1 100644 --- a/src/OpticalComponents/Lenses.jl +++ b/src/OpticalComponents/Lenses.jl @@ -35,8 +35,8 @@ by specialized subtypes. abstract type AbstractRefractiveOptic{T, F} <: AbstractObject{T} end refractive_index(object::AbstractRefractiveOptic) = object.n -# The result is converted to the number type of the optic and the wavelength, since `n` may be any -# function: e.g. a `ForwardDiff.Dual` within automatic differentiation, else `Float64`. +# The result is converted to the number type of the optic and the wavelength, since a model `n` may +# return another one: e.g. a `ForwardDiff.Dual` within automatic differentiation, else `Float64`. function refractive_index(object::AbstractRefractiveOptic{T, <:AbstractRefractiveIndex}, λ::L) where {T, L <: Real} return convert(promote_type(T, L), object.n(λ))::promote_type(T, L) end diff --git a/src/Properties.jl b/src/Properties.jl index 3bf6037b9..9cdf59ee3 100644 --- a/src/Properties.jl +++ b/src/Properties.jl @@ -29,7 +29,7 @@ julia> BeamletOptics.properties(SphericalLens(50e-3, -50e-3, 5e-3, 25.4e-3)) "Optical axis" => [0.0, 1.0, 0.0] "Shape" => "UnionSDF" "Thickness [m]" => 0.005 - "Index model" => "function of λ" + "Index model" => "ConstantRefractiveIndex" "n(λ₀)" => 1.5 "λ₀ [m]" => 1.0e-6 ``` diff --git a/test/Components/TestBeamsplitters.jl b/test/Components/TestBeamsplitters.jl index 0cb2a91f8..86cf7df37 100644 --- a/test/Components/TestBeamsplitters.jl +++ b/test/Components/TestBeamsplitters.jl @@ -1,203 +1,203 @@ -module TestBeamsplitters - -using BeamletOptics -using LinearAlgebra -using Test - -const BMO = BeamletOptics - -const mm = 1e-3 - -@testset "Beamsplitters" begin - N0 = 1.5 - @testset "Testing RectangularPlateBeamsplitter with Beam" begin - # Init splitter - N0 = 1.5 - pbs = RectangularPlateBeamsplitter(36mm, 25mm, 1mm, ConstantRefractiveIndex(N0)) - system = System([pbs]) - beam = Beam([0, -50mm, 0], [0, 1, 0], 1e-6) - # Trace normally - zrotate3d!(pbs, deg2rad(45)) - # Keep geometry regressions from growing an unbounded beam tree in CI. - solve_system!(system, beam; depth_max=4) - - @testset "Test pos/dir" begin - @test position(pbs) == zeros(3) - @test orientation(pbs) ≈ orientation(pbs.substrate) - end - - @testset "Test children after tracing" begin - p = beam.rays - t = beam.children[1].rays - r = beam.children[2].rays - # no of rays - @test length(p) == 1 - @test length(t) == 2 - @test length(r) == 1 - # correct ref. index - @test all(BMO.refractive_index.(p) .== 1) - @test all(BMO.refractive_index.(t) .== [N0, 1]) - @test all(BMO.refractive_index.(r) .== 1) - # correct dir - @test BMO.direction(first(p)) ≈ BMO.direction(last(t)) - @test BMO.direction(first(r)) ≈ [1, 0, 0] - end - - # Solve again with the backside - zrotate3d!(pbs, π) - solve_system!(system, beam; depth_max=4) - - @testset "Test children after solving again" begin - p = beam.rays - t = beam.children[1].rays - r = beam.children[2].rays - # no of rays - @test length(p) == 2 - @test length(t) == 1 - @test length(r) == 2 - # correct ref. index - @test all(BMO.refractive_index.(p) .== [1, N0]) - @test all(BMO.refractive_index.(t) .== 1) - @test all(BMO.refractive_index.(r) .== [N0, 1]) - # correct dir - @test BMO.direction(first(p)) ≈ BMO.direction(last(t)) - @test BMO.direction(last(r)) ≈ [1, 0, 0] - end - end - - @testset "Testing CubeBeamsplitter with Beam" begin - # Init splitter - cbs = CubeBeamsplitter(25e-3, ConstantRefractiveIndex(N0)) - translate3d!(cbs, [0, 50mm, 0]) - system = System([cbs]) - beam = Beam([0, 0, 0], [0, 1, 0], 1e-6) - - @testset "Initial CBS tracing" begin - # Trace normally - solve_system!(system, beam) - # Test correct ray length, ref. indices, dirs - p = BMO.rays(beam) - t = BMO.rays(beam.children[1]) - r = BMO.rays(beam.children[2]) - - @test length(p) == 2 - @test length(r) == 2 - @test length(t) == 2 - @test BMO.refractive_index.(p) == [1, N0] - @test BMO.refractive_index.(t) == [N0, 1] - @test BMO.refractive_index.(r) == [N0, 1] - @test BMO.direction(last(t)) ≈ BMO.direction(first(p)) - @test BMO.direction(last(r)) ≈ [-1, 0, 0] - end - - @testset "Solve again after 45° CBS rotation" begin - # Solve again - zrotate3d!(cbs, π / 2) - solve_system!(system, beam) - - # Test correct ray dirs - p = BMO.rays(beam) - t = BMO.rays(beam.children[1]) - r = BMO.rays(beam.children[2]) - - @test BMO.direction(last(t)) ≈ BMO.direction(first(p)) - @test BMO.direction(last(t)) ≈ [0, 1, 0] - @test BMO.position(p[2]) ≈ [0, 50mm - 12.5mm, 0] - end - - @testset "Solve again, CBS backside" begin - # Solve again, backside - zrotate3d!(cbs, π / 2) - solve_system!(system, beam) - - # Test correct ray length, ref. indices, dirs - p = BMO.rays(beam) - t = BMO.rays(beam.children[1]) - r = BMO.rays(beam.children[2]) - - @test length(p) == 2 - @test length(r) == 2 - @test length(t) == 2 - @test BMO.refractive_index.(p) == [1, N0] - @test BMO.refractive_index.(t) == [N0, 1] - @test BMO.refractive_index.(r) == [N0, 1] - @test BMO.direction(last(t)) ≈ BMO.direction(first(p)) - @test BMO.direction(last(r)) ≈ [-1, 0, 0] - end - end - - @testset "Splitters and prisms with a constant refractive index" begin - prism = RightAnglePrism(25e-3, 20e-3, ConstantRefractiveIndex(N0)) - @test prism isa Prism - @test BMO.refractive_index(prism, 1e-6) == N0 - @test BMO.refractive_index(prism, 500e-9) == N0 - @test BMO.refractive_index(prism) == ConstantRefractiveIndex(N0) - for substrate in (RectangularPlateBeamsplitter(25e-3, 25e-3, 5e-3, ConstantRefractiveIndex(N0)).substrate, - RoundPlateBeamsplitter(25e-3, 5e-3, ConstantRefractiveIndex(N0)).substrate, - RectangularCompensatorPlate(25e-3, 25e-3, 5e-3, ConstantRefractiveIndex(N0))) - @test BMO.refractive_index(substrate) == ConstantRefractiveIndex(N0) - end - - cbs = CubeBeamsplitter(25e-3, ConstantRefractiveIndex(N0); reflectance = 0.3) - @test BMO.refractive_index(cbs, 1e-6) == N0 - @test BMO.refractive_index(cbs, 500e-9) == N0 - translate3d!(cbs, [0, 50mm, 0]) - beam = Beam([0, 0, 0], [0, 1, 0], 1e-6) - solve_system!(System([cbs]), beam) - @test BMO.refractive_index.(BMO.rays(beam)) == [1, N0] - @test length(beam.children) == 2 - end - - @testset "depth_max branch limiting" begin - beamsplitter = CubeBeamsplitter(25e-3, ConstantRefractiveIndex(N0)) - translate3d!(beamsplitter, [0, 50mm, 0]) - system = System([beamsplitter]) - - beam = Beam([0, 0, 0], [0, 1, 0], 1e-6) - solve_system!(system, beam; depth_max=0) - @test isempty(beam.children) - - beam = Beam([0, 0, 0], [0, 1, 0], 1e-6) - solve_system!(system, beam; depth_max=1) - @test length(beam.children) == 2 - end - - @testset "depth_max default" begin - beamsplitter = CubeBeamsplitter(25e-3, ConstantRefractiveIndex(N0)) - translate3d!(beamsplitter, [0, 50mm, 0]) - system = System([beamsplitter]) - beam = Beam([0, 0, 0], [0, 1, 0], 1e-6) - - @test BMO.get_default_depth_max() == 100 - solve_system!(system, beam) - @test length(beam.children) == 2 - end - - @testset "Field of a polarized ray behind the plate" begin - # the coating transmits half of the power into the glass, the uncoated back takes its - # Fresnel loss: the field of the transmitted ray is transverse and has this amplitude - n = 1.5 - θ_i = π / 4 - θ_t = asin(sin(θ_i) / n) - r_s = (cos(θ_i) - n * cos(θ_t)) / (cos(θ_i) + n * cos(θ_t)) - r_p = (n * cos(θ_i) - cos(θ_t)) / (n * cos(θ_i) + cos(θ_t)) - for (e, R_back) in (([0.0, 0, 1], r_s^2), ([1.0, 0, 0], r_p^2)) - pbs = RectangularPlateBeamsplitter(25e-3, 25e-3, 5e-3, ConstantRefractiveIndex(n)) - zrotate3d!(pbs, π / 4) - beam = Beam(PolarizedRay([0.0, -0.1, 0], [0.0, 1, 0], 1e-6, e)) - solve_system!(System([pbs]), beam) - transmitted, reflected = BMO.children(beam) - inside, outside = BMO.rays(transmitted) - @test BMO.refractive_index(inside) == n - for ray in (inside, outside, first(BMO.rays(reflected))) - @test abs(dot(BMO.direction(ray), BMO.polarization(ray))) < 1e-12 - end - @test sum(abs2, BMO.polarization(first(BMO.rays(reflected)))) ≈ 1 / 2 - @test sum(abs2, BMO.polarization(outside)) ≈ (1 - R_back) / 2 - # in the glass the power is n cos(θ) |E|² per area of the surface - @test n * cos(θ_t) * sum(abs2, BMO.polarization(inside)) ≈ cos(θ_i) / 2 - end - end -end - -end +module TestBeamsplitters + +using BeamletOptics +using LinearAlgebra +using Test + +const BMO = BeamletOptics + +const mm = 1e-3 + +@testset "Beamsplitters" begin + N0 = 1.5 + @testset "Testing RectangularPlateBeamsplitter with Beam" begin + # Init splitter + N0 = 1.5 + pbs = RectangularPlateBeamsplitter(36mm, 25mm, 1mm, ConstantRefractiveIndex(N0)) + system = System([pbs]) + beam = Beam([0, -50mm, 0], [0, 1, 0], 1e-6) + # Trace normally + zrotate3d!(pbs, deg2rad(45)) + # Keep geometry regressions from growing an unbounded beam tree in CI. + solve_system!(system, beam; depth_max=4) + + @testset "Test pos/dir" begin + @test position(pbs) == zeros(3) + @test orientation(pbs) ≈ orientation(pbs.substrate) + end + + @testset "Test children after tracing" begin + p = beam.rays + t = beam.children[1].rays + r = beam.children[2].rays + # no of rays + @test length(p) == 1 + @test length(t) == 2 + @test length(r) == 1 + # correct ref. index + @test all(BMO.refractive_index.(p) .== 1) + @test all(BMO.refractive_index.(t) .== [N0, 1]) + @test all(BMO.refractive_index.(r) .== 1) + # correct dir + @test BMO.direction(first(p)) ≈ BMO.direction(last(t)) + @test BMO.direction(first(r)) ≈ [1, 0, 0] + end + + # Solve again with the backside + zrotate3d!(pbs, π) + solve_system!(system, beam; depth_max=4) + + @testset "Test children after solving again" begin + p = beam.rays + t = beam.children[1].rays + r = beam.children[2].rays + # no of rays + @test length(p) == 2 + @test length(t) == 1 + @test length(r) == 2 + # correct ref. index + @test all(BMO.refractive_index.(p) .== [1, N0]) + @test all(BMO.refractive_index.(t) .== 1) + @test all(BMO.refractive_index.(r) .== [N0, 1]) + # correct dir + @test BMO.direction(first(p)) ≈ BMO.direction(last(t)) + @test BMO.direction(last(r)) ≈ [1, 0, 0] + end + end + + @testset "Testing CubeBeamsplitter with Beam" begin + # Init splitter + cbs = CubeBeamsplitter(25e-3, ConstantRefractiveIndex(N0)) + translate3d!(cbs, [0, 50mm, 0]) + system = System([cbs]) + beam = Beam([0, 0, 0], [0, 1, 0], 1e-6) + + @testset "Initial CBS tracing" begin + # Trace normally + solve_system!(system, beam) + # Test correct ray length, ref. indices, dirs + p = BMO.rays(beam) + t = BMO.rays(beam.children[1]) + r = BMO.rays(beam.children[2]) + + @test length(p) == 2 + @test length(r) == 2 + @test length(t) == 2 + @test BMO.refractive_index.(p) == [1, N0] + @test BMO.refractive_index.(t) == [N0, 1] + @test BMO.refractive_index.(r) == [N0, 1] + @test BMO.direction(last(t)) ≈ BMO.direction(first(p)) + @test BMO.direction(last(r)) ≈ [-1, 0, 0] + end + + @testset "Solve again after 45° CBS rotation" begin + # Solve again + zrotate3d!(cbs, π / 2) + solve_system!(system, beam) + + # Test correct ray dirs + p = BMO.rays(beam) + t = BMO.rays(beam.children[1]) + r = BMO.rays(beam.children[2]) + + @test BMO.direction(last(t)) ≈ BMO.direction(first(p)) + @test BMO.direction(last(t)) ≈ [0, 1, 0] + @test BMO.position(p[2]) ≈ [0, 50mm - 12.5mm, 0] + end + + @testset "Solve again, CBS backside" begin + # Solve again, backside + zrotate3d!(cbs, π / 2) + solve_system!(system, beam) + + # Test correct ray length, ref. indices, dirs + p = BMO.rays(beam) + t = BMO.rays(beam.children[1]) + r = BMO.rays(beam.children[2]) + + @test length(p) == 2 + @test length(r) == 2 + @test length(t) == 2 + @test BMO.refractive_index.(p) == [1, N0] + @test BMO.refractive_index.(t) == [N0, 1] + @test BMO.refractive_index.(r) == [N0, 1] + @test BMO.direction(last(t)) ≈ BMO.direction(first(p)) + @test BMO.direction(last(r)) ≈ [-1, 0, 0] + end + end + + @testset "Splitters and prisms with a constant refractive index" begin + prism = RightAnglePrism(25e-3, 20e-3, ConstantRefractiveIndex(N0)) + @test prism isa Prism + @test BMO.refractive_index(prism, 1e-6) == N0 + @test BMO.refractive_index(prism, 500e-9) == N0 + @test BMO.refractive_index(prism) == ConstantRefractiveIndex(N0) + for substrate in (RectangularPlateBeamsplitter(25e-3, 25e-3, 5e-3, ConstantRefractiveIndex(N0)).substrate, + RoundPlateBeamsplitter(25e-3, 5e-3, ConstantRefractiveIndex(N0)).substrate, + RectangularCompensatorPlate(25e-3, 25e-3, 5e-3, ConstantRefractiveIndex(N0))) + @test BMO.refractive_index(substrate) == ConstantRefractiveIndex(N0) + end + + cbs = CubeBeamsplitter(25e-3, ConstantRefractiveIndex(N0); reflectance = 0.3) + @test BMO.refractive_index(cbs, 1e-6) == N0 + @test BMO.refractive_index(cbs, 500e-9) == N0 + translate3d!(cbs, [0, 50mm, 0]) + beam = Beam([0, 0, 0], [0, 1, 0], 1e-6) + solve_system!(System([cbs]), beam) + @test BMO.refractive_index.(BMO.rays(beam)) == [1, N0] + @test length(beam.children) == 2 + end + + @testset "depth_max branch limiting" begin + beamsplitter = CubeBeamsplitter(25e-3, ConstantRefractiveIndex(N0)) + translate3d!(beamsplitter, [0, 50mm, 0]) + system = System([beamsplitter]) + + beam = Beam([0, 0, 0], [0, 1, 0], 1e-6) + solve_system!(system, beam; depth_max=0) + @test isempty(beam.children) + + beam = Beam([0, 0, 0], [0, 1, 0], 1e-6) + solve_system!(system, beam; depth_max=1) + @test length(beam.children) == 2 + end + + @testset "depth_max default" begin + beamsplitter = CubeBeamsplitter(25e-3, ConstantRefractiveIndex(N0)) + translate3d!(beamsplitter, [0, 50mm, 0]) + system = System([beamsplitter]) + beam = Beam([0, 0, 0], [0, 1, 0], 1e-6) + + @test BMO.get_default_depth_max() == 100 + solve_system!(system, beam) + @test length(beam.children) == 2 + end + + @testset "Field of a polarized ray behind the plate" begin + # the coating transmits half of the power into the glass, the uncoated back takes its + # Fresnel loss: the field of the transmitted ray is transverse and has this amplitude + n = 1.5 + θ_i = π / 4 + θ_t = asin(sin(θ_i) / n) + r_s = (cos(θ_i) - n * cos(θ_t)) / (cos(θ_i) + n * cos(θ_t)) + r_p = (n * cos(θ_i) - cos(θ_t)) / (n * cos(θ_i) + cos(θ_t)) + for (e, R_back) in (([0.0, 0, 1], r_s^2), ([1.0, 0, 0], r_p^2)) + pbs = RectangularPlateBeamsplitter(25e-3, 25e-3, 5e-3, ConstantRefractiveIndex(n)) + zrotate3d!(pbs, π / 4) + beam = Beam(PolarizedRay([0.0, -0.1, 0], [0.0, 1, 0], 1e-6, e)) + solve_system!(System([pbs]), beam) + transmitted, reflected = BMO.children(beam) + inside, outside = BMO.rays(transmitted) + @test BMO.refractive_index(inside) == n + for ray in (inside, outside, first(BMO.rays(reflected))) + @test abs(dot(BMO.direction(ray), BMO.polarization(ray))) < 1e-12 + end + @test sum(abs2, BMO.polarization(first(BMO.rays(reflected)))) ≈ 1 / 2 + @test sum(abs2, BMO.polarization(outside)) ≈ (1 - R_back) / 2 + # in the glass the power is n cos(θ) |E|² per area of the surface + @test n * cos(θ_t) * sum(abs2, BMO.polarization(inside)) ≈ cos(θ_i) / 2 + end + end +end + +end