OneSimulation docs Website Free books Plans on Gumroad

Docs › Build a first case in each physics package › Chemistry

5.4 Chemistry

onesim.physics.chemistry

Choose SpeciesTransport for line advection, diffusion and decay. Use a TransportSpecies entry per stable species ID and obtain each field with model.concentration(species_id). Initialize all transported concentrations on the same spatial support. Set diffusivity in m\(^2\)/s and first-order decay in s\(^{-1}\). A periodic Fourier mode translates at the prescribed velocity and attenuates at a rate given by diffusion plus decay. For an open domain include boundary transport in the budget. For FD, FV, H1 and DG variants see test_species_transport_flexibility.py.

# Use run_periodic_line from this chapter.
from onesim.numerics.fvm import FiniteVolume, MUSCL, Rusanov, TwoPointDiffusion
from onesim.physics.chemistry import SpeciesTransport, TransportSpecies

line = Line(start=(0, 0, 0), end=(2*np.pi, 0, 0))
v, diffusion, decay = 0.6, 0.03, 0.2
transport = SpeciesTransport(
    domain=line.line, velocity=v,
    species=(TransportSpecies("tracer", diffusivity=diffusion, decay_rate=decay),),
)
tracer = transport.concentration("tracer")
n, t = 48, 0.1
result = run_periodic_line(
    line, transport, ((tracer, lambda x: 2 + np.cos(x)),),
    Numerics(
        spatial=FiniteVolume(convection=Rusanov(), reconstruction=MUSCL(),
                             diffusion=TwoPointDiffusion()),
        time_integrator=ClassicalRungeKutta4(),
    ),
    cells=n, end=t, dt=0.001,
)
field = result.field(tracer)
x = field.space.dof_coordinates[:, 0]
exact = np.exp(-decay*t) * (
    2 + np.sinc(1/n) * np.exp(-diffusion*t) * np.cos(x - v*t)
)
np.testing.assert_allclose(field.values, exact, atol=0.002, rtol=0)

import matplotlib.pyplot as plt
import onesimulation_plot_theme as theme

theme.use_theme()
fig, ax = plt.subplots(figsize=(6.8,3.4),layout="constrained")
ax.plot(x, exact, "--", label="Cell-average reference")
ax.plot(x, field.values, ".", label="Species transport")
ax.set(xlabel="World x [m]",ylabel=f"Tracer [{tracer.unit}]")
ax.legend(frameon=False)
plt.show()
Figure computed with OneSimulation: recipe species transport

For homogeneous kinetics, build ReactionNetworkIR from species and reaction declarations, use ReactionKinetics, and initialize each transported number density. Chemistry and ChemistryKinetics describe the evaluation contract for the network. Check stoichiometric invariants and the rate-law units before choosing a time integrator. Strongly separated reaction times suggest comparing against a small-step reference and using an implicit stage with a compatible rate derivative. LumpedElectrode composes Butler–Volmer reactions and a double layer. LumpedBattery uses BatteryChemistry for open-circuit and resistance closures. Plot voltage, state of charge and temperature against time using a clearly stated current sign. See test_kinetics_input_flexibility.py for a network-driven global model.