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 = 9
sizeStates = 7
sizeConstants = 35
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_states[0] = "IP3 in component IP3_dynamics (micromolar)"
legend_algebraic[0] = "j_IP3 in component IP3_dynamics (micromolar_micrometre_per_second)"
legend_constants[0] = "J_IP3 in component IP3_dynamics (micromolar_micrometre_per_second)"
legend_constants[1] = "k_0 in component IP3_dynamics (first_order_rate_constant)"
legend_constants[2] = "k_degr in component IP3_dynamics (first_order_rate_constant)"
legend_constants[3] = "IP3_0 in component IP3_dynamics (micromolar)"
legend_constants[4] = "Ca_ER in component ER (micromolar)"
legend_states[1] = "Ca in component Calcium_dynamics (micromolar)"
legend_constants[5] = "alpha in component Calcium_dynamics (dimensionless)"
legend_algebraic[1] = "J_channel in component Channel_kinetics (flux)"
legend_algebraic[7] = "J_pump in component SERCA_pump_kinetics (flux)"
legend_algebraic[8] = "J_leak in component Leak (flux)"
legend_constants[27] = "R_buffering in component Calcium_buffering (flux)"
legend_constants[6] = "J_max in component Channel_kinetics (flux)"
legend_states[2] = "h in component Channel_kinetics (dimensionless)"
legend_constants[7] = "K_act in component Channel_kinetics (micromolar)"
legend_constants[8] = "K_IP3 in component Channel_kinetics (micromolar)"
legend_constants[9] = "K_inh in component Channel_kinetics (micromolar)"
legend_constants[10] = "k_on in component Channel_kinetics (second_order_rate_constant)"
legend_constants[11] = "V_max in component SERCA_pump_kinetics (flux)"
legend_constants[12] = "K_p in component SERCA_pump_kinetics (micromolar)"
legend_constants[13] = "L in component Leak (flux)"
legend_constants[14] = "R1 in component Calcium_buffering (flux)"
legend_constants[15] = "R2 in component Calcium_buffering (flux)"
legend_states[3] = "B1 in component Calcium_buffering (micromolar)"
legend_states[4] = "B2 in component Calcium_buffering (micromolar)"
legend_states[5] = "CaB1 in component Calcium_buffering (micromolar)"
legend_states[6] = "CaB2 in component Calcium_buffering (micromolar)"
legend_algebraic[2] = "k1_on in component Calcium_buffering (second_order_rate_constant)"
legend_algebraic[3] = "k1_off in component Calcium_buffering (first_order_rate_constant)"
legend_algebraic[4] = "k2_on in component Calcium_buffering (second_order_rate_constant)"
legend_algebraic[5] = "k2_off in component Calcium_buffering (first_order_rate_constant)"
legend_constants[16] = "K1 in component Calcium_buffering (micromolar)"
legend_constants[17] = "K2 in component Calcium_buffering (micromolar)"
legend_constants[18] = "soma_or_neurite in component Plasma_membrane_extrusion_mechanisms (dimensionless)"
legend_algebraic[6] = "j_Ca in component Plasma_membrane_extrusion_mechanisms (micromolar_micrometre_per_second)"
legend_constants[19] = "gamma_0 in component Plasma_membrane_extrusion_mechanisms (micrometre_per_second)"
legend_constants[30] = "gamma in component Plasma_membrane_extrusion_mechanisms (micrometre_per_second)"
legend_constants[28] = "gamma_s in component Plasma_membrane_extrusion_mechanisms (micrometre_per_second)"
legend_constants[29] = "gamma_n in component Plasma_membrane_extrusion_mechanisms (micrometre_per_second)"
legend_constants[20] = "delta in component Plasma_membrane_extrusion_mechanisms (dimensionless)"
legend_constants[21] = "sigma in component Plasma_membrane_extrusion_mechanisms (per_micrometre)"
legend_constants[22] = "w_n in component Plasma_membrane_extrusion_mechanisms (dimensionless)"
legend_constants[23] = "w_s in component Plasma_membrane_extrusion_mechanisms (dimensionless)"
legend_constants[24] = "sigma_soma_2D in component Plasma_membrane_extrusion_mechanisms (per_micrometre)"
legend_constants[25] = "sigma_neurite_2D in component Plasma_membrane_extrusion_mechanisms (per_micrometre)"
legend_constants[26] = "Ca_c in component Plasma_membrane_extrusion_mechanisms (micromolar)"
legend_rates[0] = "d/dt IP3 in component IP3_dynamics (micromolar)"
legend_rates[1] = "d/dt Ca in component Calcium_dynamics (micromolar)"
legend_rates[2] = "d/dt h in component Channel_kinetics (dimensionless)"
legend_rates[3] = "d/dt B1 in component Calcium_buffering (micromolar)"
legend_rates[5] = "d/dt CaB1 in component Calcium_buffering (micromolar)"
legend_rates[4] = "d/dt B2 in component Calcium_buffering (micromolar)"
legend_rates[6] = "d/dt CaB2 in component Calcium_buffering (micromolar)"
return (legend_states, legend_algebraic, legend_voi, legend_constants)
def initConsts():
constants = [0.0] * sizeConstants; states = [0.0] * sizeStates;
states[0] = 3.0
constants[0] = 20.86
constants[1] = 1.188
constants[2] = 0.14
constants[3] = 0.16
constants[4] = 400.0
states[1] = 0.05
constants[5] = 0.0
constants[6] = 3500.0
states[2] = 0.8
constants[7] = 0.3
constants[8] = 0.8
constants[9] = 0.2
constants[10] = 2.7
constants[11] = 3.75
constants[12] = 0.27
constants[13] = 0.1
constants[14] = 0.1
constants[15] = 0.1
states[3] = 450.0
states[4] = 75.0
states[5] = 0
states[6] = 0
constants[16] = 10.0
constants[17] = 0.24
constants[18] = -1
constants[19] = 8.0
constants[20] = 1.45
constants[21] = 0.263
constants[22] = 0.377
constants[23] = 0.623
constants[24] = 0.132
constants[25] = 0.479
constants[26] = 0.2
constants[27] = constants[14]+constants[15]
constants[28] = (constants[19]*constants[21])/(constants[20]*constants[25]*constants[22]+constants[24]*constants[23])
constants[31] = constants[14]
constants[32] = -constants[14]
constants[33] = constants[15]
constants[34] = -constants[15]
constants[29] = (constants[19]*constants[21]*constants[20])/(constants[20]*constants[25]*constants[22]+constants[24]*constants[23])
constants[30] = custom_piecewise([less_equal(constants[18] , 0.00000), constants[28] , True, constants[29]])
return (states, constants)
def computeRates(voi, states, constants):
rates = [0.0] * sizeStates; algebraic = [0.0] * sizeAlgebraic
rates[3] = constants[31]
rates[5] = constants[32]
rates[4] = constants[33]
rates[6] = constants[34]
rates[0] = -(constants[2]*(states[0]-constants[3]))
rates[2] = constants[10]*(constants[9]-states[2]*(states[1]+constants[9]))
algebraic[1] = constants[6]*(power((states[0]/(states[0]+constants[8]))*(states[1]/(states[1]+constants[7]))*states[2], 3.00000))*(1.00000-states[1]/constants[4])
algebraic[7] = constants[11]*((power(states[1], 2.00000))/(power(states[1], 2.00000)+power(constants[12], 2.00000)))
algebraic[8] = constants[13]*(1.00000-states[1]/constants[4])
rates[1] = constants[5]*(algebraic[1]+-algebraic[7]+algebraic[8])+constants[27]
return(rates)
def computeAlgebraic(constants, states, voi):
algebraic = array([[0.0] * len(voi)] * sizeAlgebraic)
states = array(states)
voi = array(voi)
algebraic[1] = constants[6]*(power((states[0]/(states[0]+constants[8]))*(states[1]/(states[1]+constants[7]))*states[2], 3.00000))*(1.00000-states[1]/constants[4])
algebraic[7] = constants[11]*((power(states[1], 2.00000))/(power(states[1], 2.00000)+power(constants[12], 2.00000)))
algebraic[8] = constants[13]*(1.00000-states[1]/constants[4])
algebraic[0] = constants[0]*exp(-constants[1]*voi)
rootfind_0(voi, constants, rates, states, algebraic)
rootfind_1(voi, constants, rates, states, algebraic)
algebraic[6] = custom_piecewise([greater(states[1] , constants[26]), constants[30]*(states[1]-constants[26]) , True, 0.00000])
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[2] = soln[0]
algebraic[3] = 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[2][i] = soln[0]
algebraic[3][i] = soln[1]
def residualSN_0(algebraicCandidate, algebraic, voi, constants, rates, states):
resid = array([0.0] * 2)
algebraic[2] = algebraicCandidate[0]
algebraic[3] = algebraicCandidate[1]
resid[0] = (constants[14]-(-(algebraic[2]*states[1]*states[3])+algebraic[3]*states[5]))
resid[1] = (constants[16]-algebraic[3]/algebraic[2])
return resid
initialGuess1 = None
def rootfind_1(voi, constants, rates, states, algebraic):
"""Calculate values of algebraic variables for DAE"""
from scipy.optimize import fsolve
global initialGuess1
if initialGuess1 is None: initialGuess1 = ones(2)*0.1
if not iterable(voi):
soln = fsolve(residualSN_1, initialGuess1, args=(algebraic, voi, constants, rates, states), xtol=1E-6)
initialGuess1 = soln
algebraic[4] = soln[0]
algebraic[5] = soln[1]
else:
for (i,t) in enumerate(voi):
soln = fsolve(residualSN_1, initialGuess1, args=(algebraic[:,i], voi[i], constants, rates[:i], states[:,i]), xtol=1E-6)
initialGuess1 = soln
algebraic[4][i] = soln[0]
algebraic[5][i] = soln[1]
def residualSN_1(algebraicCandidate, algebraic, voi, constants, rates, states):
resid = array([0.0] * 2)
algebraic[4] = algebraicCandidate[0]
algebraic[5] = algebraicCandidate[1]
resid[0] = (constants[15]-(-(algebraic[4]*states[1]*states[4])+algebraic[5]*states[6]))
resid[1] = (constants[17]-algebraic[5]/algebraic[4])
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)
