# Size of variable arrays: sizeAlgebraic = 37 sizeStates = 19 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_states[0] = "q_RM2_inactive in component environment (fmol)" legend_states[1] = "q_L_RM2_inactive in component environment (fmol)" legend_algebraic[4] = "q_LM2 in component environment (fmol)" legend_algebraic[3] = "q_LM2_stim in component environment (fmol)" legend_states[2] = "q_LM2_ode in component environment (fmol)" legend_states[3] = "q_RM2 in component environment (fmol)" legend_states[4] = "q_Gi in component environment (fmol)" legend_states[5] = "q_RM2_Gi in component environment (fmol)" legend_states[6] = "q_L_RM2 in component environment (fmol)" legend_states[7] = "q_L_RM2_Gi in component environment (fmol)" legend_states[8] = "q_Gia_GTP in component environment (fmol)" legend_states[9] = "q_Gibetagamma in component environment (fmol)" legend_states[10] = "q_Gia_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_RM2_tag in component environment (fmol)" legend_states[15] = "q_L_RM2_tag in component environment (fmol)" legend_states[16] = "q_RM2_GRKArr in component environment (fmol)" legend_states[17] = "q_L_RM2_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[0] = "conserv_R_T in component environment (fmol)" legend_algebraic[5] = "conserv_L_T in component environment (fmol)" legend_algebraic[1] = "conserv_G_T in component environment (fmol)" legend_algebraic[2] = "total_R in component environment (fmol)" legend_algebraic[25] = "v_Rswitch_M2 in component GPCR_M2_reduced (fmol_per_sec)" legend_algebraic[26] = "v_LRswitch_M2 in component GPCR_M2_reduced (fmol_per_sec)" legend_algebraic[27] = "v_C_M2 in component GPCR_M2_reduced (fmol_per_sec)" legend_algebraic[28] = "v_R_M2 in component GPCR_M2_reduced (fmol_per_sec)" legend_algebraic[29] = "v_L_M2 in component GPCR_M2_reduced (fmol_per_sec)" legend_algebraic[30] = "v_Act1_Gi in component GPCR_M2_reduced (fmol_per_sec)" legend_algebraic[31] = "v_Act2_Gi in component GPCR_M2_reduced (fmol_per_sec)" legend_algebraic[33] = "v_Hyd_Gi in component GPCR_M2_reduced (fmol_per_sec)" legend_algebraic[35] = "v_Reassoc_Gi in component GPCR_M2_reduced (fmol_per_sec)" legend_algebraic[34] = "v_InternR_M2 in component GPCR_M2_reduced (fmol_per_sec)" legend_algebraic[36] = "v_InternLR_M2 in component GPCR_M2_reduced (fmol_per_sec)" legend_algebraic[32] = "v_Rsynthesis in component environment (fmol_per_sec)" legend_constants[5] = "kappa_Rswitch_M2 in component GPCR_M2_reduced_parameters (fmol_per_sec)" legend_constants[6] = "kappa_LRswitch_M2 in component GPCR_M2_reduced_parameters (fmol_per_sec)" legend_constants[7] = "kappa_C_M2 in component GPCR_M2_reduced_parameters (fmol_per_sec)" legend_constants[8] = "kappa_R_M2 in component GPCR_M2_reduced_parameters (fmol_per_sec)" legend_constants[9] = "kappa_L_M2 in component GPCR_M2_reduced_parameters (fmol_per_sec)" legend_constants[10] = "kappa_Act1_Gi in component GPCR_M2_reduced_parameters (fmol_per_sec)" legend_constants[11] = "kappa_Act2_Gi in component GPCR_M2_reduced_parameters (fmol_per_sec)" legend_constants[12] = "kappa_Hyd_Gi in component GPCR_M2_reduced_parameters (fmol_per_sec)" legend_constants[13] = "kappa_Reassoc_Gi in component GPCR_M2_reduced_parameters (fmol_per_sec)" legend_constants[14] = "kappa_InternR_M2 in component GPCR_M2_reduced_parameters (fmol_per_sec)" legend_constants[15] = "kappa_InternLR_M2 in component GPCR_M2_reduced_parameters (fmol_per_sec)" legend_constants[16] = "K_RM2_inactive in component GPCR_M2_reduced_parameters (per_fmol)" legend_constants[17] = "K_L_RM2_inactive in component GPCR_M2_reduced_parameters (per_fmol)" legend_constants[18] = "K_LM2 in component GPCR_M2_reduced_parameters (per_fmol)" legend_constants[19] = "K_RM2 in component GPCR_M2_reduced_parameters (per_fmol)" legend_constants[20] = "K_Gi in component GPCR_M2_reduced_parameters (per_fmol)" legend_constants[21] = "K_RM2_Gi in component GPCR_M2_reduced_parameters (per_fmol)" legend_constants[22] = "K_L_RM2 in component GPCR_M2_reduced_parameters (per_fmol)" legend_constants[23] = "K_L_RM2_Gi in component GPCR_M2_reduced_parameters (per_fmol)" legend_constants[24] = "K_Gia_GTP in component GPCR_M2_reduced_parameters (per_fmol)" legend_constants[25] = "K_Gibetagamma in component GPCR_M2_reduced_parameters (per_fmol)" legend_constants[26] = "K_Gia_GDP in component GPCR_M2_reduced_parameters (per_fmol)" legend_constants[27] = "K_GTP in component GPCR_M2_reduced_parameters (per_fmol)" legend_constants[28] = "K_GDP in component GPCR_M2_reduced_parameters (per_fmol)" legend_constants[29] = "K_Pi in component GPCR_M2_reduced_parameters (per_fmol)" legend_constants[30] = "K_RM2_tag in component GPCR_M2_reduced_parameters (per_fmol)" legend_constants[31] = "K_L_RM2_tag in component GPCR_M2_reduced_parameters (per_fmol)" legend_constants[32] = "K_RM2_GRKArr in component GPCR_M2_reduced_parameters (per_fmol)" legend_constants[33] = "K_L_RM2_GRKArr in component GPCR_M2_reduced_parameters (per_fmol)" legend_constants[34] = "K_GRKArr in component GPCR_M2_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[6] = "mu_RM2_inactive in component GPCR_M2_reduced (J_per_mol)" legend_algebraic[7] = "mu_L_RM2_inactive in component GPCR_M2_reduced (J_per_mol)" legend_algebraic[8] = "mu_LM2 in component GPCR_M2_reduced (J_per_mol)" legend_algebraic[9] = "mu_RM2 in component GPCR_M2_reduced (J_per_mol)" legend_algebraic[10] = "mu_Gi in component GPCR_M2_reduced (J_per_mol)" legend_algebraic[11] = "mu_RM2_Gi in component GPCR_M2_reduced (J_per_mol)" legend_algebraic[12] = "mu_L_RM2 in component GPCR_M2_reduced (J_per_mol)" legend_algebraic[13] = "mu_L_RM2_Gi in component GPCR_M2_reduced (J_per_mol)" legend_algebraic[14] = "mu_Gia_GTP in component GPCR_M2_reduced (J_per_mol)" legend_algebraic[15] = "mu_Gibetagamma in component GPCR_M2_reduced (J_per_mol)" legend_algebraic[16] = "mu_Gia_GDP in component GPCR_M2_reduced (J_per_mol)" legend_algebraic[17] = "mu_GTP in component GPCR_M2_reduced (J_per_mol)" legend_algebraic[18] = "mu_GDP in component GPCR_M2_reduced (J_per_mol)" legend_algebraic[19] = "mu_Pi in component GPCR_M2_reduced (J_per_mol)" legend_algebraic[20] = "mu_RM2_tag in component GPCR_M2_reduced (J_per_mol)" legend_algebraic[21] = "mu_L_RM2_tag in component GPCR_M2_reduced (J_per_mol)" legend_algebraic[22] = "mu_RM2_GRKArr in component GPCR_M2_reduced (J_per_mol)" legend_algebraic[23] = "mu_L_RM2_GRKArr in component GPCR_M2_reduced (J_per_mol)" legend_algebraic[24] = "mu_GRKArr in component GPCR_M2_reduced (J_per_mol)" legend_constants[37] = "F in component constants (C_per_mol)" legend_rates[0] = "d/dt q_RM2_inactive in component environment (fmol)" legend_rates[1] = "d/dt q_L_RM2_inactive in component environment (fmol)" legend_rates[2] = "d/dt q_LM2_ode in component environment (fmol)" legend_rates[3] = "d/dt q_RM2 in component environment (fmol)" legend_rates[4] = "d/dt q_Gi in component environment (fmol)" legend_rates[5] = "d/dt q_RM2_Gi in component environment (fmol)" legend_rates[6] = "d/dt q_L_RM2 in component environment (fmol)" legend_rates[7] = "d/dt q_L_RM2_Gi in component environment (fmol)" legend_rates[8] = "d/dt q_Gia_GTP in component environment (fmol)" legend_rates[9] = "d/dt q_Gibetagamma in component environment (fmol)" legend_rates[10] = "d/dt q_Gia_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_RM2_tag in component environment (fmol)" legend_rates[15] = "d/dt q_L_RM2_tag in component environment (fmol)" legend_rates[16] = "d/dt q_RM2_GRKArr in component environment (fmol)" legend_rates[17] = "d/dt q_L_RM2_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.00072162 states[1] = 1e-18 states[2] = 0 states[3] = 1e-2 states[4] = 0.00836 states[5] = 1e-18 states[6] = 1e-18 states[7] = 1e-18 states[8] = 1e-18 states[9] = 1e-18 states[10] = 1e-18 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-18 constants[0] = 2e1 constants[1] = 2e1 constants[2] = 1e1 constants[3] = 1e2 constants[4] = 1e-8 constants[5] = 3.25763 constants[6] = 0.00165864 constants[7] = 14461.5 constants[8] = 736.312 constants[9] = 5769.58 constants[10] = 0.0114404 constants[11] = 0.381268 constants[12] = 0.585749 constants[13] = 0.0140382 constants[14] = 0.0665538 constants[15] = 2.218 constants[16] = 0.892359 constants[17] = 1752.63 constants[18] = 0.0519039 constants[19] = 0.00892359 constants[20] = 0.0207076 constants[21] = 0.1907 constants[22] = 0.175263 constants[23] = 0.286109 constants[24] = 0.0397027 constants[25] = 0.412109 constants[26] = 0.176744 constants[27] = 0.019367 constants[28] = 0.000190324 constants[29] = 8.16257e-05 constants[30] = 0.200446 constants[31] = 0.00601463 constants[32] = 0.00436786 constants[33] = 0.000131063 constants[34] = 6.3345 constants[35] = 8.31 constants[36] = 310 constants[37] = 96485 constants[38] = constants[3]/constants[2] return (states, constants) def computeRates(voi, states, constants): rates = [0.0] * sizeStates; algebraic = [0.0] * sizeAlgebraic algebraic[7] = constants[35]*constants[36]*log(constants[17]*states[1]) algebraic[12] = constants[35]*constants[36]*log(constants[22]*states[6]) algebraic[26] = constants[6]*(exp(algebraic[7]/(constants[35]*constants[36]))-exp(algebraic[12]/(constants[35]*constants[36]))) rates[1] = -algebraic[26] algebraic[6] = constants[35]*constants[36]*log(constants[16]*states[0]) algebraic[9] = constants[35]*constants[36]*log(constants[19]*states[3]) algebraic[25] = constants[5]*(exp(algebraic[6]/(constants[35]*constants[36]))-exp(algebraic[9]/(constants[35]*constants[36]))) algebraic[3] = 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(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[4] = algebraic[3]+states[2] algebraic[8] = constants[35]*constants[36]*log(constants[18]*algebraic[4]) algebraic[29] = constants[9]*(exp((algebraic[9]+algebraic[8])/(constants[35]*constants[36]))-exp(algebraic[12]/(constants[35]*constants[36]))) rates[0] = -algebraic[25]-algebraic[29] rates[2] = -algebraic[29] algebraic[10] = constants[35]*constants[36]*log(constants[20]*states[4]) algebraic[11] = constants[35]*constants[36]*log(constants[21]*states[5]) algebraic[27] = constants[7]*(exp((algebraic[9]+algebraic[10])/(constants[35]*constants[36]))-exp(algebraic[11]/(constants[35]*constants[36]))) algebraic[14] = constants[35]*constants[36]*log(constants[24]*states[8]) algebraic[15] = constants[35]*constants[36]*log(constants[25]*states[9]) algebraic[17] = constants[35]*constants[36]*log(constants[27]*states[11]) algebraic[18] = constants[35]*constants[36]*log(constants[28]*states[12]) algebraic[20] = constants[35]*constants[36]*log(constants[30]*states[14]) algebraic[30] = constants[10]*(exp((algebraic[11]+algebraic[17])/(constants[35]*constants[36]))-exp((algebraic[14]+algebraic[15]+algebraic[20]+algebraic[18])/(constants[35]*constants[36]))) rates[5] = algebraic[27]-algebraic[30] algebraic[13] = constants[35]*constants[36]*log(constants[23]*states[7]) algebraic[28] = constants[8]*(exp((algebraic[12]+algebraic[10])/(constants[35]*constants[36]))-exp(algebraic[13]/(constants[35]*constants[36]))) rates[6] = -algebraic[28]+algebraic[29]+algebraic[26] algebraic[32] = (algebraic[27])+algebraic[29] rates[3] = (-algebraic[27]-algebraic[29])+1.00000*algebraic[32]+algebraic[25] algebraic[21] = constants[35]*constants[36]*log(constants[31]*states[15]) algebraic[31] = constants[11]*(exp((algebraic[13]+algebraic[17])/(constants[35]*constants[36]))-exp((algebraic[14]+algebraic[15]+algebraic[21]+algebraic[18])/(constants[35]*constants[36]))) rates[7] = algebraic[28]-algebraic[31] rates[11] = -algebraic[30]-algebraic[31] rates[12] = algebraic[30]+algebraic[31] algebraic[16] = constants[35]*constants[36]*log(constants[26]*states[10]) algebraic[19] = constants[35]*constants[36]*log(constants[29]*states[13]) algebraic[33] = constants[12]*(exp(algebraic[14]/(constants[35]*constants[36]))-exp((algebraic[16]+algebraic[19])/(constants[35]*constants[36]))) rates[8] = (algebraic[30]+algebraic[31])-algebraic[33] rates[13] = algebraic[33] algebraic[22] = constants[35]*constants[36]*log(constants[32]*states[16]) algebraic[24] = constants[35]*constants[36]*log(constants[34]*states[18]) algebraic[34] = constants[14]*(exp((algebraic[20]+algebraic[24])/(constants[35]*constants[36]))-exp(algebraic[22]/(constants[35]*constants[36]))) rates[14] = algebraic[30]-algebraic[34] rates[16] = algebraic[34] algebraic[35] = constants[13]*(exp((algebraic[16]+algebraic[15])/(constants[35]*constants[36]))-exp(algebraic[10]/(constants[35]*constants[36]))) rates[4] = (-algebraic[27]-algebraic[28])+algebraic[35] rates[9] = (algebraic[30]+algebraic[31])-algebraic[35] rates[10] = algebraic[33]-algebraic[35] algebraic[23] = constants[35]*constants[36]*log(constants[33]*states[17]) algebraic[36] = constants[15]*(exp((algebraic[21]+algebraic[24])/(constants[35]*constants[36]))-exp(algebraic[23]/(constants[35]*constants[36]))) rates[15] = algebraic[31]-algebraic[36] rates[17] = algebraic[36] rates[18] = -algebraic[34]-algebraic[36] return(rates) def computeAlgebraic(constants, states, voi): algebraic = array([[0.0] * len(voi)] * sizeAlgebraic) states = array(states) voi = array(voi) algebraic[7] = constants[35]*constants[36]*log(constants[17]*states[1]) algebraic[12] = constants[35]*constants[36]*log(constants[22]*states[6]) algebraic[26] = constants[6]*(exp(algebraic[7]/(constants[35]*constants[36]))-exp(algebraic[12]/(constants[35]*constants[36]))) algebraic[6] = constants[35]*constants[36]*log(constants[16]*states[0]) algebraic[9] = constants[35]*constants[36]*log(constants[19]*states[3]) algebraic[25] = constants[5]*(exp(algebraic[6]/(constants[35]*constants[36]))-exp(algebraic[9]/(constants[35]*constants[36]))) algebraic[3] = 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(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[4] = algebraic[3]+states[2] algebraic[8] = constants[35]*constants[36]*log(constants[18]*algebraic[4]) algebraic[29] = constants[9]*(exp((algebraic[9]+algebraic[8])/(constants[35]*constants[36]))-exp(algebraic[12]/(constants[35]*constants[36]))) algebraic[10] = constants[35]*constants[36]*log(constants[20]*states[4]) algebraic[11] = constants[35]*constants[36]*log(constants[21]*states[5]) algebraic[27] = constants[7]*(exp((algebraic[9]+algebraic[10])/(constants[35]*constants[36]))-exp(algebraic[11]/(constants[35]*constants[36]))) algebraic[14] = constants[35]*constants[36]*log(constants[24]*states[8]) algebraic[15] = constants[35]*constants[36]*log(constants[25]*states[9]) algebraic[17] = constants[35]*constants[36]*log(constants[27]*states[11]) algebraic[18] = constants[35]*constants[36]*log(constants[28]*states[12]) algebraic[20] = constants[35]*constants[36]*log(constants[30]*states[14]) algebraic[30] = constants[10]*(exp((algebraic[11]+algebraic[17])/(constants[35]*constants[36]))-exp((algebraic[14]+algebraic[15]+algebraic[20]+algebraic[18])/(constants[35]*constants[36]))) algebraic[13] = constants[35]*constants[36]*log(constants[23]*states[7]) algebraic[28] = constants[8]*(exp((algebraic[12]+algebraic[10])/(constants[35]*constants[36]))-exp(algebraic[13]/(constants[35]*constants[36]))) algebraic[32] = (algebraic[27])+algebraic[29] algebraic[21] = constants[35]*constants[36]*log(constants[31]*states[15]) algebraic[31] = constants[11]*(exp((algebraic[13]+algebraic[17])/(constants[35]*constants[36]))-exp((algebraic[14]+algebraic[15]+algebraic[21]+algebraic[18])/(constants[35]*constants[36]))) algebraic[16] = constants[35]*constants[36]*log(constants[26]*states[10]) algebraic[19] = constants[35]*constants[36]*log(constants[29]*states[13]) algebraic[33] = constants[12]*(exp(algebraic[14]/(constants[35]*constants[36]))-exp((algebraic[16]+algebraic[19])/(constants[35]*constants[36]))) algebraic[22] = constants[35]*constants[36]*log(constants[32]*states[16]) algebraic[24] = constants[35]*constants[36]*log(constants[34]*states[18]) algebraic[34] = constants[14]*(exp((algebraic[20]+algebraic[24])/(constants[35]*constants[36]))-exp(algebraic[22]/(constants[35]*constants[36]))) algebraic[35] = constants[13]*(exp((algebraic[16]+algebraic[15])/(constants[35]*constants[36]))-exp(algebraic[10]/(constants[35]*constants[36]))) algebraic[23] = constants[35]*constants[36]*log(constants[33]*states[17]) algebraic[36] = constants[15]*(exp((algebraic[21]+algebraic[24])/(constants[35]*constants[36]))-exp(algebraic[23]/(constants[35]*constants[36]))) algebraic[0] = states[3]+states[5]+states[6]+states[7]+states[14]+states[15]+states[16]+states[17]+states[0]+states[1] algebraic[1] = states[4]+states[5]+states[7]+states[8]+states[10] algebraic[2] = states[3]+states[5]+states[6]+states[7]+states[0]+states[1] algebraic[5] = algebraic[4]+states[6]+states[7]+states[15]+states[17]+states[1] 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)