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 = 16
sizeStates = 5
sizeConstants = 35
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 time_s (second)"
    legend_algebraic[8] = "V in component Vm (mV)"
    legend_algebraic[2] = "p1 in component Vm (mV_per_s)"
    legend_algebraic[4] = "p2 in component Vm (mV)"
    legend_algebraic[6] = "t0 in component Vm (second)"
    legend_constants[0] = "tss in component Vm (second)"
    legend_constants[1] = "Nai in component control_para (mM)"
    legend_constants[2] = "inhPump in component control_para (dimensionless)"
    legend_constants[3] = "K_Cahalf in component control_para (mV)"
    legend_states[0] = "Cai in component Cai (mM)"
    legend_algebraic[15] = "J_VOCC in component J_VOCC (mM_per_s)"
    legend_algebraic[10] = "J_CaPump in component J_CaPump (mM_per_s)"
    legend_algebraic[13] = "J_NaCa in component J_NaCa (mM_per_s)"
    legend_algebraic[0] = "stress in component CB4HM (dimensionless)"
    legend_algebraic[1] = "phosphorylation in component CB4HM (dimensionless)"
    legend_constants[4] = "R in component constants (J_per_K_mol)"
    legend_constants[5] = "F in component constants (C_per_mmol)"
    legend_constants[6] = "T in component model_para (kelvin)"
    legend_constants[7] = "Nao in component model_para (mM)"
    legend_constants[8] = "Cao in component model_para (mM)"
    legend_constants[9] = "V_cell in component model_para (fm3)"
    legend_constants[10] = "V_Cahalf in component model_para (mV)"
    legend_constants[11] = "g_mCa in component model_para (nS)"
    legend_constants[12] = "V_pmax in component model_para (mM_per_s)"
    legend_constants[13] = "n in component model_para (dimensionless)"
    legend_constants[14] = "K_ph in component model_para (mM)"
    legend_constants[15] = "K_NaCa in component model_para (mM)"
    legend_constants[16] = "G_NaCa in component model_para (mM_per_s_mV)"
    legend_constants[17] = "Cai_init in component initials (mM)"
    legend_constants[18] = "n_M in component model_para (dimensionless)"
    legend_constants[19] = "Ca_halfMLCK in component model_para (mM)"
    legend_constants[20] = "M_init in component initials (dimensionless)"
    legend_constants[21] = "Mp_init in component initials (dimensionless)"
    legend_constants[22] = "AM_init in component initials (dimensionless)"
    legend_constants[23] = "AMp_init in component initials (dimensionless)"
    legend_constants[24] = "K_7 in component model_para (per_s)"
    legend_constants[25] = "K_2 in component model_para (per_s)"
    legend_constants[26] = "K_3 in component model_para (per_s)"
    legend_algebraic[9] = "rho_vCa in component J_VOCC (dimensionless)"
    legend_constants[27] = "Nai in component model_para (mM)"
    legend_constants[28] = "K_Cahalf in component model_para (mV)"
    legend_constants[29] = "inhPump in component model_para (dimensionless)"
    legend_constants[30] = "z_Ca in component E_Ca (dimensionless)"
    legend_algebraic[11] = "E in component Nernst_potential (mV)"
    legend_constants[31] = "z_Na in component E_Na (dimensionless)"
    legend_constants[32] = "E in component Nernst_potential (mV)"
    legend_algebraic[14] = "I in component Ionic_currents (pA)"
    legend_algebraic[12] = "V_mNaCa in component J_NaCa (mV)"
    legend_algebraic[5] = "K_1 in component K_1 (per_s)"
    legend_constants[33] = "K_4 in component CB4HM (per_s)"
    legend_constants[34] = "K_5 in component CB4HM (per_s)"
    legend_algebraic[7] = "K_6 in component CB4HM (per_s)"
    legend_algebraic[3] = "norm in component CB4HM (dimensionless)"
    legend_states[1] = "M in component CB4HM (dimensionless)"
    legend_states[2] = "Mp in component CB4HM (dimensionless)"
    legend_states[3] = "AM in component CB4HM (dimensionless)"
    legend_states[4] = "AMp in component CB4HM (dimensionless)"
    legend_rates[0] = "d/dt Cai in component Cai (mM)"
    legend_rates[1] = "d/dt M in component CB4HM (dimensionless)"
    legend_rates[2] = "d/dt Mp in component CB4HM (dimensionless)"
    legend_rates[3] = "d/dt AM in component CB4HM (dimensionless)"
    legend_rates[4] = "d/dt AMp in component CB4HM (dimensionless)"
    return (legend_states, legend_algebraic, legend_voi, legend_constants)

