Skip to content

Automatic differentiation

Implicit pole derivative

For a simple non-Hermitian pole

\[ A(\tilde\omega,\theta)u=0,\qquad v^\dagger A=0, \]

differentiation with respect to parameter \(\theta_j\) gives

\[ \frac{d\tilde\omega}{d\theta_j}= -\frac{v^\dagger(\partial_{\theta_j}A)u} {v^\dagger(\partial_\omega A)u}. \]

No derivative of the arbitrarily normalized eigenvector is required for the pole. The denominator is also the QNM residue normalization; a small magnitude warns of ill-conditioning or a multiple pole.

With \(\tilde\omega=a+ib\), \(b<0\),

\[ Q=-\frac{a}{2b},\qquad dQ=-\frac{da}{2b}+\frac{a}{2b^2}db. \]

Geometry derivatives

Current nanobeam kernels differentiate exact Fourier/Galerkin projections of translated elliptical holes. A Jacobian-vector product differentiates the operator action; a vector-Jacobian contraction computes all geometry coordinates without materializing a dense operator Jacobian. Layer thickness and beam width modify the layered cross-section operator and use separately contracted fixed-pole differences in the legacy implementation.

“Automatic differentiation” here means that the end-to-end objective exposes a programmatic derivative. Individual kernels may use analytic partials, JVPs, VJPs, implicit differentiation, or carefully isolated finite differences. Every approximation must be named in the recipe and gradient audit.

cavitygrad.autodiff.autograd_primitive is the adapter used by Design 23. A value-and-adjoint-gradient callback becomes an opaque primitive with a registered reverse-mode VJP, so ordinary autograd.grad composition works without replaying or differentiating through nonlinear pole iterations. The forward value/VJP pair is cached for a differentiated input to avoid paying for the eigensolve twice during backpropagation.

Observable derivatives

Mode volume and guided coupling depend on both pole and eigenvectors. Their total derivatives require the linearized QNM state or an adjoint solve plus explicit point/port-map derivatives. Secant models can be used experimentally, but must not be described as exact gradients and must remain inside a guarded trust region.

Gradient audit

For a normalized random direction \(p\), compare

\[ g^Tp \quad\text{with}\quad \frac{J(\theta+hp)-J(\theta-hp)}{2h} \]

over a logarithmic sweep of \(h\). The error should decrease quadratically before cancellation dominates. Repeat for representative individual gaps, radii, cross-section parameters, and the full objective. Each perturbed solve must re-track the same branch.