# opencr

*module*

OpenCR: collisional-radiative models of low-temperature plasmas.

OpenCR builds collisional-radiative (CR) models from explicit states and
reactions with traceable data, and solves their population equations: steady
states, quasi-static excited states with effective rate coefficients, and time
evolution. The physics is defined independently of the numerical backend; the
reference implementation uses NumPy and SciPy, cunumpy and JAX are optional.
Cross sections and Maxwellian rates come from `plasmacoll`.

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

## opencr.Conditions

*class* · *dataclass*

```python
class Conditions
```

Re-exported from: `opencr.core.conditions`

Plasma conditions, SI with temperatures in eV.

The temperatures and densities take quantities (see the module
docstring): ``Conditions("300 K", 1e12 * u.cm**-3)``.

**Attributes**

- `electron_temperature` (`float`) — ``kT_e`` in eV. With a non-Maxwellian ``eedf`` it is the temperature of the rates that are not cross sections and of detailed balance; `from_eedf` sets it to ``2/3 <eps>``.
- `electron_density` (`float | None`) — ``n_e`` in m^-3, or ``None`` for quasi-neutrality, ``n_e = sum z_i n_i`` over every charged state (evolving and prescribed).
- `eedf` (`Any`) — The electron energy distribution (an `opencr.kinetics.eedf.EEDF`), or ``None`` for a Maxwellian at ``electron_temperature``.
- `densities` (`Mapping[str, float]`) — Densities in m^-3 of the prescribed states, by name: the drivers of a quasi-static solve (e.g. ``H(n=1)`` and ``H+``) and fixed backgrounds (e.g. ``H2`` in an atomic model).
- `totals` (`Mapping[str, float]`) — For a closed steady state, the total density of each element in m^-3 over the evolving states, e.g. ``{"H": 1e19}``.
- `ion_temperature` (`float | None`) — ``kT_i`` in eV; the gas temperature if ``None``.
- `gas_temperature` (`float`) — ``kT`` of the neutral gas in eV (0.026 eV is 300 K).
- `escape` (`Mapping[str, float]`) — Escape factors in [0, 1] by reaction name, overriding the reactions' own and anything ``trapping`` computes.
- `trapping` (`RadiationTrapping | None`) — Compute the escape factors of the lines self-consistently from the populations (`opencr.kinetics.radiation`); ``None`` for an optically thin plasma (apart from ``escape``).

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

### opencr.Conditions.densities

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

```python
densities: Mapping[str, float] = unit_field('m-3', default_factory=dict)
```

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

### opencr.Conditions.eedf

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

```python
eedf: Any = None
```

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

### opencr.Conditions.electron_density

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

```python
electron_density: float | None = unit_field('m-3', None)
```

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

### opencr.Conditions.electron_temperature

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

```python
electron_temperature: float = unit_field('eV')
```

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

### opencr.Conditions.escape

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

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

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

### opencr.Conditions.gas_temperature

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

```python
gas_temperature: float = unit_field('eV', 0.026)
```

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

### opencr.Conditions.ion_temperature

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

```python
ion_temperature: float | None = unit_field('eV', None)
```

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

### opencr.Conditions.totals

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

```python
totals: Mapping[str, float] = unit_field('m-3', default_factory=dict)
```

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

### opencr.Conditions.trapping

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

```python
trapping: RadiationTrapping | None = None
```

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

### opencr.Conditions.heavy_temperature

*property*

```python
heavy_temperature: dict[str, float]
```

The temperature of each kind of rate dependence (see `opencr.core.rates`).

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

### opencr.Conditions.quasi_neutral

*property*

```python
quasi_neutral: bool
```

Whether ``n_e`` follows from quasi-neutrality.

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

### opencr.Conditions.from_eedf

*method* · *classmethod*

```python
def from_eedf(eedf: Any, **kwargs: Any) -> Conditions
```

Return conditions with ``eedf`` and its effective temperature ``2/3 <eps>``.

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

### opencr.Conditions.replace

*method*

```python
def replace(**changes: Any) -> Conditions
```

Return a copy with ``changes`` applied.

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

### opencr.Conditions.to_dict

*method*

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

Return the conditions as plain data (the EEDF by its description).

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

## opencr.Model

