Source code for pnps.flux

# SPDX-FileCopyrightText: 2026 John C. Travers and contributors
#
# SPDX-License-Identifier: GPL-3.0-or-later

"""How this package measures energy: the axial flux of the medium.

A spectrometer records the energy per unit frequency that crosses its aperture, the
time-integrated Poynting flux. In the scalar forward model a plane-wave component
``(k_perp, omega)`` of the spectral state carries the axial flux

``S_z = (eps_0 c / 2) eta |E|^2``, with ``eta = c Re k_z / omega``,

the dimensionless axial admittance of the medium: ``n cos(theta)`` in a lossless
one, and zero on a column that does not propagate. This is solprop's measure (its
SEP 34): every energy, spectrum and absorbed power this package reports weights
``|E|^2`` by ``eta``, and so does every statistic solprop forms.

The measure fixes both ends of a run:

- **the launch.** ``Config.energy`` is the energy of the beam in air before the
  mask. Air's admittance is ``cos(theta_air)``. Each component then crosses into the
  slab keeping its flux (an ideal face: no Fresnel reflection), so its field is
  multiplied by :func:`launch_factor`, ``sqrt(eta_air / eta)``;
- **the observables.** The windowed, on-axis and full spectra, the energy and the
  absorbed powers all carry ``eta``.
"""

import numpy as np
from jaxtyping import Complex, Float
from scipy.constants import c

__all__ = [
    "air_admittance",
    "axial_admittance",
    "launch_factor",
]


[docs] def axial_admittance( k2: Complex[np.ndarray, "*_"], kperp2: Float[np.ndarray, "*_"], omega: Float[np.ndarray, "*_"] | float, ) -> Complex[np.ndarray, "*_"]: """Return the dimensionless axial admittance ``c k_z / omega`` of the medium. ``k_z`` is the principal root of ``k^2 - k_perp^2``. The admittance is zero where the column does not propagate (``Re(k^2) - k_perp^2 <= 0``) and at non-positive frequencies: such a bin is not a forward power port. Its real part ``eta`` weights ``|E|^2`` in every measured energy; the imaginary part, non-zero only in an absorbing medium, enters the on-axis spectrum's magnetic closure. Parameters ---------- k2 : numpy.ndarray The material wavenumber squared ``(n omega / c)^2``, rad^2/m^2, complex. kperp2 : numpy.ndarray ``k_x^2 + k_y^2``, rad^2/m^2. omega : numpy.ndarray or float Absolute angular frequency, rad/s. The three arrays broadcast together. Returns ------- numpy.ndarray The complex admittance, of the broadcast shape. Examples -------- >>> import numpy as np >>> from scipy.constants import c >>> omega = np.array([c, c, -c]) >>> axial_admittance(1.5**2 * (omega / c) ** 2 + 0j, np.array([0.0, 4.0, 0.0]), ... omega).real.round(12).tolist() [1.5, 0.0, 0.0] """ square = (np.real(k2) - kperp2) + 1j * np.imag(k2) omega = np.asarray(omega, dtype=float) live = (np.real(square) > 0.0) & (omega > 0.0) root = np.sqrt(np.where(live, square, 1.0)) return np.where(live, c * root / np.where(omega > 0.0, omega, 1.0), 0.0)
[docs] def air_admittance( kperp2: Float[np.ndarray, "*_"], omega: Float[np.ndarray, "*_"] | float ) -> Float[np.ndarray, "*_"]: """Return the axial admittance of air, ``cos(theta_air)``. Air is taken as vacuum. A component whose transverse wavevector exceeds ``omega / c`` does not propagate in air and gets zero, as do non-positive frequencies. Parameters ---------- kperp2 : numpy.ndarray ``k_x^2 + k_y^2``, rad^2/m^2. omega : numpy.ndarray or float Absolute angular frequency, rad/s. The arrays broadcast together. Returns ------- numpy.ndarray ``c Re k_z / omega`` in vacuum, of the broadcast shape. Examples -------- >>> import numpy as np >>> from scipy.constants import c >>> kperp2 = np.array([0.0, 0.75, 2.0]) * (2.0 / c) ** 2 >>> air_admittance(kperp2, 2.0).round(12).tolist() [1.0, 0.5, 0.0] """ omega = np.asarray(omega, dtype=float) return np.real( axial_admittance(np.asarray((omega / c) ** 2, dtype=complex), kperp2, omega) )
[docs] def launch_factor( k2: Complex[np.ndarray, "*_"], kperp2: Float[np.ndarray, "*_"], omega: Float[np.ndarray, "*_"], ) -> Float[np.ndarray, "*_"]: """Return the factor that carries an air field into the slab at constant flux. ``sqrt(eta_air / eta)``: a component of the air field, multiplied by it, carries the same axial flux in the medium as it did in air (an ideal entrance face). Zero where either medium does not carry the component. Parameters ---------- k2 : numpy.ndarray The slab's material wavenumber squared, rad^2/m^2, complex. kperp2 : numpy.ndarray ``k_x^2 + k_y^2``, rad^2/m^2. omega : numpy.ndarray Absolute angular frequency, rad/s. The arrays broadcast together. Returns ------- numpy.ndarray The real, non-negative factor, of the broadcast shape. Examples -------- >>> import numpy as np >>> from scipy.constants import c >>> factor = launch_factor(np.array([2.25 + 0j]), np.array([0.0]), np.array([c])) >>> factor.round(12).tolist() [0.816496580928] """ air = air_admittance(kperp2, omega) medium = np.real(axial_admittance(k2, kperp2, omega)) live = (air > 0.0) & (medium > 0.0) return np.where(live, np.sqrt(air / np.where(live, medium, 1.0)), 0.0)