This example illustrates how to define a kinetic model using the scripting interface.Normally one uses standard model formats like SBML or kkit to concisely define kinetic models, but in some cases one would like to modify the model through the script.

This example creates a reaction model

In [1]:
# first step is to import moose
import moose
import pylab
import numpy
%matplotlib inline
# create container for model
model = moose.Neutral( 'model' )

#create chemical compartment either `CubeMesh` or `CylMesh` and set the volume
compartment = moose.CubeMesh( '/model/compartment' )
compartment.volume = 1e-20

# create molecules and reactions
sub = moose.Pool( '/model/compartment/Sub' )
sub.concInit = 0.001
prd = moose.Pool( '/model/compartment/Prd' )
reac = moose.Reac( '/model/compartment/reac' )
reac.Kf = 0.1
reac.Kb = 0.001

# connect them up for reactions
moose.connect( reac, 'sub', sub, 'reac' )
moose.connect( reac, 'prd', prd, 'reac' )


#setting up the KSolve
gsolve = moose.Ksolve( '/model/compartment/ksolve' )
stoich = moose.Stoich( '/model/compartment/stoich' )
stoich.compartment = moose.element( '/model/compartment' )
stoich.ksolve = gsolve
stoich.path = "/model/compartment/##"
moose.setClock( 15, 1.0 ) # clock for the solver
moose.useClock( 15, '/model/compartment/gsolve', 'process' )

# Create the output tables
graphs = moose.Neutral( '/model/graphs' )
outputA = moose.Table2 ( '/model/graphs/concA' )
outputB = moose.Table2 ( '/model/graphs/concB' )

# connect up the tables for plot substrate and product concentration
moose.connect( outputA, 'requestOut', sub, 'getConc' );
moose.connect( outputB, 'requestOut', prd, 'getConc' );

# reinit and run for 100s
moose.reinit()
moose.start(100)

#setting up displaying plots in matplotlib
for x in moose.wildcardFind( '/model/graphs/#[TYPE=Table2]' ):
    t = numpy.arange( 0, x.vector.size, 1 ) #sec
    pylab.plot( t, x.vector, label=x.name,linewidth=2)
pylab.legend()
pylab.show()