# Size of variable arrays: sizeAlgebraic = 40 sizeStates = 11 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 = "t in component environment (second)" legend_constants[0] = "R in component environment (J_per_K_per_mol)" legend_constants[1] = "T in component environment (kelvin)" legend_constants[2] = "F in component environment (C_per_mol)" legend_constants[3] = "C_m in component environment (fF)" legend_states[0] = "q_Ca_o in component environment (fmol)" legend_states[1] = "q_Ca_i in component environment (fmol)" legend_states[2] = "q_Na_o in component environment (fmol)" legend_states[3] = "q_Na_i in component environment (fmol)" legend_states[4] = "q_P1_NCX in component environment (fmol)" legend_states[5] = "q_P2_NCX in component environment (fmol)" legend_states[6] = "q_P3_NCX in component environment (fmol)" legend_states[7] = "q_P4_NCX in component environment (fmol)" legend_states[8] = "q_P5_NCX in component environment (fmol)" legend_states[9] = "q_P6_NCX in component environment (fmol)" legend_algebraic[29] = "v_Ca_o_NCX in component NCX (fmol_per_sec)" legend_algebraic[22] = "v_Ca_i_NCX in component NCX (fmol_per_sec)" legend_algebraic[33] = "v_Na_o_NCX in component NCX (fmol_per_sec)" legend_algebraic[19] = "v_Na_i_NCX in component NCX (fmol_per_sec)" legend_states[10] = "q_mem in component environment (fC)" legend_algebraic[37] = "I_mem_NCX in component NCX (fA)" legend_constants[4] = "kappa_1_NCX in component NCX_parameters (fmol_per_sec)" legend_constants[5] = "kappa_2_NCX in component NCX_parameters (fmol_per_sec)" legend_constants[6] = "kappa_3_NCX in component NCX_parameters (fmol_per_sec)" legend_constants[7] = "kappa_4_NCX in component NCX_parameters (fmol_per_sec)" legend_constants[8] = "kappa_5_NCX in component NCX_parameters (fmol_per_sec)" legend_constants[9] = "kappa_6_NCX in component NCX_parameters (fmol_per_sec)" legend_constants[10] = "K_1_NCX in component NCX_parameters (per_fmol)" legend_constants[11] = "K_2_NCX in component NCX_parameters (per_fmol)" legend_constants[12] = "K_3_NCX in component NCX_parameters (per_fmol)" legend_constants[13] = "K_4_NCX in component NCX_parameters (per_fmol)" legend_constants[14] = "K_5_NCX in component NCX_parameters (per_fmol)" legend_constants[15] = "K_6_NCX in component NCX_parameters (per_fmol)" legend_constants[16] = "K_Na_i in component NCX_parameters (per_fmol)" legend_constants[17] = "K_Ca_i in component NCX_parameters (per_fmol)" legend_constants[18] = "K_Na_o in component NCX_parameters (per_fmol)" legend_constants[19] = "K_Ca_o in component NCX_parameters (per_fmol)" legend_constants[20] = "nNa_o_NCX in component NCX_parameters (dimensionless)" legend_constants[21] = "nNa_i_NCX in component NCX_parameters (dimensionless)" legend_constants[22] = "zf_NCX in component NCX_parameters (dimensionless)" legend_constants[23] = "zr_NCX in component NCX_parameters (dimensionless)" legend_algebraic[1] = "mu_Ca_o in component NCX (J_per_mol)" legend_algebraic[2] = "mu_Ca_i in component NCX (J_per_mol)" legend_algebraic[3] = "mu_Na_o in component NCX (J_per_mol)" legend_algebraic[4] = "mu_Na_i in component NCX (J_per_mol)" legend_algebraic[5] = "mu_P1_NCX in component NCX (J_per_mol)" legend_algebraic[6] = "mu_P2_NCX in component NCX (J_per_mol)" legend_algebraic[7] = "mu_P3_NCX in component NCX (J_per_mol)" legend_algebraic[8] = "mu_P4_NCX in component NCX (J_per_mol)" legend_algebraic[9] = "mu_P5_NCX in component NCX (J_per_mol)" legend_algebraic[10] = "mu_P6_NCX in component NCX (J_per_mol)" legend_algebraic[38] = "v_P1_NCX in component NCX (fmol_per_sec)" legend_algebraic[23] = "v_P2_NCX in component NCX (fmol_per_sec)" legend_algebraic[26] = "v_P3_NCX in component NCX (fmol_per_sec)" legend_algebraic[30] = "v_P4_NCX in component NCX (fmol_per_sec)" legend_algebraic[35] = "v_P5_NCX in component NCX (fmol_per_sec)" legend_algebraic[39] = "v_P6_NCX in component NCX (fmol_per_sec)" legend_algebraic[11] = "Af_r1 in component NCX (J_per_mol)" legend_algebraic[17] = "Ar_r1 in component NCX (J_per_mol)" legend_algebraic[18] = "v_r1 in component NCX (fmol_per_sec)" legend_algebraic[12] = "Af_r2 in component NCX (J_per_mol)" legend_algebraic[20] = "Ar_r2 in component NCX (J_per_mol)" legend_algebraic[21] = "v_r2 in component NCX (fmol_per_sec)" legend_algebraic[13] = "Af_r3 in component NCX (J_per_mol)" legend_algebraic[24] = "Ar_r3 in component NCX (J_per_mol)" legend_algebraic[25] = "v_r3 in component NCX (fmol_per_sec)" legend_algebraic[14] = "Af_r4 in component NCX (J_per_mol)" legend_algebraic[27] = "Ar_r4 in component NCX (J_per_mol)" legend_algebraic[28] = "v_r4 in component NCX (fmol_per_sec)" legend_algebraic[15] = "Af_r5 in component NCX (J_per_mol)" legend_algebraic[31] = "Ar_r5 in component NCX (J_per_mol)" legend_algebraic[32] = "v_r5 in component NCX (fmol_per_sec)" legend_algebraic[16] = "Af_r6 in component NCX (J_per_mol)" legend_algebraic[34] = "Ar_r6 in component NCX (J_per_mol)" legend_algebraic[36] = "v_r6 in component NCX (fmol_per_sec)" legend_algebraic[0] = "V_mem in component NCX (volt)" legend_rates[0] = "d/dt q_Ca_o in component environment (fmol)" legend_rates[1] = "d/dt q_Ca_i in component environment (fmol)" legend_rates[2] = "d/dt q_Na_o in component environment (fmol)" legend_rates[3] = "d/dt q_Na_i in component environment (fmol)" legend_rates[10] = "d/dt q_mem in component environment (fC)" legend_rates[4] = "d/dt q_P1_NCX in component environment (fmol)" legend_rates[5] = "d/dt q_P2_NCX in component environment (fmol)" legend_rates[6] = "d/dt q_P3_NCX in component environment (fmol)" legend_rates[7] = "d/dt q_P4_NCX in component environment (fmol)" legend_rates[8] = "d/dt q_P5_NCX in component environment (fmol)" legend_rates[9] = "d/dt q_P6_NCX in component environment (fmol)" return (legend_states, legend_algebraic, legend_voi, legend_constants) def initConsts(): constants = [0.0] * sizeConstants; states = [0.0] * sizeStates; constants[0] = 8.314 constants[1] = 310 constants[2] = 96485 constants[3] = 153400 states[0] = 9.3276 states[1] = 0.00456 states[2] = 725.48 states[3] = 380 states[4] = 0.000103509 states[5] = 0.000103509 states[6] = 0.000103509 states[7] = 0.000103509 states[8] = 0.000103509 states[9] = 0.000103509 states[10] = -13039 constants[4] = 417309 constants[5] = 1.39522e+07 constants[6] = 472.755 constants[7] = 1.39522e+07 constants[8] = 417309 constants[9] = 0.370405 constants[10] = 2.93394e+08 constants[11] = 14.1045 constants[12] = 263471 constants[13] = 2.39875e+06 constants[14] = 2373.73 constants[15] = 956.214 constants[16] = 0.0137751 constants[17] = 10.2446 constants[18] = 0.470129 constants[19] = 7572.91 constants[20] = 3 constants[21] = 3 constants[22] = -0.55679 constants[23] = 0.44321 return (states, constants) def computeRates(voi, states, constants): rates = [0.0] * sizeStates; algebraic = [0.0] * sizeAlgebraic algebraic[5] = constants[0]*constants[1]*log(constants[10]*states[4]) algebraic[11] = algebraic[5] algebraic[4] = constants[0]*constants[1]*log(constants[16]*states[3]) algebraic[6] = constants[0]*constants[1]*log(constants[11]*states[5]) algebraic[17] = constants[21]*algebraic[4]+algebraic[6] algebraic[18] = constants[4]*(exp(algebraic[11]/(constants[0]*constants[1]))-exp(algebraic[17]/(constants[0]*constants[1]))) algebraic[19] = constants[21]*algebraic[18] rates[3] = algebraic[19] algebraic[2] = constants[0]*constants[1]*log(constants[17]*states[1]) algebraic[12] = algebraic[6]+algebraic[2] algebraic[7] = constants[0]*constants[1]*log(constants[12]*states[6]) algebraic[20] = algebraic[7] algebraic[21] = constants[5]*(exp(algebraic[12]/(constants[0]*constants[1]))-exp(algebraic[20]/(constants[0]*constants[1]))) algebraic[22] = -algebraic[21] rates[1] = algebraic[22] algebraic[23] = algebraic[18]-algebraic[21] rates[5] = algebraic[23] algebraic[13] = algebraic[7] algebraic[8] = constants[0]*constants[1]*log(constants[13]*states[7]) algebraic[24] = algebraic[8] algebraic[25] = constants[6]*(exp(algebraic[13]/(constants[0]*constants[1]))-exp(algebraic[24]/(constants[0]*constants[1]))) algebraic[26] = algebraic[21]-algebraic[25] rates[6] = algebraic[26] algebraic[14] = algebraic[8] algebraic[1] = constants[0]*constants[1]*log(constants[19]*states[0]) algebraic[9] = constants[0]*constants[1]*log(constants[14]*states[8]) algebraic[27] = algebraic[9]+algebraic[1] algebraic[28] = constants[7]*(exp(algebraic[14]/(constants[0]*constants[1]))-exp(algebraic[27]/(constants[0]*constants[1]))) algebraic[29] = algebraic[28] rates[0] = algebraic[29] algebraic[30] = -algebraic[28]+algebraic[25] rates[7] = algebraic[30] algebraic[3] = constants[0]*constants[1]*log(constants[18]*states[2]) algebraic[15] = algebraic[9]+constants[20]*algebraic[3] algebraic[10] = constants[0]*constants[1]*log(constants[15]*states[9]) algebraic[31] = algebraic[10] algebraic[32] = constants[8]*(exp(algebraic[15]/(constants[0]*constants[1]))-exp(algebraic[31]/(constants[0]*constants[1]))) algebraic[33] = -constants[20]*algebraic[32] rates[2] = algebraic[33] algebraic[35] = -algebraic[32]+algebraic[28] rates[8] = algebraic[35] algebraic[0] = states[10]/constants[3] algebraic[16] = constants[0]*constants[1]*log(constants[15]*states[9])+constants[22]*constants[2]*algebraic[0] algebraic[34] = constants[0]*constants[1]*log(constants[10]*states[4])+constants[23]*constants[2]*algebraic[0] algebraic[36] = constants[9]*(exp(algebraic[16]/(constants[0]*constants[1]))-exp(algebraic[34]/(constants[0]*constants[1]))) algebraic[37] = constants[2]*(-constants[22]*algebraic[36]+constants[23]*algebraic[36]) rates[10] = algebraic[37] algebraic[38] = algebraic[36]-algebraic[18] rates[4] = algebraic[38] algebraic[39] = -algebraic[36]+algebraic[32] rates[9] = algebraic[39] return(rates) def computeAlgebraic(constants, states, voi): algebraic = array([[0.0] * len(voi)] * sizeAlgebraic) states = array(states) voi = array(voi) algebraic[5] = constants[0]*constants[1]*log(constants[10]*states[4]) algebraic[11] = algebraic[5] algebraic[4] = constants[0]*constants[1]*log(constants[16]*states[3]) algebraic[6] = constants[0]*constants[1]*log(constants[11]*states[5]) algebraic[17] = constants[21]*algebraic[4]+algebraic[6] algebraic[18] = constants[4]*(exp(algebraic[11]/(constants[0]*constants[1]))-exp(algebraic[17]/(constants[0]*constants[1]))) algebraic[19] = constants[21]*algebraic[18] algebraic[2] = constants[0]*constants[1]*log(constants[17]*states[1]) algebraic[12] = algebraic[6]+algebraic[2] algebraic[7] = constants[0]*constants[1]*log(constants[12]*states[6]) algebraic[20] = algebraic[7] algebraic[21] = constants[5]*(exp(algebraic[12]/(constants[0]*constants[1]))-exp(algebraic[20]/(constants[0]*constants[1]))) algebraic[22] = -algebraic[21] algebraic[23] = algebraic[18]-algebraic[21] algebraic[13] = algebraic[7] algebraic[8] = constants[0]*constants[1]*log(constants[13]*states[7]) algebraic[24] = algebraic[8] algebraic[25] = constants[6]*(exp(algebraic[13]/(constants[0]*constants[1]))-exp(algebraic[24]/(constants[0]*constants[1]))) algebraic[26] = algebraic[21]-algebraic[25] algebraic[14] = algebraic[8] algebraic[1] = constants[0]*constants[1]*log(constants[19]*states[0]) algebraic[9] = constants[0]*constants[1]*log(constants[14]*states[8]) algebraic[27] = algebraic[9]+algebraic[1] algebraic[28] = constants[7]*(exp(algebraic[14]/(constants[0]*constants[1]))-exp(algebraic[27]/(constants[0]*constants[1]))) algebraic[29] = algebraic[28] algebraic[30] = -algebraic[28]+algebraic[25] algebraic[3] = constants[0]*constants[1]*log(constants[18]*states[2]) algebraic[15] = algebraic[9]+constants[20]*algebraic[3] algebraic[10] = constants[0]*constants[1]*log(constants[15]*states[9]) algebraic[31] = algebraic[10] algebraic[32] = constants[8]*(exp(algebraic[15]/(constants[0]*constants[1]))-exp(algebraic[31]/(constants[0]*constants[1]))) algebraic[33] = -constants[20]*algebraic[32] algebraic[35] = -algebraic[32]+algebraic[28] algebraic[0] = states[10]/constants[3] algebraic[16] = constants[0]*constants[1]*log(constants[15]*states[9])+constants[22]*constants[2]*algebraic[0] algebraic[34] = constants[0]*constants[1]*log(constants[10]*states[4])+constants[23]*constants[2]*algebraic[0] algebraic[36] = constants[9]*(exp(algebraic[16]/(constants[0]*constants[1]))-exp(algebraic[34]/(constants[0]*constants[1]))) algebraic[37] = constants[2]*(-constants[22]*algebraic[36]+constants[23]*algebraic[36]) algebraic[38] = algebraic[36]-algebraic[18] algebraic[39] = -algebraic[36]+algebraic[32] 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)