# Size of variable arrays: sizeAlgebraic = 54 sizeStates = 7 sizeConstants = 46 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 params_BG (second)" legend_constants[0] = "R in component params_BG (J_per_K_mol)" legend_constants[1] = "T in component params_BG (kelvin)" legend_constants[2] = "F in component params_BG (C_per_mol)" legend_constants[3] = "K_Nai in component params_BG (per_fmol)" legend_constants[38] = "q_Nai in component params_BG (fmol)" legend_constants[4] = "K_Nao in component params_BG (per_fmol)" legend_constants[40] = "q_Nao in component params_BG (fmol)" legend_constants[5] = "K_Glci in component params_BG (per_fmol)" legend_constants[42] = "q_Glci in component params_BG (fmol)" legend_constants[6] = "K_Glco in component params_BG (per_fmol)" legend_constants[44] = "q_Glco in component params_BG (fmol)" legend_constants[7] = "K_1 in component params_BG (per_fmol)" legend_constants[8] = "q_init_1 in component params_BG (fmol)" legend_constants[9] = "K_2 in component params_BG (per_fmol)" legend_constants[10] = "q_init_2 in component params_BG (fmol)" legend_constants[11] = "K_3 in component params_BG (per_fmol)" legend_constants[12] = "q_init_3 in component params_BG (fmol)" legend_constants[13] = "K_4 in component params_BG (per_fmol)" legend_constants[14] = "q_init_4 in component params_BG (fmol)" legend_constants[15] = "K_5 in component params_BG (per_fmol)" legend_constants[16] = "q_init_5 in component params_BG (fmol)" legend_constants[17] = "K_6 in component params_BG (per_fmol)" legend_constants[18] = "q_init_6 in component params_BG (fmol)" legend_algebraic[24] = "V0_Vm in component params_BG (volt)" legend_constants[19] = "z_zf1 in component params_BG (dimensionless)" legend_constants[20] = "z_zr1 in component params_BG (dimensionless)" legend_constants[21] = "z_zf6 in component params_BG (dimensionless)" legend_constants[22] = "z_zr6 in component params_BG (dimensionless)" legend_constants[23] = "kappa_r1 in component params_BG (fmol_per_s)" legend_constants[24] = "kappa_r2 in component params_BG (fmol_per_s)" legend_constants[25] = "kappa_r3 in component params_BG (fmol_per_s)" legend_constants[26] = "kappa_r4 in component params_BG (fmol_per_s)" legend_constants[27] = "kappa_r5 in component params_BG (fmol_per_s)" legend_constants[28] = "kappa_r6 in component params_BG (fmol_per_s)" legend_constants[29] = "kappa_r7 in component params_BG (fmol_per_s)" legend_constants[39] = "mu_Nai in component SGLT1_BG (J_per_mol)" legend_constants[41] = "mu_Nao in component SGLT1_BG (J_per_mol)" legend_constants[43] = "mu_Glci in component SGLT1_BG (J_per_mol)" legend_constants[45] = "mu_Glco in component SGLT1_BG (J_per_mol)" legend_algebraic[0] = "mu_1 in component SGLT1_BG (J_per_mol)" legend_algebraic[46] = "v_1 in component SGLT1_BG (fmol_per_s)" legend_algebraic[2] = "mu_2 in component SGLT1_BG (J_per_mol)" legend_algebraic[38] = "v_2 in component SGLT1_BG (fmol_per_s)" legend_algebraic[3] = "mu_3 in component SGLT1_BG (J_per_mol)" legend_algebraic[13] = "v_3 in component SGLT1_BG (fmol_per_s)" legend_algebraic[4] = "mu_4 in component SGLT1_BG (J_per_mol)" legend_algebraic[17] = "v_4 in component SGLT1_BG (fmol_per_s)" legend_algebraic[5] = "mu_5 in component SGLT1_BG (J_per_mol)" legend_algebraic[25] = "v_5 in component SGLT1_BG (fmol_per_s)" legend_algebraic[6] = "mu_6 in component SGLT1_BG (J_per_mol)" legend_algebraic[47] = "v_6 in component SGLT1_BG (fmol_per_s)" legend_algebraic[26] = "V_Vm in component SGLT1_BG (volt)" legend_algebraic[27] = "V_zf1 in component SGLT1_BG (volt)" legend_algebraic[40] = "I_zf1 in component SGLT1_BG (fA)" legend_algebraic[28] = "mu_zf1 in component SGLT1_BG (J_per_mol)" legend_algebraic[39] = "v_zf1 in component SGLT1_BG (fmol_per_s)" legend_algebraic[29] = "V_zr1 in component SGLT1_BG (volt)" legend_algebraic[42] = "I_zr1 in component SGLT1_BG (fA)" legend_algebraic[30] = "mu_zr1 in component SGLT1_BG (J_per_mol)" legend_algebraic[41] = "v_zr1 in component SGLT1_BG (fmol_per_s)" legend_algebraic[31] = "V_zf6 in component SGLT1_BG (volt)" legend_algebraic[50] = "I_zf6 in component SGLT1_BG (fA)" legend_algebraic[32] = "mu_zf6 in component SGLT1_BG (J_per_mol)" legend_algebraic[48] = "v_zf6 in component SGLT1_BG (fmol_per_s)" legend_algebraic[33] = "V_zr6 in component SGLT1_BG (volt)" legend_algebraic[52] = "I_zr6 in component SGLT1_BG (fA)" legend_algebraic[34] = "mu_zr6 in component SGLT1_BG (J_per_mol)" legend_algebraic[51] = "v_zr6 in component SGLT1_BG (fmol_per_s)" legend_algebraic[35] = "A_f_r1 in component SGLT1_BG (J_per_mol)" legend_algebraic[37] = "v_r1 in component SGLT1_BG (fmol_per_s)" legend_algebraic[36] = "A_r_r1 in component SGLT1_BG (J_per_mol)" legend_algebraic[7] = "A_f_r2 in component SGLT1_BG (J_per_mol)" legend_algebraic[9] = "v_r2 in component SGLT1_BG (fmol_per_s)" legend_algebraic[8] = "A_r_r2 in component SGLT1_BG (J_per_mol)" legend_algebraic[10] = "A_f_r3 in component SGLT1_BG (J_per_mol)" legend_algebraic[12] = "v_r3 in component SGLT1_BG (fmol_per_s)" legend_algebraic[11] = "A_r_r3 in component SGLT1_BG (J_per_mol)" legend_algebraic[14] = "A_f_r4 in component SGLT1_BG (J_per_mol)" legend_algebraic[16] = "v_r4 in component SGLT1_BG (fmol_per_s)" legend_algebraic[15] = "A_r_r4 in component SGLT1_BG (J_per_mol)" legend_algebraic[18] = "A_f_r5 in component SGLT1_BG (J_per_mol)" legend_algebraic[20] = "v_r5 in component SGLT1_BG (fmol_per_s)" legend_algebraic[19] = "A_r_r5 in component SGLT1_BG (J_per_mol)" legend_algebraic[43] = "A_f_r6 in component SGLT1_BG (J_per_mol)" legend_algebraic[45] = "v_r6 in component SGLT1_BG (fmol_per_s)" legend_algebraic[44] = "A_r_r6 in component SGLT1_BG (J_per_mol)" legend_algebraic[21] = "A_f_r7 in component SGLT1_BG (J_per_mol)" legend_algebraic[23] = "v_r7 in component SGLT1_BG (fmol_per_s)" legend_algebraic[22] = "A_r_r7 in component SGLT1_BG (J_per_mol)" legend_states[0] = "q_1 in component SGLT1_BG (fmol)" legend_states[1] = "q_2 in component SGLT1_BG (fmol)" legend_states[2] = "q_3 in component SGLT1_BG (fmol)" legend_states[3] = "q_4 in component SGLT1_BG (fmol)" legend_states[4] = "q_5 in component SGLT1_BG (fmol)" legend_states[5] = "q_6 in component SGLT1_BG (fmol)" legend_algebraic[53] = "Ii in component SGLT1_BG (fA)" legend_algebraic[49] = "I_ss in component SGLT1_BG (fA)" legend_constants[30] = "Nai in component params_BG (mM)" legend_constants[31] = "Nao in component params_BG (mM)" legend_constants[32] = "Glci in component params_BG (mM)" legend_constants[33] = "Glco in component params_BG (mM)" legend_constants[34] = "V_i in component params_BG (pL)" legend_constants[35] = "V_o in component params_BG (pL)" legend_states[6] = "V_E in component params_BG (volt)" legend_constants[36] = "test_volt in component params_BG (volt)" legend_algebraic[1] = "rate_VE in component params_BG (volt_per_s)" legend_constants[37] = "dt in component params_BG (second)" legend_rates[0] = "d/dt q_1 in component SGLT1_BG (fmol)" legend_rates[1] = "d/dt q_2 in component SGLT1_BG (fmol)" legend_rates[2] = "d/dt q_3 in component SGLT1_BG (fmol)" legend_rates[3] = "d/dt q_4 in component SGLT1_BG (fmol)" legend_rates[4] = "d/dt q_5 in component SGLT1_BG (fmol)" legend_rates[5] = "d/dt q_6 in component SGLT1_BG (fmol)" legend_rates[6] = "d/dt V_E in component params_BG (volt)" return (legend_states, legend_algebraic, legend_voi, legend_constants) def initConsts(): constants = [0.0] * sizeConstants; states = [0.0] * sizeStates; constants[0] = 8.31 constants[1] = 293 constants[2] = 96485 constants[3] = 3.216e-08 constants[4] = 3.216e-08 constants[5] = 4.848e-06 constants[6] = 4.848e-06 constants[7] = 2.235 constants[8] = 13.7727015449461 constants[9] = 10.437 constants[10] = 72.295598813025 constants[11] = 8.602 constants[12] = 0.16701061319503 constants[13] = 8.602 constants[14] = 0.233806833618937 constants[15] = 28.628 constants[16] = 1.73527350102727 constants[17] = 0.192 constants[18] = 11.4302815603453 constants[19] = 0.3 constants[20] = 0.3 constants[21] = 0.7 constants[22] = 0.7 constants[23] = 47.905 constants[24] = 2.325 constants[25] = 5.812 constants[26] = 92.998 constants[27] = 0.349 constants[28] = 15.661 constants[29] = 0.029 constants[30] = 20 constants[31] = 100 constants[32] = 10e-3 constants[33] = 1e-12 constants[34] = 8.5e5 constants[35] = 8.5e5 states[6] = -0.05 constants[36] = 0 constants[37] = 1e-3 constants[38] = constants[30]*constants[34] constants[39] = constants[0]*constants[1]*log(constants[3]*constants[38]) constants[40] = constants[31]*constants[35] constants[41] = constants[0]*constants[1]*log(constants[4]*constants[40]) constants[42] = constants[32]*constants[34] constants[43] = constants[0]*constants[1]*log(constants[5]*constants[42]) constants[44] = constants[33]*constants[35] constants[45] = constants[0]*constants[1]*log(constants[6]*constants[44]) states[0] = constants[8] states[1] = constants[10] states[2] = constants[12] states[3] = constants[14] states[4] = constants[16] states[5] = constants[18] return (states, constants) def computeRates(voi, states, constants): rates = [0.0] * sizeStates; algebraic = [0.0] * sizeAlgebraic algebraic[1] = custom_piecewise([less(voi , 1.20475), 0.00000 , greater_equal(voi , 1.20475) & less(voi , 1.20475+constants[37]), (constants[36]+0.0500000)/constants[37] , greater_equal(voi , 1.20475+constants[37]) & less(voi , 2.98475), 0.00000 , greater_equal(voi , 2.98475) & less(voi , 2.98475+constants[37]), -(constants[36]+0.0500000)/constants[37] , True, 0.00000]) rates[6] = algebraic[1] algebraic[2] = constants[0]*constants[1]*log(constants[9]*states[1]) algebraic[7] = constants[45]+algebraic[2] algebraic[3] = constants[0]*constants[1]*log(constants[11]*states[2]) algebraic[8] = algebraic[3] algebraic[9] = constants[24]*(exp(algebraic[7]/(constants[0]*constants[1]))-exp(algebraic[8]/(constants[0]*constants[1]))) algebraic[10] = algebraic[3] algebraic[4] = constants[0]*constants[1]*log(constants[13]*states[3]) algebraic[11] = algebraic[4] algebraic[12] = constants[25]*(exp(algebraic[10]/(constants[0]*constants[1]))-exp(algebraic[11]/(constants[0]*constants[1]))) algebraic[13] = algebraic[9]-algebraic[12] rates[2] = algebraic[13] algebraic[14] = algebraic[4] algebraic[5] = constants[0]*constants[1]*log(constants[15]*states[4]) algebraic[15] = constants[43]+algebraic[5] algebraic[16] = constants[26]*(exp(algebraic[14]/(constants[0]*constants[1]))-exp(algebraic[15]/(constants[0]*constants[1]))) algebraic[17] = algebraic[12]-algebraic[16] rates[3] = algebraic[17] algebraic[18] = algebraic[5] algebraic[6] = constants[0]*constants[1]*log(constants[17]*states[5]) algebraic[19] = 2.00000*constants[39]+algebraic[6] algebraic[20] = constants[27]*(exp(algebraic[18]/(constants[0]*constants[1]))-exp(algebraic[19]/(constants[0]*constants[1]))) algebraic[21] = algebraic[2] algebraic[22] = algebraic[5] algebraic[23] = constants[29]*(exp(algebraic[21]/(constants[0]*constants[1]))-exp(algebraic[22]/(constants[0]*constants[1]))) algebraic[25] = (algebraic[16]-algebraic[20])+algebraic[23] rates[4] = algebraic[25] algebraic[0] = constants[0]*constants[1]*log(constants[7]*states[0]) algebraic[24] = states[6] algebraic[26] = algebraic[24] algebraic[27] = algebraic[26] algebraic[28] = constants[19]*constants[2]*algebraic[27] algebraic[35] = (2.00000*constants[41]+algebraic[0])-algebraic[28] algebraic[29] = algebraic[26] algebraic[30] = constants[20]*constants[2]*algebraic[29] algebraic[36] = algebraic[2]+algebraic[30] algebraic[37] = constants[23]*(exp(algebraic[35]/(constants[0]*constants[1]))-exp(algebraic[36]/(constants[0]*constants[1]))) algebraic[38] = (algebraic[37]-algebraic[9])-algebraic[23] rates[1] = algebraic[38] algebraic[31] = algebraic[26] algebraic[32] = constants[21]*constants[2]*algebraic[31] algebraic[43] = algebraic[6]-algebraic[32] algebraic[33] = algebraic[26] algebraic[34] = constants[22]*constants[2]*algebraic[33] algebraic[44] = algebraic[0]+algebraic[34] algebraic[45] = constants[28]*(exp(algebraic[43]/(constants[0]*constants[1]))-exp(algebraic[44]/(constants[0]*constants[1]))) algebraic[46] = -algebraic[37]+algebraic[45] rates[0] = algebraic[46] algebraic[47] = algebraic[20]-algebraic[45] rates[5] = algebraic[47] return(rates) def computeAlgebraic(constants, states, voi): algebraic = array([[0.0] * len(voi)] * sizeAlgebraic) states = array(states) voi = array(voi) algebraic[1] = custom_piecewise([less(voi , 1.20475), 0.00000 , greater_equal(voi , 1.20475) & less(voi , 1.20475+constants[37]), (constants[36]+0.0500000)/constants[37] , greater_equal(voi , 1.20475+constants[37]) & less(voi , 2.98475), 0.00000 , greater_equal(voi , 2.98475) & less(voi , 2.98475+constants[37]), -(constants[36]+0.0500000)/constants[37] , True, 0.00000]) algebraic[2] = constants[0]*constants[1]*log(constants[9]*states[1]) algebraic[7] = constants[45]+algebraic[2] algebraic[3] = constants[0]*constants[1]*log(constants[11]*states[2]) algebraic[8] = algebraic[3] algebraic[9] = constants[24]*(exp(algebraic[7]/(constants[0]*constants[1]))-exp(algebraic[8]/(constants[0]*constants[1]))) algebraic[10] = algebraic[3] algebraic[4] = constants[0]*constants[1]*log(constants[13]*states[3]) algebraic[11] = algebraic[4] algebraic[12] = constants[25]*(exp(algebraic[10]/(constants[0]*constants[1]))-exp(algebraic[11]/(constants[0]*constants[1]))) algebraic[13] = algebraic[9]-algebraic[12] algebraic[14] = algebraic[4] algebraic[5] = constants[0]*constants[1]*log(constants[15]*states[4]) algebraic[15] = constants[43]+algebraic[5] algebraic[16] = constants[26]*(exp(algebraic[14]/(constants[0]*constants[1]))-exp(algebraic[15]/(constants[0]*constants[1]))) algebraic[17] = algebraic[12]-algebraic[16] algebraic[18] = algebraic[5] algebraic[6] = constants[0]*constants[1]*log(constants[17]*states[5]) algebraic[19] = 2.00000*constants[39]+algebraic[6] algebraic[20] = constants[27]*(exp(algebraic[18]/(constants[0]*constants[1]))-exp(algebraic[19]/(constants[0]*constants[1]))) algebraic[21] = algebraic[2] algebraic[22] = algebraic[5] algebraic[23] = constants[29]*(exp(algebraic[21]/(constants[0]*constants[1]))-exp(algebraic[22]/(constants[0]*constants[1]))) algebraic[25] = (algebraic[16]-algebraic[20])+algebraic[23] algebraic[0] = constants[0]*constants[1]*log(constants[7]*states[0]) algebraic[24] = states[6] algebraic[26] = algebraic[24] algebraic[27] = algebraic[26] algebraic[28] = constants[19]*constants[2]*algebraic[27] algebraic[35] = (2.00000*constants[41]+algebraic[0])-algebraic[28] algebraic[29] = algebraic[26] algebraic[30] = constants[20]*constants[2]*algebraic[29] algebraic[36] = algebraic[2]+algebraic[30] algebraic[37] = constants[23]*(exp(algebraic[35]/(constants[0]*constants[1]))-exp(algebraic[36]/(constants[0]*constants[1]))) algebraic[38] = (algebraic[37]-algebraic[9])-algebraic[23] algebraic[31] = algebraic[26] algebraic[32] = constants[21]*constants[2]*algebraic[31] algebraic[43] = algebraic[6]-algebraic[32] algebraic[33] = algebraic[26] algebraic[34] = constants[22]*constants[2]*algebraic[33] algebraic[44] = algebraic[0]+algebraic[34] algebraic[45] = constants[28]*(exp(algebraic[43]/(constants[0]*constants[1]))-exp(algebraic[44]/(constants[0]*constants[1]))) algebraic[46] = -algebraic[37]+algebraic[45] algebraic[47] = algebraic[20]-algebraic[45] algebraic[39] = algebraic[37] algebraic[40] = constants[19]*constants[2]*algebraic[39] algebraic[41] = algebraic[37] algebraic[42] = constants[20]*constants[2]*algebraic[41] algebraic[48] = algebraic[45] algebraic[49] = 2.00000*constants[2]*-algebraic[45] algebraic[50] = constants[21]*constants[2]*algebraic[48] algebraic[51] = algebraic[45] algebraic[52] = constants[22]*constants[2]*algebraic[51] algebraic[53] = algebraic[40]+algebraic[42]+algebraic[50]+algebraic[52] 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)