# opencr.solvers.steady

*module*

Steady states of the population equations.

`solve_steady` finds ``R(n) = 0`` for the evolving states of a
`PopulationSystem`:

* **Linear systems** (one evolving reactant per reaction, ``n_e`` given) are
  solved directly by sparse LU with one step of iterative refinement.
* **Conserved elements.** When the evolving states exchange an element only
  among themselves (a closed system), ``R = 0`` is singular; one equation per
  conserved element is replaced by its total from ``totals``. A conserved
  element without a total raises `UnderdeterminedError`.
* **Quasi-neutral systems** that are linear for a given ``n_e`` are a 1D root
  in ``ln n_e`` (Brent's method), each evaluation a linear solve.
* **Other nonlinear systems** (two evolving reactants, or a given start) use
  a pseudo-transient continuation: implicit-Euler steps ``(F/dt - J) dx = R``
  whose ``dt`` grows as the residual falls (switched evolution relaxation),
  which follows the physical relaxation and keeps the totals of a closed
  system, and tends to Newton's method as ``dt`` grows. There is no separate
  Newton phase, and no line search beyond damping the steps that would turn
  a density negative.

Every solve checks the relative residual, the conservation of the totals and
the positivity of the densities, and returns a
`SolverReport`.

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

## opencr.solvers.steady.constraint_rows

*function*

```python
def constraint_rows(system: PopulationSystem, totals: Mapping[str, float] | None) -> tuple[np.ndarray, np.ndarray, np.ndarray, tuple[str, ...]]
```

Return the rows to replace, the constraint matrix and the totals.

For each conserved element the equation of the state with the most atoms
of it (the first such state not yet used) is replaced.

**Raises**

- `UnderdeterminedError` — A conserved element has no total.

**Numerics**

*constraint*

$$
R_{i_e}(\mathbf n) \ \to\ \sum_i c_{ei}\, n_i - N_e = 0, \qquad i_e = \arg\max_i c_{ei} \ \text{(first unused, stable order)}
$$

Assumptions:

- one row per conserved element replaced by its total $N_e$ from `Conditions.totals`; a missing total raises `UnderdeterminedError`
- for atomic hydrogen in `full` mode every state holds one H, so the H(n=1) row is replaced

Built from:

- `opencr.kinetics.system.PopulationSystem.conservation`

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/solvers/steady.py#L51-L87)

## opencr.solvers.steady.solve_steady

*function*

```python
def solve_steady(system: PopulationSystem, totals: Mapping[str, float] | None = None, initial: np.ndarray | None = None, mode: str = 'quasi-static', rtol: float = 1e-09, max_iterations: int = 200) -> tuple[np.ndarray, SolverReport]
```

Return the steady densities of the evolving states and the report.

**Parameters**

- `system` (`PopulationSystem`) — The population equations.
- `totals` (`Mapping[str, float] | None`) (default: `None`) — The total density of each conserved element (m^-3).
- `initial` (`np.ndarray | None`) (default: `None`) — A starting point for a nonlinear solve; by default the totals are put in the lowest-energy state of each element, a tenth of it ionized when ``n_e`` is quasi-neutral.
- `mode` (`str`) (default: `'quasi-static'`) — The name of the solve for the report.
- `rtol` (`float`) (default: `1e-09`) — The tolerance of the relative residual.
- `max_iterations` (`int`) (default: `200`) — Continuation steps.

**Raises**

- `UnderdeterminedError` — A conserved element has no total.
- `SingularSystemError` — The linear system is singular.
- `ConvergenceError` — A nonlinear solve did not converge.

**Numerics**

*linear*

$$
J(\mathbf 0)\,\mathbf n = -R(\mathbf 0) \quad \text{by sparse LU, one step of iterative refinement}
$$

*quasi neutral*

$$
\ln\sum_j z_j n_j(n_e) - \ln n_e = 0 \quad \text{by Brent's method in } \ln n_e, \ \text{each evaluation a linear solve}
$$

*continuation*

$$
\Big(\frac{F}{\Delta t} - J\Big)\,\delta\mathbf n = R(\mathbf n), \qquad F_i = \begin{cases} 1 & \text{free row} \\ 0 & \text{constraint row} \end{cases}, \qquad \Delta t_0 = \frac{1}{\max(\max_i |J_{ii}|, 1)}
$$

*damping*

$$
f = \min_{i:\, n_i + \delta n_i < 0} \frac{0.9\, n_i}{-\delta n_i} \ (\ge 10^{-6}), \qquad \mathbf n \leftarrow \max(\mathbf n + f\,\delta\mathbf n, 0)
$$

*relaxation*

$$
\Delta t \leftarrow \Delta t\ \mathrm{clip}\!\left(\frac{2\|R\|_{*,\mathrm{old}}}{\|R\|_{*,\mathrm{new}}},\ 0.5,\ 10^3\right), \qquad \|R\|_* = \frac{\|R\|_2}{\sum_i |n_i|}
$$

Assumptions:

- linear systems (one evolving reactant, given $n_e$): one LU solve; a zero diagonal (a state without losses) raises `SingularSystemError`
- quasi-neutral and linear for given $n_e$, without a start: the bracket $[10^{-30}, 10]\,s$ ($s$ the total charge scale) widened by $\times 100$ up to 20 times; a negative excess at the lower end raises
- otherwise pseudo-transient continuation with switched evolution relaxation (no separate Newton phase); a step that raises $\|R\|_*$ tenfold undamped is rejected with $\Delta t \leftarrow \Delta t/4$, a non-finite one with $\Delta t \leftarrow \Delta t/10$
- the default start puts each total in the lowest-energy neutral holder (10% in the lowest ion when quasi-neutral), plus $10^{-12}\max(N, 1)$ in every state
- negatives within $10^{-8}$ of the largest density are zeroed; only the relative residual decides failure, above $\max(\mathrm{rtol}, 10^{-6})$

References:

- [Virtanen et al., Nat. Methods 17, 261 (2020)](https://doi.org/10.1038/s41592-019-0686-2) (`splu`, `brentq`)

Built from:

- `opencr.solvers.steady.constraint_rows`
- `opencr.solvers.diagnostics.relative_residual`
- `opencr.kinetics.system.PopulationSystem.jacobian`

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/solvers/steady.py#L259-L335)
