Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions docs/src/basics/components/detectors.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,30 @@ Detector

After solving a system containing a `Detector`, the methods listed below can be used in order to analyze the stored data. If no data is obtained during the tracing procedure, an error message will be stored.

## Reading out a detector

A detector stores *hits*. The following rules define what a readout computes.

1. A hit holds the values of the ray or beamlet at the moment of the solve. Solving again, or moving a source or any other component, does not change hits that are already stored.
2. Any kinematic call on the detector (e.g. [`translate3d!`](@ref) or [`rotate3d!`](@ref)) empties it, also when it is moved as part of an [`ObjectGroup`](@ref), since its hits would no longer lie on its surface.
3. A detector can hold the hits of solves that follow each other in time side by side. It is up to the user when to evaluate them. [`spot_diagram`](@ref) shows all hits, which is harmless. [`electric_field`](@ref), [`intensity`](@ref) and [`optical_power`](@ref) superpose all hits **coherently**. This is right for beams that exist at the same time, e.g. a beam group or an interferometer. For solves that follow each other in time, the user must know what this computes, or evaluate after each solve and reset the detector (`initialize = true`, [`initialize!`](@ref) or [`empty!`](@ref)).

```julia
# Reset and evaluate after every solve
for y in ys
translate_to3d!(mirror, [0, y, 0])
solve_system!(system, source; initialize = true)
push!(powers, optical_power(pd))
end

# Solve several times, evaluate once: the spots of all solves
for _ in 1:10
zrotate3d!(source, deg2rad(1))
solve_system!(system, source)
end
spots = spot_diagram(pd)
```

## Spot diagrams

The `spot_diagram` method provides a straight forward way to generate spot diagrams, which are commonly used to perform initial assessments of the optical performance of an imaging setup.
Expand Down
5 changes: 3 additions & 2 deletions ext/BeamletOpticsOpticsBaseExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ const _PREFIX = "BeamletOptics/OpticsBase extension"
origin = BeamletOptics.position(detector), axes = <default detector frame, see below>)

