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 = 7
sizeStates = 2
sizeConstants = 30
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 (second)"
    legend_algebraic[2] = "F_CE in component F_CE (newton)"
    legend_algebraic[0] = "f_L_CE in component f_L_CE (newton)"
    legend_algebraic[1] = "g_V_CE in component g_V_CE (dimensionless)"
    legend_constants[0] = "a in component user_defined_constants (dimensionless)"
    legend_constants[1] = "F_min in component f_L_CE (newton)"
    legend_constants[2] = "F_max in component user_defined_constants (newton)"
    legend_states[0] = "L_CE in component L_CE (metre)"
    legend_constants[3] = "L_CE_opt in component user_defined_constants (metre)"
    legend_constants[4] = "W in component f_L_CE (dimensionless)"
    legend_constants[25] = "lambda_a in component lambda_a (second_per_metre)"
    legend_constants[5] = "V_max in component g_V_CE (metre_per_second)"
    legend_states[1] = "V_CE in component V_CE (dimensionless)"
    legend_constants[6] = "A in component g_V_CE (dimensionless)"
    legend_constants[7] = "g_max in component g_V_CE (dimensionless)"
    legend_constants[26] = "d1 in component d1 (dimensionless)"
    legend_constants[27] = "d2 in component d2 (dimensionless)"
    legend_constants[28] = "d3 in component d3 (dimensionless)"
    legend_constants[8] = "gamma in component g_V_CE (dimensionless)"
    legend_constants[9] = "V_max in component d1 (metre_per_second)"
    legend_constants[10] = "A in component d1 (dimensionless)"
    legend_constants[11] = "g_max in component d1 (dimensionless)"
    legend_constants[12] = "S in component d1 (metre_per_second)"
    legend_constants[13] = "S in component d2 (metre_per_second)"
    legend_constants[14] = "A in component d2 (dimensionless)"
    legend_constants[15] = "V_max in component d2 (metre_per_second)"
    legend_constants[16] = "gamma in component d2 (dimensionless)"
    legend_constants[17] = "g_max in component d3 (dimensionless)"
    legend_constants[18] = "gamma in component d3 (dimensionless)"
    legend_algebraic[5] = "F_SEE in component F_SEE (newton)"
    legend_constants[19] = "k_SEE in component F_SEE (newton_per_metre2)"
    legend_algebraic[4] = "L_SEE in component L_SEE (metre)"
    legend_constants[20] = "L_slack in component F_SEE (metre)"
    legend_algebraic[3] = "F_PEE in component F_PEE (newton)"
    legend_constants[29] = "k_PEE in component k_PEE (newton_per_metre2)"
    legend_constants[21] = "L_slack in component F_PEE (metre)"
    legend_constants[22] = "W in component k_PEE (dimensionless)"
    legend_constants[23] = "L_CE_opt in component k_PEE (metre)"
    legend_constants[24] = "L_m in component user_defined_constants (metre)"
    legend_algebraic[6] = "F_m in component F_m (newton)"
    legend_rates[0] = "d/dt L_CE in component L_CE (metre)"
    legend_rates[1] = "d/dt V_CE in component V_CE (dimensionless)"
    return (legend_states, legend_algebraic, legend_voi, legend_constants)

def initConsts():
    constants = [0.0] * sizeConstants; states = [0.0] * sizeStates;
    constants[0] = 0.8
    constants[1] = 10
    constants[2] = 7000
    states[0] = 0.02
    constants[3] = 0.093
    constants[4] = 0.63
    constants[5] = 0.93
    states[1] = 0.001
    constants[6] = 0.25
    constants[7] = 1.5
    constants[8] = 5.67
    constants[9] = 0.93
    constants[10] = 0.25
    constants[11] = 1.5
    constants[12] = 2
    constants[13] = 2
    constants[14] = 0.25
    constants[15] = 0.93
    constants[16] = 5.67
    constants[17] = 1.5
    constants[18] = 5.67
    constants[19] = 1000000
    constants[20] = 0.025
    constants[21] = 0.025
    constants[22] = 0.63
    constants[23] = 0.01
    constants[24] = 0.038
    constants[25] = 1.00000*((1.00000-exp(-3.82000*constants[0]))+constants[0]*exp(-3.82000))
    constants[26] = (constants[9]*constants[10]*(constants[11]-1.00000))/(constants[12]*(constants[10]+1.00000))
    constants[27] = (constants[13]*(constants[14]+1.00000))/(constants[15]*constants[14]*(power(constants[16]+1.00000, 2.00000)))
    constants[28] = ((constants[17]-1.00000)*(power(constants[18], 2.00000)))/(power(constants[18]+1.00000, 2.00000))+1.00000
    constants[29] = constants[2]/(power(constants[22]*constants[23], 2.00000))
    return (states, constants)

