# Size of variable arrays: sizeAlgebraic = 8 sizeStates = 4 sizeConstants = 28 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 (millisecond)" legend_states[0] = "V in component membrane (millivolt)" legend_constants[0] = "Cm in component membrane (femtoF)" legend_algebraic[4] = "i_Ca in component calcium_current (picoA)" legend_algebraic[0] = "i_K in component rapidly_activating_K_current (picoA)" legend_algebraic[5] = "i_K_Ca in component calcium_activated_K_current (picoA)" legend_algebraic[7] = "i_Na_Ca in component Na_Ca_exchanger_current (picoA)" legend_constants[1] = "V_K in component rapidly_activating_K_current (millivolt)" legend_constants[2] = "g_K in component rapidly_activating_K_current (picoS)" legend_states[1] = "n in component rapidly_activating_K_current_n_gate (dimensionless)" legend_algebraic[1] = "n_infinity in component rapidly_activating_K_current_n_gate (dimensionless)" legend_constants[3] = "lamda in component rapidly_activating_K_current_n_gate (dimensionless)" legend_algebraic[3] = "tau_n in component rapidly_activating_K_current_n_gate (millisecond)" legend_constants[4] = "V_n in component rapidly_activating_K_current_n_gate (millivolt)" legend_constants[5] = "S_n in component rapidly_activating_K_current_n_gate (millivolt)" legend_constants[6] = "a in component rapidly_activating_K_current_n_gate (millivolt)" legend_constants[7] = "b in component rapidly_activating_K_current_n_gate (millivolt)" legend_constants[8] = "c in component rapidly_activating_K_current_n_gate (millisecond)" legend_constants[9] = "V_ in component rapidly_activating_K_current_n_gate (millivolt)" legend_constants[10] = "V_Ca in component calcium_current (millivolt)" legend_constants[11] = "g_Ca in component calcium_current (picoS)" legend_algebraic[2] = "m_infinity in component calcium_current_m_gate (dimensionless)" legend_constants[12] = "V_m in component calcium_current_m_gate (millivolt)" legend_constants[13] = "S_m in component calcium_current_m_gate (millivolt)" legend_constants[14] = "g_K_Ca in component calcium_activated_K_current (picoS)" legend_constants[15] = "K_d in component calcium_activated_K_current (micromolar)" legend_states[2] = "Ca_i in component ionic_concentrations (micromolar)" legend_constants[16] = "g_Na_Ca in component Na_Ca_exchanger_current (picoS)" legend_constants[17] = "K_1_2 in component Na_Ca_exchanger_current (micromolar)" legend_algebraic[6] = "V_Na_Ca in component Na_Ca_exchanger_current (millivolt)" legend_constants[18] = "RT_F in component Na_Ca_exchanger_current (millivolt)" legend_constants[19] = "nH in component Na_Ca_exchanger_current (dimensionless)" legend_constants[20] = "Ca_o in component ionic_concentrations (micromolar)" legend_constants[21] = "Na_i in component ionic_concentrations (millimolar)" legend_constants[22] = "Na_o in component ionic_concentrations (millimolar)" legend_states[3] = "Ca_ret in component ionic_concentrations (micromolar)" legend_constants[23] = "f in component ionic_concentrations (dimensionless)" legend_constants[24] = "k_Ca in component ionic_concentrations (per_millisecond)" legend_constants[25] = "k_rel in component ionic_concentrations (per_millisecond)" legend_constants[26] = "k_pump in component ionic_concentrations (per_millisecond)" legend_constants[27] = "alpha in component ionic_concentrations (mole_per_microlitre_coulomb)" legend_rates[0] = "d/dt V in component membrane (millivolt)" legend_rates[1] = "d/dt n in component rapidly_activating_K_current_n_gate (dimensionless)" legend_rates[2] = "d/dt Ca_i in component ionic_concentrations (micromolar)" legend_rates[3] = "d/dt Ca_ret in component ionic_concentrations (micromolar)" return (legend_states, legend_algebraic, legend_voi, legend_constants) def initConsts(): constants = [0.0] * sizeConstants; states = [0.0] * sizeStates; states[0] = -76.0 constants[0] = 5310.0 constants[1] = -75.0 constants[2] = 2700.0 states[1] = 0.1 constants[3] = 0.85 constants[4] = -15.0 constants[5] = 5.6 constants[6] = 65.0 constants[7] = 20.0 constants[8] = 6.0 constants[9] = -75.0 constants[10] = 25.0 constants[11] = 1000.0 constants[12] = -20.0 constants[13] = 12.0 constants[14] = 30000.0 constants[15] = 70.0 states[2] = 0.52 constants[16] = 1000.0 constants[17] = 1.5 constants[18] = 26.54 constants[19] = 5.0 constants[20] = 2600.0 constants[21] = 10.0 constants[22] = 140.0 states[3] = 0.7 constants[23] = 0.001 constants[24] = 0.64 constants[25] = 0.0006 constants[26] = 0.2 constants[27] = 0.00006 return (states, constants) def computeRates(voi, states, constants): rates = [0.0] * sizeStates; algebraic = [0.0] * sizeAlgebraic rates[3] = -constants[25]*(states[3]-states[2])+constants[26]*states[2] algebraic[1] = 1.00000/(1.00000+exp((constants[4]-states[0])/constants[5])) algebraic[3] = constants[8]/(exp((states[0]-constants[9])/constants[6])+exp((constants[9]-states[0])/constants[7])) rates[1] = constants[3]*((algebraic[1]-states[1])/algebraic[3]) algebraic[2] = 1.00000/(1.00000+exp((constants[12]-states[0])/constants[13])) algebraic[4] = constants[11]*algebraic[2]*(states[0]-constants[10]) algebraic[0] = constants[2]*states[1]*(states[0]-constants[1]) algebraic[5] = constants[14]*(states[2]/(constants[15]+states[2]))*(states[0]-constants[1]) algebraic[6] = constants[18]*(3.00000*log(constants[22]/constants[21]-log(constants[20]/states[2]))) algebraic[7] = constants[16]*((power(states[2], constants[19]))/(power(constants[17], constants[19])+power(states[2], constants[19])))*(states[0]-algebraic[6]) rates[0] = -(algebraic[0]+algebraic[4]+algebraic[5]+algebraic[7])/constants[0] rates[2] = (constants[23]*(-constants[27]*(algebraic[4]-2.00000*algebraic[7])-constants[24]*states[2])+constants[25]*(states[3]-states[2]))-constants[26]*states[2] return(rates) def computeAlgebraic(constants, states, voi): algebraic = array([[0.0] * len(voi)] * sizeAlgebraic) states = array(states) voi = array(voi) algebraic[1] = 1.00000/(1.00000+exp((constants[4]-states[0])/constants[5])) algebraic[3] = constants[8]/(exp((states[0]-constants[9])/constants[6])+exp((constants[9]-states[0])/constants[7])) algebraic[2] = 1.00000/(1.00000+exp((constants[12]-states[0])/constants[13])) algebraic[4] = constants[11]*algebraic[2]*(states[0]-constants[10]) algebraic[0] = constants[2]*states[1]*(states[0]-constants[1]) algebraic[5] = constants[14]*(states[2]/(constants[15]+states[2]))*(states[0]-constants[1]) algebraic[6] = constants[18]*(3.00000*log(constants[22]/constants[21]-log(constants[20]/states[2]))) algebraic[7] = constants[16]*((power(states[2], constants[19]))/(power(constants[17], constants[19])+power(states[2], constants[19])))*(states[0]-algebraic[6]) 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)