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 = 14
sizeStates = 7
sizeConstants = 24
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 environment (second)"
    legend_algebraic[0] = "Vm in component membrane (millivolt)"
    legend_constants[0] = "Cm in component membrane (microfarad)"
    legend_algebraic[11] = "I_b in component I_b (nanoampere)"
    legend_algebraic[2] = "I_Kir in component I_Kir (nanoampere)"
    legend_algebraic[9] = "I_Shkr in component I_Shkr (nanoampere)"
    legend_algebraic[13] = "I_stim in component I_stim (nanoampere)"
    legend_states[0] = "VmReal in component membrane (millivolt)"
    legend_constants[1] = "GKir in component I_Kir (microsiemens)"
    legend_constants[2] = "aKir in component I_Kir (dimensionless)"
    legend_constants[3] = "bKir in component I_Kir (dimensionless)"
    legend_constants[23] = "EK in component I_Kir (millivolt)"
    legend_algebraic[1] = "OKir in component I_Kir (dimensionless)"
    legend_constants[4] = "R in component model_parameters (millijoule_per_kelvin_mole)"
    legend_constants[5] = "T in component model_parameters (kelvin)"
    legend_constants[6] = "F in component model_parameters (coulomb_per_mole)"
    legend_constants[7] = "Ko in component model_parameters (millimolar)"
    legend_constants[8] = "Ki in component model_parameters (millimolar)"
    legend_constants[9] = "PShkr in component I_Shkr (microlitre_per_second)"
    legend_states[1] = "C0ShkrReal in component I_Shkr (dimensionless)"
    legend_states[2] = "C1ShkrReal in component I_Shkr (dimensionless)"
    legend_states[3] = "C2ShkrReal in component I_Shkr (dimensionless)"
    legend_states[4] = "C3ShkrReal in component I_Shkr (dimensionless)"
    legend_states[5] = "C4ShkrReal in component I_Shkr (dimensionless)"
    legend_states[6] = "OShkrReal in component I_Shkr (dimensionless)"
    legend_algebraic[3] = "C0Shkr in component I_Shkr (dimensionless)"
    legend_algebraic[4] = "C1Shkr in component I_Shkr (dimensionless)"
    legend_algebraic[5] = "C2Shkr in component I_Shkr (dimensionless)"
    legend_algebraic[6] = "C3Shkr in component I_Shkr (dimensionless)"
    legend_algebraic[7] = "C4Shkr in component I_Shkr (dimensionless)"
    legend_algebraic[8] = "OShkr in component I_Shkr (dimensionless)"
    legend_algebraic[10] = "kv in component I_Shkr (first_order_rate_constant)"
    legend_algebraic[12] = "k_v in component I_Shkr (first_order_rate_constant)"
    legend_constants[10] = "kv0 in component I_Shkr (first_order_rate_constant)"
    legend_constants[11] = "zv in component I_Shkr (dimensionless)"
    legend_constants[12] = "k_v0 in component I_Shkr (first_order_rate_constant)"
    legend_constants[13] = "z_v in component I_Shkr (dimensionless)"
    legend_constants[14] = "ko in component I_Shkr (first_order_rate_constant)"
    legend_constants[15] = "k_o in component I_Shkr (first_order_rate_constant)"
    legend_constants[16] = "Gb in component I_b (microsiemens)"
    legend_constants[17] = "Eb in component I_b (millivolt)"
    legend_constants[18] = "stim_start in component I_stim (second)"
    legend_constants[19] = "stim_end in component I_stim (second)"
    legend_constants[20] = "stim_period in component I_stim (second)"
    legend_constants[21] = "stim_duration in component I_stim (second)"
    legend_constants[22] = "stim_amplitude in component I_stim (nanoampere)"
    legend_rates[0] = "d/dt VmReal in component membrane (millivolt)"
    legend_rates[1] = "d/dt C0ShkrReal in component I_Shkr (dimensionless)"
    legend_rates[2] = "d/dt C1ShkrReal in component I_Shkr (dimensionless)"
    legend_rates[3] = "d/dt C2ShkrReal in component I_Shkr (dimensionless)"
    legend_rates[4] = "d/dt C3ShkrReal in component I_Shkr (dimensionless)"
    legend_rates[5] = "d/dt C4ShkrReal in component I_Shkr (dimensionless)"
    legend_rates[6] = "d/dt OShkrReal in component I_Shkr (dimensionless)"
    return (legend_states, legend_algebraic, legend_voi, legend_constants)

