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 pymiediff

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.12.tar.gz (123.4 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.12-py3-none-any.whl (104.2 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for pymiediff-0.12.tar.gz
Algorithm Hash digest
SHA256 8fd6f352156a5e9c6785fe6ebeb946e91541d3ebbf4a3c0adcfc991ec11822fd
MD5 fc90949639b657594fa971b456d648bc
BLAKE2b-256 770e56b40155901b537c63e8513d0d27c714ed2cd05f86e661e1f76b5bf09e64

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pymiediff-0.12-py3-none-any.whl
  • Upload date:
  • Size: 104.2 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.12-py3-none-any.whl
Algorithm Hash digest
SHA256 10ffd39ede084c1ecfefe8da9c0302db6d049553a1d6b18f7b246e2a2eeb66e8
MD5 c7bdaafbabed1065e92fc0c288a978f4
BLAKE2b-256 641d2f55f8a708f6f83db5755f85ffbeb0d92b9d754521467f296002f6414ab1

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