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
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.
There is no eigensolver, no square root, no branch choice and no P⁻¹.
All solves of an evaluation go into one batch on the GPU.
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.
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.
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.
pip install spectra-rcwaIf 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}
}