PyTorch implementation of Mie theory
Project description
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
Particleclass:- definition of multi-shell particles and high-level interface to main functionalities
multishellsubmodule:- Mie coefficients and observables for multishell particles.
specialsubmodule:- Contains PyTorch compatible Spherical Bessel and Hankel functions and angular functions pi and tau.
materialssubpackage:- 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
- documentation: https://uos-integrated-nanophotonics-group.github.io/MieDiff/index.html
- github repository: https://github.com/UoS-Integrated-Nanophotonics-group/MieDiff
Licensing
The code in this project is licensed under the GNU GPLv3.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8fc428832ecd501b5b0a58fd44d4b22602b185450751c10d38496b4967db3cb3
|
|
| MD5 |
f5bcadfa0011def248bcb78e82b5c482
|
|
| BLAKE2b-256 |
6f1ec6be14bb0bc5a7a8aefa89eb8e27c06c7dc8295a2b3c83708709534a90bf
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
00d455af1612f021f3e5934a042eacd30a770e72eb0266f20fb532cdd22d5780
|
|
| MD5 |
2df3f84bf7179400b4f331b21b9a2467
|
|
| BLAKE2b-256 |
f8de8ddaa491683d78f366df1964a552cb881828bdc63c30e130c8f4c994302b
|