Skip to main content

Nonlinear transfer matrix method

Project description

PyPI version Python License: MIT Pytest Pre-commit Build and upload to PyPI

NonlinearTMM: Nonlinear Transfer-Matrix Method

A Python library for optical simulations of multilayer structures using the transfer-matrix method, extended to support nonlinear processes (SHG, SFG, DFG) and Gaussian beam propagation.

Gaussian beam exciting surface plasmon polaritons

See also: GeneralTmm — a 4×4 TMM for anisotropic (birefringent) multilayer structures.

Table of Contents

Features

  • Standard TMM — reflection, transmission, absorption for p- and s-polarized plane waves at arbitrary angles
  • Parameter sweeps — over wavelength, angle of incidence, layer thickness, or any other parameter
  • 1D and 2D electromagnetic field profiles — E and H field distributions through the structure
  • Field enhancement — calculation of field enhancement factors (e.g. for SPP excitation)
  • Gaussian beam propagation — any beam profile through layered structures, not just plane waves
  • Second-order nonlinear processes — SHG, SFG, DFG in multilayer structures
  • Wavelength-dependent materials — interpolated from measured optical data (YAML format)
  • High performance — C++ core (Eigen) with Cython bindings, OpenMP parallelization
  • Cross-platform wheels — Linux (x86_64), Windows (x64, ARM64), macOS (ARM64); Python 3.10–3.14

Installation

pip install NonlinearTMM

Pre-built wheels are available for most platforms. A C++ compiler is only needed when installing from source.

API Overview

The library exposes three main classes: Material, TMM, and SecondOrderNLTMM.

Class / method Purpose
Material(wls, ns) Wavelength-dependent material from arrays of λ and complex n
Material.Static(n) Constant refractive index (shortcut)
TMM(wl=…, pol=…, I0=…) Create a solver; wl = wavelength (m), pol = "p" or "s"
tmm.AddLayer(d, mat) Append layer (d in m, inf for semi-infinite)
tmm.Sweep(param, values) Solve for an array of values of any parameter
tmm.GetFields(zs) E, H field profiles along the layer normal
tmm.GetFields2D(zs, xs) E, H on a 2-D grid
tmm.GetEnhancement(layerNr) Field enhancement in a given layer
tmm.wave Access _Wave parameters for Gaussian beam calculations
tmm.WaveSweep(param, values) Parameter sweep for beam calculations
tmm.WaveGetFields2D(zs, xs) 2-D field map for beam excitation
SecondOrderNLTMM(…) Second-order nonlinear TMM (SHG, SFG, DFG)

For the full API, see the reference documentation.

Examples

Surface Plasmon Polaritons — ExampleTMM.py

Kretschmann configuration (prism | 50 nm Ag | air) at 532 nm. Demonstrates reflection sweeps, field enhancement, and 1D/2D field visualization of surface plasmon polaritons.

import math
import numpy as np
from NonlinearTMM import TMM, Material

# Materials
prism = Material.Static(1.5)
ag = Material.Static(0.054007 + 3.4290j)  # Silver @ 532nm
air = Material.Static(1.0)

# Set up TMM (Kretschmann configuration)
tmm = TMM(wl=532e-9, pol="p", I0=1.0)
tmm.AddLayer(math.inf, prism)
tmm.AddLayer(50e-9, ag)
tmm.AddLayer(math.inf, air)

# Sweep angle of incidence
betas = np.sin(np.radians(np.linspace(0, 80, 500))) * 1.5
result = tmm.Sweep("beta", betas, outEnh=True, layerNr=2)

SPP reflection, enhancement, and field profiles

Gaussian Beam Excitation — ExampleTMMForWaves.py

The same Kretschmann structure excited by a 10 mW Gaussian beam (waist 10 μm). Shows how finite beam width affects resonance depth and field enhancement.

Gaussian beam SPP excitation

Second-Harmonic Generation — ExampleSecondOrderNonlinearTmm.py

Second-harmonic generation (SHG) in a 1 mm nonlinear crystal with χ⁽²⁾ nonlinearity. Two s-polarized pump beams at 1000 nm generate a second-harmonic signal at 500 nm. The SecondOrderNLTMM class also supports sum-frequency generation (SFG) and difference-frequency generation (DFG).

SHG reflected and transmitted intensity vs beta

References

Loot, A., & Hizhnyakov, V. (2017). Extension of standard transfer-matrix method for three-wave mixing for plasmonic structures. Applied Physics A, 123(3), 152. doi:10.1007/s00339-016-0733-0

