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
4 changes: 2 additions & 2 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Evaluate every API and architecture decision against this. The core principles,

**The extension promise:** a developer defines a new `AbstractObject` subtype and its
`interact3d(system, object, beam, ray)` method (plus `intersect3d` if it needs custom
geometry), and the rest of the API (kinematics, threading, retracing) works without further
geometry), and the rest of the API (kinematics, threading) works without further
integration. A component may also add a `card_rows` method (and `card_actions`) to show its own rows
on its card in the interactive GUI, which lives in the separate package
[BeamletOpticsGUI](https://github.com/StackEnjoyer/BeamletOpticsGUI.jl) (recipe and developer
Expand All @@ -53,7 +53,7 @@ doublets) is handled per component by returning a `Hint`

- `src/AbstractTypes/`: the interfaces (`AbstractObject`, `AbstractShape`, shape traits,
`AbstractRay`/`Intersection`, `AbstractBeam`, `AbstractSystem`/`Hint`, kinematic trait).
- `src/System.jl`: the intersect-interact loop, tracing and retracing.
- `src/System.jl`: the intersect-interact loop and tracing.
- `src/Rays.jl`, `PolarizedRays.jl`, `Beam.jl`, `Gaussian.jl`, `AstigmaticGaussian.jl`,
`BeamGroups/`: beam models and sources.
- `src/OpticalComponents/`: components, one family per folder or file.
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "BeamletOptics"
uuid = "c387492b-ffec-4e51-9a46-bd230226031c"
version = "0.13.13"
version = "0.14.0"
authors = ["Hugo Uittenbosch <hugo.uittenbosch@dlr.de>, Oliver Kliebisch <oliver.kliebisch@dlr.de> and Thomas Dekorsy <thomas.dekorsy@dlr.de>"]

[deps]
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

# Features

- Hybrid sequential and non-sequential 3D ray tracing without paraxial approximation
- Non-sequential 3D ray tracing without paraxial approximation
- TEM₀₀ Gaussian beamlet models
- Various optical components
- Mirrors
Expand Down
2 changes: 1 addition & 1 deletion docs/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ url = "https://github.com/StackEnjoyer/OpticsBase.jl"
rev = "main"

