Generated Code

The following is python code generated by the CellML API from this CellML file. (Back to language selection)

The raw code is available.

# Size of variable arrays:
sizeAlgebraic = 19
sizeStates = 9
sizeConstants = 29
from math import *
from numpy import *

def createLegends():
    legend_states = [""] * sizeStates
    legend_rates = [""] * sizeStates
    legend_algebraic = [""] * sizeAlgebraic
    legend_voi = ""
    legend_constants = [""] * sizeConstants
    legend_voi = "time in component environment (hour)"
    legend_algebraic[1] = "v_1 in component main (pmol_per_ug_DNA_per_hour)"
    legend_constants[25] = "v_2 in component main (pmol_per_ug_DNA_per_hour)"
    legend_constants[26] = "v_3 in component main (pmol_per_ug_DNA_per_hour)"
    legend_algebraic[2] = "v_4 in component main (pmol_per_ug_DNA_per_hour)"
    legend_constants[27] = "v_5 in component main (pmol_per_ug_DNA_per_hour)"
    legend_algebraic[3] = "v_6 in component main (pmol_per_ug_DNA_per_hour)"
    legend_constants[28] = "v_7 in component main (pmol_per_ug_DNA_per_hour)"
    legend_algebraic[4] = "v_8 in component main (pmol_per_ug_DNA_per_hour)"
    legend_algebraic[5] = "v_9 in component main (pmol_per_ug_DNA_per_hour)"
    legend_algebraic[6] = "v_10 in component main (pmol_per_ug_DNA_per_hour)"
    legend_algebraic[7] = "v_11 in component main (pmol_per_ug_DNA_per_hour)"
    legend_algebraic[8] = "v_12 in component main (pmol_per_ug_DNA_per_hour)"
    legend_algebraic[9] = "v_13 in component main (pmol_per_ug_DNA_per_hour)"
    legend_algebraic[10] = "v_14 in component main (pmol_per_ug_DNA_per_hour)"
    legend_algebraic[11] = "v_15 in component main (pmol_per_ug_DNA_per_hour)"
    legend_algebraic[12] = "v_16 in component main (pmol_per_ug_DNA_per_hour)"
    legend_algebraic[13] = "v_17 in component main (pmol_per_ug_DNA_per_hour)"
    legend_algebraic[14] = "v_18 in component main (pmol_per_ug_DNA_per_hour)"
    legend_algebraic[15] = "v_19 in component main (pmol_per_ug_DNA_per_hour)"
    legend_algebraic[17] = "v_20 in component main (pmol_per_ug_DNA_per_hour)"
    legend_algebraic[16] = "v_21 in component main (pmol_per_ug_DNA_per_hour)"
    legend_algebraic[18] = "v_22 in component main (pmol_per_ug_DNA_per_hour)"
    legend_constants[0] = "k_1 in component main (pmol_per_ug_DNA_per_hour)"
    legend_constants[1] = "k_2 in component main (pmol_per_ug_DNA_per_hour)"
    legend_constants[2] = "k_3 in component main (per_hour)"
    legend_constants[3] = "k_4 in component main (per_hour)"
    legend_constants[4] = "k_5 in component main (ug_DNA_per_pmol_per_hour)"
    legend_constants[5] = "k_6 in component main (per_hour)"
    legend_constants[6] = "k_7 in component main (per_hour)"
    legend_constants[7] = "k_8 in component main (per_hour)"
    legend_constants[8] = "k_9 in component main (per_hour)"
    legend_constants[9] = "k_10 in component main (ug_DNA_per_pmol_per_hour)"
    legend_constants[10] = "k_11 in component main (per_hour)"
    legend_constants[11] = "k_12 in component main (per_hour)"
    legend_constants[12] = "k_13 in component main (per_hour)"
    legend_constants[13] = "k_14 in component main (per_hour)"
    legend_constants[14] = "k_15 in component main (per_hour)"
    legend_constants[15] = "k_16 in component main (per_hour)"
    legend_constants[16] = "k_17 in component main (per_hour)"
    legend_constants[17] = "k_18 in component main (per_hour)"
    legend_constants[18] = "k_19 in component main (per_hour)"
    legend_constants[19] = "k_20 in component main (per_hour)"
    legend_constants[20] = "k_21 in component main (per_hour)"
    legend_constants[21] = "k_22 in component main (per_hour)"
    legend_algebraic[0] = "LPS in component main (dimensionless)"
    legend_constants[22] = "PIP_2 in component main (dimensionless)"
    legend_constants[23] = "DG in component main (pmol_per_ug_DNA)"
    legend_constants[24] = "GPCho in component main (pmol_per_ug_DNA)"
    legend_states[0] = "AA in component main (pmol_per_ug_DNA)"
    legend_states[1] = "HETE in component main (pmol_per_ug_DNA)"
    legend_states[2] = "PGH_2 in component main (pmol_per_ug_DNA)"
    legend_states[3] = "PGE_2 in component main (pmol_per_ug_DNA)"
    legend_states[4] = "PGF_2a in component main (pmol_per_ug_DNA)"
    legend_states[5] = "PGD_2 in component main (pmol_per_ug_DNA)"
    legend_states[6] = "PGJ_2 in component main (pmol_per_ug_DNA)"
    legend_states[7] = "dPGD_2 in component main (pmol_per_ug_DNA)"
    legend_states[8] = "dPGJ_2 in component main (pmol_per_ug_DNA)"
    legend_rates[0] = "d/dt AA in component main (pmol_per_ug_DNA)"
    legend_rates[1] = "d/dt HETE in component main (pmol_per_ug_DNA)"
    legend_rates[2] = "d/dt PGH_2 in component main (pmol_per_ug_DNA)"
    legend_rates[3] = "d/dt PGE_2 in component main (pmol_per_ug_DNA)"
    legend_rates[4] = "d/dt PGF_2a in component main (pmol_per_ug_DNA)"
    legend_rates[5] = "d/dt PGD_2 in component main (pmol_per_ug_DNA)"
    legend_rates[6] = "d/dt PGJ_2 in component main (pmol_per_ug_DNA)"
    legend_rates[7] = "d/dt dPGD_2 in component main (pmol_per_ug_DNA)"
    legend_rates[8] = "d/dt dPGJ_2 in component main (pmol_per_ug_DNA)"
    return (legend_states, legend_algebraic, legend_voi, legend_constants)

