# Size of variable arrays: sizeAlgebraic = 5 sizeStates = 2 sizeConstants = 20 from math import * from numpy import * def createLegends(): legend_states = [""] * sizeStates legend_rates = [""] * sizeStates legend_algebraic = [""] * sizeAlgebraic legend_voi = "" legend_constants = [""] * sizeConstants legend_voi = "time in component environment (minute)" legend_states[0] = "G in component G (mg_l)" legend_constants[0] = "Vg in component G (litre)" legend_constants[1] = "Gin in component G (mg_l_min)" legend_algebraic[0] = "f2_G in component G (mg_min)" legend_algebraic[2] = "f3_G in component G (per_l)" legend_algebraic[3] = "f4_I in component G (mg_min)" legend_algebraic[4] = "f5_I in component G (mg_min)" legend_constants[2] = "C2 in component G (mg_l)" legend_constants[3] = "C3 in component G (mg_l)" legend_constants[4] = "C4 in component G (mU_l)" legend_constants[5] = "C5 in component G (mU_l)" legend_constants[6] = "U0 in component G (mg_min)" legend_constants[7] = "Um in component G (mg_min)" legend_constants[8] = "Ub in component G (mg_min)" legend_constants[9] = "beta in component G (dimensionless)" legend_constants[10] = "Rg in component G (mg_min)" legend_constants[11] = "alpha in component G (l_mU)" legend_constants[12] = "ti in component G (minute)" legend_constants[13] = "E in component G (l_min)" legend_constants[14] = "Vp in component G (litre)" legend_constants[15] = "Vi in component G (litre)" legend_states[1] = "I in component I (mU_l)" legend_algebraic[1] = "f1_G in component I (mU_min)" legend_constants[16] = "Rm in component I (mU_min)" legend_constants[17] = "C1 in component I (mg_l)" legend_constants[18] = "a1 in component I (mg_l)" legend_constants[19] = "di in component I (first_order_rate_constant)" legend_rates[0] = "d/dt G in component G (mg_l)" legend_rates[1] = "d/dt I in component I (mU_l)" return (legend_states, legend_algebraic, legend_voi, legend_constants) def initConsts(): constants = [0.0] * sizeConstants; states = [0.0] * sizeStates; states[0] = 1000.0 constants[0] = 10.0 constants[1] = 0.54 constants[2] = 144.0 constants[3] = 1000.0 constants[4] = 80.0 constants[5] = 26.0 constants[6] = 40.0 constants[7] = 940.0 constants[8] = 72.0 constants[9] = 1.77 constants[10] = 180.0 constants[11] = 0.29 constants[12] = 100 constants[13] = 0.2 constants[14] = 3.0 constants[15] = 11.0 states[1] = 9000.0 constants[16] = 210.0 constants[17] = 2000.0 constants[18] = 300.0 constants[19] = 0.06 return (states, constants) def computeRates(voi, states, constants): rates = [0.0] * sizeStates; algebraic = [0.0] * sizeAlgebraic algebraic[1] = constants[16]/(1.00000+exp(((constants[17]-states[0])/(1.00000*constants[0]))/constants[18])) rates[1] = 1.00000*algebraic[1]-constants[19]*states[1] algebraic[0] = constants[8]*(1.00000-exp(1.00000*(-states[0]/(constants[2]*constants[0])))) algebraic[2] = states[0]/(constants[3]*constants[0]) algebraic[3] = constants[6]+(constants[7]-constants[6])/(1.00000+exp(-constants[9]*log(1.00000*(states[1]/(constants[4]*(1.00000/constants[15]+1.00000/(constants[13]*constants[12]))))))) algebraic[4] = constants[10]/(1.00000+exp(constants[11]*(states[1]/(1.00000*constants[14]-1.00000*constants[5])))) rates[0] = constants[1]+1.00000*algebraic[4]+-(1.00000*algebraic[0]+algebraic[2]*algebraic[3]) return(rates) def computeAlgebraic(constants, states, voi): algebraic = array([[0.0] * len(voi)] * sizeAlgebraic) states = array(states) voi = array(voi) algebraic[1] = constants[16]/(1.00000+exp(((constants[17]-states[0])/(1.00000*constants[0]))/constants[18])) algebraic[0] = constants[8]*(1.00000-exp(1.00000*(-states[0]/(constants[2]*constants[0])))) algebraic[2] = states[0]/(constants[3]*constants[0]) algebraic[3] = constants[6]+(constants[7]-constants[6])/(1.00000+exp(-constants[9]*log(1.00000*(states[1]/(constants[4]*(1.00000/constants[15]+1.00000/(constants[13]*constants[12]))))))) algebraic[4] = constants[10]/(1.00000+exp(constants[11]*(states[1]/(1.00000*constants[14]-1.00000*constants[5])))) 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)