# opencr.kinetics.system

*module*

The population equations ``dn/dt = R(n)`` of a network, with their Jacobian.

`PopulationSystem` compiles a network for one set of rate coefficients
and conditions: which states evolve, the densities of the prescribed ones,
and ``n_e`` (given, or quasi-neutral). Reaction ``r`` proceeds at

$$
w_r = k_r\, n_e^{a_{re}} \prod_p n_p^{a_{rp}} \prod_i n_i^{a_{ri}},
$$

with ``p`` the prescribed and ``i`` the evolving reactants, and
``R = N w`` with the net stoichiometry ``N`` over the evolving states. The
Jacobian ``dR/dn = N dw/dn`` is assembled analytically in sparse form,
including the dependence of a quasi-neutral ``n_e`` on the charged states.

When every reaction has at most one evolving reactant and ``n_e`` is given, the
system is linear, ``R = M n + b`` (`PopulationSystem.linear_parts`).

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

## opencr.kinetics.system.MissingDensityError

*class*

```python
class MissingDensityError(ValueError)
```

Bases: `ValueError`

A prescribed state has no density in the conditions.

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

## opencr.kinetics.system.PopulationSystem

*class*

```python
class PopulationSystem
```

The population equations of the evolving states.

**Parameters**

- `network` (`ReactionNetwork`) — The reaction network.
- `rates` (`np.ndarray`) — ``k_r`` for every reaction (`RateEvaluator`).
- `conditions` (`Conditions`) — The conditions: ``n_e`` and the prescribed densities.
- `evolving` (`Sequence[str]`) — The names of the evolving states; the others are prescribed and need a density in ``conditions.densities``.

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

### opencr.kinetics.system.PopulationSystem.charges

*attribute* · *instance attribute*

```python
charges = np.array([network.state(n).charge for n in self.evolving], dtype=float)
```

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

### opencr.kinetics.system.PopulationSystem.conditions

*attribute* · *instance attribute*

```python
conditions = conditions
```

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

### opencr.kinetics.system.PopulationSystem.evolving

*attribute* · *instance attribute*

```python
evolving = tuple(evolving)
```

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

### opencr.kinetics.system.PopulationSystem.index

*attribute* · *instance attribute*

```python
index = index
```

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

### opencr.kinetics.system.PopulationSystem.max_order

*attribute* · *instance attribute*

```python
max_order = width
```

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

### opencr.kinetics.system.PopulationSystem.network

*attribute* · *instance attribute*

```python
network = network
```

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

### opencr.kinetics.system.PopulationSystem.prescribed

*attribute* · *instance attribute*

```python
prescribed = tuple((n for n in network.state_names if n not in self.evolving))
```

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

### opencr.kinetics.system.PopulationSystem.rates

*attribute* · *instance attribute*

```python
rates = rates
```

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

### opencr.kinetics.system.PopulationSystem.stoichiometry

*attribute* · *instance attribute*

```python
stoichiometry = sps.csr_matrix((values, (rows, cols)), shape=(len(self.evolving), n_r))
```

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

### opencr.kinetics.system.PopulationSystem.is_linear

*property*

```python
is_linear: bool
```

Whether ``R`` is linear in the evolving densities (given ``n_e``, one evolving reactant).

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

### opencr.kinetics.system.PopulationSystem.size

*property*

```python
size: int
```

The number of evolving states.

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

### opencr.kinetics.system.PopulationSystem.conservation

*method*

```python
def conservation() -> tuple[tuple[str, ...], np.ndarray]
```

Return the conserved elements and their matrix over the evolving states.

An element is conserved by the evolving states when no reaction moves it
to or from a prescribed state or out of the system (wall losses, pumping).

**Numerics**

*conserved*

$$
\sum_i c_{ei}\, \nu_{ir} = 0 \quad \text{for every reaction } r \qquad \Rightarrow \qquad \frac{d}{dt}\sum_i c_{ei} n_i = 0
$$

Assumptions:

- $c_{ei}$ the atoms of element $e$ in evolving state $i$; checked to $10^{-12}$
- elements only: charge is not a row, since the electron is not a state
- a reaction that moves an element to a prescribed state or out of the system (wall loss, pumping) removes it from the list

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

