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 = 16
sizeStates = 3
sizeConstants = 34
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 interface (ms)"
    legend_states[0] = "Vm in component interface (mV)"
    legend_states[1] = "cMgADP in component interface (concentration)"
    legend_states[2] = "cNa_i in component interface (concentration)"
    legend_algebraic[15] = "v_cyc in component NaK_pump (rate)"
    legend_algebraic[9] = "net_free_energy in component NaK_pump (energy)"
    legend_constants[0] = "body_temp in component NaK_pump (kelvin)"
    legend_constants[1] = "gas_const in component NaK_pump (gas_constant)"
    legend_constants[2] = "faraday_const in component NaK_pump (faraday_constant)"
    legend_constants[3] = "k1 in component NaK_pump (rate)"
    legend_constants[4] = "minus_k1 in component NaK_pump (minus_k1_rate)"
    legend_constants[5] = "k2 in component NaK_pump (rate)"
    legend_constants[6] = "minus_k2 in component NaK_pump (rate)"
    legend_constants[7] = "k3 in component NaK_pump (rate)"
    legend_constants[8] = "minus_k3 in component NaK_pump (minus_k3_rate)"
    legend_constants[9] = "k4 in component NaK_pump (rate)"
    legend_constants[10] = "minus_k4 in component NaK_pump (rate)"
    legend_constants[11] = "eq_Na_base_e in component NaK_pump (concentration)"
    legend_constants[12] = "eq_Na_base_i in component NaK_pump (concentration)"
    legend_constants[13] = "eq_K_e in component NaK_pump (concentration)"
    legend_constants[14] = "eq_K_i in component NaK_pump (concentration)"
    legend_constants[15] = "eq_MgATP in component NaK_pump (concentration)"
    legend_constants[16] = "eq_HPi in component NaK_pump (concentration)"
    legend_constants[17] = "eq_KPi in component NaK_pump (concentration)"
    legend_constants[18] = "eq_NaPi in component NaK_pump (concentration)"
    legend_algebraic[3] = "dimless_Na_e in component NaK_pump (dimensionless)"
    legend_algebraic[1] = "dimless_Na_i in component NaK_pump (dimensionless)"
    legend_constants[27] = "dimless_K_e in component NaK_pump (dimensionless)"
    legend_constants[26] = "dimless_K_i in component NaK_pump (dimensionless)"
    legend_constants[28] = "dimless_MgATP in component NaK_pump (dimensionless)"
    legend_algebraic[5] = "alpha1 in component NaK_pump (rate)"
    legend_constants[29] = "alpha2 in component NaK_pump (rate)"
    legend_algebraic[7] = "alpha3 in component NaK_pump (rate)"
    legend_constants[30] = "alpha4 in component NaK_pump (rate)"
    legend_algebraic[10] = "minus_alpha1 in component NaK_pump (rate)"
    legend_algebraic[11] = "minus_alpha2 in component NaK_pump (rate)"
    legend_algebraic[12] = "minus_alpha3 in component NaK_pump (rate)"
    legend_algebraic[13] = "minus_alpha4 in component NaK_pump (rate)"
    legend_constants[19] = "cNa_e in component NaK_pump (concentration)"
    legend_constants[20] = "cK_e in component NaK_pump (concentration)"
    legend_constants[21] = "cK_i in component NaK_pump (concentration)"
    legend_constants[22] = "cMgATP in component NaK_pump (concentration)"
    legend_constants[23] = "cPi_sum in component NaK_pump (concentration)"
    legend_algebraic[0] = "cPi in component NaK_pump (concentration)"
    legend_constants[24] = "cH in component NaK_pump (concentration)"
    legend_algebraic[2] = "dG_Na in component NaK_pump (energy)"
    legend_algebraic[4] = "dG_K in component NaK_pump (energy)"
    legend_algebraic[6] = "dG_pump in component NaK_pump (energy)"
    legend_algebraic[8] = "dG_ATP in component NaK_pump (energy)"
    legend_constants[25] = "partition_factor in component NaK_pump (dimensionless)"
    legend_algebraic[14] = "diagram_sum in component NaK_pump (rate_diagram_sum)"
    legend_rates[0] = "d/dt Vm in component interface (mV)"
    legend_rates[1] = "d/dt cMgADP in component interface (concentration)"
    legend_rates[2] = "d/dt cNa_i in component interface (concentration)"
    return (legend_states, legend_algebraic, legend_voi, legend_constants)

