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 = 42
sizeStates = 10
sizeConstants = 47
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 (millisecond)"
    legend_states[0] = "Gi in component Gi (millimolar)"
    legend_algebraic[4] = "Jglut in component Jglut (millimolar_per_millisecond)"
    legend_algebraic[6] = "Jgk in component Jgk (millimolar_per_millisecond)"
    legend_states[1] = "Ge in component Ge (millimolar)"
    legend_algebraic[1] = "G_infinity in component Ge (millimolar)"
    legend_constants[0] = "tau_G in component Ge (millisecond)"
    legend_constants[1] = "Gmax in component Ge (millimolar)"
    legend_constants[2] = "Gmin in component Ge (millimolar)"
    legend_constants[3] = "SG in component Ge (dimensionless)"
    legend_constants[4] = "I_ in component Ge (dimensionless)"
    legend_states[2] = "I in component I (dimensionless)"
    legend_states[3] = "G6P in component G6P (millimolar)"
    legend_constants[5] = "kappa in component model_parameters (dimensionless)"
    legend_algebraic[41] = "JPFK in component JPFK (micromolar_per_millisecond)"
    legend_states[4] = "FBP in component FBP (micromolar)"
    legend_algebraic[7] = "JGPDH in component JGPDH (micromolar_per_millisecond)"
    legend_algebraic[0] = "F6P in component F6P (millimolar)"
    legend_constants[6] = "Kglut in component Jglut (millimolar)"
    legend_constants[7] = "Vglut in component Jglut (millimolar_per_millisecond)"
    legend_constants[8] = "Kgk in component Jgk (millimolar)"
    legend_constants[9] = "Vgk in component Jgk (millimolar_per_millisecond)"
    legend_constants[10] = "ngk in component Jgk (dimensionless)"
    legend_constants[11] = "lambda in component JPFK (dimensionless)"
    legend_constants[12] = "Vmax in component JPFK (micromolar_per_millisecond)"
    legend_constants[46] = "w0000 in component w (dimensionless)"
    legend_algebraic[25] = "w1000 in component w (dimensionless)"
    legend_algebraic[8] = "w0100 in component w (dimensionless)"
    legend_algebraic[9] = "w0010 in component w (dimensionless)"
    legend_algebraic[27] = "w0001 in component w (dimensionless)"
    legend_algebraic[29] = "w1100 in component w (dimensionless)"
    legend_algebraic[31] = "w1010 in component w (dimensionless)"
    legend_algebraic[33] = "w1001 in component w (dimensionless)"
    legend_algebraic[34] = "w0101 in component w (dimensionless)"
    legend_algebraic[35] = "w0011 in component w (dimensionless)"
    legend_algebraic[10] = "w0110 in component w (dimensionless)"
    legend_algebraic[36] = "w1110 in component w (dimensionless)"
    legend_algebraic[37] = "w0111 in component w (dimensionless)"
    legend_algebraic[38] = "w1011 in component w (dimensionless)"
    legend_algebraic[39] = "w1101 in component w (dimensionless)"
    legend_algebraic[40] = "w1111 in component w (dimensionless)"
    legend_constants[13] = "f13 in component w (dimensionless)"
    legend_constants[14] = "f23 in component w (dimensionless)"
    legend_constants[15] = "f41 in component w (dimensionless)"
    legend_constants[16] = "f42 in component w (dimensionless)"
    legend_constants[17] = "f43 in component w (dimensionless)"
    legend_constants[18] = "K1 in component w (micromolar)"
    legend_constants[19] = "K2 in component w (micromolar)"
    legend_constants[20] = "K3 in component w (micromolar)"
    legend_constants[21] = "K4 in component w (micromolar)"
    legend_algebraic[23] = "AMP in component AMP (micromolar)"
    legend_algebraic[24] = "ATP in component ATP (micromolar)"
    legend_constants[22] = "Atot in component ATP (micromolar)"
    legend_states[5] = "ADP in component ADP (micromolar)"
    legend_constants[23] = "tau_a in component ADP (millisecond)"
    legend_constants[24] = "r in component ADP (dimensionless)"
    legend_constants[25] = "r1 in component ADP (micromolar)"
    legend_algebraic[11] = "gamma in component gamma (dimensionless)"
    legend_states[6] = "Ca in component Ca (micromolar)"
    legend_constants[26] = "v_gamma in component gamma (dimensionless)"
    legend_constants[27] = "k_gamma in component gamma (micromolar_per_millisecond)"
    legend_states[7] = "v in component membrane (millivolt)"
    legend_constants[28] = "cm in component membrane (femtofarad)"
    legend_algebraic[14] = "I_Ca in component I_Ca (picoampere)"
    legend_algebraic[12] = "I_K in component I_K (picoampere)"
    legend_algebraic[17] = "I_K_Ca in component I_K_Ca (picoampere)"
    legend_algebraic[32] = "I_K_ATP in component I_K_ATP (picoampere)"
    legend_constants[29] = "gK_ in component I_K (picosiemens)"
    legend_constants[30] = "vK in component model_parameters (millivolt)"
    legend_states[8] = "n in component n (dimensionless)"
    legend_algebraic[5] = "n_infinity in component n (dimensionless)"
    legend_algebraic[2] = "tau_n in component n (millisecond)"
    legend_constants[31] = "gCa_ in component I_Ca (picosiemens)"
    legend_constants[32] = "vCa in component model_parameters (millivolt)"
    legend_algebraic[13] = "m_infinity in component m (dimensionless)"
    legend_constants[33] = "gkCa_ in component I_K_Ca (picosiemens)"
    legend_algebraic[15] = "gkCa in component I_K_Ca (picosiemens)"
    legend_constants[34] = "KD in component I_K_Ca (micromolar)"
    legend_constants[35] = "gkATP_ in component I_K_ATP (picosiemens)"
    legend_algebraic[30] = "gkATP in component I_K_ATP (picosiemens)"
    legend_algebraic[28] = "o_infinity in component I_K_ATP (dimensionless)"
    legend_algebraic[19] = "MgADP in component I_K_ATP (micromolar)"
    legend_algebraic[21] = "ADP3_ in component I_K_ATP (micromolar)"
    legend_algebraic[26] = "ATP4_ in component I_K_ATP (micromolar)"
    legend_constants[36] = "fcyt in component Ca (dimensionless)"
    legend_algebraic[16] = "Jmem in component Jmem (micromolar_per_millisecond)"
    legend_algebraic[22] = "Jer in component Jer (micromolar_per_millisecond)"
    legend_states[9] = "Caer in component Caer (micromolar)"
    legend_constants[37] = "fer in component Caer (dimensionless)"
    legend_constants[38] = "Vcyt_Ver in component Caer (dimensionless)"
    legend_constants[39] = "kPMCA in component Jmem (first_order_rate_constant)"
    legend_constants[40] = "alpha in component Jmem (micromolar_per_millisecond)"
    legend_algebraic[20] = "Jleak in component Jleak (micromolar_per_millisecond)"
    legend_algebraic[18] = "JSERCA in component JSERCA (micromolar_per_millisecond)"
    legend_constants[41] = "kSERCA in component JSERCA (first_order_rate_constant)"
    legend_constants[42] = "pleak in component Jleak (first_order_rate_constant)"
    legend_algebraic[3] = "I_infinity in component I (dimensionless)"
    legend_constants[43] = "tau_I in component I (millisecond)"
    legend_constants[44] = "I_slope in component I (per_micromolar)"
    legend_constants[45] = "Canull in component I (micromolar)"
    legend_rates[0] = "d/dt Gi in component Gi (millimolar)"
    legend_rates[1] = "d/dt Ge in component Ge (millimolar)"
    legend_rates[3] = "d/dt G6P in component G6P (millimolar)"
    legend_rates[4] = "d/dt FBP in component FBP (micromolar)"
    legend_rates[5] = "d/dt ADP in component ADP (micromolar)"
    legend_rates[7] = "d/dt v in component membrane (millivolt)"
    legend_rates[8] = "d/dt n in component n (dimensionless)"
    legend_rates[6] = "d/dt Ca in component Ca (micromolar)"
    legend_rates[9] = "d/dt Caer in component Caer (micromolar)"
    legend_rates[2] = "d/dt I in component I (dimensionless)"
    return (legend_states, legend_algebraic, legend_voi, legend_constants)