def initConsts():
    constants = [0.0] * sizeConstants; states = [0.0] * sizeStates;
    constants[0] = 4.5e-6
    states[0] = -58.00
    constants[1] = 1e-3
    constants[2] = 0.94
    constants[3] = 1.26
    constants[4] = 8310
    constants[5] = 295
    constants[6] = 9.65e4
    constants[7] = 5
    constants[8] = 140
    constants[9] = 5.4e-9
    states[1] = 9.11e-1
    states[2] = 8.57e-2
    states[3] = 3.02e-3
    states[4] = 4.74e-5
    states[5] = 2.79e-7
    states[6] = 0
    constants[10] = 30
    constants[11] = 1.28
    constants[12] = 2
    constants[13] = -1.53
    constants[14] = 77
    constants[15] = 18
    constants[16] = 6.9e-6
    constants[17] = 0
    constants[18] = 0.1
    constants[19] = 10
    constants[20] = 1
    constants[21] = 0.001
    constants[22] = 0.1
    constants[23] = ((constants[4]*constants[5])/constants[6])*log(constants[7]/constants[8])
    return (states, constants)

def computeRates(voi, states, constants):
    rates = [0.0] * sizeStates; algebraic = [0.0] * sizeAlgebraic
    algebraic[7] = custom_piecewise([less(states[5] , 0.00000), 0.00000 , greater(states[5] , 1.00000), 1.00000 , True, states[5]])
    algebraic[8] = custom_piecewise([less(states[6] , 0.00000), 0.00000 , greater(states[6] , 1.00000), 1.00000 , True, states[6]])
    rates[6] = constants[14]*algebraic[7]-constants[15]*algebraic[8]
    algebraic[3] = custom_piecewise([less(states[1] , 0.00000), 0.00000 , greater(states[1] , 1.00000), 1.00000 , True, states[1]])
    algebraic[4] = custom_piecewise([less(states[2] , 0.00000), 0.00000 , greater(states[2] , 1.00000), 1.00000 , True, states[2]])
    algebraic[0] = custom_piecewise([less(fabs(states[0]) , 1.00000e-07), 1.00000e-07 , True, states[0]])
    algebraic[10] = constants[10]*exp((algebraic[0]*constants[11]*constants[6])/(constants[4]*constants[5]))
    algebraic[12] = constants[12]*exp((algebraic[0]*constants[13]*constants[6])/(constants[4]*constants[5]))
    rates[1] = algebraic[12]*algebraic[4]-4.00000*algebraic[10]*algebraic[3]
    algebraic[5] = custom_piecewise([less(states[3] , 0.00000), 0.00000 , greater(states[3] , 1.00000), 1.00000 , True, states[3]])
    rates[2] = (2.00000*algebraic[12]*algebraic[5]+4.00000*algebraic[10]*algebraic[3])-(3.00000*algebraic[10]+algebraic[12])*algebraic[4]
    algebraic[6] = custom_piecewise([less(states[4] , 0.00000), 0.00000 , greater(states[4] , 1.00000), 1.00000 , True, states[4]])
    rates[3] = (3.00000*algebraic[12]*algebraic[6]+3.00000*algebraic[10]*algebraic[4])-(2.00000*algebraic[10]+2.00000*algebraic[12])*algebraic[5]
    rates[4] = (4.00000*algebraic[12]*algebraic[7]+2.00000*algebraic[10]*algebraic[5])-(algebraic[10]+3.00000*algebraic[12])*algebraic[6]
    rates[5] = (constants[15]*algebraic[8]+algebraic[10]*algebraic[6])-(constants[14]+4.00000*algebraic[12])*algebraic[7]
    algebraic[11] = constants[16]*(algebraic[0]-constants[17])
    algebraic[1] = 1.00000/(constants[2]+exp((constants[3]*(algebraic[0]-constants[23])*constants[6])/(constants[4]*constants[5])))
    algebraic[2] = constants[1]*algebraic[1]*(power(constants[7]*0.00100000, 1.0/2))*(algebraic[0]-constants[23])
    algebraic[9] = (((constants[9]*algebraic[8]*algebraic[0]*(power(constants[6], 2.00000)))/(constants[4]*constants[5]))*(constants[8]-constants[7]*exp((-algebraic[0]*constants[6])/(constants[4]*constants[5]))))/(1.00000-exp((-algebraic[0]*constants[6])/(constants[4]*constants[5])))
    algebraic[13] = custom_piecewise([greater_equal(voi , constants[18]) & (less_equal(voi , constants[19]) & less_equal((voi-constants[18])-floor((voi-constants[18])/constants[20])*constants[20] , constants[21])), constants[22] , True, 0.00000])
    rates[0] = -((algebraic[2]+algebraic[9]+algebraic[11])-algebraic[13])/constants[0]
    return(rates)

