# opencr.backends.batched

*module*

The quasi-static reduction of many conditions at once, on cunumpy.

For a network that is linear in its states (one heavy reactant per reaction)
the rate matrix of one set of conditions is

$$
M = N\,\mathrm{diag}\big(k\, n_e^{a_e}\big)\, S,
$$

with ``N`` the net stoichiometry (states x reactions), ``S`` the selector of
each reaction's heavy reactant (reactions x states) and ``a_e`` the number
of electrons among the reactants. For a batch of ``B`` electron temperatures
and densities the matrices are a dense ``(B, n, n)`` array assembled in one
``einsum``, and the quasi-static reduction of `opencr.kinetics.effective`
is a batched dense solve, ``C = -M_QQ^-1 M_QP``, ``M_eff = M_PP + M_PQ C``.
Every point is independent, so the batch is the natural unit of work for a
GPU (``cunumpy.set_backend("cupy")``); the result comes back to the host and
is checked against the per-point sparse reference solves in the tests.

The drivers are the states with a density in the conditions, or the model's
drivers when the conditions give none; the driver densities do not enter the
coefficients (the system is linear).

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

## opencr.backends.batched.BatchedQuasiStatic

*class*

```python
class BatchedQuasiStatic
```

The batched quasi-static reduction of a model.

**Parameters**

- `model` (`Model`) — The model; its network must be linear in the states.

**Raises**

- `ValueError` — A reaction has two heavy reactants.

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

### opencr.backends.batched.BatchedQuasiStatic.model

*attribute* · *instance attribute*

```python
model = model
```

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

### opencr.backends.batched.BatchedQuasiStatic.assemble

*method*

```python
def assemble(te: np.ndarray, ne: np.ndarray, drivers: tuple[str, ...], excited: tuple[str, ...], coefficients: np.ndarray, effective: np.ndarray, rates: np.ndarray) -> BatchedReduction
```

Return the `BatchedReduction` of host arrays, with the PECs from ``coefficients``.

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

### opencr.backends.batched.BatchedQuasiStatic.drivers_of

*method*

```python
def drivers_of(conditions: Conditions) -> tuple[str, ...]
```

Return the drivers: the prescribed states of ``conditions``, else the model's.

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

### opencr.backends.batched.BatchedQuasiStatic.matrices

*method*

```python
def matrices(conditions: Conditions, electron_temperatures: Any, electron_densities: Any) -> tuple[Any, Any]
```

Return ``k`` of shape ``(B, n_reactions)`` and ``M`` of shape ``(B, n, n)`` on ``xp``.

**Numerics**

*matrix*

$$
M_b = N\,\mathrm{diag}\big(k_b\, n_{e,b}^{a_e}\big)\, S, \qquad b = 1, \dots, B
$$

Assumptions:

- $N$ the net stoichiometry (states $\times$ reactions), $S$ the selector of each reaction's one heavy reactant (reactions $\times$ states), $a_e$ the electrons among the reactants
- one dense `einsum` on cunumpy's `xp` (NumPy or CuPy for the whole process)
- Maxwellian electrons: the cross-section integrals of all reactions stacked and evaluated 32 temperatures per chunk; a non-Maxwellian EEDF is refused

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

### opencr.backends.batched.BatchedQuasiStatic.partition

*method*

```python
def partition(conditions: Conditions) -> tuple[tuple[str, ...], tuple[str, ...], list[int], list[int]]
```

Return the drivers, the excited states and their indices in the state order.

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

### opencr.backends.batched.BatchedQuasiStatic.run

*method*

```python
def run(conditions: Conditions, electron_temperatures: Any, electron_densities: Any) -> BatchedReduction
```

Return the reduction at every pair of ``electron_temperatures`` and ``electron_densities``.

The two are broadcast against each other and flattened; the result's
arrays follow that flat order.

**Numerics**

*reduction*

$$
C_b = -M_{QQ,b}^{-1} M_{QP,b}, \qquad M^{\mathrm{eff}}_b = M_{PP,b} + M_{PQ,b}\, C_b
$$

*pec*

$$
\mathrm{PEC}_{d,b} = \frac{k_{A,b}\, C_{ud,b}}{n_{e,b}}
$$

