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 = 27
sizeStates = 9
sizeConstants = 36
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 main (second)"
    legend_constants[0] = "RT in component main (J_per_mol)"
    legend_constants[1] = "F in component main (C_per_mol)"
    legend_states[0] = "q_ac_W in component main (litre)"
    legend_states[1] = "q_vc_W in component main (litre)"
    legend_states[2] = "q_gi_W in component main (litre)"
    legend_states[3] = "q_pt_W in component main (litre)"
    legend_constants[2] = "q_giEpi_W in component main (litre)"
    legend_constants[3] = "q_ptEpi_W in component main (litre)"
    legend_algebraic[0] = "q_tot_W in component main (litre)"
    legend_states[4] = "q_vc_Na in component main (mole)"
    legend_states[5] = "q_gi_Na in component main (mole)"
    legend_states[6] = "q_pt_Na in component main (mole)"
    legend_states[7] = "q_giEpi_Na in component main (mole)"
    legend_states[8] = "q_ptEpi_Na in component main (mole)"
    legend_constants[4] = "q_vc_K in component main (mole)"
    legend_constants[5] = "q_giEpi_K in component main (mole)"
    legend_constants[6] = "q_ptEpi_K in component main (mole)"
    legend_algebraic[1] = "q_tot_Na in component main (mole)"
    legend_algebraic[2] = "c_vc_Na in component main (mol_per_L)"
    legend_algebraic[11] = "c_gi_Na in component main (mol_per_L)"
    legend_algebraic[16] = "c_pt_Na in component main (mol_per_L)"
    legend_algebraic[3] = "c_giEpi_Na in component main (mol_per_L)"
    legend_algebraic[4] = "c_ptEpi_Na in component main (mol_per_L)"
    legend_algebraic[12] = "C_vc_Na in component main (mM)"
    legend_algebraic[17] = "C_gi_Na in component main (mM)"
    legend_algebraic[20] = "C_pt_Na in component main (mM)"
    legend_algebraic[13] = "C_giEpi_Na in component main (mM)"
    legend_algebraic[14] = "C_ptEpi_Na in component main (mM)"
    legend_constants[7] = "v_lv_W in component main (L_per_s)"
    legend_constants[8] = "v_in_W in component main (L_per_s)"
    legend_constants[9] = "v_out1_W in component main (L_per_s)"
    legend_constants[10] = "v_out2_W in component main (L_per_s)"
    legend_algebraic[21] = "v_gl_W in component main (L_per_s)"
    legend_algebraic[24] = "v_gi_W in component main (L_per_s)"
    legend_algebraic[26] = "v_pt_W in component main (L_per_s)"
    legend_algebraic[18] = "v_cc_W in component main (L_per_s)"
    legend_constants[11] = "v_in_Na in component main (mol_per_s)"
    legend_constants[12] = "v_out1_Na in component main (mol_per_s)"
    legend_constants[13] = "v_out2_Na in component main (mol_per_s)"
    legend_algebraic[5] = "v_gl_Na in component main (mol_per_s)"
    legend_algebraic[6] = "v_gi_Na in component main (mol_per_s)"
    legend_algebraic[7] = "v_pt_Na in component main (mol_per_s)"
    legend_algebraic[8] = "v_giEpi_NKE in component main (mol_per_s)"
    legend_algebraic[9] = "v_ptEpi_NKE in component main (mol_per_s)"
    legend_algebraic[10] = "u_vc_W in component main (kPa)"
    legend_algebraic[15] = "u_ac_W in component main (kPa)"
    legend_algebraic[19] = "u_pt_W in component main (kPa)"
    legend_algebraic[23] = "u_vc_osmotic in component main (kPa)"
    legend_algebraic[22] = "u_gi_osmotic in component main (kPa)"
    legend_algebraic[25] = "u_pt_osmotic in component main (kPa)"
    legend_constants[14] = "u_giEpi_e in component main (J_per_C)"
    legend_constants[15] = "u_ptEpi_e in component main (J_per_C)"
    legend_constants[16] = "k_gi_W in component main (L_per_s_per_kPa)"
    legend_constants[17] = "k_pt_W in component main (L_per_s_per_kPa)"
    legend_constants[18] = "k_gl_W in component main (L_per_s_per_kPa)"
    legend_constants[19] = "k_cc_W in component main (L_per_s_per_kPa)"
    legend_constants[20] = "kK_gi_Na in component main (per_s)"
    legend_constants[21] = "kK_pt_Na in component main (per_s)"
    legend_constants[22] = "kK_gl_Na in component main (per_s)"
    legend_constants[23] = "k_giEpi_NKE in component main (mol_per_s)"
    legend_constants[24] = "k_ptEpi_NKE in component main (mol_per_s)"
    legend_constants[25] = "K_Na in component main (L_per_mol)"
    legend_constants[26] = "K_K in component main (L_per_mol)"
    legend_constants[27] = "E_vc in component main (joule)"
    legend_constants[28] = "E_ac in component main (joule)"
    legend_constants[29] = "E_pt in component main (joule)"
    legend_constants[30] = "U_ac_W in component main (litre)"
    legend_constants[31] = "U_vc_W in component main (litre)"
    legend_constants[32] = "U_pt_W in component main (litre)"
    legend_constants[33] = "L_ac_W in component main (litre)"
    legend_constants[34] = "L_vc_W in component main (litre)"
    legend_constants[35] = "L_pt_W in component main (litre)"
    legend_rates[2] = "d/dt q_gi_W in component main (litre)"
    legend_rates[3] = "d/dt q_pt_W in component main (litre)"
    legend_rates[1] = "d/dt q_vc_W in component main (litre)"
    legend_rates[0] = "d/dt q_ac_W in component main (litre)"
    legend_rates[5] = "d/dt q_gi_Na in component main (mole)"
    legend_rates[7] = "d/dt q_giEpi_Na in component main (mole)"
    legend_rates[6] = "d/dt q_pt_Na in component main (mole)"
    legend_rates[8] = "d/dt q_ptEpi_Na in component main (mole)"
    legend_rates[4] = "d/dt q_vc_Na in component main (mole)"
    return (legend_states, legend_algebraic, legend_voi, legend_constants)