def initConsts():
    constants = [0.0] * sizeConstants; states = [0.0] * sizeStates;
    states[0] = 0
    states[1] = 7
    constants[0] = 450000
    constants[1] = 15
    constants[2] = 1
    constants[3] = 1
    constants[4] = 5
    states[2] = 0
    states[3] = 200
    constants[5] = 0.005
    states[4] = 40
    constants[6] = 7
    constants[7] = 8
    constants[8] = 7
    constants[9] = 0.8
    constants[10] = 4
    constants[11] = 0.06
    constants[12] = 2
    constants[13] = 0.02
    constants[14] = 0.2
    constants[15] = 20
    constants[16] = 20
    constants[17] = 20
    constants[18] = 30
    constants[19] = 1
    constants[20] = 50000
    constants[21] = 1000
    constants[22] = 3000
    states[5] = 780
    constants[23] = 300000
    constants[24] = 0.5
    constants[25] = 0.35
    states[6] = 0.1
    constants[26] = 2.2
    constants[27] = 0.1
    states[7] = -60
    constants[28] = 5300
    constants[29] = 2700
    constants[30] = -75
    states[8] = 0
    constants[31] = 1000
    constants[32] = 25
    constants[33] = 400
    constants[34] = 0.5
    constants[35] = 40000
    constants[36] = 0.01
    states[9] = 185
    constants[37] = 0.01
    constants[38] = 31
    constants[39] = 0.18
    constants[40] = 4.5e-6
    constants[41] = 0.4
    constants[42] = 0.0002
    constants[43] = 10000
    constants[44] = 210
    constants[45] = 0.055
    constants[46] = 1.00000
    return (states, constants)

