Skip to content

Feature: detector field of astigmatic beamlets as a 3D vector, Poynthing flux definition - #161

Merged
StackEnjoyer merged 8 commits into
masterfrom
fix/astigmatic-vector-field
Oct 21, 2026
Merged

StackEnjoyer merged 8 commits into
masterfrom
fix/astigmatic-vector-field

Conversation

@TacHawkes

@TacHawkes TacHawkes commented Oct 15, 2026 •

Copy link
Copy Markdown
Collaborator

Features

  • electric_field(pd, xs, zs), intensity(pd, xs, zs): field and intensity on a grid of one's own

Bugfixes

  • Breaking: electric_field(pd) returns complex 3-vectors for astigmatic beamlet hits, as for polarized rays
  • Breaking: intensity(pd) has no Z argument; it is the Poynting flux through the detector
  • Changes results: astigmatic beamlets: orthogonal polarizations interfered, circular ones got 90° phase errors
  • Changes results: tilted detectors: optical_power is the beam power for every beam type, field unprojected
  • Changes results: GaussianBeamletHit field was √n too large inside a medium
  • Changes results: ray hits: phase behind the hit point lacked the factor n in a medium
  • Automatic grid window cut off beamlets on tilted detectors, footprint now stretched by 1/cos θ

Doc changes

  • electric_field, intensity, optical_power: formulas per hit type, Poynting flux, power integral
  • Detectors page: vector field of astigmatic beamlets, readout definitions embedded
  • intensity(E) of a field value documented as |E|²/(2Z), not the detector irradiance
  • Agent skill: API.md, components/detectors.md updated

Internal changes

  • Detector.jl split into abstract type, hits, detector and hit buffers

Closes #159, closes #160

@TacHawkes TacHawkes added this to the v0.14.0 milestone Oct 15, 2026
@TacHawkes TacHawkes added the bug Something isn't working label Oct 15, 2026
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ⚠️ Failed 2026-10-15T18:09:54.463368Z 72b2276 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@codecov

codecov Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.68786% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.25%. Comparing base (d27a435) to head (501b15c).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
...rc/OpticalComponents/Detectors/AbstractDetector.jl 70.00% 3 Missing ⚠️
src/OpticalComponents/Detectors/SpotDiagram.jl 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #161      +/-   ##
==========================================
- Coverage   90.26%   90.25%   -0.01%     
==========================================
  Files          82       85       +3     
  Lines        4632     4587      -45     
==========================================
- Hits         4181     4140      -41     
+ Misses        451      447       -4     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@StackEnjoyer

StackEnjoyer commented Oct 19, 2026 •

Copy link
Copy Markdown
Owner

Ich hab eine generelle Anmerkung zu den verschiedenen Ansätzen zu den Projektionsfaktoren. Ich würde gerne eine einheitliche Lösung haben, wäre dann eine Frage welche Definition am sinnvollsten ist. Soll ich ein seperate issue anlegen?

Am liebsten wäre mir: electric_field liefert vektor-matrix-feld der nicht projizierten werte für alle PolarizedRay-Typen, d.h. polarisierte PSF und AGB - und projizierte skalare Felder für alle Ray-Typen. Was denkst du @TacHawkes?

Projection factors

Side note, not blocking for this PR: with this change the astigmatic beamlets use the same vector sum as PolarizedRays, but the detector sums still use three different projection factors:

Hit type Factor on the amplitude Effect on |E|²
GaussianBeamletHit, AstigmaticGaussianBeamletHit sqrt_proj = √cos θ cos θ, the flux per detector area
RayHit proj = cos θ cos² θ
PolarizedRayHit none 1

The electric_field docstring justifies the missing factor for PolarizedRayHits with "the relative projection between rays is already encoded in their vector directions". I don't think that holds. The direction of E0 decides how the field splits into components, but |E0|² does not depend on the angle of incidence on the detector, so the cos θ of the flux is not in the vectors.

The ray fields are raw/unscaled, so this affects no absolute power. At high NA, though, the scalar and the polarized PSF of the same system weight the marginal rays differently (cos² θ vs. 1). Part of the difference between the two PSFs is then this weighting, not the vector effect.

@TacHawkes

Copy link
Copy Markdown
Collaborator Author

Yes, a separate issue makes sense. I'd split your proposal into two independent questions, though:

