# opencr.kinetics.rates

*module*

Rate coefficients of every reaction of a network for given conditions.

`RateEvaluator` prepares each reaction once and then returns the vector
of rate coefficients ``k_r`` (SI) for any `Conditions`,
or for many electron temperatures at once.

**Cross sections, Maxwellian.** plasmacoll's cross sections are linear between
their table points and constant beyond the last one, so
``k(T) = (8 / (pi mu))^1/2 (kT)^-3/2 int sigma(E) E exp(-E / kT) dE`` is a sum of
integrals of quadratics times an exponential, done in closed form per
segment: exact for the table, with no integration grid. Each evaluator
returns ``k exp(E_th / T)`` with the threshold ``E_th`` taken out, so that
detailed balance never forms ``exp(+E/T)`` of a tiny number.

**Cross sections, any EEDF.** With a non-Maxwellian
`EEDF`, the electron-impact rates are
``(2 e / m_e)^1/2 int eps sigma F0 deps`` (`plasmacoll.eedf_rate_coefficient`)
on the distribution's grid. Rates that are not cross sections use the
conditions' electron temperature (``2/3 <eps>`` from
`from_eedf`).

**Detailed balance.** The inverse of an electron-impact excitation uses the
Klein-Rosseland inverse cross section
``sigma_inv(eps) = (g_l / g_u) (eps + dE) / eps sigma(eps + dE)`` for any
EEDF, which for a Maxwellian is the Boltzmann relation
``k_inv = k (g_l / g_u) exp(dE / T)`` (used in closed form). An inverse with one
electron more on the forward product side (three-body recombination from
ionization) follows the Saha relation
``k_inv = k (g_0 / (g_e g_+)) lambda^3 exp(dE / T)`` with the electron's thermal
de Broglie volume ``lambda^3``, at the electron temperature (the effective one
for a non-Maxwellian EEDF). ``dE`` and the weights come from the states.

**Escape factors** multiply the spontaneous emission rates (radiation
trapping): the conditions' value for the reaction if given, else the
reaction's own.

**Arrays.** Every evaluator is written against cunumpy's ``xp`` (NumPy or
CuPy for the whole process, see `opencr.backends`; the optional JAX
backend passes ``jax.numpy`` in its place):
`RateEvaluator.coefficients_batched` evaluates all reactions at many
electron temperatures at once there, `RateEvaluator.coefficients` one
set of conditions, as floats on the host. A `Function`
rate runs on the host (its temperatures are copied there and the result
back); a non-Maxwellian EEDF is integrated on the host by plasmacoll and is
not batched.

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

## opencr.kinetics.rates.MaxwellianIntegral

*class* · *dataclass*

```python
class MaxwellianIntegral
```

The exact Maxwellian average of a piecewise-linear cross section.

**Attributes**

- `threshold` (`float`) — The threshold in the centre-of-mass frame (eV).
- `mass` (`float`) — The reduced mass (kg).
- `y` (`np.ndarray`) — The segment boundaries above the threshold (eV), ``n + 1`` values; the last segment continues to infinity with a constant cross section.
- `c0,` (`(c1, c2)`) — The coefficients of ``sigma(E) E = c0 + c1 y + c2 y^2`` per segment (``n + 1`` values, the last for the tail with ``c2 = 0``).

**Numerics**

*maxwellian*

$$
k(T) = \sqrt{\frac{8}{\pi\mu}}\,(kT)^{-3/2} \int_{E_{\mathrm{th}}}^\infty \sigma(E)\,E\,e^{-E/kT}\,dE
$$

*segment*

$$
y = E - E_{\mathrm{th}}, \qquad \sigma(E)\,E = c_0 + c_1 y + c_2 y^2, \qquad \sigma = a + s\,y:\ c_0 = a E_{\mathrm{th}},\ c_1 = a + s E_{\mathrm{th}},\ c_2 = s
$$

*antiderivative*