def computeRates(voi, states, constants):
    rates = [0.0] * sizeStates; algebraic = [0.0] * sizeAlgebraic
    algebraic[1] = constants[2]+(constants[1]-constants[2])/(1.00000+exp((states[2]-constants[4])/constants[3]))
    rates[1] = (algebraic[1]-states[1])/constants[0]
    algebraic[3] = custom_piecewise([greater_equal(states[6] , constants[45]), constants[44]*(states[6]-constants[45]) , less(states[6] , constants[45]), 0.00000 , True, float('nan')])
    rates[2] = (algebraic[3]-states[2])/constants[43]
    algebraic[5] = 1.00000/(1.00000+exp(-(states[7]+16.0000)/5.60000))
    algebraic[2] = 1.00000/(0.0350000*cosh((states[7]+16.0000)/22.4000))
    rates[8] = (algebraic[5]-states[8])/algebraic[2]
    algebraic[4] = (constants[7]*(states[1]-states[0])*constants[6])/((constants[6]+states[1])*(constants[6]+states[0]))
    algebraic[6] = (constants[9]*(power(states[0], constants[10])))/(power(constants[8], constants[10])+power(states[0], constants[10]))
    rates[0] = algebraic[4]-algebraic[6]
    algebraic[13] = 1.00000/(1.00000+exp(-(states[7]+20.0000)/12.0000))
    algebraic[14] = constants[31]*algebraic[13]*(states[7]-constants[32])
    algebraic[16] = -(constants[40]*algebraic[14]+constants[39]*states[6])
    algebraic[20] = constants[42]*(states[9]-states[6])
    algebraic[18] = constants[41]*states[6]
    algebraic[22] = algebraic[20]-algebraic[18]
    rates[6] = constants[36]*(algebraic[16]+algebraic[22])
    rates[9] = -constants[37]*constants[38]*algebraic[22]
    rootfind_0(voi, constants, rates, states, algebraic)
    algebraic[7] = 0.200000*(power(states[4]/1.00000, 1.0/2))
    algebraic[11] = (constants[26]*algebraic[7])/(constants[27]+algebraic[7])
    rates[5] = (1.00000/constants[23])*(algebraic[24]-states[5]*exp((constants[24]+algebraic[11])*(1.00000-states[6]/constants[25])))
    algebraic[12] = constants[29]*states[8]*(states[7]-constants[30])
    algebraic[15] = (constants[33]*(power(states[6], 2.00000)))/(power(constants[34], 2.00000)+power(states[6], 2.00000))
    algebraic[17] = algebraic[15]*(states[7]-constants[30])
    algebraic[19] = 0.165000*states[5]
    algebraic[21] = 0.135000*states[5]
    algebraic[26] = 0.00500000*algebraic[24]
    algebraic[28] = (0.0800000*(1.00000+(2.00000*algebraic[19])/17.0000)+0.890000*(power(algebraic[19]/17.0000, 2.00000)))/((power(1.00000+algebraic[19]/17.0000, 2.00000))*(1.00000+algebraic[21]/26.0000+algebraic[26]/1.00000))
    algebraic[30] = constants[35]*algebraic[28]
    algebraic[32] = algebraic[30]*(states[7]-constants[30])
    rates[7] = -(algebraic[12]+algebraic[14]+algebraic[17]+algebraic[32])/constants[28]
    algebraic[25] = power(algebraic[23]/constants[18], 1.00000)
    algebraic[8] = power(states[4]/constants[19], 1.00000)
    algebraic[0] = 0.300000*states[3]
    algebraic[9] = power((power(algebraic[0], 2.00000))/constants[20], 1.00000)
    algebraic[27] = power((power(algebraic[24], 2.00000))/constants[21], 1.00000)
    algebraic[29] = (power(algebraic[23]/constants[18], 1.00000))*(power(states[4]/constants[19], 1.00000))
    algebraic[31] = (1.00000/(power(constants[13], 1.00000*1.00000)))*(power(algebraic[23]/constants[18], 1.00000))*(power((power(algebraic[0], 2.00000))/constants[20], 1.00000))
    algebraic[33] = (1.00000/(power(constants[15], 1.00000*1.00000)))*(power(algebraic[23]/constants[18], 1.00000))*(power((power(algebraic[24], 2.00000))/constants[21], 1.00000))
    algebraic[34] = (1.00000/(power(constants[16], 1.00000*1.00000)))*(power(states[4]/constants[19], 1.00000))*(power((power(algebraic[24], 2.00000))/constants[21], 1.00000))
    algebraic[35] = (1.00000/(power(constants[17], 1.00000*1.00000)))*(power((power(algebraic[0], 2.00000))/constants[20], 1.00000))*(power((power(algebraic[24], 2.00000))/constants[21], 1.00000))
    algebraic[10] = (1.00000/(power(constants[14], 1.00000*1.00000)))*(power(states[4]/constants[19], 1.00000))*(power((power(algebraic[0], 2.00000))/constants[20], 1.00000))
    algebraic[36] = (1.00000/((power(constants[13], 1.00000*1.00000))*(power(constants[14], 1.00000*1.00000))))*(power(algebraic[23]/constants[18], 1.00000))*(power(states[4]/constants[19], 1.00000))*(power((power(algebraic[0], 2.00000))/constants[20], 1.00000))
    algebraic[37] = (1.00000/((power(constants[14], 1.00000*1.00000))*(power(constants[16], 1.00000*1.00000))*(power(constants[17], 1.00000*1.00000))))*(power(states[4]/constants[19], 1.00000))*(power((power(algebraic[0], 2.00000))/constants[20], 1.00000))*(power((power(algebraic[24], 2.00000))/constants[21], 1.00000))
    algebraic[38] = (1.00000/((power(constants[13], 1.00000*1.00000))*(power(constants[15], 1.00000*1.00000))*(power(constants[17], 1.00000*1.00000))))*(power(algebraic[23]/constants[18], 1.00000))*(power((power(algebraic[0], 2.00000))/constants[20], 1.00000))*(power((power(algebraic[24], 2.00000))/constants[21], 1.00000))
    algebraic[39] = (1.00000/((power(constants[15], 1.00000*1.00000))*(power(constants[16], 1.00000*1.00000))))*(power(algebraic[23]/constants[18], 1.00000))*(power(states[4]/constants[19], 1.00000))*(power((power(algebraic[24], 2.00000))/constants[21], 1.00000))
    algebraic[40] = (1.00000/((power(constants[13], 1.00000*1.00000))*(power(constants[14], 1.00000*1.00000))*(power(constants[15], 1.00000*1.00000))*(power(constants[16], 1.00000*1.00000))*(power(constants[17], 1.00000*1.00000))))*(power(algebraic[23]/constants[18], 1.00000))*(power(states[4]/constants[19], 1.00000))*(power((power(algebraic[0], 2.00000))/constants[20], 1.00000))*(power((power(algebraic[24], 2.00000))/constants[21], 1.00000))
    algebraic[41] = (constants[12]*((1.00000-constants[11])*algebraic[36]+constants[11]*(algebraic[9]+algebraic[31]+algebraic[35]+algebraic[36]+algebraic[37]+algebraic[38]+algebraic[10]+algebraic[40])))/(constants[46]+algebraic[25]+algebraic[8]+algebraic[9]+algebraic[27]+algebraic[29]+algebraic[31]+algebraic[33]+algebraic[34]+algebraic[35]+algebraic[10]+algebraic[36]+algebraic[37]+algebraic[38]+algebraic[39]+algebraic[40])
    rates[3] = constants[5]*(algebraic[6]-algebraic[41])
    rates[4] = constants[5]*(algebraic[41]-0.500000*algebraic[7])
    return(rates)

