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 = 2
sizeStates = 9
sizeConstants = 38
from math import *
from numpy import *

def createLegends():
    legend_states = [""] * sizeStates
    legend_rates = [""] * sizeStates
    legend_algebraic = [""] * sizeAlgebraic
    legend_voi = ""
    legend_constants = [""] * sizeConstants
    legend_constants[0] = "BW in component main (kilogram)"
    legend_constants[1] = "Qcc in component main (litre_per_hr_kg)"
    legend_constants[2] = "Qliv in component main (dimensionless)"
    legend_constants[3] = "Qfat in component main (dimensionless)"
    legend_constants[4] = "Qbrain in component main (dimensionless)"
    legend_constants[5] = "Qslow in component main (dimensionless)"
    legend_constants[20] = "Qc in component main (litre_per_hr)"
    legend_constants[28] = "Ql in component main (litre_per_hr)"
    legend_constants[33] = "Qf in component main (litre_per_hr)"
    legend_constants[35] = "Qbr in component main (litre_per_hr)"
    legend_constants[36] = "Qs in component main (litre_per_hr)"
    legend_constants[37] = "Qr in component main (litre_per_hr)"
    legend_constants[6] = "Vblood in component main (dimensionless)"
    legend_constants[7] = "Vbrain in component main (dimensionless)"
    legend_constants[8] = "Vliver in component main (dimensionless)"
    legend_constants[21] = "Vfat in component main (dimensionless)"
    legend_constants[9] = "Vslow in component main (dimensionless)"
    legend_constants[29] = "Vrap in component main (dimensionless)"
    legend_constants[30] = "Vf in component main (litre)"
    legend_constants[22] = "Vs in component main (litre)"
    legend_constants[23] = "Vl in component main (litre)"
    legend_constants[34] = "Vr in component main (litre)"
    legend_constants[24] = "Vbr in component main (litre)"
    legend_constants[25] = "Vb in component main (litre)"
    legend_constants[31] = "Vven in component main (litre)"
    legend_constants[32] = "Vart in component main (litre)"
    legend_constants[10] = "Pfat_bl in component main (dimensionless)"
    legend_constants[11] = "Pslow_bl in component main (dimensionless)"
    legend_constants[12] = "Pliv_bl in component main (dimensionless)"
    legend_constants[13] = "Prapid_bl in component main (dimensionless)"
    legend_constants[14] = "Pbrain_bl in component main (dimensionless)"
    legend_constants[15] = "dose in component main (mg_per_kg)"
    legend_constants[16] = "F in component main (dimensionless)"
    legend_constants[26] = "D0 in component main (mg)"
    legend_constants[17] = "Ka in component main (per_hr)"
    legend_constants[18] = "Kfc in component main (per_hr)"
    legend_constants[27] = "KF in component main (per_hr_kg)"
    legend_voi = "t in component main (hr)"
    legend_states[0] = "A_fat in component main (mg)"
    legend_states[1] = "A_slow in component main (mg)"
    legend_states[2] = "A_liv in component main (mg)"
    legend_states[3] = "A_rapid in component main (mg)"
    legend_states[4] = "A_brain in component main (mg)"
    legend_constants[19] = "A_blood in component main (mg)"
    legend_states[5] = "A_stomach in component main (mg)"
    legend_states[6] = "A_ven in component main (mg)"
    legend_states[7] = "A_art in component main (mg)"
    legend_states[8] = "A_liv_CL in component main (mg)"
    legend_algebraic[0] = "C_art in component main (mg_per_litre)"
    legend_algebraic[1] = "C_brain in component main (mg_per_litre)"
    legend_rates[6] = "d/dt A_ven in component main (mg)"
    legend_rates[7] = "d/dt A_art in component main (mg)"
    legend_rates[0] = "d/dt A_fat in component main (mg)"
    legend_rates[1] = "d/dt A_slow in component main (mg)"
    legend_rates[4] = "d/dt A_brain in component main (mg)"
    legend_rates[3] = "d/dt A_rapid in component main (mg)"
    legend_rates[5] = "d/dt A_stomach in component main (mg)"
    legend_rates[2] = "d/dt A_liv in component main (mg)"
    legend_rates[8] = "d/dt A_liv_CL in component main (mg)"
    return (legend_states, legend_algebraic, legend_voi, legend_constants)

