diff --git a/docs/src/basics/components/detectors.md b/docs/src/basics/components/detectors.md index e2ba3e2f..561a8b1e 100644 --- a/docs/src/basics/components/detectors.md +++ b/docs/src/basics/components/detectors.md @@ -23,6 +23,13 @@ Detector(::Real, ::Bool) Detector ``` +Whether a detector stops the beams can be switched later, e.g. to let the beams pass a screen that has been placed for a first check of the setup: + +```@docs +BeamletOptics.stop +BeamletOptics.stop! +``` + After solving a system containing a `Detector`, the methods listed below can be used in order to analyze the stored data. If no hits are stored, the readouts return zero or empty results. ## Reading out a detector diff --git a/skills/beamletoptics/components/detectors.md b/skills/beamletoptics/components/detectors.md index c3997af1..ef83477b 100644 --- a/skills/beamletoptics/components/detectors.md +++ b/skills/beamletoptics/components/detectors.md @@ -7,7 +7,7 @@ Detector(edge_length, stop = true) - An infinitely thin square screen at the origin in the x-z plane. Its normal points along −y, so beams travelling along +y hit it. Orient it like any other object, e.g. after `zrotate3d!(det, deg2rad(90))` it catches beams travelling along −x. -- With `stop = false`, rays and beamlets continue unchanged behind the detector, e.g. to sample a beam on an intermediate plane. +- With `stop = false`, rays and beamlets continue unchanged behind the detector, e.g. to sample a beam on an intermediate plane. `BeamletOptics.stop!(d, false)` switches an existing detector from the next solve on (stored hits stay), `BeamletOptics.stop(d)` reads it. A beamlet that the detector clips only partly ends there, as at any object: make the detector larger than the beamlet. - Stores *hits* of exactly **one** type (Ray, PolarizedRay, GaussianBeamlet or AstigmaticGaussianBeamlet). - **Hits are values** of the beam state at the solve. Solving again, or moving a source, mirror or lens, never changes stored hits. diff --git a/src/Exports.jl b/src/Exports.jl index 61b68e1d..4fd80faf 100644 --- a/src/Exports.jl +++ b/src/Exports.jl @@ -85,6 +85,8 @@ public properties, default_properties public relaunch!, GaussianBeamletHit, AstigmaticGaussianBeamletHit # own detector samplings, see AbstractDetectorSampling public AbstractDetectorSampling, sample_points, DetectorSamples +# whether a detector stops the beams, e.g. a switch in a GUI +public stop, stop! # optional part of the shape interface, see AbstractShape public bounding_sphere_of, AbstractBoundingSphere, NoBoundingSphere, SingleBoundingSphere, MultiBoundingSphere # own assembly types of separate parts, see AbstractObjectGroup diff --git a/src/OpticalComponents/Detectors/Detector.jl b/src/OpticalComponents/Detectors/Detector.jl index 431fb5fb..47e47cb6 100644 --- a/src/OpticalComponents/Detectors/Detector.jl +++ b/src/OpticalComponents/Detectors/Detector.jl @@ -87,6 +87,8 @@ Additionally, continued tracing can be configured via the `stop` flag where - `false` indicates continued tracing: rays and beamlets pass the detector unchanged - `true` stops the incoming beams as with any hard target +It can be changed later via [`stop!`](@ref). + !!! warning "Partly clipped beamlets" A beamlet whose rays hit the detector only partly ends there, as at any other object, also with `stop = false`. To sample a beamlet on an intermediate plane, make the detector larger @@ -112,9 +114,19 @@ The number of hits stored in the detector `d`, `0` before the first solve and af """ length(d::Detector) = isnothing(hits(d)) ? 0 : length(hits(d)) -"Allows continued tracing if set to false" +""" + stop(d::Detector) -> Bool + stop!(d::Detector, stop::Bool) + +Returns or sets whether the detector `d` stops the beams that hit it, see [`Detector`](@ref): with +`false`, rays and beamlets pass it. A change applies from the next solve on; the hits stored by +earlier solves are kept. +""" stop(d::Detector) = d.stop +"""Sets whether the detector `d` stops the beams that hit it, see [`stop`](@ref).""" +stop!(d::Detector, new::Bool) = (d.stop = new; nothing) + """ local_axes(pd::Detector) -> (x̂, ẑ) diff --git a/test/Components/TestDetector.jl b/test/Components/TestDetector.jl index c2210bb4..3b8f87de 100644 --- a/test/Components/TestDetector.jl +++ b/test/Components/TestDetector.jl @@ -146,6 +146,31 @@ end @test BMO.polarization(rays(beam)[2]) == BMO.polarization(rays(beam)[1]) end +@testset "The stop of a detector can be switched after it is built" begin + d1 = Detector(20mm) + d2 = Detector(20mm) + translate_to3d!(d1, [0.0, 0.10, 0.0]) + translate_to3d!(d2, [0.0, 0.20, 0.0]) + system = System([d1, d2]) + @test BMO.stop(d1) + beam = Beam(Ray([0.0, 0.0, 0.0], [0.0, 1.0, 0.0])) + solve_system!(system, beam) + @test (length(d1), length(d2)) == (1, 0) + # The beam passes d1 from the next solve on, its earlier hit is kept + @test isnothing(BMO.stop!(d1, false)) + @test !BMO.stop(d1) + @test length(d1) == 1 + beam = Beam(Ray([0.0, 0.0, 0.0], [0.0, 1.0, 0.0])) + solve_system!(system, beam) + @test (length(d1), length(d2)) == (2, 1) + @test length(rays(beam)) == 2 + # And stops there again + BMO.stop!(d1, true) + beam = Beam(Ray([0.0, 0.0, 0.0], [0.0, 1.0, 0.0])) + solve_system!(system, beam) + @test (length(d1), length(d2)) == (3, 1) +end + @testset "Beamlets pass a detector with stop = false" begin # The field on a later detector is the same with and without the passed detector, # for a beamlet along +y and one along +x