SPECTRA-RCWA
Open source · MIT

SPECTRA-RCWA: Differentiable GPU RCWA Solver for PyTorch

SPECTRA-RCWA is an open-source (MIT) solver for periodic layered structures by rigorous coupled-wave analysis (RCWA, also called the Fourier modal method, FMM) that needs no eigendecomposition. It is written in PyTorch, runs many solves as one batch on a GPU (or on the CPU), and returns diffraction efficiencies, fields and the gradient of any objective with respect to every pixel of the pattern from one adjoint field.

pip install spectra-rcwa
A periodic stack of patterned layers solved as one batch, modal RCWA with one eigendecomposition per solve, and SPECTRA-RCWA with batched matrix products that are smooth at cutoff
One objective over many wavelengths, angles and polarisations as one batch of matrix products, with no eigendecomposition. (a) A periodic stack of patterned layers, each cut into steps of thickness h. (b) Modal RCWA diagonalises the layer matrix A = PQ, one solve at a time, as VΛV⁻¹; its propagation constants are square roots of the eigenvalues λ, which are singular at the cutoff of an order (λ = 0), and so is the gradient. (c) SPECTRA-RCWA forms C, S and G for all B solves at once by matrix products. These are entire functions of A, smooth at the cutoff.
Speed

144 solves with gradients in 0.19 s on one RTX 5090

0.19 svalue and gradient, 144 solves with 578 x 578 layer matrices, complex64
38×or more faster than the other GPU RCWA codes we tested
2.2e-6max error of the zeroth-order transmission
6.1 GiBpeak memory
Time for 144 solves, value only and value with gradient, for SPECTRA-RCWA, TorchRDIT, fmmax, torcwa, Meent, grcwa, inkstone, rcwa and S4
Time for 144 solves (9 wavelengths x 16 incidence directions, both polarisations, (8, 8) orders, 578 x 578 layer matrices) of a 0.25 µm freeform layer, one RTX 5090. (a) Value only. (b) Value and gradient with respect to all 64 x 64 pattern values. Ratios are against SPECTRA-RCWA on the same problem. S4 was measured on a problem of the same size (0.8 µm layer, 128 x 128 values) on 12 CPU cores; its ratio uses SPECTRA-RCWA's value time there (0.21 s).

SPECTRA-RCWA 1.6.2, TorchRDIT, fmmax and torcwa in complex64, Meent in complex128 on the RTX 5090; grcwa, inkstone and rcwa in complex128 on 4 CPU threads. Meent, grcwa, inkstone and rcwa were timed on 16 of the 144 solves (one wavelength, all directions) and multiplied by 9: these codes solve one wavelength and direction per call. Error: largest absolute error of the zeroth-order transmission (average of two orthogonal polarisations) over the 144 solves, against a converged complex128 eigendecomposition solution with the Laurent rule and rectangular truncation. SPECTRA-RCWA, TorchRDIT, fmmax, torcwa and Meent (torch) were timed on 2026-11-25 on one machine, the others on 2026-11-23 on another. Codes and versions as installed from PyPI on 2026-11-23, each with its documented API and default settings. The full table is in the README, the records in benchmarks/results.

No eigendecomposition

There is no eigensolver, no square root, no branch choice and no P⁻¹.

One batch

All solves of an evaluation go into one batch on the GPU.

One adjoint field

The gradient has neither 1/(λi − λj) nor 1/√λ terms, and a layer order at cutoff (λ near 0) needs no special handling.

Use it to simulate and optimise diffraction gratings, metagratings, metasurfaces, photonic-crystal slabs, thin-film stacks and colour routers, in particular when an inverse design needs value and gradient over many wavelengths and incidence angles, for thick or multilayer patterns, or near the cutoff of a diffraction order, where eigendecomposition gradients fail.

Install

pip install spectra-rcwa              # or pip install git+https://github.com/hyoseokp/spectra-rcwa
pip install "spectra-rcwa[mcp]"       # the MCP server for agents

torch >= 2.6 and numpy; complex64 on CUDA needs torch >= 2.8. The torch wheel is enough, no CUDA toolkit or CUDA_HOME is needed.

Minimal example

import torch
from spectra_rcwa import ColorRouterForward, Stack, shifted_disk_pupil, value_and_grad

stack = Stack(period_um=(2.0, 2.0), thickness_um=0.8, density_shape=(64, 64), fourier_orders=(4, 4),
              eps_design=2.05 ** 2, eps_fill=1.0, eps_in=1.0, eps_out=1.46 ** 2, detector_z_um=1.25)
rays = shifted_disk_pupil(numerical_aperture=0.30, chief_ray_angle_deg=2.5, chief_ray_azimuth_deg=22.5)
fwd = ColorRouterForward(stack=stack, rays=rays, wavelengths_nm=(450.0, 540.0, 630.0))     # on the GPU if there is one

density = torch.rand(64, 64)
tabs = fwd.response(density.to(fwd.device))   # Tabs[well (R, G2, G1, B), wavelength], float64, differentiable
target = torch.eye(4, 3, dtype=torch.float64)
tabs, value, grad = value_and_grad(fwd, density, lambda t: (t * target).sum())

examples/minimal.py runs a smaller case of it on CPU or GPU. Permittivities can be numbers or functions of the wavelength in µm.

Documentation

Citation

If you use SPECTRA-RCWA, please cite the paper, arXiv:2611.30305.

@misc{park2026spectrarcwa,
  author        = {Park, Hyoseok},
  title         = {{SPECTRA-RCWA}: A Spectral Propagation Engine for Coupled-Wave Theory with Recursive Admittance},
  year          = {2026},
  eprint        = {2611.30305},
  archivePrefix = {arXiv},
  primaryClass  = {physics.optics},
  doi           = {10.48550/arXiv.2611.30305},
  url           = {https://arxiv.org/abs/2611.30305}
}