# opencr.model

*module*

A CR model: a reaction network with its drivers, and the solves on it.

`Model` is what `opencr.load_model` returns and what a custom
network is wrapped in. It evaluates the rate coefficients (once per set of
conditions), builds the population equations and solves them (see the guide
"The CR model"):

* `Model.solve_steady_state` with ``mode="quasi-static"`` (the default):
  the states with a density in the conditions are prescribed, the rest are in
  steady state;
* ``mode="full"``: a closed steady state with the element totals of the
  conditions;
* `Model.effective_rates`: the quasi-static reduction to effective rate
  and population coefficients;
* `Model.solve_transient`: the populations against time from initial
  densities, with constant or time-dependent conditions.

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

## opencr.model.MODES

*attribute* · *module attribute*

```python
MODES = ('quasi-static', 'full')
```

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

## opencr.model.Model

*class*

```python
class Model
```

A reaction network with its default drivers.

**Parameters**

- `network` (`ReactionNetwork`) — The reaction network.
- `drivers` (`Sequence[str]`) (default: `()`) — The states a quasi-static solve needs densities for (for atomic hydrogen H(n=1) and H+); checked against the conditions.
- `description` (`str`) (default: `''`) — One sentence on what the model is.

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

### opencr.model.Model.description

*attribute* · *instance attribute*

```python
description = description
```

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

### opencr.model.Model.drivers

*attribute* · *instance attribute*

```python
drivers = tuple(drivers)
```

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

### opencr.model.Model.evaluator

*attribute* · *instance attribute*

```python
evaluator = RateEvaluator(network)
```

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

### opencr.model.Model.network

*attribute* · *instance attribute*

```python
network = network
```

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

### opencr.model.Model.name

*property*

```python
name: str
```

The network's name.

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/model.py#L68-L70)

### opencr.model.Model.state_names

*property*

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

The state names.

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/model.py#L73-L75)

### opencr.model.Model.effective_rates

*method*

```python
def effective_rates(conditions: Conditions) -> QuasiStatic
```

Return the quasi-static reduction with the prescribed states of ``conditions``.

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

### opencr.model.Model.manifest

*method*

```python
def manifest() -> dict[str, Any]
```

Return the network's manifest with the drivers and the description.

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/model.py#L77-L83)

### opencr.model.Model.rate_coefficients

*method*

```python
def rate_coefficients(conditions: Conditions) -> dict[str, float]
```

Return every reaction's rate coefficient (SI) by name.

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

### opencr.model.Model.scaled_rates

*method*

```python
def scaled_rates(conditions: Conditions, factors: Mapping[str, float] | None = None) -> np.ndarray
```

Return the rate coefficients with some multiplied by ``factors`` (by name).

A detailed-balance inverse takes the factor of its forward reaction
(unless it has its own), so the scaled network keeps detailed balance:
this is what sensitivities and uncertainty propagation perturb.

**Numerics**

*scaled*

$$
k_r \leftarrow f_r\, k_r, \qquad f_{\mathrm{inv}} = f_{\mathrm{fwd}} \ \text{for a detailed-balance inverse without a factor of its own}
$$

Assumptions:

- an inverse moves with its forward reaction, so the scaled network keeps detailed balance: what sensitivities and uncertainty propagation perturb

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/model.py#L101-L130)

### opencr.model.Model.solve_steady_state

*method*

```python
def solve_steady_state(conditions: Conditions, mode: str = 'quasi-static', initial: dict[str, float] | None = None, rtol: float = 1e-09, factors: Mapping[str, float] | None = None)
```

Return the steady state as a `Result`.

**Parameters**

- `conditions` (`Conditions`) — The conditions. In ``"quasi-static"`` mode the drivers need densities; in ``"full"`` mode the conserved elements need totals.
- `mode` (`str`) (default: `'quasi-static'`) — One of `MODES`.
- `initial` (`dict[str, float] | None`) (default: `None`) — Starting densities by state name for a nonlinear solve, in m^-3 (or quantities).
- `rtol` (`float`) (default: `1e-09`) — The tolerance of the relative residual.
- `factors` (`Mapping[str, float] | None`) (default: `None`) — Multiply the rate coefficients of these reactions (by name) by these factors (see `scaled_rates`).

**Numerics**

*fixed point*

$$
g^{(k)}_{\mathrm{new}} = g\big(\tau_0[n_l(g^{(k)})]\big), \qquad \text{given factors override computed ones line by line}
$$

*update*

$$
g^{(k+1)} = g^{(k)} + d\,\big(g^{(k)}_{\mathrm{new}} - g^{(k)}\big), \qquad d = 1 \to 0.5 \ \text{once the largest change grows}
$$

*stop*

$$
\max_{\mathrm{lines}} \big|g^{(k)}_{\mathrm{new}} - g^{(k)}\big| \le \texttt{tolerance}
$$

Assumptions:

- only with `Conditions.trapping`: the first solve is optically thin apart from the given factors (an absent $g$ counts as 1)
- the result returned was solved with the previous factors; at `max_iterations` a `ConvergenceError` is raised
- the damping never goes back to 1

Built from:

- `opencr.solvers.steady.solve_steady`
- `opencr.kinetics.radiation.escape_factors`

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/model.py#L182-L236)

### opencr.model.Model.solve_transient

*method*

```python
def solve_transient(conditions: Conditions | Callable[[float], Conditions], initial: dict[str, float], times: np.ndarray, method: str = 'BDF', rtol: float = 1e-06, atol: float | None = None)
```

Return the populations against time as a `TransientResult`.

The states with a density in the conditions stay prescribed (constant,
or following ``conditions(t)``); the others evolve from ``initial``.
``initial``, ``times`` and ``atol`` take astropy quantities or strings
such as ``"1 us"`` as well as numbers in their documented units.

**Parameters**

- `conditions` (`Conditions | Callable[[float], Conditions]`) — The conditions, or a function of the time in s returning them (same prescribed states at every time).
- `initial` (`dict[str, float]`) — The densities in m^-3 at ``times[0]`` by state name; an evolving state not listed starts empty.
- `times` (`np.ndarray`) — The output times in s, increasing.
- `method` (`str`) (default: `'BDF'`) — ``"BDF"`` (default), ``"Radau"`` or ``"LSODA"``.
- `rtol` (`float`) (default: `1e-06`) — The relative tolerance.
- `atol` (`float | None`) (default: `None`) — The absolute tolerance in m^-3 (default ``1e-12`` of the largest initial, prescribed or total density).

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/model.py#L238-L308)

### opencr.model.Model.system

*method*

```python
def system(conditions: Conditions, rates: np.ndarray | None = None) -> PopulationSystem
```

Return the population equations with the prescribed states of ``conditions``.

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/model.py#L90-L99)
