Metadata-Version: 2.5
Name: gasfir
Version: 2.0.0
Summary: GASFIR: General Approximator for Strong Field Ionization Rates
Author-email: Manoram Agarwal <manoram.agarwal@mpq.mpg.de>
License: MIT License
        
        Copyright (c) 2025 Manoram Agarwal
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
License-File: LICENSE
Keywords: autodiff,ionization,jax,laser,physics,strong-field
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Scientific/Engineering :: Physics
Requires-Python: >=3.11
Requires-Dist: jax<0.12,>=0.4.35
Requires-Dist: numpy<3,>=1.26.4
Provides-Extra: dev
Requires-Dist: black==26.5.1; extra == 'dev'
Requires-Dist: cma>=3.3; extra == 'dev'
Requires-Dist: corner>=2.2; extra == 'dev'
Requires-Dist: flake8==7.3.0; extra == 'dev'
Requires-Dist: h5py>=3.11; extra == 'dev'
Requires-Dist: isort==8.0.1; extra == 'dev'
Requires-Dist: matplotlib>=3.8; extra == 'dev'
Requires-Dist: mypy>=1.11; extra == 'dev'
Requires-Dist: pandas>=2.0; extra == 'dev'
Requires-Dist: pre-commit>=3.7; extra == 'dev'
Requires-Dist: psutil>=5.9; extra == 'dev'
Requires-Dist: pytest-cov>=5.0; extra == 'dev'
Requires-Dist: pytest>=7.0.0; extra == 'dev'
Requires-Dist: scipy>=1.15; extra == 'dev'
Provides-Extra: docs
Requires-Dist: myst-parser>=3.0; extra == 'docs'
Requires-Dist: sphinx-copybutton>=0.5; extra == 'docs'
Requires-Dist: sphinx-rtd-theme>=2.0; extra == 'docs'
Requires-Dist: sphinx>=8.0; extra == 'docs'
Provides-Extra: inference
Requires-Dist: cma>=3.3; extra == 'inference'
Requires-Dist: h5py>=3.11; extra == 'inference'
Requires-Dist: pandas>=2.0; extra == 'inference'
Requires-Dist: psutil>=5.9; extra == 'inference'
Provides-Extra: plots
Requires-Dist: corner>=2.2; extra == 'plots'
Requires-Dist: matplotlib>=3.8; extra == 'plots'
Provides-Extra: retrieval
Requires-Dist: cma>=3.3; extra == 'retrieval'
Requires-Dist: corner>=2.2; extra == 'retrieval'
Requires-Dist: h5py>=3.11; extra == 'retrieval'
Requires-Dist: matplotlib>=3.8; extra == 'retrieval'
Requires-Dist: pandas>=2.0; extra == 'retrieval'
Requires-Dist: psutil>=5.9; extra == 'retrieval'
Description-Content-Type: text/markdown

# GASFIR (v2.0)

**General Approximator for Strong Field Ionization Rates** — JAX-native differentiable
strong-field ionization rates in gases and solids (Agarwal, Scrinzi & Yakovlev,
*Phys. Rev. A* **113**, L021101, 2026).

**Documentation:** with `pip install -e ".[docs]"`, build via
`sphinx-build -b html -W docs docs/_build/html`, or browse [`docs/`](docs/)
(start at [`docs/user_guide/getting_started.rst`](docs/user_guide/getting_started.rst)).

## Overview

GASFIR computes sub-cycle ionization rates and total ionization probabilities for
arbitrary laser pulses by integrating an oscillatory kernel over a two-dimensional
grid of ionization moment `t` and excursion half-time `T`. Version 2.0 is built on
JAX so that the entire computation is end-to-end differentiable, vectorized across
pulses and pump–probe delays, and runs unchanged on CPUs and GPUs:

- **Derivatives with respect to pulse and medium parameters.**
  `∂P/∂(intensity, CEP, FWHM, delay)` and `∂P/∂(a0..a6, m_eff, alpha_pol)` are
  available via automatic differentiation — enabling gradient-based pulse
  characterization, TIPTOE, and NPS waveform retrieval.
