This code is a simulation and fitting tool that extracts nonlinear optical coefficients from Z-scan measurements. It utilizes a quasi-analytical treatment that is valid at large nonlinear phase shifts and handles nonlinear absorption and refraction simultaneously. The code is written in Python with a desktop GUI for interactive fitting.

This code is based on work performed during graduate school at the University of Central Florida and further developed at the Air Force Institute of Technology. The code can be downloaded from the Poesjelabs Gitea site under the GPL 3.0 open source license.

Z-scan measurement

The Z-scan experiment characterizes a material’s nonlinear response by translating a sample through the focus of a high-power ultrafast laser beam. As the sample approaches focus the spot size decreases, increasing the irradiance and any induced nonlinear effect. The open-aperture (OA) channel collects all transmitted light and is sensitive only to nonlinear absorption (NLA), producing a dip at focus when the nonlinear absorption is positive. The closed-aperture (CA) channel sits behind a partially closed iris and is sensitive to nonlinear refraction (NLR) induced phase changes.

The induced self-focusing changes the beam divergence on either side of focus, expanding the spot on the aperture before focus and shrinking it after, producing the characteristic peak to valley signature. The CA signal is affected by both NLA and NLR, hence both the OA and CA signals are utilized for measurements of NLR, while only the OA measurement is required to measure NLA. Typically a reference channel is used to measure laser power fluctuations for noise mitigation.

Open aperture and closed aperture signals from Z-scan experiment.

Modeling experimental data

The electric field of the pulse at the exit face of the sample is solved analytically under the slowly-varying-envelope, thin-sample, and small-phase-change approximations. For an instantaneous nonlinearity propagation through the sample is governed by

dIdz′=−α0I−α2I2,dϕdz′=k0n2I\frac{dI}{dz’} = -\alpha_0 I – \alpha_2 I^2, \qquad \frac{d\phi}{dz’} = k_0 n_2 I

which integrate to a closed-form expression for the field at the exit face for each radial point on the beam. That field is then Fresnel-propagated numerically to the aperture plane, and the transmitted power is integrated over space and time. The OA signal is integrated over the full extent of the beam. The CA signal is integrated only out to the aperture radius. Both are normalized by their value far from focus to give normalized transmission as a function of sample position ZZ.

Because the beam is cylindrically symmetric, the propagation integral is evaluated as a Hankel transform against a zeroth-order Bessel kernel.

Eap[z,ra,t]=k0idaexp⁡[iπra2λda]∫0rmax⁡Eexit[z,r,t]exp⁡[iπr2λda]J0[k0rrada]rdr,\begin{equation} E_{\mathrm{ap}}[z, r_a, t] = \frac{k_0}{i\, d_a}\, \exp\!\left[\frac{i\pi r_a^{2}}{\lambda\, d_a}\right] \int_{0}^{r_{\max}} E_{\mathrm{exit}}[z, r, t]\, \exp\!\left[\frac{i\pi r^{2}}{\lambda\, d_a}\right] J_0\left[\frac{k_0\, r\, r_a}{d_a}\right] r \,\mathrm{d}r, \end{equation}

Unlike Gaussian decomposition, this method handles both nonlinear absorption and refraction and can accommodate larger transmission and phase changes.

Additionally, the code accounts the following:

  • Higher-order nonlinearities. Refraction and absorption are each expanded to third order in irradiance, giving six adjustable coefficients rather than the usual two. The integrated exit-field expressions carry these terms through analytically.
  • Irradiance-dependent surface reflection. Fresnel reflection at the sample face is computed using the nonlinearly modified index, so the reflected fraction varies across the beam profile rather than being treated as a constant.
  • Real beam quality. An M² factor enters the spot-size evolution and the aperture geometry, so the model is not restricted to ideal Gaussian beams.
  • Non-uniform sampling in z. Simulated positions are distributed quadratically rather than linearly, concentrating points near focus where the signal has structure and spending few on the flat wings.
  • Independent axis offsets. The OA, CA, and CA/OA curves each carry their own z offset, absorbing the small registration differences between channels that show up in real measurements. Each fit curve is spline interpolated onto its own offset axis before comparison.

Performance enhancements

The propagation step was originally written as a direct four-dimensional array over position, radius, time, and aperture radius, then integrated over the radial axis. At default grid resolution that intermediate array exceeds 1.5 GB, and building it dominated both the memory footprint and the runtime of the entire simulation.

