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 = 69
sizeStates = 22
sizeConstants = 62
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 Time (time_units)"
    legend_constants[0] = "T in component Environment (Temperature_units)"
    legend_constants[1] = "F in component Environment (F_units)"
    legend_constants[2] = "R in component Environment (R_units)"
    legend_constants[3] = "Ca_o in component Environment (millimolar)"
    legend_constants[4] = "Na_o in component Environment (millimolar)"
    legend_constants[5] = "K_o in component Environment (millimolar)"
    legend_constants[56] = "FoRT in component Environment (inverse_voltage_units)"
    legend_constants[57] = "RToF in component Environment (voltage_units)"
    legend_constants[6] = "Cm in component membrane (capacitance_units)"
    legend_states[0] = "Vm in component membrane (voltage_units)"
    legend_states[1] = "Ca_i_peripheral in component membrane (millimolar)"
    legend_states[2] = "Ca_i_bulk in component membrane (millimolar)"
    legend_states[3] = "Ca_sr in component membrane (millimolar)"
    legend_states[4] = "Na_i in component membrane (millimolar)"
    legend_states[5] = "K_i in component membrane (millimolar)"
    legend_algebraic[43] = "i_k1 in component i_k1 (current_units)"
    legend_algebraic[33] = "i_to_fast in component i_to_fast (current_units)"
    legend_algebraic[36] = "i_to_sustained in component i_to_sustained (current_units)"
    legend_algebraic[46] = "i_kr in component i_kr (current_units)"
    legend_algebraic[48] = "i_ks in component i_ks (current_units)"
    legend_algebraic[54] = "i_kb in component i_kb (current_units)"
    legend_algebraic[52] = "i_nak in component i_nak (current_units)"
    legend_algebraic[31] = "i_cal in component i_cal (current_units)"
    legend_algebraic[25] = "i_cat in component i_cat (current_units)"
    legend_algebraic[38] = "i_na_fast in component i_na_fast (current_units)"
    legend_algebraic[40] = "i_na_late in component i_na_late (current_units)"
    legend_algebraic[59] = "i_pmca in component i_pmca (current_units)"
    legend_algebraic[58] = "i_cab in component i_cab (current_units)"
    legend_algebraic[66] = "i_f_na in component i_f_na (current_units)"
    legend_algebraic[62] = "i_f_k in component i_f_k (current_units)"
    legend_algebraic[49] = "i_naca in component i_naca (current_units)"
    legend_algebraic[56] = "i_nab in component i_nab (current_units)"
    legend_algebraic[0] = "i_stim in component membrane (current_units)"
    legend_algebraic[61] = "i_rel_per in component i_rel_per (current_units)"
    legend_algebraic[63] = "i_serca_per in component i_serca_per (current_units)"
    legend_algebraic[65] = "i_serca_bulk in component i_serca_bulk (current_units)"
    legend_algebraic[68] = "i_diff in component i_diff (current_units)"
    legend_algebraic[67] = "i_leak in component i_leak (current_units)"
    legend_constants[59] = "peripheral_volume in component membrane (volume_units)"
    legend_constants[58] = "bulk_volume in component membrane (volume_units)"
    legend_constants[61] = "diffusable_volume in component membrane (volume_units)"
    legend_constants[60] = "sr_volume in component membrane (volume_units)"
    legend_constants[7] = "cell_volume in component membrane (volume_units)"
    legend_constants[8] = "bulk_fraction in component membrane (dimensionless)"
    legend_constants[9] = "periphery_fraction in component membrane (dimensionless)"
    legend_constants[10] = "sr_fraction in component membrane (dimensionless)"
    legend_constants[11] = "stim_start in component membrane (time_units)"
    legend_constants[12] = "stim_end in component membrane (time_units)"
    legend_constants[13] = "stim_period in component membrane (time_units)"
    legend_constants[14] = "stim_duration in component membrane (time_units)"
    legend_constants[15] = "stim_amplitude in component membrane (current_units)"
    legend_algebraic[1] = "x_inf_Ttype in component x_Ttype (dimensionless)"
    legend_constants[16] = "tau_x_Ttype in component x_Ttype (time_units)"
    legend_states[6] = "x_Ttype in component x_Ttype (dimensionless)"
    legend_algebraic[2] = "y_inf_Ttype in component y_Ttype (dimensionless)"
    legend_algebraic[17] = "tau_y_Ttype in component y_Ttype (time_units)"
    legend_states[7] = "y_Ttype in component y_Ttype (dimensionless)"
    legend_algebraic[16] = "E_Ca in component i_cat (voltage_units)"
    legend_constants[17] = "G_max_Ttype in component i_cat (conductance_units)"
    legend_algebraic[3] = "x_inf_Ltype in component x_Ltype (dimensionless)"
    legend_constants[18] = "tau_x_Ltype in component x_Ltype (time_units)"
    legend_states[8] = "x_Ltype in component x_Ltype (dimensionless)"
    legend_algebraic[4] = "y_inf_Ltype in component y_Ltype (dimensionless)"
    legend_algebraic[18] = "tau_y_Ltype in component y_Ltype (time_units)"
    legend_states[9] = "y_Ltype in component y_Ltype (dimensionless)"
    legend_algebraic[5] = "y_ca_inf_Ltype in component y_ca_Ltype (dimensionless)"
    legend_algebraic[19] = "tau_y_ca_Ltype in component y_ca_Ltype (time_units)"
    legend_states[10] = "y_ca_Ltype in component y_ca_Ltype (dimensionless)"
    legend_algebraic[28] = "E_Ca in component i_cal (voltage_units)"
    legend_constants[19] = "G_max_Ltype in component i_cal (conductance_units)"
    legend_algebraic[6] = "x_inf_to_fast in component x_to_fast (dimensionless)"
    legend_constants[20] = "tau_x_to_fast in component x_to_fast (time_units)"
    legend_states[11] = "x_to_fast in component x_to_fast (dimensionless)"
    legend_algebraic[7] = "y_inf_to_fast in component y_to_fast (dimensionless)"
    legend_constants[21] = "tau_y_to_fast in component y_to_fast (time_units)"
    legend_states[12] = "y_to_fast in component y_to_fast (dimensionless)"
    legend_algebraic[32] = "E_k in component i_to_fast (voltage_units)"
    legend_constants[22] = "G_max_to_fast in component i_to_fast (conductance_units)"
    legend_algebraic[34] = "x_to_sustained in component x_to_sustained (dimensionless)"
    legend_algebraic[35] = "E_k in component i_to_sustained (voltage_units)"
    legend_constants[23] = "G_max_to_sustained in component i_to_sustained (conductance_units)"
    legend_algebraic[8] = "x_inf_na_fast in component x_na_fast (dimensionless)"
    legend_constants[24] = "tau_x_na_fast in component x_na_fast (time_units)"
    legend_states[13] = "x_na_fast in component x_na_fast (dimensionless)"
    legend_algebraic[9] = "y_inf_na_fast in component y_na_fast (dimensionless)"
    legend_constants[25] = "tau_y_na_fast in component y_na_fast (time_units)"
    legend_states[14] = "y_na_fast in component y_na_fast (dimensionless)"
    legend_algebraic[37] = "E_na in component i_na_fast (voltage_units)"
    legend_constants[26] = "G_max_na_fast in component i_na_fast (conductance_units)"
    legend_algebraic[10] = "x_inf_na_late in component x_na_late (dimensionless)"
    legend_constants[27] = "tau_x_na_late in component x_na_late (time_units)"
    legend_states[15] = "x_na_late in component x_na_late (dimensionless)"
    legend_algebraic[11] = "y_inf_na_late in component y_na_late (dimensionless)"
    legend_algebraic[20] = "tau_y_na_late in component y_na_late (time_units)"
    legend_states[16] = "y_na_late in component y_na_late (dimensionless)"
    legend_algebraic[39] = "E_na in component i_na_late (voltage_units)"
    legend_constants[28] = "G_max_na_late in component i_na_late (conductance_units)"
    legend_algebraic[41] = "x_k1 in component x_k1 (dimensionless)"
    legend_algebraic[42] = "E_k in component i_k1 (voltage_units)"
    legend_constants[29] = "G_max_k1 in component i_k1 (conductance_units)"
    legend_algebraic[44] = "x_kr in component x_kr (dimensionless)"
    legend_algebraic[12] = "y_inf_kr in component y_kr (dimensionless)"
    legend_algebraic[29] = "tau_y_kr in component y_kr (time_units)"
    legend_states[17] = "y_kr in component y_kr (dimensionless)"
    legend_algebraic[21] = "ykrv1 in component y_kr (rate_constants_units)"
    legend_algebraic[26] = "ykrv2 in component y_kr (rate_constants_units)"
    legend_algebraic[45] = "E_k in component i_kr (voltage_units)"
    legend_constants[30] = "G_max_kr in component i_kr (conductance_units)"
    legend_algebraic[13] = "x_inf_ks in component x_ks (dimensionless)"
    legend_algebraic[22] = "tau_x_ks in component x_ks (time_units)"
    legend_states[18] = "x_ks in component x_ks (dimensionless)"
    legend_algebraic[27] = "y_inf_ks in component y_ks (dimensionless)"
    legend_algebraic[30] = "tau_y_ks in component y_ks (time_units)"
    legend_states[19] = "y_ks in component y_ks (dimensionless)"
    legend_algebraic[47] = "E_k in component i_ks (voltage_units)"
    legend_constants[31] = "G_max_ks in component i_ks (conductance_units)"
    legend_constants[32] = "n_NaCa in component i_naca (dimensionless)"
    legend_constants[33] = "g_NaCa in component i_naca (current_units)"
    legend_constants[34] = "d_NaCa in component i_naca (dimensionless)"
    legend_constants[35] = "gamma in component i_naca (dimensionless)"
    legend_algebraic[50] = "x_nak in component x_nak (dimensionless)"
    legend_algebraic[51] = "y_nak in component y_nak (dimensionless)"
    legend_constants[36] = "g_nak in component i_nak (current_units)"
    legend_algebraic[53] = "E_k in component i_kb (voltage_units)"
    legend_constants[37] = "G_max_kb in component i_kb (conductance_units)"
    legend_algebraic[55] = "E_na in component i_nab (voltage_units)"
    legend_constants[38] = "G_max_nab in component i_nab (conductance_units)"
    legend_algebraic[57] = "E_ca in component i_cab (voltage_units)"
    legend_constants[39] = "G_max_cab in component i_cab (conductance_units)"
    legend_constants[40] = "PMCA_max in component i_pmca (current_units)"
    legend_constants[41] = "Kpmca in component i_pmca (millimolar)"
    legend_constants[42] = "Hpmca in component i_pmca (dimensionless)"
    legend_constants[43] = "G_f_k in component i_f_k (conductance_units)"
    legend_algebraic[60] = "E_k in component i_f_k (voltage_units)"
    legend_states[20] = "y_gate_f_k in component y_gate_f_k (dimensionless)"
    legend_constants[44] = "G_f_na in component i_f_na (conductance_units)"
    legend_algebraic[64] = "E_na in component i_f_na (voltage_units)"
    legend_states[21] = "y_gate_f_na in component y_gate_f_na (dimensionless)"
    legend_algebraic[14] = "y_inf_f_gate in component y_gate_f_k (dimensionless)"
    legend_algebraic[23] = "tau_y_f_gate in component y_gate_f_k (time_units)"
    legend_algebraic[15] = "y_inf_f_gate in component y_gate_f_na (dimensionless)"
    legend_algebraic[24] = "tau_y_f_gate in component y_gate_f_na (time_units)"
    legend_constants[45] = "REL_max in component i_rel_per (current_per_millimolar_units)"
    legend_constants[46] = "Krel in component i_rel_per (millimolar)"
    legend_constants[47] = "SERCA_max in component i_serca_per (current_units)"
    legend_constants[48] = "Kmf in component i_serca_per (millimolar)"
    legend_constants[49] = "Kmr in component i_serca_per (millimolar)"
    legend_constants[50] = "H in component i_serca_per (dimensionless)"
    legend_constants[51] = "Kmf in component i_serca_bulk (millimolar)"
    legend_constants[52] = "Kmr in component i_serca_bulk (millimolar)"
    legend_constants[53] = "H in component i_serca_bulk (dimensionless)"
    legend_constants[54] = "LEAK_max in component i_leak (current_per_millimolar_units)"
    legend_constants[55] = "DIFF_max in component i_diff (current_per_millimolar_units)"
    legend_rates[0] = "d/dt Vm in component membrane (voltage_units)"
    legend_rates[1] = "d/dt Ca_i_peripheral in component membrane (millimolar)"
    legend_rates[2] = "d/dt Ca_i_bulk in component membrane (millimolar)"
    legend_rates[3] = "d/dt Ca_sr in component membrane (millimolar)"
    legend_rates[5] = "d/dt K_i in component membrane (millimolar)"
    legend_rates[4] = "d/dt Na_i in component membrane (millimolar)"
    legend_rates[6] = "d/dt x_Ttype in component x_Ttype (dimensionless)"
    legend_rates[7] = "d/dt y_Ttype in component y_Ttype (dimensionless)"
    legend_rates[8] = "d/dt x_Ltype in component x_Ltype (dimensionless)"
    legend_rates[9] = "d/dt y_Ltype in component y_Ltype (dimensionless)"
    legend_rates[10] = "d/dt y_ca_Ltype in component y_ca_Ltype (dimensionless)"
    legend_rates[11] = "d/dt x_to_fast in component x_to_fast (dimensionless)"
    legend_rates[12] = "d/dt y_to_fast in component y_to_fast (dimensionless)"
    legend_rates[13] = "d/dt x_na_fast in component x_na_fast (dimensionless)"
    legend_rates[14] = "d/dt y_na_fast in component y_na_fast (dimensionless)"
    legend_rates[15] = "d/dt x_na_late in component x_na_late (dimensionless)"
    legend_rates[16] = "d/dt y_na_late in component y_na_late (dimensionless)"
    legend_rates[17] = "d/dt y_kr in component y_kr (dimensionless)"
    legend_rates[18] = "d/dt x_ks in component x_ks (dimensionless)"
    legend_rates[19] = "d/dt y_ks in component y_ks (dimensionless)"
    legend_rates[20] = "d/dt y_gate_f_k in component y_gate_f_k (dimensionless)"
    legend_rates[21] = "d/dt y_gate_f_na in component y_gate_f_na (dimensionless)"
    return (legend_states, legend_algebraic, legend_voi, legend_constants)