1. Return type. Agreed: vector field for polarized hit types (PolarizedRayHit, AstigmaticGaussianBeamletHit), scalar field for the others. This PR already does that for the AGB.

2. Projection. I wouldn't tie it to polarization. The projection describes flux through a tilted surface and has nothing to do with polarization. With your proposal, an AGB on a detector tilted by 45° would give optical_power ≈ 1.41 P (unprojected), while the same beam as a stigmatic GaussianBeamlet gives P (projected). optical_power must not depend on the beam model.

My proposal for one definition:

  • electric_field returns the unprojected field at the detector points for all hit types: the field that is actually there.
  • intensity / optical_power return what a detector measures: the normal component of the Poynting vector, ½ Re(E × H*) · n, with H_j = n/Z · s_j × E_j per hit. This needs no projection factor at all, and it is also correct for the coherent sum of hits at different angles. √cos θ on the amplitude is exact only for a single beam; the cross terms need (cos θ_j + cos θ_k)/2. For scalar hits a second accumulator Σ cos θ_j E_j is enough. It also matches the OpticsBase PlaneField, which stores tangential E and H.

3. Ray hits are a separate problem. For them the "projection factor" stands in for a missing quadrature weight: rays are samples, not fields. For an aplanatic system with uniformly sampled pupil and equal power per ray, Richards–Wolf gives a per-ray amplitude weight ∝ 1/√cos θ. That is neither cos θ (RayHit) nor 1 (PolarizedRayHit). In general the weight needs the ray-tube area, i.e. the ray density in k-space, which a single hit doesn't know. Until that is solved, I'd document ray fields as raw and unweighted rather than apply a factor that suggests accuracy it doesn't have.

@TacHawkes

Copy link
Copy Markdown
Collaborator Author

A follow up my Opus 5.5, how it would tackle 2. and 3.:

Sketches for 2 and 3 below. They are independent; I'd do 2 first, since it fixes the beamlets, and 3 then only changes the per-hit amplitudes.

2: the detector reads the normal Poynting flux

electric_field returns the unprojected field at the detector points for all hit types. intensity / optical_power return what a detector measures, S_n = ½ Re(E × H*) · n̂. Per pixel, accumulate a second field next to E; per hit, precompute the direction s_j (chief ray d0 for beamlets) and the index n_j:

# vector hits (PolarizedRayHit, AstigmaticGaussianBeamletHit)
E += E_j                              # unprojected, returned by electric_field
H += n_j / Z_vacuum * (s_j × E_j)
# readout: S_n = ½ Re((E × conj(H)) ⋅ n̂)

# scalar hits (RayHit, GaussianBeamletHit)
E += E_j
F += n_j / Z_vacuum * (s_j ⋅ n̂) * E_j
# readout: S_n = ½ Re(conj(E) * F)
  • No projection factor at all. A single plane wave gives n |E|² cos θ / (2 Z₀), which includes the refractive index that |E|²/(2Z) with a vacuum Z misses today. For coherent sums of hits at different angles, the cross terms get (cos θ_j + cos θ_k)/2 automatically; √cos θ on the amplitude is exact only for a single beam.
  • Only the tangential parts of E and H enter, which is what PlaneField stores, so the detector and the OpticsBase extension can share the accumulation.
  • API:
    • electric_field drops sqrt_proj, which changes beamlet results on tilted detectors.
    • intensity returns the irradiance on the detector in W/m²; the Z argument goes away.
    • optical_power is exact for any tilt and any beam model.
    • In the hits, sqrt_proj is replaced by s_j.
  • Cost: one extra accumulator per pixel.
  • Tests: GB, AGB and polarized rays on a detector tilted by 0…60° give optical_power ≈ P; two beamlets crossing at ±θ give a fringe-averaged flux equal to the sum of their fluxes.

3: ray hits as quadrature points of the Debye integral

In the focal region the field is the Debye–Wolf integral over the directions of the converging wave, E(p) = −(ik/2π) ∫ A(s) exp(ik s·p) dΩ with n|A|²/(2Z₀) = J(s) (W/sr). A ray carrying power P_j within the solid angle ΔΩ_j is one quadrature point:

a_j = (k / 2π) · √(2 Z₀ P_j ΔΩ_j / n_j) · exp(i k₀ opl_j),     k = n_j k₀

