Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ instead models the measurement physics directly:
through the nonlinear medium, with refractive indices of glasses, crystals and
gases from [optmat](https://optmat.readthedocs.io); for SHG, phase matching and
group-velocity mismatch in a Type I crystal such as BBO;
- **geometric delay smearing** of the crossed-beam (BOXCARS) geometry, with a
closed-form kernel set by the mask geometry, or fitted from the data;
- **geometric delay smearing** of the crossed-beam geometry (BOXCARS for PG and
SD, two beams for SHG), with a closed-form kernel set by the beam geometry, or
fitted from the data;
- **chromatic focusing and the collection aperture**, as a coherent chromatic
filter on the signal (the full focal model).

Expand Down
1 change: 1 addition & 0 deletions changelog.d/shg-smearing.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Two-beam delay smearing for SHG FROG: `croak.smearing.shg_two_beam_kernel` builds the delay blur of two Gaussian beams from a `croak.geometry.TwoBeamGeometry` with a `beam_diameter` (rms width w0 tan(alpha) / (sqrt 2 c) = lambda d / (sqrt 2 pi D c), after Taft et al. 1996), and the forward models, solvers and smearing fit accept it for SHG, with or without a crystal; `croak.smearing.check_kernel` refuses a kernel with a shape parameter for SHG. Sessions take `beam_diameter_mm` and build the kernel from the crystal's own beams at the pulse's wavelength, and the GUI's Beam geometry group has the diameter, the smearing switch and a width read-out. See [Two-beam SHG](docs/howto/geometric_smearing.md#two-beam-shg) (#24).
1 change: 1 addition & 0 deletions changelog.d/split-fit-shape-channel.changed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
A split smearing fit (`fit_smearing_split`) is now refused, with a clear error, for a kernel with no shape channel (`sigma_p = 0`): the two-beam SD kernel (`collinear_sd_kernel`, a session's `smear_layout="sd2"`) and the two-beam SHG kernel. Its shape-channel multiplier could not move the trace, so it stayed where it started and the covariance reported it as exact. Fit the one width with `fit_smearing` alone (`croak.smearing.check_split_fit`).
6 changes: 4 additions & 2 deletions croak/cmaes.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
from .focal import FocalMixture
from .forward_jax import make_param_trace_fn
from .result import RetrievalResult
from .smearing import SmearingKernel
from .smearing import SmearingKernel, check_split_fit
from .solver import (
Retriever,
assemble_result,
Expand Down Expand Up @@ -199,7 +199,8 @@ class CMAES(Retriever):
Forward-model options shared with the gradient solvers.
smearing : SmearingKernel or None, optional
Geometric time-smearing kernel of a non-collinear BOXCARS geometry
(default ``None`` = off). PG (TG) and SD only. See :mod:`croak.smearing`
(default ``None`` = off): a BOXCARS kernel for PG (TG) and SD, or the
two-beam kernel for SHG. See :mod:`croak.smearing`
and :doc:`/howto/geometric_smearing`.
reg_amp, reg_phase, reg_spectrum, spectrum_target
Smoothness / spectral-match regularisation weights, identical to the
Expand Down Expand Up @@ -361,6 +362,7 @@ def _solve(
scale_tau = (
float(np.median(np.abs(np.diff(delays)))) if delays.size > 1 else 1.0
)
check_split_fit(self.smearing, self.fit_smearing_split)
spec = ExtraParamSpec(
fit_thickness=self.fit_thickness,
thickness0=self.thickness,
Expand Down
3 changes: 2 additions & 1 deletion croak/covariance.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
from .crystals import CrystalSetup
from .forward_jax import make_param_trace_fn # enables jax_enable_x64 at import
from .result import RetrievalResult
from .smearing import SmearingKernel
from .smearing import SmearingKernel, check_split_fit
from .uncertainty import (
NoiseModel,
UncertaintyResult,
Expand Down Expand Up @@ -269,6 +269,7 @@ def _compute_core(
)
delays = np.asarray(result.delays, dtype=float)
scale_tau = float(np.median(np.abs(np.diff(delays)))) if delays.size > 1 else 1.0
check_split_fit(smearing, fit_smearing_split)
spec = ExtraParamSpec(
fit_thickness=fit_thickness,
thickness0=central_thickness,
Expand Down
17 changes: 9 additions & 8 deletions croak/forward.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
from . import crystals
from .crystals import CrystalSetup
from .interactions import Interaction, get_interaction
from .smearing import SmearingKernel, delay_frequency_grid
from .smearing import SmearingKernel, check_kernel, delay_frequency_grid

__all__ = ["ForwardModel", "quadrature_nodes_weights", "maketrace"]

Expand Down Expand Up @@ -198,12 +198,7 @@ def __init__(
# depth quadrature rather than coupling to it.
self.smearing = smearing
if smearing is not None:
if self.interaction.name not in ("pg", "sd"):
raise ValueError(
f"geometrical smearing is not supported for the "
f"{self.interaction.name.upper()} interaction; only PG (TG) and SD "
f"have the required three-arm form"
)
check_kernel(self.interaction, smearing)
self.p_nodes, self.p_weights = smearing.nodes_weights()
self.p_mu, self.p_sigma = smearing.conditional_delay(self.p_nodes)
self.delay_omega = delay_frequency_grid(self.delays, smearing.sigma_delta)
Expand Down Expand Up @@ -314,7 +309,13 @@ def _trace_smeared(
"""
per_node = np.empty((self.n, self.m, self.p_nodes.size), dtype=float)
for j, tau in enumerate(self.delays):
per_node[:, j, :] = np.abs(self.signal_smeared(ew, tau)) ** 2
if self.interaction.name == "shg":
# two beams: no shape parameter (check_kernel), one node, and the
# unsmeared signal with each arm's own propagator (a crystal's
# arms differ, CrystalSetup.arm_shift)
per_node[:, j, 0] = np.abs(self.signal_single(ew, tau)) ** 2
else:
per_node[:, j, :] = np.abs(self.signal_smeared(ew, tau)) ** 2
# Characteristic function of N(mu_k, sigma^2), shape (M, K):
# exp(-i Omega mu - sigma^2 Omega^2 / 2)
omega_delay = self.delay_omega[:, None]
Expand Down
32 changes: 24 additions & 8 deletions croak/forward_jax.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
from .maths import wlfreq # noqa: E402 - see the x64 note above
from .smearing import ( # noqa: E402 - see the x64 note above
SmearingKernel,
check_kernel,
delay_frequency_grid,
)

Expand Down Expand Up @@ -107,8 +108,8 @@
}

#: JAX twin of :meth:`croak.interactions.Interaction.smeared_signal`: combine the
#: ``p``-independent replica with the pair at ``varying_base -/+ p/2``. SHG is absent —
#: geometric smearing is only defined for the three-arm PG/SD forms.
#: ``p``-independent replica with the pair at ``varying_base -/+ p/2``. SHG is absent:
#: with two beams its smearing has no ``p`` and is a pure delay convolution.
_JAX_SMEARED_INTERACTIONS: dict[
str, Callable[[jnp.ndarray, jnp.ndarray, jnp.ndarray], jnp.ndarray]
] = {
Expand Down Expand Up @@ -659,11 +660,8 @@ def make_param_trace_fn(
also be a length-2 vector for split channel scaling.
"""
inter = get_interaction(interaction)
if smearing is not None and inter.name not in _JAX_SMEARED_INTERACTIONS:
raise ValueError(
f"geometrical smearing is not supported for the {inter.name.upper()} "
f"interaction; only PG (TG) and SD have the required three-arm form"
)
if smearing is not None:
check_kernel(inter, smearing)
# Same guard as the forward model: SHG needs a crystal (croak.crystals).
crystals.check_medium(
inter,
Expand Down Expand Up @@ -1130,13 +1128,31 @@ def trace(ew, thickness_v, tau0_v, smear_scale=1.0):
# (s_p, s_d) at fixed rho maps the node positions to s_p*p_k and the conditional
# delay moments to s_d*mu_k and s_d*sigma_cond — so a split scale is exact, not
# an approximation.
omega_delay = jnp.asarray(delay_frequency_grid(delays, smearing.sigma_delta))
if inter.name == "shg":
# Two beams: only the delay offset survives (check_kernel), so the smeared
# trace is the unsmeared one (each arm with its own propagator, which a
# crystal needs) convolved along delay with N(0, sigma_delta^2).
sigma_delta = smearing.sigma_delta

def shg_trace(ew, thickness_v, tau0_v, smear_scale=1.0):
_, scale_d = _smear_scale_pair(smear_scale)
psis = jax.vmap(lambda tau: signal(ew, tau, thickness_v, tau0_v))(delays_j)
kernel = jnp.exp(-0.5 * (scale_d * sigma_delta) ** 2 * omega_delay**2)
spec = jnp.fft.fft(jnp.abs(psis) ** 2, axis=0)
t = jnp.real(jnp.fft.ifft(spec * kernel[:, None], axis=0)).T
if normalize:
t = t / jnp.max(t)
return t

return shg_trace

smeared_map = _JAX_SMEARED_INTERACTIONS[inter.name]
p_nodes_np, p_weights_np = smearing.nodes_weights()
mu_np, sigma_delta_cond = smearing.conditional_delay(p_nodes_np)
p_nodes = jnp.asarray(p_nodes_np)
p_weights = jnp.asarray(p_weights_np)
mu_nodes = jnp.asarray(mu_np)
omega_delay = jnp.asarray(delay_frequency_grid(delays, smearing.sigma_delta))

def signal_smeared(ew, tau, thickness_v, tau0_v, scale_p):
ew_bin = jnp.fft.ifftshift(ew)
Expand Down
Loading
Loading