# Size of variable arrays: sizeAlgebraic = 19 sizeStates = 17 sizeConstants = 18 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 (second)" legend_states[0] = "A in component differentials (molar)" legend_states[1] = "G in component differentials (molar)" legend_states[2] = "GA in component differentials (molar)" legend_states[3] = "T in component differentials (molar)" legend_states[4] = "R in component differentials (molar)" legend_states[5] = "GT in component differentials (molar)" legend_states[6] = "GD in component differentials (molar)" legend_states[7] = "Pi_ in component differentials (molar)" legend_states[8] = "D in component differentials (molar)" legend_states[9] = "RG in component differentials (molar)" legend_states[10] = "RGT in component differentials (molar)" legend_states[11] = "GAT in component differentials (molar)" legend_states[12] = "GAD in component differentials (molar)" legend_states[13] = "RGD in component differentials (molar)" legend_states[14] = "RGA in component differentials (molar)" legend_states[15] = "RGAT in component differentials (molar)" legend_states[16] = "RGAD in component differentials (molar)" legend_algebraic[7] = "A2 in component A2 (molar_per_second)" legend_algebraic[8] = "A3 in component A3 (molar_per_second)" legend_algebraic[16] = "A5 in component A5 (molar_per_second)" legend_algebraic[6] = "R2 in component R2 (molar_per_second)" legend_algebraic[9] = "R3 in component R3 (molar_per_second)" legend_algebraic[14] = "R4 in component R4 (molar_per_second)" legend_algebraic[0] = "T1 in component T1 (molar_per_second)" legend_algebraic[5] = "T3 in component T3 (molar_per_second)" legend_algebraic[15] = "T4 in component T4 (molar_per_second)" legend_algebraic[3] = "P1 in component P1 (molar_per_second)" legend_algebraic[12] = "P2 in component P2 (molar_per_second)" legend_algebraic[10] = "P3 in component P3 (molar_per_second)" legend_algebraic[17] = "P4 in component P4 (molar_per_second)" legend_algebraic[4] = "D1 in component D1 (molar_per_second)" legend_algebraic[13] = "D2 in component D2 (molar_per_second)" legend_algebraic[11] = "D3 in component D3 (molar_per_second)" legend_algebraic[18] = "D4 in component D4 (molar_per_second)" legend_algebraic[1] = "Z in component output (dimensionless)" legend_algebraic[2] = "v in component output (per_second)" legend_constants[0] = "G_tot in component output (molar)" legend_constants[1] = "k1 in component P1 (per_second)" legend_constants[2] = "k1 in component P2 (per_second)" legend_constants[3] = "k1 in component P3 (per_second)" legend_constants[4] = "k1 in component P4 (per_second)" legend_constants[5] = "k1 in component T1 (per_molar_per_second)" legend_constants[6] = "k1 in component D1 (per_second)" legend_constants[7] = "k1 in component T3 (per_molar_per_second)" legend_constants[8] = "k2 in component R2 (per_second)" legend_constants[9] = "k1 in component A2 (per_molar_per_second)" legend_constants[10] = "k2 in component A3 (per_second)" legend_constants[11] = "k1 in component R3 (per_molar_per_second)" legend_constants[12] = "k1 in component D3 (per_second)" legend_constants[13] = "k1 in component D2 (per_second)" legend_constants[14] = "k1 in component R4 (per_molar_per_second)" legend_constants[15] = "k1 in component T4 (per_molar_per_second)" legend_constants[16] = "k1 in component A5 (per_molar_per_second)" legend_constants[17] = "k1 in component D4 (per_second)" legend_rates[0] = "d/dt A in component differentials (molar)" legend_rates[1] = "d/dt G in component differentials (molar)" legend_rates[2] = "d/dt GA in component differentials (molar)" legend_rates[3] = "d/dt T in component differentials (molar)" legend_rates[4] = "d/dt R in component differentials (molar)" legend_rates[5] = "d/dt GT in component differentials (molar)" legend_rates[6] = "d/dt GD in component differentials (molar)" legend_rates[7] = "d/dt Pi_ in component differentials (molar)" legend_rates[8] = "d/dt D in component differentials (molar)" legend_rates[9] = "d/dt RG in component differentials (molar)" legend_rates[10] = "d/dt RGT in component differentials (molar)" legend_rates[11] = "d/dt GAT in component differentials (molar)" legend_rates[12] = "d/dt GAD in component differentials (molar)" legend_rates[13] = "d/dt RGD in component differentials (molar)" legend_rates[14] = "d/dt RGA in component differentials (molar)" legend_rates[15] = "d/dt RGAT in component differentials (molar)" legend_rates[16] = "d/dt RGAD in component differentials (molar)" return (legend_states, legend_algebraic, legend_voi, legend_constants) def initConsts(): constants = [0.0] * sizeConstants; states = [0.0] * sizeStates; states[0] = 0 states[1] = 0 states[2] = 0 states[3] = 0.000468 states[4] = 0 states[5] = 0 states[6] = 1e-8 states[7] = 0.0044 states[8] = 0.000149 states[9] = 0 states[10] = 0 states[11] = 0 states[12] = 0 states[13] = 0 states[14] = 0 states[15] = 0 states[16] = 0 constants[0] = 1e-8 constants[1] = 0.013 constants[2] = 25 constants[3] = 0.013 constants[4] = 25 constants[5] = 529000 constants[6] = 0.0001 constants[7] = 853000 constants[8] = 1.28 constants[9] = 386000 constants[10] = 0.95 constants[11] = 94700000 constants[12] = 2 constants[13] = 0.0001 constants[14] = 22800000 constants[15] = 1620000 constants[16] = 6300000 constants[17] = 2.75 return (states, constants) def computeRates(voi, states, constants): rates = [0.0] * sizeStates; algebraic = [0.0] * sizeAlgebraic algebraic[0] = constants[5]*states[1]*states[3] algebraic[4] = constants[6]*states[6] rates[1] = algebraic[4]-algebraic[0] algebraic[7] = constants[9]*states[5]*states[0] algebraic[6] = -constants[8]*states[10] algebraic[3] = constants[1]*states[5] rates[5] = ((algebraic[0]-algebraic[3])-algebraic[6])-algebraic[7] algebraic[8] = -constants[10]*states[12] algebraic[9] = constants[11]*states[6]*states[4] rates[6] = ((algebraic[3]-algebraic[4])-algebraic[8])-algebraic[9] algebraic[5] = constants[7]*states[9]*states[3] algebraic[11] = constants[12]*states[13] rates[9] = -algebraic[5]+algebraic[11] algebraic[10] = constants[3]*states[10] rates[13] = (algebraic[9]+algebraic[10])-algebraic[11] algebraic[12] = constants[2]*states[11] rates[11] = algebraic[7]-algebraic[12] algebraic[13] = constants[13]*states[12] rates[12] = (algebraic[8]+algebraic[12])-algebraic[13] algebraic[14] = constants[14]*states[2]*states[4] rates[2] = algebraic[13]-algebraic[14] rates[4] = -(algebraic[6]+algebraic[9]+algebraic[14]) algebraic[15] = constants[15]*states[14]*states[3] rates[3] = -(algebraic[0]+algebraic[5]+algebraic[15]) algebraic[16] = constants[16]*states[10]*states[0] rates[0] = -(algebraic[7]+algebraic[8]+algebraic[16]) rates[10] = ((algebraic[5]+algebraic[6])-algebraic[10])-algebraic[16] algebraic[17] = constants[4]*states[15] rates[7] = algebraic[3]+algebraic[10]+algebraic[12]+algebraic[17] rates[15] = (algebraic[15]+algebraic[16])-algebraic[17] algebraic[18] = constants[17]*states[16] rates[8] = algebraic[4]+algebraic[11]+algebraic[13]+algebraic[18] rates[14] = (algebraic[14]-algebraic[15])+algebraic[18] rates[16] = algebraic[17]-algebraic[18] return(rates) def computeAlgebraic(constants, states, voi): algebraic = array([[0.0] * len(voi)] * sizeAlgebraic) states = array(states) voi = array(voi) algebraic[0] = constants[5]*states[1]*states[3] algebraic[4] = constants[6]*states[6] algebraic[7] = constants[9]*states[5]*states[0] algebraic[6] = -constants[8]*states[10] algebraic[3] = constants[1]*states[5] algebraic[8] = -constants[10]*states[12] algebraic[9] = constants[11]*states[6]*states[4] algebraic[5] = constants[7]*states[9]*states[3] algebraic[11] = constants[12]*states[13] algebraic[10] = constants[3]*states[10] algebraic[12] = constants[2]*states[11] algebraic[13] = constants[13]*states[12] algebraic[14] = constants[14]*states[2]*states[4] algebraic[15] = constants[15]*states[14]*states[3] algebraic[16] = constants[16]*states[10]*states[0] algebraic[17] = constants[4]*states[15] algebraic[18] = constants[17]*states[16] algebraic[1] = (states[5]+states[10]+states[15]+states[11])/constants[0] algebraic[2] = (constants[4]*states[15]+constants[2]*states[11]+constants[3]*states[10]+constants[1]*states[5])/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)