Skip to content
Projects

Live demo

This page runs opencr in your browser: Pyodide is Python compiled to WebAssembly, so nothing is sent to a server. Choose a model and move the sliders to solve its steady state again. The electron density and the density of the neutral ground state (H, H2 or Ar) are inputs, as in a script; the other species are solved for.

Loading Python (about 10 s the first time)…

The figure appears once Python runs.
The populations of the states of the model in the steady state.
The figure appears once Python runs.
The synthetic spectrum of the hydrogen models, or the fractions of Ar(m) and Ar⁺ against the electron temperature for argon.
The Python of this demo
"""The Python side of the live demo: a steady-state solve of one of the models.
It runs in the browser (Pyodide); ``run`` takes the values of the controls and returns the
figures (populations, then the spectrum or a temperature scan) and the numbers.
"""
import numpy as np
import opencr
from opencr.solvers.diagnostics import ConvergenceError
ATOMIC = "Atomic hydrogen"
MOLECULAR = "Molecular hydrogen"
ARGON = "Argon"
WALLS = {"Ar+": 1e5, "Ar(m)": 1e4} # s^-1, argon has no steady state without losses
_models = {}
def _model(name, n_max):
key = (name, n_max if name == ATOMIC else 0)
if key not in _models:
if name == ATOMIC:
_models[key] = opencr.load_model("hydrogen/atomic/basic", n_max=n_max)
elif name == MOLECULAR:
_models[key] = opencr.load_model("hydrogen/molecular/basic")
else:
_models[key] = opencr.load_model("argon/metastable", wall_loss=WALLS)
return _models[key]
def _densities(name, neutral_density, electron_density):
if name == ATOMIC:
return {"H(n=1)": neutral_density, "H+": electron_density}
if name == MOLECULAR:
return {
"H2(X,v=0)": neutral_density,
"H(n=1)": 0.1 * neutral_density,
"H+": 0.5 * electron_density,
"H2+": 0.5 * electron_density,
}
return {"Ar": neutral_density}
def _solve(name, model, temperature, electron_density, neutral_density):
conditions = opencr.Conditions(
electron_temperature=temperature,
electron_density=electron_density,
densities=_densities(name, neutral_density, electron_density),
)
return model.solve_steady_state(conditions)
def _bars(states, populations, title):
return {
"data": [{"type": "bar", "x": states, "y": populations}],
"layout": {
"title": {"text": title},
"yaxis": {
"title": {"text": "population [m⁻³]"},
"type": "log",
"exponentformat": "power",
},
"showlegend": False,
},
}
def _spectrum(result):
wavelength = np.linspace(350.0, 700.0, 1400)
spectrum = result.emission_spectrum(wavelength=wavelength, fwhm=0.05)
return {
"data": [
{
"type": "scatter",
"mode": "lines",
"x": wavelength.tolist(),
"y": np.asarray(spectrum.values, dtype=float).tolist(),
}
],
"layout": {
"title": {"text": "Synthetic spectrum"},
"xaxis": {"title": {"text": "wavelength [nm]"}},
"yaxis": {"title": {"text": "power [W m⁻³ nm⁻¹]"}},
"showlegend": False,
},
}
def _scan(name, model, electron_density, neutral_density):
temperatures = np.geomspace(0.5, 30.0, 24)
fractions = {"Ar(m)": [], "Ar+": []}
for temperature in temperatures:
populations = _solve(
name, model, float(temperature), electron_density, neutral_density
).populations
for state in fractions:
fractions[state].append(float(populations[state]) / neutral_density)
return {
"data": [
{
"type": "scatter",
"mode": "lines",
"x": temperatures.tolist(),
"y": y,
"name": state,
}
for state, y in fractions.items()
],
"layout": {
"title": {"text": "Fractions of the neutral density against Tₑ"},
"xaxis": {"title": {"text": "electron temperature [eV]"}, "type": "log"},
"yaxis": {
"title": {"text": "n / n(Ar)"},
"type": "log",
"exponentformat": "power",
},
},
}
def run(
model=ATOMIC,
electron_temperature=3.0,
electron_density=1e18,
neutral_density=1e19,
n_max=15,
):
"""Solve one model at one set of conditions.
Args:
model: ``"Atomic hydrogen"``, ``"Molecular hydrogen"`` or ``"Argon"``.
electron_temperature: Electron temperature in eV.
electron_density: Electron density in m^-3.
neutral_density: Density of the ground state of the neutral (H, H2 or Ar) in m^-3.
n_max: Highest principal quantum number (atomic hydrogen only).
Returns:
The populations and the spectrum (or a temperature scan for argon) as figures, and
the effective coefficients, photon emissivity coefficients and emission as numbers.
"""
n_max = int(n_max)
temperature = float(electron_temperature)
ne = float(electron_density)
n0 = float(neutral_density)
opencr_model = _model(model, n_max)
try:
result = _solve(model, opencr_model, temperature, ne, n0)
except ConvergenceError:
return {
"numbers": {},
"text": "No steady state at these conditions: try a different density or Tₑ.",
}
populations = {name: float(value) for name, value in result.populations.items()}
if model == ATOMIC:
states = [f"H(n={n})" for n in range(1, n_max + 1)]
levels = list(range(1, n_max + 1))
figure = _bars(levels, [populations[s] for s in states], "Populations of H(n)")
figure["layout"]["xaxis"] = {
"title": {"text": "principal quantum number n"},
"dtick": 1 if n_max <= 15 else 5,
}
reduction = result.reduction
pec = result.photon_emissivity_coefficients().sel(line="A:3-2")
numbers = {
"Ionization S<sub>CR</sub> [m³ s⁻¹]": float(
reduction.effective_rate("H(n=1)", "H+")
),
"Recombination α<sub>CR</sub> [m³ s⁻¹]": float(
reduction.effective_rate("H+", "H(n=1)")
),
"H-alpha PEC from H(n=1) [m³ s⁻¹]": float(pec.sel(driver="H(n=1)")),
"H-alpha PEC from H⁺ [m³ s⁻¹]": float(pec.sel(driver="H+")),
"H-alpha emissivity [photons m⁻³ s⁻¹]": float(
result.emission().sel(line="A:3-2")["emissivity"]
),
}
return {"figure": [figure, _spectrum(result)], "numbers": numbers}
if model == MOLECULAR:
states = [s for s in populations if s.startswith("H2(")]
figure = _bars(states, [populations[s] for s in states], "Populations of H₂")
numbers = {
"H₂ ionization [m³ s⁻¹]": float(
result.reduction.effective_rate("H2(X,v=0)", "H2+")
),
"Vibrational ratio n(v=1) / n(v=0)": populations["H2(X,v=1)"]
/ populations["H2(X,v=0)"],
"Emitted power [W m⁻³]": float(result.emission()["power"].sum()),
}
return {"figure": [figure, _spectrum(result)], "numbers": numbers}
states = ["Ar", "Ar(m)", "Ar(r)", "Ar+"]
figure = _bars(states, [populations[s] for s in states], "Populations of argon")
numbers = {
"Metastable fraction n(Ar(m)) / n(Ar)": populations["Ar(m)"] / n0,
"Ionization fraction n(Ar⁺) / n(Ar)": populations["Ar+"] / n0,
"Electron density [m⁻³]": ne,
}
out = {"figure": [figure, _scan(model, opencr_model, ne, n0)], "numbers": numbers}
if populations["Ar+"] > n0:
out["text"] = (
"n(Ar⁺) exceeds n(Ar): the electron density is an input here, not solved for, "
"so these conditions are not self-consistent."
)
return out

The numbers below the figures are the effective coefficients and the H-alpha photon emissivity coefficients (PECs) for atomic hydrogen, the ionization of H2 and the emitted power for molecular hydrogen, and the metastable and ion fractions for argon. Argon needs wall losses to have a steady state (here 105 s-1 for Ar+ and 104 s-1 for Ar(m)); the same calls for atomic hydrogen in a script:

import opencr
model = opencr.load_model("hydrogen/atomic/basic", n_max=15)
conditions = opencr.Conditions(
electron_temperature=3.0,
electron_density=1e19,
densities={"H(n=1)": 1e19, "H+": 1e19},
)
result = model.solve_steady_state(conditions)
result.populations["H(n=3)"]
result.reduction.effective_rate("H(n=1)", "H+") # S_CR, m^3/s
result.emission().sel(line="A:3-2") # H-alpha

The page installs the opencr wheel built with the site, and its dependencies from PyPI, the first time it is opened; the browser keeps them afterwards.