Loot, A., & Hizhnyakov, V. (2018). Modeling of enhanced spontaneous parametric down-conversion in plasmonic and dielectric structures with realistic waves. Journal of Optics, 20, 055502. doi:10.1088/2040-8986/aab6c0

Documentation

Full documentation is available at https://ardiloot.github.io/NonlinearTMM/.

Development

Setup

git clone --recurse-submodules https://github.com/ardiloot/NonlinearTMM.git
cd NonlinearTMM

# Install uv if not already installed:
# https://docs.astral.sh/uv/getting-started/installation/

# Create venv, build the C++ extension, and install all dependencies
uv sync

Running tests

uv run pytest -v

Code formatting and linting

Pre-commit hooks are configured to enforce formatting (ruff, clang-format) and catch common issues. To install the git hook locally:

uv run pre-commit install

To run all checks manually:

uv run pre-commit run --all-files

CI overview

Workflow Trigger What it does
Pytest Push to master / PRs Tests on {ubuntu, windows, macos} × Python {3.10–3.14}
Pre-commit Push to master / PRs Runs ruff, clang-format, ty, and other checks
Publish to PyPI Release published Builds wheels + sdist via cibuildwheel, uploads to PyPI
Publish docs Release published Builds Sphinx docs and deploys to GitHub Pages

Releasing

Versioning is handled automatically by setuptools-scm from git tags.

  1. Ensure CI is green on the master branch.
  2. Create a new release on GitHub:
    • Go to ReleasesDraft a new release
    • Create a new tag following PEP 440 (e.g. v1.2.0)
    • Target the master branch (or a specific commit on master)
    • Click Generate release notes for an auto-generated changelog
    • For pre-releases (e.g. v1.2.0rc1), check Set as a pre-release — these upload to TestPyPI instead of PyPI
  3. Publish the release — the workflow builds wheels for Linux (x86_64), Windows (x64, ARM64), and macOS (ARM64), and uploads to PyPI.

License

MIT

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

nonlineartmm-1.4.2.tar.gz (2.3 MB view details)

Uploaded Source

Built Distributions

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

nonlineartmm-1.4.2-cp314-cp314-win_arm64.whl (404.9 kB view details)

Uploaded CPython 3.14Windows ARM64

nonlineartmm-1.4.2-cp314-cp314-win_amd64.whl (437.4 kB view details)

Uploaded CPython 3.14Windows x86-64

nonlineartmm-1.4.2-cp314-cp314-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (4.6 MB view details)

Uploaded CPython 3.14manylinux: glibc 2.24+ x86-64manylinux: glibc 2.28+ x86-64

nonlineartmm-1.4.2-cp314-cp314-macosx_11_0_arm64.whl (459.8 kB view details)

Uploaded CPython 3.14macOS 11.0+ ARM64

nonlineartmm-1.4.2-cp313-cp313-win_arm64.whl (395.5 kB view details)

Uploaded CPython 3.13Windows ARM64

nonlineartmm-1.4.2-cp313-cp313-win_amd64.whl (427.3 kB view details)

Uploaded CPython 3.13Windows x86-64

