Skip to content
Projects

opencr.backends.jax_backend

The optional JAX backend: jitted rates, vmapped scans, automatic-differentiation Jacobians.

Nothing in the core imports JAX; this module does, and it is only usable with the jax extra installed. It reuses the reference’s prepared rate evaluators with jax.numpy as the array namespace and the structure of a PopulationSystem, so that

  • JaxModel.rates evaluates every reaction’s Maxwellian rate coefficient at one or many electron temperatures in one jitted call;
  • JaxModel.rhs and JaxModel.jacobian give R(n) and dR/dn of the population equations by forward-mode automatic differentiation, for the general (nonlinear, quasi-neutral) equations;
  • JaxModel.reduction is the quasi-static reduction of a batch of (T_e, n_e) with jax.vmap, the counterpart of opencr.backends.batched;
  • JaxModel.effective_rate is differentiable: jax.grad of it gives the sensitivity of an effective coefficient to T_e and n_e.

Two things differ from the reference: a Function rate (Python code) is tabulated on its file_temperatures and interpolated in log-log (the same as when it is written to a file), and a non-Maxwellian EEDF is refused. The first call of each jitted function compiles it; the benchmarks separate that from the warm time. Computations are in float64 (jax_enable_x64).

Classes

NameDescription
JaxModelA model on JAX, at fixed heavy-particle temperatures and escape factors.

JaxModelclass#

class JaxModel(model: Model, conditions: Conditions)

A model on JAX, at fixed heavy-particle temperatures and escape factors.

Parameters

NameTypeDescription
modelModelThe model.
conditionsConditionsThe conditions whose heavy temperatures, escape factors and prescribed densities are baked in; T_e and n_e are the arguments of the methods. A non-Maxwellian EEDF is refused.

conditionsattributeinstance attribute#

conditions = conditions

modelattributeinstance attribute#

model = model

systemattributeinstance attribute#

system = system

effective_ratemethod#

def effective_rate(source: str, target: str, electron_temperature: Any, electron_density: Any) -> Any

Return M_eff[target, source] / n_e (m^3/s) as a differentiable JAX scalar.

jax.grad(jax_model.effective_rate, argnums=2)(src, tgt, T_e, n_e) is the sensitivity to T_e.

Numerics

gradient

∂∂Te Mtarget,sourceeff(Te,ne)neby jax.grad\frac{\partial}{\partial T_e}\, \frac{M^{\mathrm{eff}}_{\mathrm{target},\mathrm{source}}(T_e, n_e)}{n_e} \quad \text{by } \texttt{jax.grad}

Assumptions:

  • the reduction of quasi_static written in jax.numpy, vmapped over (Te,ne)(T_e, n_e); float64

Built from:

  • opencr.kinetics.effective.QuasiStatic.effective_rate

jacobianmethod#

def jacobian(n: Any, electron_temperature: float, electron_density: float | None = None) -> Any

Return dR/dn as a dense array by forward-mode automatic differentiation.

Numerics

jacobian

J=∂R∂nby forward-mode automatic differentiation (jax.jacfwd) of R(n)=∑rνirwrJ = \frac{\partial R}{\partial \mathbf n} \quad \text{by forward-mode automatic differentiation (}\texttt{jax.jacfwd}\text{) of } R(\mathbf n) = \sum_r \nu_{ir} w_r

Assumptions:

  • dense; the general equations (nonlinear, quasi-neutral) with Maxwellian rates
  • a Function rate is tabulated on its file_temperatures and interpolated in log-log

Built from:

  • opencr.kinetics.system.PopulationSystem.jacobian

ratesmethod#

def rates(electron_temperature: Any) -> Any

Return k of shape (..., n_reactions) for Maxwellian electrons at T_e (eV).

reductionmethod#

def reduction(electron_temperatures: Any, electron_densities: Any) -> BatchedReduction

Return the quasi-static reduction of every (T_e, n_e) pair (vmapped).

The drivers are the prescribed states of the conditions, else the model’s. The result holds NumPy arrays.

rhsmethod#

def rhs(n: Any, electron_temperature: float, electron_density: float | None = None) -> Any

Return R(n) = dn/dt of the evolving states (m^-3 s^-1).

electron_density=None means quasi-neutrality, as in the conditions.