miepython

repository·main·Indexed 42 days ago

https://github.com/scottprahl/miepython

A pure Python library for validated and efficient Mie scattering calculations of a plane wave by a sphere. It provides tools to calculate scattering efficiencies (qext, qsca, qback), anisotropy (g), complex scattering amplitudes S1 and S2, and the scattering phase matrix. The library includes specialized functions for small spheres, perfectly conducting spheres, and near-field calculations via the miepython.field module, as well as Rayleigh scattering approximations and Monte Carlo angle sampling.

Tokens
15K
Snippets
59
Records
73
Agent score
53%

What's inside miepython

  1. Overview of miepython capabilities

    main

    miepython is a pure Python implementation of Mie scattering for spherical particles. It is designed for scientific and educational research and includes:

    • Extinction, scattering, and absorption efficiencies
    • Asymmetry parameter (scattering anisotropy)
    • Angle-resolved scattering intensities
    • Mie expansion coefficients
    • Complex amplitude functions and Mueller matrices
    • E-field and H-field calculations in the near-field around a sphere
  2. Quick Start: Calculate scattering efficiencies

    main

    To perform a basic Mie scattering calculation, use the mie.efficiencies() function. This returns the extinction, scattering, and backscattering efficiencies, as well as the asymmetry parameter (g).

    import miepython as mie
    
    m = 1.5 - 0.1j     # refractive index
    d = 100            # diameter (nm)
    lambda0 = 314.15   # wavelength (nm)
    
    qext, qsca, qback, g = mie.efficiencies(m, d, lambda0)
  3. Use miepython in the browser via JupyterLite

    main
    You can use the entire miepython package immediately in a web browser without any local installation using the JupyterLite interface. This environment runs client-side via Pyodide and is suitable for interactive notebooks, real-time plotting, and reproducible experiments.
  4. Accelerate miepython with Numba JIT

    main

    For large parameter sweeps or ensemble calculations, you can enable Numba JIT compilation to achieve 10–50× speedups.

    Important: The environment variable MIEPYTHON_USE_JIT must be set to "1" before importing miepython.

    import os
    os.environ["MIEPYTHON_USE_JIT"] = "1"  # must be set before import
    import miepython
  5. Calculate radiation pressure efficiency

    main

    The radiation pressure efficiency $Q_{pr}$ represents the momentum given to a scattering particle in the direction of the incident wave. It is calculated using the extinction efficiency, scattering efficiency, and the scattering anisotropy ($g$).

    Formula: $Q_{pr} = Q_{ext} - g Q_{sca}$

    The radiation pressure cross section is $\sigma_{pr} = \pi r^2 Q_{pr}$.

    # r is radius, qext, qsca, g are from mie.efficiencies_mx
    qpr = qext - g * qsca
  6. Configure scattering function normalization

    main

    The miepython library supports multiple normalization conventions for scattering phase functions. This allows you to match the output of different scientific software packages or theoretical models. When calling functions like i_unpolarized, i_per, or i_par, you can specify the normalization using the norm parameter.

    Supported normalization strings include:

    • albedo: (Default) Integrates to the single scattering albedo $a = Q_{sca}/Q_{ext}$.
    • one: Integrates to $1$.
    • 4pi: Integrates to $4 ext{pi}$.
    • qsca: Integrates to the scattering efficiency $Q_{sca}$.
    • qext: Integrates to the extinction efficiency $Q_{ext}$.
    • bohren: Integrates to $4 ext{pi} x^2 Q_{sca}$ (Bohren & Huffman convention).
    • wiscombe: Integrates to $ ext{pi} x^2 Q_{sca}$ (Wiscombe convention).

    Note that $x$ is the size parameter ($x = 2 ext{pi} a / ext{lambda}$).

    # Example: Using Wiscombe normalization
    intensity = mie.i_unpolarized(m, x, mu, norm='wiscombe')
  7. Calculate scattering and absorption cross sections

    main

    Cross sections ($\sigma$) have units of area and are derived from dimensionless efficiencies by multiplying by the sphere's geometric cross section ($\pi r^2$).

    • Scattering cross section: $\sigma_{sca} = \pi r^2 Q_{sca}$
    • Extinction cross section: $\sigma_{ext} = \pi r^2 Q_{ext}$
    • Backscattering cross section: $\sigma_{back} = \pi r^2 Q_{back}$
    • Absorption cross section: $\sigma_{abs} = \sigma_{ext} - \sigma_{sca}$
    r = 0.3  # radius in microns
    geometric_cross_section = np.pi * r**2
    
    # Assuming qext, qsca are obtained from mie.efficiencies_mx
    absorb = (qext - qsca) * geometric_cross_section
    scatt = qsca * geometric_cross_section
    extinct = qext * geometric_cross_section
  8. Compare Mie scattering to Henyey-Greenstein approximation

    main

    The Henyey-Greenstein (HG) phase function is often used to approximate Mie scattering, but it can be a poor approximation for large droplets (like fog) where forward scattering is extremely strong.

    To compare them, you must ensure both are normalized. The integral of the Mie scattering function over all angles is $\pi x^2 Q_{\text{sca}}$.

    Henyey-Greenstein formula: $$HG(g, \cos\theta) = \frac{1}{4\pi} \frac{1 - g^2}{(1 + g^2 - 2g\cos\theta)^{1.5}}$$

    Note that for large particles, the Mie scattering function has a much narrower and higher peak in the forward direction than the HG function, even if they share the same anisotropy parameter $g$.

  9. Conventions for electromagnetic field calculations

    main

    When using miepython.field, the following physical and mathematical conventions are applied:

    • Phasor Time Dependence: $\exp(-i \cdot \omega \cdot t)$.
    • Incident Plane Wave: Propagates along $+z$, with electric field $\mathbf{E}$ along $+x$ and magnetic field $\mathbf{H}$ along $+y$. The incident amplitude is $E_0 = 1$.
    • Spherical Coordinates: $\theta$ is measured from the $+z$ axis; $\phi$ is measured from the $+x$ axis toward the $+y$ axis.
    • Refractive Indices: lambda0 is the vacuum wavelength. n_env is the surrounding medium index and m_sphere is the sphere index. The relative index is $m_{rel} = m_{sphere} / n_{env}$.
    • Size Parameter: $x = \pi \cdot d_{sphere} \cdot n_{env} / \lambda_0$.
    • Near Field Definition:
      • Outside the sphere: The returned field is the sum of the incident and scattered fields.
      • Inside the sphere: The returned field is the internal field.
    • Magnetic Properties: Materials are assumed to be non-magnetic (relative permeability $\mu_r = 1$).
  10. How JIT-accelerated Mie functions work

    main

    The mie_jit.py module provides low-level Mie scattering kernels optimized using numba.njit. These functions are designed for high-performance execution by compiling Python code to machine code.

    Key Characteristics:

    • Stability: The module uses stable recurrence methods (like Miller's algorithm for downwards recurrence) to avoid numerical divergence in Riccati-Bessel functions.
    • Wiscombe's Criterion: The implementation uses Wiscombe's truncation and recurrence selection to balance accuracy and performance, particularly for small spheres where upwards recurrence can become unstable.
    • Precision: The kernels avoid fastmath to preserve exact identities (like $\text{Re}(a_n) == |a_n|^2$ for lossless spheres) and to ensure correct handling of infinite/NaN values.
    • Memory Management: Functions like _pi_tau_nb use pre-allocated arrays to minimize overhead in tight loops.