Generated Code

The following is python code generated by the CellML API from this CellML file. (Back to language selection)

The raw code is available.

# Size of variable arrays:
sizeAlgebraic = 14
sizeStates = 4
sizeConstants = 40
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[1] = "pi_TGF_beta_act in component TGF_beta (dimensionless)"
    legend_algebraic[2] = "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] = "A_OB_a in component OB_a (first_order_rate_constant)"
    legend_constants[4] = "pd_OB_a in component OB_a (first_order_rate_constant)"
    legend_states[2] = "OC_a in component OC_a (dimensionless)"
    legend_algebraic[0] = "D_OC_p in component OC_a (first_order_rate_constant)"
    legend_constants[5] = "A_OC_a in component OC_a (first_order_rate_constant)"
    legend_constants[6] = "pd_OC_a in component OC_a (first_order_rate_constant)"
    legend_algebraic[13] = "pi_RANKL_act in component RANKL (dimensionless)"
    legend_states[3] = "BV in component BV (dimensionless)"
    legend_constants[7] = "OC_a_initial in component BV (dimensionless)"
    legend_constants[8] = "OB_a_initial 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_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[38] = "pi_PTH_act in component PTH (dimensionless)"
    legend_constants[39] = "pi_PTH_rep in component PTH (dimensionless)"
    legend_constants[14] = "beta_PTH in component PTH (flux)"
    legend_constants[15] = "Deg_PTH in component PTH (first_order_rate_constant)"
    legend_constants[16] = "P_PTH_d in component PTH (flux)"
    legend_constants[35] = "PTH_tot in component PTH (pM)"
    legend_constants[17] = "KD_PTH_repress in component PTH (pM)"
    legend_constants[18] = "KD_PTH_activate in component PTH (pM)"
    legend_algebraic[4] = "OPG in component OPG (pM)"
    legend_constants[19] = "beta_OPG in component OPG (flux)"
    legend_constants[20] = "beta1_OB_p_OPG in component OPG (dimensionless)"
    legend_constants[21] = "beta2_OB_a_OPG in component OPG (dimensionless)"
    legend_constants[22] = "OPG_max in component OPG (pM)"
    legend_algebraic[3] = "OPG_eff in component OPG (flux)"
    legend_constants[23] = "P_OPG_d in component OPG (flux)"
    legend_constants[24] = "Deg_OPG in component OPG (first_order_rate_constant)"
    legend_algebraic[5] = "Degr_OPG in component OPG (flux)"
    legend_algebraic[6] = "prod_OPG in component OPG (flux)"
    legend_algebraic[9] = "RANKL in component RANKL (dimensionless)"
    legend_constants[25] = "K_A2_RANKL in component RANKL (dimensionless)"
    legend_constants[26] = "K_A1_RANKL in component RANKL (dimensionless)"
    legend_constants[27] = "RANK in component RANKL (pM)"
    legend_algebraic[7] = "RANKL_eff in component RANKL (pM)"
    legend_algebraic[8] = "RANKL_tot in component RANKL (pM)"
    legend_constants[36] = "K_RANKL_OB_p in component RANKL (pM)"
    legend_constants[37] = "K_RANKL_OB_a in component RANKL (pM)"
    legend_algebraic[11] = "OPG_RANKL in component RANKL (pM)"
    legend_constants[28] = "R_RANKL in component RANKL (dimensionless)"
    legend_algebraic[10] = "RANKL_RANK in component RANKL (pM)"
    legend_algebraic[12] = "pd_RANKL in component RANKL (flux)"
    legend_constants[29] = "P_RANKL_d in component RANKL (flux)"
    legend_constants[30] = "R1_OB_p_RANKL in component RANKL (pM)"
    legend_constants[31] = "R2_OB_a_RANKL in component RANKL (pM)"
    legend_constants[32] = "beta_RANKL in component RANKL (flux)"
    legend_constants[33] = "Deg_RANKL in component RANKL (first_order_rate_constant)"
    legend_constants[34] = "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-4
    constants[1] = 0.0
    constants[2] = 2.674e-1
    states[1] = 5.584e-4
    constants[3] = 1.890e-1
    constants[4] = 0.0
    states[2] = 8.070e-4
    constants[5] = 7.000e-1
    constants[6] = 0.0
    states[3] = 100.0
    constants[7] = 1.0
    constants[8] = 1.0
    constants[9] = 1.571e0
    constants[10] = 200.00
    constants[11] = 1.416e-3
    constants[12] = 4.545e-3
    constants[13] = 1.0
    constants[14] = 2.500e2
    constants[15] = 8.600e1
    constants[16] = 0.0
    constants[17] = 1.5e2
    constants[18] = 2.226e-1
    constants[19] = 1.625e8
    constants[20] = 0.0
    constants[21] = 1.0
    constants[22] = 2.000e8
    constants[23] = 0.0
    constants[24] = 3.500e-1
    constants[25] = 3.412e-2
    constants[26] = 1.000e-3
    constants[27] = 1.000e1
    constants[28] = 2.703e6
    constants[29] = 0.0
    constants[30] = 1.0
    constants[31] = 0.0
    constants[32] = 1.684e4
    constants[33] = 1.013e1
    constants[34] = 4.457e0
    constants[35] = (constants[14]+constants[16])/constants[15]
    constants[36] = constants[30]*constants[28]
    constants[37] = constants[31]*constants[28]
    constants[38] = constants[35]/(constants[35]+constants[18])
    constants[39] = 1.00000/(1.00000+constants[35]/constants[17])
    return (states, constants)