[compat]
BeamletOptics = "0.13"
BeamletOptics = "0.14"
CairoMakie = "0.15"
Documenter = "1.11"
DocumenterCitations = "1.5"
Expand Down
37 changes: 5 additions & 32 deletions docs/src/api/core.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ The first two principles will be elaborated upon in more detail in the [Geometry
flowchart TB
S(["<b>solve_system!</b><br/>system + beam"]) --> I
I["<b>1. Intersect</b><br/>hinted shape first,<br/>else all objects"] -- hit --> X["<b>2. Interact</b><br/>compute<br/>AbstractInteraction"]
X --> R["<b>3. Repeat</b><br/>attach / overwrite next<br/>ray or beam segment"]
X --> R["<b>3. Repeat</b><br/>attach next<br/>ray or beam segment"]
R -- "optional Hint" --> I
I -- "no hit or r_max" --> E([done])

Expand All @@ -34,10 +34,10 @@ This scheme is loosely referred to as the **Intersect-Interact-Repeat-Loop** and

1. Calculate the closest **Intersection** between a ray/beam and the objects within the system
2. Calculate the optical **Interaction** that occurs at the surface or within the volume of the element
3. Attach or overwrite the next part of the ray chain or beam tree
3. Attach the next part of the ray chain or beam tree
4. Use the new information to repeat 1.

Once this procedure has been completed, the alignment of the system or other time-dependent optical properties (e.g. the phase of a Gaussian beamlet) can be updated. When rerunning the solver, the algorithm will try to reuse information about the previously intersected objects to speed up the calculation of the next simulation step. This is described in more detail in the sections: [Tracing systems](@ref) and [Retracing systems](@ref).
Once this procedure has been completed, the alignment of the system or other time-dependent optical properties (e.g. the phase of a Gaussian beamlet) can be updated. A new call of [`solve_system!`](@ref) solves the beam again from its start. This is described in more detail in the section: [Tracing systems](@ref).

The next sections will focus on the **Intersection** and **Interaction** steps.

Expand Down Expand Up @@ -119,37 +119,10 @@ In the initial state, is is assumed that the problem consists of `objects <: Abs
BeamletOptics.trace_system!
```

This non-sequential mode is comparatively safe in determining the "true" beam path, but will scale suboptimally in time-complexity with the amount of optical elements. After solving the system, the beam path is known and can be potentially reused in the future.
This non-sequential mode is comparatively safe in determining the "true" beam path, but will scale suboptimally in time-complexity with the amount of optical elements.

!!! info "Object order"
Unlike with classic, surface-based ray tracers, the order in which objects are listed in the [`System`](@ref) object vector/tuple is not considered for the purpose of tracing or retracing.

### Retracing systems

Once a system has been traced for the first time, the system and beam can be solved again. However, this time the solver will try to reuse as much information from the previous run as possible by testing if the previous beam trajectory is still valid in a sequential tracing mode. Retracing systems assumes that the kinematic changes (e.g. optomechanical aligment) between the current tracing procedure and the previous one are small. If an intersection along the beam trajectory becomes invalid, the solver will perform a non-sequential trace for all invalidated parts of the beam.

```mermaid
flowchart TB
A(["<b>retrace_system!</b>"]) --> B{"Beam path<br/>known?"}
B -- "no, first trace" --> C["<b>Brute-force</b><br/>trace_system!"]
B -- "yes, reuse hints" --> D["<b>Sequential retrace</b><br/>along known path"]
D --> E{"Previous<br/>intersections<br/>still valid?"}
E -- "no" --> G["<b>Brute-force</b><br/>trace of invalidated<br/>segments"]
E -- "yes" --> F([done])
G --> F
C --> F

classDef blue fill:#4063D826,stroke:#4063D8,stroke-width:2px
classDef purple fill:#9558B226,stroke:#9558B2,stroke-width:2px
classDef terminal fill:transparent,stroke:#CB3C33,stroke-width:1.5px,stroke-dasharray:4 3
class C,G blue
class D purple
class A,F terminal
```

```@docs; canonical=false
BeamletOptics.retrace_system!
```
Unlike with classic, surface-based ray tracers, the order in which objects are listed in the [`System`](@ref) object vector/tuple is not considered for the purpose of tracing.

## CPU and GPU support

Expand Down
20 changes: 0 additions & 20 deletions docs/src/assets/mi_assets/michelson_showcase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,6 @@ const BMO = BeamletOptics
const cm = 1e-2
const mm = 1e-3

function reset_beamlet!(beam::GaussianBeamlet)
BeamletOptics._drop_beams!(beam)
r_c = first(BeamletOptics.rays(beam.chief))
r_w = first(BeamletOptics.rays(beam.waist))
r_d = first(BeamletOptics.rays(beam.divergence))
BeamletOptics.intersection!(r_c, nothing)
BeamletOptics.intersection!(r_w, nothing)
BeamletOptics.intersection!(r_d, nothing)
beam.chief.rays = [r_c]
beam.waist.rays = [r_w]
beam.divergence.rays = [r_d]
return nothing
end

splitter_origin = [18.81cm, 23.5cm,0]

asset_dir = @__DIR__
Expand Down Expand Up @@ -148,8 +134,6 @@ set_view(ax, mirror_camera_view)
save("mi_corner_mirror.png", fig; px_per_unit=8, update = false)

## splitter fig
reset_beamlet!(beam)

system = System([laser_assembly, mirror_assembly, splitter_assembly])
solve_system!(system, beam)

Expand All @@ -172,8 +156,6 @@ set_view(ax, splitter_view)
save("mi_beamsplitter.png", splitter_fig; px_per_unit=8, update = false)

## arms fig
reset_beamlet!(beam)

system = System([
laser_assembly,
mirror_assembly,
Expand Down Expand Up @@ -202,8 +184,6 @@ set_view(ax, mi_arms_view)
save("mi_arms.png", fig; px_per_unit=8, update = false)

## system figure
reset_beamlet!(beam)

system = System([
laser_assembly,
mirror_assembly,
Expand Down
2 changes: 1 addition & 1 deletion docs/src/basics/components/beamsplitters.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Main.DocUtils.conditional_include(joinpath(bs_showcase_dir, "cbs_showcase.jl"))

# Beamsplitters

Beamsplitters are used to split beams of light, enabling the separation of an incoming beam into reflected and transmitted parts. In this package, beamsplitters are implemented via the [`BeamletOptics.AbstractBeamsplitter`](@ref) interface. This type loosely defines the interaction logic used for the tracing and retracing of optical systems that incorporate these devices. When a beam is
Beamsplitters are used to split beams of light, enabling the separation of an incoming beam into reflected and transmitted parts. In this package, beamsplitters are implemented via the [`BeamletOptics.AbstractBeamsplitter`](@ref) interface. This type loosely defines the interaction logic used for the tracing of optical systems that incorporate these devices. When a beam is
split the new beams are referred to as the `children` of the `parent` beam. This package uses the `AbstractTrees.jl` interface in order to represent the splitting of [`BeamletOptics.AbstractBeam`](@ref)s.
For concrete implementations of splitters, skip to the [Rectangular plate beamsplitter](@ref) or [Cube beamsplitter](@ref) section.

Expand Down
2 changes: 1 addition & 1 deletion docs/src/basics/kinematics/sources.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[`Ray`](@ref)s, [`Beam`](@ref)s, beamlets like the [`GaussianBeamlet`](@ref) and groups of beams can be repositioned with the commands listed in the [Kinematics](@ref) section.

!!! warning "Every move resets the source"
A traced beam/beamlet whose start is moved would otherwise keep stale rays and child beams from the old, now geometrically wrong, light path. To avoid this, **every** call to one of the translation, rotation or reset commands first resets the moved beam (or, for a group, every one of its beams) to its untraced start state: all rays beyond the start ray (of every component beam of a beamlet) are dropped, and any child beams (e.g. created by a beamsplitter interaction) are removed. Call [`solve_system!`](@ref) again after moving a source to retrace it. [`set_pivot3d!`](@ref) is the exception, see below.
A traced beam/beamlet whose start is moved would otherwise keep stale rays and child beams from the old, now geometrically wrong, light path. To avoid this, **every** call to one of the translation, rotation or reset commands first resets the moved beam (or, for a group, every one of its beams) to its untraced start state: all rays beyond the start ray (of every component beam of a beamlet) are dropped, and any child beams (e.g. created by a beamsplitter interaction) are removed. Call [`solve_system!`](@ref) again after moving a source to trace it again. [`set_pivot3d!`](@ref) is the exception, see below.

```julia
using BeamletOptics
Expand Down
2 changes: 1 addition & 1 deletion docs/src/basics/systems.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Base.delete!(::System, ::BeamletOptics.AbstractObject)

## Solving systems

In order to solve optical systems, this package uses a hybrid sequential and non-sequential mode. Which mode is being used is determined automatically by the [`solve_system!`](@ref) function. This is explained in more detail in the section: [Tracing logic](@ref).
Optical systems are solved non-sequentially by the [`solve_system!`](@ref) function: for every ray the solver searches the system for the object that is hit next. A component can shortcut this search for the following ray by returning a [`BeamletOptics.Hint`](@ref). This is explained in more detail in the section: [Tracing logic](@ref).

```@docs; canonical=false
solve_system!
Expand Down
2 changes: 1 addition & 1 deletion docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ hero:
features:
- icon: "🔦"
title: 3D ray tracing
details: Hybrid sequential and non-sequential ray tracing without paraxial approximation.
details: Non-sequential ray tracing with sequential shortcuts through `Hint`s, without paraxial approximation.
link: /basics/beams/overview#Beam-catalog
linkText: Learn more
- icon: "🌊"
Expand Down
9 changes: 2 additions & 7 deletions docs/src/tutorials/michelson.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,10 @@ The `flen` keyword sets the plotted length of "infinite" beams to 10 cm. Using t

### Specific constants and functions

For the context of this tutorial, the following constants and helper functions have been defined in the `michelson_showcase.jl` file
For the context of this tutorial, the following constants have been defined in the `michelson_showcase.jl` file

- `cm`: corresponds to 1e-2 m
- `mm`: corresponds to 1e-3 m
- `reset_beamlet`: a helper function that resets an already solved `GaussianBeamlet` beam path to its initial position and direction

## Building the optical system

Expand Down Expand Up @@ -144,11 +143,9 @@ Once the mirror is in place, we can run the simulation to see how the beam inter

## Placing the beamsplitter

The beamsplitter splits the beam into a reference and probe beam path. In this step, a 1" [`CubeBeamsplitter`](@ref) with a splitting ratio of 50% will be placed within the system, along with the previously defined mirror. Refer to the [Cube beamsplitter](@ref) section for more information. We also reset the beam to ensure we start from its original parameters before propagation through the new configuration:
The beamsplitter splits the beam into a reference and probe beam path. In this step, a 1" [`CubeBeamsplitter`](@ref) with a splitting ratio of 50% will be placed within the system, along with the previously defined mirror. Refer to the [Cube beamsplitter](@ref) section for more information. Since `solve_system!` solves the beam again from its start, the beam does not need to be reset after the system changed:

```julia
reset_beamlet!(beam)

# define NBK7 ref. index for λ
NBK7 = DiscreteRefractiveIndex([632.8e-9], [1.51509])

Expand Down Expand Up @@ -193,8 +190,6 @@ The following figure shows two 1" mirrors mounted in [KC1L/M](https://www.thorla
The final step in creating a functional interferometer simulation is to add a [`Detector`](@ref) that captures the field data of the incoming beamlets.

```julia
reset_beamlet!(beam)

# define detector with 5x5 mm active area
pd = Detector(5e-3)

Expand Down
2 changes: 1 addition & 1 deletion ext/BeamletOpticsOpticsBaseExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ 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 or
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).

# Arguments
Expand Down
7 changes: 3 additions & 4 deletions skills/beamletoptics/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,15 @@ BeamletOptics.properties(x::MyFilter) =
## Solving

```julia
solve_system!(system, beam; r_max = 100, retrace = true, depth_max = 100,
solve_system!(system, beam; r_max = 100, depth_max = 100,
check_invariant = true, threshold = get_invariant_threshold())
solve_system!(system, beam_group; progress = true, kwargs...) # multithreaded over member beams
```

- `r_max`: max. rays per beam leaf (raise it for resonators, e.g. facing mirrors)
- `depth_max`: max. splitting depth of the beam tree
- `retrace`: after the first solve, try to retrace the previous path sequentially and fall back to a
full non-sequential solve where it breaks. Pass `retrace = false` if an element was moved *into*
the existing beam path (e.g. a chopper), which otherwise fails silently.
- Every call solves the beam again from its start; `empty!` detectors first. Child beams are new
objects after every solve: read `beam.children` again after each solve instead of keeping a child.
- Julia threads (`julia -t auto`) speed up solving of sources with many beams.
- `progress`: beam groups and detector readout show a progress bar once they have run for
`get_progress_threshold()` s (default 5 s, `set_progress_threshold!(Inf)` disables it). It is
Expand Down
3 changes: 1 addition & 2 deletions skills/beamletoptics/CHECKLIST.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,5 @@
use `set_pivot3d!`.
12. **`reset_rotation3d!` throws for rays/beams**; use `align3d!` there.
13. **Moving a child beam is not allowed**; move the source (this also resets its trace).
14. **`retrace = false`** is needed when a moved element newly blocks an old path segment.
15. **Rendering**: `flen` defaults to 1 m for rays; camera helpers need an `LScene`; GLMakie needs a
14. **Rendering**: `flen` defaults to 1 m for rays; camera helpers need an `LScene`; GLMakie needs a
display (`xvfb-run -a` on headless Linux). Without OpenGL, fall back to CairoMakie (`Axis3` only).
7 changes: 4 additions & 3 deletions skills/beamletoptics/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ name: beamletoptics
description: Build, modify, and debug optical simulations with BeamletOptics.jl (Julia, "BMO"). Use when writing Julia code that uses BeamletOptics, or when the user asks to simulate or ray-trace lenses, mirrors, beamsplitters, polarizers, detectors, Gaussian beams/beamlets, interferometers, PSFs, spot diagrams, or to render an optical setup with Makie.
allowed-tools: Read, Write, Edit, Grep, Glob, Bash
metadata:
beamletoptics-version: "0.13"
beamletoptics-version: "0.14"
---

You are helping the user build optical simulations with **BeamletOptics.jl**: a Julia package for
non-sequential 3D ray tracing and Gaussian beamlet propagation, with a Makie extension for rendering.

When this Skill is active:

- This skill describes BeamletOptics **0.13** (`beamletoptics-version` above). Check the installed version
- This skill describes BeamletOptics **0.14** (`beamletoptics-version` above). Check the installed version
with `julia --project=<env> -e 'using BeamletOptics; println(pkgversion(BeamletOptics))'`. If its
major or minor version differs, treat signatures in these files as possibly outdated and confirm every
constructor, keyword and function with `scripts/api_lookup.jl` before using it, and tell the user
Expand Down Expand Up @@ -56,7 +56,8 @@ When this Skill is active:
- `system = System([obj1, obj2, ...])` (order does not matter, tracing is non-sequential);
`StaticSystem` for small fixed systems that are solved many times.
- `solve_system!(system, beam_or_source)`.
- For parameter scans, mutate the scene in a loop, `empty!` detectors, and re-solve (retracing is automatic).
- For parameter scans, mutate the scene in a loop, `empty!` detectors, and call `solve_system!` again (it solves from the start).
Mesh scenes solved thousands of times can use `StaticSystem`.

5) Evaluate
- Rays: `rays(beam)`, `position`, `direction`, `length(beam)`, `beam.children` (splitters: transmitted first).
Expand Down
6 changes: 3 additions & 3 deletions skills/beamletoptics/WORKFLOW.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ For long stacks keep a running `y` and `translate3d!(Li, [0, y, 0]); y += thickn

## Parameter scans (focus search, alignment, fringes)

Mutate the scene inside a loop and re-solve. Retracing is automatic and fast.
Mutate the scene inside a loop and call `solve_system!` again; it solves from the start each time.
Mesh scenes solved thousands of times can use `StaticSystem`.

```julia
for y in ys
Expand All @@ -43,8 +44,7 @@ for y in ys
end
```

- Read everything you need from a detector **before** moving it again (hits reference beam state).
- If a moved element blocks a path segment that was previously free, pass `retrace = false`.
- Read everything you need from a detector **before** moving anything or solving again (hits reference beam state).
- See `templates/01_singlet_spot_diagram.jl` (focus scan) and `templates/04_michelson_scan.jl` (fringes).

## Interferometers and coherent sums
Expand Down
6 changes: 3 additions & 3 deletions skills/beamletoptics/components/systems-and-groups.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ pop!(system) # removes and returns the last top-level objec
popat!(system, i) # removes and returns the i-th top-level object (a group is one)
```

Beams and sources solved before a change keep their old path: `empty!(beam)`, then
`solve_system!(system, beam)`. `System(v)` copies the vector `v`.
Solve beams and sources again after a change: `solve_system!(system, beam)`.
`System(v)` copies the vector `v`.

```julia
solve_system!(system, beam_or_source; r_max = 100, retrace = true, depth_max = 100,
solve_system!(system, beam_or_source; r_max = 100, depth_max = 100,
check_invariant = true, threshold = get_invariant_threshold())
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ beam = Beam([0, -50mm, 0], [0, 1, 0], 633e-9)
solve_system!(system, beam)
println("path 1: ", length(rays(beam)), " ray segments, end direction ", direction(last(rays(beam))))

# Nudge the whole assembly and resolve (retrace is automatic; reset the detector first)
# Nudge the whole assembly and resolve (reset the detector first)
translate3d!(periscope, [0, 5mm, 0])
empty!(det)
solve_system!(system, beam)
Expand Down
Loading
Loading