# Size of variable arrays: sizeAlgebraic = 31 sizeStates = 14 sizeConstants = 31 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] = "vol_myo in component environment (pL)" legend_states[0] = "q_DAG in component environment (fmol)" legend_states[1] = "q_PC in component environment (fmol)" legend_states[2] = "q_IP3 in component environment (fmol)" legend_states[3] = "q_Ino in component environment (fmol)" legend_states[4] = "q_PLC in component environment (fmol)" legend_states[5] = "q_Gq in component environment (fmol)" legend_states[6] = "q_Ca in component environment (fmol)" legend_states[7] = "q_Gq_PLC in component environment (fmol)" legend_states[8] = "q_Ca_PLC in component environment (fmol)" legend_states[9] = "q_Ca_Gq_PLC in component environment (fmol)" legend_states[10] = "q_G_GDP in component environment (fmol)" legend_states[11] = "q_PIP2 in component environment (fmol)" legend_states[12] = "q_CaPLC_PIP2 in component environment (fmol)" legend_states[13] = "q_CaGqPLC_PIP2 in component environment (fmol)" legend_algebraic[20] = "v_R0_PLC in component PLC (fmol_per_sec)" legend_algebraic[21] = "v_R1_PLC in component PLC (fmol_per_sec)" legend_algebraic[22] = "v_R2a_PLC in component PLC (fmol_per_sec)" legend_algebraic[23] = "v_R2b_PLC in component PLC (fmol_per_sec)" legend_algebraic[24] = "v_R3a_PLC in component PLC (fmol_per_sec)" legend_algebraic[25] = "v_R3b_PLC in component PLC (fmol_per_sec)" legend_algebraic[26] = "v_R4_PLC in component PLC (fmol_per_sec)" legend_algebraic[27] = "v_R5_PLC in component PLC (fmol_per_sec)" legend_algebraic[28] = "v_R6_PLC in component PLC (fmol_per_sec)" legend_algebraic[29] = "v_R7_PLC in component PLC (fmol_per_sec)" legend_algebraic[30] = "v_R8_PLC in component PLC (fmol_per_sec)" legend_algebraic[5] = "Gq_stim in component environment (fmol_per_sec)" legend_constants[1] = "stimPeriod in component environment (second)" legend_constants[2] = "stimDuration in component environment (second)" legend_algebraic[0] = "tPeriod in component environment (second)" legend_algebraic[1] = "cGq in component environment (mM)" legend_algebraic[2] = "cCa in component environment (mM)" legend_algebraic[3] = "cDAG in component environment (mM)" legend_algebraic[4] = "cIP3 in component environment (mM)" legend_constants[3] = "kappa_R0_PLC in component PLC_parameters (fmol_per_sec)" legend_constants[4] = "kappa_R1_PLC in component PLC_parameters (fmol_per_sec)" legend_constants[5] = "kappa_R2a_PLC in component PLC_parameters (fmol_per_sec)" legend_constants[6] = "kappa_R2b_PLC in component PLC_parameters (fmol_per_sec)" legend_constants[7] = "kappa_R3a_PLC in component PLC_parameters (fmol_per_sec)" legend_constants[8] = "kappa_R3b_PLC in component PLC_parameters (fmol_per_sec)" legend_constants[9] = "kappa_R4_PLC in component PLC_parameters (fmol_per_sec)" legend_constants[10] = "kappa_R5_PLC in component PLC_parameters (fmol_per_sec)" legend_constants[11] = "kappa_R6_PLC in component PLC_parameters (fmol_per_sec)" legend_constants[12] = "kappa_R7_PLC in component PLC_parameters (fmol_per_sec)" legend_constants[13] = "kappa_R8_PLC in component PLC_parameters (fmol_per_sec)" legend_constants[14] = "K_DAG in component PLC_parameters (per_fmol)" legend_constants[15] = "K_PC in component PLC_parameters (per_fmol)" legend_constants[16] = "K_IP3 in component PLC_parameters (per_fmol)" legend_constants[17] = "K_Ino in component PLC_parameters (per_fmol)" legend_constants[18] = "K_PLC in component PLC_parameters (per_fmol)" legend_constants[19] = "K_Gq in component PLC_parameters (per_fmol)" legend_constants[20] = "K_Ca in component PLC_parameters (per_fmol)" legend_constants[21] = "K_Gq_PLC in component PLC_parameters (per_fmol)" legend_constants[22] = "K_Ca_PLC in component PLC_parameters (per_fmol)" legend_constants[23] = "K_Ca_Gq_PLC in component PLC_parameters (per_fmol)" legend_constants[24] = "K_G_GDP in component PLC_parameters (per_fmol)" legend_constants[25] = "K_PIP2 in component PLC_parameters (per_fmol)" legend_constants[26] = "K_CaPLC_PIP2 in component PLC_parameters (per_fmol)" legend_constants[27] = "K_CaGqPLC_PIP2 in component PLC_parameters (per_fmol)" legend_constants[28] = "R in component constants (J_per_K_per_mol)" legend_constants[29] = "T in component constants (kelvin)" legend_algebraic[6] = "mu_DAG in component PLC (J_per_mol)" legend_algebraic[7] = "mu_PC in component PLC (J_per_mol)" legend_algebraic[8] = "mu_IP3 in component PLC (J_per_mol)" legend_algebraic[9] = "mu_Ino in component PLC (J_per_mol)" legend_algebraic[10] = "mu_PLC in component PLC (J_per_mol)" legend_algebraic[11] = "mu_Gq in component PLC (J_per_mol)" legend_algebraic[12] = "mu_Ca in component PLC (J_per_mol)" legend_algebraic[13] = "mu_Gq_PLC in component PLC (J_per_mol)" legend_algebraic[14] = "mu_Ca_PLC in component PLC (J_per_mol)" legend_algebraic[15] = "mu_Ca_Gq_PLC in component PLC (J_per_mol)" legend_algebraic[16] = "mu_G_GDP in component PLC (J_per_mol)" legend_algebraic[17] = "mu_PIP2 in component PLC (J_per_mol)" legend_algebraic[18] = "mu_CaPLC_PIP2 in component PLC (J_per_mol)" legend_algebraic[19] = "mu_CaGqPLC_PIP2 in component PLC (J_per_mol)" legend_constants[30] = "F in component constants (C_per_mol)" legend_rates[0] = "d/dt q_DAG in component environment (fmol)" legend_rates[1] = "d/dt q_PC in component environment (fmol)" legend_rates[2] = "d/dt q_IP3 in component environment (fmol)" legend_rates[3] = "d/dt q_Ino in component environment (fmol)" legend_rates[4] = "d/dt q_PLC in component environment (fmol)" legend_rates[5] = "d/dt q_Gq in component environment (fmol)" legend_rates[6] = "d/dt q_Ca in component environment (fmol)" legend_rates[7] = "d/dt q_Gq_PLC in component environment (fmol)" legend_rates[8] = "d/dt q_Ca_PLC in component environment (fmol)" legend_rates[9] = "d/dt q_Ca_Gq_PLC in component environment (fmol)" legend_rates[10] = "d/dt q_G_GDP in component environment (fmol)" legend_rates[11] = "d/dt q_PIP2 in component environment (fmol)" legend_rates[12] = "d/dt q_CaPLC_PIP2 in component environment (fmol)" legend_rates[13] = "d/dt q_CaGqPLC_PIP2 in component environment (fmol)" return (legend_states, legend_algebraic, legend_voi, legend_constants) def initConsts(): constants = [0.0] * sizeConstants; states = [0.0] * sizeStates; constants[0] = 38 states[0] = 0 states[1] = 0 states[2] = 0 states[3] = 0 states[4] = 30.4 states[5] = 38 states[6] = 38 states[7] = 0 states[8] = 0 states[9] = 38 states[10] = 0 states[11] = 380 states[12] = 0 states[13] = 0 constants[1] = 1 constants[2] = 0.01 constants[3] = 0.0700317 constants[4] = 0.178887 constants[5] = 71285.5 constants[6] = 2.73747 constants[7] = 5697.81 constants[8] = 0.00287164 constants[9] = 2.13383 constants[10] = 2.28508 constants[11] = 0.170556 constants[12] = 0.170556 constants[13] = 0.00226839 constants[14] = 0.577328 constants[15] = 0.00384885 constants[16] = 3.76694 constants[17] = 0.00150678 constants[18] = 0.000942826 constants[19] = 0.000141635 constants[20] = 0.000180564 constants[21] = 0.117957 constants[22] = 0.126318 constants[23] = 1.58037 constants[24] = 0.000141635 constants[25] = 8.06835 constants[26] = 18.9051 constants[27] = 938.634 constants[28] = 8.31 constants[29] = 310 constants[30] = 96485 return (states, constants) def computeRates(voi, states, constants): rates = [0.0] * sizeStates; algebraic = [0.0] * sizeAlgebraic algebraic[6] = constants[28]*constants[29]*log(constants[14]*states[0]) algebraic[7] = constants[28]*constants[29]*log(constants[15]*states[1]) algebraic[20] = constants[3]*(exp(algebraic[6]/(constants[28]*constants[29]))-exp(algebraic[7]/(constants[28]*constants[29]))) rates[1] = algebraic[20] algebraic[8] = constants[28]*constants[29]*log(constants[16]*states[2]) algebraic[9] = constants[28]*constants[29]*log(constants[17]*states[3]) algebraic[21] = constants[4]*(exp(algebraic[8]/(constants[28]*constants[29]))-exp(algebraic[9]/(constants[28]*constants[29]))) rates[3] = algebraic[21] algebraic[14] = constants[28]*constants[29]*log(constants[22]*states[8]) algebraic[17] = constants[28]*constants[29]*log(constants[25]*states[11]) algebraic[18] = constants[28]*constants[29]*log(constants[26]*states[12]) algebraic[22] = constants[5]*(exp((algebraic[17]+algebraic[14])/(constants[28]*constants[29]))-exp(algebraic[18]/(constants[28]*constants[29]))) algebraic[23] = constants[6]*(exp(algebraic[18]/(constants[28]*constants[29]))-exp((algebraic[6]+algebraic[8]+algebraic[14])/(constants[28]*constants[29]))) rates[12] = algebraic[22]-algebraic[23] algebraic[15] = constants[28]*constants[29]*log(constants[23]*states[9]) algebraic[19] = constants[28]*constants[29]*log(constants[27]*states[13]) algebraic[24] = constants[7]*(exp((algebraic[17]+algebraic[15])/(constants[28]*constants[29]))-exp(algebraic[19]/(constants[28]*constants[29]))) rates[11] = -algebraic[22]-algebraic[24] algebraic[25] = constants[8]*(exp(algebraic[19]/(constants[28]*constants[29]))-exp((algebraic[6]+algebraic[8]+algebraic[15])/(constants[28]*constants[29]))) rates[0] = (algebraic[23]+algebraic[25])-algebraic[20] rates[2] = (algebraic[23]+algebraic[25])-algebraic[21] rates[13] = algebraic[24]-algebraic[25] algebraic[10] = constants[28]*constants[29]*log(constants[18]*states[4]) algebraic[12] = constants[28]*constants[29]*log(constants[20]*states[6]) algebraic[26] = constants[9]*(exp((algebraic[10]+algebraic[12])/(constants[28]*constants[29]))-exp(algebraic[14]/(constants[28]*constants[29]))) algebraic[11] = constants[28]*constants[29]*log(constants[19]*states[5]) algebraic[13] = constants[28]*constants[29]*log(constants[21]*states[7]) algebraic[27] = constants[10]*(exp((algebraic[10]+algebraic[11])/(constants[28]*constants[29]))-exp(algebraic[13]/(constants[28]*constants[29]))) rates[4] = -algebraic[26]-algebraic[27] algebraic[28] = constants[11]*(exp((algebraic[13]+algebraic[12])/(constants[28]*constants[29]))-exp(algebraic[15]/(constants[28]*constants[29]))) rates[6] = -algebraic[26]-algebraic[28] rates[7] = algebraic[27]-algebraic[28] algebraic[29] = constants[12]*(exp(algebraic[15]/(constants[28]*constants[29]))-exp((algebraic[11]+algebraic[14])/(constants[28]*constants[29]))) algebraic[0] = voi-floor(voi/constants[1])*constants[1] algebraic[5] = custom_piecewise([greater_equal(algebraic[0] , 0.300000) & less_equal(algebraic[0] , 0.300000+constants[2]), 0.00000*100.000 , True, 0.00000]) rates[5] = -algebraic[27]+algebraic[29]+algebraic[5] algebraic[16] = constants[28]*constants[29]*log(constants[24]*states[10]) algebraic[30] = constants[13]*(exp(algebraic[15]/(constants[28]*constants[29]))-exp((algebraic[16]+algebraic[14])/(constants[28]*constants[29]))) rates[8] = -algebraic[22]+algebraic[23]+algebraic[26]+algebraic[29]+algebraic[30] rates[9] = ((-algebraic[24]+algebraic[25]+algebraic[28])-algebraic[29])-algebraic[30] rates[10] = algebraic[30] return(rates) def computeAlgebraic(constants, states, voi): algebraic = array([[0.0] * len(voi)] * sizeAlgebraic) states = array(states) voi = array(voi) algebraic[6] = constants[28]*constants[29]*log(constants[14]*states[0]) algebraic[7] = constants[28]*constants[29]*log(constants[15]*states[1]) algebraic[20] = constants[3]*(exp(algebraic[6]/(constants[28]*constants[29]))-exp(algebraic[7]/(constants[28]*constants[29]))) algebraic[8] = constants[28]*constants[29]*log(constants[16]*states[2]) algebraic[9] = constants[28]*constants[29]*log(constants[17]*states[3]) algebraic[21] = constants[4]*(exp(algebraic[8]/(constants[28]*constants[29]))-exp(algebraic[9]/(constants[28]*constants[29]))) algebraic[14] = constants[28]*constants[29]*log(constants[22]*states[8]) algebraic[17] = constants[28]*constants[29]*log(constants[25]*states[11]) algebraic[18] = constants[28]*constants[29]*log(constants[26]*states[12]) algebraic[22] = constants[5]*(exp((algebraic[17]+algebraic[14])/(constants[28]*constants[29]))-exp(algebraic[18]/(constants[28]*constants[29]))) algebraic[23] = constants[6]*(exp(algebraic[18]/(constants[28]*constants[29]))-exp((algebraic[6]+algebraic[8]+algebraic[14])/(constants[28]*constants[29]))) algebraic[15] = constants[28]*constants[29]*log(constants[23]*states[9]) algebraic[19] = constants[28]*constants[29]*log(constants[27]*states[13]) algebraic[24] = constants[7]*(exp((algebraic[17]+algebraic[15])/(constants[28]*constants[29]))-exp(algebraic[19]/(constants[28]*constants[29]))) algebraic[25] = constants[8]*(exp(algebraic[19]/(constants[28]*constants[29]))-exp((algebraic[6]+algebraic[8]+algebraic[15])/(constants[28]*constants[29]))) algebraic[10] = constants[28]*constants[29]*log(constants[18]*states[4]) algebraic[12] = constants[28]*constants[29]*log(constants[20]*states[6]) algebraic[26] = constants[9]*(exp((algebraic[10]+algebraic[12])/(constants[28]*constants[29]))-exp(algebraic[14]/(constants[28]*constants[29]))) algebraic[11] = constants[28]*constants[29]*log(constants[19]*states[5]) algebraic[13] = constants[28]*constants[29]*log(constants[21]*states[7]) algebraic[27] = constants[10]*(exp((algebraic[10]+algebraic[11])/(constants[28]*constants[29]))-exp(algebraic[13]/(constants[28]*constants[29]))) algebraic[28] = constants[11]*(exp((algebraic[13]+algebraic[12])/(constants[28]*constants[29]))-exp(algebraic[15]/(constants[28]*constants[29]))) algebraic[29] = constants[12]*(exp(algebraic[15]/(constants[28]*constants[29]))-exp((algebraic[11]+algebraic[14])/(constants[28]*constants[29]))) algebraic[0] = voi-floor(voi/constants[1])*constants[1] algebraic[5] = custom_piecewise([greater_equal(algebraic[0] , 0.300000) & less_equal(algebraic[0] , 0.300000+constants[2]), 0.00000*100.000 , True, 0.00000]) algebraic[16] = constants[28]*constants[29]*log(constants[24]*states[10]) algebraic[30] = constants[13]*(exp(algebraic[15]/(constants[28]*constants[29]))-exp((algebraic[16]+algebraic[14])/(constants[28]*constants[29]))) algebraic[1] = states[5]/constants[0] algebraic[2] = states[6]/constants[0] algebraic[3] = states[0]/constants[0] algebraic[4] = states[2]/constants[0] 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)