Backends
A backend turns the compiled laws of a system into numeric functions, with NumPy, Numba, or JAX.
get_supported_tags() of a backend class returns the times, spaces, and law kinds it can write functions for.
simulate raises a TagError for a backend that lacks a tag of the system, and uses the NumPy backend when you leave
it out. The table lists the supported tags of every backend.
| Backend | Time | Space | Law kinds |
|---|---|---|---|
JaxBackend |
continuous, discrete |
none |
equation, rate, update |
NumbaBackend |
any | any | equation, jump, noise, rate, sampling, update |
NumpyBackend |
any | any | equation, jump, noise, rate, sampling, update |
A compatible backend
NumbaBackend supports every tag of a decay, so it runs the system.
from skcomplex.core.components.elements import Element
from skcomplex.core.engine.backends import NumbaBackend
from skcomplex.core.systems import System
from skcomplex.systems.general.reactions import ReactionInteraction
tank = Element('tank', values={'x': 10.0})
decay = ReactionInteraction('decay', target='tank', reactants={'x': 1}, k=0.5)
system = System(elements=[tank], interactions=[decay], backend=NumbaBackend()).prepare()
round(system.simulate(until=1).get_values('tank.x')[-1], 2)
6.07
The tank holds 6.07 after one time unit, which is 10 exp(-0.5).
An incompatible backend
JaxBackend writes deterministic functions only, so prepare rejects a noise term and names it.
from skcomplex.core.components.elements import Element
from skcomplex.core.components.interactions import Interaction
from skcomplex.core.components.laws import NoiseLaw, RateLaw
from skcomplex.core.engine.backends import JaxBackend
from skcomplex.core.errors import SolverError
from skcomplex.core.systems import System
tank = Element('tank', values={'x': 10.0})
laws = lambda s: [RateLaw(s.target.x, -0.5 * s.target.x), NoiseLaw(s.target.x, 0.1)]
decay = Interaction('decay', roles={'target': 'tank'}, laws=laws)
sorted(JaxBackend.get_supported_tags().laws)
try:
System(elements=[tank], interactions=[decay], backend=JaxBackend()).prepare()
except SolverError as error:
message = str(error)
message
assert 'backend JaxBackend lacks' in message
['equation', 'rate', 'update']
The system is not valid, with 1 error(s):
- decay: The NoiseLaw of decay needs diagonal noise, which the backend JaxBackend lacks. Pass a solver and a backend that support diagonal noise. [noise-unsupported] The laws make stochastic differential equations, which CoSimulationSolver, EulerMaruyamaSolver, FractionalEulerMaruyamaSolver, HeunSolver, LammpsSolver, MilsteinSolver, RenewalSolver support.
The law kinds of JaxBackend are equation, rate, and update. The message says that the noise law needs diagonal
noise, which the backend lacks.