def initConsts():
    constants = [0.0] * sizeConstants; states = [0.0] * sizeStates;
    constants[0] = 0
    constants[1] = 2.9836
    constants[2] = 1
    constants[3] = 11
    constants[4] = 8.314
    constants[5] = 96.48534
    constants[6] = 310
    constants[7] = 140
    constants[8] = 2
    constants[9] = 21
    constants[10] = -27
    constants[11] = 0.046842
    constants[12] = 5.1449e-4
    constants[13] = 1.9015
    constants[14] = 0.6e-3
    constants[15] = 7e-3
    constants[16] = 5.7297e-5
    constants[17] = 0.1e-6
    constants[18] = 8.7613
    constants[19] = 256.98e-6
    constants[20] = 1
    constants[21] = 0
    constants[22] = 0
    constants[23] = 0
    constants[24] = 0.0378
    constants[25] = 1.2387
    constants[26] = 0.1419
    constants[27] = 2.9836
    constants[28] = 11
    constants[29] = 1
    constants[30] = 2
    constants[31] = 1
    constants[32] = ((constants[4]*constants[6])/(constants[31]*constants[5]))*log(constants[7]/constants[1])
    constants[33] = constants[26]/4.00000
    constants[34] = constants[25]
    states[0] = constants[17]
    states[1] = constants[20]
    states[2] = constants[21]
    states[3] = constants[22]
    states[4] = constants[23]
    return (states, constants)

