# Size of variable arrays: sizeAlgebraic = 28 sizeStates = 9 sizeConstants = 41 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 main (second)" legend_constants[0] = "RT in component main (J_per_mol)" legend_constants[1] = "F in component main (C_per_mol)" legend_constants[2] = "q_w_gut in component main (litre)" legend_constants[3] = "q_w_epi in component main (litre)" legend_constants[4] = "q_w_cap in component main (litre)" legend_constants[33] = "q_w_tot in component main (litre)" legend_states[0] = "q_Na_gut in component main (mole)" legend_states[1] = "q_Na_epi in component main (mole)" legend_constants[5] = "q_Na_cap in component main (mole)" legend_algebraic[0] = "c_Na_gut in component main (mM)" legend_algebraic[3] = "c_Na_epi in component main (mM)" legend_constants[34] = "c_Na_cap in component main (mM)" legend_constants[6] = "K_mm in component main (millimols_per_mol)" legend_algebraic[1] = "q_Na_tot in component main (mole)" legend_states[2] = "q_K_epi in component main (mole)" legend_constants[7] = "q_K_cap in component main (mole)" legend_algebraic[6] = "c_K_epi in component main (mM)" legend_constants[35] = "c_K_cap in component main (mM)" legend_states[3] = "q_Cl_epi in component main (mole)" legend_constants[8] = "q_Cl_cap in component main (mole)" legend_algebraic[8] = "c_Cl_epi in component main (mM)" legend_constants[36] = "c_Cl_cap in component main (mM)" legend_states[4] = "q_e in component main (coulomb)" legend_constants[9] = "u_ee in component main (J_per_C)" legend_algebraic[11] = "u_e in component main (J_per_C)" legend_constants[10] = "C_m in component main (C2_per_J)" legend_constants[11] = "q_Glc_gut in component main (mole)" legend_states[5] = "q_Glc_epi in component main (mole)" legend_constants[12] = "q_Glc_cap in component main (mole)" legend_constants[37] = "c_Glc_gut in component main (mM)" legend_algebraic[12] = "c_Glc_epi in component main (mM)" legend_constants[38] = "c_Glc_cap in component main (mM)" legend_constants[13] = "u_w_gut in component main (kPa)" legend_constants[39] = "u_w_epi in component main (kPa)" legend_constants[14] = "u_w_cap in component main (kPa)" legend_constants[15] = "E_w_epi in component main (joule)" legend_constants[16] = "U_w_epi in component main (litre)" legend_constants[17] = "L_w_epi in component main (litre)" legend_algebraic[4] = "v_w_epiApex in component main (L_per_s)" legend_algebraic[5] = "v_w_epiBase in component main (L_per_s)" legend_constants[18] = "k_w_m in component main (L_per_s_per_kPa)" legend_constants[19] = "c_O2 in component main (mM)" legend_constants[20] = "c_CO2 in component main (mM)" legend_constants[21] = "c_HCO3 in component main (mM)" legend_states[6] = "q_ATP in component main (mole)" legend_states[7] = "q_ADP in component main (mole)" legend_states[8] = "q_Pi in component main (mole)" legend_constants[22] = "q_H in component main (mole)" legend_algebraic[2] = "q_adenosine in component main (mole)" legend_algebraic[13] = "c_ATP in component main (mM)" legend_algebraic[16] = "c_ADP in component main (mM)" legend_algebraic[17] = "c_Pi in component main (mM)" legend_constants[40] = "c_H in component main (mM)" legend_constants[23] = "kappa_SGLT1 in component main (mol_per_s)" legend_algebraic[18] = "v_SGLT1 in component main (mol_per_s)" legend_constants[24] = "kappa_GLUT2 in component main (mol_per_s)" legend_algebraic[20] = "v_GLUT2 in component main (mol_per_s)" legend_constants[25] = "k_ATP_eq in component main (mM2)" legend_constants[26] = "kappa_NKE in component main (mol_per_s)" legend_algebraic[21] = "v_NKE in component main (mol_per_s)" legend_constants[27] = "kappa_NKCC1 in component main (mol_per_s)" legend_algebraic[22] = "v_NKCC1 in component main (mol_per_s)" legend_constants[28] = "kappa_KCC1 in component main (mol_per_s)" legend_algebraic[24] = "v_KCC1 in component main (mol_per_s)" legend_constants[29] = "kappa_K in component main (C_per_J_per_s)" legend_algebraic[26] = "v_K in component main (mol_per_s)" legend_algebraic[25] = "GHKterm in component main (J_per_C)" legend_constants[30] = "kappa_Na in component main (per_s_per_V)" legend_algebraic[27] = "v_Na in component main (mol_per_s)" legend_constants[31] = "k_AM_eq in component main (per_mM)" legend_constants[32] = "kappa_AM in component main (mol_per_s)" legend_algebraic[23] = "v_AM in component main (mol_per_s)" legend_algebraic[14] = "u_SGLT1_reversal in component main (J_per_C)" legend_algebraic[9] = "u_K_reversal in component main (J_per_C)" legend_algebraic[7] = "u_Na_reversal in component main (J_per_C)" legend_algebraic[15] = "c_Na_gutThreshold in component main (mM)" legend_algebraic[19] = "c_Na_epiEquilibrium in component main (mM)" legend_algebraic[10] = "c_Cl_epiEquilibrium in component main (mM)" legend_rates[0] = "d/dt q_Na_gut in component main (mole)" legend_rates[1] = "d/dt q_Na_epi in component main (mole)" legend_rates[2] = "d/dt q_K_epi in component main (mole)" legend_rates[3] = "d/dt q_Cl_epi in component main (mole)" legend_rates[4] = "d/dt q_e in component main (coulomb)" legend_rates[5] = "d/dt q_Glc_epi in component main (mole)" legend_rates[6] = "d/dt q_ATP in component main (mole)" legend_rates[7] = "d/dt q_ADP in component main (mole)" legend_rates[8] = "d/dt q_Pi in component main (mole)" return (legend_states, legend_algebraic, legend_voi, legend_constants) def initConsts(): constants = [0.0] * sizeConstants; states = [0.0] * sizeStates; constants[0] = 2.5e3 constants[1] = 0.965e5 constants[2] = 1 constants[3] = 1 constants[4] = 1 states[0] = 0.1 states[1] = 0.001 constants[5] = 0.140 constants[6] = 1.e3 states[2] = 0.130 constants[7] = 0.004 states[3] = 0.001 constants[8] = 0.100 states[4] = 1e-5 constants[9] = -0.08 constants[10] = 5e4 constants[11] = 0.01 states[5] = 0.001 constants[12] = 0.01 constants[13] = 1 constants[14] = 1 constants[15] = 1 constants[16] = 0.5 constants[17] = 5 constants[18] = 1e-4 constants[19] = 9.25 constants[20] = 1.20 constants[21] = 25.0 states[6] = 0.003 states[7] = 0.0025 states[8] = 0.003 constants[22] = 1e-7 constants[23] = 1e-2 constants[24] = 1e1 constants[25] = 2e4 constants[26] = 1e2 constants[27] = 1e-1 constants[28] = 1e-1 constants[29] = 2e3 constants[30] = 0 constants[31] = 1e0 constants[32] = 1e1 constants[33] = constants[2]+constants[3]+constants[4] constants[34] = (constants[5]*constants[6])/constants[4] constants[35] = (constants[7]*constants[6])/constants[4] constants[36] = (constants[8]*constants[6])/constants[4] constants[37] = (constants[11]*constants[6])/constants[2] constants[38] = (constants[12]*constants[6])/constants[4] constants[39] = (constants[15]*(constants[3]-constants[16]))/(power(constants[17]-constants[3], 2.00000)) constants[40] = (constants[22]*constants[6])/constants[3] return (states, constants) def computeRates(voi, states, constants): rates = [0.0] * sizeStates; algebraic = [0.0] * sizeAlgebraic algebraic[0] = (states[0]*constants[6])/constants[2] algebraic[3] = (states[1]*constants[6])/constants[3] algebraic[11] = states[4]/constants[10] algebraic[12] = (states[5]*constants[6])/constants[3] algebraic[18] = (constants[23]*(((power(algebraic[0]/algebraic[3], 2.00000))*constants[37])/algebraic[12]-exp((2.00000*constants[1]*algebraic[11])/constants[0])))/((1.00000+power(algebraic[0]/20.0000, 2.00000))*(1.00000+power(algebraic[3]/100.000, 2.00000))) rates[0] = -2.00000*algebraic[18] algebraic[20] = (constants[24]*(algebraic[12]/constants[38]-1.00000))/(1.00000+algebraic[12]/5.00000+constants[38]/10.0000+(algebraic[12]*constants[38])/100.000) algebraic[13] = (states[6]*constants[6])/constants[3] algebraic[16] = (states[7]*constants[6])/constants[3] algebraic[23] = constants[32]*((constants[31]*algebraic[12]*algebraic[16])/algebraic[13]-1.00000) rates[5] = (algebraic[18]-algebraic[20])-algebraic[23] algebraic[6] = (states[2]*constants[6])/constants[3] algebraic[17] = (states[8]*constants[6])/constants[3] algebraic[21] = (constants[26]*(((power(algebraic[3]/constants[34], 3.00000))*algebraic[13]*constants[25])/(algebraic[16]*algebraic[17]*constants[40])-(power(algebraic[6]/constants[35], 2.00000))*exp((constants[1]*algebraic[11])/constants[0])))/((1.00000+power(algebraic[3]/1.00000, 3.00000))*(1.00000+power(constants[34]/100.000, 3.00000))) rates[6] = -algebraic[21]+32.0000*algebraic[23] rates[7] = algebraic[21]-32.0000*algebraic[23] rates[8] = algebraic[21]-32.0000*algebraic[23] algebraic[8] = (states[3]*constants[6])/constants[3] algebraic[22] = (constants[27]*((((constants[34]/algebraic[3])*constants[35])/algebraic[6])*(power(constants[36]/algebraic[8], 2.00000))-1.00000))/((1.00000+constants[34]/70.0000)*(1.00000+algebraic[3]/5.00000)*(1.00000+constants[35]/2.00000)*(1.00000+algebraic[6]/60.0000)*(1.00000+power(constants[36]/50.0000, 2.00000))*(1.00000+power(algebraic[8]/10.0000, 2.00000))) algebraic[24] = (constants[28]*(((algebraic[6]/constants[35])*algebraic[8])/constants[36]-1.00000))/((1.00000+constants[36]/50.0000)*(1.00000+constants[35]/2.00000)*(1.00000+algebraic[8]/10.0000)*(1.00000+algebraic[6]/60.0000)) rates[3] = 2.00000*algebraic[22]-algebraic[24] algebraic[25] = algebraic[11]/(exp((constants[1]*algebraic[11])/constants[0])-1.00000) algebraic[26] = constants[29]*(states[2]*exp((constants[1]*algebraic[11])/constants[0])-constants[7])*algebraic[25] rates[2] = ((2.00000*algebraic[21]-algebraic[26])+algebraic[22])-algebraic[24] algebraic[27] = constants[30]*(constants[5]-states[1]*exp((constants[1]*algebraic[11])/constants[0]))*algebraic[25] rates[1] = (2.00000*algebraic[18]-3.00000*algebraic[21])+algebraic[22]+algebraic[27] rates[4] = constants[1]*(((2.00000*algebraic[18]-algebraic[21])-algebraic[26])+algebraic[27]) return(rates) def computeAlgebraic(constants, states, voi): algebraic = array([[0.0] * len(voi)] * sizeAlgebraic) states = array(states) voi = array(voi) algebraic[0] = (states[0]*constants[6])/constants[2] algebraic[3] = (states[1]*constants[6])/constants[3] algebraic[11] = states[4]/constants[10] algebraic[12] = (states[5]*constants[6])/constants[3] algebraic[18] = (constants[23]*(((power(algebraic[0]/algebraic[3], 2.00000))*constants[37])/algebraic[12]-exp((2.00000*constants[1]*algebraic[11])/constants[0])))/((1.00000+power(algebraic[0]/20.0000, 2.00000))*(1.00000+power(algebraic[3]/100.000, 2.00000))) algebraic[20] = (constants[24]*(algebraic[12]/constants[38]-1.00000))/(1.00000+algebraic[12]/5.00000+constants[38]/10.0000+(algebraic[12]*constants[38])/100.000) algebraic[13] = (states[6]*constants[6])/constants[3] algebraic[16] = (states[7]*constants[6])/constants[3] algebraic[23] = constants[32]*((constants[31]*algebraic[12]*algebraic[16])/algebraic[13]-1.00000) algebraic[6] = (states[2]*constants[6])/constants[3] algebraic[17] = (states[8]*constants[6])/constants[3] algebraic[21] = (constants[26]*(((power(algebraic[3]/constants[34], 3.00000))*algebraic[13]*constants[25])/(algebraic[16]*algebraic[17]*constants[40])-(power(algebraic[6]/constants[35], 2.00000))*exp((constants[1]*algebraic[11])/constants[0])))/((1.00000+power(algebraic[3]/1.00000, 3.00000))*(1.00000+power(constants[34]/100.000, 3.00000))) algebraic[8] = (states[3]*constants[6])/constants[3] algebraic[22] = (constants[27]*((((constants[34]/algebraic[3])*constants[35])/algebraic[6])*(power(constants[36]/algebraic[8], 2.00000))-1.00000))/((1.00000+constants[34]/70.0000)*(1.00000+algebraic[3]/5.00000)*(1.00000+constants[35]/2.00000)*(1.00000+algebraic[6]/60.0000)*(1.00000+power(constants[36]/50.0000, 2.00000))*(1.00000+power(algebraic[8]/10.0000, 2.00000))) algebraic[24] = (constants[28]*(((algebraic[6]/constants[35])*algebraic[8])/constants[36]-1.00000))/((1.00000+constants[36]/50.0000)*(1.00000+constants[35]/2.00000)*(1.00000+algebraic[8]/10.0000)*(1.00000+algebraic[6]/60.0000)) algebraic[25] = algebraic[11]/(exp((constants[1]*algebraic[11])/constants[0])-1.00000) algebraic[26] = constants[29]*(states[2]*exp((constants[1]*algebraic[11])/constants[0])-constants[7])*algebraic[25] algebraic[27] = constants[30]*(constants[5]-states[1]*exp((constants[1]*algebraic[11])/constants[0]))*algebraic[25] algebraic[1] = states[0]+states[1]+constants[5] algebraic[2] = states[6]+states[7] algebraic[4] = constants[18]*((constants[13]-constants[39])-(constants[0]/constants[6])*(algebraic[0]-algebraic[3])) algebraic[5] = constants[18]*((constants[39]-constants[14])-(constants[0]/constants[6])*(algebraic[3]-constants[34])) algebraic[7] = (constants[0]/constants[1])*log(constants[34]/algebraic[3]) algebraic[9] = (constants[0]/constants[1])*log(constants[35]/algebraic[6]) algebraic[10] = constants[36]*(power(((constants[34]/algebraic[3])*constants[35])/algebraic[6], 1.0/2)) algebraic[14] = (constants[0]/(2.00000*constants[1]))*log(((power(algebraic[0]/algebraic[3], 2.00000))*constants[37])/algebraic[12]) algebraic[15] = algebraic[3]*(power(algebraic[12]/constants[37], 1.0/2))*exp((constants[1]*algebraic[11])/constants[0]) algebraic[19] = constants[34]*(power(((algebraic[16]/algebraic[13])*algebraic[17]*constants[40])/constants[25], 0.333340))*(power(algebraic[6]/constants[35], 0.666670))*exp((constants[1]*algebraic[11])/(3.00000*constants[0])) 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)