$$
\int g(y)\, e^{-y/T}\, dy = -T e^{-y/T}\big(g + T g' + 2 c_2 T^2\big)
$$

*scaled*

$$
\ln\!\big(k\, e^{E_{\mathrm{th}}/T}\big) = \ln\Big[\sqrt{\tfrac{8}{\pi\mu}}\,(kT)^{-3/2}\, e^2 \sum_{\mathrm{segments} + \mathrm{tail}} \big[\cdots\big]\Big]
$$

Order of accuracy: 2.

Assumptions:

- exact for the piecewise-linear table, plus the constant tail to infinity ($c_2 = 0$); no energy grid
- the order is that of the table spacing, for a smooth cross section sampled on the table
- the threshold is taken out ($e^{-E/T} = e^{-E_{\mathrm{th}}/T} e^{-y/T}$), so detailed balance never forms $e^{+E/T}$ of a tiny number; $e^2$ because the integral is in eV$^2$ m$^2$

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/kinetics/rates.py#L234-L313)

### opencr.kinetics.rates.MaxwellianIntegral.c0

*attribute* · *instance attribute*

```python
c0: np.ndarray
```

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

### opencr.kinetics.rates.MaxwellianIntegral.c1

*attribute* · *instance attribute*

```python
c1: np.ndarray
```

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/kinetics/rates.py#L267-L267)

### opencr.kinetics.rates.MaxwellianIntegral.c2

*attribute* · *instance attribute*

```python
c2: np.ndarray
```

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/kinetics/rates.py#L268-L268)

### opencr.kinetics.rates.MaxwellianIntegral.mass

*attribute* · *instance attribute*

```python
mass: float
```

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

### opencr.kinetics.rates.MaxwellianIntegral.threshold

*attribute* · *instance attribute*

```python
threshold: float
```

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/kinetics/rates.py#L263-L263)

### opencr.kinetics.rates.MaxwellianIntegral.y

*attribute* · *instance attribute*

```python
y: np.ndarray
```

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/kinetics/rates.py#L265-L265)

### opencr.kinetics.rates.MaxwellianIntegral.from_rate

*method* · *classmethod*

```python
def from_rate(rate: CrossSectionRate) -> MaxwellianIntegral
```

Prepare the integral of a cross-section rate model.

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/kinetics/rates.py#L270-L290)

### opencr.kinetics.rates.MaxwellianIntegral.log_scaled

*method*

```python
def log_scaled(temperature: Any, xp: ModuleType = xp) -> Any
```

Return ``log(k exp(E_th / T))`` with ``k`` in m^3/s at ``temperature`` (eV), on ``xp``.

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/kinetics/rates.py#L292-L313)

## opencr.kinetics.rates.RateEvaluator

*class*

```python
class RateEvaluator
```

Rate coefficients of a network's reactions.

**Parameters**

- `network` (`ReactionNetwork`) — The reaction network.

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/kinetics/rates.py#L442-L728)

### opencr.kinetics.rates.RateEvaluator.network

*attribute* · *instance attribute*

```python
network = network
```

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/kinetics/rates.py#L511-L511)

### opencr.kinetics.rates.RateEvaluator.names

*property*

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

The reaction names, in the order of the coefficient vector.

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/kinetics/rates.py#L565-L567)

### opencr.kinetics.rates.RateEvaluator.coefficients

*method*

```python
def coefficients(conditions: Conditions) -> np.ndarray
```

Return ``k_r`` in SI for every reaction at ``conditions``.

**Numerics**

*scaled*

$$
\ln k = \ln\!\big(k\, e^{E_{\mathrm{th}}/T}\big) - \frac{E_{\mathrm{th}}}{T}, \qquad k = 0 \ \text{if}\ \ln k < \ln(2.2\times10^{-308}), \qquad \ln k \le 700
$$

*inverse*

$$
\ln k_{\mathrm{inv}} = \ln\!\big(k_{\mathrm{fwd}}\, e^{E_{\mathrm{th}}/T}\big) + \ln\frac{g_{\mathrm{in}}}{g_{\mathrm{out}}} + \frac{\Delta E - E_{\mathrm{th}}}{T} + \tfrac32\, \Delta e\, \Big[\ln\frac{h^2}{2\pi m_e e} - \ln T\Big]
$$

*boltzmann*

$$
k_{ul} = k_{lu}\,\frac{g_l}{g_u}\, e^{\Delta E/T}
$$

*saha*

$$
k_{3b} = k_{\mathrm{ion}}\,\frac{g_0}{g_e\, g_+}\,\Lambda^3\, e^{I/T}, \qquad \Lambda^3 = \left(\frac{h^2}{2\pi m_e kT}\right)^{3/2}
$$

*escape*

$$
k_r \leftarrow g_r\, k_r \quad \text{for every spontaneous emission}
$$

Assumptions:

- `depends_on` picks the temperature: $T_e$ ($T_{\mathrm{eff}}$ for a non-Maxwellian EEDF), $T_i$ (the gas temperature when not given), $T_{\mathrm{gas}}$, or none
- with a non-Maxwellian EEDF only the electron cross sections (and their Klein-Rosseland inverses) are integrated over it (`plasmacoll.eedf_rate_coefficient`)
- the escape factor $g_r$ is `Conditions.escape` by name, else the reaction's own `escape`

References:

- [Klein and Rosseland, Z. Phys. 4, 46 (1921)](https://doi.org/10.1007/BF01328041)
- [Tiesinga et al., Rev. Mod. Phys. 93, 025010 (2021)](https://doi.org/10.1103/RevModPhys.93.025010) (CODATA 2018)

Built from:

- `opencr.kinetics.rates.MaxwellianIntegral`
- `opencr.kinetics.rates.inverse_cross_section`

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/kinetics/rates.py#L590-L628)

### opencr.kinetics.rates.RateEvaluator.coefficients_batched

*method*

```python
def coefficients_batched(conditions: Conditions, electron_temperatures: Any)
```

Return ``k`` of shape ``(len(electron_temperatures), n_reactions)`` on ``xp``.

Maxwellian electrons at each temperature; everything else (the heavy
temperatures, the escape factors) from ``conditions``. The input is
copied to ``xp`` and the result stays there (``xp.to_numpy`` brings it
back).

**Raises**

- `ValueError` — A non-Maxwellian EEDF (integrated on the host only), or
a temperature that is not positive.

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/kinetics/rates.py#L630-L655)

### opencr.kinetics.rates.RateEvaluator.coefficients_scan

*method*

```python
def coefficients_scan(conditions: Conditions, electron_temperatures: np.ndarray) -> np.ndarray
```

Return ``k`` of shape ``(len(electron_temperatures), n_reactions)`` on the host.

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/kinetics/rates.py#L724-L728)

### opencr.kinetics.rates.RateEvaluator.escape_factors

*method*

```python
def escape_factors(conditions: Conditions) -> np.ndarray
```

Return the factor of every reaction: the escape factor of a line, 1 otherwise.

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/kinetics/rates.py#L579-L585)

### opencr.kinetics.rates.RateEvaluator.evaluate

*method*

```python
def evaluate(prepared: Sequence[_Prepared], conditions: Conditions, electron_temperatures: Any, xp: ModuleType = xp) -> Any
```

Return ``k`` of shape ``(..., n_reactions)`` on the namespace ``xp``.

The core of `coefficients_batched`, for a given list of prepared
reactions (the JAX backend passes its own, with ``Function`` rates
tabulated) and any array namespace with the NumPy API; ``conditions``
gives the heavy temperatures and the escape factors. The Maxwellian
integrals of all cross sections are evaluated at once
(`_Stack`), the thresholds and detailed-balance factors as
vectors; only Arrhenius, tabulated and function rates go one by one.
An intermediate of ``(..., U, S)`` numbers (integrals times table
points) is formed, so a long batch is better evaluated in chunks.

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/kinetics/rates.py#L663-L722)

## opencr.kinetics.rates.inverse_cross_section

*function*

```python
def inverse_cross_section(cross_section: CrossSection, weight_ratio: float, energy_gap: float) -> CrossSection
```

Return the Klein-Rosseland inverse of an electron-impact cross section.

``sigma_inv(eps) = weight_ratio (eps + dE) / eps sigma(eps + dE)`` with
``dE = energy_gap``; the table starts just above zero energy.

**Numerics**

*reversibility*

$$
g_l\,\varepsilon_l\,\sigma_{lu}(\varepsilon_l) = g_u\,\varepsilon_u\,\sigma_{ul}(\varepsilon_u), \qquad \varepsilon_l = \varepsilon_u + \Delta E
$$

*klein rosseland*

$$
\sigma_{ul}(\varepsilon) = \frac{g_l}{g_u}\,\frac{\varepsilon + \Delta E}{\varepsilon}\,\sigma_{lu}(\varepsilon + \Delta E)
$$

Assumptions:

- microscopic reversibility, so it holds for any electron energy distribution
- tabulated on the forward table points shifted down by $\Delta E$, from $10^{-6}\max(\Delta E, 10^{-3})$ eV
- $\Delta E$ is the cross section's threshold (13.6-based for hydrogen); the Maxwellian path uses the state energy gap (13.598-based)

References:

- [Klein and Rosseland, Z. Phys. 4, 46 (1921)](https://doi.org/10.1007/BF01328041)

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/kinetics/rates.py#L316-L349)
