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 = 0
sizeStates = 10
sizeConstants = 15
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 (hour)"
    legend_states[0] = "c11 in component c11 (per_millilitre)"
    legend_constants[0] = "p1 in component model_parameters (per_hour)"
    legend_constants[1] = "p2 in component model_parameters (per_hour)"
    legend_constants[2] = "p5 in component model_parameters (per_hour)"
    legend_constants[3] = "p6 in component model_parameters (per_hour)"
    legend_states[1] = "c12 in component c12 (per_millilitre)"
    legend_states[2] = "c21 in component c21 (per_millilitre)"
    legend_constants[4] = "p4 in component model_parameters (per_hour)"
    legend_constants[5] = "p11 in component model_parameters (per_hour)"
    legend_constants[6] = "p12 in component model_parameters (per_hour)"
    legend_states[3] = "c22 in component c22 (per_millilitre)"
    legend_states[4] = "c31 in component c31 (per_millilitre)"
    legend_constants[7] = "p3 in component model_parameters (per_hour)"
    legend_constants[8] = "p7 in component model_parameters (per_hour)"
    legend_constants[9] = "p8 in component model_parameters (per_hour)"
    legend_states[5] = "c32 in component c32 (per_millilitre)"
    legend_states[6] = "c41 in component c41 (per_millilitre)"
    legend_constants[10] = "p9 in component model_parameters (per_hour)"
    legend_constants[11] = "p10 in component model_parameters (per_hour)"
    legend_states[7] = "c42 in component c42 (per_millilitre)"
    legend_states[8] = "c51 in component c51 (per_millilitre)"
    legend_constants[12] = "p13 in component model_parameters (per_hour)"
    legend_constants[13] = "p14 in component model_parameters (per_hour)"
    legend_constants[14] = "p15 in component model_parameters (per_hour)"
    legend_states[9] = "c52 in component c52 (per_millilitre)"
    legend_rates[0] = "d/dt c11 in component c11 (per_millilitre)"
    legend_rates[2] = "d/dt c21 in component c21 (per_millilitre)"
    legend_rates[4] = "d/dt c31 in component c31 (per_millilitre)"
    legend_rates[6] = "d/dt c41 in component c41 (per_millilitre)"
    legend_rates[8] = "d/dt c51 in component c51 (per_millilitre)"
    legend_rates[1] = "d/dt c12 in component c12 (per_millilitre)"
    legend_rates[3] = "d/dt c22 in component c22 (per_millilitre)"
    legend_rates[5] = "d/dt c32 in component c32 (per_millilitre)"
    legend_rates[7] = "d/dt c42 in component c42 (per_millilitre)"
    legend_rates[9] = "d/dt c52 in component c52 (per_millilitre)"
    return (legend_states, legend_algebraic, legend_voi, legend_constants)

def initConsts():
    constants = [0.0] * sizeConstants; states = [0.0] * sizeStates;
    states[0] = 9000
    constants[0] = 0.032044
    constants[1] = 0.016630
    constants[2] = 6.363016
    constants[3] = 7.574118
    states[1] = 12000
    states[2] = 0
    constants[4] = 0.019571
    constants[5] = 4.881967
    constants[6] = 5.356513
    states[3] = 0
    states[4] = 0
    constants[7] = 0.001885
    constants[8] = 1.140535
    constants[9] = 1.429125
    states[5] = 0
    states[6] = 0
    constants[10] = 9.768637
    constants[11] = 5.3640806
    states[7] = 0
    states[8] = 0
    constants[12] = 0.052968
    constants[13] = 0.443166
    constants[14] = 0.103700
    states[9] = 0
    return (states, constants)

def computeRates(voi, states, constants):
    rates = [0.0] * sizeStates; algebraic = [0.0] * sizeAlgebraic
    rates[0] = ((-constants[0]*states[0]-constants[1]*states[0])-constants[2]*states[0])+constants[3]*states[1]
    rates[2] = ((constants[0]*states[0]-constants[4]*states[2])-constants[5]*states[2])+constants[6]*states[3]
    rates[4] = ((constants[1]*states[0]-constants[7]*states[4])-constants[8]*states[4])+constants[9]*states[5]
    rates[6] = ((constants[7]*states[4]+constants[4]*states[2])-constants[10]*states[6])+constants[11]*states[7]
    rates[8] = (constants[12]*states[0]-constants[13]*states[8])+constants[14]*states[9]
    rates[1] = constants[2]*states[0]-constants[3]*states[1]
    rates[3] = constants[5]*states[2]-constants[6]*states[3]
    rates[5] = constants[8]*states[4]-constants[9]*states[5]
    rates[7] = constants[10]*states[6]-constants[11]*states[7]
    rates[9] = constants[13]*states[8]-constants[14]*states[9]
    return(rates)

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