Field notes

Note 002 Numerics

Tracing a ray through the Morse lens

A ray tracer is a small experiment in numerical geometry: four equations, a step size, and a convergence check. The hardest part is deciding what the picture means.

Zheng Xiangsheng 24 Oct 2026 Revised 10 Nov 2026 11 min read Working note
Three panels of ray traces through Luneburg, Morse and Eaton refractive-index profiles, clipped at the lens rim.
Fig. 1 — Parallel rays through three profiles: (A) Luneburg, $B=\tfrac12$; (B) intermediate Morse, $B=\tfrac34$; (C) Eaton, $B=1$. Paths are clipped at the lens rim. The caustic moves inward as $B$ increases, and at $B=1$ the rays turn around.

A ray tracer is easy to write and hard to trust. The equation is short, the integrator is standard, and the picture is convincing. The work is in the checks: does the step size matter, does the ray invariant hold, and is the feature you are looking at a property of the optics rather than of the discretisation?

The equation

Fermat's principle gives the ray equation

\[ \frac{d}{ds}\left(n\frac{d\mathbf{r}}{ds}\right)=\nabla n, \]

with $s$ the Euclidean arc length. Writing $\mathbf{u}=d\mathbf{r}/ds$, this is

\[ \frac{d\mathbf{u}}{ds}=\frac{\nabla n-(\nabla n\cdot\mathbf{u})\,\mathbf{u}}{n}. \]

The second form is convenient because it keeps $\lvert\mathbf{u}\rvert=1$ by construction. It is also the form that survives the index step at the lens rim: $n$ is continuous there for the profiles considered here, even though $\nabla n$ is not.1

For the Luneburg profile, $n^{2}=2-r^{2}$ and $\nabla n=-\mathbf{r}/n$. The equation becomes

\[ \frac{d\mathbf{u}}{ds}=\frac{-\mathbf{r}+(\mathbf{r}\cdot\mathbf{u})\,\mathbf{u}}{n^{2}}, \]

which is smooth inside the lens and straight outside. The Morse family replaces $n^{2}$ by

\[ n^{2}(r)=2r^{-2+1/B}-r^{-2+2/B}. \]

For $B>\tfrac12$ the index diverges at the origin, so the integration must stay away from $r=0$; parallel rays with nonzero impact parameter do, but the step size still has to resolve the turning region.

The integrator

I use classical RK4 in Cartesian coordinates. Cartesian avoids the $1/r$ terms and the coordinate singularity of polar coordinates; the price is that the lens rim is a circle rather than a coordinate line, which a small step handles.

import math

def grad_n(x, y, B):
    r = math.hypot(x, y)
    if r >= 1.0:
        return 1.0, 0.0, 0.0
    p, q = -2.0 + 1.0 / B, 1.0 / B
    n = math.sqrt(2.0 * r**p - r**(p + q))
    dn = (p * r**(p - 1) - 0.5 * (p + q) * r**(p + q - 1)) / n
    return n, dn * x / r, dn * y / r

def rhs(x, y, vx, vy, B):
    n, gx, gy = grad_n(x, y, B)
    dot = gx * vx + gy * vy
    return vx, vy, (gx - dot * vx) / n, (gy - dot * vy) / n

def trace(b, B, h=0.0025, x0=-1.3):
    x, y, vx, vy = x0, b, 1.0, 0.0
    pts = [(x, y)]
    for _ in range(8000):
        k1 = rhs(x, y, vx, vy, B)
        k2 = rhs(x + h*k1[0]/2, y + h*k1[1]/2, vx + h*k1[2]/2, vy + h*k1[3]/2, B)
        k3 = rhs(x + h*k2[0]/2, y + h*k2[1]/2, vx + h*k2[2]/2, vy + h*k2[3]/2, B)
        k4 = rhs(x + h*k3[0], y + h*k3[1], vx + h*k3[2], vy + h*k3[3], B)
        x  += h*(k1[0] + 2*k2[0] + 2*k3[0] + k4[0]) / 6
        y  += h*(k1[1] + 2*k2[1] + 2*k3[1] + k4[1]) / 6
        vx += h*(k1[2] + 2*k2[2] + 2*k3[2] + k4[2]) / 6
        vy += h*(k1[3] + 2*k2[3] + 2*k3[3] + k4[3]) / 6
        pts.append((x, y))
        if x > 1.45 or math.hypot(x, y) > 2.0:
            break
    return pts

The stop condition is part of the model. For the figures on this page I stop at the first rim crossing, so the panels show the paths inside the lens; for other questions the interesting part may be the exit field.

Does the step size matter?

Take a bundle of parallel rays with impact parameters $b=\pm0.1$ to $\pm0.8$ in steps of $0.1$, find the first axis crossing of each ray, and look at the mean and the RMS spread. The table compares four step sizes. The reference is not an analytic formula but the finest integration; the point is to see what changes.

TABLE I. Focus statistics versus RK4 step size (16 rays, $b=\pm0.1$ to $\pm0.8$ in steps of $0.1$; $x_0=-1.3$).
Step size $h$$\bar{x}$, $B=\tfrac12$RMS, $B=\tfrac12$$\bar{x}$, $B=\tfrac34$RMS, $B=\tfrac34$
0.011.000330.001920.243730.17095
0.0051.000390.000350.244060.17125
0.00251.000030.000420.243920.17117
0.001251.000080.000080.243940.17116

Two things are happening. The Luneburg focus converges to $x=1$ and the RMS spread falls with the step; that is the integrator behaving as expected. The Morse $B=\tfrac34$ statistics stop changing at about four decimal places while the RMS spread stays near $0.17$. That residual is not numerical error. It is the aberration of the profile: the bundle does not have a point focus.2

What the three panels show

Panel A is the Luneburg lens. The parallel bundle focuses at the rear rim; the caustic is a point to the accuracy of the table. Panel B is the intermediate Morse profile. The caustic has moved inward and broadened; the rays cross the axis over a range of positions. Panel C is Eaton: the index diverges at the origin, the rays turn before reaching it, and the bundle leaves the lens on the entrance side.

The visual difference between B and C is not a difference in numerical quality. It is the profile changing from a focusing lens into a retroreflector. The same integrator, step, and stop condition produce all three panels.

What I would check next

Three checks would make the tracer more useful. First, a conserved quantity: for a rotationally symmetric index Bouguer's invariant $n\,r\sin\psi$ — the optical angular momentum, with $\psi$ the angle between the ray and the radius — is constant along the path, and its drift is a cheap error monitor. In the Cartesian variables of the code it is $n\,(x\,u_{y}-y\,u_{x})$. Second, an analytic limit: the Luneburg case has a closed-form focus, so it can calibrate the integrator independently of the surface construction. Third, a convergence study in the surface metric, not only in the bulk: the geodesic integration and the GRIN integration should agree at the same rate.

None of these is difficult. They are the difference between a picture and an experiment.

Revision history

References

  1. R. K. Luneburg, Mathematical Theory of Optics. Brown University Press, 1944.
  2. M. Šarbort and T. Tyc, “Spherical media and geodesic lenses in geometrical optics,” J. Opt. 14, 075705 (2012). doi
  3. H. Chen, “The Morse lens,” arXiv:2002.03884 (2020). arXiv
  4. W. H. Press et al., Numerical Recipes, 3rd ed. Cambridge University Press, 2007. (RK4 and step-size control.)
Note 002 · Working note, not a peer-reviewed paper · revised 10 Nov 2026 Corrections: email