A toolkit for shape (and topology) optimization of 2D and 3D electromagnetic structures.
EMopt offers a suite of tools for simulating and optimizing electromagnetic structures. It includes 2D and 3D finite difference frequency domain solvers, 1D and 2D mode solvers, a flexible and easily extensible adjoint method implementation, and a simple wrapper around scipy.minimize. Out of the box, it provides just about everything needed to apply cutting-edge inverse design techniques to your electromagnetic devices.
A key emphasis of EMopt's is shape optimization. Using boundary smoothing techniques, EMopt allows you to compute sensitivities (i.e. gradient of a figure of merit with respect to design variables which define an electromagnetic device's shape) with very high accuracy. This allows you to easily take adavantage of powerful minimization techniques in order to optimize your electromagnetic device.
Details on how to install and use EMopt can be found on readthedocs. Check this link periodically as the documentation is constantly being improved and examples added.
Requirements: Ubuntu 22.04 / WSL2 Linux, sudo access, ~5 GB disk, ~30 min first run.
bash setup-system-deps.shThis installs g++, gfortran, OpenMPI, Eigen, Boost, and Poppler via apt,
then builds PETSc (with complex arithmetic) and SLEPc from source into
~/.emopt.
bash setup-python.shCreates .venv/ (Python 3.13), installs mpi4py, petsc4py, slepc4py,
CPU-only PyTorch, pandas, pyyaml, pdftotext, and emopt itself, then
builds the native C++ extensions.
Useful overrides:
bash setup-python.sh --no-torch
bash setup-python.sh --torch-gpuValidated working combination on this branch:
- Python
3.13 - PETSc
3.21.5 - SLEPc
3.21.2 petsc4py==3.21.5slepc4py==3.21.2setuptools<70Cython==3.0.10
source .venv/bin/activateTo recreate the Python environment later (without rebuilding PETSc):
source ~/.emopt_deps && bash setup-python.shIf you want to use emopt from a separate uv-managed project, treat this
repo as the system-dependency provider and install emopt into the other
project's virtual environment.
git clone <your-emopt-fork-url>
cd emopt
bash setup-system-deps.shThis installs PETSc and SLEPc into ~/.emopt and writes the environment file
~/.emopt_deps.
cd /path/to/your-other-repo
uv venv --python 3.13
source .venv/bin/activate
source ~/.emopt_depsuv pip install "setuptools<70" wheel "Cython==3.0.10"
uv pip install numpy scipy matplotlib h5py pyyaml pandas requests future pdftotext
uv pip install mpi4py --no-build-isolation
uv pip install "petsc4py==3.21.5" --no-build-isolation --no-deps
uv pip install "slepc4py==3.21.2" --no-build-isolation --no-depsuv pip install -e /path/to/emopt --no-build-isolationOr, from the active environment in the other repo, run the helper shipped in this repo:
/path/to/emopt/scripts/install-into-active-venv.shIf you change native code in emopt/src, rebuild from the emopt checkout:
cd /path/to/emopt
source ~/.emopt_deps
makepython - <<'PY'
import emopt
from petsc4py import PETSc
print("emopt ok")
print("PETSc scalar type:", PETSc.ScalarType)
PYNotes:
source ~/.emopt_depsmust be active whenever you build or reinstallpetsc4py,slepc4py, oremopt.- The other repo does not need to vendor PETSc or SLEPc itself; it can reuse
the shared
~/.emoptinstallation created bysetup-system-deps.sh. - Experimental
emopt.experimentalworkflows additionally require PyTorch.
New optional experimental modules for topology optimization and automatic differentiation enhanced feature-mapping approaches are implemented in emopt/experimental, with corresponding examples in examples/experimental. The AutoDiff methods can result in large improvements in optimization speed for designs with variables that parameterize global geometric features. Please see our preprint below and examples for correct usage. Note: Requires PyTorch installation. These features are still in development.
Andrew Michaels
Sean Hooten (Topology and AutoDiff methods)
EMOpt is currently released under the BSD-3 license (see LICENSE.md for details)
The methods employed by EMopt are described in:
Andrew Michaels and Eli Yablonovitch, "Leveraging continuous material averaging for inverse electromagnetic design," Opt. Express 26, 31717-31737 (2018)
An example of applying these methods to real design problems can be found in:
Andrew Michaels and Eli Yablonovitch, "Inverse design of near unity efficiency perfectly vertical grating couplers," Opt. Express 26, 4766-4779 (2018)
Shape optimization feature-mapping methods accelerated by automatic differentiation:
S. Hooten, P. Sun, L. Gantz, M. Fiorentino, R. Beausoleil, T. Van Vaerenbergh, "Automatic Differentiation Accelerated Shape Optimization Approaches to Photonic Inverse Design on Rectilinear Simulation Grids." arXiv [cs.CE], 2311.05646 (2023). Link here.