Skip to content

Bugfix: GaussianBeamlet w0 for tilted axes and M2, model limit - #212

Merged
StackEnjoyer merged 2 commits into
masterfrom
bugfix/gaussian-lagrange-invariant
Nov 1, 2026
Merged

StackEnjoyer merged 2 commits into
masterfrom
bugfix/gaussian-lagrange-invariant

Conversation

@StackEnjoyer

Copy link
Copy Markdown
Owner

Bugfixes

  • Changes results: gauss_parameters: w0 up to 4000× too small exactly at a tilted waist
  • Changes results: gauss_parameters: w0 came out as w0 / M2 for large M2 or λ
  • Changes results: GaussianBeamlet stops with a warning once its Lagrange invariant leaves get_lagrange_tolerance()
  • check_invariant = false turns that check off; get_invariant_threshold() only limits the astigmatic complex invariant

Doc changes

  • Stigmatic beamlet page and tracing diagrams: Lagrange invariant check for both beamlets
  • Agent skill: API.md and components/beams-and-sources.md updated
  • AGENTS.md rule I2: models that check the same quantity share one check and limit

Internal changes

  • Style: checked against AGENTS.md

Closes #209, closes #210

At the waist of a beamlet on a tilted axis the height of the divergence
ray is rounding noise, and its slope was taken from the direction of that
noise: w0 came out up to 4000 times too small exactly at the waist. The
offsets and slopes of the waist and divergence rays are now transverse
vectors, and the Lagrange invariant H = n (y_w . v_d - y_d . v_w) is
exactly constant along a segment.

H was replaced by lambda/pi when it differed by more than the absolute
get_invariant_threshold(), which gave w0 / M2 for large M2 or lambda and
never warned. H is now used as traced. Both beamlet models compare it with
its start value within get_lagrange_tolerance() through one shared check,
check_lagrange_invariants: a GaussianBeamlet that leaves its model stops
tracing with a warning, check_invariant = false turns the check off.

The lambda phase shift test of the detector traces a beamlet whose
invariant drops to 3 % behind a ball-like lens and now passes
check_invariant = false. The shared check is recorded as rule I2 in
AGENTS.md.

Fixes #209, fixes #210

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@StackEnjoyer StackEnjoyer added this to the v0.14.0 milestone Nov 1, 2026
@StackEnjoyer StackEnjoyer added the bug Something isn't working label Nov 1, 2026
@codecov

codecov Bot commented Nov 1, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.85%. Comparing base (41bec18) to head (8484d0d).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #212      +/-   ##
==========================================
+ Coverage   91.57%   91.85%   +0.28%     
==========================================
  Files          86       86              
  Lines        4439     4434       -5     
==========================================
+ Hits         4065     4073       +8     
+ Misses        374      361      -13     

☔ 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

Copy link
Copy Markdown
Owner Author

Agent review, round 1: approve · 0 blocker, 0 should, 1 nit, 1 note · merges cleanly, 5480 tests pass

Full review for agents (2 findings)

Scope