def initConsts():
    constants = [0.0] * sizeConstants; states = [0.0] * sizeStates;
    constants[0] = 2.5e3
    constants[1] = 0.965e5
    states[0] = 1
    states[1] = 2
    states[2] = 1
    states[3] = 0.1
    constants[2] = 0.5
    constants[3] = 0.5
    states[4] = 0.84
    states[5] = 0.1
    states[6] = 0.01
    states[7] = 0.012
    states[8] = 0.012
    constants[4] = 0.03
    constants[5] = 0.012
    constants[6] = 0.012
    constants[7] = 0.1
    constants[8] = 0
    constants[9] = 0
    constants[10] = 0
    constants[11] = 1.16e-6
    constants[12] = 0
    constants[13] = 1.16e-6
    constants[14] = -0.040
    constants[15] = -0.040
    constants[16] = 1
    constants[17] = 0
    constants[18] = 1
    constants[19] = 1
    constants[20] = 1
    constants[21] = 1
    constants[22] = 1
    constants[23] = 1e4
    constants[24] = 1e5
    constants[25] = 1
    constants[26] = 1
    constants[27] = 6e1
    constants[28] = 6e1
    constants[29] = 1
    constants[30] = 0.5
    constants[31] = 1.5
    constants[32] = 0.1
    constants[33] = 1.5
    constants[34] = 2.5
    constants[35] = 1
    return (states, constants)

def computeRates(voi, states, constants):
    rates = [0.0] * sizeStates; algebraic = [0.0] * sizeAlgebraic
    rates[2] = (constants[8]-constants[0]*constants[16]*(states[4]/states[1]-states[5]/states[2]))-constants[9]
    rates[3] = (constants[18]*((constants[28]*(states[0]-constants[30]))/(power(constants[33]-states[0], 2.00000))-(constants[29]*(states[3]-constants[32]))/(power(constants[35]-states[3], 2.00000)))-constants[0]*constants[17]*(states[4]/states[1]-states[6]/states[3]))-constants[10]
    rates[1] = (constants[19]*((constants[28]*(states[0]-constants[30]))/(power(constants[33]-states[0], 2.00000))-(constants[27]*(states[1]-constants[31]))/(power(constants[34]-states[1], 2.00000)))+constants[0]*constants[16]*(states[4]/states[1]-states[5]/states[2])+constants[0]*constants[17]*(states[4]/states[1]-states[6]/states[3]))-constants[7]
    rates[0] = ((constants[7]+(constants[19]*constants[27]*(states[1]-constants[31]))/(power(constants[34]-states[1], 2.00000)))-((constants[18]+constants[19])*constants[28]*(states[0]-constants[30]))/(power(constants[33]-states[0], 2.00000)))+(constants[18]*constants[29]*(states[3]-constants[32]))/(power(constants[35]-states[3], 2.00000))
    rates[5] = (constants[11]-constants[20]*(states[5]-states[7]))-constants[12]
    rates[7] = constants[20]*(states[5]-states[7])-(3.00000*constants[23]*((power((constants[25]*states[7])/constants[2], 3.00000))*(power((constants[26]*constants[4])/states[1], 2.00000))-(power((constants[25]*states[4])/states[1], 3.00000))*(power((constants[26]*constants[5])/constants[2], 2.00000))*exp((2.00000*constants[1]*constants[14])/constants[0])))/((1.00000+power((constants[25]*states[7])/constants[2], 3.00000))*(1.00000+power((constants[25]*states[4])/states[1], 3.00000))*(1.00000+power((constants[26]*constants[4])/states[1], 2.00000))*(1.00000+power((constants[26]*constants[5])/constants[2], 2.00000)))
    rates[6] = (constants[22]*(states[4]-states[6])-constants[21]*(states[6]-states[8]))-constants[13]
    rates[8] = constants[21]*(states[6]-states[8])-(3.00000*constants[24]*((power((constants[25]*states[8])/constants[3], 3.00000))*(power((constants[26]*constants[4])/states[1], 2.00000))-(power((constants[25]*states[4])/states[1], 3.00000))*(power((constants[26]*constants[6])/constants[3], 2.00000))*exp((2.00000*constants[1]*constants[15])/constants[0])))/((1.00000+power((constants[25]*states[8])/constants[3], 3.00000))*(1.00000+power((constants[25]*states[4])/states[1], 3.00000))*(1.00000+power((constants[26]*constants[4])/states[1], 2.00000))*(1.00000+power((constants[26]*constants[6])/constants[3], 2.00000)))
    rates[4] = ((3.00000*constants[23]*((power((constants[25]*states[7])/constants[2], 3.00000))*(power((constants[26]*constants[4])/states[1], 2.00000))-(power((constants[25]*states[4])/states[1], 3.00000))*(power((constants[26]*constants[5])/constants[2], 2.00000))*exp((2.00000*constants[1]*constants[14])/constants[0])))/((1.00000+power((constants[25]*states[7])/constants[2], 3.00000))*(1.00000+power((constants[25]*states[4])/states[1], 3.00000))*(1.00000+power((constants[26]*constants[4])/states[1], 2.00000))*(1.00000+power((constants[26]*constants[5])/constants[2], 2.00000)))+(3.00000*constants[24]*((power((constants[25]*states[8])/constants[3], 3.00000))*(power((constants[26]*constants[4])/states[1], 2.00000))-(power((constants[25]*states[4])/states[1], 3.00000))*(power((constants[26]*constants[6])/constants[3], 2.00000))*exp((2.00000*constants[1]*constants[15])/constants[0])))/((1.00000+power((constants[25]*states[8])/constants[3], 3.00000))*(1.00000+power((constants[25]*states[4])/states[1], 3.00000))*(1.00000+power((constants[26]*constants[4])/states[1], 2.00000))*(1.00000+power((constants[26]*constants[6])/constants[3], 2.00000))))-constants[22]*(states[4]-states[6])
    return(rates)

def computeAlgebraic(constants, states, voi):
    algebraic = array([[0.0] * len(voi)] * sizeAlgebraic)
    states = array(states)
    voi = array(voi)
    algebraic[0] = states[0]+states[1]+states[2]+states[3]+constants[2]+constants[3]
    algebraic[1] = states[4]+states[5]+states[6]+states[7]+states[8]
    algebraic[2] = states[4]/states[1]
    algebraic[3] = states[7]/constants[2]
    algebraic[4] = states[8]/constants[3]
    algebraic[5] = constants[22]*(states[4]-states[6])
    algebraic[6] = constants[20]*(states[5]-states[7])
    algebraic[7] = constants[21]*(states[6]-states[8])
    algebraic[8] = (3.00000*constants[23]*((power((constants[25]*states[7])/constants[2], 3.00000))*(power((constants[26]*constants[4])/states[1], 2.00000))-(power((constants[25]*states[4])/states[1], 3.00000))*(power((constants[26]*constants[5])/constants[2], 2.00000))*exp((2.00000*constants[1]*constants[14])/constants[0])))/((1.00000+power((constants[25]*states[7])/constants[2], 3.00000))*(1.00000+power((constants[25]*states[4])/states[1], 3.00000))*(1.00000+power((constants[26]*constants[4])/states[1], 2.00000))*(1.00000+power((constants[26]*constants[5])/constants[2], 2.00000)))
    algebraic[9] = (3.00000*constants[24]*((power((constants[25]*states[8])/constants[3], 3.00000))*(power((constants[26]*constants[4])/states[1], 2.00000))-(power((constants[25]*states[4])/states[1], 3.00000))*(power((constants[26]*constants[6])/constants[3], 2.00000))*exp((2.00000*constants[1]*constants[15])/constants[0])))/((1.00000+power((constants[25]*states[8])/constants[3], 3.00000))*(1.00000+power((constants[25]*states[4])/states[1], 3.00000))*(1.00000+power((constants[26]*constants[4])/states[1], 2.00000))*(1.00000+power((constants[26]*constants[6])/constants[3], 2.00000)))
    algebraic[10] = (constants[27]*(states[1]-constants[31]))/(power(constants[34]-states[1], 2.00000))
    algebraic[11] = states[5]/states[2]
    algebraic[12] = 1000.00*algebraic[2]
    algebraic[13] = 1000.00*algebraic[3]
    algebraic[14] = 1000.00*algebraic[4]
    algebraic[15] = (constants[28]*(states[0]-constants[30]))/(power(constants[33]-states[0], 2.00000))
    algebraic[16] = states[6]/states[3]
    algebraic[17] = 1000.00*algebraic[11]
    algebraic[18] = constants[19]*(algebraic[15]-algebraic[10])
    algebraic[19] = (constants[29]*(states[3]-constants[32]))/(power(constants[35]-states[3], 2.00000))
    algebraic[20] = 1000.00*algebraic[16]
    algebraic[21] = constants[18]*(algebraic[15]-algebraic[19])
    algebraic[22] = constants[0]*algebraic[11]
    algebraic[23] = constants[0]*algebraic[2]
    algebraic[24] = constants[16]*(algebraic[23]-algebraic[22])
    algebraic[25] = constants[0]*algebraic[16]
    algebraic[26] = constants[17]*(algebraic[23]-algebraic[25])
    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)