def computeAlgebraic(constants, states, voi):
    algebraic = array([[0.0] * len(voi)] * sizeAlgebraic)
    states = array(states)
    voi = array(voi)
    algebraic[7] = custom_piecewise([less(states[5] , 0.00000), 0.00000 , greater(states[5] , 1.00000), 1.00000 , True, states[5]])
    algebraic[8] = custom_piecewise([less(states[6] , 0.00000), 0.00000 , greater(states[6] , 1.00000), 1.00000 , True, states[6]])
    algebraic[3] = custom_piecewise([less(states[1] , 0.00000), 0.00000 , greater(states[1] , 1.00000), 1.00000 , True, states[1]])
    algebraic[4] = custom_piecewise([less(states[2] , 0.00000), 0.00000 , greater(states[2] , 1.00000), 1.00000 , True, states[2]])
    algebraic[0] = custom_piecewise([less(fabs(states[0]) , 1.00000e-07), 1.00000e-07 , True, states[0]])
    algebraic[10] = constants[10]*exp((algebraic[0]*constants[11]*constants[6])/(constants[4]*constants[5]))
    algebraic[12] = constants[12]*exp((algebraic[0]*constants[13]*constants[6])/(constants[4]*constants[5]))
    algebraic[5] = custom_piecewise([less(states[3] , 0.00000), 0.00000 , greater(states[3] , 1.00000), 1.00000 , True, states[3]])
    algebraic[6] = custom_piecewise([less(states[4] , 0.00000), 0.00000 , greater(states[4] , 1.00000), 1.00000 , True, states[4]])
    algebraic[11] = constants[16]*(algebraic[0]-constants[17])
    algebraic[1] = 1.00000/(constants[2]+exp((constants[3]*(algebraic[0]-constants[23])*constants[6])/(constants[4]*constants[5])))
    algebraic[2] = constants[1]*algebraic[1]*(power(constants[7]*0.00100000, 1.0/2))*(algebraic[0]-constants[23])
    algebraic[9] = (((constants[9]*algebraic[8]*algebraic[0]*(power(constants[6], 2.00000)))/(constants[4]*constants[5]))*(constants[8]-constants[7]*exp((-algebraic[0]*constants[6])/(constants[4]*constants[5]))))/(1.00000-exp((-algebraic[0]*constants[6])/(constants[4]*constants[5])))
    algebraic[13] = custom_piecewise([greater_equal(voi , constants[18]) & (less_equal(voi , constants[19]) & less_equal((voi-constants[18])-floor((voi-constants[18])/constants[20])*constants[20] , constants[21])), constants[22] , True, 0.00000])
    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)