*class*

```python
class Model
```

Re-exported from: `opencr.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.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.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.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.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.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.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.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.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.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.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.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.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.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)

## opencr.RadiationTrapping

*class* · *dataclass*

```python
class RadiationTrapping
```

Re-exported from: `opencr.core.conditions`

The escape-factor configuration of a solve (see `opencr.kinetics.radiation`).

The length takes a quantity (see the module docstring).

**Attributes**

- `length` (`float`) — The absorption length ``L`` in m (the distance to the wall).
- `model` (`str`) — One of `ESCAPE_MODELS`.
- `lines` (`tuple[str, ...] | None`) — The names of the ``spontaneous_emission`` reactions to trap, or ``None`` for all of them.
- `tolerance` (`float`) — The self-consistent iteration stops when no escape factor changes by more than this.
- `max_iterations` (`int`) — The limit of that iteration.

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

### opencr.RadiationTrapping.length

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

```python
length: float = unit_field('m')
```

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

### opencr.RadiationTrapping.lines

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

```python
lines: tuple[str, ...] | None = None
```

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

### opencr.RadiationTrapping.max_iterations

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

```python
max_iterations: int = 100
```

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

### opencr.RadiationTrapping.model

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

```python
model: str = 'mewe'
```

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

### opencr.RadiationTrapping.tolerance

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

```python
tolerance: float = 1e-06
```

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

### opencr.RadiationTrapping.to_dict

*method*

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

Return the configuration as plain data.

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

## opencr.Result

*class*

```python
class Result
```

Re-exported from: `opencr.analysis.results`

A steady state.

**Attributes**

- `model` — The model solved.
- `conditions` — The conditions.
- `rates` — The rate coefficient of every reaction (SI), in network order.
- `system` — The population equations.
- `evolving` — The steady densities of the evolving states (m^-3).
- `report` — The solver's report.

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

### opencr.Result.conditions

*attribute* · *instance attribute*

```python
conditions = conditions
```

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

### opencr.Result.evolving

*attribute* · *instance attribute*

```python
evolving = np.asarray(evolving, dtype=float)
```

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

### opencr.Result.model

*attribute* · *instance attribute*

```python
model = model
```

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

### opencr.Result.rates

*attribute* · *instance attribute*

```python
rates = rates
```

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

### opencr.Result.report

*attribute* · *instance attribute*

```python
report = report
```

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

### opencr.Result.system

*attribute* · *instance attribute*

```python
system = system
```

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

### opencr.Result.electron_density

*property*

```python
electron_density: float
```

``n_e`` in m^-3.

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

### opencr.Result.fluxes

*property* · *cached*

```python
fluxes: dict[str, float]
```

The rate of every reaction in m^-3 s^-1, by name.

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/analysis/results.py#L100-L103)

### opencr.Result.mode

*property*

```python
mode: str
```

The solve: ``"quasi-static"`` or ``"full"``.

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

### opencr.Result.populations

*property* · *cached*

```python
populations: dict[str, float]
```

The density of every state in m^-3, by name, in network order.

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

### opencr.Result.reduction

*property* · *cached*

```python
reduction: QuasiStatic
```

The quasi-static reduction at these conditions (population and effective coefficients).

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

### opencr.Result.dominant_processes

*method*

```python
def dominant_processes(state: str, number: int = 5) -> list[dict[str, Any]]
```

Return the largest gains and losses of ``state``.

Each entry has the reaction ``name``, its ``equation``, the ``rate`` of
change of the state (m^-3 s^-1; negative for a loss) and its ``fraction``
of the state's total gain or total loss. Sorted by ``|rate|``.

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/analysis/results.py#L105-L124)

### opencr.Result.emission

*method*

```python
def emission(air: bool = False) -> xr.Dataset
```

Return the lines: wavelength, emissivity and power (`line_table`).

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/analysis/results.py#L126-L128)

### opencr.Result.emission_spectrum

*method*

```python
def emission_spectrum(wavelength: np.ndarray | None = None, fwhm: float = 0.0, air: bool = False, quantity: str = 'power') -> xr.DataArray
```

Return a synthetic spectrum: the lines Doppler broadened at the gas temperature.

**Parameters**

- `wavelength` (`np.ndarray | None`) (default: `None`) — The grid in nm; by default 2000 points around the visible lines (350-700 nm).
- `fwhm` (`float`) (default: `0.0`) — An instrumental FWHM in nm, added in quadrature.
- `air` (`bool`) (default: `False`) — Wavelengths in air.
- `quantity` (`str`) (default: `'power'`) — ``"power"`` or ``"emissivity"``.

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/analysis/results.py#L175-L197)

### opencr.Result.export

*method*

```python
def export(path: str | Path) -> Path
```

Write `to_dataset` to a NetCDF file and return its path.

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/analysis/results.py#L253-L257)

### opencr.Result.photon_emissivity_coefficients

*method*

```python
def photon_emissivity_coefficients() -> xr.DataArray
```

Return ``PEC_d`` of every line per driver in m^3/s (quasi-static only).

``emissivity = n_e sum_d PEC_d n_d``; for atomic hydrogen the ``H(n=1)``
part is the excitation component and the ``H+`` part the recombination one.

**Numerics**

*pec*

$$
\mathrm{PEC}_d = \frac{g\, A_{ul}\, C_{ud}}{n_e} \quad [\mathrm{m^3/s}], \qquad \epsilon = n_e \sum_d \mathrm{PEC}_d\, n_d
$$

Assumptions:

- a quasi-static solve: $n_u = \sum_d C_{ud} n_d$; zero for a line whose upper state is a driver
- for atomic hydrogen the H(n=1) part is the excitation component, the H$^+$ part the recombination one

References:

- [Summers et al., Plasma Phys. Control. Fusion 48, 263 (2006)](https://doi.org/10.1088/0741-3335/48/2/007)

Built from:

- `opencr.kinetics.effective.quasi_static`
- `opencr.analysis.emission.line_table`

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

### opencr.Result.population

*method*

```python
def population(state: str) -> float
```

Return the density of ``state`` in m^-3.

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/analysis/results.py#L80-L82)

### opencr.Result.to_dataset

*method*

```python
def to_dataset() -> xr.Dataset
```

Return the populations, rates, fluxes and lines as one xarray dataset.

The conditions, the solver report and the model's manifest are in the
attributes, as JSON.

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/analysis/results.py#L199-L251)

## opencr.available_models

*function*

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

Re-exported from: `opencr.models`

Return the names of the predefined models with one sentence each.

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

## opencr.load_model

*function*

```python
def load_model(name: str, **options: Any) -> Model
```

Re-exported from: `opencr.models`

Return the predefined model ``name`` built with ``options``.

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

## opencr.scan

*function*

```python
def scan(model: Model, conditions: Conditions, mode: str = 'quasi-static', effective: bool = True, lines: bool = True, **axes: Sequence[float]) -> xr.Dataset
```

Re-exported from: `opencr.analysis.scan`

Solve ``model`` on a grid of conditions.

**Parameters**

- `model` (`Model`) — The model.
- `conditions` (`Conditions`) — The conditions every point starts from.
- `mode` (`str`) (default: `'quasi-static'`) — The steady-state mode.
- `effective` (`bool`) (default: `True`) — Add ``effective_rate`` (target driver, source driver) and, with ``lines``, ``pec`` (line, driver) for quasi-static solves.
- `lines` (`bool`) (default: `True`) — Add the line emissivities.
- `**axes` (`Sequence[float]`) (default: `{}`) — The condition fields to scan and their values, e.g. ``electron_temperature=np.geomspace(0.5, 50, 20)``. Use ``{"density:H(n=1)": [...]}`` or ``{"total:H": [...]}`` unpacked with ``**``. The values are in eV or m^-3, or quantities.

**Returns**

- (`xr.Dataset`) — A dataset over the axes (and ``state``, ``line``, ``driver``).

[View source](https://github.com/max-models/OpenCR/blob/devel/src/opencr/analysis/scan.py#L72-L164)

**Modules**

- `opencr.analysis`
- `opencr.backends`
- `opencr.cli`
- `opencr.constants`
- `opencr.core`
- `opencr.data`
- `opencr.inference`
- `opencr.kinetics`
- `opencr.ml`
- `opencr.model`
- `opencr.models`
- `opencr.solvers`
- `opencr.tables`
