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 = 18
sizeStates = 9
sizeConstants = 49
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_constants[0] = "Eta_b in component Parameters (mmHg_sec)"
    legend_constants[1] = "Rho_b in component Parameters (gram_per_cubic_meter)"
    legend_constants[2] = "E_p in component Parameters (mmHg)"
    legend_constants[3] = "r_p in component Parameters (meter)"
    legend_constants[4] = "l_p in component Parameters (meter)"
    legend_constants[5] = "h_p in component Parameters (meter)"
    legend_constants[6] = "r_a0 in component Parameters (meter)"
    legend_constants[7] = "h_a0 in component Parameters (meter)"
    legend_constants[8] = "sigma_ae0 in component Parameters (mmHg)"
    legend_constants[9] = "K_a_sigma in component Parameters (dimensionless)"
    legend_constants[10] = "sigma_ac in component Parameters (mmHg)"
    legend_constants[11] = "T_a_max0 in component Parameters (mmHg_m)"
    legend_constants[12] = "r_am in component Parameters (meter)"
    legend_constants[13] = "r_at in component Parameters (meter)"
    legend_constants[14] = "n_am in component Parameters (dimensionless)"
    legend_constants[15] = "Eta_a in component Parameters (mmHg_sec)"
    legend_constants[16] = "Q_bl in component Parameters (cubic_m_per_sec)"
    legend_constants[17] = "P_ic in component Parameters (mmHg)"
    legend_constants[18] = "R_v in component Parameters (mmHg_sec_per_cubic_m)"
    legend_constants[19] = "C_v in component Parameters (cubic_m_per_mmHg)"
    legend_constants[20] = "P_ai in component Parameters (mmHg)"
    legend_constants[21] = "T_myo0 in component Parameters (mmHg_m)"
    legend_constants[22] = "T_myo_s in component Parameters (mmHg_m)"
    legend_constants[23] = "Tau_myo in component Parameters (second)"
    legend_constants[24] = "Tau_shear in component Parameters (second)"
    legend_constants[25] = "G_myo in component Parameters (dimensionless)"
    legend_constants[26] = "G_shear in component Parameters (dimensionless)"
    legend_constants[27] = "G_neuro in component Parameters (dimensionless)"
    legend_constants[28] = "G_meta in component Parameters (dimensionless)"
    legend_constants[29] = "x_ini in component Parameters (dimensionless)"
    legend_constants[30] = "Tau_neuro in component Parameters (second)"
    legend_constants[31] = "Tau_meta in component Parameters (second)"
    legend_constants[32] = "conc_CO2_a in component Parameters (mol_per_m3)"
    legend_constants[33] = "M_CO2_0 in component Parameters (mol_per_sec)"
    legend_constants[34] = "alpha_tv in component Parameters (dimensionless)"
    legend_constants[35] = "beta_tv in component Parameters (mol_per_m3)"
    legend_constants[36] = "vol_PVC in component Parameters (cubic_m)"
    legend_constants[37] = "K_shear in component Parameters (second)"
    legend_constants[38] = "K_Ra in component Parameters (mmHg_sec_m)"
    legend_constants[39] = "conc_CO2_t_sp in component Parameters (mol_per_m3)"
    legend_constants[40] = "f_Q in component Parameters (dimensionless)"
    legend_constants[41] = "n_QM in component Parameters (dimensionless)"
    legend_constants[42] = "K_Va in component Parameters (meter)"
    legend_constants[43] = "P_vo in component Parameters (mmHg)"
    legend_states[0] = "Q_Lp in component PCA (cubic_m_per_sec)"
    legend_states[1] = "P_p in component PCA (mmHg)"
    legend_constants[46] = "C_p in component PCA_Components (cubic_m_per_mmHg)"
    legend_constants[47] = "L_p in component PCA_Components (mmHg_sec2_per_m3)"
    legend_constants[48] = "R_p in component PCA_Components (mmHg_sec_per_cubic_m)"
    legend_algebraic[0] = "R_a in component AC_Components (mmHg_sec_per_cubic_m)"
    legend_algebraic[11] = "P_a in component Arteriolar_Circulation (mmHg)"
    legend_algebraic[2] = "C_a in component AC_Components (cubic_m_per_mmHg)"
    legend_algebraic[13] = "Q_a in component AC_Flow (cubic_m_per_sec)"
    legend_states[2] = "P_v in component Venous_Circulation (mmHg)"
    legend_states[3] = "r_a in component Arteriolar_Circulation (meter)"
    legend_algebraic[4] = "T_ae in component AC_Tension (mmHg_m)"
    legend_algebraic[8] = "T_am in component AC_Tension (mmHg_m)"
    legend_algebraic[9] = "T_av in component AC_Tension (mmHg_m)"
    legend_algebraic[10] = "T_a in component AC_Tension (mmHg_m)"
    legend_algebraic[3] = "h_a in component AC_Tension_Variables (meter)"
    legend_algebraic[7] = "T_a_max in component AC_Tension_Variables (mmHg_m)"
    legend_algebraic[6] = "M_s in component CBF_Regulation (dimensionless)"
    legend_algebraic[5] = "M_s1 in component CBF_Regulation (dimensionless)"
    legend_states[4] = "x_myo in component Myo_Regulation (dimensionless)"
    legend_states[5] = "x_shear in component Shear_Regulation (dimensionless)"
    legend_states[6] = "x_neuro in component Neuro_Regulation (dimensionless)"
    legend_states[7] = "x_meta in component Meta_Regulation (dimensionless)"
    legend_algebraic[12] = "A_myo in component Myo_Regulation (dimensionless)"
    legend_algebraic[14] = "A_shear in component Shear_Regulation (dimensionless)"
    legend_algebraic[15] = "A_neuro in component Neuro_Regulation (dimensionless)"
    legend_algebraic[1] = "A_Meta in component Meta_Regulation (dimensionless)"
    legend_constants[44] = "G_a_Meta in component Meta_Regulation (m3_per_mol)"
    legend_states[8] = "conc_CO2_t in component Meta_CO2_Kinetics (mol_per_m3)"
    legend_algebraic[16] = "conc_CO2_v in component Meta_CO2_Kinetics (mol_per_m3)"
    legend_algebraic[17] = "M_CO2 in component VC_Meta_Stim (mol_per_sec)"
    legend_constants[45] = "unity in component VC_Meta_Stim (dimensionless)"
    legend_rates[1] = "d/dt P_p in component PCA (mmHg)"
    legend_rates[0] = "d/dt Q_Lp in component PCA (cubic_m_per_sec)"
    legend_rates[3] = "d/dt r_a in component Arteriolar_Circulation (meter)"
    legend_rates[2] = "d/dt P_v in component Venous_Circulation (mmHg)"
    legend_rates[4] = "d/dt x_myo in component Myo_Regulation (dimensionless)"
    legend_rates[5] = "d/dt x_shear in component Shear_Regulation (dimensionless)"
    legend_rates[6] = "d/dt x_neuro in component Neuro_Regulation (dimensionless)"
    legend_rates[7] = "d/dt x_meta in component Meta_Regulation (dimensionless)"
    legend_rates[8] = "d/dt conc_CO2_t in component Meta_CO2_Kinetics (mol_per_m3)"
    return (legend_states, legend_algebraic, legend_voi, legend_constants)