def computeRates(voi, states, constants):
    rates = [0.0] * sizeStates; algebraic = [0.0] * sizeAlgebraic
    algebraic[5] = ((power(states[0], constants[18]))/(power(constants[19], constants[18])+power(states[0], constants[18])))*1.00000
    algebraic[3] = states[1]+states[2]+states[3]+states[4]
    rates[1] = (-algebraic[5]*states[1])/algebraic[3]+(constants[25]*states[2])/algebraic[3]+(constants[24]*states[3])/algebraic[3]
    rates[2] = ((constants[33]*states[4])/algebraic[3]+(algebraic[5]*states[1])/algebraic[3])-((constants[25]+constants[26])*states[2])/algebraic[3]
    algebraic[7] = algebraic[5]
    rates[3] = (constants[34]*states[4])/algebraic[3]-((algebraic[7]+constants[24])*states[3])/algebraic[3]
    rates[4] = ((constants[26]*states[2])/algebraic[3]+(algebraic[7]*states[3])/algebraic[3])-((constants[33]+constants[34])*states[4])/algebraic[3]
    algebraic[2] = custom_piecewise([less(voi-constants[0] , 0.474200), 0.00000 , greater_equal(voi-constants[0] , 0.474200) & less(voi-constants[0] , 9.76840), 0.250800 , greater_equal(voi-constants[0] , 9.76840) & less(voi-constants[0] , 10.8076), -4.29660 , greater_equal(voi-constants[0] , 10.8076) & less(voi-constants[0] , 12.0313), 3.62830 , greater_equal(voi-constants[0] , 12.0313) & less(voi-constants[0] , 18.3268), 6.71270 , greater_equal(voi-constants[0] , 18.3268) & less(voi-constants[0] , 19.7879), 3.61510 , greater_equal(voi-constants[0] , 19.7879) & less(voi-constants[0] , 21.9031), -6.84810 , greater_equal(voi-constants[0] , 21.9031) & less(voi-constants[0] , 55.9040), -0.0369000 , greater_equal(voi-constants[0] , 55.9040) & less(voi-constants[0] , 56.2152), -66.1632 , greater_equal(voi-constants[0] , 56.2152) & less(voi-constants[0] , 62.2548), -1.44560 , greater_equal(voi-constants[0] , 62.2548) & less(voi-constants[0] , 82.4301), -0.155700 , greater_equal(voi-constants[0] , 82.4301) & less(voi-constants[0] , 113.961), -0.0623000 , True, 0.00000])
    algebraic[4] = custom_piecewise([less(voi-constants[0] , 0.474200), -63.0930 , greater_equal(voi-constants[0] , 0.474200) & less(voi-constants[0] , 9.76840), -63.0930 , greater_equal(voi-constants[0] , 9.76840) & less(voi-constants[0] , 10.8076), -60.7620 , greater_equal(voi-constants[0] , 10.8076) & less(voi-constants[0] , 12.0313), -65.2270 , greater_equal(voi-constants[0] , 12.0313) & less(voi-constants[0] , 18.3268), -60.7870 , greater_equal(voi-constants[0] , 18.3268) & less(voi-constants[0] , 19.7879), -18.5270 , greater_equal(voi-constants[0] , 19.7879) & less(voi-constants[0] , 21.9031), -13.2450 , greater_equal(voi-constants[0] , 21.9031) & less(voi-constants[0] , 55.9040), -27.7300 , greater_equal(voi-constants[0] , 55.9040) & less(voi-constants[0] , 56.2152), -28.9850 , greater_equal(voi-constants[0] , 56.2152) & less(voi-constants[0] , 62.2548), -49.5750 , greater_equal(voi-constants[0] , 62.2548) & less(voi-constants[0] , 82.4301), -58.3060 , greater_equal(voi-constants[0] , 82.4301) & less(voi-constants[0] , 113.961), -61.4480 , True, -63.4110])
    algebraic[6] = custom_piecewise([less(voi-constants[0] , 0.474200), 0.00000 , greater_equal(voi-constants[0] , 0.474200) & less(voi-constants[0] , 9.76840), 0.474200 , greater_equal(voi-constants[0] , 9.76840) & less(voi-constants[0] , 10.8076), 9.76840 , greater_equal(voi-constants[0] , 10.8076) & less(voi-constants[0] , 12.0313), 10.8076 , greater_equal(voi-constants[0] , 12.0313) & less(voi-constants[0] , 18.3268), 12.0313 , greater_equal(voi-constants[0] , 18.3268) & less(voi-constants[0] , 19.7879), 18.3268 , greater_equal(voi-constants[0] , 19.7879) & less(voi-constants[0] , 21.9031), 19.7879 , greater_equal(voi-constants[0] , 21.9031) & less(voi-constants[0] , 55.9040), 21.9031 , greater_equal(voi-constants[0] , 55.9040) & less(voi-constants[0] , 56.2151), 55.9040 , greater_equal(voi-constants[0] , 56.2151) & less(voi-constants[0] , 62.2548), 56.2151 , greater_equal(voi-constants[0] , 62.2548) & less(voi-constants[0] , 82.4301), 62.2548 , greater_equal(voi-constants[0] , 82.4301) & less(voi-constants[0] , 113.961), 82.4299 , True, 0.00000])
    algebraic[8] = algebraic[2]*((voi-algebraic[6])-constants[0])+algebraic[4]
    algebraic[9] = 1.00000/(1.00000+exp((constants[10]-algebraic[8])/constants[3]))
    algebraic[11] = ((constants[4]*constants[6])/(constants[30]*constants[5]))*log(constants[8]/states[0])
    algebraic[14] = constants[11]*algebraic[9]*(algebraic[8]-algebraic[11])
    algebraic[15] = -algebraic[14]/(2.00000*constants[9]*constants[5])
    algebraic[10] = (-constants[12]*(power(states[0], constants[13])))/(power(constants[14], constants[13])+power(states[0], constants[13]))
    algebraic[12] = 3.00000*constants[32]-2.00000*algebraic[11]
    algebraic[13] = ((constants[16]*states[0])/(states[0]+constants[15]))*(algebraic[8]-algebraic[12])
    rates[0] = algebraic[15]+constants[2]*algebraic[10]+algebraic[13]
    return(rates)

