Skip to content

Keep the sign of the phase of relief rulings, and measure it from the side the light arrives on - #302

Merged
roytsmart merged 2 commits into
mainfrom
fix/rulings-gamma-sign
Dec 2, 2026
Merged

roytsmart merged 2 commits into
mainfrom
fix/rulings-gamma-sign

Conversation

@roytsmart

@roytsmart roytsmart commented Dec 1, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes two bugs in AbstractRulings._gamma, the phase amplitude γ = π h₁ (n₁ cos α − n₂ cos β) / 2λ that every relief profile (sinusoidal, square, sawtooth, triangular, blazed, rectangular) is built on. Both were found in the review of #301.

Bugs

  1. Transmission blazed the wrong orders. _gamma took np.abs(n1 cos α − n2 cos β). For a symmetric profile the sign doesn't matter, but for an asymmetric one it decides which orders are blazed: a facet that reflects light one way refracts light entering a denser medium the other way. SawtoothRulings and BlazedRulings blazed the same orders in both cases. For example, at 3°, 20 µm, 500 nm, normal incidence into n = 1.5, the peak was at order −1 when the facet refracts the light into +1.
  2. Light from the back of the surface went entirely into order 0. cos α = −(a · n) is negative when the ray travels along the normal, but cos β was always a positive square root. For a mirror, n₁(cos α + cos β) then cancels to about 0, so every relief profile returned η₀ = 1 and nothing in any other order. incident_effective already counts the orders from the side the light arrives on, using sign(a · n), but the efficiency didn't.

Fix

  • cos α = |a · n|: both angles are measured from the normal on the side the light arrives on, which is the side incident_effective counts the orders from.
  • Keep the sign of n₁ cos α − n₂ cos β. It is positive for a mirror and negative for transmission into a medium where n₂ cos β > n₁ cos α.
  • TriangularRulings takes |γ|, because its closed form assumes γ ≥ 0. The other symmetric profiles are even in γ.
  • Front-side mirrors, the usual case, are unchanged.
  • The notes of AbstractRulings.efficiency, _gamma, SawtoothRulings and BlazedRulings now give γ with its sign, and say which orders each profile blazes in reflection and in transmission.

Tests

  • test_efficiency_facet: for a sawtooth and for a 3°/12° BlazedRulings, for mirror and transmission, from the front and from the back, at 0° and 10° incidence, the blazed order is the one closest to where the blaze facet reflects or refracts the light (geometric optics), with efficiency > 0.8 (sawtooth) or > 0.5 (blazed).
    • main fails 12 of these 16 cases; only the front-side mirror cases pass.
  • test_efficiency_back: for every relief profile, mirror and transmission, light from the back has the efficiency of light from the front, reflected through the surface, in the opposite order of the profile turned upside down.
    • main fails all 12 cases.
  • The rulings list from test_efficiency_reciprocal, now with BlazedRulings, moved to a module variable, _rulings_relief, so the new test can share it.

All 255 rulings tests and all 151 surface tests pass locally; test_to_dxf only passes serially, the known xdist race. Black and ruff are clean.

Rebased on #301

Rebased onto main after #301 (BlazedRulings) merged: its notes give γ with its sign, it is in _rulings_relief, and it is in the facet and back-side tests. The test helpers both PRs defined are no longer duplicated.

🤖 Generated with Claude Code

https://claude.ai/code/session_01StcJvUf6xFg2ese9XDDhaB

@codecov

codecov Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (873ee18) to head (274528f).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main      #302   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          142       142           
  Lines        14007     14059   +52     
=========================================
+ Hits         14007     14059   +52     
Flag Coverage Δ
unittests 100.00% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ 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.

…side the light arrives on

`AbstractRulings._gamma` took the magnitude of `n1 cos(alpha) - n2 cos(beta)`,
and measured `alpha` from the normal whichever side the light arrived on.

- The magnitude threw away which way an asymmetric profile blazes. A facet
  that reflects light one way refracts the light entering a denser medium
  the other way, but `SawtoothRulings` and `BlazedRulings` blazed the same
  orders for both.
- Light arriving along the normal, from the back of the surface, made
  `cos(alpha)` negative while `cos(beta)` stayed positive, so a mirror's
  phase cancelled and every relief profile sent all of the light into
  order 0.

Now `_gamma` measures both angles from the side the light arrives on,
which is the side `incident_effective` counts the orders from, and keeps
the sign. The symmetric profiles do not depend on it, and
`TriangularRulings`, whose form assumed it was not negative, takes its
magnitude. The notes of `SawtoothRulings` and `BlazedRulings` say which
orders they blaze in reflection and in transmission.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01StcJvUf6xFg2ese9XDDhaB
@roytsmart
roytsmart force-pushed the fix/rulings-gamma-sign branch from 3b553c1 to 4b0afb1 Compare December 1, 2026 22:41
- The notes of the symmetric profiles, and a comment in
  `BlazedRulings.efficiency`, still wrote gamma with an absolute value.
  They now write it with its sign, and say that a symmetric profile depends
  only on its magnitude.
- Gamma has the sign of the depth for a mirror, not always positive, and
  the opposite sign for transmission only where n2 cos(beta) > n1 cos(alpha),
  which steep orders entering a denser medium do not satisfy.
- Annotate the new tests with `-> None`, and build their orders with
  `na.arange`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01StcJvUf6xFg2ese9XDDhaB
@roytsmart
roytsmart merged commit 7e70955 into main Dec 2, 2026
12 checks passed
@roytsmart
roytsmart deleted the fix/rulings-gamma-sign branch December 2, 2026 13:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant