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 = 3
sizeStates = 1
sizeConstants = 43
from math import *
from numpy import *

def createLegends():
    legend_states = [""] * sizeStates
    legend_rates = [""] * sizeStates
    legend_algebraic = [""] * sizeAlgebraic
    legend_voi = ""
    legend_constants = [""] * sizeConstants
    legend_voi = "t in component GLUT2_BG (second)"
    legend_constants[0] = "K_Ai in component params_BG (per_fmol)"
    legend_constants[1] = "K_Ao in component params_BG (per_fmol)"
    legend_constants[2] = "K_1 in component params_BG (per_fmol)"
    legend_constants[3] = "K_2 in component params_BG (per_fmol)"
    legend_constants[4] = "K_3 in component params_BG (per_fmol)"
    legend_constants[5] = "K_4 in component params_BG (per_fmol)"
    legend_constants[6] = "kappa_r1 in component params_BG (fmol_per_s)"
    legend_constants[7] = "kappa_r2 in component params_BG (fmol_per_s)"
    legend_constants[8] = "kappa_r3 in component params_BG (fmol_per_s)"
    legend_constants[9] = "kappa_r4 in component params_BG (fmol_per_s)"
    legend_constants[30] = "q_Ai in component GLUT2_BG (fmol)"
    legend_algebraic[0] = "q_Ao in component GLUT2_BG (fmol)"
    legend_constants[37] = "lambda in component GLUT2_BG (dimensionless)"
    legend_constants[33] = "q_tot in component GLUT2_BG (fmol)"
    legend_constants[38] = "v_max in component GLUT2_BG (fmol_per_s)"
    legend_constants[39] = "k_m_1 in component GLUT2_BG (dimensionless)"
    legend_constants[40] = "k_m_2 in component GLUT2_BG (dimensionless)"
    legend_constants[41] = "k_m_3 in component GLUT2_BG (dimensionless)"
    legend_algebraic[1] = "v in component GLUT2_BG (fmol_per_s)"
    legend_constants[35] = "v_max_free in component GLUT2_BG (fmol_per_s)"
    legend_algebraic[2] = "v_free in component GLUT2_BG (fmol_per_s)"
    legend_constants[34] = "k_m_1_free in component GLUT2_BG (dimensionless)"
    legend_constants[36] = "k_m_2_free in component GLUT2_BG (dimensionless)"
    legend_constants[10] = "k_m_3_free in component GLUT2_BG (dimensionless)"
    legend_constants[11] = "k_oi_math in component GLUT2_BG (mM)"
    legend_constants[12] = "k_io_math in component GLUT2_BG (mM)"
    legend_constants[13] = "v_oi_max in component GLUT2_BG (mM_per_s)"
    legend_constants[14] = "v_io_max in component GLUT2_BG (mM_per_s)"
    legend_constants[15] = "V_E in component GLUT2_BG (pL)"
    legend_constants[16] = "GC in component GLUT2_BG (uM)"
    legend_constants[17] = "g_i in component GLUT2_BG (mM)"
    legend_states[0] = "g_o in component GLUT2_BG (mM)"
    legend_constants[18] = "V_i in component GLUT2_BG (pL)"
    legend_constants[19] = "V_o in component GLUT2_BG (pL)"
    legend_constants[20] = "R in component params_BG (J_per_K_mol)"
    legend_constants[21] = "T in component params_BG (kelvin)"
    legend_constants[31] = "q_init_Ai in component params_BG (fmol)"
    legend_constants[32] = "q_init_Ao in component params_BG (fmol)"
    legend_constants[22] = "q_init_1 in component params_BG (fmol)"
    legend_constants[23] = "q_init_2 in component params_BG (fmol)"
    legend_constants[24] = "q_init_3 in component params_BG (fmol)"
    legend_constants[25] = "q_init_4 in component params_BG (fmol)"
    legend_constants[26] = "V_i in component params_BG (pL)"
    legend_constants[27] = "g_i in component params_BG (mM)"
    legend_constants[28] = "g_o in component params_BG (mM)"
    legend_constants[29] = "V_o in component params_BG (pL)"
    legend_rates[0] = "d/dt g_o in component GLUT2_BG (mM)"
    return (legend_states, legend_algebraic, legend_voi, legend_constants)

def initConsts():
    constants = [0.0] * sizeConstants; states = [0.0] * sizeStates;
    constants[0] = 149.65
    constants[1] = 149.65
    constants[2] = 33.20
    constants[3] = 4.25e+03
    constants[4] = 344.59
    constants[5] = 1.99
    constants[6] = 0.36
    constants[7] = 0.26
    constants[8] = 1.01e+05
    constants[9] = 1.01e+04
    constants[10] = 218.96
    constants[11] = 0.1094
    constants[12] = 1.609
    constants[13] = 0.004839
    constants[14] = 0.07117
    constants[15] = 1
    constants[16] = 6.67
    constants[17] = 1e-8
    states[0] = 1e-8
    constants[18] = 0.09
    constants[19] = 0.09
    constants[20] = 8.31
    constants[21] = 273.15
    constants[22] = 0.0017
    constants[23] = 0.0017
    constants[24] = 0.0017
    constants[25] = 0.0017
    constants[26] = 0.09
    constants[27] = 10
    constants[28] = 1e-5
    constants[29] = 0.09
    constants[30] = constants[17]*constants[18]
    constants[31] = constants[27]*constants[26]
    constants[32] = constants[28]*constants[29]
    constants[42] = 1.00000
    constants[33] = (constants[16]*constants[15]*1.00000)/1000.00
    constants[34] = constants[11]*constants[18]*constants[0]
    constants[35] = (constants[13]*constants[15])/constants[34]
    constants[36] = (constants[14]*constants[15])/constants[35]
    constants[37] = constants[7]/constants[6]
    constants[38] = (constants[33]*constants[7]*constants[4])/(constants[4]/constants[2]+constants[4]/constants[5])
    constants[39] = (constants[4]/constants[2]+constants[4]/constants[5])/(constants[4]/constants[3]+(constants[37]*constants[4])/constants[5])
    constants[40] = (constants[4]/constants[2]+constants[4]/constants[5])/(1.00000+(constants[37]*constants[4])/constants[2])
    constants[41] = (constants[4]/constants[2]+constants[4]/constants[5])/(constants[37]*(1.00000+constants[4]/constants[3]))
    return (states, constants)

def computeRates(voi, states, constants):
    rates = [0.0] * sizeStates; algebraic = [0.0] * sizeAlgebraic
    rates[0] = constants[42]
    return(rates)

def computeAlgebraic(constants, states, voi):
    algebraic = array([[0.0] * len(voi)] * sizeAlgebraic)
    states = array(states)
    voi = array(voi)
    algebraic[0] = states[0]*constants[19]
    algebraic[1] = (constants[38]*(constants[1]*algebraic[0]-constants[0]*constants[30]))/(1.00000+(constants[1]*algebraic[0])/constants[39]+(constants[0]*constants[30])/constants[40]+(constants[1]*algebraic[0]*constants[0]*constants[30])/constants[41])
    algebraic[2] = (constants[35]*(constants[1]*algebraic[0]-constants[0]*constants[30]))/(1.00000+(constants[1]*algebraic[0])/constants[34]+(constants[0]*constants[30])/constants[36]+(constants[1]*algebraic[0]*constants[0]*constants[30])/constants[10])
    return algebraic

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)