### opencr.kinetics.system.PopulationSystem.densities

*method*

```python
def densities(n: np.ndarray) -> Mapping[str, float]
```

Return every state's density by name: the evolving ``n`` and the prescribed ones.

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

### opencr.kinetics.system.PopulationSystem.electron_density

*method*

```python
def electron_density(n: np.ndarray) -> float
```

Return ``n_e`` for the evolving densities ``n``.

**Numerics**

*quasi neutral*

$$
n_e = \max\Big(0,\ \sum_j z_j n_j\Big)
$$

Assumptions:

- the sum runs over every charged state, prescribed and evolving; the electron is never a state
- with `Conditions.electron_density` given, $n_e$ is that value

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

### opencr.kinetics.system.PopulationSystem.jacobian

*method*

```python
def jacobian(n: np.ndarray) -> sps.csr_matrix
```

Return ``dR/dn`` as a sparse matrix.

**Numerics**

*jacobian*

$$
\frac{\partial R_i}{\partial n_c} = \sum_r \nu_{ir} \left[ k_r n_e^{a_{re}} \prod_p n_p^{a_{rp}} \frac{\partial}{\partial n_c}\prod_j n_j^{a_{rj}} + a_{re}\, k_r\, n_e^{a_{re}-1} z_c \prod_p n_p^{a_{rp}} \prod_j n_j^{a_{rj}} \right]
$$

Assumptions:

- the second term only for a quasi-neutral $n_e$
- a reactant listed twice is two slots: the derivative of each slot is the product of the others, summed
- assembled analytically as a sparse matrix

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

### opencr.kinetics.system.PopulationSystem.linear_parts

*method*

```python
def linear_parts() -> tuple[sps.csr_matrix, np.ndarray]
```

Return ``M`` and ``b`` of ``R = M n + b`` for a linear system.

**Numerics**

*linear*

$$
R(\mathbf n) = M\mathbf n + \mathbf b, \qquad M = J(\mathbf 0), \qquad \mathbf b = R(\mathbf 0)
$$

Assumptions:

- at most one evolving reactant per reaction and a given $n_e$

Built from:

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

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

### opencr.kinetics.system.PopulationSystem.reaction_rates

*method*

```python
def reaction_rates(n: np.ndarray) -> np.ndarray
```

Return ``w_r`` in m^-3 s^-1 for every reaction at the densities ``n``.

**Numerics**

*rate*

$$
w_r = k_r\, n_e^{a_{re}} \prod_p n_p^{a_{rp}} \prod_i n_i^{a_{ri}}
$$

Assumptions:

- $a_{ri}$ the multiplicity of state $i$ among the reactants (a state listed twice counts twice), $a_{re}$ that of the electron
- $p$ the prescribed states (`Conditions.densities`), $i$ the evolving ones; $k_r$ and the prescribed densities are folded into one factor per reaction
- mass action: $k_r$ in m$^{3(k-1)}$/s for $k$ reactants

Built from:

- `opencr.kinetics.system.PopulationSystem.electron_density`

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

### opencr.kinetics.system.PopulationSystem.rhs

*method*

```python
def rhs(n: np.ndarray) -> np.ndarray
```

Return ``R(n) = dn/dt`` for the evolving states.

**Numerics**

*balance*

$$
\frac{dn_i}{dt} = R_i(\mathbf n) = \sum_r \nu_{ir}\, w_r
$$

Assumptions:

- $\nu_{ir}$ the net stoichiometry (products minus reactants) over the evolving states
- zero-dimensional: no transport beyond explicit wall-loss and pumping reactions

References:

- [Summers et al., Plasma Phys. Control. Fusion 48, 263 (2006)](https://doi.org/10.1088/0741-3335/48/2/007)

Built from:

- `opencr.kinetics.system.PopulationSystem.reaction_rates`

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

### opencr.kinetics.system.PopulationSystem.source_by_prescribed

*method*

```python
def source_by_prescribed() -> dict[str, np.ndarray]
```

Return ``b`` split by prescribed state: the source with only that state present.

The remainder (sources that need two prescribed states at once) is under
``"(mixed)"`` when it is not zero.

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