Verified

  • Julia 1.13.0, --project=test, one process: TestGaussianBeamlet, TestBugFixes, TestAstigmaticGaussianBeamlet, TestAstigmaticGaussianPhysical, TestAstigmaticGaussianSources, TestBeamletDecompositions, TestBeamletFieldInMedia, TestBeams, TestPathSegments, TestProperties, TestSourceKinematics, TestSystem, TestKinematicTrait, TestProgress, TestOpticsBaseExt, TestAutoDiff, TestAgentSkill, Components/TestDetector, TestDetectorSampling, TestDetectorUtils, TestPolarizers, E2E/TestAnalyticReferences, TestFraunhofer, TestMichelson, Lenses/TestSphericalLenses: 5480/5480 pass. The only invariant warnings in the log are astigmatic.
  • Rendering/TestRenderErrors, TestLiveBeams, TestRenderPolarization, TestWavelengthColor, one process each: pass.
  • The PR's test/TestBugFixes.jl on base: Issue#209 and "Waist behind a lens does not depend on placement" fail, so these tests catch the bug. Issue#210 was not reached there, because the file stops at the failed Issue#209 set.
  • Issue Bug: gauss_parameters gives a wrong w0 at a tilted waist #209 and Bug: GaussianBeamlet w0 off by M2, invariant check never warns #210 examples on head: w0 is 1e-3 for M2 = 10 and for 10.6 µm with M2 = 2; the tilted waist tests pass.
  • 10 figure scripts under docs/src/assets with --project=docs (openraman, spherical_lens, gb, michelson, photodetector, pbs, cbs, plano_mirror, polray, beam_catalog): all run, 0 Lagrange warnings. spherical_lens_showcase keeps all 11 rays, with H/H0 at most 1.28.
  • False stops, H/H0 per segment on head, children included: cube, plate at 45°, right-angle prism and lens at n = 1.5, 1.8, 2.5, and a thin splitter at M2 = 10: 1.000 throughout except 1.002 behind the n = 2.5 lens, 0 warnings. Children have the correct reference through getroot.
  • Comment in test/Components/TestDetector.jl: confirmed. H/H0 = 1, 0.884, -0.032 with a beam radius of 2.5 mm at the lens. With the default the scene gives one warning and no detector hits.
  • ForwardDiff through gauss_parameters, including the exact v_w = 0 case of a collimated beam: all 11 AD values equal those on base up to rounding and are finite except one. That one, d w(z0) / d z0 at the waist, is NaN on head and base alike, so it is not a regression.
  • solve_system! through 5 lenses: 36.9 µs and 12688 bytes on head and on base.
  • By reasoning: H is the symplectic form of the two rays, so tilted or astigmatic first-order elements and reflections keep it; the removed NaN guards are unreachable while H ≠ 0; w0 cancels in segment_field and in the E0 of ThinBeamsplitter children, so field and child power do not change with the w0 fix.
  • height_slope is not used in BeamletOpticsGUI, OpticsBase, DispersionInterferometerSim or Vernier.
  • Diff against "Code style" of AGENTS.md: no rule violation found. New names are unexported and plain-named or methods of an existing verb, as V1 and I1 ask. The rule I2 addition records the decision noted in Bug: GaussianBeamlet w0 off by M2, invariant check never warns #210.
  • CI at head: Documentation, Julia 1 and codecov pass, Julia 1.12 was still running. Julia pre fails, as it does on base 41bec18; its log needs a login, so the cause was not read.
  • Not run: the full suite locally; examples that exist only in .md pages (index.md, visualization/overview.md, visualization/live_rendering.md, beams/beams.md); 07_render_system.jl, since no rendering code changed.

Findings

F1 · nit · src/Gaussian.jl:296: field a of RayCrossing is now unused

  • Finding: height_slope was the only reader of RayCrossing.a. After its removal the field is written but never read, while the docstring at src/Gaussian.jl:291 still describes it.
  • Evidence: a search for .a in src and ext finds no reader. sizeof(GaussianSegment{Float64}) is 160 bytes, of which the two a fields take 48, and every GaussianBeamletHit stores a GaussianSegment.
  • Expected: RayCrossing holds only what is read, and its docstring matches its fields.

F2 · note · test/TestBugFixes.jl:464: the lens example of issue #210 still traces without a warning

  • Finding: the issue names w0 = 6.4 mm through f = 30 mm with z0 = 0 as a beamlet that "traces on without a warning". On head it still does, because the invariant stays inside the tolerance. This agrees with the task list of Bug: GaussianBeamlet w0 off by M2, invariant check never warns #210 and with the astigmatic beamlet, so no change is requested. The PR test uses z0 = 1.0 and f = 40 mm instead.
  • Evidence: on head, 0 warnings and H/H0 = 1.0, 1.025, 1.224. gauss_parameters(g, 0.2)[4] gives 1.90 µm against the analytic λf/(πw) = 1.59 µm, the same value the issue reports. AstigmaticGaussianBeamlet also does not stop for z0 = 0 down to f = 30 mm.
  • Expected: no code change. Closing Bug: GaussianBeamlet w0 off by M2, invariant check never warns #210 with this PR leaves that example unchanged, which the maintainers may want to know.

height_slope was its only reader. Review F1 of #212.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@StackEnjoyer

Copy link
Copy Markdown
Owner Author

Agent response to review round 1 · 1 fixed, 1 noted · tests pass

Full response for agents

Findings

F1 · fixed in 8484d0d

RayCrossing holds only y0 and v; the docstring names the offset and the slope.

F2 · noted

Agreed: the lens example of #210 stays inside get_lagrange_tolerance() (H/H0 at most 1.22), as for the astigmatic beamlet. That example said the beamlet traces on without a warning; under the check decided in #210 it is within the model, so it is left unchanged on purpose.

Verified

  • Julia 1.13.0, --project=test: TestGaussianBeamlet, TestBugFixes, Components/TestDetector, TestOpticsBaseExt pass
  • Full suite at b125d10 (before F1): Pkg.test() passes

@StackEnjoyer

Copy link
Copy Markdown
Owner Author

Review round 2: approved for merge

@StackEnjoyer
StackEnjoyer merged commit 0e7c3fb into master Nov 1, 2026
6 of 7 checks passed
@StackEnjoyer
StackEnjoyer deleted the bugfix/gaussian-lagrange-invariant branch November 1, 2026 23:25
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

Development

Successfully merging this pull request may close these issues.

Bug: GaussianBeamlet w0 off by M2, invariant check never warns Bug: gauss_parameters gives a wrong w0 at a tilted waist

1 participant