def initConsts():
    constants = [0.0] * sizeConstants; states = [0.0] * sizeStates;
    constants[0] = 3E-5
    constants[1] = 1.05E6
    constants[2] = 12E3
    constants[3] = 1.05E-3
    constants[4] = 8.6E-2
    constants[5] = 2.6E-4
    constants[6] = 7.5E-5
    constants[7] = 2.5E-5
    constants[8] = 11.19
    constants[9] = 4.5
    constants[10] = 41.32
    constants[11] = 1.50E-2
    constants[12] = 1.28E-4
    constants[13] = 1.74E-4
    constants[14] = 1.75
    constants[15] = 47.8
    constants[16] = 8.8E-7
    constants[17] = 10
    constants[18] = 1.6E7
    constants[19] = 2.5E-8
    constants[20] = 96.0
    constants[21] = 3.6E-3
    constants[22] = 3E-3
    constants[23] = 7
    constants[24] = 60
    constants[25] = 4
    constants[26] = 0
    constants[27] = -0.7
    constants[28] = 0
    constants[29] = 0
    constants[30] = 5
    constants[31] = 15
    constants[32] = 20.65
    constants[33] = 1.35E-7
    constants[34] = 0.96
    constants[35] = 8.9
    constants[36] = 5.99E-6
    constants[37] = 5.2E-7
    constants[38] = 2.5E-9
    constants[39] = 12.41
    constants[40] = 1.25
    constants[41] = 2.2
    constants[42] = 120
    constants[43] = 14
    states[0] = 8.8E-7
    states[1] = 91
    states[2] = 21
    states[3] = 7.7E-5
    states[4] = 0
    states[5] = 0
    states[6] = 0
    states[7] = 0
    constants[44] = 0.59
    states[8] = 12.41
    constants[45] = 1
    constants[46] = ((3.00000* pi*(power(constants[3], 2.00000))*(power(constants[3]/constants[5]+1.00000, 2.00000)))/(constants[2]*((2.00000*constants[3])/constants[5]+1.00000)))*constants[4]
    constants[47] = (constants[1]/( pi*(power(constants[3], 2.00000))))*constants[4]*7.50000e-06
    constants[48] = ((8.00000*constants[0])/( pi*(power(constants[3], 4.00000))))*constants[4]
    return (states, constants)

