# Size of variable arrays: sizeAlgebraic = 11 sizeStates = 6 sizeConstants = 16 from math import * from numpy import * def createLegends(): legend_states = [""] * sizeStates legend_rates = [""] * sizeStates legend_algebraic = [""] * sizeAlgebraic legend_voi = "" legend_constants = [""] * sizeConstants legend_algebraic[0] = "N_x in component N_x (dimensionless)" legend_constants[0] = "N_0 in component model_parameters (dimensionless)" legend_voi = "x in component model_parameters (mm)" legend_constants[1] = "ksh in component model_parameters (per_mm)" legend_states[0] = "F_DVR_v in component F_DVR_v (nl_min)" legend_algebraic[8] = "Jv in component model_parameters (nl_min_mm)" legend_states[1] = "F_DVR_GLU in component F_DVR_GLU (pmol_min)" legend_algebraic[9] = "JGLU in component JGLU (pmol_min_mm)" legend_states[2] = "F_DVR_LAC in component F_DVR_LAC (pmol_min)" legend_algebraic[10] = "JLAC in component JLAC (pmol_min_mm)" legend_states[3] = "F_AVR_v in component F_AVR_v (nl_min)" legend_algebraic[7] = "J_ABS_V in component J_ABS_V (nl_min_mm)" legend_states[4] = "F_AVR_GLU in component F_AVR_GLU (pmol_min)" legend_algebraic[6] = "JGLY in component JGLY (pmol_min_mm)" legend_states[5] = "F_AVR_LAC in component F_AVR_LAC (pmol_min)" legend_constants[2] = "PGLU in component JGLU (nl_min_mm)" legend_constants[3] = "sigma_GLU in component JGLU (dimensionless)" legend_algebraic[3] = "c_DVR_GLU in component c_DVR_GLU (millimolar)" legend_algebraic[4] = "c_AVR_GLU in component c_AVR_GLU (millimolar)" legend_constants[4] = "PLAC in component JLAC (nl_min_mm)" legend_constants[5] = "sigma_LAC in component JLAC (dimensionless)" legend_algebraic[5] = "c_AVR_LAC in component c_AVR_LAC (millimolar)" legend_algebraic[1] = "c_DVR_LAC in component c_DVR_LAC (millimolar)" legend_constants[15] = "Vmax in component JGLY (pmol_min_mm)" legend_constants[6] = "Km in component JGLY (millimolar)" legend_constants[7] = "GlyFract in component JGLY (dimensionless)" legend_constants[14] = "F_DVR_G_0 in component model_parameters (pmol_min)" legend_constants[8] = "L in component model_parameters (mm)" legend_constants[13] = "kv in component kv (nl_min_mm)" legend_constants[9] = "VolFract in component kv (dimensionless)" legend_constants[12] = "F_DVR_V_0 in component model_parameters (nl_min)" legend_constants[10] = "c_DVR_GLU_0 in component model_parameters (millimolar)" legend_algebraic[2] = "x_L in component model_parameters (dimensionless)" legend_constants[11] = "b in component model_parameters (dimensionless)" legend_rates[0] = "d/dt F_DVR_v in component F_DVR_v (nl_min)" legend_rates[1] = "d/dt F_DVR_GLU in component F_DVR_GLU (pmol_min)" legend_rates[2] = "d/dt F_DVR_LAC in component F_DVR_LAC (pmol_min)" legend_rates[3] = "d/dt F_AVR_v in component F_AVR_v (nl_min)" legend_rates[4] = "d/dt F_AVR_GLU in component F_AVR_GLU (pmol_min)" legend_rates[5] = "d/dt F_AVR_LAC in component F_AVR_LAC (pmol_min)" return (legend_states, legend_algebraic, legend_voi, legend_constants) def initConsts(): constants = [0.0] * sizeConstants; states = [0.0] * sizeStates; constants[0] = 128.0 constants[1] = 1.213 states[0] = 3.75 states[1] = 0.01 states[2] = 0.01 states[3] = 0.01 states[4] = 0.01 states[5] = 0.01 constants[2] = 1.2 constants[3] = 0.5 constants[4] = 33.93 constants[5] = 0.5 constants[6] = 0.1 constants[7] = 0.2 constants[8] = 4.0 constants[9] = 0.3 constants[10] = 10.0 constants[11] = 4.0 constants[12] = 3.75000*constants[0] constants[13] = (constants[1]/(constants[0]*(1.00000-exp(-(constants[1]*constants[8])))))*constants[9]*constants[12] constants[14] = constants[12]*constants[10] constants[15] = (constants[1]/(constants[0]*(1.00000-exp(-(constants[1]*constants[8])))))*(constants[7]*constants[14]) return (states, constants) def computeRates(voi, states, constants): rates = [0.0] * sizeStates; algebraic = [0.0] * sizeAlgebraic algebraic[0] = constants[0]*exp(-(constants[1]*voi)) algebraic[8] = 0.300000*(states[0]/(constants[0]*constants[11]))*algebraic[0] rates[0] = -(algebraic[8]+constants[1]*states[0]) algebraic[7] = constants[13]*algebraic[0] rates[3] = algebraic[8]+constants[1]*states[0]+algebraic[7] algebraic[3] = states[1]/states[0] algebraic[4] = states[4]/states[3] algebraic[9] = algebraic[0]*constants[2]*(algebraic[3]-algebraic[4])+(1.00000-constants[3])*algebraic[8]*((algebraic[3]+algebraic[4])/2.00000) rates[1] = -(algebraic[9]+constants[1]*states[1]) algebraic[5] = states[5]/states[3] algebraic[10] = algebraic[0]*constants[4]*(algebraic[3]-algebraic[5])+(1.00000-constants[5])*algebraic[8]*((algebraic[3]+algebraic[5])/2.00000) rates[2] = -(algebraic[10]+constants[1]*states[2]) algebraic[6] = algebraic[0]*((constants[15]*algebraic[4])/(constants[6]+algebraic[4])) rates[4] = (algebraic[9]+constants[1]*states[1])-algebraic[6] rates[5] = algebraic[10]+constants[1]*states[2]+2.00000*algebraic[6] return(rates) def computeAlgebraic(constants, states, voi): algebraic = array([[0.0] * len(voi)] * sizeAlgebraic) states = array(states) voi = array(voi) algebraic[0] = constants[0]*exp(-(constants[1]*voi)) algebraic[8] = 0.300000*(states[0]/(constants[0]*constants[11]))*algebraic[0] algebraic[7] = constants[13]*algebraic[0] algebraic[3] = states[1]/states[0] algebraic[4] = states[4]/states[3] algebraic[9] = algebraic[0]*constants[2]*(algebraic[3]-algebraic[4])+(1.00000-constants[3])*algebraic[8]*((algebraic[3]+algebraic[4])/2.00000) algebraic[5] = states[5]/states[3] algebraic[10] = algebraic[0]*constants[4]*(algebraic[3]-algebraic[5])+(1.00000-constants[5])*algebraic[8]*((algebraic[3]+algebraic[5])/2.00000) algebraic[6] = algebraic[0]*((constants[15]*algebraic[4])/(constants[6]+algebraic[4])) algebraic[1] = states[2]/states[0] algebraic[2] = voi/constants[8] 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)