def initConsts():
    constants = [0.0] * sizeConstants; states = [0.0] * sizeStates;
    states[0] = -150
    states[1] = 0.01
    states[2] = 50.
    constants[0] = 310.
    constants[1] = 8.314
    constants[2] = 96485.
    constants[3] = 1050.
    constants[4] = 172.1
    constants[5] = 481.
    constants[6] = 40.1
    constants[7] = 2000.
    constants[8] = 79287.1
    constants[9] = 320.
    constants[10] = 40.1
    constants[11] = 15.5
    constants[12] = 2.49
    constants[13] = 0.213
    constants[14] = 0.5
    constants[15] = 2.51
    constants[16] = 0.000169
    constants[17] = 292.
    constants[18] = 224.
    constants[19] = 150.
    constants[20] = 5.4
    constants[21] = 140.
    constants[22] = 9.8
    constants[23] = 4.2
    constants[24] = 0.000081283
    constants[25] = -0.031288692380984445
    constants[26] = constants[21]/constants[14]
    constants[31] = 1.00000
    constants[32] = 0.00000
    constants[33] = 0.00000
    constants[27] = constants[20]/constants[13]
    constants[28] = constants[22]/constants[15]
    constants[29] = constants[5]
    constants[30] = (constants[9]*constants[28])/(1.00000+constants[28])
    return (states, constants)

def computeRates(voi, states, constants):
    rates = [0.0] * sizeStates; algebraic = [0.0] * sizeAlgebraic
    rates[0] = constants[31]
    rates[1] = constants[32]
    rates[2] = constants[33]
    return(rates)

def computeAlgebraic(constants, states, voi):
    algebraic = array([[0.0] * len(voi)] * sizeAlgebraic)
    states = array(states)
    voi = array(voi)
    algebraic[0] = constants[23]/(1.00000+constants[21]/constants[17]+constants[24]/constants[16]+states[2]/constants[18])
    algebraic[1] = states[2]/(constants[12]*exp((constants[25]*constants[2]*0.00100000*states[0])/(3.00000*constants[1]*constants[0])))
    algebraic[2] = constants[1]*constants[0]*log(constants[19]/states[2])-constants[2]*0.00100000*states[0]
    algebraic[3] = constants[19]/(constants[11]*exp(((1.00000+constants[25])*constants[2]*0.00100000*states[0])/(3.00000*constants[1]*constants[0])))
    algebraic[4] = constants[1]*constants[0]*log(constants[21]/constants[20])+constants[2]*0.00100000*states[0]
    algebraic[5] = (constants[3]*(power(algebraic[1], 3.00000)))/((power(1.00000+algebraic[1], 3.00000)+power(1.00000+constants[26], 2.00000))-1.00000)
    algebraic[6] = 2.00000*algebraic[4]+3.00000*algebraic[2]
    algebraic[7] = (constants[7]*(power(constants[27], 2.00000)))/((power(1.00000+algebraic[3], 3.00000)+power(1.00000+constants[27], 2.00000))-1.00000)
    algebraic[8] = -29600.0-constants[1]*constants[0]*log(constants[22]/(0.00100000*states[1]*algebraic[0]))
    algebraic[9] = algebraic[8]+algebraic[6]
    algebraic[10] = constants[4]*states[1]
    algebraic[11] = (constants[6]*(power(algebraic[3], 3.00000)))/((power(1.00000+algebraic[3], 3.00000)+power(1.00000+constants[27], 2.00000))-1.00000)
    algebraic[12] = (constants[8]*algebraic[0]*constants[24])/(1.00000+constants[28])
    algebraic[13] = (constants[10]*(power(constants[26], 2.00000)))/((power(1.00000+algebraic[1], 3.00000)+power(1.00000+constants[26], 2.00000))-1.00000)
    algebraic[14] = algebraic[12]*algebraic[11]*algebraic[10]+constants[30]*algebraic[11]*algebraic[10]+constants[30]*constants[29]*algebraic[7]+constants[30]*algebraic[10]*algebraic[7]+algebraic[12]*algebraic[11]*algebraic[5]+constants[30]*algebraic[11]*algebraic[5]+constants[30]*algebraic[5]*algebraic[7]+algebraic[12]*algebraic[5]*constants[29]+constants[30]*algebraic[5]*constants[29]+algebraic[5]*constants[29]*algebraic[7]+algebraic[13]*algebraic[12]*algebraic[10]+algebraic[13]*algebraic[12]*constants[29]+algebraic[13]*algebraic[12]*algebraic[11]+algebraic[13]*algebraic[10]*algebraic[11]+algebraic[13]*constants[29]*algebraic[7]+algebraic[13]*algebraic[10]*algebraic[7]
    algebraic[15] = (algebraic[5]*constants[29]*algebraic[7]*constants[30]-algebraic[10]*algebraic[11]*algebraic[12]*algebraic[13])/algebraic[14]
    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)