# opencr.core.network

*module*

A reaction network: the states, the reactions between them and what is assumed.

`ReactionNetwork` holds plain Python objects and no arrays; the
numerical layer compiles it (`opencr.kinetics.system`). Building one
checks it (`ReactionNetwork.problems`): unique names, every state of
every reaction in the network, the forward reaction of every detailed
balance, the energy ordering of excitations and emissions. The
`manifest` lists the states, the reactions, their
sources and the assumptions in machine-readable form.

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

## opencr.core.network.NetworkError

*class*

```python
class NetworkError(ValueError)
```

Bases: `ValueError`

A reaction network is inconsistent; ``problems`` lists every problem found.

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/core/network.py#L28-L34)

### opencr.core.network.NetworkError.problems

*attribute* · *instance attribute*

```python
problems = list(problems)
```

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

## opencr.core.network.ReactionNetwork

*class*

```python
class ReactionNetwork
```

States and reactions, with the model's assumptions.

**Parameters**

- `states` (`Iterable[State]`) — The states (the electron is implicit and must not be listed).
- `reactions` (`Iterable[Reaction]`) — The reactions; every state they use must be in ``states``.
- `name` (`str`) (default: `''`) — The name of the network, e.g. ``"hydrogen/atomic/basic"``.
- `assumptions` (`Iterable[str]`) (default: `()`) — The model's assumptions and omissions, one sentence each.
- `check` (`bool`) (default: `True`) — Raise `NetworkError` if `problems` finds any.

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

### opencr.core.network.ReactionNetwork.assumptions

*attribute* · *instance attribute*

```python
assumptions: tuple[str, ...] = tuple(assumptions)
```

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

### opencr.core.network.ReactionNetwork.name

*attribute* · *instance attribute*

```python
name = name
```

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

### opencr.core.network.ReactionNetwork.reactions

*attribute* · *instance attribute*

```python
reactions: tuple[Reaction, ...] = tuple(reactions)
```

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

### opencr.core.network.ReactionNetwork.states

*attribute* · *instance attribute*

```python
states: tuple[State, ...] = tuple(states)
```

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

### opencr.core.network.ReactionNetwork.species

*property*

```python
species: tuple[Species, ...]
```

The species of the states, each once, in order of appearance.

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

### opencr.core.network.ReactionNetwork.state_names

*property*

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

The names of the states, in order.

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

### opencr.core.network.ReactionNetwork.conservation

*method*

```python
def conservation() -> _Conservation
```

Return the conserved quantities over the states: the elements and the charge.

The matrix has one row per quantity and one column per state: the
number of atoms of the element in the state, or its charge. Electrons
are not states, so charge is conserved only together with them.

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

### opencr.core.network.ReactionNetwork.manifest

*method*

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

Return the states, reactions, sources and assumptions as plain data.

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/core/network.py#L237-L268)

### opencr.core.network.ReactionNetwork.merge

*method*

```python
def merge(other: ReactionNetwork, name: str = '') -> ReactionNetwork
```

Return the union of two networks; shared states must be identical.

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/core/network.py#L220-L235)

### opencr.core.network.ReactionNetwork.problems

*method*

```python
def problems() -> list[str]
```

Return every inconsistency found, as sentences; empty if none.

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/core/network.py#L146-L180)

### opencr.core.network.ReactionNetwork.reaction

*method*

```python
def reaction(name: str) -> Reaction
```

Return the reaction called ``name``.

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/core/network.py#L102-L109)

### opencr.core.network.ReactionNetwork.reactions_of

*method*

```python
def reactions_of(state: str) -> tuple[Reaction, ...]
```

Return the reactions that change the population of ``state``.

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

### opencr.core.network.ReactionNetwork.state

*method*

```python
def state(name: str) -> State
```

Return the state called ``name``.

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/core/network.py#L87-L94)

### opencr.core.network.ReactionNetwork.state_index

*method*

```python
def state_index(name: str) -> int
```

Return the position of the state called ``name``.

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/core/network.py#L96-L100)

### opencr.core.network.ReactionNetwork.subset

*method*

```python
def subset(keep: Iterable[str], name: str | None = None) -> ReactionNetwork
```

Return the network of the states ``keep`` and the reactions among them only.

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/core/network.py#L197-L218)