def computeRates(voi, states, constants):
    rates = [0.0] * sizeStates; algebraic = [0.0] * sizeAlgebraic
    rates[0] = ((constants[20]-states[1])-states[0]*constants[48])/constants[47]
    algebraic[1] = constants[44]*(states[8]-constants[39])
    rates[7] = (algebraic[1]-states[7])/constants[31]
    algebraic[0] = constants[38]/(power(states[3], 4.00000))
    algebraic[3] = power(power(states[3], 2.00000)+2.00000*constants[6]*constants[7]+power(constants[7], 2.00000), 1.0/2)-states[3]
    algebraic[4] = algebraic[3]*(constants[8]*(exp((constants[9]*(states[3]-constants[6]))/constants[6])-1.00000)-constants[10])
    algebraic[5] = constants[25]*states[4]+constants[26]*states[5]+constants[27]*states[6]+constants[28]*states[7]+constants[29]
    algebraic[6] = (exp(2.00000*algebraic[5])-1.00000)/(exp(2.00000*algebraic[5])+1.00000)
    algebraic[7] = constants[11]*(1.00000+algebraic[6])
    algebraic[8] = algebraic[7]*exp(-(power(fabs((states[3]-constants[12])/(constants[13]-constants[12])), constants[14])))
    rates[3] = (constants[6]*((states[1]*(algebraic[0]+constants[18])*states[3]+states[2]*algebraic[0]*states[3])-(algebraic[4]+algebraic[8]+constants[17]*(states[3]+algebraic[3]))*(2.00000*algebraic[0]+constants[18])))/(constants[6]*(power(states[3], 2.00000))*constants[42]*algebraic[0]*(algebraic[0]+constants[18])+constants[15]*algebraic[3]*(2.00000*algebraic[0]+constants[18]))
    algebraic[9] = (constants[15]/constants[6])*rates[3]*algebraic[3]
    algebraic[10] = algebraic[4]+algebraic[8]+algebraic[9]
    algebraic[11] = (algebraic[10]+constants[17]*(states[3]+algebraic[3]))/states[3]
    rates[1] = (2.00000*((states[0]*algebraic[0]-2.00000*states[1])+2.00000*algebraic[11]))/(algebraic[0]*constants[46])
    rates[2] = (2.00000/constants[19])*((algebraic[11]-states[2])/(algebraic[0]+constants[18])-(states[2]-constants[43])/constants[18])
    algebraic[12] = (algebraic[10]-constants[21])/constants[22]
    rates[4] = (algebraic[12]-states[4])/constants[23]
    algebraic[13] = (2.00000*(algebraic[11]-states[2]))/(algebraic[0]+constants[18])
    algebraic[14] = (constants[37]*algebraic[13])/(power(states[3], 3.00000))-1.00000
    rates[5] = (algebraic[14]-states[5])/constants[24]
    algebraic[15] = custom_piecewise([greater_equal(voi , 1.00000), 1.00000 , True, 0.00000])
    rates[6] = (algebraic[15]-states[6])/constants[30]
    algebraic[16] = constants[34]*states[8]+constants[35]
    algebraic[17] = constants[33]*(constants[45]+algebraic[15]*((constants[40]-constants[45])/constants[41]))
    rates[8] = (1.00000/constants[36])*(algebraic[17]-algebraic[13]*(algebraic[16]-constants[32]))
    return(rates)

def computeAlgebraic(constants, states, voi):
    algebraic = array([[0.0] * len(voi)] * sizeAlgebraic)
    states = array(states)
    voi = array(voi)
    algebraic[1] = constants[44]*(states[8]-constants[39])
    algebraic[0] = constants[38]/(power(states[3], 4.00000))
    algebraic[3] = power(power(states[3], 2.00000)+2.00000*constants[6]*constants[7]+power(constants[7], 2.00000), 1.0/2)-states[3]
    algebraic[4] = algebraic[3]*(constants[8]*(exp((constants[9]*(states[3]-constants[6]))/constants[6])-1.00000)-constants[10])
    algebraic[5] = constants[25]*states[4]+constants[26]*states[5]+constants[27]*states[6]+constants[28]*states[7]+constants[29]
    algebraic[6] = (exp(2.00000*algebraic[5])-1.00000)/(exp(2.00000*algebraic[5])+1.00000)
    algebraic[7] = constants[11]*(1.00000+algebraic[6])
    algebraic[8] = algebraic[7]*exp(-(power(fabs((states[3]-constants[12])/(constants[13]-constants[12])), constants[14])))
    algebraic[9] = (constants[15]/constants[6])*rates[3]*algebraic[3]
    algebraic[10] = algebraic[4]+algebraic[8]+algebraic[9]
    algebraic[11] = (algebraic[10]+constants[17]*(states[3]+algebraic[3]))/states[3]
    algebraic[12] = (algebraic[10]-constants[21])/constants[22]
    algebraic[13] = (2.00000*(algebraic[11]-states[2]))/(algebraic[0]+constants[18])
    algebraic[14] = (constants[37]*algebraic[13])/(power(states[3], 3.00000))-1.00000
    algebraic[15] = custom_piecewise([greater_equal(voi , 1.00000), 1.00000 , True, 0.00000])
    algebraic[16] = constants[34]*states[8]+constants[35]
    algebraic[17] = constants[33]*(constants[45]+algebraic[15]*((constants[40]-constants[45])/constants[41]))
    algebraic[2] = 1.03120/algebraic[0]
    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)