gfem

Electromagnetics you can differentiate.

Gradients of full-wave FEM results with respect to your geometry and materials, in Python.

Driven, eigenmode and electrostatic problems, from microwave circuits to photonic waveguides, on NGSolve and JAX.

value_and_grad = jax.jit(jax.value_and_grad(missed_power(bend, grid, blur), has_aux=True))
ρ→mirror about y = x→filter→project→permittivity→gfem.solve→1 − |S21|² ← jax.value_and_grad back to ρ
A 90° bend between two 0.5 µm guides of 220 nm silicon in oxide, optimized in 3D at 1.55 µm: each of the 42 iterations is one driven solve of 2.49 M unknowns and its gradient. Grey: the silicon in the 3 µm × 3 µm design square, shaded by density; red: the surface where |E| is 0.35 of its peak in the input guide, with 1 W incident at p1, and |E| on the core mid-plane drawn on the floor of the box. The last iteration gives way to final, the design thresholded at 0.5.Rendered from a rerun of the 3D bend

Where that gradient leads

Which pattern of silicon and oxide, in the 3 µm square where the two guides meet, carries the most 1.55 µm light round the turn?

42 Adam steps on 60 × 60 pixels of 0.05 µm, from 0.5 everywhere, the projection sharpened every 7; one step took a median 147 s on 16 cores. final carries 0.9726 of the power into the output guide's fundamental mode, a loss of 0.121 dB.

It was optimized at 1.55 µm only: over 1.45 to 1.65 µm, |S21|² falls to 0.8254 at 1.65 µm. The tutorial shows the spectrum.

Read the 3D bend tutorial
iteration 0|S21|² 0.022
iteration 14|S21|² 0.899
iteration 27|S21|² 0.961
iteration 41|S21|² 0.973
Si, n = 3.48SiO2, n = 1.44

The media are lossless, so for final the tutorial checks reciprocity, |S12 − S21| = 1.5e-14, and power balance, |S11|² + |S21|² = 0.9730, at most 1: the rest leaves as the guide's second mode or radiation, which the tutorial splits.

What differentiates

Every supported gradient is checked against finite differences. An unsupported one raises an error instead of returning zero; a zero means the physics never reads that input.

337 cells (271 on the direct solver, 66 repeated on BDDC + GMRES), each checked along 3 random directions against fourth-order central differences. How gfem is checked

Full matrix on Capabilities
ProblemPermittivityConductivitySheet fillNode coordinates
DrivenS-parameters, uniform and adaptive sweep FD-verified FD-verified FD-verified FD-verified
Eigenmodefrequency and Q FD-verified FD-verified FD-verified FD-verified
Electrostaticcapacitance FD-verified exact zero raises NotImplementedError FD-verified
Port mode problemeffective index, impedance and field FD-verified FD-verified FD-verified FD-verified
checked against finite differencesexact zero: the physics never reads the inputraises: NonDifferentiableError, or the error the cell names

Install

gfem is v0.2.0.

Get started
pip install gfem

Linux x86_64, macOS arm64 and Windows x86_64; Python 3.12 or later. Importing gfem turns on JAX double precision.