# Size of variable arrays: sizeAlgebraic = 40 sizeStates = 6 sizeConstants = 57 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_constants[4] = "K_1 in component environment (per_fmol)" legend_constants[5] = "K_13 in component environment (per_fmol)" legend_constants[6] = "K_14 in component environment (per_fmol)" legend_constants[7] = "K_3 in component environment (per_fmol)" legend_constants[8] = "K_6 in component environment (per_fmol)" legend_constants[9] = "K_7 in component environment (per_fmol)" legend_constants[10] = "K_H in component environment (per_fmol)" legend_constants[11] = "K_Ke in component environment (per_fmol)" legend_constants[12] = "K_Ki in component environment (per_fmol)" legend_constants[13] = "K_MgADP in component environment (per_fmol)" legend_constants[14] = "K_MgATP in component environment (per_fmol)" legend_constants[15] = "K_Nae in component environment (per_fmol)" legend_constants[16] = "K_Nai in component environment (per_fmol)" legend_constants[17] = "K_P in component environment (per_fmol)" legend_constants[18] = "kappa_1 in component environment (fmol_per_sec)" legend_constants[19] = "kappa_13 in component environment (fmol_per_sec)" legend_constants[20] = "kappa_15 in component environment (fmol_per_sec)" legend_constants[21] = "kappa_3 in component environment (fmol_per_sec)" legend_constants[22] = "kappa_6 in component environment (fmol_per_sec)" legend_constants[23] = "kappa_8 in component environment (fmol_per_sec)" legend_constants[24] = "z_5 in component environment (dimensionless)" legend_constants[25] = "z_8 in component environment (dimensionless)" legend_constants[40] = "zF_5 in component environment (C_per_mol)" legend_constants[41] = "zF_8 in component environment (C_per_mol)" legend_constants[39] = "x_H in component environment (fmol)" legend_constants[37] = "x_Ke in component environment (fmol)" legend_constants[49] = "x_Ki in component environment (fmol)" legend_constants[52] = "x_MgADP in component environment (fmol)" legend_constants[38] = "x_MgATP in component environment (fmol)" legend_constants[35] = "x_Nae in component environment (fmol)" legend_constants[36] = "x_Nai in component environment (fmol)" legend_states[0] = "x_P7 in component environment (fmol)" legend_constants[55] = "x_P_i in component environment (fmol)" legend_constants[26] = "q_mem in component environment (fC)" legend_states[1] = "x_P14 in component environment (fmol)" legend_states[2] = "x_P1 in component environment (fmol)" legend_states[3] = "x_P3 in component environment (fmol)" legend_states[4] = "x_P6 in component environment (fmol)" legend_states[5] = "x_P13 in component environment (fmol)" legend_algebraic[0] = "x_tot in component environment (fmol)" legend_constants[27] = "small_C in component environment (mM)" legend_constants[28] = "W_i in component environment (pL)" legend_constants[29] = "W_e in component environment (pL)" legend_constants[30] = "c_Ke in component environment (mM)" legend_constants[48] = "c_Ki in component environment (mM)" legend_constants[51] = "c_MgADP in component environment (mM)" legend_constants[31] = "c_MgATP in component environment (mM)" legend_constants[32] = "c_Nae in component environment (mM)" legend_constants[33] = "c_Nai in component environment (mM)" legend_constants[54] = "c_P_i in component environment (mM)" legend_constants[34] = "pH in component environment (dimensionless)" legend_constants[42] = "mu_H in component environment (J_per_mol)" legend_algebraic[29] = "v_H in component environment (fmol_per_sec)" legend_constants[43] = "mu_Ke in component environment (J_per_mol)" legend_algebraic[35] = "v_Ke in component environment (fmol_per_sec)" legend_constants[50] = "mu_Ki in component environment (J_per_mol)" legend_algebraic[18] = "v_Ki in component environment (fmol_per_sec)" legend_constants[53] = "mu_MgADP in component environment (J_per_mol)" legend_algebraic[14] = "v_MgADP in component environment (fmol_per_sec)" legend_constants[44] = "mu_MgATP in component environment (J_per_mol)" legend_algebraic[9] = "v_MgATP in component environment (fmol_per_sec)" legend_constants[45] = "mu_Nae in component environment (J_per_mol)" legend_algebraic[36] = "v_Nae in component environment (fmol_per_sec)" legend_constants[46] = "mu_Nai in component environment (J_per_mol)" legend_algebraic[23] = "v_Nai in component environment (fmol_per_sec)" legend_algebraic[1] = "mu_P7 in component environment (J_per_mol)" legend_algebraic[37] = "v_P7 in component environment (fmol_per_sec)" legend_constants[56] = "mu_P_i in component environment (J_per_mol)" legend_algebraic[30] = "v_P_i in component environment (fmol_per_sec)" legend_constants[47] = "V_mem in component environment (volt)" legend_algebraic[38] = "I_mem in component environment (fA)" legend_algebraic[27] = "Af_R13 in component environment (J_per_mol)" legend_algebraic[4] = "Ar_R13 in component environment (J_per_mol)" legend_algebraic[28] = "v_R13 in component environment (fmol_per_sec)" legend_algebraic[5] = "Af_R15 in component environment (J_per_mol)" legend_algebraic[7] = "Ar_R15 in component environment (J_per_mol)" legend_algebraic[8] = "v_R15 in component environment (fmol_per_sec)" legend_algebraic[12] = "Af_R6 in component environment (J_per_mol)" legend_algebraic[2] = "Ar_R6 in component environment (J_per_mol)" legend_algebraic[13] = "v_R6 in component environment (fmol_per_sec)" legend_algebraic[3] = "mu_P14 in component environment (J_per_mol)" legend_algebraic[31] = "v_P14 in component environment (fmol_per_sec)" legend_algebraic[6] = "mu_P1 in component environment (J_per_mol)" legend_algebraic[19] = "v_P1 in component environment (fmol_per_sec)" legend_algebraic[10] = "mu_P3 in component environment (J_per_mol)" legend_algebraic[24] = "v_P3 in component environment (fmol_per_sec)" legend_algebraic[11] = "mu_P6 in component environment (J_per_mol)" legend_algebraic[25] = "v_P6 in component environment (fmol_per_sec)" legend_algebraic[15] = "Af_R1 in component environment (J_per_mol)" legend_algebraic[16] = "Ar_R1 in component environment (J_per_mol)" legend_algebraic[17] = "v_R1 in component environment (fmol_per_sec)" legend_algebraic[20] = "Af_R3 in component environment (J_per_mol)" legend_algebraic[21] = "Ar_R3 in component environment (J_per_mol)" legend_algebraic[22] = "v_R3 in component environment (fmol_per_sec)" legend_algebraic[26] = "mu_P13 in component environment (J_per_mol)" legend_algebraic[39] = "v_P13 in component environment (fmol_per_sec)" legend_algebraic[32] = "Af_R8 in component environment (J_per_mol)" legend_algebraic[33] = "Ar_R8 in component environment (J_per_mol)" legend_algebraic[34] = "v_R8 in component environment (fmol_per_sec)" legend_rates[0] = "d/dt x_P7 in component environment (fmol)" legend_rates[1] = "d/dt x_P14 in component environment (fmol)" legend_rates[2] = "d/dt x_P1 in component environment (fmol)" legend_rates[3] = "d/dt x_P3 in component environment (fmol)" legend_rates[4] = "d/dt x_P6 in component environment (fmol)" legend_rates[5] = "d/dt x_P13 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 constants[4] = 13085378.038167767 constants[5] = 0.6020622798357556 constants[6] = 69.38568269665376 constants[7] = 46.24091666474313 constants[8] = 79.09015560598799 constants[9] = 4239.416835123694 constants[10] = 0.04565 constants[11] = 0.009236 constants[12] = 0.0012595 constants[13] = 7.976e-05 constants[14] = 2.3715 constants[15] = 0.0061242 constants[16] = 0.00083514 constants[17] = 0.04565 constants[18] = 6.973455976480863 constants[19] = 475.81557480252013 constants[20] = 0.035418056157766185 constants[21] = 861399.5138047301 constants[22] = 1.4100547742408052 constants[23] = 2.2855 constants[24] = -0.054951 constants[25] = -0.94505 states[0] = 0.8590 constants[26] = -18408 states[1] = 0.0100192141898171 states[2] = 6.50836196830909e-10 states[3] = 0.00292644413780957 states[4] = 0.0154965229998754 states[5] = 0.112426414453276 constants[27] = 1e-3 constants[28] = 38 constants[29] = 5.182 constants[30] = 5.4 constants[31] = 10 constants[32] = 150 constants[33] = 50 constants[34] = 7.4 constants[35] = constants[32]*constants[29] constants[36] = constants[33]*constants[28] constants[37] = constants[30]*constants[29] constants[38] = constants[31]*constants[28] constants[39] = (power(10.0000, -constants[34]))*1000.00*constants[28] constants[40] = constants[24]*constants[2] constants[41] = constants[25]*constants[2] constants[42] = constants[0]*constants[1]*log(constants[10]*constants[39]) constants[43] = constants[0]*constants[1]*log(constants[11]*constants[37]) constants[44] = constants[0]*constants[1]*log(constants[14]*constants[38]) constants[45] = constants[0]*constants[1]*log(constants[15]*constants[35]) constants[46] = constants[0]*constants[1]*log(constants[16]*constants[36]) constants[47] = constants[26]/constants[3] constants[48] = constants[27] constants[49] = constants[48]*constants[28] constants[50] = constants[0]*constants[1]*log(constants[12]*constants[49]) constants[51] = constants[27] constants[52] = constants[51]*constants[28] constants[53] = constants[0]*constants[1]*log(constants[13]*constants[52]) constants[54] = constants[27] constants[55] = constants[54]*constants[28] constants[56] = constants[0]*constants[1]*log(constants[17]*constants[55]) return (states, constants) def computeRates(voi, states, constants): rates = [0.0] * sizeStates; algebraic = [0.0] * sizeAlgebraic algebraic[3] = constants[0]*constants[1]*log(constants[6]*states[1]) algebraic[5] = algebraic[3]+constants[44] algebraic[6] = constants[0]*constants[1]*log(constants[4]*states[2]) algebraic[7] = algebraic[6] algebraic[8] = constants[20]*(exp(algebraic[5]/(constants[0]*constants[1]))-exp(algebraic[7]/(constants[0]*constants[1]))) algebraic[15] = algebraic[6] algebraic[10] = constants[0]*constants[1]*log(constants[7]*states[3]) algebraic[16] = algebraic[10]+2.00000*constants[50] algebraic[17] = constants[18]*(exp(algebraic[15]/(constants[0]*constants[1]))-exp(algebraic[16]/(constants[0]*constants[1]))) algebraic[19] = algebraic[8]-algebraic[17] rates[2] = algebraic[19] algebraic[20] = algebraic[10]+3.00000*constants[46] algebraic[11] = constants[0]*constants[1]*log(constants[8]*states[4]) algebraic[21] = constants[40]*constants[47]+algebraic[11] algebraic[22] = constants[21]*(exp(algebraic[20]/(constants[0]*constants[1]))-exp(algebraic[21]/(constants[0]*constants[1]))) algebraic[24] = algebraic[17]-algebraic[22] rates[3] = algebraic[24] algebraic[12] = algebraic[11] algebraic[1] = constants[0]*constants[1]*log(constants[9]*states[0]) algebraic[2] = algebraic[1]+constants[53] algebraic[13] = constants[22]*(exp(algebraic[12]/(constants[0]*constants[1]))-exp(algebraic[2]/(constants[0]*constants[1]))) algebraic[25] = algebraic[22]-algebraic[13] rates[4] = algebraic[25] algebraic[26] = constants[0]*constants[1]*log(constants[5]*states[5]) algebraic[27] = algebraic[26] algebraic[4] = algebraic[3]+constants[56]+constants[42] algebraic[28] = constants[19]*(exp(algebraic[27]/(constants[0]*constants[1]))-exp(algebraic[4]/(constants[0]*constants[1]))) algebraic[31] = algebraic[28]-algebraic[8] rates[1] = algebraic[31] algebraic[32] = algebraic[1]+2.00000*constants[43] algebraic[33] = algebraic[26]+3.00000*constants[45]+constants[41]*constants[47] algebraic[34] = constants[23]*(exp(algebraic[32]/(constants[0]*constants[1]))-exp(algebraic[33]/(constants[0]*constants[1]))) algebraic[37] = algebraic[13]-algebraic[34] rates[0] = algebraic[37] algebraic[39] = algebraic[34]-algebraic[28] rates[5] = algebraic[39] return(rates) def computeAlgebraic(constants, states, voi): algebraic = array([[0.0] * len(voi)] * sizeAlgebraic) states = array(states) voi = array(voi) algebraic[3] = constants[0]*constants[1]*log(constants[6]*states[1]) algebraic[5] = algebraic[3]+constants[44] algebraic[6] = constants[0]*constants[1]*log(constants[4]*states[2]) algebraic[7] = algebraic[6] algebraic[8] = constants[20]*(exp(algebraic[5]/(constants[0]*constants[1]))-exp(algebraic[7]/(constants[0]*constants[1]))) algebraic[15] = algebraic[6] algebraic[10] = constants[0]*constants[1]*log(constants[7]*states[3]) algebraic[16] = algebraic[10]+2.00000*constants[50] algebraic[17] = constants[18]*(exp(algebraic[15]/(constants[0]*constants[1]))-exp(algebraic[16]/(constants[0]*constants[1]))) algebraic[19] = algebraic[8]-algebraic[17] algebraic[20] = algebraic[10]+3.00000*constants[46] algebraic[11] = constants[0]*constants[1]*log(constants[8]*states[4]) algebraic[21] = constants[40]*constants[47]+algebraic[11] algebraic[22] = constants[21]*(exp(algebraic[20]/(constants[0]*constants[1]))-exp(algebraic[21]/(constants[0]*constants[1]))) algebraic[24] = algebraic[17]-algebraic[22] algebraic[12] = algebraic[11] algebraic[1] = constants[0]*constants[1]*log(constants[9]*states[0]) algebraic[2] = algebraic[1]+constants[53] algebraic[13] = constants[22]*(exp(algebraic[12]/(constants[0]*constants[1]))-exp(algebraic[2]/(constants[0]*constants[1]))) algebraic[25] = algebraic[22]-algebraic[13] algebraic[26] = constants[0]*constants[1]*log(constants[5]*states[5]) algebraic[27] = algebraic[26] algebraic[4] = algebraic[3]+constants[56]+constants[42] algebraic[28] = constants[19]*(exp(algebraic[27]/(constants[0]*constants[1]))-exp(algebraic[4]/(constants[0]*constants[1]))) algebraic[31] = algebraic[28]-algebraic[8] algebraic[32] = algebraic[1]+2.00000*constants[43] algebraic[33] = algebraic[26]+3.00000*constants[45]+constants[41]*constants[47] algebraic[34] = constants[23]*(exp(algebraic[32]/(constants[0]*constants[1]))-exp(algebraic[33]/(constants[0]*constants[1]))) algebraic[37] = algebraic[13]-algebraic[34] algebraic[39] = algebraic[34]-algebraic[28] algebraic[0] = states[2]+states[3]+states[4]+states[5]+states[0]+states[1] algebraic[9] = -algebraic[8] algebraic[14] = algebraic[13] algebraic[18] = 2.00000*algebraic[17] algebraic[23] = -3.00000*algebraic[22] algebraic[29] = algebraic[28] algebraic[30] = algebraic[28] algebraic[35] = -2.00000*algebraic[34] algebraic[36] = 3.00000*algebraic[34] algebraic[38] = constants[40]*algebraic[22]+constants[41]*algebraic[34] 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)