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 = 32
sizeStates = 9
sizeConstants = 42
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] = "pulse_time in component main (second)"
    legend_constants[1] = "RT in component main (J_per_mol)"
    legend_constants[2] = "F in component main (C_per_mol)"
    legend_constants[3] = "q_w_gut in component main (litre)"
    legend_constants[4] = "q_w_epi in component main (litre)"
    legend_constants[5] = "q_w_cap in component main (litre)"
    legend_constants[36] = "q_w_tot in component main (litre)"
    legend_states[0] = "q_Na_gut in component main (mole)"
    legend_states[1] = "q_Na_epi in component main (mole)"
    legend_algebraic[0] = "q_Na_cap in component main (mole)"
    legend_constants[6] = "q_Na_cap_init in component main (mole)"
    legend_constants[7] = "q_Na_cap_scale in component main (dimensionless)"
    legend_algebraic[2] = "c_Na_gut in component main (mM)"
    legend_algebraic[4] = "c_Na_epi in component main (mM)"
    legend_algebraic[5] = "c_Na_cap in component main (mM)"
    legend_constants[8] = "K_mm in component main (millimols_per_mol)"
    legend_algebraic[3] = "q_Na_tot in component main (mole)"
    legend_states[2] = "q_K_epi in component main (mole)"
    legend_constants[9] = "q_K_cap in component main (mole)"
    legend_algebraic[7] = "c_K_epi in component main (mM)"
    legend_constants[37] = "c_K_cap in component main (mM)"
    legend_states[3] = "q_Cl_epi in component main (mole)"
    legend_constants[10] = "q_Cl_cap in component main (mole)"
    legend_algebraic[10] = "c_Cl_epi in component main (mM)"
    legend_constants[38] = "c_Cl_cap in component main (mM)"
    legend_states[4] = "q_e in component main (coulomb)"
    legend_constants[11] = "u_ee in component main (J_per_C)"
    legend_algebraic[13] = "u_e in component main (J_per_C)"
    legend_constants[12] = "C_m in component main (C2_per_J)"
    legend_algebraic[14] = "q_Glc_gut in component main (mole)"
    legend_states[5] = "q_Glc_epi in component main (mole)"
    legend_constants[13] = "q_Glc_cap in component main (mole)"
    legend_constants[14] = "q_Glc_gut_init in component main (mole)"
    legend_constants[15] = "q_Glc_gut_scale in component main (dimensionless)"
    legend_algebraic[15] = "c_Glc_gut in component main (mM)"
    legend_algebraic[16] = "c_Glc_epi in component main (mM)"
    legend_constants[39] = "c_Glc_cap in component main (mM)"
    legend_constants[16] = "u_w_gut in component main (kPa)"
    legend_constants[40] = "u_w_epi in component main (kPa)"
    legend_constants[17] = "u_w_cap in component main (kPa)"
    legend_constants[18] = "E_w_epi in component main (joule)"
    legend_constants[19] = "U_w_epi in component main (litre)"
    legend_constants[20] = "L_w_epi in component main (litre)"
    legend_algebraic[6] = "v_w_epiApex in component main (L_per_s)"
    legend_algebraic[8] = "v_w_epiBase in component main (L_per_s)"
    legend_constants[21] = "k_w_m in component main (L_per_s_per_kPa)"
    legend_constants[22] = "c_O2 in component main (mM)"
    legend_constants[23] = "c_CO2 in component main (mM)"
    legend_constants[24] = "c_HCO3 in component main (mM)"
    legend_states[6] = "q_ATP in component main (mole)"
    legend_states[7] = "q_ADP in component main (mole)"
    legend_states[8] = "q_Pi in component main (mole)"
    legend_constants[25] = "q_H in component main (mole)"
    legend_algebraic[1] = "q_adenosine in component main (mole)"
    legend_algebraic[17] = "c_ATP in component main (mM)"
    legend_algebraic[20] = "c_ADP in component main (mM)"
    legend_algebraic[21] = "c_Pi in component main (mM)"
    legend_constants[41] = "c_H in component main (mM)"
    legend_constants[26] = "kappa_SGLT1 in component main (mol_per_s)"
    legend_algebraic[22] = "v_SGLT1 in component main (mol_per_s)"
    legend_constants[27] = "kappa_GLUT2 in component main (mol_per_s)"
    legend_algebraic[24] = "v_GLUT2 in component main (mol_per_s)"
    legend_constants[28] = "k_ATP_eq in component main (mM2)"
    legend_constants[29] = "kappa_NKE in component main (mol_per_s)"
    legend_algebraic[25] = "v_NKE in component main (mol_per_s)"
    legend_constants[30] = "kappa_NKCC1 in component main (mol_per_s)"
    legend_algebraic[26] = "v_NKCC1 in component main (mol_per_s)"
    legend_constants[31] = "kappa_KCC1 in component main (mol_per_s)"
    legend_algebraic[28] = "v_KCC1 in component main (mol_per_s)"
    legend_constants[32] = "kappa_K in component main (C_per_J_per_s)"
    legend_algebraic[30] = "v_K in component main (mol_per_s)"
    legend_algebraic[29] = "GHKterm in component main (J_per_C)"
    legend_constants[33] = "kappa_Na in component main (per_s_per_V)"
    legend_algebraic[31] = "v_Na in component main (mol_per_s)"
    legend_constants[34] = "k_AM_eq in component main (per_mM)"
    legend_constants[35] = "kappa_AM in component main (mol_per_s)"
    legend_algebraic[27] = "v_AM in component main (mol_per_s)"
    legend_algebraic[18] = "u_SGLT1_reversal in component main (J_per_C)"
    legend_algebraic[11] = "u_K_reversal in component main (J_per_C)"
    legend_algebraic[9] = "u_Na_reversal in component main (J_per_C)"
    legend_algebraic[19] = "c_Na_gutThreshold in component main (mM)"
    legend_algebraic[23] = "c_Na_epiEquilibrium in component main (mM)"
    legend_algebraic[12] = "c_Cl_epiEquilibrium in component main (mM)"
    legend_rates[0] = "d/dt q_Na_gut in component main (mole)"
    legend_rates[1] = "d/dt q_Na_epi in component main (mole)"
    legend_rates[2] = "d/dt q_K_epi in component main (mole)"
    legend_rates[3] = "d/dt q_Cl_epi in component main (mole)"
    legend_rates[4] = "d/dt q_e in component main (coulomb)"
    legend_rates[5] = "d/dt q_Glc_epi in component main (mole)"
    legend_rates[6] = "d/dt q_ATP in component main (mole)"
    legend_rates[7] = "d/dt q_ADP in component main (mole)"
    legend_rates[8] = "d/dt q_Pi 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] = 0.1
    constants[1] = 2.5e3
    constants[2] = 0.965e5
    constants[3] = 1
    constants[4] = 1
    constants[5] = 1
    states[0] = 0.1
    states[1] = 0.001
    constants[6] = 0.140
    constants[7] = 1.0
    constants[8] = 1.e3
    states[2] = 0.130
    constants[9] = 0.004
    states[3] = 0.001
    constants[10] = 0.100
    states[4] = 1e-5
    constants[11] = -0.08
    constants[12] = 5e4
    states[5] = 0.001
    constants[13] = 0.01
    constants[14] = 0.01
    constants[15] = 1.0
    constants[16] = 1
    constants[17] = 1
    constants[18] = 1
    constants[19] = 0.5
    constants[20] = 5
    constants[21] = 1e-4
    constants[22] = 9.25
    constants[23] = 1.20
    constants[24] = 25.0
    states[6] = 0.003
    states[7] = 0.0025
    states[8] = 0.003
    constants[25] = 1e-7
    constants[26] = 1e-2
    constants[27] = 1e1
    constants[28] = 2e4
    constants[29] = 1e2
    constants[30] = 1e-1
    constants[31] = 1e-1
    constants[32] = 2e3
    constants[33] = 0
    constants[34] = 1e0
    constants[35] = 1e1
    constants[36] = constants[3]+constants[4]+constants[5]
    constants[37] = (constants[9]*constants[8])/constants[5]
    constants[38] = (constants[10]*constants[8])/constants[5]
    constants[39] = (constants[13]*constants[8])/constants[5]
    constants[40] = (constants[18]*(constants[4]-constants[19]))/(power(constants[20]-constants[4], 2.00000))
    constants[41] = (constants[25]*constants[8])/constants[4]
    return (states, constants)

