Source code for pnps.geometry

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

"""The beam layouts: where the input holes are, and where the signal appears.

Two layouts share the propagation and the chi3 response; only the input differs.

- ``"tg"``, transient grating: the four-hole boxcar. Holes at ``(+-d, +-d)`` with
  ``d = (mask_spacing + mask_diam) / 2``; gate 1 at ``(+d, +d)``, gate 2 at
  ``(+d, -d)`` and the delayed test arm at ``(-d, +d)``, so the signal
  ``k_test - k_g1 + k_g2`` appears in the fourth corner, ``(-d, -d)``.
- ``"sd"``, self-diffraction: two holes on the x axis, ``s = mask_spacing + mask_diam``
  apart centre to centre. The probe E sits at ``(-s/2, 0)`` and the delayed gate G at
  ``(+s/2, 0)``; the signal ``2 k_E - k_G`` appears one slot further out, at ``(-3s/2,
  0)``. The layout is symmetric about the axis so that both beams are cut from the same
  part of the illumination: the signal is ``E^2 G*``, and an energy imbalance between
  the two would not cancel. The gate, which appears once and conjugated, carries the
  delay.

Positions are in the mask plane, in the stored transverse axes (a mask-plane position
``x`` is the stored wavevector ``kx = x omega / (c f_foc)``). The input former takes the
layout as three arms, gate 1, gate 2 and the test arm (:data:`pnps.form_input.ARMS`),
with a weight each: self-diffraction puts the probe in the gate-1 slot, the gate in the
test slot and gives gate 2 no weight.

The signal region is what ``i_omega_full`` sums, the un-windowed signal against which
the windows' collection efficiency is measured (:class:`SignalRegion`): for TG the
quadrant ``kx, ky < 0`` (the same at every frequency), for SD the half-plane beyond the
bisector between the signal and the probe, mask-plane ``x < -s``, which moves with
frequency, inside air's light cone.
"""

from typing import Literal, NamedTuple, get_args

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

from pnps.exceptions import ConfigError

__all__ = [
    "Geometry",
    "Layout",
    "SignalRegion",
    "check_geometry",
    "input_layout",
    "region_mask",
    "signal_centre",
    "signal_half_plane",
    "signal_region",
]

#: The beam layouts: ``"tg"`` (four-hole boxcar) and ``"sd"`` (self-diffraction).
Geometry = Literal["tg", "sd"]


