Skip to content

sbmlsim

Simulation of SBML models with sbmlsim-core.

sbmlsim-core compiles an SBML model to WebAssembly and simulates it with its engine (CVODE of SUNDIALS). It is part of the simulate extra and imported when a simulation runs, so the rest of the package works without it. See SBMLSIM_INSTALL.

The compiler reads flat SBML level 3 (version 1 or 2) without a required package, while the converters read every level: compilable_sbml converts a level 1 or 2 model and flattens the comp package of a model before it is compiled.

runtime

runtime()

The runtime of this process, created on first use.

The engine is compiled once and shared by every model of the process.

Returns:

Type Description
Any

The sbmlsim_core.Runtime.

compilable_sbml

compilable_sbml(sbml, base_path=None)

The model as flat SBML level 3, converted when it is not.

Parameters:

Name Type Description Default
sbml str

SBML document as string.

required
base_path Path | None

directory the source of an external model definition of a hierarchical model is resolved from, the working directory when None.

None

Returns:

Type Description
str

The document, converted to level 3 version 2 when its level is lower

str

and flattened when it requires the comp package; the string itself

str

when neither applies.

Raises:

Type Description
SimulationError

if the document has no model or cannot be converted.

state_scale

state_scale(model)

Smallest nonzero magnitude of the initial value of a state.

The states of the solver of an SBML model are the amounts of its species and the compartments and parameters a rate rule determines; the compiler lists them in the description of the model and the loaded model evaluates their initial values. An absolute tolerance above the magnitude of a state makes its trajectory meaningless, and the amounts of a model with a small compartment are small: a species with a concentration of 1 in a compartment of 1e-13 litre has the amount 1e-13. run_timecourse therefore scales the absolute tolerance with this magnitude, as roadrunner does.

Parameters:

Name Type Description Default
model Any

a LoadedModel of sbmlsim-core.

required

Returns:

Type Description
float

The smallest nonzero magnitude of an initial value of a state, 1.0

float

when the model has no state with a finite value other than zero.

solver_settings

solver_settings(
    relative_tolerance=None,
    absolute_tolerance=None,
    maximum_number_of_steps=None,
)

Solver settings of a request of simulation/1.

Every field of the settings of a request is explicit, so the defaults are spelled out here: CVODE with the time steps it chooses itself (maximum_time_step and initial_time_step 0.0) and the linear solver and the jacobian the compiler chose (auto).

Parameters:

Name Type Description Default
relative_tolerance float | None

relative tolerance of CVODE, RELATIVE_TOLERANCE when None.

None
absolute_tolerance float | None

absolute tolerance of CVODE, ABSOLUTE_TOLERANCE when None.

None
maximum_number_of_steps int | None

internal steps of CVODE between two time points, MAXIMUM_NUMBER_OF_STEPS when None.

None

Returns:

Type Description
dict[str, Any]

The settings of the request.

load_model

load_model(sbml, base_path=None)

Compile and load an SBML model, from the cache when it is there.

The model is compiled to WebAssembly and loaded into the runtime of the process; the result is kept by the hash of the SBML text, so a model simulated again is neither converted with libsbml nor compiled a second time (libopencor caches a CellML file the same way). An instance of the loaded model is cheap and simulates on a store of its own, so the cached model is shared without a simulation seeing another one.

Parameters:

Name Type Description Default
sbml str

SBML document as string, every level.

required
base_path Path | None

directory the source of an external model definition of a hierarchical model is resolved from.

None

Returns:

Type Description
Any

The LoadedModel of sbmlsim-core.

Raises:

Type Description
ImportError

if sbmlsim-core is not installed.

SimulationError

if the model cannot be made compilable.

SbmlsimCoreError

if sbmlsim-core cannot compile or load the model.

run_timecourse

run_timecourse(
    sbml,
    selections=None,
    start=0.0,
    end=1.0,
    steps=100,
    relative_tolerance=None,
    absolute_tolerance=None,
    maximum_number_of_steps=None,
    scale_tolerance=True,
    warmup=False,
    units=None,
    timings=None,
    base_path=None,
)

Uniform timecourse of an SBML model.

The call is the one of sbml2cellml.simulate.run_timecourse, which simulates the CellML model of the same conversion: the same arguments in the same order, a frame with time in the first column and the selected variables after it, and the units of the columns in units when a dictionary is given.

Parameters:

Name Type Description Default
sbml str | Path

path of an SBML file, or the document as a string.

required
selections list[str] | None

the variables of the result after time, by id; a species in brackets ([S1]) is its concentration, and every variable of the model is taken when None.

None
start float

start time.

0.0
end float

end time.

1.0
steps int

number of intervals, the result has steps + 1 rows.

100
relative_tolerance float | None

relative tolerance of CVODE, see solver_settings.

None
absolute_tolerance float | None

absolute tolerance of CVODE, see solver_settings.

None
maximum_number_of_steps int | None

internal steps of CVODE between two time points, see solver_settings.

None
scale_tolerance bool

multiply the absolute tolerance with the smallest initial value of a state when it is below one (state_scale), the tolerance of a model with small amounts would be above its states otherwise.

True
warmup bool

simulate the model once on an instance of its own before the simulation of the result and keep the discarded simulation out of the integration time, as on the CellML side, where libopencor generates the code of a model when it runs it the first time.

False
units dict[str, str] | None

filled with the units of every column when a dictionary is given, by the name of the column (_column_units).

None
timings dict[str, float] | None

filled with the wall time of the phases when given: setup is the conversion, the compilation and the loading of the model (nothing of it when the model is in the cache of load_model) and simulate the integration.

None
base_path Path | None

directory the source of an external model definition of a hierarchical model is resolved from; the directory of the file when sbml is a path.

None

Returns:

Type Description
DataFrame

The timecourse with time as first column and the selections after

DataFrame

it, every variable of the model when none are given, one row per

DataFrame

time point.

Raises:

Type Description
ImportError

if sbmlsim-core is not installed.

SimulationError

if the model cannot be made compilable.

SbmlsimCoreError

if sbmlsim-core cannot compile, load or simulate the model.