def computeRates(voi, states, constants):
    rates = [0.0] * sizeStates; algebraic = [0.0] * sizeAlgebraic
    rates[3] = constants[9]*(states[1]-constants[8])-constants[10]*(states[2]-constants[7])
    algebraic[1] = (constants[13]*states[2])/(constants[12]+constants[13]*states[2])
    algebraic[2] = 1.00000/(1.00000+(constants[13]*states[2])/constants[11])
    rates[0] = (constants[0]*algebraic[1]-constants[2]*algebraic[2]*states[0])+constants[1]
    rates[1] = (constants[2]*algebraic[2]*states[0]-constants[3]*states[1])+constants[4]
    algebraic[0] = custom_piecewise([greater(voi , 0.00000) & less_equal(voi , 100.000), 0.0210000 , True, 0.00210000])
    algebraic[7] = (constants[36]*states[0]+constants[37]*states[1])*constants[38]
    algebraic[3] = (constants[20]*constants[19]*states[0]+constants[21]*constants[19]*states[1])*constants[39]
    algebraic[4] = (constants[23]+algebraic[3])/(algebraic[3]/constants[22]+constants[24])
    algebraic[8] = 1.00000+constants[25]*constants[27]+constants[26]*algebraic[4]
    algebraic[9] = (constants[32]+constants[29])/(algebraic[8]*(constants[32]/algebraic[7]+constants[33]))
    algebraic[10] = constants[25]*algebraic[9]*constants[27]
    algebraic[13] = algebraic[10]/(constants[34]+algebraic[10])
    rates[2] = (algebraic[0]*algebraic[13]-constants[5]*algebraic[1]*states[2])+constants[6]
    return(rates)

def computeAlgebraic(constants, states, voi):
    algebraic = array([[0.0] * len(voi)] * sizeAlgebraic)
    states = array(states)
    voi = array(voi)
    algebraic[1] = (constants[13]*states[2])/(constants[12]+constants[13]*states[2])
    algebraic[2] = 1.00000/(1.00000+(constants[13]*states[2])/constants[11])
    algebraic[0] = custom_piecewise([greater(voi , 0.00000) & less_equal(voi , 100.000), 0.0210000 , True, 0.00210000])
    algebraic[7] = (constants[36]*states[0]+constants[37]*states[1])*constants[38]
    algebraic[3] = (constants[20]*constants[19]*states[0]+constants[21]*constants[19]*states[1])*constants[39]
    algebraic[4] = (constants[23]+algebraic[3])/(algebraic[3]/constants[22]+constants[24])
    algebraic[8] = 1.00000+constants[25]*constants[27]+constants[26]*algebraic[4]
    algebraic[9] = (constants[32]+constants[29])/(algebraic[8]*(constants[32]/algebraic[7]+constants[33]))
    algebraic[10] = constants[25]*algebraic[9]*constants[27]
    algebraic[13] = algebraic[10]/(constants[34]+algebraic[10])
    algebraic[5] = constants[24]*algebraic[4]
    algebraic[6] = algebraic[3]*(1.00000-algebraic[4]/constants[22])+constants[23]
    algebraic[11] = constants[26]*algebraic[4]*algebraic[9]
    algebraic[12] = (constants[32]*(1.00000-algebraic[9]*(algebraic[8]/algebraic[7]))-constants[33]*algebraic[9]*algebraic[8])+constants[29]
    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)