Samples the coherent field of the beamlets hitting a BeamletOptics (BMO) `Detector` into
an [`OpticsBase.PlaneField`](https://StackEnjoyer.github.io/OpticsBase.jl/dev/). Call it after `solve_system!` and before moving any object, solving again or
emptying the detector. Requires `using OpticsBase` (package extension).
an [`OpticsBase.PlaneField`](https://StackEnjoyer.github.io/OpticsBase.jl/dev/). Call it after `solve_system!`. The hits keep the state of
their solve: solving again or moving other objects does not change them, moving the detector
empties it. Requires `using OpticsBase` (package extension).

# Arguments

Expand Down
2 changes: 1 addition & 1 deletion skills/beamletoptics/CHECKLIST.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
- [ ] Lens radius signs follow ISO 10110 (biconvex: `r1 > 0`, `r2 < 0`).
- [ ] Geometry assumes +y as the optical axis; rotations are cumulative and relative.
- [ ] Sources start **outside** of all elements and point at them.
- [ ] Detectors emptied before every re-solve in a loop (`initialize = true`, `initialize!(system)` or `empty!(detector)`); results read out before the detector is moved.
- [ ] Detectors emptied before every re-solve in a loop (`initialize = true`, `initialize!(system)` or `empty!(detector)`), unless the hits of all solves are wanted together (e.g. the spots of a scan); `electric_field`, `intensity` and `optical_power` superpose all stored hits coherently; moving the detector empties it.
- [ ] Beam model fits the question (see below).
- [ ] Script was **run**, and the printed numbers were compared with a paraxial or analytic estimate.
- [ ] For non-trivial geometry, a rendered image was checked.
Expand Down
2 changes: 1 addition & 1 deletion skills/beamletoptics/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ When this Skill is active:
`λ -> 1.5`, `DiscreteRefractiveIndex(...)` or `SellmeierEquation(...)`.
- **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.
between sources that should superpose. Field functions superpose all stored hits coherently; moving the detector empties it.
- **All kinematics are relative** (`translate3d!`, `xrotate3d!`, …), angles in radians (`deg2rad`).
Use `translate_to3d!` for absolute positioning.
- Run the script after writing it. A simulation that "looks right" but was never executed is not done.
Expand Down
3 changes: 2 additions & 1 deletion skills/beamletoptics/WORKFLOW.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ for y in ys
end
```

- Read everything you need from a detector **before** moving anything or solving again (hits reference beam state).
- Hits are values of the solve: solving again or moving anything but the detector does not change them. Moving the detector (also via a group) empties it.
- A detector holds the hits of consecutive solves side by side; you decide when to evaluate. `spot_diagram` shows all hits; `electric_field`, `intensity`, `optical_power` superpose all hits **coherently**. For solves that follow each other in time, know what this computes, or evaluate after each solve and reset (`initialize = true`).
- See `templates/01_singlet_spot_diagram.jl` (focus scan) and `templates/04_michelson_scan.jl` (fringes).

## Interferometers and coherent sums
Expand Down
6 changes: 4 additions & 2 deletions skills/beamletoptics/components/detectors.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ Detector(edge_length, stop = true)
it catches beams travelling along −x.
- With `stop = false`, beams continue behind the detector.
- Stores *hits* of exactly **one** type (Ray, PolarizedRay, GaussianBeamlet or AstigmaticGaussianBeamlet).
- **Accumulates across solves:** solve with `initialize = true`, or call `initialize!(system)` (all detectors of a system, nested groups included) or `empty!(det)` (one detector) before each `solve_system!` that reuses it. By default the solver does not reset detectors: all beams of a beam group, and several sources solved one after another, superpose on purpose.
- Read out all results before moving the detector again.
- **Hits are values** of the beam state at the solve. Solving again, or moving a source, mirror or lens, never changes stored hits.
- **Moving the detector empties it:** every kinematic call on it (also a move of a group containing it) clears its hits.
- **Accumulates across solves:** by default the solver does not reset detectors, so hits of consecutive solves sit side by side and all beams of a beam group, and several sources solved one after another, superpose on purpose. The user decides when to evaluate. To start empty, solve with `initialize = true`, or call `initialize!(system)` (all detectors of a system, nested groups included) or `empty!(det)` (one detector).
- `spot_diagram` shows all stored hits. `electric_field`, `intensity` and `optical_power` superpose **all** stored hits **coherently**, as for beams that exist at the same time. For solves that follow each other in time, know what this computes, or evaluate after each solve and reset (`initialize = true`).

## Readout

Expand Down
18 changes: 18 additions & 0 deletions src/AbstractTypes/AbstractRay.jl
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,24 @@ end

Base.empty!(ray::AbstractRay) = intersection!(ray, nothing)

"""
hit_point(ray::AbstractRay)

Returns the point where the `ray` ends, i.e. where it hits the surface of its [`Intersection`](@ref).
"""
hit_point(ray::AbstractRay) = position(ray) + length(ray) * direction(ray)

"""
projection_factor(ray::AbstractRay)

Returns `|d · n|` of the direction `d` of the `ray` and the normal `n` of the surface it ends on;
`1` if the ray ends without an intersection.
"""
function projection_factor(ray::AbstractRay{T}) where {T}
isnothing(intersection(ray)) && return one(T)
return abs(dot(direction(ray), normal3d(intersection(ray))))
end

"""
translate3d!(::Movable, ray::AbstractRay, offset)

Expand Down
2 changes: 1 addition & 1 deletion src/Beam.jl
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ function Base.show(io::IO, ::MIME"text/plain", beam::Beam)
println(io, " Intersects with $(typeof(object(intersection(ray))))")
println(io, " Pos.: $(position(ray))")
println(io, " Dir.: $(direction(ray))")
println(io, " End.: $(position(ray) .+ length(ray) .* direction(ray))")
println(io, " End.: $(hit_point(ray))")
end
end
return nothing
Expand Down
98 changes: 69 additions & 29 deletions src/Gaussian.jl
Original file line number Diff line number Diff line change
Expand Up @@ -302,40 +302,80 @@ function gauss_parameters(
)
p0, index = hint
chief = gauss.chief.rays[index]
div = gauss.divergence.rays[index]
waist = gauss.waist.rays[index]
w, R, ψ, w0 = gauss_parameters(GaussianSegment(gauss, index), dot(p0 - position(chief), direction(chief)))
return (w, R, ψ + ψ0, w0)
end

"""
GaussianSegment{T}

The waist and divergence rays of one segment of a [`GaussianBeamlet`](@ref), as values relative
to its chief ray: on the plane normal to the chief ray at the distance `l` behind its start, an
auxiliary ray with direction `a` crosses at the transverse offset `y0 + l * v` from the chief ray.
Both rays are straight within the segment, hence this holds exactly. `n` is the refractive index
of the segment and `λ` the vacuum wavelength. [`gauss_parameters`](@ref)`(segment, l)` evaluates it,
a [`GaussianBeamletHit`](@ref) stores it.
"""
struct GaussianSegment{T}
y0_w::Point3{T}
v_w::Point3{T}
a_w::Point3{T}
y0_d::Point3{T}
v_d::Point3{T}
a_d::Point3{T}
n::T
λ::T
end

function GaussianSegment(gauss::GaussianBeamlet{T}, index::Integer) where {T}
chief = gauss.chief.rays[index]
p, d = position(chief), direction(chief)
y0_w, v_w, a_w = _crossing(p, d, gauss.waist.rays[index])
y0_d, v_d, a_d = _crossing(p, d, gauss.divergence.rays[index])
return GaussianSegment{T}(y0_w, v_w, a_w, y0_d, v_d, a_d, refractive_index(chief), wavelength(gauss))
end

# Offset `y0 + l v` from the chief ray (start `p`, direction `d`) at which `ray` crosses the plane
# normal to `d` at the distance `l` behind `p`, and the direction of `ray`
function _crossing(p, d, ray)
a = direction(ray)
c = dot(d, a)
return position(ray) + dot(p - position(ray), d) / c * a - p, a / c - d, a
end

# Height and slope of an auxiliary ray with the offset `y0 + l v` and the direction `a`: the slope
# is tan(π/2 − θ) = cot θ of the angle θ between the offset and `a`
function _height_slope(y0, v, a, l)
y = y0 + l * v
h = norm(y)
ŷ = y / h
return h, dot(ŷ, a) / norm(cross(ŷ, a))
end

"""
gauss_parameters(segment::GaussianSegment, l)

Returns `(w, R, ψ, w0)` of the [`GaussianSegment`](@ref) at the distance `l` behind the start of
its chief ray, as [`gauss_parameters`](@ref)`(gauss, z)` does, but with the Gouy phase `ψ` relative
to the local waist of the segment.
"""
function gauss_parameters(s::GaussianSegment, l::Real)
#=
Divergence ray height and slope (same for waist ray)
- find divergence ray "height" and "slope" at intersection point y0 with target plane at p0 of chief ray
- ray height "y_d" is length between p0 and y0
- ray slope "m_d" is angle between vector p0 -> y0 and divergence ray direction -> gives unambiguous angle for signed ray slope calculation
- fails if y_d is zero -> catch R=Inf, ψ=0 and H=λ/π
Height y and slope m of the divergence and waist rays on the plane at l
- the height is the distance between the chief ray and the crossing point of the ray
- the slope is the cotangent of the angle between the offset and the ray direction, which
gives the signed slope
- fails if a height is zero -> catch R=Inf, ψ=0 and H=λ/π
=#
intersection_len = line_plane_distance3d(p0,
direction(chief),
position(div),
direction(div))
y0 = position(div) + intersection_len * direction(div) - p0
y_d = norm(y0)
y0 /= y_d
m_d = tan(π / 2 - angle3d(y0, direction(div)))
# Waist ray height and slope
intersection_len = line_plane_distance3d(p0,
direction(chief),
position(waist),
direction(waist))
y0 = position(waist) + intersection_len * direction(waist) - p0
y_w = norm(y0)
y0 /= y_w
m_w = tan(π / 2 - angle3d(y0, direction(waist)))
y_d, m_d = _height_slope(s.y0_d, s.v_d, s.a_d, l)
y_w, m_w = _height_slope(s.y0_w, s.v_w, s.a_w, l)
# Beam parameters as per Arnaud (1985) and DeJager (1992)
n = refractive_index(chief)
n = s.n
H = abs(n * (y_w * m_d - y_d * m_w))
# Test optical invariant
λ = wavelength(gauss)
λ = s.λ
if !isapprox(H, λ / π, atol = Config.get_invariant_threshold())
H = λ / π
# println("H not fulfilled at z=$z")
end
E_kt = y_d * m_d + y_w * m_w
F_kt = sqrt(m_d^2 + m_w^2)
Expand All @@ -349,7 +389,7 @@ function gauss_parameters(
isnan(ψ) && (ψ = zero(ψ))
isnan(w0) && (w0 = w)
R < 0 && (ψ = -ψ)
return (w, R, ψ + ψ0, w0)
return (w, R, ψ, w0)
end

"""
Expand Down Expand Up @@ -437,7 +477,7 @@ _local_gouy_start(gauss::GaussianBeamlet{T}, index::Integer) where {T} =
function _local_gouy_end(gauss::GaussianBeamlet{T}, index::Integer) where {T}
ray = rays(gauss.chief)[index]
return gauss_parameters(gauss, zero(T), zero(T);
hint = (position(ray) + length(ray) * direction(ray), index))[3]
hint = (hit_point(ray), index))[3]
end

"""
Expand Down
8 changes: 4 additions & 4 deletions src/OpticalComponents/Beamsplitters/ThinBeamsplitter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -74,23 +74,23 @@ Base.isvalid(bs::AbstractBeamsplitter) = reflectance(bs)^2 + transmittance(bs)^2

@inline function _beamsplitter_transmitted_beam(
::AbstractBeamsplitter, ::Beam{T, R}, ray::R) where {T <: Real, R <: Ray{T}}
pos = position(ray) + length(ray) * direction(ray)
pos = hit_point(ray)
dir = direction(ray)
return Beam(Ray(pos, dir, wavelength(ray)))
end

@inline function _beamsplitter_reflected_beam(
::AbstractBeamsplitter, ::Beam{T, R}, ray::R) where {T <: Real, R <: Ray{T}}
normal = normal3d(intersection(ray))
pos = position(ray) + length(ray) * direction(ray)
pos = hit_point(ray)
dir = reflection3d(direction(ray), normal)
return Beam(Ray(pos, dir, wavelength(ray)))
end

@inline function _beamsplitter_transmitted_beam(bs::AbstractBeamsplitter, ::Beam{T, R},
ray::R) where {T <: Real, R <: PolarizedRay{T}}
J = SPBasis(transmittance(bs), 0, 0, transmittance(bs))
pos = position(ray) + length(ray) * direction(ray)
pos = hit_point(ray)
dir = direction(ray)
E0 = _calculate_global_E0(bs, ray, dir, J)
return Beam(PolarizedRay(pos, dir, wavelength(ray), E0))
Expand All @@ -100,7 +100,7 @@ end
ray::R) where {T <: Real, R <: PolarizedRay{T}}
J = SPBasis(-reflectance(bs), 0, 0, reflectance(bs))
normal = normal3d(intersection(ray))
pos = position(ray) + length(ray) * direction(ray)
pos = hit_point(ray)
in_dir = direction(ray)
out_dir = reflection3d(in_dir, normal)
E0 = _calculate_global_E0(bs, ray, out_dir, J)
Expand Down
Loading
Loading