Metadata-Version: 2.4
Name: gfem
Version: 0.2.0
Summary: Differentiable finite-element electromagnetics for RF, microwave, mm-wave and superconducting quantum devices.
Author: Benjamin Vial
Author-email: Benjamin Vial <contact@bvial.info>
License-Expression: MIT
License-File: LICENSE
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Science/Research
Classifier: Operating System :: MacOS
Classifier: Operating System :: Microsoft :: Windows
Classifier: Operating System :: POSIX :: Linux
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Topic :: Scientific/Engineering :: Physics
Classifier: Typing :: Typed
Requires-Dist: jax>=0.8.3
Requires-Dist: meshio>=5.3.5
Requires-Dist: ngsolve>=6.2.2608,<6.3
Requires-Dist: numpy>=2.2
Requires-Dist: scipy>=1.15
Requires-Dist: nvmath-python[cu12]>=1.0,<1.1 ; (platform_machine == 'x86_64' and sys_platform == 'linux' and extra == 'cuda12') or (sys_platform == 'win32' and extra == 'cuda12')
Requires-Dist: nvmath-python[cu13]>=1.0,<1.1 ; (platform_machine == 'x86_64' and sys_platform == 'linux' and extra == 'cuda13') or (sys_platform == 'win32' and extra == 'cuda13')
Requires-Dist: mkl>=2026.2 ; (platform_machine == 'x86_64' and sys_platform == 'linux' and extra == 'mkl') or (platform_machine == 'AMD64' and sys_platform == 'win32' and extra == 'mkl')
Requires-Dist: mpi4py>=4.1.2,<5 ; platform_machine == 'x86_64' and sys_platform == 'linux' and extra == 'mpi'
Requires-Dist: pyvista ; extra == 'viz'
Requires-Dist: xarray>=2024.10 ; extra == 'xarray'
Requires-Python: >=3.12
Project-URL: Homepage, https://bvial.info/gfem/
Project-URL: Documentation, https://bvial.info/gfem/
Project-URL: Source, https://github.com/benvial/gfem
Project-URL: Issues, https://github.com/benvial/gfem/issues
Provides-Extra: cuda12
Provides-Extra: cuda13
Provides-Extra: mkl
Provides-Extra: mpi
Provides-Extra: viz
Provides-Extra: xarray
Description-Content-Type: text/markdown

<h1>
  <picture>
    <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/benvial/gfem/main/docs/_static/logo/gfem-lockup-dark.svg">
    <img alt="gfem" src="https://raw.githubusercontent.com/benvial/gfem/main/docs/_static/logo/gfem-lockup-light.svg" height="64">
  </picture>
</h1>

Differentiable finite-element electromagnetics for RF, microwave, mm-wave and superconducting quantum devices.
gfem runs on [NGSolve](https://ngsolve.org) for discretization and [JAX](https://jax.readthedocs.io) for
everything differentiable.

gfem 0.2.0 is the current release. The API may still change before 1.0.

## Install

```bash
pip install gfem
```

Each optional extra goes in brackets after `gfem`:

```bash
pip install "gfem[mkl]"     # a faster CPU direct solver, MKL PARDISO (Linux x86_64, Windows); read the licence first
pip install "gfem[cuda12]"  # the direct solver on an NVIDIA GPU, cuDSS (Linux x86_64, Windows); cuda13 for CUDA 13
pip install "gfem[xarray]"  # results as xarray datasets (.to_xarray())
pip install "gfem[viz]"     # fields as PyVista grids (.to_pyvista())
pip install "gfem[mpi]"     # one solve across MPI ranks (Linux x86_64; Open MPI from your machine)
```

Supported platforms: Linux x86_64 (glibc >= 2.28), macOS arm64 and Windows x86_64, Python >= 3.12.

Importing gfem turns on JAX double precision (`jax_enable_x64`), which every gfem solve requires.

## A first solve

A parallel-plate line 40 mm long, 6 mm wide and 1 mm high joins two lumped ports matched to it. Its middle 20 mm
is filled with a ceramic of relative permittivity 10. How much of the incident power reaches port 2 at 2, 5 and
8 GHz?

```python
import urllib.request

import gfem

url = "https://raw.githubusercontent.com/benvial/gfem/main/docs/tutorials/data/line.msh"
urllib.request.urlretrieve(url, "line.msh")
mesh = gfem.Mesh.from_gmsh("line.msh", length_unit="mm")

z_line = 376.73 / 6  # ohm: the 6 mm wide, 1 mm high parallel-plate line in air
model = gfem.Model(
    mesh,
    materials={"feed": gfem.Material(), "design": gfem.Material(eps_r=10.0)},
    boundaries=[gfem.PEC("bottom"), gfem.PEC("top")],
    ports=[
        gfem.LumpedPort("p1", "port1", "+z", impedance=z_line),
        gfem.LumpedPort("p2", "port2", "+z", impedance=z_line),
    ],
)
result = gfem.solve(model, gfem.Driven(frequency=(2e9, 5e9, 8e9), excite="p1"))
print(abs(result.s["p2", "p1"]))  # |S21| at 2, 5 and 8 GHz
```

```text
[0.83062238 0.90133217 0.60517471]
```

[Get started](https://bvial.info/gfem/get-started.html) explains each line, checks the numbers by hand and differentiates the
result.

## Documentation

The documentation is at <https://bvial.info/gfem/>. Good places to start:

- [Get started](https://bvial.info/gfem/get-started.html): install, a first solve and its gradient.
- [Tutorials](https://bvial.info/gfem/tutorials/topology.html): a band-stop filter, a coplanar line matched to 50 Ω, photonic
  waveguide bends and a metagrating, each designed with gradients.
- [Heaters and thermo-optic phase](https://bvial.info/gfem/recipes/heater-temperature.html): steady and transient
  heat conduction, Joule heating from a DC current, and the phase a heater puts on a waveguide, in 2D and 3D.
- [PETSc and SLEPc](https://bvial.info/gfem/recipes/petsc-slepc.html): the PETSc and SLEPc solvers, which need a source build.
- [Distributed solve](https://bvial.info/gfem/recipes/distributed-solve.html): one driven, electrostatic or eigenmode solve across the ranks of an MPI job.

## Licence

MIT. gfem depends on NGSolve (LGPL-2.1), whose PyPI wheels also contain GPL-2.0+ SuiteSparse code (UMFPACK, CHOLMOD
Supernodal); keep that in mind when redistributing a whole gfem environment. The `mkl` and `cuda12`/`cuda13`
extras pull proprietary vendor libraries: see [Licence](https://bvial.info/gfem/project/licence.html).

## Contributing

To build gfem, run its tests or rebuild the docs, see [Contributing](https://bvial.info/gfem/project/development.html).
