Ionization balance
Without prescribed drivers, a closed steady state needs the total density of each element; with electron_density=None the electron density follows from quasi-neutrality, which makes the problem nonlinear.
import numpy as npimport plasma_plotsimport xarray as xr
import opencr
model = opencr.load_model("hydrogen/atomic/basic", n_max=16)balance = model.solve_steady_state( opencr.Conditions(2.0, None, totals={"H": 1e19}), mode="full")print(balance.report.summary())print( f"n_e = {balance.electron_density:.3e} m^-3, ionized fraction {balance.population('H+') / 1e19:.3f}")
plasma_plots.set_backend("plotly") # interactive figures, embedded in the docsplasma_plots.set_math("latex") # formulas in the figures typeset as LaTeXfull solve by quasi-neutral root of sparse LU solves: converged in 5 iterations relative residual 1.90e-13, min/max density 3.48e-08 conservation error 0.00e+00 (H)n_e = 9.869e+18 m^-3, ionized fraction 0.987'unicode'The ionized fraction against temperature. It is set by , so it also depends on the density:
temperatures = np.geomspace(0.5, 10.0, 20)curves = []for total in (1e17, 1e19, 1e21): data = opencr.scan( model, opencr.Conditions(1.0, None, totals={"H": total}), mode="full", lines=False, electron_temperature=temperatures, ) exponent = f"{np.log10(total):.0f}" curves.append( ( f"$n_H = 10^{{{exponent}}}$ m$^{{-3}}$", data["population"].sel(state="H+") / total, ) )with plasma_plots.figure(title="Ionized fraction") as fig: for label, curve in curves: curve.plasma.plot.lineout(ax=fig[0], title="", xlabel="$T_e$ [eV]", label=label) fig[0].set_xscale("log") fig[0].set_ylabel(r"$n(\mathrm{H}^+) / n_H$") fig[0].legend()Loading figure…
Saving a result
Section titled “Saving a result”export writes the populations, rates, fluxes and lines to NetCDF, with the conditions, the solver report and the model’s manifest as attributes.
import tempfilefrom pathlib import Path
path = balance.export(Path(tempfile.mkdtemp()) / "balance.nc")saved = xr.open_dataset(path, engine="scipy")print(saved["population"].sel(state=["H(n=1)", "H(n=2)", "H+"]).values)print(saved.attrs["model"], saved.attrs["solve"])[1.30760593e+17 4.59711727e+11 9.86922048e+18]hydrogen/atomic/basic full