Export ODEs to Mathematica (pysb.export.mathematica)

Module containing a class for converting a PySB model to a set of ordinary differential equations for integration or analysis in Mathematica.

For information on how to use the model exporters, see the documentation for pysb.export.

Output for the Robertson example model

The Mathematica code produced will follow the form as given below for pysb.examples.robertson:

A simple three-species chemical kinetics system known as "Robertson's
example", as presented in:

H. H. Robertson, The solution of a set of reaction rate equations, in Numerical
Analysis: An Introduction, J. Walsh, ed., Academic Press, 1966, pp. 178-182.

Mathematica model definition file for model robertson.
Generated by pysb.export.mathematica.MathematicaExporter.

Run with (for example):
tmax = 10
soln = NDSolve[Join[odes, initconds], slist, {t, 0, tmax}]
Plot[s0[t] /. soln, {t, 0, tmax}, PlotRange -> All]

(* Parameters *)
k1 = 0.040000000000000001;
k2 = 30000000;
k3 = 10000;
A0 = 1;
B0 = 0;
C0 = 0;

(* List of Species *)
(* s0[t] = A() *)
(* s1[t] = B() *)
(* s2[t] = C() *)

(* ODEs *)
odes = {
s0'[t] == -k1*s0[t] + k3*s1[t]*s2[t],
s1'[t] == k1*s0[t] - k2*s1[t]^2 - k3*s1[t]*s2[t],
s2'[t] == k2*s1[t]^2

(* Initial Conditions *)
initconds = {
s0[0] == A0,
s1[0] == B0,
s2[0] == C0

(* List of Variables (e.g., as an argument to NDSolve) *)
solvelist = {

(* Run the simulation -- example *)
tmax = 100
soln = NDSolve[Join[odes, initconds], solvelist, {t, 0, tmax}]

(* Observables *)
Atotal = (s0[t] * 1) /. soln
Btotal = (s1[t] * 1) /. soln
Ctotal = (s2[t] * 1) /. soln

The output consists of a block of commands that define the ODEs, parameters, species and other variables for the model, along with a set of descriptive comments. The sections are as follows:

  • The header comments identify the model and show an example of how to integrate the ODEs in Mathematica.

  • The parameters block defines the numerical values of the named parameters.

  • The list of species gives the mapping between the indexed species (s0, s1, s2) and their representation in PySB (A(), B(), C()).

  • The ODEs block defines the set of ordinary differential equations and assigns the set of equations to the variable odes.

  • The initial conditions block defines the initial values for each species and assigns the set of conditions to the variable initconds.

  • The “list of variables” block enumerates all of the species in the model (s0[t], s1[t], s2[t]) and assigns them to the variable solvelist; this list can be passed to the Mathematica command NDSolve to indicate the variables to be solved for.

  • This is followed by an example of how to call NDSolve to integrate the equations.

  • Finally, the observables block enumerates the observables in the model, expressing each one as a linear combination of the appropriate species in the model. The interpolating functions returned by NDSolve are substituted in from the solution variable soln, allowing the observables to be plotted.

Note that Mathematica does not permit underscores in variable names, so any underscores used in PySB variables will be removed (e.g., A_total will be converted to Atotal).

class pysb.export.mathematica.MathematicaExporter(model, docstring=None)[source]

A class for returning the ODEs for a given PySB model for use in Mathematica.

Inherits from pysb.export.Exporter, which implements basic functionality for all exporters.


Generate the corresponding Mathematica ODEs for the PySB model associated with the exporter.


String containing the Mathematica code for the model’s ODEs.