# Size of variable arrays: sizeAlgebraic = 12 sizeStates = 4 sizeConstants = 44 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] = "K_D1_TGF_beta in component OB_p (pM)" legend_constants[2] = "K_D2_TGF_beta in component model_parameters (pM)" legend_constants[3] = "D_OB_p in component model_parameters (first_order_rate_constant)" legend_algebraic[1] = "TGF_beta in component TGF_beta (pM)" legend_states[1] = "OB_a in component OB_a (dimensionless)" legend_constants[4] = "A_OB_a in component OB_a (first_order_rate_constant)" legend_states[2] = "OC_a in component OC_a (dimensionless)" legend_constants[5] = "D_OC_p in component OC_a (first_order_rate_constant)" legend_constants[6] = "A_OC_a in component OC_a (first_order_rate_constant)" legend_constants[7] = "K_D3_TGF_beta in component OC_a (pM)" legend_constants[8] = "OC_p in component OC_a (dimensionless)" legend_algebraic[6] = "RANKL in component RANKL (pM)" legend_algebraic[7] = "pi_RANKL_act_OC_p in component RANKL (dimensionless)" legend_states[3] = "BV in component BV (dimensionless)" legend_algebraic[0] = "OC_a_tilde in component BV (dimensionless)" legend_algebraic[2] = "OB_a_tilde in component BV (dimensionless)" legend_constants[9] = "OC_a_initial in component BV (dimensionless)" legend_constants[10] = "OB_a_initial in component BV (dimensionless)" legend_constants[11] = "k_form in component BV (first_order_rate_constant)" legend_constants[12] = "k_res in component model_parameters (first_order_rate_constant)" legend_constants[13] = "alpha in component TGF_beta (pM)" legend_constants[14] = "D_TGF_beta_tilde in component TGF_beta (first_order_rate_constant)" legend_constants[15] = "S_TGF_beta in component TGF_beta (flux)" legend_constants[37] = "PTH in component PTH (pM)" legend_constants[38] = "pi_PTH_rep_OB_p in component PTH (dimensionless)" legend_constants[39] = "pi_PTH_act_OB_p in component PTH (dimensionless)" legend_constants[16] = "beta_PTH in component PTH (flux)" legend_constants[17] = "D_PTH_tilde in component PTH (first_order_rate_constant)" legend_constants[18] = "P_PTH_d in component PTH (flux)" legend_constants[19] = "K_D4_PTH in component PTH (pM)" legend_constants[20] = "K_D5_PTH in component PTH (pM)" legend_constants[21] = "K_D6_PTH in component PTH (pM)" legend_constants[22] = "K_D7_PTH in component PTH (pM)" legend_constants[40] = "pi_PTH_act_OB_a in component PTH (dimensionless)" legend_constants[41] = "pi_PTH_rep_OB_a in component PTH (dimensionless)" legend_algebraic[3] = "OPG in component OPG (pM)" legend_constants[23] = "beta1_OPG in component OPG (flux)" legend_constants[24] = "beta2_OPG in component OPG (flux)" legend_constants[25] = "OPG_max in component OPG (pM)" legend_algebraic[4] = "P_OPG_e in component OPG (flux)" legend_constants[26] = "P_OPG_d in component OPG (flux)" legend_constants[27] = "D_OPG_tilde in component OPG (first_order_rate_constant)" legend_constants[42] = "pi_PTH_rep_OB in component OPG (dimensionless)" legend_constants[28] = "K_A2_RANKL in component RANKL (per_pM)" legend_constants[29] = "K_A1_RANKL in component RANKL (per_pM)" legend_constants[30] = "RANK in component RANKL (pM)" legend_algebraic[5] = "RANKL_eff in component RANKL (pM)" legend_algebraic[8] = "RANKL_tot in component RANKL (pM)" legend_algebraic[11] = "P_RANKL in component RANKL (flux)" legend_algebraic[9] = "P_RANKL_e in component RANKL (flux)" legend_constants[31] = "P_RANKL_d in component RANKL (flux)" legend_constants[32] = "R1_RANKL in component RANKL (pM)" legend_constants[33] = "R2_RANKL in component RANKL (pM)" legend_constants[34] = "beta_RANKL in component RANKL (flux)" legend_algebraic[10] = "D_RANKL in component RANKL (flux)" legend_constants[35] = "D_RANKL_tilde in component RANKL (first_order_rate_constant)" legend_constants[43] = "pi_PTH_act_OB in component RANKL (dimensionless)" legend_constants[36] = "K_D8_RANKL 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] = 1.0 constants[0] = 7.000e-4 constants[1] = 4.545e-3 constants[2] = 1.416e-3 constants[3] = 5.348e-0 states[1] = 1.0 constants[4] = 1.890e-1 states[2] = 1.0 constants[5] = 2.100e-3 constants[6] = 7.000e-1 constants[7] = 1.416e-3 constants[8] = 1.0 states[3] = 100.0 constants[9] = 1.0 constants[10] = 1.0 constants[11] = 1.571e0 constants[12] = 1.000e0 constants[13] = 1.000e0 constants[14] = 1.000e0 constants[15] = 0.0 constants[16] = 2.500e2 constants[17] = 8.600e1 constants[18] = 0.0 constants[19] = 1.5e2 constants[20] = 1.5e2 constants[21] = 2.226e-1 constants[22] = 2.226e-1 constants[23] = 1.464e8 constants[24] = 1.464e8 constants[25] = 2.000e8 constants[26] = 0.0 constants[27] = 3.500e-1 constants[28] = 3.412e-2 constants[29] = 1.000e-3 constants[30] = 1.000e1 constants[31] = 0.0 constants[32] = 3.000e6 constants[33] = 3.000e6 constants[34] = 1.684e4 constants[35] = 1.013e1 constants[36] = 1.306e1 constants[37] = (constants[16]+constants[18])/constants[17] constants[38] = 1.00000/(1.00000+constants[37]/constants[21]) constants[39] = constants[37]/(constants[19]+constants[37]) constants[40] = constants[37]/(constants[20]+constants[37]) constants[41] = 1.00000/(1.00000+constants[37]/constants[22]) constants[42] = constants[38] constants[43] = constants[39] return (states, constants) def computeRates(voi, states, constants): rates = [0.0] * sizeStates; algebraic = [0.0] * sizeAlgebraic algebraic[1] = (constants[13]*constants[12]*states[2]+constants[15])/constants[14] rates[0] = constants[0]*(algebraic[1]/(constants[1]+algebraic[1]))-constants[3]*states[0]*(1.00000/(1.00000+algebraic[1]/constants[2])) rates[1] = constants[3]*states[0]*(1.00000/(1.00000+algebraic[1]/constants[2]))-constants[4]*states[1] algebraic[0] = states[2]-constants[9] algebraic[2] = states[1]-constants[10] rates[3] = constants[11]*algebraic[2]-constants[12]*algebraic[0] algebraic[3] = ((constants[23]*states[0]+constants[24]*states[1])*constants[42]+constants[26])/(((constants[23]*states[0]+constants[24]*states[1])*constants[42])/constants[25]+constants[27]) algebraic[5] = (constants[32]*states[0]+constants[33]*states[1])*constants[43] algebraic[6] = (algebraic[5]/(1.00000+constants[29]*algebraic[3]+constants[28]*constants[30]))*((constants[34]+constants[31])/(constants[34]+constants[35]*algebraic[5])) algebraic[7] = algebraic[6]/(constants[36]+algebraic[6]) rates[2] = constants[5]*constants[8]*algebraic[7]-constants[6]*states[2]*(algebraic[1]/(constants[7]+algebraic[1])) 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[12]*states[2]+constants[15])/constants[14] algebraic[0] = states[2]-constants[9] algebraic[2] = states[1]-constants[10] algebraic[3] = ((constants[23]*states[0]+constants[24]*states[1])*constants[42]+constants[26])/(((constants[23]*states[0]+constants[24]*states[1])*constants[42])/constants[25]+constants[27]) algebraic[5] = (constants[32]*states[0]+constants[33]*states[1])*constants[43] algebraic[6] = (algebraic[5]/(1.00000+constants[29]*algebraic[3]+constants[28]*constants[30]))*((constants[34]+constants[31])/(constants[34]+constants[35]*algebraic[5])) algebraic[7] = algebraic[6]/(constants[36]+algebraic[6]) algebraic[4] = (constants[23]*states[0]+constants[24]*states[1])*constants[42]*(1.00000-algebraic[3]/constants[25]) algebraic[8] = algebraic[6]*(1.00000+constants[29]*algebraic[3]+constants[28]*constants[30]) algebraic[9] = constants[34]*(1.00000-algebraic[8]/algebraic[5]) algebraic[10] = -constants[35]*algebraic[8] algebraic[11] = algebraic[9]+constants[31] return algebraic 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)