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 = 13
sizeStates = 4
sizeConstants = 29
from math import *
from numpy import *

def createLegends():
    legend_states = [""] * sizeStates
    legend_rates = [""] * sizeStates
    legend_algebraic = [""] * sizeAlgebraic
    legend_voi = ""
    legend_constants = [""] * sizeConstants
    legend_states[0] = "P_Open in component kinetics (dimensionless)"
    legend_states[1] = "P_Closed in component kinetics (dimensionless)"
    legend_states[2] = "P_I1 in component kinetics (dimensionless)"
    legend_states[3] = "P_I2 in component kinetics (dimensionless)"
    legend_voi = "time in component environment (ms)"
    legend_algebraic[2] = "current in component stimulus_protocol (nA_per_nF)"
    legend_algebraic[3] = "voltage_out in component environment (mV)"
    legend_algebraic[4] = "transmembranevoltage in component environment (mV)"
    legend_algebraic[1] = "pressure in component environment (mmHg)"
    legend_algebraic[0] = "stretch in component stimulus_protocol (dimensionless)"
    legend_constants[0] = "pressure_stim_offset in component environment (ms)"
    legend_constants[1] = "pressure_stim_period in component environment (ms)"
    legend_constants[2] = "pressure_stim_duration in component environment (ms)"
    legend_constants[3] = "pressure_stim_amplitude in component environment (dimensionless)"
    legend_constants[4] = "current_stim_offset in component environment (ms)"
    legend_constants[5] = "current_stim_period in component environment (ms)"
    legend_constants[6] = "current_stim_duration in component environment (ms)"
    legend_constants[7] = "current_stim_amplitude in component environment (nA_per_nF)"
    legend_constants[8] = "current_stim_end in component environment (ms)"
    legend_constants[9] = "current_N_period_total in component environment (dimensionless)"
    legend_constants[10] = "current_N_period_applied in component environment (dimensionless)"
    legend_constants[11] = "r1 in component kinetics (per_ms)"
    legend_constants[12] = "r3 in component kinetics (per_ms)"
    legend_constants[13] = "r4 in component kinetics (per_ms)"
    legend_constants[14] = "r5 in component kinetics (per_ms)"
    legend_constants[15] = "r6 in component kinetics (per_ms)"
    legend_constants[16] = "r7 in component kinetics (per_ms)"
    legend_constants[17] = "r8 in component kinetics (per_ms)"
    legend_constants[18] = "ce1 in component kinetics (dimensionless)"
    legend_constants[19] = "ce3 in component kinetics (dimensionless)"
    legend_constants[20] = "ce4 in component kinetics (dimensionless)"
    legend_constants[21] = "cm4 in component kinetics (dimensionless)"
    legend_constants[22] = "ce5 in component kinetics (dimensionless)"
    legend_constants[23] = "ce6 in component kinetics (dimensionless)"
    legend_constants[24] = "ce7 in component kinetics (dimensionless)"
    legend_constants[25] = "ce8 in component kinetics (dimensionless)"
    legend_constants[26] = "r2 in component kinetics (per_ms)"
    legend_constants[27] = "ce2 in component kinetics (dimensionless)"
    legend_constants[28] = "cm2 in component kinetics (dimensionless)"
    legend_algebraic[5] = "p_O_I1 in component kinetics (per_ms)"
    legend_algebraic[6] = "p_I1_O in component kinetics (per_ms)"
    legend_algebraic[7] = "p_I1_C in component kinetics (per_ms)"
    legend_algebraic[8] = "p_C_I1 in component kinetics (per_ms)"
    legend_algebraic[9] = "p_C_O in component kinetics (per_ms)"
    legend_algebraic[10] = "p_O_C in component kinetics (per_ms)"
    legend_algebraic[11] = "p_I2_O in component kinetics (per_ms)"
    legend_algebraic[12] = "p_O_I2 in component kinetics (per_ms)"
    legend_rates[0] = "d/dt P_Open in component kinetics (dimensionless)"
    legend_rates[1] = "d/dt P_Closed in component kinetics (dimensionless)"
    legend_rates[2] = "d/dt P_I1 in component kinetics (dimensionless)"
    legend_rates[3] = "d/dt P_I2 in component kinetics (dimensionless)"
    return (legend_states, legend_algebraic, legend_voi, legend_constants)