nonlineartmm-1.4.2-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (4.6 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.24+ x86-64manylinux: glibc 2.28+ x86-64

nonlineartmm-1.4.2-cp313-cp313-macosx_11_0_arm64.whl (453.1 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

nonlineartmm-1.4.2-cp312-cp312-win_arm64.whl (395.6 kB view details)

Uploaded CPython 3.12Windows ARM64

nonlineartmm-1.4.2-cp312-cp312-win_amd64.whl (427.7 kB view details)

Uploaded CPython 3.12Windows x86-64

nonlineartmm-1.4.2-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (4.7 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.24+ x86-64manylinux: glibc 2.28+ x86-64

nonlineartmm-1.4.2-cp312-cp312-macosx_11_0_arm64.whl (453.5 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

nonlineartmm-1.4.2-cp311-cp311-win_amd64.whl (431.1 kB view details)

Uploaded CPython 3.11Windows x86-64

nonlineartmm-1.4.2-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (4.6 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.24+ x86-64manylinux: glibc 2.28+ x86-64

nonlineartmm-1.4.2-cp311-cp311-macosx_11_0_arm64.whl (451.8 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

nonlineartmm-1.4.2-cp310-cp310-win_amd64.whl (431.1 kB view details)

Uploaded CPython 3.10Windows x86-64

nonlineartmm-1.4.2-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (4.6 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.24+ x86-64manylinux: glibc 2.28+ x86-64

nonlineartmm-1.4.2-cp310-cp310-macosx_11_0_arm64.whl (451.7 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

File details

Details for the file nonlineartmm-1.4.2.tar.gz.

File metadata

  • Download URL: nonlineartmm-1.4.2.tar.gz
  • Upload date:
  • Size: 2.3 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for nonlineartmm-1.4.2.tar.gz
Algorithm Hash digest
SHA256 1b04802822d1bba25142c6a05fbb1497d2f02e38857f23838c2b619e02ec585d
MD5 ee25fc84dc4454f431bfb63d051ece31
BLAKE2b-256 adbb97a4ec5190097c0b0b860d4284481c8627ea4e7efa805267072808dc6c53

See more details on using hashes here.

File details

Details for the file nonlineartmm-1.4.2-cp314-cp314-win_arm64.whl.

File metadata

File hashes

Hashes for nonlineartmm-1.4.2-cp314-cp314-win_arm64.whl
Algorithm Hash digest
SHA256 0840d4c536a7146df28fcc5eb5bd16253d2c591fe89d77ce2695c3dc7e527b70
MD5 659eb795af4bacb51641f2f302bf5d51
BLAKE2b-256 6fd9130a5bd1eb1418830ad2f85e5addb0ab1796d0143ba15734b003822e4c1b

See more details on using hashes here.

File details

Details for the file nonlineartmm-1.4.2-cp314-cp314-win_amd64.whl.

File metadata

File hashes

Hashes for nonlineartmm-1.4.2-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 939f1990c4ef458e7f7ec6064f3a8d4863fc21f4df60f5baf2d479af391e3536
MD5 2300e8429862efa6cfc0ce7238523527
BLAKE2b-256 91b56e741c29764db361810bc83e28e1e5f338b620eaddcd90a4fcd7e8aae82b

See more details on using hashes here.

File details

Details for the file nonlineartmm-1.4.2-cp314-cp314-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for nonlineartmm-1.4.2-cp314-cp314-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 008d0ccc0b42204bea04a3c0936813eb4bb413dd7afd4904de77e138dab57290
MD5 c45880473c223349b406ca715bef3cbb
BLAKE2b-256 ed5b25da9e58c133895f7fd27fec5cb25808700b0f14c21bc206e5944efceedb

See more details on using hashes here.

File details

Details for the file nonlineartmm-1.4.2-cp314-cp314-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for nonlineartmm-1.4.2-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 0162b52a813f41b72078bc01bdb4320ed3cf7f07167908b36f37a6221e24900c
MD5 bf3f9adb452c105ad5c1007dd223d57d
BLAKE2b-256 8e46d77ce5062999f1beabea619add601aad9ea9bdc1e8dd82a3cb5e57c43c80

See more details on using hashes here.

File details

Details for the file nonlineartmm-1.4.2-cp313-cp313-win_arm64.whl.

File metadata

File hashes

Hashes for nonlineartmm-1.4.2-cp313-cp313-win_arm64.whl
Algorithm Hash digest
SHA256 f7136e4a2126c867d2ba1508fabe2f78cc1064db6760508f329ec4f993318790
MD5 3a77544faf13efb8cc8207be917efc75
BLAKE2b-256 9140a2906ca1115bcc04644e0874c0b5bb9fd7fbb2ab428c2f1f8083e6bc5cdf

See more details on using hashes here.

File details

Details for the file nonlineartmm-1.4.2-cp313-cp313-win_amd64.whl.

File metadata

File hashes

Hashes for nonlineartmm-1.4.2-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 f6b32b8e72e7a7e5288ff8a4023c9ca5a4521dfa833efef5f0e904855cd03783
MD5 5e18eef736f2f9ac242afbd8b7f247e4
BLAKE2b-256 968f4b9e4224485d25c28645ddcf593ca1680e71cacbd33e392b896cdf2b8aff

See more details on using hashes here.

File details

Details for the file nonlineartmm-1.4.2-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for nonlineartmm-1.4.2-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 66ae8be272058438711308ac3caa9a2a1dc91fcc10c95f400addccb25d0c0da3
MD5 5b9cbbc556cdafa1906345b5194d8821
BLAKE2b-256 8a1a56cdabe3c4eb204b89acdd7dffdc2df4e1bbf761619646d32a0d0b11d863

See more details on using hashes here.

File details

Details for the file nonlineartmm-1.4.2-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for nonlineartmm-1.4.2-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 291ecebb854d3435a58434e8f45ca551c5468d5cd4137b3a35622c664b6f59bd
MD5 1499464b3125861650aa2f060997bed5
BLAKE2b-256 2ea2059bc463bfae2d957223a1b710e3067eecf3c92492d879352097ef55ae35

See more details on using hashes here.

File details

Details for the file nonlineartmm-1.4.2-cp312-cp312-win_arm64.whl.

File metadata

File hashes

Hashes for nonlineartmm-1.4.2-cp312-cp312-win_arm64.whl
Algorithm Hash digest
SHA256 7b6ede0af649a365aee90d2617d6d4af403d46c2ee71d994c36fbdce82f59750
MD5 371369874cd2666d3676b6bdf7fc76b5
BLAKE2b-256 4e6352b1c49ad4ec16ee04661f45367832ef4abee151f860c85cad8188a14eb1

See more details on using hashes here.

File details

Details for the file nonlineartmm-1.4.2-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for nonlineartmm-1.4.2-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 68f64d8e8bf05154613134bc4b0da75bfe2f116dc3b546b3696b9dfa516a6122
MD5 c9f1cbb7d29a8209aebe50153927ebfb
BLAKE2b-256 77cfaafbeebaf574addd8df34315236dd47b073f07e353aa18e6b0b9c4abfc56

See more details on using hashes here.

File details

Details for the file nonlineartmm-1.4.2-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for nonlineartmm-1.4.2-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 c8e5f86b955da70762bed58e83271852c06a39e292a6d9e6b0d07d45209870c5
MD5 9a5927a30e2b1e12c25bfa4d22253cf0
BLAKE2b-256 1f3b725eebd44a9d7b739fc6f7a3f9f4104041761b9558ca0d181168c3fffaf9

See more details on using hashes here.

File details

Details for the file nonlineartmm-1.4.2-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for nonlineartmm-1.4.2-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 746f26c8b6b14a8d6884fc44928d43f4bd2eb45a1be0a9330d9490095a888b83
MD5 1e152f8bc431c643591fd6bf066a8ad7
BLAKE2b-256 815fcee097b91b4eaec4ce4d8433c4d2b393bae4579bbb19c6b704d883d3cbeb

See more details on using hashes here.

File details

Details for the file nonlineartmm-1.4.2-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for nonlineartmm-1.4.2-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 957881370a6f4f2a6024ad4a9559f191d4c329825836bcd6dde4b257d2824a0d
MD5 990983c9e6172091aefc77b664ffde2d
BLAKE2b-256 e883421f0b3f5b377d5c55a22edb99106b2ee4722fb430937958a1546d01ab4e

See more details on using hashes here.

File details

Details for the file nonlineartmm-1.4.2-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for nonlineartmm-1.4.2-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 b36d466915cd02a71c5a8f80003bc15307d2f325196921cb925ac31c921e5198
MD5 bfda936c80bf1c117effeaa708f90fad
BLAKE2b-256 de8fb2aad6804387eb1c626b9f94ec1b22acbe4462651d129b92d803b3140ec9

See more details on using hashes here.

File details

Details for the file nonlineartmm-1.4.2-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for nonlineartmm-1.4.2-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 70b66f1df2a904b807577d1feeb5ec6d05159f1b164ba9250ca10a5ad31c96b1
MD5 cca9cc02c20d1b6d26e7157ea638cd0b
BLAKE2b-256 3c3a8266bafd834982bc618e37e1fd2fa4d75a3ae6c3da709c2b63a4c121d94f

See more details on using hashes here.

File details

Details for the file nonlineartmm-1.4.2-cp310-cp310-win_amd64.whl.

File metadata

File hashes

Hashes for nonlineartmm-1.4.2-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 19f02b83613b9c25cb0a6fbcfdb8975f48417dcb96326c2556c587a4157e4480
MD5 3c5fc03b332354f968d83bc9eb5aceda
BLAKE2b-256 e6fb869a0386784011e8b93d7d7af38fc1d7cbb67c762493f32bc0d83d705fe8

See more details on using hashes here.

File details

Details for the file nonlineartmm-1.4.2-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for nonlineartmm-1.4.2-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 f39603ae704a8ad2a8d04f5bb31db92b02a07c002e25b4e1fa45060977fe94ed
MD5 5811d75a8cd21ab7db3bcc7e635ae20f
BLAKE2b-256 2535b14ce36d9b160f175818603ad9d314bbf992dd9c4f6861b8d2218ad56e00

See more details on using hashes here.

File details

Details for the file nonlineartmm-1.4.2-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for nonlineartmm-1.4.2-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 7aa1a975b164f2d52a885ba304c4cb02f5c163bd43d22c353ef95be5e1efe93b
MD5 f0695eeb57df8080a5aa2feb0819f6d3
BLAKE2b-256 94bdfff54ac3e0d6252853fd03fe880df7c5580aa8031c1ff3e95a94db0937e8

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