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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ instead models the measurement physics directly:

- **dispersive propagation** of the input fields and the generated signal
through the nonlinear medium, with refractive indices of glasses, crystals and
gases from [optmat](https://optmat.readthedocs.io);
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;
- **chromatic focusing and the collection aperture**, as a coherent chromatic
Expand Down
1 change: 1 addition & 0 deletions changelog.d/shg-crystal-integration.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SHG retrievals through a Type I crystal everywhere a slab material is accepted: every solver, `retrieve_from_tracedata`, the bootstraps, the covariance and the thickness uncertainty take a `croak.crystals.CrystalSetup` as `material` (the thickness bootstrap gives each drawn thickness the depth nodes it needs). BBO is a built-in crystal (`croak.crystals.builtin_crystal`, optmat's Tamošauskas 2018 indices), so croak now needs `optmat[gas]>=0.10.0`, the first optmat release with BBO. Sessions describe the crystal (`crystal`, `cut_angle_deg`, `angle_mode`, `tilt_deg`, `phase_match_nm`, `polarization`, `delayed_beam`, `beam_separation_mm`), the GUI's Retrieve stage shows it as *Nonlinear crystal* and *Beam geometry* for SHG data, and the Synthetic stage can generate SHG traces through it. A crystal model refuses a marginal-corrected trace, which `TraceData.marginal_corrected` now records. A session's thickness uncertainty now raises a clear error when dispersive propagation is off, where it used to pass the unused slab material on. See [SHG FROG with a thick crystal](docs/howto/shg_crystal.md) (#24).
3 changes: 2 additions & 1 deletion croak/cmaes.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
build_parameterisation,
time_window_mask,
)
from .crystals import CrystalSetup
from .focal import FocalMixture
from .forward_jax import make_param_trace_fn
from .result import RetrievalResult
Expand Down Expand Up @@ -261,7 +262,7 @@ def __init__(
strategy: str = "cma",
popsize: int = 0,
std_init: float = 0.5,
material: str | None = None,
material: str | CrystalSetup | None = None,
thickness: float = 0.0,
npoints: int = 20,
omega0: float = 0.0,
Expand Down
10 changes: 6 additions & 4 deletions croak/copra.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import numpy as np
from numpy.typing import NDArray

from .crystals import CrystalSetup
from .forward import ForwardModel
from .grid import Grid
from .interactions import get_interaction
Expand Down Expand Up @@ -313,9 +314,10 @@ class COPRA(Retriever):
``maxiters``); see :func:`run_copra`.
abstol : float, optional
Absolute target FROG error (default ``0`` = off); see :func:`run_copra`.
material : str or None, optional
Slab material for dispersive propagation (e.g. ``"SiO2"``); ``None`` for
a thin medium.
material : str, CrystalSetup or None, optional
Slab material for dispersive propagation (e.g. ``"SiO2"``) for PG and SD,
or the nonlinear crystal for SHG (:class:`croak.crystals.CrystalSetup`);
``None`` for a thin medium.
thickness : float, optional
Slab thickness (m).
npoints : int, optional
Expand All @@ -342,7 +344,7 @@ def __init__(
alpha: float = 0.25,
reltol: float = 0.0,
abstol: float = 0.0,
material: str | None = None,
material: str | CrystalSetup | None = None,
thickness: float = 0.0,
npoints: int = 20,
omega0: float = 0.0,
Expand Down
3 changes: 2 additions & 1 deletion croak/copra_jax.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from numpy.typing import NDArray

from . import metrics_jax as mj
from .crystals import CrystalSetup
from .forward_jax import make_forward_adjoint_fns
from .grid import Grid
from .interactions import get_interaction
Expand Down Expand Up @@ -359,7 +360,7 @@ def __init__(
alpha: float = 0.25,
reltol: float = 0.0,
abstol: float = 0.0,
material: str | None = None,
material: str | CrystalSetup | None = None,
thickness: float = 0.0,
npoints: int = 20,
omega0: float = 0.0,
Expand Down
7 changes: 4 additions & 3 deletions croak/covariance.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@

from . import metrics_jax as mj
from ._jax_pulse import ExtraParamSpec, augment, ls_parameterisation
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
Expand Down Expand Up @@ -209,7 +210,7 @@ def _compute_core(
*,
noise: NoiseModel | float | None,
weights: NDArray[np.float64] | None,
material: str | None,
material: str | CrystalSetup | None,
thickness: float,
npoints: int,
omega0: float | None,
Expand Down Expand Up @@ -378,7 +379,7 @@ def parameter_covariance(
*,
noise: NoiseModel | float | None = None,
weights: NDArray[np.float64] | None = None,
material: str | None = None,
material: str | CrystalSetup | None = None,
thickness: float = 0.0,
npoints: int = 20,
omega0: float | None = None,
Expand Down Expand Up @@ -507,7 +508,7 @@ def covariance_uncertainty(
interval: str = "bias-corrected",
rng: np.random.Generator | None = None,
weights: NDArray[np.float64] | None = None,
material: str | None = None,
material: str | CrystalSetup | None = None,
thickness: float = 0.0,
npoints: int = 20,
omega0: float | None = None,
Expand Down
59 changes: 59 additions & 0 deletions croak/crystals.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@

from __future__ import annotations

import functools
import math
import warnings
from collections.abc import Callable
Expand All @@ -100,6 +101,8 @@
import numpy as np
import scipy.optimize
from numpy.typing import ArrayLike, NDArray
from optmat.exceptions import OptmatError
from optmat.solid.models.registry import default_model

from . import materials
from .constants import C
Expand All @@ -108,11 +111,13 @@
from .maths import derivative, wlfreq

__all__ = [
"CRYSTALS",
"TILT_ERROR_WARN",
"CrystalSetup",
"CrystalApproximationWarning",
"Propagation",
"UniaxialCrystal",
"builtin_crystal",
"check_medium",
"check_tilt",
"depth_points",
Expand Down Expand Up @@ -193,6 +198,60 @@ def transparent(self, omega: ArrayLike) -> NDArray[np.bool_]:
return inside


#: Names of the built-in crystals. Each uses optmat's recommended models of its
#: ordinary and extraordinary rays (``optmat.solid.models.registry.default_model``),
#: which state their sources and validity ranges.
CRYSTALS: tuple[str, ...] = ("BBO",)


def builtin_crystal(name: str) -> UniaxialCrystal:
"""Return a built-in crystal by name.

Parameters
----------
name : str
One of :data:`CRYSTALS`, case-insensitive.

Returns
-------
UniaxialCrystal
The crystal, with optmat's recommended ordinary and extraordinary
models and, as its transparency range, the range both models hold over.

Raises
------
ValueError
If ``name`` is not a built-in crystal.
"""
match = next((c for c in CRYSTALS if c.lower() == name.lower()), None)
if match is None:
raise ValueError(
f"unknown crystal {name!r}; the built-in crystals are {', '.join(CRYSTALS)}"
)
return _load_crystal(match)


@functools.cache
def _load_crystal(name: str) -> UniaxialCrystal:
"""Build a built-in crystal from optmat's models, once per canonical name."""
try:
ordinary = default_model(name, axis="o")
extraordinary = default_model(name, axis="e")
except OptmatError as err:
raise ValueError(
f"the installed optmat has no indices for {name}: {err} (croak needs an "
f"optmat with {name}'s models)"
) from err
lo = max(ordinary.wavelength_range[0], extraordinary.wavelength_range[0])
hi = min(ordinary.wavelength_range[1], extraordinary.wavelength_range[1])
return UniaxialCrystal(
name,
materials.optmat_index(ordinary),
materials.optmat_index(extraordinary),
(float(lo), float(hi)),
)


def extraordinary_index(
n_o: ArrayLike, n_e: ArrayLike, theta: ArrayLike
) -> NDArray[np.float64]:
Expand Down
Loading
Loading