def initConsts():
    constants = [0.0] * sizeConstants; states = [0.0] * sizeStates;
    states[0] = 0.0
    states[1] = 0.3
    states[2] = 0.1
    states[3] = 0.6
    constants[0] = 100
    constants[1] = 2000
    constants[2] = 5
    constants[3] = 1.1
    constants[4] = 100
    constants[5] = 1000
    constants[6] = 500
    constants[7] = 80
    constants[8] = 999999999999999
    constants[9] = 1
    constants[10] = 1
    constants[11] = 0.02634342
    constants[12] = 0.0008000712
    constants[13] = 0.008945307
    constants[14] = 1.3529515e-5
    constants[15] = 6.206403e-5
    constants[16] = 6.603723e-6
    constants[17] = 0.00079357607
    constants[18] = -2.3753126
    constants[19] = -5.6010337
    constants[20] = 0.70629007
    constants[21] = -12.101605
    constants[22] = 6.46389
    constants[23] = -1.2173947
    constants[24] = 8.739162
    constants[25] = -3.364573
    constants[26] = (constants[11]*constants[12]*constants[14])/(constants[13]*constants[15])
    constants[27] = ((constants[18]+constants[19]+constants[22])-constants[20])-constants[23]
    constants[28] = -constants[21]
    return (states, constants)

def computeRates(voi, states, constants):
    rates = [0.0] * sizeStates; algebraic = [0.0] * sizeAlgebraic
    algebraic[2] = custom_piecewise([greater_equal(voi , constants[4]) & less(voi , constants[8]) & less_equal( voi-constants[4] % constants[5] , constants[6]) & less( voi-constants[4] % constants[9]*constants[5] , constants[10]*constants[5]), constants[7] , True, 0.00000])
    algebraic[3] = (algebraic[2]/1.00000)*1.00000
    algebraic[5] = constants[11]*exp((constants[18]*algebraic[3])/140.000)
    algebraic[0] = custom_piecewise([greater_equal(voi , constants[0]) & less_equal( voi-constants[0] % constants[1] , constants[2]), constants[3] , True, 1.00000])
    algebraic[1] = 350.000*amax(vstack(0.00000, algebraic[0]-1.00000),0)
    algebraic[6] = constants[26]*exp((constants[27]*algebraic[3])/140.000+(constants[28]*algebraic[1])/70.0000)
    algebraic[7] = constants[12]*exp((constants[19]*algebraic[3])/140.000)
    algebraic[8] = constants[13]*exp((constants[20]*algebraic[3])/140.000+(constants[21]*algebraic[1])/70.0000)
    rates[2] = -(algebraic[6]+algebraic[7])*states[2]+algebraic[8]*states[1]+algebraic[5]*states[0]
    algebraic[9] = constants[14]*exp((constants[22]*algebraic[3])/140.000)
    algebraic[10] = constants[15]*exp((constants[23]*algebraic[3])/140.000)
    rates[1] = -(algebraic[8]+algebraic[9])*states[1]+algebraic[10]*states[0]+algebraic[7]*states[2]
    algebraic[11] = constants[16]*exp((((constants[24]*algebraic[3])/140.000)*algebraic[1])/70.0000)
    algebraic[12] = constants[17]*exp((constants[25]*algebraic[3])/140.000)
    rates[0] = -(algebraic[5]+algebraic[12]+algebraic[10])*states[0]+algebraic[9]*states[1]+algebraic[6]*states[2]+algebraic[11]*states[3]
    rates[3] = -algebraic[11]*states[3]+algebraic[12]*states[0]
    return(rates)

def computeAlgebraic(constants, states, voi):
    algebraic = array([[0.0] * len(voi)] * sizeAlgebraic)
    states = array(states)
    voi = array(voi)
    algebraic[2] = custom_piecewise([greater_equal(voi , constants[4]) & less(voi , constants[8]) & less_equal( voi-constants[4] % constants[5] , constants[6]) & less( voi-constants[4] % constants[9]*constants[5] , constants[10]*constants[5]), constants[7] , True, 0.00000])
    algebraic[3] = (algebraic[2]/1.00000)*1.00000
    algebraic[5] = constants[11]*exp((constants[18]*algebraic[3])/140.000)
    algebraic[0] = custom_piecewise([greater_equal(voi , constants[0]) & less_equal( voi-constants[0] % constants[1] , constants[2]), constants[3] , True, 1.00000])
    algebraic[1] = 350.000*amax(vstack(0.00000, algebraic[0]-1.00000),0)
    algebraic[6] = constants[26]*exp((constants[27]*algebraic[3])/140.000+(constants[28]*algebraic[1])/70.0000)
    algebraic[7] = constants[12]*exp((constants[19]*algebraic[3])/140.000)
    algebraic[8] = constants[13]*exp((constants[20]*algebraic[3])/140.000+(constants[21]*algebraic[1])/70.0000)
    algebraic[9] = constants[14]*exp((constants[22]*algebraic[3])/140.000)
    algebraic[10] = constants[15]*exp((constants[23]*algebraic[3])/140.000)
    algebraic[11] = constants[16]*exp((((constants[24]*algebraic[3])/140.000)*algebraic[1])/70.0000)
    algebraic[12] = constants[17]*exp((constants[25]*algebraic[3])/140.000)
    algebraic[4] = (algebraic[3]-5.00000)*states[0]
    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)