def initConsts():
    constants = [0.0] * sizeConstants; states = [0.0] * sizeStates;
    constants[0] = 310.0
    constants[1] = 96485.3415
    constants[2] = 8314.472
    constants[3] = 2.0
    constants[4] = 140
    constants[5] = 5.4
    constants[6] = 69.0
    states[0] = -88.34
    states[1] = 0.00001
    states[2] = 0.000032
    states[3] = 0.17
    states[4] = 6.7
    states[5] = 140
    constants[7] = 13266.5
    constants[8] = 0.6
    constants[9] = 0.2
    constants[10] = 0.06
    constants[11] = 700
    constants[12] = 9000000
    constants[13] = 500
    constants[14] = 0.5
    constants[15] = -4320.0
    constants[16] = 1.0
    states[6] = 0.001337
    states[7] = 0.01
    constants[17] = 0.9
    constants[18] = 0.7
    states[8] = 0.000003
    states[9] = 0.1
    states[10] = 0.7
    constants[19] = 5.4
    constants[20] = 5.0
    states[11] = 0.0
    constants[21] = 350.0
    states[12] = 0.7
    constants[22] = 10.0
    constants[23] = 3.0
    constants[24] = 0.005
    states[13] = 0.000007
    constants[25] = 2.0
    states[14] = 0.978861
    constants[26] = 1140.0
    constants[27] = 15.0
    states[15] = 0.000012
    states[16] = 0.864489
    constants[28] = 2.0
    constants[29] = 20.0
    states[17] = 0.25
    constants[30] = 1.5
    states[18] = 1.0
    states[19] = 0.0
    constants[31] = 3.0
    constants[32] = 3
    constants[33] = 0.001
    constants[34] = 0.001
    constants[35] = 0.5
    constants[36] = 442.2
    constants[37] = 0.01
    constants[38] = 0.01
    constants[39] = 0.0001
    constants[40] = 5.0
    constants[41] = 0.0001
    constants[42] = 1.5
    constants[43] = 0.188709677
    states[20] = 0.011099
    constants[44] = 0.045290323
    states[21] = 0.011099
    constants[45] = 2500.0
    constants[46] = 0.001
    constants[47] = 120.0
    constants[48] = 0.000246
    constants[49] = 1.7
    constants[50] = 1.6
    constants[51] = 0.000246
    constants[52] = 1.7
    constants[53] = 1.6
    constants[54] = 10.0
    constants[55] = 5000.0
    constants[56] = constants[1]/(constants[2]*constants[0])
    constants[57] = (constants[2]*constants[0])/constants[1]
    constants[58] = constants[8]*constants[7]
    constants[59] = constants[9]*constants[7]
    constants[60] = constants[10]*constants[7]
    constants[61] = (constants[8]+constants[9])*constants[7]
    return (states, constants)