def computeAlgebraic(constants, states, voi):
    algebraic = array([[0.0] * len(voi)] * sizeAlgebraic)
    states = array(states)
    voi = array(voi)
    algebraic[1] = constants[2]+(constants[1]-constants[2])/(1.00000+exp((states[2]-constants[4])/constants[3]))
    algebraic[3] = custom_piecewise([greater_equal(states[6] , constants[45]), constants[44]*(states[6]-constants[45]) , less(states[6] , constants[45]), 0.00000 , True, float('nan')])
    algebraic[5] = 1.00000/(1.00000+exp(-(states[7]+16.0000)/5.60000))
    algebraic[2] = 1.00000/(0.0350000*cosh((states[7]+16.0000)/22.4000))
    algebraic[4] = (constants[7]*(states[1]-states[0])*constants[6])/((constants[6]+states[1])*(constants[6]+states[0]))
    algebraic[6] = (constants[9]*(power(states[0], constants[10])))/(power(constants[8], constants[10])+power(states[0], constants[10]))
    algebraic[13] = 1.00000/(1.00000+exp(-(states[7]+20.0000)/12.0000))
    algebraic[14] = constants[31]*algebraic[13]*(states[7]-constants[32])
    algebraic[16] = -(constants[40]*algebraic[14]+constants[39]*states[6])
    algebraic[20] = constants[42]*(states[9]-states[6])
    algebraic[18] = constants[41]*states[6]
    algebraic[22] = algebraic[20]-algebraic[18]
    algebraic[7] = 0.200000*(power(states[4]/1.00000, 1.0/2))
    algebraic[11] = (constants[26]*algebraic[7])/(constants[27]+algebraic[7])
    algebraic[12] = constants[29]*states[8]*(states[7]-constants[30])
    algebraic[15] = (constants[33]*(power(states[6], 2.00000)))/(power(constants[34], 2.00000)+power(states[6], 2.00000))
    algebraic[17] = algebraic[15]*(states[7]-constants[30])
    algebraic[19] = 0.165000*states[5]
    algebraic[21] = 0.135000*states[5]
    algebraic[26] = 0.00500000*algebraic[24]
    algebraic[28] = (0.0800000*(1.00000+(2.00000*algebraic[19])/17.0000)+0.890000*(power(algebraic[19]/17.0000, 2.00000)))/((power(1.00000+algebraic[19]/17.0000, 2.00000))*(1.00000+algebraic[21]/26.0000+algebraic[26]/1.00000))
    algebraic[30] = constants[35]*algebraic[28]
    algebraic[32] = algebraic[30]*(states[7]-constants[30])
    algebraic[25] = power(algebraic[23]/constants[18], 1.00000)
    algebraic[8] = power(states[4]/constants[19], 1.00000)
    algebraic[0] = 0.300000*states[3]
    algebraic[9] = power((power(algebraic[0], 2.00000))/constants[20], 1.00000)
    algebraic[27] = power((power(algebraic[24], 2.00000))/constants[21], 1.00000)
    algebraic[29] = (power(algebraic[23]/constants[18], 1.00000))*(power(states[4]/constants[19], 1.00000))
    algebraic[31] = (1.00000/(power(constants[13], 1.00000*1.00000)))*(power(algebraic[23]/constants[18], 1.00000))*(power((power(algebraic[0], 2.00000))/constants[20], 1.00000))
    algebraic[33] = (1.00000/(power(constants[15], 1.00000*1.00000)))*(power(algebraic[23]/constants[18], 1.00000))*(power((power(algebraic[24], 2.00000))/constants[21], 1.00000))
    algebraic[34] = (1.00000/(power(constants[16], 1.00000*1.00000)))*(power(states[4]/constants[19], 1.00000))*(power((power(algebraic[24], 2.00000))/constants[21], 1.00000))
    algebraic[35] = (1.00000/(power(constants[17], 1.00000*1.00000)))*(power((power(algebraic[0], 2.00000))/constants[20], 1.00000))*(power((power(algebraic[24], 2.00000))/constants[21], 1.00000))
    algebraic[10] = (1.00000/(power(constants[14], 1.00000*1.00000)))*(power(states[4]/constants[19], 1.00000))*(power((power(algebraic[0], 2.00000))/constants[20], 1.00000))
    algebraic[36] = (1.00000/((power(constants[13], 1.00000*1.00000))*(power(constants[14], 1.00000*1.00000))))*(power(algebraic[23]/constants[18], 1.00000))*(power(states[4]/constants[19], 1.00000))*(power((power(algebraic[0], 2.00000))/constants[20], 1.00000))
    algebraic[37] = (1.00000/((power(constants[14], 1.00000*1.00000))*(power(constants[16], 1.00000*1.00000))*(power(constants[17], 1.00000*1.00000))))*(power(states[4]/constants[19], 1.00000))*(power((power(algebraic[0], 2.00000))/constants[20], 1.00000))*(power((power(algebraic[24], 2.00000))/constants[21], 1.00000))
    algebraic[38] = (1.00000/((power(constants[13], 1.00000*1.00000))*(power(constants[15], 1.00000*1.00000))*(power(constants[17], 1.00000*1.00000))))*(power(algebraic[23]/constants[18], 1.00000))*(power((power(algebraic[0], 2.00000))/constants[20], 1.00000))*(power((power(algebraic[24], 2.00000))/constants[21], 1.00000))
    algebraic[39] = (1.00000/((power(constants[15], 1.00000*1.00000))*(power(constants[16], 1.00000*1.00000))))*(power(algebraic[23]/constants[18], 1.00000))*(power(states[4]/constants[19], 1.00000))*(power((power(algebraic[24], 2.00000))/constants[21], 1.00000))
    algebraic[40] = (1.00000/((power(constants[13], 1.00000*1.00000))*(power(constants[14], 1.00000*1.00000))*(power(constants[15], 1.00000*1.00000))*(power(constants[16], 1.00000*1.00000))*(power(constants[17], 1.00000*1.00000))))*(power(algebraic[23]/constants[18], 1.00000))*(power(states[4]/constants[19], 1.00000))*(power((power(algebraic[0], 2.00000))/constants[20], 1.00000))*(power((power(algebraic[24], 2.00000))/constants[21], 1.00000))
    algebraic[41] = (constants[12]*((1.00000-constants[11])*algebraic[36]+constants[11]*(algebraic[9]+algebraic[31]+algebraic[35]+algebraic[36]+algebraic[37]+algebraic[38]+algebraic[10]+algebraic[40])))/(constants[46]+algebraic[25]+algebraic[8]+algebraic[9]+algebraic[27]+algebraic[29]+algebraic[31]+algebraic[33]+algebraic[34]+algebraic[35]+algebraic[10]+algebraic[36]+algebraic[37]+algebraic[38]+algebraic[39]+algebraic[40])
    return algebraic

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

def residualSN_0(algebraicCandidate, algebraic, voi, constants, rates, states):
    resid = array([0.0] * 2)
    algebraic[23] = algebraicCandidate[0]
    algebraic[24] = algebraicCandidate[1]
    resid[0] = (algebraic[24]-(constants[22]-(states[5]+algebraic[23])))
    resid[1] = (algebraic[23]-(power(states[5], 2.00000))/algebraic[24])
    return resid

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)