# Size of variable arrays: sizeAlgebraic = 35 sizeStates = 19 sizeConstants = 40 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] = "q_RB1_inactive in component environment (fmol)" legend_states[1] = "q_L_RB1_inactive in component environment (fmol)" legend_states[2] = "q_LB1_ode in component environment (fmol)" legend_algebraic[0] = "q_LB1_stim in component environment (fmol)" legend_algebraic[3] = "q_LB1 in component environment (fmol)" legend_states[3] = "q_RB1 in component environment (fmol)" legend_states[4] = "q_Gs in component environment (fmol)" legend_states[5] = "q_RB1_Gs in component environment (fmol)" legend_states[6] = "q_L_RB1 in component environment (fmol)" legend_states[7] = "q_L_RB1_Gs in component environment (fmol)" legend_states[8] = "q_Gsa_GTP in component environment (fmol)" legend_states[9] = "q_Gsbetagamma in component environment (fmol)" legend_states[10] = "q_Gsa_GDP in component environment (fmol)" legend_states[11] = "q_GTP in component environment (fmol)" legend_states[12] = "q_GDP in component environment (fmol)" legend_states[13] = "q_Pi in component environment (fmol)" legend_states[14] = "q_RB1_tag in component environment (fmol)" legend_states[15] = "q_L_RB1_tag in component environment (fmol)" legend_states[16] = "q_RB1_GRKArr in component environment (fmol)" legend_states[17] = "q_L_RB1_GRKArr in component environment (fmol)" legend_states[18] = "q_GRKArr in component environment (fmol)" legend_constants[0] = "stimSt in component environment (second)" legend_constants[1] = "stimDur in component environment (second)" legend_constants[2] = "tR in component environment (second)" legend_constants[3] = "stimMag in component environment (fmol)" legend_constants[4] = "stimHolding in component environment (fmol)" legend_constants[38] = "m in component environment (fmol_per_sec)" legend_algebraic[24] = "v_Rswitch_B1 in component GPCR_B1AR_reduced (fmol_per_sec)" legend_algebraic[25] = "v_LRswitch_B1 in component GPCR_B1AR_reduced (fmol_per_sec)" legend_algebraic[26] = "v_C_B1 in component GPCR_B1AR_reduced (fmol_per_sec)" legend_algebraic[27] = "v_R_B1 in component GPCR_B1AR_reduced (fmol_per_sec)" legend_algebraic[28] = "v_L_B1 in component GPCR_B1AR_reduced (fmol_per_sec)" legend_algebraic[29] = "v_Act1_Gs in component GPCR_B1AR_reduced (fmol_per_sec)" legend_algebraic[30] = "v_Act2_Gs in component GPCR_B1AR_reduced (fmol_per_sec)" legend_algebraic[31] = "v_Hyd_Gs in component GPCR_B1AR_reduced (fmol_per_sec)" legend_algebraic[33] = "v_Reassoc_Gs in component GPCR_B1AR_reduced (fmol_per_sec)" legend_algebraic[32] = "v_InternR_B1 in component GPCR_B1AR_reduced (fmol_per_sec)" legend_algebraic[34] = "v_InternLR_B1 in component GPCR_B1AR_reduced (fmol_per_sec)" legend_constants[39] = "v_Rsynthesis in component environment (fmol_per_sec)" legend_algebraic[1] = "conserv_R_T in component environment (fmol)" legend_algebraic[5] = "conserv_L_T in component environment (fmol)" legend_algebraic[2] = "conserv_G_T in component environment (fmol)" legend_constants[5] = "kappa_Rswitch_B1 in component GPCR_B1AR_reduced_parameters (fmol_per_sec)" legend_constants[6] = "kappa_LRswitch_B1 in component GPCR_B1AR_reduced_parameters (fmol_per_sec)" legend_constants[7] = "kappa_C_B1 in component GPCR_B1AR_reduced_parameters (fmol_per_sec)" legend_constants[8] = "kappa_R_B1 in component GPCR_B1AR_reduced_parameters (fmol_per_sec)" legend_constants[9] = "kappa_L_B1 in component GPCR_B1AR_reduced_parameters (fmol_per_sec)" legend_constants[10] = "kappa_Act1_Gs in component GPCR_B1AR_reduced_parameters (fmol_per_sec)" legend_constants[11] = "kappa_Act2_Gs in component GPCR_B1AR_reduced_parameters (fmol_per_sec)" legend_constants[12] = "kappa_Hyd_Gs in component GPCR_B1AR_reduced_parameters (fmol_per_sec)" legend_constants[13] = "kappa_Reassoc_Gs in component GPCR_B1AR_reduced_parameters (fmol_per_sec)" legend_constants[14] = "kappa_InternR_B1 in component GPCR_B1AR_reduced_parameters (fmol_per_sec)" legend_constants[15] = "kappa_InternLR_B1 in component GPCR_B1AR_reduced_parameters (fmol_per_sec)" legend_constants[16] = "K_RB1_inactive in component GPCR_B1AR_reduced_parameters (per_fmol)" legend_constants[17] = "K_L_RB1_inactive in component GPCR_B1AR_reduced_parameters (per_fmol)" legend_constants[18] = "K_LB1 in component GPCR_B1AR_reduced_parameters (per_fmol)" legend_constants[19] = "K_RB1 in component GPCR_B1AR_reduced_parameters (per_fmol)" legend_constants[20] = "K_Gs in component GPCR_B1AR_reduced_parameters (per_fmol)" legend_constants[21] = "K_RB1_Gs in component GPCR_B1AR_reduced_parameters (per_fmol)" legend_constants[22] = "K_L_RB1 in component GPCR_B1AR_reduced_parameters (per_fmol)" legend_constants[23] = "K_L_RB1_Gs in component GPCR_B1AR_reduced_parameters (per_fmol)" legend_constants[24] = "K_Gsa_GTP in component GPCR_B1AR_reduced_parameters (per_fmol)" legend_constants[25] = "K_Gsbetagamma in component GPCR_B1AR_reduced_parameters (per_fmol)" legend_constants[26] = "K_Gsa_GDP in component GPCR_B1AR_reduced_parameters (per_fmol)" legend_constants[27] = "K_GTP in component GPCR_B1AR_reduced_parameters (per_fmol)" legend_constants[28] = "K_GDP in component GPCR_B1AR_reduced_parameters (per_fmol)" legend_constants[29] = "K_Pi in component GPCR_B1AR_reduced_parameters (per_fmol)" legend_constants[30] = "K_RB1_tag in component GPCR_B1AR_reduced_parameters (per_fmol)" legend_constants[31] = "K_L_RB1_tag in component GPCR_B1AR_reduced_parameters (per_fmol)" legend_constants[32] = "K_RB1_GRKArr in component GPCR_B1AR_reduced_parameters (per_fmol)" legend_constants[33] = "K_L_RB1_GRKArr in component GPCR_B1AR_reduced_parameters (per_fmol)" legend_constants[34] = "K_GRKArr in component GPCR_B1AR_reduced_parameters (per_fmol)" legend_constants[35] = "R in component constants (J_per_K_per_mol)" legend_constants[36] = "T in component constants (kelvin)" legend_algebraic[4] = "mu_RB1_inactive in component GPCR_B1AR_reduced (J_per_mol)" legend_algebraic[6] = "mu_L_RB1_inactive in component GPCR_B1AR_reduced (J_per_mol)" legend_algebraic[7] = "mu_LB1 in component GPCR_B1AR_reduced (J_per_mol)" legend_algebraic[8] = "mu_RB1 in component GPCR_B1AR_reduced (J_per_mol)" legend_algebraic[9] = "mu_Gs in component GPCR_B1AR_reduced (J_per_mol)" legend_algebraic[10] = "mu_RB1_Gs in component GPCR_B1AR_reduced (J_per_mol)" legend_algebraic[11] = "mu_L_RB1 in component GPCR_B1AR_reduced (J_per_mol)" legend_algebraic[12] = "mu_L_RB1_Gs in component GPCR_B1AR_reduced (J_per_mol)" legend_algebraic[13] = "mu_Gsa_GTP in component GPCR_B1AR_reduced (J_per_mol)" legend_algebraic[14] = "mu_Gsbetagamma in component GPCR_B1AR_reduced (J_per_mol)" legend_algebraic[15] = "mu_Gsa_GDP in component GPCR_B1AR_reduced (J_per_mol)" legend_algebraic[16] = "mu_GTP in component GPCR_B1AR_reduced (J_per_mol)" legend_algebraic[17] = "mu_GDP in component GPCR_B1AR_reduced (J_per_mol)" legend_algebraic[18] = "mu_Pi in component GPCR_B1AR_reduced (J_per_mol)" legend_algebraic[19] = "mu_RB1_tag in component GPCR_B1AR_reduced (J_per_mol)" legend_algebraic[20] = "mu_L_RB1_tag in component GPCR_B1AR_reduced (J_per_mol)" legend_algebraic[21] = "mu_RB1_GRKArr in component GPCR_B1AR_reduced (J_per_mol)" legend_algebraic[22] = "mu_L_RB1_GRKArr in component GPCR_B1AR_reduced (J_per_mol)" legend_algebraic[23] = "mu_GRKArr in component GPCR_B1AR_reduced (J_per_mol)" legend_constants[37] = "F in component constants (C_per_mol)" legend_rates[0] = "d/dt q_RB1_inactive in component environment (fmol)" legend_rates[1] = "d/dt q_L_RB1_inactive in component environment (fmol)" legend_rates[2] = "d/dt q_LB1_ode in component environment (fmol)" legend_rates[3] = "d/dt q_RB1 in component environment (fmol)" legend_rates[4] = "d/dt q_Gs in component environment (fmol)" legend_rates[5] = "d/dt q_RB1_Gs in component environment (fmol)" legend_rates[6] = "d/dt q_L_RB1 in component environment (fmol)" legend_rates[7] = "d/dt q_L_RB1_Gs in component environment (fmol)" legend_rates[8] = "d/dt q_Gsa_GTP in component environment (fmol)" legend_rates[9] = "d/dt q_Gsbetagamma in component environment (fmol)" legend_rates[10] = "d/dt q_Gsa_GDP in component environment (fmol)" legend_rates[11] = "d/dt q_GTP in component environment (fmol)" legend_rates[12] = "d/dt q_GDP in component environment (fmol)" legend_rates[13] = "d/dt q_Pi in component environment (fmol)" legend_rates[14] = "d/dt q_RB1_tag in component environment (fmol)" legend_rates[15] = "d/dt q_L_RB1_tag in component environment (fmol)" legend_rates[16] = "d/dt q_RB1_GRKArr in component environment (fmol)" legend_rates[17] = "d/dt q_L_RB1_GRKArr in component environment (fmol)" legend_rates[18] = "d/dt q_GRKArr in component environment (fmol)" return (legend_states, legend_algebraic, legend_voi, legend_constants) def initConsts(): constants = [0.0] * sizeConstants; states = [0.0] * sizeStates; states[0] = 0.0004579000e0 states[1] = 1e-18 states[2] = 0 states[3] = 1e-18 states[4] = 0.1455400000 states[5] = 1e-18 states[6] = 1e-18 states[7] = 1e-18 states[8] = 0.01 states[9] = 0.02 states[10] = 0.01 states[11] = 2.2 states[12] = 1.1 states[13] = 570 states[14] = 1e-18 states[15] = 1e-18 states[16] = 1e-18 states[17] = 1e-18 states[18] = 1e-3 constants[0] = 0.5e1 constants[1] = 5e-1 constants[2] = 5e-1 constants[3] = 1e1 constants[4] = 1e-8 constants[5] = 1036.38 constants[6] = 1.83921e-07 constants[7] = 891573 constants[8] = 1.58222e+06 constants[9] = 52417.5 constants[10] = 0.000610519 constants[11] = 0.576677 constants[12] = 0.0661821 constants[13] = 8.60273e-06 constants[14] = 0.00230556 constants[15] = 2.17775 constants[16] = 28.0492 constants[17] = 1.58056e+11 constants[18] = 0.0574759 constants[19] = 0.280492 constants[20] = 0.00337913 constants[21] = 1.07597 constants[22] = 0.158056 constants[23] = 0.00113911 constants[24] = 0.00878478 constants[25] = 736.699 constants[26] = 161.34 constants[27] = 823.314 constants[28] = 3.61368e-10 constants[29] = 7.91408e-11 constants[30] = 0.500146 constants[31] = 0.000529497 constants[32] = 1.26086e-05 constants[33] = 1.33485e-08 constants[34] = 732843 constants[35] = 8.31 constants[36] = 310 constants[37] = 96485 constants[38] = constants[3]/constants[2] constants[39] = 0.000100000 return (states, constants) def computeRates(voi, states, constants): rates = [0.0] * sizeStates; algebraic = [0.0] * sizeAlgebraic algebraic[4] = constants[35]*constants[36]*log(constants[16]*states[0]) algebraic[8] = constants[35]*constants[36]*log(constants[19]*states[3]) algebraic[24] = constants[5]*(exp(algebraic[4]/(constants[35]*constants[36]))-exp(algebraic[8]/(constants[35]*constants[36]))) rates[0] = -algebraic[24]+0.00000*constants[39] algebraic[6] = constants[35]*constants[36]*log(constants[17]*states[1]) algebraic[11] = constants[35]*constants[36]*log(constants[22]*states[6]) algebraic[25] = constants[6]*(exp(algebraic[6]/(constants[35]*constants[36]))-exp(algebraic[11]/(constants[35]*constants[36]))) rates[1] = -algebraic[25] algebraic[0] = custom_piecewise([less(voi , constants[0]) & greater(voi , constants[0]-constants[2]), constants[4]+constants[38]*((voi-constants[0])+constants[2]) , greater_equal(voi , constants[0]) & less(voi , constants[0]+constants[1]), constants[3]+constants[4] , less_equal(voi , constants[0]+constants[2]+constants[1]) & greater_equal(voi , constants[0]+constants[1]), constants[4]+-constants[38]*(((voi-constants[0])-constants[2])-constants[1]) , True, constants[4]]) algebraic[3] = algebraic[0]+states[2] algebraic[7] = constants[35]*constants[36]*log(constants[18]*algebraic[3]) algebraic[28] = constants[9]*(exp((algebraic[8]+algebraic[7])/(constants[35]*constants[36]))-exp(algebraic[11]/(constants[35]*constants[36]))) rates[2] = -algebraic[28] algebraic[9] = constants[35]*constants[36]*log(constants[20]*states[4]) algebraic[10] = constants[35]*constants[36]*log(constants[21]*states[5]) algebraic[26] = constants[7]*(exp((algebraic[8]+algebraic[9])/(constants[35]*constants[36]))-exp(algebraic[10]/(constants[35]*constants[36]))) rates[3] = (algebraic[24]-algebraic[26])-algebraic[28] algebraic[13] = constants[35]*constants[36]*log(constants[24]*states[8]) algebraic[14] = constants[35]*constants[36]*log(constants[25]*states[9]) algebraic[16] = constants[35]*constants[36]*log(constants[27]*states[11]) algebraic[17] = constants[35]*constants[36]*log(constants[28]*states[12]) algebraic[19] = constants[35]*constants[36]*log(constants[30]*states[14]) algebraic[29] = constants[10]*(exp((algebraic[10]+algebraic[16])/(constants[35]*constants[36]))-exp((algebraic[13]+algebraic[14]+algebraic[19]+algebraic[17])/(constants[35]*constants[36]))) rates[5] = algebraic[26]-algebraic[29] algebraic[12] = constants[35]*constants[36]*log(constants[23]*states[7]) algebraic[27] = constants[8]*(exp((algebraic[11]+algebraic[9])/(constants[35]*constants[36]))-exp(algebraic[12]/(constants[35]*constants[36]))) rates[6] = (algebraic[25]-algebraic[27])+algebraic[28] algebraic[20] = constants[35]*constants[36]*log(constants[31]*states[15]) algebraic[30] = constants[11]*(exp((algebraic[12]+algebraic[16])/(constants[35]*constants[36]))-exp((algebraic[13]+algebraic[14]+algebraic[20]+algebraic[17])/(constants[35]*constants[36]))) rates[7] = algebraic[27]-algebraic[30] rates[11] = -algebraic[29]-algebraic[30] rates[12] = algebraic[29]+algebraic[30] algebraic[15] = constants[35]*constants[36]*log(constants[26]*states[10]) algebraic[18] = constants[35]*constants[36]*log(constants[29]*states[13]) algebraic[31] = constants[12]*(exp(algebraic[13]/(constants[35]*constants[36]))-exp((algebraic[15]+algebraic[18])/(constants[35]*constants[36]))) rates[8] = (algebraic[29]+algebraic[30])-algebraic[31] rates[13] = algebraic[31] algebraic[21] = constants[35]*constants[36]*log(constants[32]*states[16]) algebraic[23] = constants[35]*constants[36]*log(constants[34]*states[18]) algebraic[32] = constants[14]*(exp((algebraic[19]+algebraic[23])/(constants[35]*constants[36]))-exp(algebraic[21]/(constants[35]*constants[36]))) rates[14] = algebraic[29]-algebraic[32] rates[16] = algebraic[32] algebraic[33] = constants[13]*(exp((algebraic[15]+algebraic[14])/(constants[35]*constants[36]))-exp(algebraic[9]/(constants[35]*constants[36]))) rates[4] = (-algebraic[26]-algebraic[27])+algebraic[33] rates[9] = (algebraic[29]+algebraic[30])-algebraic[33] rates[10] = algebraic[31]-algebraic[33] algebraic[22] = constants[35]*constants[36]*log(constants[33]*states[17]) algebraic[34] = constants[15]*(exp((algebraic[20]+algebraic[23])/(constants[35]*constants[36]))-exp(algebraic[22]/(constants[35]*constants[36]))) rates[15] = algebraic[30]-algebraic[34] rates[17] = algebraic[34] rates[18] = -algebraic[32]-algebraic[34] return(rates) def computeAlgebraic(constants, states, voi): algebraic = array([[0.0] * len(voi)] * sizeAlgebraic) states = array(states) voi = array(voi) algebraic[4] = constants[35]*constants[36]*log(constants[16]*states[0]) algebraic[8] = constants[35]*constants[36]*log(constants[19]*states[3]) algebraic[24] = constants[5]*(exp(algebraic[4]/(constants[35]*constants[36]))-exp(algebraic[8]/(constants[35]*constants[36]))) algebraic[6] = constants[35]*constants[36]*log(constants[17]*states[1]) algebraic[11] = constants[35]*constants[36]*log(constants[22]*states[6]) algebraic[25] = constants[6]*(exp(algebraic[6]/(constants[35]*constants[36]))-exp(algebraic[11]/(constants[35]*constants[36]))) algebraic[0] = custom_piecewise([less(voi , constants[0]) & greater(voi , constants[0]-constants[2]), constants[4]+constants[38]*((voi-constants[0])+constants[2]) , greater_equal(voi , constants[0]) & less(voi , constants[0]+constants[1]), constants[3]+constants[4] , less_equal(voi , constants[0]+constants[2]+constants[1]) & greater_equal(voi , constants[0]+constants[1]), constants[4]+-constants[38]*(((voi-constants[0])-constants[2])-constants[1]) , True, constants[4]]) algebraic[3] = algebraic[0]+states[2] algebraic[7] = constants[35]*constants[36]*log(constants[18]*algebraic[3]) algebraic[28] = constants[9]*(exp((algebraic[8]+algebraic[7])/(constants[35]*constants[36]))-exp(algebraic[11]/(constants[35]*constants[36]))) algebraic[9] = constants[35]*constants[36]*log(constants[20]*states[4]) algebraic[10] = constants[35]*constants[36]*log(constants[21]*states[5]) algebraic[26] = constants[7]*(exp((algebraic[8]+algebraic[9])/(constants[35]*constants[36]))-exp(algebraic[10]/(constants[35]*constants[36]))) algebraic[13] = constants[35]*constants[36]*log(constants[24]*states[8]) algebraic[14] = constants[35]*constants[36]*log(constants[25]*states[9]) algebraic[16] = constants[35]*constants[36]*log(constants[27]*states[11]) algebraic[17] = constants[35]*constants[36]*log(constants[28]*states[12]) algebraic[19] = constants[35]*constants[36]*log(constants[30]*states[14]) algebraic[29] = constants[10]*(exp((algebraic[10]+algebraic[16])/(constants[35]*constants[36]))-exp((algebraic[13]+algebraic[14]+algebraic[19]+algebraic[17])/(constants[35]*constants[36]))) algebraic[12] = constants[35]*constants[36]*log(constants[23]*states[7]) algebraic[27] = constants[8]*(exp((algebraic[11]+algebraic[9])/(constants[35]*constants[36]))-exp(algebraic[12]/(constants[35]*constants[36]))) algebraic[20] = constants[35]*constants[36]*log(constants[31]*states[15]) algebraic[30] = constants[11]*(exp((algebraic[12]+algebraic[16])/(constants[35]*constants[36]))-exp((algebraic[13]+algebraic[14]+algebraic[20]+algebraic[17])/(constants[35]*constants[36]))) algebraic[15] = constants[35]*constants[36]*log(constants[26]*states[10]) algebraic[18] = constants[35]*constants[36]*log(constants[29]*states[13]) algebraic[31] = constants[12]*(exp(algebraic[13]/(constants[35]*constants[36]))-exp((algebraic[15]+algebraic[18])/(constants[35]*constants[36]))) algebraic[21] = constants[35]*constants[36]*log(constants[32]*states[16]) algebraic[23] = constants[35]*constants[36]*log(constants[34]*states[18]) algebraic[32] = constants[14]*(exp((algebraic[19]+algebraic[23])/(constants[35]*constants[36]))-exp(algebraic[21]/(constants[35]*constants[36]))) algebraic[33] = constants[13]*(exp((algebraic[15]+algebraic[14])/(constants[35]*constants[36]))-exp(algebraic[9]/(constants[35]*constants[36]))) algebraic[22] = constants[35]*constants[36]*log(constants[33]*states[17]) algebraic[34] = constants[15]*(exp((algebraic[20]+algebraic[23])/(constants[35]*constants[36]))-exp(algebraic[22]/(constants[35]*constants[36]))) algebraic[1] = states[0]+states[1]+states[3]+states[5]+states[6]+states[7]+states[14]+states[15]+states[16]+states[17] algebraic[2] = states[4]+states[5]+states[7]+states[8]+states[10] algebraic[5] = states[1]+algebraic[3]+states[6]+states[7]+states[15]+states[17] return algebraic def custom_piecewise(cases): """Compute result of a piecewise function""" return select(cases[0::2],cases[1::2]) 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)