Skip to content

gfem: finite element electromagnetics you can differentiate

gfem is a new open-source Python package for finite element electromagnetics in which every result is a JAX function of the materials and the geometry, ready for inverse design.

3D topology-optimized silicon waveguide bend: the silicon pattern in the design square (grey) and the field carried round the 90° turn (red)

gfem is a new open-source Python package I have been writing over the past weeks. It solves Maxwell’s equations with the finite element method, and every result it returns (S-parameters, resonant frequencies and Q factors, capacitances, fields) is a differentiable function of the materials and the geometry. Version 0.2.0 is on PyPI, and the documentation lives at bvial.info/gfem.

Why

Inverse design needs gradients. Topology or shape optimization of a device with thousands of design variables only works if each simulation also returns the derivative of the objective with respect to all of them. I wanted full-wave finite element results, the ones I use for RF, superconducting quantum and photonic devices, with those gradients attached, from plain Python, and checked rather than assumed.

How it works

gfem uses NGSolve to discretize and solve, and JAX for everything differentiable. gfem.solve is a JAX function, so jax.grad gives the gradient by the adjoint method, at the cost of one extra linear solve.

The example below takes a parallel-plate line whose middle section holds a ceramic of relative permittivity 10 (the first example of the Get started page), and differentiates the transmitted power at 5 GHz with respect to that permittivity:

import jax
import gfem

at_5ghz = gfem.Driven(frequency=5e9, excite="p1")


def transmission(ceramic):
    """|S21|^2 at 5 GHz with this ceramic in the middle section."""
    s21 = gfem.solve(model.with_material("design", ceramic), at_5ghz).s["p2", "p1"]
    return abs(s21[0]) ** 2


ceramic = gfem.MaterialField.uniform("design", gfem.Material(eps_r=10.0))
power, grad = jax.value_and_grad(transmission)(ceramic)
print(power, grad.eps_r)  # 0.8124 -0.3002

The same works with a permittivity per element (topology optimization), with the fill of a patterned metal sheet, with the mesh node coordinates (shape optimization) and with the parameters of Drude, Lorentz or Debye permittivity laws. A test suite checks every supported gradient against finite differences, and asking for an unsupported one raises an error instead of silently returning a wrong value. The documentation publishes this gradient matrix in full.

A 3D example

The figure above comes from the 3D bend tutorial. It designs a 90° bend between two 500 nm wide, 220 nm thick silicon waveguides in oxide, at 1.55 µm. The design region is a 3 µm × 3 µm square of 60 × 60 pixels, starting from a uniform density of 0.5. Each of the 42 Adam iterations runs one driven solve with 2.5 million unknowns and its gradient, in a median of 147 s on 16 cores. The final design, thresholded to pure silicon and oxide, carries 97.3 % of the power round the turn (a loss of 0.12 dB).

What else it solves

The electromagnetic core covers driven problems with lumped, wave and Floquet ports, eigenmode problems with their Q factors, electrostatic capacitance matrices and waveguide modes with their dispersion. Around it, gfem handles open and periodic structures (perfectly matched layers, plane waves, dipoles, far fields, band diagrams), anisotropic and dispersive materials, and multiphysics: heat conduction with Joule and electromagnetic heating, piezoelectric and mechanical problems, and the thermo-optic, electro-optic and acousto-optic phase shifts these produce. For superconducting circuits it computes participation ratios, loss budgets and dressed transmon frequencies.

The tutorials go from a microwave band-stop filter and a coplanar line matched to 50 Ω to a transmon, a metalens, a thermo-optic heater and an acousto-optic modulator, many of them designed with gradients. Large models run across MPI ranks or on an NVIDIA GPU, and gfem installs with pip on Linux, macOS (Apple Silicon) and Windows.

Try it

pip install gfem

gfem is MIT-licensed. The API may still change before 1.0, so issues and feedback on GitHub are very welcome, especially from anyone doing inverse design of RF, quantum or photonic hardware.