def initConsts():
    constants = [0.0] * sizeConstants; states = [0.0] * sizeStates;
    constants[0] = 355.637
    constants[1] = 1e-15
    constants[2] = 1e-15
    constants[3] = 1e-15
    constants[4] = 1e-15
    constants[5] = 0.33
    constants[6] = 1e-15
    constants[7] = 0.007
    constants[8] = 0.187
    constants[9] = 0.024
    constants[10] = 0.111
    constants[11] = 0.098
    constants[12] = 0.204
    constants[13] = 1e-15
    constants[14] = 0.061
    constants[15] = 1e-15
    constants[16] = 3.116
    constants[17] = 0.054
    constants[18] = 0.029
    constants[19] = 0.014
    constants[20] = 0.034
    constants[21] = 0.116
    constants[22] = 1
    constants[23] = 0
    constants[24] = 1
    states[0] = 25
    states[1] = 0
    states[2] = 0
    states[3] = 0
    states[4] = 0
    states[5] = 0
    states[6] = 0
    states[7] = 0
    states[8] = 0
    constants[25] = constants[22]*constants[1]
    constants[26] = constants[23]*constants[2]
    constants[27] = constants[23]*constants[24]*constants[4]
    constants[28] = constants[24]*constants[6]
    return (states, constants)