def computeAlgebraic(constants, states, voi):
    algebraic = array([[0.0] * len(voi)] * sizeAlgebraic)
    states = array(states)
    voi = array(voi)
    algebraic[5] = ((power(states[0], constants[18]))/(power(constants[19], constants[18])+power(states[0], constants[18])))*1.00000
    algebraic[3] = states[1]+states[2]+states[3]+states[4]
    algebraic[7] = algebraic[5]
    algebraic[2] = custom_piecewise([less(voi-constants[0] , 0.474200), 0.00000 , greater_equal(voi-constants[0] , 0.474200) & less(voi-constants[0] , 9.76840), 0.250800 , greater_equal(voi-constants[0] , 9.76840) & less(voi-constants[0] , 10.8076), -4.29660 , greater_equal(voi-constants[0] , 10.8076) & less(voi-constants[0] , 12.0313), 3.62830 , greater_equal(voi-constants[0] , 12.0313) & less(voi-constants[0] , 18.3268), 6.71270 , greater_equal(voi-constants[0] , 18.3268) & less(voi-constants[0] , 19.7879), 3.61510 , greater_equal(voi-constants[0] , 19.7879) & less(voi-constants[0] , 21.9031), -6.84810 , greater_equal(voi-constants[0] , 21.9031) & less(voi-constants[0] , 55.9040), -0.0369000 , greater_equal(voi-constants[0] , 55.9040) & less(voi-constants[0] , 56.2152), -66.1632 , greater_equal(voi-constants[0] , 56.2152) & less(voi-constants[0] , 62.2548), -1.44560 , greater_equal(voi-constants[0] , 62.2548) & less(voi-constants[0] , 82.4301), -0.155700 , greater_equal(voi-constants[0] , 82.4301) & less(voi-constants[0] , 113.961), -0.0623000 , True, 0.00000])
    algebraic[4] = custom_piecewise([less(voi-constants[0] , 0.474200), -63.0930 , greater_equal(voi-constants[0] , 0.474200) & less(voi-constants[0] , 9.76840), -63.0930 , greater_equal(voi-constants[0] , 9.76840) & less(voi-constants[0] , 10.8076), -60.7620 , greater_equal(voi-constants[0] , 10.8076) & less(voi-constants[0] , 12.0313), -65.2270 , greater_equal(voi-constants[0] , 12.0313) & less(voi-constants[0] , 18.3268), -60.7870 , greater_equal(voi-constants[0] , 18.3268) & less(voi-constants[0] , 19.7879), -18.5270 , greater_equal(voi-constants[0] , 19.7879) & less(voi-constants[0] , 21.9031), -13.2450 , greater_equal(voi-constants[0] , 21.9031) & less(voi-constants[0] , 55.9040), -27.7300 , greater_equal(voi-constants[0] , 55.9040) & less(voi-constants[0] , 56.2152), -28.9850 , greater_equal(voi-constants[0] , 56.2152) & less(voi-constants[0] , 62.2548), -49.5750 , greater_equal(voi-constants[0] , 62.2548) & less(voi-constants[0] , 82.4301), -58.3060 , greater_equal(voi-constants[0] , 82.4301) & less(voi-constants[0] , 113.961), -61.4480 , True, -63.4110])
    algebraic[6] = custom_piecewise([less(voi-constants[0] , 0.474200), 0.00000 , greater_equal(voi-constants[0] , 0.474200) & less(voi-constants[0] , 9.76840), 0.474200 , greater_equal(voi-constants[0] , 9.76840) & less(voi-constants[0] , 10.8076), 9.76840 , greater_equal(voi-constants[0] , 10.8076) & less(voi-constants[0] , 12.0313), 10.8076 , greater_equal(voi-constants[0] , 12.0313) & less(voi-constants[0] , 18.3268), 12.0313 , greater_equal(voi-constants[0] , 18.3268) & less(voi-constants[0] , 19.7879), 18.3268 , greater_equal(voi-constants[0] , 19.7879) & less(voi-constants[0] , 21.9031), 19.7879 , greater_equal(voi-constants[0] , 21.9031) & less(voi-constants[0] , 55.9040), 21.9031 , greater_equal(voi-constants[0] , 55.9040) & less(voi-constants[0] , 56.2151), 55.9040 , greater_equal(voi-constants[0] , 56.2151) & less(voi-constants[0] , 62.2548), 56.2151 , greater_equal(voi-constants[0] , 62.2548) & less(voi-constants[0] , 82.4301), 62.2548 , greater_equal(voi-constants[0] , 82.4301) & less(voi-constants[0] , 113.961), 82.4299 , True, 0.00000])
    algebraic[8] = algebraic[2]*((voi-algebraic[6])-constants[0])+algebraic[4]
    algebraic[9] = 1.00000/(1.00000+exp((constants[10]-algebraic[8])/constants[3]))
    algebraic[11] = ((constants[4]*constants[6])/(constants[30]*constants[5]))*log(constants[8]/states[0])
    algebraic[14] = constants[11]*algebraic[9]*(algebraic[8]-algebraic[11])
    algebraic[15] = -algebraic[14]/(2.00000*constants[9]*constants[5])
    algebraic[10] = (-constants[12]*(power(states[0], constants[13])))/(power(constants[14], constants[13])+power(states[0], constants[13]))
    algebraic[12] = 3.00000*constants[32]-2.00000*algebraic[11]
    algebraic[13] = ((constants[16]*states[0])/(states[0]+constants[15]))*(algebraic[8]-algebraic[12])
    algebraic[0] = states[4]+states[3]
    algebraic[1] = states[4]+states[2]
    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)