def initConsts():
    constants = [0.0] * sizeConstants; states = [0.0] * sizeStates;
    constants[0] = 0.1615
    constants[1] = 14
    constants[2] = 0.25
    constants[3] = 0.09
    constants[4] = 0.03
    constants[5] = 0.278
    constants[6] = 0.05
    constants[7] = 0.0116
    constants[8] = 0.04
    constants[9] = 0.63
    constants[10] = 186
    constants[11] = 3.4
    constants[12] = 6.1
    constants[13] = 6.1
    constants[14] = 3
    constants[15] = 60
    constants[16] = 0.8
    constants[17] = 0.1
    constants[18] = 6
    states[0] = 0
    states[1] = 0
    states[2] = 0
    states[3] = 0
    states[4] = 0
    constants[19] = 0
    states[6] = 0
    states[7] = 0
    states[8] = 0
    constants[20] = constants[1]*(power(constants[0], 0.740000))
    constants[21] = 0.0100000*(20.0000*constants[0]+1.66400)
    constants[22] = constants[9]*constants[0]
    constants[23] = constants[8]*constants[0]
    constants[24] = constants[7]*constants[0]
    constants[25] = constants[6]*constants[0]
    constants[26] = constants[16]*constants[15]*constants[0]
    constants[27] = constants[18]/(power(constants[0], 0.300000))
    constants[28] = constants[2]*constants[20]
    constants[29] = (((1.00000-constants[6])-constants[8])-constants[21])-constants[9]
    constants[30] = constants[21]*constants[0]
    constants[31] = 0.750000*constants[25]
    constants[32] = 0.250000*constants[25]
    constants[33] = constants[3]*constants[20]
    constants[34] = constants[29]*constants[0]
    constants[35] = constants[4]*constants[20]
    constants[36] = constants[5]*constants[20]
    constants[37] = (((constants[20]-constants[28])-constants[33])-constants[35])-constants[36]
    states[5] = constants[26]
    return (states, constants)

def computeRates(voi, states, constants):
    rates = [0.0] * sizeStates; algebraic = [0.0] * sizeAlgebraic
    rates[6] = ((constants[33]*states[0])/constants[30])/constants[10]+((constants[35]*states[4])/constants[24])/constants[14]+((constants[36]*states[1])/constants[22])/constants[11]+((constants[28]*states[2])/constants[23])/constants[12]+((constants[37]*states[3])/constants[34])/constants[13]
    rates[0] = constants[33]*(states[7]/constants[25]-(states[0]/constants[30])/constants[10])
    rates[1] = constants[36]*(states[7]/constants[25]-(states[1]/constants[22])/constants[11])
    rates[4] = constants[35]*(states[7]/constants[25]-(states[4]/constants[24])/constants[14])
    rates[3] = constants[37]*(states[7]/constants[25]-(states[3]/constants[34])/constants[13])
    rates[5] = -constants[17]*states[5]
    rates[2] = (constants[28]*(states[7]/constants[25]-(states[2]/constants[23])/constants[12])-((constants[27]*constants[23]*states[2])/constants[23])/constants[12])+constants[17]*states[5]
    rates[8] = ((constants[27]*constants[23]*states[2])/constants[23])/constants[12]
    rates[7] = rates[6]-(constants[20]*states[7])/constants[25]
    return(rates)

def computeAlgebraic(constants, states, voi):
    algebraic = array([[0.0] * len(voi)] * sizeAlgebraic)
    states = array(states)
    voi = array(voi)
    algebraic[0] = states[7]/constants[25]
    algebraic[1] = states[4]/constants[24]
    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)