Skip to main content

PyTorch implementation of Mie theory

Project description

pymiediff logo

pyMieDiff

pyTorch implementation of Mie theory

pyMieDiff is a Mie scattering toolkit for layered spherical (nano-)particles in a homogeneous, isotropic environment. It is a pure PyTorch implementation of stable, logarithmic derivative based Mie series (following Peña and Pal, CPC 180, 2348 (2009)). The outstanding feature, compared to similar tools, is the general support of torch's automatic differentiation, fully vectorization and full GPU compatibility.

The source code is available on the github repository. For details, please see the online documentation.

If you use pyMieDiff for your projects, please cite our paper (arxiv:2512.08614).

How to use

Forward Mie evaluation:

import torch
import pymiediff as pmd

# - setup the particle
mat_core = pmd.materials.MatDatabase("Si")
mat_shell = pmd.materials.MatDatabase("Ge")

p = pmd.Particle(
    r_layers=[50.0, 80.0],  # nm
    mat_layers=[mat_core, mat_shell],
)

# - calculate efficiencies / cross section spectra
wl = torch.linspace(500, 1000, 50)
cs = p.get_cross_sections(k0=2 * torch.pi / wl)

plt.plot(cs["wavelength"], cs["q_ext"], label="$Q_{ext}$")

Autograd

PyMieDiff fully supports native torch autograd:

# - gradient of scattering wrt wavelength
wl = torch.as_tensor(500.0)
wl.requires_grad = True
cs = p.get_cross_sections(k0=2 * torch.pi / wl)

cs["q_sca"].backward()
dQdWl = wl.grad

Installing / Requirements

Installation should work via pip on all major operating systems. For now please use the github repository as source, we will publish pymiediff on PyPi once all main features are implemented and tested.

pip install https://github.com/UoS-Integrated-Nanophotonics-group/MieDiff/archive/refs/heads/main.zip

pymiediff is tested under linux and windows with python versions 3.10 to 3.13. It requires following python packages

  • pytorch (v2.0+)
  • scipy (v1.9+)

Optional dependencies:

  • matplotlib (plotting)
  • pyyaml (tabulated permittivity data from refractiveindex.info)

GPU support

Simply pass the device argument to the particle class:

p = pmd.Particle(
    r_layers=[50.0, 80.0],  # nm
    mat_layers=[mat_core, mat_shell],
    device="cuda",
)

Note that GPU performance is heavily memory transfer bound, GPU starts to be of advantage only for several thousand concurrent vectorized evaluations.

Features

List of features

  • multilayer spherical particles
  • scattering and extinction cross sections
  • angular scattering
  • scattered and internal near-fields
  • pure python / pytorch
  • full support of torch's automatic differentiation
  • GPU support
  • fully vectorized

Package Layout

Main package incudes

  • Particle class:
    • definition of multi-shell particles and high-level interface to main functionalities
  • multishell submodule:
    • Mie coefficients and observables for multishell particles.
  • special submodule:
    • Contains PyTorch compatible Spherical Bessel and Hankel functions and angular functions pi and tau.
  • materials subpackage:
    • PyTorch compatible materials classes for permittivity interpolation, based on the refractiveindex.info yaml format.

Contributing

If you'd like to contribute, please fork the repository and use a feature branch. Pull requests are warmly welcome.

Links

Licensing

The code in this project is licensed under the GNU GPLv3.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pymiediff-0.11.tar.gz (123.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pymiediff-0.11-py3-none-any.whl (103.8 kB view details)

Uploaded Python 3

File details

Details for the file pymiediff-0.11.tar.gz.

File metadata

  • Download URL: pymiediff-0.11.tar.gz
  • Upload date:
  • Size: 123.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for pymiediff-0.11.tar.gz
Algorithm Hash digest
SHA256 8fc428832ecd501b5b0a58fd44d4b22602b185450751c10d38496b4967db3cb3
MD5 f5bcadfa0011def248bcb78e82b5c482
BLAKE2b-256 6f1ec6be14bb0bc5a7a8aefa89eb8e27c06c7dc8295a2b3c83708709534a90bf

See more details on using hashes here.

File details

Details for the file pymiediff-0.11-py3-none-any.whl.

File metadata

  • Download URL: pymiediff-0.11-py3-none-any.whl
  • Upload date:
  • Size: 103.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for pymiediff-0.11-py3-none-any.whl
Algorithm Hash digest
SHA256 00d455af1612f021f3e5934a042eacd30a770e72eb0266f20fb532cdd22d5780
MD5 2df3f84bf7179400b4f331b21b9a2467
BLAKE2b-256 f8de8ddaa491683d78f366df1964a552cb881828bdc63c30e130c8f4c994302b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page