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 = 13
sizeStates = 4
sizeConstants = 43
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[3] = "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_algebraic[1] = "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] = "K_D3_TGF_beta in component OC_a (pM)"
    legend_constants[7] = "OC_p in component OC_a (dimensionless)"
    legend_algebraic[7] = "RANKL in component RANKL (pM)"
    legend_algebraic[8] = "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[8] = "OC_a_initial in component BV (dimensionless)"
    legend_constants[9] = "OB_a_initial in component BV (dimensionless)"
    legend_constants[10] = "k_form in component BV (first_order_rate_constant)"
    legend_constants[11] = "k_res in component model_parameters (first_order_rate_constant)"
    legend_constants[12] = "alpha in component TGF_beta (pM)"
    legend_constants[13] = "D_TGF_beta_tilde in component TGF_beta (first_order_rate_constant)"
    legend_constants[14] = "S_TGF_beta in component TGF_beta (flux)"
    legend_constants[36] = "PTH in component PTH (pM)"
    legend_constants[37] = "pi_PTH_rep_OB_p in component PTH (dimensionless)"
    legend_constants[38] = "pi_PTH_act_OB_p in component PTH (dimensionless)"
    legend_constants[15] = "beta_PTH in component PTH (flux)"
    legend_constants[16] = "D_PTH_tilde in component PTH (first_order_rate_constant)"
    legend_constants[17] = "P_PTH_d in component PTH (flux)"
    legend_constants[18] = "K_D4_PTH in component PTH (pM)"
    legend_constants[19] = "K_D5_PTH in component PTH (pM)"
    legend_constants[20] = "K_D6_PTH in component PTH (pM)"
    legend_constants[21] = "K_D7_PTH in component PTH (pM)"
    legend_constants[39] = "pi_PTH_act_OB_a in component PTH (dimensionless)"
    legend_constants[40] = "pi_PTH_rep_OB_a in component PTH (dimensionless)"
    legend_algebraic[4] = "OPG in component OPG (pM)"
    legend_constants[22] = "beta1_OPG in component OPG (flux)"
    legend_constants[23] = "beta2_OPG in component OPG (flux)"
    legend_constants[24] = "OPG_max in component OPG (pM)"
    legend_algebraic[5] = "P_OPG_e in component OPG (flux)"
    legend_constants[25] = "P_OPG_d in component OPG (flux)"
    legend_constants[26] = "D_OPG_tilde in component OPG (first_order_rate_constant)"
    legend_constants[41] = "pi_PTH_rep_OB in component OPG (dimensionless)"
    legend_constants[27] = "K_A2_RANKL in component RANKL (per_pM)"
    legend_constants[28] = "K_A1_RANKL in component RANKL (per_pM)"
    legend_constants[29] = "RANK in component RANKL (pM)"
    legend_algebraic[6] = "RANKL_eff in component RANKL (pM)"
    legend_algebraic[9] = "RANKL_tot in component RANKL (pM)"
    legend_algebraic[12] = "P_RANKL in component RANKL (flux)"
    legend_algebraic[10] = "P_RANKL_e in component RANKL (flux)"
    legend_constants[30] = "P_RANKL_d in component RANKL (flux)"
    legend_constants[31] = "R1_RANKL in component RANKL (pM)"
    legend_constants[32] = "R2_RANKL in component RANKL (pM)"
    legend_constants[33] = "beta_RANKL in component RANKL (flux)"
    legend_algebraic[11] = "D_RANKL in component RANKL (flux)"
    legend_constants[34] = "D_RANKL_tilde in component RANKL (first_order_rate_constant)"
    legend_constants[42] = "pi_PTH_act_OB in component RANKL (dimensionless)"
    legend_constants[35] = "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] = 7.000e-1
    constants[6] = 1.416e-3
    constants[7] = 1.0
    states[3] = 100.0
    constants[8] = 1.0
    constants[9] = 1.0
    constants[10] = 1.571e0
    constants[11] = 1.000e0
    constants[12] = 1.000e0
    constants[13] = 1.000e0
    constants[14] = 0.0
    constants[15] = 2.500e2
    constants[16] = 8.600e1
    constants[17] = 0.0
    constants[18] = 1.5e2
    constants[19] = 1.5e2
    constants[20] = 2.226e-1
    constants[21] = 2.226e-1
    constants[22] = 1.0
    constants[23] = 0.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] = 0.0
    constants[31] = 0.0
    constants[32] = 1.0
    constants[33] = 1.684e4
    constants[34] = 1.013e1
    constants[35] = 1.306e1
    constants[36] = (constants[15]+constants[17])/constants[16]
    constants[37] = 1.00000/(1.00000+constants[36]/constants[20])
    constants[38] = constants[36]/(constants[18]+constants[36])
    constants[39] = constants[36]/(constants[19]+constants[36])
    constants[40] = 1.00000/(1.00000+constants[36]/constants[21])
    constants[41] = constants[37]
    constants[42] = constants[38]
    return (states, constants)

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

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