# Size of variable arrays: sizeAlgebraic = 15 sizeStates = 4 sizeConstants = 42 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 (day)" legend_states[0] = "OB_p in component OB_p (dimensionless)" legend_constants[0] = "D_OB_u in component OB_p (first_order_rate_constant)" legend_constants[1] = "pd_OB_p in component OB_p (first_order_rate_constant)" legend_algebraic[2] = "pi_TGF_beta_act in component TGF_beta (dimensionless)" legend_algebraic[3] = "pi_TGF_beta_rep in component TGF_beta (dimensionless)" legend_constants[2] = "D_OB_p in component model_parameters (first_order_rate_constant)" legend_states[1] = "OB_a in component OB_a (dimensionless)" legend_constants[3] = "OB_a_initial in component OB_a (dimensionless)" legend_constants[4] = "A_OB_a in component OB_a (first_order_rate_constant)" legend_constants[5] = "pd_OB_a in component OB_a (first_order_rate_constant)" legend_states[2] = "OC_a in component OC_a (dimensionless)" legend_constants[6] = "OC_a_initial in component OC_a (dimensionless)" legend_algebraic[0] = "D_OC_p in component OC_a (first_order_rate_constant)" legend_constants[7] = "A_OC_a in component OC_a (first_order_rate_constant)" legend_constants[8] = "pd_OC_a in component OC_a (first_order_rate_constant)" legend_algebraic[10] = "pi_RANKL_act in component RANKL (dimensionless)" legend_states[3] = "BV in component BV (dimensionless)" legend_constants[9] = "k_form in component BV (first_order_rate_constant)" legend_constants[10] = "k_res in component BV (first_order_rate_constant)" legend_algebraic[1] = "TGF_beta in component TGF_beta (dimensionless)" legend_constants[11] = "KD_TGF_repress in component TGF_beta (pM)" legend_constants[12] = "KD_TGF_activate in component TGF_beta (pM)" legend_constants[13] = "alpha in component TGF_beta (pM)" legend_constants[14] = "S_TGF_beta in component TGF_beta (pM)" legend_constants[15] = "Deg_TGF_beta in component TGF_beta (pM)" legend_constants[40] = "pi_PTH_act in component PTH (dimensionless)" legend_constants[41] = "pi_PTH_rep in component PTH (dimensionless)" legend_constants[16] = "beta_PTH in component PTH (flux)" legend_constants[17] = "Deg_PTH in component PTH (first_order_rate_constant)" legend_constants[18] = "P_PTH_d in component PTH (flux)" legend_constants[37] = "PTH_tot in component PTH (pM)" legend_constants[19] = "KD_PTH_repress in component PTH (pM)" legend_constants[20] = "KD_PTH_activate in component PTH (pM)" legend_algebraic[5] = "OPG in component OPG (pM)" legend_constants[21] = "beta_OPG in component OPG (flux)" legend_constants[22] = "beta1_OB_p_OPG in component OPG (dimensionless)" legend_constants[23] = "beta2_OB_a_OPG in component OPG (dimensionless)" legend_constants[24] = "OPG_max in component OPG (pM)" legend_algebraic[4] = "OPG_eff in component OPG (flux)" legend_constants[25] = "P_OPG_d in component OPG (flux)" legend_constants[26] = "Deg_OPG in component OPG (first_order_rate_constant)" legend_algebraic[6] = "Degr_OPG in component OPG (flux)" legend_algebraic[7] = "prod_OPG in component OPG (flux)" legend_algebraic[9] = "RANKL in component RANKL (dimensionless)" legend_constants[27] = "K_A2_RANKL in component RANKL (dimensionless)" legend_constants[28] = "K_A1_RANKL in component RANKL (dimensionless)" legend_constants[29] = "RANK in component RANKL (pM)" legend_algebraic[8] = "RANKL_eff in component RANKL (pM)" legend_algebraic[11] = "RANKL_tot in component RANKL (pM)" legend_constants[38] = "K_RANKL_OB_p in component RANKL (pM)" legend_constants[39] = "K_RANKL_OB_a in component RANKL (pM)" legend_algebraic[12] = "OPG_RANKL in component RANKL (pM)" legend_constants[30] = "R_RANKL in component RANKL (dimensionless)" legend_algebraic[13] = "RANKL_RANK in component RANKL (pM)" legend_algebraic[14] = "pd_RANKL in component RANKL (flux)" legend_constants[31] = "P_RANKL_d in component RANKL (flux)" legend_constants[32] = "R1_OB_p_RANKL in component RANKL (pM)" legend_constants[33] = "R2_OB_a_RANKL in component RANKL (pM)" legend_constants[34] = "beta_RANKL in component RANKL (flux)" legend_constants[35] = "Deg_RANKL in component RANKL (first_order_rate_constant)" legend_constants[36] = "KD_RANKL_activate in component RANKL (pM)" legend_rates[0] = "d/dt OB_p in component OB_p (dimensionless)" legend_rates[1] = "d/dt OB_a in component OB_a (dimensionless)" legend_rates[2] = "d/dt OC_a in component OC_a (dimensionless)" legend_rates[3] = "d/dt BV in component BV (dimensionless)" return (legend_states, legend_algebraic, legend_voi, legend_constants) def initConsts(): constants = [0.0] * sizeConstants; states = [0.0] * sizeStates; states[0] = 6.194e-4 constants[0] = 7.000e-04 constants[1] = 0.0 constants[2] = 2.674e-1 states[1] = 5.584e-4 constants[3] = 5.584e-4 constants[4] = 1.890e-1 constants[5] = 0.0 states[2] = 8.070e-4 constants[6] = 8.070e-4 constants[7] = 7.000e-1 constants[8] = 0.0 states[3] = 100.0 constants[9] = 1.571e0 constants[10] = 1.000e0 constants[11] = 1.416e-3 constants[12] = 4.545e-3 constants[13] = 1.0 constants[14] = 0.0 constants[15] = 1.0 constants[16] = 2.500e2 constants[17] = 8.600e1 constants[18] = 0.0 constants[19] = 2.226e-1 constants[20] = 1.500e2 constants[21] = 1.464e8 constants[22] = 0.0 constants[23] = 1.0 constants[24] = 2.000e8 constants[25] = 0.0 constants[26] = 3.500e-1 constants[27] = 3.412e-2 constants[28] = 1.000e-3 constants[29] = 1.000e1 constants[30] = 3.000e6 constants[31] = 0.0 constants[32] = 1.0 constants[33] = 0.0 constants[34] = 1.684e4 constants[35] = 1.013e1 constants[36] = 1.306e1 constants[37] = (constants[16]+constants[18])/constants[17] constants[38] = constants[32]*constants[30] constants[39] = constants[33]*constants[30] constants[40] = constants[37]/(constants[37]+constants[20]) constants[41] = 1.00000/(1.00000+constants[37]/constants[19]) return (states, constants) def computeRates(voi, states, constants): rates = [0.0] * sizeStates; algebraic = [0.0] * sizeAlgebraic rates[3] = constants[9]*(states[1]-constants[3])-constants[10]*(states[2]-constants[6]) algebraic[1] = (constants[13]*constants[10]*states[2]+constants[14])/constants[15] algebraic[2] = algebraic[1]/(constants[12]+algebraic[1]) algebraic[3] = 1.00000/(1.00000+algebraic[1]/constants[11]) rates[0] = (constants[0]*algebraic[2]-constants[2]*algebraic[3]*states[0])+constants[1] rates[1] = (constants[2]*algebraic[3]*states[0]-constants[4]*states[1])+constants[5] algebraic[0] = custom_piecewise([greater(voi , 0.00000) & less_equal(voi , 100.000), 0.0210000 , True, 0.00210000]) algebraic[4] = (constants[22]*constants[21]*states[0]+constants[23]*constants[21]*states[1])*constants[41] algebraic[5] = (algebraic[4]+constants[25])/(algebraic[4]/constants[24]+constants[26]) algebraic[8] = (constants[38]*states[0]+constants[39]*states[1])*constants[40] algebraic[9] = ((algebraic[8]/(1.00000+constants[28]*algebraic[5]+constants[27]*constants[29]))*(constants[34]+constants[31]))/(constants[34]+constants[35]*algebraic[8]) algebraic[10] = algebraic[9]/(constants[36]+algebraic[9]) rates[2] = (algebraic[0]*algebraic[10]-constants[7]*algebraic[2]*states[2])+constants[8] return(rates) def computeAlgebraic(constants, states, voi): algebraic = array([[0.0] * len(voi)] * sizeAlgebraic) states = array(states) voi = array(voi) algebraic[1] = (constants[13]*constants[10]*states[2]+constants[14])/constants[15] algebraic[2] = algebraic[1]/(constants[12]+algebraic[1]) algebraic[3] = 1.00000/(1.00000+algebraic[1]/constants[11]) algebraic[0] = custom_piecewise([greater(voi , 0.00000) & less_equal(voi , 100.000), 0.0210000 , True, 0.00210000]) algebraic[4] = (constants[22]*constants[21]*states[0]+constants[23]*constants[21]*states[1])*constants[41] algebraic[5] = (algebraic[4]+constants[25])/(algebraic[4]/constants[24]+constants[26]) algebraic[8] = (constants[38]*states[0]+constants[39]*states[1])*constants[40] algebraic[9] = ((algebraic[8]/(1.00000+constants[28]*algebraic[5]+constants[27]*constants[29]))*(constants[34]+constants[31]))/(constants[34]+constants[35]*algebraic[8]) algebraic[10] = algebraic[9]/(constants[36]+algebraic[9]) algebraic[6] = constants[26]*algebraic[5] algebraic[7] = algebraic[4]*(1.00000-algebraic[5]/constants[24])+constants[25] algebraic[11] = algebraic[9]*(1.00000+constants[28]*algebraic[5]+constants[27]*constants[29]) algebraic[12] = constants[28]*algebraic[5]*algebraic[9] algebraic[13] = constants[27]*algebraic[9]*constants[29] algebraic[14] = (constants[34]*(1.00000-algebraic[11]/algebraic[8])-constants[35]*algebraic[11])+constants[31] 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)