Skip to content

ForwardDiff gives wrong derivatives for rays on the optical axis #147

Closed

Description

@TacHawkes

Derivatives through solve_system! with ForwardDiff agree with finite differences for rays off the axis, but are wrong for rays exactly on the axis, without a warning. Example: the derivative of the spot position behind a component w.r.t. a lateral shift of the component, ray on the axis:

component ForwardDiff finite difference
bi-convex SphericalLens 0.0 1.90
bi-concave SphericalLens −0.97 −1.98
meniscus SphericalLens −0.96 0.73
ParabolicMirror, ConicMirror NaN finite

Rays on the axis, e.g. the chief ray of a rotationally symmetric system, are the ones an optimizer typically differentiates.

Causes (all where the derivative of a normal is taken, i.e. in nested AD)

  1. The radius of revolution norm(Point2(x, z)) evaluates to NaN on the axis in nested AD. This flips comparisons between SDF branches, and the derivative of the radius is not defined there.
  2. The vertex of a concave surface touches the face of the lens body. Both are equally close, and the union took the flat face, which has no curvature.
  3. A hit of _raymarch_outside may end slightly inside the shape, where the gradient of a composite SDF belongs to another part.
  4. refractive_index(optic, λ) returned Float64, which drops derivatives w.r.t. a refractive index or the wavelength.

Activity

  1. added this to the v0.14.0 milestone on Oct 11, 2026
  2. added
    bug
    on Oct 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions