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 = 2
sizeConstants = 13
from math import *
from numpy import *
def createLegends():
legend_states = [""] * sizeStates
legend_rates = [""] * sizeStates
legend_algebraic = [""] * sizeAlgebraic
legend_voi = ""
legend_constants = [""] * sizeConstants
legend_voi = "t in component main (second)"
legend_constants[0] = "V_m in component main (J_per_C)"
legend_constants[1] = "RTF in component main (J_per_C)"
legend_constants[10] = "epsilon in component main (dimensionless)"
legend_states[0] = "TA_i in component main (mol_per_m3)"
legend_algebraic[0] = "CO2_o in component main (mol_per_m3)"
legend_algebraic[5] = "CO2_i in component main (mol_per_m3)"
legend_algebraic[7] = "HCO3_i in component main (mol_per_m3)"
legend_algebraic[1] = "HCO3_o in component main (mol_per_m3)"
legend_states[1] = "H_i in component main (mol_per_m3)"
legend_constants[2] = "H_o in component main (mol_per_m3)"
legend_constants[3] = "H_Lim in component main (mol_per_m3)"
legend_algebraic[6] = "M_CO2 in component main (mol_per_m2_s)"
legend_algebraic[8] = "M_HCO3 in component main (mol_per_m2_s)"
legend_algebraic[3] = "M_H in component main (mol_per_m2_s)"
legend_algebraic[2] = "pH_i in component main (dimensionless)"
legend_constants[11] = "pH_o in component main (dimensionless)"
legend_constants[12] = "pH_Lim in component main (dimensionless)"
legend_constants[4] = "P_CO2 in component main (m_per_s)"
legend_constants[5] = "P_HCO3 in component main (m_per_s)"
legend_constants[6] = "k in component main (m_per_s)"
legend_constants[7] = "K_A in component main (mol_per_m3)"
legend_constants[8] = "rho in component main (per_m)"
legend_algebraic[4] = "alpha_i in component main (dimensionless)"
legend_constants[9] = "beta in component main (mol_per_m3)"
legend_rates[0] = "d/dt TA_i in component main (mol_per_m3)"
legend_rates[1] = "d/dt H_i in component main (mol_per_m3)"
return (legend_states, legend_algebraic, legend_voi, legend_constants)
def initConsts():
constants = [0.0] * sizeConstants; states = [0.0] * sizeStates;
constants[0] = -0.057
constants[1] = 0.0256796
states[0] = 0.0
states[1] = 3.981071705534970e-05
constants[2] = 1.995262314968879e-05
constants[3] = 3.981071705534970e-05
constants[4] = 6e-5
constants[5] = 5e-9
constants[6] = 0.0375
constants[7] = 1e-3
constants[8] = 8000
constants[9] = -26
constants[10] = exp(-constants[0]/constants[1])
constants[11] = -log(0.00100000*constants[2], 10)
constants[12] = -log(0.00100000*constants[3], 10)
return (states, constants)
def computeRates(voi, states, constants):
rates = [0.0] * sizeStates; algebraic = [0.0] * sizeAlgebraic
algebraic[0] = custom_piecewise([less(voi , 100.000), 0.00000 , less(voi , 2800.00), 1.18770 , True, 0.00000])
algebraic[4] = states[1]/(states[1]+constants[7])
algebraic[5] = algebraic[4]*states[0]
algebraic[6] = constants[4]*(algebraic[0]-algebraic[5])
algebraic[7] = (1.00000-algebraic[4])*states[0]
algebraic[1] = (constants[7]*algebraic[0])/constants[2]
algebraic[8] = (((constants[5]*constants[0])/constants[1])*(algebraic[1]-algebraic[7]*constants[10]))/(1.00000-constants[10])
rates[0] = constants[8]*(algebraic[6]+algebraic[8])
algebraic[2] = -log(0.00100000*states[1], 10)
algebraic[3] = custom_piecewise([less(algebraic[2] , constants[12]), constants[6]*(states[1]-constants[3]) , True, 0.00000])
rates[1] = ((-2.30300*states[1])/constants[9])*constants[8]*(((1.00000-algebraic[4])*algebraic[6]-algebraic[4]*algebraic[8])-algebraic[3])
return(rates)
def computeAlgebraic(constants, states, voi):
algebraic = array([[0.0] * len(voi)] * sizeAlgebraic)
states = array(states)
voi = array(voi)
algebraic[0] = custom_piecewise([less(voi , 100.000), 0.00000 , less(voi , 2800.00), 1.18770 , True, 0.00000])
algebraic[4] = states[1]/(states[1]+constants[7])
algebraic[5] = algebraic[4]*states[0]
algebraic[6] = constants[4]*(algebraic[0]-algebraic[5])
algebraic[7] = (1.00000-algebraic[4])*states[0]
algebraic[1] = (constants[7]*algebraic[0])/constants[2]
algebraic[8] = (((constants[5]*constants[0])/constants[1])*(algebraic[1]-algebraic[7]*constants[10]))/(1.00000-constants[10])
algebraic[2] = -log(0.00100000*states[1], 10)
algebraic[3] = custom_piecewise([less(algebraic[2] , constants[12]), constants[6]*(states[1]-constants[3]) , True, 0.00000])
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)