def computeRates(voi, states, constants):
    rates = [0.0] * sizeStates; algebraic = [0.0] * sizeAlgebraic
    algebraic[1] = 1.00000/(1.00000+exp((states[0]+47.8000)/-5.50000))
    rates[6] = (algebraic[1]-states[6])/constants[16]
    algebraic[3] = 1.00000/(1.00000+exp((states[0]+14.6000)/-5.50000))
    rates[8] = (algebraic[3]-states[8])/constants[18]
    algebraic[6] = 1.00000/(1.00000+exp((states[0]+-7.00000)/-9.00000))
    rates[11] = (algebraic[6]-states[11])/constants[20]
    algebraic[7] = 1.00000/(1.00000+exp((states[0]+27.5000)/8.00000))
    rates[12] = (algebraic[7]-states[12])/constants[21]
    algebraic[8] = 1.00000/(1.00000+exp((states[0]+25.0000)/-5.00000))
    rates[13] = (algebraic[8]-states[13])/constants[24]
    algebraic[9] = 1.00000/(1.00000+exp((states[0]+69.0000)/3.96000))
    rates[14] = (algebraic[9]-states[14])/constants[25]
    algebraic[10] = 1.00000/(1.00000+exp((states[0]+30.0000)/-5.00000))
    rates[15] = (algebraic[10]-states[15])/constants[27]
    algebraic[2] = 1.00000/(1.00000+exp((states[0]+67.9000)/3.87000))
    algebraic[17] = 1.42271*exp(-0.0511900*states[0])
    rates[7] = (algebraic[2]-states[7])/algebraic[17]
    algebraic[4] = 1.00000/(1.00000+exp((states[0]+31.0000)/5.54000))
    algebraic[18] = 25.1000/(0.0400000+0.700000*exp(-1.00000*(power(0.0280000*(states[0]+14.5000), 2.00000))))
    rates[9] = (algebraic[4]-states[9])/algebraic[18]
    algebraic[5] = 0.400000+0.600000/(1.00000+power(states[1]/0.000100000, 2.00000))
    algebraic[19] = 2.00000+80.0000/(1.00000+power(states[1]/0.000100000, 2.00000))
    rates[10] = (algebraic[5]-states[10])/algebraic[19]
    algebraic[11] = 0.100000+0.900000/(1.00000+exp((states[0]+75.6000)/6.30000))
    algebraic[20] = 120.000+1.00000*exp((states[0]+100.000)/25.0000)
    rates[16] = (algebraic[11]-states[16])/algebraic[20]
    algebraic[13] = 1.00000/(1.00000+exp((states[0]-1.50000)/-16.7000))
    algebraic[22] = custom_piecewise([less(fabs(states[0]+30.0000) , 0.0145000), 417.946 , True, 1.00000/((7.19000e-05*(states[0]+30.0000))/(1.00000-exp(-0.148000*(states[0]+30.0000)))+(0.000131000*(states[0]+30.0000))/(exp(0.0687000*(states[0]+30.0000))-1.00000))])
    rates[18] = (algebraic[13]-states[18])/algebraic[22]
    algebraic[14] = 1.00000/(1.00000+exp((states[0]+109.000)/10.0000))
    algebraic[23] = 6000.00/(exp(-1.00000*(2.90000+0.0400000*states[0]))+exp(1.00000*(3.60000+0.110000*states[0])))
    rates[20] = (algebraic[14]-states[20])/algebraic[23]
    algebraic[15] = 1.00000/(1.00000+exp((states[0]+109.000)/10.0000))
    algebraic[24] = 6000.00/(exp(-1.00000*(2.90000+0.0400000*states[0]))+exp(1.00000*(3.60000+0.110000*states[0])))
    rates[21] = (algebraic[15]-states[21])/algebraic[24]
    algebraic[12] = 1.00000/(1.00000+exp((states[0]+50.0000)/-7.50000))
    algebraic[21] = custom_piecewise([greater(fabs(states[0]+7.00000) , 0.00100000), (0.00138000*1.00000*(states[0]+7.00000))/(1.00000-exp(-0.123000*(states[0]+7.00000))) , True, 0.00138000/0.123000])
    algebraic[26] = custom_piecewise([greater(fabs(states[0]+10.0000) , 0.00100000), (6.10000e-05*1.00000*(states[0]+10.0000))/(exp(0.145000*(states[0]+10.0000))-1.00000) , True, 0.000610000/0.145000])
    algebraic[29] = 1.00000/(algebraic[21]+algebraic[26])
    rates[17] = (algebraic[12]-states[17])/algebraic[29]
    algebraic[27] = algebraic[13]
    algebraic[30] = 4.00000*algebraic[22]
    rates[19] = (algebraic[27]-states[19])/algebraic[30]
    algebraic[41] = 1.00000/(1.00000+exp((92.0000+states[0])/10.0000))
    algebraic[42] = constants[57]*log(constants[5]/states[5])
    algebraic[43] = constants[29]*(power(constants[5]/5.40000, 0.800000))*algebraic[41]*(states[0]-algebraic[42])
    algebraic[32] = constants[57]*log(constants[5]/states[5])
    algebraic[33] = constants[22]*states[11]*states[12]*(states[0]-algebraic[32])
    algebraic[34] = 1.00000/(1.00000+exp((5.00000-states[0])/17.0000))
    algebraic[35] = constants[57]*log(constants[5]/states[5])
    algebraic[36] = constants[23]*algebraic[34]*(states[0]-algebraic[35])
    algebraic[44] = 1.00000/(1.00000+exp((33.0000+states[0])/22.4000))
    algebraic[45] = constants[57]*log(constants[5]/states[5])
    algebraic[46] = constants[30]*(power(constants[5]/5.40000, 1.00000))*algebraic[44]*states[17]*(states[0]-algebraic[45])
    algebraic[47] = constants[57]*log(constants[5]/states[5])
    algebraic[48] = constants[31]*states[18]*states[19]*(states[0]-algebraic[47])
    algebraic[53] = constants[57]*log(constants[5]/states[5])
    algebraic[54] = constants[37]*(states[0]-algebraic[53])
    algebraic[50] = 1.00000/(1.00000+exp((states[0]+80.0000)/-45.0000))
    algebraic[51] = 1.00000/(1.00000+exp((states[0]+0.00000)/125.000))
    algebraic[52] = constants[36]*algebraic[50]*algebraic[51]*(1.00000/(1.00000+power(1.90000/constants[5], 1.45000)))*(1.00000/(1.00000+power(31.9800/states[4], 1.00000)))
    algebraic[60] = constants[57]*log(constants[5]/states[5])
    algebraic[62] = constants[43]*states[20]*(states[0]-algebraic[60])
    algebraic[0] = custom_piecewise([greater_equal(voi , constants[11]) & less_equal(voi , constants[12]) & less_equal((voi-constants[11])-floor((voi-constants[11])/constants[13])*constants[13] , constants[14]), constants[15] , True, 0.00000])
    rates[5] = ((-1.00000*algebraic[33]+-1.00000*algebraic[36]+-1.00000*algebraic[46]+-1.00000*algebraic[48]+-1.00000*algebraic[43]+-1.00000*algebraic[54]+-1.00000*algebraic[62]+-1.00000*algebraic[0]+2.00000*algebraic[52])*1000.00)/(constants[1]*constants[61])
    algebraic[28] = 0.500000*constants[57]*log(constants[3]/states[1])
    algebraic[31] = constants[19]*states[8]*states[9]*states[10]*(states[0]-algebraic[28])
    algebraic[16] = 0.500000*constants[57]*log(constants[3]/states[1])
    algebraic[25] = constants[17]*states[6]*states[7]*(states[0]-algebraic[16])
    algebraic[37] = constants[57]*log(constants[4]/states[4])
    algebraic[38] = constants[26]*states[13]*states[14]*(states[0]-algebraic[37])
    algebraic[39] = constants[57]*log(constants[4]/states[4])
    algebraic[40] = constants[28]*states[15]*states[16]*(states[0]-algebraic[39])
    algebraic[59] = constants[40]*(1.00000/(1.00000+power(constants[41]/states[1], constants[42])))
    algebraic[57] = 0.500000*constants[57]*log(constants[3]/states[1])
    algebraic[58] = constants[39]*(states[0]-algebraic[57])
    algebraic[64] = constants[57]*log(constants[4]/states[4])
    algebraic[66] = constants[44]*states[21]*(states[0]-algebraic[64])
    algebraic[49] = (512.009*constants[33]*(exp((constants[35]*(constants[32]-2.00000)*states[0])/constants[57])*(power(states[4], constants[32]))*constants[3]-states[1]*exp(((constants[35]-1.00000)*(constants[32]-2.00000)*states[0])/constants[57])*(power(constants[4], constants[32]))))/((1.00000+constants[34]*(states[1]*(power(constants[4], constants[32]))+constants[3]*(power(states[4], constants[32]))))*(1.00000+states[1]/0.00690000))
    algebraic[55] = constants[57]*log(constants[4]/states[4])
    algebraic[56] = constants[38]*(states[0]-algebraic[55])
    rates[0] = ((-1.00000*1.00000)/constants[6])*(algebraic[43]+algebraic[33]+algebraic[36]+algebraic[46]+algebraic[48]+algebraic[54]+algebraic[52]+algebraic[31]+algebraic[25]+algebraic[38]+algebraic[40]+algebraic[59]+algebraic[58]+algebraic[66]+algebraic[62]+algebraic[49]+algebraic[56]+algebraic[0])
    algebraic[61] = constants[45]*(states[3]/(1.00000+power(constants[46]/states[1], 2.00000)))
    algebraic[63] = constants[47]*((power(states[1]/constants[48], constants[50])-power(states[3]/constants[49], constants[50]))/(1.00000+power(states[1]/constants[48], constants[50])+power(states[3]/constants[49], constants[50])))
    algebraic[65] = constants[47]*((power(states[2]/constants[51], constants[53])-power(states[3]/constants[52], constants[53]))/(1.00000+power(states[2]/constants[51], constants[53])+power(states[3]/constants[52], constants[53])))
    algebraic[67] = constants[54]*(states[3]-states[2])
    rates[3] = ((-1.00000*algebraic[61]+-1.00000*algebraic[67]+algebraic[65]+algebraic[63])*1000.00)/(2.00000*constants[1]*constants[60])
    rates[4] = ((-1.00000*algebraic[38]+-1.00000*algebraic[40]+-3.00000*algebraic[52]+-3.00000*algebraic[49]+-1.00000*algebraic[66]+-1.00000*algebraic[56])*1000.00)/(constants[1]*constants[61])
    algebraic[68] = constants[55]*(states[1]-states[2])
    rates[1] = ((-1.00000*algebraic[31]+-1.00000*algebraic[25]+-1.00000*algebraic[58]+1.00000*algebraic[61]+-1.00000*algebraic[63]+-1.00000*algebraic[68]+2.00000*algebraic[49]+-1.00000*algebraic[59])*1000.00)/(2.00000*constants[1]*constants[59])
    rates[2] = ((-1.00000*algebraic[65]+algebraic[68]+algebraic[67])*1000.00)/(2.00000*constants[1]*constants[58])
    return(rates)

