Skip to content
Projects

Batched scans

Download notebookView on GitHubRun when the site was built.

opencr.scan solves one point after another on the reference path. For quasi-static solves of Maxwellian plasmas, batched_scan evaluates the rate coefficients and the quasi-static reduction of every (Te,ne)(T_e, n_e) pair at once as dense arrays on cunumpy’s xp: NumPy, or CuPy on a GPU after cunumpy.set_backend("cupy"). See Backends for what is batched and where the execution boundaries are.

import time
import numpy as np
import plasma_plots
import opencr
from opencr.backends import device
from opencr.backends.batched import batched_scan
plasma_plots.set_backend("plotly") # interactive figures, embedded in the docs
plasma_plots.set_math("latex") # formulas in the figures typeset as LaTeX
model = opencr.load_model("hydrogen/atomic/basic", n_max=16)
temperatures = np.geomspace(0.5, 100.0, 40)
densities = np.geomspace(1e16, 1e22, 7)
start = time.perf_counter()
data = batched_scan(model, opencr.Conditions(1.0, 1e18), temperatures, densities)
print(
f"{temperatures.size * densities.size} points in {time.perf_counter() - start:.3f} s on the {device()}"
)
data
280 points in 0.689 s on the cpu
<xarray.Dataset> Size: 618kB
Dimensions:                 (electron_temperature: 40, electron_density: 7,
                             target: 2, source: 2, state: 15, driver: 2,
                             line: 120)
Coordinates:
  * electron_temperature    (electron_temperature) float64 320B 0.5 ... 100.0
  * electron_density        (electron_density) float64 56B 1e+16 1e+17 ... 1e+22
  * target                  (target) <U6 48B 'H(n=1)' 'H+'
  * source                  (source) <U6 48B 'H(n=1)' 'H+'
  * state                   (state) <U7 420B 'H(n=2)' 'H(n=3)' ... 'H(n=16)'
  * driver                  (driver) <U6 48B 'H(n=1)' 'H+'
  * line                    (line) <U7 3kB 'A:2-1' 'A:3-1' ... 'A:16-15'
Data variables:
    effective_rate          (electron_temperature, electron_density, target, source) float64 9kB ...
    population_coefficient  (electron_temperature, electron_density, state, driver) float64 67kB ...
    pec                     (electron_temperature, electron_density, line, driver) float64 538kB ...
Attributes:
    model:    hydrogen/atomic/basic
    solve:    quasi-static
    device:   cpu

The effective ionization coefficient SCRS_\mathrm{CR} against TeT_e for every density:

s_cr = data["effective_rate"].sel(target="H+", source="H(n=1)")
with plasma_plots.figure(title="Effective ionization coefficient") as fig:
for density in densities:
s_cr.sel(electron_density=density).plasma.plot.lineout(
ax=fig[0],
title="",
xlabel="$T_e$ [eV]",
label=f"$n_e = 10^{{{np.log10(density):.0f}}}$ m$^{{-3}}$",
)
fig[0].set_xscale("log")
fig[0].set_yscale("log")
fig[0].set_ylabel(r"$S_\mathrm{CR}$ [m$^3$/s]")
fig[0].legend()
Loading figure…

The batched result is the same as the per-point reference solve:

reference = opencr.scan(
model,
opencr.Conditions(1.0, 1e18, densities={"H(n=1)": 1e19, "H+": 1e18}),
lines=False,
electron_temperature=temperatures[::8],
electron_density=densities[::3],
)
batched = data["effective_rate"].sel(
electron_temperature=temperatures[::8], electron_density=densities[::3]
)
print(
f"largest relative difference: {float(abs(batched / reference['effective_rate'] - 1).max()):.1e}"
)
largest relative difference: 3.4e-10