# opencr.solvers.diagnostics

*module*

What a solve reports: convergence, residuals, conservation and positivity.

Every solve returns a `SolverReport`. A solve that does not converge
raises `ConvergenceError` with the report attached; an underdetermined
system (a conserved element without a total) raises
`UnderdeterminedError`; a singular one
`SingularSystemError`.

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

## opencr.solvers.diagnostics.ConvergenceError

*class*

```python
class ConvergenceError(RuntimeError)
```

Bases: `RuntimeError`

A solve did not converge; ``report`` says how far it got.

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

### opencr.solvers.diagnostics.ConvergenceError.report

*attribute* · *instance attribute*

```python
report = report
```

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

## opencr.solvers.diagnostics.SingularSystemError

*class*

```python
class SingularSystemError(ValueError)
```

Bases: `ValueError`

Re-exported from: `opencr.kinetics.effective`

The quasi-static system has no unique solution; the message says which states.

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

## opencr.solvers.diagnostics.SolverReport

*class* · *dataclass*

```python
class SolverReport
```

The diagnostics of one solve.

**Attributes**

- `mode` (`str`) — ``"quasi-static"`` or ``"full"``.
- `method` (`str`) — The numerical method, e.g. ``"sparse LU"`` or ``"Newton"``.
- `converged` (`bool`) — Whether the tolerances were met.
- `iterations` (`int`) — Newton iterations or integrator steps.
- `residual` (`float`) — The largest ``|R_i|`` relative to the gross flow through state ``i`` (the sum of all its gains and losses), see `relative_residual`.
- `conservation` (`dict[str, float]`) — The relative error of each conserved total.
- `minimum` (`float`) — The smallest density divided by the largest.
- `clipped` (`list[str]`) — States whose small negative densities (round-off) were set to zero.
- `messages` (`list[str]`) — Anything worth knowing, in sentences.

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

### opencr.solvers.diagnostics.SolverReport.clipped

*attribute* · *class attribute* · *instance attribute*

```python
clipped: list[str] = field(default_factory=list)
```

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

### opencr.solvers.diagnostics.SolverReport.conservation

*attribute* · *class attribute* · *instance attribute*

```python
conservation: dict[str, float] = field(default_factory=dict)
```

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

### opencr.solvers.diagnostics.SolverReport.converged

*attribute* · *class attribute* · *instance attribute*

```python
converged: bool = True
```

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

### opencr.solvers.diagnostics.SolverReport.iterations

*attribute* · *class attribute* · *instance attribute*

```python
iterations: int = 0
```

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

### opencr.solvers.diagnostics.SolverReport.messages

*attribute* · *class attribute* · *instance attribute*

```python
messages: list[str] = field(default_factory=list)
```

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

### opencr.solvers.diagnostics.SolverReport.method

*attribute* · *instance attribute*

```python
method: str
```

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

### opencr.solvers.diagnostics.SolverReport.minimum

*attribute* · *class attribute* · *instance attribute*

```python
minimum: float = 0.0
```

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

### opencr.solvers.diagnostics.SolverReport.mode

*attribute* · *instance attribute*

```python
mode: str
```

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

### opencr.solvers.diagnostics.SolverReport.residual

*attribute* · *class attribute* · *instance attribute*

```python
residual: float = 0.0
```

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

### opencr.solvers.diagnostics.SolverReport.summary

*method*

```python
def summary() -> str
```

Return the report as a few lines of text.

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

## opencr.solvers.diagnostics.UnderdeterminedError

*class*

```python
class UnderdeterminedError(ValueError)
```

Bases: `ValueError`

The steady state is not unique: a conserved quantity has no given total.

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

## opencr.solvers.diagnostics.relative_residual

*function*

```python
def relative_residual(system: PopulationSystem, n: np.ndarray) -> float
```

Return ``max_i |R_i| / (sum_r |N_ir| w_r)``, the residual relative to the gross flow.

**Numerics**

*residual*

$$
\epsilon_R = \max_i \frac{|R_i|}{\sum_r |\nu_{ir}|\, w_r}
$$

*conservation*

$$
\epsilon_e = \frac{\big|\sum_i c_{ei} n_i - N_e\big|}{N_e}
$$

Assumptions:

- the residual over the gross flow through state $i$ (1 where there is none)
- the conservation error is reported (and enters the continuation's stopping test), but never raises on its own

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