def computeAlgebraic(constants, states, voi):
    algebraic = array([[0.0] * len(voi)] * sizeAlgebraic)
    states = array(states)
    voi = array(voi)
    algebraic[1] = 1.00000/(1.00000+exp((states[0]+47.8000)/-5.50000))
    algebraic[3] = 1.00000/(1.00000+exp((states[0]+14.6000)/-5.50000))
    algebraic[6] = 1.00000/(1.00000+exp((states[0]+-7.00000)/-9.00000))
    algebraic[7] = 1.00000/(1.00000+exp((states[0]+27.5000)/8.00000))
    algebraic[8] = 1.00000/(1.00000+exp((states[0]+25.0000)/-5.00000))
    algebraic[9] = 1.00000/(1.00000+exp((states[0]+69.0000)/3.96000))
    algebraic[10] = 1.00000/(1.00000+exp((states[0]+30.0000)/-5.00000))
    algebraic[2] = 1.00000/(1.00000+exp((states[0]+67.9000)/3.87000))
    algebraic[17] = 1.42271*exp(-0.0511900*states[0])
    algebraic[4] = 1.00000/(1.00000+exp((states[0]+31.0000)/5.54000))
    algebraic[18] = 25.1000/(0.0400000+0.700000*exp(-1.00000*(power(0.0280000*(states[0]+14.5000), 2.00000))))
    algebraic[5] = 0.400000+0.600000/(1.00000+power(states[1]/0.000100000, 2.00000))
    algebraic[19] = 2.00000+80.0000/(1.00000+power(states[1]/0.000100000, 2.00000))
    algebraic[11] = 0.100000+0.900000/(1.00000+exp((states[0]+75.6000)/6.30000))
    algebraic[20] = 120.000+1.00000*exp((states[0]+100.000)/25.0000)
    algebraic[13] = 1.00000/(1.00000+exp((states[0]-1.50000)/-16.7000))
    algebraic[22] = custom_piecewise([less(fabs(states[0]+30.0000) , 0.0145000), 417.946 , True, 1.00000/((7.19000e-05*(states[0]+30.0000))/(1.00000-exp(-0.148000*(states[0]+30.0000)))+(0.000131000*(states[0]+30.0000))/(exp(0.0687000*(states[0]+30.0000))-1.00000))])
    algebraic[14] = 1.00000/(1.00000+exp((states[0]+109.000)/10.0000))
    algebraic[23] = 6000.00/(exp(-1.00000*(2.90000+0.0400000*states[0]))+exp(1.00000*(3.60000+0.110000*states[0])))
    algebraic[15] = 1.00000/(1.00000+exp((states[0]+109.000)/10.0000))
    algebraic[24] = 6000.00/(exp(-1.00000*(2.90000+0.0400000*states[0]))+exp(1.00000*(3.60000+0.110000*states[0])))
    algebraic[12] = 1.00000/(1.00000+exp((states[0]+50.0000)/-7.50000))
    algebraic[21] = custom_piecewise([greater(fabs(states[0]+7.00000) , 0.00100000), (0.00138000*1.00000*(states[0]+7.00000))/(1.00000-exp(-0.123000*(states[0]+7.00000))) , True, 0.00138000/0.123000])
    algebraic[26] = custom_piecewise([greater(fabs(states[0]+10.0000) , 0.00100000), (6.10000e-05*1.00000*(states[0]+10.0000))/(exp(0.145000*(states[0]+10.0000))-1.00000) , True, 0.000610000/0.145000])
    algebraic[29] = 1.00000/(algebraic[21]+algebraic[26])
    algebraic[27] = algebraic[13]
    algebraic[30] = 4.00000*algebraic[22]
    algebraic[41] = 1.00000/(1.00000+exp((92.0000+states[0])/10.0000))
    algebraic[42] = constants[57]*log(constants[5]/states[5])
    algebraic[43] = constants[29]*(power(constants[5]/5.40000, 0.800000))*algebraic[41]*(states[0]-algebraic[42])
    algebraic[32] = constants[57]*log(constants[5]/states[5])
    algebraic[33] = constants[22]*states[11]*states[12]*(states[0]-algebraic[32])
    algebraic[34] = 1.00000/(1.00000+exp((5.00000-states[0])/17.0000))
    algebraic[35] = constants[57]*log(constants[5]/states[5])
    algebraic[36] = constants[23]*algebraic[34]*(states[0]-algebraic[35])
    algebraic[44] = 1.00000/(1.00000+exp((33.0000+states[0])/22.4000))
    algebraic[45] = constants[57]*log(constants[5]/states[5])
    algebraic[46] = constants[30]*(power(constants[5]/5.40000, 1.00000))*algebraic[44]*states[17]*(states[0]-algebraic[45])
    algebraic[47] = constants[57]*log(constants[5]/states[5])
    algebraic[48] = constants[31]*states[18]*states[19]*(states[0]-algebraic[47])
    algebraic[53] = constants[57]*log(constants[5]/states[5])
    algebraic[54] = constants[37]*(states[0]-algebraic[53])
    algebraic[50] = 1.00000/(1.00000+exp((states[0]+80.0000)/-45.0000))
    algebraic[51] = 1.00000/(1.00000+exp((states[0]+0.00000)/125.000))
    algebraic[52] = constants[36]*algebraic[50]*algebraic[51]*(1.00000/(1.00000+power(1.90000/constants[5], 1.45000)))*(1.00000/(1.00000+power(31.9800/states[4], 1.00000)))
    algebraic[60] = constants[57]*log(constants[5]/states[5])
    algebraic[62] = constants[43]*states[20]*(states[0]-algebraic[60])
    algebraic[0] = custom_piecewise([greater_equal(voi , constants[11]) & less_equal(voi , constants[12]) & less_equal((voi-constants[11])-floor((voi-constants[11])/constants[13])*constants[13] , constants[14]), constants[15] , True, 0.00000])
    algebraic[28] = 0.500000*constants[57]*log(constants[3]/states[1])
    algebraic[31] = constants[19]*states[8]*states[9]*states[10]*(states[0]-algebraic[28])
    algebraic[16] = 0.500000*constants[57]*log(constants[3]/states[1])
    algebraic[25] = constants[17]*states[6]*states[7]*(states[0]-algebraic[16])
    algebraic[37] = constants[57]*log(constants[4]/states[4])
    algebraic[38] = constants[26]*states[13]*states[14]*(states[0]-algebraic[37])
    algebraic[39] = constants[57]*log(constants[4]/states[4])
    algebraic[40] = constants[28]*states[15]*states[16]*(states[0]-algebraic[39])
    algebraic[59] = constants[40]*(1.00000/(1.00000+power(constants[41]/states[1], constants[42])))
    algebraic[57] = 0.500000*constants[57]*log(constants[3]/states[1])
    algebraic[58] = constants[39]*(states[0]-algebraic[57])
    algebraic[64] = constants[57]*log(constants[4]/states[4])
    algebraic[66] = constants[44]*states[21]*(states[0]-algebraic[64])
    algebraic[49] = (512.009*constants[33]*(exp((constants[35]*(constants[32]-2.00000)*states[0])/constants[57])*(power(states[4], constants[32]))*constants[3]-states[1]*exp(((constants[35]-1.00000)*(constants[32]-2.00000)*states[0])/constants[57])*(power(constants[4], constants[32]))))/((1.00000+constants[34]*(states[1]*(power(constants[4], constants[32]))+constants[3]*(power(states[4], constants[32]))))*(1.00000+states[1]/0.00690000))
    algebraic[55] = constants[57]*log(constants[4]/states[4])
    algebraic[56] = constants[38]*(states[0]-algebraic[55])
    algebraic[61] = constants[45]*(states[3]/(1.00000+power(constants[46]/states[1], 2.00000)))
    algebraic[63] = constants[47]*((power(states[1]/constants[48], constants[50])-power(states[3]/constants[49], constants[50]))/(1.00000+power(states[1]/constants[48], constants[50])+power(states[3]/constants[49], constants[50])))
    algebraic[65] = constants[47]*((power(states[2]/constants[51], constants[53])-power(states[3]/constants[52], constants[53]))/(1.00000+power(states[2]/constants[51], constants[53])+power(states[3]/constants[52], constants[53])))
    algebraic[67] = constants[54]*(states[3]-states[2])
    algebraic[68] = constants[55]*(states[1]-states[2])
    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)