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 = 8
sizeConstants = 37
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 Environmental (second)"
legend_constants[0] = "S in component Parameters (hertz)"
legend_constants[1] = "Zeta in component Parameters (dimensionless)"
legend_constants[2] = "R0 in component Parameters (mm)"
legend_constants[3] = "Cwall in component Parameters (mm_per_mmHg)"
legend_constants[4] = "Kne in component Parameters (mmHg_per_mm)"
legend_constants[5] = "K1 in component Parameters (mmHg_per_mm)"
legend_constants[6] = "K2 in component Parameters (mmHg_per_mm)"
legend_constants[7] = "K3 in component Parameters (mmHg_per_mm)"
legend_constants[8] = "Bwall in component Parameters (mmHg_s_per_sq_mm)"
legend_constants[9] = "B1 in component Parameters (mmHg_s_per_mm)"
legend_constants[10] = "B2 in component Parameters (mmHg_s_per_mm)"
legend_constants[11] = "B3 in component Parameters (mmHg_s_per_mm)"
legend_constants[12] = "Tsmax in component Parameters (AU)"
legend_constants[13] = "Tpmax in component Parameters (AU)"
legend_constants[14] = "Tsmin in component Parameters (AU)"
legend_constants[15] = "Tpmin in component Parameters (AU)"
legend_constants[16] = "Gcns in component Parameters (dimensionless)"
legend_constants[17] = "Gs in component Parameters (per_Hertz)"
legend_constants[18] = "Gp in component Parameters (per_Hertz)"
legend_constants[19] = "tau_nor in component Parameters (second)"
legend_constants[20] = "tau_ach in component Parameters (second)"
legend_constants[21] = "tau_HR_nor in component Parameters (second)"
legend_constants[22] = "tau_HR_ach in component Parameters (second)"
legend_constants[23] = "HRo in component Parameters (Beats_per_min)"
legend_constants[24] = "HRmax in component Parameters (Beats_per_min)"
legend_constants[25] = "HRmin in component Parameters (Beats_per_min)"
legend_constants[26] = "Beta in component Parameters (dimensionless)"
legend_constants[27] = "delta_th in component Parameters (dimensionless)"
legend_constants[28] = "q_nor in component Parameters (per_s)"
legend_constants[29] = "q_ach in component Parameters (per_s)"
legend_constants[30] = "K_nor in component Parameters (AU)"
legend_constants[31] = "K_ach in component Parameters (AU)"
legend_constants[32] = "Gamma in component Parameters (dimensionless)"
legend_algebraic[10] = "alpha_cns in component Nervous_System (hertz)"
legend_algebraic[8] = "n in component Nervous_System (hertz)"
legend_algebraic[6] = "Delta in component Coupling_Dynamics (dimensionless)"
legend_constants[33] = "alpha_s0 in component Nervous_System (hertz)"
legend_constants[34] = "alpha_p0 in component Nervous_System (hertz)"
legend_states[0] = "A in component Aortic_Wall (mm_sq)"
legend_algebraic[0] = "P in component Aortic_Wall (mmHg)"
legend_algebraic[1] = "R in component Aortic_Wall (mm)"
legend_states[1] = "Eps_1 in component Coupling_Dynamics (dimensionless)"
legend_states[2] = "Eps_2 in component Coupling_Dynamics (dimensionless)"
legend_states[3] = "Eps_3 in component Coupling_Dynamics (dimensionless)"
legend_algebraic[4] = "Eps_wall in component Coupling_Dynamics (dimensionless)"
legend_algebraic[12] = "Ts in component PNS_tones (AU)"
legend_algebraic[13] = "Tp in component PNS_tones (AU)"
legend_states[4] = "c_nor in component Norepinephrine (AU)"
legend_states[5] = "C_ach in component Acetylcholine (AU)"
legend_algebraic[2] = "delta_HR_ss in component Heart_Response_Nor (Beats_per_min)"
legend_constants[35] = "delta_HR_smax in component Heart_Response_Nor (Beats_per_min)"
legend_states[6] = "delta_HR_s in component Heart_Response_Nor (Beats_per_min)"
legend_algebraic[3] = "delta_HR_ps in component HR_ach (Beats_per_min)"
legend_constants[36] = "delta_HR_pmax in component HR_ach (Beats_per_min)"
legend_algebraic[5] = "delta_HR_pfast in component HR_ach (Beats_per_min)"
legend_states[7] = "delta_HR_pslow in component HR_ach (Beats_per_min)"
legend_algebraic[7] = "delta_HR_p in component HR_ach (Beats_per_min)"
legend_algebraic[14] = "HR in component HR_Combined (Beats_per_min)"
legend_algebraic[11] = "HR_p in component HR_Combined (Beats_per_min)"
legend_algebraic[9] = "HR_s in component HR_Combined (Beats_per_min)"
legend_algebraic[15] = "Period in component HR_Combined (Sec_per_Beat)"
legend_rates[0] = "d/dt A in component Aortic_Wall (mm_sq)"
legend_rates[1] = "d/dt Eps_1 in component Coupling_Dynamics (dimensionless)"
legend_rates[2] = "d/dt Eps_2 in component Coupling_Dynamics (dimensionless)"
legend_rates[3] = "d/dt Eps_3 in component Coupling_Dynamics (dimensionless)"
legend_rates[4] = "d/dt c_nor in component Norepinephrine (AU)"
legend_rates[5] = "d/dt C_ach in component Acetylcholine (AU)"
legend_rates[6] = "d/dt delta_HR_s in component Heart_Response_Nor (Beats_per_min)"
legend_rates[7] = "d/dt delta_HR_pslow in component HR_ach (Beats_per_min)"
return (legend_states, legend_algebraic, legend_voi, legend_constants)
def initConsts():
constants = [0.0] * sizeConstants; states = [0.0] * sizeStates;
constants[0] = 480
constants[1] = 1
constants[2] = 1.6
constants[3] = 0.006
constants[4] = 1
constants[5] = 1.5
constants[6] = 3.75
constants[7] = 1.05
constants[8] = 1
constants[9] = 1
constants[10] = 10
constants[11] = 206.973
constants[12] = 4.12
constants[13] = 4.994
constants[14] = 0.5
constants[15] = 1.6
constants[16] = 1
constants[17] = 0.178
constants[18] = 0.492
constants[19] = 9.1
constants[20] = 0.2
constants[21] = 2.1
constants[22] = 2.5
constants[23] = 282.648
constants[24] = 483.218
constants[25] = 226.238
constants[26] = 0.175
constants[27] = 0
constants[28] = 0.1099
constants[29] = 5
constants[30] = 1.12
constants[31] = 0.65
constants[32] = 0.75
constants[33] = 58.6
constants[34] = 76.019
states[0] = 15.20531
states[1] = 0.2042
states[2] = 0.183
states[3] = 0.161
states[4] = 1.441
states[5] = 1.0
states[6] = 0
states[7] = 0
constants[35] = constants[24]-constants[23]
constants[36] = constants[23]-constants[25]
return (states, constants)
def computeRates(voi, states, constants):
rates = [0.0] * sizeStates; algebraic = [0.0] * sizeAlgebraic
algebraic[0] = 100.000+10.0000*sin(5.00000*voi)
rates[0] = (-(power(states[0]/ pi, 1.0/2)-constants[2])/constants[3]+algebraic[0])/constants[8]
algebraic[2] = (constants[35]*(power(states[4], 2.00000)))/(power(constants[30], 2.00000)+power(states[4], 2.00000))
rates[6] = (-states[6]+algebraic[2])/constants[21]
algebraic[3] = (constants[36]*(power(states[5], 2.00000)))/(power(constants[31], 2.00000)+power(states[5], 2.00000))
rates[7] = (-states[7]+(1.00000-constants[32])*algebraic[3])/constants[22]
algebraic[1] = power(states[0]/ pi, 0.500000)
algebraic[4] = (algebraic[1]-constants[2])/constants[2]
rootfind_0(voi, constants, rates, states, algebraic)
algebraic[6] = algebraic[4]-states[1]
algebraic[8] = constants[0]*(algebraic[6]-constants[1]*constants[27])
algebraic[10] = constants[16]*algebraic[8]
algebraic[12] = constants[14]+(constants[12]-constants[14])/(exp(constants[17]*(algebraic[10]-constants[33]))+1.00000)
rates[4] = -(states[4]/constants[19])+constants[28]*algebraic[12]
algebraic[13] = constants[15]+(constants[13]-constants[15])/(exp(-constants[18]*(algebraic[10]-constants[34]))+1.00000)
rates[5] = -(states[5]/constants[20])+constants[29]*algebraic[13]
return(rates)
def computeAlgebraic(constants, states, voi):
algebraic = array([[0.0] * len(voi)] * sizeAlgebraic)
states = array(states)
voi = array(voi)
algebraic[0] = 100.000+10.0000*sin(5.00000*voi)
algebraic[2] = (constants[35]*(power(states[4], 2.00000)))/(power(constants[30], 2.00000)+power(states[4], 2.00000))
algebraic[3] = (constants[36]*(power(states[5], 2.00000)))/(power(constants[31], 2.00000)+power(states[5], 2.00000))
algebraic[1] = power(states[0]/ pi, 0.500000)
algebraic[4] = (algebraic[1]-constants[2])/constants[2]
algebraic[6] = algebraic[4]-states[1]
algebraic[8] = constants[0]*(algebraic[6]-constants[1]*constants[27])
algebraic[10] = constants[16]*algebraic[8]
algebraic[12] = constants[14]+(constants[12]-constants[14])/(exp(constants[17]*(algebraic[10]-constants[33]))+1.00000)
algebraic[13] = constants[15]+(constants[13]-constants[15])/(exp(-constants[18]*(algebraic[10]-constants[34]))+1.00000)
algebraic[5] = constants[32]*algebraic[3]
algebraic[7] = algebraic[5]+states[7]
algebraic[9] = constants[23]+states[6]
algebraic[11] = constants[23]-algebraic[7]
algebraic[14] = algebraic[11]+((algebraic[9]-constants[23])*(algebraic[11]-constants[26]*constants[25]))/(constants[23]-constants[26]*constants[25])
algebraic[15] = 60.0000/algebraic[14]
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(3)*0.1
if not iterable(voi):
soln = fsolve(residualSN_0, initialGuess0, args=(algebraic, voi, constants, rates, states), xtol=1E-6)
initialGuess0 = soln
rates[1] = soln[0]
rates[2] = soln[1]
rates[3] = soln[2]
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
rates[1][i] = soln[0]
rates[2][i] = soln[1]
rates[3][i] = soln[2]
def residualSN_0(algebraicCandidate, algebraic, voi, constants, rates, states):
resid = array([0.0] * 3)
rates[1] = algebraicCandidate[0]
rates[2] = algebraicCandidate[1]
rates[3] = algebraicCandidate[2]
resid[0] = (rates[1]-((constants[4]*(algebraic[4]-states[1])-constants[5]*(states[1]-states[2]))/constants[9]+rates[2]))
resid[1] = (rates[2]-((constants[5]*(states[1]-states[2])-constants[6]*(states[2]-states[3]))+constants[9]*rates[1]+constants[10]*rates[3])/(constants[9]+constants[10]))
resid[2] = (rates[3]-((constants[6]*(states[2]-states[3])-constants[7]*states[3])+constants[10]*rates[2])/(constants[10]+constants[11]))
return resid
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)