This is mathematically just a trapezoid-weighted contraction over one axis, so it was rewritten as a batched matrix multiply: fold the quadrature weights into the field array, transpose, and perform the contraction per z-position. The results are identical the original implementation to floating-point precision, roughly 250 times faster, using much less memory.

Parameter optimization

The improvement in speed enables automatic parameter refinement via a downhill simplex (Nelder-Mead) optimization to minimize RMSE between simulation and data. Two details were necessary to get it to converge at all.

The fit parameters span roughly thirty orders of magnitude in SI units, from n₂ near 10⁻¹⁸ to α₄ near 10⁻⁴³. Nelder-Mead is not scale-invariant, so the search runs in normalized coordinates where each parameter is divided by its own characteristic scale and every dimension starts near unity.

SciPy constructs the initial simplex by perturbing each coordinate relatively, which yields a zero-width step for any parameter starting at exactly zero. Since the higher-order coefficients usually start disabled, the simplex is constructed explicitly with a fixed absolute step per dimension, which explores every direction regardless of starting value.

Limitations

The model assumes instantaneous nonlinearities only. It does not model carrier redistribution, molecular reorientation, thermal lensing, or any other process with a response time comparable to or longer than the pulse. These nonlinear phenomena will be incorporated into future versions of the tool.

The thin-sample approximation requires the effective sample length to be small compared to the Rayleigh range, so diffraction inside the sample can be neglected. The tool calculates and displays the Rayleigh range, so the user can verify if this condition was violated.

The uncertainty is propagated from the fractional errors on pulse energy, spot size, and pulse duration, which are the dominant contributors to absolute accuracy. It does not currently include a confidence interval on the fitted coefficients themselves.

Workflow

The GUI wraps the engine so fitting can be done interactively. The measured data is imported by column index, with the reference and frequency channels optional. The simulation runs on a background thread so the window stays responsive, since a single run takes anywhere from a fraction of a second to several seconds depending on grid size.

Reference correction and background removal. The reference channel divides out laser power fluctuations (thin trace raw, heavy trace corrected). A line fitted through the outer 10% of points on each side removes the linear background and normalizes the transmission.

Reference correction is done by median-ratio scaling rather than naive division, which keeps the correction stable when the reference channel has different gain or sign than the signal channels. Background removal fits a line through a configurable fraction of the points at each end of the scan, where the sample is far enough from focus that the nonlinear response is negligible.

Once the has been referenced and normalized, the theoretical curves are computed and overlaid on the measurement. The parameters can be adjusted by hand until the shapes generally agree, then handed to the optimizer for refinement. Generally, fitting one parameter at a time starting at the lowest order nonlinear parameter that is present gives the most reliable results.

Converged fit against measured data. Open circles are normalized transmission, solid lines the simulation. This fit converged in 62 iterations to an RMSE of 0.0150.

Implementation

Every run writes a complete log of the input file, column mapping, processing flags, grid settings, and fitted coefficients in display units. The export command produces a CSV of data and fit values at each stage position, the settings file, and the figure in both JPG and SVG for easy copy and paste into publications. The settings of the last run persist between sessions through a JSON file.

The generated fit log. Recording the full configuration alongside the result makes a fit reproducible months later without guessing at which flags were set.

The current code is a refactor of an earlier monolithic script, itself a translation from MATLAB. The code implements Python, with NumPy and SciPy for the numerics, Matplotlib for figures, and Tkinter for the interface. The engine is fully separated from the GUI — it holds no plotting or interface state and builds figures as detached objects, so it can equally be driven from a script or a notebook. Configuration is a single dataclass that serializes to JSON, which is what makes runs reproducible and settings portable.

References

  • M. Sheik-Bahae, A. A. Said, T.-H. Wei, D. J. Hagan, and E. W. Van Stryland, “Sensitive measurement of optical nonlinearities using a single beam,” IEEE J. Quantum Electron. 26, 760–769 (1990).
  • M. R. Ferdinandus, M. Reichert, T. R. Ensley, H. Hu, D. A. Fishman, S. Webster, D. J. Hagan, and E. W. Van Stryland, “Dual-arm Z-scan technique to extract dilute solute nonlinearities from solution measurements,” Opt. Mater. Express 2, 1776–1790 (2012).