def computeRates(voi, states, constants):
    rates = [0.0] * sizeStates; algebraic = [0.0] * sizeAlgebraic
    algebraic[4] = states[0]*constants[7]
    algebraic[5] = states[1]*constants[8]
    rates[1] = algebraic[4]-algebraic[5]
    algebraic[0] = custom_piecewise([less_equal(voi , 0.500000), voi*2.00000 , greater(voi , 0.500000) & less(voi , 2.00000), 1.00000-(voi-0.500000)/1.50000 , True, 0.00000])
    algebraic[1] = algebraic[0]*constants[22]*constants[0]
    algebraic[2] = states[0]*constants[3]
    algebraic[3] = algebraic[0]*constants[24]*constants[5]
    algebraic[6] = constants[23]*states[0]*constants[9]
    algebraic[7] = algebraic[0]*states[0]*constants[10]
    algebraic[8] = states[0]*constants[11]
    rates[0] = ((((((algebraic[1]+constants[25]+constants[26])-algebraic[2])+constants[27]+algebraic[3]+constants[28])-algebraic[4])-algebraic[6])-algebraic[7])-algebraic[8]
    algebraic[9] = states[2]*constants[12]
    algebraic[10] = states[3]*constants[13]
    rates[3] = algebraic[9]-algebraic[10]
    algebraic[11] = states[2]*constants[14]
    algebraic[13] = states[2]*constants[16]
    rates[2] = (((algebraic[6]+algebraic[7]+algebraic[8])-algebraic[9])-algebraic[11])-algebraic[13]
    algebraic[12] = states[4]*constants[15]
    rates[4] = algebraic[11]-algebraic[12]
    algebraic[14] = states[5]*constants[17]
    algebraic[15] = states[5]*constants[18]
    rates[5] = (algebraic[13]-algebraic[14])-algebraic[15]
    algebraic[16] = states[6]*constants[20]
    rates[6] = algebraic[14]-algebraic[16]
    algebraic[17] = states[7]*constants[19]
    rates[7] = algebraic[15]-algebraic[17]
    algebraic[18] = states[8]*constants[21]
    rates[8] = algebraic[16]-algebraic[18]
    return(rates)

def computeAlgebraic(constants, states, voi):
    algebraic = array([[0.0] * len(voi)] * sizeAlgebraic)
    states = array(states)
    voi = array(voi)
    algebraic[4] = states[0]*constants[7]
    algebraic[5] = states[1]*constants[8]
    algebraic[0] = custom_piecewise([less_equal(voi , 0.500000), voi*2.00000 , greater(voi , 0.500000) & less(voi , 2.00000), 1.00000-(voi-0.500000)/1.50000 , True, 0.00000])
    algebraic[1] = algebraic[0]*constants[22]*constants[0]
    algebraic[2] = states[0]*constants[3]
    algebraic[3] = algebraic[0]*constants[24]*constants[5]
    algebraic[6] = constants[23]*states[0]*constants[9]
    algebraic[7] = algebraic[0]*states[0]*constants[10]
    algebraic[8] = states[0]*constants[11]
    algebraic[9] = states[2]*constants[12]
    algebraic[10] = states[3]*constants[13]
    algebraic[11] = states[2]*constants[14]
    algebraic[13] = states[2]*constants[16]
    algebraic[12] = states[4]*constants[15]
    algebraic[14] = states[5]*constants[17]
    algebraic[15] = states[5]*constants[18]
    algebraic[16] = states[6]*constants[20]
    algebraic[17] = states[7]*constants[19]
    algebraic[18] = states[8]*constants[21]
    return algebraic

def custom_piecewise(cases):
    """Compute result of a piecewise function"""
    return select(cases[0::2],cases[1::2])

def solve_model():
    """Solve model with ODE solver"""
    from scipy.integrate import ode
    # Initialise constants and state variables
    (init_states, constants) = initConsts()

    # Set timespan to solve over
    voi = linspace(0, 10, 500)

    # Construct ODE object to solve
    r = ode(computeRates)
    r.set_integrator('vode', method='bdf', atol=1e-06, rtol=1e-06, max_step=1)
    r.set_initial_value(init_states, voi[0])
    r.set_f_params(constants)

    # Solve model
    states = array([[0.0] * len(voi)] * sizeStates)
    states[:,0] = init_states
    for (i,t) in enumerate(voi[1:]):
        if r.successful():
            r.integrate(t)
            states[:,i+1] = r.y
        else:
            break

    # Compute algebraic variables
    algebraic = computeAlgebraic(constants, states, voi)
    return (voi, states, algebraic)

def plot_model(voi, states, algebraic):
    """Plot variables against variable of integration"""
    import pylab
    (legend_states, legend_algebraic, legend_voi, legend_constants) = createLegends()
    pylab.figure(1)
    pylab.plot(voi,vstack((states,algebraic)).T)
    pylab.xlabel(legend_voi)
    pylab.legend(legend_states + legend_algebraic, loc='best')
    pylab.show()

if __name__ == "__main__":
    (voi, states, algebraic) = solve_model()
    plot_model(voi, states, algebraic)