TorchFDTD

https://github.com/user-attachments/assets/021d317c-bd63-4019-8391-9211d9be4166
Also on YouTube: TorchFDTD Explained: GPU-Accelerated FDTD for Inverse Design.
Open-source GPU FDTD for photonics, with PyTorch gradients and a browser workbench. MIT licensed.
Build a device in Python or in the browser, simulate its electromagnetic fields, and use discrete adjoints with PyTorch autograd for inverse design. AI agents can drive it through the bundled MCP server and the agent guide.
It covers waveguides and splitters, ring resonators, photonic crystals, gratings, metasurfaces and metalenses in 2D and 3D. It runs natively on Windows and Linux, on NVIDIA GPUs through PyTorch with a CPU fallback, and is validated against analytic solutions, Mie theory, Meep, FDTDX, BeamZ and RCWA (records).
If TorchFDTD is useful to you, a GitHub star helps other researchers find it.
Quick start · Adjoint inverse design · Paper designs · Examples · Validation · Documentation · Paper

A pulse couples into a microring, circulates and leaks back into the bus waveguide. Actual 2D FDTD, showing optical-cycle RMS Ez with a fixed color scale. Full-resolution video · Model and reproduction.
- Run on your NVIDIA GPU. Fused CUDA kernels and CUDA Graphs accelerate field updates. CPU execution is available too.
- Differentiate your simulation. Discrete adjoints connect material, geometry and source parameters to PyTorch autograd.
- Work in Python or the browser. Both use the same project format. Save a browser-built device and run it from Python, or open a Python-built project in the workbench.
Quick start
Install and open the workbench
Install from PyPI with Python 3.10 or 3.12 and start the workbench:
pip install torchfdtd
torchfdtd serve
The package includes the browser workbench, so you do not need Node.js or a source checkout. Open http://127.0.0.1:8765 after starting the server.
NVIDIA GPU installation on Windows / PowerShell
Select a CUDA-enabled PyTorch build and install the cuda-kernels extra:
python -m venv torchfdtd-env
torchfdtd-env/Scripts/python.exe -m pip install torch --index-url https://download.pytorch.org/whl/cu126
torchfdtd-env/Scripts/python.exe -m pip install "torchfdtd[cuda-kernels]"
torchfdtd-env/Scripts/torchfdtd doctor
torchfdtd-env/Scripts/torchfdtd serve
torchfdtd doctor checks the installation, CUDA device and fused-kernel launch before you start.
For a CPU-only installation, use the PyTorch index https://download.pytorch.org/whl/cpu and omit [cuda-kernels] from the installation command. The cuda-kernels extra installs CuPy for the fused CUDA path. See installation and tested versions for requirements and troubleshooting.
The server listens on loopback only. Use SSH forwarding for a remote GPU. The default workbench limit is 8 million resident cells. For larger local projects, see memory admission and server limits.
Run from Python
This example runs a 3D waveguide on the fused CUDA path and saves both the project and its result:
from torchfdtd import Project, Region, Structure, Source, Monitor, Simulation
project = Project(
name="My waveguide",
region=Region(dimension="3d", size=(8, 6, 2), mesh=0.05, steps=1000,
backend="cuda", cuda_kernel="fused"),
structures=[Structure(name="core", size=(8, 0.65, 0.4))],
sources=[Source(center=(-2.5, 0, 0), wavelength=1.55)],
monitors=[Monitor(name="output", center=(2, 0, 0))],
)
project.save("project.json") # opens in the browser
result = Simulation(project).run()
result.save("results/run.npz")