Skip to main content


Project description

ceviche Build Status

Electromagnetic Simulation Tools + Automatic Differentiation. Code for the arxiv preprint Forward-Mode Differentiation of Maxwell's Equations.


(logo by @nagilmer)

What is ceviche?

ceviche provides two core electromagnetic simulation tools for solving Maxwell's equations:

  • finite-difference frequency-domain (FDFD)

  • finite-difference time-domain (FDTD)

Both are written in numpy / scipy and are compatible with the HIPS autograd package, supporting forward-mode and reverse-mode automatic differentiation.

This allows you to write code to solve your E&M problem, and then use automatic differentiation on your results.

As a result, you can do gradient-based optimization, sensitivity analysis, or plug your E&M solver into a machine learning model without the tedius process of deriving your derivatives analytically.


There is a comprehensive ceviche tutorial available at this link with several ipython notebook examples:

  1. Running FDFD simulations in ceviche.
  2. Performing inverse design of a mode converter.
  3. Adding fabrication constraints and device parameterizations.
  4. Inverse design of a wavelength-division multiplexer and advanced topics.

There are also a few examples in the examples/* directory.

What can it do? An Example

Let's saw we have a simulation where we inject a current source at position source and measure the electric field intensity at probe.

Between these two points, there's a box at location pos_box with permittivity eps.

We're interested in computing how the intensity measured changes with respect to eps.

With ceviche, we first write a simple function computing the measured intensity as a function of eps using FDFD

import autograd.numpy as np           # import the autograd wrapper for numpy
from ceviche import fdfd_ez as fdfd   # import the FDFD solver

# make an FDFD simulation
f = fdfd(omega, dl, eps_box, npml=[10, 10])

def intensity(eps):
    """ computes electric intensity at `probe` for a given box permittivity of `eps`

        source |-----| probe
            .  | eps |  .

    # set the permittivity in the box region to the input argument
    fdfd.eps_r[box_pos] = eps

    # solve the fields
    Ex, Ey, Hz = f.solve(source)

    # compute the intensity at `probe`
    I = np.square(np.abs(Ex)) + np.square(np.abs(Ey))
    return = np.sum(I * probe)

Then, we can easily take the derivative of the intensity with respect to eps using a ceviche function

# use autograd to differentiate `intensity` function
grad_fn = ceviche.jacobian(intensity)

# then, evaluate it at the current value of `eps`
dI_deps = grad_fn(eps_curr)

The beauty is that ceviche lets you compute this derivative without having to do any calculations by hand! Using automatic differentiation, each step of the calculated is recorded and its derivative information is already known. This lets us take derivatives of arbitrary complex code, where the output depends in some way on the electromagnetic simulation.

Armed with this capability, we can now do things like performing gradient-based optimization (inverse design) to maximize the intensity.

for _ in range(10):
    eps_current += step_size * dI_deps_fn(eps_current)

It's also worth noting that the mathematics behind this gradient implementation uses the 'adjoint method', which lets you take derivatives with several degrees of freedom. This is perfect for inverse design problems, or training of machine learning models that involve running an FDFD or FDTD simulation. If you're interested in the connection between adjoint methods and backpropagation in the context of photonics, check out our group's earlier work on the subject link.


There are many ways to install ceviche.

The easiest is by

pip install ceviche

But to install from a local copy, one can instead do

git clone
pip install -e ceviche
pip install -r ceviche/requirements.txt

from the main directory.

Alternatively, just download it:

git clone

and then import the package from within your python script

import sys

Package Structure


The ceviche directory contains everything needed.

To get the FDFD and FDTD simulators, import directly from ceviche import fdtd, fdfd_ez, fdfd_hz, fdfd_ez_nl

To get the differentiation, import from ceviche import jacobian. contains some constants EPSILON_0, C_0, ETA_0, Q_E, which are needed throughout the package contains a few useful functions for plotting, autogradding, and various other things.


There are many demos in the examples directory, which will give you a good sense of how to use the package.


Tests are located in tests. To run, cd into tests and

python -m unittest

to run all or


to run a specific one. Some of these tests involve visual inspection of the field plots rather than error checking on values.

To run all of the gradient checking functions, run

chmod + test/


If you use this for your research or work, please cite

Author = {Tyler W Hughes and Ian A D Williamson and Momchil Minkov and Shanhui Fan},
Title = {Forward-Mode Differentiation of Maxwell's Equations},
Year = {2019},
Eprint = {arXiv:1908.10507},

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ceviche-0.1.0.tar.gz (26.7 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page