Repository navigation
Fix AD on optical axis - #148
Conversation
The distance field of an aspheric or acylindric surface evaluated the asphere at its edge (sag and gradient) on every call, although both are constant for a surface, and summed the even terms with integer powers. The SDFs now store the edge values (`edge`, see `aspheric_edge`) and the sums use Horner's scheme in r². An empty coefficient vector now gives the conic surface instead of an error. One `sdf` call of an asphere drops from ~107 ns to ~29 ns; a beam group of 20000 rays through an aspheric lens solves in 83 ms instead of 163 ms (1 thread). Distances agree with the previous code to ~3e-15 relative, ray intersections are bit-identical. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ForwardDiff through `solve_system!` gave the correct derivative for rays off the axis but 0 or a wrong value for rays on it, without a warning, e.g. for the spot position behind a lens w.r.t. its lateral shift: 0 instead of 1.90 (bi-convex), -0.97 instead of -1.98 (bi-concave), NaN for parabolic and conic mirrors. Rays on the axis, e.g. the chief ray of a rotationally symmetric system, are the ones an optimizer differentiates. The causes, all where the derivative of a normal is taken, i.e. in nested automatic differentiation: - The radius of revolution `norm(Point2(x, z))` is `NaN` on the axis in nested AD, which turned the comparisons of SDF branches, and the derivative of the radius is not defined there. `_radial` is exactly 0 on the axis, and the surfaces that cross the axis are computed from smooth expressions: the 3D distance from the center of a sphere (`CutSphereSDF`, `ConvexSphericalSurfaceSDF`), the sag and slope of aspheres and conics as functions of r². - Where a concave surface touches the face of the lens body at its vertex, both are equally close, and the flat face was taken, which has no curvature. `UnionSDF` now resolves such ties by the operand that is closest beside the point (`_closest_operand`), independent of the order of the operands; within the aspheric SDFs the surface wins ties with the segments that close the perimeter. - A hit of `_raymarch_outside` that ends slightly within the shape has its normal evaluated outside, as the exits of the inside march. - `refractive_index(optic, λ)` returned `Float64`, which dropped the derivative w.r.t. a refractive index or the wavelength; it returns the number type of the optic and the wavelength. New `test/TestAutoDiff.jl`: ForwardDiff against central finite differences for rays on and off the axis through spherical, meniscus, aspheric and cylindrical lenses and spherical, parabolic and conic mirrors, w.r.t. a refractive index and through a GaussianBeamlet (ForwardDiff added to the test dependencies). SDF values, normals and hit lengths agree with the previous code to ~1e-17 m. Built on the aspheric changes of #137.
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #148 +/- ##
==========================================
- Coverage 90.85% 90.52% -0.34%
==========================================
Files 86 86
Lines 4626 4610 -16
==========================================
- Hits 4203 4173 -30
- Misses 423 437 +14 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
A wider check of ForwardDiff against finite differences found further failures, now fixed: - Polarized rays and astigmatic beamlets at normal incidence threw an orthogonality error: `isparallel3d` took the `norm` of a cross product that vanishes but has a derivative, which is `NaN` (also its value) for dual numbers, hence the s-p basis was built from a zero vector. It now compares the squared norm. The Fresnel coefficients of lenses and polarizers are computed from the cosine of the angle of incidence (`_fresnel_coefficients`, `cos_angle3d`), since the derivative of the `acos` is not defined at normal incidence. - `ThinBeamsplitter` (also within `CubeBeamsplitter`) required the reflectance in the number type of the shape; a constructor converts it. - `CircularFlatMesh` built its vertices as `Float64`, which broke round plate beamsplitters, polarization filters and mirrors. `test/TestAutoDiff.jl` now covers 68 cases: radius, thickness, tilt, refractive index, polarized rays and astigmatic beamlets on the axis, thin, plate and cube beamsplitters, total internal reflection, Malus' law, the power on a detector and the power of a Michelson interferometer vs. the position of a mirror. New docs page "Automatic differentiation" (usage, covered components, limitations: discrete events, edges, forward mode only, compile time, `ObjectGroup(objects, T)`), and a section in the agent skill (`WORKFLOW.md`). `ObjectGroup` still defaults to `Float64`; its constructor is changed by #135.
# Conflicts: # src/SDFs/AsphericalLensSDF.jl
|
Agent review, round 1: approve with changes · 0 blocker, 4 should, 1 nit, 2 notes · merges cleanly, 5425 tests pass
Full review for agents (7 findings)Scope
Verified
FindingsF1 · should ·
|
The surface distances computed the sag and the slope via the functions of r for the NaN guard and the branch conditions, then again via the functions of r^2 for the distance. They now evaluate both once, as functions of r^2, with the guard taken from the square root argument. gradient_aspheric_equation delegates to the same slope, so its formula exists once. Review F1 on PR #148. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Review F2 on PR #148. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
For a ray on the axis and a laterally shifted lens or mirror, the derivative of the spot w.r.t. the shift is 1 - A of the paraxial ABCD matrix, built in the test from the parameters. Review F3 on PR #148. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Agent has addressed review comments |
Since #171, optical_power takes the sampling as an argument instead of the keywords n, x_min, ... Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Conflict with #174 in _raymarch_outside: the hit is the secant root of #174, and its normal is evaluated outside as in this branch. _secant_root also returns the SDF at the root and the slope along the ray; the point is moved back until it lies about as far outside as the root lies inside, at least eps/100 (ties of composite SDFs exactly on the surface) and at most eps.
Julia 1.14.0-alpha1 calls issubnormal in normalize, which ForwardDiff 1.4.6 does not define for Dual (JuliaDiff/ForwardDiff.jl#852, fix in #855). The job keeps running and reporting, but no longer blocks merges.
|
Merged master into the branch (010acd3). The conflict with #174 is in |
Conflicts with #148: the Hint glue in Lenses.jl and LinearPolarizer.jl stays removed, the generic methods use the transition. Ported from #148 into the transition code: Fresnel coefficients from the cosine of the angle of incidence (cross_boundary, PolarizationFilter), smooth for AD at normal incidence; the refractive indices of a transition keep the number type of the ray (e.g. ForwardDiff.Dual) instead of Float64.
_radialis exactly 0 on the axis. Surfaces that cross the axis are computed from smooth expressions: the 3D distance to the sphere center (CutSphereSDF,ConvexSphericalSurfaceSDF), and sag and slope of aspheres and conics as functions of r².UnionSDFresolves ties between operands by the operand that is closest beside the point, independent of operand order. Within the aspheric SDFs, the surface wins ties with its closing segments._raymarch_outsidethat end inside have their normal evaluated outside.refractive_index(optic, λ)returns the number type of the optic and the wavelength.Tests: new
test/TestAutoDiff.jlcompares ForwardDiff with central finite differences (42 tests):GaussianBeamletForwardDiff is added to the test dependencies. SDF values, normals and hit lengths agree with master to ~1e-17 m. Merged with #142, the AD tests and the exit-point check pass as well.
Cost: no additional allocations. 40 lenses take ~11 % longer (more SDF evaluations for normals in unions), the aspheric lens slightly less time.
Agent skill: updated skills/beamletoptics/WORKFLOW.md (new section "Derivatives"); no other skill files affected.
Fixes #147.