- **Unified arbitrary-polarization tensor kernel.** Linear (`n_pol=1`), in-plane
  elliptical/circular (`n_pol=2`), and full 3-D (`n_pol=3`) fields share a single
  bilinear-form code path, with support for anisotropic inverse effective-mass and
  Stark polarizability tensors.
- **Precomputed pump–probe, TIPTOE, and NPS setups.** `PumpProbe`, `TIPTOE`, and
  `NPS` precompute the shared grid and field integrals across an entire delay scan
  with probe-rate Nyquist delay sampling (`return_delay_array`), evaluating
  differential ionization yields or orthogonal residual drift currents in
  vectorized, memory-budgeted batches.
- **About 1.5× faster than GASFIR v1.x on CPU** on the same grid (51-pulse
  scan: 1093 vs. 1652 ms per evaluation at `dT = 0.125`;
  [`benchmarks/RESULTS_v1_speedup.md`](benchmarks/RESULTS_v1_speedup.md)), and
  **444× faster on an NVIDIA A100 GPU** than a single CPU core on 100-pulse
  batches (904 pulses/s).  The default `dT` is twice as fine as v1's, to keep a
  small `a1` resolved, so a default evaluation costs more than v1's did.

Parameter fitting (`gasfir.retrieval`) runs a three-phase pipeline (CMA-ES
global search, Levenberg–Marquardt local polish, and Goodman–Weare ensemble
MCMC). The local polish defaults to finite differences (`lm_fd_step=1e-6`),
which need no more memory than the residual itself; forward-mode AD
(`"columnwise"` or `"batched"`) remains selectable via
`RetrievalConfig(lm_jacobian_mode=...)`.

[`CHANGELOG.md`](CHANGELOG.md) tracks the release history.

## Install

### From PyPI

```bash
# Core package (ionization kernel, pulses, pump-probe/TIPTOE/NPS, parameter sets)
pip install gasfir

# With parameter retrieval and diagnostic plots
pip install "gasfir[retrieval]"

# With CUDA 12 GPU support
pip install "gasfir[retrieval]" "jax[cuda12]"
```

### From the Git repository (source / development)

```bash
git clone https://gitlab.mpcdf.mpg.de/gaf/gasfir.git
cd gasfir
pip install -e ".[dev,docs]"
```

Or using Conda:

```bash
git clone https://gitlab.mpcdf.mpg.de/gaf/gasfir.git
cd gasfir
conda env create -f environment.yml && conda activate gasfir
pip install -e ".[dev,docs]"
```

Float64 precision is required (`jax_enable_x64` is enabled on import) because
the kernel integrates an oscillatory phase across many orders of magnitude.
**Do not install `jax-metal`** on Apple silicon: it lacks float64 support and
will silently lose precision. Use the default CPU backend on macOS, or
`jax[cuda12]` on CUDA GPUs.

## Quick Start

A probability in three lines (`probability_from_pulse` builds the grid and field
integrals automatically):

```python
from gasfir import create_pulse, get_parameters, probability_from_pulse

pulse  = create_pulse(800.0, 1e14, 0.0, 2.0)   # nm, W/cm², CEP, cycles FWHM
params = get_parameters("H_diabatic")
P      = probability_from_pulse(pulse, params) # 1.064600e-02
```

Gradients with respect to pulse parameters:

```python
import jax

jax.grad(lambda I: probability_from_pulse(create_pulse(800.0, I, 0.0, 2.0), params))(1e14)
# 5.169048e-16 per W/cm²
```

When the pulse is fixed and evaluated across many medium parameter sets (e.g.
during parameter fitting), precompute the grid and field integrals once with
`prepare`:

```python
from gasfir import Grid, ionization_probability
from gasfir.observables import prepare

t_min, t_max = pulse.time_interval()
grid   = Grid(t_min=t_min, t_max=t_max, dt=2.0, dT=0.25)
fields = prepare(pulse, grid)                            # once-only precomputation
P      = ionization_probability(fields, grid, params.to_kernel_params())
```

Pump–probe (TIPTOE and NPS):

