Toy Examples of Format Features

This notebook demonstrates minimal examples of the following format features:

  • Time-dependent right hand sides

  • Step functions in the right hand side

  • Function definitions

Time-dependent right hand side:

time_dependent_rhs.yml implements the ODE

\begin{align} \dot{x} &= -\frac{x}{t+1} \\ x(0) &= 1 \end{align}

via

time:
    variable: t

odes:
    - stateId: x
      rightHandSide: -x/(t+1)
      initialValue: 1

The time variable t needs to be defined.

[1]:
import yaml2sbml

yaml_file_basic = 'time_dependent_rhs.yml'
sbml_output_file = 'time_dependent_rhs.xml'

yaml2sbml.yaml2sbml(yaml_file_basic, sbml_output_file)

For the sake of brevity, simulation and plotting is done in a separate function. Details on these steps can be found in other notebooks.

[2]:
%matplotlib inline

from simulation_and_plotting import simulate_AMICI, plot_AMICI
import amici.plotting

#simulate
amici_model, rdata = simulate_AMICI(sbml_output_file)
#plot
plot_AMICI(amici_model, rdata, 'Time-dependent right hand side')
../../_images/examples_Format_Features_Format_Features_4_0.png

Step functions in the right hand side

step_function.yml implements the ODE

\begin{align*} \dot{x} = \begin{cases} x , & t < 1 \\ -x, & \text{otherwise} \end{cases} \end{align*}

via

time:
    variable: t

odes:
    - stateId: x
      rightHandSide: piecewise(x, t < 1, -x)
      initialValue: 1

More details on piecewise functions can be found in the libsbml documentation.

[3]:
import yaml2sbml

yaml_file_basic = 'step_function.yml'
sbml_output_file = 'step_function.xml'

# translate yaml file to sbml
yaml2sbml.yaml2sbml(yaml_file_basic, sbml_output_file)

As discussed above, we plot via:

[4]:
%matplotlib inline

#simulate
amici_model, rdata = simulate_AMICI(sbml_output_file)

#plot
plot_AMICI(amici_model, rdata, 'Step function')
../../_images/examples_Format_Features_Format_Features_8_0.png

Function definition

yaml2sbml allows to define functions, that can then be called in other parts of the model. functions_in_rhs.yaml implements the ODE

\begin{align*} \dot{x}_1 &= f(x_2, 1, 2) \\ \dot{x}_2 &= f(x_1, -1, 0), \end{align*}

where \(f(x, a, b) = a \cdot x + b\) via

odes:
    - stateId: x_1
      rightHandSide: f(x_2, 1, 2)
      initialValue: 1

    - stateId: x_2
      rightHandSide: f(x_1, -1, 0)
      initialValue: 1

functions:
    - functionId: f
      arguments: x, a, b
      formula: a * x + b
[6]:
import yaml2sbml

yaml_file_basic = 'functions_in_rhs.yml'
sbml_output_file = 'functions_in_rhs.xml'

# translate yaml file to sbml
yaml2sbml.yaml2sbml(yaml_file_basic, sbml_output_file)

We now follow the usual work flow for simulation and plotting.

[7]:
%matplotlib inline

#simulate
amici_model, rdata = simulate_AMICI(sbml_output_file)

#plot
plot_AMICI(amici_model, rdata, 'Step function')
../../_images/examples_Format_Features_Format_Features_12_0.png