# Size of variable arrays: sizeAlgebraic = 36 sizeStates = 14 sizeConstants = 39 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_constants[0] = "q_cAMP_init in component environment (fmol)" legend_constants[1] = "q_PKI_init in component environment (fmol)" legend_constants[2] = "q_PKACI_init in component environment (fmol)" legend_constants[3] = "q_ARCI_init in component environment (fmol)" legend_constants[4] = "q_A2RCI_init in component environment (fmol)" legend_constants[5] = "q_CI_init in component environment (fmol)" legend_constants[6] = "q_A2RI_init in component environment (fmol)" legend_constants[7] = "q_PKACI_PKI_init in component environment (fmol)" legend_constants[8] = "q_PKACII_init in component environment (fmol)" legend_constants[9] = "q_ARCII_init in component environment (fmol)" legend_constants[10] = "q_A2RCII_init in component environment (fmol)" legend_constants[11] = "q_CII_init in component environment (fmol)" legend_constants[12] = "q_A2RII_init in component environment (fmol)" legend_constants[13] = "q_PKACII_PKI_init in component environment (fmol)" legend_algebraic[0] = "q_cAMP in component environment (fmol)" legend_algebraic[1] = "q_PKI in component environment (fmol)" legend_algebraic[2] = "q_PKACI in component environment (fmol)" legend_algebraic[3] = "q_ARCI in component environment (fmol)" legend_algebraic[4] = "q_A2RCI in component environment (fmol)" legend_algebraic[5] = "q_CI in component environment (fmol)" legend_algebraic[6] = "q_A2RI in component environment (fmol)" legend_algebraic[7] = "q_PKACI_PKI in component environment (fmol)" legend_algebraic[8] = "q_PKACII in component environment (fmol)" legend_algebraic[9] = "q_ARCII in component environment (fmol)" legend_algebraic[10] = "q_A2RCII in component environment (fmol)" legend_algebraic[11] = "q_CII in component environment (fmol)" legend_algebraic[12] = "q_A2RII in component environment (fmol)" legend_algebraic[13] = "q_PKACII_PKI in component environment (fmol)" legend_states[0] = "q_cAMP in component PKA (fmol)" legend_states[1] = "q_PKI in component PKA (fmol)" legend_states[2] = "q_PKACI in component PKA (fmol)" legend_states[3] = "q_ARCI in component PKA (fmol)" legend_states[4] = "q_A2RCI in component PKA (fmol)" legend_states[5] = "q_CI in component PKA (fmol)" legend_states[6] = "q_A2RI in component PKA (fmol)" legend_states[7] = "q_PKACI_PKI in component PKA (fmol)" legend_states[8] = "q_PKACII in component PKA (fmol)" legend_states[9] = "q_ARCII in component PKA (fmol)" legend_states[10] = "q_A2RCII in component PKA (fmol)" legend_states[11] = "q_CII in component PKA (fmol)" legend_states[12] = "q_A2RII in component PKA (fmol)" legend_states[13] = "q_PKACII_PKI in component PKA (fmol)" legend_constants[14] = "kappa_aI in component PKA_parameters (fmol_per_sec)" legend_constants[15] = "kappa_bI in component PKA_parameters (fmol_per_sec)" legend_constants[16] = "kappa_dI in component PKA_parameters (fmol_per_sec)" legend_constants[17] = "kappa_iI in component PKA_parameters (fmol_per_sec)" legend_constants[18] = "kappa_aII in component PKA_parameters (fmol_per_sec)" legend_constants[19] = "kappa_bII in component PKA_parameters (fmol_per_sec)" legend_constants[20] = "kappa_dII in component PKA_parameters (fmol_per_sec)" legend_constants[21] = "kappa_iII in component PKA_parameters (fmol_per_sec)" legend_constants[22] = "K_cAMP in component PKA_parameters (per_fmol)" legend_constants[23] = "K_PKI in component PKA_parameters (per_fmol)" legend_constants[24] = "K_PKACI in component PKA_parameters (per_fmol)" legend_constants[25] = "K_ARCI in component PKA_parameters (per_fmol)" legend_constants[26] = "K_A2RCI in component PKA_parameters (per_fmol)" legend_constants[27] = "K_CI in component PKA_parameters (per_fmol)" legend_constants[28] = "K_A2RI in component PKA_parameters (per_fmol)" legend_constants[29] = "K_PKACI_PKI in component PKA_parameters (per_fmol)" legend_constants[30] = "K_PKACII in component PKA_parameters (per_fmol)" legend_constants[31] = "K_ARCII in component PKA_parameters (per_fmol)" legend_constants[32] = "K_A2RCII in component PKA_parameters (per_fmol)" legend_constants[33] = "K_CII in component PKA_parameters (per_fmol)" legend_constants[34] = "K_A2RII in component PKA_parameters (per_fmol)" legend_constants[35] = "K_PKACII_PKI in component PKA_parameters (per_fmol)" legend_constants[36] = "R in component constants (J_per_K_per_mol)" legend_constants[37] = "T in component constants (kelvin)" legend_algebraic[28] = "vaI in component PKA (fmol_per_sec)" legend_algebraic[29] = "vbI in component PKA (fmol_per_sec)" legend_algebraic[30] = "vdI in component PKA (fmol_per_sec)" legend_algebraic[31] = "viI in component PKA (fmol_per_sec)" legend_algebraic[32] = "vaII in component PKA (fmol_per_sec)" legend_algebraic[33] = "vbII in component PKA (fmol_per_sec)" legend_algebraic[34] = "vdII in component PKA (fmol_per_sec)" legend_algebraic[35] = "viII in component PKA (fmol_per_sec)" legend_algebraic[14] = "mu_cAMP in component PKA (J_per_mol)" legend_algebraic[15] = "mu_PKI in component PKA (J_per_mol)" legend_algebraic[16] = "mu_PKACI in component PKA (J_per_mol)" legend_algebraic[17] = "mu_ARCI in component PKA (J_per_mol)" legend_algebraic[18] = "mu_A2RCI in component PKA (J_per_mol)" legend_algebraic[19] = "mu_CI in component PKA (J_per_mol)" legend_algebraic[20] = "mu_A2RI in component PKA (J_per_mol)" legend_algebraic[21] = "mu_PKACI_PKI in component PKA (J_per_mol)" legend_algebraic[22] = "mu_PKACII in component PKA (J_per_mol)" legend_algebraic[23] = "mu_ARCII in component PKA (J_per_mol)" legend_algebraic[24] = "mu_A2RCII in component PKA (J_per_mol)" legend_algebraic[25] = "mu_CII in component PKA (J_per_mol)" legend_algebraic[26] = "mu_A2RII in component PKA (J_per_mol)" legend_algebraic[27] = "mu_PKACII_PKI in component PKA (J_per_mol)" legend_constants[38] = "F in component constants (C_per_mol)" legend_rates[0] = "d/dt q_cAMP in component PKA (fmol)" legend_rates[1] = "d/dt q_PKI in component PKA (fmol)" legend_rates[2] = "d/dt q_PKACI in component PKA (fmol)" legend_rates[3] = "d/dt q_ARCI in component PKA (fmol)" legend_rates[4] = "d/dt q_A2RCI in component PKA (fmol)" legend_rates[5] = "d/dt q_CI in component PKA (fmol)" legend_rates[6] = "d/dt q_A2RI in component PKA (fmol)" legend_rates[7] = "d/dt q_PKACI_PKI in component PKA (fmol)" legend_rates[8] = "d/dt q_PKACII in component PKA (fmol)" legend_rates[9] = "d/dt q_ARCII in component PKA (fmol)" legend_rates[10] = "d/dt q_A2RCII in component PKA (fmol)" legend_rates[11] = "d/dt q_CII in component PKA (fmol)" legend_rates[12] = "d/dt q_A2RII in component PKA (fmol)" legend_rates[13] = "d/dt q_PKACII_PKI in component PKA (fmol)" return (legend_states, legend_algebraic, legend_voi, legend_constants) def initConsts(): constants = [0.0] * sizeConstants; states = [0.0] * sizeStates; constants[0] = 3.212E-02 constants[1] = 6.840E-03 constants[2] = 2.242E-02 constants[3] = 3.800E-15 constants[4] = 3.800E-15 constants[5] = 3.800E-15 constants[6] = 3.800E-15 constants[7] = 3.800E-15 constants[8] = 9.500E-04 constants[9] = 3.800E-15 constants[10] = 3.800E-15 constants[11] = 3.800E-15 constants[12] = 3.800E-15 constants[13] = 3.800E-15 states[0] = 1e-16 states[1] = 1e-16 states[2] = 1e-16 states[3] = 1e-16 states[4] = 1e-16 states[5] = 1e-16 states[6] = 1e-16 states[7] = 1e-16 states[8] = 1e-16 states[9] = 1e-16 states[10] = 1e-16 states[11] = 1e-16 states[12] = 1e-16 states[13] = 1e-16 constants[14] = 48.0417 constants[15] = 21.1895 constants[16] = 4.84331 constants[17] = 149.139 constants[18] = 48.0417 constants[19] = 21.1895 constants[20] = 4.84331 constants[21] = 149.139 constants[22] = 0.0401879 constants[23] = 14.3814 constants[24] = 0.0478876 constants[25] = 0.605095 constants[26] = 1.3719 constants[27] = 1.96997 constants[28] = 0.0885687 constants[29] = 0.194918 constants[30] = 0.0478876 constants[31] = 0.605095 constants[32] = 1.3719 constants[33] = 1.96997 constants[34] = 0.0885687 constants[35] = 0.194918 constants[36] = 8.31 constants[37] = 310 constants[38] = 96485 return (states, constants) def computeRates(voi, states, constants): rates = [0.0] * sizeStates; algebraic = [0.0] * sizeAlgebraic algebraic[0] = states[0]+constants[0] algebraic[14] = constants[36]*constants[37]*log(constants[22]*algebraic[0]) algebraic[2] = states[2]+constants[2] algebraic[16] = constants[36]*constants[37]*log(constants[24]*algebraic[2]) algebraic[3] = states[3]+constants[3] algebraic[17] = constants[36]*constants[37]*log(constants[25]*algebraic[3]) algebraic[28] = constants[14]*(exp((algebraic[16]+algebraic[14])/(constants[36]*constants[37]))-exp(algebraic[17]/(constants[36]*constants[37]))) rates[2] = -algebraic[28] algebraic[4] = states[4]+constants[4] algebraic[18] = constants[36]*constants[37]*log(constants[26]*algebraic[4]) algebraic[29] = constants[15]*(exp((algebraic[17]+algebraic[14])/(constants[36]*constants[37]))-exp(algebraic[18]/(constants[36]*constants[37]))) rates[3] = algebraic[28]-algebraic[29] algebraic[5] = states[5]+constants[5] algebraic[19] = constants[36]*constants[37]*log(constants[27]*algebraic[5]) algebraic[6] = states[6]+constants[6] algebraic[20] = constants[36]*constants[37]*log(constants[28]*algebraic[6]) algebraic[30] = constants[16]*(exp(algebraic[18]/(constants[36]*constants[37]))-exp((algebraic[20]+algebraic[19])/(constants[36]*constants[37]))) rates[4] = algebraic[29]-algebraic[30] rates[6] = algebraic[30] algebraic[1] = states[1]+constants[1] algebraic[15] = constants[36]*constants[37]*log(constants[23]*algebraic[1]) algebraic[7] = states[7]+constants[7] algebraic[21] = constants[36]*constants[37]*log(constants[29]*algebraic[7]) algebraic[31] = constants[17]*(exp((algebraic[19]+algebraic[15])/(constants[36]*constants[37]))-exp(algebraic[21]/(constants[36]*constants[37]))) rates[5] = algebraic[30]-algebraic[31] rates[7] = algebraic[31] algebraic[8] = states[8]+constants[8] algebraic[22] = constants[36]*constants[37]*log(constants[30]*algebraic[8]) algebraic[9] = states[9]+constants[9] algebraic[23] = constants[36]*constants[37]*log(constants[31]*algebraic[9]) algebraic[32] = constants[18]*(exp((algebraic[22]+algebraic[14])/(constants[36]*constants[37]))-exp(algebraic[23]/(constants[36]*constants[37]))) rates[8] = -algebraic[32] algebraic[10] = states[10]+constants[10] algebraic[24] = constants[36]*constants[37]*log(constants[32]*algebraic[10]) algebraic[33] = constants[19]*(exp((algebraic[23]+algebraic[14])/(constants[36]*constants[37]))-exp(algebraic[24]/(constants[36]*constants[37]))) rates[0] = ((-algebraic[28]-algebraic[29])-algebraic[32])-algebraic[33] rates[9] = algebraic[32]-algebraic[33] algebraic[11] = states[11]+constants[11] algebraic[25] = constants[36]*constants[37]*log(constants[33]*algebraic[11]) algebraic[12] = states[12]+constants[12] algebraic[26] = constants[36]*constants[37]*log(constants[34]*algebraic[12]) algebraic[34] = constants[20]*(exp(algebraic[24]/(constants[36]*constants[37]))-exp((algebraic[26]+algebraic[25])/(constants[36]*constants[37]))) rates[10] = algebraic[33]-algebraic[34] rates[12] = algebraic[34] algebraic[13] = states[13]+constants[13] algebraic[27] = constants[36]*constants[37]*log(constants[35]*algebraic[13]) algebraic[35] = constants[21]*(exp((algebraic[25]+algebraic[15])/(constants[36]*constants[37]))-exp(algebraic[27]/(constants[36]*constants[37]))) rates[1] = -algebraic[31]-algebraic[35] rates[11] = algebraic[34]-algebraic[35] rates[13] = algebraic[35] 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[0] algebraic[14] = constants[36]*constants[37]*log(constants[22]*algebraic[0]) algebraic[2] = states[2]+constants[2] algebraic[16] = constants[36]*constants[37]*log(constants[24]*algebraic[2]) algebraic[3] = states[3]+constants[3] algebraic[17] = constants[36]*constants[37]*log(constants[25]*algebraic[3]) algebraic[28] = constants[14]*(exp((algebraic[16]+algebraic[14])/(constants[36]*constants[37]))-exp(algebraic[17]/(constants[36]*constants[37]))) algebraic[4] = states[4]+constants[4] algebraic[18] = constants[36]*constants[37]*log(constants[26]*algebraic[4]) algebraic[29] = constants[15]*(exp((algebraic[17]+algebraic[14])/(constants[36]*constants[37]))-exp(algebraic[18]/(constants[36]*constants[37]))) algebraic[5] = states[5]+constants[5] algebraic[19] = constants[36]*constants[37]*log(constants[27]*algebraic[5]) algebraic[6] = states[6]+constants[6] algebraic[20] = constants[36]*constants[37]*log(constants[28]*algebraic[6]) algebraic[30] = constants[16]*(exp(algebraic[18]/(constants[36]*constants[37]))-exp((algebraic[20]+algebraic[19])/(constants[36]*constants[37]))) algebraic[1] = states[1]+constants[1] algebraic[15] = constants[36]*constants[37]*log(constants[23]*algebraic[1]) algebraic[7] = states[7]+constants[7] algebraic[21] = constants[36]*constants[37]*log(constants[29]*algebraic[7]) algebraic[31] = constants[17]*(exp((algebraic[19]+algebraic[15])/(constants[36]*constants[37]))-exp(algebraic[21]/(constants[36]*constants[37]))) algebraic[8] = states[8]+constants[8] algebraic[22] = constants[36]*constants[37]*log(constants[30]*algebraic[8]) algebraic[9] = states[9]+constants[9] algebraic[23] = constants[36]*constants[37]*log(constants[31]*algebraic[9]) algebraic[32] = constants[18]*(exp((algebraic[22]+algebraic[14])/(constants[36]*constants[37]))-exp(algebraic[23]/(constants[36]*constants[37]))) algebraic[10] = states[10]+constants[10] algebraic[24] = constants[36]*constants[37]*log(constants[32]*algebraic[10]) algebraic[33] = constants[19]*(exp((algebraic[23]+algebraic[14])/(constants[36]*constants[37]))-exp(algebraic[24]/(constants[36]*constants[37]))) algebraic[11] = states[11]+constants[11] algebraic[25] = constants[36]*constants[37]*log(constants[33]*algebraic[11]) algebraic[12] = states[12]+constants[12] algebraic[26] = constants[36]*constants[37]*log(constants[34]*algebraic[12]) algebraic[34] = constants[20]*(exp(algebraic[24]/(constants[36]*constants[37]))-exp((algebraic[26]+algebraic[25])/(constants[36]*constants[37]))) algebraic[13] = states[13]+constants[13] algebraic[27] = constants[36]*constants[37]*log(constants[35]*algebraic[13]) algebraic[35] = constants[21]*(exp((algebraic[25]+algebraic[15])/(constants[36]*constants[37]))-exp(algebraic[27]/(constants[36]*constants[37]))) 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)