OpenCL 3D electromagnetic FDFD solver
Jan Petykiewicz 4b798893bc Use python3 for setup 5 months ago
opencl_fdfd Move version string into module 5 months ago
.gitignore Cleanup and comment 2 years ago add license 2 years ago move code to new location 1 year ago Use python3 for setup 5 months ago


opencl_fdfd is a 3D Finite Difference Frequency Domain (FDFD) electromagnetic solver implemented in Python and OpenCL.


  • Arbitrary distributions of the following:
    • Dielectric constant (epsilon)
    • Magnetic permeabilty (mu)
    • Perfect electric conductor (PEC)
    • Perfect magnetic conductor (PMC)
  • Variable-sized rectangular grids
    • Stretched-coordinate PMLs (complex cell sizes allowed)

Currently, only periodic boundary conditions are included. PEC/PMC boundaries can be implemented by drawing PEC/PMC cells near the edges. Bloch boundary conditions are not included but wouldn't be very hard to add.

The default solver opencl_fdfd.cg_solver(...) located in implements the E-field wave operator directly (ie, as a list of OpenCL instructions rather than a matrix). Additionally, there is a slower (and slightly more versatile) solver in which attempts to solve an arbitrary sparse matrix in compressed sparse row (CSR) format using the same conjugate gradient method as the default solver. The CSR solver is significantly slower, but can be very useful for testing alternative formulations of the FDFD electromagnetic wave equation.

Currently, this solver only uses a single GPU or other OpenCL accelerator; generalization to multiple GPUs should be pretty straightforward (ie, just copy over edge values during the matrix multiplication step).



  • python 3 (written and tested with 3.5)
  • numpy
  • pyopencl
  • jinja2
  • fdfd_tools (>=0.2)

Install with pip, via git:

pip install git+


See the documentation for opencl_fdfd.cg_solver(...) (located in for details about how to call the solver. The FDFD arguments are identical to those in fdfd_tools.solvers.generic(...), and a few solver-specific arguments are available.

An alternate (slower) FDFD solver and a general gpu-based sparse matrix solver is available in These aren't particularly well-optimized, and something like MAGMA would probably be a better choice if you absolutely need to solve arbitrary sparse matrices and can tolerate writing and compiling C/C++ code. Still, they're usually quite a bit faster than the scipy.linalg solvers.