# Size of variable arrays: sizeAlgebraic = 31 sizeStates = 12 sizeConstants = 32 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_algebraic[0] = "q_L_B1_init in component environment (fmol)" legend_constants[0] = "q_R_B1_init in component environment (fmol)" legend_constants[1] = "q_Gs_init in component environment (fmol)" legend_constants[2] = "q_LR_B1_init in component environment (fmol)" legend_constants[3] = "q_LR_B1Gs_init in component environment (fmol)" legend_constants[4] = "q_B1d_init in component environment (fmol)" legend_constants[5] = "q_BARK_init in component environment (fmol)" legend_constants[6] = "q_LR_B1_BARK_init in component environment (fmol)" legend_constants[7] = "q_LR_B1Gs_BARK_init in component environment (fmol)" legend_constants[8] = "q_B1p_init in component environment (fmol)" legend_constants[9] = "q_PKACI_init in component environment (fmol)" legend_constants[10] = "q_R_PKACI_init in component environment (fmol)" legend_algebraic[2] = "q_L_B1 in component environment (fmol)" legend_algebraic[1] = "q_R_B1 in component environment (fmol)" legend_algebraic[4] = "q_Gs in component environment (fmol)" legend_algebraic[6] = "q_LR_B1 in component environment (fmol)" legend_algebraic[8] = "q_LR_B1Gs in component environment (fmol)" legend_algebraic[10] = "q_B1d in component environment (fmol)" legend_algebraic[12] = "q_BARK in component environment (fmol)" legend_algebraic[14] = "q_LR_B1_BARK in component environment (fmol)" legend_algebraic[17] = "q_LR_B1Gs_BARK in component environment (fmol)" legend_algebraic[3] = "q_B1p in component environment (fmol)" legend_algebraic[5] = "q_PKACI in component environment (fmol)" legend_algebraic[7] = "q_R_PKACI in component environment (fmol)" legend_states[0] = "q_L_B1 in component B1AR (fmol)" legend_states[1] = "q_R_B1 in component B1AR (fmol)" legend_states[2] = "q_Gs in component B1AR (fmol)" legend_states[3] = "q_LR_B1 in component B1AR (fmol)" legend_states[4] = "q_LR_B1Gs in component B1AR (fmol)" legend_states[5] = "q_B1d in component B1AR (fmol)" legend_states[6] = "q_BARK in component B1AR (fmol)" legend_states[7] = "q_LR_B1_BARK in component B1AR (fmol)" legend_states[8] = "q_LR_B1Gs_BARK in component B1AR (fmol)" legend_states[9] = "q_B1p in component B1AR (fmol)" legend_states[10] = "q_PKACI in component B1AR (fmol)" legend_states[11] = "q_R_PKACI in component B1AR (fmol)" legend_constants[11] = "kappa_B11 in component B1AR_parameters (fmol_per_sec)" legend_constants[12] = "kappa_B12 in component B1AR_parameters (fmol_per_sec)" legend_constants[13] = "kappa_B21 in component B1AR_parameters (fmol_per_sec)" legend_constants[14] = "kappa_B22 in component B1AR_parameters (fmol_per_sec)" legend_constants[15] = "kappa_P1 in component B1AR_parameters (fmol_per_sec)" legend_constants[16] = "kappa_P2 in component B1AR_parameters (fmol_per_sec)" legend_constants[17] = "K_L_B1 in component B1AR_parameters (per_fmol)" legend_constants[18] = "K_R_B1 in component B1AR_parameters (per_fmol)" legend_constants[19] = "K_Gs in component B1AR_parameters (per_fmol)" legend_constants[20] = "K_LR_B1 in component B1AR_parameters (per_fmol)" legend_constants[21] = "K_LR_B1Gs in component B1AR_parameters (per_fmol)" legend_constants[22] = "K_B1d in component B1AR_parameters (per_fmol)" legend_constants[23] = "K_BARK in component B1AR_parameters (per_fmol)" legend_constants[24] = "K_LR_B1_BARK in component B1AR_parameters (per_fmol)" legend_constants[25] = "K_LR_B1Gs_BARK in component B1AR_parameters (per_fmol)" legend_constants[26] = "K_B1p in component B1AR_parameters (per_fmol)" legend_constants[27] = "K_PKACI in component B1AR_parameters (per_fmol)" legend_constants[28] = "K_R_PKACI in component B1AR_parameters (per_fmol)" legend_constants[29] = "R in component constants (J_per_K_per_mol)" legend_constants[30] = "T in component constants (kelvin)" legend_algebraic[27] = "vB11 in component B1AR (fmol_per_sec)" legend_algebraic[28] = "vB12 in component B1AR (fmol_per_sec)" legend_algebraic[29] = "vB21 in component B1AR (fmol_per_sec)" legend_algebraic[30] = "vB22 in component B1AR (fmol_per_sec)" legend_algebraic[16] = "vP1 in component B1AR (fmol_per_sec)" legend_algebraic[18] = "vP2 in component B1AR (fmol_per_sec)" legend_algebraic[19] = "mu_L_B1 in component B1AR (J_per_mol)" legend_algebraic[9] = "mu_R_B1 in component B1AR (J_per_mol)" legend_algebraic[20] = "mu_Gs in component B1AR (J_per_mol)" legend_algebraic[21] = "mu_LR_B1 in component B1AR (J_per_mol)" legend_algebraic[22] = "mu_LR_B1Gs in component B1AR (J_per_mol)" legend_algebraic[23] = "mu_B1d in component B1AR (J_per_mol)" legend_algebraic[24] = "mu_BARK in component B1AR (J_per_mol)" legend_algebraic[25] = "mu_LR_B1_BARK in component B1AR (J_per_mol)" legend_algebraic[26] = "mu_LR_B1Gs_BARK in component B1AR (J_per_mol)" legend_algebraic[11] = "mu_B1p in component B1AR (J_per_mol)" legend_algebraic[13] = "mu_PKACI in component B1AR (J_per_mol)" legend_algebraic[15] = "mu_R_PKACI in component B1AR (J_per_mol)" legend_constants[31] = "F in component constants (C_per_mol)" legend_rates[0] = "d/dt q_L_B1 in component B1AR (fmol)" legend_rates[1] = "d/dt q_R_B1 in component B1AR (fmol)" legend_rates[2] = "d/dt q_Gs in component B1AR (fmol)" legend_rates[3] = "d/dt q_LR_B1 in component B1AR (fmol)" legend_rates[5] = "d/dt q_B1d in component B1AR (fmol)" legend_rates[6] = "d/dt q_BARK in component B1AR (fmol)" legend_rates[7] = "d/dt q_LR_B1_BARK in component B1AR (fmol)" legend_rates[4] = "d/dt q_LR_B1Gs in component B1AR (fmol)" legend_rates[8] = "d/dt q_LR_B1Gs_BARK in component B1AR (fmol)" legend_rates[9] = "d/dt q_B1p in component B1AR (fmol)" legend_rates[10] = "d/dt q_PKACI in component B1AR (fmol)" legend_rates[11] = "d/dt q_R_PKACI in component B1AR (fmol)" return (legend_states, legend_algebraic, legend_voi, legend_constants) def initConsts(): constants = [0.0] * sizeConstants; states = [0.0] * sizeStates; constants[0] = 0.00005 constants[1] = 0.00015 constants[2] = 0 constants[3] = 0.0000508778 constants[4] = 0 constants[5] = 0.0228 constants[6] = 0.0000508778 constants[7] = 0.0000508778 constants[8] = 0 constants[9] = 0.000221209 constants[10] = 0.008778 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 constants[11] = 0.000282928 constants[12] = 128604 constants[13] = 0.000200356 constants[14] = 91071.1 constants[15] = 2.28764e-05 constants[16] = 10398.4 constants[17] = 2.90698e-08 constants[18] = 1849.44 constants[19] = 0.0410501 constants[20] = 0.0242248 constants[21] = 0.0342084 constants[22] = 2.90698e-08 constants[23] = 0.226042 constants[24] = 0.226042 constants[25] = 0.319199 constants[26] = 1.13021e-09 constants[27] = 7.19049e-05 constants[28] = 2.79561 constants[29] = 8.31 constants[30] = 310 constants[31] = 96485 return (states, constants) def computeRates(voi, states, constants): rates = [0.0] * sizeStates; algebraic = [0.0] * sizeAlgebraic algebraic[1] = states[1]+constants[0] algebraic[9] = constants[29]*constants[30]*log(constants[18]*algebraic[1]) algebraic[5] = states[10]+constants[9] algebraic[13] = constants[29]*constants[30]*log(constants[27]*algebraic[5]) algebraic[7] = states[11]+constants[10] algebraic[15] = constants[29]*constants[30]*log(constants[28]*algebraic[7]) algebraic[16] = constants[15]*(exp((algebraic[9]+algebraic[13])/(constants[29]*constants[30]))-exp(algebraic[15]/(constants[29]*constants[30]))) rates[1] = -algebraic[16] rates[9] = -algebraic[16] algebraic[3] = states[9]+constants[8] algebraic[11] = constants[29]*constants[30]*log(constants[26]*algebraic[3]) algebraic[18] = constants[16]*(exp(algebraic[15]/(constants[29]*constants[30]))-exp((algebraic[13]+algebraic[11])/(constants[29]*constants[30]))) rates[10] = algebraic[18]-algebraic[16] rates[11] = algebraic[16]-algebraic[18] algebraic[6] = states[3]+constants[2] algebraic[21] = constants[29]*constants[30]*log(constants[20]*algebraic[6]) algebraic[12] = states[6]+constants[5] algebraic[24] = constants[29]*constants[30]*log(constants[23]*algebraic[12]) algebraic[14] = states[7]+constants[6] algebraic[25] = constants[29]*constants[30]*log(constants[24]*algebraic[14]) algebraic[27] = constants[11]*(exp((algebraic[21]+algebraic[24])/(constants[29]*constants[30]))-exp(algebraic[25]/(constants[29]*constants[30]))) rates[3] = -algebraic[27] algebraic[0] = custom_piecewise([greater(sin(2.00000* pi*voi) , 0.100000), 0.000100000 , True, 1.00000e-06]) algebraic[2] = states[0]+algebraic[0] algebraic[19] = constants[29]*constants[30]*log(constants[17]*algebraic[2]) algebraic[10] = states[5]+constants[4] algebraic[23] = constants[29]*constants[30]*log(constants[22]*algebraic[10]) algebraic[28] = constants[12]*(exp(algebraic[25]/(constants[29]*constants[30]))-exp((algebraic[24]+algebraic[23]+algebraic[19])/(constants[29]*constants[30]))) rates[7] = algebraic[27]-algebraic[28] algebraic[8] = states[4]+constants[3] algebraic[22] = constants[29]*constants[30]*log(constants[21]*algebraic[8]) algebraic[17] = states[8]+constants[7] algebraic[26] = constants[29]*constants[30]*log(constants[25]*algebraic[17]) algebraic[29] = constants[13]*(exp((algebraic[22]+algebraic[24])/(constants[29]*constants[30]))-exp(algebraic[26]/(constants[29]*constants[30]))) rates[4] = -algebraic[29] algebraic[4] = states[2]+constants[1] algebraic[20] = constants[29]*constants[30]*log(constants[19]*algebraic[4]) algebraic[30] = constants[14]*(exp(algebraic[26]/(constants[29]*constants[30]))-exp((algebraic[24]+algebraic[23]+algebraic[19]+algebraic[20])/(constants[29]*constants[30]))) rates[0] = algebraic[28]+algebraic[30] rates[2] = algebraic[30] rates[5] = algebraic[28]+algebraic[30] rates[6] = ((algebraic[28]+algebraic[30])-algebraic[27])-algebraic[29] rates[8] = algebraic[29]-algebraic[30] return(rates) def computeAlgebraic(constants, states, voi): algebraic = array([[0.0] * len(voi)] * sizeAlgebraic) states = array(states) voi = array(voi) algebraic[1] = states[1]+constants[0] algebraic[9] = constants[29]*constants[30]*log(constants[18]*algebraic[1]) algebraic[5] = states[10]+constants[9] algebraic[13] = constants[29]*constants[30]*log(constants[27]*algebraic[5]) algebraic[7] = states[11]+constants[10] algebraic[15] = constants[29]*constants[30]*log(constants[28]*algebraic[7]) algebraic[16] = constants[15]*(exp((algebraic[9]+algebraic[13])/(constants[29]*constants[30]))-exp(algebraic[15]/(constants[29]*constants[30]))) algebraic[3] = states[9]+constants[8] algebraic[11] = constants[29]*constants[30]*log(constants[26]*algebraic[3]) algebraic[18] = constants[16]*(exp(algebraic[15]/(constants[29]*constants[30]))-exp((algebraic[13]+algebraic[11])/(constants[29]*constants[30]))) algebraic[6] = states[3]+constants[2] algebraic[21] = constants[29]*constants[30]*log(constants[20]*algebraic[6]) algebraic[12] = states[6]+constants[5] algebraic[24] = constants[29]*constants[30]*log(constants[23]*algebraic[12]) algebraic[14] = states[7]+constants[6] algebraic[25] = constants[29]*constants[30]*log(constants[24]*algebraic[14]) algebraic[27] = constants[11]*(exp((algebraic[21]+algebraic[24])/(constants[29]*constants[30]))-exp(algebraic[25]/(constants[29]*constants[30]))) algebraic[0] = custom_piecewise([greater(sin(2.00000* pi*voi) , 0.100000), 0.000100000 , True, 1.00000e-06]) algebraic[2] = states[0]+algebraic[0] algebraic[19] = constants[29]*constants[30]*log(constants[17]*algebraic[2]) algebraic[10] = states[5]+constants[4] algebraic[23] = constants[29]*constants[30]*log(constants[22]*algebraic[10]) algebraic[28] = constants[12]*(exp(algebraic[25]/(constants[29]*constants[30]))-exp((algebraic[24]+algebraic[23]+algebraic[19])/(constants[29]*constants[30]))) algebraic[8] = states[4]+constants[3] algebraic[22] = constants[29]*constants[30]*log(constants[21]*algebraic[8]) algebraic[17] = states[8]+constants[7] algebraic[26] = constants[29]*constants[30]*log(constants[25]*algebraic[17]) algebraic[29] = constants[13]*(exp((algebraic[22]+algebraic[24])/(constants[29]*constants[30]))-exp(algebraic[26]/(constants[29]*constants[30]))) algebraic[4] = states[2]+constants[1] algebraic[20] = constants[29]*constants[30]*log(constants[19]*algebraic[4]) algebraic[30] = constants[14]*(exp(algebraic[26]/(constants[29]*constants[30]))-exp((algebraic[24]+algebraic[23]+algebraic[19]+algebraic[20])/(constants[29]*constants[30]))) 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)