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 = 22
sizeStates = 4
sizeConstants = 26
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] = "R in component params_BG (J_per_K_mol)"
    legend_constants[1] = "T in component params_BG (kelvin)"
    legend_constants[2] = "K_Ai in component params_BG (per_fmol)"
    legend_constants[23] = "q_init_Ai in component params_BG (fmol)"
    legend_constants[3] = "K_Ao in component params_BG (per_fmol)"
    legend_constants[20] = "q_init_Ao in component params_BG (fmol)"
    legend_constants[4] = "K_1 in component params_BG (per_fmol)"
    legend_constants[5] = "q_init_1 in component params_BG (fmol)"
    legend_constants[6] = "K_2 in component params_BG (per_fmol)"
    legend_constants[7] = "q_init_2 in component params_BG (fmol)"
    legend_constants[8] = "K_3 in component params_BG (per_fmol)"
    legend_constants[9] = "q_init_3 in component params_BG (fmol)"
    legend_constants[10] = "K_4 in component params_BG (per_fmol)"
    legend_constants[11] = "q_init_4 in component params_BG (fmol)"
    legend_constants[12] = "kappa_r1 in component params_BG (fmol_per_s)"
    legend_constants[13] = "kappa_r2 in component params_BG (fmol_per_s)"
    legend_constants[14] = "kappa_r3 in component params_BG (fmol_per_s)"
    legend_constants[15] = "kappa_r4 in component params_BG (fmol_per_s)"
    legend_constants[25] = "mu_Ai in component GLUT2_BG (J_per_mol)"
    legend_algebraic[16] = "v_Ai in component GLUT2_BG (fmol_per_s)"
    legend_constants[22] = "mu_Ao in component GLUT2_BG (J_per_mol)"
    legend_algebraic[17] = "v_Ao in component GLUT2_BG (fmol_per_s)"
    legend_algebraic[0] = "mu_1 in component GLUT2_BG (J_per_mol)"
    legend_algebraic[18] = "v_1 in component GLUT2_BG (fmol_per_s)"
    legend_algebraic[1] = "mu_2 in component GLUT2_BG (J_per_mol)"
    legend_algebraic[19] = "v_2 in component GLUT2_BG (fmol_per_s)"
    legend_algebraic[2] = "mu_3 in component GLUT2_BG (J_per_mol)"
    legend_algebraic[20] = "v_3 in component GLUT2_BG (fmol_per_s)"
    legend_algebraic[3] = "mu_4 in component GLUT2_BG (J_per_mol)"
    legend_algebraic[21] = "v_4 in component GLUT2_BG (fmol_per_s)"
    legend_algebraic[4] = "A_f_r1 in component GLUT2_BG (J_per_mol)"
    legend_algebraic[6] = "v_r1 in component GLUT2_BG (fmol_per_s)"
    legend_algebraic[5] = "A_r_r1 in component GLUT2_BG (J_per_mol)"
    legend_algebraic[7] = "A_f_r2 in component GLUT2_BG (J_per_mol)"
    legend_algebraic[9] = "v_r2 in component GLUT2_BG (fmol_per_s)"
    legend_algebraic[8] = "A_r_r2 in component GLUT2_BG (J_per_mol)"
    legend_algebraic[10] = "A_f_r3 in component GLUT2_BG (J_per_mol)"
    legend_algebraic[14] = "v_r3 in component GLUT2_BG (fmol_per_s)"
    legend_algebraic[12] = "A_r_r3 in component GLUT2_BG (J_per_mol)"
    legend_algebraic[11] = "A_f_r4 in component GLUT2_BG (J_per_mol)"
    legend_algebraic[15] = "v_r4 in component GLUT2_BG (fmol_per_s)"
    legend_algebraic[13] = "A_r_r4 in component GLUT2_BG (J_per_mol)"
    legend_constants[24] = "q_Ai in component GLUT2_BG (fmol)"
    legend_constants[21] = "q_Ao in component GLUT2_BG (fmol)"
    legend_states[0] = "q_1 in component GLUT2_BG (fmol)"
    legend_states[1] = "q_2 in component GLUT2_BG (fmol)"
    legend_states[2] = "q_3 in component GLUT2_BG (fmol)"
    legend_states[3] = "q_4 in component GLUT2_BG (fmol)"
    legend_constants[16] = "V_i in component params_BG (pL)"
    legend_constants[17] = "g_i in component params_BG (mM)"
    legend_constants[18] = "g_o in component params_BG (mM)"
    legend_constants[19] = "V_o in component params_BG (pL)"
    legend_rates[0] = "d/dt q_1 in component GLUT2_BG (fmol)"
    legend_rates[1] = "d/dt q_2 in component GLUT2_BG (fmol)"
    legend_rates[2] = "d/dt q_3 in component GLUT2_BG (fmol)"
    legend_rates[3] = "d/dt q_4 in component GLUT2_BG (fmol)"
    return (legend_states, legend_algebraic, legend_voi, legend_constants)