[docs] class Layout(NamedTuple): """The input holes of a layout, in the former's arm order. Attributes ---------- holes : numpy.ndarray Hole centres ``(3, 2)`` in the mask plane, m, for gate 1, gate 2 and the test arm. weights : numpy.ndarray Arm weights ``(3,)``, each 1.0 or 0.0: an arm of weight zero is not part of the layout. """ holes: Float[np.ndarray, "n_arms 2"] weights: Float[np.ndarray, " n_arms"]
[docs] class SignalRegion(NamedTuple): """The momentum region of the signal, what ``i_omega_full`` sums. ``{kx < kx_max(omega), ky < ky_max(omega), kx^2 + ky^2 < kperp2_max(omega)}``. Attributes ---------- kx_max, ky_max : numpy.ndarray Exclusive upper bounds on the stored wavevectors per frequency bin, ``(n_omega,)``, rad/m; ``-inf`` excludes a bin, ``+inf`` leaves an axis free. kperp2_max : numpy.ndarray Exclusive upper bound on ``kx^2 + ky^2`` per bin, ``(n_omega,)``, rad^2/m^2; ``+inf`` for none. """ kx_max: Float[np.ndarray, " n_omega"] ky_max: Float[np.ndarray, " n_omega"] kperp2_max: Float[np.ndarray, " n_omega"]
[docs] def check_geometry(geometry: str) -> None: """Refuse an unknown layout. Parameters ---------- geometry : str The layout's name. Raises ------ ConfigError If ``geometry`` is not one of :data:`Geometry`. """ if geometry not in get_args(Geometry): raise ConfigError(f"geometry must be 'tg' or 'sd', got {geometry!r}")
[docs] def input_layout(geometry: str, mask_diam: float, mask_spacing: float) -> Layout: """Return the input holes of a layout. Parameters ---------- geometry : {"tg", "sd"} The layout. mask_diam, mask_spacing : float Hole diameter and edge-to-edge spacing, m. Returns ------- Layout The hole centres and arm weights. Raises ------ ConfigError For an unknown layout. Examples -------- >>> from pnps.geometry import input_layout >>> layout = input_layout("sd", 1e-3, 1e-3) >>> layout.holes.tolist(), layout.weights.tolist() ([[-0.001, 0.0], [-0.001, 0.0], [0.001, 0.0]], [1.0, 0.0, 1.0]) """ check_geometry(geometry) if geometry == "sd": half = (mask_spacing + mask_diam) / 2 # Probe, no second gate (its hole is the probe's, never formed), then the gate. holes = [[-half, 0.0], [-half, 0.0], [half, 0.0]] return Layout(np.array(holes), np.array([1.0, 0.0, 1.0])) d = mask_spacing / 2 + mask_diam / 2 holes = [[d, d], [d, -d], [-d, d]] return Layout(np.array(holes), np.ones(3))
[docs] def signal_centre( geometry: str, mask_diam: float, mask_spacing: float ) -> tuple[float, float]: """Return where the signal appears in the mask plane, where the windows go. Parameters ---------- geometry : {"tg", "sd"} The layout. mask_diam, mask_spacing : float Hole diameter and edge-to-edge spacing, m. Returns ------- tuple of float ``(x, y)`` in m: ``(-d, -d)`` for TG, ``(-3s/2, 0)`` for SD. Raises ------ ConfigError For an unknown layout. Examples -------- >>> from pnps.geometry import signal_centre >>> signal_centre("tg", 1e-3, 1e-3), signal_centre("sd", 1e-3, 1e-3) ((-0.001, -0.001), (-0.003, 0.0)) """ check_geometry(geometry) if geometry == "sd": return -1.5 * (mask_spacing + mask_diam), 0.0 d = mask_spacing / 2 + mask_diam / 2 return -d, -d
[docs] def signal_region( geometry: str, omega: Float[np.ndarray, " n_omega"], f_foc: float, mask_diam: float, mask_spacing: float, ) -> SignalRegion: """Return the momentum region of the signal, what ``i_omega_full`` sums. Parameters ---------- geometry : {"tg", "sd"} The layout. omega : numpy.ndarray Absolute angular frequency, FFT-ordered, rad/s. f_foc : float Focal length of the input mapping, m. mask_diam, mask_spacing : float Hole diameter and edge-to-edge spacing, m. Returns ------- SignalRegion TG: the quadrant ``kx, ky < 0`` at every bin, the second half of each FFT-ordered axis. SD: ``kx < -s omega / (c f_foc)`` at positive frequencies, any ``ky``, inside air's light cone ``kx^2 + ky^2 < (omega / c)^2``, and nothing at the other frequencies, where the mapping diverges. The SD region is a half-plane of the mask, which only light that can leave the slab into air reaches; it is solprop's half-plane aperture (:func:`signal_half_plane`), which reaches the same columns. Raises ------ ConfigError For an unknown layout. """ check_geometry(geometry) omega = np.asarray(omega, dtype=float) if geometry == "tg": return SignalRegion( np.zeros_like(omega), np.zeros_like(omega), np.full_like(omega, np.inf) ) spacing = mask_spacing + mask_diam kx_max = np.where(omega > 0.0, -spacing * omega / (c * f_foc), -np.inf) cone = np.where(omega > 0.0, (omega / c) ** 2, np.inf) return SignalRegion(kx_max, np.full_like(omega, np.inf), cone)
[docs] def signal_half_plane( geometry: str, f_foc: float, mask_diam: float, mask_spacing: float ) -> tuple[float, float] | None: """Return the SD signal region as a mask-plane edge, or None for TG. The SD region is the half of the mask beyond the gate-side edge ``x = s`` in solprop's physical axes (this package's stored ``kx < -s omega / (c f_foc)``, negated), at the input mapping's distance ``f_foc``: a ``half_plane_x_ge`` row of a solprop aperture set. The TG quadrant is a fixed momentum box instead. Parameters ---------- geometry : {"tg", "sd"} The layout. f_foc : float Focal length of the input mapping, m. mask_diam, mask_spacing : float Hole diameter and edge-to-edge spacing, m. Returns ------- tuple of float or None ``(edge, distance)`` in m, or None for TG. Raises ------ ConfigError For an unknown layout. Examples -------- >>> signal_half_plane("sd", 0.1, 1e-3, 1e-3) (0.002, 0.1) >>> signal_half_plane("tg", 0.1, 1e-3, 1e-3) is None True """ check_geometry(geometry) if geometry == "tg": return None return mask_spacing + mask_diam, f_foc
[docs] def region_mask( region: SignalRegion, ky: Float[np.ndarray, " n_y"], kx: Float[np.ndarray, " n_x"], ) -> Bool[np.ndarray, "n_y n_x n_omega"]: """Return a signal region as a mask of the state, for the NumPy references. Parameters ---------- region : SignalRegion The region. ky, kx : numpy.ndarray The stored momentum axes, rad/m. Returns ------- numpy.ndarray True inside the region, ``(n_y, n_x, n_omega)``. """ kx = np.asarray(kx)[None, :, None] ky = np.asarray(ky)[:, None, None] return ( (kx < region.kx_max) & (ky < region.ky_max) & (kx * kx + ky * ky < region.kperp2_max) )