Assumptions:

- one heavy reactant per reaction and a given $n_e$; the driver densities do not enter (the system is linear)
- one batched dense `xp.linalg.solve` for all $B$ points; the reference is the per-point sparse `quasi_static`

Built from:

- `opencr.backends.batched.BatchedQuasiStatic.matrices`
- `opencr.kinetics.effective.quasi_static`

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

## opencr.backends.batched.BatchedReduction

*class* · *dataclass*

```python
class BatchedReduction
```

The quasi-static reduction at a batch of conditions (NumPy arrays).

**Attributes**

- `electron_temperatures` (`np.ndarray`) — ``T_e`` in eV, shape ``(B,)``.
- `electron_densities` (`np.ndarray`) — ``n_e`` in m^-3, shape ``(B,)``.
- `drivers` (`tuple[str, ...]`) — The names of the prescribed states ``P``.
- `excited` (`tuple[str, ...]`) — The names of the quasi-static states ``Q``.
- `coefficients` (`np.ndarray`) — ``C`` with shape ``(B, len(excited), len(drivers))``.
- `effective` (`np.ndarray`) — ``M_eff`` with shape ``(B, len(drivers), len(drivers))`` in s^-1.
- `rates` (`np.ndarray`) — ``k_r`` in SI with shape ``(B, n_reactions)``.
- `lines` (`tuple[str, ...]`) — The names of the spontaneous emissions.
- `pec` (`np.ndarray`) — The photon emissivity coefficients ``(B, len(lines), len(drivers))`` in m^3/s.

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

### opencr.backends.batched.BatchedReduction.coefficients

*attribute* · *instance attribute*

```python
coefficients: np.ndarray
```

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

### opencr.backends.batched.BatchedReduction.drivers

*attribute* · *instance attribute*

```python
drivers: tuple[str, ...]
```

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

### opencr.backends.batched.BatchedReduction.effective

*attribute* · *instance attribute*

```python
effective: np.ndarray
```

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

### opencr.backends.batched.BatchedReduction.electron_densities

*attribute* · *instance attribute*

```python
electron_densities: np.ndarray
```

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

### opencr.backends.batched.BatchedReduction.electron_temperatures

*attribute* · *instance attribute*

```python
electron_temperatures: np.ndarray
```

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

### opencr.backends.batched.BatchedReduction.excited

*attribute* · *instance attribute*

```python
excited: tuple[str, ...]
```

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

### opencr.backends.batched.BatchedReduction.lines

*attribute* · *instance attribute*

```python
lines: tuple[str, ...]
```

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

### opencr.backends.batched.BatchedReduction.pec

*attribute* · *instance attribute*

```python
pec: np.ndarray
```

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

### opencr.backends.batched.BatchedReduction.rates

*attribute* · *instance attribute*

```python
rates: np.ndarray
```

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

### opencr.backends.batched.BatchedReduction.effective_rate

*method*

```python
def effective_rate(source: str, target: str) -> np.ndarray
```

Return ``M_eff[target, source] / n_e`` in m^3/s for every point.

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

### opencr.backends.batched.BatchedReduction.population_coefficient

*method*

```python
def population_coefficient(state: str, driver: str) -> np.ndarray
```

Return ``n_state / (n_e n_driver)`` in m^3 for every point.

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

### opencr.backends.batched.BatchedReduction.to_dataset

*method*

```python
def to_dataset() -> xr.Dataset
```

Return the effective rates, population coefficients and PECs over ``point``.

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

## opencr.backends.batched.batched_scan

*function*

```python
def batched_scan(model: Model, conditions: Conditions, electron_temperature: Sequence[float], electron_density: Sequence[float]) -> xr.Dataset
```

Return the effective rates and PECs on a grid of ``T_e`` and ``n_e``.

The batched counterpart of `opencr.scan` for quasi-static solves of
Maxwellian plasmas: the same variables (``effective_rate`` over
``(electron_temperature, electron_density, target, source)``,
``pec`` over ``(..., line, driver)`` and ``population_coefficient`` over
``(..., state, driver)``), computed in one batch on cunumpy's ``xp``.
The temperatures are in eV and the densities in m^-3, or quantities.

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