def initConsts():
    constants = [0.0] * sizeConstants; states = [0.0] * sizeStates;
    constants[0] = 8.31
    constants[1] = 273.15
    constants[2] = 149.65
    constants[3] = 149.65
    constants[4] = 33.20
    constants[5] = 0.0017
    constants[6] = 4.25e+03
    constants[7] = 0.0017
    constants[8] = 344.59
    constants[9] = 0.0017
    constants[10] = 1.99
    constants[11] = 0.0017
    constants[12] = 0.36
    constants[13] = 0.26
    constants[14] = 1.01e+05
    constants[15] = 1.01e+04
    constants[16] = 0.09
    constants[17] = 10
    constants[18] = 1e-5
    constants[19] = 0.09
    constants[20] = constants[18]*constants[19]
    constants[21] = constants[20]
    constants[22] = constants[0]*constants[1]*log(constants[3]*constants[21])
    constants[23] = constants[17]*constants[16]
    constants[24] = constants[23]
    constants[25] = constants[0]*constants[1]*log(constants[2]*constants[24])
    states[0] = constants[5]
    states[1] = constants[7]
    states[2] = constants[9]
    states[3] = constants[11]
    return (states, constants)

def computeRates(voi, states, constants):
    rates = [0.0] * sizeStates; algebraic = [0.0] * sizeAlgebraic
    algebraic[3] = constants[0]*constants[1]*log(constants[10]*states[3])
    algebraic[4] = algebraic[3]
    algebraic[0] = constants[0]*constants[1]*log(constants[4]*states[0])
    algebraic[5] = algebraic[0]
    algebraic[6] = constants[12]*(exp(algebraic[4]/(constants[0]*constants[1]))-exp(algebraic[5]/(constants[0]*constants[1])))
    algebraic[10] = constants[22]+algebraic[0]
    algebraic[1] = constants[0]*constants[1]*log(constants[6]*states[1])
    algebraic[12] = algebraic[1]
    algebraic[14] = constants[14]*(exp(algebraic[10]/(constants[0]*constants[1]))-exp(algebraic[12]/(constants[0]*constants[1])))
    algebraic[18] = algebraic[6]-algebraic[14]
    rates[0] = algebraic[18]
    algebraic[7] = algebraic[1]
    algebraic[2] = constants[0]*constants[1]*log(constants[8]*states[2])
    algebraic[8] = algebraic[2]
    algebraic[9] = constants[13]*(exp(algebraic[7]/(constants[0]*constants[1]))-exp(algebraic[8]/(constants[0]*constants[1])))
    algebraic[19] = algebraic[14]-algebraic[9]
    rates[1] = algebraic[19]
    algebraic[11] = algebraic[2]
    algebraic[13] = constants[25]+algebraic[3]
    algebraic[15] = constants[15]*(exp(algebraic[11]/(constants[0]*constants[1]))-exp(algebraic[13]/(constants[0]*constants[1])))
    algebraic[20] = algebraic[9]-algebraic[15]
    rates[2] = algebraic[20]
    algebraic[21] = algebraic[15]-algebraic[6]
    rates[3] = algebraic[21]
    return(rates)

def computeAlgebraic(constants, states, voi):
    algebraic = array([[0.0] * len(voi)] * sizeAlgebraic)
    states = array(states)
    voi = array(voi)
    algebraic[3] = constants[0]*constants[1]*log(constants[10]*states[3])
    algebraic[4] = algebraic[3]
    algebraic[0] = constants[0]*constants[1]*log(constants[4]*states[0])
    algebraic[5] = algebraic[0]
    algebraic[6] = constants[12]*(exp(algebraic[4]/(constants[0]*constants[1]))-exp(algebraic[5]/(constants[0]*constants[1])))
    algebraic[10] = constants[22]+algebraic[0]
    algebraic[1] = constants[0]*constants[1]*log(constants[6]*states[1])
    algebraic[12] = algebraic[1]
    algebraic[14] = constants[14]*(exp(algebraic[10]/(constants[0]*constants[1]))-exp(algebraic[12]/(constants[0]*constants[1])))
    algebraic[18] = algebraic[6]-algebraic[14]
    algebraic[7] = algebraic[1]
    algebraic[2] = constants[0]*constants[1]*log(constants[8]*states[2])
    algebraic[8] = algebraic[2]
    algebraic[9] = constants[13]*(exp(algebraic[7]/(constants[0]*constants[1]))-exp(algebraic[8]/(constants[0]*constants[1])))
    algebraic[19] = algebraic[14]-algebraic[9]
    algebraic[11] = algebraic[2]
    algebraic[13] = constants[25]+algebraic[3]
    algebraic[15] = constants[15]*(exp(algebraic[11]/(constants[0]*constants[1]))-exp(algebraic[13]/(constants[0]*constants[1])))
    algebraic[20] = algebraic[9]-algebraic[15]
    algebraic[21] = algebraic[15]-algebraic[6]
    algebraic[16] = algebraic[15]
    algebraic[17] = -algebraic[14]
    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)