Integrator configuration
Integrator configuration¶
You can explicitly control which integrator is used for the simulations.
As all integrators have different settings, changing them is done on the integrator object itself, which can be found at Simulator().integrator
.
By default, the Assimulo
integrator is used.
However, that package currenlty requires installation via conda-forge
.
If mxlpy
was installed from pypi
, this package is not available and mxlpy
will fall back to the Scipy
package.
Thus, when setting integrator settings you should always check which integrator you are actually working with, as this otherwise might lead bugs on other systems.
In [1]:
Copied!
from functools import partial
from example_models import get_linear_chain_2v
from mxlpy import Simulator, unwrap
from mxlpy.integrators import Scipy
model = get_linear_chain_2v()
# You can explicitly set an integrator for the simulation
s = Simulator(model, integrator=Scipy)
# You can also change the default settings of each integrator
s = Simulator(model, integrator=partial(Scipy, atol=1e-6, rtol=1e-6))
s.simulate(5)
# You can change integration settings mid simulation
# As not all integrators have the same settings, we recommend explicitly checking
# which integrator is currently set
if isinstance(s.integrator, Scipy):
s.integrator.atol = 1e-8 # set absolute tolerance
s.integrator.rtol = 1e-8 # set relative tolerance
_ = unwrap(s.simulate(10).get_result()).variables.plot()
from functools import partial
from example_models import get_linear_chain_2v
from mxlpy import Simulator, unwrap
from mxlpy.integrators import Scipy
model = get_linear_chain_2v()
# You can explicitly set an integrator for the simulation
s = Simulator(model, integrator=Scipy)
# You can also change the default settings of each integrator
s = Simulator(model, integrator=partial(Scipy, atol=1e-6, rtol=1e-6))
s.simulate(5)
# You can change integration settings mid simulation
# As not all integrators have the same settings, we recommend explicitly checking
# which integrator is currently set
if isinstance(s.integrator, Scipy):
s.integrator.atol = 1e-8 # set absolute tolerance
s.integrator.rtol = 1e-8 # set relative tolerance
_ = unwrap(s.simulate(10).get_result()).variables.plot()
Diffrax¶
In [2]:
Copied!
model.get_initial_conditions()
model.get_initial_conditions()
Out[2]:
{'x': 1.0, 'y': 1.0}
In [3]:
Copied!
from diffrax import Tsit5
from mxlpy import Diffrax
model = get_linear_chain_2v()
# Use diffrax integrator suite
s = unwrap(
Simulator(model, integrator=Diffrax) # Use default solver
.simulate(5)
.get_result()
).variables.plot()
# Explicit solver choice
s = unwrap(
Simulator(model, integrator=partial(Diffrax, solver=Tsit5()))
.simulate(5)
.get_result()
).variables.plot()
from diffrax import Tsit5
from mxlpy import Diffrax
model = get_linear_chain_2v()
# Use diffrax integrator suite
s = unwrap(
Simulator(model, integrator=Diffrax) # Use default solver
.simulate(5)
.get_result()
).variables.plot()
# Explicit solver choice
s = unwrap(
Simulator(model, integrator=partial(Diffrax, solver=Tsit5()))
.simulate(5)
.get_result()
).variables.plot()
INFO:2025-08-01 09:05:31,629:jax._src.xla_bridge:752: Unable to initialize backend 'tpu': INTERNAL: Failed to open libtpu.so: libtpu.so: cannot open shared object file: No such file or directory
INFO:jax._src.xla_bridge:Unable to initialize backend 'tpu': INTERNAL: Failed to open libtpu.so: libtpu.so: cannot open shared object file: No such file or directory
In [ ]:
Copied!