def computeRates(voi, states, constants):
    rates = [0.0] * sizeStates; algebraic = [0.0] * sizeAlgebraic
    rates[1] = 1.00000*states[0]
    rootfind_0(voi, constants, rates, states, algebraic)
    algebraic[1] = custom_piecewise([less_equal(states[1] , 0.00000), (constants[25]*constants[5]+states[1])/(constants[25]*constants[5]-states[1]/constants[6]) , less(0.00000 , states[1]) & less_equal(states[1] , constants[8]*constants[26]), (constants[7]*states[1]+constants[26])/(states[1]+constants[26]) , greater(states[1] , constants[8]*constants[26]), constants[28]+constants[27]*states[1] , True, float('nan')])
    algebraic[4] = constants[24]-states[0]
    algebraic[5] = custom_piecewise([less_equal(algebraic[4] , constants[20]), 0.00000 , True, constants[19]*(power(algebraic[4]-constants[20], 2.00000))])
    algebraic[3] = custom_piecewise([less_equal(states[0] , constants[21]), 0.00000 , True, constants[29]*(power(states[0]-constants[21], 2.00000))])
    rates[0] = ((1.00000/algebraic[1])*(algebraic[5]*(constants[24]-states[0])-algebraic[3]*states[0]))/(constants[0]*algebraic[0])
    return(rates)

def computeAlgebraic(constants, states, voi):
    algebraic = array([[0.0] * len(voi)] * sizeAlgebraic)
    states = array(states)
    voi = array(voi)
    algebraic[1] = custom_piecewise([less_equal(states[1] , 0.00000), (constants[25]*constants[5]+states[1])/(constants[25]*constants[5]-states[1]/constants[6]) , less(0.00000 , states[1]) & less_equal(states[1] , constants[8]*constants[26]), (constants[7]*states[1]+constants[26])/(states[1]+constants[26]) , greater(states[1] , constants[8]*constants[26]), constants[28]+constants[27]*states[1] , True, float('nan')])
    algebraic[4] = constants[24]-states[0]
    algebraic[5] = custom_piecewise([less_equal(algebraic[4] , constants[20]), 0.00000 , True, constants[19]*(power(algebraic[4]-constants[20], 2.00000))])
    algebraic[3] = custom_piecewise([less_equal(states[0] , constants[21]), 0.00000 , True, constants[29]*(power(states[0]-constants[21], 2.00000))])
    algebraic[2] = algebraic[0]*algebraic[1]*constants[0]
    algebraic[6] = 1.00000*algebraic[5]*(constants[24]-states[0])
    return algebraic

initialGuess0 = None
def rootfind_0(voi, constants, states, algebraic):
    """Calculate value of algebraic variable for DAE"""
    from scipy.optimize import fsolve
    global initialGuess0
    if initialGuess0 is None: initialGuess0 = 0.1
    if not iterable(voi):
        algebraic[0] = fsolve(residualSN_0, initialGuess0, args=(algebraic, voi, constants, rates, states), xtol=1E-6)
        initialGuess0 = algebraic[0]
    else:
        for (i,t) in enumerate(voi):
            algebraic[0][i] = fsolve(residualSN_0, initialGuess0, args=(algebraic[:,i], voi[i], constants, rates, states[:,i]), xtol=1E-6)
            initialGuess0 = algebraic[0][i]

def residualSN_0(algebraicCandidate, algebraic, voi, constants, rates, states):
    algebraic[0] = algebraicCandidate
    return (algebraic[0]) - (custom_piecewise([less_equal(algebraic[0] , 10.0000), constants[1] , True, constants[2]*(1.00000-(power(states[0]-constants[3], 2.00000))/((power(constants[4], 2.00000))*(power(constants[3], 2.00000))))]))

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)