```python
from gasfir import NPS, TIPTOE, create_pulse

pump = create_pulse(800.0, 1e14, 0.0, 2.0)
probe = create_pulse(800.0, 1e11, 0.0, 4.0)

tiptoe = TIPTOE(pump, probe)
delays = tiptoe.return_delay_array(oversample=8)
diff_yield = tiptoe.signal("H_diabatic", delays)

nps = NPS(pump, probe)
delays_sio2 = nps.return_delay_array(oversample=8)
j_probe = nps.signal("SiO2_Ulmic", delays_sio2, gate_only=True)
```

Fitting parameters with the retrieval pipeline (`pip install "gasfir[retrieval]"`):

```python
from gasfir import KernelParams, ionization_probability_batch
from gasfir.retrieval import Parameters, RetrievalConfig, retrieve

params = Parameters()
params.add("a0", 6.5, vary=True, min=1e-6, max=100.0)
params.add("a1", 3.5, vary=False)
params.add("a2", 3.4, vary=True, min=-5.0, max=20.0)
params.add("a3", 3.2, vary=True, min=-5.0, max=20.0)
params.add("a4", -0.5, vary=False)

result = retrieve(residual_fn, params, RetrievalConfig(verbose=True),
                  phases="global,local,mcmc")
```

From a source checkout with the benchmark datasets, a full end-to-end validation
and fitting run (writing corner plots, walker traces, HDF5 chains, and a
compilable LaTeX report to `benchmarks/validation_H/`) can also be launched via:

```bash
python benchmarks/scan_retrieval.py --medium H --calibrate \
    --phases global,local,mcmc --mcmc-steps 400
```

## GPU vs CPU Performance

One batch of 100 pulses (2-cycle, 400–1200 nm, 1e14 W/cm², Argon, float64,
4 M kernel elements each), timed as a fit evaluation:

| device | host cores | time (s) | pulses/s | vs 1 CPU core |
|---|--:|--:|--:|--:|
| Xeon Platinum 8360Y (Icelake) | 1 | 49.2 | 2.0 | 1× |
| Xeon Platinum 8360Y (Icelake) | 2 | 24.7 | 4.0 | 2.0× |
| **NVIDIA A100 40 GB** | 1 | **0.111** | **904** | **444×** |

- **One A100 matches ~440 CPU cores** for this kernel; CPU scaling is linear
  (2 cores = 2.0×) in the measured range.
- **The GPU needs one host core.** 1 and 18 host cores yield the same runtime,
  with minimal host CPU utilization while the GPU computes.
- **Full-rate float64 throughput is critical:** consumer GPUs with 1/32-rate
  float64 (e.g., Quadro P400, 17.7 s on the same batch) are slower than CPU.

Full benchmark details and caveats: [`benchmarks/RESULTS_raven.md`](benchmarks/RESULTS_raven.md).

## The layered design

An optimizer step should recompute as little as possible:

| Layer | What | Reruns when |
|---|---|---|
| 0 `Pulse` | pulse parameters (differentiable leaves) | — |
| 1 `PulseFields` | dense-grid `A`, `E`, `∫A`, `∫AᵢAⱼ`, `∫EᵢEⱼ` | pulse changes |
| 2 `ContractedFields` | mass / polarizability tensors folded in | `m_eff`, `α` change |
| 3 kernel | the `(n_T × n_t)` hot loop | every parameter update |

Fitting `a0…a6` pays only Layer 3. The integrals are stored as *tensors* rather
than pre-contracted scalars so that fitting an anisotropic mass does not
invalidate Layer 1.

A pulse is defined by a single vector potential function `A(t)`. The electric
field `E(t) = −dA/dt` is computed via `jax.jacfwd`, and the quadratic field
moments are integrated via mean-detrended cumulative Simpson quadrature.

## Development

```bash
black --check src tests benchmarks docs && isort --check-only src tests benchmarks docs
flake8 src tests benchmarks
mypy src/gasfir
pytest tests/ -m "not slow"
sphinx-build -b html -W docs docs/_build/html
```

`pre-commit install --hook-type pre-commit --hook-type pre-push` runs the
formatters on commit.

The test suite is property-based: published constants, analytic limits, round trips
and invariants. See [`docs/development/contributing.rst`](docs/development/contributing.rst)
for contributor guidelines, and [`docs/examples/performance.rst`](docs/examples/performance.rst)
for default optimizations and memory tuning.
