Skip to content

gbeane66/pytmat

Repository files navigation

🌈 pyTMat

PyPI version Downloads CI License: MIT Python 3.8+

pyTMat is a blazing-fast, user-friendly Python package for simulating optical multilayer stacks using the Transfer Matrix Method (TMM). Powered by a Rust core for maximum performance, pyTMat makes it easy to compute reflection and transmission spectra for complex photonic structures — perfect for research, engineering, and education.


🚀 Features

  • Ultra-fast: Rust-powered core for high-performance calculations
  • Easy-to-use: Simple Python API, no need to write Rust
  • Parallelized: Automatically uses all your CPU cores for large wavelength arrays
  • Polarization Support: TE, TM, and arbitrary polarization angles
  • Complex Refractive Indices: Supports lossy materials with complex indices
  • Flexible Layer Configurations: Any number of layers with arbitrary thicknesses
  • Incident Angles: Specify any angle of incidence
  • Wavelength Range: Define custom wavelength ranges for your simulations
  • Numpy Integration: Works seamlessly with NumPy arrays
  • Perfect for DBRs, filters, sensors, and more!

📦 Installation

Install the latest release directly from PyPI:

pip install pytmat

🧑‍💻 Quick Start

import numpy as np
import pytmat
import matplotlib.pyplot as plt

# Define layer thicknesses (nm)
d = np.array([200.0])  # Example: 3 layers, with two seminfinite layers on either side of a 200 nm layer.

# Define complex refractive indices for each layer at each wavelength
# Shape: (num_layers, num_wavelengths)
# Define number of wavelengths (300 for example)
N = 300
# Define refractive indices for 3 layers (air, glass, air) at 300 wavelengths
# Here, we assume air (n=1.0) and a glass layer (n=1.5)
# The first and last layers are seminfinite air layers, so their refractive index is 1.0
# The middle layer is a glass layer with a refractive index of 1.5
n_air = np.full(N, 1.0, dtype=np.complex128)
n_glass = np.full(N, 1.5, dtype=np.complex128)

n = np.array([n_air,n_glass,n_air])

# Wavelengths (nm)
wl = np.linspace(400, 700, N)

# Angle of incidence (radians) and polarization angle (radians)
theta = 0.0  # normal incidence
phi = 0.0    # TE polarization

# Create the TMM data object
data = pytmat.DataPy(d, n, wl, theta, phi)

# Simulate the multilayer stack
simulation = data.simulate()

# Compute reflection and transmission spectra
R, T = simulation.r, simulation.t

fig, ax = plt.subplots()
ax.plot(wl, R, label='Reflection',color='blue')
ax.plot(wl, T, label='Transmission', color='orange')
ax.set_xlabel('Wavelength (nm)')
ax.set_ylabel('Reflectance / Transmittance')
plt.title('Multilayer Stack Reflection and Transmission')
ax.legend()
plt.show()

📚 API Overview

  • DataPy(d, n, wl, theta, phi): Main class for defining your multilayer stack.
    • d: 1D array of layer thicknesses (float, nm or μm)
    • n: 2D array of complex refractive indices (layers × wavelengths)
    • wl: 1D array of wavelengths
    • theta: Angle of incidence (radians)
    • phi: Polarization angle (radians, 0=TE, π/2=TM)
  • simulate(): DataPy method to run the simulation.
  • Simulation: Result object containing:
    • r: Reflection spectrum (array)
    • t: Transmission spectrum (array)

🛠️ Advanced Usage

  • Arbitrary polarization: Set phi between 0 (TE) and π/2 (TM) for mixed polarization.
  • Large arrays: For >100 wavelengths, pyTMat automatically parallelizes computations.
  • Complex indices: Supports lossy materials (complex n).

🏗️ Project Structure

pytmat/
├── tmatrix/         # Rust core library
├── pytmat/          # Python bindings (PyO3/maturin)
├── tests/           # Python tests
├── README.md
└── ...

🤝 Contributing

Contributions, bug reports, and feature requests are welcome!
Please open an issue or submit a pull request.


📄 License

This project is licensed under the MIT License.


🌟 Acknowledgements


🔗 Links


pyTMat — Fast, flexible, and fun multilayer optics for Python

About

Fast multilayer optics simulation in Python with Rust (Transfer Matrix Method)

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages