# Size of variable arrays: sizeAlgebraic = 26 sizeStates = 17 sizeConstants = 49 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[0] = "A1 in component A1 (molar_per_second)" legend_algebraic[9] = "A2 in component A2 (molar_per_second)" legend_algebraic[10] = "A3 in component A3 (molar_per_second)" legend_algebraic[17] = "A4 in component A4 (molar_per_second)" legend_algebraic[21] = "A5 in component A5 (molar_per_second)" legend_algebraic[23] = "A6 in component A6 (molar_per_second)" legend_algebraic[4] = "R1 in component R1 (molar_per_second)" legend_algebraic[8] = "R2 in component R2 (molar_per_second)" legend_algebraic[11] = "R3 in component R3 (molar_per_second)" legend_algebraic[18] = "R4 in component R4 (molar_per_second)" legend_algebraic[20] = "R5 in component R5 (molar_per_second)" legend_algebraic[24] = "R6 in component R6 (molar_per_second)" legend_algebraic[3] = "T1 in component T1 (molar_per_second)" legend_algebraic[14] = "T2 in component T2 (molar_per_second)" legend_algebraic[7] = "T3 in component T3 (molar_per_second)" legend_algebraic[19] = "T4 in component T4 (molar_per_second)" legend_algebraic[5] = "P1 in component P1 (molar_per_second)" legend_algebraic[15] = "P2 in component P2 (molar_per_second)" legend_algebraic[12] = "P3 in component P3 (molar_per_second)" legend_algebraic[22] = "P4 in component P4 (molar_per_second)" legend_algebraic[6] = "D1 in component D1 (molar_per_second)" legend_algebraic[16] = "D2 in component D2 (molar_per_second)" legend_algebraic[13] = "D3 in component D3 (molar_per_second)" legend_algebraic[25] = "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 A1 (per_molar_per_second)" legend_constants[6] = "k2 in component A1 (per_second)" legend_constants[7] = "k1 in component T1 (per_molar_per_second)" legend_constants[8] = "k2 in component T1 (per_second)" legend_constants[9] = "k1 in component R1 (per_molar_per_second)" legend_constants[10] = "k2 in component R1 (per_second)" legend_constants[11] = "k2 in component P1 (per_molar_per_second)" legend_constants[12] = "k1 in component D1 (per_second)" legend_constants[13] = "k2 in component D1 (per_molar_per_second)" legend_constants[14] = "k1 in component T3 (per_molar_per_second)" legend_constants[15] = "k2 in component T3 (per_second)" legend_constants[16] = "k1 in component R2 (per_molar_per_second)" legend_constants[17] = "k2 in component R2 (per_second)" legend_constants[18] = "k1 in component A2 (per_molar_per_second)" legend_constants[19] = "k2 in component A2 (per_second)" legend_constants[20] = "k1 in component A3 (per_molar_per_second)" legend_constants[21] = "k2 in component A3 (per_second)" legend_constants[22] = "k1 in component R3 (per_molar_per_second)" legend_constants[23] = "k2 in component R3 (per_second)" legend_constants[24] = "k2 in component P3 (per_molar_per_second)" legend_constants[25] = "k1 in component D3 (per_second)" legend_constants[26] = "k2 in component D3 (per_molar_per_second)" legend_constants[27] = "k1 in component T2 (per_molar_per_second)" legend_constants[28] = "k2 in component T2 (per_second)" legend_constants[29] = "k2 in component P2 (per_molar_per_second)" legend_constants[30] = "k1 in component D2 (per_second)" legend_constants[31] = "k2 in component D2 (per_molar_per_second)" legend_constants[32] = "k1 in component A4 (per_molar_per_second)" legend_constants[33] = "k2 in component A4 (per_second)" legend_constants[34] = "k1 in component R4 (per_molar_per_second)" legend_constants[35] = "k2 in component R4 (per_second)" legend_constants[36] = "k1 in component T4 (per_molar_per_second)" legend_constants[37] = "k2 in component T4 (per_second)" legend_constants[38] = "k1 in component R5 (per_molar_per_second)" legend_constants[39] = "k2 in component R5 (per_second)" legend_constants[40] = "k1 in component A5 (per_molar_per_second)" legend_constants[41] = "k2 in component A5 (per_second)" legend_constants[42] = "k2 in component P4 (per_molar_per_second)" legend_constants[43] = "k1 in component A6 (per_molar_per_second)" legend_constants[44] = "k2 in component A6 (per_second)" legend_constants[45] = "k1 in component R6 (per_molar_per_second)" legend_constants[46] = "k2 in component R6 (per_second)" legend_constants[47] = "k1 in component D4 (per_second)" legend_constants[48] = "k2 in component D4 (per_molar_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] = 1e-6 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] = 8780000 constants[6] = 8 constants[7] = 529000 constants[8] = 8.38e-6 constants[9] = 636000000 constants[10] = 0.0179 constants[11] = 9.03e-7 constants[12] = 0.0001 constants[13] = 62.3 constants[14] = 853000 constants[15] = 0.00468 constants[16] = 132000000 constants[17] = 1.28 constants[18] = 386000 constants[19] = 0.0408 constants[20] = 64100 constants[21] = 0.95 constants[22] = 94700000 constants[23] = 0.00227 constants[24] = 2.22e-9 constants[25] = 2 constants[26] = 1470000 constants[27] = 44700 constants[28] = 8.32e-8 constants[29] = 0.244 constants[30] = 0.0001 constants[31] = 3.83 constants[32] = 74300 constants[33] = 0.00572 constants[34] = 22800000 constants[35] = 5.43e-5 constants[36] = 1620000 constants[37] = 0.00875 constants[38] = 6200000 constants[39] = 0.0433 constants[40] = 6300000 constants[41] = 0.478 constants[42] = 0.00297 constants[43] = 13000 constants[44] = 0.685 constants[45] = 49400000 constants[46] = 0.00421 constants[47] = 2.75 constants[48] = 2940 return (states, constants) def computeRates(voi, states, constants): rates = [0.0] * sizeStates; algebraic = [0.0] * sizeAlgebraic algebraic[0] = constants[5]*states[1]*states[0]-constants[6]*states[2] algebraic[4] = constants[9]*states[1]*states[4]-constants[10]*states[9] algebraic[3] = constants[7]*states[1]*states[3]-constants[8]*states[5] algebraic[6] = constants[12]*states[6]-constants[13]*states[1]*states[8] rates[1] = algebraic[6]-(algebraic[0]+algebraic[3]+algebraic[4]) algebraic[9] = constants[18]*states[5]*states[0]-constants[19]*states[11] algebraic[8] = constants[16]*states[5]*states[4]-constants[17]*states[10] algebraic[5] = constants[1]*states[5]-constants[11]*states[6]*states[7] rates[5] = ((algebraic[3]-algebraic[5])-algebraic[8])-algebraic[9] algebraic[10] = constants[20]*states[6]*states[0]-constants[21]*states[12] algebraic[11] = constants[22]*states[6]*states[4]-constants[23]*states[13] rates[6] = ((algebraic[5]-algebraic[6])-algebraic[10])-algebraic[11] algebraic[17] = constants[32]*states[9]*states[0]-constants[33]*states[14] algebraic[7] = constants[14]*states[9]*states[3]-constants[15]*states[10] algebraic[13] = constants[25]*states[13]-constants[26]*states[9]*states[8] rates[9] = ((algebraic[4]-algebraic[7])+algebraic[13])-algebraic[17] algebraic[18] = constants[34]*states[2]*states[4]-constants[35]*states[14] algebraic[14] = constants[27]*states[2]*states[3]-constants[28]*states[11] algebraic[16] = constants[30]*states[12]-constants[31]*states[2]*states[8] rates[2] = ((algebraic[0]-algebraic[14])+algebraic[16])-algebraic[18] algebraic[19] = constants[36]*states[14]*states[3]-constants[37]*states[15] rates[3] = -(algebraic[3]+algebraic[7]+algebraic[14]+algebraic[19]) algebraic[20] = constants[38]*states[11]*states[4]-constants[39]*states[15] algebraic[15] = constants[2]*states[11]-constants[29]*states[12]*states[7] rates[11] = ((algebraic[9]+algebraic[14])-algebraic[15])-algebraic[20] algebraic[21] = constants[40]*states[10]*states[0]-constants[41]*states[15] algebraic[12] = constants[3]*states[10]-constants[24]*states[13]*states[7] rates[10] = ((algebraic[7]+algebraic[8])-algebraic[12])-algebraic[21] algebraic[22] = constants[4]*states[15]-constants[42]*states[16]*states[7] rates[7] = algebraic[5]+algebraic[12]+algebraic[15]+algebraic[22] rates[15] = (algebraic[19]+algebraic[20]+algebraic[21])-algebraic[22] algebraic[23] = constants[43]*states[13]*states[0]-constants[44]*states[16] rates[0] = -(algebraic[0]+algebraic[9]+algebraic[10]+algebraic[17]+algebraic[21]+algebraic[23]) rates[13] = ((algebraic[11]+algebraic[12])-algebraic[13])-algebraic[23] algebraic[24] = constants[45]*states[12]*states[4]-constants[46]*states[16] rates[4] = -(algebraic[4]+algebraic[8]+algebraic[11]+algebraic[18]+algebraic[20]+algebraic[24]) rates[12] = ((algebraic[10]+algebraic[15])-algebraic[16])-algebraic[24] algebraic[25] = constants[47]*states[16]-constants[48]*states[14]*states[8] rates[8] = algebraic[6]+algebraic[13]+algebraic[16]+algebraic[25] rates[14] = ((algebraic[17]+algebraic[18])-algebraic[19])+algebraic[25] rates[16] = (algebraic[22]+algebraic[23]+algebraic[24])-algebraic[25] 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[0]-constants[6]*states[2] algebraic[4] = constants[9]*states[1]*states[4]-constants[10]*states[9] algebraic[3] = constants[7]*states[1]*states[3]-constants[8]*states[5] algebraic[6] = constants[12]*states[6]-constants[13]*states[1]*states[8] algebraic[9] = constants[18]*states[5]*states[0]-constants[19]*states[11] algebraic[8] = constants[16]*states[5]*states[4]-constants[17]*states[10] algebraic[5] = constants[1]*states[5]-constants[11]*states[6]*states[7] algebraic[10] = constants[20]*states[6]*states[0]-constants[21]*states[12] algebraic[11] = constants[22]*states[6]*states[4]-constants[23]*states[13] algebraic[17] = constants[32]*states[9]*states[0]-constants[33]*states[14] algebraic[7] = constants[14]*states[9]*states[3]-constants[15]*states[10] algebraic[13] = constants[25]*states[13]-constants[26]*states[9]*states[8] algebraic[18] = constants[34]*states[2]*states[4]-constants[35]*states[14] algebraic[14] = constants[27]*states[2]*states[3]-constants[28]*states[11] algebraic[16] = constants[30]*states[12]-constants[31]*states[2]*states[8] algebraic[19] = constants[36]*states[14]*states[3]-constants[37]*states[15] algebraic[20] = constants[38]*states[11]*states[4]-constants[39]*states[15] algebraic[15] = constants[2]*states[11]-constants[29]*states[12]*states[7] algebraic[21] = constants[40]*states[10]*states[0]-constants[41]*states[15] algebraic[12] = constants[3]*states[10]-constants[24]*states[13]*states[7] algebraic[22] = constants[4]*states[15]-constants[42]*states[16]*states[7] algebraic[23] = constants[43]*states[13]*states[0]-constants[44]*states[16] algebraic[24] = constants[45]*states[12]*states[4]-constants[46]*states[16] algebraic[25] = constants[47]*states[16]-constants[48]*states[14]*states[8] 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)