For vector hits, a_j multiplies the unit polarization. For an aplanatic lens with uniformly sampled pupil, ΔΩ_j ∝ 1/cos θ_j, so a_j ∝ 1/√cos θ_j: the Richards–Wolf apodization. Together with 2, ray PSFs come out in absolute W/m², and the Strehl ratio can be read directly.

What a hit needs in addition:

  • P_j: the source weight of the ray (Ring sampling of sources: every ray represents the same area #153) times the transmission (|E0|² for PolarizedRay).
  • ΔΩ_j: the solid angle the ray represents at the detector. Either:
    • (a) from the neighbours in the source lattice (rings × azimuth): the hit stores its sample index, ΔΩ_j = |Δs_u × Δs_v| with central differences, one-sided at edges and at vignetted neighbours. No extra tracing.
    • (b) from differential ray tubes: two neighbour rays per ray, like the waist and divergence rays of a GaussianBeamlet. General, but 3× the tracing, so opt-in.

Validity, for the docstring: focal region only (Fresnel number ≫ 1); it fails where the mapping from pupil to direction folds (ΔΩ_j → 0).

Checked numerically (standalone script; ideal aplanatic rays on a polar pupil lattice, ΔΩ_j from the lattice neighbours, variant (a)):

Ray sum Reference
NA 0.05, peak irradiance 7.8532e6 W/m² Airy P π NA²/λ²: 7.8540e6 W/m²
NA 0.05, first zero 0.6100 λ/NA 0.6098 λ/NA
NA 0.05, encircled energy at 1.12 / 12 λ/NA 0.9097 / 0.9915 0.9099 / 0.9916
NA 0.9, peak irradiance 2.57673e9 W/m² scalar Richards–Wolf: 2.57688e9 W/m²
NA 0.9, encircled energy at 0.5 / 8 λ/NA 0.83326 / 0.98681 0.83329 / 0.98681
  • Deviations are ≤ 1e-4 over the whole profile.
  • ΔΩ_j cos θ_j f² / A_cell = 1 to within 0.7 %; the largest deviation is at the edge ring.
  • With today's weights, renormalized to the same total power, the NA 0.9 peak comes out at 0.69 (cos θ) and 0.82 (1) of the reference.
  • Not covered: the vector variant, and rays traced through a real lens by BMO.

@StackEnjoyer

Copy link
Copy Markdown
Owner

Return types skalar/vektoriell ist keine debatte, und wo man den projektions-faktor anwendet ist auch nur eine frage der API. Aber da die funktion electric_field heißt ist es für mich naheliegend, hier für vektorielle werte noch keine projektion in der detektor-ebene durchzuführen. das kann dann in intensity passieren.

ist im wesentlichen was dein klanker auch sagt. alles was danach kommt schießt über meine eigentlich frage hinaus

TacHawkes and others added 3 commits October 20, 2026 12:43
`electric_field(pd)` added the scalar fields of AstigmaticGaussianBeamlet
hits, `E_ref_amp · ψ`, where the reference amplitude has the phase of the
largest component of the polarization. This summed the polarization out:

- orthogonally polarized beamlets interfered: an x and a z polarized
  beamlet on top of each other gave 4 times the power of one instead
  of 2, wrong for any polarization interferometry
- for a circular polarization, two components are equally large and
  rounding decides the reference: hits that chose differently added
  with a 90° phase error, e.g. a coherent cone source with circular E0
  carried 0.866 of the power with linear E0 (502 hits referred to x,
  498 to z)

The field is now added as 3D vectors, Σ beamlet_hit_field(hit, p) ·
beamlet_hit_polarization(hit), as for PolarizedRay hits and as the
OpticsBase extension already does; the reference phase cancels.
`electric_field(pd)` returns a `Matrix{Point3{Complex}}` for these hits;
`intensity` and `optical_power` are unchanged in use.

Tests: equal, orthogonal and opposite polarizations of two beamlets,
the source with circular vs. linear E0; three tests that read the
scalar field take its dominant component or `sum(abs2, e)`.

Docs: electric_field, detectors page. Agent skill:
`components/detectors.md`, `API.md`.
…tector

`electric_field(pd)` returns the coherent sum of the unprojected fields of the
hits, and `intensity(pd)` the Poynting flux through the detector, ½ Re(E × H*)·n̂
with H = Σ (n/Z₀) s × E (scalar hits: ½ Re(E* F), F = Σ (n/Z₀) cos θ E). A
single hit gives n|E|² cos θ / (2Z₀), so `optical_power` is the beam power for
any angle of incidence, and hits under different angles interfere with the mean
of their cos θ. Before, beamlets carried √cos θ on the amplitude, rays cos θ and
polarized rays nothing.

- `electric_field(hit, p)`: field of one hit, the only per-type method of a hit
- `electric_field(pd, xs, zs)`, `intensity(pd, xs, zs)`: grids of one's own,
  all readouts share one sampling loop
- `intensity(pd)` has no `Z` argument any more
- `GaussianBeamletHit`: field divided by √n, the scalar model is √n too large
  in a medium; beamlet hits drop `sqrt_proj`
- automatic window of beamlet hits grows with 1/cos θ of the footprint

Tests: power on a detector tilted by 0–60° for both beamlet models, the
unprojected field, cos θ irradiance of rays, crossing beams, grid methods.

Docs: docstrings, detectors page, OpticsBase extension. Agent skill: `API.md`,
`components/detectors.md`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@StackEnjoyer
StackEnjoyer force-pushed the fix/astigmatic-vector-field branch from b8f9d63 to 005f578 Compare October 20, 2026 15:52
@StackEnjoyer

StackEnjoyer commented Oct 20, 2026 •

Copy link
Copy Markdown
Owner

Summary of changes in spirit of 1+2:

  • electric_field(pd) now returns the unprojected field for all hit types: a scalar for Ray/GaussianBeamlet, a 3D vector for PolarizedRay/AstigmaticGaussianBeamlet.
  • intensity(pd) is the Poynting flux through the detector, ½ Re(E × H*)·n̂ with H = Σ (n/Z₀) s × E, and ½ Re(E* F) for scalar hits. optical_power is therefore the beam power for any angle of incidence, and cross terms get the mean of their cos θ. The Z argument of intensity is gone.
  • DRY: each hit type only implements electric_field(hit, p), and all readouts share one sampling loop. New: electric_field(pd, xs, zs) and intensity(pd, xs, zs) for grids of one's own, e.g. per cell of an adaptive sampling as in Adaptive discretization algorithm for detector surface #53.
  • Fixed along the way: the GaussianBeamletHit field is divided by √n, since the scalar model is √n too large in a medium. The automatic window for beamlets grows with 1/cos θ; before, 0.27 % of the power was missing at 60°. sqrt_proj is removed from the beamlet hits.
  • Tests: power on a detector tilted by 0–60° equals P0 for GB and AGB, the unprojected field, cos θ for rays, crossing beams, the grid methods. Three expected values in TestPolarizedRays now follow cos θ instead of cos² θ or 1.
  • Point 3 (rays as Debye–Wolf quadrature) is left out on purpose.

Left-to-do:

  • Check API for future-proofness (hopefully we will not have to touch this for a while)
  • Check docs for clear explanation of the electric_field/intensity/etc API

Hugo Uittenbosch and others added 2 commits October 21, 2026 18:39
…t buffers

Pure move, no code changes. Detectors/Detectors.jl holds the includes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@StackEnjoyer StackEnjoyer changed the title Detector field of astigmatic beamlets as a 3D vector Feature: detector field of astigmatic beamlets as a 3D vector, Poynthing flux definition Oct 21, 2026
Also drops the unused argument name of spot_diagram(::Detector, ::Nothing).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@StackEnjoyer

Copy link
Copy Markdown
Owner

Hab die Detectors.jl Datei aufgesplittet, weil die mit 600+ Zeilen nicht mehr übersichtlich war

uitt_hu and others added 2 commits October 21, 2026 22:11
The docstrings state the field of each hit type, the Poynting flux of
the coherent sum through the detector and the integral of the power.
The detectors page embeds them. intensity(E) of a field value is
|E|^2/(2Z), not the irradiance on a detector.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
On a tilted detector in a medium, the phase ramp of ray hits was too
flat by the factor n.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@StackEnjoyer

Copy link
Copy Markdown
Owner

Docs sind geprüft, dabei noch ein kleiner bug für einen theoretischen case behoben (detektor in glas)
Jetzt noch Prüfung der future-proofness der API für v0.14.2

@StackEnjoyer
StackEnjoyer merged commit 59682ca into master Oct 21, 2026
7 checks passed
@StackEnjoyer
StackEnjoyer deleted the fix/astigmatic-vector-field branch October 21, 2026 20:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

2 participants