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 of.
simulate rejects a backend that lacks a tag of the system, and uses the NumPy backend when you leave it out.
| Backend | Time | Space | Law kinds |
|---|---|---|---|
JaxBackend |
continuous, discrete |
none |
equation, rate, update |
NumbaBackend |
any | any | any |
NumpyBackend |
any | any | any |
A compatible backend
NumbaBackend supports every tag of a decay, so it runs the system.
from skcomplex.core.components.elements import CompartmentElement
from skcomplex.core.components.interactions import RateInteraction
from skcomplex.core.components.system import System
from skcomplex.core.engine.backends import NumbaBackend
from skcomplex.core.engine.runtime import simulate
tank = CompartmentElement('tank', n=10.0)
decay = RateInteraction('decay', target='tank', state='n', rate=lambda s: -0.5 * s.target.n)
trajectory = simulate(System(elements=[tank], interactions=[decay]), until=1, backend=NumbaBackend())
round(trajectory.values('tank.n')[-1], 2)
6.07
An incompatible backend
JaxBackend writes deterministic functions only, so it rejects a noise term and names the law kind.
from skcomplex.core.components.elements import CompartmentElement
from skcomplex.core.components.interactions import RateInteraction
from skcomplex.core.components.system import System
from skcomplex.core.engine.backends import JaxBackend
from skcomplex.core.engine.runtime import simulate
from skcomplex.core.errors import TagError
tank = CompartmentElement('tank', n=10.0)
decay = RateInteraction('decay', target='tank', state='n', rate=lambda s: -0.5 * s.target.n, noise=0.1)
sorted(JaxBackend.get_supported_tags().laws)
try:
simulate(System(elements=[tank], interactions=[decay]), until=1, backend=JaxBackend())
except TagError as error:
message = str(error)
message
assert 'laws noise' in message
['equation', 'rate', 'update']
The backend JaxBackend does not support the laws noise of the system. Pass a backend that supports them, or none for the default.