# opencr.backends.jax_backend

*module*

The optional JAX backend: jitted rates, vmapped scans, automatic-differentiation Jacobians.

Nothing in the core imports JAX; this module does, and it is only usable
with the ``jax`` extra installed. It reuses the reference's prepared rate
evaluators with ``jax.numpy`` as the array namespace and the structure of a
`PopulationSystem`, so that

* `JaxModel.rates` evaluates every reaction's Maxwellian rate
  coefficient at one or many electron temperatures in one jitted call;
* `JaxModel.rhs` and `JaxModel.jacobian` give ``R(n)`` and
  ``dR/dn`` of the population equations by forward-mode automatic
  differentiation, for the general (nonlinear, quasi-neutral) equations;
* `JaxModel.reduction` is the quasi-static reduction of a batch of
  ``(T_e, n_e)`` with ``jax.vmap``, the counterpart of
  `opencr.backends.batched`;
* `JaxModel.effective_rate` is differentiable: ``jax.grad`` of it
  gives the sensitivity of an effective coefficient to ``T_e`` and ``n_e``.

Two things differ from the reference: a `Function`
rate (Python code) is tabulated on its ``file_temperatures`` and interpolated
in log-log (the same as when it is written to a file), and a non-Maxwellian
EEDF is refused. The first call of each jitted function compiles it; the
benchmarks separate that from the warm time. Computations are in float64
(``jax_enable_x64``).

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/backends/jax_backend.py#L1-L1)

## opencr.backends.jax_backend.JaxModel

*class*

```python
class JaxModel
```

A model on JAX, at fixed heavy-particle temperatures and escape factors.

**Parameters**

- `model` (`Model`) — The model.
- `conditions` (`Conditions`) — The conditions whose heavy temperatures, escape factors and prescribed densities are baked in; ``T_e`` and ``n_e`` are the arguments of the methods. A non-Maxwellian EEDF is refused.

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/backends/jax_backend.py#L52-L254)

### opencr.backends.jax_backend.JaxModel.conditions

*attribute* · *instance attribute*

```python
conditions = conditions
```

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/backends/jax_backend.py#L100-L100)

### opencr.backends.jax_backend.JaxModel.model

*attribute* · *instance attribute*

```python
model = model
```

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/backends/jax_backend.py#L99-L99)

### opencr.backends.jax_backend.JaxModel.system

*attribute* · *instance attribute*

```python
system = system
```

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/backends/jax_backend.py#L141-L141)

### opencr.backends.jax_backend.JaxModel.effective_rate

*method*

```python
def effective_rate(source: str, target: str, electron_temperature: Any, electron_density: Any) -> Any
```

Return ``M_eff[target, source] / n_e`` (m^3/s) as a differentiable JAX scalar.

``jax.grad(jax_model.effective_rate, argnums=2)(src, tgt, T_e, n_e)`` is
the sensitivity to ``T_e``.

**Numerics**

*gradient*

$$
\frac{\partial}{\partial T_e}\, \frac{M^{\mathrm{eff}}_{\mathrm{target},\mathrm{source}}(T_e, n_e)}{n_e} \quad \text{by } \texttt{jax.grad}
$$

Assumptions:

- the reduction of `quasi_static` written in `jax.numpy`, vmapped over $(T_e, n_e)$; float64

Built from:

- `opencr.kinetics.effective.QuasiStatic.effective_rate`

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/backends/jax_backend.py#L230-L254)

### opencr.backends.jax_backend.JaxModel.jacobian

*method*

```python
def jacobian(n: Any, electron_temperature: float, electron_density: float | None = None) -> Any
```

Return ``dR/dn`` as a dense array by forward-mode automatic differentiation.

**Numerics**

*jacobian*

$$
J = \frac{\partial R}{\partial \mathbf n} \quad \text{by forward-mode automatic differentiation (}\texttt{jax.jacfwd}\text{) of } R(\mathbf n) = \sum_r \nu_{ir} w_r
$$

Assumptions:

- dense; the general equations (nonlinear, quasi-neutral) with Maxwellian rates
- a `Function` rate is tabulated on its `file_temperatures` and interpolated in log-log

Built from:

- `opencr.kinetics.system.PopulationSystem.jacobian`

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/backends/jax_backend.py#L170-L187)

### opencr.backends.jax_backend.JaxModel.rates

*method*

```python
def rates(electron_temperature: Any) -> Any
```

Return ``k`` of shape ``(..., n_reactions)`` for Maxwellian electrons at ``T_e`` (eV).

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/backends/jax_backend.py#L155-L158)

### opencr.backends.jax_backend.JaxModel.reduction

*method*

```python
def reduction(electron_temperatures: Any, electron_densities: Any) -> BatchedReduction
```

Return the quasi-static reduction of every ``(T_e, n_e)`` pair (vmapped).

The drivers are the prescribed states of the conditions, else the
model's. The result holds NumPy arrays.

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/backends/jax_backend.py#L199-L228)

### opencr.backends.jax_backend.JaxModel.rhs

*method*

```python
def rhs(n: Any, electron_temperature: float, electron_density: float | None = None) -> Any
```

Return ``R(n) = dn/dt`` of the evolving states (m^-3 s^-1).

``electron_density=None`` means quasi-neutrality, as in the conditions.

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/backends/jax_backend.py#L160-L168)
