# opencr.solvers.transient

*module*

Time evolution of the populations with stiff integrators.

`solve_transient` integrates ``dn/dt = R(n, t)`` for the evolving states
of a `PopulationSystem` from initial densities
with SciPy's implicit methods (BDF by default, Radau or LSODA) and, for BDF
and Radau, the analytic sparse Jacobian of the system. The rates span 1e-3 to 1e9 s^-1 in a CR model, so the
problem is stiff; explicit methods are refused.

The conditions are prescribed: constant (one system), or given as a function
of time (``system_at(t)`` returns the system of that moment, built by
`solve_transient` from ``conditions(t)``). The
report records the integrator's statistics, the drift of every element total
the evolving states conserve (the particle balance; the charge balance holds
by construction with a given ``n_e`` or quasi-neutrality) and the smallest
density reached.

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

## opencr.solvers.transient.METHODS

*attribute* · *module attribute*

```python
METHODS = ('BDF', 'Radau', 'LSODA')
```

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

## opencr.solvers.transient.TransientReport

*class* · *dataclass*

```python
class TransientReport
```

The diagnostics of a time integration.

**Attributes**

- `method` (`str`) — The integrator.
- `success` (`bool`) — Whether the integration reached the final time.
- `evaluations` (`int`) — Right-hand-side evaluations.
- `jacobians` (`int`) — Jacobian evaluations.
- `factorisations` (`int`) — LU factorisations of the iteration matrix.
- `conservation` (`dict[str, float]`) — The largest relative drift of each conserved total over the output times.
- `minimum` (`float`) — The smallest density reached divided by the largest.
- `messages` (`list[str]`) — Anything worth knowing, in sentences.

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

### opencr.solvers.transient.TransientReport.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/transient.py#L60-L60)

### opencr.solvers.transient.TransientReport.evaluations

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

```python
evaluations: int = 0
```

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

### opencr.solvers.transient.TransientReport.factorisations

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

```python
factorisations: int = 0
```

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

### opencr.solvers.transient.TransientReport.jacobians

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

```python
jacobians: int = 0
```

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

### opencr.solvers.transient.TransientReport.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/transient.py#L62-L62)

### opencr.solvers.transient.TransientReport.method

*attribute* · *instance attribute*

```python
method: str
```

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

### opencr.solvers.transient.TransientReport.minimum

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

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

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

### opencr.solvers.transient.TransientReport.success

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

```python
success: bool = True
```

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

### opencr.solvers.transient.TransientReport.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/transient.py#L64-L79)

## opencr.solvers.transient.solve_transient

*function*

```python
def solve_transient(system: PopulationSystem, initial: np.ndarray, times: np.ndarray, method: str = 'BDF', rtol: float = 1e-06, atol: float | None = None, system_at: Callable[[float], PopulationSystem] | None = None) -> tuple[np.ndarray, TransientReport]
```

Return the densities of the evolving states at ``times`` and the report.

**Parameters**

- `system` (`PopulationSystem`) — The population equations (the structure, and the rates when ``system_at`` is not given).
- `initial` (`np.ndarray`) — The densities of the evolving states at ``times[0]`` (m^-3).
- `times` (`np.ndarray`) — The output times in s, increasing; the integration runs from the first to the last.
- `method` (`str`) (default: `'BDF'`) — One of `METHODS`.
- `rtol` (`float`) (default: `1e-06`) — The relative tolerance of the integrator.
- `atol` (`float | None`) (default: `None`) — The absolute tolerance in m^-3; by default ``1e-12`` times the largest density of the problem (initial, prescribed or a total).
- `system_at` (`Callable[[float], PopulationSystem] | None`) (default: `None`) — For time-dependent conditions, the system at a time ``t`` (same network and evolving states); called at every evaluation.

**Returns**

- (`tuple[np.ndarray, TransientReport]`) — The densities with shape ``(len(times), system.size)`` and the report.

**Raises**

- `ValueError` — An explicit method, or times that do not increase.
- `RuntimeError` — The integrator failed (the report is in the message).

**Numerics**

*ode*

$$
\frac{d\mathbf n}{dt} = R\big(\max(\mathbf n, 0), t\big), \qquad \mathbf n(t_0) = \mathbf n_0
$$

*tolerance*

$$
\mathrm{atol} = 10^{-12} \max\big(\mathbf n_0,\ \mathbf n_P,\ N_e\big) \ (1 \text{ if all zero})
$$

*drift*

$$
\max_t \frac{\big|\sum_i c_{ei} n_i(t) - \sum_i c_{ei} n_i(t_0)\big|}{\sum_i c_{ei} n_i(t_0)}
$$

Assumptions:

- stiff integrators only (`BDF`, the default, `Radau`, `LSODA`): CR rates span $10^{-3}$ to $10^9$ s$^{-1}$
- BDF and Radau get the analytic sparse Jacobian; LSODA forms its own by finite differences
- time-dependent conditions rebuild the rates and the system at each new time (a one-entry cache); the prescribed states stay the same
- negative output densities are set to zero, with a message

References:

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

Built from:

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

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