def computeRates(voi, states, constants):
    rates = [0.0] * sizeStates; algebraic = [0.0] * sizeAlgebraic
    algebraic[2] = (states[0]*constants[8])/constants[3]
    algebraic[4] = (states[1]*constants[8])/constants[4]
    algebraic[13] = states[4]/constants[12]
    algebraic[14] = custom_piecewise([greater(voi , constants[0]), constants[14]*constants[15] , True, constants[14]])
    algebraic[15] = (algebraic[14]*constants[8])/constants[3]
    algebraic[16] = (states[5]*constants[8])/constants[4]
    algebraic[22] = (constants[26]*(((power(algebraic[2]/algebraic[4], 2.00000))*algebraic[15])/algebraic[16]-exp((2.00000*constants[2]*algebraic[13])/constants[1])))/((1.00000+power(algebraic[2]/20.0000, 2.00000))*(1.00000+power(algebraic[4]/100.000, 2.00000)))
    rates[0] = -2.00000*algebraic[22]
    algebraic[24] = (constants[27]*(algebraic[16]/constants[39]-1.00000))/(1.00000+algebraic[16]/5.00000+constants[39]/10.0000+(algebraic[16]*constants[39])/100.000)
    algebraic[17] = (states[6]*constants[8])/constants[4]
    algebraic[20] = (states[7]*constants[8])/constants[4]
    algebraic[27] = constants[35]*((constants[34]*algebraic[16]*algebraic[20])/algebraic[17]-1.00000)
    rates[5] = (algebraic[22]-algebraic[24])-algebraic[27]
    algebraic[0] = custom_piecewise([greater(voi , constants[0]), constants[6]*constants[7] , True, constants[6]])
    algebraic[5] = (algebraic[0]*constants[8])/constants[5]
    algebraic[7] = (states[2]*constants[8])/constants[4]
    algebraic[21] = (states[8]*constants[8])/constants[4]
    algebraic[25] = (constants[29]*(((power(algebraic[4]/algebraic[5], 3.00000))*algebraic[17]*constants[28])/(algebraic[20]*algebraic[21]*constants[41])-(power(algebraic[7]/constants[37], 2.00000))*exp((constants[2]*algebraic[13])/constants[1])))/((1.00000+power(algebraic[4]/1.00000, 3.00000))*(1.00000+power(algebraic[5]/100.000, 3.00000)))
    rates[6] = -algebraic[25]+32.0000*algebraic[27]
    rates[7] = algebraic[25]-32.0000*algebraic[27]
    rates[8] = algebraic[25]-32.0000*algebraic[27]
    algebraic[10] = (states[3]*constants[8])/constants[4]
    algebraic[26] = (constants[30]*((((algebraic[5]/algebraic[4])*constants[37])/algebraic[7])*(power(constants[38]/algebraic[10], 2.00000))-1.00000))/((1.00000+algebraic[5]/70.0000)*(1.00000+algebraic[4]/5.00000)*(1.00000+constants[37]/2.00000)*(1.00000+algebraic[7]/60.0000)*(1.00000+power(constants[38]/50.0000, 2.00000))*(1.00000+power(algebraic[10]/10.0000, 2.00000)))
    algebraic[28] = (constants[31]*(((algebraic[7]/constants[37])*algebraic[10])/constants[38]-1.00000))/((1.00000+constants[38]/50.0000)*(1.00000+constants[37]/2.00000)*(1.00000+algebraic[10]/10.0000)*(1.00000+algebraic[7]/60.0000))
    rates[3] = 2.00000*algebraic[26]-algebraic[28]
    algebraic[29] = algebraic[13]/(exp((constants[2]*algebraic[13])/constants[1])-1.00000)
    algebraic[30] = constants[32]*(states[2]*exp((constants[2]*algebraic[13])/constants[1])-constants[9])*algebraic[29]
    rates[2] = ((2.00000*algebraic[25]-algebraic[30])+algebraic[26])-algebraic[28]
    algebraic[31] = constants[33]*(algebraic[0]-states[1]*exp((constants[2]*algebraic[13])/constants[1]))*algebraic[29]
    rates[1] = (2.00000*algebraic[22]-3.00000*algebraic[25])+algebraic[26]+algebraic[31]
    rates[4] = constants[2]*(((2.00000*algebraic[22]-algebraic[25])-algebraic[30])+algebraic[31])
    return(rates)

def computeAlgebraic(constants, states, voi):
    algebraic = array([[0.0] * len(voi)] * sizeAlgebraic)
    states = array(states)
    voi = array(voi)
    algebraic[2] = (states[0]*constants[8])/constants[3]
    algebraic[4] = (states[1]*constants[8])/constants[4]
    algebraic[13] = states[4]/constants[12]
    algebraic[14] = custom_piecewise([greater(voi , constants[0]), constants[14]*constants[15] , True, constants[14]])
    algebraic[15] = (algebraic[14]*constants[8])/constants[3]
    algebraic[16] = (states[5]*constants[8])/constants[4]
    algebraic[22] = (constants[26]*(((power(algebraic[2]/algebraic[4], 2.00000))*algebraic[15])/algebraic[16]-exp((2.00000*constants[2]*algebraic[13])/constants[1])))/((1.00000+power(algebraic[2]/20.0000, 2.00000))*(1.00000+power(algebraic[4]/100.000, 2.00000)))
    algebraic[24] = (constants[27]*(algebraic[16]/constants[39]-1.00000))/(1.00000+algebraic[16]/5.00000+constants[39]/10.0000+(algebraic[16]*constants[39])/100.000)
    algebraic[17] = (states[6]*constants[8])/constants[4]
    algebraic[20] = (states[7]*constants[8])/constants[4]
    algebraic[27] = constants[35]*((constants[34]*algebraic[16]*algebraic[20])/algebraic[17]-1.00000)
    algebraic[0] = custom_piecewise([greater(voi , constants[0]), constants[6]*constants[7] , True, constants[6]])
    algebraic[5] = (algebraic[0]*constants[8])/constants[5]
    algebraic[7] = (states[2]*constants[8])/constants[4]
    algebraic[21] = (states[8]*constants[8])/constants[4]
    algebraic[25] = (constants[29]*(((power(algebraic[4]/algebraic[5], 3.00000))*algebraic[17]*constants[28])/(algebraic[20]*algebraic[21]*constants[41])-(power(algebraic[7]/constants[37], 2.00000))*exp((constants[2]*algebraic[13])/constants[1])))/((1.00000+power(algebraic[4]/1.00000, 3.00000))*(1.00000+power(algebraic[5]/100.000, 3.00000)))
    algebraic[10] = (states[3]*constants[8])/constants[4]
    algebraic[26] = (constants[30]*((((algebraic[5]/algebraic[4])*constants[37])/algebraic[7])*(power(constants[38]/algebraic[10], 2.00000))-1.00000))/((1.00000+algebraic[5]/70.0000)*(1.00000+algebraic[4]/5.00000)*(1.00000+constants[37]/2.00000)*(1.00000+algebraic[7]/60.0000)*(1.00000+power(constants[38]/50.0000, 2.00000))*(1.00000+power(algebraic[10]/10.0000, 2.00000)))
    algebraic[28] = (constants[31]*(((algebraic[7]/constants[37])*algebraic[10])/constants[38]-1.00000))/((1.00000+constants[38]/50.0000)*(1.00000+constants[37]/2.00000)*(1.00000+algebraic[10]/10.0000)*(1.00000+algebraic[7]/60.0000))
    algebraic[29] = algebraic[13]/(exp((constants[2]*algebraic[13])/constants[1])-1.00000)
    algebraic[30] = constants[32]*(states[2]*exp((constants[2]*algebraic[13])/constants[1])-constants[9])*algebraic[29]
    algebraic[31] = constants[33]*(algebraic[0]-states[1]*exp((constants[2]*algebraic[13])/constants[1]))*algebraic[29]
    algebraic[1] = states[6]+states[7]
    algebraic[3] = states[0]+states[1]+algebraic[0]
    algebraic[6] = constants[21]*((constants[16]-constants[40])-(constants[1]/constants[8])*(algebraic[2]-algebraic[4]))
    algebraic[8] = constants[21]*((constants[40]-constants[17])-(constants[1]/constants[8])*(algebraic[4]-algebraic[5]))
    algebraic[9] = (constants[1]/constants[2])*log(algebraic[5]/algebraic[4])
    algebraic[11] = (constants[1]/constants[2])*log(constants[37]/algebraic[7])
    algebraic[12] = constants[38]*(power(((algebraic[5]/algebraic[4])*constants[37])/algebraic[7], 1.0/2))
    algebraic[18] = (constants[1]/(2.00000*constants[2]))*log(((power(algebraic[2]/algebraic[4], 2.00000))*algebraic[15])/algebraic[16])
    algebraic[19] = algebraic[4]*(power(algebraic[16]/algebraic[15], 1.0/2))*exp((constants[2]*algebraic[13])/constants[1])
    algebraic[23] = algebraic[5]*(power(((algebraic[20]/algebraic[17])*algebraic[21]*constants[41])/constants[28], 0.333340))*(power(algebraic[7]/constants[37], 0.666670))*exp((constants[2]*algebraic[13])/(3.00000*constants[1]))
    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)